blob: 702fc966a73fd5f1559a8b721ca4245f76a5a40e [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001/* example.c - an example of using libpng */
2
Andreas Dilger47a0c421997-05-16 02:46:07 -05003/* This is an example of how to use libpng to read and write PNG files.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004 * The file libpng.txt is much more verbose then this. If you have not
5 * read it, do so first. This was designed to be a starting point of an
6 * implementation. This is not officially part of libpng, and therefore
7 * does not require a copyright notice.
8 *
9 * This file does not currently compile, because it is missing certain
10 * parts, like allocating memory to hold an image. You will have to
11 * supply these parts to get it to compile. For an example of a minimal
12 * working PNG reader/writer, see pngtest.c, included in this distribution.
13 */
Guy Schalnat0d580581995-07-20 02:43:20 -050014
15#include <png.h>
16
Andreas Dilger47a0c421997-05-16 02:46:07 -050017/* Check to see if a file is a PNG file using png_check_sig(). Returns
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060018 * non-zero if the image is a PNG, and 0 if it isn't a PNG.
19 *
20 * If this call is successful, and you are going to keep the file open,
21 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once
22 * you have created the png_ptr, so that libpng knows your application
23 * has read that many bytes from the start of the file. Make sure you
24 * don't call png_set_sig_bytes() with more than 8 bytes read or give it
25 * an incorrect number of bytes read, or you will either have read too
26 * many bytes (your fault), or you are telling libpng to read the wrong
27 * number of magic bytes (also your fault).
28 *
29 * Many applications already read the first 2 or 4 bytes from the start
30 * of the image to determine the file type, so it would be easiest just
31 * to pass the bytes to png_check_sig() or even skip that if you know
32 * you have a PNG file, and call png_set_sig_bytes().
33 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050034#define PNG_BYTES_TO_CHECK 4
35int check_if_png(char *file_name, FILE **fp)
Guy Schalnat0d580581995-07-20 02:43:20 -050036{
Andreas Dilger47a0c421997-05-16 02:46:07 -050037 char buf[PNG_BYTES_TO_CHECK];
Guy Schalnat0d580581995-07-20 02:43:20 -050038
Andreas Dilger47a0c421997-05-16 02:46:07 -050039 /* Open the prospective PNG file. */
40 if ((*fp = fopen(file_name, "rb")) != NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -050041 return 0;
42
Andreas Dilger47a0c421997-05-16 02:46:07 -050043 /* Read in the signature bytes */
44 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK)
45 return 0;
Guy Schalnat0d580581995-07-20 02:43:20 -050046
Andreas Dilger47a0c421997-05-16 02:46:07 -050047 /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */
48 return(png_check_sig(buf, PNG_BYTES_TO_CHECK));
Guy Schalnat0d580581995-07-20 02:43:20 -050049}
50
Andreas Dilger47a0c421997-05-16 02:46:07 -050051/* Read a PNG file. You may want to return an error code if the read
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060052 * fails (depending upon the failure). There are two "prototypes" given
53 * here - one where we are given the filename, and we need to open the
54 * file, and the other where we are given an open file (possibly with
55 * some or all of the magic bytes read - see comments above).
56 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050057**** prototype 1 ****
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060058void read_png(char *file_name) /* We need to open the file */
Guy Schalnat0d580581995-07-20 02:43:20 -050059{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060060 png_structp png_ptr;
Guy Schalnat6d764711995-12-19 03:22:19 -060061 png_infop info_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -050062 unsigned int sig_read = 0;
63 png_uint_32 width, height;
64 int bit_depth, color_type, interlace_type;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060065 FILE *fp;
Guy Schalnat0d580581995-07-20 02:43:20 -050066
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 if ((fp = fopen(file_name, "rb")) == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -050068 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050069**** prototype 2 ****
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060070void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
71{
72 png_structp png_ptr;
73 png_infop info_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -050074 png_uint_32 width, height;
75 int bit_depth, color_type, interlace_type;
76**** only use one prototype! ****
Guy Schalnat0d580581995-07-20 02:43:20 -050077
Guy Schalnate5a37791996-06-05 15:50:50 -050078 /* Create and initialize the png_struct with the desired error handler
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 * functions. If you want to use the default stderr and longjump method,
80 * you can supply NULL for the last three parameters. We also supply the
81 * the compiler header file version, so that we know if the application
82 * was compiled with a compatible version of the library. REQUIRED
83 */
Guy Schalnate5a37791996-06-05 15:50:50 -050084 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
85 (void *)user_error_ptr, user_error_fn, user_warning_fn);
86
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 if (png_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -050088 {
89 fclose(fp);
90 return;
91 }
92
Andreas Dilger47a0c421997-05-16 02:46:07 -050093 /* Allocate/initialize the memory for image information. REQUIRED. */
Guy Schalnate5a37791996-06-05 15:50:50 -050094 info_ptr = png_create_info_struct();
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 if (info_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -050096 {
97 fclose(fp);
Andreas Dilger47a0c421997-05-16 02:46:07 -050098 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -050099 return;
100 }
101
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 /* Set error handling if you are using the setjmp/longjmp method (this is
103 * the normal method of doing things with libpng). REQUIRED unless you
104 * set up your own error handlers in the png_create_read_struct() earlier.
105 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 if (setjmp(png_ptr->jmpbuf))
107 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500108 /* Free all of the memory associated with the png_ptr and info_ptr */
109 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500110 fclose(fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500111 /* If we get here, we had a problem reading the file */
112 return;
113 }
114
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115 /* One of the following I/O initialization methods is REQUIRED */
116**** PNG file I/O method 1 ****
117 /* Set up the input control if you are using standard C streams */
Guy Schalnat0d580581995-07-20 02:43:20 -0500118 png_init_io(png_ptr, fp);
119
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120**** PNG file I/O method 2 ****
121 /* If you are using replacement read functions, instead of calling
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600122 * png_init_io() here you would call:
123 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500124 png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
125 /* where user_io_ptr is a structure you want available to the callbacks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500126**** Use only one I/O method! ****
Guy Schalnat69b14481996-01-10 02:56:49 -0600127
Andreas Dilger47a0c421997-05-16 02:46:07 -0500128 /* If we have already read some of the signature */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600129 png_set_sig_bytes_read(png_ptr, sig_read);
130
Andreas Dilger47a0c421997-05-16 02:46:07 -0500131 /* The call to png_read_info() gives us all of the information from the
132 * PNG file before the first IDAT (image data chunk). REQUIRED
133 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600134 png_read_info(png_ptr, info_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -0600135
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
137 &interlace_type, NULL, NULL);
138
139/**** Set up the data transformations you want. Note that these are all
140 **** optional. Only call them if you want/need them. Many of the
141 **** transformations only work on specific types of images, and many
142 **** are mutually exclusive.
143 ****/
144
145 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
146 png_set_strip_16(png_ptr);
147
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600148 /* Strip alpha bytes from the input data without combining with th
149 * background (not recommended).
150 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151 png_set_strip_alpha(png_ptr);
152
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600153 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
Andreas Dilger47a0c421997-05-16 02:46:07 -0500154 * byte into separate bytes (useful for paletted and grayscale images).
155 */
156 png_set_packing(png_ptr);
157
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600158 /* Change the order of packed pixels to least significant bit first
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 * (not useful if you are using png_set_packing). */
160 png_set_packswap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500161
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600162 /* Expand paletted colors into true RGB triplets */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500163 if (color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500164 png_set_expand(png_ptr);
165
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600166 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -0500168 png_set_expand(png_ptr);
169
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600170 /* Expand paletted or RGB images with transparency to full alpha channels
171 * so the data will be available as RGBA quartets.
172 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
Guy Schalnat0d580581995-07-20 02:43:20 -0500174 png_set_expand(png_ptr);
175
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176 /* Set the background color to draw transparent and alpha images over.
177 * It is possible to set the red, green, and blue components directly
178 * for paletted images instead of supplying a palette index. Note that
179 * even if the PNG file supplies a background, you are not required to
180 * use it - you should use the (solid) application background if it has one.
181 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500182
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 png_color_16 my_background, *image_background);
Guy Schalnat0d580581995-07-20 02:43:20 -0500184
Andreas Dilger47a0c421997-05-16 02:46:07 -0500185 if (png_get_bKGD(png_ptr, info_ptr, &image_background);
186 png_set_background(png_ptr, image_background),
Guy Schalnate5a37791996-06-05 15:50:50 -0500187 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188 else
189 png_set_background(png_ptr, &my_background,
Guy Schalnate5a37791996-06-05 15:50:50 -0500190 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500191
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 /* Some suggestions as to how to get a screen gamma value */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600193
194 /* Note that screen gamma is (display_gamma/viewing_gamma)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 if (/* We have a user-defined screen gamma value */)
196 {
197 screen_gamma = user-defined screen_gamma;
198 }
199 /* This is one way that applications share the same screen gamma value */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600200 else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500201 {
202 screen_gamma = atof(gamma_str);
203 }
204 /* If we don't have another value */
205 else
206 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600207 screen_gamma = 2.2; /* A good guess for a PC monitors in a dimly
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600208 lit room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500209 screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */
210 }
211
212 /* Tell libpng to handle the gamma conversion for you. The second call
213 * is a good guess for PC generated images, but it should be configurable
214 * by the user at run time by the user. It is strongly suggested that
215 * your application support gamma correction.
216 */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600217
218 int intent;
219
220 if (png_get_sRGB(png_ptr, info_ptr, &intent)
221 png_set_sRGB(png_ptr, intent, 0);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600222 else
223 if (png_get_gAMA(png_ptr, info_ptr, &image_gamma)
224 png_set_gamma(png_ptr, screen_gamma, image_gamma);
225 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600226 png_set_gamma(png_ptr, screen_gamma, 0.50);
Guy Schalnat0d580581995-07-20 02:43:20 -0500227
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228 /* Dither RGB files down to 8 bit palette or reduce palettes
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600229 * to the number of colors available on your screen.
230 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500231 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnat0d580581995-07-20 02:43:20 -0500232 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 png_uint_32 num_palette;
234 png_colorp palette;
235
236 /* This reduces the image to the application supplied palette */
237 if (we have our own palette)
Guy Schalnat0d580581995-07-20 02:43:20 -0500238 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239 /* An array of colors to which the image should be dithered */
240 png_color std_color_cube[MAX_SCREEN_COLORS];
Guy Schalnat0d580581995-07-20 02:43:20 -0500241
242 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500243 MAX_SCREEN_COLORS, NULL, 0);
244 }
245 /* This reduces the image to the palette supplied in the file */
246 else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)))
247 {
248 png_color16p histogram;
249
250 png_get_hIST(png_ptr, info_ptr, &histogram);
251
252 png_set_dither(png_ptr, palette, num_palette,
253 max_screen_colors, histogram, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500254 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600255 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500256
Guy Schalnate5a37791996-06-05 15:50:50 -0500257 /* invert monocrome files to have 0 as white and 1 as black */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600258 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500259
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260 /* If you want to shift the pixel values from the range [0,255] or
261 * [0,65535] to the original [0,7] or [0,31], or whatever range the
262 * colors were originally in:
263 */
264 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
265 {
266 png_color8p sig_bit;
Guy Schalnat0d580581995-07-20 02:43:20 -0500267
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
269 png_set_shift(png_ptr, sig_bit);
270 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500271
Andreas Dilger47a0c421997-05-16 02:46:07 -0500272 /* flip the RGB pixels to BGR (or RGBA to BGRA) */
273 png_set_bgr(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500274
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
276 png_set_swap_alpha(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500277
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 /* swap bytes of 16 bit files to least significant byte first */
279 png_set_swap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500280
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281 /* Add filler (or alpha) byte (before/after each RGB triplet) */
282 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
283
284 /* Turn on interlace handling. REQUIRED if you are not using
285 * png_read_image(). To see how to handle interlacing passes,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600286 * see the png_read_row() method below:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500287 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500288 number_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500289
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600290 /* Optional call to gamma correct and add the background to the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291 * and update info structure. REQUIRED if you are expecting libpng to
292 * update the palette for you (ie you selected such a transform above).
293 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500294 png_read_update_info(png_ptr, info_ptr);
295
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600296 /* Allocate the memory to hold the image using the fields of info_ptr. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500297
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600298 /* The easiest way to read the image: */
Guy Schalnat6d764711995-12-19 03:22:19 -0600299 png_bytep row_pointers[height];
Guy Schalnate5a37791996-06-05 15:50:50 -0500300
301 for (row = 0; row < height; row++)
302 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 row_pointers[row] = malloc(png_get_rowbytes(png_ptr, info_ptr));
Guy Schalnate5a37791996-06-05 15:50:50 -0500304 }
305
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306 /* Now it's time to read the image. One of these methods is REQUIRED */
307**** Read the entire image in one go ****
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500308 png_read_image(png_ptr, row_pointers);
309
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310**** Read the image one or more scanlines at a time ****
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600311 /* The other way to read images - deal with interlacing: */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500312
Guy Schalnat0d580581995-07-20 02:43:20 -0500313 for (pass = 0; pass < number_passes; pass++)
314 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315[[[[[[[ Read the image a single row at a time ]]]]]]]
Guy Schalnat0d580581995-07-20 02:43:20 -0500316 for (y = 0; y < height; y++)
317 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600318 png_bytep row_pointers = row[y];
Guy Schalnat0d580581995-07-20 02:43:20 -0500319 png_read_rows(png_ptr, &row_pointers, NULL, 1);
320 }
321
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322[[[[[[[ Read the image several rows at a time ]]]]]]]
323 for (y = 0; y < height; y += number_of_rows)
324 {
325<<<<<<<<<< Read the image using the "sparkle" effect. >>>>>>>>>>
326 png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
327
328<<<<<<<<<< Read the image using the "rectangle" effect >>>>>>>>>>
329 png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);
330<<<<<<<<<< use only one of these two methods >>>>>>>>>>
331 }
332
Guy Schalnat0d580581995-07-20 02:43:20 -0500333 /* if you want to display the image after every pass, do
334 so here */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500335[[[[[[[ use only one of these two methods ]]]]]]]
Guy Schalnat0d580581995-07-20 02:43:20 -0500336 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337**** use only one of these two methods ****
Guy Schalnat0d580581995-07-20 02:43:20 -0500338
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600340 png_read_end(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500341
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 /* clean up after the read, and free any memory allocated - REQUIRED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500343 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500344
345 /* close the file */
346 fclose(fp);
347
348 /* that's it */
349 return;
350}
351
Guy Schalnat6d764711995-12-19 03:22:19 -0600352/* progressively read a file */
353
Guy Schalnat6d764711995-12-19 03:22:19 -0600354int
Guy Schalnate5a37791996-06-05 15:50:50 -0500355initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -0600356{
Guy Schalnate5a37791996-06-05 15:50:50 -0500357 /* Create and initialize the png_struct with the desired error handler
Andreas Dilger47a0c421997-05-16 02:46:07 -0500358 * functions. If you want to use the default stderr and longjump method,
359 * you can supply NULL for the last three parameters. We also check that
360 * the library version is compatible in case we are using dynamically
361 * linked libraries.
Guy Schalnate5a37791996-06-05 15:50:50 -0500362 */
363 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
364 (void *)user_error_ptr, user_error_fn, user_warning_fn);
365
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 if (*png_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600367 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500368 *info_ptr = NULL;
369 return ERROR;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600370 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600371
Guy Schalnate5a37791996-06-05 15:50:50 -0500372 *info_ptr = png_create_info_struct(png_ptr);
373
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 if (*info_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600375 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500376 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
377 return ERROR;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600378 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600379
Guy Schalnate5a37791996-06-05 15:50:50 -0500380 if (setjmp((*png_ptr)->jmpbuf))
381 {
382 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
383 return ERROR;
384 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600385
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600386 /* this one's new. You will need to provide all three
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 * function callbacks, even if you aren't using them all.
388 * These functions shouldn't be dependent on global or
389 * static variables if you are decoding several images
390 * simultaneously. You should store stream specific data
391 * in a separate struct, given as the second parameter,
392 * and retrieve the pointer from inside the callbacks using
393 * the function png_get_progressive_ptr(png_ptr).
394 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500395 png_set_progressive_read_fn(*png_ptr, (void *)stream_data,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600396 info_callback, row_callback, end_callback);
Guy Schalnat6d764711995-12-19 03:22:19 -0600397
Guy Schalnate5a37791996-06-05 15:50:50 -0500398 return OK;
Guy Schalnat6d764711995-12-19 03:22:19 -0600399}
400
401int
Guy Schalnate5a37791996-06-05 15:50:50 -0500402process_data(png_structp *png_ptr, png_infop *info_ptr,
403 png_bytep buffer, png_uint_32 length)
Guy Schalnat6d764711995-12-19 03:22:19 -0600404{
Guy Schalnate5a37791996-06-05 15:50:50 -0500405 if (setjmp((*png_ptr)->jmpbuf))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600406 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500407 /* Free the png_ptr and info_ptr memory on error */
408 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
409 return ERROR;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600410 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600411
Andreas Dilger47a0c421997-05-16 02:46:07 -0500412 /* This one's new also. Simply give it chunks of data as
413 * they arrive from the data stream (in order, of course).
414 * On Segmented machines, don't give it any more than 64K.
415 * The library seems to run fine with sizes of 4K, although
416 * you can give it much less if necessary (I assume you can
417 * give it chunks of 1 byte, but I haven't tried with less
418 * than 256 bytes yet). When this function returns, you may
419 * want to display any rows that were generated in the row
420 * callback, if you aren't already displaying them there.
421 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500422 png_process_data(*png_ptr, *info_ptr, buffer, length);
423 return OK;
Guy Schalnat6d764711995-12-19 03:22:19 -0600424}
425
426info_callback(png_structp png_ptr, png_infop info)
427{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600428/* do any setup here, including setting any of the transformations
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 * mentioned in the Reading PNG files section. For now, you _must_
430 * call either png_start_read_image() or png_read_update_info()
431 * after all the transformations are set (even if you don't set
432 * any). You may start getting rows before png_process_data()
433 * returns, so this is your last chance to prepare for that.
434 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600435}
436
437row_callback(png_structp png_ptr, png_bytep new_row,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600438 png_uint_32 row_num, int pass)
Guy Schalnat6d764711995-12-19 03:22:19 -0600439{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600440/* this function is called for every row in the image. If the
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 * image is interlacing, and you turned on the interlace handler,
442 * this function will be called for every row in every pass.
443 * Some of these rows will not be changed from the previous pass.
444 * When the row is not changed, the new_row variable will be NULL.
445 * The rows and passes are called in order, so you don't really
446 * need the row_num and pass, but I'm supplying them because it
447 * may make your life easier.
448 *
449 * For the non-NULL rows of interlaced images, you must call
450 * png_progressive_combine_row() passing in the row and the
451 * old row. You can call this function for NULL rows (it will
452 * just return) and for non-interlaced images (it just does the
453 * memcpy for you) if it will make the code easier. Thus, you
454 * can just do this for all cases:
455 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600456
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600457 png_progressive_combine_row(png_ptr, old_row, new_row);
Guy Schalnat6d764711995-12-19 03:22:19 -0600458
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600459/* where old_row is what was displayed for previous rows. Note
Andreas Dilger47a0c421997-05-16 02:46:07 -0500460 * that the first pass (pass == 0 really) will completely cover
461 * the old row, so the rows do not have to be initialized. After
462 * the first pass (and only for interlaced images), you will have
463 * to pass the current row, and the function will combine the
464 * old row and the new row.
465 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600466}
467
468end_callback(png_structp png_ptr, png_infop info)
469{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600470/* this function is called when the whole image has been read,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471 * including any chunks after the image (up to and including
472 * the IEND). You will usually have the same info chunk as you
473 * had in the header, although some data may have been added
474 * to the comments and time fields.
475 *
476 * Most people won't do much here, perhaps setting a flag that
477 * marks the image as finished.
478 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600479}
480
Guy Schalnat0d580581995-07-20 02:43:20 -0500481/* write a png file */
482void write_png(char *file_name, ... other image information ...)
483{
484 FILE *fp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600485 png_structp png_ptr;
486 png_infop info_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500487
488 /* open the file */
489 fp = fopen(file_name, "wb");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 if (fp == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 return;
492
Guy Schalnate5a37791996-06-05 15:50:50 -0500493 /* Create and initialize the png_struct with the desired error handler
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494 * functions. If you want to use the default stderr and longjump method,
495 * you can supply NULL for the last three parameters. We also check that
496 * the library version is compatible with the one used at compile time,
497 * in case we are using dynamically linked libraries. REQUIRED.
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 */
499 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
500 (void *)user_error_ptr, user_error_fn, user_warning_fn);
501
Andreas Dilger47a0c421997-05-16 02:46:07 -0500502 if (png_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500503 {
504 fclose(fp);
505 return;
506 }
507
Andreas Dilger47a0c421997-05-16 02:46:07 -0500508 /* Allocate/initialize the image information data. REQUIRED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500509 info_ptr = png_create_info_struct(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510 if (info_ptr == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500511 {
512 fclose(fp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500514 return;
515 }
516
Andreas Dilger47a0c421997-05-16 02:46:07 -0500517 /* Set error handling. REQUIRED if you aren't supplying your own
518 * error hadnling functions in the png_create_write_struct() call.
519 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600520 if (setjmp(png_ptr->jmpbuf))
Guy Schalnat0d580581995-07-20 02:43:20 -0500521 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500522 /* If we get here, we had a problem reading the file */
Guy Schalnate5a37791996-06-05 15:50:50 -0500523 fclose(fp);
524 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500525 return;
526 }
527
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 /* One of the following I/O initialization functions is REQUIRED */
529**** I/O initialization method 1 ****
Guy Schalnat69b14481996-01-10 02:56:49 -0600530 /* set up the output control if you are using standard C streams */
Guy Schalnat0d580581995-07-20 02:43:20 -0500531 png_init_io(png_ptr, fp);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500532**** I/O initialization method 2 ****
533 /* If you are using replacement read functions, instead of calling
534 * png_init_io() here you would call */
535 png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
536 user_IO_flush_function);
537 /* where user_io_ptr is a structure you want available to the callbacks */
538**** only use 1 initialization method ****
Guy Schalnat0d580581995-07-20 02:43:20 -0500539
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 /* Set the image information here. Width and height are up to 2^31,
541 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
542 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
543 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
544 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
545 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
546 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
547 */
548 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???,
549 PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
Guy Schalnat0d580581995-07-20 02:43:20 -0500550
Andreas Dilger47a0c421997-05-16 02:46:07 -0500551 /* set the palette if there is one. REQUIRED for indexed-color images */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600552 palette = (png_colorp)png_malloc(png_ptr, 256 * sizeof (png_color));
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 ... set palette colors ...
Andreas Dilger47a0c421997-05-16 02:46:07 -0500554 png_set_PLTE(png_ptr, info_ptr, palette, 256);
Guy Schalnat0d580581995-07-20 02:43:20 -0500555
556 /* optional significant bit chunk */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600557 /* if we are dealing with a grayscale image then */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 sig_bit.gray = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600559 /* otherwise, if we are dealing with a color image then */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500560 sig_bit.red = true_red_bit_depth;
561 sig_bit.green = true_green_bit_depth;
562 sig_bit.blue = true_blue_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600563 /* if the image has an alpha channel then */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564 sig_bit.alpha = true_alpha_bit_depth;
565 png_set_sBIT(png_ptr, info_ptr, sig_bit);
566
Guy Schalnat69b14481996-01-10 02:56:49 -0600567
Andreas Dilger47a0c421997-05-16 02:46:07 -0500568 /* Optional gamma chunk is strongly suggested if you have any guess
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600569 * as to the correct gamma of the image.
570 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571 png_set_gAMA(png_ptr, info_ptr, gamma);
Guy Schalnat0d580581995-07-20 02:43:20 -0500572
Andreas Dilger47a0c421997-05-16 02:46:07 -0500573 /* Optionally write comments into the image */
574 text_ptr[0].key = "Title";
575 text_ptr[0].text = "Mona Lisa";
576 text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
577 text_ptr[1].key = "Author";
578 text_ptr[1].text = "Leonardo DaVinci";
579 text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
580 text_ptr[2].key = "Description";
581 text_ptr[2].text = "<long text>";
582 text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt;
583 png_set_text(png_ptr, info_ptr, text_ptr, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500584
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600586 /* note that if sRGB is present the cHRM chunk must be ignored
587 * on read and must be written in accordance with the sRGB profile */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588
589 /* Write the file header information. REQUIRED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500590 png_write_info(png_ptr, info_ptr);
591
Andreas Dilger47a0c421997-05-16 02:46:07 -0500592 /* Once we write out the header, the compression type on the text
593 * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or
594 * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again
595 * at the end.
596 */
597
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 /* set up the transformations you want. Note that these are
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600599 * all optional. Only call them if you want them.
600 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500601
602 /* invert monocrome pixels */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600603 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500604
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605 /* Shift the pixels up to a legal bit depth and fill in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600606 * as appropriate to correctly scale the image.
607 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 png_set_shift(png_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -0500609
610 /* pack pixels into bytes */
611 png_set_packing(png_ptr);
612
Andreas Dilger47a0c421997-05-16 02:46:07 -0500613 /* swap location of alpha bytes from ARGB to RGBA */
614 png_set_swap_alpha(png_ptr);
615
616 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600617 * RGB (4 channels -> 3 channels). The second parameter is not used.
618 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500619 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
620
621 /* flip BGR pixels to RGB */
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 png_set_bgr(png_ptr);
623
Andreas Dilger47a0c421997-05-16 02:46:07 -0500624 /* swap bytes of 16-bit files to most significant byte first */
Guy Schalnat0d580581995-07-20 02:43:20 -0500625 png_set_swap(png_ptr);
626
Andreas Dilger47a0c421997-05-16 02:46:07 -0500627 /* swap bits of 1, 2, 4 bit packed pixel formats */
628 png_set_packswap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500629
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500630 /* turn on interlace handling if you are not using png_write_image() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500631 if (interlacing)
632 number_passes = png_set_interlace_handling(png_ptr);
633 else
634 number_passes = 1;
635
Andreas Dilger47a0c421997-05-16 02:46:07 -0500636 /* The easiest way to write the image (you may have a different memory
637 * layout, however, so choose what fits your needs best). You need to
638 * use the first method if you aren't handling interlacing yourself.
639 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500640 png_byte row_pointers[height][width];
641
Andreas Dilger47a0c421997-05-16 02:46:07 -0500642 /* One of the following output methods is REQUIRED */
643**** write out the entire image data in one call ***
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500644 png_write_image(png_ptr, row_pointers);
645
646 /* the other way to write the image - deal with interlacing */
647
Andreas Dilger47a0c421997-05-16 02:46:07 -0500648**** write out the image data by one or more scanlines ****
649 /* The number of passes is either 1 for non-interlaced images,
650 * or 7 for interlaced images.
651 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500652 for (pass = 0; pass < number_passes; pass++)
653 {
654 /* Write a few rows at a time. */
655 png_write_rows(png_ptr, row_pointers, number_of_rows);
656
657 /* If you are only writing one row at a time, this works */
658 for (y = 0; y < height; y++)
659 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600660 png_bytep row_pointers = row[y];
Guy Schalnat0d580581995-07-20 02:43:20 -0500661 png_write_rows(png_ptr, &row_pointers, 1);
662 }
663 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500664**** use only one output method ****
Guy Schalnat0d580581995-07-20 02:43:20 -0500665
Andreas Dilger47a0c421997-05-16 02:46:07 -0500666 /* You can write optional chunks like tEXt, zTXt, and tIME at the end
667 * as well.
Guy Schalnate5a37791996-06-05 15:50:50 -0500668 */
669
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670 /* It is REQUIRED to call this to finish writing the rest of the file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500671 png_write_end(png_ptr, info_ptr);
672
Guy Schalnat0d580581995-07-20 02:43:20 -0500673 /* if you malloced the palette, free it here */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500674 free(info_ptr->palette);
Guy Schalnat0d580581995-07-20 02:43:20 -0500675
Guy Schalnate5a37791996-06-05 15:50:50 -0500676 /* if you allocated any text comments, free them here */
677
678 /* clean up after the write, and free any memory allocated */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500679 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500680
681 /* close the file */
682 fclose(fp);
683
684 /* that's it */
685 return;
686}
687