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

79 lines
2.2 KiB
C++

#include "Display_PNG.h"
PNG png;
File Image_file;
uint16_t Image_Count = 0;
char Image_Names[100][100];
int16_t xpos = 0;
int16_t ypos = 0;
void * pngOpen(const char *filePath, int32_t *size) {
Image_file = SD_MMC.open(filePath);
*size = Image_file.size();
return &Image_file;
}
void pngClose(void *handle) {
File Image_file = *((File*)handle);
if (Image_file) Image_file.close();
}
int32_t pngRead(PNGFILE *page, uint8_t *buffer, int32_t length) {
if (!Image_file) return 0;
return Image_file.read(buffer, length);
}
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];
void pngDraw(PNGDRAW *pDraw) {
png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
uint32_t size = pDraw->iWidth;
for (size_t i = 0; i < size; i++) {
lineBuffer[i] = (((lineBuffer[i] >> 8) & 0xFF) | ((lineBuffer[i] << 8) & 0xFF00));
}
LCD_addWindow(xpos, pDraw->y, xpos + pDraw->iWidth - 1, ypos + pDraw->y, lineBuffer);
}
void Search_Images(const char* directory) {
Image_Count = Folder_retrieval(directory, ".png", Image_Names, 100);
}
void Display_Image_By_Path(const char* filePath) {
printf("Displaying image: %s\r\n", filePath);
int16_t ret = png.open(filePath, pngOpen, pngClose, pngRead, pngSeek, pngDraw);
if (ret == PNG_SUCCESS) {
printf("Image dimensions: %d x %d, %d bpp\r\n", png.getWidth(), png.getHeight(), png.getBpp());
if (png.getWidth() > MAX_IMAGE_WIDTH) {
printf("Image is too wide for line buffer!\r\n");
} else {
ret = png.decode(NULL, 0);
png.close();
}
} else {
printf("Failed to open PNG image! Error code: %d\r\n", ret);
}
}
void Display_Image_By_Index(const char* directory, uint16_t index) {
if (Image_Count == 0) {
Search_Images(directory);
}
if (Image_Count > 0 && index < Image_Count) {
String filePath;
if (String(directory) == "/") {
filePath = String(directory) + Image_Names[index];
} else {
filePath = String(directory) + "/" + Image_Names[index];
}
Display_Image_By_Path(filePath.c_str());
} else {
printf("Invalid image index: %d (Total images: %d)\r\n", index, Image_Count);
}
}