[libpng16] Add "free()" and "png_free_image()" calls to example code.
This commit is contained in:
parent
5e934ba900
commit
414769b415
@ -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
|
||||
|
@ -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
|
||||
|
13
example.c
13
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
|
||||
|
Loading…
Reference in New Issue
Block a user