]> git.saurik.com Git - wxWidgets.git/blob - src/png/example.c
1. wxStaticBox doesn't draw over the underlying controls any more
[wxWidgets.git] / src / png / example.c
1
2 /* example.c - an example of using libpng */
3
4 /* This is an example of how to use libpng to read and write PNG files.
5 * The file libpng.txt is much more verbose then this. If you have not
6 * read it, do so first. This was designed to be a starting point of an
7 * implementation. This is not officially part of libpng, and therefore
8 * does not require a copyright notice.
9 *
10 * This file does not currently compile, because it is missing certain
11 * parts, like allocating memory to hold an image. You will have to
12 * supply these parts to get it to compile. For an example of a minimal
13 * working PNG reader/writer, see pngtest.c, included in this distribution.
14 */
15
16 #include "png.h"
17
18 /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp()
19 * returns zero if the image is a PNG and nonzero if it isn't a PNG.
20 *
21 * The function check_if_png() shown here, but not used, returns nonzero (true)
22 * if the file can be opened and is a PNG, 0 (false) otherwise.
23 *
24 * If this call is successful, and you are going to keep the file open,
25 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once
26 * you have created the png_ptr, so that libpng knows your application
27 * has read that many bytes from the start of the file. Make sure you
28 * don't call png_set_sig_bytes() with more than 8 bytes read or give it
29 * an incorrect number of bytes read, or you will either have read too
30 * many bytes (your fault), or you are telling libpng to read the wrong
31 * number of magic bytes (also your fault).
32 *
33 * Many applications already read the first 2 or 4 bytes from the start
34 * of the image to determine the file type, so it would be easiest just
35 * to pass the bytes to png_sig_cmp() or even skip that if you know
36 * you have a PNG file, and call png_set_sig_bytes().
37 */
38 #define PNG_BYTES_TO_CHECK 4
39 int check_if_png(char *file_name, FILE **fp)
40 {
41 char buf[PNG_BYTES_TO_CHECK];
42
43 /* Open the prospective PNG file. */
44 if ((*fp = fopen(file_name, "rb")) != NULL);
45 return 0;
46
47 /* Read in some of the signature bytes */
48 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK)
49 return 0;
50
51 /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
52 Return nonzero (true) if they match */
53
54 return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
55 }
56
57 /* Read a PNG file. You may want to return an error code if the read
58 * fails (depending upon the failure). There are two "prototypes" given
59 * here - one where we are given the filename, and we need to open the
60 * file, and the other where we are given an open file (possibly with
61 * some or all of the magic bytes read - see comments above).
62 */
63 #ifdef open_file /* prototype 1 */
64 void read_png(char *file_name) /* We need to open the file */
65 {
66 png_structp png_ptr;
67 png_infop info_ptr;
68 unsigned int sig_read = 0;
69 png_uint_32 width, height;
70 int bit_depth, color_type, interlace_type;
71 FILE *fp;
72
73 if ((fp = fopen(file_name, "rb")) == NULL)
74 return;
75 #else no_open_file /* prototype 2 */
76 void read_png(FILE *fp, unsigned int sig_read) /* file is already open */
77 {
78 png_structp png_ptr;
79 png_infop info_ptr;
80 png_uint_32 width, height;
81 int bit_depth, color_type, interlace_type;
82 #endif no_open_file /* only use one prototype! */
83
84 /* Create and initialize the png_struct with the desired error handler
85 * functions. If you want to use the default stderr and longjump method,
86 * you can supply NULL for the last three parameters. We also supply the
87 * the compiler header file version, so that we know if the application
88 * was compiled with a compatible version of the library. REQUIRED
89 */
90 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
91 png_voidp user_error_ptr, user_error_fn, user_warning_fn);
92
93 if (png_ptr == NULL)
94 {
95 fclose(fp);
96 return;
97 }
98
99 /* Allocate/initialize the memory for image information. REQUIRED. */
100 info_ptr = png_create_info_struct(png_ptr);
101 if (info_ptr == NULL)
102 {
103 fclose(fp);
104 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
105 return;
106 }
107
108 /* Set error handling if you are using the setjmp/longjmp method (this is
109 * the normal method of doing things with libpng). REQUIRED unless you
110 * set up your own error handlers in the png_create_read_struct() earlier.
111 */
112 if (setjmp(png_ptr->jmpbuf))
113 {
114 /* Free all of the memory associated with the png_ptr and info_ptr */
115 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
116 fclose(fp);
117 /* If we get here, we had a problem reading the file */
118 return;
119 }
120
121 /* One of the following I/O initialization methods is REQUIRED */
122 #ifdef streams /* PNG file I/O method 1 */
123 /* Set up the input control if you are using standard C streams */
124 png_init_io(png_ptr, fp);
125
126 #else no_streams /* PNG file I/O method 2 */
127 /* If you are using replacement read functions, instead of calling
128 * png_init_io() here you would call:
129 */
130 png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
131 /* where user_io_ptr is a structure you want available to the callbacks */
132 #endif no_streams /* Use only one I/O method! */
133
134 /* If we have already read some of the signature */
135 png_set_sig_bytes(png_ptr, sig_read);
136
137 /* The call to png_read_info() gives us all of the information from the
138 * PNG file before the first IDAT (image data chunk). REQUIRED
139 */
140 png_read_info(png_ptr, info_ptr);
141
142 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
143 &interlace_type, NULL, NULL);
144
145 /**** Set up the data transformations you want. Note that these are all
146 **** optional. Only call them if you want/need them. Many of the
147 **** transformations only work on specific types of images, and many
148 **** are mutually exclusive.
149 ****/
150
151 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
152 png_set_strip_16(png_ptr);
153
154 /* Strip alpha bytes from the input data without combining with th
155 * background (not recommended).
156 */
157 png_set_strip_alpha(png_ptr);
158
159 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
160 * byte into separate bytes (useful for paletted and grayscale images).
161 */
162 png_set_packing(png_ptr);
163
164 /* Change the order of packed pixels to least significant bit first
165 * (not useful if you are using png_set_packing). */
166 png_set_packswap(png_ptr);
167
168 /* Expand paletted colors into true RGB triplets */
169 if (color_type == PNG_COLOR_TYPE_PALETTE)
170 png_set_expand(png_ptr);
171
172 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
173 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
174 png_set_expand(png_ptr);
175
176 /* Expand paletted or RGB images with transparency to full alpha channels
177 * so the data will be available as RGBA quartets.
178 */
179 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
180 png_set_expand(png_ptr);
181
182 /* Set the background color to draw transparent and alpha images over.
183 * It is possible to set the red, green, and blue components directly
184 * for paletted images instead of supplying a palette index. Note that
185 * even if the PNG file supplies a background, you are not required to
186 * use it - you should use the (solid) application background if it has one.
187 */
188
189 png_color_16 my_background, *image_background;
190
191 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
192 png_set_background(png_ptr, image_background,
193 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
194 else
195 png_set_background(png_ptr, &my_background,
196 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
197
198 /* Some suggestions as to how to get a screen gamma value */
199
200 /* Note that screen gamma is (display_gamma/viewing_gamma) */
201 if (/* We have a user-defined screen gamma value */)
202 {
203 screen_gamma = user-defined screen_gamma;
204 }
205 /* This is one way that applications share the same screen gamma value */
206 else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL)
207 {
208 screen_gamma = atof(gamma_str);
209 }
210 /* If we don't have another value */
211 else
212 {
213 screen_gamma = 2.2; /* A good guess for a PC monitors in a dimly
214 lit room */
215 screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */
216 }
217
218 /* Tell libpng to handle the gamma conversion for you. The second call
219 * is a good guess for PC generated images, but it should be configurable
220 * by the user at run time by the user. It is strongly suggested that
221 * your application support gamma correction.
222 */
223
224 int intent;
225
226 if (png_get_sRGB(png_ptr, info_ptr, &intent))
227 png_set_sRGB(png_ptr, intent, 0);
228 else
229 {
230 double image_gamma;
231 if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
232 png_set_gamma(png_ptr, screen_gamma, image_gamma);
233 else
234 png_set_gamma(png_ptr, screen_gamma, 0.45455);
235 }
236
237 /* Dither RGB files down to 8 bit palette or reduce palettes
238 * to the number of colors available on your screen.
239 */
240 if (color_type & PNG_COLOR_MASK_COLOR)
241 {
242 png_uint_32 num_palette;
243 png_colorp palette;
244
245 /* This reduces the image to the application supplied palette */
246 if (/* we have our own palette */)
247 {
248 /* An array of colors to which the image should be dithered */
249 png_color std_color_cube[MAX_SCREEN_COLORS];
250
251 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
252 MAX_SCREEN_COLORS, NULL, 0);
253 }
254 /* This reduces the image to the palette supplied in the file */
255 else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette))
256 {
257 png_color16p histogram;
258
259 png_get_hIST(png_ptr, info_ptr, &histogram);
260
261 png_set_dither(png_ptr, palette, num_palette,
262 max_screen_colors, histogram, 0);
263 }
264 }
265
266 /* invert monocrome files to have 0 as white and 1 as black */
267 png_set_invert_mono(png_ptr);
268
269 /* If you want to shift the pixel values from the range [0,255] or
270 * [0,65535] to the original [0,7] or [0,31], or whatever range the
271 * colors were originally in:
272 */
273 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
274 {
275 png_color8p sig_bit;
276
277 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
278 png_set_shift(png_ptr, sig_bit);
279 }
280
281 /* flip the RGB pixels to BGR (or RGBA to BGRA) */
282 png_set_bgr(png_ptr);
283
284 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
285 png_set_swap_alpha(png_ptr);
286
287 /* swap bytes of 16 bit files to least significant byte first */
288 png_set_swap(png_ptr);
289
290 /* Add filler (or alpha) byte (before/after each RGB triplet) */
291 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
292
293 /* Turn on interlace handling. REQUIRED if you are not using
294 * png_read_image(). To see how to handle interlacing passes,
295 * see the png_read_row() method below:
296 */
297 number_passes = png_set_interlace_handling(png_ptr);
298
299 /* Optional call to gamma correct and add the background to the palette
300 * and update info structure. REQUIRED if you are expecting libpng to
301 * update the palette for you (ie you selected such a transform above).
302 */
303 png_read_update_info(png_ptr, info_ptr);
304
305 /* Allocate the memory to hold the image using the fields of info_ptr. */
306
307 /* The easiest way to read the image: */
308 png_bytep row_pointers[height];
309
310 for (row = 0; row < height; row++)
311 {
312 row_pointers[row] = malloc(png_get_rowbytes(png_ptr, info_ptr));
313 }
314
315 /* Now it's time to read the image. One of these methods is REQUIRED */
316 #ifdef entire /* Read the entire image in one go */
317 png_read_image(png_ptr, row_pointers);
318
319 #else no_entire /* Read the image one or more scanlines at a time */
320 /* The other way to read images - deal with interlacing: */
321
322 for (pass = 0; pass < number_passes; pass++)
323 {
324 #ifdef single /* Read the image a single row at a time */
325 for (y = 0; y < height; y++)
326 {
327 png_read_rows(png_ptr, &row_pointers[y], NULL, 1);
328 }
329
330 #else no_single /* Read the image several rows at a time */
331 for (y = 0; y < height; y += number_of_rows)
332 {
333 #ifdef sparkle /* Read the image using the "sparkle" effect. */
334 png_read_rows(png_ptr, &row_pointers[y], NULL, number_of_rows);
335
336 png_read_rows(png_ptr, NULL, row_pointers[y], number_of_rows);
337 #else no_sparkle /* Read the image using the "rectangle" effect */
338 png_read_rows(png_ptr, NULL, &row_pointers[y], number_of_rows);
339 #endif no_sparkle /* use only one of these two methods */
340 }
341
342 /* if you want to display the image after every pass, do
343 so here */
344 #endif no_single /* use only one of these two methods */
345 }
346 #endif no_entire /* use only one of these two methods */
347
348 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
349 png_read_end(png_ptr, info_ptr);
350
351 /* clean up after the read, and free any memory allocated - REQUIRED */
352 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
353
354 /* close the file */
355 fclose(fp);
356
357 /* that's it */
358 return;
359 }
360
361 /* progressively read a file */
362
363 int
364 initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr)
365 {
366 /* Create and initialize the png_struct with the desired error handler
367 * functions. If you want to use the default stderr and longjump method,
368 * you can supply NULL for the last three parameters. We also check that
369 * the library version is compatible in case we are using dynamically
370 * linked libraries.
371 */
372 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
373 png_voidp user_error_ptr, user_error_fn, user_warning_fn);
374
375 if (*png_ptr == NULL)
376 {
377 *info_ptr = NULL;
378 return ERROR;
379 }
380
381 *info_ptr = png_create_info_struct(png_ptr);
382
383 if (*info_ptr == NULL)
384 {
385 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
386 return ERROR;
387 }
388
389 if (setjmp((*png_ptr)->jmpbuf))
390 {
391 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
392 return ERROR;
393 }
394
395 /* This one's new. You will need to provide all three
396 * function callbacks, even if you aren't using them all.
397 * If you aren't using all functions, you can specify NULL
398 * parameters. Even when all three functions are NULL,
399 * you need to call png_set_progressive_read_fn().
400 * These functions shouldn't be dependent on global or
401 * static variables if you are decoding several images
402 * simultaneously. You should store stream specific data
403 * in a separate struct, given as the second parameter,
404 * and retrieve the pointer from inside the callbacks using
405 * the function png_get_progressive_ptr(png_ptr).
406 */
407 png_set_progressive_read_fn(*png_ptr, (void *)stream_data,
408 info_callback, row_callback, end_callback);
409
410 return OK;
411 }
412
413 int
414 process_data(png_structp *png_ptr, png_infop *info_ptr,
415 png_bytep buffer, png_uint_32 length)
416 {
417 if (setjmp((*png_ptr)->jmpbuf))
418 {
419 /* Free the png_ptr and info_ptr memory on error */
420 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
421 return ERROR;
422 }
423
424 /* This one's new also. Simply give it chunks of data as
425 * they arrive from the data stream (in order, of course).
426 * On Segmented machines, don't give it any more than 64K.
427 * The library seems to run fine with sizes of 4K, although
428 * you can give it much less if necessary (I assume you can
429 * give it chunks of 1 byte, but I haven't tried with less
430 * than 256 bytes yet). When this function returns, you may
431 * want to display any rows that were generated in the row
432 * callback, if you aren't already displaying them there.
433 */
434 png_process_data(*png_ptr, *info_ptr, buffer, length);
435 return OK;
436 }
437
438 info_callback(png_structp png_ptr, png_infop info)
439 {
440 /* do any setup here, including setting any of the transformations
441 * mentioned in the Reading PNG files section. For now, you _must_
442 * call either png_start_read_image() or png_read_update_info()
443 * after all the transformations are set (even if you don't set
444 * any). You may start getting rows before png_process_data()
445 * returns, so this is your last chance to prepare for that.
446 */
447 }
448
449 row_callback(png_structp png_ptr, png_bytep new_row,
450 png_uint_32 row_num, int pass)
451 {
452 /* this function is called for every row in the image. If the
453 * image is interlacing, and you turned on the interlace handler,
454 * this function will be called for every row in every pass.
455 * Some of these rows will not be changed from the previous pass.
456 * When the row is not changed, the new_row variable will be NULL.
457 * The rows and passes are called in order, so you don't really
458 * need the row_num and pass, but I'm supplying them because it
459 * may make your life easier.
460 *
461 * For the non-NULL rows of interlaced images, you must call
462 * png_progressive_combine_row() passing in the row and the
463 * old row. You can call this function for NULL rows (it will
464 * just return) and for non-interlaced images (it just does the
465 * memcpy for you) if it will make the code easier. Thus, you
466 * can just do this for all cases:
467 */
468
469 png_progressive_combine_row(png_ptr, old_row, new_row);
470
471 /* where old_row is what was displayed for previous rows. Note
472 * that the first pass (pass == 0 really) will completely cover
473 * the old row, so the rows do not have to be initialized. After
474 * the first pass (and only for interlaced images), you will have
475 * to pass the current row, and the function will combine the
476 * old row and the new row.
477 */
478 }
479
480 end_callback(png_structp png_ptr, png_infop info)
481 {
482 /* this function is called when the whole image has been read,
483 * including any chunks after the image (up to and including
484 * the IEND). You will usually have the same info chunk as you
485 * had in the header, although some data may have been added
486 * to the comments and time fields.
487 *
488 * Most people won't do much here, perhaps setting a flag that
489 * marks the image as finished.
490 */
491 }
492
493 /* write a png file */
494 void write_png(char *file_name /* , ... other image information ... */)
495 {
496 FILE *fp;
497 png_structp png_ptr;
498 png_infop info_ptr;
499
500 /* open the file */
501 fp = fopen(file_name, "wb");
502 if (fp == NULL)
503 return;
504
505 /* Create and initialize the png_struct with the desired error handler
506 * functions. If you want to use the default stderr and longjump method,
507 * you can supply NULL for the last three parameters. We also check that
508 * the library version is compatible with the one used at compile time,
509 * in case we are using dynamically linked libraries. REQUIRED.
510 */
511 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
512 png_voidp user_error_ptr, user_error_fn, user_warning_fn);
513
514 if (png_ptr == NULL)
515 {
516 fclose(fp);
517 return;
518 }
519
520 /* Allocate/initialize the image information data. REQUIRED */
521 info_ptr = png_create_info_struct(png_ptr);
522 if (info_ptr == NULL)
523 {
524 fclose(fp);
525 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
526 return;
527 }
528
529 /* Set error handling. REQUIRED if you aren't supplying your own
530 * error hadnling functions in the png_create_write_struct() call.
531 */
532 if (setjmp(png_ptr->jmpbuf))
533 {
534 /* If we get here, we had a problem reading the file */
535 fclose(fp);
536 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
537 return;
538 }
539
540 /* One of the following I/O initialization functions is REQUIRED */
541 #ifdef streams /* I/O initialization method 1 */
542 /* set up the output control if you are using standard C streams */
543 png_init_io(png_ptr, fp);
544 #else no_streams /* I/O initialization method 2 */
545 /* If you are using replacement read functions, instead of calling
546 * png_init_io() here you would call */
547 png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
548 user_IO_flush_function);
549 /* where user_io_ptr is a structure you want available to the callbacks */
550 #endif no_streams /* only use one initialization method */
551
552 /* Set the image information here. Width and height are up to 2^31,
553 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
554 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
555 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
556 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
557 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
558 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
559 */
560 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???,
561 PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
562
563 /* set the palette if there is one. REQUIRED for indexed-color images */
564 palette = (png_colorp)png_malloc(png_ptr, 256 * sizeof (png_color));
565 /* ... set palette colors ... */
566 png_set_PLTE(png_ptr, info_ptr, palette, 256);
567
568 /* optional significant bit chunk */
569 /* if we are dealing with a grayscale image then */
570 sig_bit.gray = true_bit_depth;
571 /* otherwise, if we are dealing with a color image then */
572 sig_bit.red = true_red_bit_depth;
573 sig_bit.green = true_green_bit_depth;
574 sig_bit.blue = true_blue_bit_depth;
575 /* if the image has an alpha channel then */
576 sig_bit.alpha = true_alpha_bit_depth;
577 png_set_sBIT(png_ptr, info_ptr, sig_bit);
578
579
580 /* Optional gamma chunk is strongly suggested if you have any guess
581 * as to the correct gamma of the image.
582 */
583 png_set_gAMA(png_ptr, info_ptr, gamma);
584
585 /* Optionally write comments into the image */
586 text_ptr[0].key = "Title";
587 text_ptr[0].text = "Mona Lisa";
588 text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
589 text_ptr[1].key = "Author";
590 text_ptr[1].text = "Leonardo DaVinci";
591 text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
592 text_ptr[2].key = "Description";
593 text_ptr[2].text = "<long text>";
594 text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt;
595 png_set_text(png_ptr, info_ptr, text_ptr, 3);
596
597 /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
598 /* note that if sRGB is present the cHRM chunk must be ignored
599 * on read and must be written in accordance with the sRGB profile */
600
601 /* Write the file header information. REQUIRED */
602 png_write_info(png_ptr, info_ptr);
603
604 /* Once we write out the header, the compression type on the text
605 * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or
606 * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again
607 * at the end.
608 */
609
610 /* set up the transformations you want. Note that these are
611 * all optional. Only call them if you want them.
612 */
613
614 /* invert monocrome pixels */
615 png_set_invert_mono(png_ptr);
616
617 /* Shift the pixels up to a legal bit depth and fill in
618 * as appropriate to correctly scale the image.
619 */
620 png_set_shift(png_ptr, &sig_bit);
621
622 /* pack pixels into bytes */
623 png_set_packing(png_ptr);
624
625 /* swap location of alpha bytes from ARGB to RGBA */
626 png_set_swap_alpha(png_ptr);
627
628 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
629 * RGB (4 channels -> 3 channels). The second parameter is not used.
630 */
631 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
632
633 /* flip BGR pixels to RGB */
634 png_set_bgr(png_ptr);
635
636 /* swap bytes of 16-bit files to most significant byte first */
637 png_set_swap(png_ptr);
638
639 /* swap bits of 1, 2, 4 bit packed pixel formats */
640 png_set_packswap(png_ptr);
641
642 /* turn on interlace handling if you are not using png_write_image() */
643 if (interlacing)
644 number_passes = png_set_interlace_handling(png_ptr);
645 else
646 number_passes = 1;
647
648 /* The easiest way to write the image (you may have a different memory
649 * layout, however, so choose what fits your needs best). You need to
650 * use the first method if you aren't handling interlacing yourself.
651 */
652 png_uint_32 k, height, width;
653 png_byte image[height][width];
654 png_bytep row_pointers[height];
655 for (k = 0; k < height; k++)
656 row_pointers[k] = image + k*width;
657
658 /* One of the following output methods is REQUIRED */
659 #ifdef entire /* write out the entire image data in one call */
660 png_write_image(png_ptr, row_pointers);
661
662 /* the other way to write the image - deal with interlacing */
663
664 #else no_entire /* write out the image data by one or more scanlines */
665 /* The number of passes is either 1 for non-interlaced images,
666 * or 7 for interlaced images.
667 */
668 for (pass = 0; pass < number_passes; pass++)
669 {
670 /* Write a few rows at a time. */
671 png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows);
672
673 /* If you are only writing one row at a time, this works */
674 for (y = 0; y < height; y++)
675 {
676 png_write_rows(png_ptr, &row_pointers[y], 1);
677 }
678 }
679 #endif no_entire /* use only one output method */
680
681 /* You can write optional chunks like tEXt, zTXt, and tIME at the end
682 * as well.
683 */
684
685 /* It is REQUIRED to call this to finish writing the rest of the file */
686 png_write_end(png_ptr, info_ptr);
687
688 /* if you malloced the palette, free it here */
689 free(info_ptr->palette);
690
691 /* if you allocated any text comments, free them here */
692
693 /* clean up after the write, and free any memory allocated */
694 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
695
696 /* close the file */
697 fclose(fp);
698
699 /* that's it */
700 return;
701 }
702