@@ -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{
6464uint16_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{
7274PRIVATE *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
0 commit comments