From 941d2d5c13fec7abe2c32e68673a103d909d77ad Mon Sep 17 00:00:00 2001 From: JQ Date: Tue, 6 May 2025 20:47:14 -0700 Subject: [PATCH 1/2] fixing scaling of bitmaps for 7789 display --- src/helpers/ui/ST7789Display.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/helpers/ui/ST7789Display.cpp b/src/helpers/ui/ST7789Display.cpp index d1a4aed3..f0ae8557 100644 --- a/src/helpers/ui/ST7789Display.cpp +++ b/src/helpers/ui/ST7789Display.cpp @@ -111,7 +111,35 @@ void ST7789Display::drawRect(int x, int y, int w, int h) { } void ST7789Display::drawXbm(int x, int y, const uint8_t* bits, int w, int h) { - display.drawBitmap(x*SCALE_X + X_OFFSET, y*SCALE_Y + Y_OFFSET, w, h, bits); + // Calculate the base position in display coordinates + uint16_t startX = x * SCALE_X + X_OFFSET; + uint16_t startY = y * SCALE_Y + Y_OFFSET; + + // Width in bytes for bitmap processing + uint16_t widthInBytes = (w + 7) / 8; + + // Process the bitmap row by row + for (uint16_t by = 0; by < h; by++) { + // Scan across the row bit by bit + for (uint16_t bx = 0; bx < w; bx++) { + // Get the current bit + uint16_t byteOffset = (by * widthInBytes) + (bx / 8); + uint8_t bitMask = 0x80 >> (bx & 7); + bool bitSet = pgm_read_byte(bits + byteOffset) & bitMask; + + // If the bit is set, draw pixels using setPixel with scaling + if (bitSet) { + // Apply scaling - draw multiple pixels for each original bitmap pixel + for (uint16_t scaleY = 0; scaleY <= (uint16_t)SCALE_Y; scaleY++) { + for (uint16_t scaleX = 0; scaleX <= (uint16_t)SCALE_X; scaleX++) { + uint16_t pixelX = startX + (uint16_t)(bx * SCALE_X) + scaleX; + uint16_t pixelY = startY + (uint16_t)(by * SCALE_Y) + scaleY; + display.setPixel(pixelX, pixelY); + } + } + } + } + } } uint16_t ST7789Display::getTextWidth(const char* str) { From 94db70d511291d9ef0474fda2d3d3c50fbe697c9 Mon Sep 17 00:00:00 2001 From: JQ Date: Wed, 7 May 2025 18:14:56 -0700 Subject: [PATCH 2/2] new implementation --- src/helpers/ui/ST7789Display.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/helpers/ui/ST7789Display.cpp b/src/helpers/ui/ST7789Display.cpp index f0ae8557..0deebe3f 100644 --- a/src/helpers/ui/ST7789Display.cpp +++ b/src/helpers/ui/ST7789Display.cpp @@ -120,23 +120,27 @@ void ST7789Display::drawXbm(int x, int y, const uint8_t* bits, int w, int h) { // Process the bitmap row by row for (uint16_t by = 0; by < h; by++) { + // Calculate the target y-coordinates for this logical row + int y1 = startY + (int)(by * SCALE_Y); + int y2 = startY + (int)((by + 1) * SCALE_Y); + int block_h = y2 - y1; + // Scan across the row bit by bit for (uint16_t bx = 0; bx < w; bx++) { + // Calculate the target x-coordinates for this logical column + int x1 = startX + (int)(bx * SCALE_X); + int x2 = startX + (int)((bx + 1) * SCALE_X); + int block_w = x2 - x1; + // Get the current bit uint16_t byteOffset = (by * widthInBytes) + (bx / 8); uint8_t bitMask = 0x80 >> (bx & 7); bool bitSet = pgm_read_byte(bits + byteOffset) & bitMask; - // If the bit is set, draw pixels using setPixel with scaling + // If the bit is set, draw a block of pixels if (bitSet) { - // Apply scaling - draw multiple pixels for each original bitmap pixel - for (uint16_t scaleY = 0; scaleY <= (uint16_t)SCALE_Y; scaleY++) { - for (uint16_t scaleX = 0; scaleX <= (uint16_t)SCALE_X; scaleX++) { - uint16_t pixelX = startX + (uint16_t)(bx * SCALE_X) + scaleX; - uint16_t pixelY = startY + (uint16_t)(by * SCALE_Y) + scaleY; - display.setPixel(pixelX, pixelY); - } - } + // Draw the block as a filled rectangle + display.fillRect(x1, y1, block_w, block_h); } } }