From e3c2b04cab9c91301eb18933ecde668fdc68a789 Mon Sep 17 00:00:00 2001 From: Brackyt <60280126+Brackyt@users.noreply.github.com> Date: Sun, 25 Jan 2026 21:10:10 +0100 Subject: [PATCH] feat: border radius on bitmap --- lib/GfxRenderer/GfxRenderer.cpp | 108 ++++++++++++++++++++++++ lib/GfxRenderer/GfxRenderer.h | 1 + lib/ThemeEngine/include/BasicElements.h | 8 ++ lib/ThemeEngine/src/BasicElements.cpp | 12 ++- lib/ThemeEngine/src/ThemeManager.cpp | 2 + 5 files changed, 129 insertions(+), 2 deletions(-) diff --git a/lib/GfxRenderer/GfxRenderer.cpp b/lib/GfxRenderer/GfxRenderer.cpp index 62f790ea..60970c82 100644 --- a/lib/GfxRenderer/GfxRenderer.cpp +++ b/lib/GfxRenderer/GfxRenderer.cpp @@ -882,6 +882,114 @@ void GfxRenderer::drawTransparentBitmap(const Bitmap& bitmap, const int x, const free(rowBytes); } +void GfxRenderer::drawRoundedBitmap(const Bitmap& bitmap, const int x, const int y, const int w, const int h, + const int radius) const { + if (radius <= 0) { + drawBitmap(bitmap, x, y, w, h); + return; + } + + float scale = 1.0f; + bool isScaled = false; + if (w > 0 && bitmap.getWidth() > w) { + scale = static_cast(w) / static_cast(bitmap.getWidth()); + isScaled = true; + } + if (h > 0 && bitmap.getHeight() > h) { + scale = std::min(scale, static_cast(h) / static_cast(bitmap.getHeight())); + isScaled = true; + } + + // Pre-calculate squared radius for containment checks + const int r2 = radius * radius; + + // Lambda to check if a pixel is inside the rounded rect + // We use relative coordinates (px, py) from the top-left of the destination rect + auto isVisible = [&](int px, int py) -> bool { + // Top-left + if (px < radius && py < radius) { + int dx = radius - px; + int dy = radius - py; + return (dx * dx + dy * dy) <= r2; + } + // Top-right + if (px >= w - radius && py < radius) { + int dx = px - (w - 1 - radius); + int dy = radius - py; + return (dx * dx + dy * dy) <= r2; + } + // Bottom-left + if (px < radius && py >= h - radius) { + int dx = radius - px; + int dy = py - (h - 1 - radius); + return (dx * dx + dy * dy) <= r2; + } + // Bottom-right + if (px >= w - radius && py >= h - radius) { + int dx = px - (w - 1 - radius); + int dy = py - (h - 1 - radius); + return (dx * dx + dy * dy) <= r2; + } + return true; // Safe center area + }; + + const int outputRowSize = (bitmap.getWidth() + 3) / 4; + auto* outputRow = static_cast(malloc(outputRowSize)); + auto* rowBytes = static_cast(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(std::floor(bmpYOffset * scale)) : bmpYOffset); + + if (screenY >= getScreenHeight()) continue; + if (screenY < 0) continue; + + // Relative Y for rounded check + int relY = screenY - y; + if (relY < 0 || relY >= h) continue; + + for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) { + int screenX = x + (isScaled ? static_cast(std::floor(bmpX * scale)) : bmpX); + + if (screenX >= getScreenWidth()) break; + if (screenX < 0) continue; + + // Relative X for rounded check + int relX = screenX - x; + if (relX < 0 || relX >= w) continue; + + // Check mask + if (!isVisible(relX, relY)) continue; + + const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3; + + if (renderMode == BW) { + drawPixel(screenX, screenY, val < 2); + } else if (renderMode == GRAYSCALE_MSB && (val == 1 || val == 2)) { + drawPixel(screenX, screenY, false); + } else if (renderMode == GRAYSCALE_LSB && val == 1) { + drawPixel(screenX, screenY, false); + } + } + } + + free(outputRow); + free(rowBytes); +} + void GfxRenderer::fillPolygon(const int *xPoints, const int *yPoints, int numPoints, bool state) const { if (numPoints < 3) diff --git a/lib/GfxRenderer/GfxRenderer.h b/lib/GfxRenderer/GfxRenderer.h index 20a6c8e0..77572377 100644 --- a/lib/GfxRenderer/GfxRenderer.h +++ b/lib/GfxRenderer/GfxRenderer.h @@ -81,6 +81,7 @@ public: void drawBitmap1Bit(const Bitmap &bitmap, int x, int y, int maxWidth, int maxHeight) const; void drawTransparentBitmap(const Bitmap& bitmap, int x, int y, int w, int h) const; + void drawRoundedBitmap(const Bitmap& bitmap, int x, int y, int w, int h, int radius) 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; diff --git a/lib/ThemeEngine/include/BasicElements.h b/lib/ThemeEngine/include/BasicElements.h index f3c31acc..e167bed8 100644 --- a/lib/ThemeEngine/include/BasicElements.h +++ b/lib/ThemeEngine/include/BasicElements.h @@ -364,6 +364,7 @@ class BitmapElement : public UIElement { Expression srcExpr; bool scaleToFit = true; bool preserveAspect = true; + int borderRadius = 0; public: BitmapElement(const std::string& id) : UIElement(id) { @@ -386,6 +387,13 @@ class BitmapElement : public UIElement { invalidateCache(); } + void setBorderRadius(int r) { + borderRadius = r; + // Radius doesn't affect cache key unless we baked it in (we don't currently), + // but we should redraw. + markDirty(); + } + void draw(const GfxRenderer& renderer, const ThemeContext& context) override; }; diff --git a/lib/ThemeEngine/src/BasicElements.cpp b/lib/ThemeEngine/src/BasicElements.cpp index ce658d46..e488875c 100644 --- a/lib/ThemeEngine/src/BasicElements.cpp +++ b/lib/ThemeEngine/src/BasicElements.cpp @@ -57,7 +57,11 @@ void BitmapElement::draw(const GfxRenderer& renderer, const ThemeContext& contex if (bmp.getWidth() < absW) drawX += (absW - bmp.getWidth()) / 2; if (bmp.getHeight() < absH) drawY += (absH - bmp.getHeight()) / 2; - renderer.drawBitmap(bmp, drawX, drawY, absW, absH); + if (borderRadius > 0) { + renderer.drawRoundedBitmap(bmp, drawX, drawY, absW, absH, borderRadius); + } else { + renderer.drawBitmap(bmp, drawX, drawY, absW, absH); + } drawSuccess = true; } file.close(); @@ -75,7 +79,11 @@ void BitmapElement::draw(const GfxRenderer& renderer, const ThemeContext& contex if (bmp.getWidth() < absW) drawX += (absW - bmp.getWidth()) / 2; if (bmp.getHeight() < absH) drawY += (absH - bmp.getHeight()) / 2; - renderer.drawBitmap(bmp, drawX, drawY, absW, absH); + if (borderRadius > 0) { + renderer.drawRoundedBitmap(bmp, drawX, drawY, absW, absH, borderRadius); + } else { + renderer.drawBitmap(bmp, drawX, drawY, absW, absH); + } drawSuccess = true; } } diff --git a/lib/ThemeEngine/src/ThemeManager.cpp b/lib/ThemeEngine/src/ThemeManager.cpp index a5f5d2c9..d10494e4 100644 --- a/lib/ThemeEngine/src/ThemeManager.cpp +++ b/lib/ThemeEngine/src/ThemeManager.cpp @@ -125,6 +125,8 @@ void ThemeManager::applyProperties(UIElement* elem, const std::map(elem)->setBorderRadius(std::stoi(val)); + } else if (elemType == UIElement::ElementType::Bitmap) { + static_cast(elem)->setBorderRadius(std::stoi(val)); } } else if (key == "Spacing") { if (elemType == UIElement::ElementType::HStack) {