diff --git a/bmp2h.py b/bmp2h.py index cd3a517..e93db03 100644 --- a/bmp2h.py +++ b/bmp2h.py @@ -13,24 +13,23 @@ def get_bytestring(fname): const uint8_t """ + bmp_name + """_data[] PROGMEM = {""", end="") with Image.open(fname) as im: - w = im.size[0] - h = im.size[1] + w, h = im.size im = im.convert('L') pix = im.load() - for i in range(w*h // 8): - x = (i * 8) % w - y = i // (w // 8) - b = 0 - if x == 0: - print("") - for bit in range(8): - if x + bit >= w: break - if y >= h: break - if pix[x + bit, y] < 127: - b += 1 << bit - print("\t", hex(b), end=", ") + row_bytes = (w + 7) // 8 + for y in range(h): + print("") + for bx in range(row_bytes): + x = bx * 8 + b = 0 + for bit in range(8): + if x + bit >= w: + break + if pix[x + bit, y] < 127: + b += 1 << bit + print("\t", hex(b), end=", ") print(""" diff --git a/esp-gfx/gfx.c b/esp-gfx/gfx.c index 3f3e1ff..174f78c 100644 --- a/esp-gfx/gfx.c +++ b/esp-gfx/gfx.c @@ -93,11 +93,17 @@ void gfx_fill_box(const gfx_canvas_t *canvas, const uint8_t pos_x, const uint8_t void gfx_draw_bitmap(const gfx_canvas_t *canvas, const uint8_t pos_x, const uint8_t pos_y, const gfx_bitmap_t bmp, const bool inverted) { void (*draw_cb)(void* ctx, uint8_t x, uint8_t y) = inverted ? canvas->clear_pixel_callback : canvas->draw_pixel_callback; - for (uint16_t i = 0; i < (uint16_t)(bmp.height * bmp.width / 8); i++) { - const uint8_t x = (i * 8) % bmp.width; - const uint8_t y = i / (bmp.width / 8); - for (uint8_t j = 0; j < 8; j++) - if (bmp.data[i] & (1 << j)) - draw_cb(canvas->ctx, pos_x + x + j, pos_y + y); + const uint8_t row_bytes = (bmp.width + 7) / 8; + + for (uint8_t y = 0; y < bmp.height; y++) { + for (uint8_t bx = 0; bx < row_bytes; bx++) { + const uint8_t byte = bmp.data[y * row_bytes + bx]; + const uint8_t x = bx * 8; + for (uint8_t j = 0; j < 8; j++) { + if (x + j >= bmp.width) break; + if (byte & (1 << j)) + draw_cb(canvas->ctx, pos_x + x + j, pos_y + y); + } + } } } \ No newline at end of file