Reformatted comments in example.c

Added advice about setting pointers to NULL after png_free(),
capitalized sentences, reformatted long comments in libpng style.
This commit is contained in:
Glenn Randers-Pehrson 2009-05-13 15:23:03 -05:00
parent 640b7d514d
commit 64548deafc

120
example.c
View File

@ -91,6 +91,7 @@ void read_png(char *file_name) /* We need to open the file */
if ((fp = fopen(file_name, "rb")) == NULL) if ((fp = fopen(file_name, "rb")) == NULL)
return (ERROR); return (ERROR);
#else no_open_file /* prototype 2 */ #else no_open_file /* prototype 2 */
void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
{ {
@ -164,6 +165,7 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
* pixels) into the info structure with this call: * pixels) into the info structure with this call:
*/ */
png_read_png(png_ptr, info_ptr, png_transforms, NULL); png_read_png(png_ptr, info_ptr, png_transforms, NULL);
#else #else
/* OK, you're doing it the hard way, with the lower-level functions */ /* OK, you're doing it the hard way, with the lower-level functions */
@ -181,7 +183,7 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
* are mutually exclusive. * are mutually exclusive.
*/ */
/* tell libpng to strip 16 bit/color files down to 8 bits/color */ /* Tell libpng to strip 16 bit/color files down to 8 bits/color */
png_set_strip_16(png_ptr); png_set_strip_16(png_ptr);
/* Strip alpha bytes from the input data without combining with the /* Strip alpha bytes from the input data without combining with the
@ -231,7 +233,8 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
/* Some suggestions as to how to get a screen gamma value */ /* Some suggestions as to how to get a screen gamma value */
/* Note that screen gamma is the display_exponent, which includes /* Note that screen gamma is the display_exponent, which includes
* the CRT_exponent and any correction for viewing conditions */ * the CRT_exponent and any correction for viewing conditions
*/
if (/* We have a user-defined screen gamma value */) if (/* We have a user-defined screen gamma value */)
{ {
screen_gamma = user-defined screen_gamma; screen_gamma = user-defined screen_gamma;
@ -297,7 +300,7 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
} }
} }
/* invert monochrome files to have 0 as white and 1 as black */ /* Invert monochrome files to have 0 as white and 1 as black */
png_set_invert_mono(png_ptr); png_set_invert_mono(png_ptr);
/* If you want to shift the pixel values from the range [0,255] or /* If you want to shift the pixel values from the range [0,255] or
@ -312,14 +315,14 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
png_set_shift(png_ptr, sig_bit_p); png_set_shift(png_ptr, sig_bit_p);
} }
/* flip the RGB pixels to BGR (or RGBA to BGRA) */ /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
if (color_type & PNG_COLOR_MASK_COLOR) if (color_type & PNG_COLOR_MASK_COLOR)
png_set_bgr(png_ptr); png_set_bgr(png_ptr);
/* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
png_set_swap_alpha(png_ptr); png_set_swap_alpha(png_ptr);
/* swap bytes of 16 bit files to least significant byte first */ /* Swap bytes of 16 bit files to least significant byte first */
png_set_swap(png_ptr); png_set_swap(png_ptr);
/* Add filler (or alpha) byte (before/after each RGB triplet) */ /* Add filler (or alpha) byte (before/after each RGB triplet) */
@ -377,29 +380,28 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
#endif no_sparkle /* use only one of these two methods */ #endif no_sparkle /* use only one of these two methods */
} }
/* if you want to display the image after every pass, do /* if you want to display the image after every pass, do so here */
so here */
#endif no_single /* use only one of these two methods */ #endif no_single /* use only one of these two methods */
} }
#endif no_entire /* use only one of these two methods */ #endif no_entire /* use only one of these two methods */
/* read rest of file, and get additional chunks in info_ptr - REQUIRED */ /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
png_read_end(png_ptr, info_ptr); png_read_end(png_ptr, info_ptr);
#endif hilevel #endif hilevel
/* At this point you have read the entire image */ /* At this point you have read the entire image */
/* clean up after the read, and free any memory allocated - REQUIRED */ /* Clean up after the read, and free any memory allocated - REQUIRED */
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
/* close the file */ /* Close the file */
fclose(fp); fclose(fp);
/* that's it */ /* that's it */
return (OK); return (OK);
} }
/* progressively read a file */ /* Progressively read a file */
int int
initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr) initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr)
@ -464,7 +466,7 @@ process_data(png_structp *png_ptr, png_infop *info_ptr,
/* This one's new also. Simply give it chunks of data as /* This one's new also. Simply give it chunks of data as
* they arrive from the data stream (in order, of course). * they arrive from the data stream (in order, of course).
* On Segmented machines, don't give it any more than 64K. * On segmented machines, don't give it any more than 64K.
* The library seems to run fine with sizes of 4K, although * The library seems to run fine with sizes of 4K, although
* you can give it much less if necessary (I assume you can * you can give it much less if necessary (I assume you can
* give it chunks of 1 byte, but I haven't tried with less * give it chunks of 1 byte, but I haven't tried with less
@ -478,7 +480,7 @@ process_data(png_structp *png_ptr, png_infop *info_ptr,
info_callback(png_structp png_ptr, png_infop info) info_callback(png_structp png_ptr, png_infop info)
{ {
/* do any setup here, including setting any of the transformations /* Do any setup here, including setting any of the transformations
* mentioned in the Reading PNG files section. For now, you _must_ * mentioned in the Reading PNG files section. For now, you _must_
* call either png_start_read_image() or png_read_update_info() * call either png_start_read_image() or png_read_update_info()
* after all the transformations are set (even if you don't set * after all the transformations are set (even if you don't set
@ -499,7 +501,7 @@ row_callback(png_structp png_ptr, png_bytep new_row,
* libpng called new_row that is to replace a corresponding row (of * libpng called new_row that is to replace a corresponding row (of
* the same data format) in a buffer allocated by your application. * the same data format) in a buffer allocated by your application.
* *
* The new row data pointer new_row may be NULL, indicating there is * The new row data pointer "new_row" may be NULL, indicating there is
* no new data to be replaced (in cases of interlace loading). * no new data to be replaced (in cases of interlace loading).
* *
* If new_row is not NULL then you need to call * If new_row is not NULL then you need to call
@ -546,7 +548,7 @@ row_callback(png_structp png_ptr, png_bytep new_row,
end_callback(png_structp png_ptr, png_infop info) end_callback(png_structp png_ptr, png_infop info)
{ {
/* this function is called when the whole image has been read, /* This function is called when the whole image has been read,
* including any chunks after the image (up to and including * including any chunks after the image (up to and including
* the IEND). You will usually have the same info chunk as you * the IEND). You will usually have the same info chunk as you
* had in the header, although some data may have been added * had in the header, although some data may have been added
@ -557,7 +559,7 @@ end_callback(png_structp png_ptr, png_infop info)
*/ */
} }
/* write a png file */ /* Write a png file */
void write_png(char *file_name /* , ... other image information ... */) void write_png(char *file_name /* , ... other image information ... */)
{ {
FILE *fp; FILE *fp;
@ -565,7 +567,7 @@ void write_png(char *file_name /* , ... other image information ... */)
png_infop info_ptr; png_infop info_ptr;
png_colorp palette; png_colorp palette;
/* open the file */ /* Open the file */
fp = fopen(file_name, "wb"); fp = fopen(file_name, "wb");
if (fp == NULL) if (fp == NULL)
return (ERROR); return (ERROR);
@ -607,11 +609,13 @@ void write_png(char *file_name /* , ... other image information ... */)
/* One of the following I/O initialization functions is REQUIRED */ /* One of the following I/O initialization functions is REQUIRED */
#ifdef streams /* I/O initialization method 1 */ #ifdef streams /* I/O initialization method 1 */
/* set up the output control if you are using standard C streams */ /* Set up the output control if you are using standard C streams */
png_init_io(png_ptr, fp); png_init_io(png_ptr, fp);
#else no_streams /* I/O initialization method 2 */ #else no_streams /* I/O initialization method 2 */
/* If you are using replacement write functions, instead of calling /* If you are using replacement write functions, instead of calling
* png_init_io() here you would call */ * png_init_io() here you would call
*/
png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn, png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
user_IO_flush_function); user_IO_flush_function);
/* where user_io_ptr is a structure you want available to the callbacks */ /* where user_io_ptr is a structure you want available to the callbacks */
@ -619,10 +623,11 @@ void write_png(char *file_name /* , ... other image information ... */)
#ifdef hilevel #ifdef hilevel
/* This is the easy way. Use it if you already have all the /* This is the easy way. Use it if you already have all the
* image info living info in the structure. You could "|" many * image info living in the structure. You could "|" many
* PNG_TRANSFORM flags into the png_transforms integer here. * PNG_TRANSFORM flags into the png_transforms integer here.
*/ */
png_write_png(png_ptr, info_ptr, png_transforms, NULL); png_write_png(png_ptr, info_ptr, png_transforms, NULL);
#else #else
/* This is the hard way */ /* This is the hard way */
@ -637,24 +642,25 @@ void write_png(char *file_name /* , ... other image information ... */)
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???, png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???,
PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
/* set the palette if there is one. REQUIRED for indexed-color images */ /* Set the palette if there is one. REQUIRED for indexed-color images */
palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH
* png_sizeof(png_color)); * png_sizeof(png_color));
/* ... set palette colors ... */ /* ... Set palette colors ... */
png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH); png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
/* You must not free palette here, because png_set_PLTE only makes a link to /* You must not free palette here, because png_set_PLTE only makes a link to
the palette that you malloced. Wait until you are about to destroy * the palette that you malloced. Wait until you are about to destroy
the png structure. */ * the png structure.
*/
/* optional significant bit chunk */ /* Optional significant bit (sBIT) chunk */
png_color_8 sig_bit; png_color_8 sig_bit;
/* if we are dealing with a grayscale image then */ /* If we are dealing with a grayscale image then */
sig_bit.gray = true_bit_depth; sig_bit.gray = true_bit_depth;
/* otherwise, if we are dealing with a color image then */ /* Otherwise, if we are dealing with a color image then */
sig_bit.red = true_red_bit_depth; sig_bit.red = true_red_bit_depth;
sig_bit.green = true_green_bit_depth; sig_bit.green = true_green_bit_depth;
sig_bit.blue = true_blue_bit_depth; sig_bit.blue = true_blue_bit_depth;
/* if the image has an alpha channel then */ /* If the image has an alpha channel then */
sig_bit.alpha = true_alpha_bit_depth; sig_bit.alpha = true_alpha_bit_depth;
png_set_sBIT(png_ptr, info_ptr, &sig_bit); png_set_sBIT(png_ptr, info_ptr, &sig_bit);
@ -681,11 +687,12 @@ void write_png(char *file_name /* , ... other image information ... */)
#endif #endif
png_set_text(png_ptr, info_ptr, text_ptr, 3); png_set_text(png_ptr, info_ptr, text_ptr, 3);
/* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs */ /* Other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs */
/* note that if sRGB is present the gAMA and cHRM chunks must be ignored /* Note that if sRGB is present the gAMA and cHRM chunks must be ignored
* on read and, if your application chooses to write them, they must * on read and, if your application chooses to write them, they must
* be written in accordance with the sRGB profile */ * be written in accordance with the sRGB profile
*/
/* Write the file header information. REQUIRED */ /* Write the file header information. REQUIRED */
png_write_info(png_ptr, info_ptr); png_write_info(png_ptr, info_ptr);
@ -707,11 +714,11 @@ void write_png(char *file_name /* , ... other image information ... */)
* at the end. * at the end.
*/ */
/* set up the transformations you want. Note that these are /* Set up the transformations you want. Note that these are
* all optional. Only call them if you want them. * all optional. Only call them if you want them.
*/ */
/* invert monochrome pixels */ /* Invert monochrome pixels */
png_set_invert_mono(png_ptr); png_set_invert_mono(png_ptr);
/* Shift the pixels up to a legal bit depth and fill in /* Shift the pixels up to a legal bit depth and fill in
@ -719,10 +726,10 @@ void write_png(char *file_name /* , ... other image information ... */)
*/ */
png_set_shift(png_ptr, &sig_bit); png_set_shift(png_ptr, &sig_bit);
/* pack pixels into bytes */ /* Pack pixels into bytes */
png_set_packing(png_ptr); png_set_packing(png_ptr);
/* swap location of alpha bytes from ARGB to RGBA */ /* Swap location of alpha bytes from ARGB to RGBA */
png_set_swap_alpha(png_ptr); png_set_swap_alpha(png_ptr);
/* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
@ -730,16 +737,16 @@ void write_png(char *file_name /* , ... other image information ... */)
*/ */
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
/* flip BGR pixels to RGB */ /* Flip BGR pixels to RGB */
png_set_bgr(png_ptr); png_set_bgr(png_ptr);
/* swap bytes of 16-bit files to most significant byte first */ /* Swap bytes of 16-bit files to most significant byte first */
png_set_swap(png_ptr); png_set_swap(png_ptr);
/* swap bits of 1, 2, 4 bit packed pixel formats */ /* Swap bits of 1, 2, 4 bit packed pixel formats */
png_set_packswap(png_ptr); png_set_packswap(png_ptr);
/* turn on interlace handling if you are not using png_write_image() */ /* Turn on interlace handling if you are not using png_write_image() */
if (interlacing) if (interlacing)
number_passes = png_set_interlace_handling(png_ptr); number_passes = png_set_interlace_handling(png_ptr);
else else
@ -760,12 +767,14 @@ void write_png(char *file_name /* , ... other image information ... */)
row_pointers[k] = image + k*width*bytes_per_pixel; row_pointers[k] = image + k*width*bytes_per_pixel;
/* One of the following output methods is REQUIRED */ /* One of the following output methods is REQUIRED */
#ifdef entire /* write out the entire image data in one call */
#ifdef entire /* Write out the entire image data in one call */
png_write_image(png_ptr, row_pointers); png_write_image(png_ptr, row_pointers);
/* the other way to write the image - deal with interlacing */ /* the other way to write the image - deal with interlacing */
#else no_entire /* write out the image data by one or more scanlines */ #else no_entire /* Write out the image data by one or more scanlines */
/* The number of passes is either 1 for non-interlaced images, /* The number of passes is either 1 for non-interlaced images,
* or 7 for interlaced images. * or 7 for interlaced images.
*/ */
@ -778,7 +787,7 @@ void write_png(char *file_name /* , ... other image information ... */)
for (y = 0; y < height; y++) for (y = 0; y < height; y++)
png_write_rows(png_ptr, &row_pointers[y], 1); png_write_rows(png_ptr, &row_pointers[y], 1);
} }
#endif no_entire /* use only one output method */ #endif no_entire /* Use only one output method */
/* You can write optional chunks like tEXt, zTXt, and tIME at the end /* You can write optional chunks like tEXt, zTXt, and tIME at the end
* as well. Shouldn't be necessary in 1.2.0 and up as all the public * as well. Shouldn't be necessary in 1.2.0 and up as all the public
@ -791,26 +800,33 @@ void write_png(char *file_name /* , ... other image information ... */)
#endif hilevel #endif hilevel
/* If you png_malloced a palette, free it here (don't free info_ptr->palette, /* If you png_malloced a palette, free it here (don't free info_ptr->palette,
as recommended in versions 1.0.5m and earlier of this example; if * as recommended in versions 1.0.5m and earlier of this example; if
libpng mallocs info_ptr->palette, libpng will free it). If you * libpng mallocs info_ptr->palette, libpng will free it). If you
allocated it with malloc() instead of png_malloc(), use free() instead * allocated it with malloc() instead of png_malloc(), use free() instead
of png_free(). */ * of png_free().
*/
png_free(png_ptr, palette); png_free(png_ptr, palette);
palette = NULL; palette = NULL;
/* Similarly, if you png_malloced any data that you passed in with /* Similarly, if you png_malloced any data that you passed in with
png_set_something(), such as a hist or trans array, free it here, * png_set_something(), such as a hist or trans array, free it here,
when you can be sure that libpng is through with it. */ * when you can be sure that libpng is through with it.
*/
png_free(png_ptr, trans); png_free(png_ptr, trans);
trans = NULL; trans = NULL;
/* Whenever you use png_free() it is a good idea to set the pointer to
* NULL in case your application inadvertently tries to png_free() it
* again. When png_free() sees a NULL it returns without action, thus
* avoiding the double-free security problem.
*/
/* clean up after the write, and free any memory allocated */ /* Clean up after the write, and free any memory allocated */
png_destroy_write_struct(&png_ptr, &info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr);
/* close the file */ /* Close the file */
fclose(fp); fclose(fp);
/* that's it */ /* That's it */
return (OK); return (OK);
} }