1LIBPNG(3)                  Library Functions Manual                  LIBPNG(3)
2
3
4

NAME

6       libpng - Portable Network Graphics (PNG) Reference Library 1.5.13
7

SYNOPSIS

9
10
11       #include <png.h>
12
13
14
15       png_uint_32 png_access_version_number (void);
16
17
18
19       void png_benign_error (png_structp png_ptr, png_const_charp error);
20
21
22
23       void png_build_grayscale_palette (int bit_depth, png_colorp palette);
24
25
26
27       png_voidp png_calloc (png_structp png_ptr, png_alloc_size_t size);
28
29
30
31       void   png_chunk_benign_error   (png_structp  png_ptr,  png_const_charp
32       error);
33
34
35
36       void png_chunk_error (png_structp png_ptr, png_const_charp error);
37
38
39
40       void png_chunk_warning (png_structp png_ptr, png_const_charp message);
41
42
43
44       void png_convert_from_struct_tm  (png_timep  ptime,  struct  tm  FAR  *
45       ttime);
46
47
48
49       void png_convert_from_time_t (png_timep ptime, time_t ttime);
50
51
52
53       png_charp   png_convert_to_rfc1123   (png_structp   png_ptr,  png_timep
54       ptime);
55
56
57
58       png_infop png_create_info_struct (png_structp png_ptr);
59
60
61
62       png_structp   png_create_read_struct   (png_const_charp   user_png_ver,
63       png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn);
64
65
66
67       png_structp   png_create_read_struct_2  (png_const_charp  user_png_ver,
68       png_voidp error_ptr,  png_error_ptr  error_fn,  png_error_ptr  warn_fn,
69       png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn);
70
71
72
73       png_structp   png_create_write_struct   (png_const_charp  user_png_ver,
74       png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn);
75
76
77
78       png_structp  png_create_write_struct_2  (png_const_charp  user_png_ver,
79       png_voidp  error_ptr,  png_error_ptr  error_fn,  png_error_ptr warn_fn,
80       png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn);
81
82
83
84       void  png_data_freer  (png_structp  png_ptr,  png_infop  info_ptr,  int
85       freer, png_uint_32 mask));
86
87
88
89       void    png_destroy_info_struct    (png_structp   png_ptr,   png_infopp
90       info_ptr_ptr);
91
92
93
94       void  png_destroy_read_struct  (png_structpp  png_ptr_ptr,   png_infopp
95       info_ptr_ptr, png_infopp end_info_ptr_ptr);
96
97
98
99       void  png_destroy_write_struct  (png_structpp  png_ptr_ptr,  png_infopp
100       info_ptr_ptr);
101
102
103
104       void png_err (png_structp png_ptr);
105
106
107
108       void png_error (png_structp png_ptr, png_const_charp error);
109
110
111
112       void png_free (png_structp png_ptr, png_voidp ptr);
113
114
115
116       void png_free_chunk_list (png_structp png_ptr);
117
118
119
120       void png_free_default (png_structp png_ptr, png_voidp ptr);
121
122
123
124       void png_free_data (png_structp png_ptr, png_infop info_ptr, int num);
125
126
127
128       png_byte png_get_bit_depth (png_const_structp png_ptr,  png_const_infop
129       info_ptr);
130
131
132
133       png_uint_32    png_get_bKGD   (png_const_structp   png_ptr,   png_infop
134       info_ptr, png_color_16p *background);
135
136
137
138       png_byte png_get_channels (png_const_structp  png_ptr,  png_const_infop
139       info_ptr);
140
141
142
143       png_uint_32  png_get_cHRM  (png_const_structp  png_ptr, png_const_infop
144       info_ptr, double  *white_x,  double  *white_y,  double  *red_x,  double
145       *red_y,  double  *green_x,  double  *green_y,  double  *blue_x,  double
146       *blue_y);
147
148
149
150       png_uint_32     png_get_cHRM_fixed     (png_const_structp      png_ptr,
151       png_const_infop  info_ptr,  png_uint_32 *white_x, png_uint_32 *white_y,
152       png_uint_32   *red_x,   png_uint_32   *red_y,   png_uint_32   *green_x,
153       png_uint_32 *green_y, png_uint_32 *blue_x, png_uint_32 *blue_y);
154
155
156
157       png_uint_32 png_get_cHRM_XYZ (png_structp png_ptr,
158
159       png_const_infop info_ptr, double *red_X, double *red_Y, double *red_Z,
160
161       double *green_X, double *green_Y, double *green_Z, double *blue_X,
162
163       double *blue_Y, double *blue_Z);
164
165
166
167       png_uint_32      png_get_cHRM_XYZ_fixed      (png_structp      png_ptr,
168       png_const_infop info_ptr, png_fixed_point  *int_red_X,  png_fixed_point
169       *int_red_Y,  png_fixed_point  *int_red_Z, png_fixed_point *int_green_X,
170       png_fixed_point     *int_green_Y,     png_fixed_point     *int_green_Z,
171       png_fixed_point      *int_blue_X,      png_fixed_point     *int_blue_Y,
172       png_fixed_point *int_blue_Z);
173
174
175
176       png_uint_32 png_get_chunk_cache_max (png_const_structp png_ptr);
177
178
179
180       png_alloc_size_t png_get_chunk_malloc_max (png_const_structp png_ptr);
181
182
183
184       png_byte png_get_color_type (png_const_structp png_ptr, png_const_infop
185       info_ptr);
186
187
188
189       png_uint_32      png_get_compression_buffer_size     (png_const_structp
190       png_ptr);
191
192
193
194       png_byte    png_get_compression_type    (png_const_structp     png_ptr,
195       png_const_infop info_ptr);
196
197
198
199       png_byte png_get_copyright (png_const_structp png_ptr);
200
201
202
203       png_uint_32 png_get_current_row_number (png_const_structp);
204
205
206
207       png_byte png_get_current_pass_number (png_const_structp);
208
209
210
211       png_voidp png_get_error_ptr (png_const_structp png_ptr);
212
213
214
215       png_byte      png_get_filter_type      (png_const_structp      png_ptr,
216       png_const_infop info_ptr);
217
218
219
220       png_uint_32 png_get_gAMA  (png_const_structp  png_ptr,  png_const_infop
221       info_ptr, double *file_gamma);
222
223
224
225       png_uint_32      png_get_gAMA_fixed     (png_const_structp     png_ptr,
226       png_const_infop info_ptr, png_uint_32 *int_file_gamma);
227
228
229
230       png_byte png_get_header_ver (png_const_structp png_ptr);
231
232
233
234       png_byte png_get_header_version (png_const_structp png_ptr);
235
236
237
238       png_uint_32 png_get_hIST  (png_const_structp  png_ptr,  png_const_infop
239       info_ptr, png_uint_16p *hist);
240
241
242
243       png_uint_32  png_get_iCCP  (png_const_structp  png_ptr, png_const_infop
244       info_ptr, png_charpp name, int *compression_type,  png_bytepp  profile,
245       png_uint_32 *proflen);
246
247
248
249       png_uint_32  png_get_IHDR  (png_structp  png_ptr,  png_infop  info_ptr,
250       png_uint_32  *width,   png_uint_32   *height,   int   *bit_depth,   int
251       *color_type,  int  *interlace_type,  int  *compression_type,  int *fil‐
252       ter_type);
253
254
255
256       png_uint_32    png_get_image_height     (png_const_structp     png_ptr,
257       png_const_infop info_ptr);
258
259
260
261       png_uint_32     png_get_image_width     (png_const_structp     png_ptr,
262       png_const_infop info_ptr);
263
264
265
266       png_int_32 png_get_int_32 (png_bytep buf);
267
268
269
270       png_byte     png_get_interlace_type     (png_const_structp     png_ptr,
271       png_const_infop info_ptr);
272
273
274
275       png_const_bytep png_get_io_chunk_name (png_structp png_ptr);
276
277
278
279       png_uint_32 png_get_io_chunk_type (png_const_structp png_ptr);
280
281
282
283       png_voidp png_get_io_ptr (png_structp png_ptr);
284
285
286
287       png_uint_32 png_get_io_state (png_structp png_ptr);
288
289
290
291       png_byte png_get_libpng_ver (png_const_structp png_ptr);
292
293
294
295       png_voidp png_get_mem_ptr (png_const_structp png_ptr);
296
297
298
299       png_uint_32  png_get_oFFs  (png_const_structp  png_ptr, png_const_infop
300       info_ptr,   png_uint_32   *offset_x,   png_uint_32    *offset_y,    int
301       *unit_type);
302
303
304
305       png_uint_32  png_get_pCAL  (png_const_structp  png_ptr, png_const_infop
306       info_ptr, png_charp  *purpose,  png_int_32  *X0,  png_int_32  *X1,  int
307       *type, int *nparams, png_charp *units, png_charpp *params);
308
309
310
311       png_uint_32  png_get_pHYs  (png_const_structp  png_ptr, png_const_infop
312       info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type);
313
314
315
316       float    png_get_pixel_aspect_ratio     (png_const_structp     png_ptr,
317       png_const_infop info_ptr);
318
319
320
321       png_uint_32      png_get_pHYs_dpi      (png_const_structp      png_ptr,
322       png_const_infop info_ptr, png_uint_32 *res_x, png_uint_32  *res_y,  int
323       *unit_type);
324
325
326
327       png_fixed_point   png_get_pixel_aspect_ratio_fixed   (png_const_structp
328       png_ptr, png_const_infop info_ptr);
329
330
331
332       png_uint_32   png_get_pixels_per_inch    (png_const_structp    png_ptr,
333       png_const_infop info_ptr);
334
335
336
337       png_uint_32    png_get_pixels_per_meter   (png_const_structp   png_ptr,
338       png_const_infop info_ptr);
339
340
341
342       png_voidp png_get_progressive_ptr (png_const_structp png_ptr);
343
344
345
346       png_uint_32 png_get_PLTE  (png_const_structp  png_ptr,  png_const_infop
347       info_ptr, png_colorp *palette, int *num_palette);
348
349
350
351       png_byte png_get_rgb_to_gray_status (png_const_structp png_ptr)
352
353       png_uint_32      png_get_rowbytes      (png_const_structp      png_ptr,
354       png_const_infop info_ptr);
355
356
357
358       png_bytepp  png_get_rows  (png_const_structp  png_ptr,  png_const_infop
359       info_ptr);
360
361
362
363       png_uint_32    png_get_sBIT   (png_const_structp   png_ptr,   png_infop
364       info_ptr, png_color_8p *sig_bit);
365
366
367
368       void png_get_sCAL (png_const_structp png_ptr, png_const_infop info_ptr,
369       int* unit, double* width, double* height);
370
371
372
373       void  png_get_sCAL_fixed  (png_const_structp  png_ptr,  png_const_infop
374       info_ptr, int* unit, png_fixed_pointp width, png_fixed_pointp height);
375
376
377
378       void   png_get_sCAL_s   (png_const_structp   png_ptr,   png_const_infop
379       info_ptr, int* unit, png_charpp width, png_charpp height);
380
381
382
383       png_bytep   png_get_signature   (png_const_structp  png_ptr,  png_infop
384       info_ptr);
385
386
387
388       png_uint_32 png_get_sPLT  (png_const_structp  png_ptr,  png_const_infop
389       info_ptr, png_spalette_p *splt_ptr);
390
391
392
393       png_uint_32  png_get_sRGB  (png_const_structp  png_ptr, png_const_infop
394       info_ptr, int *file_srgb_intent);
395
396
397
398       png_uint_32 png_get_text  (png_const_structp  png_ptr,  png_const_infop
399       info_ptr, png_textp *text_ptr, int *num_text);
400
401
402
403       png_uint_32    png_get_tIME   (png_const_structp   png_ptr,   png_infop
404       info_ptr, png_timep *mod_time);
405
406
407
408       png_uint_32   png_get_tRNS   (png_const_structp   png_ptr,    png_infop
409       info_ptr,   png_bytep   *trans_alpha,   int  *num_trans,  png_color_16p
410       *trans_color);
411
412
413
414       /* This function is really an inline macro. */
415
416       png_uint_16 png_get_uint_16 (png_bytep buf);
417
418
419
420       png_uint_32 png_get_uint_31 (png_structp png_ptr, png_bytep buf);
421
422
423
424       /* This function is really an inline macro. */
425
426       png_uint_32 png_get_uint_32 (png_bytep buf);
427
428
429
430       png_uint_32    png_get_unknown_chunks    (png_const_structp    png_ptr,
431       png_const_infop info_ptr, png_unknown_chunkpp unknowns);
432
433
434
435       png_voidp png_get_user_chunk_ptr (png_const_structp png_ptr);
436
437
438
439       png_uint_32 png_get_user_height_max (png_const_structp png_ptr);
440
441
442
443       png_voidp png_get_user_transform_ptr (png_const_structp png_ptr);
444
445
446
447       png_uint_32 png_get_user_width_max (png_const_structp png_ptr);
448
449
450
451       png_uint_32  png_get_valid  (png_const_structp png_ptr, png_const_infop
452       info_ptr, png_uint_32 flag);
453
454
455
456       float     png_get_x_offset_inches      (png_const_structp      png_ptr,
457       png_const_infop info_ptr);
458
459
460
461       png_fixed_point   png_get_x_offset_inches_fixed  (png_structp  png_ptr,
462       png_const_infop info_ptr);
463
464
465
466       png_int_32   png_get_x_offset_microns    (png_const_structp    png_ptr,
467       png_const_infop info_ptr);
468
469
470
471       png_int_32    png_get_x_offset_pixels    (png_const_structp    png_ptr,
472       png_const_infop info_ptr);
473
474
475
476       png_uint_32   png_get_x_pixels_per_inch   (png_const_structp   png_ptr,
477       png_const_infop info_ptr);
478
479
480
481       png_uint_32   png_get_x_pixels_per_meter   (png_const_structp  png_ptr,
482       png_const_infop info_ptr);
483
484
485
486       float     png_get_y_offset_inches      (png_const_structp      png_ptr,
487       png_const_infop info_ptr);
488
489
490
491       png_fixed_point   png_get_y_offset_inches_fixed  (png_structp  png_ptr,
492       png_const_infop info_ptr);
493
494
495
496       png_int_32   png_get_y_offset_microns    (png_const_structp    png_ptr,
497       png_const_infop info_ptr);
498
499
500
501       png_int_32    png_get_y_offset_pixels    (png_const_structp    png_ptr,
502       png_const_infop info_ptr);
503
504
505
506       png_uint_32   png_get_y_pixels_per_inch   (png_const_structp   png_ptr,
507       png_const_infop info_ptr);
508
509
510
511       png_uint_32   png_get_y_pixels_per_meter   (png_const_structp  png_ptr,
512       png_const_infop info_ptr);
513
514
515
516       int png_handle_as_unknown (png_structp png_ptr, png_bytep chunk_name);
517
518
519
520       void     png_info_init_3     (png_infopp      info_ptr,      png_size_t
521       png_info_struct_size);
522
523
524
525       void png_init_io (png_structp png_ptr, FILE *fp);
526
527
528
529       void png_longjmp (png_structp png_ptr, int val);
530
531
532
533       png_voidp png_malloc (png_structp png_ptr, png_alloc_size_t size);
534
535
536
537       png_voidp  png_malloc_default  (png_structp  png_ptr,  png_alloc_size_t
538       size);
539
540
541
542       png_voidp png_malloc_warn (png_structp png_ptr, png_alloc_size_t size);
543
544
545
546       png_uint_32 png_permit_mng_features (png_structp  png_ptr,  png_uint_32
547       mng_features_permitted);
548
549
550
551       void   png_process_data   (png_structp   png_ptr,  png_infop  info_ptr,
552       png_bytep buffer, png_size_t buffer_size);
553
554
555
556       png_size_t png_process_data_pause (png_structp, int save);
557
558
559
560       png_uint_32 png_process_data_skip (png_structp);
561
562
563
564       void  png_progressive_combine_row   (png_structp   png_ptr,   png_bytep
565       old_row, png_bytep new_row);
566
567
568
569       void png_read_end (png_structp png_ptr, png_infop info_ptr);
570
571
572
573       void png_read_image (png_structp png_ptr, png_bytepp image);
574
575
576
577       void png_read_info (png_structp png_ptr, png_infop info_ptr);
578
579
580
581       void  png_read_png (png_structp png_ptr, png_infop info_ptr, int trans‐
582       forms, png_voidp params);
583
584
585
586       void png_read_row (png_structp png_ptr, png_bytep row,  png_bytep  dis‐
587       play_row);
588
589
590
591       void  png_read_rows  (png_structp  png_ptr,  png_bytepp row, png_bytepp
592       display_row, png_uint_32 num_rows);
593
594
595
596       void png_read_update_info (png_structp png_ptr, png_infop info_ptr);
597
598
599
600       int png_reset_zstream (png_structp png_ptr);
601
602
603
604       void png_save_int_32 (png_bytep buf, png_int_32 i);
605
606
607
608       void png_save_uint_16 (png_bytep buf, unsigned int i);
609
610
611
612       void png_save_uint_32 (png_bytep buf, png_uint_32 i);
613
614
615
616       void png_set_add_alpha (png_structp png_ptr,  png_uint_32  filler,  int
617       flags);
618
619
620
621       void  png_set_alpha_mode  (png_structp  png_ptr,  int mode, double out‐
622       put_gamma);
623
624
625
626       void   png_set_alpha_mode_fixed   (png_structp   png_ptr,   int   mode,
627       png_fixed_point output_gamma);
628
629
630
631       void   png_set_background  (png_structp  png_ptr,  png_color_16p  back‐
632       ground_color, int background_gamma_code, int need_expand, double  back‐
633       ground_gamma);
634
635
636
637       void png_set_background_fixed (png_structp png_ptr, png_color_16p back‐
638       ground_color, int background_gamma_code, int  need_expand,  png_uint_32
639       background_gamma);
640
641
642
643       void png_set_benign_errors (png_structp png_ptr, int allowed);
644
645
646
647       void png_set_bgr (png_structp png_ptr);
648
649
650
651       void    png_set_bKGD    (png_structp   png_ptr,   png_infop   info_ptr,
652       png_color_16p background);
653
654
655
656       void    png_set_check_for_invalid_index(png_structrp    png_ptr,    int
657       allowed);
658
659
660
661       void  png_set_cHRM  (png_structp  png_ptr,  png_infop  info_ptr, double
662       white_x, double white_y, double red_x, double  red_y,  double  green_x,
663       double green_y, double blue_x, double blue_y);
664
665
666
667       void   png_set_cHRM_fixed  (png_structp  png_ptr,  png_infop  info_ptr,
668       png_uint_32   white_x,   png_uint_32   white_y,   png_uint_32    red_x,
669       png_uint_32    red_y,   png_uint_32   green_x,   png_uint_32   green_y,
670       png_uint_32 blue_x, png_uint_32 blue_y);
671
672
673
674       void png_set_cHRM_XYZ (png_structp png_ptr, png_infop info_ptr,  double
675       red_X, double red_Y, double red_Z, double green_X, double green_Y,
676
677       double green_Z, double blue_X, double blue_Y, double blue_Z);
678
679
680
681       void  png_set_cHRM_XYZ_fixed  (png_structp png_ptr, png_infop info_ptr,
682       png_fixed_point int_red_X, png_fixed_point  int_red_Y,  png_fixed_point
683       int_red_Z,  png_fixed_point  int_green_X,  png_fixed_point int_green_Y,
684       png_fixed_point      int_green_Z,      png_fixed_point      int_blue_X,
685       png_fixed_point int_blue_Y, png_fixed_point int_blue_Z);
686
687
688
689       void    png_set_chunk_cache_max   (png_structp   png_ptr,   png_uint_32
690       user_chunk_cache_max);
691
692
693
694       void png_set_compression_level (png_structp png_ptr, int level);
695
696
697
698       void   png_set_compression_mem_level    (png_structp    png_ptr,    int
699       mem_level);
700
701
702
703       void png_set_compression_method (png_structp png_ptr, int method);
704
705
706
707       void png_set_compression_strategy (png_structp png_ptr, int strategy);
708
709
710
711       void  png_set_compression_window_bits  (png_structp  png_ptr,  int win‐
712       dow_bits);
713
714
715
716       void png_set_crc_action  (png_structp  png_ptr,  int  crit_action,  int
717       ancil_action);
718
719
720
721       void   png_set_error_fn   (png_structp  png_ptr,  png_voidp  error_ptr,
722       png_error_ptr error_fn, png_error_ptr warning_fn);
723
724
725
726       void png_set_expand (png_structp png_ptr);
727
728
729
730       void png_set_expand_16 (png_structp png_ptr);
731
732
733
734       void png_set_expand_gray_1_2_4_to_8 (png_structp png_ptr);
735
736
737
738       void  png_set_filler  (png_structp  png_ptr,  png_uint_32  filler,  int
739       flags);
740
741
742
743       void png_set_filter (png_structp png_ptr, int method, int filters);
744
745
746
747       void   png_set_filter_heuristics   (png_structp  png_ptr,  int  heuris‐
748       tic_method, int num_weights,  png_doublep  filter_weights,  png_doublep
749       filter_costs);
750
751
752
753       void  png_set_filter_heuristics_fixed (png_structp png_ptr, int heuris‐
754       tic_method,   int   num_weights,   png_fixed_point_p    filter_weights,
755       png_fixed_point_p filter_costs);
756
757
758
759       void png_set_flush (png_structp png_ptr, int nrows);
760
761
762
763       void  png_set_gamma  (png_structp  png_ptr, double screen_gamma, double
764       default_file_gamma);
765
766
767
768       void    png_set_gamma_fixed    (png_structp    png_ptr,     png_uint_32
769       screen_gamma, png_uint_32 default_file_gamma);
770
771
772
773       void  png_set_gAMA  (png_structp  png_ptr,  png_infop  info_ptr, double
774       file_gamma);
775
776
777
778       void  png_set_gAMA_fixed  (png_structp  png_ptr,  png_infop   info_ptr,
779       png_uint_32 file_gamma);
780
781
782
783       void png_set_gray_1_2_4_to_8 (png_structp png_ptr);
784
785
786
787       void png_set_gray_to_rgb (png_structp png_ptr);
788
789
790
791       void    png_set_hIST    (png_structp   png_ptr,   png_infop   info_ptr,
792       png_uint_16p hist);
793
794
795
796       void   png_set_iCCP   (png_structp   png_ptr,    png_infop    info_ptr,
797       png_const_charp  name,  int  compression_type, png_const_bytep profile,
798       png_uint_32 proflen);
799
800
801
802       int png_set_interlace_handling (png_structp png_ptr);
803
804
805
806       void png_set_invalid  (png_structp  png_ptr,  png_infop  info_ptr,  int
807       mask);
808
809
810
811       void png_set_invert_alpha (png_structp png_ptr);
812
813
814
815       void png_set_invert_mono (png_structp png_ptr);
816
817
818
819       void png_set_IHDR (png_structp png_ptr, png_infop info_ptr, png_uint_32
820       width, png_uint_32 height, int bit_depth, int  color_type,  int  inter‐
821       lace_type, int compression_type, int filter_type);
822
823
824
825       void   png_set_keep_unknown_chunks   (png_structp  png_ptr,  int  keep,
826       png_bytep chunk_list, int num_chunks);
827
828
829
830       jmp_buf*  png_set_longjmp_fn  (png_structp   png_ptr,   png_longjmp_ptr
831       longjmp_fn, size_t jmp_buf_size);
832
833
834
835       void  png_set_chunk_malloc_max  (png_structp  png_ptr, png_alloc_size_t
836       user_chunk_cache_max);
837
838
839
840       void png_set_compression_buffer_size (png_structp png_ptr,  png_uint_32
841       size);
842
843
844
845       void  png_set_mem_fn  (png_structp png_ptr, png_voidp mem_ptr, png_mal‐
846       loc_ptr malloc_fn, png_free_ptr free_fn);
847
848
849
850       void png_set_oFFs (png_structp png_ptr, png_infop info_ptr, png_uint_32
851       offset_x, png_uint_32 offset_y, int unit_type);
852
853
854
855       void png_set_packing (png_structp png_ptr);
856
857
858
859       void png_set_packswap (png_structp png_ptr);
860
861
862
863       void png_set_palette_to_rgb (png_structp png_ptr);
864
865
866
867       void  png_set_pCAL  (png_structp png_ptr, png_infop info_ptr, png_charp
868       purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_charp
869       units, png_charpp params);
870
871
872
873       void png_set_pHYs (png_structp png_ptr, png_infop info_ptr, png_uint_32
874       res_x, png_uint_32 res_y, int unit_type);
875
876
877
878       void png_set_progressive_read_fn (png_structp png_ptr,  png_voidp  pro‐
879       gressive_ptr, png_progressive_info_ptr info_fn, png_progressive_row_ptr
880       row_fn, png_progressive_end_ptr end_fn);
881
882
883
884       void png_set_PLTE (png_structp png_ptr, png_infop info_ptr,  png_colorp
885       palette, int num_palette);
886
887
888
889       void  png_set_quantize  (png_structp  png_ptr,  png_colorp palette, int
890       num_palette, int maximum_colors, png_uint_16p histogram, int full_quan‐
891       tize);
892
893
894
895       void png_set_read_fn (png_structp png_ptr, png_voidp io_ptr, png_rw_ptr
896       read_data_fn);
897
898
899
900       void png_set_read_status_fn (png_structp  png_ptr,  png_read_status_ptr
901       read_row_fn);
902
903
904
905       void   png_set_read_user_chunk_fn   (png_structp   png_ptr,   png_voidp
906       user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn);
907
908
909
910       void     png_set_read_user_transform_fn      (png_structp      png_ptr,
911       png_user_transform_ptr read_user_transform_fn);
912
913
914
915       void png_set_rgb_to_gray (png_structp png_ptr, int error_action, double
916       red, double green);
917
918
919
920       void png_set_rgb_to_gray_fixed (png_structp png_ptr,  int  error_action
921       png_uint_32 red, png_uint_32 green);
922
923
924
925       void  png_set_rows (png_structp png_ptr, png_infop info_ptr, png_bytepp
926       row_pointers);
927
928
929
930       void   png_set_sBIT   (png_structp   png_ptr,    png_infop    info_ptr,
931       png_color_8p sig_bit);
932
933
934
935       void  png_set_sCAL  (png_structp png_ptr, png_infop info_ptr, int unit,
936       double width, double height);
937
938
939
940       void png_set_sCAL_fixed (png_structp png_ptr, png_infop  info_ptr,  int
941       unit, png_fixed_point width, png_fixed_point height);
942
943
944
945       void png_set_sCAL_s (png_structp png_ptr, png_infop info_ptr, int unit,
946       png_charp width, png_charp height);
947
948
949
950       void png_set_scale_16 (png_structp png_ptr);
951
952
953
954       void png_set_shift (png_structp png_ptr, png_color_8p true_bits);
955
956
957
958       void png_set_sig_bytes (png_structp png_ptr, int num_bytes);
959
960
961
962       void   png_set_sPLT   (png_structp   png_ptr,    png_infop    info_ptr,
963       png_spalette_p splt_ptr, int num_spalettes);
964
965
966
967       void   png_set_sRGB   (png_structp  png_ptr,  png_infop  info_ptr,  int
968       srgb_intent);
969
970
971
972       void   png_set_sRGB_gAMA_and_cHRM   (png_structp   png_ptr,   png_infop
973       info_ptr, int srgb_intent);
974
975
976
977       void png_set_strip_16 (png_structp png_ptr);
978
979
980
981       void png_set_strip_alpha (png_structp png_ptr);
982
983
984
985       void   png_set_strip_error_numbers  (png_structp  png_ptr,  png_uint_32
986       strip_mode);
987
988
989
990       void png_set_swap (png_structp png_ptr);
991
992
993
994       void png_set_swap_alpha (png_structp png_ptr);
995
996
997
998       void png_set_text (png_structp png_ptr, png_infop  info_ptr,  png_textp
999       text_ptr, int num_text);
1000
1001
1002
1003       void png_set_text_compression_level (png_structp png_ptr, int level);
1004
1005
1006
1007       void   png_set_text_compression_mem_level   (png_structp  png_ptr,  int
1008       mem_level);
1009
1010
1011
1012       void png_set_text_compression_strategy (png_structp png_ptr, int strat‐
1013       egy);
1014
1015
1016
1017       void  png_set_text_compression_window_bits  (png_structp  png_ptr,  int
1018       window_bits);
1019
1020
1021
1022       void   png_set_text_compression_method,   (png_structp   png_ptr,   int
1023       method));
1024
1025
1026
1027       void  png_set_tIME  (png_structp png_ptr, png_infop info_ptr, png_timep
1028       mod_time);
1029
1030
1031
1032       void png_set_tRNS (png_structp png_ptr, png_infop  info_ptr,  png_bytep
1033       trans_alpha, int num_trans, png_color_16p trans_color);
1034
1035
1036
1037       void png_set_tRNS_to_alpha (png_structp png_ptr);
1038
1039
1040
1041       png_uint_32   png_set_unknown_chunks  (png_structp  png_ptr,  png_infop
1042       info_ptr, png_unknown_chunkp unknowns, int num, int location);
1043
1044
1045
1046       void  png_set_unknown_chunk_location  (png_structp  png_ptr,  png_infop
1047       info_ptr, int chunk, int location);
1048
1049
1050
1051       void     png_set_user_limits    (png_structp    png_ptr,    png_uint_32
1052       user_width_max, png_uint_32 user_height_max);
1053
1054
1055
1056       void  png_set_user_transform_info   (png_structp   png_ptr,   png_voidp
1057       user_transform_ptr,  int user_transform_depth, int user_transform_chan‐
1058       nels);
1059
1060
1061
1062       void   png_set_write_fn   (png_structp   png_ptr,   png_voidp   io_ptr,
1063       png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn);
1064
1065
1066
1067       void png_set_write_status_fn (png_structp png_ptr, png_write_status_ptr
1068       write_row_fn);
1069
1070
1071
1072       void     png_set_write_user_transform_fn     (png_structp      png_ptr,
1073       png_user_transform_ptr write_user_transform_fn);
1074
1075
1076
1077       int   png_sig_cmp   (png_bytep   sig,   png_size_t   start,  png_size_t
1078       num_to_check);
1079
1080
1081
1082       void png_start_read_image (png_structp png_ptr);
1083
1084
1085
1086       void png_warning (png_structp png_ptr, png_const_charp message);
1087
1088
1089
1090       void  png_write_chunk  (png_structp  png_ptr,   png_bytep   chunk_name,
1091       png_bytep data, png_size_t length);
1092
1093
1094
1095       void   png_write_chunk_data   (png_structp   png_ptr,  png_bytep  data,
1096       png_size_t length);
1097
1098
1099
1100       void png_write_chunk_end (png_structp png_ptr);
1101
1102
1103
1104       void png_write_chunk_start (png_structp png_ptr, png_bytep  chunk_name,
1105       png_uint_32 length);
1106
1107
1108
1109       void png_write_end (png_structp png_ptr, png_infop info_ptr);
1110
1111
1112
1113       void png_write_flush (png_structp png_ptr);
1114
1115
1116
1117       void png_write_image (png_structp png_ptr, png_bytepp image);
1118
1119
1120
1121       void png_write_info (png_structp png_ptr, png_infop info_ptr);
1122
1123
1124
1125       void   png_write_info_before_PLTE   (png_structp   png_ptr,   png_infop
1126       info_ptr);
1127
1128
1129
1130       void png_write_png (png_structp png_ptr, png_infop info_ptr, int trans‐
1131       forms, png_voidp params);
1132
1133
1134
1135       void png_write_row (png_structp png_ptr, png_bytep row);
1136
1137
1138
1139       void  png_write_rows  (png_structp png_ptr, png_bytepp row, png_uint_32
1140       num_rows);
1141
1142
1143
1144       void png_write_sig (png_structp png_ptr);
1145
1146
1147
1148

DESCRIPTION

1150       The libpng library supports encoding, decoding, and  various  manipula‐
1151       tions  of  the  Portable Network Graphics (PNG) format image files.  It
1152       uses the zlib(3) compression library.   Following  is  a  copy  of  the
1153       libpng-manual.txt file that accompanies libpng.
1154

LIBPNG.TXT

