1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-26 13:25:54 +00:00

🧑‍💻 Prevent mixed bitmap encoding

Followup to #26419
This commit is contained in:
Scott Lahteine 2023-11-14 01:23:37 -06:00
parent c751dcfcf9
commit 10e06e1970

View File

@ -69,15 +69,15 @@ def addCompressedData(input_file, output_file):
# - A value of 16 indicates a run of 16-270 calculated using the next two bytes. # - A value of 16 indicates a run of 16-270 calculated using the next two bytes.
# #
def bitwise_rle_encode(data): 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 get_bit(data, n): return 1 if (data[n // 8] & (0x80 >> (n & 7))) else 0
isext = False def try_encode(data, isext):
bitslen = len(data) * 8 bitslen = len(data) * 8
bitstate = get_bit(data, 0) bitstate = get_bit(data, 0)
rledata = [ bitstate ] rledata = [ bitstate ]
bigrun = 256 if isext else 272
medrun = False
i = 0 i = 0
runlen = -1 runlen = -1
@ -85,12 +85,14 @@ def addCompressedData(input_file, output_file):
if i < bitslen: b = get_bit(data, i) if i < bitslen: b = get_bit(data, i)
runlen += 1 runlen += 1
if bitstate != b or i == bitslen: if bitstate != b or i == bitslen:
if runlen >= 272: if runlen >= bigrun:
isext = True isext = True
if medrun: return [], isext
rem = runlen & 0xFF rem = runlen & 0xFF
rledata += [ 15, 15, rem // 16, rem % 16 ] rledata += [ 15, 15, rem // 16, rem % 16 ]
elif runlen >= 16: elif runlen >= 16:
rledata += [ 15, runlen // 16 - 1, runlen % 16 ] rledata += [ 15, runlen // 16 - 1, runlen % 16 ]
if runlen >= 256: medrun = True
else: else:
rledata += [ runlen - 1 ] rledata += [ runlen - 1 ]
bitstate ^= 1 bitstate ^= 1
@ -108,7 +110,16 @@ def addCompressedData(input_file, output_file):
encoded += [ v ] encoded += [ v ]
ri += 2 ri += 2
print("\nencoded", encoded) #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
encoded, isext = try_encode(data, isext)
if len(encoded) == 0:
encoded, isext = try_encode(data, True)
return encoded, isext return encoded, isext
def bitwise_rle_decode(isext, rledata, invert=0): def bitwise_rle_decode(isext, rledata, invert=0):