mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-08 08:37:38 +03:00
Compare commits
No commits in common. "9b40d1cb32e5ed62afd641f6ab4c5ffb6b801ccd" and "a2d32640f250574a3edb1028bc08486790cbeddf" have entirely different histories.
9b40d1cb32
...
a2d32640f2
@ -95,76 +95,46 @@ bool PngToFramebufferConverter::getDimensionsStatic(const std::string& imagePath
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert entire source line to grayscale with alpha blending to white background.
|
// Helper to get grayscale from PNG pixel data
|
||||||
// For indexed PNGs with tRNS chunk, alpha values are stored at palette[768] onwards.
|
static uint8_t getGrayFromPixel(uint8_t* pPixels, int x, int pixelType, uint8_t* palette) {
|
||||||
// Processing the whole line at once improves cache locality and reduces per-pixel overhead.
|
|
||||||
static void convertLineToGray(uint8_t* pPixels, uint8_t* grayLine, int width, int pixelType, uint8_t* palette,
|
|
||||||
int hasAlpha) {
|
|
||||||
switch (pixelType) {
|
switch (pixelType) {
|
||||||
case PNG_PIXEL_GRAYSCALE:
|
case PNG_PIXEL_GRAYSCALE:
|
||||||
memcpy(grayLine, pPixels, width);
|
return pPixels[x];
|
||||||
break;
|
|
||||||
|
|
||||||
case PNG_PIXEL_TRUECOLOR:
|
case PNG_PIXEL_TRUECOLOR: {
|
||||||
for (int x = 0; x < width; x++) {
|
uint8_t* p = &pPixels[x * 3];
|
||||||
uint8_t* p = &pPixels[x * 3];
|
return (uint8_t)((p[0] * 77 + p[1] * 150 + p[2] * 29) >> 8);
|
||||||
grayLine[x] = (uint8_t)((p[0] * 77 + p[1] * 150 + p[2] * 29) >> 8);
|
}
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PNG_PIXEL_INDEXED:
|
case PNG_PIXEL_INDEXED: {
|
||||||
|
uint8_t paletteIndex = pPixels[x];
|
||||||
if (palette) {
|
if (palette) {
|
||||||
if (hasAlpha) {
|
uint8_t* p = &palette[paletteIndex * 3];
|
||||||
for (int x = 0; x < width; x++) {
|
return (uint8_t)((p[0] * 77 + p[1] * 150 + p[2] * 29) >> 8);
|
||||||
uint8_t idx = pPixels[x];
|
|
||||||
uint8_t* p = &palette[idx * 3];
|
|
||||||
uint8_t gray = (uint8_t)((p[0] * 77 + p[1] * 150 + p[2] * 29) >> 8);
|
|
||||||
uint8_t alpha = palette[768 + idx];
|
|
||||||
grayLine[x] = (uint8_t)((gray * alpha + 255 * (255 - alpha)) / 255);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
uint8_t* p = &palette[pPixels[x] * 3];
|
|
||||||
grayLine[x] = (uint8_t)((p[0] * 77 + p[1] * 150 + p[2] * 29) >> 8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
memcpy(grayLine, pPixels, width);
|
|
||||||
}
|
}
|
||||||
break;
|
return paletteIndex;
|
||||||
|
}
|
||||||
|
|
||||||
case PNG_PIXEL_GRAY_ALPHA:
|
case PNG_PIXEL_GRAY_ALPHA:
|
||||||
for (int x = 0; x < width; x++) {
|
return pPixels[x * 2];
|
||||||
uint8_t gray = pPixels[x * 2];
|
|
||||||
uint8_t alpha = pPixels[x * 2 + 1];
|
|
||||||
grayLine[x] = (uint8_t)((gray * alpha + 255 * (255 - alpha)) / 255);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PNG_PIXEL_TRUECOLOR_ALPHA:
|
case PNG_PIXEL_TRUECOLOR_ALPHA: {
|
||||||
for (int x = 0; x < width; x++) {
|
uint8_t* p = &pPixels[x * 4];
|
||||||
uint8_t* p = &pPixels[x * 4];
|
return (uint8_t)((p[0] * 77 + p[1] * 150 + p[2] * 29) >> 8);
|
||||||
uint8_t gray = (uint8_t)((p[0] * 77 + p[1] * 150 + p[2] * 29) >> 8);
|
}
|
||||||
uint8_t alpha = p[3];
|
|
||||||
grayLine[x] = (uint8_t)((gray * alpha + 255 * (255 - alpha)) / 255);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
memset(grayLine, 128, width);
|
return 128;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stack buffer for grayscale line conversion (max width from PNGdec)
|
|
||||||
static uint8_t grayLineBuffer[PNG_MAX_BUFFERED_PIXELS / 2];
|
|
||||||
|
|
||||||
int pngDrawCallback(PNGDRAW* pDraw) {
|
int pngDrawCallback(PNGDRAW* pDraw) {
|
||||||
PngContext* ctx = reinterpret_cast<PngContext*>(pDraw->pUser);
|
PngContext* ctx = reinterpret_cast<PngContext*>(pDraw->pUser);
|
||||||
if (!ctx || !ctx->config || !ctx->renderer) return 0;
|
if (!ctx || !ctx->config || !ctx->renderer) return 0;
|
||||||
|
|
||||||
int srcY = pDraw->y;
|
int srcY = pDraw->y;
|
||||||
int srcWidth = ctx->srcWidth;
|
uint8_t* pPixels = pDraw->pPixels;
|
||||||
|
int pixelType = pDraw->iPixelType;
|
||||||
|
|
||||||
// Calculate destination Y with scaling
|
// Calculate destination Y with scaling
|
||||||
int dstY = (int)(srcY * ctx->scale);
|
int dstY = (int)(srcY * ctx->scale);
|
||||||
@ -179,41 +149,26 @@ int pngDrawCallback(PNGDRAW* pDraw) {
|
|||||||
int outY = ctx->config->y + dstY;
|
int outY = ctx->config->y + dstY;
|
||||||
if (outY >= ctx->screenHeight) return 1;
|
if (outY >= ctx->screenHeight) return 1;
|
||||||
|
|
||||||
// Convert entire source line to grayscale (improves cache locality)
|
// Render scaled row using nearest-neighbor sampling
|
||||||
convertLineToGray(pDraw->pPixels, grayLineBuffer, srcWidth, pDraw->iPixelType, pDraw->pPalette, pDraw->iHasAlpha);
|
for (int dstX = 0; dstX < ctx->dstWidth; dstX++) {
|
||||||
|
int outX = ctx->config->x + dstX;
|
||||||
|
if (outX >= ctx->screenWidth) continue;
|
||||||
|
|
||||||
// Render scaled row using Bresenham-style integer stepping (no floating-point division)
|
// Map destination X back to source X
|
||||||
int dstWidth = ctx->dstWidth;
|
int srcX = (int)(dstX / ctx->scale);
|
||||||
int outXBase = ctx->config->x;
|
if (srcX >= ctx->srcWidth) srcX = ctx->srcWidth - 1;
|
||||||
int screenWidth = ctx->screenWidth;
|
|
||||||
bool useDithering = ctx->config->useDithering;
|
|
||||||
bool caching = ctx->caching;
|
|
||||||
|
|
||||||
int srcX = 0;
|
uint8_t gray = getGrayFromPixel(pPixels, srcX, pixelType, pDraw->pPalette);
|
||||||
int error = 0;
|
|
||||||
|
|
||||||
for (int dstX = 0; dstX < dstWidth; dstX++) {
|
uint8_t ditheredGray;
|
||||||
int outX = outXBase + dstX;
|
if (ctx->config->useDithering) {
|
||||||
if (outX < screenWidth) {
|
ditheredGray = applyBayerDither4Level(gray, outX, outY);
|
||||||
uint8_t gray = grayLineBuffer[srcX];
|
} else {
|
||||||
|
ditheredGray = gray / 85;
|
||||||
uint8_t ditheredGray;
|
if (ditheredGray > 3) ditheredGray = 3;
|
||||||
if (useDithering) {
|
|
||||||
ditheredGray = applyBayerDither4Level(gray, outX, outY);
|
|
||||||
} else {
|
|
||||||
ditheredGray = gray / 85;
|
|
||||||
if (ditheredGray > 3) ditheredGray = 3;
|
|
||||||
}
|
|
||||||
drawPixelWithRenderMode(*ctx->renderer, outX, outY, ditheredGray);
|
|
||||||
if (caching) ctx->cache.setPixel(outX, outY, ditheredGray);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bresenham-style stepping: advance srcX based on ratio srcWidth/dstWidth
|
|
||||||
error += srcWidth;
|
|
||||||
while (error >= dstWidth) {
|
|
||||||
error -= dstWidth;
|
|
||||||
srcX++;
|
|
||||||
}
|
}
|
||||||
|
drawPixelWithRenderMode(*ctx->renderer, outX, outY, ditheredGray);
|
||||||
|
if (ctx->caching) ctx->cache.setPixel(outX, outY, ditheredGray);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -260,6 +215,10 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath
|
|||||||
warnUnsupportedFeature("bit depth (" + std::to_string(png.getBpp()) + "bpp)", imagePath);
|
warnUnsupportedFeature("bit depth (" + std::to_string(png.getBpp()) + "bpp)", imagePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (png.hasAlpha()) {
|
||||||
|
warnUnsupportedFeature("alpha channel", imagePath);
|
||||||
|
}
|
||||||
|
|
||||||
// Allocate cache buffer using SCALED dimensions
|
// Allocate cache buffer using SCALED dimensions
|
||||||
ctx.caching = !config.cachePath.empty();
|
ctx.caching = !config.cachePath.empty();
|
||||||
if (ctx.caching) {
|
if (ctx.caching) {
|
||||||
@ -269,9 +228,7 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long decodeStart = millis();
|
|
||||||
rc = png.decode(&ctx, 0);
|
rc = png.decode(&ctx, 0);
|
||||||
unsigned long decodeTime = millis() - decodeStart;
|
|
||||||
if (rc != PNG_SUCCESS) {
|
if (rc != PNG_SUCCESS) {
|
||||||
Serial.printf("[%lu] [PNG] Decode failed: %d\n", millis(), rc);
|
Serial.printf("[%lu] [PNG] Decode failed: %d\n", millis(), rc);
|
||||||
png.close();
|
png.close();
|
||||||
@ -279,7 +236,7 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath
|
|||||||
}
|
}
|
||||||
|
|
||||||
png.close();
|
png.close();
|
||||||
Serial.printf("[%lu] [PNG] PNG decoding complete - render time: %lu ms\n", millis(), decodeTime);
|
Serial.printf("[%lu] [PNG] PNG decoding complete\n", millis());
|
||||||
|
|
||||||
// Write cache file if caching was enabled and buffer was allocated
|
// Write cache file if caching was enabled and buffer was allocated
|
||||||
if (ctx.caching) {
|
if (ctx.caching) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user