Skip to content

Commit 28b221d

Browse files
Laurence BankLaurence Bank
authored andcommitted
Updated PNGDisplay helper class to support LittleFS
1 parent b0c7d7f commit 28b221d

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The PNG image specification was written at a time when computers had megabytes o
2020

2121
Feature summary:<br>
2222
----------------<br>
23+
- *New* Added PNGDisplay helper class to simplify displaying images on displays supported by my bb_spi_lcd library<br>
2324
- Runs on any MCU with at least 48K of free RAM<br>
2425
- No external dependencies (including malloc/free)<br>
2526
- Decode an image line by line with a callback function<br>

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=PNGdec
2-
version=1.1.2
2+
version=1.1.3
33
author=Larry Bank
44
maintainer=Larry Bank
55
sentence=Universal PNG decoder for MCUs with at least 48K of RAM.

src/PNGDisplay.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
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

3742
static 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+
}
5974
static 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

153209
int 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

Comments
 (0)