Skip to content

Commit db9ce9c

Browse files
committed
Added early abort option to PNGDRAW callback
1 parent 9ef2f46 commit db9ce9c

File tree

13 files changed

+49
-248
lines changed

13 files changed

+49
-248
lines changed

MacOS/PNG_Test/PNG_Test/main.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ typedef struct my_private_struct
5959
int xoff, yoff; // corner offset
6060
} PRIVATE;
6161
// Second draw callback for testing conversion to RGB565
62-
void PNGDraw2(PNGDRAW *pDraw)
62+
int PNGDraw2(PNGDRAW *pDraw)
6363
{
6464
uint16_t usPixels[320];
6565
png.getLineAsRGB565(pDraw, usPixels, ucPixelType, u32BG);
6666
if (pDraw->y == 0) { // get first pixel of first line for testing
6767
u16Out = usPixels[0];
6868
}
69+
return 1;
6970
} /* PNGDraw2() */
70-
void PNGDraw(PNGDRAW *pDraw)
71+
72+
int PNGDraw(PNGDRAW *pDraw)
7173
{
7274
PRIVATE *pPriv = (PRIVATE *)pDraw->pUser;
7375

@@ -83,8 +85,13 @@ PRIVATE *pPriv = (PRIVATE *)pDraw->pUser;
8385
iBpp = pDraw->iBpp;
8486
iWidth = pDraw->iWidth;
8587
iLines++;
88+
return 1;
8689
} /* PNGDraw() */
8790

91+
int PNGDraw3(PNGDRAW *pDraw)
92+
{
93+
return 0; // abort the decode immediately
94+
}
8895
//
8996
// Simple logging print
9097
//
@@ -296,6 +303,23 @@ int main(int argc, const char * argv[]) {
296303
iTotalFail++;
297304
PNGLOG(__LINE__, szTestName, " - FAILED");
298305
}
306+
// Test 10 - check the decoding can be aborted when the PNGDraw callback returns 0
307+
ucPixelType = PNG_RGB565_BIG_ENDIAN;
308+
szTestName = (char *)"PNG decode aborted early";
309+
iTotal++;
310+
PNGLOG(__LINE__, szTestName, szStart);
311+
u16Out = 0xffff;
312+
png.openFLASH((uint8_t *)octocat_8bpp, sizeof(octocat_8bpp), PNGDraw3);
313+
png.setBuffer(NULL);
314+
rc = png.decode(NULL, 0);
315+
png.close();
316+
if (rc == PNG_QUIT_EARLY) { // check transparnet pixel (0,0) to see if it matches the 32-bit BG color we asked for
317+
iTotalPass++;
318+
PNGLOG(__LINE__, szTestName, " - PASSED");
319+
} else {
320+
iTotalFail++;
321+
PNGLOG(__LINE__, szTestName, " - FAILED");
322+
}
299323

300324
// FUZZ testing
301325
// Randomize the input data (file header and compressed data) and confirm that the library returns an error code

examples/1bpp_transparent_oled/1bpp_transparent_oled.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ uint8_t *d, ucPix, ucAlpha, ucMask;
4747
} // for each group of 8 pixels
4848
} /* DrawPixelsMasked() */
4949

50-
void PNGDraw(PNGDRAW *pDraw)
50+
int PNGDraw(PNGDRAW *pDraw)
5151
{
5252
uint8_t ucMask[32];
5353

5454
if (png.getAlphaMask(pDraw, ucMask, 255)) { // if any pixels are opaque, draw them
5555
DrawPixelsMasked(pDraw->y, pDraw->pPixels, ucMask, pDraw->iWidth);
5656
}
57+
return 1;
5758
} /* PNGDraw() */
5859

