Skip to content

Commit 2a0eaff

Browse files
committed
doc what i can doc
1 parent eeb2048 commit 2a0eaff

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

source/utils/fakeBlurhash.brs

+17-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import "pkg:/source/utils/config.brs"
77
import "pkg:/source/utils/librokudev.brs"
88

9+
' construct a bmp by hand in hex to avoid drawing
910
function bitmapImageByteArray(numX as integer, numY as integer, pixels)
1011
bmp = "424D4C000000000000001A0000000C000000"
1112
bmp = bmp + rdINTtoHEX(numX) + "00" + rdINTtoHEX(numY) + "0001001800"
@@ -37,7 +38,6 @@ function decode83(str as string) as integer
3738
end function
3839

3940
function isBlurhashValid(blurhash as string) as boolean
40-
4141
if blurhash = invalid or len(blurhash) < 6
4242
print "The blurhash string must be at least 6 characters"
4343
return false
@@ -113,17 +113,25 @@ function linearTosRGB(value as float)
113113
end if
114114
end function
115115

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
116121
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)
119126
digest = CreateObject("roEVPDigest")
120127
digest.Setup("md5")
121-
digest.Update(bhfn)
122-
fn = digest.Final()
128+
digest.Update(blurhashByteArray)
129+
filename = digest.Final()
123130
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"
126133
end if
134+
' doesn't exist in fs. render it.
127135
if isBlurhashValid(blurhash) = false then return invalid
128136
sizeFlag = decode83(Mid(blurhash, 1, 1))
129137
numY = Fix(sizeFlag / 9) + 1
@@ -169,6 +177,6 @@ function renderFakeBlurhash(blurhash as string, width as integer, height as inte
169177
pixels.push(pixel)
170178
end for
171179
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"
174182
end function

0 commit comments

Comments
 (0)