Files
sunflower-picture-app/sound_picture_app/Display_PNG.cpp

74 lines
2.6 KiB
C++

#include "Display_PNG.h"
static PNG png;
static File Image_file;
static int16_t xpos = 0;
static int16_t ypos = 0;
// ── PNG callbacks ─────────────────────────────────────────────────────────────
static void* pngOpen(const char* filePath, int32_t* size) {
Image_file = SD_MMC.open(filePath);
if (!Image_file) { printf("PNG: failed to open %s\r\n", filePath); return nullptr; }
*size = Image_file.size();
return &Image_file;
}
static void pngClose(void* handle) {
File* fp = (File*)handle;
if (fp && *fp) fp->close();
}
static int32_t pngRead(PNGFILE* page, uint8_t* buffer, int32_t length) {
if (!Image_file) return 0;
return Image_file.read(buffer, length);
}
static int32_t pngSeek(PNGFILE* page, int32_t position) {
if (!Image_file) return 0;
return Image_file.seek(position);
}
static uint16_t lineBuffer[MAX_IMAGE_WIDTH];
static void pngDraw(PNGDRAW* pDraw) {
png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
uint32_t w = pDraw->iWidth;
for (size_t i = 0; i < w; i++) lineBuffer[i] = __builtin_bswap16(lineBuffer[i]);
LCD_addWindow(xpos, pDraw->y, xpos + w - 1, ypos + pDraw->y, lineBuffer);
}
// ── Screen fill ───────────────────────────────────────────────────────────────
static void clearScreen() {
static uint16_t blackLine[MAX_IMAGE_WIDTH];
memset(blackLine, 0, sizeof(blackLine));
for (int y = 0; y < LCD_HEIGHT; y++)
LCD_addWindow(0, y, LCD_WIDTH - 1, y, blackLine);
}
// ── Public API ────────────────────────────────────────────────────────────────
void Display_Image_From_SD(const char* fullPath) {
printf("PNG: loading %s\r\n", fullPath);
int16_t rc = png.open(fullPath, pngOpen, pngClose, pngRead, pngSeek, pngDraw);
if (rc != PNG_SUCCESS) {
printf("PNG: open failed, rc=%d\r\n", rc);
return;
}
if (png.getWidth() > MAX_IMAGE_WIDTH) {
printf("PNG: image too wide (%d px)\r\n", png.getWidth());
png.close();
return;
}
clearScreen();
png.decode(NULL, 0);
png.close();
}
void Display_Memory_Photo(int memoryIndex) {
if (memoryIndex < 0 || memoryIndex >= g_memoryCount) {
printf("PNG: invalid memory index %d\r\n", memoryIndex);
return;
}
char path[120];
snprintf(path, sizeof(path), "/photos/%s", g_memories[memoryIndex].photo);
Display_Image_From_SD(path);
}