1156       Libpng-manual.txt - A description on how to use and modify libpng
1157
1158        libpng version 1.5.13 - September 27, 2012
1159        Updated and distributed by Glenn Randers-Pehrson
1160        <glennrp at users.sourceforge.net>
1161        Copyright (c) 1998-2012 Glenn Randers-Pehrson
1162
1163        This document is released under the libpng license.
1164        For conditions of distribution and use, see the disclaimer
1165        and license in png.h
1166
1167        Based on:
1168
1169        libpng  versions  0.97,  January  1998, through 1.5.13 - September 27,
1170       2012
1171        Updated and distributed by Glenn Randers-Pehrson
1172        Copyright (c) 1998-2012 Glenn Randers-Pehrson
1173
1174        libpng 1.0 beta 6  version 0.96 May 28, 1997
1175        Updated and distributed by Andreas Dilger
1176        Copyright (c) 1996, 1997 Andreas Dilger
1177
1178        libpng 1.0 beta 2 - version 0.88  January 26, 1996
1179        For conditions of distribution and use, see copyright
1180        notice in png.h. Copyright (c) 1995, 1996 Guy Eric
1181        Schalnat, Group 42, Inc.
1182
1183        Updated/rewritten per request in the libpng FAQ
1184        Copyright (c) 1995, 1996 Frank J. T. Wojcik
1185        December 18, 1995 & January 20, 1996
1186
1187

I. Introduction

1189       This file describes how to use and modify  the  PNG  reference  library
1190       (known  as  libpng)  for your own use.  There are five sections to this
1191       file: introduction, structures, reading, writing, and modification  and
1192       configuration notes for various special platforms.  In addition to this
1193       file, example.c is a good starting point for using the library,  as  it
1194       is  heavily  commented  and  should include everything most people will
1195       need.  We assume that libpng is already installed; see the INSTALL file
1196       for instructions on how to install libpng.
1197
1198       For  examples  of libpng usage, see the files "example.c", "pngtest.c",
1199       and the files in the "contrib" directory, all of which are included  in
1200       the libpng distribution.
1201
1202       Libpng was written as a companion to the PNG specification, as a way of
1203       reducing the amount of time and effort it takes to support the PNG file
1204       format in application programs.
1205
1206       The  PNG specification (second edition), November 2003, is available as
1207       a W3C Recommendation and as an ISO Standard (ISO/IEC 15948:2003 (E)) at
1208       <http://www.w3.org/TR/2003/REC-PNG-20031110/  The W3C and ISO documents
1209       have identical technical content.
1210
1211       The       PNG-1.2       specification       is       available       at
1212       <http://www.libpng.org/pub/png/documents/>.   It is technically equiva‐
1213       lent to the PNG specification (second edition) but has some  additional
1214       material.
1215
1216       The    PNG-1.0    specification    is    available    as    RFC    2083
1217       <http://www.libpng.org/pub/png/documents/> and as a W3C  Recommendation
1218       <http://www.w3.org/TR/REC.png.html>.
1219
1220       Some  additional  chunks  are  described  in the special-purpose public
1221       chunks documents at <http://www.libpng.org/pub/png/documents/>.
1222
1223       Other information about PNG, and the latest version of libpng,  can  be
1224       found at the PNG home page, <http://www.libpng.org/pub/png/>.
1225
1226       Most  users will not have to modify the library significantly; advanced
1227       users may want to modify it more.  All attempts were made to make it as
1228       complete  as possible, while keeping the code easy to understand.  Cur‐
1229       rently, this library only supports C.  Support for other  languages  is
1230       being considered.
1231
1232       Libpng has been designed to handle multiple sessions at one time, to be
1233       easily modifiable, to be portable to  the  vast  majority  of  machines
1234       (ANSI,  K&R,  16-,  32-,  and 64-bit) available, and to be easy to use.
1235       The ultimate goal of libpng is to promote the  acceptance  of  the  PNG
1236       file  format in whatever way possible.  While there is still work to be
1237       done (see the TODO file), libpng should cover the majority of the needs
1238       of its users.
1239
1240       Libpng  uses  zlib  for its compression and decompression of PNG files.
1241       Further information about zlib, and the latest version of zlib, can  be
1242       found      at     the     zlib     home     page,     <http://www.info-
1243       zip.org/pub/infozip/zlib/>.  The zlib compression utility is a  general
1244       purpose utility that is useful for more than PNG files, and can be used
1245       without libpng.  See the documentation delivered  with  zlib  for  more
1246       details.   You  can  usually find the source files for the zlib utility
1247       wherever you find the libpng source files.
1248
1249       Libpng is  thread  safe,  provided  the  threads  are  using  different
1250       instances   of  the  structures.   Each  thread  should  have  its  own
1251       png_struct and png_info instances, and thus its own image.  Libpng does
1252       not  protect  itself  against  two threads using the same instance of a
1253       structure.
1254
1255

II. Structures

