Skip to content

Commit

Permalink
doc what i can doc
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenrats committed Nov 10, 2023
1 parent eeb2048 commit 2a0eaff
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions source/utils/fakeBlurhash.brs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import "pkg:/source/utils/config.brs"
import "pkg:/source/utils/librokudev.brs"

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

function isBlurhashValid(blurhash as string) as boolean

if blurhash = invalid or len(blurhash) < 6
print "The blurhash string must be at least 6 characters"
return false
Expand Down Expand Up @@ -113,17 +113,25 @@ function linearTosRGB(value as float)
end if
end function

' this function takes a blurhash, a dimensions in terms of elements,
' renders the blurhash using the fake-blurhash decoding algorithm
' then returns a filesystem uri pointing at the file
' the images in the fs are named with a hash of the blurhash string
' so that items which already exist are not rendered twice
function renderFakeBlurhash(blurhash as string, width as integer, height as integer, punch = 1 as float)
bhfn = CreateObject("roByteArray")
bhfn.FromAsciiString(blurhash)
' determine the file name
' create the hasher and the bytestring for the hasher
blurhashByteArray = CreateObject("roByteArray")
blurhashByteArray.FromAsciiString(blurhash)
digest = CreateObject("roEVPDigest")
digest.Setup("md5")
digest.Update(bhfn)
fn = digest.Final()
digest.Update(blurhashByteArray)
filename = digest.Final()
localFileSystem = CreateObject("roFileSystem")
if localFileSystem.Exists("tmp://" + fn + ".bmp")
return "tmp://" + fn + ".bmp"
if localFileSystem.Exists("tmp://" + filename + ".bmp")
return "tmp://" + filename + ".bmp"
end if
' doesn't exist in fs. render it.
if isBlurhashValid(blurhash) = false then return invalid
sizeFlag = decode83(Mid(blurhash, 1, 1))
numY = Fix(sizeFlag / 9) + 1
Expand Down Expand Up @@ -169,6 +177,6 @@ function renderFakeBlurhash(blurhash as string, width as integer, height as inte
pixels.push(pixel)
end for
ba = bitmapImageByteArray(numX, numY, pixels)
ba.WriteFile("tmp://" + fn + ".bmp")
return "tmp://" + fn + ".bmp"
ba.WriteFile("tmp://" + filename + ".bmp")
return "tmp://" + filename + ".bmp"
end function

0 comments on commit 2a0eaff

Please sign in to comment.