Extended bitmap viewer by option to togglie between grayscale and bw mode
This commit is contained in:
@@ -60,7 +60,11 @@ struct RenderConfig {
|
||||
ImageDitherMode ditherMode = ImageDitherMode::Bayer;
|
||||
bool performanceMode = false;
|
||||
bool useExactDimensions = false; // If true, use maxWidth/maxHeight as exact output size (no recalculation)
|
||||
std::string cachePath; // If non-empty, decoder will write pixel cache to this path
|
||||
// If true, the decoder uses a 1-bit Atkinson dither and emits only the values 0/3 — suitable for
|
||||
// pure black-and-white display (no grayscale planes). The 4-level dither path produces values 1/2
|
||||
// which the BW DirectPixelWriter collapses to black, making mid-grays render very dark.
|
||||
bool monochromeOutput = false;
|
||||
std::string cachePath; // If non-empty, decoder will write pixel cache to this path
|
||||
};
|
||||
|
||||
class ImageToFramebufferDecoder {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "JpegToFramebufferConverter.h"
|
||||
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
#include <BitmapHelpers.h>
|
||||
#endif
|
||||
#include <FsHelpers.h>
|
||||
#include <GfxRenderer.h>
|
||||
#include <HalStorage.h>
|
||||
@@ -42,6 +40,11 @@ struct JpegContext {
|
||||
PixelCache cache;
|
||||
bool caching;
|
||||
|
||||
// See PngContext for the rationale: monochromeOutput requests a 1-bit Atkinson dither
|
||||
// emitting only 0/3 so the BW DirectPixelWriter (`pixelValue < 3` rule) maps cleanly.
|
||||
int oneBitDitherRow;
|
||||
Atkinson1BitDitherer* atkinson1BitDitherer;
|
||||
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
int currentDitherRow;
|
||||
AtkinsonDitherer* atkinsonDitherer;
|
||||
@@ -59,7 +62,9 @@ struct JpegContext {
|
||||
dstHeight(0),
|
||||
fineScaleFP(1 << 16),
|
||||
invScaleFP(1 << 16),
|
||||
caching(false)
|
||||
caching(false),
|
||||
oneBitDitherRow(-1),
|
||||
atkinson1BitDitherer(nullptr)
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
,
|
||||
currentDitherRow(-1),
|
||||
@@ -69,14 +74,32 @@ struct JpegContext {
|
||||
{
|
||||
}
|
||||
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
~JpegContext() {
|
||||
delete atkinson1BitDitherer;
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
delete atkinsonDitherer;
|
||||
delete diffusedBayerDitherer;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
// Advance the 1-bit Atkinson ditherer to the requested destination row.
|
||||
// Handles non-monotonic row walks (block-based JPEG decode) by reset+replay.
|
||||
void prepareOneBitDitherRow(JpegContext& ctx, int dstY) {
|
||||
if (!ctx.atkinson1BitDitherer) return;
|
||||
|
||||
if (ctx.oneBitDitherRow == -1 || dstY < ctx.oneBitDitherRow) {
|
||||
ctx.atkinson1BitDitherer->reset();
|
||||
ctx.oneBitDitherRow = dstY;
|
||||
return;
|
||||
}
|
||||
|
||||
while (ctx.oneBitDitherRow < dstY) {
|
||||
ctx.atkinson1BitDitherer->nextRow();
|
||||
ctx.oneBitDitherRow++;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
void prepareDitherRow(JpegContext& ctx, int dstY) {
|
||||
if (!ctx.config || !ctx.config->useDithering) return;
|
||||
@@ -96,6 +119,10 @@ void prepareDitherRow(JpegContext& ctx, int dstY) {
|
||||
}
|
||||
|
||||
uint8_t ditherGray(JpegContext& ctx, uint8_t gray, int localX, int outX, int outY) {
|
||||
if (ctx.atkinson1BitDitherer) {
|
||||
return ctx.atkinson1BitDitherer->processPixel(gray, localX) ? 3 : 0;
|
||||
}
|
||||
|
||||
if (!ctx.config || !ctx.config->useDithering) {
|
||||
return quantizeGray4Level(gray);
|
||||
}
|
||||
@@ -121,7 +148,9 @@ uint8_t ditherGray(JpegContext& ctx, uint8_t gray, int localX, int outX, int out
|
||||
}
|
||||
#else
|
||||
uint8_t ditherGray(JpegContext& ctx, uint8_t gray, int localX, int outX, int outY) {
|
||||
(void)ctx;
|
||||
if (ctx.atkinson1BitDitherer) {
|
||||
return ctx.atkinson1BitDitherer->processPixel(gray, localX) ? 3 : 0;
|
||||
}
|
||||
(void)localX;
|
||||
return applyBayerDither4Level(gray, outX, outY);
|
||||
}
|
||||
@@ -254,6 +283,7 @@ int jpegDrawCallback(JPEGDRAW* pDraw) {
|
||||
if (fineScaleFP == FP_ONE) {
|
||||
for (int dstY = dstYStart; dstY < dstYEnd; dstY++) {
|
||||
const int outY = cfgY + dstY;
|
||||
prepareOneBitDitherRow(*ctx, dstY);
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
prepareDitherRow(*ctx, dstY);
|
||||
#endif
|
||||
@@ -285,6 +315,7 @@ int jpegDrawCallback(JPEGDRAW* pDraw) {
|
||||
|
||||
for (int dstY = dstYStart; dstY < dstYEnd; dstY++) {
|
||||
const int outY = cfgY + dstY;
|
||||
prepareOneBitDitherRow(*ctx, dstY);
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
prepareDitherRow(*ctx, dstY);
|
||||
#endif
|
||||
@@ -367,6 +398,7 @@ int jpegDrawCallback(JPEGDRAW* pDraw) {
|
||||
// === Nearest-neighbor (downscale: fineScale < 1.0) ===
|
||||
for (int dstY = dstYStart; dstY < dstYEnd; dstY++) {
|
||||
const int outY = cfgY + dstY;
|
||||
prepareOneBitDitherRow(*ctx, dstY);
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
prepareDitherRow(*ctx, dstY);
|
||||
#endif
|
||||
@@ -531,7 +563,16 @@ bool JpegToFramebufferConverter::decodeToFramebuffer(const std::string& imagePat
|
||||
}
|
||||
}
|
||||
|
||||
if (config.useDithering) {
|
||||
// See PngToFramebufferConverter for rationale: BW-only display needs a 1-bit
|
||||
// dither so mid-grays don't collapse to black under DirectPixelWriter's `< 3` rule.
|
||||
if (config.monochromeOutput) {
|
||||
ctx.atkinson1BitDitherer = new (std::nothrow) Atkinson1BitDitherer(destWidth);
|
||||
if (!ctx.atkinson1BitDitherer) {
|
||||
LOG_ERR("JPG", "Failed to allocate 1-bit Atkinson ditherer, falling back to 4-level dither");
|
||||
}
|
||||
}
|
||||
|
||||
if (config.useDithering && !ctx.atkinson1BitDitherer) {
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
switch (config.ditherMode) {
|
||||
case ImageDitherMode::Atkinson:
|
||||
|
||||
@@ -38,12 +38,11 @@ struct PngContext {
|
||||
|
||||
uint8_t* grayLineBuffer;
|
||||
|
||||
// When the renderer is in BW mode the framebuffer is 1 bpp and the
|
||||
// DirectPixelWriter collapses any value < 3 to black. The 4-level dither
|
||||
// path therefore turns mid-grays into solid black. In that case we run a
|
||||
// proper 1-bit Atkinson dither (matching PngToBmpConverter's BW path) and
|
||||
// emit only values 0 or 3, which round-trip cleanly through the BW writer.
|
||||
bool renderModeIsBW;
|
||||
// When the caller requests monochrome output (RenderConfig::monochromeOutput),
|
||||
// we run a proper 1-bit Atkinson dither (matching PngToBmpConverter's BW path)
|
||||
// and emit only values 0 or 3, which round-trip cleanly through the BW writer's
|
||||
// `pixelValue < 3` rule. The 4-level dither path collapses mid-grays to solid
|
||||
// black under that rule.
|
||||
int oneBitDitherRow;
|
||||
Atkinson1BitDitherer* atkinson1BitDitherer;
|
||||
|
||||
@@ -66,7 +65,6 @@ struct PngContext {
|
||||
lastDstY(-1),
|
||||
caching(false),
|
||||
grayLineBuffer(nullptr),
|
||||
renderModeIsBW(false),
|
||||
oneBitDitherRow(-1),
|
||||
atkinson1BitDitherer(nullptr)
|
||||
#ifdef ENABLE_IMAGE_DITHERING_EXTENSION
|
||||
@@ -494,17 +492,13 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath
|
||||
}
|
||||
}
|
||||
|
||||
// When the renderer is in BW mode, use a 1-bit Atkinson ditherer instead of
|
||||
// the 4-level paths below. The 4-level dither produces values 1-2 for mid
|
||||
// grays, which DirectPixelWriter then collapses to black under its `< 3`
|
||||
// BW rule, making images render very dark. The 1-bit ditherer emits only
|
||||
// 0 or 3 so the BW writer maps cleanly to black/white. Caching still uses
|
||||
// the 2-bit cache file format, but caching is disabled in this path
|
||||
// (BmpViewerActivity passes an empty cachePath).
|
||||
ctx.renderModeIsBW = (renderer.getRenderMode() == GfxRenderer::BW);
|
||||
LOG_DBG("PNG", "Render mode at decode: %d (BW=%d) -> 1bit dither=%d", (int)renderer.getRenderMode(),
|
||||
(int)GfxRenderer::BW, ctx.renderModeIsBW ? 1 : 0);
|
||||
if (ctx.renderModeIsBW) {
|
||||
// When the caller explicitly requests monochrome output, use a 1-bit Atkinson
|
||||
// ditherer instead of the 4-level paths below. The 4-level dither produces
|
||||
// values 1-2 for mid grays, which DirectPixelWriter then collapses to black
|
||||
// under its `< 3` BW rule, making images render very dark in BW-only mode.
|
||||
// The 1-bit ditherer emits only 0 or 3 so the BW writer maps cleanly to
|
||||
// black/white.
|
||||
if (config.monochromeOutput) {
|
||||
ctx.atkinson1BitDitherer = new (std::nothrow) Atkinson1BitDitherer(ctx.dstWidth);
|
||||
if (!ctx.atkinson1BitDitherer) {
|
||||
LOG_ERR("PNG", "Failed to allocate 1-bit Atkinson ditherer, falling back to 4-level dither");
|
||||
|
||||
Reference in New Issue
Block a user