1257       There are two main structures that are important to libpng,  png_struct
1258       and  png_info.  Both are internal structures that are no longer exposed
1259       in the libpng interface (as of libpng 1.5.0).
1260
1261       The png_info structure is designed to provide information about the PNG
1262       file.  At one time, the fields of png_info were intended to be directly
1263       accessible to the user.  However, this tended to  cause  problems  with
1264       applications  using dynamically loaded libraries, and as a result a set
1265       of interface functions for png_info (the  png_get_*()  and  png_set_*()
1266       functions)  was developed, and direct access to the png_info fields was
1267       deprecated..
1268
1269       The png_struct structure is the object used by the library to decode  a
1270       single image.  As of 1.5.0 this structure is also not exposed.
1271
1272       Almost  all  libpng APIs require a pointer to a png_struct as the first
1273       argument.  Many (in particular  the  png_set  and  png_get  APIs)  also
1274       require a pointer to png_info as the second argument.  Some application
1275       visible macros defined in png.h designed for basic data access (reading
1276       and  writing integers in the PNG format) don't take a png_info pointer,
1277       but it's almost always safe to assume that a (png_struct*)  has  to  be
1278       passed to call an API function.
1279
1280       You can have more than one png_info structure associated with an image,
1281       as illustrated in pngtest.c, one for information  valid  prior  to  the
1282       IDAT  chunks  and  another  (called  "end_info" below) for things after
1283       them.
1284
1285       The png.h header file is an invaluable reference for  programming  with
1286       libpng.   And  while I'm on the topic, make sure you include the libpng
1287       header file:
1288
1289       #include <png.h>
1290
1291       and also (as of libpng-1.5.0) the zlib header file, if you need it:
1292
1293       #include <zlib.h>
1294
1295
1296   Types
1297       The png.h header file defines a number of integral types  used  by  the
1298       APIs.   Most of these are fairly obvious; for example types correspond‐
1299       ing to integers of particular sizes and types for passing color values.
1300
1301       One exception is how non-integral numbers are handled.  For application
1302       convenience most APIs that take such numbers have C (double) arguments;
1303       however, internally PNG, and libpng, use 32  bit  signed  integers  and
1304       encode  the value by multiplying by 100,000.  As of libpng 1.5.0 a con‐
1305       venience  macro  PNG_FP_1  is  defined  in  png.h  along  with  a  type
1306       (png_fixed_point) which is simply (png_int_32).
1307
1308       All  APIs  that  take  (double) arguments also have a matching API that
1309       takes the corresponding fixed point integer arguments.  The fixed point
1310       API has the same name as the floating point one with "_fixed" appended.
1311       The actual range of values permitted in the  APIs  is  frequently  less
1312       than the full range of (png_fixed_point) (-21474 to +21474).  When APIs
1313       require a non-negative argument the type  is  recorded  as  png_uint_32
1314       above.   Consult  the  header file and the text below for more informa‐
1315       tion.
1316
1317       Special care must be take with sCAL chunk handling  because  the  chunk
1318       itself  uses  non-integral values encoded as strings containing decimal
1319       floating point numbers.  See the comments in the header file.
1320
1321
1322   Configuration
1323       The main header file function declarations are frequently protected  by
1324       C preprocessing directives of the form:
1325
1326           #ifdef PNG_feature_SUPPORTED
1327           declare-function
1328           #endif
1329           ...
1330           #ifdef PNG_feature_SUPPORTED
1331           use-function
1332           #endif
1333
1334       The  library  can  be  built without support for these APIs, although a
1335       standard build will have all implemented  APIs.   Application  programs
1336       should  check the feature macros before using an API for maximum porta‐
1337       bility.  From libpng 1.5.0 the feature macros set during the  build  of
1338       libpng  are recorded in the header file "pnglibconf.h" and this file is
1339       always included by png.h.
1340
1341       If you don't need to change the library configuration from the default,
1342       skip to the next section ("Reading").
1343
1344       Notice  that  some  of the makefiles in the 'scripts' directory and (in
1345       1.5.0) all of the build project files in the 'projects' directory  sim‐
1346       ply  copy  scripts/pnglibconf.h.prebuilt  to  pnglibconf.h.  This means
1347       that these build systems do not permit easy auto-configuration  of  the
1348       library - they only support the default configuration.
1349
1350       The  easiest way to make minor changes to the libpng configuration when
1351       auto-configuration is supported is to add definitions  to  the  command
1352       line using (typically) CPPFLAGS.  For example:
1353
1354       CPPFLAGS=-DPNG_NO_FLOATING_ARITHMETIC
1355
1356       will  change  the internal libpng math implementation for gamma correc‐
1357       tion and other arithmetic calculations to  fixed  point,  avoiding  the
1358       need  for  fast  floating point support.  The result can be seen in the
1359       generated pnglibconf.h - make sure  it  contains  the  changed  feature
1360       macro setting.
1361
1362       If  you  need  to make more extensive configuration changes - more than
1363       one or two feature macro settings - you can either add  -DPNG_USER_CON‐
1364       FIG  to the build command line and put a list of feature macro settings
1365       in pngusr.h or you can set DFA_XTRA (a makefile  variable)  to  a  file
1366       containing the same information in the form of 'option' settings.
1367
1368       A. Changing pnglibconf.h
1369
1370       A  variety  of methods exist to build libpng.  Not all of these support
1371       reconfiguration of pnglibconf.h.  To reconfigure pnglibconf.h  it  must
1372       either  be  rebuilt from scripts/pnglibconf.dfa using awk or it must be
1373       edited by hand.
1374
1375       Hand editing is achieved by  copying  scripts/pnglibconf.h.prebuilt  to
1376       pnglibconf.h  and  changing  the lines defining the supported features,
1377       paying  very  close  attention   to   the   'option'   information   in
1378       scripts/pnglibconf.dfa that describes those features and their require‐
1379       ments.  This is easy to get wrong.
1380
1381       B. Configuration using DFA_XTRA
1382
1383       Rebuilding from pnglibconf.dfa is easy if a  functioning  'awk',  or  a
1384       later  variant  such  as 'nawk' or 'gawk', is available.  The configure
1385       build will automatically find an  appropriate  awk  and  build  pnglib‐
1386       conf.h.   The  scripts/pnglibconf.mak file contains a set of make rules
1387       for doing the same thing if configure is not  used,  and  many  of  the
1388       makefiles in the scripts directory use this approach.
1389
1390       When  rebuilding simply write a new file containing changed options and
1391       set DFA_XTRA to the name of this file.  This causes the build to append
1392       the new file to the end of scripts/pnglibconf.dfa.  The pngusr.dfa file
1393       should contain lines of the following forms:
1394
1395       everything = off
1396
1397       This turns all optional features off.   Include  it  at  the  start  of
1398       pngusr.dfa  to  make  it  easier to build a minimal configuration.  You
1399       will need to turn at least some features on afterward to enable  either
1400       reading or writing code, or both.
1401
1402       option feature on option feature off
1403
1404       Enable  or  disable  a  single feature.  This will automatically enable
1405       other features required by a feature that is turned on or disable other
1406       features  that require a feature which is turned off.  Conflicting set‐
1407       tings will cause an error message to be emitted by awk.
1408
1409       setting feature default value
1410
1411       Changes the default value of setting 'feature' to 'value'.  There are a
1412       small  number  of  settings listed at the top of pnglibconf.h, they are
1413       documented in the source code.  Most of these values  have  performance
1414       implications for the library but most of them have no visible effect on
1415       the API.  Some can also be overridden from the API.
1416
1417       This method of building a customized  pnglibconf.h  is  illustrated  in
1418       contrib/pngminim/*.   See  the "$(PNGCONF):" target in the makefile and
1419       pngusr.dfa in these directories.
1420
1421       C. Configuration using PNG_USR_CONFIG
1422
1423       If -DPNG_USR_CONFIG is added to the CFLAGS when pnglibconf.h  is  built
1424       the  file pngusr.h will automatically be included before the options in
1425       scripts/pnglibconf.dfa are processed.  Your pngusr.h file  should  con‐
1426       tain  only macro definitions turning features on or off or setting set‐
1427       tings.
1428
1429       Apart from the global setting "everything = off" all the options listed
1430       above can be set using macros in pngusr.h:
1431
1432       #define PNG_feature_SUPPORTED
1433
1434       is equivalent to:
1435
1436       option feature on
1437
1438       #define PNG_NO_feature
1439
1440       is equivalent to:
1441
1442       option feature off
1443
1444       #define PNG_feature value
1445
1446       is equivalent to:
1447
1448       setting feature default value
1449
1450       Notice that in both cases, pngusr.dfa and pngusr.h, the contents of the
1451       pngusr file you supply override the contents of scripts/pnglibconf.dfa
1452
1453       If confusing or incomprehensible behavior results  it  is  possible  to
1454       examine  the  intermediate  file pnglibconf.dfn to find the full set of
1455       dependency information for each setting and option.  Simply locate  the
1456       feature in the file and read the C comments that precede it.
1457
1458       This method is also illustrated in the contrib/pngminim/* makefiles and
1459       pngusr.h.
1460
1461

III. Reading

1463       We'll now walk you through the possible functions to call when  reading
1464       in  a  PNG file sequentially, briefly explaining the syntax and purpose
1465       of each one.  See example.c and png.h for more detail.  While  progres‐
1466       sive  reading  is covered in the next section, you will still need some
1467       of the functions discussed in this section to read a PNG file.
1468
1469
1470   Setup
1471       You will want to do the  I/O  initialization(*)  before  you  get  into
1472       libpng, so if it doesn't work, you don't have much to undo.  Of course,
1473       you will also want to insure that you are, in fact, dealing with a  PNG
1474       file.   Libpng  provides a simple check to see if a file is a PNG file.
1475       To use it, pass in the first 1 to 8 bytes of the file to  the  function
1476       png_sig_cmp(), and it will return 0 (false) if the bytes match the cor‐
1477       responding bytes of the PNG signature, or nonzero (true) otherwise.  Of
1478       course,  the  more  bytes  you pass in, the greater the accuracy of the
1479       prediction.
1480
1481       If you are intending to keep the file pointer open for use  in  libpng,
1482       you  must ensure you don't read more than 8 bytes from the beginning of
1483       the file, and you also have to make a call to  png_set_sig_bytes_read()
1484       with the number of bytes you read from the beginning.  Libpng will then
1485       only check the bytes (if any) that your program didn't read.
1486
1487       (*): If you are not using the standard I/O functions, you will need  to
1488       replace them with custom functions.  See the discussion under Customiz‐
1489       ing libpng.
1490
1491
1492           FILE *fp = fopen(file_name, "rb");
1493           if (!fp)
1494           {
1495              return (ERROR);
1496           }
1497
1498           fread(header, 1, number, fp);
1499           is_png = !png_sig_cmp(header, 0, number);
1500
1501           if (!is_png)
1502           {
1503              return (NOT_PNG);
1504           }
1505
1506
1507       Next, png_struct and png_info need to be allocated and initialized.  In
1508       order  to ensure that the size of these structures is correct even with
1509       a dynamically linked libpng, there  are  functions  to  initialize  and
1510       allocate  the  structures.   We also pass the library version, optional
1511       pointers to error handling functions, and a pointer to  a  data  struct
1512       for use by the error functions, if necessary (the pointer and functions
1513       can be NULL if the default error handlers are to  be  used).   See  the
1514       section  on  Changes  to  Libpng below regarding the old initialization
1515       functions.  The structure allocation functions quietly return  NULL  if
1516       they fail to create the structure, so your application should check for
1517       that.
1518
1519           png_structp png_ptr = png_create_read_struct
1520               (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
1521               user_error_fn, user_warning_fn);
1522
1523           if (!png_ptr)
1524              return (ERROR);
1525
1526           png_infop info_ptr = png_create_info_struct(png_ptr);
1527
1528           if (!info_ptr)
1529           {
1530              png_destroy_read_struct(&png_ptr,
1531                  (png_infopp)NULL, (png_infopp)NULL);
1532              return (ERROR);
1533           }
1534
1535       If you want to use your own memory allocation routines,  use  a  libpng
1536       that  was  built  with PNG_USER_MEM_SUPPORTED defined, and use png_cre‐
1537       ate_read_struct_2() instead of png_create_read_struct():
1538
1539           png_structp png_ptr = png_create_read_struct_2
1540               (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
1541               user_error_fn, user_warning_fn, (png_voidp)
1542               user_mem_ptr, user_malloc_fn, user_free_fn);
1543
1544       The error handling routines passed to png_create_read_struct() and  the
1545       memory  alloc/free  routines  passed  to png_create_struct_2() are only
1546       necessary if you are not using the libpng supplied error  handling  and
1547       memory alloc/free functions.
1548
1549       When  libpng  encounters  an  error, it expects to longjmp back to your
1550       routine.  Therefore, you  will  need  to  call  setjmp  and  pass  your
1551       png_jmpbuf(png_ptr).  If you read the file from different routines, you
1552       will need to update the longjmp buffer every time you enter a new  rou‐
1553       tine that will call a png_*() function.
1554
1555       See  your  documentation  of  setjmp/longjmp for your compiler for more
1556       information on setjmp/longjmp.  See the discussion on libpng error han‐
1557       dling  in  the Customizing Libpng section below for more information on
1558       the libpng error handling.  If an error occurs,  and  libpng  longjmp's
1559       back to your setjmp, you will want to call png_destroy_read_struct() to
1560       free any memory.
1561
1562           if (setjmp(png_jmpbuf(png_ptr)))
1563           {
1564              png_destroy_read_struct(&png_ptr, &info_ptr,
1565                  &end_info);
1566              fclose(fp);
1567              return (ERROR);
1568           }
1569
1570       Pass (png_infopp)NULL instead of &end_info  if  you  didn't  create  an
1571       end_info structure.
1572
1573       If  you would rather avoid the complexity of setjmp/longjmp issues, you
1574       can compile libpng with PNG_NO_SETJMP, in which case errors will result
1575       in a call to PNG_ABORT() which defaults to abort().
1576
1577       You can #define PNG_ABORT() to a function that does something more use‐
1578       ful than abort(), as long as your function does not return.
1579
1580       Now you need to set up the input code.  The default for  libpng  is  to
1581       use  the  C function fread().  If you use this, you will need to pass a
1582       valid FILE * in the function png_init_io().  Be sure that the  file  is
1583       opened  in  binary mode.  If you wish to handle reading data in another
1584       way, you need not call the png_init_io() function, but  you  must  then
1585       implement  the  libpng  I/O methods discussed in the Customizing Libpng
1586       section below.
1587
1588           png_init_io(png_ptr, fp);
1589
1590       If you had previously opened the file and read  any  of  the  signature
1591       from  the beginning in order to see if this was a PNG file, you need to
1592       let libpng know that there are some bytes missing from the start of the
1593       file.
1594
1595           png_set_sig_bytes(png_ptr, number);
1596
1597       You  can change the zlib compression buffer size to be used while read‐
1598       ing compressed data with
1599
1600           png_set_compression_buffer_size(png_ptr, buffer_size);
1601
1602       where the default size is 8192 bytes.  Note that  the  buffer  size  is
1603       changed  immediately and the buffer is reallocated immediately, instead
1604       of setting a flag to be acted upon later.
1605
1606       If you want CRC errors to be handled in a  different  manner  than  the
1607       default, use
1608
1609           png_set_crc_action(png_ptr, crit_action, ancil_action);
1610
1611       The  values  for  png_set_crc_action()  say how libpng is to handle CRC
1612       errors in ancillary and critical chunks, and whether to  use  the  data
1613       contained  therein.   Note that it is impossible to "discard" data in a
1614       critical chunk.
1615
1616       Choices for (int) crit_action are
1617          PNG_CRC_DEFAULT      0  error/quit
1618          PNG_CRC_ERROR_QUIT   1  error/quit
1619          PNG_CRC_WARN_USE     3  warn/use data
1620          PNG_CRC_QUIET_USE    4  quiet/use data
1621          PNG_CRC_NO_CHANGE    5  use the current value
1622
1623       Choices for (int) ancil_action are
1624          PNG_CRC_DEFAULT      0  error/quit
1625          PNG_CRC_ERROR_QUIT   1  error/quit
1626          PNG_CRC_WARN_DISCARD 2  warn/discard data
1627          PNG_CRC_WARN_USE     3  warn/use data
1628          PNG_CRC_QUIET_USE    4  quiet/use data
1629          PNG_CRC_NO_CHANGE    5  use the current value
1630
1631
1632   Setting up callback code
1633       You can set up a callback function to handle any unknown chunks in  the
1634       input stream. You must supply the function
1635
1636           read_chunk_callback(png_structp png_ptr,
1637                png_unknown_chunkp chunk);
1638           {
1639              /* The unknown chunk structure contains your
1640                 chunk data, along with similar data for any other
1641                 unknown chunks: */
1642
1643                  png_byte name[5];
1644                  png_byte *data;
1645                  png_size_t size;
1646
1647              /* Note that libpng has already taken care of
1648                 the CRC handling */
1649
1650              /* put your code here.  Search for your chunk in the
1651                 unknown chunk structure, process it, and return one
1652                 of the following: */
1653
1654              return (-n); /* chunk had an error */
1655              return (0); /* did not recognize */
1656              return (n); /* success */
1657           }
1658
1659       (You  can  give  your  function  another  name that you like instead of
1660       "read_chunk_callback")
1661
1662       To inform libpng about your function, use
1663
1664           png_set_read_user_chunk_fn(png_ptr, user_chunk_ptr,
1665               read_chunk_callback);
1666
1667       This names not only the callback function, but also a user pointer that
1668       you can retrieve with
1669
1670           png_get_user_chunk_ptr(png_ptr);
1671
1672       If you call the png_set_read_user_chunk_fn() function, then all unknown
1673       chunks will be saved when read, in case  your  callback  function  will
1674       need  one  or  more  of  them.   This  behavior can be changed with the
1675       png_set_keep_unknown_chunks() function, described below.
1676
1677       At this point, you can set up a callback function that will  be  called
1678       after  each  row has been read, which you can use to control a progress
1679       meter or the like.  It's demonstrated in pngtest.c.  You must supply  a
1680       function
1681
1682           void read_row_callback(png_structp png_ptr,
1683              png_uint_32 row, int pass);
1684           {
1685             /* put your code here */
1686           }
1687
1688       (You  can give it another name that you like instead of "read_row_call‐
1689       back")
1690
1691       To inform libpng about your function, use
1692
1693           png_set_read_status_fn(png_ptr, read_row_callback);
1694
1695       When this function is called the row has already been  completely  pro‐
1696       cessed  and  the  'row' and 'pass' refer to the next row to be handled.
1697       For the non-interlaced case the row that was just handled is simply one
1698       less than the passed in row number, and pass will always be 0.  For the
1699       interlaced case the same applies unless the row value is  0,  in  which
1700       case  the  row  just handled was the last one from one of the preceding
1701       passes.  Because interlacing may skip a pass you cannot  be  sure  that
1702       the  preceding  pass  is just 'pass-1', if you really need to know what
1703       the last pass is record (row,pass) from the callback and use  the  last
1704       recorded value each time.
1705
1706       As  with  the  user  transform  you  can  find the output row using the
1707       PNG_ROW_FROM_PASS_ROW macro.
1708
1709
1710   Unknown-chunk handling
1711       Now you get to set the way the library processes unknown chunks in  the
1712       input  PNG  stream. Both known and unknown chunks will be read.  Normal
1713       behavior is that known chunks will be parsed into information in  vari‐
1714       ous  info_ptr  members  while  unknown  chunks  will be discarded. This
1715       behavior can be wasteful if your application will never use some  known
1716       chunk types. To change this, you can call:
1717
1718           png_set_keep_unknown_chunks(png_ptr, keep,
1719               chunk_list, num_chunks);
1720           keep       - 0: default unknown chunk handling
1721                        1: ignore; do not keep
1722                        2: keep only if safe-to-copy
1723                        3: keep even if unsafe-to-copy
1724
1725                      You can use these definitions:
1726                        PNG_HANDLE_CHUNK_AS_DEFAULT   0
1727                        PNG_HANDLE_CHUNK_NEVER        1
1728                        PNG_HANDLE_CHUNK_IF_SAFE      2
1729                        PNG_HANDLE_CHUNK_ALWAYS       3
1730
1731           chunk_list - list of chunks affected (a byte string,
1732                        five bytes per chunk, NULL or ' ' if
1733                        num_chunks is 0)
1734
1735           num_chunks - number of chunks affected; if 0, all
1736                        unknown chunks are affected.  If nonzero,
1737                        only the chunks in the list are affected
1738
1739       Unknown  chunks  declared  in this way will be saved as raw data onto a
1740       list of png_unknown_chunk structures.  If  a  chunk  that  is  normally
1741       known  to  libpng  is named in the list, it will be handled as unknown,
1742       according to the "keep" directive.  If a chunk is named  in  successive
1743       instances  of  png_set_keep_unknown_chunks(),  the  final instance will
1744       take precedence.  The IHDR and IEND  chunks  should  not  be  named  in
1745       chunk_list;  if they are, libpng will process them normally anyway.  If
1746       you know that your application will never make use of  some  particular
1747       chunks, use PNG_HANDLE_CHUNK_NEVER (or 1) as demonstrated below.
1748
1749       Here is an example of the usage of png_set_keep_unknown_chunks(), where
1750       the private "vpAg" chunk will later be processed by a user chunk  call‐
1751       back function:
1752
1753           png_byte vpAg[5]={118, 112,  65, 103, (png_byte) ' '};
1754
1755           #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1756             png_byte unused_chunks[]=
1757             {
1758               104,  73,  83,  84, (png_byte) ' ',   /* hIST */
1759               105,  84,  88, 116, (png_byte) ' ',   /* iTXt */
1760               112,  67,  65,  76, (png_byte) ' ',   /* pCAL */
1761               115,  67,  65,  76, (png_byte) ' ',   /* sCAL */
1762               115,  80,  76,  84, (png_byte) ' ',   /* sPLT */
1763               116,  73,  77,  69, (png_byte) ' ',   /* tIME */
1764             };
1765           #endif
1766
1767           ...
1768
1769           #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1770             /* ignore all unknown chunks: */
1771             png_set_keep_unknown_chunks(read_ptr, 1, NULL, 0);
1772
1773             /* except for vpAg: */
1774             png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1);
1775
1776             /* also ignore unused known chunks: */
1777             png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks,
1778                (int)sizeof(unused_chunks)/5);
1779           #endif
1780
1781
1782   User limits
1783       The  PNG specification allows the width and height of an image to be as
1784       large as 2^31-1 (0x7fffffff), or about 2.147 billion rows and  columns.
1785       Since  very  few applications really need to process such large images,
1786       we have imposed an arbitrary  1-million  limit  on  rows  and  columns.
1787       Larger  images will be rejected immediately with a png_error() call. If
1788       you wish to change this limit, you can use
1789
1790          png_set_user_limits(png_ptr, width_max, height_max);
1791
1792       to set your own limits, or use width_max = height_max = 0x7fffffffL  to
1793       allow  all  valid  dimensions (libpng may reject some very large images
1794       anyway because of potential buffer overflow conditions).
1795
1796       You should put this statement after you create the  PNG  structure  and
1797       before calling png_read_info(), png_read_png(), or png_process_data().
1798
1799       When  writing  a  PNG  datastream,  put  this  statement before calling
1800       png_write_info() or png_write_png().
1801
1802       If you need to retrieve the limits that are being applied, use
1803
1804          width_max = png_get_user_width_max(png_ptr);
1805          height_max = png_get_user_height_max(png_ptr);
1806
1807       The PNG specification sets no limit on the number of  ancillary  chunks
1808       allowed  in a PNG datastream.  You can impose a limit on the total num‐
1809       ber of sPLT, tEXt, iTXt, zTXt, and unknown chunks that will be  stored,
1810       with
1811
1812          png_set_chunk_cache_max(png_ptr, user_chunk_cache_max);
1813
1814       where 0x7fffffffL means unlimited.  You can retrieve this limit with
1815
1816          chunk_cache_max = png_get_chunk_cache_max(png_ptr);
1817
1818       This  limit also applies to the number of buffers that can be allocated
1819       by png_decompress_chunk() while  decompressing  iTXt,  zTXt,  and  iCCP
1820       chunks.
1821
1822       You  can  also  set  a  limit on the amount of memory that a compressed
1823       chunk other than IDAT can occupy, with
1824
1825          png_set_chunk_malloc_max(png_ptr, user_chunk_malloc_max);
1826
1827       and you can retrieve the limit with
1828
1829          chunk_malloc_max = png_get_chunk_malloc_max(png_ptr);
1830
1831       Any chunks that would cause either of these limits to be exceeded  will
1832       be ignored.
1833
1834
1835   Information about your system
1836       If  you  intend  to display the PNG or to incorporate it in other image
1837       data you need to tell libpng information about your display or  drawing
1838       surface so that libpng can convert the values in the image to match the
1839       display.
1840
1841       From libpng-1.5.4 this information can be set before  reading  the  PNG
1842       file  header.   In earlier versions png_set_gamma() existed but behaved
1843       incorrectly if called before the PNG file  header  had  been  read  and
1844       png_set_alpha_mode() did not exist.
1845
1846       If  you need to support versions prior to libpng-1.5.4 test the version
1847       number as illustrated below using "PNG_LIBPNG_VER >= 10504" and  follow
1848       the procedures described in the appropriate manual page.
1849
1850       You  give  libpng  the  encoding expected by your system expressed as a
1851       'gamma' value.  You can also specify a default  encoding  for  the  PNG
1852       file  in  case  the  required information is missing from the file.  By
1853       default libpng assumes that the PNG data matches your system,  to  keep
1854       this default call:
1855
1856          png_set_gamma(png_ptr, screen_gamma, 1/screen_gamma/*file gamma*/);
1857
1858       or you can use the fixed point equivalent:
1859
1860          png_set_gamma_fixed(png_ptr,                  PNG_FP_1*screen_gamma,
1861       PNG_FP_1/screen_gamma);
1862
1863       If you don't know the gamma for your system it is probably 2.2 - a good
1864       approximation  to  the  IEC  standard  for  display systems (sRGB).  If
1865       images are too contrasty or washed out you got the value wrong -  check
1866       your system documentation!
1867
1868       Many  systems  permit the system gamma to be changed via a lookup table
1869       in the display driver, a few systems, including older Macs, change  the
1870       response by default.  As of 1.5.4 three special values are available to
1871       handle common situations:
1872
1873          PNG_DEFAULT_sRGB: Indicates that the  system  conforms  to  the  IEC
1874       61966-2-1
1875                            standard.  This matches almost all systems.
1876          PNG_GAMMA_MAC_18:  Indicates that the system is an older (pre Mac OS
1877       10.6)
1878                            Apple Macintosh system with the default settings.
1879          PNG_GAMMA_LINEAR: Just the fixed point value  for  1.0  -  indicates
1880       that the
1881                            system expects data with no gamma encoding.
1882
1883       You  would  use the linear (unencoded) value if you need to process the
1884       pixel values further because this avoids the need to decode  and  reen‐
1885       code  each  component value whenever arithmetic is performed.  A lot of
1886       graphics software uses linear values for this reason, often with higher
1887       precision component values to preserve overall accuracy.
1888
1889       The  second  thing you may need to tell libpng about is how your system
1890       handles alpha channel information.  Some, but not all, PNG  files  con‐
1891       tain  an  alpha  channel.  To display these files correctly you need to
1892       compose the data onto a suitable background, as described  in  the  PNG
1893       specification.
1894
1895       Libpng only supports composing onto a single color (using png_set_back‐
1896       ground; see below).  Otherwise you must  do  the  composition  yourself
1897       and, in this case, you may need to call png_set_alpha_mode:
1898
1899           #if PNG_LIBPNG_VER >= 10504
1900              png_set_alpha_mode(png_ptr, mode, screen_gamma);
1901           #else
1902              png_set_gamma(png_ptr, screen_gamma, 1.0/screen_gamma);
1903           #endif
1904
1905       The  screen_gamma  value  is the same as the argument to png_set_gamma;
1906       however,  how  it   affects   the   output   depends   on   the   mode.
1907       png_set_alpha_mode()  sets the file gamma default to 1/screen_gamma, so
1908       normally you don't need to call png_set_gamma.  If you  need  different
1909       defaults call png_set_gamma() before png_set_alpha_mode() - if you call
1910       it after it will override the settings made by png_set_alpha_mode().
1911
1912       The mode is as follows:
1913
1914           PNG_ALPHA_PNG: The data is encoded according to the PNG  specifica‐
1915       tion.  Red, green and blue, or gray, components are gamma encoded color
1916       values and are not premultiplied by the alpha value.  The  alpha  value
1917       is a linear measure of the contribution of the pixel to the correspond‐
1918       ing final output pixel.
1919
1920       You should normally use this format if you intend to perform color cor‐
1921       rection on the color values; most, maybe all, color correction software
1922       has no handling for the alpha channel and, anyway, the math  to  handle
1923       pre-multiplied component values is unnecessarily complex.
1924
1925       Before you do any arithmetic on the component values you need to remove
1926       the gamma encoding and multiply out the alpha  channel.   See  the  PNG
1927       specification  for  more  detail.  It is important to note that when an
1928       image with an alpha channel is scaled, linear  encoded,  pre-multiplied
1929       component values must be used!
1930
1931       The  remaining modes assume you don't need to do any further color cor‐
1932       rection or that if you do, your color  correction  software  knows  all
1933       about alpha (it probably doesn't!)
1934
1935           PNG_ALPHA_STANDARD:   The  data  libpng  produces is encoded in the
1936       standard way assumed by most correctly written graphics software.   The
1937       gamma  encoding will be removed by libpng and the linear component val‐
1938       ues will be pre-multiplied by the alpha channel.
1939
1940       With this format the final image must be re-encoded to match  the  dis‐
1941       play  gamma  before  the image is displayed.  If your system doesn't do
1942       that, yet still seems to  perform  arithmetic  on  the  pixels  without
1943       decoding them, it is broken - check out the modes below.
1944
1945       With PNG_ALPHA_STANDARD libpng always produces linear component values,
1946       whatever screen_gamma you supply.  The screen_gamma value is,  however,
1947       used  as  a  default  for  the  file gamma if the PNG file has no gamma
1948       information.
1949
1950       If you call png_set_gamma() after png_set_alpha_mode() you  will  over‐
1951       ride the linear encoding.  Instead the pre-multiplied pixel values will
1952       be gamma encoded but the alpha channel will still be linear.  This  may
1953       actually  match  the  requirements  of  some broken software, but it is
1954       unlikely.
1955
1956       While linear 8-bit data is often used it has insufficient precision for
1957       any  image  with a reasonable dynamic range.  To avoid problems, and if
1958       your software supports it, use png_set_expand_16() to force all  compo‐
1959       nents to 16 bits.
1960
1961           PNG_ALPHA_OPTIMIZED:  This  mode  is the same as PNG_ALPHA_STANDARD
1962       except that completely opaque pixels are gamma encoded according to the
1963       screen_gamma  value.   Pixels  with alpha less than 1.0 will still have
1964       linear components.
1965
1966       Use this format if you have control over your compositing software  and
1967       so don't do other arithmetic (such as scaling) on the data you get from
1968       libpng.  Your compositing software can simply copy opaque pixels to the
1969       output but still has linear values for the non-opaque pixels.
1970
1971       In  normal  compositing,  where the alpha channel encodes partial pixel
1972       coverage (as opposed to broad area translucency), the  inaccuracies  of
1973       the 8-bit representation of non-opaque pixels are irrelevant.
1974
1975       You  can also try this format if your software is broken; it might look
1976       better.
1977
1978           PNG_ALPHA_BROKEN: This is PNG_ALPHA_STANDARD; however,  all  compo‐
1979       nent values, including the alpha channel are gamma encoded.  This is an
1980       appropriate format to try if your software, or more likely hardware, is
1981       totally  broken,  i.e.,  if  it  performs linear arithmetic directly on
1982       gamma encoded values.
1983
1984       In most cases of broken software or hardware the bug in the final  dis‐
1985       play  manifests  as a subtle halo around composited parts of the image.
1986       You may not even perceive this as a halo; the composited  part  of  the
1987       image  may simply appear separate from the background, as though it had
1988       been cut out of paper and pasted on afterward.
1989
1990       If you don't have to deal with bugs in software or hardware, or if  you
1991       can   fix   them,   there   are   three   recommended   ways  of  using
1992       png_set_alpha_mode():
1993
1994          png_set_alpha_mode(png_ptr, PNG_ALPHA_PNG,
1995              screen_gamma);
1996
1997       You can do color correction on the result (libpng  does  not  currently
1998       support  color correction internally).  When you handle the alpha chan‐
1999       nel you need to undo the gamma encoding and multiply out the alpha.
2000
2001          png_set_alpha_mode(png_ptr, PNG_ALPHA_STANDARD,
2002              screen_gamma);
2003          png_set_expand_16(png_ptr);
2004
2005       If   you   are   using   the   high   level   interface,   don't   call
2006       png_set_expand_16(); instead pass PNG_TRANSFORM_EXPAND_16 to the inter‐
2007       face.
2008
2009       With this mode you can't do color correction, but  you  can  do  arith‐
2010       metic,  including  composition and scaling, on the data without further
2011       processing.
2012
2013          png_set_alpha_mode(png_ptr, PNG_ALPHA_OPTIMIZED,
2014              screen_gamma);
2015
2016       You can avoid the expansion to 16-bit components with  this  mode,  but
2017       you  lose the ability to scale the image or perform other linear arith‐
2018       metic.  All you can do is compose the result onto  a  matching  output.
2019       Since this mode is libpng-specific you also need to write your own com‐
2020       position software.
2021
2022       If you don't need, or can't handle, the  alpha  channel  you  can  call
2023       png_set_background() to remove it by compositing against a fixed color.
2024       Don't call png_set_strip_alpha() to do this - it  will  leave  spurious
2025       pixel values in transparent parts of this image.
2026
2027          png_set_background(png_ptr, &background_color,
2028              PNG_BACKGROUND_GAMMA_SCREEN, 0, 1);
2029
2030       The background_color is an RGB or grayscale value according to the data
2031       format libpng will produce for you.  Because you  don't  yet  know  the
2032       format  of  the  PNG file, if you call png_set_background at this point
2033       you must arrange for the format produced by libpng to always have 8-bit
2034       or  16-bit  components  and  then store the color as an 8-bit or 16-bit
2035       color as appropriate.  The color contains separate gray and RGB  compo‐
2036       nent values, so you can let libpng produce gray or RGB output according
2037       to the input format, but low bit depth grayscale images must always  be
2038       converted  to  at  least  8-bit  format.   (Even  though  low bit depth
2039       grayscale images can't have an alpha channel they can have a  transpar‐
2040       ent color!)
2041
2042       You  set  the  transforms  you  need later, either as flags to the high
2043       level interface or libpng API calls for the low level  interface.   For
2044       reference the settings and API calls required are:
2045
2046       8-bit values:
2047          PNG_TRANSFORM_SCALE_16 | PNG_EXPAND
2048          png_set_expand(png_ptr); png_set_scale_16(png_ptr);
2049
2050          If you must get exactly the same inaccurate results
2051          produced by default in versions prior to libpng-1.5.4,
2052          use PNG_TRANSFORM_STRIP_16 and png_set_strip_16(png_ptr)
2053          instead.
2054
2055       16-bit values:
2056          PNG_TRANSFORM_EXPAND_16
2057          png_set_expand_16(png_ptr);
2058
2059       In either case palette image data will be expanded to RGB.  If you just
2060       want   color   data   you   can   add   PNG_TRANSFORM_GRAY_TO_RGB    or
2061       png_set_gray_to_rgb(png_ptr) to the list.
2062
2063       Calling  png_set_background before the PNG file header is read will not
2064       work prior to libpng-1.5.4.  Because the failure may  result  in  unex‐
2065       pected   warnings  or  errors  it  is  therefore  much  safer  to  call
2066       png_set_background after the head has been  read.   Unfortunately  this
2067       means  that prior to libpng-1.5.4 it cannot be used with the high level
2068       interface.
2069
2070
2071   The high-level read interface
2072       At this point there are two ways to  proceed;  through  the  high-level
2073       read  interface,  or  through  a sequence of low-level read operations.
2074       You can use the high-level interface if (a) you are willing to read the
2075       entire image into memory, and (b) the input transformations you want to
2076       do are limited to the following set:
2077
2078           PNG_TRANSFORM_IDENTITY      No transformation
2079           PNG_TRANSFORM_SCALE_16      Strip 16-bit samples to
2080                                       8-bit accurately
2081           PNG_TRANSFORM_STRIP_16      Chop 16-bit samples to
2082                                       8-bit less accurately
2083           PNG_TRANSFORM_STRIP_ALPHA   Discard the alpha channel
2084           PNG_TRANSFORM_PACKING       Expand 1, 2 and 4-bit
2085                                       samples to bytes
2086           PNG_TRANSFORM_PACKSWAP      Change order of packed
2087                                       pixels to LSB first
2088           PNG_TRANSFORM_EXPAND        Perform set_expand()
2089           PNG_TRANSFORM_INVERT_MONO   Invert monochrome images
2090           PNG_TRANSFORM_SHIFT         Normalize pixels to the
2091                                       sBIT depth
2092           PNG_TRANSFORM_BGR           Flip RGB to BGR, RGBA
2093                                       to BGRA
2094           PNG_TRANSFORM_SWAP_ALPHA    Flip RGBA to ARGB or GA
2095                                       to AG
2096           PNG_TRANSFORM_INVERT_ALPHA  Change alpha from opacity
2097                                       to transparency
2098           PNG_TRANSFORM_SWAP_ENDIAN   Byte-swap 16-bit samples
2099           PNG_TRANSFORM_GRAY_TO_RGB   Expand grayscale samples
2100                                       to RGB (or GA to RGBA)
2101           PNG_TRANSFORM_EXPAND_16     Expand samples to 16 bits
2102
2103       (This excludes setting a background color, doing gamma  transformation,
2104       quantizing, and setting filler.)  If this is the case, simply do this:
2105
2106           png_read_png(png_ptr, info_ptr, png_transforms, NULL)
2107
2108       where  png_transforms  is  an integer containing the bitwise OR of some
2109       set  of   transformation   flags.    This   call   is   equivalent   to
2110       png_read_info(),  followed  the set of transformations indicated by the
2111       transform mask, then png_read_image(), and finally png_read_end().
2112
2113       (The final parameter of this call is not yet used.   Someday  it  might
2114       point to transformation parameters required by some future input trans‐
2115       form.)
2116
2117       You must use png_transforms and not call any png_set_transform()  func‐
2118       tions when you use png_read_png().
2119
2120       After  you  have called png_read_png(), you can retrieve the image data
2121       with
2122
2123          row_pointers = png_get_rows(png_ptr, info_ptr);
2124
2125       where row_pointers is an array of pointers to the pixel data  for  each
2126       row:
2127
2128          png_bytep row_pointers[height];
2129
2130       If you know your image size and pixel size ahead of time, you can allo‐
2131       cate row_pointers prior to calling png_read_png() with
2132
2133          if (height > PNG_UINT_32_MAX/png_sizeof(png_byte))
2134             png_error (png_ptr,
2135                 "Image is too tall to process in memory");
2136
2137          if (width > PNG_UINT_32_MAX/pixel_size)
2138             png_error (png_ptr,
2139                 "Image is too wide to process in memory");
2140
2141          row_pointers = png_malloc(png_ptr,
2142              height*png_sizeof(png_bytep));
2143
2144          for (int i=0; i<height, i++)
2145             row_pointers[i]=NULL;  /* security precaution */
2146
2147          for (int i=0; i<height, i++)
2148             row_pointers[i]=png_malloc(png_ptr,
2149                 width*pixel_size);
2150
2151          png_set_rows(png_ptr, info_ptr, &row_pointers);
2152
2153       Alternatively you could allocate your image in one big block and define
2154       row_pointers[i] to point into the proper places in your block.
2155
2156       If  you  use png_set_rows(), the application is responsible for freeing
2157       row_pointers (and row_pointers[i], if they were separately allocated).
2158
2159       If you don't allocate row_pointers ahead of time,  png_read_png()  will
2160       do it, and it'll be free'ed by libpng when you call png_destroy_*().
2161
2162
2163   The low-level read interface
2164       If you are going the low-level route, you are now ready to read all the
2165       file information up to the actual image data.  You do this with a  call
2166       to png_read_info().
2167
2168           png_read_info(png_ptr, info_ptr);
2169
2170       This will process all chunks up to but not including the image data.
2171
2172       This  also  copies  some  of the data from the PNG file into the decode
2173       structure for use  in  later  transformations.   Important  information
2174       copied in is:
2175
2176       1) The PNG file gamma from the gAMA chunk.  This overwrites the default
2177       value   provided   by   an   earlier   call   to    png_set_gamma    or
2178       png_set_alpha_mode.
2179
2180       2)  Prior to libpng-1.5.4 the background color from a bKGd chunk.  This
2181       damages the information provided by an earlier  call  to  png_set_back‐
2182       ground  resulting  in unexpected behavior.  Libpng-1.5.4 no longer does
2183       this.
2184
2185       3) The number of significant bits in each component value.  Libpng uses
2186       this  to  optimize gamma handling by reducing the internal lookup table
2187       sizes.
2188
2189       4) The transparent color information from a tRNS chunk.   This  can  be
2190       modified by a later call to png_set_tRNS.
2191
2192
2193   Querying the info structure
2194       Functions are used to get the information from the info_ptr once it has
2195       been read.  Note that these fields may  not  be  completely  filled  in
2196       until png_read_end() has read the chunk data following the image.
2197
2198           png_get_IHDR(png_ptr, info_ptr, &width, &height,
2199              &bit_depth, &color_type, &interlace_type,
2200              &compression_type, &filter_method);
2201
2202           width          - holds the width of the image
2203                            in pixels (up to 2^31).
2204
2205           height         - holds the height of the image
2206                            in pixels (up to 2^31).
2207
2208           bit_depth      - holds the bit depth of one of the
2209                            image channels.  (valid values are
2210                            1, 2, 4, 8, 16 and depend also on
2211                            the color_type.  See also
2212                            significant bits (sBIT) below).
2213
2214           color_type     - describes which color/alpha channels
2215                                are present.
2216                            PNG_COLOR_TYPE_GRAY
2217                               (bit depths 1, 2, 4, 8, 16)
2218                            PNG_COLOR_TYPE_GRAY_ALPHA
2219                               (bit depths 8, 16)
2220                            PNG_COLOR_TYPE_PALETTE
2221                               (bit depths 1, 2, 4, 8)
2222                            PNG_COLOR_TYPE_RGB
2223                               (bit_depths 8, 16)
2224                            PNG_COLOR_TYPE_RGB_ALPHA
2225                               (bit_depths 8, 16)
2226
2227                            PNG_COLOR_MASK_PALETTE
2228                            PNG_COLOR_MASK_COLOR
2229                            PNG_COLOR_MASK_ALPHA
2230
2231           interlace_type - (PNG_INTERLACE_NONE or
2232                            PNG_INTERLACE_ADAM7)
2233
2234           compression_type - (must be PNG_COMPRESSION_TYPE_BASE
2235                            for PNG 1.0)
2236
2237           filter_method  - (must be PNG_FILTER_TYPE_BASE
2238                            for PNG 1.0, and can also be
2239                            PNG_INTRAPIXEL_DIFFERENCING if
2240                            the PNG datastream is embedded in
2241                            a MNG-1.0 datastream)
2242
2243           Any or all of interlace_type, compression_type, or
2244           filter_method can be NULL if you are
2245           not interested in their values.
2246
2247           Note that png_get_IHDR() returns 32-bit data into
2248           the application's width and height variables.
2249           This is an unsafe situation if these are 16-bit
2250           variables.  In such situations, the
2251           png_get_image_width() and png_get_image_height()
2252           functions described below are safer.
2253
2254           width            = png_get_image_width(png_ptr,
2255                                info_ptr);
2256
2257           height           = png_get_image_height(png_ptr,
2258                                info_ptr);
2259
2260           bit_depth        = png_get_bit_depth(png_ptr,
2261                                info_ptr);
2262
2263           color_type       = png_get_color_type(png_ptr,
2264                                info_ptr);
2265
2266           interlace_type   = png_get_interlace_type(png_ptr,
2267                                info_ptr);
2268
2269           compression_type = png_get_compression_type(png_ptr,
2270                                info_ptr);
2271
2272           filter_method    = png_get_filter_type(png_ptr,
2273                                info_ptr);
2274
2275           channels = png_get_channels(png_ptr, info_ptr);
2276
2277           channels       - number of channels of info for the
2278                            color type (valid values are 1 (GRAY,
2279                            PALETTE), 2 (GRAY_ALPHA), 3 (RGB),
2280                            4 (RGB_ALPHA or RGB + filler byte))
2281
2282           rowbytes = png_get_rowbytes(png_ptr, info_ptr);
2283
2284           rowbytes       - number of bytes needed to hold a row
2285
2286           signature = png_get_signature(png_ptr, info_ptr);
2287
2288           signature      - holds the signature read from the
2289                            file (if any).  The data is kept in
2290                            the same offset it would be if the
2291                            whole signature were read (i.e. if an
2292                            application had already read in 4
2293                            bytes of signature before starting
2294                            libpng, the remaining 4 bytes would
2295                            be in signature[4] through signature[7]
2296                            (see png_set_sig_bytes())).
2297
2298       These  are  also  important,  but their validity depends on whether the
2299       chunk   has   been   read.    The   png_get_valid(png_ptr,    info_ptr,
2300       PNG_INFO_<chunk>) and png_get_<chunk>(png_ptr, info_ptr, ...) functions
2301       return non-zero if the data has been read, or zero if  it  is  missing.
2302       The parameters to the png_get_<chunk> are set directly if they are sim‐
2303       ple data types, or a pointer into the info_ptr is returned for any com‐
2304       plex types.
2305
2306       The  colorspace  data  from  gAMA, cHRM, sRGB, iCCP, and sBIT chunks is
2307       simply returned to give the application information about how the image
2308       was  encoded.   Libpng  itself only does transformations using the file
2309       gamma when combining semitransparent pixels with the background color.
2310
2311           png_get_PLTE(png_ptr, info_ptr, &palette,
2312                            &num_palette);
2313
2314           palette        - the palette for the file
2315                            (array of png_color)
2316
2317           num_palette    - number of entries in the palette
2318
2319           png_get_gAMA(png_ptr, info_ptr, &file_gamma);
2320           png_get_gAMA_fixed(png_ptr, info_ptr, &int_file_gamma);
2321
2322           file_gamma     - the gamma at which the file was
2323                            written (PNG_INFO_gAMA)
2324
2325           int_file_gamma - 100,000 times the gamma at which the
2326                            file is written
2327
2328           png_get_cHRM(png_ptr, info_ptr,  &white_x, &white_y, &red_x,
2329                            &red_y, &green_x, &green_y, &blue_x, &blue_y)
2330           png_get_cHRM_XYZ(png_ptr,   info_ptr,   &red_X,   &red_Y,   &red_Z,
2331       &green_X,
2332                            &green_Y, &green_Z, &blue_X, &blue_Y, &blue_Z)
2333           png_get_cHRM_fixed(png_ptr, info_ptr, &int_white_x, &int_white_y,
2334                            &int_red_x,        &int_red_y,       &int_green_x,
2335       &int_green_y,
2336                            &int_blue_x, &int_blue_y)
2337           png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, &int_red_X, &int_red_Y,
2338                            &int_red_Z,      &int_green_X,       &int_green_Y,
2339       &int_green_Z,
2340                            &int_blue_X, &int_blue_Y, &int_blue_Z)
2341
2342           {white,red,green,blue}_{x,y}
2343                            A color space encoding specified using the
2344                            chromaticities of the end points and the
2345                            white point. (PNG_INFO_cHRM)
2346
2347           {red,green,blue}_{X,Y,Z}
2348                            A  color space encoding specified using the encod‐
2349       ing end
2350                            points - the CIE tristimulus specification of  the
2351       intended
2352                            color  of  the red, green and blue channels in the
2353       PNG RGB
2354                            data.  The white point is simply the  sum  of  the
2355       three end
2356                            points. (PNG_INFO_cHRM)
2357
2358           png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
2359
2360           file_srgb_intent - the rendering intent (PNG_INFO_sRGB)
2361                            The presence of the sRGB chunk
2362                            means that the pixel data is in the
2363                            sRGB color space.  This chunk also
2364                            implies specific values of gAMA and
2365                            cHRM.
2366
2367           png_get_iCCP(png_ptr, info_ptr, &name,
2368              &compression_type, &profile, &proflen);
2369
2370           name             - The profile name.
2371
2372           compression_type - The compression type; always
2373                              PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
2374                              You may give NULL to this argument to
2375                              ignore it.
2376
2377           profile          - International Color Consortium color
2378                              profile data. May contain NULs.
2379
2380           proflen          - length of profile data in bytes.
2381
2382           png_get_sBIT(png_ptr, info_ptr, &sig_bit);
2383
2384           sig_bit        - the number of significant bits for
2385                            (PNG_INFO_sBIT) each of the gray,
2386                            red, green, and blue channels,
2387                            whichever are appropriate for the
2388                            given color type (png_color_16)
2389
2390           png_get_tRNS(png_ptr, info_ptr, &trans_alpha,
2391                            &num_trans, &trans_color);
2392
2393           trans_alpha    - array of alpha (transparency)
2394                            entries for palette (PNG_INFO_tRNS)
2395
2396           num_trans      - number of transparent entries
2397                            (PNG_INFO_tRNS)
2398
2399           trans_color    - graylevel or color sample values of
2400                            the single transparent color for
2401                            non-paletted images (PNG_INFO_tRNS)
2402
2403           png_get_hIST(png_ptr, info_ptr, &hist);
2404                            (PNG_INFO_hIST)
2405
2406           hist           - histogram of palette (array of
2407                            png_uint_16)
2408
2409           png_get_tIME(png_ptr, info_ptr, &mod_time);
2410
2411           mod_time       - time image was last modified
2412                           (PNG_VALID_tIME)
2413
2414           png_get_bKGD(png_ptr, info_ptr, &background);
2415
2416           background     - background color (of type
2417                            png_color_16p) (PNG_VALID_bKGD)
2418                            valid 16-bit red, green and blue
2419                            values, regardless of color_type
2420
2421           num_comments   = png_get_text(png_ptr, info_ptr,
2422                            &text_ptr, &num_text);
2423
2424           num_comments   - number of comments
2425
2426           text_ptr       - array of png_text holding image
2427                            comments
2428
2429           text_ptr[i].compression - type of compression used
2430                        on "text" PNG_TEXT_COMPRESSION_NONE
2431                                  PNG_TEXT_COMPRESSION_zTXt
2432                                  PNG_ITXT_COMPRESSION_NONE
2433                                  PNG_ITXT_COMPRESSION_zTXt
2434
2435           text_ptr[i].key   - keyword for comment.  Must contain
2436                                1-79 characters.
2437
2438           text_ptr[i].text  - text comments for current
2439                                keyword.  Can be empty.
2440
2441           text_ptr[i].text_length - length of text string,
2442                        after decompression, 0 for iTXt
2443
2444           text_ptr[i].itxt_length - length of itxt string,
2445                        after decompression, 0 for tEXt/zTXt
2446
2447           text_ptr[i].lang  - language of comment (empty
2448                                string for unknown).
2449
2450           text_ptr[i].lang_key  - keyword in UTF-8
2451                                (empty string for unknown).
2452
2453           Note that the itxt_length, lang, and lang_key
2454           members of the text_ptr structure only exist when the
2455           library is built with iTXt chunk support.  Prior to
2456           libpng-1.4.0 the library was built by default without
2457           iTXt support. Also note that when iTXt is supported,
2458           they contain NULL pointers when the "compression"
2459           field contains PNG_TEXT_COMPRESSION_NONE or
2460           PNG_TEXT_COMPRESSION_zTXt.
2461
2462           num_text       - number of comments (same as
2463                            num_comments; you can put NULL here
2464                            to avoid the duplication)
2465
2466           Note while png_set_text() will accept text, language,
2467           and translated keywords that can be NULL pointers, the
2468           structure returned by png_get_text will always contain
2469           regular zero-terminated C strings.  They might be
2470           empty strings but they will never be NULL pointers.
2471
2472           num_spalettes = png_get_sPLT(png_ptr, info_ptr,
2473              &palette_ptr);
2474
2475           num_spalettes  - number of sPLT chunks read.
2476
2477           palette_ptr    - array of palette structures holding
2478                            contents of one or more sPLT chunks
2479                            read.
2480
2481           png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y,
2482              &unit_type);
2483
2484           offset_x       - positive offset from the left edge
2485                            of the screen (can be negative)
2486
2487           offset_y       - positive offset from the top edge
2488                            of the screen (can be negative)
2489
2490           unit_type      - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
2491
2492           png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y,
2493              &unit_type);
2494
2495           res_x          - pixels/unit physical resolution in
2496                            x direction
2497
2498           res_y          - pixels/unit physical resolution in
2499                            x direction
2500
2501           unit_type      - PNG_RESOLUTION_UNKNOWN,
2502                            PNG_RESOLUTION_METER
2503
2504           png_get_sCAL(png_ptr, info_ptr, &unit, &width,
2505              &height)
2506
2507           unit        - physical scale units (an integer)
2508
2509           width       - width of a pixel in physical scale units
2510
2511           height      - height of a pixel in physical scale units
2512                        (width and height are doubles)
2513
2514           png_get_sCAL_s(png_ptr, info_ptr, &unit, &width,
2515              &height)
2516
2517           unit        - physical scale units (an integer)
2518
2519           width       - width of a pixel in physical scale units
2520                         (expressed as a string)
2521
2522           height      - height of a pixel in physical scale units
2523                        (width and height are strings like "2.54")
2524
2525           num_unknown_chunks = png_get_unknown_chunks(png_ptr,
2526              info_ptr, &unknowns)
2527
2528           unknowns          - array of png_unknown_chunk
2529                               structures holding unknown chunks
2530
2531           unknowns[i].name  - name of unknown chunk
2532
2533           unknowns[i].data  - data of unknown chunk
2534
2535           unknowns[i].size  - size of unknown chunk's data
2536
2537           unknowns[i].location - position of chunk in file
2538
2539           The value of "i" corresponds to the order in which the
2540           chunks were read from the PNG file or inserted with the
2541           png_set_unknown_chunks() function.
2542
2543           The value of "location" is a bitwise "or" of
2544
2545                PNG_HAVE_IHDR  (0x01)
2546                PNG_HAVE_PLTE  (0x02)
2547                PNG_AFTER_IDAT (0x08)
2548
2549       The  data  from  the  pHYs chunk can be retrieved in several convenient
2550       forms:
2551
2552           res_x = png_get_x_pixels_per_meter(png_ptr,
2553              info_ptr)
2554
2555           res_y = png_get_y_pixels_per_meter(png_ptr,
2556              info_ptr)
2557
2558           res_x_and_y = png_get_pixels_per_meter(png_ptr,
2559              info_ptr)
2560
2561           res_x = png_get_x_pixels_per_inch(png_ptr,
2562              info_ptr)
2563
2564           res_y = png_get_y_pixels_per_inch(png_ptr,
2565              info_ptr)
2566
2567           res_x_and_y = png_get_pixels_per_inch(png_ptr,
2568              info_ptr)
2569
2570           aspect_ratio = png_get_pixel_aspect_ratio(png_ptr,
2571              info_ptr)
2572
2573           Each of these returns 0 [signifying "unknown"] if
2574              the data is not present or if res_x is 0;
2575              res_x_and_y is 0 if res_x != res_y
2576
2577           Note that because of the way the resolutions are
2578              stored internally, the inch conversions won't
2579              come out to exactly even number.  For example,
2580              72 dpi is stored as 0.28346 pixels/meter, and
2581              when this is retrieved it is 71.9988 dpi, so
2582              be sure to round the returned value appropriately
2583              if you want to display a reasonable-looking result.
2584
2585       The data from the oFFs chunk can be  retrieved  in  several  convenient
2586       forms:
2587
2588           x_offset = png_get_x_offset_microns(png_ptr, info_ptr);
2589
2590           y_offset = png_get_y_offset_microns(png_ptr, info_ptr);
2591
2592           x_offset = png_get_x_offset_inches(png_ptr, info_ptr);
2593
2594           y_offset = png_get_y_offset_inches(png_ptr, info_ptr);
2595
2596           Each of these returns 0 [signifying "unknown" if both
2597              x and y are 0] if the data is not present or if the
2598              chunk is present but the unit is the pixel.  The
2599              remark about inexact inch conversions applies here
2600              as well, because a value in inches can't always be
2601              converted to microns and back without some loss
2602              of precision.
2603
2604       For more information, see the PNG specification for chunk contents.  Be
2605       careful with trusting rowbytes, as some of  the  transformations  could
2606       increase  the  space needed to hold a row (expand, filler, gray_to_rgb,
2607       etc.).  See png_read_update_info(), below.
2608
2609       A quick word about text_ptr and num_text.  PNG stores comments in  key‐
2610       word/text  pairs,  one  pair  per chunk, with no limit on the number of
2611       text chunks, and a 2^31 byte limit on their size.  While there are sug‐
2612       gested  keywords,  there is no requirement to restrict the use to these
2613       strings.  It is strongly suggested that keywords and text  be  sensible
2614       to humans (that's the point), so don't use abbreviations.  Non-printing
2615       symbols are not allowed.  See the PNG specification for  more  details.
2616       There is also no requirement to have text after the keyword.
2617
2618       Keywords  should be limited to 79 Latin-1 characters without leading or
2619       trailing spaces, but non-consecutive spaces are allowed within the key‐
2620       word.   It  is  possible  to have the same keyword any number of times.
2621       The text_ptr is an array of png_text structures, each holding a pointer
2622       to  a  language  string, a pointer to a keyword and a pointer to a text
2623       string.  The text string, language code, and translated keyword may  be
2624       empty  or NULL pointers.  The keyword/text pairs are put into the array
2625       in the order that they are received.  However, some or all of the  text
2626       chunks  may  be after the image, so, to make sure you have read all the
2627       text chunks, don't mess with these until after you read the stuff after
2628       the  image.   This will be mentioned again below in the discussion that
2629       goes with png_read_end().
2630
2631
2632   Input transformations
2633       After you've read the header information, you can set up the library to
2634       handle any special transformations of the image data.  The various ways
2635       to transform the data will be described in the order that  they  should
2636       occur.   This  is  important,  as  some  of these change the color type
2637       and/or bit depth of the data, and some  others  only  work  on  certain
2638       color types and bit depths.
2639
2640       Transformations  you request are ignored if they don't have any meaning
2641       for a particular input data format.  However some  transformations  can
2642       have  an effect as a result of a previous transformation.  If you spec‐
2643       ify a contradictory set of transformations, for example both adding and
2644       removing the alpha channel, you cannot predict the final result.
2645
2646       The  color  used  for the transparency values should be supplied in the
2647       same format/depth as the current image data.  It is stored in the  same
2648       format/depth  as the image data in a tRNS chunk, so this is what libpng
2649       expects for this data.
2650
2651       The color used for the background  value  depends  on  the  need_expand
2652       argument as described below.
2653
2654       Data  will  be  decoded into the supplied row buffers packed into bytes
2655       unless the library has been told to transform it into  another  format.
2656       For  example, 4 bit/pixel paletted or grayscale data will be returned 2
2657       pixels/byte with the leftmost pixel in the high-order bits of the byte,
2658       unless  png_set_packing()  is called.  8-bit RGB data will be stored in
2659       RGB RGB RGB format unless png_set_filler()  or  png_set_add_alpha()  is
2660       called to insert filler bytes, either before or after each RGB triplet.
2661       16-bit RGB data will be returned RRGGBB RRGGBB, with the most  signifi‐
2662       cant byte of the color value first, unless png_set_scale_16() is called
2663       to transform it to regular RGB RGB  triplets,  or  png_set_filler()  or
2664       png_set_add  alpha() is called to insert filler bytes, either before or
2665       after each RRGGBB triplet.  Similarly, 8-bit or 16-bit  grayscale  data
2666       can    be    modified   with   png_set_filler(),   png_set_add_alpha(),
2667       png_set_strip_16(), or png_set_scale_16().
2668
2669       The following code transforms grayscale images of  less  than  8  to  8
2670       bits,  changes paletted images to RGB, and adds a full alpha channel if
2671       there is transparency information in a tRNS chunk.  This is most useful
2672       on  grayscale  images with bit depths of 2 or 4 or if there is a multi‐
2673       ple-image viewing application that wishes to treat all  images  in  the
2674       same way.
2675
2676           if (color_type == PNG_COLOR_TYPE_PALETTE)
2677               png_set_palette_to_rgb(png_ptr);
2678
2679           if (png_get_valid(png_ptr, info_ptr,
2680               PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
2681
2682           if (color_type == PNG_COLOR_TYPE_GRAY &&
2683               bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
2684
2685       The  first  two  functions  are  actually aliases for png_set_expand(),
2686       added in libpng version 1.0.4, with  the  function  names  expanded  to
2687       improve  code readability.  In some future version they may actually do
2688       different things.
2689
2690       As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() was added.
2691       It expands the sample depth without changing tRNS to alpha.
2692
2693       As  of libpng version 1.5.2, png_set_expand_16() was added.  It behaves
2694       as png_set_expand(); however,  the  resultant  channels  have  16  bits
2695       rather  than  8.   Use  this when the output color or gray channels are
2696       made linear to avoid fairly severe accuracy loss.
2697
2698          if (bit_depth < 16)
2699             png_set_expand_16(png_ptr);
2700
2701       PNG can have files with 16 bits per channel.  If you only can handle  8
2702       bits per channel, this will strip the pixels down to 8-bit.
2703
2704           if (bit_depth == 16) #if PNG_LIBPNG_VER >= 10504
2705              png_set_scale_16(png_ptr); #else
2706              png_set_strip_16(png_ptr); #endif
2707
2708       (The  more accurate "png_set_scale_16()" API became available in libpng
2709       version 1.5.4).
2710
2711       If you need to process the alpha channel on the image  separately  from
2712       the  image  data (for example if you convert it to a bitmap mask) it is
2713       possible to have libpng strip the channel  leaving  just  RGB  or  gray
2714       data:
2715
2716           if (color_type & PNG_COLOR_MASK_ALPHA)
2717              png_set_strip_alpha(png_ptr);
2718
2719       If you strip the alpha channel you need to find some other way of deal‐
2720       ing with the information.  If, instead, you want to convert  the  image
2721       to  an opaque version with no alpha channel use png_set_background; see
2722       below.
2723
2724       As of libpng version 1.5.2, almost all useful expansions are supported,
2725       the  major  ommissions  are  conversion  of grayscale to indexed images
2726       (which can be done trivially in  the  application)  and  conversion  of
2727       indexed  to  grayscale  (which can be done by a trivial manipulation of
2728       the palette.)
2729
2730       In the following table, the 01 means grayscale with depth<8,  31  means
2731       indexed  with  depth<8,  other  numerals  represent the color type, "T"
2732       means the tRNS chunk is present, A means an alpha channel  is  present,
2733       and  O  means  tRNS or alpha is present but all pixels in the image are
2734       opaque.
2735
2736         FROM  01  31   0  0T  0O   2  2T  2O   3  3T  3O  4A  4O  6A  6O
2737          TO
2738          01    -  [G]  -   -   -   -   -   -   -   -   -   -   -   -   -
2739          31   [Q]  Q  [Q] [Q] [Q]  Q   Q   Q   Q   Q   Q  [Q] [Q]  Q   Q
2740           0    1   G   +   .   .   G   G   G   G   G   G   B   B  GB  GB
2741          0T    lt  Gt  t   +   .   Gt  G   G   Gt  G   G   Bt  Bt GBt GBt
2742          0O    lt  Gt  t   .   +   Gt  Gt  G   Gt  Gt  G   Bt  Bt GBt GBt
2743           2    C   P   C   C   C   +   .   .   C   -   -  CB  CB   B   B
2744          2T    Ct  -   Ct  C   C   t   +   t   -   -   -  CBt CBt  Bt  Bt
2745          2O    Ct  -   Ct  C   C   t   t   +   -   -   -  CBt CBt  Bt  Bt
2746           3   [Q]  p  [Q] [Q] [Q]  Q   Q   Q   +   .   .  [Q] [Q]  Q   Q
2747          3T   [Qt] p  [Qt][Q] [Q]  Qt  Qt  Qt  t   +   t  [Qt][Qt] Qt  Qt
2748          3O   [Qt] p  [Qt][Q] [Q]  Qt  Qt  Qt  t   t   +  [Qt][Qt] Qt  Qt
2749          4A    lA  G   A   T   T   GA  GT  GT  GA  GT  GT  +   BA  G  GBA
2750          4O    lA GBA  A   T   T   GA  GT  GT  GA  GT  GT  BA  +  GBA  G
2751          6A    CA  PA  CA  C   C   A   T  tT   PA  P   P   C  CBA  +   BA
2752          6O    CA PBA  CA  C   C   A  tT   T   PA  P   P  CBA  C   BA  +
2753
2754       Within the matrix,
2755            "+" identifies entries where 'from' and 'to' are the same.
2756            "-" means the transformation is not supported.
2757            "." means nothing is necessary (a tRNS chunk can just be ignored).
2758            "t" means the transformation is obtained by png_set_tRNS.
2759            "A" means the transformation is obtained by png_set_add_alpha().
2760            "X" means the transformation is obtained by png_set_expand().
2761            "1" means the transformation is obtained by
2762                png_set_expand_gray_1_2_4_to_8() (and by png_set_expand()
2763                if there is no transparency in the original or the final
2764                format).
2765            "C" means the transformation is obtained by png_set_gray_to_rgb().
2766            "G" means the transformation is obtained by png_set_rgb_to_gray().
2767            "P" means the transformation is obtained by
2768                png_set_expand_palette_to_rgb().
2769            "p" means the transformation is obtained by png_set_packing().
2770            "Q" means the transformation is obtained by png_set_quantize().
2771            "T" means the transformation is obtained by
2772                png_set_tRNS_to_alpha().
2773            "B" means the transformation is obtained by
2774                png_set_background(), or png_strip_alpha().
2775
2776       When an entry has multiple transforms listed all are required to  cause
2777       the right overall transformation.  When two transforms are separated by
2778       a comma either will do the job.  When transforms are enclosed in [] the
2779       transform  should  do  the  job but this is currently unimplemented - a
2780       different format will result if the suggested transformations are used.
2781
2782       In PNG files, the alpha channel in an image is the  level  of  opacity.
2783       If  you  need  the  alpha channel in an image to be the level of trans‐
2784       parency instead of opacity, you can invert the alpha  channel  (or  the
2785       tRNS chunk data) after it's read, so that 0 is fully opaque and 255 (in
2786       8-bit or paletted images) or 65535 (in 16-bit images) is  fully  trans‐
2787       parent, with
2788
2789           png_set_invert_alpha(png_ptr);
2790
2791       PNG  files pack pixels of bit depths 1, 2, and 4 into bytes as small as
2792       they can, resulting in, for example, 8 pixels per byte for 1 bit files.
2793       This  code  expands  to 1 pixel per byte without changing the values of
2794       the pixels:
2795
2796           if (bit_depth < 8)
2797              png_set_packing(png_ptr);
2798
2799       PNG files have possible bit depths of 1, 2, 4, 8, and 16.   All  pixels
2800       stored  in  a  PNG image have been "scaled" or "shifted" up to the next
2801       higher possible bit depth (e.g. from 5 bits/sample in the range  [0,31]
2802       to  8 bits/sample in the range [0, 255]).  However, it is also possible
2803       to convert the PNG pixel data back to the original  bit  depth  of  the
2804       image.   This  call  reduces  the  pixels back down to the original bit
2805       depth:
2806
2807           png_color_8p sig_bit;
2808
2809           if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
2810              png_set_shift(png_ptr, sig_bit);
2811
2812       PNG files store 3-color pixels in red, green, blue  order.   This  code
2813       changes the storage of the pixels to blue, green, red:
2814
2815           if (color_type == PNG_COLOR_TYPE_RGB ||
2816               color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2817              png_set_bgr(png_ptr);
2818
2819       PNG  files store RGB pixels packed into 3 or 6 bytes. This code expands
2820       them into 4 or 8 bytes for windowing systems that  need  them  in  this
2821       format:
2822
2823           if (color_type == PNG_COLOR_TYPE_RGB)
2824              png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE);
2825
2826       where "filler" is the 8 or 16-bit number to fill with, and the location
2827       is either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
2828       you  want the filler before the RGB or after.  This transformation does
2829       not affect images that already have full alpha  channels.   To  add  an
2830       opaque  alpha  channel,  use filler=0xff or 0xffff and PNG_FILLER_AFTER
2831       which will generate RGBA pixels.
2832
2833       Note that png_set_filler() does not change the color type.  If you want
2834       to do that, you can add a true alpha channel with
2835
2836           if (color_type == PNG_COLOR_TYPE_RGB ||
2837              color_type == PNG_COLOR_TYPE_GRAY)
2838              png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
2839
2840       where  "filler" contains the alpha value to assign to each pixel.  This
2841       function was added in libpng-1.2.7.
2842
2843       If you are reading an image with an alpha channel,  and  you  need  the
2844       data as ARGB instead of the normal PNG format RGBA:
2845
2846           if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2847              png_set_swap_alpha(png_ptr);
2848
2849       For some uses, you may want a grayscale image to be represented as RGB.
2850       This code will do that conversion:
2851
2852           if (color_type == PNG_COLOR_TYPE_GRAY ||
2853               color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2854              png_set_gray_to_rgb(png_ptr);
2855
2856       Conversely, you can convert an  RGB  or  RGBA  image  to  grayscale  or
2857       grayscale with alpha.
2858
2859           if (color_type == PNG_COLOR_TYPE_RGB ||
2860               color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2861              png_set_rgb_to_gray(png_ptr, error_action,
2862                 double red_weight, double green_weight);
2863
2864           error_action = 1: silently do the conversion
2865
2866           error_action = 2: issue a warning if the original
2867                             image has any pixel where
2868                             red != green or red != blue
2869
2870           error_action = 3: issue an error and abort the
2871                             conversion if the original
2872                             image has any pixel where
2873                             red != green or red != blue
2874
2875           red_weight:       weight of red component
2876
2877           green_weight:     weight of green component
2878                             If either weight is negative, default
2879                             weights are used.
2880
2881       In  the  corresponding  fixed point API the red_weight and green_weight
2882       values are simply scaled by 100,000:
2883
2884           png_set_rgb_to_gray(png_ptr, error_action,
2885              png_fixed_point red_weight,
2886              png_fixed_point green_weight);
2887
2888       If you have set error_action = 1 or 2, you can later check whether  the
2889       image  really  was  gray,  after  processing  the  image rows, with the
2890       png_get_rgb_to_gray_status(png_ptr)  function.   It   will   return   a
2891       png_byte that is zero if the image was gray or 1 if there were any non-
2892       gray pixels.  Background and sBIT data will be  silently  converted  to
2893       grayscale,  using  the  green  channel data for sBIT, regardless of the
2894       error_action setting.
2895
2896       The default values come from the PNG file cHRM chunk if present; other‐
2897       wise, the defaults correspond to the ITU-R recommendation 709, and also
2898       the sRGB color space, as recommended in the  Charles  Poynton's  Colour
2899       FAQ, <http://www.poynton.com/>, in section 9:
2900
2901          <http://www.poynton.com/notes/colour_and_gamma/Color
2902       FAQ.html#RTFToC9>
2903
2904           Y = 0.2126 * R + 0.7152 * G + 0.0722 * B
2905
2906       Previous versions of this document, 1998 through  2002,  recommended  a
2907       slightly different formula:
2908
2909           Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2910
2911       Libpng uses an integer approximation:
2912
2913           Y = (6968 * R + 23434 * G + 2366 * B)/32768
2914
2915       The  calculation is done in a linear colorspace, if the image gamma can
2916       be determined.
2917
2918       The png_set_background() function has been described already; it  tells
2919       libpng  to  composite  images with alpha or simple transparency against
2920       the supplied background color.   For  compatibility  with  versions  of
2921       libpng  earlier  than  libpng-1.5.4 it is recommended that you call the
2922       function after reading the file header, even if you don't want  to  use
2923       the color in a bKGD chunk, if one exists.
2924
2925       If  the  PNG  file contains a bKGD chunk (PNG_INFO_bKGD valid), you may
2926       use this color, or supply another color more suitable for  the  current
2927       display (e.g., the background color from a web page).  You need to tell
2928       libpng how the color is represented, both the format of  the  component
2929       values  in the color (the number of bits) and the gamma encoding of the
2930       color.  The function takes  two  arguments,  background_gamma_mode  and
2931       need_expand  to convey this information; however, only two combinations
2932       are likely to be useful:
2933
2934           png_color_16 my_background;
2935           png_color_16p image_background;
2936
2937           if (png_get_bKGD(png_ptr, info_ptr, &image_background))
2938              png_set_background(png_ptr, image_background,
2939                  PNG_BACKGROUND_GAMMA_FILE, 1/*needs to be expanded*/, 1);
2940           else
2941              png_set_background(png_ptr, &my_background,
2942                  PNG_BACKGROUND_GAMMA_SCREEN, 0/*do not expand*/, 1);
2943
2944       The second call was described above - my_background is in the format of
2945       the  final,  display,  output produced by libpng.  Because you now know
2946       the format of the PNG it is possible to avoid the need to choose either
2947       8-bit or 16-bit output and to retain palette images (the palette colors
2948       will be modified appropriately and the tRNS chunk  removed.)   However,
2949       if  you  are doing this, take great care not to ask for transformations
2950       without checking first that they apply!
2951
2952       In the first call the background color has the original bit  depth  and
2953       color  type  of the PNG file.  So, for palette images the color is sup‐
2954       plied as a palette index and for low bit greyscale images the color  is
2955       a reduced bit value in image_background->gray.
2956
2957       If  you didn't call png_set_gamma() before reading the file header, for
2958       example if you need your code to remain compatible with older  versions
2959       of libpng prior to libpng-1.5.4, this is the place to call it.
2960
2961       Do not call it if you called png_set_alpha_mode(); doing so will damage
2962       the   settings   put   in   place   by    png_set_alpha_mode().     (If
2963       png_set_alpha_mode()   is   supported   then   you   can  certainly  do
2964       png_set_gamma() before reading the PNG header.)
2965
2966       This API unconditionally sets the screen and file gamma values,  so  it
2967       will  override the value in the PNG file unless it is called before the
2968       PNG file reading starts.  For this reason you must always call it  with
2969       the PNG file value when you call it in this position:
2970
2971          if (png_get_gAMA(png_ptr, info_ptr, &file_gamma))
2972             png_set_gamma(png_ptr, screen_gamma, file_gamma);
2973
2974          else
2975             png_set_gamma(png_ptr, screen_gamma, 0.45455);
2976
2977       If  you need to reduce an RGB file to a paletted file, or if a paletted
2978       file has more entries then will fit on your screen,  png_set_quantize()
2979       will  do  that.   Note  that  this  is a simple match quantization that
2980       merely finds the closest color available.  This should work fairly well
2981       with  optimized palettes, but fairly badly with linear color cubes.  If
2982       you pass a palette that is larger than maximum_colors,  the  file  will
2983       reduce  the  number  of colors in the palette so it will fit into maxi‐
2984       mum_colors.  If there is a histogram, libpng will use it to  make  more
2985       intelligent  choices  when  reducing  the palette.  If there is no his‐
2986       togram, it may not do as good a job.
2987
2988          if (color_type & PNG_COLOR_MASK_COLOR)
2989          {
2990             if (png_get_valid(png_ptr, info_ptr,
2991                 PNG_INFO_PLTE))
2992             {
2993                png_uint_16p histogram = NULL;
2994
2995                png_get_hIST(png_ptr, info_ptr,
2996                    &histogram);
2997                png_set_quantize(png_ptr, palette, num_palette,
2998                   max_screen_colors, histogram, 1);
2999             }
3000
3001             else
3002             {
3003                png_color std_color_cube[MAX_SCREEN_COLORS] =
3004                   { ... colors ... };
3005
3006                png_set_quantize(png_ptr, std_color_cube,
3007                   MAX_SCREEN_COLORS, MAX_SCREEN_COLORS,
3008                   NULL,0);
3009             }
3010          }
3011
3012       PNG files describe monochrome as black being zero and white being  one.
3013       The  following  code  will reverse this (make black be one and white be
3014       zero):
3015
3016          if (bit_depth == 1 && color_type == PNG_COLOR_TYPE_GRAY)
3017             png_set_invert_mono(png_ptr);
3018
3019       This function can also be  used  to  invert  grayscale  and  gray-alpha
3020       images:
3021
3022          if (color_type == PNG_COLOR_TYPE_GRAY ||
3023              color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3024             png_set_invert_mono(png_ptr);
3025
3026       PNG  files  store  16-bit pixels in network byte order (big-endian, ie.
3027       most significant bits first).  This code changes  the  storage  to  the
3028       other  way  (little-endian,  i.e. least significant bits first, the way
3029       PCs store them):
3030
3031           if (bit_depth == 16)
3032              png_set_swap(png_ptr);
3033
3034       If you are using packed-pixel images (1, 2, or 4 bits/pixel),  and  you
3035       need to change the order the pixels are packed into bytes, you can use:
3036
3037           if (bit_depth < 8)
3038              png_set_packswap(png_ptr);
3039
3040       Finally,  you can write your own transformation function if none of the
3041       existing ones meets your needs.  This is done  by  setting  a  callback
3042       with
3043
3044           png_set_read_user_transform_fn(png_ptr,
3045               read_transform_fn);
3046
3047       You must supply the function
3048
3049           void read_transform_fn(png_structp png_ptr, png_row_infop
3050               row_info, png_bytep data)
3051
3052       See  pngtest.c  for  a  working  example.  Your function will be called
3053       after all of the other transformations have been processed.  Take  care
3054       with  interlaced images if you do the interlace yourself - the width of
3055       the row is the width in 'row_info', not the overall image width.
3056
3057       If supported, libpng provides two information routines that you can use
3058       to find where you are in processing the image:
3059
3060          png_get_current_pass_number(png_structp png_ptr);
3061          png_get_current_row_number(png_structp png_ptr);
3062
3063       Don't  try  using these outside a transform callback - firstly they are
3064       only supported if user transforms are supported, secondly they may well
3065       return unexpected results unless the row is actually being processed at
3066       the moment they are called.
3067
3068       With interlaced images the value returned is the row in the input  sub-
3069       image     image.      Use    PNG_ROW_FROM_PASS_ROW(row,    pass)    and
3070       PNG_COL_FROM_PASS_COL(col, pass) to find the output pixel  (x,y)  given
3071       an interlaced sub-image pixel (row,col,pass).
3072
3073       The discussion of interlace handling above contains more information on
3074       how to use these values.
3075
3076       You can also set up a pointer to a user structure for use by your call‐
3077       back  function,  and you can inform libpng that your transform function
3078       will change the number of channels or bit depth with the function
3079
3080           png_set_user_transform_info(png_ptr, user_ptr,
3081               user_depth, user_channels);
3082
3083       The user's application, not libpng, is responsible for  allocating  and
3084       freeing any memory required for the user structure.
3085
3086       You  can  retrieve  the  pointer  via  the function png_get_user_trans‐
3087       form_ptr().  For example:
3088
3089           voidp read_user_transform_ptr =
3090               png_get_user_transform_ptr(png_ptr);
3091
3092       The last thing to handle is interlacing;  this  is  covered  in  detail
3093       below, but you must call the function here if you want libpng to handle
3094       expansion of the interlaced image.
3095
3096           number_of_passes = png_set_interlace_handling(png_ptr);
3097
3098       After setting the transformations,  libpng  can  update  your  png_info
3099       structure  to  reflect  any  transformations you've requested with this
3100       call.
3101
3102           png_read_update_info(png_ptr, info_ptr);
3103
3104       This is most useful to update the info structure's  rowbytes  field  so
3105       you  can use it to allocate your image memory.  This function will also
3106       update your palette with the correct  screen_gamma  and  background  if
3107       these  have  been  given  with  the  calls  above.   You  may only call
3108       png_read_update_info() once with a particular info_ptr.
3109
3110       After you call png_read_update_info(), you can allocate any memory  you
3111       need  to  hold the image.  The row data is simply raw byte data for all
3112       forms of images.  As the actual allocation varies  among  applications,
3113       no  example  will be given.  If you are allocating one large chunk, you
3114       will need to build an array of pointers to each  row,  as  it  will  be
3115       needed for some of the functions below.
3116
3117       Remember: Before you call png_read_update_info(), the png_get_*() func‐
3118       tions return the values corresponding to the original PNG image.  After
3119       you call png_read_update_info the values refer to the image that libpng
3120       will output.  Consequently you must call  all  the  png_set_  functions
3121       before you call png_read_update_info().  This is particularly important
3122       for  png_set_interlace_handling()  -  if  you   are   going   to   call
3123       png_read_update_info()   you   must  call  png_set_interlace_handling()
3124       before it unless you want to receive interlaced output.
3125
3126
3127   Reading image data
3128       After you've allocated memory, you can read the image data.   The  sim‐
3129       plest  way  to  do this is in one function call.  If you are allocating
3130       enough  memory  to  hold  the  whole   image,   you   can   just   call
3131       png_read_image()  and libpng will read in all the image data and put it
3132       in the memory area supplied.  You will need to  pass  in  an  array  of
3133       pointers to each row.
3134
3135       This  function  automatically handles interlacing, so you don't need to
3136       call      png_set_interlace_handling()      (unless      you       call
3137       png_read_update_info()) or call this function multiple times, or any of
3138       that other stuff necessary with png_read_rows().
3139
3140          png_read_image(png_ptr, row_pointers);
3141
3142       where row_pointers is:
3143
3144          png_bytep row_pointers[height];
3145
3146       You can point to void or char or whatever you use for pixels.
3147
3148       If you don't want to read in the whole  image  at  once,  you  can  use
3149       png_read_rows()  instead.   If  there  is  no interlacing (check inter‐
3150       lace_type == PNG_INTERLACE_NONE), this is simple:
3151
3152           png_read_rows(png_ptr, row_pointers, NULL,
3153               number_of_rows);
3154
3155       where row_pointers is the same as in the png_read_image() call.
3156
3157       If you are doing this just one row at a time, you can do  this  with  a
3158       single row_pointer instead of an array of row_pointers:
3159
3160           png_bytep row_pointer = row;
3161           png_read_row(png_ptr, row_pointer, NULL);
3162
3163       If  the  file  is  interlaced  (interlace_type != 0 in the IHDR chunk),
3164       things get somewhat harder.  The only current (PNG  Specification  ver‐
3165       sion  1.2)  interlacing  type  for PNG is (interlace_type == PNG_INTER‐
3166       LACE_ADAM7); a somewhat  complicated  2D  interlace  scheme,  known  as
3167       Adam7,  that  breaks down an image into seven smaller images of varying
3168       size, based on an 8x8 grid.  This number is defined (from  libpng  1.5)
3169       as PNG_INTERLACE_ADAM7_PASSES in png.h
3170
3171       libpng  can  fill  out those images or it can give them to you "as is".
3172       It is almost always better to have libpng handle  the  interlacing  for
3173       you.  If you want the images filled out, there are two ways to do that.
3174       The one mentioned in the PNG specification is to expand each  pixel  to
3175       cover  those  pixels  that  have  not  been  read  yet (the "rectangle"
3176       method).  This results in a blocky image  for  the  first  pass,  which
3177       gradually smooths out as more pixels are read.  The other method is the
3178       "sparkle" method, where pixels are drawn only in their final locations,
3179       with the rest of the image remaining whatever colors they were initial‐
3180       ized to before the start of the read.  The first method  usually  looks
3181       better,  but tends to be slower, as there are more pixels to put in the
3182       rows.
3183
3184       If, as is likely, you want libpng  to  expand  the  images,  call  this
3185       before calling png_start_read_image() or png_read_update_info():
3186
3187           if (interlace_type == PNG_INTERLACE_ADAM7)
3188              number_of_passes
3189                  = png_set_interlace_handling(png_ptr);
3190
3191       This  will  return  the  number  of  passes needed.  Currently, this is
3192       seven, but may change if another interlace type is added.   This  func‐
3193       tion  can  be  called even if the file is not interlaced, where it will
3194       return one  pass.   You  then  need  to  read  the  whole  image  'num‐
3195       ber_of_passes'  times.   Each  time will distribute the pixels from the
3196       current pass to the correct place in the output image, so you  need  to
3197       supply the same rows to png_read_rows in each pass.
3198
3199       If  you  are  not  going  to display the image after each pass, but are
3200       going to wait until the entire  image  is  read  in,  use  the  sparkle
3201       effect.   This  effect is faster and the end result of either method is
3202       exactly the same.  If you are planning on displaying  the  image  after
3203       each  pass,  the  "rectangle" effect is generally considered the better
3204       looking one.
3205
3206       If you only want the "sparkle" effect,  just  call  png_read_rows()  as
3207       normal,  with  the  third parameter NULL.  Make sure you make pass over
3208       the image number_of_passes times, and you don't change the data in  the
3209       rows between calls.  You can change the locations of the data, just not
3210       the data.  Each pass only writes the pixels appropriate for that  pass,
3211       and assumes the data from previous passes is still valid.
3212
3213           png_read_rows(png_ptr, row_pointers, NULL,
3214               number_of_rows);
3215
3216       If  you  only  want  the  first effect (the rectangles), do the same as
3217       before except pass the row buffer in the third parameter, and leave the
3218       second parameter NULL.
3219
3220           png_read_rows(png_ptr, NULL, row_pointers,
3221               number_of_rows);
3222
3223       If  you  don't want libpng to handle the interlacing details, just call
3224       png_read_rows() PNG_INTERLACE_ADAM7_PASSES times to  read  in  all  the
3225       images.   Each  of  the images is a valid image by itself; however, you
3226       will almost certainly need to distribute the pixels from each sub-image
3227       to the correct place.  This is where everything gets very tricky.
3228
3229       If  you  want to retrieve the separate images you must pass the correct
3230       number of rows to each successive call of png_read_rows().  The  calcu‐
3231       lation  gets pretty complicated for small images, where some sub-images
3232       may not even exist because either their width or height ends  up  zero.
3233       libpng provides two macros to help you in 1.5 and later versions:
3234
3235          png_uint_32 width = PNG_PASS_COLS(image_width, pass_number);
3236          png_uint_32 height = PNG_PASS_ROWS(image_height, pass_number);
3237
3238       Respectively  these tell you the width and height of the sub-image cor‐
3239       responding to the numbered pass.  'pass' is in in the range 0  to  6  -
3240       this  can  be  confusing  because  the specification refers to the same
3241       passes as 1 to 7!  Be careful, you must check both the width and height
3242       before  calling png_read_rows() and not call it for that pass if either
3243       is zero.
3244
3245       You can, of course, read each sub-image row by row.   If  you  want  to
3246       produce  optimal  code  to  make  a pixel-by-pixel transformation of an
3247       interlaced image this is the best approach; read each row of each pass,
3248       transform it, and write it out to a new interlaced image.
3249
3250       If  you want to de-interlace the image yourself libpng provides further
3251       macros to help that tell you where to place the pixels  in  the  output
3252       image.   Because the interlacing scheme is rectangular - sub-image pix‐
3253       els are always arranged on a rectangular grid - all you  need  to  know
3254       for each pass is the starting column and row in the output image of the
3255       first pixel plus the spacing between each  pixel.   As  of  libpng  1.5
3256       there are four macros to retrieve this information:
3257
3258          png_uint_32 x = PNG_PASS_START_COL(pass);
3259          png_uint_32 y = PNG_PASS_START_ROW(pass);
3260          png_uint_32 xStep = 1U << PNG_PASS_COL_SHIFT(pass);
3261          png_uint_32 yStep = 1U << PNG_PASS_ROW_SHIFT(pass);
3262
3263       These allow you to write the obvious loop:
3264
3265          png_uint_32 input_y = 0;
3266          png_uint_32 output_y = PNG_PASS_START_ROW(pass);
3267
3268          while (output_y < output_image_height)
3269          {
3270             png_uint_32 input_x = 0;
3271             png_uint_32 output_x = PNG_PASS_START_COL(pass);
3272
3273             while (output_x < output_image_width)
3274             {
3275                image[output_y][output_x] =
3276                    subimage[pass][input_y][input_x++];
3277
3278                output_x += xStep;
3279             }
3280
3281             ++input_y;
3282             output_y += yStep;
3283          }
3284
3285       Notice  that  the  steps between successive output rows and columns are
3286       returned as shifts.  This is possible because the pixels in the  subim‐
3287       ages are always a power of 2 apart - 1, 2, 4 or 8 pixels - in the orig‐
3288       inal image.  In practice you may need to directly calculate the  output
3289       coordinate  given  an  input  coordinate.   libpng provides two further
3290       macros for this purpose:
3291
3292          png_uint_32 output_x = PNG_COL_FROM_PASS_COL(input_x, pass);
3293          png_uint_32 output_y = PNG_ROW_FROM_PASS_ROW(input_y, pass);
3294
3295       Finally a pair of macros are provided to tell you if a particular image
3296       row or column appears in a given pass:
3297
3298          int col_in_pass = PNG_COL_IN_INTERLACE_PASS(output_x, pass);
3299          int row_in_pass = PNG_ROW_IN_INTERLACE_PASS(output_y, pass);
3300
3301       Bear  in  mind  that you will probably also need to check the width and
3302       height of the pass in addition to the above to be sure  the  pass  even
3303       exists!
3304
3305       With  any  luck you are convinced by now that you don't want to do your
3306       own interlace handling.  In reality normally the only good  reason  for
3307       doing this is if you are processing PNG files on a pixel-by-pixel basis
3308       and don't want to load the whole file into memory  when  it  is  inter‐
3309       laced.
3310
3311       libpng  includes a test program, pngvalid, that illustrates reading and
3312       writing of interlaced images.  If you can't get interlacing to work  in
3313       your  code  and  don't  want  to  leave  it  to libpng (the recommended
3314       approach), see how pngvalid.c does it.
3315
3316
3317   Finishing a sequential read
3318       After you are finished reading the image through the  low-level  inter‐
3319       face,  you  can finish reading the file.  If you are interested in com‐
3320       ments or time, which may be stored either before  or  after  the  image
3321       data,  you should pass the separate png_info struct if you want to keep
3322       the comments from before and after the image separate.
3323
3324           png_infop end_info = png_create_info_struct(png_ptr);
3325
3326           if (!end_info)
3327           {
3328              png_destroy_read_struct(&png_ptr, &info_ptr,
3329                  (png_infopp)NULL);
3330              return (ERROR);
3331           }
3332
3333          png_read_end(png_ptr, end_info);
3334
3335       If you are not interested, you should still call png_read_end() but you
3336       can pass NULL, avoiding the need to create an end_info structure.
3337
3338          png_read_end(png_ptr, (png_infop)NULL);
3339
3340       If  you  don't call png_read_end(), then your file pointer will be left
3341       pointing to the first chunk after the last IDAT, which is probably  not
3342       what you want if you expect to read something beyond the end of the PNG
3343       datastream.
3344
3345       When you are done, you can free all memory  allocated  by  libpng  like
3346       this:
3347
3348          png_destroy_read_struct(&png_ptr, &info_ptr,
3349              &end_info);
3350
3351       or, if you didn't create an end_info structure,
3352
3353          png_destroy_read_struct(&png_ptr, &info_ptr,
3354              (png_infopp)NULL);
3355
3356       It  is  also  possible  to  individually free the info_ptr members that
3357       point to libpng-allocated storage with the following function:
3358
3359           png_free_data(png_ptr, info_ptr, mask, seq)
3360
3361           mask - identifies data to be freed, a mask
3362                  containing the bitwise OR of one or
3363                  more of
3364                    PNG_FREE_PLTE, PNG_FREE_TRNS,
3365                    PNG_FREE_HIST, PNG_FREE_ICCP,
3366                    PNG_FREE_PCAL, PNG_FREE_ROWS,
3367                    PNG_FREE_SCAL, PNG_FREE_SPLT,
3368                    PNG_FREE_TEXT, PNG_FREE_UNKN,
3369                  or simply PNG_FREE_ALL
3370
3371           seq  - sequence number of item to be freed
3372                  (-1 for all items)
3373
3374       This function may be  safely  called  when  the  relevant  storage  has
3375       already  been freed, or has not yet been allocated, or was allocated by
3376       the user and not by libpng,  and will in those cases do  nothing.   The
3377       "seq"  parameter is ignored if only one item of the selected data type,
3378       such as PLTE, is allowed.  If "seq" is not -1, and multiple  items  are
3379       allowed for the data type identified in the mask, such as text or sPLT,
3380       only the n'th item in the structure is freed, where n is "seq".
3381
3382       The default behavior is only to free data that was allocated internally
3383       by libpng.  This can be changed, so that libpng will not free the data,
3384       or so that it will free data  that  was  allocated  by  the  user  with
3385       png_malloc()  or png_calloc() and passed in via a png_set_*() function,
3386       with
3387
3388           png_data_freer(png_ptr, info_ptr, freer, mask)
3389
3390           freer  - one of
3391                      PNG_DESTROY_WILL_FREE_DATA
3392                      PNG_SET_WILL_FREE_DATA
3393                      PNG_USER_WILL_FREE_DATA
3394
3395           mask   - which data elements are affected
3396                    same choices as in png_free_data()
3397
3398       This function only affects data that has already been  allocated.   You
3399       can  call  this  function after reading the PNG data but before calling
3400       any  png_set_*()  functions,  to  control  whether  the  user  or   the
3401       png_set_*()  function is responsible for freeing any existing data that
3402       might be present, and again after the png_set_*() functions to  control
3403       whether the user or png_destroy_*() is supposed to free the data.  When
3404       the user assumes responsibility for libpng-allocated data, the applica‐
3405       tion  must  use  png_free()  to  free  it,  and when the user transfers
3406       responsibility to libpng for data that the user has allocated, the user
3407       must have used png_malloc() or png_calloc() to allocate it.
3408
3409       If  you  allocated  your  row_pointers  in a single block, as suggested
3410       above in the description of the high level read interface, you must not
3411       transfer   responsibility   for  freeing  it  to  the  png_set_rows  or
3412       png_read_destroy function, because they would  also  try  to  free  the
3413       individual row_pointers[i].
3414
3415       If  you  allocated  text_ptr.text,  text_ptr.lang,  and text_ptr.trans‐
3416       lated_keyword separately, do not transfer  responsibility  for  freeing
3417       text_ptr  to  libpng, because when libpng fills a png_text structure it
3418       combines these members with the key member,  and  png_free_data()  will
3419       free  only text_ptr.key.  Similarly, if you transfer responsibility for
3420       free'ing text_ptr from libpng to  your  application,  your  application
3421       must not separately free those members.
3422
3423       The  png_free_data()  function  will turn off the "valid" flag for any‐
3424       thing it frees.  If you need to turn the flag off for a chunk that  was
3425       freed by your application instead of by libpng, you can use
3426
3427           png_set_invalid(png_ptr, info_ptr, mask);
3428
3429           mask - identifies the chunks to be made invalid,
3430                  containing the bitwise OR of one or
3431                  more of
3432                    PNG_INFO_gAMA, PNG_INFO_sBIT,
3433                    PNG_INFO_cHRM, PNG_INFO_PLTE,
3434                    PNG_INFO_tRNS, PNG_INFO_bKGD,
3435                    PNG_INFO_hIST, PNG_INFO_pHYs,
3436                    PNG_INFO_oFFs, PNG_INFO_tIME,
3437                    PNG_INFO_pCAL, PNG_INFO_sRGB,
3438                    PNG_INFO_iCCP, PNG_INFO_sPLT,
3439                    PNG_INFO_sCAL, PNG_INFO_IDAT
3440
3441       For  a  more compact example of reading a PNG image, see the file exam‐
3442       ple.c.
3443
3444
3445   Reading PNG files progressively
3446       The progressive reader is slightly different then  the  non-progressive
3447       reader.   Instead  of  calling  png_read_info(),  png_read_rows(),  and
3448       png_read_end(), you make one call to  png_process_data(),  which  calls
3449       callbacks  when  it  has the info, a row, or the end of the image.  You
3450       set up these callbacks with png_set_progressive_read_fn().   You  don't
3451       have  to  worry  about the input/output functions of libpng, as you are
3452       giving the library the data directly  in  png_process_data().   I  will
3453       assume  that you have read the section on reading PNG files above, so I
3454       will only highlight the differences (although I will show  all  of  the
3455       code).
3456
3457       png_structp png_ptr; png_infop info_ptr;
3458
3459        /*  An example code fragment of how you would
3460            initialize the progressive reader in your
3461            application. */
3462        int
3463        initialize_png_reader()
3464        {
3465           png_ptr = png_create_read_struct
3466               (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
3467                user_error_fn, user_warning_fn);
3468
3469           if (!png_ptr)
3470               return (ERROR);
3471
3472           info_ptr = png_create_info_struct(png_ptr);
3473
3474           if (!info_ptr)
3475           {
3476              png_destroy_read_struct(&png_ptr,
3477                 (png_infopp)NULL, (png_infopp)NULL);
3478              return (ERROR);
3479           }
3480
3481           if (setjmp(png_jmpbuf(png_ptr)))
3482           {
3483              png_destroy_read_struct(&png_ptr, &info_ptr,
3484                 (png_infopp)NULL);
3485              return (ERROR);
3486           }
3487
3488           /* This one's new.  You can provide functions
3489              to be called when the header info is valid,
3490              when each row is completed, and when the image
3491              is finished.  If you aren't using all functions,
3492              you can specify NULL parameters.  Even when all
3493              three functions are NULL, you need to call
3494              png_set_progressive_read_fn().  You can use
3495              any struct as the user_ptr (cast to a void pointer
3496              for the function call), and retrieve the pointer
3497              from inside the callbacks using the function
3498
3499                 png_get_progressive_ptr(png_ptr);
3500
3501              which will return a void pointer, which you have
3502              to cast appropriately.
3503            */
3504           png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
3505               info_callback, row_callback, end_callback);
3506
3507           return 0;
3508        }
3509
3510        /* A code fragment that you call as you receive blocks
3511          of data */
3512        int
3513        process_data(png_bytep buffer, png_uint_32 length)
3514        {
3515           if (setjmp(png_jmpbuf(png_ptr)))
3516           {
3517              png_destroy_read_struct(&png_ptr, &info_ptr,
3518                  (png_infopp)NULL);
3519              return (ERROR);
3520           }
3521
3522           /* This one's new also.  Simply give it a chunk
3523              of data from the file stream (in order, of
3524              course).  On machines with segmented memory
3525              models machines, don't give it any more than
3526              64K.  The library seems to run fine with sizes
3527              of 4K. Although you can give it much less if
3528              necessary (I assume you can give it chunks of
3529              1 byte, I haven't tried less then 256 bytes
3530              yet).  When this function returns, you may
3531              want to display any rows that were generated
3532              in the row callback if you don't already do
3533              so there.
3534            */
3535           png_process_data(png_ptr, info_ptr, buffer, length);
3536
3537           /* At this point you can call png_process_data_skip if
3538              you want to handle data the library will skip yourself;
3539              it simply returns the number of bytes to skip (and stops
3540              libpng skipping that number of bytes on the next
3541              png_process_data call).
3542           return 0;
3543        }
3544
3545        /* This function is called (as set by
3546           png_set_progressive_read_fn() above) when enough data
3547           has been supplied so all of the header has been
3548           read.
3549        */
3550        void
3551        info_callback(png_structp png_ptr, png_infop info)
3552        {
3553           /* Do any setup here, including setting any of
3554              the transformations mentioned in the Reading
3555              PNG files section.  For now, you _must_ call
3556              either png_start_read_image() or
3557              png_read_update_info() after all the
3558              transformations are set (even if you don't set
3559              any).  You may start getting rows before
3560              png_process_data() returns, so this is your
3561              last chance to prepare for that.
3562
3563              This is where you turn on interlace handling,
3564              assuming you don't want to do it yourself.
3565
3566              If you need to you can stop the processing of
3567              your original input data at this point by calling
3568              png_process_data_pause.  This returns the number
3569              of unprocessed bytes from the last png_process_data
3570              call - it is up to you to ensure that the next call
3571              sees these bytes again.  If you don't want to bother
3572              with this you can get libpng to cache the unread
3573              bytes by setting the 'save' parameter (see png.h) but
3574              then libpng will have to copy the data internally.
3575            */
3576        }
3577
3578        /* This function is called when each row of image
3579           data is complete */
3580        void
3581        row_callback(png_structp png_ptr, png_bytep new_row,
3582           png_uint_32 row_num, int pass)
3583        {
3584           /* If the image is interlaced, and you turned
3585              on the interlace handler, this function will
3586              be called for every row in every pass.  Some
3587              of these rows will not be changed from the
3588              previous pass.  When the row is not changed,
3589              the new_row variable will be NULL.  The rows
3590              and passes are called in order, so you don't
3591              really need the row_num and pass, but I'm
3592              supplying them because it may make your life
3593              easier.
3594
3595              If you did not turn on interlace handling then
3596              the callback is called for each row of each
3597              sub-image when the image is interlaced.  In this
3598              case 'row_num' is the row in the sub-image, not
3599              the row in the output image as it is in all other
3600              cases.
3601
3602              For the non-NULL rows of interlaced images when
3603              you have switched on libpng interlace handling,
3604              you must call png_progressive_combine_row()
3605              passing in the row and the old row.  You can
3606              call this function for NULL rows (it will just
3607              return) and for non-interlaced images (it just
3608              does the memcpy for you) if it will make the
3609              code easier.  Thus, you can just do this for
3610              all cases if you switch on interlace handling;
3611            */
3612
3613               png_progressive_combine_row(png_ptr, old_row,
3614                 new_row);
3615
3616           /* where old_row is what was displayed for
3617              previously for the row.  Note that the first
3618              pass (pass == 0, really) will completely cover
3619              the old row, so the rows do not have to be
3620              initialized.  After the first pass (and only
3621              for interlaced images), you will have to pass
3622              the current row, and the function will combine
3623              the old row and the new row.
3624
3625              You can also call png_process_data_pause in this
3626              callback - see above.
3627           */
3628        }
3629
3630        void
3631        end_callback(png_structp png_ptr, png_infop info)
3632        {
3633           /* This function is called after the whole image
3634              has been read, including any chunks after the
3635              image (up to and including the IEND).  You
3636              will usually have the same info chunk as you
3637              had in the header, although some data may have
3638              been added to the comments and time fields.
3639
3640              Most people won't do much here, perhaps setting
3641              a flag that marks the image as finished.
3642            */
3643        }
3644
3645
3646
3647

IV. Writing

3649       Much of this is very similar to reading.  However, everything of impor‐
3650       tance is repeated here, so you won't have to constantly look back up in
3651       the reading section to understand writing.
3652
3653
3654   Setup
3655       You  will want to do the I/O initialization before you get into libpng,
3656       so if it doesn't work, you don't have anything to undo. If you are  not
3657       using  the  standard  I/O functions, you will need to replace them with
3658       custom writing functions.  See the discussion under Customizing libpng.
3659
3660           FILE *fp = fopen(file_name, "wb");
3661
3662           if (!fp)
3663              return (ERROR);
3664
3665       Next, png_struct and png_info need to be allocated and initialized.  As
3666       these  can be both relatively large, you may not want to store these on
3667       the stack, unless you have stack space to spare.  Of course,  you  will
3668       want  to check if they return NULL.  If you are also reading, you won't
3669       want to  name  your  read  structure  and  your  write  structure  both
3670       "png_ptr";  you can call them anything you like, such as "read_ptr" and
3671       "write_ptr".  Look at pngtest.c, for example.
3672
3673           png_structp png_ptr = png_create_write_struct
3674              (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
3675               user_error_fn, user_warning_fn);
3676
3677           if (!png_ptr)
3678              return (ERROR);
3679
3680           png_infop info_ptr = png_create_info_struct(png_ptr);
3681           if (!info_ptr)
3682           {
3683              png_destroy_write_struct(&png_ptr,
3684                  (png_infopp)NULL);
3685              return (ERROR);
3686           }
3687
3688       If you  want  to  use  your  own  memory  allocation  routines,  define
3689       PNG_USER_MEM_SUPPORTED  and  use png_create_write_struct_2() instead of
3690       png_create_write_struct():
3691
3692           png_structp png_ptr = png_create_write_struct_2
3693              (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
3694               user_error_fn, user_warning_fn, (png_voidp)
3695               user_mem_ptr, user_malloc_fn, user_free_fn);
3696
3697       After you have these structures, you will need to set up the error han‐
3698       dling.   When  libpng encounters an error, it expects to longjmp() back
3699       to your routine.  Therefore, you will need to call  setjmp()  and  pass
3700       the  png_jmpbuf(png_ptr).   If  you  write the file from different rou‐
3701       tines, you will need to update the png_jmpbuf(png_ptr) every  time  you
3702       enter  a new routine that will call a png_*() function.  See your docu‐
3703       mentation of setjmp/longjmp for your compiler for more  information  on
3704       setjmp/longjmp.   See  the  discussion  on libpng error handling in the
3705       Customizing Libpng section below for more  information  on  the  libpng
3706       error handling.
3707
3708           if (setjmp(png_jmpbuf(png_ptr)))
3709           {
3710           png_destroy_write_struct(&png_ptr, &info_ptr);
3711              fclose(fp);
3712              return (ERROR);
3713           }
3714           ...
3715           return;
3716
3717       If  you would rather avoid the complexity of setjmp/longjmp issues, you
3718       can compile libpng with PNG_NO_SETJMP, in which case errors will result
3719       in a call to PNG_ABORT() which defaults to abort().
3720
3721       You can #define PNG_ABORT() to a function that does something more use‐
3722       ful than abort(), as long as your function does not return.
3723
3724       Now you need to set up the output code.  The default for libpng  is  to
3725       use  the C function fwrite().  If you use this, you will need to pass a
3726       valid FILE * in the function png_init_io().  Be sure that the  file  is
3727       opened  in  binary  mode.  Again, if you wish to handle writing data in
3728       another way, see the discussion on libpng I/O handling in the Customiz‐
3729       ing Libpng section below.
3730
3731           png_init_io(png_ptr, fp);
3732
3733       If  you are embedding your PNG into a datastream such as MNG, and don't
3734       want libpng to write the 8-byte signature, or if you have already writ‐
3735       ten the signature in your application, use
3736
3737           png_set_sig_bytes(png_ptr, 8);
3738
3739       to inform libpng that it should not write a signature.
3740
3741
3742   Write callbacks
3743       At  this  point, you can set up a callback function that will be called
3744       after each row has been  written,  which  you  can  use  to  control  a
3745       progress  meter or the like.  It's demonstrated in pngtest.c.  You must
3746       supply a function
3747
3748           void write_row_callback(png_structp png_ptr, png_uint_32 row,
3749              int pass);
3750           {
3751             /* put your code here */
3752           }
3753
3754       (You can give it another name that you like instead of "write_row_call‐
3755       back")
3756
3757       To inform libpng about your function, use
3758
3759           png_set_write_status_fn(png_ptr, write_row_callback);
3760
3761       When  this  function is called the row has already been completely pro‐
3762       cessed and it has also been written out.  The 'row' and 'pass' refer to
3763       the  next  row to be handled.  For the non-interlaced case the row that
3764       was just handled is simply one less than the passed in row number,  and
3765       pass will always be 0.  For the interlaced case the same applies unless
3766       the row value is 0, in which case the row just handled was the last one
3767       from  one of the preceding passes.  Because interlacing may skip a pass
3768       you cannot be sure that the preceding pass is  just  'pass-1',  if  you
3769       really  need  to  know what the last pass is record (row,pass) from the
3770       callback and use the last recorded value each time.
3771
3772       As with the user transform you  can  find  the  output  row  using  the
3773       PNG_ROW_FROM_PASS_ROW macro.
3774
3775       You  now  have the option of modifying how the compression library will
3776       run.  The following functions are mainly for testing, but may be useful
3777       in  some  cases, like if you need to write PNG files extremely fast and
3778       are willing to give up some compression, or if you want to get the max‐
3779       imum  possible  compression  at  the expense of slower writing.  If you
3780       have no special needs in this area, let the library do what it wants by
3781       not  calling  this  function  at all, as it has been tuned to deliver a
3782       good speed/compression ratio. The second parameter to  png_set_filter()
3783       is  the filter method, for which the only valid values are 0 (as of the
3784       July 1999 PNG specification, version 1.2) or 64 (if you are  writing  a
3785       PNG  datastream that is to be embedded in a MNG datastream).  The third
3786       parameter is a flag that indicates  which  filter  type(s)  are  to  be
3787       tested for each scanline.  See the PNG specification for details on the
3788       specific filter types.
3789
3790
3791           /* turn on or off filtering, and/or choose
3792              specific filters.  You can use either a single
3793              PNG_FILTER_VALUE_NAME or the bitwise OR of one
3794              or more PNG_FILTER_NAME masks.
3795            */
3796           png_set_filter(png_ptr, 0,
3797              PNG_FILTER_NONE  | PNG_FILTER_VALUE_NONE |
3798              PNG_FILTER_SUB   | PNG_FILTER_VALUE_SUB  |
3799              PNG_FILTER_UP    | PNG_FILTER_VALUE_UP   |
3800              PNG_FILTER_AVG   | PNG_FILTER_VALUE_AVG  |
3801              PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH|
3802              PNG_ALL_FILTERS);
3803
3804       If an application wants to start and stop using particular filters dur‐
3805       ing compression, it should start out with all of the filters (to ensure
3806       that the previous row of pixels will be  stored  in  case  it's  needed
3807       later), and then add and remove them after the start of compression.
3808
3809       If  you  are  writing  a PNG datastream that is to be embedded in a MNG
3810       datastream, the second parameter can be either 0 or 64.
3811
3812       The png_set_compression_*() functions interface to the zlib compression
3813       library,  and  should mostly be ignored unless you really know what you
3814       are  doing.   The  only  generally  useful  call  is   png_set_compres‐
3815       sion_level()  which changes how much time zlib spends on trying to com‐
3816       press the image data.  See the Compression Library  (zlib.h  and  algo‐
3817       rithm.txt,  distributed  with zlib) for details on the compression lev‐
3818       els.
3819
3820           #include zlib.h
3821
3822           /* Set the zlib compression level */
3823           png_set_compression_level(png_ptr,
3824               Z_BEST_COMPRESSION);
3825
3826           /* Set other zlib parameters for compressing IDAT */
3827           png_set_compression_mem_level(png_ptr, 8);
3828           png_set_compression_strategy(png_ptr,
3829               Z_DEFAULT_STRATEGY);
3830           png_set_compression_window_bits(png_ptr, 15);
3831           png_set_compression_method(png_ptr, 8);
3832           png_set_compression_buffer_size(png_ptr, 8192)
3833
3834           /* Set zlib parameters for text compression
3835            * If you don't call these, the parameters
3836            * fall back on those defined for IDAT chunks
3837            */
3838           png_set_text_compression_mem_level(png_ptr, 8);
3839           png_set_text_compression_strategy(png_ptr,
3840               Z_DEFAULT_STRATEGY);
3841           png_set_text_compression_window_bits(png_ptr, 15);
3842           png_set_text_compression_method(png_ptr, 8);
3843
3844
3845   Setting the contents of info for output
3846       You now need to fill in the png_info structure with all  the  data  you
3847       wish  to  write  before the actual image.  Note that the only thing you
3848       are allowed to write after the image is the text chunks  and  the  time
3849       chunk  (as  of PNG Specification 1.2, anyway).  See png_write_end() and
3850       the latest PNG specification for more information on that.  If you wish
3851       to write them before the image, fill them in now, and flag that data as
3852       being valid.  If you want to wait until after the data, don't fill them
3853       until  png_write_end().   For all the fields in png_info and their data
3854       types, see png.h.  For explanations of what the fields contain, see the
3855       PNG specification.
3856
3857       Some of the more important parts of the png_info are:
3858
3859           png_set_IHDR(png_ptr, info_ptr, width, height,
3860              bit_depth, color_type, interlace_type,
3861              compression_type, filter_method)
3862
3863           width          - holds the width of the image
3864                            in pixels (up to 2^31).
3865
3866           height         - holds the height of the image
3867                            in pixels (up to 2^31).
3868
3869           bit_depth      - holds the bit depth of one of the
3870                            image channels.
3871                            (valid values are 1, 2, 4, 8, 16
3872                            and depend also on the
3873                            color_type.  See also significant
3874                            bits (sBIT) below).
3875
3876           color_type     - describes which color/alpha
3877                            channels are present.
3878                            PNG_COLOR_TYPE_GRAY
3879                               (bit depths 1, 2, 4, 8, 16)
3880                            PNG_COLOR_TYPE_GRAY_ALPHA
3881                               (bit depths 8, 16)
3882                            PNG_COLOR_TYPE_PALETTE
3883                               (bit depths 1, 2, 4, 8)
3884                            PNG_COLOR_TYPE_RGB
3885                               (bit_depths 8, 16)
3886                            PNG_COLOR_TYPE_RGB_ALPHA
3887                               (bit_depths 8, 16)
3888
3889                            PNG_COLOR_MASK_PALETTE
3890                            PNG_COLOR_MASK_COLOR
3891                            PNG_COLOR_MASK_ALPHA
3892
3893           interlace_type - PNG_INTERLACE_NONE or
3894                            PNG_INTERLACE_ADAM7
3895
3896           compression_type - (must be
3897                            PNG_COMPRESSION_TYPE_DEFAULT)
3898
3899           filter_method  - (must be PNG_FILTER_TYPE_DEFAULT
3900                            or, if you are writing a PNG to
3901                            be embedded in a MNG datastream,
3902                            can also be
3903                            PNG_INTRAPIXEL_DIFFERENCING)
3904
3905       If  you  call  png_set_IHDR(),  the  call must appear before any of the
3906       other png_set_*() functions, because they might require access to  some
3907       of  the  IHDR  settings.   The  remaining  png_set_*() functions can be
3908       called in any order.
3909
3910       If you wish, you can reset  the  compression_type,  interlace_type,  or
3911       filter_method  later  by  calling png_set_IHDR() again; if you do this,
3912       the width, height, bit_depth, and color_type must be the same  in  each
3913       call.
3914
3915           png_set_PLTE(png_ptr, info_ptr, palette,
3916              num_palette);
3917
3918           palette        - the palette for the file
3919                            (array of png_color)
3920           num_palette    - number of entries in the palette
3921
3922           png_set_gAMA(png_ptr, info_ptr, file_gamma);
3923           png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
3924
3925           file_gamma     - the gamma at which the image was
3926                            created (PNG_INFO_gAMA)
3927
3928           int_file_gamma - 100,000 times the gamma at which
3929                            the image was created
3930
3931           png_set_cHRM(png_ptr, info_ptr,  white_x, white_y, red_x, red_y,
3932                            green_x, green_y, blue_x, blue_y)
3933           png_set_cHRM_XYZ(png_ptr, info_ptr, red_X, red_Y, red_Z, green_X,
3934                            green_Y, green_Z, blue_X, blue_Y, blue_Z)
3935           png_set_cHRM_fixed(png_ptr, info_ptr, int_white_x, int_white_y,
3936                            int_red_x, int_red_y, int_green_x, int_green_y,
3937                            int_blue_x, int_blue_y)
3938           png_set_cHRM_XYZ_fixed(png_ptr, info_ptr, int_red_X, int_red_Y,
3939                            int_red_Z, int_green_X, int_green_Y, int_green_Z,
3940                            int_blue_X, int_blue_Y, int_blue_Z)
3941
3942           {white,red,green,blue}_{x,y}
3943                            A  color  space encoding specified using the chro‐
3944       maticities
3945                            of the end points and the white point.
3946
3947           {red,green,blue}_{X,Y,Z}
3948                            A color space encoding specified using the  encod‐
3949       ing end
3950                            points  - the CIE tristimulus specification of the
3951       intended
3952                            color of the red, green and blue channels  in  the
3953       PNG RGB
3954                            data.   The  white  point is simply the sum of the
3955       three end
3956                            points.
3957
3958           png_set_sRGB(png_ptr, info_ptr, srgb_intent);
3959
3960           srgb_intent    - the rendering intent
3961                            (PNG_INFO_sRGB) The presence of
3962                            the sRGB chunk means that the pixel
3963                            data is in the sRGB color space.
3964                            This chunk also implies specific
3965                            values of gAMA and cHRM.  Rendering
3966                            intent is the CSS-1 property that
3967                            has been defined by the International
3968                            Color Consortium
3969                            (http://www.color.org).
3970                            It can be one of
3971                            PNG_sRGB_INTENT_SATURATION,
3972                            PNG_sRGB_INTENT_PERCEPTUAL,
3973                            PNG_sRGB_INTENT_ABSOLUTE, or
3974                            PNG_sRGB_INTENT_RELATIVE.
3975
3976
3977           png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
3978              srgb_intent);
3979
3980           srgb_intent    - the rendering intent
3981                            (PNG_INFO_sRGB) The presence of the
3982                            sRGB chunk means that the pixel
3983                            data is in the sRGB color space.
3984                            This function also causes gAMA and
3985                            cHRM chunks with the specific values
3986                            that are consistent with sRGB to be
3987                            written.
3988
3989           png_set_iCCP(png_ptr, info_ptr, name, compression_type,
3990                              profile, proflen);
3991
3992           name             - The profile name.
3993
3994           compression_type - The compression type; always
3995                              PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
3996                              You may give NULL to this argument to
3997                              ignore it.
3998
3999           profile          - International Color Consortium color
4000                              profile data. May contain NULs.
4001
4002           proflen          - length of profile data in bytes.
4003
4004           png_set_sBIT(png_ptr, info_ptr, sig_bit);
4005
4006           sig_bit        - the number of significant bits for
4007                            (PNG_INFO_sBIT) each of the gray, red,
4008                            green, and blue channels, whichever are
4009                            appropriate for the given color type
4010                            (png_color_16)
4011
4012           png_set_tRNS(png_ptr, info_ptr, trans_alpha,
4013              num_trans, trans_color);
4014
4015           trans_alpha    - array of alpha (transparency)
4016                            entries for palette (PNG_INFO_tRNS)
4017
4018           num_trans      - number of transparent entries
4019                            (PNG_INFO_tRNS)
4020
4021           trans_color    - graylevel or color sample values
4022                            (in order red, green, blue) of the
4023                            single transparent color for
4024                            non-paletted images (PNG_INFO_tRNS)
4025
4026           png_set_hIST(png_ptr, info_ptr, hist);
4027
4028           hist           - histogram of palette (array of
4029                            png_uint_16) (PNG_INFO_hIST)
4030
4031           png_set_tIME(png_ptr, info_ptr, mod_time);
4032
4033           mod_time       - time image was last modified
4034                            (PNG_VALID_tIME)
4035
4036           png_set_bKGD(png_ptr, info_ptr, background);
4037
4038           background     - background color (of type
4039                            png_color_16p) (PNG_VALID_bKGD)
4040
4041           png_set_text(png_ptr, info_ptr, text_ptr, num_text);
4042
4043           text_ptr       - array of png_text holding image
4044                            comments
4045
4046           text_ptr[i].compression - type of compression used
4047                        on "text" PNG_TEXT_COMPRESSION_NONE
4048                                  PNG_TEXT_COMPRESSION_zTXt
4049                                  PNG_ITXT_COMPRESSION_NONE
4050                                  PNG_ITXT_COMPRESSION_zTXt
4051           text_ptr[i].key   - keyword for comment.  Must contain
4052                        1-79 characters.
4053           text_ptr[i].text  - text comments for current
4054                                keyword.  Can be NULL or empty.
4055           text_ptr[i].text_length - length of text string,
4056                        after decompression, 0 for iTXt
4057           text_ptr[i].itxt_length - length of itxt string,
4058                        after decompression, 0 for tEXt/zTXt
4059           text_ptr[i].lang  - language of comment (NULL or
4060                                empty for unknown).
4061           text_ptr[i].translated_keyword  - keyword in UTF-8 (NULL
4062                                or empty for unknown).
4063
4064           Note that the itxt_length, lang, and lang_key
4065           members of the text_ptr structure only exist when the
4066           library is built with iTXt chunk support.  Prior to
4067           libpng-1.4.0 the library was built by default without
4068           iTXt support. Also note that when iTXt is supported,
4069           they contain NULL pointers when the "compression"
4070           field contains PNG_TEXT_COMPRESSION_NONE or
4071           PNG_TEXT_COMPRESSION_zTXt.
4072
4073           num_text       - number of comments
4074
4075           png_set_sPLT(png_ptr, info_ptr, &palette_ptr,
4076              num_spalettes);
4077
4078           palette_ptr    - array of png_sPLT_struct structures
4079                            to be added to the list of palettes
4080                            in the info structure.
4081           num_spalettes  - number of palette structures to be
4082                            added.
4083
4084           png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y,
4085               unit_type);
4086
4087           offset_x  - positive offset from the left
4088                            edge of the screen
4089
4090           offset_y  - positive offset from the top
4091                            edge of the screen
4092
4093           unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
4094
4095           png_set_pHYs(png_ptr, info_ptr, res_x, res_y,
4096               unit_type);
4097
4098           res_x       - pixels/unit physical resolution
4099                         in x direction
4100
4101           res_y       - pixels/unit physical resolution
4102                         in y direction
4103
4104           unit_type   - PNG_RESOLUTION_UNKNOWN,
4105                         PNG_RESOLUTION_METER
4106
4107           png_set_sCAL(png_ptr, info_ptr, unit, width, height)
4108
4109           unit        - physical scale units (an integer)
4110
4111           width       - width of a pixel in physical scale units
4112
4113           height      - height of a pixel in physical scale units
4114                         (width and height are doubles)
4115
4116           png_set_sCAL_s(png_ptr, info_ptr, unit, width, height)
4117
4118           unit        - physical scale units (an integer)
4119
4120           width       - width of a pixel in physical scale units
4121                         expressed as a string
4122
4123           height      - height of a pixel in physical scale units
4124                        (width and height are strings like "2.54")
4125
4126           png_set_unknown_chunks(png_ptr, info_ptr, &unknowns,
4127              num_unknowns)
4128
4129           unknowns          - array of png_unknown_chunk
4130                               structures holding unknown chunks
4131           unknowns[i].name  - name of unknown chunk
4132           unknowns[i].data  - data of unknown chunk
4133           unknowns[i].size  - size of unknown chunk's data
4134           unknowns[i].location - position to write chunk in file
4135                                  0: do not write chunk
4136                                  PNG_HAVE_IHDR: before PLTE
4137                                  PNG_HAVE_PLTE: before IDAT
4138                                  PNG_AFTER_IDAT: after IDAT
4139
4140       The "location" member is set automatically according to  what  part  of
4141       the  output  file  has  already been written.  You can change its value
4142       after calling png_set_unknown_chunks() as  demonstrated  in  pngtest.c.
4143       Within  each  of the "locations", the chunks are sequenced according to
4144       their position in the structure (that is, the value of  "i",  which  is
4145       the  order  in  which  the chunk was either read from the input file or
4146       defined with png_set_unknown_chunks).
4147
4148       A quick word about text and num_text.  text is  an  array  of  png_text
4149       structures.   num_text  is the number of valid structures in the array.
4150       Each png_text structure holds a language code, a keyword, a text value,
4151       and a compression type.
4152
4153       The  compression  types  have the same valid numbers as the compression
4154       types of the image data.  Currently, the only  valid  number  is  zero.
4155       However,  you  can store text either compressed or uncompressed, unlike
4156       images, which always have to be compressed.  So if you don't  want  the
4157       text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
4158       Because tEXt and zTXt chunks don't have a language field, if you  spec‐
4159       ify PNG_TEXT_COMPRESSION_NONE or PNG_TEXT_COMPRESSION_zTXt any language
4160       code or translated keyword will not be written out.
4161
4162       Until text gets around a few hundred bytes, it is not worth compressing
4163       it.   After  the text has been written out to the file, the compression
4164       type  is  set  to  PNG_TEXT_COMPRESSION_NONE_WR  or   PNG_TEXT_COMPRES‐
4165       SION_zTXt_WR,  so  that  it isn't written out again at the end (in case
4166       you are calling png_write_end() with the same struct).
4167
4168       The keywords that are given in the PNG Specification are:
4169
4170           Title            Short (one line) title or
4171                            caption for image
4172
4173           Author           Name of image's creator
4174
4175           Description      Description of image (possibly long)
4176
4177           Copyright        Copyright notice
4178
4179           Creation Time    Time of original image creation
4180                            (usually RFC 1123 format, see below)
4181
4182           Software         Software used to create the image
4183
4184           Disclaimer       Legal disclaimer
4185
4186           Warning          Warning of nature of content
4187
4188           Source           Device used to create the image
4189
4190           Comment          Miscellaneous comment; conversion
4191                            from other image format
4192
4193       The keyword-text pairs work like this.  Keywords should be short simple
4194       descriptions  of  what the comment is about.  Some typical keywords are
4195       found in the PNG specification, as is some recommendations on keywords.
4196       You can repeat keywords in a file.  You can even write some text before
4197       the image and some after.  For example, you may want to put a  descrip‐
4198       tion  of  the  image  before  the image, but leave the disclaimer until
4199       after, so viewers working over modem connections don't have to wait for
4200       the disclaimer to go over the modem before they start seeing the image.
4201       Finally, keywords should be full words,  not  abbreviations.   Keywords
4202       and  text  are in the ISO 8859-1 (Latin-1) character set (a superset of
4203       regular ASCII) and can not contain NUL characters, and should not  con‐
4204       tain  control  or  other  unprintable characters.  To make the comments
4205       widely readable, stick with basic ASCII,  and  avoid  machine  specific
4206       character  set  extensions  like the IBM-PC character set.  The keyword
4207       must be present, but you can leave off  the  text  string  on  non-com‐
4208       pressed  pairs.   Compressed pairs must have a text string, as only the
4209       text string is compressed anyway, so the compression would be  meaning‐
4210       less.
4211
4212       PNG supports modification time via the png_time structure.  Two conver‐
4213       sion routines are provided, png_convert_from_time_t()  for  time_t  and
4214       png_convert_from_struct_tm()  for  struct  tm.  The time_t routine uses
4215       gmtime().  You don't have to use either of these, but if  you  wish  to
4216       fill in the png_time structure directly, you should provide the time in
4217       universal time (GMT) if possible instead of your local time.  Note that
4218       the  year  number  is the full year (e.g. 1998, rather than 98 - PNG is
4219       year 2000 compliant!), and that months start with 1.
4220
4221       If you want to store the time  of  the  original  image  creation,  you
4222       should  use  a plain tEXt chunk with the "Creation Time" keyword.  This
4223       is necessary because the "creation time" of a  PNG  image  is  somewhat
4224       vague,  depending  on whether you mean the PNG file, the time the image
4225       was created in a non-PNG format, a still photo from which the image was
4226       scanned, or possibly the subject matter itself.  In order to facilitate
4227       machine-readable dates, it is recommended that the "Creation Time" tEXt
4228       chunk  use  RFC  1123  format  dates (e.g. "22 May 1997 18:07:10 GMT"),
4229       although this isn't a requirement.  Unlike the tIME  chunk,  the  "Cre‐
4230       ation  Time"  tEXt chunk is not expected to be automatically changed by
4231       the software.  To facilitate the use of  RFC  1123  dates,  a  function
4232       png_convert_to_rfc1123(png_ptr,  png_timep) is provided to convert from
4233       PNG time to an RFC 1123 format string.
4234
4235
4236   Writing unknown chunks
4237       You can use the png_set_unknown_chunks function to queue up chunks  for
4238       writing.   You  give  it a chunk name, raw data, and a size; that's all
4239       there is to it.  The chunks will  be  written  by  the  next  following
4240       png_write_info_before_PLTE,  png_write_info, or png_write_end function.
4241       Any chunks previously read into the info structure's unknown-chunk list
4242       will  also be written out in a sequence that satisfies the PNG specifi‐
4243       cation's ordering rules.
4244
4245
4246   The high-level write interface
4247       At this point there are two ways to  proceed;  through  the  high-level
4248       write  interface,  or through a sequence of low-level write operations.
4249       You can use the high-level interface if your image data is  present  in
4250       the  info structure.  All defined output transformations are permitted,
4251       enabled by the following masks.
4252
4253           PNG_TRANSFORM_IDENTITY      No transformation
4254           PNG_TRANSFORM_PACKING       Pack 1, 2 and 4-bit samples
4255           PNG_TRANSFORM_PACKSWAP      Change order of packed
4256                                       pixels to LSB first
4257           PNG_TRANSFORM_INVERT_MONO   Invert monochrome images
4258           PNG_TRANSFORM_SHIFT         Normalize pixels to the
4259                                       sBIT depth
4260           PNG_TRANSFORM_BGR           Flip RGB to BGR, RGBA
4261                                       to BGRA
4262           PNG_TRANSFORM_SWAP_ALPHA    Flip RGBA to ARGB or GA
4263                                       to AG
4264           PNG_TRANSFORM_INVERT_ALPHA  Change alpha from opacity
4265                                       to transparency
4266           PNG_TRANSFORM_SWAP_ENDIAN   Byte-swap 16-bit samples
4267           PNG_TRANSFORM_STRIP_FILLER        Strip out filler
4268                                             bytes (deprecated).
4269           PNG_TRANSFORM_STRIP_FILLER_BEFORE Strip out leading
4270                                             filler bytes
4271           PNG_TRANSFORM_STRIP_FILLER_AFTER  Strip out trailing
4272                                             filler bytes
4273
4274       If you have valid image  data  in  the  info  structure  (you  can  use
4275       png_set_rows()  to  put  image  data  in the info structure), simply do
4276       this:
4277
4278           png_write_png(png_ptr, info_ptr, png_transforms, NULL)
4279
4280       where png_transforms is an integer containing the bitwise  OR  of  some
4281       set   of   transformation   flags.    This   call   is   equivalent  to
4282       png_write_info(), followed the set of transformations indicated by  the
4283       transform mask, then png_write_image(), and finally png_write_end().
4284
4285       (The  final  parameter  of this call is not yet used.  Someday it might
4286       point to transformation  parameters  required  by  some  future  output
4287       transform.)
4288
4289       You  must use png_transforms and not call any png_set_transform() func‐
4290       tions when you use png_write_png().
4291
4292
4293   The low-level write interface
4294       If you are going the low-level route instead,  you  are  now  ready  to
4295       write  all  the  file  information up to the actual image data.  You do
4296       this with a call to png_write_info().
4297
4298           png_write_info(png_ptr, info_ptr);
4299
4300       Note that there is  one  transformation  you  may  need  to  do  before
4301       png_write_info().   In  PNG files, the alpha channel in an image is the
4302       level of opacity.  If your data is supplied as a level of transparency,
4303       you  can  invert  the  alpha  channel before you write it, so that 0 is
4304       fully transparent and 255 (in 8-bit or paletted images)  or  65535  (in
4305       16-bit images) is fully opaque, with
4306
4307           png_set_invert_alpha(png_ptr);
4308
4309       This  must  appear  before  png_write_info()  instead of later with the
4310       other transformations because in the case of paletted images  the  tRNS
4311       chunk  data  has  to  be inverted before the tRNS chunk is written.  If
4312       your image is not a paletted image, the tRNS data (which in such  cases
4313       represents  a single color to be rendered as transparent) won't need to
4314       be changed, and you  can  safely  do  this  transformation  after  your
4315       png_write_info() call.
4316
4317       If you need to write a private chunk that you want to appear before the
4318       PLTE chunk when PLTE is present, you can write  the  PNG  info  in  two
4319       steps, and insert code to write your own chunk between them:
4320
4321           png_write_info_before_PLTE(png_ptr, info_ptr);
4322           png_set_unknown_chunks(png_ptr, info_ptr, ...);
4323           png_write_info(png_ptr, info_ptr);
4324
4325       After  you've  written the file information, you can set up the library
4326       to handle any special transformations of the image data.   The  various
4327       ways  to  transform  the  data will be described in the order that they
4328       should occur.  This is important, as some of  these  change  the  color
4329       type and/or bit depth of the data, and some others only work on certain
4330       color types and bit depths.  Even though each transformation checks  to
4331       see  if it has data that it can do something with, you should make sure
4332       to only enable a transformation if it will be valid for the data.   For
4333       example, don't swap red and blue on grayscale data.
4334
4335       PNG  files  store RGB pixels packed into 3 or 6 bytes.  This code tells
4336       the library to strip input data that has 4 or 8 bytes per pixel down to
4337       3  or  6  bytes  (or  strip 2 or 4-byte grayscale+filler data to 1 or 2
4338       bytes per pixel).
4339
4340           png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
4341
4342       where the 0 is unused, and the location is either PNG_FILLER_BEFORE  or
4343       PNG_FILLER_AFTER,  depending  upon whether the filler byte in the pixel
4344       is stored XRGB or RGBX.
4345
4346       PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small  as
4347       they can, resulting in, for example, 8 pixels per byte for 1 bit files.
4348       If the data is supplied at 1 pixel per byte, use this code, which  will
4349       correctly pack the pixels into a single byte:
4350
4351           png_set_packing(png_ptr);
4352
4353       PNG  files  reduce  possible bit depths to 1, 2, 4, 8, and 16.  If your
4354       data is of another bit depth, you can write an sBIT chunk into the file
4355       so that decoders can recover the original data if desired.
4356
4357           /* Set the true bit depth of the image data */
4358           if (color_type & PNG_COLOR_MASK_COLOR)
4359           {
4360              sig_bit.red = true_bit_depth;
4361              sig_bit.green = true_bit_depth;
4362              sig_bit.blue = true_bit_depth;
4363           }
4364
4365           else
4366           {
4367              sig_bit.gray = true_bit_depth;
4368           }
4369
4370           if (color_type & PNG_COLOR_MASK_ALPHA)
4371           {
4372              sig_bit.alpha = true_bit_depth;
4373           }
4374
4375           png_set_sBIT(png_ptr, info_ptr, &sig_bit);
4376
4377       If  the  data is stored in the row buffer in a bit depth other than one
4378       supported by PNG (e.g. 3 bit data in the range 0-7 for  a  4-bit  PNG),
4379       this  will scale the values to appear to be the correct bit depth as is
4380       required by PNG.
4381
4382           png_set_shift(png_ptr, &sig_bit);
4383
4384       PNG files store 16-bit pixels in network byte  order  (big-endian,  ie.
4385       most significant bits first).  This code would be used if they are sup‐
4386       plied the other way (little-endian, i.e. least significant bits  first,
4387       the way PCs store them):
4388
4389           if (bit_depth > 8)
4390              png_set_swap(png_ptr);
4391
4392       If  you  are using packed-pixel images (1, 2, or 4 bits/pixel), and you
4393       need to change the order the pixels are packed into bytes, you can use:
4394
4395           if (bit_depth < 8)
4396              png_set_packswap(png_ptr);
4397
4398       PNG files store 3 color pixels in red, green, blue  order.   This  code
4399       would be used if they are supplied as blue, green, red:
4400
4401           png_set_bgr(png_ptr);
4402
4403       PNG  files describe monochrome as black being zero and white being one.
4404       This code would be used if the pixels are supplied with  this  reversed
4405       (black being one and white being zero):
4406
4407           png_set_invert_mono(png_ptr);
4408
4409       Finally,  you can write your own transformation function if none of the
4410       existing ones meets your needs.  This is done  by  setting  a  callback
4411       with
4412
4413           png_set_write_user_transform_fn(png_ptr,
4414              write_transform_fn);
4415
4416       You must supply the function
4417
4418           void write_transform_fn(png_structp png_ptr, png_row_infop
4419              row_info, png_bytep data)
4420
4421       See  pngtest.c  for  a  working  example.  Your function will be called
4422       before any of the other transformations are  processed.   If  supported
4423       libpng  also  supplies  an  information routine that may be called from
4424       your callback:
4425
4426          png_get_current_row_number(png_ptr);
4427          png_get_current_pass_number(png_ptr);
4428
4429       This returns the current row passed to the transform.  With  interlaced
4430       images the value returned is the row in the input sub-image image.  Use
4431       PNG_ROW_FROM_PASS_ROW(row, pass) and  PNG_COL_FROM_PASS_COL(col,  pass)
4432       to  find  the  output  pixel  (x,y) given an interlaced sub-image pixel
4433       (row,col,pass).
4434
4435       The discussion of interlace handling above contains more information on
4436       how to use these values.
4437
4438       You can also set up a pointer to a user structure for use by your call‐
4439       back function.
4440
4441           png_set_user_transform_info(png_ptr, user_ptr, 0, 0);
4442
4443       The user_channels  and  user_depth  parameters  of  this  function  are
4444       ignored when writing; you can set them to zero as shown.
4445
4446       You  can  retrieve  the  pointer  via  the function png_get_user_trans‐
4447       form_ptr().  For example:
4448
4449           voidp write_user_transform_ptr =
4450              png_get_user_transform_ptr(png_ptr);
4451
4452       It is possible to have libpng flush any pending  output,  either  manu‐
4453       ally,  or automatically after a certain number of lines have been writ‐
4454       ten.  To flush the output stream a single time call:
4455
4456           png_write_flush(png_ptr);
4457
4458       and to have libpng flush the output stream periodically after a certain
4459       number of scanlines have been written, call:
4460
4461           png_set_flush(png_ptr, nrows);
4462
4463       Note   that   the   distance   between  rows  is  from  the  last  time
4464       png_write_flush() was called, or the first row of the image if  it  has
4465       never  been  called.   So if you write 50 lines, and then png_set_flush
4466       25, it will flush the output on the next scanline, and every  25  lines
4467       thereafter,  unless  png_write_flush()  is  called before 25 more lines
4468       have been written.  If nrows is too small (less than about 10 lines for
4469       a  640 pixel wide RGB image) the image compression may decrease notice‐
4470       ably (although this may  be  acceptable  for  real-time  applications).
4471       Infrequent  flushing will only degrade the compression performance by a
4472       few percent over images that do not use flushing.
4473
4474
4475   Writing the image data
4476       That's it for the transformations.  Now you can write the  image  data.
4477       The  simplest  way to do this is in one function call.  If you have the
4478       whole image in memory, you can just call png_write_image()  and  libpng
4479       will write the image.  You will need to pass in an array of pointers to
4480       each row.  This function  automatically  handles  interlacing,  so  you
4481       don't  need  to call png_set_interlace_handling() or call this function
4482       multiple  times,  or  any  of   that   other   stuff   necessary   with
4483       png_write_rows().
4484
4485           png_write_image(png_ptr, row_pointers);
4486
4487       where row_pointers is:
4488
4489           png_byte *row_pointers[height];
4490
4491       You can point to void or char or whatever you use for pixels.
4492
4493       If  you  don't  want  to  write  the  whole  image at once, you can use
4494       png_write_rows() instead.  If the file is not interlaced, this is  sim‐
4495       ple:
4496
4497           png_write_rows(png_ptr, row_pointers,
4498              number_of_rows);
4499
4500       row_pointers is the same as in the png_write_image() call.
4501
4502       If  you are just writing one row at a time, you can do this with a sin‐
4503       gle row_pointer instead of an array of row_pointers:
4504
4505           png_bytep row_pointer = row;
4506
4507           png_write_row(png_ptr, row_pointer);
4508
4509       When the file is interlaced, things can get a good  deal  more  compli‐
4510       cated.   The  only  currently (as of the PNG Specification version 1.2,
4511       dated July 1999) defined  interlacing  scheme  for  PNG  files  is  the
4512       "Adam7"  interlace scheme, that breaks down an image into seven smaller
4513       images of varying size.  libpng will build these images for you, or you
4514       can  do them yourself.  If you want to build them yourself, see the PNG
4515       specification for details of which pixels to write when.
4516
4517       If you don't want libpng to handle the interlacing  details,  just  use
4518       png_set_interlace_handling() and call png_write_rows() the correct num‐
4519       ber of times to write all the sub-images  (png_set_interlace_handling()
4520       returns the number of sub-images.)
4521
4522       If  you want libpng to build the sub-images, call this before you start
4523       writing any rows:
4524
4525           number_of_passes = png_set_interlace_handling(png_ptr);
4526
4527       This will return the number  of  passes  needed.   Currently,  this  is
4528       seven, but may change if another interlace type is added.
4529
4530       Then write the complete image number_of_passes times.
4531
4532           png_write_rows(png_ptr, row_pointers, number_of_rows);
4533
4534       Think  carefully  before you write an interlaced image.  Typically code
4535       that reads such images reads all the image  data  into  memory,  uncom‐
4536       pressed,  before  doing  any processing.  Only code that can display an
4537       image on the fly can take advantage of the interlacing  and  even  then
4538       the  image  has  to  be exactly the correct size for the output device,
4539       because scaling an image requires adjacent pixels  and  these  are  not
4540       available until all the passes have been read.
4541
4542       If you do write an interlaced image you will hardly ever need to handle
4543       the interlacing yourself.  Call  png_set_interlace_handling()  and  use
4544       the approach described above.
4545
4546       The  only  time it is conceivable that you will really need to write an
4547       interlaced image pass-by-pass is when you have read one  pass  by  pass
4548       and  made some pixel-by-pixel transformation to it, as described in the
4549       read code above.  In this case use the PNG_PASS_ROWS and  PNG_PASS_COLS
4550       macros to determine the size of each sub-image in turn and simply write
4551       the rows you obtained from the read code.
4552
4553
4554   Finishing a sequential write
4555       After you are finished writing the image, you should finish writing the
4556       file.   If  you  are interested in writing comments or time, you should
4557       pass an appropriately filled png_info pointer.  If you are  not  inter‐
4558       ested, you can pass NULL.
4559
4560           png_write_end(png_ptr, info_ptr);
4561
4562       When you are done, you can free all memory used by libpng like this:
4563
4564           png_destroy_write_struct(&png_ptr, &info_ptr);
4565
4566       It  is  also  possible  to  individually free the info_ptr members that
4567       point to libpng-allocated storage with the following function:
4568
4569           png_free_data(png_ptr, info_ptr, mask, seq)
4570
4571           mask  - identifies data to be freed, a mask
4572                   containing the bitwise OR of one or
4573                   more of
4574                     PNG_FREE_PLTE, PNG_FREE_TRNS,
4575                     PNG_FREE_HIST, PNG_FREE_ICCP,
4576                     PNG_FREE_PCAL, PNG_FREE_ROWS,
4577                     PNG_FREE_SCAL, PNG_FREE_SPLT,
4578                     PNG_FREE_TEXT, PNG_FREE_UNKN,
4579                   or simply PNG_FREE_ALL
4580
4581           seq   - sequence number of item to be freed
4582                   (-1 for all items)
4583
4584       This function may be  safely  called  when  the  relevant  storage  has
4585       already  been freed, or has not yet been allocated, or was allocated by
4586       the user  and not by libpng,  and will in those cases do nothing.   The
4587       "seq"  parameter is ignored if only one item of the selected data type,
4588       such as PLTE, is allowed.  If "seq" is not -1, and multiple  items  are
4589       allowed for the data type identified in the mask, such as text or sPLT,
4590       only the n'th item in the structure is freed, where n is "seq".
4591
4592       If you allocated data such as a palette that you passed  in  to  libpng
4593       with  png_set_*,  you  must  not  free it until just before the call to
4594       png_destroy_write_struct().
4595
4596       The default behavior is only to free data that was allocated internally
4597       by libpng.  This can be changed, so that libpng will not free the data,
4598       or so that it will free data  that  was  allocated  by  the  user  with
4599       png_malloc()  or png_calloc() and passed in via a png_set_*() function,
4600       with
4601
4602           png_data_freer(png_ptr, info_ptr, freer, mask)
4603
4604           freer  - one of
4605                      PNG_DESTROY_WILL_FREE_DATA
4606                      PNG_SET_WILL_FREE_DATA
4607                      PNG_USER_WILL_FREE_DATA
4608
4609           mask   - which data elements are affected
4610                    same choices as in png_free_data()
4611
4612       For example, to transfer responsibility  for  some  data  from  a  read
4613       structure to a write structure, you could use
4614
4615           png_data_freer(read_ptr, read_info_ptr,
4616              PNG_USER_WILL_FREE_DATA,
4617              PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
4618
4619           png_data_freer(write_ptr, write_info_ptr,
4620              PNG_DESTROY_WILL_FREE_DATA,
4621              PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
4622
4623       thereby  briefly reassigning responsibility for freeing to the user but
4624       immediately afterwards reassigning it once more  to  the  write_destroy
4625       function.   Having done this, it would then be safe to destroy the read
4626       structure and continue to use the PLTE, tRNS,  and  hIST  data  in  the
4627       write structure.
4628
4629       This  function  only affects data that has already been allocated.  You
4630       can call this function before calling after the  png_set_*()  functions
4631       to  control whether the user or png_destroy_*() is supposed to free the
4632       data.  When the user assumes responsibility for libpng-allocated  data,
4633       the  application  must  use  png_free()  to  free it, and when the user
4634       transfers responsibility to libpng for data that  the  user  has  allo‐
4635       cated, the user must have used png_malloc() or png_calloc() to allocate
4636       it.
4637
4638       If you  allocated  text_ptr.text,  text_ptr.lang,  and  text_ptr.trans‐
4639       lated_keyword  separately,  do  not transfer responsibility for freeing
4640       text_ptr to libpng, because when libpng fills a png_text  structure  it
4641       combines  these  members  with the key member, and png_free_data() will
4642       free only text_ptr.key.  Similarly, if you transfer responsibility  for
4643       free'ing  text_ptr  from  libpng  to your application, your application
4644       must not separately free those members.  For a more compact example  of
4645       writing a PNG image, see the file example.c.
4646
4647

V. Modifying/Customizing libpng:

4649       There are two issues here.  The first is changing how libpng does stan‐
4650       dard things like memory allocation, input/output, and  error  handling.
4651       The  second  deals with more complicated things like adding new chunks,
4652       adding new transformations, and generally changing  how  libpng  works.
4653       Both  of  those  are  compile-time  issues; that is, they are generally
4654       determined at the time the code is written, and there is rarely a  need
4655       to provide the user with a means of changing them.
4656
4657       Memory allocation, input/output, and error handling
4658
4659       All  of  the  memory  allocation,  input/output,  and error handling in
4660       libpng goes through callbacks that are user-settable.  The default rou‐
4661       tines  are  in  pngmem.c,  pngrio.c,  pngwio.c, and pngerror.c, respec‐
4662       tively.  To change these functions, call the appropriate png_set_*_fn()
4663       function.
4664
4665       Memory  allocation is done through the functions png_malloc(), png_cal‐
4666       loc(), and png_free().  The png_malloc() and png_free() functions  cur‐
4667       rently  just  call  the  standard  C  functions  and png_calloc() calls
4668       png_malloc() and then clears the newly allocated memory to  zero;  note
4669       that  png_calloc(png_ptr,  size)  is not the same as the calloc(number,
4670       size) function provided by stdlib.h.  There is limited support for cer‐
4671       tain  systems  with  segmented  memory  architectures  and the types of
4672       pointers declared by png.h match this; you will have to use appropriate
4673       pointers  in your application.  Since it is unlikely that the method of
4674       handling memory allocation on a platform will change  between  applica‐
4675       tions, these functions must be modified in the library at compile time.
4676       If you prefer to use a different method of allocating and freeing data,
4677       you  can  use png_create_read_struct_2() or png_create_write_struct_2()
4678       to register your own functions as  described  above.   These  functions
4679       also provide a void pointer that can be retrieved via
4680
4681           mem_ptr=png_get_mem_ptr(png_ptr);
4682
4683       Your replacement memory functions must have prototypes as follows:
4684
4685           png_voidp malloc_fn(png_structp png_ptr,
4686              png_alloc_size_t size);
4687
4688           void free_fn(png_structp png_ptr, png_voidp ptr);
4689
4690       Your malloc_fn() must return NULL in case of failure.  The png_malloc()
4691       function will normally call png_error() if it receives a NULL from  the
4692       system memory allocator or from your replacement malloc_fn().
4693
4694       Your  free_fn()  will  never  be called with a NULL ptr, since libpng's
4695       png_free() checks for NULL before calling free_fn().
4696
4697       Input/Output in libpng is  done  through  png_read()  and  png_write(),
4698       which  currently  just call fread() and fwrite().  The FILE * is stored
4699       in png_struct and is initialized via png_init_io().   If  you  wish  to
4700       change  the  method of I/O, the library supplies callbacks that you can
4701       set through the function png_set_read_fn()  and  png_set_write_fn()  at
4702       run  time,  instead of calling the png_init_io() function.  These func‐
4703       tions also provide a void pointer that can be retrieved via  the  func‐
4704       tion png_get_io_ptr().  For example:
4705
4706           png_set_read_fn(png_structp read_ptr,
4707               voidp read_io_ptr, png_rw_ptr read_data_fn)
4708
4709           png_set_write_fn(png_structp write_ptr,
4710               voidp write_io_ptr, png_rw_ptr write_data_fn,
4711               png_flush_ptr output_flush_fn);
4712
4713           voidp read_io_ptr = png_get_io_ptr(read_ptr);
4714           voidp write_io_ptr = png_get_io_ptr(write_ptr);
4715
4716       The replacement I/O functions must have prototypes as follows:
4717
4718           void user_read_data(png_structp png_ptr,
4719               png_bytep data, png_size_t length);
4720
4721           void user_write_data(png_structp png_ptr,
4722               png_bytep data, png_size_t length);
4723
4724           void user_flush_data(png_structp png_ptr);
4725
4726       The user_read_data() function is responsible for detecting and handling
4727       end-of-data errors.
4728
4729       Supplying NULL for the read, write, or flush functions sets  them  back
4730       to  using  the  default  C stream functions, which expect the io_ptr to
4731       point to a standard *FILE structure.  It is probably a mistake  to  use
4732       NULL for one of write_data_fn and output_flush_fn but not both of them,
4733       unless you have built libpng with PNG_NO_WRITE_FLUSH defined.  It is an
4734       error to read from a write stream, and vice versa.
4735
4736       Error handling in libpng is done through png_error() and png_warning().
4737       Errors handled through png_error() are fatal, meaning that  png_error()
4738       should  never  return  to  its  caller.  Currently, this is handled via
4739       setjmp()  and  longjmp()  (unless  you  have   compiled   libpng   with
4740       PNG_NO_SETJMP,  in  which  case it is handled via PNG_ABORT()), but you
4741       could change this to do things like exit() if you should wish, as  long
4742       as your function does not return.
4743
4744       On  non-fatal  errors,  png_warning() is called to print a warning mes‐
4745       sage, and then  control  returns  to  the  calling  code.   By  default
4746       png_error()  and  png_warning() print a message on stderr via fprintf()
4747       unless the library is compiled with PNG_NO_CONSOLE_IO defined  (because
4748       you don't want the messages) or PNG_NO_STDIO defined (because fprintf()
4749       isn't available).  If you wish to change  the  behavior  of  the  error
4750       functions,  you  will need to set up your own message callbacks.  These
4751       functions are normally supplied at the time that the png_struct is cre‐
4752       ated.   It is also possible to redirect errors and warnings to your own
4753       replacement functions after png_create_*_struct() has  been  called  by
4754       calling:
4755
4756           png_set_error_fn(png_structp png_ptr,
4757               png_voidp error_ptr, png_error_ptr error_fn,
4758               png_error_ptr warning_fn);
4759
4760           png_voidp error_ptr = png_get_error_ptr(png_ptr);
4761
4762       If  NULL is supplied for either error_fn or warning_fn, then the libpng
4763       default function will be used, calling fprintf() and/or longjmp() if  a
4764       problem  is  encountered.   The replacement error functions should have
4765       parameters as follows:
4766
4767           void user_error_fn(png_structp png_ptr,
4768               png_const_charp error_msg);
4769
4770           void user_warning_fn(png_structp png_ptr,
4771               png_const_charp warning_msg);
4772
4773       The motivation behind using setjmp() and longjmp() is the C++ throw and
4774       catch  exception  handling methods.  This makes the code much easier to
4775       write, as there is no need to check every return code of every function
4776       call.   However, there are some uncertainties about the status of local
4777       variables after a longjmp, so the user may want  to  be  careful  about
4778       doing  anything after setjmp returns non-zero besides returning itself.
4779       Consult your compiler documentation for more details.  For an  alterna‐
4780       tive  approach,  you  may  wish  to  use  the  "cexcept"  facility (see
4781       http://cexcept.sourceforge.net), which is illustrated in pngvalid.c and
4782       in contrib/visupng.
4783
4784
4785   Custom chunks
4786       If  you need to read or write custom chunks, you may need to get deeper
4787       into the libpng code.  The library now has mechanisms for  storing  and
4788       writing chunks of unknown type; you can even declare callbacks for cus‐
4789       tom chunks.  However, this may not be good enough if the  library  code
4790       itself needs to know about interactions between your chunk and existing
4791       `intrinsic' chunks.
4792
4793       If you need to write a new intrinsic chunk, first read the PNG specifi‐
4794       cation.  Acquire  a  first level of understanding of how it works.  Pay
4795       particular attention to the sections that  describe  chunk  names,  and
4796       look at how other chunks were designed, so you can do things similarly.
4797       Second, check out the sections of libpng that read  and  write  chunks.
4798       Try  to find a chunk that is similar to yours and use it as a template.
4799       More details can be found in the comments inside the code.  It is  best
4800       to  handle  private or unknown chunks in a generic method, via callback
4801       functions, instead of by modifying libpng  functions.  This  is  illus‐
4802       trated in pngtest.c, which uses a callback function to handle a private
4803       "vpAg" chunk and the new  "sTER"  chunk,  which  are  both  unknown  to
4804       libpng.
4805
4806       If you wish to write your own transformation for the data, look through
4807       the part of the code that does the transformations, and check out  some
4808       of  the  simpler  ones  to get an idea of how they work.  Try to find a
4809       similar transformation to the one you want to add and copy off  of  it.
4810       More details can be found in the comments inside the code itself.
4811
4812
4813   Configuring for 16-bit platforms
4814       You  will want to look into zconf.h to tell zlib (and thus libpng) that
4815       it cannot allocate more then 64K at a time.  Even if you can, the  mem‐
4816       ory  won't  be accessible.  So limit zlib and libpng to 64K by defining
4817       MAXSEG_64K.
4818
4819
4820   Configuring for DOS
4821       For DOS users who only have access to the lower 640K, you will have  to
4822       limit  zlib's  memory usage via a png_set_compression_mem_level() call.
4823       See zlib.h or zconf.h in the zlib library for more information.
4824
4825
4826   Configuring for Medium Model
4827       Libpng's support for medium model has been tested on most of the  popu‐
4828       lar compilers.  Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets
4829       defined, and FAR gets defined to far in pngconf.h, and  you  should  be
4830       all  set.   Everything  in the library (except for zlib's structure) is
4831       expecting far data.  You must use the typedefs with the p or pp on  the
4832       end  for pointers (or at least look at them and be careful).  Make note
4833       that the rows of data are defined as png_bytepp, which is an  "unsigned
4834       char far * far *".
4835
4836
4837   Configuring for gui/windowing platforms:
4838       You will need to write new error and warning functions that use the GUI
4839       interface, as described previously, and set them to be  the  error  and
4840       warning  functions at the time that png_create_*_struct() is called, in
4841       order to have them available during the structure initialization.  They
4842       can  be  changed  later via png_set_error_fn().  On some compilers, you
4843       may also have to change the memory allocators (png_malloc, etc.).
4844
4845
4846   Configuring for compiler xxx:
4847       All includes for libpng are in pngconf.h.  If you need to  add,  change
4848       or  delete  an  include, this is the place to do it.  The includes that
4849       are not needed outside libpng are placed in pngpriv.h,  which  is  only
4850       used  by the routines inside libpng itself.  The files in libpng proper
4851       only include pngpriv.h and png.h, which %14%in turn includes pngconf.h.
4852       in  turn  includes pngconf.h and, as of libpng-1.5.0, pnglibconf.h.  As
4853       of libpng-1.5.0, pngpriv.h also includes  three  other  private  header
4854       files,  pngstruct.h,  pnginfo.h, and pngdebug.h, which contain material
4855       that previously appeared in the public headers.
4856
4857
4858   Configuring zlib:
4859       There are special functions to configure the compression.  Perhaps  the
4860       most  useful  one  changes  the compression level, which currently uses
4861       input compression values in the range 0 - 9.  The library normally uses
4862       the  default compression level (Z_DEFAULT_COMPRESSION = 6).  Tests have
4863       shown that for a large majority of images, compression  values  in  the
4864       range  3-6  compress  nearly  as  well as higher levels, and do so much
4865       faster.  For online applications it may be desirable  to  have  maximum
4866       speed  (Z_BEST_SPEED  = 1).  With versions of zlib after v0.99, you can
4867       also specify no compression (Z_NO_COMPRESSION = 0), but this would cre‐
4868       ate files larger than just storing the raw bitmap.  You can specify the
4869       compression level by calling:
4870
4871           #include zlib.h
4872           png_set_compression_level(png_ptr, level);
4873
4874       Another useful one is to reduce the memory level used by  the  library.
4875       The  memory level defaults to 8, but it can be lowered if you are short
4876       on memory (running DOS, for example, where you only have  640K).   Note
4877       that  the  memory level does have an effect on compression; among other
4878       things, lower levels will result in  sections  of  incompressible  data
4879       being  emitted  in smaller stored blocks, with a correspondingly larger
4880       relative overhead of up to 15% in the worst case.
4881
4882           #include zlib.h
4883           png_set_compression_mem_level(png_ptr, level);
4884
4885       The other functions are for configuring zlib.  They are not recommended
4886       for  normal  use  and  may  result in writing an invalid PNG file.  See
4887       zlib.h for more information on what these mean.
4888
4889           #include zlib.h
4890           png_set_compression_strategy(png_ptr,
4891               strategy);
4892
4893           png_set_compression_window_bits(png_ptr,
4894               window_bits);
4895
4896           png_set_compression_method(png_ptr, method);
4897
4898           png_set_compression_buffer_size(png_ptr, size);
4899
4900       As of libpng version 1.5.4, additional APIs  became  available  to  set
4901       these separately for non-IDAT compressed chunks such as zTXt, iTXt, and
4902       iCCP:
4903
4904           #include zlib.h
4905           #if PNG_LIBPNG_VER >= 10504
4906           png_set_text_compression_level(png_ptr, level);
4907
4908           png_set_text_compression_mem_level(png_ptr, level);
4909
4910           png_set_text_compression_strategy(png_ptr,
4911               strategy);
4912
4913           png_set_text_compression_window_bits(png_ptr,
4914               window_bits);
4915
4916           png_set_text_compression_method(png_ptr, method);
4917           #endif
4918
4919
4920   Controlling row filtering
4921       If you want to control whether libpng uses filtering or not, which fil‐
4922       ters  are used, and how it goes about picking row filters, you can call
4923       one of these functions.  The selection and configuration of row filters
4924       can  have  a  significant  impact  on the size and encoding speed and a
4925       somewhat lesser impact on the decoding speed of an image.  Filtering is
4926       enabled  by  default  for  RGB  and  grayscale images (with and without
4927       alpha), but not for paletted images nor for any images with bit  depths
4928       less than 8 bits/pixel.
4929
4930       The  'method'  parameter  sets the main filtering method, which is cur‐
4931       rently only '0' in the PNG 1.2 specification.  The 'filters'  parameter
4932       sets which filter(s), if any, should be used for each scanline.  Possi‐
4933       ble values are PNG_ALL_FILTERS and PNG_NO_FILTERS to turn filtering  on
4934       and off, respectively.
4935
4936       Individual  filter  types are PNG_FILTER_NONE, PNG_FILTER_SUB, PNG_FIL‐
4937       TER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which  can  be  bitwise  ORed
4938       together with '|' to specify one or more filters to use.  These filters
4939       are described in more detail in the PNG specification.  If  you  intend
4940       to  change  the filter type during the course of writing the image, you
4941       should start with flags set for all of the filters you intend to use so
4942       that  libpng  can  initialize its internal structures appropriately for
4943       all of the filter types.  (Note that this  means  the  first  row  must
4944       always  be adaptively filtered, because libpng currently does not allo‐
4945       cate the filter buffers until png_write_row() is called for  the  first
4946       time.)
4947
4948           filters = PNG_FILTER_NONE | PNG_FILTER_SUB
4949                     PNG_FILTER_UP | PNG_FILTER_AVG |
4950                     PNG_FILTER_PAETH | PNG_ALL_FILTERS;
4951
4952           png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE,
4953              filters);
4954                     The second parameter can also be
4955                     PNG_INTRAPIXEL_DIFFERENCING if you are
4956                     writing a PNG to be embedded in a MNG
4957                     datastream.  This parameter must be the
4958                     same as the value of filter_method used
4959                     in png_set_IHDR().
4960
4961       It  is  also  possible  to  influence how libpng chooses from among the
4962       available filters.  This is done in one  or  both  of  two  ways  -  by
4963       telling  it  how important it is to keep the same filter for successive
4964       rows, and by telling it the relative computational costs  of  the  fil‐
4965       ters.
4966
4967           double weights[3] = {1.5, 1.3, 1.1},
4968              costs[PNG_FILTER_VALUE_LAST] =
4969              {1.0, 1.3, 1.3, 1.5, 1.7};
4970
4971           png_set_filter_heuristics(png_ptr,
4972              PNG_FILTER_HEURISTIC_WEIGHTED, 3,
4973              weights, costs);
4974
4975       The  weights  are  multiplying factors that indicate to libpng that the
4976       row filter should be the same for successive rows  unless  another  row
4977       filter  is  that  many  times  better than the previous filter.  In the
4978       above example, if the previous 3 filters were SUB, SUB, NONE,  the  SUB
4979       filter  could  have  a  "sum  of  absolute differences" 1.5 x 1.3 times
4980       higher than other filters and still be chosen, while  the  NONE  filter
4981       could  have a sum 1.1 times higher than other filters and still be cho‐
4982       sen.  Unspecified weights are  taken  to  be  1.0,  and  the  specified
4983       weights  should  probably  be  declining  like  those above in order to
4984       emphasize recent filters over older filters.
4985
4986       The filter costs specify for each filter type a relative decoding  cost
4987       to  be  considered when selecting row filters.  This means that filters
4988       with higher costs are less likely to be chosen over filters with  lower
4989       costs, unless their "sum of absolute differences" is that much smaller.
4990       The costs do not necessarily reflect the exact computational speeds  of
4991       the  various filters, since this would unduly influence the final image
4992       size.
4993
4994       Note that the numbers above were invented purely for this  example  and
4995       are  given only to help explain the function usage.  Little testing has
4996       been done to find optimum values for either the costs or the weights.
4997
4998
4999   Removing unwanted object code
5000       There are a bunch of #define's in pngconf.h that control what parts  of
5001       libpng  are  compiled.   All the defines end in _SUPPORTED.  If you are
5002       never going to use a capability, you can change the #define  to  #undef
5003       before recompiling libpng and save yourself code and data space, or you
5004       can turn off individual  capabilities  with  defines  that  begin  with
5005       PNG_NO_.
5006
5007       In libpng-1.5.0 and later, the #define's are in pnglibconf.h instead.
5008
5009       You  can  also turn all of the transforms and ancillary chunk capabili‐
5010       ties off en masse with compiler directives that  define  PNG_NO_READ[or
5011       WRITE]_TRANSFORMS,  or  PNG_NO_READ[or  WRITE]_ANCILLARY_CHUNKS, or all
5012       four, along with directives to turn on any of the capabilities that you
5013       do  want.   The PNG_NO_READ[or WRITE]_TRANSFORMS directives disable the
5014       extra transformations but still leave  the  library  fully  capable  of
5015       reading  and writing PNG files with all known public chunks. Use of the
5016       PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS  directive  produces  a  library
5017       that  is  incapable of reading or writing ancillary chunks.  If you are
5018       not using the progressive reading capability, you  can  turn  that  off
5019       with  PNG_NO_PROGRESSIVE_READ  (don't confuse this with the INTERLACING
5020       capability, which you'll still have).
5021
5022       All the reading and writing specific code are in separate files, so the
5023       linker  should  only  grab the files it needs.  However, if you want to
5024       make sure, or if you are building a stand alone library, all the  read‐
5025       ing  files  start  with  "pngr"  and  all  the writing files start with
5026       "pngw".  The files that don't match  either  (like  png.c,  pngtrans.c,
5027       etc.)   are  used  for  both reading and writing, and always need to be
5028       included.  The progressive reader is in pngpread.c
5029
5030       If you are creating or distributing a dynamically linked library (a .so
5031       or  DLL  file),  you  should  not  remove  or  disable any parts of the
5032       library, as this will cause applications linked with different versions
5033       of  the  library  to  fail if they call functions not available in your
5034       library.  The size of the  library  itself  should  not  be  an  issue,
5035       because  only those sections that are actually used will be loaded into
5036       memory.
5037
5038
5039   Requesting debug printout
5040       The macro definition PNG_DEBUG can be used to request debugging  print‐
5041       out.   Set  it to an integer value in the range 0 to 3.  Higher numbers
5042       result in increasing amounts of debugging information.  The information
5043       is  printed to the "stderr" file, unless another file name is specified
5044       in the PNG_DEBUG_FILE macro definition.
5045
5046       When PNG_DEBUG > 0, the following functions (macros) become available:
5047
5048          png_debug(level, message)
5049          png_debug1(level, message, p1)
5050          png_debug2(level, message, p1, p2)
5051
5052       in which "level" is compared to PNG_DEBUG to decide  whether  to  print
5053       the  message,  "message"  is the formatted string to be printed, and p1
5054       and p2 are parameters that are to be embedded in the  string  according
5055       to printf-style formatting directives.  For example,
5056
5057          png_debug1(2, "foo=%d0, foo);
5058
5059       is expanded to
5060
5061          if (PNG_DEBUG > 2)
5062             fprintf(PNG_DEBUG_FILE, "foo=%d0, foo);
5063
5064       When  PNG_DEBUG  is defined but is zero, the macros aren't defined, but
5065       you can still use PNG_DEBUG to control your own debugging:
5066
5067          #ifdef PNG_DEBUG
5068              fprintf(stderr, ...
5069          #endif
5070
5071       When PNG_DEBUG = 1, the macros are defined, but only  png_debug  state‐
5072       ments  having  level = 0 will be printed.  There aren't any such state‐
5073       ments in this version of libpng, but if you insert some  they  will  be
5074       printed.
5075
5076

VI. MNG support

5078       The  MNG  specification  (available  at  http://www.libpng.org/pub/mng)
5079       allows certain extensions to PNG for PNG images that  are  embedded  in
5080       MNG  datastreams.   Libpng  can  support  some of these extensions.  To
5081       enable them, use the png_permit_mng_features() function:
5082
5083          feature_set = png_permit_mng_features(png_ptr, mask)
5084
5085          mask is a png_uint_32 containing the bitwise OR of the
5086               features you want to enable.  These include
5087               PNG_FLAG_MNG_EMPTY_PLTE
5088               PNG_FLAG_MNG_FILTER_64
5089               PNG_ALL_MNG_FEATURES
5090
5091          feature_set is a png_uint_32 that is the bitwise AND of
5092             your mask with the set of MNG features that is
5093             supported by the version of libpng that you are using.
5094
5095       It is an error to use this function when reading or  writing  a  stand‐
5096       alone  PNG file with the PNG 8-byte signature.  The PNG datastream must
5097       be wrapped in a MNG datastream.  As a minimum, it  must  have  the  MNG
5098       8-byte signature and the MHDR and MEND chunks.  Libpng does not provide
5099       support for these or any other MNG chunks; your application  must  pro‐
5100       vide  its  own support for them.  You may wish to consider using libmng
5101       (available at http://www.libmng.com) instead.
5102
5103

VII. Changes to Libpng from version 0.88

5105       It should be noted that versions of libpng later than 0.96 are not dis‐
5106       tributed  by  the  original libpng author, Guy Schalnat, nor by Andreas
5107       Dilger, who had taken over from Guy during 1996 and 1997, and  distrib‐
5108       uted  versions  0.89  through 0.96, but rather by another member of the
5109       original PNG Group, Glenn Randers-Pehrson.  Guy and Andreas  are  still
5110       alive and well, but they have moved on to other things.
5111
5112       The    old    libpng   functions   png_read_init(),   png_write_init(),
5113       png_info_init(), png_read_destroy(), and png_write_destroy() have  been
5114       moved  to  PNG_INTERNAL in version 0.95 to discourage their use.  These
5115       functions will be removed from libpng version 1.4.0.
5116
5117       The preferred method of creating and initializing the libpng structures
5118       is  via  the  png_create_read_struct(),  png_create_write_struct(), and
5119       png_create_info_struct() because they isolate the size  of  the  struc‐
5120       tures  from  the  application,  allow  version error checking, and also
5121       allow the use of custom error handling routines during the  initializa‐
5122       tion, which the old functions do not.  The functions png_read_destroy()
5123       and png_write_destroy() do not actually free  the  memory  that  libpng
5124       allocated  for  these  structs,  but just reset the data structures, so
5125       they   can   be   used   instead   of   png_destroy_read_struct()   and
5126       png_destroy_write_struct()  if  you feel there is too much system over‐
5127       head allocating and freeing the png_struct for each image read.
5128
5129       Setting   the   error   callbacks   via   png_set_message_fn()   before
5130       png_read_init()  as was suggested in libpng-0.88 is no longer supported
5131       because this caused applications that do not use custom error functions
5132       to fail if the png_ptr was not initialized to zero.  It is still possi‐
5133       ble to set the error callbacks AFTER png_read_init(), or to change them
5134       with  png_set_error_fn(),  which  is essentially the same function, but
5135       with a new name to force compilation errors with applications that  try
5136       to use the old method.
5137
5138       Starting  with  version  1.0.7,  you  can find out which version of the
5139       library you are using at run-time:
5140
5141          png_uint_32 libpng_vn = png_access_version_number();
5142
5143       The number libpng_vn is constructed from the major version, minor  ver‐
5144       sion  with  leading  zero, and release number with leading zero, (e.g.,
5145       libpng_vn for version 1.0.7 is 10007).
5146
5147       Note that this function does not take a png_ptr, so  you  can  call  it
5148       before you've created one.
5149
5150       You  can also check which version of png.h you used when compiling your
5151       application:
5152
5153          png_uint_32 application_vn = PNG_LIBPNG_VER;
5154
5155

VIII. Changes to Libpng from version 1.0.x to 1.2.x

5157       Support for user memory management was enabled by default.   To  accom‐
5158       plish   this,   the   functions   png_create_read_struct_2(),  png_cre‐
5159       ate_write_struct_2(),  png_set_mem_fn(),  png_get_mem_ptr(),   png_mal‐
5160       loc_default(), and png_free_default() were added.
5161
5162       Support  for  the  iTXt chunk has been enabled by default as of version
5163       1.2.41.
5164
5165       Support for certain MNG features was enabled.
5166
5167       Support for numbered error messages was added.  However, we  never  got
5168       around   to  actually  numbering  the  error  messages.   The  function
5169       png_set_strip_error_numbers() was added (Note: the prototype  for  this
5170       function  was inadvertently removed from png.h in PNG_NO_ASSEMBLER_CODE
5171       builds of libpng-1.2.15.  It was restored in libpng-1.2.36).
5172
5173       The png_malloc_warn() function was added at libpng-1.2.3.  This  issues
5174       a  png_warning  and  returns  NULL instead of aborting when it fails to
5175       acquire the requested memory allocation.
5176
5177       Support for setting user limits on image width and height  was  enabled
5178       by       default.        The      functions      png_set_user_limits(),
5179       png_get_user_width_max(), and png_get_user_height_max() were  added  at
5180       libpng-1.2.6.
5181
5182       The png_set_add_alpha() function was added at libpng-1.2.7.
5183
5184       The    function    png_set_expand_gray_1_2_4_to_8()    was   added   at
5185       libpng-1.2.9.  Unlike png_set_gray_1_2_4_to_8(), the new function  does
5186       not expand the tRNS chunk to alpha. The png_set_gray_1_2_4_to_8() func‐
5187       tion is deprecated.
5188
5189       A number of macro definitions in support of runtime selection of assem‐
5190       bler  code  features  (especially Intel MMX code support) were added at
5191       libpng-1.2.0:
5192
5193           PNG_ASM_FLAG_MMX_SUPPORT_COMPILED
5194           PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
5195           PNG_ASM_FLAG_MMX_READ_COMBINE_ROW
5196           PNG_ASM_FLAG_MMX_READ_INTERLACE
5197           PNG_ASM_FLAG_MMX_READ_FILTER_SUB
5198           PNG_ASM_FLAG_MMX_READ_FILTER_UP
5199           PNG_ASM_FLAG_MMX_READ_FILTER_AVG
5200           PNG_ASM_FLAG_MMX_READ_FILTER_PAETH
5201           PNG_ASM_FLAGS_INITIALIZED
5202           PNG_MMX_READ_FLAGS
5203           PNG_MMX_FLAGS
5204           PNG_MMX_WRITE_FLAGS
5205           PNG_MMX_FLAGS
5206
5207       We added the following functions in support  of  runtime  selection  of
5208       assembler code features:
5209
5210           png_get_mmx_flagmask()
5211           png_set_mmx_thresholds()
5212           png_get_asm_flags()
5213           png_get_mmx_bitdepth_threshold()
5214           png_get_mmx_rowbytes_threshold()
5215           png_set_asm_flags()
5216
5217       We  replaced all of these functions with simple stubs in libpng-1.2.20,
5218       when the Intel assembler code was removed due to a licensing issue.
5219
5220       These macros are deprecated:
5221
5222           PNG_READ_TRANSFORMS_NOT_SUPPORTED
5223           PNG_PROGRESSIVE_READ_NOT_SUPPORTED
5224           PNG_NO_SEQUENTIAL_READ_SUPPORTED
5225           PNG_WRITE_TRANSFORMS_NOT_SUPPORTED
5226           PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED
5227           PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED
5228
5229       They have been replaced, respectively, by:
5230
5231           PNG_NO_READ_TRANSFORMS
5232           PNG_NO_PROGRESSIVE_READ
5233           PNG_NO_SEQUENTIAL_READ
5234           PNG_NO_WRITE_TRANSFORMS
5235           PNG_NO_READ_ANCILLARY_CHUNKS
5236           PNG_NO_WRITE_ANCILLARY_CHUNKS
5237
5238       PNG_MAX_UINT was replaced with PNG_UINT_31_MAX.  It has been deprecated
5239       since libpng-1.0.16 and libpng-1.2.6.
5240
5241       The function
5242           png_check_sig(sig, num) was replaced with
5243           !png_sig_cmp(sig, 0, num) It has been deprecated since libpng-0.90.
5244
5245       The function
5246           png_set_gray_1_2_4_to_8()  which  also  expands  tRNS  to alpha was
5247       replaced with
5248           png_set_expand_gray_1_2_4_to_8() which does not. It has been depre‐
5249       cated since libpng-1.0.18 and 1.2.9.
5250
5251

IX. Changes to Libpng from version 1.0.x/1.2.x to 1.4.x

5253       Private  libpng  prototypes and macro definitions were moved from png.h
5254       and pngconf.h into a new pngpriv.h header file.
5255
5256       Functions     png_set_benign_errors(),     png_benign_error(),      and
5257       png_chunk_benign_error() were added.
5258
5259       Support  for  setting the maximum amount of memory that the application
5260       will allocate for reading chunks was added, as a security measure.  The
5261       functions  png_set_chunk_cache_max() and png_get_chunk_cache_max() were
5262       added to the library.
5263
5264       We implemented support for I/O states by adding png_ptr member io_state
5265       and   functions   png_get_io_chunk_name()   and  png_get_io_state()  in
5266       pngget.c
5267
5268       We added PNG_TRANSFORM_GRAY_TO_RGB to the  available  high-level  input
5269       transforms.
5270
5271       Checking  for  and  reporting of errors in the IHDR chunk is more thor‐
5272       ough.
5273
5274       Support for global arrays was removed, to improve thread safety.
5275
5276       Some obsolete/deprecated macros and functions have been removed.
5277
5278       Typecasted NULL definitions such as
5279          #define png_voidp_NULL            (png_voidp)NULL  were  eliminated.
5280       If you used these in your application, just use NULL instead.
5281
5282       The  png_struct and info_struct members "trans" and "trans_values" were
5283       changed to "trans_alpha" and "trans_color", respectively.
5284
5285       The obsolete, unused pnggccrd.c and pngvcrd.c files and  related  make‐
5286       files were removed.
5287
5288       The PNG_1_0_X and PNG_1_2_X macros were eliminated.
5289
5290       The PNG_LEGACY_SUPPORTED macro was eliminated.
5291
5292       Many WIN32_WCE #ifdefs were removed.
5293
5294       The    functions   png_read_init(info_ptr),   png_write_init(info_ptr),
5295       png_info_init(info_ptr),  png_read_destroy(),  and  png_write_destroy()
5296       have been removed.  They have been deprecated since libpng-0.95.
5297
5298       The  png_permit_empty_plte()  was removed. It has been deprecated since
5299       libpng-1.0.9.  Use png_permit_mng_features() instead.
5300
5301       We  removed  the  obsolete   stub   functions   png_get_mmx_flagmask(),
5302       png_set_mmx_thresholds(),     png_get_asm_flags(),     png_get_mmx_bit‐
5303       depth_threshold(),                    png_get_mmx_rowbytes_threshold(),
5304       png_set_asm_flags(), and png_mmx_supported()
5305
5306       We   removed  the  obsolete  png_check_sig(),  png_memcpy_check(),  and
5307       png_memset_check() functions.   Instead  use  !png_sig_cmp(),  png_mem‐
5308       cpy(), and png_memset(), respectively.
5309
5310       The  function png_set_gray_1_2_4_to_8() was removed. It has been depre‐
5311       cated  since  libpng-1.0.18  and  1.2.9,  when  it  was  replaced  with
5312       png_set_expand_gray_1_2_4_to_8()   because  the  former  function  also
5313       expanded any tRNS chunk to an alpha channel.
5314
5315       Macros for png_get_uint_16, png_get_uint_32,  and  png_get_int_32  were
5316       added  and  are used by default instead of the corresponding functions.
5317       Unfortunately, from libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro
5318       (but   not   the   function)  incorrectly  returned  a  value  of  type
5319       png_uint_32.
5320
5321       We changed the prototype for png_malloc() from
5322           png_malloc(png_structp png_ptr, png_uint_32 size) to
5323           png_malloc(png_structp png_ptr, png_alloc_size_t size)
5324
5325       This also applies to  the  prototype  for  the  user  replacement  mal‐
5326       loc_fn().
5327
5328       The  png_calloc()  function  was  added  and  is  used  in  place of of
5329       "png_malloc(); memset();" except in the case  in  png_read_png()  where
5330       the array consists of pointers; in this case a "for" loop is used after
5331       the png_malloc() to set the pointers to NULL, to give robust.  behavior
5332       in  case  the  application  runs  out  of  memory  part-way through the
5333       process.
5334
5335       We changed  the  prototypes  of  png_get_compression_buffer_size()  and
5336       png_set_compression_buffer_size()  to  work  with png_size_t instead of
5337       png_uint_32.
5338
5339       Support for numbered error messages was removed by  default,  since  we
5340       never got around to actually numbering the error messages. The function
5341       png_set_strip_error_numbers() was removed from the library by default.
5342
5343       The png_zalloc() and png_zfree() functions are no longer exported.  The
5344       png_zalloc()  function  no  longer  zeroes out the memory that it allo‐
5345       cates.  Applications that called png_zalloc(png_ptr, number, size)  can
5346       call  png_calloc(png_ptr, number*size) instead, and can call png_free()
5347       instead of png_zfree().
5348
5349       Support for dithering was disabled by default in libpng-1.4.0,  because
5350       it  has  not  been well tested and doesn't actually "dither".  The code
5351       was not removed, however, and could be enabled by building libpng  with
5352       PNG_READ_DITHER_SUPPORTED  defined.   In libpng-1.4.2, this support was
5353       reenabled, but the function was renamed png_set_quantize()  to  reflect
5354       more  accurately  what  it  actually  does.   At  the  same  time,  the
5355       PNG_DITHER_[RED,GREEN_BLUE]_BITS macros were also renamed to  PNG_QUAN‐
5356       TIZE_[RED,GREEN,BLUE]_BITS,  and  PNG_READ_DITHER_SUPPORTED was renamed
5357       to PNG_READ_QUANTIZE_SUPPORTED.
5358
5359       We removed the trailing '.' from the warning and error messages.
5360
5361

X. Changes to Libpng from version 1.4.x to 1.5.x

5363       From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but  not  the
5364       function) incorrectly returned a value of type png_uint_32.
5365
5366       Checking for invalid palette index on read or write was added at libpng
5367       1.5.10.  When an invalid index is found, libpng issues a benign  error.
5368       This is enabled by default but can be disabled in each png_ptr with
5369
5370          png_set_check_for_invalid_index(png_ptr, allowed);
5371
5372             allowed  - one of
5373                        0: disable
5374                        1: enable
5375
5376       A. Changes that affect users of libpng
5377
5378       There  are  no substantial API changes between the non-deprecated parts
5379       of the 1.4.5 API and the 1.5.0 API; however, the  ability  to  directly
5380       access  members  of  the main libpng control structures, png_struct and
5381       png_info, deprecated in earlier versions of libpng, has been completely
5382       removed from libpng 1.5.
5383
5384       We no longer include zlib.h in png.h.  Applications that need access to
5385       information in zlib.h will need to add the '#include  "zlib.h"'  direc‐
5386       tive.   It  does  not matter whether it is placed prior to or after the
5387       '"#include png.h"' directive.
5388
5389       The png_sprintf(), png_strcpy(), and png_strncpy() macros are no longer
5390       used and were removed.
5391
5392       We moved the png_strlen(), png_memcpy(), png_memset(), and png_memcmp()
5393       macros into a private header file (pngpriv.h) that is not accessible to
5394       applications.
5395
5396       In  png_get_iCCP,  the type of "profile" was changed from png_charpp to
5397       png_bytepp, and in png_set_iCCP, from png_charp to png_const_bytep.
5398
5399       There are changes of form in png.h, including new and changed macros to
5400       declare  parts  of the API.  Some API functions with arguments that are
5401       pointers to data not modified within the function have  been  corrected
5402       to declare these arguments with PNG_CONST.
5403
5404       Much  of  the internal use of C macros to control the library build has
5405       also changed and some of this is visible in the exported header  files,
5406       in  particular the use of macros to control data and API elements visi‐
5407       ble during application compilation may require significant revision  to
5408       application  code.   (It  is  extremely  rare  for an application to do
5409       this.)
5410
5411       Any program that compiled against libpng 1.4 and did not use deprecated
5412       features  or access internal library structures should compile and work
5413       against libpng  1.5,  except  for  the  change  in  the  prototype  for
5414       png_get_iCCP() and png_set_iCCP() API functions mentioned above.
5415
5416       libpng  1.5.0  adds PNG_ PASS macros to help in the reading and writing
5417       of interlaced images.  The macros return the number of rows and columns
5418       in  each  pass and information that can be used to de-interlace and (if
5419       absolutely necessary) interlace an image.
5420
5421       libpng 1.5.0 adds an API png_longjmp(png_ptr, value).  This  API  calls
5422       the  application-provided png_longjmp_ptr on the internal, but applica‐
5423       tion initialized, longjmp buffer.  It is provided as a  convenience  to
5424       avoid  the  need to use the png_jmpbuf macro, which had the unnecessary
5425       side effect of resetting the internal png_longjmp_ptr value.
5426
5427       libpng 1.5.0 includes a complete fixed point API.  By default  this  is
5428       present  along  with  the corresponding floating point API.  In general
5429       the fixed point API is faster and smaller than the floating  point  one
5430       because the PNG file format used fixed point, not floating point.  This
5431       applies even if the library uses floating point  in  internal  calcula‐
5432       tions.  A new macro, PNG_FLOATING_ARITHMETIC_SUPPORTED, reveals whether
5433       the library uses floating point arithmetic (the default) or fixed point
5434       arithmetic  internally  for  performance  critical calculations such as
5435       gamma correction.  In some cases, the gamma  calculations  may  produce
5436       slightly   different   results.    This  has  changed  the  results  in
5437       png_rgb_to_gray and in alpha composition (png_set_background for  exam‐
5438       ple). This applies even if the original image was already linear (gamma
5439       == 1.0) and, therefore, it is not necessary  to  linearize  the  image.
5440       This  is  because  libpng  has *not* been changed to optimize that case
5441       correctly, yet.
5442
5443       Fixed point support for the sCAL chunk comes with an important  caveat;
5444       the sCAL specification uses a decimal encoding of floating point values
5445       and the accuracy of PNG fixed point values is insufficient  for  repre‐
5446       sentation  of these values. Consequently a "string" API (png_get_sCAL_s
5447       and png_set_sCAL_s) is the only reliable way of reading arbitrary  sCAL
5448       chunks  in  the  absence  of  either the floating point API or internal
5449       floating point calculations.
5450
5451       Applications no longer need to include the optional distribution header
5452       file  pngusr.h  or  define  the corresponding macros during application
5453       build in order to see the correct variant  of  the  libpng  API.   From
5454       1.5.0  application  code  can  check  for  the corresponding _SUPPORTED
5455       macro:
5456
5457       #ifdef PNG_INCH_CONVERSIONS_SUPPORTED
5458          /* code that uses the inch conversion APIs. */ #endif
5459
5460       This macro will only be defined if the inch conversion  functions  have
5461       been  compiled into libpng.  The full set of macros, and whether or not
5462       support has been compiled in, are available in the header file  pnglib‐
5463       conf.h.  This header file is specific to the libpng build.  Notice that
5464       prior to 1.5.0 the _SUPPORTED macros would always have the default def‐
5465       inition  unless  reset  by pngusr.h or by explicit settings on the com‐
5466       piler command line.  These settings may produce  compiler  warnings  or
5467       errors in 1.5.0 because of macro redefinition.
5468
5469       From  libpng-1.4.0  until 1.4.4, the png_get_uint_16 macro (but not the
5470       function) incorrectly returned a value  of  type  png_uint_32.   libpng
5471       1.5.0  is  consistent with the implementation in 1.4.5 and 1.2.x (where
5472       the macro did not exist.)
5473
5474       Applications can now choose whether to use these macros or to call  the
5475       corresponding    function    by    defining    PNG_USE_READ_MACROS   or
5476       PNG_NO_USE_READ_MACROS before including png.h.   Notice  that  this  is
5477       only  supported  from  1.5.0  -defining PNG_NO_USE_READ_MACROS prior to
5478       1.5.0 will lead to a link failure.
5479
5480       Prior to libpng-1.5.4, the zlib compressor used the same set of parame‐
5481       ters  when  compressing the IDAT data and textual data such as zTXt and
5482       iCCP.  In libpng-1.5.4 we reinitialized the zlib stream for  each  type
5483       of  data.   We  added  five  png_set_text_*() functions for setting the
5484       parameters to use with textual data.
5485
5486       Prior to  libpng-1.5.4,  the  PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
5487       option  was  off  by default, and slightly inaccurate scaling occurred.
5488       This option can no longer be turned off, and the choice of accurate  or
5489       inaccurate  16-to-8 scaling is by using the new png_set_scale_16_to_8()
5490       API for accurate scaling or the  old  png_set_strip_16_to_8()  API  for
5491       simple chopping.
5492
5493       Prior to libpng-1.5.4, the png_set_user_limits() function could only be
5494       used  to  reduce  the  width  and  height  limits  from  the  value  of
5495       PNG_USER_WIDTH_MAX and PNG_USER_HEIGHT_MAX, although this document said
5496       that it could be used to override them.  Now this function will  reduce
5497       or increase the limits.
5498
5499       Starting in libpng-1.5.10, the user limits can be set en masse with the
5500       configuration option  PNG_SAFE_LIMITS_SUPPORTED.   If  this  option  is
5501       enabled,  a set of "safe" limits is applied in pngpriv.h.  These can be
5502       overridden   by    application    calls    to    png_set_user_limits(),
5503       png_set_user_chunk_cache_max(),  and/or  png_set_user_malloc_max() that
5504       increase or decrease the limits.  Also, in  libpng-1.5.10  the  default
5505       width  and  height  limits  were  increased from 1,000,000 to 0x7ffffff
5506       (i.e., made unlimited).  Therefore, the limits are now
5507                                      default      safe
5508          png_user_width_max        0x7fffffff    1,000,000
5509          png_user_height_max       0x7fffffff    1,000,000
5510          png_user_chunk_cache_max  0 (unlimited)   128
5511          png_user_chunk_malloc_max 0 (unlimited) 8,000,000
5512
5513       B. Changes to the build and configuration of libpng
5514
5515       Details of internal changes to the library code can  be  found  in  the
5516       CHANGES  file and in the GIT repository logs.  These will be of no con‐
5517       cern to the vast majority of library users or  builders;  however,  the
5518       few  who  configure  libpng  to  a  non-default feature set may need to
5519       change how this is done.
5520
5521       There should be no need for library builders to alter build scripts  if
5522       these  use the distributed build support - configure or the makefiles -
5523       however, users of the makefiles may care to update their build  scripts
5524       to build pnglibconf.h where the corresponding makefile does not do so.
5525
5526       Building  libpng  with  a  non-default  configuration  has changed com‐
5527       pletely.  The old method using pngusr.h  should  still  work  correctly
5528       even  though  the  way  pngusr.h is used in the build has been changed;
5529       however, library builders will probably want to examine the changes  to
5530       take advantage of new capabilities and to simplify their build system.
5531
5532       B.1 Specific changes to library configuration capabilities
5533
5534       The  library now supports a complete fixed point implementation and can
5535       thus be used on systems that have no floating  point  support  or  very
5536       limited  or  slow  support.   Previously gamma correction, an essential
5537       part of complete PNG support, required reasonably fast floating point.
5538
5539       As part of this the choice of internal  implementation  has  been  made
5540       independent  of  the choice of fixed versus floating point APIs and all
5541       the missing fixed point APIs have been implemented.
5542
5543       The exact mechanism used to control attributes  of  API  functions  has
5544       changed.   A  single  set of operating system independent macro defini‐
5545       tions is used and operating system specific directives are  defined  in
5546       pnglibconf.h
5547
5548       As  part  of this the mechanism used to choose procedure call standards
5549       on those systems that allow a choice has been changed.  At present this
5550       only  affects certain Microsoft (DOS, Windows) and IBM (OS/2) operating
5551       systems running on Intel processors.   As  before,  PNGAPI  is  defined
5552       where  required to control the exported API functions; however, two new
5553       macros, PNGCBAPI and PNGCAPI, are used instead for  callback  functions
5554       (PNGCBAPI) and (PNGCAPI) for functions that must match a C library pro‐
5555       totype (currently only png_longjmp_ptr, which must match the C  longjmp
5556       function.)  The new approach is documented in pngconf.h
5557
5558       Despite these changes, libpng 1.5.0 only supports the native C function
5559       calling standard on those platforms tested so far (__cdecl on Microsoft
5560       Windows).   This  is  because  the support requirements for alternative
5561       calling conventions seem to no longer exist.  Developers  who  find  it
5562       necessary to set PNG_API_RULE to 1 should advise the mailing list (png-
5563       mng-implement) of this and library  builders  who  use  Openwatcom  and
5564       therefore set PNG_API_RULE to 2 should also contact the mailing list.
5565
5566       A new test program, pngvalid, is provided in addition to pngtest.  png‐
5567       valid validates the arithmetic accuracy of the gamma correction  calcu‐
5568       lations  and  includes  a  number of validations of the file format.  A
5569       subset of the full range of tests is run when "make check" is done  (in
5570       the  'configure'  build.)   pngvalid also allows total allocated memory
5571       usage to be evaluated and performs additional memory overwrite  valida‐
5572       tion.
5573
5574       Many changes to individual feature macros have been made. The following
5575       are the changes most likely to be noticed by library builders who  con‐
5576       figure libpng:
5577
5578       1) All feature macros now have consistent naming:
5579
5580       #define  PNG_NO_feature  turns the feature off #define PNG_feature_SUP‐
5581       PORTED turns the feature on
5582
5583       pnglibconf.h contains one line for each feature macro which is either:
5584
5585       #define PNG_feature_SUPPORTED
5586
5587       if the feature is supported or:
5588
5589       /*#undef PNG_feature_SUPPORTED*/
5590
5591       if it is not.  Library code consistently  checks  for  the  'SUPPORTED'
5592       macro.   It does not, and libpng applications should not, check for the
5593       'NO' macro which will not normally be defined even if  the  feature  is
5594       not supported.  The 'NO' macros are only used internally for setting or
5595       not setting the corresponding 'SUPPORTED' macros.
5596
5597       Compatibility with the old names is provided as follows:
5598
5599       PNG_INCH_CONVERSIONS turns on PNG_INCH_CONVERSIONS_SUPPORTED
5600
5601       And the following definitions disable the corresponding feature:
5602
5603       PNG_SETJMP_NOT_SUPPORTED disables  SETJMP  PNG_READ_TRANSFORMS_NOT_SUP‐
5604       PORTED  disables  READ_TRANSFORMS PNG_NO_READ_COMPOSITED_NODIV disables
5605       READ_COMPOSITE_NODIV    PNG_WRITE_TRANSFORMS_NOT_SUPPORTED     disables
5606       WRITE_TRANSFORMS    PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED    disables
5607       READ_ANCILLARY_CHUNKS PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED disables
5608       WRITE_ANCILLARY_CHUNKS
5609
5610       Library builders should remove use of the above, inconsistent, names.
5611
5612       2)  Warning  and error message formatting was previously conditional on
5613       the STDIO feature. The library has been changed to use  the  CONSOLE_IO
5614       feature  instead. This means that if CONSOLE_IO is disabled the library
5615       no longer  uses  the  printf(3)  functions,  even  though  the  default
5616       read/write implementations use (FILE) style stdio.h functions.
5617
5618       3) Three feature macros now control the fixed/floating point decisions:
5619
5620       PNG_FLOATING_POINT_SUPPORTED enables the floating point APIs
5621
5622       PNG_FIXED_POINT_SUPPORTED  enables  the  fixed  point APIs; however, in
5623       practice these are normally required internally anyway (because the PNG
5624       file format is fixed point), therefore in most cases PNG_NO_FIXED_POINT
5625       merely stops the function from being exported.
5626
5627       PNG_FLOATING_ARITHMETIC_SUPPORTED chooses between the internal floating
5628       point implementation or the fixed point one.  Typically the fixed point
5629       implementation is larger and slower than the floating point implementa‐
5630       tion  on  a  system  that  supports  floating point; however, it may be
5631       faster on a system which lacks floating point  hardware  and  therefore
5632       uses a software emulation.
5633
5634       4)  Added  PNG_{READ,WRITE}_INT_FUNCTIONS_SUPPORTED.   This  allows the
5635       functions to read and  write  ints  to  be  disabled  independently  of
5636       PNG_USE_READ_MACROS, which allows libpng to be built with the functions
5637       even though the default is to use the macros - this allows applications
5638       to  choose  at  app  buildtime whether or not to use macros (previously
5639       impossible because the functions weren't in the default build.)
5640
5641       B.2 Changes to the configuration mechanism
5642
5643       Prior to libpng-1.5.0 library builders who needed to  configure  libpng
5644       had  either  to modify the exported pngconf.h header file to add system
5645       specific configuration or had to write feature  selection  macros  into
5646       pngusr.h  and  cause  this  to  be  included into pngconf.h by defining
5647       PNG_USER_CONFIG. The latter mechanism  had  the  disadvantage  that  an
5648       application built without PNG_USER_CONFIG defined would see the unmodi‐
5649       fied, default, libpng API and thus would probably fail to link.
5650
5651       These mechanisms still work in the configure build and in any  makefile
5652       build  that  builds pnglibconf.h, although the feature selection macros
5653       have changed somewhat as described above.  In 1.5.0, however,  pngusr.h
5654       is  processed  only once, when the exported header file pnglibconf.h is
5655       built.  pngconf.h no longer includes pngusr.h,  therefore  pngusr.h  is
5656       ignored  after the build of pnglibconf.h and it is never included in an
5657       application build.
5658
5659       The rarely used alternative of adding a list of feature macros  to  the
5660       CFLAGS  setting in the build also still works; however, the macros will
5661       be copied to pnglibconf.h and this may produce macro redefinition warn‐
5662       ings when the individual C files are compiled.
5663
5664       All  configuration  now  only  works  if  pnglibconf.h  is  built  from
5665       scripts/pnglibconf.dfa.   This  requires  the   program   awk.    Brian
5666       Kernighan  (the original author of awk) maintains C source code of that
5667       awk and this and all known later implementations (often called by  sub‐
5668       tly  different names - nawk and gawk for example) are adequate to build
5669       pnglibconf.h.  The Sun Microsystems (now Oracle) program  'awk'  is  an
5670       earlier version and does not work; this may also apply to other systems
5671       that have a functioning awk called 'nawk'.
5672
5673       Configuration options are  now  documented  in  scripts/pnglibconf.dfa.
5674       This  file also includes dependency information that ensures a configu‐
5675       ration is consistent; that is, if a feature is switched  off  dependent
5676       features  are also removed.  As a recommended alternative to using fea‐
5677       ture macros in pngusr.h a system builder  may  also  define  equivalent
5678       options  in  pngusr.dfa (or, indeed, any file) and add that to the con‐
5679       figuration by setting DFA_XTRA to the file name.  The makefiles in con‐
5680       trib/pngminim  illustrate  how to do this, and a case where pngusr.h is
5681       still required.
5682
5683

XI. Detecting libpng

5685       The png_get_io_ptr() function has been present since  libpng-0.88,  has
5686       never changed, and is unaffected by conditional compilation macros.  It
5687       is the best choice for use in configure scripts for detecting the pres‐
5688       ence  of  any libpng version since 0.88.  In an autoconf "configure.in"
5689       you could use
5690
5691           AC_CHECK_LIB(png, png_get_io_ptr, ...
5692
5693

XII. Source code repository

5695       Since about February 2009, version 1.2.34, libpng has been under  "git"
5696       source  control.   The  git  repository  was  built  from  old  libpng-
5697       x.y.z.tar.gz files going back to version 0.70.  You can access the  git
5698       repository (read only) at
5699
5700           git://libpng.git.sourceforge.net/gitroot/libpng
5701
5702       or you can browse it via "gitweb" at
5703
5704           http://libpng.git.sourceforge.net/git/gitweb.cgi?p=libpng
5705
5706       Patches  can be sent to glennrp at users.sourceforge.net or to png-mng-
5707       implement at lists.sourceforge.net or you can upload them to the libpng
5708       bug tracker at
5709
5710           http://libpng.sourceforge.net
5711
5712       We  also  accept  patches  built from the tar or zip distributions, and
5713       simple verbal discriptions of bug fixes, reported either to the Source‐
5714       Forge  bug  tracker,  to  the png-mng-implement at lists.sf.net mailing
5715       list, or directly to glennrp.
5716
5717

XIII. Coding style

5719       Our coding style is similar to the "Allman" style, with curly braces on
5720       separate lines:
5721
5722           if (condition)
5723           {
5724              action;
5725           }
5726
5727           else if (another condition)
5728           {
5729              another action;
5730           }
5731
5732       The braces can be omitted from simple one-line actions:
5733
5734           if (condition)
5735              return (0);
5736
5737       We  use  3-space indentation, except for continued statements which are
5738       usually indented the same as the first line of the statement plus  four
5739       more spaces.
5740
5741       For  macro  definitions  we use 2-space indentation, always leaving the
5742       "#" in the first column.
5743
5744           #ifndef PNG_NO_FEATURE
5745           #  ifndef PNG_FEATURE_SUPPORTED
5746           #    define PNG_FEATURE_SUPPORTED
5747           #  endif
5748           #endif
5749
5750       Comments appear with the leading "/*" at the same  indentation  as  the
5751       statement that follows the comment:
5752
5753           /* Single-line comment */
5754           statement;
5755
5756           /* This is a multiple-line
5757            * comment.
5758            */
5759           statement;
5760
5761       Very  short  comments  can  be placed after the end of the statement to
5762       which they pertain:
5763
5764           statement;    /* comment */
5765
5766       We don't use C++ style ("//") comments. We have, however, used them  in
5767       the past in some now-abandoned MMX assembler code.
5768
5769       Functions  and  their curly braces are not indented, and exported func‐
5770       tions are marked with PNGAPI:
5771
5772        /* This is a public function that is visible to
5773         * application programmers. It does thus-and-so.
5774         */
5775        void PNGAPI
5776        png_exported_function(png_ptr, png_info, foo)
5777        {
5778           body;
5779        }
5780
5781       The prototypes for all exported functions appear in  png.h,  above  the
5782       comment that says
5783
5784           /* Maintainer: Put new public prototypes here ... */
5785
5786       We mark all non-exported functions with "/* PRIVATE */"":
5787
5788        void /* PRIVATE */
5789        png_non_exported_function(png_ptr, png_info, foo)
5790        {
5791           body;
5792        }
5793
5794       The prototypes for non-exported functions (except for those in pngtest)
5795       appear in pngpriv.h above the comment that says
5796
5797         /* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */
5798
5799       To avoid polluting the global namespace,  the  names  of  all  exported
5800       functions  and  variables begin with "png_", and all publicly visible C
5801       preprocessor macros begin with "PNG".   We  request  that  applications
5802       that  use  libpng  *not*  begin any of their own symbols with either of
5803       these strings.
5804
5805       We put a space after each comma  and  after  each  semicolon  in  "for"
5806       statements,  and  we put spaces before and after each C binary operator
5807       and after "for" or "while", and before  "?".   We  don't  put  a  space
5808       between  a  typecast  and  the expression being cast, nor do we put one
5809       between a function name and the left parenthesis that follows it:
5810
5811           for (i = 2; i > 0; --i)
5812              y[i] = a(x) + (int)b;
5813
5814       We prefer #ifdef and #ifndef to #if defined() and  if  !defined()  when
5815       there is only one macro being tested.
5816
5817       We prefer to express integers that are used as bit masks in hex format,
5818       with an even  number  of  lower-case  hex  digits  (e.g.,  0x00,  0xff,
5819       0x0100).
5820
5821       We do not use the TAB character for indentation in the C sources.
5822
5823       Lines do not exceed 80 characters.
5824
5825       Other rules can be inferred by inspecting the libpng source.
5826
5827

XIV. Y2K Compliance in libpng

5829       September 27, 2012
5830
5831       Since  the  PNG  Development  group is an ad-hoc body, we can't make an
5832       official declaration.
5833
5834       This is your unofficial assurance that libpng  from  version  0.71  and
5835       upward  through 1.5.13 are Y2K compliant.  It is my belief that earlier
5836       versions were also Y2K compliant.
5837
5838       Libpng only has two year fields.  One is a 2-byte unsigned integer that
5839       will  hold years up to 65535.  The other holds the date in text format,
5840       and will hold years up to 9999.
5841
5842       The integer is
5843           "png_uint_16 year" in png_time_struct.
5844
5845       The string is
5846           "char time_buffer[29]" in png_struct.  This will no longer be  used
5847       in libpng-1.6.x and will be removed from libpng-1.7.0.
5848
5849       There are seven time-related functions:
5850
5851           png_convert_to_rfc_1123() in png.c
5852             (formerly png_convert_to_rfc_1152() in error)
5853           png_convert_from_struct_tm() in pngwrite.c, called
5854             in pngwrite.c
5855           png_convert_from_time_t() in pngwrite.c
5856           png_get_tIME() in pngget.c
5857           png_handle_tIME() in pngrutil.c, called in pngread.c
5858           png_set_tIME() in pngset.c
5859           png_write_tIME() in pngwutil.c, called in pngwrite.c
5860
5861       All appear to handle dates properly in a Y2K environment.  The png_con‐
5862       vert_from_time_t() function calls gmtime() to convert from system clock
5863       time,  which  returns  (year  - 1900), which we properly convert to the
5864       full 4-digit year.  There is  a  possibility  that  applications  using
5865       libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
5866       function, or that they are incorrectly  passing  only  a  2-digit  year
5867       instead  of  "year  - 1900" into the png_convert_from_struct_tm() func‐
5868       tion, but this is not under our control.  The libpng documentation  has
5869       always  stated that it works with 4-digit years, and the APIs have been
5870       documented as such.
5871
5872       The tIME chunk itself is also Y2K compliant.  It uses a 2-byte unsigned
5873       integer to hold the year, and can hold years as large as 65535.
5874
5875       zlib, upon which libpng depends, is also Y2K compliant.  It contains no
5876       date-related code.
5877
5878
5879          Glenn Randers-Pehrson
5880          libpng maintainer
5881          PNG Development Group
5882
5883

NOTE

5885       Note about libpng version numbers:
5886
5887       Due to various miscommunications, unforeseen code incompatibilities and
5888       occasional  factors  outside the authors' control, version numbering on
5889       the library has not always been consistent  and  straightforward.   The
5890       following  table  summarizes matters since version 0.89c, which was the
5891       first widely used release:
5892
5893        source             png.h  png.h  shared-lib
5894        version            string   int  version
5895        -------            ------  ----- ----------
5896        0.89c ("beta 3")  0.89       89  1.0.89
5897        0.90  ("beta 4")  0.90       90  0.90
5898        0.95  ("beta 5")  0.95       95  0.95
5899        0.96  ("beta 6")  0.96       96  0.96
5900        0.97b ("beta 7")  1.00.97    97  1.0.1
5901        0.97c             0.97       97  2.0.97
5902        0.98              0.98       98  2.0.98
5903        0.99              0.99       98  2.0.99
5904        0.99a-m           0.99       99  2.0.99
5905        1.00              1.00      100  2.1.0
5906        1.0.0             1.0.0     100  2.1.0
5907        1.0.0   (from here on, the  100  2.1.0
5908        1.0.1    png.h string is  10001  2.1.0
5909        1.0.1a-e identical to the 10002  from here on, the
5910        1.0.2    source version)  10002  shared library is 2.V
5911        1.0.2a-b                  10003  where V is the source
5912        1.0.1                     10001  code version except as
5913        1.0.1a-e                  10002  2.1.0.1a-e   noted.
5914        1.0.2                     10002  2.1.0.2
5915        1.0.2a-b                  10003  2.1.0.2a-b
5916        1.0.3                     10003  2.1.0.3
5917        1.0.3a-d                  10004  2.1.0.3a-d
5918        1.0.4                     10004  2.1.0.4
5919        1.0.4a-f                  10005  2.1.0.4a-f
5920        1.0.5 (+ 2 patches)       10005  2.1.0.5
5921        1.0.5a-d                  10006  2.1.0.5a-d
5922        1.0.5e-r                  10100  2.1.0.5e-r
5923        1.0.5s-v                  10006  2.1.0.5s-v
5924        1.0.6 (+ 3 patches)       10006  2.1.0.6
5925        1.0.6d-g                  10007  2.1.0.6d-g
5926        1.0.6h                    10007  10.6h
5927        1.0.6i                    10007  10.6i
5928        1.0.6j                    10007  2.1.0.6j
5929        1.0.7beta11-14    DLLNUM  10007  2.1.0.7beta11-14
5930        1.0.7beta15-18       1    10007  2.1.0.7beta15-18
5931        1.0.7rc1-2           1    10007  2.1.0.7rc1-2
5932        1.0.7                1    10007  2.1.0.7
5933        1.0.8beta1-4         1    10008  2.1.0.8beta1-4
5934        1.0.8rc1             1    10008  2.1.0.8rc1
5935        1.0.8                1    10008  2.1.0.8
5936        1.0.9beta1-6         1    10009  2.1.0.9beta1-6
5937        1.0.9rc1             1    10009  2.1.0.9rc1
5938        1.0.9beta7-10        1    10009  2.1.0.9beta7-10
5939        1.0.9rc2             1    10009  2.1.0.9rc2
5940        1.0.9                1    10009  2.1.0.9
5941        1.0.10beta1          1    10010  2.1.0.10beta1
5942        1.0.10rc1            1    10010  2.1.0.10rc1
5943        1.0.10               1    10010  2.1.0.10
5944        1.0.11beta1-3        1    10011  2.1.0.11beta1-3
5945        1.0.11rc1            1    10011  2.1.0.11rc1
5946        1.0.11               1    10011  2.1.0.11
5947        1.0.12beta1-2        2    10012  2.1.0.12beta1-2
5948        1.0.12rc1            2    10012  2.1.0.12rc1
5949        1.0.12               2    10012  2.1.0.12
5950        1.1.0a-f             -    10100  2.1.1.0a-f abandoned
5951        1.2.0beta1-2         2    10200  2.1.2.0beta1-2
5952        1.2.0beta3-5         3    10200  3.1.2.0beta3-5
5953        1.2.0rc1             3    10200  3.1.2.0rc1
5954        1.2.0                3    10200  3.1.2.0
5955        1.2.1beta-4          3    10201  3.1.2.1beta1-4
5956        1.2.1rc1-2           3    10201  3.1.2.1rc1-2
5957        1.2.1                3    10201  3.1.2.1
5958        1.2.2beta1-6        12    10202  12.so.0.1.2.2beta1-6
5959        1.0.13beta1         10    10013  10.so.0.1.0.13beta1
5960        1.0.13rc1           10    10013  10.so.0.1.0.13rc1
5961        1.2.2rc1            12    10202  12.so.0.1.2.2rc1
5962        1.0.13              10    10013  10.so.0.1.0.13
5963        1.2.2               12    10202  12.so.0.1.2.2
5964        1.2.3rc1-6          12    10203  12.so.0.1.2.3rc1-6
5965        1.2.3               12    10203  12.so.0.1.2.3
5966        1.2.4beta1-3        13    10204  12.so.0.1.2.4beta1-3
5967        1.2.4rc1            13    10204  12.so.0.1.2.4rc1
5968        1.0.14              10    10014  10.so.0.1.0.14
5969        1.2.4               13    10204  12.so.0.1.2.4
5970        1.2.5beta1-2        13    10205  12.so.0.1.2.5beta1-2
5971        1.0.15rc1           10    10015  10.so.0.1.0.15rc1
5972        1.0.15              10    10015  10.so.0.1.0.15
5973        1.2.5               13    10205  12.so.0.1.2.5
5974        1.2.6beta1-4        13    10206  12.so.0.1.2.6beta1-4
5975        1.2.6rc1-5          13    10206  12.so.0.1.2.6rc1-5
5976        1.0.16              10    10016  10.so.0.1.0.16
5977        1.2.6               13    10206  12.so.0.1.2.6
5978        1.2.7beta1-2        13    10207  12.so.0.1.2.7beta1-2
5979        1.0.17rc1           10    10017  12.so.0.1.0.17rc1
5980        1.2.7rc1            13    10207  12.so.0.1.2.7rc1
5981        1.0.17              10    10017  12.so.0.1.0.17
5982        1.2.7               13    10207  12.so.0.1.2.7
5983        1.2.8beta1-5        13    10208  12.so.0.1.2.8beta1-5
5984        1.0.18rc1-5         10    10018  12.so.0.1.0.18rc1-5
5985        1.2.8rc1-5          13    10208  12.so.0.1.2.8rc1-5
5986        1.0.18              10    10018  12.so.0.1.0.18
5987        1.2.8               13    10208  12.so.0.1.2.8
5988        1.2.9beta1-3        13    10209  12.so.0.1.2.9beta1-3
5989        1.2.9beta4-11       13    10209  12.so.0.9[.0]
5990        1.2.9rc1            13    10209  12.so.0.9[.0]
5991        1.2.9               13    10209  12.so.0.9[.0]
5992        1.2.10beta1-7       13    10210  12.so.0.10[.0]
5993        1.2.10rc1-2         13    10210  12.so.0.10[.0]
5994        1.2.10              13    10210  12.so.0.10[.0]
5995        1.4.0beta1-6        14    10400  14.so.0.0[.0]
5996        1.2.11beta1-4       13    10210  12.so.0.11[.0]
5997        1.4.0beta7-8        14    10400  14.so.0.0[.0]
5998        1.2.11              13    10211  12.so.0.11[.0]
5999        1.2.12              13    10212  12.so.0.12[.0]
6000        1.4.0beta9-14       14    10400  14.so.0.0[.0]
6001        1.2.13              13    10213  12.so.0.13[.0]
6002        1.4.0beta15-36      14    10400  14.so.0.0[.0]
6003        1.4.0beta37-87      14    10400  14.so.14.0[.0]
6004        1.4.0rc01           14    10400  14.so.14.0[.0]
6005        1.4.0beta88-109     14    10400  14.so.14.0[.0]
6006        1.4.0rc02-08        14    10400  14.so.14.0[.0]
6007        1.4.0               14    10400  14.so.14.0[.0]
6008        1.4.1beta01-03      14    10401  14.so.14.1[.0]
6009        1.4.1rc01           14    10401  14.so.14.1[.0]
6010        1.4.1beta04-12      14    10401  14.so.14.1[.0]
6011        1.4.1               14    10401  14.so.14.1[.0]
6012        1.4.2               14    10402  14.so.14.2[.0]
6013        1.4.3               14    10403  14.so.14.3[.0]
6014        1.4.4               14    10404  14.so.14.4[.0]
6015        1.5.0beta01-58      15    10500  15.so.15.0[.0]
6016        1.5.0rc01-07        15    10500  15.so.15.0[.0]
6017        1.5.0               15    10500  15.so.15.0[.0]
6018        1.5.1beta01-11      15    10501  15.so.15.1[.0]
6019        1.5.1rc01-02        15    10501  15.so.15.1[.0]
6020        1.5.1               15    10501  15.so.15.1[.0]
6021        1.5.2beta01-03      15    10502  15.so.15.2[.0]
6022        1.5.2rc01-03        15    10502  15.so.15.2[.0]
6023        1.5.2               15    10502  15.so.15.2[.0]
6024        1.5.3beta01-10      15    10503  15.so.15.3[.0]
6025        1.5.3rc01-02        15    10503  15.so.15.3[.0]
6026        1.5.3beta11         15    10503  15.so.15.3[.0]
6027        1.5.3 [omitted]
6028        1.5.4beta01-08      15    10504  15.so.15.4[.0]
6029        1.5.4rc01           15    10504  15.so.15.4[.0]
6030        1.5.4               15    10504  15.so.15.4[.0]
6031        1.5.5beta01-08      15    10505  15.so.15.5[.0]
6032        1.5.5rc01           15    10505  15.so.15.5[.0]
6033        1.5.5               15    10505  15.so.15.5[.0]
6034        1.5.6beta01-07      15    10506  15.so.15.6[.0]
6035        1.5.6rc01-03        15    10506  15.so.15.6[.0]
6036        1.5.6               15    10506  15.so.15.6[.0]
6037        1.5.7beta01-05      15    10507  15.so.15.7[.0]
6038        1.5.7rc01-03        15    10507  15.so.15.7[.0]
6039        1.5.7               15    10507  15.so.15.7[.0]
6040        1.5.8beta01         15    10508  15.so.15.8[.0]
6041        1.5.8rc01           15    10508  15.so.15.8[.0]
6042        1.5.8               15    10508  15.so.15.8[.0]
6043        1.5.9beta01-02      15    10509  15.so.15.9[.0]
6044        1.5.9rc01           15    10509  15.so.15.9[.0]
6045        1.5.9               15    10509  15.so.15.9[.0]
6046        1.5.10beta01-05     15    10510  15.so.15.10[.0]
6047        1.5.10              15    10510  15.so.15.10[.0]
6048        1.5.11beta01        15    10511  15.so.15.11[.0]
6049        1.5.11rc01-05       15    10511  15.so.15.11[.0]
6050        1.5.11              15    10511  15.so.15.11[.0]
6051        1.5.12              15    10512  15.so.15.12[.0]
6052        1.5.13beta01-02     15    10513  15.so.15.13[.0]
6053        1.5.13rc01          15    10513  15.so.15.13[.0]
6054        1.5.13              15    10513  15.so.15.13[.0]
6055
6056       Henceforth the source version will match the shared-library  minor  and
6057       patch numbers; the shared-library major version number will be used for
6058       changes  in  backward  compatibility,   as   it   is   intended.    The
6059       PNG_PNGLIB_VER  macro, which is not used within libpng but is available
6060       for applications, is an unsigned integer of the form xyyzz  correspond‐
6061       ing  to the source version x.y.z (leading zeros in y and z).  Beta ver‐
6062       sions were given the previous public  release  number  plus  a  letter,
6063       until  version 1.0.6j; from then on they were given the upcoming public
6064       release number plus "betaNN" or "rcN".
6065
6066

SEE ALSO

6068       png(5), libpngpf(3), zlib(3), deflate(5), and zlib(5)
6069
6070
6071       libpng:
6072
6073              http://libpng.sourceforge.net  (follow  the   [DOWNLOAD]   link)
6074              http://www.libpng.org/pub/png
6075
6076
6077       zlib:
6078
6079              (generally) at the same location as libpng or at
6080              ftp://ftp.info-zip.org/pub/infozip/zlib
6081
6082
6083       PNGspecification:RFC2083
6084
6085              (generally) at the same location as libpng or at
6086              ftp://ds.internic.net/rfc/rfc2083.txt
6087              or (as a W3C Recommendation) at
6088              http://www.w3.org/TR/REC-png.html
6089
6090
6091       In the case of any inconsistency between the PNG specification and this
6092       library, the specification takes precedence.
6093
6094

AUTHORS

6096       This man page: Glenn Randers-Pehrson <glennrp at users.sourceforge.net>
6097
6098       The contributing authors would like to thank all those who helped  with
6099       testing,  bug  fixes,  and  patience.  This wouldn't have been possible
6100       without all of you.
6101
6102       Thanks to Frank J. T. Wojcik for helping with the documentation.
6103
6104       Libpng version 1.5.13 - September 27, 2012: Initially created  in  1995
6105       by  Guy  Eric Schalnat, then of Group 42, Inc.  Currently maintained by
6106       Glenn Randers-Pehrson (glennrp at users.sourceforge.net).
6107
6108       Supported by the PNG development group
6109       png-mng-implement at lists.sf.net (subscription  required;  visit  png-
6110       mng-implement  at  lists.sourceforge.net  (subscription required; visit
6111       https://lists.sourceforge.net/lists/listinfo/png-mng-implement to  sub‐
6112       scribe).
6113
6114
6116       (This  copy of the libpng notices is provided for your convenience.  In
6117       case of any discrepancy between this copy and the notices in  the  file
6118       png.h  that  is  included  in the libpng distribution, the latter shall
6119       prevail.)
6120
6121       If you modify libpng you may insert additional notices immediately fol‐
6122       lowing this sentence.
6123
6124       This code is released under the libpng license.
6125
6126       libpng  versions  1.2.6, August 15, 2004, through 1.5.13, September 27,
6127       2012, are Copyright (c) 2004,2006-2007 Glenn Randers-Pehrson,  and  are
6128       distributed   according   to   the   same  disclaimer  and  license  as
6129       libpng-1.2.5 with the following individual added to the  list  of  Con‐
6130       tributing Authors
6131
6132          Cosmin Truta
6133
6134       libpng  versions  1.0.7, July 1, 2000, through 1.2.5 - October 3, 2002,
6135       are Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are  distributed
6136       according  to  the same disclaimer and license as libpng-1.0.6 with the
6137       following individuals added to the list of Contributing Authors
6138
6139          Simon-Pierre Cadieux
6140          Eric S. Raymond
6141          Gilles Vollant
6142
6143       and with the following additions to the disclaimer:
6144
6145          There is no warranty against interference with your
6146          enjoyment of the library or against infringement.
6147          There is no warranty that our efforts or the library
6148          will fulfill any of your particular purposes or needs.
6149          This library is provided with all faults, and the entire
6150          risk of satisfactory quality, performance, accuracy, and
6151          effort is with the user.
6152
6153       libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000,  are
6154       Copyright (c) 1998, 1999 Glenn Randers-Pehrson Distributed according to
6155       the same disclaimer and license  as  libpng-0.96,  with  the  following
6156       individuals added to the list of Contributing Authors:
6157
6158          Tom Lane
6159          Glenn Randers-Pehrson
6160          Willem van Schaik
6161
6162       libpng  versions 0.89, June 1996, through 0.96, May 1997, are Copyright
6163       (c) 1996, 1997 Andreas Dilger Distributed according to  the  same  dis‐
6164       claimer  and  license  as  libpng-0.88,  with the following individuals
6165       added to the list of Contributing Authors:
6166
6167          John Bowler
6168          Kevin Bracey
6169          Sam Bushell
6170          Magnus Holmgren
6171          Greg Roelofs
6172          Tom Tanner
6173
6174       libpng versions 0.5, May 1995, through 0.88, January  1996,  are  Copy‐
6175       right (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
6176
6177       For  the purposes of this copyright and license, "Contributing Authors"
6178       is defined as the following set of individuals:
6179
6180          Andreas Dilger
6181          Dave Martindale
6182          Guy Eric Schalnat
6183          Paul Schmidt
6184          Tim Wegner
6185
6186       The PNG Reference  Library  is  supplied  "AS  IS".   The  Contributing
6187       Authors  and  Group  42,  Inc.  disclaim  all  warranties, expressed or
6188       implied, including, without limitation, the warranties of merchantabil‐
6189       ity and of fitness for any purpose.  The Contributing Authors and Group
6190       42, Inc.  assume no liability for direct,  indirect,  incidental,  spe‐
6191       cial,  exemplary,  or  consequential damages, which may result from the
6192       use of the PNG Reference Library, even if advised of the possibility of
6193       such damage.
6194
6195       Permission  is hereby granted to use, copy, modify, and distribute this
6196       source code, or portions hereof, for any purpose, without fee,  subject
6197       to the following restrictions:
6198
6199       1. The origin of this source code must not be misrepresented.
6200
6201       2. Altered versions must be plainly marked as such and
6202          must not be misrepresented as being the original source.
6203
6204       3. This Copyright notice may not be removed or altered from
6205          any source or altered source distribution.
6206
6207       The  Contributing Authors and Group 42, Inc. specifically permit, with‐
6208       out fee, and encourage the use of this source code as  a  component  to
6209       supporting the PNG file format in commercial products.  If you use this
6210       source code in a product, acknowledgment is not required but  would  be
6211       appreciated.
6212
6213
6214       A  "png_get_copyright"  function  is  available,  for convenient use in
6215       "about" boxes and the like:
6216
6217          printf("%s",png_get_copyright(NULL));
6218
6219       Also, the PNG logo (in PNG format, of course) is supplied in the  files
6220       "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
6221
6222       Libpng  is  OSI  Certified  Open  Source  Software.  OSI Certified Open
6223       Source is a certification mark of the Open Source Initiative.
6224
6225       Glenn Randers-Pehrson glennrp at  users.sourceforge.net  September  27,
6226       2012
6227
6228
6229
6230
6231
6232                              September 27, 2012                     LIBPNG(3)
Impressum