mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 22:57:50 +03:00
1696 lines
54 KiB
C++
1696 lines
54 KiB
C++
#include "GfxRenderer.h"
|
|
|
|
#include <Utf8.h>
|
|
#include <algorithm>
|
|
|
|
void GfxRenderer::insertFont(const int fontId, EpdFontFamily font) {
|
|
fontMap.insert({fontId, font});
|
|
}
|
|
|
|
void GfxRenderer::rotateCoordinates(const int x, const int y, int *rotatedX,
|
|
int *rotatedY) const {
|
|
switch (orientation) {
|
|
case Portrait: {
|
|
// Logical portrait (480x800) → panel (800x480)
|
|
// Rotation: 90 degrees clockwise
|
|
*rotatedX = y;
|
|
*rotatedY = HalDisplay::DISPLAY_HEIGHT - 1 - x;
|
|
break;
|
|
}
|
|
case LandscapeClockwise: {
|
|
// Logical landscape (800x480) rotated 180 degrees (swap top/bottom and left/right)
|
|
*rotatedX = HalDisplay::DISPLAY_WIDTH - 1 - x;
|
|
*rotatedY = HalDisplay::DISPLAY_HEIGHT - 1 - y;
|
|
break;
|
|
}
|
|
case PortraitInverted: {
|
|
// Logical portrait (480x800) → panel (800x480)
|
|
// Rotation: 90 degrees counter-clockwise
|
|
*rotatedX = HalDisplay::DISPLAY_WIDTH - 1 - y;
|
|
*rotatedY = x;
|
|
break;
|
|
}
|
|
case LandscapeCounterClockwise: {
|
|
// Logical landscape (800x480) aligned with panel orientation
|
|
*rotatedX = x;
|
|
*rotatedY = y;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawPixel(const int x, const int y, const bool state) const {
|
|
uint8_t* frameBuffer = display.getFrameBuffer();
|
|
|
|
// Early return if no framebuffer is set
|
|
if (!frameBuffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer\n", millis());
|
|
return;
|
|
}
|
|
|
|
int rotatedX = 0;
|
|
int rotatedY = 0;
|
|
rotateCoordinates(x, y, &rotatedX, &rotatedY);
|
|
|
|
// Bounds checking against physical panel dimensions
|
|
if (rotatedX < 0 || rotatedX >= HalDisplay::DISPLAY_WIDTH || rotatedY < 0 || rotatedY >= HalDisplay::DISPLAY_HEIGHT) {
|
|
Serial.printf("[%lu] [GFX] !! Outside range (%d, %d) -> (%d, %d)\n", millis(), x, y, rotatedX, rotatedY);
|
|
return;
|
|
}
|
|
|
|
// Calculate byte position and bit position
|
|
const uint16_t byteIndex = rotatedY * HalDisplay::DISPLAY_WIDTH_BYTES + (rotatedX / 8);
|
|
const uint8_t bitPosition = 7 - (rotatedX % 8); // MSB first
|
|
|
|
if (state) {
|
|
frameBuffer[byteIndex] &= ~(1 << bitPosition); // Clear bit
|
|
} else {
|
|
frameBuffer[byteIndex] |= 1 << bitPosition; // Set bit
|
|
}
|
|
}
|
|
|
|
bool GfxRenderer::readPixel(const int x, const int y) const {
|
|
uint8_t *frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
return false;
|
|
}
|
|
|
|
int rotatedX = 0;
|
|
int rotatedY = 0;
|
|
rotateCoordinates(x, y, &rotatedX, &rotatedY);
|
|
|
|
// Bounds checking against physical panel dimensions
|
|
if (rotatedX < 0 || rotatedX >= HalDisplay::DISPLAY_WIDTH ||
|
|
rotatedY < 0 || rotatedY >= HalDisplay::DISPLAY_HEIGHT) {
|
|
return false;
|
|
}
|
|
|
|
const uint16_t byteIndex =
|
|
rotatedY * HalDisplay::DISPLAY_WIDTH_BYTES + (rotatedX / 8);
|
|
const uint8_t bitPosition = 7 - (rotatedX % 8);
|
|
|
|
// Bit cleared = black, bit set = white
|
|
return !(frameBuffer[byteIndex] & (1 << bitPosition));
|
|
}
|
|
|
|
int GfxRenderer::getTextWidth(const int fontId, const char *text,
|
|
const EpdFontFamily::Style style) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
int w = 0, h = 0;
|
|
fontMap.at(fontId).getTextDimensions(text, &w, &h, style);
|
|
return w;
|
|
}
|
|
|
|
void GfxRenderer::drawCenteredText(const int fontId, const int y,
|
|
const char *text, const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
const int x = (getScreenWidth() - getTextWidth(fontId, text, style)) / 2;
|
|
drawText(fontId, x, y, text, black, style);
|
|
}
|
|
|
|
void GfxRenderer::drawText(const int fontId, const int x, const int y,
|
|
const char *text, const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
const int yPos = y + getFontAscenderSize(fontId);
|
|
int xpos = x;
|
|
|
|
// cannot draw a NULL / empty string
|
|
if (text == nullptr || *text == '\0') {
|
|
return;
|
|
}
|
|
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return;
|
|
}
|
|
const auto font = fontMap.at(fontId);
|
|
|
|
// no printable characters
|
|
if (!font.hasPrintableChars(text, style)) {
|
|
return;
|
|
}
|
|
|
|
uint32_t cp;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t **>(&text)))) {
|
|
renderChar(font, cp, &xpos, &yPos, black, style);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawLine(int x1, int y1, int x2, int y2,
|
|
const bool state) const {
|
|
// Bresenham's line algorithm
|
|
int dx = abs(x2 - x1);
|
|
int dy = abs(y2 - y1);
|
|
int sx = (x1 < x2) ? 1 : -1;
|
|
int sy = (y1 < y2) ? 1 : -1;
|
|
int err = dx - dy;
|
|
|
|
while (true) {
|
|
drawPixel(x1, y1, state);
|
|
|
|
if (x1 == x2 && y1 == y2) break;
|
|
|
|
int e2 = 2 * err;
|
|
if (e2 > -dy) {
|
|
err -= dy;
|
|
x1 += sx;
|
|
}
|
|
if (e2 < dx) {
|
|
err += dx;
|
|
y1 += sy;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawRect(const int x, const int y, const int width,
|
|
const int height, const bool state) const {
|
|
drawLine(x, y, x + width - 1, y, state);
|
|
drawLine(x + width - 1, y, x + width - 1, y + height - 1, state);
|
|
drawLine(x + width - 1, y + height - 1, x, y + height - 1, state);
|
|
drawLine(x, y, x, y + height - 1, state);
|
|
}
|
|
|
|
void GfxRenderer::fillRect(const int x, const int y, const int width,
|
|
const int height, const bool state) const {
|
|
uint8_t *frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
return;
|
|
}
|
|
|
|
const int screenWidth = getScreenWidth();
|
|
const int screenHeight = getScreenHeight();
|
|
|
|
// Clip to screen bounds
|
|
const int x1 = std::max(0, x);
|
|
const int y1 = std::max(0, y);
|
|
const int x2 = std::min(screenWidth - 1, x + width - 1);
|
|
const int y2 = std::min(screenHeight - 1, y + height - 1);
|
|
|
|
if (x1 > x2 || y1 > y2)
|
|
return;
|
|
|
|
// Optimized path for Portrait mode (most common)
|
|
if (orientation == Portrait) {
|
|
for (int sy = y1; sy <= y2; sy++) {
|
|
// In Portrait: logical (x, y) -> physical (y, DISPLAY_HEIGHT - 1 - x)
|
|
const int physX = sy;
|
|
const uint8_t physXByte = physX / 8;
|
|
const uint8_t physXBit = 7 - (physX % 8);
|
|
const uint8_t mask = 1 << physXBit;
|
|
|
|
for (int sx = x1; sx <= x2; sx++) {
|
|
const int physY = HalDisplay::DISPLAY_HEIGHT - 1 - sx;
|
|
const uint16_t byteIndex =
|
|
physY * HalDisplay::DISPLAY_WIDTH_BYTES + physXByte;
|
|
|
|
if (state) {
|
|
frameBuffer[byteIndex] &= ~mask; // Black
|
|
} else {
|
|
frameBuffer[byteIndex] |= mask; // White
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Optimized path for PortraitInverted
|
|
if (orientation == PortraitInverted) {
|
|
for (int sy = y1; sy <= y2; sy++) {
|
|
const int physX = HalDisplay::DISPLAY_WIDTH - 1 - sy;
|
|
const uint8_t physXByte = physX / 8;
|
|
const uint8_t physXBit = 7 - (physX % 8);
|
|
const uint8_t mask = 1 << physXBit;
|
|
|
|
for (int sx = x1; sx <= x2; sx++) {
|
|
const int physY = sx;
|
|
const uint16_t byteIndex =
|
|
physY * HalDisplay::DISPLAY_WIDTH_BYTES + physXByte;
|
|
|
|
if (state) {
|
|
frameBuffer[byteIndex] &= ~mask;
|
|
} else {
|
|
frameBuffer[byteIndex] |= mask;
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Optimized horizontal line fill for Landscape modes
|
|
if (orientation == LandscapeCounterClockwise) {
|
|
for (int sy = y1; sy <= y2; sy++) {
|
|
const int physY = sy;
|
|
const uint16_t rowOffset = physY * HalDisplay::DISPLAY_WIDTH_BYTES;
|
|
|
|
// Fill full bytes where possible
|
|
const int physX1 = x1;
|
|
const int physX2 = x2;
|
|
const int byteStart = physX1 / 8;
|
|
const int byteEnd = physX2 / 8;
|
|
|
|
for (int bx = byteStart; bx <= byteEnd; bx++) {
|
|
uint8_t mask = 0xFF;
|
|
|
|
// Mask out bits before start on first byte
|
|
if (bx == byteStart) {
|
|
const int startBit = physX1 % 8;
|
|
mask &= (0xFF >> startBit);
|
|
}
|
|
// Mask out bits after end on last byte
|
|
if (bx == byteEnd) {
|
|
const int endBit = physX2 % 8;
|
|
mask &= (0xFF << (7 - endBit));
|
|
}
|
|
|
|
if (state) {
|
|
frameBuffer[rowOffset + bx] &= ~mask;
|
|
} else {
|
|
frameBuffer[rowOffset + bx] |= mask;
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Fallback for LandscapeClockwise and any other cases
|
|
for (int fillY = y1; fillY <= y2; fillY++) {
|
|
drawLine(x1, fillY, x2, fillY, state);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::fillRectDithered(const int x, const int y, const int width,
|
|
const int height, const uint8_t grayLevel) const {
|
|
// Simulate grayscale using dithering patterns
|
|
// 0x00 = black, 0xFF = white, values in between = dithered
|
|
|
|
if (grayLevel == 0x00) {
|
|
fillRect(x, y, width, height, true); // Solid black
|
|
return;
|
|
}
|
|
if (grayLevel >= 0xF0) {
|
|
fillRect(x, y, width, height, false); // Solid white
|
|
return;
|
|
}
|
|
|
|
// Use ordered dithering (Bayer matrix 2x2)
|
|
const int screenWidth = getScreenWidth();
|
|
const int screenHeight = getScreenHeight();
|
|
|
|
const int x1 = std::max(0, x);
|
|
const int y1 = std::max(0, y);
|
|
const int x2 = std::min(screenWidth - 1, x + width - 1);
|
|
const int y2 = std::min(screenHeight - 1, y + height - 1);
|
|
|
|
if (x1 > x2 || y1 > y2) return;
|
|
|
|
int threshold = (grayLevel * 4) / 255;
|
|
|
|
for (int sy = y1; sy <= y2; sy++) {
|
|
for (int sx = x1; sx <= x2; sx++) {
|
|
int bayerValue;
|
|
int px = sx % 2;
|
|
int py = sy % 2;
|
|
if (px == 0 && py == 0) bayerValue = 0;
|
|
else if (px == 1 && py == 0) bayerValue = 2;
|
|
else if (px == 0 && py == 1) bayerValue = 3;
|
|
else bayerValue = 1;
|
|
|
|
bool isBlack = bayerValue >= threshold;
|
|
drawPixel(sx, sy, isBlack);
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawRoundedRect(const int x, const int y, const int width,
|
|
const int height, const int radius, const bool state) const {
|
|
if (radius <= 0) {
|
|
drawRect(x, y, width, height, state);
|
|
return;
|
|
}
|
|
|
|
int r = std::min(radius, std::min(width / 2, height / 2));
|
|
int cx, cy;
|
|
int px = 0, py = r;
|
|
int d = 1 - r;
|
|
|
|
while (px <= py) {
|
|
cx = x + r; cy = y + r;
|
|
drawPixel(cx - py, cy - px, state);
|
|
drawPixel(cx - px, cy - py, state);
|
|
|
|
cx = x + width - 1 - r; cy = y + r;
|
|
drawPixel(cx + py, cy - px, state);
|
|
drawPixel(cx + px, cy - py, state);
|
|
|
|
cx = x + r; cy = y + height - 1 - r;
|
|
drawPixel(cx - py, cy + px, state);
|
|
drawPixel(cx - px, cy + py, state);
|
|
|
|
cx = x + width - 1 - r; cy = y + height - 1 - r;
|
|
drawPixel(cx + py, cy + px, state);
|
|
drawPixel(cx + px, cy + py, state);
|
|
|
|
if (d < 0) {
|
|
d += 2 * px + 3;
|
|
} else {
|
|
d += 2 * (px - py) + 5;
|
|
py--;
|
|
}
|
|
px++;
|
|
}
|
|
|
|
drawLine(x + r, y, x + width - 1 - r, y, state);
|
|
drawLine(x + r, y + height - 1, x + width - 1 - r, y + height - 1, state);
|
|
drawLine(x, y + r, x, y + height - 1 - r, state);
|
|
drawLine(x + width - 1, y + r, x + width - 1, y + height - 1 - r, state);
|
|
}
|
|
|
|
void GfxRenderer::fillRoundedRect(const int x, const int y, const int width,
|
|
const int height, const int radius, const bool state) const {
|
|
if (radius <= 0) {
|
|
fillRect(x, y, width, height, state);
|
|
return;
|
|
}
|
|
|
|
int r = std::min(radius, std::min(width / 2, height / 2));
|
|
fillRect(x + r, y, width - 2 * r, height, state);
|
|
fillRect(x, y + r, r, height - 2 * r, state);
|
|
fillRect(x + width - r, y + r, r, height - 2 * r, state);
|
|
|
|
int px = 0, py = r;
|
|
int d = 1 - r;
|
|
|
|
while (px <= py) {
|
|
drawLine(x + r - py, y + r - px, x + r, y + r - px, state);
|
|
drawLine(x + width - 1 - r, y + r - px, x + width - 1 - r + py, y + r - px, state);
|
|
drawLine(x + r - px, y + r - py, x + r, y + r - py, state);
|
|
drawLine(x + width - 1 - r, y + r - py, x + width - 1 - r + px, y + r - py, state);
|
|
|
|
drawLine(x + r - py, y + height - 1 - r + px, x + r, y + height - 1 - r + px, state);
|
|
drawLine(x + width - 1 - r, y + height - 1 - r + px, x + width - 1 - r + py, y + height - 1 - r + px, state);
|
|
drawLine(x + r - px, y + height - 1 - r + py, x + r, y + height - 1 - r + py, state);
|
|
drawLine(x + width - 1 - r, y + height - 1 - r + py, x + width - 1 - r + px, y + height - 1 - r + py, state);
|
|
|
|
if (d < 0) {
|
|
d += 2 * px + 3;
|
|
} else {
|
|
d += 2 * (px - py) + 5;
|
|
py--;
|
|
}
|
|
px++;
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::fillRoundedRectDithered(const int x, const int y, const int width,
|
|
const int height, const int radius,
|
|
const uint8_t grayLevel) const {
|
|
if (grayLevel == 0x00) {
|
|
fillRoundedRect(x, y, width, height, radius, true);
|
|
return;
|
|
}
|
|
if (grayLevel >= 0xF0) {
|
|
fillRoundedRect(x, y, width, height, radius, false);
|
|
return;
|
|
}
|
|
|
|
int r = std::min(radius, std::min(width / 2, height / 2));
|
|
if (r <= 0) {
|
|
fillRectDithered(x, y, width, height, grayLevel);
|
|
return;
|
|
}
|
|
|
|
const int screenWidth = getScreenWidth();
|
|
const int screenHeight = getScreenHeight();
|
|
int threshold = (grayLevel * 4) / 255;
|
|
|
|
auto isInside = [&](int px, int py) -> bool {
|
|
if (px < x + r && py < y + r) {
|
|
int dx = px - (x + r);
|
|
int dy = py - (y + r);
|
|
return (dx * dx + dy * dy) <= r * r;
|
|
}
|
|
if (px >= x + width - r && py < y + r) {
|
|
int dx = px - (x + width - 1 - r);
|
|
int dy = py - (y + r);
|
|
return (dx * dx + dy * dy) <= r * r;
|
|
}
|
|
if (px < x + r && py >= y + height - r) {
|
|
int dx = px - (x + r);
|
|
int dy = py - (y + height - 1 - r);
|
|
return (dx * dx + dy * dy) <= r * r;
|
|
}
|
|
if (px >= x + width - r && py >= y + height - r) {
|
|
int dx = px - (x + width - 1 - r);
|
|
int dy = py - (y + height - 1 - r);
|
|
return (dx * dx + dy * dy) <= r * r;
|
|
}
|
|
return px >= x && px < x + width && py >= y && py < y + height;
|
|
};
|
|
|
|
const int x1 = std::max(0, x);
|
|
const int y1 = std::max(0, y);
|
|
const int x2 = std::min(screenWidth - 1, x + width - 1);
|
|
const int y2 = std::min(screenHeight - 1, y + height - 1);
|
|
|
|
for (int sy = y1; sy <= y2; sy++) {
|
|
for (int sx = x1; sx <= x2; sx++) {
|
|
if (!isInside(sx, sy)) continue;
|
|
|
|
int bayerValue;
|
|
int bx = sx % 2;
|
|
int by = sy % 2;
|
|
if (bx == 0 && by == 0) bayerValue = 0;
|
|
else if (bx == 1 && by == 0) bayerValue = 2;
|
|
else if (bx == 0 && by == 1) bayerValue = 3;
|
|
else bayerValue = 1;
|
|
|
|
bool isBlack = bayerValue >= threshold;
|
|
drawPixel(sx, sy, isBlack);
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, const int width, const int height) const {
|
|
int rotatedX = 0;
|
|
int rotatedY = 0;
|
|
rotateCoordinates(x, y, &rotatedX, &rotatedY);
|
|
// Rotate origin corner
|
|
switch (orientation) {
|
|
case Portrait:
|
|
rotatedY = rotatedY - height;
|
|
break;
|
|
case PortraitInverted:
|
|
rotatedX = rotatedX - width;
|
|
break;
|
|
case LandscapeClockwise:
|
|
rotatedY = rotatedY - height;
|
|
rotatedX = rotatedX - width;
|
|
break;
|
|
case LandscapeCounterClockwise:
|
|
break;
|
|
}
|
|
// TODO: Rotate bits
|
|
display.drawImage(bitmap, rotatedX, rotatedY, width, height);
|
|
}
|
|
|
|
void GfxRenderer::drawBitmap(const Bitmap &bitmap, const int x, const int y,
|
|
const int maxWidth, const int maxHeight,
|
|
const float cropX, const float cropY) const {
|
|
// For 1-bit bitmaps, use optimized 1-bit rendering path (no crop support for
|
|
// 1-bit)
|
|
if (bitmap.is1Bit() && cropX == 0.0f && cropY == 0.0f) {
|
|
drawBitmap1Bit(bitmap, x, y, maxWidth, maxHeight);
|
|
return;
|
|
}
|
|
|
|
float scale = 1.0f;
|
|
bool isScaled = false;
|
|
int cropPixX = std::floor(bitmap.getWidth() * cropX / 2.0f);
|
|
int cropPixY = std::floor(bitmap.getHeight() * cropY / 2.0f);
|
|
|
|
if (maxWidth > 0 && (1.0f - cropX) * bitmap.getWidth() > maxWidth) {
|
|
scale = static_cast<float>(maxWidth) /
|
|
static_cast<float>((1.0f - cropX) * bitmap.getWidth());
|
|
isScaled = true;
|
|
}
|
|
if (maxHeight > 0 && (1.0f - cropY) * bitmap.getHeight() > maxHeight) {
|
|
scale = std::min(
|
|
scale, static_cast<float>(maxHeight) /
|
|
static_cast<float>((1.0f - cropY) * bitmap.getHeight()));
|
|
isScaled = true;
|
|
}
|
|
|
|
// Calculate output row size (2 bits per pixel, packed into bytes)
|
|
// IMPORTANT: Use int, not uint8_t, to avoid overflow for images > 1020 pixels
|
|
// wide
|
|
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() - cropPixY); bmpY++) {
|
|
// The BMP's (0, 0) is the bottom-left corner (if the height is positive,
|
|
// top-left if negative). Screen's (0, 0) is the top-left corner.
|
|
int screenY =
|
|
-cropPixY + (bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY);
|
|
if (isScaled) {
|
|
screenY = std::floor(screenY * scale);
|
|
}
|
|
screenY += y; // the offset should not be scaled
|
|
if (screenY >= getScreenHeight()) {
|
|
break;
|
|
}
|
|
|
|
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
|
Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(),
|
|
bmpY);
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
if (screenY < 0) {
|
|
continue;
|
|
}
|
|
|
|
if (bmpY < cropPixY) {
|
|
// Skip the row if it's outside the crop area
|
|
continue;
|
|
}
|
|
|
|
for (int bmpX = cropPixX; bmpX < bitmap.getWidth() - cropPixX; bmpX++) {
|
|
int screenX = bmpX - cropPixX;
|
|
if (isScaled) {
|
|
screenX = std::floor(screenX * scale);
|
|
}
|
|
screenX += x; // the offset should not be scaled
|
|
if (screenX >= getScreenWidth()) {
|
|
break;
|
|
}
|
|
if (screenX < 0) {
|
|
continue;
|
|
}
|
|
|
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
|
|
// In BW mode:
|
|
// 0 = Black (< 45)
|
|
// 1 = Dark Gray (< 70)
|
|
// 2 = Light Gray (< 140)
|
|
// 3 = White
|
|
// Draw black for val < 2, and white for val >= 2 (Opaque)
|
|
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::draw2BitImage(const uint8_t data[], int x, int y, int w,
|
|
int h) const {
|
|
uint8_t *frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
return;
|
|
}
|
|
|
|
const int screenWidth = getScreenWidth();
|
|
const int screenHeight = getScreenHeight();
|
|
|
|
// Pre-compute row byte width for 2-bit packed data (4 pixels per byte)
|
|
const int srcRowBytes = (w + 3) / 4;
|
|
|
|
// Optimized path for Portrait mode with BW rendering (most common case)
|
|
// In Portrait: logical (x, y) -> physical (y, DISPLAY_HEIGHT - 1 - x)
|
|
if (orientation == Portrait && renderMode == BW) {
|
|
for (int row = 0; row < h; row++) {
|
|
const int screenY = y + row;
|
|
if (screenY < 0 || screenY >= screenHeight)
|
|
continue;
|
|
|
|
// In Portrait, screenY maps to physical X coordinate
|
|
const int physX = screenY;
|
|
const uint8_t *srcRow = data + row * srcRowBytes;
|
|
|
|
for (int col = 0; col < w; col++) {
|
|
const int screenX = x + col;
|
|
if (screenX < 0 || screenX >= screenWidth)
|
|
continue;
|
|
|
|
// Extract 2-bit value (4 pixels per byte)
|
|
const uint8_t val = (srcRow[col / 4] >> (6 - ((col % 4) * 2))) & 0x3;
|
|
|
|
// val < 2 means black pixel in 2-bit representation (0=Black, 1=DarkGray)
|
|
// 2=LightGray, 3=White -> Treat as White
|
|
if (val < 2) {
|
|
// In Portrait: physical Y = DISPLAY_HEIGHT - 1 - screenX
|
|
const int physY = HalDisplay::DISPLAY_HEIGHT - 1 - screenX;
|
|
const uint16_t byteIndex =
|
|
physY * HalDisplay::DISPLAY_WIDTH_BYTES + (physX / 8);
|
|
const uint8_t bitPosition = 7 - (physX % 8);
|
|
frameBuffer[byteIndex] &= ~(1 << bitPosition); // Clear bit = black
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Optimized path for PortraitInverted mode with BW rendering
|
|
if (orientation == PortraitInverted && renderMode == BW) {
|
|
for (int row = 0; row < h; row++) {
|
|
const int screenY = y + row;
|
|
if (screenY < 0 || screenY >= screenHeight)
|
|
continue;
|
|
|
|
const uint8_t *srcRow = data + row * srcRowBytes;
|
|
|
|
for (int col = 0; col < w; col++) {
|
|
const int screenX = x + col;
|
|
if (screenX < 0 || screenX >= screenWidth)
|
|
continue;
|
|
|
|
const uint8_t val = (srcRow[col / 4] >> (6 - ((col % 4) * 2))) & 0x3;
|
|
|
|
if (val < 3) {
|
|
// PortraitInverted: physical X = DISPLAY_WIDTH - 1 - screenY
|
|
// physical Y = screenX
|
|
const int physX = HalDisplay::DISPLAY_WIDTH - 1 - screenY;
|
|
const int physY = screenX;
|
|
const uint16_t byteIndex =
|
|
physY * HalDisplay::DISPLAY_WIDTH_BYTES + (physX / 8);
|
|
const uint8_t bitPosition = 7 - (physX % 8);
|
|
frameBuffer[byteIndex] &= ~(1 << bitPosition);
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Optimized path for Landscape modes with BW rendering
|
|
if ((orientation == LandscapeClockwise ||
|
|
orientation == LandscapeCounterClockwise) &&
|
|
renderMode == BW) {
|
|
for (int row = 0; row < h; row++) {
|
|
const int screenY = y + row;
|
|
if (screenY < 0 || screenY >= screenHeight)
|
|
continue;
|
|
|
|
const uint8_t *srcRow = data + row * srcRowBytes;
|
|
|
|
for (int col = 0; col < w; col++) {
|
|
const int screenX = x + col;
|
|
if (screenX < 0 || screenX >= screenWidth)
|
|
continue;
|
|
|
|
const uint8_t val = (srcRow[col / 4] >> (6 - ((col % 4) * 2))) & 0x3;
|
|
|
|
if (val < 3) {
|
|
int physX, physY;
|
|
if (orientation == LandscapeClockwise) {
|
|
physX = HalDisplay::DISPLAY_WIDTH - 1 - screenX;
|
|
physY = HalDisplay::DISPLAY_HEIGHT - 1 - screenY;
|
|
} else {
|
|
physX = screenX;
|
|
physY = screenY;
|
|
}
|
|
const uint16_t byteIndex =
|
|
physY * HalDisplay::DISPLAY_WIDTH_BYTES + (physX / 8);
|
|
const uint8_t bitPosition = 7 - (physX % 8);
|
|
frameBuffer[byteIndex] &= ~(1 << bitPosition);
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Fallback: generic path for grayscale modes
|
|
for (int row = 0; row < h; row++) {
|
|
const int screenY = y + row;
|
|
if (screenY < 0 || screenY >= screenHeight)
|
|
continue;
|
|
|
|
const uint8_t *srcRow = data + row * srcRowBytes;
|
|
|
|
for (int col = 0; col < w; col++) {
|
|
const int screenX = x + col;
|
|
if (screenX < 0 || screenX >= screenWidth)
|
|
continue;
|
|
|
|
const uint8_t val = (srcRow[col / 4] >> (6 - ((col % 4) * 2))) & 0x3;
|
|
|
|
if (renderMode == GRAYSCALE_MSB && (val == 1 || val == 2)) {
|
|
drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && val == 1) {
|
|
drawPixel(screenX, screenY, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawBitmap1Bit(const Bitmap &bitmap, const int x, const int y,
|
|
const int maxWidth,
|
|
const int maxHeight) const {
|
|
float scale = 1.0f;
|
|
bool isScaled = false;
|
|
if (maxWidth > 0 && bitmap.getWidth() > maxWidth) {
|
|
scale =
|
|
static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
|
isScaled = true;
|
|
}
|
|
if (maxHeight > 0 && bitmap.getHeight() > maxHeight) {
|
|
scale = std::min(scale, static_cast<float>(maxHeight) /
|
|
static_cast<float>(bitmap.getHeight()));
|
|
isScaled = true;
|
|
}
|
|
|
|
// For 1-bit BMP, output is still 2-bit packed (for consistency with
|
|
// readNextRow)
|
|
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 1-bit BMP row buffers\n",
|
|
millis());
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
|
// Read rows sequentially using readNextRow
|
|
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
|
Serial.printf("[%lu] [GFX] Failed to read row %d from 1-bit bitmap\n",
|
|
millis(), bmpY);
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
// Calculate screen Y based on whether BMP is top-down or bottom-up
|
|
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; // Continue reading to keep row counter in sync
|
|
}
|
|
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 (result of readNextRow quantization)
|
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
|
|
// For 1-bit source: 0 or 1 -> map to black (0,1,2) or white (3)
|
|
// Draw black if val < 3, Draw white if val == 3 (Opaque)
|
|
drawPixel(screenX, screenY, val < 3);
|
|
// White pixels (val == 3) are not drawn (leave background)
|
|
}
|
|
}
|
|
|
|
free(outputRow);
|
|
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) {
|
|
scale = static_cast<float>(w) / static_cast<float>(bitmap.getWidth());
|
|
isScaled = true;
|
|
}
|
|
if (h > 0) {
|
|
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;
|
|
|
|
// Calculate target Y span
|
|
int startY = y + static_cast<int>(std::floor(bmpYOffset * scale));
|
|
int endY = y + static_cast<int>(std::floor((bmpYOffset + 1) * scale));
|
|
|
|
// Clamp to screen
|
|
if (startY < 0) startY = 0;
|
|
if (endY > getScreenHeight()) endY = getScreenHeight();
|
|
if (startY >= endY) continue;
|
|
|
|
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
|
// Calculate target X span
|
|
int startX = x + static_cast<int>(std::floor(bmpX * scale));
|
|
int endX = x + static_cast<int>(std::floor((bmpX + 1) * scale));
|
|
|
|
if (startX < 0) startX = 0;
|
|
if (endX > getScreenWidth()) endX = getScreenWidth();
|
|
if (startX >= endX) continue;
|
|
|
|
// Extract 2-bit value (0=Black, 3=White for 1-bit BMP)
|
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
|
|
if (val < 3) {
|
|
for (int sy = startY; sy < endY; sy++) {
|
|
for (int sx = startX; sx < endX; sx++) {
|
|
drawPixel(sx, sy, true); // Black
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
free(outputRow);
|
|
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) {
|
|
scale = static_cast<float>(w) / static_cast<float>(bitmap.getWidth());
|
|
isScaled = true;
|
|
}
|
|
if (h > 0) {
|
|
scale = std::min(scale, static_cast<float>(h) / static_cast<float>(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<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;
|
|
|
|
// Calculate target Y span
|
|
int startY = y + static_cast<int>(std::floor(bmpYOffset * scale));
|
|
int endY = y + static_cast<int>(std::floor((bmpYOffset + 1) * scale));
|
|
|
|
if (startY < 0) startY = 0;
|
|
if (endY > getScreenHeight()) endY = getScreenHeight();
|
|
if (startY >= endY) continue;
|
|
|
|
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
|
int startX = x + static_cast<int>(std::floor(bmpX * scale));
|
|
int endX = x + static_cast<int>(std::floor((bmpX + 1) * scale));
|
|
|
|
if (startX < 0) startX = 0;
|
|
if (endX > getScreenWidth()) endX = getScreenWidth();
|
|
if (startX >= endX) continue;
|
|
|
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
bool pixelBlack = false;
|
|
|
|
if (renderMode == BW) {
|
|
pixelBlack = (val < 2);
|
|
} else if (renderMode == GRAYSCALE_MSB) {
|
|
pixelBlack = (val < 3); // Draw all non-white as black for icons/covers
|
|
} else if (renderMode == GRAYSCALE_LSB) {
|
|
pixelBlack = (val == 0);
|
|
}
|
|
|
|
if (pixelBlack) {
|
|
for (int sy = startY; sy < endY; sy++) {
|
|
int relY = sy - y;
|
|
for (int sx = startX; sx < endX; sx++) {
|
|
int relX = sx - x;
|
|
if (isVisible(relX, relY)) {
|
|
drawPixel(sx, sy, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
}
|
|
|
|
void GfxRenderer::fillPolygon(const int *xPoints, const int *yPoints,
|
|
int numPoints, bool state) const {
|
|
if (numPoints < 3)
|
|
return;
|
|
|
|
// Find bounding box
|
|
int minY = yPoints[0], maxY = yPoints[0];
|
|
for (int i = 1; i < numPoints; i++) {
|
|
if (yPoints[i] < minY)
|
|
minY = yPoints[i];
|
|
if (yPoints[i] > maxY)
|
|
maxY = yPoints[i];
|
|
}
|
|
|
|
// Clip to screen
|
|
if (minY < 0)
|
|
minY = 0;
|
|
if (maxY >= getScreenHeight())
|
|
maxY = getScreenHeight() - 1;
|
|
|
|
// Allocate node buffer for scanline algorithm
|
|
auto *nodeX = static_cast<int *>(malloc(numPoints * sizeof(int)));
|
|
if (!nodeX) {
|
|
Serial.printf("[%lu] [GFX] !! Failed to allocate polygon node buffer\n",
|
|
millis());
|
|
return;
|
|
}
|
|
|
|
// Scanline fill algorithm
|
|
for (int scanY = minY; scanY <= maxY; scanY++) {
|
|
int nodes = 0;
|
|
|
|
// Find all intersection points with edges
|
|
int j = numPoints - 1;
|
|
for (int i = 0; i < numPoints; i++) {
|
|
if ((yPoints[i] < scanY && yPoints[j] >= scanY) ||
|
|
(yPoints[j] < scanY && yPoints[i] >= scanY)) {
|
|
// Calculate X intersection using fixed-point to avoid float
|
|
int dy = yPoints[j] - yPoints[i];
|
|
if (dy != 0) {
|
|
nodeX[nodes++] = xPoints[i] + (scanY - yPoints[i]) *
|
|
(xPoints[j] - xPoints[i]) / dy;
|
|
}
|
|
}
|
|
j = i;
|
|
}
|
|
|
|
// Sort nodes by X (simple bubble sort, numPoints is small)
|
|
for (int i = 0; i < nodes - 1; i++) {
|
|
for (int k = i + 1; k < nodes; k++) {
|
|
if (nodeX[i] > nodeX[k]) {
|
|
int temp = nodeX[i];
|
|
nodeX[i] = nodeX[k];
|
|
nodeX[k] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fill between pairs of nodes
|
|
for (int i = 0; i < nodes - 1; i += 2) {
|
|
int startX = nodeX[i];
|
|
int endX = nodeX[i + 1];
|
|
|
|
// Clip to screen
|
|
if (startX < 0)
|
|
startX = 0;
|
|
if (endX >= getScreenWidth())
|
|
endX = getScreenWidth() - 1;
|
|
|
|
// Draw horizontal line
|
|
for (int x = startX; x <= endX; x++) {
|
|
drawPixel(x, scanY, state);
|
|
}
|
|
}
|
|
}
|
|
|
|
free(nodeX);
|
|
}
|
|
|
|
uint8_t* GfxRenderer::captureRegion(int x, int y, int width, int height, size_t* outSize) const {
|
|
uint8_t* frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer || width <= 0 || height <= 0) {
|
|
if (outSize) *outSize = 0;
|
|
return nullptr;
|
|
}
|
|
|
|
// Clip to screen bounds
|
|
const int screenWidth = getScreenWidth();
|
|
const int screenHeight = getScreenHeight();
|
|
if (x < 0) { width += x; x = 0; }
|
|
if (y < 0) { height += y; y = 0; }
|
|
if (x + width > screenWidth) width = screenWidth - x;
|
|
if (y + height > screenHeight) height = screenHeight - y;
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
if (outSize) *outSize = 0;
|
|
return nullptr;
|
|
}
|
|
|
|
// Pack as 1-bit: ceil(width/8) bytes per row
|
|
const size_t rowBytes = (width + 7) / 8;
|
|
const size_t bufferSize = rowBytes * height + 4 * sizeof(int); // +header
|
|
uint8_t* buffer = static_cast<uint8_t*>(malloc(bufferSize));
|
|
if (!buffer) {
|
|
if (outSize) *outSize = 0;
|
|
return nullptr;
|
|
}
|
|
|
|
// Store dimensions in header
|
|
int* header = reinterpret_cast<int*>(buffer);
|
|
header[0] = x;
|
|
header[1] = y;
|
|
header[2] = width;
|
|
header[3] = height;
|
|
uint8_t* data = buffer + 4 * sizeof(int);
|
|
|
|
// Extract pixels - this is orientation-dependent
|
|
for (int row = 0; row < height; row++) {
|
|
const int screenY = y + row;
|
|
uint8_t* destRow = data + row * rowBytes;
|
|
memset(destRow, 0xFF, rowBytes); // Start with white
|
|
|
|
for (int col = 0; col < width; col++) {
|
|
const int screenX = x + col;
|
|
|
|
// Get physical coordinates
|
|
int physX, physY;
|
|
rotateCoordinates(screenX, screenY, &physX, &physY);
|
|
|
|
// Read pixel from framebuffer
|
|
const uint16_t byteIndex = physY * HalDisplay::DISPLAY_WIDTH_BYTES + (physX / 8);
|
|
const uint8_t bitPosition = 7 - (physX % 8);
|
|
const bool isBlack = !(frameBuffer[byteIndex] & (1 << bitPosition));
|
|
|
|
// Store in destination
|
|
if (isBlack) {
|
|
destRow[col / 8] &= ~(1 << (7 - (col % 8)));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (outSize) *outSize = bufferSize;
|
|
return buffer;
|
|
}
|
|
|
|
void GfxRenderer::restoreRegion(const uint8_t* buffer, int x, int y, int width, int height) const {
|
|
uint8_t* frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer || !buffer || width <= 0 || height <= 0) {
|
|
return;
|
|
}
|
|
|
|
const size_t rowBytes = (width + 7) / 8;
|
|
const uint8_t* data = buffer + 4 * sizeof(int); // Skip header
|
|
|
|
// Optimized path for Portrait mode
|
|
if (orientation == Portrait) {
|
|
for (int row = 0; row < height; row++) {
|
|
const int screenY = y + row;
|
|
if (screenY < 0 || screenY >= getScreenHeight()) continue;
|
|
|
|
const uint8_t* srcRow = data + row * rowBytes;
|
|
const int physX = screenY;
|
|
const uint8_t physXByte = physX / 8;
|
|
const uint8_t physXBit = 7 - (physX % 8);
|
|
const uint8_t mask = 1 << physXBit;
|
|
|
|
for (int col = 0; col < width; col++) {
|
|
const int screenX = x + col;
|
|
if (screenX < 0 || screenX >= getScreenWidth()) continue;
|
|
|
|
const bool isBlack = !(srcRow[col / 8] & (1 << (7 - (col % 8))));
|
|
const int physY = HalDisplay::DISPLAY_HEIGHT - 1 - screenX;
|
|
const uint16_t byteIndex = physY * HalDisplay::DISPLAY_WIDTH_BYTES + physXByte;
|
|
|
|
if (isBlack) {
|
|
frameBuffer[byteIndex] &= ~mask;
|
|
} else {
|
|
frameBuffer[byteIndex] |= mask;
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Generic fallback using drawPixel
|
|
for (int row = 0; row < height; row++) {
|
|
const int screenY = y + row;
|
|
const uint8_t* srcRow = data + row * rowBytes;
|
|
|
|
for (int col = 0; col < width; col++) {
|
|
const int screenX = x + col;
|
|
const bool isBlack = !(srcRow[col / 8] & (1 << (7 - (col % 8))));
|
|
drawPixel(screenX, screenY, isBlack);
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::clearScreen(const uint8_t color) const { display.clearScreen(color); }
|
|
|
|
void GfxRenderer::invertScreen() const {
|
|
uint8_t* buffer = display.getFrameBuffer();
|
|
if (!buffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer in invertScreen\n", millis());
|
|
return;
|
|
}
|
|
for (int i = 0; i < HalDisplay::BUFFER_SIZE; i++) {
|
|
buffer[i] = ~buffer[i];
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::displayBuffer(const HalDisplay::RefreshMode refreshMode) const { display.displayBuffer(refreshMode); }
|
|
|
|
std::string GfxRenderer::truncatedText(const int fontId, const char *text,
|
|
const int maxWidth,
|
|
const EpdFontFamily::Style style) const {
|
|
std::string item = text;
|
|
int itemWidth = getTextWidth(fontId, item.c_str(), style);
|
|
while (itemWidth > maxWidth && item.length() > 8) {
|
|
item.replace(item.length() - 5, 5, "...");
|
|
itemWidth = getTextWidth(fontId, item.c_str(), style);
|
|
}
|
|
return item;
|
|
}
|
|
|
|
// Note: Internal driver treats screen in command orientation; this library
|
|
// exposes a logical orientation
|
|
int GfxRenderer::getScreenWidth() const {
|
|
switch (orientation) {
|
|
case Portrait:
|
|
case PortraitInverted:
|
|
// 480px wide in portrait logical coordinates
|
|
return HalDisplay::DISPLAY_HEIGHT;
|
|
case LandscapeClockwise:
|
|
case LandscapeCounterClockwise:
|
|
// 800px wide in landscape logical coordinates
|
|
return HalDisplay::DISPLAY_WIDTH;
|
|
}
|
|
return HalDisplay::DISPLAY_HEIGHT;
|
|
}
|
|
|
|
int GfxRenderer::getScreenHeight() const {
|
|
switch (orientation) {
|
|
case Portrait:
|
|
case PortraitInverted:
|
|
// 800px tall in portrait logical coordinates
|
|
return HalDisplay::DISPLAY_WIDTH;
|
|
case LandscapeClockwise:
|
|
case LandscapeCounterClockwise:
|
|
// 480px tall in landscape logical coordinates
|
|
return HalDisplay::DISPLAY_HEIGHT;
|
|
}
|
|
return HalDisplay::DISPLAY_WIDTH;
|
|
}
|
|
|
|
int GfxRenderer::getSpaceWidth(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontMap.at(fontId).getGlyph(' ', EpdFontFamily::REGULAR)->advanceX;
|
|
}
|
|
|
|
int GfxRenderer::getFontAscenderSize(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->ascender;
|
|
}
|
|
|
|
int GfxRenderer::getLineHeight(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->advanceY;
|
|
}
|
|
|
|
void GfxRenderer::drawButtonHints(const int fontId, const char *btn1,
|
|
const char *btn2, const char *btn3,
|
|
const char *btn4) {
|
|
const Orientation orig_orientation = getOrientation();
|
|
setOrientation(Orientation::Portrait);
|
|
|
|
const int pageHeight = getScreenHeight();
|
|
constexpr int buttonWidth = 106;
|
|
constexpr int buttonHeight = 40;
|
|
constexpr int buttonY = 40; // Distance from bottom
|
|
constexpr int textYOffset = 7; // Distance from top of button to text baseline
|
|
constexpr int buttonPositions[] = {25, 130, 245, 350};
|
|
const char *labels[] = {btn1, btn2, btn3, btn4};
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
// Only draw if the label is non-empty
|
|
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
|
const int x = buttonPositions[i];
|
|
fillRect(x, pageHeight - buttonY, buttonWidth, buttonHeight, false);
|
|
drawRect(x, pageHeight - buttonY, buttonWidth, buttonHeight);
|
|
const int textWidth = getTextWidth(fontId, labels[i]);
|
|
const int textX = x + (buttonWidth - 1 - textWidth) / 2;
|
|
drawText(fontId, textX, pageHeight - buttonY + textYOffset, labels[i]);
|
|
}
|
|
}
|
|
|
|
setOrientation(orig_orientation);
|
|
}
|
|
|
|
void GfxRenderer::drawSideButtonHints(const int fontId, const char *topBtn,
|
|
const char *bottomBtn) const {
|
|
const int screenWidth = getScreenWidth();
|
|
constexpr int buttonWidth = 40; // Width on screen (height when rotated)
|
|
constexpr int buttonHeight = 80; // Height on screen (width when rotated)
|
|
constexpr int buttonX = 5; // Distance from right edge
|
|
// Position for the button group - buttons share a border so they're adjacent
|
|
constexpr int topButtonY = 345; // Top button position
|
|
|
|
const char *labels[] = {topBtn, bottomBtn};
|
|
|
|
// Draw the shared border for both buttons as one unit
|
|
const int x = screenWidth - buttonX - buttonWidth;
|
|
|
|
// Draw top button outline (3 sides, bottom open)
|
|
if (topBtn != nullptr && topBtn[0] != '\0') {
|
|
drawLine(x, topButtonY, x + buttonWidth - 1, topButtonY); // Top
|
|
drawLine(x, topButtonY, x, topButtonY + buttonHeight - 1); // Left
|
|
drawLine(x + buttonWidth - 1, topButtonY, x + buttonWidth - 1,
|
|
topButtonY + buttonHeight - 1); // Right
|
|
}
|
|
|
|
// Draw shared middle border
|
|
if ((topBtn != nullptr && topBtn[0] != '\0') ||
|
|
(bottomBtn != nullptr && bottomBtn[0] != '\0')) {
|
|
drawLine(x, topButtonY + buttonHeight, x + buttonWidth - 1,
|
|
topButtonY + buttonHeight); // Shared border
|
|
}
|
|
|
|
// Draw bottom button outline (3 sides, top is shared)
|
|
if (bottomBtn != nullptr && bottomBtn[0] != '\0') {
|
|
drawLine(x, topButtonY + buttonHeight, x,
|
|
topButtonY + 2 * buttonHeight - 1); // Left
|
|
drawLine(x + buttonWidth - 1, topButtonY + buttonHeight,
|
|
x + buttonWidth - 1,
|
|
topButtonY + 2 * buttonHeight - 1); // Right
|
|
drawLine(x, topButtonY + 2 * buttonHeight - 1, x + buttonWidth - 1,
|
|
topButtonY + 2 * buttonHeight - 1); // Bottom
|
|
}
|
|
|
|
// Draw text for each button
|
|
for (int i = 0; i < 2; i++) {
|
|
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
|
const int y = topButtonY + i * buttonHeight;
|
|
|
|
// Draw rotated text centered in the button
|
|
const int textWidth = getTextWidth(fontId, labels[i]);
|
|
const int textHeight = getTextHeight(fontId);
|
|
|
|
// Center the rotated text in the button
|
|
const int textX = x + (buttonWidth - textHeight) / 2;
|
|
const int textY = y + (buttonHeight + textWidth) / 2;
|
|
|
|
drawTextRotated90CW(fontId, textX, textY, labels[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
int GfxRenderer::getTextHeight(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->ascender;
|
|
}
|
|
|
|
void GfxRenderer::drawTextRotated90CW(const int fontId, const int x,
|
|
const int y, const char *text,
|
|
const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
// Cannot draw a NULL / empty string
|
|
if (text == nullptr || *text == '\0') {
|
|
return;
|
|
}
|
|
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return;
|
|
}
|
|
const auto font = fontMap.at(fontId);
|
|
|
|
// No printable characters
|
|
if (!font.hasPrintableChars(text, style)) {
|
|
return;
|
|
}
|
|
|
|
// For 90° clockwise rotation:
|
|
// Original (glyphX, glyphY) -> Rotated (glyphY, -glyphX)
|
|
// Text reads from bottom to top
|
|
|
|
int yPos = y; // Current Y position (decreases as we draw characters)
|
|
|
|
uint32_t cp;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t **>(&text)))) {
|
|
const EpdGlyph *glyph = font.getGlyph(cp, style);
|
|
if (!glyph) {
|
|
glyph = font.getGlyph(REPLACEMENT_GLYPH, style);
|
|
}
|
|
if (!glyph) {
|
|
continue;
|
|
}
|
|
|
|
const int is2Bit = font.getData(style)->is2Bit;
|
|
const uint32_t offset = glyph->dataOffset;
|
|
const uint8_t width = glyph->width;
|
|
const uint8_t height = glyph->height;
|
|
const int left = glyph->left;
|
|
const int top = glyph->top;
|
|
|
|
const uint8_t *bitmap = &font.getData(style)->bitmap[offset];
|
|
|
|
if (bitmap != nullptr) {
|
|
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
for (int glyphX = 0; glyphX < width; glyphX++) {
|
|
const int pixelPosition = glyphY * width + glyphX;
|
|
|
|
// 90° clockwise rotation transformation:
|
|
// screenX = x + (ascender - top + glyphY)
|
|
// screenY = yPos - (left + glyphX)
|
|
const int screenX =
|
|
x + (font.getData(style)->ascender - top + glyphY);
|
|
const int screenY = yPos - left - glyphX;
|
|
|
|
if (is2Bit) {
|
|
const uint8_t byte = bitmap[pixelPosition / 4];
|
|
const uint8_t bit_index = (3 - pixelPosition % 4) * 2;
|
|
const uint8_t bmpVal = 3 - (byte >> bit_index) & 0x3;
|
|
|
|
if (renderMode == BW && bmpVal < 3) {
|
|
drawPixel(screenX, screenY, black);
|
|
} else if (renderMode == GRAYSCALE_MSB &&
|
|
(bmpVal == 1 || bmpVal == 2)) {
|
|
drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && bmpVal == 1) {
|
|
drawPixel(screenX, screenY, false);
|
|
}
|
|
} else {
|
|
const uint8_t byte = bitmap[pixelPosition / 8];
|
|
const uint8_t bit_index = 7 - (pixelPosition % 8);
|
|
|
|
if ((byte >> bit_index) & 1) {
|
|
drawPixel(screenX, screenY, black);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Move to next character position (going up, so decrease Y)
|
|
yPos -= glyph->advanceX;
|
|
}
|
|
}
|
|
|
|
uint8_t* GfxRenderer::getFrameBuffer() const { return display.getFrameBuffer(); }
|
|
|
|
size_t GfxRenderer::getBufferSize() { return HalDisplay::BUFFER_SIZE; }
|
|
|
|
// unused
|
|
// void GfxRenderer::grayscaleRevert() const { display.grayscaleRevert(); }
|
|
|
|
void GfxRenderer::copyGrayscaleLsbBuffers() const { display.copyGrayscaleLsbBuffers(display.getFrameBuffer()); }
|
|
|
|
void GfxRenderer::copyGrayscaleMsbBuffers() const { display.copyGrayscaleMsbBuffers(display.getFrameBuffer()); }
|
|
|
|
void GfxRenderer::displayGrayBuffer() const { display.displayGrayBuffer(); }
|
|
|
|
void GfxRenderer::freeBwBufferChunks() {
|
|
for (auto &bwBufferChunk : bwBufferChunks) {
|
|
if (bwBufferChunk) {
|
|
free(bwBufferChunk);
|
|
bwBufferChunk = nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This should be called before grayscale buffers are populated.
|
|
* A `restoreBwBuffer` call should always follow the grayscale render if this
|
|
* method was called. Uses chunked allocation to avoid needing 48KB of
|
|
* contiguous memory. Returns true if buffer was stored successfully, false if
|
|
* allocation failed.
|
|
*/
|
|
bool GfxRenderer::storeBwBuffer() {
|
|
const uint8_t* frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer in storeBwBuffer\n", millis());
|
|
return false;
|
|
}
|
|
|
|
// Allocate and copy each chunk
|
|
for (size_t i = 0; i < BW_BUFFER_NUM_CHUNKS; i++) {
|
|
// Check if any chunks are already allocated
|
|
if (bwBufferChunks[i]) {
|
|
Serial.printf("[%lu] [GFX] !! BW buffer chunk %zu already stored - this "
|
|
"is likely a bug, freeing chunk\n",
|
|
millis(), i);
|
|
free(bwBufferChunks[i]);
|
|
bwBufferChunks[i] = nullptr;
|
|
}
|
|
|
|
const size_t offset = i * BW_BUFFER_CHUNK_SIZE;
|
|
bwBufferChunks[i] = static_cast<uint8_t *>(malloc(BW_BUFFER_CHUNK_SIZE));
|
|
|
|
if (!bwBufferChunks[i]) {
|
|
Serial.printf(
|
|
"[%lu] [GFX] !! Failed to allocate BW buffer chunk %zu (%zu bytes)\n",
|
|
millis(), i, BW_BUFFER_CHUNK_SIZE);
|
|
// Free previously allocated chunks
|
|
freeBwBufferChunks();
|
|
return false;
|
|
}
|
|
|
|
memcpy(bwBufferChunks[i], frameBuffer + offset, BW_BUFFER_CHUNK_SIZE);
|
|
}
|
|
|
|
Serial.printf("[%lu] [GFX] Stored BW buffer in %zu chunks (%zu bytes each)\n",
|
|
millis(), BW_BUFFER_NUM_CHUNKS, BW_BUFFER_CHUNK_SIZE);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* This can only be called if `storeBwBuffer` was called prior to the grayscale
|
|
* render. It should be called to restore the BW buffer state after grayscale
|
|
* rendering is complete. Uses chunked restoration to match chunked storage.
|
|
*/
|
|
void GfxRenderer::restoreBwBuffer() {
|
|
// Check if any all chunks are allocated
|
|
bool missingChunks = false;
|
|
for (const auto &bwBufferChunk : bwBufferChunks) {
|
|
if (!bwBufferChunk) {
|
|
missingChunks = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (missingChunks) {
|
|
freeBwBufferChunks();
|
|
return;
|
|
}
|
|
|
|
uint8_t* frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer in restoreBwBuffer\n",
|
|
millis());
|
|
freeBwBufferChunks();
|
|
return;
|
|
}
|
|
|
|
for (size_t i = 0; i < BW_BUFFER_NUM_CHUNKS; i++) {
|
|
// Check if chunk is missing
|
|
if (!bwBufferChunks[i]) {
|
|
Serial.printf(
|
|
"[%lu] [GFX] !! BW buffer chunks not stored - this is likely a bug\n",
|
|
millis());
|
|
freeBwBufferChunks();
|
|
return;
|
|
}
|
|
|
|
const size_t offset = i * BW_BUFFER_CHUNK_SIZE;
|
|
memcpy(frameBuffer + offset, bwBufferChunks[i], BW_BUFFER_CHUNK_SIZE);
|
|
}
|
|
|
|
display.cleanupGrayscaleBuffers(frameBuffer);
|
|
|
|
freeBwBufferChunks();
|
|
Serial.printf("[%lu] [GFX] Restored and freed BW buffer chunks\n", millis());
|
|
}
|
|
|
|
/**
|
|
* Cleanup grayscale buffers using the current frame buffer.
|
|
* Use this when BW buffer was re-rendered instead of stored/restored.
|
|
*/
|
|
void GfxRenderer::cleanupGrayscaleWithFrameBuffer() const {
|
|
uint8_t* frameBuffer = display.getFrameBuffer();
|
|
if (frameBuffer) {
|
|
display.cleanupGrayscaleBuffers(frameBuffer);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::renderChar(const EpdFontFamily &fontFamily, const uint32_t cp,
|
|
int *x, const int *y, const bool pixelState,
|
|
const EpdFontFamily::Style style) const {
|
|
const EpdGlyph *glyph = fontFamily.getGlyph(cp, style);
|
|
if (!glyph) {
|
|
glyph = fontFamily.getGlyph(REPLACEMENT_GLYPH, style);
|
|
}
|
|
|
|
// no glyph?
|
|
if (!glyph) {
|
|
Serial.printf("[%lu] [GFX] No glyph for codepoint %d\n", millis(), cp);
|
|
return;
|
|
}
|
|
|
|
const int is2Bit = fontFamily.getData(style)->is2Bit;
|
|
const uint32_t offset = glyph->dataOffset;
|
|
const uint8_t width = glyph->width;
|
|
const uint8_t height = glyph->height;
|
|
const int left = glyph->left;
|
|
|
|
const uint8_t *bitmap = nullptr;
|
|
bitmap = &fontFamily.getData(style)->bitmap[offset];
|
|
|
|
if (bitmap != nullptr) {
|
|
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
const int screenY = *y - glyph->top + glyphY;
|
|
for (int glyphX = 0; glyphX < width; glyphX++) {
|
|
const int pixelPosition = glyphY * width + glyphX;
|
|
const int screenX = *x + left + glyphX;
|
|
|
|
if (is2Bit) {
|
|
const uint8_t byte = bitmap[pixelPosition / 4];
|
|
const uint8_t bit_index = (3 - pixelPosition % 4) * 2;
|
|
// the direct bit from the font is 0 -> white, 1 -> light gray, 2 ->
|
|
// dark gray, 3 -> black we swap this to better match the way images
|
|
// and screen think about colors: 0 -> black, 1 -> dark grey, 2 ->
|
|
// light grey, 3 -> white
|
|
const uint8_t bmpVal = 3 - (byte >> bit_index) & 0x3;
|
|
|
|
if (renderMode == BW && bmpVal < 3) {
|
|
// Black (also paints over the grays in BW mode)
|
|
drawPixel(screenX, screenY, pixelState);
|
|
} else if (renderMode == GRAYSCALE_MSB &&
|
|
(bmpVal == 1 || bmpVal == 2)) {
|
|
// Light gray (also mark the MSB if it's going to be a dark gray
|
|
// too) We have to flag pixels in reverse for the gray buffers, as 0
|
|
// leave alone, 1 update
|
|
drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && bmpVal == 1) {
|
|
// Dark gray
|
|
drawPixel(screenX, screenY, false);
|
|
}
|
|
} else {
|
|
const uint8_t byte = bitmap[pixelPosition / 8];
|
|
const uint8_t bit_index = 7 - (pixelPosition % 8);
|
|
|
|
if ((byte >> bit_index) & 1) {
|
|
drawPixel(screenX, screenY, pixelState);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
*x += glyph->advanceX;
|
|
}
|
|
|
|
void GfxRenderer::getOrientedViewableTRBL(int *outTop, int *outRight,
|
|
int *outBottom, int *outLeft) const {
|
|
switch (orientation) {
|
|
case Portrait:
|
|
*outTop = VIEWABLE_MARGIN_TOP;
|
|
*outRight = VIEWABLE_MARGIN_RIGHT;
|
|
*outBottom = VIEWABLE_MARGIN_BOTTOM;
|
|
*outLeft = VIEWABLE_MARGIN_LEFT;
|
|
break;
|
|
case LandscapeClockwise:
|
|
*outTop = VIEWABLE_MARGIN_LEFT;
|
|
*outRight = VIEWABLE_MARGIN_TOP;
|
|
*outBottom = VIEWABLE_MARGIN_RIGHT;
|
|
*outLeft = VIEWABLE_MARGIN_BOTTOM;
|
|
break;
|
|
case PortraitInverted:
|
|
*outTop = VIEWABLE_MARGIN_BOTTOM;
|
|
*outRight = VIEWABLE_MARGIN_LEFT;
|
|
*outBottom = VIEWABLE_MARGIN_TOP;
|
|
*outLeft = VIEWABLE_MARGIN_RIGHT;
|
|
break;
|
|
case LandscapeCounterClockwise:
|
|
*outTop = VIEWABLE_MARGIN_RIGHT;
|
|
*outRight = VIEWABLE_MARGIN_BOTTOM;
|
|
*outBottom = VIEWABLE_MARGIN_LEFT;
|
|
*outLeft = VIEWABLE_MARGIN_TOP;
|
|
break;
|
|
}
|
|
}
|