Something went wrong. Try again.
A fork of https://github.com/crosspoint-reader/crosspoint-reader
Something went wrong. Try again.
2.5 kB · 106 lines
C
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107#pragma once
#include <HalStorage.h>
#include <cstdint>
#include "BitmapHelpers.h"
#pragma pack(push, 1)struct BmpHeader { struct { uint16_t bfType; uint32_t bfSize; uint16_t bfReserved1; uint16_t bfReserved2; uint32_t bfOffBits; } fileHeader; struct { uint32_t biSize; int32_t biWidth; int32_t biHeight; uint16_t biPlanes; uint16_t biBitCount; uint32_t biCompression; uint32_t biSizeImage; int32_t biXPelsPerMeter; int32_t biYPelsPerMeter; uint32_t biClrUsed; uint32_t biClrImportant; } infoHeader; struct RgbQuad { uint8_t rgbBlue; uint8_t rgbGreen; uint8_t rgbRed; uint8_t rgbReserved; }; RgbQuad colors[2];};#pragma pack(pop)
enum class BmpReaderError : uint8_t { Ok = 0, FileInvalid, SeekStartFailed,
NotBMP, DIBTooSmall,
BadPlanes, UnsupportedBpp, UnsupportedCompression,
BadDimensions, ImageTooLarge, PaletteTooLarge,
SeekPixelDataFailed, BufferTooSmall, OomRowBuffer, ShortReadRow, OomDitherer,};
class Bitmap { public: static const char* errorToString(BmpReaderError err);
explicit Bitmap(HalFile& file, bool dithering = false, bool originalThresholds = false) : file(file), dithering(dithering), originalThresholds(originalThresholds) {} ~Bitmap(); BmpReaderError parseHeaders(); BmpReaderError readNextRow(uint8_t* data, uint8_t* rowBuffer) 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; } bool is1Bit() const { return bpp == 1; } uint16_t getBpp() const { return bpp; }
private: static uint16_t readLE16(HalFile& f); static uint32_t readLE32(HalFile& f);
HalFile& file; bool dithering = false; bool originalThresholds = false; int width = 0; int height = 0; bool topDown = false; uint32_t bfOffBits = 0; uint16_t bpp = 0; uint32_t colorsUsed = 0; bool nativePalette = false; // true if all palette entries map to native gray levels int rowBytes = 0; uint8_t paletteLum[256] = {};
// Dithering state (mutable for const methods) mutable int16_t* errorCurRow = nullptr; mutable int16_t* errorNextRow = nullptr; mutable int prevRowY = -1; // Track row progression for error propagation
mutable AtkinsonDitherer* atkinsonDitherer = nullptr; mutable FloydSteinbergDitherer* fsDitherer = nullptr;};