2121#include < PNGdec.h>
2222#include < bb_spi_lcd.h>
2323#include < SD.h>
24+ #include " FS.h"
25+ #include < LittleFS.h>
26+
2427// To center one or both coordinates for the drawing position
2528// use this constant value
2629#define PNGDISPLAY_CENTER -2
@@ -30,8 +33,10 @@ class PNGDisplay
3033 public:
3134 int loadPNG (BB_SPI_LCD *pLCD, int x, int y, const void *pData, int iDataSize, uint32_t bgColor = 0xffffffff );
3235 int loadPNG (BB_SPI_LCD *pLCD, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff );
36+ int loadPNG_LFS (BB_SPI_LCD *pLCD, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff );
3337 int getPNGInfo (int *width, int *height, int *bpp, const void *pData, int iDataSize);
3438 int getPNGInfo (int *width, int *height, int *bpp, const char *fname);
39+ int getPNGInfo_LFS (int *width, int *height, int *bpp, const char *fname);
3540};
3641
3742static void PNGDraw (PNGDRAW *pDraw)
@@ -56,6 +61,16 @@ static void * pngOpen(const char *filename, int32_t *size) {
5661 *size = myfile.size ();
5762 return &myfile;
5863}
64+ static void * pngOpenLFS (const char *filename, int32_t *size) {
65+ static File myfile;
66+ myfile = LittleFS.open (filename, FILE_READ);
67+ if (myfile) {
68+ *size = myfile.size ();
69+ return &myfile;
70+ } else {
71+ return NULL ;
72+ }
73+ }
5974static void pngClose (void *handle) {
6075 File *pFile = (File *)handle;
6176 if (pFile) pFile->close ();
@@ -149,6 +164,47 @@ int PNGDisplay::loadPNG(BB_SPI_LCD *pLCD, int x, int y, const char *fname, uint3
149164 free (png);
150165 return 0 ;
151166} /* loadPNG() */
167+ int PNGDisplay::loadPNG_LFS (BB_SPI_LCD *pLCD, int x, int y, const char *fname, uint32_t bgColor)
168+ {
169+ PNG *png;
170+ int w, h, rc;
171+ uint32_t *png_info;
172+
173+ if (!LittleFS.begin (false )) {
174+ return 0 ;
175+ }
176+ png = (PNG *)malloc (sizeof (PNG));
177+ if (!png) return 0 ;
178+ rc = png->open (fname, pngOpenLFS, pngClose, pngRead, pngSeek, PNGDraw);
179+ if (rc == PNG_SUCCESS) {
180+ w = png->getWidth ();
181+ h = png->getHeight ();
182+ if (x == PNGDISPLAY_CENTER) {
183+ x = (pLCD->width () - w)/2 ;
184+ if (x < 0 ) x = 0 ;
185+ } else if (x < 0 || w + x > pLCD->width ()) {
186+ // clipping is not supported
187+ return 0 ;
188+ }
189+ if (y == PNGDISPLAY_CENTER) {
190+ y = (pLCD->height () - h)/2 ;
191+ if (y < 0 ) y = 0 ;
192+ } else if (y < 0 || y + h > pLCD->height ()) {
193+ return 0 ;
194+ }
195+ png_info = (uint32_t *)malloc ((w * sizeof (uint16_t )) + 3 * sizeof (int )); // enough for pixels and 3 32-bit values
196+ png_info[0 ] = (uint32_t )pLCD;
197+ png_info[1 ] = (uint32_t )png;
198+ png_info[2 ] = bgColor;
199+ pLCD->setAddrWindow (x, y, w, h);
200+ png->decode ((void *)png_info, 0 ); // simple decode, no options
201+ png->close ();
202+ free (png);
203+ return 1 ;
204+ }
205+ free (png);
206+ return 0 ;
207+ } /* loadPNG_LFS() */
152208
153209int PNGDisplay::getPNGInfo (int *width, int *height, int *bpp, const void *pData, int iDataSize)
154210{
@@ -194,4 +250,29 @@ int PNGDisplay::getPNGInfo(int *width, int *height, int *bpp, const char *fname)
194250 return 0 ;
195251} /* getPNGInfo() */
196252
253+ int PNGDisplay::getPNGInfo_LFS (int *width, int *height, int *bpp, const char *fname)
254+ {
255+ PNG *png;
256+ int rc;
257+ uint32_t *png_info;
258+
259+ if (!LittleFS.begin (false )) {
260+ return 0 ;
261+ }
262+ if (!width || !height || !bpp || !fname) return 0 ;
263+ png = (PNG *)malloc (sizeof (PNG));
264+ if (!png) return 0 ;
265+ rc = png->open (fname, pngOpenLFS, pngClose, pngRead, pngSeek, PNGDraw);
266+ if (rc == PNG_SUCCESS) {
267+ *width = png->getWidth ();
268+ *height = png->getHeight ();
269+ *bpp = png->getBpp ();
270+ png->close ();
271+ free (png);
272+ return 1 ;
273+ }
274+ free (png);
275+ return 0 ;
276+ } /* getPNGInfo_LFS() */
277+
197278#endif // __PNGDISPLAY__
0 commit comments