[libpng16] Add "free()" and "png_free_image()" calls to example code.

This commit is contained in:
John Bowler 2011-11-27 21:39:13 -06:00 committed by Glenn Randers-Pehrson
parent 5e934ba900
commit 414769b415
3 changed files with 29 additions and 4 deletions

View File

@ -9,7 +9,7 @@
* *
* This code illustrates basic 'by-row' reading of a PNG file using libpng. * This code illustrates basic 'by-row' reading of a PNG file using libpng.
* Rows are read until a particular pixel is found, the value of this pixel is * Rows are read until a particular pixel is found, the value of this pixel is
* then printed on stdou. * then printed on stdout.
* *
* The code illustrates how to do this on interlaced as well as non-interlaced * The code illustrates how to do this on interlaced as well as non-interlaced
* images. Normally you would call png_set_interlace_handling() to have libpng * images. Normally you would call png_set_interlace_handling() to have libpng

View File

@ -5,7 +5,8 @@
* related or neighboring rights to this work. This work is published from: * related or neighboring rights to this work. This work is published from:
* United States. * United States.
* *
* Read a PNG and write it out in a fixed format * Read a PNG and write it out in a fixed format, using the 'simplified API'
* that was introduced in libpng-1.6.0.
* *
* This sample code is just the code from the top of 'example.c' with some error * This sample code is just the code from the top of 'example.c' with some error
* handling added. See example.c for more comments. * handling added. See example.c for more comments.
@ -50,11 +51,22 @@ int main(int argc, const char **argv)
else else
fprintf(stderr, "pngtopng: write %s: %s\n", argv[2], fprintf(stderr, "pngtopng: write %s: %s\n", argv[2],
image.message); image.message);
free(buffer);
} }
else else
fprintf(stderr, "pngtopng: read %s: %s\n", argv[1], image.message); {
fprintf(stderr, "pngtopng: read %s: %s\n", argv[1],
image.message);
/* This is the only place where a 'free' is required; libpng does
* the cleanup on error and success, but in this case we couldn't
* complete the read because of running out of memory.
*/
png_image_free(&image);
}
} }
else else

View File

@ -97,6 +97,19 @@ int main(int argc, const char **argv)
exit(0); exit(0);
} }
} }
else
{
/* Calling png_free_image is optional unless the simplified API was
* not run to completion. In this case if there wasn't enough
* memory for 'buffer' we didn't complete the read, so we must free
* the image:
*/
if (buffer == NULL)
png_free_image(&image);
else
free(buffer);
} }
/* Something went wrong reading or writing the image. libpng stores a /* Something went wrong reading or writing the image. libpng stores a