Add cover image display in *Continue Reading* card with framebuffer caching (#200)
## Summary * **What is the goal of this PR?** (e.g., Fixes a bug in the user authentication module, Display the book cover image in the **"Continue Reading"** card on the home screen, with fast navigation using framebuffer caching. * **What changes are included?** - Display book cover image in the "Continue Reading" card on home screen - Load cover from cached BMP (same as sleep screen cover) - Add framebuffer store/restore functions (`copyStoredBwBuffer`, `freeStoredBwBuffer`) for fast navigation after initial render - Fix `drawBitmap` scaling bug: apply scale to offset only, not to base coordinates - Add white text boxes behind title/author/continue reading label for readability on cover - Support both EPUB and XTC file cover images - Increase HomeActivity task stack size from 2048 to 4096 for cover image rendering ## Additional Context * Add any other information that might be helpful for the reviewer (e.g., performance implications, potential risks, specific areas to focus on). - Performance: First render loads cover from SD card (~800ms), subsequent navigation uses cached framebuffer (~instant) - Memory: Framebuffer cache uses ~48KB (6 chunks × 8KB) while on home screen, freed on exit - Fallback: If cover image is not available, falls back to standard text-only display - The `drawBitmap` fix corrects a bug where screenY = (y + offset) scale was incorrectly scaling the base coordinates. Now correctly uses screenY = y + (offset scale)
This commit is contained in:
@@ -228,7 +228,10 @@ BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
|
||||
}
|
||||
case 1: {
|
||||
for (int x = 0; x < width; x++) {
|
||||
lum = (rowBuffer[x >> 3] & (0x80 >> (x & 7))) ? 0xFF : 0x00;
|
||||
// Get palette index (0 or 1) from bit at position x
|
||||
const uint8_t palIndex = (rowBuffer[x >> 3] & (0x80 >> (x & 7))) ? 1 : 0;
|
||||
// Use palette lookup for proper black/white mapping
|
||||
lum = paletteLum[palIndex];
|
||||
packPixel(lum);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -42,6 +42,8 @@ class Bitmap {
|
||||
bool isTopDown() const { return topDown; }
|
||||
bool hasGreyscale() const { return bpp > 1; }
|
||||
int getRowBytes() const { return rowBytes; }
|
||||
bool is1Bit() const { return bpp == 1; }
|
||||
uint16_t getBpp() const { return bpp; }
|
||||
|
||||
private:
|
||||
static uint16_t readLE16(FsFile& f);
|
||||
|
||||
@@ -88,3 +88,19 @@ uint8_t quantize(int gray, int x, int y) {
|
||||
return quantizeSimple(gray);
|
||||
}
|
||||
}
|
||||
|
||||
// 1-bit noise dithering for fast home screen rendering
|
||||
// Uses hash-based noise for consistent dithering that works well at small sizes
|
||||
uint8_t quantize1bit(int gray, int x, int y) {
|
||||
gray = adjustPixel(gray);
|
||||
|
||||
// Generate noise threshold using integer hash (no regular pattern to alias)
|
||||
uint32_t hash = static_cast<uint32_t>(x) * 374761393u + static_cast<uint32_t>(y) * 668265263u;
|
||||
hash = (hash ^ (hash >> 13)) * 1274126177u;
|
||||
const int threshold = static_cast<int>(hash >> 24); // 0-255
|
||||
|
||||
// Simple threshold with noise: gray >= (128 + noise offset) -> white
|
||||
// The noise adds variation around the 128 midpoint
|
||||
const int adjustedThreshold = 128 + ((threshold - 128) / 2); // Range: 64-192
|
||||
return (gray >= adjustedThreshold) ? 1 : 0;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,89 @@
|
||||
// Helper functions
|
||||
uint8_t quantize(int gray, int x, int y);
|
||||
uint8_t quantizeSimple(int gray);
|
||||
uint8_t quantize1bit(int gray, int x, int y);
|
||||
int adjustPixel(int gray);
|
||||
|
||||
// 1-bit Atkinson dithering - better quality than noise dithering for thumbnails
|
||||
// Error distribution pattern (same as 2-bit but quantizes to 2 levels):
|
||||
// X 1/8 1/8
|
||||
// 1/8 1/8 1/8
|
||||
// 1/8
|
||||
class Atkinson1BitDitherer {
|
||||
public:
|
||||
explicit Atkinson1BitDitherer(int width) : width(width) {
|
||||
errorRow0 = new int16_t[width + 4](); // Current row
|
||||
errorRow1 = new int16_t[width + 4](); // Next row
|
||||
errorRow2 = new int16_t[width + 4](); // Row after next
|
||||
}
|
||||
|
||||
~Atkinson1BitDitherer() {
|
||||
delete[] errorRow0;
|
||||
delete[] errorRow1;
|
||||
delete[] errorRow2;
|
||||
}
|
||||
|
||||
// EXPLICITLY DELETE THE COPY CONSTRUCTOR
|
||||
Atkinson1BitDitherer(const Atkinson1BitDitherer& other) = delete;
|
||||
|
||||
// EXPLICITLY DELETE THE COPY ASSIGNMENT OPERATOR
|
||||
Atkinson1BitDitherer& operator=(const Atkinson1BitDitherer& other) = delete;
|
||||
|
||||
uint8_t processPixel(int gray, int x) {
|
||||
// Apply brightness/contrast/gamma adjustments
|
||||
gray = adjustPixel(gray);
|
||||
|
||||
// Add accumulated error
|
||||
int adjusted = gray + errorRow0[x + 2];
|
||||
if (adjusted < 0) adjusted = 0;
|
||||
if (adjusted > 255) adjusted = 255;
|
||||
|
||||
// Quantize to 2 levels (1-bit): 0 = black, 1 = white
|
||||
uint8_t quantized;
|
||||
int quantizedValue;
|
||||
if (adjusted < 128) {
|
||||
quantized = 0;
|
||||
quantizedValue = 0;
|
||||
} else {
|
||||
quantized = 1;
|
||||
quantizedValue = 255;
|
||||
}
|
||||
|
||||
// Calculate error (only distribute 6/8 = 75%)
|
||||
int error = (adjusted - quantizedValue) >> 3; // error/8
|
||||
|
||||
// Distribute 1/8 to each of 6 neighbors
|
||||
errorRow0[x + 3] += error; // Right
|
||||
errorRow0[x + 4] += error; // Right+1
|
||||
errorRow1[x + 1] += error; // Bottom-left
|
||||
errorRow1[x + 2] += error; // Bottom
|
||||
errorRow1[x + 3] += error; // Bottom-right
|
||||
errorRow2[x + 2] += error; // Two rows down
|
||||
|
||||
return quantized;
|
||||
}
|
||||
|
||||
void nextRow() {
|
||||
int16_t* temp = errorRow0;
|
||||
errorRow0 = errorRow1;
|
||||
errorRow1 = errorRow2;
|
||||
errorRow2 = temp;
|
||||
memset(errorRow2, 0, (width + 4) * sizeof(int16_t));
|
||||
}
|
||||
|
||||
void reset() {
|
||||
memset(errorRow0, 0, (width + 4) * sizeof(int16_t));
|
||||
memset(errorRow1, 0, (width + 4) * sizeof(int16_t));
|
||||
memset(errorRow2, 0, (width + 4) * sizeof(int16_t));
|
||||
}
|
||||
|
||||
private:
|
||||
int width;
|
||||
int16_t* errorRow0;
|
||||
int16_t* errorRow1;
|
||||
int16_t* errorRow2;
|
||||
};
|
||||
|
||||
// Atkinson dithering - distributes only 6/8 (75%) of error for cleaner results
|
||||
// Error distribution pattern:
|
||||
// X 1/8 1/8
|
||||
|
||||
@@ -154,6 +154,12 @@ void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, co
|
||||
|
||||
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);
|
||||
@@ -195,6 +201,9 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
||||
if (screenY >= getScreenHeight()) {
|
||||
break;
|
||||
}
|
||||
if (screenY < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
||||
Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(), bmpY);
|
||||
@@ -217,6 +226,9 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
||||
if (screenX >= getScreenWidth()) {
|
||||
break;
|
||||
}
|
||||
if (screenX < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
||||
|
||||
@@ -234,6 +246,143 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
||||
free(rowBytes);
|
||||
}
|
||||
|
||||
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)
|
||||
// val < 3 means black pixel (draw it)
|
||||
if (val < 3) {
|
||||
drawPixel(screenX, screenY, true);
|
||||
}
|
||||
// White pixels (val == 3) are not drawn (leave background)
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
|
||||
|
||||
void GfxRenderer::invertScreen() const {
|
||||
|
||||
@@ -68,6 +68,8 @@ class GfxRenderer {
|
||||
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,
|
||||
float cropY = 0) const;
|
||||
void drawBitmap1Bit(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight) const;
|
||||
void fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state = true) const;
|
||||
|
||||
// Text
|
||||
int getTextWidth(int fontId, const char* text, EpdFontFamily::Style style = EpdFontFamily::REGULAR) const;
|
||||
@@ -97,8 +99,8 @@ class GfxRenderer {
|
||||
void copyGrayscaleLsbBuffers() const;
|
||||
void copyGrayscaleMsbBuffers() const;
|
||||
void displayGrayBuffer() const;
|
||||
bool storeBwBuffer(); // Returns true if buffer was stored successfully
|
||||
void restoreBwBuffer();
|
||||
bool storeBwBuffer(); // Returns true if buffer was stored successfully
|
||||
void restoreBwBuffer(); // Restore and free the stored buffer
|
||||
void cleanupGrayscaleWithFrameBuffer() const;
|
||||
|
||||
// Low level functions
|
||||
|
||||
Reference in New Issue
Block a user