Skip to content

Commit

Permalink
⚡️ Extend bitmap compression
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and eoyilmaz committed Nov 21, 2023
1 parent 6348b09 commit f938486
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
17 changes: 11 additions & 6 deletions Marlin/src/lcd/dogm/marlinui_DOGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ bool MarlinUI::detected() { return true; }
#else
const u8g_pgm_uint8_t * const bmp = (u8g_pgm_uint8_t*)pgm_read_ptr(&custom_bootscreen_animation[frame]);
#endif
#elif ENABLED(COMPACT_CUSTOM_BOOTSCREEN)
#elif ANY(COMPACT_CUSTOM_BOOTSCREEN, COMPACT_CUSTOM_BOOTSCREEN_EXT)
#define BMPSIZE (CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH * CUSTOM_BOOTSCREEN_BMPHEIGHT)
uint8_t bmp[BMPSIZE];
uint8_t *bmp_rle = (uint8_t*)custom_start_bmp_rle;
#else
const u8g_pgm_uint8_t * const bmp = custom_start_bmp;
#endif

#if ENABLED(COMPACT_CUSTOM_BOOTSCREEN)
#if ANY(COMPACT_CUSTOM_BOOTSCREEN, COMPACT_CUSTOM_BOOTSCREEN_EXT)

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];
return (i & 1 ? b & 0xF : b >> 4);
};
Expand All @@ -149,8 +149,13 @@ bool MarlinUI::detected() { return true; }
while (outindex < BMPSIZE * 8) {
int16_t c = rle_nybble(inindex++);
if (c == 15) {
c = 16 * rle_nybble(inindex) + rle_nybble(inindex + 1) + 15; // From 16 to 270
inindex += 2;
const uint8_t d = rle_nybble(inindex++), e = rle_nybble(inindex++);
#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) {
const uint8_t bitind = outindex & 7,
Expand All @@ -162,7 +167,7 @@ bool MarlinUI::detected() { return true; }
bitstate ^= 0x80;
}

#endif // COMPACT_CUSTOM_BOOTSCREEN
#endif // COMPACT_CUSTOM_BOOTSCREEN || COMPACT_CUSTOM_BOOTSCREEN_EXT

u8g.TERN(COMPACT_CUSTOM_BOOTSCREEN, drawBitmap, drawBitmapP)
(left, top, CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH, CUSTOM_BOOTSCREEN_BMPHEIGHT, bmp);
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/lcd/lcdprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
* 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*/) {
const uint8_t prop = USE_WIDE_GLYPH ? 2 : 1;
const uint8_t *p = (uint8_t*)ptpl;
char *o = outstr;
int8_t n = maxlen;
Expand Down
38 changes: 22 additions & 16 deletions buildroot/share/scripts/rle_compress_bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def bitwise_rle_encode(data):

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

isext = False
bitslen = len(data) * 8
bitstate = get_bit(data, 0)
rledata = [ bitstate ]
Expand All @@ -84,18 +85,19 @@ def get_bit(data, n): return 1 if (data[n // 8] & (0x80 >> (n & 7))) else 0
if i < bitslen: b = get_bit(data, i)
runlen += 1
if bitstate != b or i == bitslen:
if i > 11 * 56 * 8: print(f'Bit change at index {i} with runlen={runlen}')
if runlen >= 16:
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 ]
if i > 11 * 56 * 8: print(f'Storing {[ 15, runlen // 16 - 1, runlen % 16 ]}')
else:
rledata += [ runlen - 1 ]
if i > 11 * 56 * 8: print(f'Storing {[ runlen ]}')
bitstate ^= 1
runlen = 0
i += 1

print("\nrledata", rledata)
#print("\nrledata", rledata)

encoded = []
ri = 0
Expand All @@ -107,9 +109,9 @@ def get_bit(data, n): return 1 if (data[n // 8] & (0x80 >> (n & 7))) else 0
ri += 2

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

def bitwise_rle_decode(rledata, invert=0):
def bitwise_rle_decode(isext, rledata, invert=0):
expanded = []
for n in rledata: expanded += [ n >> 4, n & 0xF ]

Expand All @@ -123,7 +125,12 @@ def bitwise_rle_decode(rledata, invert=0):
if i == 1: bitstate = c ; continue

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

for _ in range(c, -1, -1):
Expand All @@ -143,7 +150,7 @@ def bitwise_rle_decode(rledata, invert=0):

return decoded

def rle_emit(ofile, arrname, rledata, rawsize):
def rle_emit(ofile, arrname, rledata, rawsize, isext):

outstr = ''
rows = [ rledata[i:i+16] for i in range(0, len(rledata), 16) ]
Expand All @@ -153,21 +160,20 @@ def rle_emit(ofile, arrname, rledata, rawsize):

outstr = outstr[:-2]
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
rledata = bitwise_rle_encode(raw_data)
rle_emit(ofile, arrname, rledata, len(raw_data))
rledata, isext = bitwise_rle_encode(raw_data)
rle_emit(ofile, arrname, rledata, len(raw_data), isext)
ofile.close()

# Validate that code properly compressed (and decompressed) the data
checkdata = bitwise_rle_decode(rledata)
badindex = -1
checkdata = bitwise_rle_decode(isext, rledata)
for i in range(0, len(checkdata)):
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
if badindex >= 0: print(f'Data mismatch at byte {badindex}')

if len(sys.argv) <= 2:
print('Usage: rle_compress_bitmap.py INPUT_FILE OUTPUT_FILE')
Expand Down

0 comments on commit f938486

Please sign in to comment.