feat: Add 4bit bmp support (#944)
## Summary * What is the goal of this PR? - Allow users to create custom sleep screen images with standard tools (ImageMagick, GIMP, etc.) that render cleanly on the e-ink display without dithering artifacts. Previously, avoiding dithering required non-standard 2-bit BMPs that no standard image editor can produce. ( see issue #931 ) * What changes are included? - Add 4-bit BMP format support to Bitmap.cpp (standard format, widely supported by image tools) - Auto-detect "native palette" images: if a BMP has ≤4 palette entries and all luminances map within ±21 of the display's native gray levels (0, 85, 170, 255), skip dithering entirely and direct-map pixels - Clarify pixel processing strategy with three distinct paths: error-diffusion dithering, simple quantization, or direct mapping - Add scripts/generate_test_bmps.py for generating test images across all supported BMP formats ## Additional Context * The e-ink display has 4 native gray levels. When a BMP already uses exactly those levels, dithering adds noise to what should be clean output. The native palette detection uses a ±21 tolerance (~10%) to handle slight rounding from color space conversions in image tools. Users can now create a 4-color grayscale BMP with (imagemagic example): ``` convert input.png -colorspace Gray -colors 4 -depth ``` --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _** YES**_
This commit is contained in:
@@ -4,10 +4,12 @@
|
||||
#include <cstring>
|
||||
|
||||
// ============================================================================
|
||||
// IMAGE PROCESSING OPTIONS - Toggle these to test different configurations
|
||||
// IMAGE PROCESSING OPTIONS
|
||||
// ============================================================================
|
||||
// Note: For cover images, dithering is done in JpegToBmpConverter.cpp
|
||||
// This file handles BMP reading - use simple quantization to avoid double-dithering
|
||||
// Dithering is applied when converting high-color BMPs to the display's native
|
||||
// 2-bit (4-level) grayscale. Images whose palette entries all map to native
|
||||
// gray levels (0, 85, 170, 255 ±21) are mapped directly without dithering.
|
||||
// For cover images, dithering is done in JpegToBmpConverter.cpp instead.
|
||||
constexpr bool USE_ATKINSON = true; // Use Atkinson dithering instead of Floyd-Steinberg
|
||||
// ============================================================================
|
||||
|
||||
@@ -57,7 +59,7 @@ const char* Bitmap::errorToString(BmpReaderError err) {
|
||||
case BmpReaderError::BadPlanes:
|
||||
return "BadPlanes (!= 1)";
|
||||
case BmpReaderError::UnsupportedBpp:
|
||||
return "UnsupportedBpp (expected 1, 2, 8, 24, or 32)";
|
||||
return "UnsupportedBpp (expected 1, 2, 4, 8, 24, or 32)";
|
||||
case BmpReaderError::UnsupportedCompression:
|
||||
return "UnsupportedCompression (expected BI_RGB or BI_BITFIELDS for 32bpp)";
|
||||
case BmpReaderError::BadDimensions:
|
||||
@@ -103,7 +105,7 @@ BmpReaderError Bitmap::parseHeaders() {
|
||||
const uint16_t planes = readLE16(file);
|
||||
bpp = readLE16(file);
|
||||
const uint32_t comp = readLE32(file);
|
||||
const bool validBpp = bpp == 1 || bpp == 2 || bpp == 8 || bpp == 24 || bpp == 32;
|
||||
const bool validBpp = bpp == 1 || bpp == 2 || bpp == 4 || bpp == 8 || bpp == 24 || bpp == 32;
|
||||
|
||||
if (planes != 1) return BmpReaderError::BadPlanes;
|
||||
if (!validBpp) return BmpReaderError::UnsupportedBpp;
|
||||
@@ -111,7 +113,9 @@ BmpReaderError Bitmap::parseHeaders() {
|
||||
if (!(comp == 0 || (bpp == 32 && comp == 3))) return BmpReaderError::UnsupportedCompression;
|
||||
|
||||
file.seekCur(12); // biSizeImage, biXPelsPerMeter, biYPelsPerMeter
|
||||
const uint32_t colorsUsed = readLE32(file);
|
||||
colorsUsed = readLE32(file);
|
||||
// BMP spec: colorsUsed==0 means default (2^bpp for paletted formats)
|
||||
if (colorsUsed == 0 && bpp <= 8) colorsUsed = 1u << bpp;
|
||||
if (colorsUsed > 256u) return BmpReaderError::PaletteTooLarge;
|
||||
file.seekCur(4); // biClrImportant
|
||||
|
||||
@@ -140,9 +144,29 @@ BmpReaderError Bitmap::parseHeaders() {
|
||||
return BmpReaderError::SeekPixelDataFailed;
|
||||
}
|
||||
|
||||
// Create ditherer if enabled (only for 2-bit output)
|
||||
// Use OUTPUT dimensions for dithering (after prescaling)
|
||||
if (bpp > 2 && dithering) {
|
||||
// Check if palette luminances map cleanly to the display's 4 native gray levels.
|
||||
// Native levels are 0, 85, 170, 255 — i.e. values where (lum >> 6) is lossless.
|
||||
// If all palette entries are near a native level, we can skip dithering entirely.
|
||||
nativePalette = bpp <= 2; // 1-bit and 2-bit are always native
|
||||
if (!nativePalette && colorsUsed > 0) {
|
||||
nativePalette = true;
|
||||
for (uint32_t i = 0; i < colorsUsed; i++) {
|
||||
const uint8_t lum = paletteLum[i];
|
||||
const uint8_t level = lum >> 6; // quantize to 0-3
|
||||
const uint8_t reconstructed = level * 85; // back to 0, 85, 170, 255
|
||||
if (lum > reconstructed + 21 || lum + 21 < reconstructed) {
|
||||
nativePalette = false; // luminance is too far from any native level
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Decide pixel processing strategy:
|
||||
// - Native palette → direct mapping, no processing needed
|
||||
// - High-color + dithering enabled → error-diffusion dithering (Atkinson or Floyd-Steinberg)
|
||||
// - High-color + dithering disabled → simple quantization (no error diffusion)
|
||||
const bool highColor = !nativePalette;
|
||||
if (highColor && dithering) {
|
||||
if (USE_ATKINSON) {
|
||||
atkinsonDitherer = new AtkinsonDitherer(width);
|
||||
} else {
|
||||
@@ -173,12 +197,12 @@ BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
|
||||
} else if (fsDitherer) {
|
||||
color = fsDitherer->processPixel(adjustPixel(lum), currentX);
|
||||
} else {
|
||||
if (bpp > 2) {
|
||||
// Simple quantization or noise dithering
|
||||
color = quantize(adjustPixel(lum), currentX, prevRowY);
|
||||
if (nativePalette) {
|
||||
// Palette matches native gray levels: direct mapping (still apply brightness/contrast/gamma)
|
||||
color = static_cast<uint8_t>(adjustPixel(lum) >> 6);
|
||||
} else {
|
||||
// do not quantize 2bpp image
|
||||
color = static_cast<uint8_t>(lum >> 6);
|
||||
// Non-native palette with dithering disabled: simple quantization
|
||||
color = quantize(adjustPixel(lum), currentX, prevRowY);
|
||||
}
|
||||
}
|
||||
currentOutByte |= (color << bitShift);
|
||||
@@ -219,6 +243,13 @@ BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
for (int x = 0; x < width; x++) {
|
||||
const uint8_t nibble = (x & 1) ? (rowBuffer[x >> 1] & 0x0F) : (rowBuffer[x >> 1] >> 4);
|
||||
packPixel(paletteLum[nibble]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
for (int x = 0; x < width; x++) {
|
||||
lum = paletteLum[(rowBuffer[x >> 2] >> (6 - ((x & 3) * 2))) & 0x03];
|
||||
|
||||
@@ -56,10 +56,12 @@ class Bitmap {
|
||||
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] = {};
|
||||
|
||||
// Floyd-Steinberg dithering state (mutable for const methods)
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user