5960
void setup() {

examples/dma_perf_boost/dma_perf_boost.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static uint16_t usPixels[320]; // make sure there is enough room for the full im
1919
// Called with a full row of pixels
2020
// for every row in the image
2121
//
22-
void PNGDraw(PNGDRAW *pDraw)
22+
int PNGDraw(PNGDRAW *pDraw)
2323
{
2424
if (pDraw->y == 0) {
2525
// set the address window when we get the first line
@@ -35,6 +35,7 @@ void PNGDraw(PNGDRAW *pDraw)
3535
} else {
3636
lcd.pushPixels(usPixels, pDraw->iWidth);
3737
}
38+
return 1;
3839
} /* PNGDraw() */
3940

4041
void setup()
@@ -75,4 +76,4 @@ void setup()
7576
void loop()
7677
{
7778

78-
}
79+
}

examples/m5stickc_plus_test/m5logosmall.h

Lines changed: 0 additions & 234 deletions
This file was deleted.

examples/png_benchmark/png_benchmark.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ typedef struct myprivate
1717
bool bConvert;
1818
} PRIVATE;
1919

20-
void PNGDraw(PNGDRAW *pDraw)
20+
int PNGDraw(PNGDRAW *pDraw)
2121
{
2222
PRIVATE *pPriv = (PRIVATE *)pDraw->pUser;
2323
uint16_t usPixels[320];
2424

2525
if (pPriv->bConvert)
2626
png.getLineAsRGB565(pDraw, usPixels, PNG_RGB565_LITTLE_ENDIAN, 0xffffffff); // don't do alpha color blending
27+
return 1;
2728
} /* PNGDraw() */
2829

2930
void setup() {

examples/png_comparison/png_comparison.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void DecodeTest(int bLode, const uint8_t *pData, size_t data_size, const char *s
7575
}
7676
} /* DecodeTest() */
7777

78-
void PNGDraw(PNGDRAW *pDraw)
78+
int PNGDraw(PNGDRAW *pDraw)
7979
{
8080
if (bDisplay) {
8181
if (pDraw->y == 0) {
@@ -84,6 +84,7 @@ void PNGDraw(PNGDRAW *pDraw)
8484
png->getLineAsRGB565(pDraw, usPixels, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
8585
lcd.pushPixels(usPixels, pDraw->iWidth, DRAW_TO_LCD | DRAW_WITH_DMA);
8686
}
87+
return 1;
8788
} /* PNGDraw() */
8889

8990
void setup() {

examples/png_transparency/png_transparency.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct my_private_struct
3030
int xoff, yoff; // corner offset
3131
} PRIVATE;
3232

33-
void PNGDraw(PNGDRAW *pDraw)
33+
int PNGDraw(PNGDRAW *pDraw)
3434
{
3535
uint16_t usPixels[320];
3636
uint8_t ucMask[40];
@@ -40,6 +40,7 @@ PRIVATE *pPriv = (PRIVATE *)pDraw->pUser;
4040
if (png.getAlphaMask(pDraw, ucMask, 255)) { // if any pixels are opaque, draw them
4141
spilcdWritePixelsMasked(&lcd, pPriv->xoff, pPriv->yoff + pDraw->y, (uint8_t *)usPixels, ucMask, pDraw->iWidth, DRAW_TO_LCD);
4242
}
43+
return 1;
4344
} /* PNGDraw() */
4445

4546
void setup() {

examples/sdcard_slideshow/sdcard_slideshow.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ int32_t mySeek(PNGFILE *handle, int32_t position) {
5959
}
6060

6161
// Function to draw pixels to the display
62-
void PNGDraw(PNGDRAW *pDraw) {
62+
int PNGDraw(PNGDRAW *pDraw) {
6363
uint16_t usPixels[320];
6464

6565
png.getLineAsRGB565(pDraw, usPixels, PNG_RGB565_LITTLE_ENDIAN, 0xffffffff);
6666
tft.writeRect(0, pDraw->y + 24, pDraw->iWidth, 1, usPixels);
67+
return 1;
6768
}
6869

6970
// Main loop, scan for all .PNG files on the card and display them

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.3
2+
version=1.1.4
33
author=Larry Bank
44
maintainer=Larry Bank
55
sentence=Universal PNG decoder for MCUs with at least 48K of RAM.

0 commit comments

Comments
 (0)