2121#include < PNGdec.h>
2222#include < bb_spi_lcd.h>
2323#include < SD.h>
24+ // To center one or both coordinates for the drawing position
25+ // use this constant value
26+ #define PNGDISPLAY_CENTER -2
2427
2528class PNGDisplay
2629{
@@ -46,24 +49,26 @@ PNG *pPNG;
4649} /* PNGDraw() */
4750
4851// Functions to access a file on the SD card
49- static File myfile;
5052
51- static void * myOpen (const char *filename, int32_t *size) {
52- Serial. printf ( " Attempting to open %s \n " , filename) ;
53+ static void * pngOpen (const char *filename, int32_t *size) {
54+ static File myfile ;
5355 myfile = SD.open (filename);
5456 *size = myfile.size ();
5557 return &myfile;
5658}
57- static void myClose (void *handle) {
58- if (myfile) myfile.close ();
59+ static void pngClose (void *handle) {
60+ File *pFile = (File *)handle;
61+ if (pFile) pFile->close ();
5962}
60- static int32_t myRead (PNGFILE *handle, uint8_t *buffer, int32_t length) {
61- if (!myfile) return 0 ;
62- return myfile.read (buffer, length);
63+ static int32_t pngRead (PNGFILE *handle, uint8_t *buffer, int32_t length) {
64+ File *pFile = (File *)handle->fHandle ;
65+ if (!pFile) return 0 ;
66+ return pFile->read (buffer, length);
6367}
64- static int32_t mySeek (PNGFILE *handle, int32_t position) {
65- if (!myfile) return 0 ;
66- return myfile.seek (position);
68+ static int32_t pngSeek (PNGFILE *handle, int32_t position) {
69+ File *pFile = (File *)handle->fHandle ;
70+ if (!pFile) return 0 ;
71+ return pFile->seek (position);
6772}
6873
6974int PNGDisplay::loadPNG (BB_SPI_LCD *pLCD, int x, int y, const void *pData, int iDataSize, uint32_t bgColor)
@@ -78,10 +83,20 @@ uint32_t *png_info;
7883 if (rc == PNG_SUCCESS) {
7984 w = png->getWidth ();
8085 h = png->getHeight ();
81- if (x < 0 || w + x > pLCD->width () || y < 0 || y + h > pLCD->height ()) {
86+ if (x == PNGDISPLAY_CENTER) {
87+ x = (pLCD->width () - w)/2 ;
88+ if (x < 0 ) x = 0 ;
89+ } else if (x < 0 || w + x > pLCD->width ()) {
8290 // clipping is not supported
8391 return 0 ;
8492 }
93+ if (y == PNGDISPLAY_CENTER) {
94+ y = (pLCD->height () - h)/2 ;
95+ if (y < 0 ) y = 0 ;
96+ } else if (y < 0 || y + h > pLCD->height ()) {
97+ // clipping is not supported
98+ return 0 ;
99+ }
85100 png_info = (uint32_t *)malloc ((w * sizeof (uint16_t )) + 3 * sizeof (int )); // enough for pixels and 3 32-bit values
86101 png_info[0 ] = (uint32_t )pLCD;
87102 png_info[1 ] = (uint32_t )png;
@@ -104,14 +119,23 @@ int PNGDisplay::loadPNG(BB_SPI_LCD *pLCD, int x, int y, const char *fname, uint3
104119
105120 png = (PNG *)malloc (sizeof (PNG));
106121 if (!png) return 0 ;
107- rc = png->open (fname, myOpen, myClose, myRead, mySeek , PNGDraw);
122+ rc = png->open (fname, pngOpen, pngClose, pngRead, pngSeek , PNGDraw);
108123 if (rc == PNG_SUCCESS) {
109124 w = png->getWidth ();
110125 h = png->getHeight ();
111- if (x < 0 || w + x > pLCD->width () || y < 0 || y + h > pLCD->height ()) {
126+ if (x == PNGDISPLAY_CENTER) {
127+ x = (pLCD->width () - w)/2 ;
128+ if (x < 0 ) x = 0 ;
129+ } else if (x < 0 || w + x > pLCD->width ()) {
112130 // clipping is not supported
113131 return 0 ;
114132 }
133+ if (y == PNGDISPLAY_CENTER) {
134+ y = (pLCD->height () - h)/2 ;
135+ if (y < 0 ) y = 0 ;
136+ } else if (y < 0 || y + h > pLCD->height ()) {
137+ return 0 ;
138+ }
115139 png_info = (uint32_t *)malloc ((w * sizeof (uint16_t )) + 3 * sizeof (int )); // enough for pixels and 3 32-bit values
116140 png_info[0 ] = (uint32_t )pLCD;
117141 png_info[1 ] = (uint32_t )png;
@@ -157,7 +181,7 @@ int PNGDisplay::getPNGInfo(int *width, int *height, int *bpp, const char *fname)
157181 if (!width || !height || !bpp || !fname) return 0 ;
158182 png = (PNG *)malloc (sizeof (PNG));
159183 if (!png) return 0 ;
160- rc = png->open (fname, myOpen, myClose, myRead, mySeek , PNGDraw);
184+ rc = png->open (fname, pngOpen, pngClose, pngRead, pngSeek , PNGDraw);
161185 if (rc == PNG_SUCCESS) {
162186 *width = png->getWidth ();
163187 *height = png->getHeight ();
0 commit comments