feat: draw icons with transparent background

This commit is contained in:
Brackyt 2026-01-25 20:35:31 +01:00
parent 12e34643e2
commit 8fa674f26d
3 changed files with 64 additions and 2 deletions

View File

@ -805,6 +805,67 @@ void GfxRenderer::drawBitmap1Bit(const Bitmap& bitmap, const int x, const int y,
free(rowBytes); free(rowBytes);
} }
void GfxRenderer::drawTransparentBitmap(const Bitmap& bitmap, const int x, const int y, const int w, const int h) const {
// Similar to drawBitmap1Bit but strictly skips 1s (white) in the source 1-bit data
// The Bitmap reader returns 2-bit packed data where 0-2=Black and 3=White for 1-bit sources
float scale = 1.0f;
bool isScaled = false;
if (w > 0 && bitmap.getWidth() > w) {
scale = static_cast<float>(w) / static_cast<float>(bitmap.getWidth());
isScaled = true;
}
if (h > 0 && bitmap.getHeight() > h) {
scale = std::min(scale, static_cast<float>(h) / static_cast<float>(bitmap.getHeight()));
isScaled = true;
}
const int outputRowSize = (bitmap.getWidth() + 3) / 4;
auto* outputRow = static_cast<uint8_t*>(malloc(outputRowSize));
auto* rowBytes = static_cast<uint8_t*>(malloc(bitmap.getRowBytes()));
if (!outputRow || !rowBytes) {
Serial.printf("[%lu] [GFX] !! Failed to allocate BMP row buffers\n", millis());
free(outputRow);
free(rowBytes);
return;
}
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
free(outputRow);
free(rowBytes);
return;
}
const int bmpYOffset = bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY;
int screenY = y + (isScaled ? static_cast<int>(std::floor(bmpYOffset * scale)) : bmpYOffset);
if (screenY >= getScreenHeight()) continue;
if (screenY < 0) continue;
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
int screenX = x + (isScaled ? static_cast<int>(std::floor(bmpX * scale)) : bmpX);
if (screenX >= getScreenWidth()) break;
if (screenX < 0) continue;
// Get 2-bit value. For 1-bit BMPs from BmpReader:
// Black in BMP -> 0 (Black)
// White in BMP -> 3 (White)
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
// Only draw if NOT white (val < 3)
if (val < 3) {
drawPixel(screenX, screenY, true); // True = Black
}
}
}
free(outputRow);
free(rowBytes);
}
void GfxRenderer::fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state) const { void GfxRenderer::fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state) const {
if (numPoints < 3) return; if (numPoints < 3) return;

View File

@ -76,7 +76,8 @@ class GfxRenderer {
void drawImage(const uint8_t bitmap[], int x, int y, int width, int height) const; void drawImage(const uint8_t bitmap[], int x, int y, int width, int height) const;
void drawBitmap(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight, float cropX = 0, void drawBitmap(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight, float cropX = 0,
float cropY = 0) const; float cropY = 0) const;
void drawBitmap1Bit(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight) const; void drawBitmap1Bit(const Bitmap& bitmap, const int x, const int y, const int maxWidth, const int maxHeight) const;
void drawTransparentBitmap(const Bitmap& bitmap, const int x, const int y, const int w, const int h) const;
void draw2BitImage(const uint8_t data[], int x, int y, int w, int h) const; void draw2BitImage(const uint8_t data[], int x, int y, int w, int h) const;
void fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state = true) const; void fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state = true) const;

View File

@ -45,7 +45,7 @@ void Icon::draw(const GfxRenderer& renderer, const ThemeContext& context) {
if (data && !data->empty()) { if (data && !data->empty()) {
Bitmap bmp(data->data(), data->size()); Bitmap bmp(data->data(), data->size());
if (bmp.parseHeaders() == BmpReaderError::Ok) { if (bmp.parseHeaders() == BmpReaderError::Ok) {
renderer.drawBitmap(bmp, absX, absY, w, h); renderer.drawTransparentBitmap(bmp, absX, absY, w, h);
markClean(); markClean();
return; return;
} }