From 3de07b925c8af5c58297524daa9b5230deff7142 Mon Sep 17 00:00:00 2001 From: Jonas Diemer Date: Sun, 4 Jan 2026 14:15:33 +0100 Subject: [PATCH] Refactor readRow to readNextRow (we anyways ignored rowY param) --- lib/GfxRenderer/Bitmap.cpp | 18 ++++++------------ lib/GfxRenderer/Bitmap.h | 4 ++-- lib/GfxRenderer/GfxRenderer.cpp | 2 +- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/GfxRenderer/Bitmap.cpp b/lib/GfxRenderer/Bitmap.cpp index 4dbe8a25..8cc8a5f3 100644 --- a/lib/GfxRenderer/Bitmap.cpp +++ b/lib/GfxRenderer/Bitmap.cpp @@ -250,35 +250,29 @@ BmpReaderError Bitmap::parseHeaders() { delete[] errorNextRow; errorCurRow = new int16_t[width + 2](); // +2 for boundary handling errorNextRow = new int16_t[width + 2](); - lastRowY = -1; + prevRowY = -1; } return BmpReaderError::Ok; } // packed 2bpp output, 0 = black, 1 = dark gray, 2 = light gray, 3 = white -// TODO: This seems to only work sequentially, rowY is mostly ignored -BmpReaderError Bitmap::readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const { +BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const { // Note: rowBuffer should be pre-allocated by the caller to size 'rowBytes' if (file.read(rowBuffer, rowBytes) != rowBytes) return BmpReaderError::ShortReadRow; // Handle Floyd-Steinberg error buffer progression const bool useFS = USE_FLOYD_STEINBERG && errorCurRow && errorNextRow; if (useFS) { - // Check if we need to advance to next row (or reset if jumping) - if (rowY != lastRowY + 1 && rowY != 0) { - // Non-sequential row access - reset error buffers - memset(errorCurRow, 0, (width + 2) * sizeof(int16_t)); - memset(errorNextRow, 0, (width + 2) * sizeof(int16_t)); - } else if (rowY > 0) { + if (prevRowY != -1) { // Sequential access - swap buffers int16_t* temp = errorCurRow; errorCurRow = errorNextRow; errorNextRow = temp; memset(errorNextRow, 0, (width + 2) * sizeof(int16_t)); } - lastRowY = rowY; } + prevRowY += 1; uint8_t* outPtr = data; uint8_t currentOutByte = 0; @@ -293,7 +287,7 @@ BmpReaderError Bitmap::readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) cons color = quantizeFloydSteinberg(lum, currentX, width, errorCurRow, errorNextRow, false); } else { // Simple quantization or noise dithering - color = quantize(lum, currentX, rowY); + color = quantize(lum, currentX, prevRowY); } currentOutByte |= (color << bitShift); if (bitShift == 0) { @@ -366,7 +360,7 @@ BmpReaderError Bitmap::rewindToData() const { if (USE_FLOYD_STEINBERG && errorCurRow && errorNextRow) { memset(errorCurRow, 0, (width + 2) * sizeof(int16_t)); memset(errorNextRow, 0, (width + 2) * sizeof(int16_t)); - lastRowY = -1; + prevRowY = -1; } return BmpReaderError::Ok; diff --git a/lib/GfxRenderer/Bitmap.h b/lib/GfxRenderer/Bitmap.h index 7e799647..a3f2e00c 100644 --- a/lib/GfxRenderer/Bitmap.h +++ b/lib/GfxRenderer/Bitmap.h @@ -31,7 +31,7 @@ class Bitmap { explicit Bitmap(FsFile& file) : file(file) {} ~Bitmap(); BmpReaderError parseHeaders(); - BmpReaderError readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const; + BmpReaderError readNextRow(uint8_t* data, uint8_t* rowBuffer) const; BmpReaderError rewindToData() const; int getWidth() const { return width; } int getHeight() const { return height; } @@ -55,5 +55,5 @@ class Bitmap { // Floyd-Steinberg dithering state (mutable for const methods) mutable int16_t* errorCurRow = nullptr; mutable int16_t* errorNextRow = nullptr; - mutable int lastRowY = -1; // Track row progression for error propagation + mutable int prevRowY = -1; // Track row progression for error propagation }; diff --git a/lib/GfxRenderer/GfxRenderer.cpp b/lib/GfxRenderer/GfxRenderer.cpp index 1750b3f3..cc1288a7 100644 --- a/lib/GfxRenderer/GfxRenderer.cpp +++ b/lib/GfxRenderer/GfxRenderer.cpp @@ -196,7 +196,7 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con break; } - if (bitmap.readRow(outputRow, rowBytes, bmpY) != BmpReaderError::Ok) { + 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);