blob: 430450437ad2048cab15906fcfb6ac8f9cd46085 [file] [log] [blame]
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05002#if 0 /* in case someone actually tries to compile this */
3
Guy Schalnat0d580581995-07-20 02:43:20 -05004/* example.c - an example of using libpng */
5
Andreas Dilger47a0c421997-05-16 02:46:07 -05006/* This is an example of how to use libpng to read and write PNG files.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06007 * The file libpng.txt is much more verbose then this. If you have not
8 * read it, do so first. This was designed to be a starting point of an
9 * implementation. This is not officially part of libpng, and therefore
10 * does not require a copyright notice.
11 *
12 * This file does not currently compile, because it is missing certain
13 * parts, like allocating memory to hold an image. You will have to
14 * supply these parts to get it to compile. For an example of a minimal
15 * working PNG reader/writer, see pngtest.c, included in this distribution.
16 */
Guy Schalnat0d580581995-07-20 02:43:20 -050017
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060018#include "png.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050019
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -060020/* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp()
21 * returns zero if the image is a PNG and nonzero if it isn't a PNG.
22 *
23 * The function check_if_png() shown here, but not used, returns nonzero (true)
24 * if the file can be opened and is a PNG, 0 (false) otherwise.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060025 *
26 * If this call is successful, and you are going to keep the file open,
27 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once
28 * you have created the png_ptr, so that libpng knows your application
29 * has read that many bytes from the start of the file. Make sure you
30 * don't call png_set_sig_bytes() with more than 8 bytes read or give it
31 * an incorrect number of bytes read, or you will either have read too
32 * many bytes (your fault), or you are telling libpng to read the wrong
33 * number of magic bytes (also your fault).
34 *
35 * Many applications already read the first 2 or 4 bytes from the start
36 * of the image to determine the file type, so it would be easiest just
Glenn Randers-Pehrson0f7202f1998-03-08 18:52:15 -060037 * to pass the bytes to png_sig_cmp() or even skip that if you know
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060038 * you have a PNG file, and call png_set_sig_bytes().
39 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050040#define PNG_BYTES_TO_CHECK 4
41int check_if_png(char *file_name, FILE **fp)
Guy Schalnat0d580581995-07-20 02:43:20 -050042{
Andreas Dilger47a0c421997-05-16 02:46:07 -050043 char buf[PNG_BYTES_TO_CHECK];
Guy Schalnat0d580581995-07-20 02:43:20 -050044
Andreas Dilger47a0c421997-05-16 02:46:07 -050045 /* Open the prospective PNG file. */
46 if ((*fp = fopen(file_name, "rb")) != NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -050047 return 0;
48
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -060049 /* Read in some of the signature bytes */
Andreas Dilger47a0c421997-05-16 02:46:07 -050050 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK)
51 return 0;
Guy Schalnat0d580581995-07-20 02:43:20 -050052
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -060053 /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
54 Return nonzero (true) if they match */
55
56 return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
Guy Schalnat0d580581995-07-20 02:43:20 -050057}
58
Andreas Dilger47a0c421997-05-16 02:46:07 -050059/* Read a PNG file. You may want to return an error code if the read
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060060 * fails (depending upon the failure). There are two "prototypes" given
61 * here - one where we are given the filename, and we need to open the
62 * file, and the other where we are given an open file (possibly with
63 * some or all of the magic bytes read - see comments above).
64 */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060065#ifdef open_file /* prototype 1 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060066void read_png(char *file_name) /* We need to open the file */
Guy Schalnat0d580581995-07-20 02:43:20 -050067{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060068 png_structp png_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -060069 png_infop info_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -050070 unsigned int sig_read = 0;
71 png_uint_32 width, height;
72 int bit_depth, color_type, interlace_type;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060073 FILE *fp;
Guy Schalnat0d580581995-07-20 02:43:20 -050074
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060075 if ((fp = fopen(file_name, "rb")) == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -050076 return;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060077#else no_open_file /* prototype 2 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060078void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
79{
80 png_structp png_ptr;
81 png_infop info_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -050082 png_uint_32 width, height;
83 int bit_depth, color_type, interlace_type;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060084#endif no_open_file /* only use one prototype! */
Guy Schalnat0d580581995-07-20 02:43:20 -050085
Guy Schalnate5a37791996-06-05 15:50:50 -050086 /* Create and initialize the png_struct with the desired error handler
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 * functions. If you want to use the default stderr and longjump method,
88 * you can supply NULL for the last three parameters. We also supply the
89 * the compiler header file version, so that we know if the application
90 * was compiled with a compatible version of the library. REQUIRED
91 */
Guy Schalnate5a37791996-06-05 15:50:50 -050092 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -050093 png_voidp user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -050094
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 if (png_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -050096 {
97 fclose(fp);
98 return;
99 }
100
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 /* Allocate/initialize the memory for image information. REQUIRED. */
Glenn Randers-Pehrson0f7202f1998-03-08 18:52:15 -0600102 info_ptr = png_create_info_struct(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500103 if (info_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 {
105 fclose(fp);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500106 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 return;
108 }
109
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110 /* Set error handling if you are using the setjmp/longjmp method (this is
111 * the normal method of doing things with libpng). REQUIRED unless you
112 * set up your own error handlers in the png_create_read_struct() earlier.
113 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500114 if (setjmp(png_ptr->jmpbuf))
115 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500116 /* Free all of the memory associated with the png_ptr and info_ptr */
117 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500118 fclose(fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119 /* If we get here, we had a problem reading the file */
120 return;
121 }
122
Andreas Dilger47a0c421997-05-16 02:46:07 -0500123 /* One of the following I/O initialization methods is REQUIRED */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600124#ifdef streams /* PNG file I/O method 1 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500125 /* Set up the input control if you are using standard C streams */
Guy Schalnat0d580581995-07-20 02:43:20 -0500126 png_init_io(png_ptr, fp);
127
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600128#else no_streams /* PNG file I/O method 2 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500129 /* If you are using replacement read functions, instead of calling
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600130 * png_init_io() here you would call:
131 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500132 png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
133 /* where user_io_ptr is a structure you want available to the callbacks */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600134#endif no_streams /* Use only one I/O method! */
Guy Schalnat69b14481996-01-10 02:56:49 -0600135
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136 /* If we have already read some of the signature */
Glenn Randers-Pehrson0f7202f1998-03-08 18:52:15 -0600137 png_set_sig_bytes(png_ptr, sig_read);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600138
Andreas Dilger47a0c421997-05-16 02:46:07 -0500139 /* The call to png_read_info() gives us all of the information from the
140 * PNG file before the first IDAT (image data chunk). REQUIRED
141 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600142 png_read_info(png_ptr, info_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -0600143
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
145 &interlace_type, NULL, NULL);
146
147/**** Set up the data transformations you want. Note that these are all
148 **** optional. Only call them if you want/need them. Many of the
149 **** transformations only work on specific types of images, and many
150 **** are mutually exclusive.
151 ****/
152
153 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
154 png_set_strip_16(png_ptr);
155
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500156 /* Strip alpha bytes from the input data without combining with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600157 * background (not recommended).
158 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 png_set_strip_alpha(png_ptr);
160
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600161 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162 * byte into separate bytes (useful for paletted and grayscale images).
163 */
164 png_set_packing(png_ptr);
165
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600166 /* Change the order of packed pixels to least significant bit first
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 * (not useful if you are using png_set_packing). */
168 png_set_packswap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500169
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600170 /* Expand paletted colors into true RGB triplets */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171 if (color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500172 png_set_expand(png_ptr);
173
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600174 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -0500176 png_set_expand(png_ptr);
177
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600178 /* Expand paletted or RGB images with transparency to full alpha channels
179 * so the data will be available as RGBA quartets.
180 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
Guy Schalnat0d580581995-07-20 02:43:20 -0500182 png_set_expand(png_ptr);
183
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 /* Set the background color to draw transparent and alpha images over.
185 * It is possible to set the red, green, and blue components directly
186 * for paletted images instead of supplying a palette index. Note that
187 * even if the PNG file supplies a background, you are not required to
188 * use it - you should use the (solid) application background if it has one.
189 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500190
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600191 png_color_16 my_background, *image_background;
Guy Schalnat0d580581995-07-20 02:43:20 -0500192
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600193 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
194 png_set_background(png_ptr, image_background,
Guy Schalnate5a37791996-06-05 15:50:50 -0500195 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600196 else
197 png_set_background(png_ptr, &my_background,
Guy Schalnate5a37791996-06-05 15:50:50 -0500198 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500199
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 /* Some suggestions as to how to get a screen gamma value */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600201
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500202 /* Note that screen gamma is the display_exponent, which includes
203 * the CRT_exponent and any correction for viewing conditions */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500204 if (/* We have a user-defined screen gamma value */)
205 {
206 screen_gamma = user-defined screen_gamma;
207 }
208 /* This is one way that applications share the same screen gamma value */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600209 else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 {
211 screen_gamma = atof(gamma_str);
212 }
213 /* If we don't have another value */
214 else
215 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600216 screen_gamma = 2.2; /* A good guess for a PC monitors in a dimly
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600217 lit room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218 screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */
219 }
220
221 /* Tell libpng to handle the gamma conversion for you. The second call
222 * is a good guess for PC generated images, but it should be configurable
223 * by the user at run time by the user. It is strongly suggested that
224 * your application support gamma correction.
225 */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600226
227 int intent;
228
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600229 if (png_get_sRGB(png_ptr, info_ptr, &intent))
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500230 png_set_sRGB(png_ptr, info_ptr, intent);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600231 else
232 {
233 double image_gamma;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600234 if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600235 png_set_gamma(png_ptr, screen_gamma, image_gamma);
236 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600237 png_set_gamma(png_ptr, screen_gamma, 0.45455);
238 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500239
Andreas Dilger47a0c421997-05-16 02:46:07 -0500240 /* Dither RGB files down to 8 bit palette or reduce palettes
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600241 * to the number of colors available on your screen.
242 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500243 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnat0d580581995-07-20 02:43:20 -0500244 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500245 png_uint_32 num_palette;
246 png_colorp palette;
247
248 /* This reduces the image to the application supplied palette */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600249 if (/* we have our own palette */)
Guy Schalnat0d580581995-07-20 02:43:20 -0500250 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 /* An array of colors to which the image should be dithered */
252 png_color std_color_cube[MAX_SCREEN_COLORS];
Guy Schalnat0d580581995-07-20 02:43:20 -0500253
254 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 MAX_SCREEN_COLORS, NULL, 0);
256 }
257 /* This reduces the image to the palette supplied in the file */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600258 else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500259 {
260 png_color16p histogram;
261
262 png_get_hIST(png_ptr, info_ptr, &histogram);
263
264 png_set_dither(png_ptr, palette, num_palette,
265 max_screen_colors, histogram, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500266 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600267 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500268
Guy Schalnate5a37791996-06-05 15:50:50 -0500269 /* invert monocrome files to have 0 as white and 1 as black */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600270 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500271
Andreas Dilger47a0c421997-05-16 02:46:07 -0500272 /* If you want to shift the pixel values from the range [0,255] or
273 * [0,65535] to the original [0,7] or [0,31], or whatever range the
274 * colors were originally in:
275 */
276 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
277 {
278 png_color8p sig_bit;
Guy Schalnat0d580581995-07-20 02:43:20 -0500279
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
281 png_set_shift(png_ptr, sig_bit);
282 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500283
Andreas Dilger47a0c421997-05-16 02:46:07 -0500284 /* flip the RGB pixels to BGR (or RGBA to BGRA) */
285 png_set_bgr(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500286
Andreas Dilger47a0c421997-05-16 02:46:07 -0500287 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
288 png_set_swap_alpha(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500289
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290 /* swap bytes of 16 bit files to least significant byte first */
291 png_set_swap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500292
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293 /* Add filler (or alpha) byte (before/after each RGB triplet) */
294 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
295
296 /* Turn on interlace handling. REQUIRED if you are not using
297 * png_read_image(). To see how to handle interlacing passes,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600298 * see the png_read_row() method below:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500300 number_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500301
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600302 /* Optional call to gamma correct and add the background to the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 * and update info structure. REQUIRED if you are expecting libpng to
304 * update the palette for you (ie you selected such a transform above).
305 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500306 png_read_update_info(png_ptr, info_ptr);
307
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600308 /* Allocate the memory to hold the image using the fields of info_ptr. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500309
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600310 /* The easiest way to read the image: */
Guy Schalnat6d764711995-12-19 03:22:19 -0600311 png_bytep row_pointers[height];
Guy Schalnate5a37791996-06-05 15:50:50 -0500312
313 for (row = 0; row < height; row++)
314 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 row_pointers[row] = malloc(png_get_rowbytes(png_ptr, info_ptr));
Guy Schalnate5a37791996-06-05 15:50:50 -0500316 }
317
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 /* Now it's time to read the image. One of these methods is REQUIRED */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600319#ifdef entire /* Read the entire image in one go */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500320 png_read_image(png_ptr, row_pointers);
321
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600322#else no_entire /* Read the image one or more scanlines at a time */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600323 /* The other way to read images - deal with interlacing: */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500324
Guy Schalnat0d580581995-07-20 02:43:20 -0500325 for (pass = 0; pass < number_passes; pass++)
326 {
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600327#ifdef single /* Read the image a single row at a time */
Guy Schalnat0d580581995-07-20 02:43:20 -0500328 for (y = 0; y < height; y++)
329 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500330 png_read_rows(png_ptr, &row_pointers[y], NULL, 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500331 }
332
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600333#else no_single /* Read the image several rows at a time */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334 for (y = 0; y < height; y += number_of_rows)
335 {
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600336#ifdef sparkle /* Read the image using the "sparkle" effect. */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500337 png_read_rows(png_ptr, &row_pointers[y], NULL, number_of_rows);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600338
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500339 png_read_rows(png_ptr, NULL, row_pointers[y], number_of_rows);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600340#else no_sparkle /* Read the image using the "rectangle" effect */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500341 png_read_rows(png_ptr, NULL, &row_pointers[y], number_of_rows);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600342#endif no_sparkle /* use only one of these two methods */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500343 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600344
Guy Schalnat0d580581995-07-20 02:43:20 -0500345 /* if you want to display the image after every pass, do
346 so here */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600347#endif no_single /* use only one of these two methods */
Guy Schalnat0d580581995-07-20 02:43:20 -0500348 }
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600349#endif no_entire /* use only one of these two methods */
Guy Schalnat0d580581995-07-20 02:43:20 -0500350
Andreas Dilger47a0c421997-05-16 02:46:07 -0500351 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600352 png_read_end(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500353
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354 /* clean up after the read, and free any memory allocated - REQUIRED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500355 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500356
357 /* close the file */
358 fclose(fp);
359
360 /* that's it */
361 return;
362}
363
Guy Schalnat6d764711995-12-19 03:22:19 -0600364/* progressively read a file */
365
Guy Schalnat6d764711995-12-19 03:22:19 -0600366int
Guy Schalnate5a37791996-06-05 15:50:50 -0500367initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600368{
Guy Schalnate5a37791996-06-05 15:50:50 -0500369 /* Create and initialize the png_struct with the desired error handler
Andreas Dilger47a0c421997-05-16 02:46:07 -0500370 * functions. If you want to use the default stderr and longjump method,
371 * you can supply NULL for the last three parameters. We also check that
372 * the library version is compatible in case we are using dynamically
373 * linked libraries.
Guy Schalnate5a37791996-06-05 15:50:50 -0500374 */
375 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500376 png_voidp user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500377
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378 if (*png_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600379 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500380 *info_ptr = NULL;
381 return ERROR;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600382 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600383
Guy Schalnate5a37791996-06-05 15:50:50 -0500384 *info_ptr = png_create_info_struct(png_ptr);
385
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 if (*info_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600387 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
389 return ERROR;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600390 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600391
Guy Schalnate5a37791996-06-05 15:50:50 -0500392 if (setjmp((*png_ptr)->jmpbuf))
393 {
394 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
395 return ERROR;
396 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600397
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600398 /* This one's new. You will need to provide all three
Andreas Dilger47a0c421997-05-16 02:46:07 -0500399 * function callbacks, even if you aren't using them all.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600400 * If you aren't using all functions, you can specify NULL
401 * parameters. Even when all three functions are NULL,
402 * you need to call png_set_progressive_read_fn().
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 * These functions shouldn't be dependent on global or
404 * static variables if you are decoding several images
405 * simultaneously. You should store stream specific data
406 * in a separate struct, given as the second parameter,
407 * and retrieve the pointer from inside the callbacks using
408 * the function png_get_progressive_ptr(png_ptr).
409 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500410 png_set_progressive_read_fn(*png_ptr, (void *)stream_data,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600411 info_callback, row_callback, end_callback);
Guy Schalnat6d764711995-12-19 03:22:19 -0600412
Guy Schalnate5a37791996-06-05 15:50:50 -0500413 return OK;
Guy Schalnat6d764711995-12-19 03:22:19 -0600414}
415
416int
Guy Schalnate5a37791996-06-05 15:50:50 -0500417process_data(png_structp *png_ptr, png_infop *info_ptr,
418 png_bytep buffer, png_uint_32 length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600419{
Guy Schalnate5a37791996-06-05 15:50:50 -0500420 if (setjmp((*png_ptr)->jmpbuf))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600421 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500422 /* Free the png_ptr and info_ptr memory on error */
423 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
424 return ERROR;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600425 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600426
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 /* This one's new also. Simply give it chunks of data as
428 * they arrive from the data stream (in order, of course).
429 * On Segmented machines, don't give it any more than 64K.
430 * The library seems to run fine with sizes of 4K, although
431 * you can give it much less if necessary (I assume you can
432 * give it chunks of 1 byte, but I haven't tried with less
433 * than 256 bytes yet). When this function returns, you may
434 * want to display any rows that were generated in the row
435 * callback, if you aren't already displaying them there.
436 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500437 png_process_data(*png_ptr, *info_ptr, buffer, length);
438 return OK;
Guy Schalnat6d764711995-12-19 03:22:19 -0600439}
440
441info_callback(png_structp png_ptr, png_infop info)
442{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600443/* do any setup here, including setting any of the transformations
Andreas Dilger47a0c421997-05-16 02:46:07 -0500444 * mentioned in the Reading PNG files section. For now, you _must_
445 * call either png_start_read_image() or png_read_update_info()
446 * after all the transformations are set (even if you don't set
447 * any). You may start getting rows before png_process_data()
448 * returns, so this is your last chance to prepare for that.
449 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600450}
451
452row_callback(png_structp png_ptr, png_bytep new_row,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600453 png_uint_32 row_num, int pass)
Guy Schalnat6d764711995-12-19 03:22:19 -0600454{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600455/* this function is called for every row in the image. If the
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 * image is interlacing, and you turned on the interlace handler,
457 * this function will be called for every row in every pass.
458 * Some of these rows will not be changed from the previous pass.
459 * When the row is not changed, the new_row variable will be NULL.
460 * The rows and passes are called in order, so you don't really
461 * need the row_num and pass, but I'm supplying them because it
462 * may make your life easier.
463 *
464 * For the non-NULL rows of interlaced images, you must call
465 * png_progressive_combine_row() passing in the row and the
466 * old row. You can call this function for NULL rows (it will
467 * just return) and for non-interlaced images (it just does the
468 * memcpy for you) if it will make the code easier. Thus, you
469 * can just do this for all cases:
470 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600471
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600472 png_progressive_combine_row(png_ptr, old_row, new_row);
Guy Schalnat6d764711995-12-19 03:22:19 -0600473
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600474/* where old_row is what was displayed for previous rows. Note
Andreas Dilger47a0c421997-05-16 02:46:07 -0500475 * that the first pass (pass == 0 really) will completely cover
476 * the old row, so the rows do not have to be initialized. After
477 * the first pass (and only for interlaced images), you will have
478 * to pass the current row, and the function will combine the
479 * old row and the new row.
480 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600481}
482
483end_callback(png_structp png_ptr, png_infop info)
484{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600485/* this function is called when the whole image has been read,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500486 * including any chunks after the image (up to and including
487 * the IEND). You will usually have the same info chunk as you
488 * had in the header, although some data may have been added
489 * to the comments and time fields.
490 *
491 * Most people won't do much here, perhaps setting a flag that
492 * marks the image as finished.
493 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600494}
495
Guy Schalnat0d580581995-07-20 02:43:20 -0500496/* write a png file */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600497void write_png(char *file_name /* , ... other image information ... */)
Guy Schalnat0d580581995-07-20 02:43:20 -0500498{
499 FILE *fp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600500 png_structp png_ptr;
501 png_infop info_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500502
503 /* open the file */
504 fp = fopen(file_name, "wb");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500505 if (fp == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500506 return;
507
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 /* Create and initialize the png_struct with the desired error handler
Andreas Dilger47a0c421997-05-16 02:46:07 -0500509 * functions. If you want to use the default stderr and longjump method,
510 * you can supply NULL for the last three parameters. We also check that
511 * the library version is compatible with the one used at compile time,
512 * in case we are using dynamically linked libraries. REQUIRED.
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 */
514 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500515 png_voidp user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500516
Andreas Dilger47a0c421997-05-16 02:46:07 -0500517 if (png_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500518 {
519 fclose(fp);
520 return;
521 }
522
Andreas Dilger47a0c421997-05-16 02:46:07 -0500523 /* Allocate/initialize the image information data. REQUIRED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500524 info_ptr = png_create_info_struct(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500525 if (info_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500526 {
527 fclose(fp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500528 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500529 return;
530 }
531
Andreas Dilger47a0c421997-05-16 02:46:07 -0500532 /* Set error handling. REQUIRED if you aren't supplying your own
533 * error hadnling functions in the png_create_write_struct() call.
534 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600535 if (setjmp(png_ptr->jmpbuf))
Guy Schalnat0d580581995-07-20 02:43:20 -0500536 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500537 /* If we get here, we had a problem reading the file */
Guy Schalnate5a37791996-06-05 15:50:50 -0500538 fclose(fp);
539 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500540 return;
541 }
542
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543 /* One of the following I/O initialization functions is REQUIRED */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600544#ifdef streams /* I/O initialization method 1 */
Guy Schalnat69b14481996-01-10 02:56:49 -0600545 /* set up the output control if you are using standard C streams */
Guy Schalnat0d580581995-07-20 02:43:20 -0500546 png_init_io(png_ptr, fp);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600547#else no_streams /* I/O initialization method 2 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500548 /* If you are using replacement read functions, instead of calling
549 * png_init_io() here you would call */
550 png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
551 user_IO_flush_function);
552 /* where user_io_ptr is a structure you want available to the callbacks */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600553#endif no_streams /* only use one initialization method */
Guy Schalnat0d580581995-07-20 02:43:20 -0500554
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 /* Set the image information here. Width and height are up to 2^31,
556 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
557 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
558 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
559 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
560 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
561 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
562 */
563 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???,
564 PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
Guy Schalnat0d580581995-07-20 02:43:20 -0500565
Andreas Dilger47a0c421997-05-16 02:46:07 -0500566 /* set the palette if there is one. REQUIRED for indexed-color images */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600567 palette = (png_colorp)png_malloc(png_ptr, 256 * sizeof (png_color));
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600568 /* ... set palette colors ... */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 png_set_PLTE(png_ptr, info_ptr, palette, 256);
Guy Schalnat0d580581995-07-20 02:43:20 -0500570
571 /* optional significant bit chunk */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600572 /* if we are dealing with a grayscale image then */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500573 sig_bit.gray = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600574 /* otherwise, if we are dealing with a color image then */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500575 sig_bit.red = true_red_bit_depth;
576 sig_bit.green = true_green_bit_depth;
577 sig_bit.blue = true_blue_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600578 /* if the image has an alpha channel then */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 sig_bit.alpha = true_alpha_bit_depth;
580 png_set_sBIT(png_ptr, info_ptr, sig_bit);
581
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600582
Andreas Dilger47a0c421997-05-16 02:46:07 -0500583 /* Optional gamma chunk is strongly suggested if you have any guess
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600584 * as to the correct gamma of the image.
585 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500586 png_set_gAMA(png_ptr, info_ptr, gamma);
Guy Schalnat0d580581995-07-20 02:43:20 -0500587
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588 /* Optionally write comments into the image */
589 text_ptr[0].key = "Title";
590 text_ptr[0].text = "Mona Lisa";
591 text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
592 text_ptr[1].key = "Author";
593 text_ptr[1].text = "Leonardo DaVinci";
594 text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
595 text_ptr[2].key = "Description";
596 text_ptr[2].text = "<long text>";
597 text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500598 png_set_text(png_ptr, info_ptr, text_ptr, 3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500599
Andreas Dilger47a0c421997-05-16 02:46:07 -0500600 /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600601 /* note that if sRGB is present the cHRM chunk must be ignored
602 * on read and must be written in accordance with the sRGB profile */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603
604 /* Write the file header information. REQUIRED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500605 png_write_info(png_ptr, info_ptr);
606
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600607 /* If you want, you can write the info in two steps, in case you need to
608 * write your private chunk ahead of PLTE:
609 *
610 * png_write_info_before_PLTE(write_ptr, write_info_ptr);
611 * write_my_chunk();
612 * png_write_info(png_ptr, info_ptr);
613 */
614
Andreas Dilger47a0c421997-05-16 02:46:07 -0500615 /* Once we write out the header, the compression type on the text
616 * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or
617 * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again
618 * at the end.
619 */
620
Guy Schalnat0d580581995-07-20 02:43:20 -0500621 /* set up the transformations you want. Note that these are
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600622 * all optional. Only call them if you want them.
623 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500624
625 /* invert monocrome pixels */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600626 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500627
Andreas Dilger47a0c421997-05-16 02:46:07 -0500628 /* Shift the pixels up to a legal bit depth and fill in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600629 * as appropriate to correctly scale the image.
630 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500631 png_set_shift(png_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -0500632
633 /* pack pixels into bytes */
634 png_set_packing(png_ptr);
635
Andreas Dilger47a0c421997-05-16 02:46:07 -0500636 /* swap location of alpha bytes from ARGB to RGBA */
637 png_set_swap_alpha(png_ptr);
638
639 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600640 * RGB (4 channels -> 3 channels). The second parameter is not used.
641 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500642 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
643
644 /* flip BGR pixels to RGB */
Guy Schalnat0d580581995-07-20 02:43:20 -0500645 png_set_bgr(png_ptr);
646
Andreas Dilger47a0c421997-05-16 02:46:07 -0500647 /* swap bytes of 16-bit files to most significant byte first */
Guy Schalnat0d580581995-07-20 02:43:20 -0500648 png_set_swap(png_ptr);
649
Andreas Dilger47a0c421997-05-16 02:46:07 -0500650 /* swap bits of 1, 2, 4 bit packed pixel formats */
651 png_set_packswap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500652
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500653 /* turn on interlace handling if you are not using png_write_image() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500654 if (interlacing)
655 number_passes = png_set_interlace_handling(png_ptr);
656 else
657 number_passes = 1;
658
Andreas Dilger47a0c421997-05-16 02:46:07 -0500659 /* The easiest way to write the image (you may have a different memory
660 * layout, however, so choose what fits your needs best). You need to
661 * use the first method if you aren't handling interlacing yourself.
662 */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500663 png_uint_32 k, height, width;
664 png_byte image[height][width];
665 png_bytep row_pointers[height];
666 for (k = 0; k < height; k++)
667 row_pointers[k] = image + k*width;
Guy Schalnate5a37791996-06-05 15:50:50 -0500668
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 /* One of the following output methods is REQUIRED */
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600670#ifdef entire /* write out the entire image data in one call */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500671 png_write_image(png_ptr, row_pointers);
672
673 /* the other way to write the image - deal with interlacing */
674
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600675#else no_entire /* write out the image data by one or more scanlines */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500676 /* The number of passes is either 1 for non-interlaced images,
677 * or 7 for interlaced images.
678 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500679 for (pass = 0; pass < number_passes; pass++)
680 {
681 /* Write a few rows at a time. */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500682 png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500683
684 /* If you are only writing one row at a time, this works */
685 for (y = 0; y < height; y++)
686 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500687 png_write_rows(png_ptr, &row_pointers[y], 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500688 }
689 }
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600690#endif no_entire /* use only one output method */
Guy Schalnat0d580581995-07-20 02:43:20 -0500691
Andreas Dilger47a0c421997-05-16 02:46:07 -0500692 /* You can write optional chunks like tEXt, zTXt, and tIME at the end
693 * as well.
Guy Schalnate5a37791996-06-05 15:50:50 -0500694 */
695
Andreas Dilger47a0c421997-05-16 02:46:07 -0500696 /* It is REQUIRED to call this to finish writing the rest of the file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500697 png_write_end(png_ptr, info_ptr);
698
Guy Schalnat0d580581995-07-20 02:43:20 -0500699 /* if you malloced the palette, free it here */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700 free(info_ptr->palette);
Guy Schalnat0d580581995-07-20 02:43:20 -0500701
Guy Schalnate5a37791996-06-05 15:50:50 -0500702 /* if you allocated any text comments, free them here */
703
704 /* clean up after the write, and free any memory allocated */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500705 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500706
707 /* close the file */
708 fclose(fp);
709
710 /* that's it */
711 return;
712}
713
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500714#endif /* if 0 */