Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Prevent mixed bitmap encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and eoyilmaz committed Nov 21, 2023
1 parent ba4e9d0 commit 97dadc1
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions buildroot/share/scripts/rle_compress_bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,46 +69,57 @@ def addCompressedData(input_file, output_file):
# - A value of 16 indicates a run of 16-270 calculated using the next two bytes.
#
def bitwise_rle_encode(data):
warn = "This may take a while" if len(data) > 300000 else ""
print("Compressing image data...", warn)

def get_bit(data, n): return 1 if (data[n // 8] & (0x80 >> (n & 7))) else 0

def try_encode(data, isext):
bitslen = len(data) * 8
bitstate = get_bit(data, 0)
rledata = [ bitstate ]
bigrun = 256 if isext else 272
medrun = False

i = 0
runlen = -1
while i <= bitslen:
if i < bitslen: b = get_bit(data, i)
runlen += 1
if bitstate != b or i == bitslen:
if runlen >= bigrun:
isext = True
if medrun: return [], isext
rem = runlen & 0xFF
rledata += [ 15, 15, rem // 16, rem % 16 ]
elif runlen >= 16:
rledata += [ 15, runlen // 16 - 1, runlen % 16 ]
if runlen >= 256: medrun = True
else:
rledata += [ runlen - 1 ]
bitstate ^= 1
runlen = 0
i += 1

#print("\nrledata", rledata)

encoded = []
ri = 0
rlen = len(rledata)
while ri < rlen:
v = rledata[ri] << 4
if (ri < rlen - 1): v |= rledata[ri + 1]
encoded += [ v ]
ri += 2

#print("\nencoded", encoded)
return encoded, isext

# Try to encode with the original isext flag
warn = "This may take a while" if len(data) > 300000 else ""
print("Compressing image data...", warn)
isext = False
bitslen = len(data) * 8
bitstate = get_bit(data, 0)
rledata = [ bitstate ]

i = 0
runlen = -1
while i <= bitslen:
if i < bitslen: b = get_bit(data, i)
runlen += 1
if bitstate != b or i == bitslen:
if runlen >= 272:
isext = True
rem = runlen & 0xFF
rledata += [ 15, 15, rem // 16, rem % 16 ]
elif runlen >= 16:
rledata += [ 15, runlen // 16 - 1, runlen % 16 ]
else:
rledata += [ runlen - 1 ]
bitstate ^= 1
runlen = 0
i += 1

#print("\nrledata", rledata)

encoded = []
ri = 0
rlen = len(rledata)
while ri < rlen:
v = rledata[ri] << 4
if (ri < rlen - 1): v |= rledata[ri + 1]
encoded += [ v ]
ri += 2

print("\nencoded", encoded)
encoded, isext = try_encode(data, isext)
if len(encoded) == 0:
encoded, isext = try_encode(data, True)
return encoded, isext

def bitwise_rle_decode(isext, rledata, invert=0):
Expand Down

0 comments on commit 97dadc1

Please sign in to comment.