diff --git a/contrib/examples/pngpixel.c b/contrib/examples/pngpixel.c index 1c29cf027..108e68696 100644 --- a/contrib/examples/pngpixel.c +++ b/contrib/examples/pngpixel.c @@ -9,7 +9,7 @@ * * 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 - * then printed on stdou. + * then printed on stdout. * * 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 diff --git a/contrib/examples/pngtopng.c b/contrib/examples/pngtopng.c index a2610bb3e..352a727a4 100644 --- a/contrib/examples/pngtopng.c +++ b/contrib/examples/pngtopng.c @@ -5,7 +5,8 @@ * related or neighboring rights to this work. This work is published from: * 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 * handling added. See example.c for more comments. @@ -50,11 +51,22 @@ int main(int argc, const char **argv) else fprintf(stderr, "pngtopng: write %s: %s\n", argv[2], - image.message); + image.message); + + free(buffer); } 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 diff --git a/example.c b/example.c index e8bfdd093..c63ef1c35 100644 --- a/example.c +++ b/example.c @@ -97,6 +97,19 @@ int main(int argc, const char **argv) 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