|
6 | 6 | import "pkg:/source/utils/config.brs"
|
7 | 7 | import "pkg:/source/utils/librokudev.brs"
|
8 | 8 |
|
| 9 | +' construct a bmp by hand in hex to avoid drawing |
9 | 10 | function bitmapImageByteArray(numX as integer, numY as integer, pixels)
|
10 | 11 | bmp = "424D4C000000000000001A0000000C000000"
|
11 | 12 | bmp = bmp + rdINTtoHEX(numX) + "00" + rdINTtoHEX(numY) + "0001001800"
|
@@ -37,7 +38,6 @@ function decode83(str as string) as integer
|
37 | 38 | end function
|
38 | 39 |
|
39 | 40 | function isBlurhashValid(blurhash as string) as boolean
|
40 |
| - |
41 | 41 | if blurhash = invalid or len(blurhash) < 6
|
42 | 42 | print "The blurhash string must be at least 6 characters"
|
43 | 43 | return false
|
@@ -113,17 +113,25 @@ function linearTosRGB(value as float)
|
113 | 113 | end if
|
114 | 114 | end function
|
115 | 115 |
|
| 116 | +' this function takes a blurhash, a dimensions in terms of elements, |
| 117 | +' renders the blurhash using the fake-blurhash decoding algorithm |
| 118 | +' then returns a filesystem uri pointing at the file |
| 119 | +' the images in the fs are named with a hash of the blurhash string |
| 120 | +' so that items which already exist are not rendered twice |
116 | 121 | function renderFakeBlurhash(blurhash as string, width as integer, height as integer, punch = 1 as float)
|
117 |
| - bhfn = CreateObject("roByteArray") |
118 |
| - bhfn.FromAsciiString(blurhash) |
| 122 | + ' determine the file name |
| 123 | + ' create the hasher and the bytestring for the hasher |
| 124 | + blurhashByteArray = CreateObject("roByteArray") |
| 125 | + blurhashByteArray.FromAsciiString(blurhash) |
119 | 126 | digest = CreateObject("roEVPDigest")
|
120 | 127 | digest.Setup("md5")
|
121 |
| - digest.Update(bhfn) |
122 |
| - fn = digest.Final() |
| 128 | + digest.Update(blurhashByteArray) |
| 129 | + filename = digest.Final() |
123 | 130 | localFileSystem = CreateObject("roFileSystem")
|
124 |
| - if localFileSystem.Exists("tmp://" + fn + ".bmp") |
125 |
| - return "tmp://" + fn + ".bmp" |
| 131 | + if localFileSystem.Exists("tmp://" + filename + ".bmp") |
| 132 | + return "tmp://" + filename + ".bmp" |
126 | 133 | end if
|
| 134 | + ' doesn't exist in fs. render it. |
127 | 135 | if isBlurhashValid(blurhash) = false then return invalid
|
128 | 136 | sizeFlag = decode83(Mid(blurhash, 1, 1))
|
129 | 137 | numY = Fix(sizeFlag / 9) + 1
|
@@ -169,6 +177,6 @@ function renderFakeBlurhash(blurhash as string, width as integer, height as inte
|
169 | 177 | pixels.push(pixel)
|
170 | 178 | end for
|
171 | 179 | ba = bitmapImageByteArray(numX, numY, pixels)
|
172 |
| - ba.WriteFile("tmp://" + fn + ".bmp") |
173 |
| - return "tmp://" + fn + ".bmp" |
| 180 | + ba.WriteFile("tmp://" + filename + ".bmp") |
| 181 | + return "tmp://" + filename + ".bmp" |
174 | 182 | end function
|
0 commit comments