2025-12-19 08:45:14 +11:00
|
|
|
#pragma once
|
|
|
|
|
|
2025-12-30 15:09:30 +10:00
|
|
|
#include <SdFat.h>
|
2025-12-19 08:45:14 +11:00
|
|
|
|
|
|
|
|
enum class BmpReaderError : uint8_t {
|
|
|
|
|
Ok = 0,
|
|
|
|
|
FileInvalid,
|
|
|
|
|
SeekStartFailed,
|
|
|
|
|
|
|
|
|
|
NotBMP,
|
|
|
|
|
DIBTooSmall,
|
|
|
|
|
|
|
|
|
|
BadPlanes,
|
|
|
|
|
UnsupportedBpp,
|
|
|
|
|
UnsupportedCompression,
|
|
|
|
|
|
|
|
|
|
BadDimensions,
|
2025-12-28 08:38:14 +09:00
|
|
|
ImageTooLarge,
|
2025-12-19 08:45:14 +11:00
|
|
|
PaletteTooLarge,
|
|
|
|
|
|
|
|
|
|
SeekPixelDataFailed,
|
|
|
|
|
BufferTooSmall,
|
|
|
|
|
OomRowBuffer,
|
|
|
|
|
ShortReadRow,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Bitmap {
|
|
|
|
|
public:
|
|
|
|
|
static const char* errorToString(BmpReaderError err);
|
|
|
|
|
|
2025-12-30 15:09:30 +10:00
|
|
|
explicit Bitmap(FsFile& file) : file(file) {}
|
2025-12-28 08:38:14 +09:00
|
|
|
~Bitmap();
|
2025-12-19 08:45:14 +11:00
|
|
|
BmpReaderError parseHeaders();
|
2025-12-28 08:38:14 +09:00
|
|
|
BmpReaderError readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const;
|
2025-12-19 08:45:14 +11:00
|
|
|
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:
|
2025-12-30 15:09:30 +10:00
|
|
|
static uint16_t readLE16(FsFile& f);
|
|
|
|
|
static uint32_t readLE32(FsFile& f);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
2025-12-30 15:09:30 +10:00
|
|
|
FsFile& file;
|
2025-12-19 08:45:14 +11:00
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
|
|
|
|
bool topDown = false;
|
|
|
|
|
uint32_t bfOffBits = 0;
|
|
|
|
|
uint16_t bpp = 0;
|
|
|
|
|
int rowBytes = 0;
|
|
|
|
|
uint8_t paletteLum[256] = {};
|
2025-12-28 08:38:14 +09:00
|
|
|
|
|
|
|
|
// 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
|
2025-12-19 08:45:14 +11:00
|
|
|
};
|