Refactor readRow to readNextRow (we anyways ignored rowY param)

This commit is contained in:
Jonas Diemer 2026-01-04 14:15:33 +01:00
parent 59e3ac28f1
commit 3de07b925c
3 changed files with 9 additions and 15 deletions

View File

@ -250,35 +250,29 @@ BmpReaderError Bitmap::parseHeaders() {
delete[] errorNextRow; delete[] errorNextRow;
errorCurRow = new int16_t[width + 2](); // +2 for boundary handling errorCurRow = new int16_t[width + 2](); // +2 for boundary handling
errorNextRow = new int16_t[width + 2](); errorNextRow = new int16_t[width + 2]();
lastRowY = -1; prevRowY = -1;
} }
return BmpReaderError::Ok; return BmpReaderError::Ok;
} }
// packed 2bpp output, 0 = black, 1 = dark gray, 2 = light gray, 3 = white // 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::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
BmpReaderError Bitmap::readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const {
// Note: rowBuffer should be pre-allocated by the caller to size 'rowBytes' // Note: rowBuffer should be pre-allocated by the caller to size 'rowBytes'
if (file.read(rowBuffer, rowBytes) != rowBytes) return BmpReaderError::ShortReadRow; if (file.read(rowBuffer, rowBytes) != rowBytes) return BmpReaderError::ShortReadRow;
// Handle Floyd-Steinberg error buffer progression // Handle Floyd-Steinberg error buffer progression
const bool useFS = USE_FLOYD_STEINBERG && errorCurRow && errorNextRow; const bool useFS = USE_FLOYD_STEINBERG && errorCurRow && errorNextRow;
if (useFS) { if (useFS) {
// Check if we need to advance to next row (or reset if jumping) if (prevRowY != -1) {
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) {
// Sequential access - swap buffers // Sequential access - swap buffers
int16_t* temp = errorCurRow; int16_t* temp = errorCurRow;
errorCurRow = errorNextRow; errorCurRow = errorNextRow;
errorNextRow = temp; errorNextRow = temp;
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t)); memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
} }
lastRowY = rowY;
} }
prevRowY += 1;
uint8_t* outPtr = data; uint8_t* outPtr = data;
uint8_t currentOutByte = 0; 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); color = quantizeFloydSteinberg(lum, currentX, width, errorCurRow, errorNextRow, false);
} else { } else {
// Simple quantization or noise dithering // Simple quantization or noise dithering
color = quantize(lum, currentX, rowY); color = quantize(lum, currentX, prevRowY);
} }
currentOutByte |= (color << bitShift); currentOutByte |= (color << bitShift);
if (bitShift == 0) { if (bitShift == 0) {
@ -366,7 +360,7 @@ BmpReaderError Bitmap::rewindToData() const {
if (USE_FLOYD_STEINBERG && errorCurRow && errorNextRow) { if (USE_FLOYD_STEINBERG && errorCurRow && errorNextRow) {
memset(errorCurRow, 0, (width + 2) * sizeof(int16_t)); memset(errorCurRow, 0, (width + 2) * sizeof(int16_t));
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t)); memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
lastRowY = -1; prevRowY = -1;
} }
return BmpReaderError::Ok; return BmpReaderError::Ok;

View File

@ -31,7 +31,7 @@ class Bitmap {
explicit Bitmap(FsFile& file) : file(file) {} explicit Bitmap(FsFile& file) : file(file) {}
~Bitmap(); ~Bitmap();
BmpReaderError parseHeaders(); 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; BmpReaderError rewindToData() const;
int getWidth() const { return width; } int getWidth() const { return width; }
int getHeight() const { return height; } int getHeight() const { return height; }
@ -55,5 +55,5 @@ class Bitmap {
// Floyd-Steinberg dithering state (mutable for const methods) // Floyd-Steinberg dithering state (mutable for const methods)
mutable int16_t* errorCurRow = nullptr; mutable int16_t* errorCurRow = nullptr;
mutable int16_t* errorNextRow = 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
}; };

View File

@ -196,7 +196,7 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
break; 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); Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(), bmpY);
free(outputRow); free(outputRow);
free(rowBytes); free(rowBytes);