️ Extend bitmap compression

Followup to #26419
This commit is contained in:
Scott Lahteine 2023-11-13 23:05:59 -06:00
parent a8313c3b7e
commit 178938d957
3 changed files with 33 additions and 23 deletions

View file

@ -127,7 +127,7 @@ bool MarlinUI::detected() { return true; }
#else #else
const u8g_pgm_uint8_t * const bmp = (u8g_pgm_uint8_t*)pgm_read_ptr(&custom_bootscreen_animation[frame]); const u8g_pgm_uint8_t * const bmp = (u8g_pgm_uint8_t*)pgm_read_ptr(&custom_bootscreen_animation[frame]);
#endif #endif
#elif ENABLED(COMPACT_CUSTOM_BOOTSCREEN) #elif ANY(COMPACT_CUSTOM_BOOTSCREEN, COMPACT_CUSTOM_BOOTSCREEN_EXT)
#define BMPSIZE (CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH * CUSTOM_BOOTSCREEN_BMPHEIGHT) #define BMPSIZE (CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH * CUSTOM_BOOTSCREEN_BMPHEIGHT)
uint8_t bmp[BMPSIZE]; uint8_t bmp[BMPSIZE];
uint8_t *bmp_rle = (uint8_t*)custom_start_bmp_rle; uint8_t *bmp_rle = (uint8_t*)custom_start_bmp_rle;
@ -135,11 +135,11 @@ bool MarlinUI::detected() { return true; }
const u8g_pgm_uint8_t * const bmp = custom_start_bmp; const u8g_pgm_uint8_t * const bmp = custom_start_bmp;
#endif #endif
#if ENABLED(COMPACT_CUSTOM_BOOTSCREEN) #if ANY(COMPACT_CUSTOM_BOOTSCREEN, COMPACT_CUSTOM_BOOTSCREEN_EXT)
uint8_t *dst = (uint8_t*)bmp; uint8_t *dst = (uint8_t*)bmp;
auto rle_nybble = [&](const uint16_t i) { auto rle_nybble = [&](const uint16_t i) -> uint8_t {
const uint8_t b = bmp_rle[i / 2]; const uint8_t b = bmp_rle[i / 2];
return (i & 1 ? b & 0xF : b >> 4); return (i & 1 ? b & 0xF : b >> 4);
}; };
@ -149,8 +149,13 @@ bool MarlinUI::detected() { return true; }
while (outindex < BMPSIZE * 8) { while (outindex < BMPSIZE * 8) {
int16_t c = rle_nybble(inindex++); int16_t c = rle_nybble(inindex++);
if (c == 15) { if (c == 15) {
c = 16 * rle_nybble(inindex) + rle_nybble(inindex + 1) + 15; // From 16 to 270 const uint8_t d = rle_nybble(inindex++), e = rle_nybble(inindex++);
inindex += 2; #if ENABLED(COMPACT_CUSTOM_BOOTSCREEN_EXT)
if (d == 15)
c = 256 + 16 * e + rle_nybble(inindex++) - 1;
else
#endif
c = 16 * d + e + 15; // From 16 to 270
} }
while (c-- >= 0) { while (c-- >= 0) {
const uint8_t bitind = outindex & 7, const uint8_t bitind = outindex & 7,
@ -162,7 +167,7 @@ bool MarlinUI::detected() { return true; }
bitstate ^= 0x80; bitstate ^= 0x80;
} }
#endif // COMPACT_CUSTOM_BOOTSCREEN #endif // COMPACT_CUSTOM_BOOTSCREEN || COMPACT_CUSTOM_BOOTSCREEN_EXT
u8g.TERN(COMPACT_CUSTOM_BOOTSCREEN, drawBitmap, drawBitmapP) u8g.TERN(COMPACT_CUSTOM_BOOTSCREEN, drawBitmap, drawBitmapP)
(left, top, CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH, CUSTOM_BOOTSCREEN_BMPHEIGHT, bmp); (left, top, CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH, CUSTOM_BOOTSCREEN_BMPHEIGHT, bmp);

View file

@ -45,7 +45,6 @@
* Return the number of characters emitted * Return the number of characters emitted
*/ */
lcd_uint_t expand_u8str_P(char * const outstr, PGM_P const ptpl, const int8_t ind, const char *cstr/*=nullptr*/, FSTR_P const fstr/*=nullptr*/, const lcd_uint_t maxlen/*=LCD_WIDTH*/) { lcd_uint_t expand_u8str_P(char * const outstr, PGM_P const ptpl, const int8_t ind, const char *cstr/*=nullptr*/, FSTR_P const fstr/*=nullptr*/, const lcd_uint_t maxlen/*=LCD_WIDTH*/) {
const uint8_t prop = USE_WIDE_GLYPH ? 2 : 1;
const uint8_t *p = (uint8_t*)ptpl; const uint8_t *p = (uint8_t*)ptpl;
char *o = outstr; char *o = outstr;
int8_t n = maxlen; int8_t n = maxlen;

View file

@ -74,6 +74,7 @@ def addCompressedData(input_file, output_file):
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
bitslen = len(data) * 8 bitslen = len(data) * 8
bitstate = get_bit(data, 0) bitstate = get_bit(data, 0)
rledata = [ bitstate ] rledata = [ bitstate ]
@ -84,18 +85,19 @@ 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 i > 11 * 56 * 8: print(f'Bit change at index {i} with runlen={runlen}') if runlen >= 272:
if runlen >= 16: isext = True
rem = runlen & 0xFF
rledata += [ 15, 15, rem // 16, rem % 16 ]
elif runlen >= 16:
rledata += [ 15, runlen // 16 - 1, runlen % 16 ] rledata += [ 15, runlen // 16 - 1, runlen % 16 ]
if i > 11 * 56 * 8: print(f'Storing {[ 15, runlen // 16 - 1, runlen % 16 ]}')
else: else:
rledata += [ runlen - 1 ] rledata += [ runlen - 1 ]
if i > 11 * 56 * 8: print(f'Storing {[ runlen ]}')
bitstate ^= 1 bitstate ^= 1
runlen = 0 runlen = 0
i += 1 i += 1
print("\nrledata", rledata) #print("\nrledata", rledata)
encoded = [] encoded = []
ri = 0 ri = 0
@ -107,9 +109,9 @@ def addCompressedData(input_file, output_file):
ri += 2 ri += 2
print("\nencoded", encoded) print("\nencoded", encoded)
return encoded return encoded, isext
def bitwise_rle_decode(rledata, invert=0): def bitwise_rle_decode(isext, rledata, invert=0):
expanded = [] expanded = []
for n in rledata: expanded += [ n >> 4, n & 0xF ] for n in rledata: expanded += [ n >> 4, n & 0xF ]
@ -123,7 +125,12 @@ def addCompressedData(input_file, output_file):
if i == 1: bitstate = c ; continue if i == 1: bitstate = c ; continue
if c == 15: if c == 15:
c = 16 * expanded[i] + expanded[i + 1] + 15 d = expanded[i] ; e = expanded[i + 1]
if isext and d == 15:
c = 256 + 16 * e + expanded[i + 2] - 1
i += 1
else:
c = 16 * d + e + 15
i += 2 i += 2
for _ in range(c, -1, -1): for _ in range(c, -1, -1):
@ -143,7 +150,7 @@ def addCompressedData(input_file, output_file):
return decoded return decoded
def rle_emit(ofile, arrname, rledata, rawsize): def rle_emit(ofile, arrname, rledata, rawsize, isext):
outstr = '' outstr = ''
rows = [ rledata[i:i+16] for i in range(0, len(rledata), 16) ] rows = [ rledata[i:i+16] for i in range(0, len(rledata), 16) ]
@ -153,21 +160,20 @@ def addCompressedData(input_file, output_file):
outstr = outstr[:-2] outstr = outstr[:-2]
size = len(rledata) size = len(rledata)
ofile.write("\n// Saves %i bytes\n%s %s_rle[%d] PROGMEM = {\n%s\n};\n" % (rawsize - size, datatype, arrname, size, outstr)) defname = 'COMPACT_CUSTOM_BOOTSCREEN_EXT' if isext else 'COMPACT_CUSTOM_BOOTSCREEN'
ofile.write(f"\n// Saves {rawsize - size} bytes\n#define {defname}\n{datatype} {arrname}_rle[{size}] PROGMEM = {{\n{outstr}\n}};\n")
# Encode the data, write it out, close the file # Encode the data, write it out, close the file
rledata = bitwise_rle_encode(raw_data) rledata, isext = bitwise_rle_encode(raw_data)
rle_emit(ofile, arrname, rledata, len(raw_data)) rle_emit(ofile, arrname, rledata, len(raw_data), isext)
ofile.close() ofile.close()
# Validate that code properly compressed (and decompressed) the data # Validate that code properly compressed (and decompressed) the data
checkdata = bitwise_rle_decode(rledata) checkdata = bitwise_rle_decode(isext, rledata)
badindex = -1
for i in range(0, len(checkdata)): for i in range(0, len(checkdata)):
if raw_data[i] != checkdata[i]: if raw_data[i] != checkdata[i]:
badindex = i print(f'Data mismatch at byte offset {i} (should be {raw_data[i]} but got {checkdata[i]})')
break break
if badindex >= 0: print(f'Data mismatch at byte {badindex}')
if len(sys.argv) <= 2: if len(sys.argv) <= 2:
print('Usage: rle_compress_bitmap.py INPUT_FILE OUTPUT_FILE') print('Usage: rle_compress_bitmap.py INPUT_FILE OUTPUT_FILE')