172 lines
5.6 KiB
C++
172 lines
5.6 KiB
C++
#include "ImageBlock.h"
|
|
#include "../converters/ImageDecoderFactory.h"
|
|
#include <GfxRenderer.h>
|
|
#include <HardwareSerial.h>
|
|
#include <Serialization.h>
|
|
#include <SDCardManager.h>
|
|
#include <FsHelpers.h>
|
|
|
|
// Cache file format:
|
|
// - uint16_t width
|
|
// - uint16_t height
|
|
// - uint8_t pixels[...] - 2 bits per pixel, packed (4 pixels per byte), row-major order
|
|
|
|
ImageBlock::ImageBlock(const std::string& imagePath, int16_t width, int16_t height)
|
|
: imagePath(imagePath), width(width), height(height) {}
|
|
|
|
bool ImageBlock::imageExists() const {
|
|
FsFile file;
|
|
return SdMan.openFileForRead("IMG", imagePath, file);
|
|
}
|
|
|
|
void ImageBlock::layout(GfxRenderer& renderer) {
|
|
}
|
|
|
|
static std::string getCachePath(const std::string& imagePath) {
|
|
// Replace extension with .pxc (pixel cache)
|
|
size_t dotPos = imagePath.rfind('.');
|
|
if (dotPos != std::string::npos) {
|
|
return imagePath.substr(0, dotPos) + ".pxc";
|
|
}
|
|
return imagePath + ".pxc";
|
|
}
|
|
|
|
static bool renderFromCache(GfxRenderer& renderer, const std::string& cachePath, int x, int y, int expectedWidth, int expectedHeight) {
|
|
FsFile cacheFile;
|
|
if (!SdMan.openFileForRead("IMG", cachePath, cacheFile)) {
|
|
return false;
|
|
}
|
|
|
|
uint16_t cachedWidth, cachedHeight;
|
|
if (cacheFile.read(&cachedWidth, 2) != 2 || cacheFile.read(&cachedHeight, 2) != 2) {
|
|
cacheFile.close();
|
|
return false;
|
|
}
|
|
|
|
// Verify dimensions are close (allow 1 pixel tolerance for rounding differences)
|
|
int widthDiff = abs(cachedWidth - expectedWidth);
|
|
int heightDiff = abs(cachedHeight - expectedHeight);
|
|
if (widthDiff > 1 || heightDiff > 1) {
|
|
Serial.printf("[%lu] [IMG] Cache dimension mismatch: %dx%d vs %dx%d\n", millis(),
|
|
cachedWidth, cachedHeight, expectedWidth, expectedHeight);
|
|
cacheFile.close();
|
|
return false;
|
|
}
|
|
|
|
// Use cached dimensions for rendering (they're the actual decoded size)
|
|
expectedWidth = cachedWidth;
|
|
expectedHeight = cachedHeight;
|
|
|
|
Serial.printf("[%lu] [IMG] Loading from cache: %s (%dx%d)\n", millis(), cachePath.c_str(), cachedWidth, cachedHeight);
|
|
|
|
// Read and render row by row to minimize memory usage
|
|
const int bytesPerRow = (cachedWidth + 3) / 4; // 2 bits per pixel, 4 pixels per byte
|
|
uint8_t* rowBuffer = (uint8_t*)malloc(bytesPerRow);
|
|
if (!rowBuffer) {
|
|
Serial.printf("[%lu] [IMG] Failed to allocate row buffer\n", millis());
|
|
cacheFile.close();
|
|
return false;
|
|
}
|
|
|
|
for (int row = 0; row < cachedHeight; row++) {
|
|
if (cacheFile.read(rowBuffer, bytesPerRow) != bytesPerRow) {
|
|
Serial.printf("[%lu] [IMG] Cache read error at row %d\n", millis(), row);
|
|
free(rowBuffer);
|
|
cacheFile.close();
|
|
return false;
|
|
}
|
|
|
|
int destY = y + row;
|
|
for (int col = 0; col < cachedWidth; col++) {
|
|
int byteIdx = col / 4;
|
|
int bitShift = 6 - (col % 4) * 2; // MSB first within byte
|
|
uint8_t pixelValue = (rowBuffer[byteIdx] >> bitShift) & 0x03;
|
|
renderer.drawPixel(x + col, destY, pixelValue < 2);
|
|
}
|
|
}
|
|
|
|
free(rowBuffer);
|
|
cacheFile.close();
|
|
Serial.printf("[%lu] [IMG] Cache render complete\n", millis());
|
|
return true;
|
|
}
|
|
|
|
void ImageBlock::render(GfxRenderer& renderer, const int x, const int y) {
|
|
Serial.printf("[%lu] [IMG] Rendering image at %d,%d: %s (%dx%d)\n", millis(), x, y, imagePath.c_str(), width, height);
|
|
|
|
const int screenWidth = renderer.getScreenWidth();
|
|
const int screenHeight = renderer.getScreenHeight();
|
|
|
|
// Bounds check render position using logical screen dimensions
|
|
if (x < 0 || y < 0 || x + width > screenWidth || y + height > screenHeight) {
|
|
Serial.printf("[%lu] [IMG] Invalid render position: (%d,%d) size (%dx%d) screen (%dx%d)\n",
|
|
millis(), x, y, width, height, screenWidth, screenHeight);
|
|
return;
|
|
}
|
|
|
|
// Try to render from cache first
|
|
std::string cachePath = getCachePath(imagePath);
|
|
if (renderFromCache(renderer, cachePath, x, y, width, height)) {
|
|
return; // Successfully rendered from cache
|
|
}
|
|
|
|
// No cache - need to decode the image
|
|
// Check if image file exists
|
|
FsFile file;
|
|
if (!SdMan.openFileForRead("IMG", imagePath, file)) {
|
|
Serial.printf("[%lu] [IMG] Image file not found: %s\n", millis(), imagePath.c_str());
|
|
return;
|
|
}
|
|
size_t fileSize = file.size();
|
|
file.close();
|
|
|
|
if (fileSize == 0) {
|
|
Serial.printf("[%lu] [IMG] Image file is empty: %s\n", millis(), imagePath.c_str());
|
|
return;
|
|
}
|
|
|
|
Serial.printf("[%lu] [IMG] Decoding and caching: %s\n", millis(), imagePath.c_str());
|
|
|
|
RenderConfig config;
|
|
config.x = x;
|
|
config.y = y;
|
|
config.maxWidth = width;
|
|
config.maxHeight = height;
|
|
config.useGrayscale = true;
|
|
config.useDithering = true;
|
|
config.performanceMode = false;
|
|
config.cachePath = cachePath; // Enable caching during decode
|
|
|
|
ImageToFramebufferDecoder* decoder = ImageDecoderFactory::getDecoder(imagePath);
|
|
if (!decoder) {
|
|
Serial.printf("[%lu] [IMG] No decoder found for image: %s\n", millis(), imagePath.c_str());
|
|
return;
|
|
}
|
|
|
|
Serial.printf("[%lu] [IMG] Using %s decoder\n", millis(), decoder->getFormatName());
|
|
|
|
bool success = decoder->decodeToFramebuffer(imagePath, renderer, config);
|
|
if (!success) {
|
|
Serial.printf("[%lu] [IMG] Failed to decode image: %s\n", millis(), imagePath.c_str());
|
|
return;
|
|
}
|
|
|
|
Serial.printf("[%lu] [IMG] Decode successful\n", millis());
|
|
}
|
|
|
|
bool ImageBlock::serialize(FsFile& file) {
|
|
serialization::writeString(file, imagePath);
|
|
serialization::writePod(file, width);
|
|
serialization::writePod(file, height);
|
|
return true;
|
|
}
|
|
|
|
std::unique_ptr<ImageBlock> ImageBlock::deserialize(FsFile& file) {
|
|
std::string path;
|
|
serialization::readString(file, path);
|
|
int16_t w, h;
|
|
serialization::readPod(file, w);
|
|
serialization::readPod(file, h);
|
|
return std::unique_ptr<ImageBlock>(new ImageBlock(path, w, h));
|
|
}
|