mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 22:57:50 +03:00
## Summary * **What is the goal of this PR?** Replace simple threshold-based grayscale quantization with ordered dithering using a 4x4 Bayer matrix. This eliminates color banding artifacts and produces smoother gradients on e-ink display. * **What changes are included?** - Add 4x4 Bayer dithering matrix for 16-level threshold patterns - Modify `grayscaleTo2Bit()` function to accept pixel coordinates and apply position-based dithering - Replace simple `grayscale >> 6` threshold with ordered dithering algorithm that produces smoother gradients ## Additional Context * Bayer matrix approach: The 4x4 Bayer matrix creates a repeating pattern that distributes quantization error spatially, effectively simulating 16 levels of gray using only 4 actual color levels (black, dark gray, light gray, white). * Cache invalidation: Existing cached `cover.bmp` files will need to be deleted to see the improved rendering, as the converter only runs when the cache is missing.
60 lines
1.3 KiB
C++
60 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <FS.h>
|
|
|
|
enum class BmpReaderError : uint8_t {
|
|
Ok = 0,
|
|
FileInvalid,
|
|
SeekStartFailed,
|
|
|
|
NotBMP,
|
|
DIBTooSmall,
|
|
|
|
BadPlanes,
|
|
UnsupportedBpp,
|
|
UnsupportedCompression,
|
|
|
|
BadDimensions,
|
|
ImageTooLarge,
|
|
PaletteTooLarge,
|
|
|
|
SeekPixelDataFailed,
|
|
BufferTooSmall,
|
|
OomRowBuffer,
|
|
ShortReadRow,
|
|
};
|
|
|
|
class Bitmap {
|
|
public:
|
|
static const char* errorToString(BmpReaderError err);
|
|
|
|
explicit Bitmap(File& file) : file(file) {}
|
|
~Bitmap();
|
|
BmpReaderError parseHeaders();
|
|
BmpReaderError readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const;
|
|
BmpReaderError rewindToData() const;
|
|
int getWidth() const { return width; }
|
|
int getHeight() const { return height; }
|
|
bool isTopDown() const { return topDown; }
|
|
bool hasGreyscale() const { return bpp > 1; }
|
|
int getRowBytes() const { return rowBytes; }
|
|
|
|
private:
|
|
static uint16_t readLE16(File& f);
|
|
static uint32_t readLE32(File& f);
|
|
|
|
File& file;
|
|
int width = 0;
|
|
int height = 0;
|
|
bool topDown = false;
|
|
uint32_t bfOffBits = 0;
|
|
uint16_t bpp = 0;
|
|
int rowBytes = 0;
|
|
uint8_t paletteLum[256] = {};
|
|
|
|
// 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
|
|
};
|