source: trunk/src/3rdparty/libpng/libpng.3@ 838

Last change on this file since 838 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 171.2 KB
Line 
1.TH LIBPNG 3 "September 10, 2009"
2.SH NAME
3libpng \- Portable Network Graphics (PNG) Reference Library 1.2.40
4.SH SYNOPSIS
5\fI\fB
6
7\fB#include <png.h>\fP
8
9\fI\fB
10
11\fBpng_uint_32 png_access_version_number \fI(void\fP\fB);\fP
12
13\fI\fB
14
15\fBint png_check_sig (png_bytep \fP\fIsig\fP\fB, int \fInum\fP\fB);\fP
16
17\fI\fB
18
19\fBvoid png_chunk_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP
20
21\fI\fB
22
23\fBvoid png_chunk_warning (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fImessage\fP\fB);\fP
24
25\fI\fB
26
27\fBvoid png_convert_from_struct_tm (png_timep \fP\fIptime\fP\fB, struct tm FAR * \fIttime\fP\fB);\fP
28
29\fI\fB
30
31\fBvoid png_convert_from_time_t (png_timep \fP\fIptime\fP\fB, time_t \fIttime\fP\fB);\fP
32
33\fI\fB
34
35\fBpng_charp png_convert_to_rfc1123 (png_structp \fP\fIpng_ptr\fP\fB, png_timep \fIptime\fP\fB);\fP
36
37\fI\fB
38
39\fBpng_infop png_create_info_struct (png_structp \fIpng_ptr\fP\fB);\fP
40
41\fI\fB
42
43\fBpng_structp png_create_read_struct (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarn_fn\fP\fB);\fP
44
45\fI\fB
46
47\fBpng_structp png_create_read_struct_2(png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fP\fIwarn_fn\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP
48
49\fI\fB
50
51\fBpng_structp png_create_write_struct (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarn_fn\fP\fB);\fP
52
53\fI\fB
54
55\fBpng_structp png_create_write_struct_2(png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fP\fIwarn_fn\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP
56
57\fI\fB
58
59\fBint png_debug(int \fP\fIlevel\fP\fB, png_const_charp \fImessage\fP\fB);\fP
60
61\fI\fB
62
63\fBint png_debug1(int \fP\fIlevel\fP\fB, png_const_charp \fP\fImessage\fP\fB, \fIp1\fP\fB);\fP
64
65\fI\fB
66
67\fBint png_debug2(int \fP\fIlevel\fP\fB, png_const_charp \fP\fImessage\fP\fB, \fP\fIp1\fP\fB, \fIp2\fP\fB);\fP
68
69\fI\fB
70
71\fBvoid png_destroy_info_struct (png_structp \fP\fIpng_ptr\fP\fB, png_infopp \fIinfo_ptr_ptr\fP\fB);\fP
72
73\fI\fB
74
75\fBvoid png_destroy_read_struct (png_structpp \fP\fIpng_ptr_ptr\fP\fB, png_infopp \fP\fIinfo_ptr_ptr\fP\fB, png_infopp \fIend_info_ptr_ptr\fP\fB);\fP
76
77\fI\fB
78
79\fBvoid png_destroy_write_struct (png_structpp \fP\fIpng_ptr_ptr\fP\fB, png_infopp \fIinfo_ptr_ptr\fP\fB);\fP
80
81\fI\fB
82
83\fBvoid png_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP
84
85\fI\fB
86
87\fBvoid png_free (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fIptr\fP\fB);\fP
88
89\fI\fB
90
91\fBvoid png_free_chunk_list (png_structp \fIpng_ptr\fP\fB);\fP
92
93\fI\fB
94
95\fBvoid png_free_default(png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fIptr\fP\fB);\fP
96
97\fI\fB
98
99\fBvoid png_free_data (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fInum\fP\fB);\fP
100
101\fI\fB
102
103\fBpng_byte png_get_bit_depth (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
104
105\fI\fB
106
107\fBpng_uint_32 png_get_bKGD (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_16p \fI*background\fP\fB);\fP
108
109\fI\fB
110
111\fBpng_byte png_get_channels (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
112
113\fI\fB
114
115\fBpng_uint_32 png_get_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fP\fI*white_x\fP\fB, double \fP\fI*white_y\fP\fB, double \fP\fI*red_x\fP\fB, double \fP\fI*red_y\fP\fB, double \fP\fI*green_x\fP\fB, double \fP\fI*green_y\fP\fB, double \fP\fI*blue_x\fP\fB, double \fI*blue_y\fP\fB);\fP
116
117\fI\fB
118
119\fBpng_uint_32 png_get_cHRM_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*white_x\fP\fB, png_uint_32 \fP\fI*white_y\fP\fB, png_uint_32 \fP\fI*red_x\fP\fB, png_uint_32 \fP\fI*red_y\fP\fB, png_uint_32 \fP\fI*green_x\fP\fB, png_uint_32 \fP\fI*green_y\fP\fB, png_uint_32 \fP\fI*blue_x\fP\fB, png_uint_32 \fI*blue_y\fP\fB);\fP
120
121\fI\fB
122
123\fBpng_byte png_get_color_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
124
125\fI\fB
126
127\fBpng_byte png_get_compression_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
128
129\fI\fB
130
131\fBpng_byte png_get_copyright (png_structp \fIpng_ptr\fP\fB);\fP
132
133\fI\fB
134
135\fBpng_voidp png_get_error_ptr (png_structp \fIpng_ptr\fP\fB);\fP
136
137\fI\fB
138
139\fBpng_byte png_get_filter_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
140
141\fI\fB
142
143\fBpng_uint_32 png_get_gAMA (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fI*file_gamma\fP\fB);\fP
144
145\fI\fB
146
147\fBpng_uint_32 png_get_gAMA_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fI*int_file_gamma\fP\fB);\fP
148
149\fI\fB
150
151\fBpng_byte png_get_header_ver (png_structp \fIpng_ptr\fP\fB);\fP
152
153\fI\fB
154
155\fBpng_byte png_get_header_version (png_structp \fIpng_ptr\fP\fB);\fP
156
157\fI\fB
158
159\fBpng_uint_32 png_get_hIST (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_16p \fI*hist\fP\fB);\fP
160
161\fI\fB
162
163\fBpng_uint_32 png_get_iCCP (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charpp \fP\fIname\fP\fB, int \fP\fI*compression_type\fP\fB, png_charpp \fP\fIprofile\fP\fB, png_uint_32 \fI*proflen\fP\fB);\fP
164
165\fI\fB
166
167\fBpng_uint_32 png_get_IHDR (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*width\fP\fB, png_uint_32 \fP\fI*height\fP\fB, int \fP\fI*bit_depth\fP\fB, int \fP\fI*color_type\fP\fB, int \fP\fI*interlace_type\fP\fB, int \fP\fI*compression_type\fP\fB, int \fI*filter_type\fP\fB);\fP
168
169\fI\fB
170
171\fBpng_uint_32 png_get_image_height (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
172
173\fI\fB
174
175\fBpng_uint_32 png_get_image_width (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
176
177\fI\fB
178
179\fB#if \fI!defined(PNG_1_0_X)
180
181\fBpng_int_32 png_get_int_32 (png_bytep \fIbuf\fP\fB);\fP
182
183\fI\fB#endif
184
185\fI\fB
186
187\fBpng_byte png_get_interlace_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
188
189\fI\fB
190
191\fBpng_voidp png_get_io_ptr (png_structp \fIpng_ptr\fP\fB);\fP
192
193\fI\fB
194
195\fBpng_byte png_get_libpng_ver (png_structp \fIpng_ptr\fP\fB);\fP
196
197\fI\fB
198
199\fBpng_voidp png_get_mem_ptr(png_structp \fIpng_ptr\fP\fB);\fP
200
201\fI\fB
202
203\fBpng_uint_32 png_get_oFFs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*offset_x\fP\fB, png_uint_32 \fP\fI*offset_y\fP\fB, int \fI*unit_type\fP\fB);\fP
204
205\fI\fB
206
207\fBpng_uint_32 png_get_pCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fI*purpose\fP\fB, png_int_32 \fP\fI*X0\fP\fB, png_int_32 \fP\fI*X1\fP\fB, int \fP\fI*type\fP\fB, int \fP\fI*nparams\fP\fB, png_charp \fP\fI*units\fP\fB, png_charpp \fI*params\fP\fB);\fP
208
209\fI\fB
210
211\fBpng_uint_32 png_get_pHYs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*res_x\fP\fB, png_uint_32 \fP\fI*res_y\fP\fB, int \fI*unit_type\fP\fB);\fP
212
213\fI\fB
214
215\fBfloat png_get_pixel_aspect_ratio (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
216
217\fI\fB
218
219\fBpng_uint_32 png_get_pixels_per_meter (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
220
221\fI\fB
222
223\fBpng_voidp png_get_progressive_ptr (png_structp \fIpng_ptr\fP\fB);\fP
224
225\fI\fB
226
227\fBpng_uint_32 png_get_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_colorp \fP\fI*palette\fP\fB, int \fI*num_palette\fP\fB);\fP
228
229\fI\fB
230
231\fBpng_byte png_get_rgb_to_gray_status (png_structp \fIpng_ptr)
232
233\fBpng_uint_32 png_get_rowbytes (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
234
235\fI\fB
236
237\fBpng_bytepp png_get_rows (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
238
239\fI\fB
240
241\fBpng_uint_32 png_get_sBIT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_8p \fI*sig_bit\fP\fB);\fP
242
243\fI\fB
244
245\fBpng_bytep png_get_signature (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
246
247\fI\fB
248
249\fBpng_uint_32 png_get_sPLT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_spalette_p \fI*splt_ptr\fP\fB);\fP
250
251\fI\fB
252
253\fBpng_uint_32 png_get_sRGB (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fI*intent\fP\fB);\fP
254
255\fI\fB
256
257\fBpng_uint_32 png_get_text (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_textp \fP\fI*text_ptr\fP\fB, int \fI*num_text\fP\fB);\fP
258
259\fI\fB
260
261\fBpng_uint_32 png_get_tIME (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_timep \fI*mod_time\fP\fB);\fP
262
263\fI\fB
264
265\fBpng_uint_32 png_get_tRNS (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fI*trans\fP\fB, int \fP\fI*num_trans\fP\fB, png_color_16p \fI*trans_values\fP\fB);\fP
266
267\fI\fB
268
269\fB#if \fI!defined(PNG_1_0_X)
270
271\fBpng_uint_16 png_get_uint_16 (png_bytep \fIbuf\fP\fB);\fP
272
273\fI\fB
274
275\fBpng_uint_32 png_get_uint_31 (png_bytep \fIbuf\fP\fB);\fP
276
277\fI\fB
278
279\fBpng_uint_32 png_get_uint_32 (png_bytep \fIbuf\fP\fB);\fP
280
281\fI\fB#endif
282
283\fI\fB
284
285\fBpng_uint_32 png_get_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_unknown_chunkpp \fIunknowns\fP\fB);\fP
286
287\fI\fB
288
289\fBpng_voidp png_get_user_chunk_ptr (png_structp \fIpng_ptr\fP\fB);\fP
290
291\fI\fB
292
293\fBpng_uint_32 png_get_user_height_max( png_structp \fIpng_ptr\fP\fB);\fP
294
295\fI\fB
296
297\fBpng_voidp png_get_user_transform_ptr (png_structp \fIpng_ptr\fP\fB);\fP
298
299\fI\fB
300
301\fBpng_uint_32 png_get_user_width_max (png_structp \fIpng_ptr\fP\fB);\fP
302
303\fI\fB
304
305\fBpng_uint_32 png_get_valid (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fIflag\fP\fB);\fP
306
307\fI\fB
308
309\fBpng_int_32 png_get_x_offset_microns (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
310
311\fI\fB
312
313\fBpng_int_32 png_get_x_offset_pixels (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
314
315\fI\fB
316
317\fBpng_uint_32 png_get_x_pixels_per_meter (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
318
319\fI\fB
320
321\fBpng_int_32 png_get_y_offset_microns (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
322
323\fI\fB
324
325\fBpng_int_32 png_get_y_offset_pixels (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
326
327\fI\fB
328
329\fBpng_uint_32 png_get_y_pixels_per_meter (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
330
331\fI\fB
332
333\fBpng_uint_32 png_get_compression_buffer_size (png_structp \fIpng_ptr\fP\fB);\fP
334
335\fI\fB
336
337\fBint png_handle_as_unknown (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fIchunk_name\fP\fB);\fP
338
339\fI\fB
340
341\fBvoid png_init_io (png_structp \fP\fIpng_ptr\fP\fB, FILE \fI*fp\fP\fB);\fP
342
343\fI\fB
344
345\fBDEPRECATED: void png_info_init (png_infop \fIinfo_ptr\fP\fB);\fP
346
347\fI\fB
348
349\fBDEPRECATED: void png_info_init_2 (png_infopp \fP\fIptr_ptr\fP\fB, png_size_t \fIpng_info_struct_size\fP\fB);\fP
350
351\fI\fB
352
353\fBpng_voidp png_malloc (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
354
355\fI\fB
356
357\fBpng_voidp png_malloc_default(png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
358
359\fI\fB
360
361\fBvoidp png_memcpy (png_voidp \fP\fIs1\fP\fB, png_voidp \fP\fIs2\fP\fB, png_size_t \fIsize\fP\fB);\fP
362
363\fI\fB
364
365\fBpng_voidp png_memcpy_check (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIs1\fP\fB, png_voidp \fP\fIs2\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
366
367\fI\fB
368
369\fBvoidp png_memset (png_voidp \fP\fIs1\fP\fB, int \fP\fIvalue\fP\fB, png_size_t \fIsize\fP\fB);\fP
370
371\fI\fB
372
373\fBpng_voidp png_memset_check (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIs1\fP\fB, int \fP\fIvalue\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
374
375\fI\fB
376
377\fBDEPRECATED: void png_permit_empty_plte (png_structp \fP\fIpng_ptr\fP\fB, int \fIempty_plte_permitted\fP\fB);\fP
378
379\fI\fB
380
381\fBvoid png_process_data (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fIbuffer\fP\fB, png_size_t \fIbuffer_size\fP\fB);\fP
382
383\fI\fB
384
385\fBvoid png_progressive_combine_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIold_row\fP\fB, png_bytep \fInew_row\fP\fB);\fP
386
387\fI\fB
388
389\fBvoid png_read_destroy (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_infop \fIend_info_ptr\fP\fB);\fP
390
391\fI\fB
392
393\fBvoid png_read_end (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
394
395\fI\fB
396
397\fBvoid png_read_image (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fIimage\fP\fB);\fP
398
399\fI\fB
400
401\fBDEPRECATED: void png_read_init (png_structp \fIpng_ptr\fP\fB);\fP
402
403\fI\fB
404
405\fBDEPRECATED: void png_read_init_2 (png_structpp \fP\fIptr_ptr\fP\fB, png_const_charp \fP\fIuser_png_ver\fP\fB, png_size_t \fP\fIpng_struct_size\fP\fB, png_size_t \fIpng_info_size\fP\fB);\fP
406
407\fI\fB
408
409\fBvoid png_read_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
410
411\fI\fB
412
413\fBvoid png_read_png (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fItransforms\fP\fB, png_voidp \fIparams\fP\fB);\fP
414
415\fI\fB
416
417\fBvoid png_read_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIrow\fP\fB, png_bytep \fIdisplay_row\fP\fB);\fP
418
419\fI\fB
420
421\fBvoid png_read_rows (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fP\fIrow\fP\fB, png_bytepp \fP\fIdisplay_row\fP\fB, png_uint_32 \fInum_rows\fP\fB);\fP
422
423\fI\fB
424
425\fBvoid png_read_update_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
426
427\fI\fB
428
429\fB#if \fI!defined(PNG_1_0_X)
430
431\fBpng_save_int_32 (png_bytep \fP\fIbuf\fP\fB, png_int_32 \fIi\fP\fB);\fP
432
433\fI\fB
434
435\fBvoid png_save_uint_16 (png_bytep \fP\fIbuf\fP\fB, unsigned int \fIi\fP\fB);\fP
436
437\fI\fB
438
439\fBvoid png_save_uint_32 (png_bytep \fP\fIbuf\fP\fB, png_uint_32 \fIi\fP\fB);\fP
440
441\fI\fB
442
443\fBvoid png_set_add_alpha (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIfiller\fP\fB, int \fIflags\fP\fB);\fP
444
445\fI\fB#endif
446
447\fI\fB
448
449\fBvoid png_set_background (png_structp \fP\fIpng_ptr\fP\fB, png_color_16p \fP\fIbackground_color\fP\fB, int \fP\fIbackground_gamma_code\fP\fB, int \fP\fIneed_expand\fP\fB, double \fIbackground_gamma\fP\fB);\fP
450
451\fI\fB
452
453\fBvoid png_set_bgr (png_structp \fIpng_ptr\fP\fB);\fP
454
455\fI\fB
456
457\fBvoid png_set_bKGD (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_16p \fIbackground\fP\fB);\fP
458
459\fI\fB
460
461\fBvoid png_set_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fP\fIwhite_x\fP\fB, double \fP\fIwhite_y\fP\fB, double \fP\fIred_x\fP\fB, double \fP\fIred_y\fP\fB, double \fP\fIgreen_x\fP\fB, double \fP\fIgreen_y\fP\fB, double \fP\fIblue_x\fP\fB, double \fIblue_y\fP\fB);\fP
462
463\fI\fB
464
465\fBvoid png_set_cHRM_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIwhite_x\fP\fB, png_uint_32 \fP\fIwhite_y\fP\fB, png_uint_32 \fP\fIred_x\fP\fB, png_uint_32 \fP\fIred_y\fP\fB, png_uint_32 \fP\fIgreen_x\fP\fB, png_uint_32 \fP\fIgreen_y\fP\fB, png_uint_32 \fP\fIblue_x\fP\fB, png_uint_32 \fIblue_y\fP\fB);\fP
466
467\fI\fB
468
469\fBvoid png_set_compression_level (png_structp \fP\fIpng_ptr\fP\fB, int \fIlevel\fP\fB);\fP
470
471\fI\fB
472
473\fBvoid png_set_compression_mem_level (png_structp \fP\fIpng_ptr\fP\fB, int \fImem_level\fP\fB);\fP
474
475\fI\fB
476
477\fBvoid png_set_compression_method (png_structp \fP\fIpng_ptr\fP\fB, int \fImethod\fP\fB);\fP
478
479\fI\fB
480
481\fBvoid png_set_compression_strategy (png_structp \fP\fIpng_ptr\fP\fB, int \fIstrategy\fP\fB);\fP
482
483\fI\fB
484
485\fBvoid png_set_compression_window_bits (png_structp \fP\fIpng_ptr\fP\fB, int \fIwindow_bits\fP\fB);\fP
486
487\fI\fB
488
489\fBvoid png_set_crc_action (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIcrit_action\fP\fB, int \fIancil_action\fP\fB);\fP
490
491\fI\fB
492
493\fBvoid png_set_dither (png_structp \fP\fIpng_ptr\fP\fB, png_colorp \fP\fIpalette\fP\fB, int \fP\fInum_palette\fP\fB, int \fP\fImaximum_colors\fP\fB, png_uint_16p \fP\fIhistogram\fP\fB, int \fIfull_dither\fP\fB);\fP
494
495\fI\fB
496
497\fBvoid png_set_error_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarning_fn\fP\fB);\fP
498
499\fI\fB
500
501\fBvoid png_set_expand (png_structp \fIpng_ptr\fP\fB);\fP
502
503\fI\fB
504
505\fBvoid png_set_expand_gray_1_2_4_to_8(png_structp \fIpng_ptr\fP\fB);\fP
506
507\fI\fB
508
509\fBvoid png_set_filler (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIfiller\fP\fB, int \fIflags\fP\fB);\fP
510
511\fI\fB
512
513\fBvoid png_set_filter (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fImethod\fP\fB, int \fIfilters\fP\fB);\fP
514
515\fI\fB
516
517\fBvoid png_set_filter_heuristics (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIheuristic_method\fP\fB, int \fP\fInum_weights\fP\fB, png_doublep \fP\fIfilter_weights\fP\fB, png_doublep \fIfilter_costs\fP\fB);\fP
518
519\fI\fB
520
521\fBvoid png_set_flush (png_structp \fP\fIpng_ptr\fP\fB, int \fInrows\fP\fB);\fP
522
523\fI\fB
524
525\fBvoid png_set_gamma (png_structp \fP\fIpng_ptr\fP\fB, double \fP\fIscreen_gamma\fP\fB, double \fIdefault_file_gamma\fP\fB);\fP
526
527\fI\fB
528
529\fBvoid png_set_gAMA (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fIfile_gamma\fP\fB);\fP
530
531\fI\fB
532
533\fBvoid png_set_gAMA_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fIfile_gamma\fP\fB);\fP
534
535\fI\fB
536
537\fBvoid png_set_gray_1_2_4_to_8(png_structp \fIpng_ptr\fP\fB);\fP
538
539\fI\fB
540
541\fBvoid png_set_gray_to_rgb (png_structp \fIpng_ptr\fP\fB);\fP
542
543\fI\fB
544
545\fBvoid png_set_hIST (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_16p \fIhist\fP\fB);\fP
546
547\fI\fB
548
549\fBvoid png_set_iCCP (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fIname\fP\fB, int \fP\fIcompression_type\fP\fB, png_charp \fP\fIprofile\fP\fB, png_uint_32 \fIproflen\fP\fB);\fP
550
551\fI\fB
552
553\fBint png_set_interlace_handling (png_structp \fIpng_ptr\fP\fB);\fP
554
555\fI\fB
556
557\fBvoid png_set_invalid (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fImask\fP\fB);\fP
558
559\fI\fB
560
561\fBvoid png_set_invert_alpha (png_structp \fIpng_ptr\fP\fB);\fP
562
563\fI\fB
564
565\fBvoid png_set_invert_mono (png_structp \fIpng_ptr\fP\fB);\fP
566
567\fI\fB
568
569\fBvoid png_set_IHDR (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIwidth\fP\fB, png_uint_32 \fP\fIheight\fP\fB, int \fP\fIbit_depth\fP\fB, int \fP\fIcolor_type\fP\fB, int \fP\fIinterlace_type\fP\fB, int \fP\fIcompression_type\fP\fB, int \fIfilter_type\fP\fB);\fP
570
571\fI\fB
572
573\fBvoid png_set_keep_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIkeep\fP\fB, png_bytep \fP\fIchunk_list\fP\fB, int \fInum_chunks\fP\fB);\fP
574
575\fI\fB
576
577\fBvoid png_set_mem_fn(png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP
578
579\fI\fB
580
581\fBvoid png_set_oFFs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIoffset_x\fP\fB, png_uint_32 \fP\fIoffset_y\fP\fB, int \fIunit_type\fP\fB);\fP
582
583\fI\fB
584
585\fBvoid png_set_packing (png_structp \fIpng_ptr\fP\fB);\fP
586
587\fI\fB
588
589\fBvoid png_set_packswap (png_structp \fIpng_ptr\fP\fB);\fP
590
591\fI\fB
592
593\fBvoid png_set_palette_to_rgb(png_structp \fIpng_ptr\fP\fB);\fP
594
595\fI\fB
596
597\fBvoid png_set_pCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fIpurpose\fP\fB, png_int_32 \fP\fIX0\fP\fB, png_int_32 \fP\fIX1\fP\fB, int \fP\fItype\fP\fB, int \fP\fInparams\fP\fB, png_charp \fP\fIunits\fP\fB, png_charpp \fIparams\fP\fB);\fP
598
599\fI\fB
600
601\fBvoid png_set_pHYs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIres_x\fP\fB, png_uint_32 \fP\fIres_y\fP\fB, int \fIunit_type\fP\fB);\fP
602
603\fI\fB
604
605\fBvoid png_set_progressive_read_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIprogressive_ptr\fP\fB, png_progressive_info_ptr \fP\fIinfo_fn\fP\fB, png_progressive_row_ptr \fP\fIrow_fn\fP\fB, png_progressive_end_ptr \fIend_fn\fP\fB);\fP
606
607\fI\fB
608
609\fBvoid png_set_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_colorp \fP\fIpalette\fP\fB, int \fInum_palette\fP\fB);\fP
610
611\fI\fB
612
613\fBvoid png_set_read_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIio_ptr\fP\fB, png_rw_ptr \fIread_data_fn\fP\fB);\fP
614
615\fI\fB
616
617\fBvoid png_set_read_status_fn (png_structp \fP\fIpng_ptr\fP\fB, png_read_status_ptr \fIread_row_fn\fP\fB);\fP
618
619\fI\fB
620
621\fBvoid png_set_read_user_transform_fn (png_structp \fP\fIpng_ptr\fP\fB, png_user_transform_ptr \fIread_user_transform_fn\fP\fB);\fP
622
623\fI\fB
624
625\fBvoid png_set_rgb_to_gray (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIerror_action\fP\fB, double \fP\fIred\fP\fB, double \fIgreen\fP\fB);\fP
626
627\fI\fB
628
629\fBvoid png_set_rgb_to_gray_fixed (png_structp \fP\fIpng_ptr\fP\fB, int error_action png_fixed_point \fP\fIred\fP\fB, png_fixed_point \fIgreen\fP\fB);\fP
630
631\fI\fB
632
633\fBvoid png_set_rows (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytepp \fIrow_pointers\fP\fB);\fP
634
635\fI\fB
636
637\fBvoid png_set_sBIT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_8p \fIsig_bit\fP\fB);\fP
638
639\fI\fB
640
641\fBvoid png_set_sCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fIunit\fP\fB, double \fP\fIwidth\fP\fB, double \fIheight\fP\fB);\fP
642
643\fI\fB
644
645\fBvoid png_set_shift (png_structp \fP\fIpng_ptr\fP\fB, png_color_8p \fItrue_bits\fP\fB);\fP
646
647\fI\fB
648
649\fBvoid png_set_sig_bytes (png_structp \fP\fIpng_ptr\fP\fB, int \fInum_bytes\fP\fB);\fP
650
651\fI\fB
652
653\fBvoid png_set_sPLT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_spalette_p \fP\fIsplt_ptr\fP\fB, int \fInum_spalettes\fP\fB);\fP
654
655\fI\fB
656
657\fBvoid png_set_sRGB (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fIintent\fP\fB);\fP
658
659\fI\fB
660
661\fBvoid png_set_sRGB_gAMA_and_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fIintent\fP\fB);\fP
662
663\fI\fB
664
665\fBvoid png_set_strip_16 (png_structp \fIpng_ptr\fP\fB);\fP
666
667\fI\fB
668
669\fBvoid png_set_strip_alpha (png_structp \fIpng_ptr\fP\fB);\fP
670
671\fI\fB
672
673\fBvoid png_set_swap (png_structp \fIpng_ptr\fP\fB);\fP
674
675\fI\fB
676
677\fBvoid png_set_swap_alpha (png_structp \fIpng_ptr\fP\fB);\fP
678
679\fI\fB
680
681\fBvoid png_set_text (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_textp \fP\fItext_ptr\fP\fB, int \fInum_text\fP\fB);\fP
682
683\fI\fB
684
685\fBvoid png_set_tIME (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_timep \fImod_time\fP\fB);\fP
686
687\fI\fB
688
689\fBvoid png_set_tRNS (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fItrans\fP\fB, int \fP\fInum_trans\fP\fB, png_color_16p \fItrans_values\fP\fB);\fP
690
691\fI\fB
692
693\fBvoid png_set_tRNS_to_alpha(png_structp \fIpng_ptr\fP\fB);\fP
694
695\fI\fB
696
697\fBpng_uint_32 png_set_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_unknown_chunkp \fP\fIunknowns\fP\fB, int \fP\fInum\fP\fB, int \fIlocation\fP\fB);\fP
698
699\fI\fB
700
701\fBvoid png_set_unknown_chunk_location(png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fIchunk\fP\fB, int \fIlocation\fP\fB);\fP
702
703\fI\fB
704
705\fBvoid png_set_read_user_chunk_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIuser_chunk_ptr\fP\fB, png_user_chunk_ptr \fIread_user_chunk_fn\fP\fB);\fP
706
707\fI\fB
708
709\fBvoid png_set_user_limits (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIuser_width_max\fP\fB, png_uint_32 \fIuser_height_max\fP\fB);\fP
710
711\fI\fB
712
713\fBvoid png_set_user_transform_info (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIuser_transform_ptr\fP\fB, int \fP\fIuser_transform_depth\fP\fB, int \fIuser_transform_channels\fP\fB);\fP
714
715\fI\fB
716
717\fBvoid png_set_write_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIio_ptr\fP\fB, png_rw_ptr \fP\fIwrite_data_fn\fP\fB, png_flush_ptr \fIoutput_flush_fn\fP\fB);\fP
718
719\fI\fB
720
721\fBvoid png_set_write_status_fn (png_structp \fP\fIpng_ptr\fP\fB, png_write_status_ptr \fIwrite_row_fn\fP\fB);\fP
722
723\fI\fB
724
725\fBvoid png_set_write_user_transform_fn (png_structp \fP\fIpng_ptr\fP\fB, png_user_transform_ptr \fIwrite_user_transform_fn\fP\fB);\fP
726
727\fI\fB
728
729\fBvoid png_set_compression_buffer_size(png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
730
731\fI\fB
732
733\fBint png_sig_cmp (png_bytep \fP\fIsig\fP\fB, png_size_t \fP\fIstart\fP\fB, png_size_t \fInum_to_check\fP\fB);\fP
734
735\fI\fB
736
737\fBvoid png_start_read_image (png_structp \fIpng_ptr\fP\fB);\fP
738
739\fI\fB
740
741\fBvoid png_warning (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fImessage\fP\fB);\fP
742
743\fI\fB
744
745\fBvoid png_write_chunk (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIchunk_name\fP\fB, png_bytep \fP\fIdata\fP\fB, png_size_t \fIlength\fP\fB);\fP
746
747\fI\fB
748
749\fBvoid png_write_chunk_data (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIdata\fP\fB, png_size_t \fIlength\fP\fB);\fP
750
751\fI\fB
752
753\fBvoid png_write_chunk_end (png_structp \fIpng_ptr\fP\fB);\fP
754
755\fI\fB
756
757\fBvoid png_write_chunk_start (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIchunk_name\fP\fB, png_uint_32 \fIlength\fP\fB);\fP
758
759\fI\fB
760
761\fBvoid png_write_destroy (png_structp \fIpng_ptr\fP\fB);\fP
762
763\fI\fB
764
765\fBvoid png_write_end (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
766
767\fI\fB
768
769\fBvoid png_write_flush (png_structp \fIpng_ptr\fP\fB);\fP
770
771\fI\fB
772
773\fBvoid png_write_image (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fIimage\fP\fB);\fP
774
775\fI\fB
776
777\fBDEPRECATED: void png_write_init (png_structp \fIpng_ptr\fP\fB);\fP
778
779\fI\fB
780
781\fBDEPRECATED: void png_write_init_2 (png_structpp \fP\fIptr_ptr\fP\fB, png_const_charp \fP\fIuser_png_ver\fP\fB, png_size_t \fP\fIpng_struct_size\fP\fB, png_size_t \fIpng_info_size\fP\fB);\fP
782
783\fI\fB
784
785\fBvoid png_write_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
786
787\fI\fB
788
789\fBvoid png_write_info_before_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
790
791\fI\fB
792
793\fBvoid png_write_png (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fItransforms\fP\fB, png_voidp \fIparams\fP\fB);\fP
794
795\fI\fB
796
797\fBvoid png_write_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fIrow\fP\fB);\fP
798
799\fI\fB
800
801\fBvoid png_write_rows (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fP\fIrow\fP\fB, png_uint_32 \fInum_rows\fP\fB);\fP
802
803\fI\fB
804
805\fBvoidpf png_zalloc (voidpf \fP\fIpng_ptr\fP\fB, uInt \fP\fIitems\fP\fB, uInt \fIsize\fP\fB);\fP
806
807\fI\fB
808
809\fBvoid png_zfree (voidpf \fP\fIpng_ptr\fP\fB, voidpf \fIptr\fP\fB);\fP
810
811\fI\fB
812
813.SH DESCRIPTION
814The
815.I libpng
816library supports encoding, decoding, and various manipulations of
817the Portable Network Graphics (PNG) format image files. It uses the
818.IR zlib(3)
819compression library.
820Following is a copy of the libpng.txt file that accompanies libpng.
821.SH LIBPNG.TXT
822libpng.txt - A description on how to use and modify libpng
823
824 libpng version 1.2.40 - September 10, 2009
825 Updated and distributed by Glenn Randers-Pehrson
826 <glennrp at users.sourceforge.net>
827 Copyright (c) 1998-2009 Glenn Randers-Pehrson
828
829 This document is released under the libpng license.
830 For conditions of distribution and use, see the disclaimer
831 and license in png.h
832
833 Based on:
834
835 libpng versions 0.97, January 1998, through 1.2.40 - September 10, 2009
836 Updated and distributed by Glenn Randers-Pehrson
837 Copyright (c) 1998-2009 Glenn Randers-Pehrson
838
839 libpng 1.0 beta 6 version 0.96 May 28, 1997
840 Updated and distributed by Andreas Dilger
841 Copyright (c) 1996, 1997 Andreas Dilger
842
843 libpng 1.0 beta 2 - version 0.88 January 26, 1996
844 For conditions of distribution and use, see copyright
845 notice in png.h. Copyright (c) 1995, 1996 Guy Eric
846 Schalnat, Group 42, Inc.
847
848 Updated/rewritten per request in the libpng FAQ
849 Copyright (c) 1995, 1996 Frank J. T. Wojcik
850 December 18, 1995 & January 20, 1996
851
852.SH I. Introduction
853
854This file describes how to use and modify the PNG reference library
855(known as libpng) for your own use. There are five sections to this
856file: introduction, structures, reading, writing, and modification and
857configuration notes for various special platforms. In addition to this
858file, example.c is a good starting point for using the library, as
859it is heavily commented and should include everything most people
860will need. We assume that libpng is already installed; see the
861INSTALL file for instructions on how to install libpng.
862
863For examples of libpng usage, see the files "example.c", "pngtest.c",
864and the files in the "contrib" directory, all of which are included in the
865libpng distribution.
866
867Libpng was written as a companion to the PNG specification, as a way
868of reducing the amount of time and effort it takes to support the PNG
869file format in application programs.
870
871The PNG specification (second edition), November 2003, is available as
872a W3C Recommendation and as an ISO Standard (ISO/IEC 15948:2003 (E)) at
873<http://www.w3.org/TR/2003/REC-PNG-20031110/
874The W3C and ISO documents have identical technical content.
875
876The PNG-1.2 specification is available at
877<http://www.libpng.org/pub/png/documents/>. It is technically equivalent
878to the PNG specification (second edition) but has some additional material.
879
880The PNG-1.0 specification is available
881as RFC 2083 <http://www.libpng.org/pub/png/documents/> and as a
882W3C Recommendation <http://www.w3.org/TR/REC.png.html>.
883
884Some additional chunks are described in the special-purpose public chunks
885documents at <http://www.libpng.org/pub/png/documents/>.
886
887Other information
888about PNG, and the latest version of libpng, can be found at the PNG home
889page, <http://www.libpng.org/pub/png/>.
890
891Most users will not have to modify the library significantly; advanced
892users may want to modify it more. All attempts were made to make it as
893complete as possible, while keeping the code easy to understand.
894Currently, this library only supports C. Support for other languages
895is being considered.
896
897Libpng has been designed to handle multiple sessions at one time,
898to be easily modifiable, to be portable to the vast majority of
899machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy
900to use. The ultimate goal of libpng is to promote the acceptance of
901the PNG file format in whatever way possible. While there is still
902work to be done (see the TODO file), libpng should cover the
903majority of the needs of its users.
904
905Libpng uses zlib for its compression and decompression of PNG files.
906Further information about zlib, and the latest version of zlib, can
907be found at the zlib home page, <http://www.info-zip.org/pub/infozip/zlib/>.
908The zlib compression utility is a general purpose utility that is
909useful for more than PNG files, and can be used without libpng.
910See the documentation delivered with zlib for more details.
911You can usually find the source files for the zlib utility wherever you
912find the libpng source files.
913
914Libpng is thread safe, provided the threads are using different
915instances of the structures. Each thread should have its own
916png_struct and png_info instances, and thus its own image.
917Libpng does not protect itself against two threads using the
918same instance of a structure.
919
920.SH II. Structures
921
922There are two main structures that are important to libpng, png_struct
923and png_info. The first, png_struct, is an internal structure that
924will not, for the most part, be used by a user except as the first
925variable passed to every libpng function call.
926
927The png_info structure is designed to provide information about the
928PNG file. At one time, the fields of png_info were intended to be
929directly accessible to the user. However, this tended to cause problems
930with applications using dynamically loaded libraries, and as a result
931a set of interface functions for png_info (the png_get_*() and png_set_*()
932functions) was developed. The fields of png_info are still available for
933older applications, but it is suggested that applications use the new
934interfaces if at all possible.
935
936Applications that do make direct access to the members of png_struct (except
937for png_ptr->jmpbuf) must be recompiled whenever the library is updated,
938and applications that make direct access to the members of png_info must
939be recompiled if they were compiled or loaded with libpng version 1.0.6,
940in which the members were in a different order. In version 1.0.7, the
941members of the png_info structure reverted to the old order, as they were
942in versions 0.97c through 1.0.5. Starting with version 2.0.0, both
943structures are going to be hidden, and the contents of the structures will
944only be accessible through the png_get/png_set functions.
945
946The png.h header file is an invaluable reference for programming with libpng.
947And while I'm on the topic, make sure you include the libpng header file:
948
949#include <png.h>
950
951.SH III. Reading
952
953We'll now walk you through the possible functions to call when reading
954in a PNG file sequentially, briefly explaining the syntax and purpose
955of each one. See example.c and png.h for more detail. While
956progressive reading is covered in the next section, you will still
957need some of the functions discussed in this section to read a PNG
958file.
959
960.SS Setup
961
962You will want to do the I/O initialization(*) before you get into libpng,
963so if it doesn't work, you don't have much to undo. Of course, you
964will also want to insure that you are, in fact, dealing with a PNG
965file. Libpng provides a simple check to see if a file is a PNG file.
966To use it, pass in the first 1 to 8 bytes of the file to the function
967png_sig_cmp(), and it will return 0 (false) if the bytes match the
968corresponding bytes of the PNG signature, or nonzero (true) otherwise.
969Of course, the more bytes you pass in, the greater the accuracy of the
970prediction.
971
972If you are intending to keep the file pointer open for use in libpng,
973you must ensure you don't read more than 8 bytes from the beginning
974of the file, and you also have to make a call to png_set_sig_bytes_read()
975with the number of bytes you read from the beginning. Libpng will
976then only check the bytes (if any) that your program didn't read.
977
978(*): If you are not using the standard I/O functions, you will need
979to replace them with custom functions. See the discussion under
980Customizing libpng.
981
982
983 FILE *fp = fopen(file_name, "rb");
984 if (!fp)
985 {
986 return (ERROR);
987 }
988 fread(header, 1, number, fp);
989 is_png = !png_sig_cmp(header, 0, number);
990 if (!is_png)
991 {
992 return (NOT_PNG);
993 }
994
995
996Next, png_struct and png_info need to be allocated and initialized. In
997order to ensure that the size of these structures is correct even with a
998dynamically linked libpng, there are functions to initialize and
999allocate the structures. We also pass the library version, optional
1000pointers to error handling functions, and a pointer to a data struct for
1001use by the error functions, if necessary (the pointer and functions can
1002be NULL if the default error handlers are to be used). See the section
1003on Changes to Libpng below regarding the old initialization functions.
1004The structure allocation functions quietly return NULL if they fail to
1005create the structure, so your application should check for that.
1006
1007 png_structp png_ptr = png_create_read_struct
1008 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
1009 user_error_fn, user_warning_fn);
1010 if (!png_ptr)
1011 return (ERROR);
1012
1013 png_infop info_ptr = png_create_info_struct(png_ptr);
1014 if (!info_ptr)
1015 {
1016 png_destroy_read_struct(&png_ptr,
1017 (png_infopp)NULL, (png_infopp)NULL);
1018 return (ERROR);
1019 }
1020
1021 png_infop end_info = png_create_info_struct(png_ptr);
1022 if (!end_info)
1023 {
1024 png_destroy_read_struct(&png_ptr, &info_ptr,
1025 (png_infopp)NULL);
1026 return (ERROR);
1027 }
1028
1029If you want to use your own memory allocation routines,
1030define PNG_USER_MEM_SUPPORTED and use
1031png_create_read_struct_2() instead of png_create_read_struct():
1032
1033 png_structp png_ptr = png_create_read_struct_2
1034 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
1035 user_error_fn, user_warning_fn, (png_voidp)
1036 user_mem_ptr, user_malloc_fn, user_free_fn);
1037
1038The error handling routines passed to png_create_read_struct()
1039and the memory alloc/free routines passed to png_create_struct_2()
1040are only necessary if you are not using the libpng supplied error
1041handling and memory alloc/free functions.
1042
1043When libpng encounters an error, it expects to longjmp back
1044to your routine. Therefore, you will need to call setjmp and pass
1045your png_jmpbuf(png_ptr). If you read the file from different
1046routines, you will need to update the jmpbuf field every time you enter
1047a new routine that will call a png_*() function.
1048
1049See your documentation of setjmp/longjmp for your compiler for more
1050information on setjmp/longjmp. See the discussion on libpng error
1051handling in the Customizing Libpng section below for more information
1052on the libpng error handling. If an error occurs, and libpng longjmp's
1053back to your setjmp, you will want to call png_destroy_read_struct() to
1054free any memory.
1055
1056 if (setjmp(png_jmpbuf(png_ptr)))
1057 {
1058 png_destroy_read_struct(&png_ptr, &info_ptr,
1059 &end_info);
1060 fclose(fp);
1061 return (ERROR);
1062 }
1063
1064If you would rather avoid the complexity of setjmp/longjmp issues,
1065you can compile libpng with PNG_SETJMP_NOT_SUPPORTED, in which case
1066errors will result in a call to PNG_ABORT() which defaults to abort().
1067
1068Now you need to set up the input code. The default for libpng is to
1069use the C function fread(). If you use this, you will need to pass a
1070valid FILE * in the function png_init_io(). Be sure that the file is
1071opened in binary mode. If you wish to handle reading data in another
1072way, you need not call the png_init_io() function, but you must then
1073implement the libpng I/O methods discussed in the Customizing Libpng
1074section below.
1075
1076 png_init_io(png_ptr, fp);
1077
1078If you had previously opened the file and read any of the signature from
1079the beginning in order to see if this was a PNG file, you need to let
1080libpng know that there are some bytes missing from the start of the file.
1081
1082 png_set_sig_bytes(png_ptr, number);
1083
1084.SS Setting up callback code
1085
1086You can set up a callback function to handle any unknown chunks in the
1087input stream. You must supply the function
1088
1089 read_chunk_callback(png_ptr ptr,
1090 png_unknown_chunkp chunk);
1091 {
1092 /* The unknown chunk structure contains your
1093 chunk data, along with similar data for any other
1094 unknown chunks: */
1095
1096 png_byte name[5];
1097 png_byte *data;
1098 png_size_t size;
1099
1100 /* Note that libpng has already taken care of
1101 the CRC handling */
1102
1103 /* put your code here. Search for your chunk in the
1104 unknown chunk structure, process it, and return one
1105 of the following: */
1106
1107 return (-n); /* chunk had an error */
1108 return (0); /* did not recognize */
1109 return (n); /* success */
1110 }
1111
1112(You can give your function another name that you like instead of
1113"read_chunk_callback")
1114
1115To inform libpng about your function, use
1116
1117 png_set_read_user_chunk_fn(png_ptr, user_chunk_ptr,
1118 read_chunk_callback);
1119
1120This names not only the callback function, but also a user pointer that
1121you can retrieve with
1122
1123 png_get_user_chunk_ptr(png_ptr);
1124
1125If you call the png_set_read_user_chunk_fn() function, then all unknown
1126chunks will be saved when read, in case your callback function will need
1127one or more of them. This behavior can be changed with the
1128png_set_keep_unknown_chunks() function, described below.
1129
1130At this point, you can set up a callback function that will be
1131called after each row has been read, which you can use to control
1132a progress meter or the like. It's demonstrated in pngtest.c.
1133You must supply a function
1134
1135 void read_row_callback(png_ptr ptr, png_uint_32 row,
1136 int pass);
1137 {
1138 /* put your code here */
1139 }
1140
1141(You can give it another name that you like instead of "read_row_callback")
1142
1143To inform libpng about your function, use
1144
1145 png_set_read_status_fn(png_ptr, read_row_callback);
1146
1147.SS Unknown-chunk handling
1148
1149Now you get to set the way the library processes unknown chunks in the
1150input PNG stream. Both known and unknown chunks will be read. Normal
1151behavior is that known chunks will be parsed into information in
1152various info_ptr members while unknown chunks will be discarded. This
1153behavior can be wasteful if your application will never use some known
1154chunk types. To change this, you can call:
1155
1156 png_set_keep_unknown_chunks(png_ptr, keep,
1157 chunk_list, num_chunks);
1158 keep - 0: default unknown chunk handling
1159 1: ignore; do not keep
1160 2: keep only if safe-to-copy
1161 3: keep even if unsafe-to-copy
1162 You can use these definitions:
1163 PNG_HANDLE_CHUNK_AS_DEFAULT 0
1164 PNG_HANDLE_CHUNK_NEVER 1
1165 PNG_HANDLE_CHUNK_IF_SAFE 2
1166 PNG_HANDLE_CHUNK_ALWAYS 3
1167 chunk_list - list of chunks affected (a byte string,
1168 five bytes per chunk, NULL or '\0' if
1169 num_chunks is 0)
1170 num_chunks - number of chunks affected; if 0, all
1171 unknown chunks are affected. If nonzero,
1172 only the chunks in the list are affected
1173
1174Unknown chunks declared in this way will be saved as raw data onto a
1175list of png_unknown_chunk structures. If a chunk that is normally
1176known to libpng is named in the list, it will be handled as unknown,
1177according to the "keep" directive. If a chunk is named in successive
1178instances of png_set_keep_unknown_chunks(), the final instance will
1179take precedence. The IHDR and IEND chunks should not be named in
1180chunk_list; if they are, libpng will process them normally anyway.
1181
1182Here is an example of the usage of png_set_keep_unknown_chunks(),
1183where the private "vpAg" chunk will later be processed by a user chunk
1184callback function:
1185
1186 png_byte vpAg[5]={118, 112, 65, 103, (png_byte) '\0'};
1187
1188 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1189 png_byte unused_chunks[]=
1190 {
1191 104, 73, 83, 84, (png_byte) '\0', /* hIST */
1192 105, 84, 88, 116, (png_byte) '\0', /* iTXt */
1193 112, 67, 65, 76, (png_byte) '\0', /* pCAL */
1194 115, 67, 65, 76, (png_byte) '\0', /* sCAL */
1195 115, 80, 76, 84, (png_byte) '\0', /* sPLT */
1196 116, 73, 77, 69, (png_byte) '\0', /* tIME */
1197 };
1198 #endif
1199
1200 ...
1201
1202 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1203 /* ignore all unknown chunks: */
1204 png_set_keep_unknown_chunks(read_ptr, 1, NULL, 0);
1205 /* except for vpAg: */
1206 png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1);
1207 /* also ignore unused known chunks: */
1208 png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks,
1209 (int)sizeof(unused_chunks)/5);
1210 #endif
1211
1212.SS User limits
1213
1214The PNG specification allows the width and height of an image to be as
1215large as 2^31-1 (0x7fffffff), or about 2.147 billion rows and columns.
1216Since very few applications really need to process such large images,
1217we have imposed an arbitrary 1-million limit on rows and columns.
1218Larger images will be rejected immediately with a png_error() call. If
1219you wish to override this limit, you can use
1220
1221 png_set_user_limits(png_ptr, width_max, height_max);
1222
1223to set your own limits, or use width_max = height_max = 0x7fffffffL
1224to allow all valid dimensions (libpng may reject some very large images
1225anyway because of potential buffer overflow conditions).
1226
1227You should put this statement after you create the PNG structure and
1228before calling png_read_info(), png_read_png(), or png_process_data().
1229If you need to retrieve the limits that are being applied, use
1230
1231 width_max = png_get_user_width_max(png_ptr);
1232 height_max = png_get_user_height_max(png_ptr);
1233
1234.SS The high-level read interface
1235
1236At this point there are two ways to proceed; through the high-level
1237read interface, or through a sequence of low-level read operations.
1238You can use the high-level interface if (a) you are willing to read
1239the entire image into memory, and (b) the input transformations
1240you want to do are limited to the following set:
1241
1242 PNG_TRANSFORM_IDENTITY No transformation
1243 PNG_TRANSFORM_STRIP_16 Strip 16-bit samples to
1244 8 bits
1245 PNG_TRANSFORM_STRIP_ALPHA Discard the alpha channel
1246 PNG_TRANSFORM_PACKING Expand 1, 2 and 4-bit
1247 samples to bytes
1248 PNG_TRANSFORM_PACKSWAP Change order of packed
1249 pixels to LSB first
1250 PNG_TRANSFORM_EXPAND Perform set_expand()
1251 PNG_TRANSFORM_INVERT_MONO Invert monochrome images
1252 PNG_TRANSFORM_SHIFT Normalize pixels to the
1253 sBIT depth
1254 PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA
1255 to BGRA
1256 PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA
1257 to AG
1258 PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity
1259 to transparency
1260 PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples
1261
1262(This excludes setting a background color, doing gamma transformation,
1263dithering, and setting filler.) If this is the case, simply do this:
1264
1265 png_read_png(png_ptr, info_ptr, png_transforms, NULL)
1266
1267where png_transforms is an integer containing the bitwise OR of
1268some set of transformation flags. This call is equivalent to png_read_info(),
1269followed the set of transformations indicated by the transform mask,
1270then png_read_image(), and finally png_read_end().
1271
1272(The final parameter of this call is not yet used. Someday it might point
1273to transformation parameters required by some future input transform.)
1274
1275You must use png_transforms and not call any png_set_transform() functions
1276when you use png_read_png().
1277
1278After you have called png_read_png(), you can retrieve the image data
1279with
1280
1281 row_pointers = png_get_rows(png_ptr, info_ptr);
1282
1283where row_pointers is an array of pointers to the pixel data for each row:
1284
1285 png_bytep row_pointers[height];
1286
1287If you know your image size and pixel size ahead of time, you can allocate
1288row_pointers prior to calling png_read_png() with
1289
1290 if (height > PNG_UINT_32_MAX/png_sizeof(png_byte))
1291 png_error (png_ptr,
1292 "Image is too tall to process in memory");
1293 if (width > PNG_UINT_32_MAX/pixel_size)
1294 png_error (png_ptr,
1295 "Image is too wide to process in memory");
1296 row_pointers = png_malloc(png_ptr,
1297 height*png_sizeof(png_bytep));
1298 for (int i=0; i<height, i++)
1299 row_pointers[i]=NULL; /* security precaution */
1300 for (int i=0; i<height, i++)
1301 row_pointers[i]=png_malloc(png_ptr,
1302 width*pixel_size);
1303 png_set_rows(png_ptr, info_ptr, &row_pointers);
1304
1305Alternatively you could allocate your image in one big block and define
1306row_pointers[i] to point into the proper places in your block.
1307
1308If you use png_set_rows(), the application is responsible for freeing
1309row_pointers (and row_pointers[i], if they were separately allocated).
1310
1311If you don't allocate row_pointers ahead of time, png_read_png() will
1312do it, and it'll be free'ed when you call png_destroy_*().
1313
1314.SS The low-level read interface
1315
1316If you are going the low-level route, you are now ready to read all
1317the file information up to the actual image data. You do this with a
1318call to png_read_info().
1319
1320 png_read_info(png_ptr, info_ptr);
1321
1322This will process all chunks up to but not including the image data.
1323
1324.SS Querying the info structure
1325
1326Functions are used to get the information from the info_ptr once it
1327has been read. Note that these fields may not be completely filled
1328in until png_read_end() has read the chunk data following the image.
1329
1330 png_get_IHDR(png_ptr, info_ptr, &width, &height,
1331 &bit_depth, &color_type, &interlace_type,
1332 &compression_type, &filter_method);
1333
1334 width - holds the width of the image
1335 in pixels (up to 2^31).
1336 height - holds the height of the image
1337 in pixels (up to 2^31).
1338 bit_depth - holds the bit depth of one of the
1339 image channels. (valid values are
1340 1, 2, 4, 8, 16 and depend also on
1341 the color_type. See also
1342 significant bits (sBIT) below).
1343 color_type - describes which color/alpha channels
1344 are present.
1345 PNG_COLOR_TYPE_GRAY
1346 (bit depths 1, 2, 4, 8, 16)
1347 PNG_COLOR_TYPE_GRAY_ALPHA
1348 (bit depths 8, 16)
1349 PNG_COLOR_TYPE_PALETTE
1350 (bit depths 1, 2, 4, 8)
1351 PNG_COLOR_TYPE_RGB
1352 (bit_depths 8, 16)
1353 PNG_COLOR_TYPE_RGB_ALPHA
1354 (bit_depths 8, 16)
1355
1356 PNG_COLOR_MASK_PALETTE
1357 PNG_COLOR_MASK_COLOR
1358 PNG_COLOR_MASK_ALPHA
1359
1360 filter_method - (must be PNG_FILTER_TYPE_BASE
1361 for PNG 1.0, and can also be
1362 PNG_INTRAPIXEL_DIFFERENCING if
1363 the PNG datastream is embedded in
1364 a MNG-1.0 datastream)
1365 compression_type - (must be PNG_COMPRESSION_TYPE_BASE
1366 for PNG 1.0)
1367 interlace_type - (PNG_INTERLACE_NONE or
1368 PNG_INTERLACE_ADAM7)
1369 Any or all of interlace_type, compression_type, of
1370 filter_method can be NULL if you are
1371 not interested in their values.
1372
1373 channels = png_get_channels(png_ptr, info_ptr);
1374 channels - number of channels of info for the
1375 color type (valid values are 1 (GRAY,
1376 PALETTE), 2 (GRAY_ALPHA), 3 (RGB),
1377 4 (RGB_ALPHA or RGB + filler byte))
1378 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
1379 rowbytes - number of bytes needed to hold a row
1380
1381 signature = png_get_signature(png_ptr, info_ptr);
1382 signature - holds the signature read from the
1383 file (if any). The data is kept in
1384 the same offset it would be if the
1385 whole signature were read (i.e. if an
1386 application had already read in 4
1387 bytes of signature before starting
1388 libpng, the remaining 4 bytes would
1389 be in signature[4] through signature[7]
1390 (see png_set_sig_bytes())).
1391
1392
1393 width = png_get_image_width(png_ptr,
1394 info_ptr);
1395 height = png_get_image_height(png_ptr,
1396 info_ptr);
1397 bit_depth = png_get_bit_depth(png_ptr,
1398 info_ptr);
1399 color_type = png_get_color_type(png_ptr,
1400 info_ptr);
1401 filter_method = png_get_filter_type(png_ptr,
1402 info_ptr);
1403 compression_type = png_get_compression_type(png_ptr,
1404 info_ptr);
1405 interlace_type = png_get_interlace_type(png_ptr,
1406 info_ptr);
1407
1408
1409These are also important, but their validity depends on whether the chunk
1410has been read. The png_get_valid(png_ptr, info_ptr, PNG_INFO_<chunk>) and
1411png_get_<chunk>(png_ptr, info_ptr, ...) functions return non-zero if the
1412data has been read, or zero if it is missing. The parameters to the
1413png_get_<chunk> are set directly if they are simple data types, or a pointer
1414into the info_ptr is returned for any complex types.
1415
1416 png_get_PLTE(png_ptr, info_ptr, &palette,
1417 &num_palette);
1418 palette - the palette for the file
1419 (array of png_color)
1420 num_palette - number of entries in the palette
1421
1422 png_get_gAMA(png_ptr, info_ptr, &gamma);
1423 gamma - the gamma the file is written
1424 at (PNG_INFO_gAMA)
1425
1426 png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
1427 srgb_intent - the rendering intent (PNG_INFO_sRGB)
1428 The presence of the sRGB chunk
1429 means that the pixel data is in the
1430 sRGB color space. This chunk also
1431 implies specific values of gAMA and
1432 cHRM.
1433
1434 png_get_iCCP(png_ptr, info_ptr, &name,
1435 &compression_type, &profile, &proflen);
1436 name - The profile name.
1437 compression - The compression type; always
1438 PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
1439 You may give NULL to this argument to
1440 ignore it.
1441 profile - International Color Consortium color
1442 profile data. May contain NULs.
1443 proflen - length of profile data in bytes.
1444
1445 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1446 sig_bit - the number of significant bits for
1447 (PNG_INFO_sBIT) each of the gray,
1448 red, green, and blue channels,
1449 whichever are appropriate for the
1450 given color type (png_color_16)
1451
1452 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
1453 &trans_values);
1454 trans - array of transparent entries for
1455 palette (PNG_INFO_tRNS)
1456 trans_values - graylevel or color sample values of
1457 the single transparent color for
1458 non-paletted images (PNG_INFO_tRNS)
1459 num_trans - number of transparent entries
1460 (PNG_INFO_tRNS)
1461
1462 png_get_hIST(png_ptr, info_ptr, &hist);
1463 (PNG_INFO_hIST)
1464 hist - histogram of palette (array of
1465 png_uint_16)
1466
1467 png_get_tIME(png_ptr, info_ptr, &mod_time);
1468 mod_time - time image was last modified
1469 (PNG_VALID_tIME)
1470
1471 png_get_bKGD(png_ptr, info_ptr, &background);
1472 background - background color (PNG_VALID_bKGD)
1473 valid 16-bit red, green and blue
1474 values, regardless of color_type
1475
1476 num_comments = png_get_text(png_ptr, info_ptr,
1477 &text_ptr, &num_text);
1478 num_comments - number of comments
1479 text_ptr - array of png_text holding image
1480 comments
1481 text_ptr[i].compression - type of compression used
1482 on "text" PNG_TEXT_COMPRESSION_NONE
1483 PNG_TEXT_COMPRESSION_zTXt
1484 PNG_ITXT_COMPRESSION_NONE
1485 PNG_ITXT_COMPRESSION_zTXt
1486 text_ptr[i].key - keyword for comment. Must contain
1487 1-79 characters.
1488 text_ptr[i].text - text comments for current
1489 keyword. Can be empty.
1490 text_ptr[i].text_length - length of text string,
1491 after decompression, 0 for iTXt
1492 text_ptr[i].itxt_length - length of itxt string,
1493 after decompression, 0 for tEXt/zTXt
1494 text_ptr[i].lang - language of comment (empty
1495 string for unknown).
1496 text_ptr[i].lang_key - keyword in UTF-8
1497 (empty string for unknown).
1498 num_text - number of comments (same as
1499 num_comments; you can put NULL here
1500 to avoid the duplication)
1501 Note while png_set_text() will accept text, language,
1502 and translated keywords that can be NULL pointers, the
1503 structure returned by png_get_text will always contain
1504 regular zero-terminated C strings. They might be
1505 empty strings but they will never be NULL pointers.
1506
1507 num_spalettes = png_get_sPLT(png_ptr, info_ptr,
1508 &palette_ptr);
1509 palette_ptr - array of palette structures holding
1510 contents of one or more sPLT chunks
1511 read.
1512 num_spalettes - number of sPLT chunks read.
1513
1514 png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y,
1515 &unit_type);
1516 offset_x - positive offset from the left edge
1517 of the screen
1518 offset_y - positive offset from the top edge
1519 of the screen
1520 unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
1521
1522 png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y,
1523 &unit_type);
1524 res_x - pixels/unit physical resolution in
1525 x direction
1526 res_y - pixels/unit physical resolution in
1527 x direction
1528 unit_type - PNG_RESOLUTION_UNKNOWN,
1529 PNG_RESOLUTION_METER
1530
1531 png_get_sCAL(png_ptr, info_ptr, &unit, &width,
1532 &height)
1533 unit - physical scale units (an integer)
1534 width - width of a pixel in physical scale units
1535 height - height of a pixel in physical scale units
1536 (width and height are doubles)
1537
1538 png_get_sCAL_s(png_ptr, info_ptr, &unit, &width,
1539 &height)
1540 unit - physical scale units (an integer)
1541 width - width of a pixel in physical scale units
1542 height - height of a pixel in physical scale units
1543 (width and height are strings like "2.54")
1544
1545 num_unknown_chunks = png_get_unknown_chunks(png_ptr,
1546 info_ptr, &unknowns)
1547 unknowns - array of png_unknown_chunk
1548 structures holding unknown chunks
1549 unknowns[i].name - name of unknown chunk
1550 unknowns[i].data - data of unknown chunk
1551 unknowns[i].size - size of unknown chunk's data
1552 unknowns[i].location - position of chunk in file
1553
1554 The value of "i" corresponds to the order in which the
1555 chunks were read from the PNG file or inserted with the
1556 png_set_unknown_chunks() function.
1557
1558The data from the pHYs chunk can be retrieved in several convenient
1559forms:
1560
1561 res_x = png_get_x_pixels_per_meter(png_ptr,
1562 info_ptr)
1563 res_y = png_get_y_pixels_per_meter(png_ptr,
1564 info_ptr)
1565 res_x_and_y = png_get_pixels_per_meter(png_ptr,
1566 info_ptr)
1567 res_x = png_get_x_pixels_per_inch(png_ptr,
1568 info_ptr)
1569 res_y = png_get_y_pixels_per_inch(png_ptr,
1570 info_ptr)
1571 res_x_and_y = png_get_pixels_per_inch(png_ptr,
1572 info_ptr)
1573 aspect_ratio = png_get_pixel_aspect_ratio(png_ptr,
1574 info_ptr)
1575
1576 (Each of these returns 0 [signifying "unknown"] if
1577 the data is not present or if res_x is 0;
1578 res_x_and_y is 0 if res_x != res_y)
1579
1580The data from the oFFs chunk can be retrieved in several convenient
1581forms:
1582
1583 x_offset = png_get_x_offset_microns(png_ptr, info_ptr);
1584 y_offset = png_get_y_offset_microns(png_ptr, info_ptr);
1585 x_offset = png_get_x_offset_inches(png_ptr, info_ptr);
1586 y_offset = png_get_y_offset_inches(png_ptr, info_ptr);
1587
1588 (Each of these returns 0 [signifying "unknown" if both
1589 x and y are 0] if the data is not present or if the
1590 chunk is present but the unit is the pixel)
1591
1592For more information, see the png_info definition in png.h and the
1593PNG specification for chunk contents. Be careful with trusting
1594rowbytes, as some of the transformations could increase the space
1595needed to hold a row (expand, filler, gray_to_rgb, etc.).
1596See png_read_update_info(), below.
1597
1598A quick word about text_ptr and num_text. PNG stores comments in
1599keyword/text pairs, one pair per chunk, with no limit on the number
1600of text chunks, and a 2^31 byte limit on their size. While there are
1601suggested keywords, there is no requirement to restrict the use to these
1602strings. It is strongly suggested that keywords and text be sensible
1603to humans (that's the point), so don't use abbreviations. Non-printing
1604symbols are not allowed. See the PNG specification for more details.
1605There is also no requirement to have text after the keyword.
1606
1607Keywords should be limited to 79 Latin-1 characters without leading or
1608trailing spaces, but non-consecutive spaces are allowed within the
1609keyword. It is possible to have the same keyword any number of times.
1610The text_ptr is an array of png_text structures, each holding a
1611pointer to a language string, a pointer to a keyword and a pointer to
1612a text string. The text string, language code, and translated
1613keyword may be empty or NULL pointers. The keyword/text
1614pairs are put into the array in the order that they are received.
1615However, some or all of the text chunks may be after the image, so, to
1616make sure you have read all the text chunks, don't mess with these
1617until after you read the stuff after the image. This will be
1618mentioned again below in the discussion that goes with png_read_end().
1619
1620.SS Input transformations
1621
1622After you've read the header information, you can set up the library
1623to handle any special transformations of the image data. The various
1624ways to transform the data will be described in the order that they
1625should occur. This is important, as some of these change the color
1626type and/or bit depth of the data, and some others only work on
1627certain color types and bit depths. Even though each transformation
1628checks to see if it has data that it can do something with, you should
1629make sure to only enable a transformation if it will be valid for the
1630data. For example, don't swap red and blue on grayscale data.
1631
1632The colors used for the background and transparency values should be
1633supplied in the same format/depth as the current image data. They
1634are stored in the same format/depth as the image data in a bKGD or tRNS
1635chunk, so this is what libpng expects for this data. The colors are
1636transformed to keep in sync with the image data when an application
1637calls the png_read_update_info() routine (see below).
1638
1639Data will be decoded into the supplied row buffers packed into bytes
1640unless the library has been told to transform it into another format.
1641For example, 4 bit/pixel paletted or grayscale data will be returned
16422 pixels/byte with the leftmost pixel in the high-order bits of the
1643byte, unless png_set_packing() is called. 8-bit RGB data will be stored
1644in RGB RGB RGB format unless png_set_filler() or png_set_add_alpha()
1645is called to insert filler bytes, either before or after each RGB triplet.
164616-bit RGB data will be returned RRGGBB RRGGBB, with the most significant
1647byte of the color value first, unless png_set_strip_16() is called to
1648transform it to regular RGB RGB triplets, or png_set_filler() or
1649png_set_add alpha() is called to insert filler bytes, either before or
1650after each RRGGBB triplet. Similarly, 8-bit or 16-bit grayscale data can
1651be modified with
1652png_set_filler(), png_set_add_alpha(), or png_set_strip_16().
1653
1654The following code transforms grayscale images of less than 8 to 8 bits,
1655changes paletted images to RGB, and adds a full alpha channel if there is
1656transparency information in a tRNS chunk. This is most useful on
1657grayscale images with bit depths of 2 or 4 or if there is a multiple-image
1658viewing application that wishes to treat all images in the same way.
1659
1660 if (color_type == PNG_COLOR_TYPE_PALETTE)
1661 png_set_palette_to_rgb(png_ptr);
1662
1663 if (color_type == PNG_COLOR_TYPE_GRAY &&
1664 bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
1665
1666 if (png_get_valid(png_ptr, info_ptr,
1667 PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
1668
1669These three functions are actually aliases for png_set_expand(), added
1670in libpng version 1.0.4, with the function names expanded to improve code
1671readability. In some future version they may actually do different
1672things.
1673
1674As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() was
1675added. It expands the sample depth without changing tRNS to alpha.
1676
1677PNG can have files with 16 bits per channel. If you only can handle
16788 bits per channel, this will strip the pixels down to 8 bit.
1679
1680 if (bit_depth == 16)
1681 png_set_strip_16(png_ptr);
1682
1683If, for some reason, you don't need the alpha channel on an image,
1684and you want to remove it rather than combining it with the background
1685(but the image author certainly had in mind that you *would* combine
1686it with the background, so that's what you should probably do):
1687
1688 if (color_type & PNG_COLOR_MASK_ALPHA)
1689 png_set_strip_alpha(png_ptr);
1690
1691In PNG files, the alpha channel in an image
1692is the level of opacity. If you need the alpha channel in an image to
1693be the level of transparency instead of opacity, you can invert the
1694alpha channel (or the tRNS chunk data) after it's read, so that 0 is
1695fully opaque and 255 (in 8-bit or paletted images) or 65535 (in 16-bit
1696images) is fully transparent, with
1697
1698 png_set_invert_alpha(png_ptr);
1699
1700PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
1701they can, resulting in, for example, 8 pixels per byte for 1 bit
1702files. This code expands to 1 pixel per byte without changing the
1703values of the pixels:
1704
1705 if (bit_depth < 8)
1706 png_set_packing(png_ptr);
1707
1708PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixels
1709stored in a PNG image have been "scaled" or "shifted" up to the next
1710higher possible bit depth (e.g. from 5 bits/sample in the range [0,31] to
17118 bits/sample in the range [0, 255]). However, it is also possible to
1712convert the PNG pixel data back to the original bit depth of the image.
1713This call reduces the pixels back down to the original bit depth:
1714
1715 png_color_8p sig_bit;
1716
1717 if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
1718 png_set_shift(png_ptr, sig_bit);
1719
1720PNG files store 3-color pixels in red, green, blue order. This code
1721changes the storage of the pixels to blue, green, red:
1722
1723 if (color_type == PNG_COLOR_TYPE_RGB ||
1724 color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1725 png_set_bgr(png_ptr);
1726
1727PNG files store RGB pixels packed into 3 or 6 bytes. This code expands them
1728into 4 or 8 bytes for windowing systems that need them in this format:
1729
1730 if (color_type == PNG_COLOR_TYPE_RGB)
1731 png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE);
1732
1733where "filler" is the 8 or 16-bit number to fill with, and the location is
1734either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
1735you want the filler before the RGB or after. This transformation
1736does not affect images that already have full alpha channels. To add an
1737opaque alpha channel, use filler=0xff or 0xffff and PNG_FILLER_AFTER which
1738will generate RGBA pixels.
1739
1740Note that png_set_filler() does not change the color type. If you want
1741to do that, you can add a true alpha channel with
1742
1743 if (color_type == PNG_COLOR_TYPE_RGB ||
1744 color_type == PNG_COLOR_TYPE_GRAY)
1745 png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
1746
1747where "filler" contains the alpha value to assign to each pixel.
1748This function was added in libpng-1.2.7.
1749
1750If you are reading an image with an alpha channel, and you need the
1751data as ARGB instead of the normal PNG format RGBA:
1752
1753 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1754 png_set_swap_alpha(png_ptr);
1755
1756For some uses, you may want a grayscale image to be represented as
1757RGB. This code will do that conversion:
1758
1759 if (color_type == PNG_COLOR_TYPE_GRAY ||
1760 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1761 png_set_gray_to_rgb(png_ptr);
1762
1763Conversely, you can convert an RGB or RGBA image to grayscale or grayscale
1764with alpha.
1765
1766 if (color_type == PNG_COLOR_TYPE_RGB ||
1767 color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1768 png_set_rgb_to_gray_fixed(png_ptr, error_action,
1769 int red_weight, int green_weight);
1770
1771 error_action = 1: silently do the conversion
1772 error_action = 2: issue a warning if the original
1773 image has any pixel where
1774 red != green or red != blue
1775 error_action = 3: issue an error and abort the
1776 conversion if the original
1777 image has any pixel where
1778 red != green or red != blue
1779
1780 red_weight: weight of red component times 100000
1781 green_weight: weight of green component times 100000
1782 If either weight is negative, default
1783 weights (21268, 71514) are used.
1784
1785If you have set error_action = 1 or 2, you can
1786later check whether the image really was gray, after processing
1787the image rows, with the png_get_rgb_to_gray_status(png_ptr) function.
1788It will return a png_byte that is zero if the image was gray or
17891 if there were any non-gray pixels. bKGD and sBIT data
1790will be silently converted to grayscale, using the green channel
1791data, regardless of the error_action setting.
1792
1793With red_weight+green_weight<=100000,
1794the normalized graylevel is computed:
1795
1796 int rw = red_weight * 65536;
1797 int gw = green_weight * 65536;
1798 int bw = 65536 - (rw + gw);
1799 gray = (rw*red + gw*green + bw*blue)/65536;
1800
1801The default values approximate those recommended in the Charles
1802Poynton's Color FAQ, <http://www.inforamp.net/~poynton/>
1803Copyright (c) 1998-01-04 Charles Poynton <poynton at inforamp.net>
1804
1805 Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
1806
1807Libpng approximates this with
1808
1809 Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
1810
1811which can be expressed with integers as
1812
1813 Y = (6969 * R + 23434 * G + 2365 * B)/32768
1814
1815The calculation is done in a linear colorspace, if the image gamma
1816is known.
1817
1818If you have a grayscale and you are using png_set_expand_depth(),
1819png_set_expand(), or png_set_gray_to_rgb to change to truecolor or to
1820a higher bit-depth, you must either supply the background color as a gray
1821value at the original file bit-depth (need_expand = 1) or else supply the
1822background color as an RGB triplet at the final, expanded bit depth
1823(need_expand = 0). Similarly, if you are reading a paletted image, you
1824must either supply the background color as a palette index (need_expand = 1)
1825or as an RGB triplet that may or may not be in the palette (need_expand = 0).
1826
1827 png_color_16 my_background;
1828 png_color_16p image_background;
1829
1830 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
1831 png_set_background(png_ptr, image_background,
1832 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
1833 else
1834 png_set_background(png_ptr, &my_background,
1835 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
1836
1837The png_set_background() function tells libpng to composite images
1838with alpha or simple transparency against the supplied background
1839color. If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
1840you may use this color, or supply another color more suitable for
1841the current display (e.g., the background color from a web page). You
1842need to tell libpng whether the color is in the gamma space of the
1843display (PNG_BACKGROUND_GAMMA_SCREEN for colors you supply), the file
1844(PNG_BACKGROUND_GAMMA_FILE for colors from the bKGD chunk), or one
1845that is neither of these gammas (PNG_BACKGROUND_GAMMA_UNIQUE - I don't
1846know why anyone would use this, but it's here).
1847
1848To properly display PNG images on any kind of system, the application needs
1849to know what the display gamma is. Ideally, the user will know this, and
1850the application will allow them to set it. One method of allowing the user
1851to set the display gamma separately for each system is to check for a
1852SCREEN_GAMMA or DISPLAY_GAMMA environment variable, which will hopefully be
1853correctly set.
1854
1855Note that display_gamma is the overall gamma correction required to produce
1856pleasing results, which depends on the lighting conditions in the surrounding
1857environment. In a dim or brightly lit room, no compensation other than
1858the physical gamma exponent of the monitor is needed, while in a dark room
1859a slightly smaller exponent is better.
1860
1861 double gamma, screen_gamma;
1862
1863 if (/* We have a user-defined screen
1864 gamma value */)
1865 {
1866 screen_gamma = user_defined_screen_gamma;
1867 }
1868 /* One way that applications can share the same
1869 screen gamma value */
1870 else if ((gamma_str = getenv("SCREEN_GAMMA"))
1871 != NULL)
1872 {
1873 screen_gamma = (double)atof(gamma_str);
1874 }
1875 /* If we don't have another value */
1876 else
1877 {
1878 screen_gamma = 2.2; /* A good guess for a
1879 PC monitor in a bright office or a dim room */
1880 screen_gamma = 2.0; /* A good guess for a
1881 PC monitor in a dark room */
1882 screen_gamma = 1.7 or 1.0; /* A good
1883 guess for Mac systems */
1884 }
1885
1886The png_set_gamma() function handles gamma transformations of the data.
1887Pass both the file gamma and the current screen_gamma. If the file does
1888not have a gamma value, you can pass one anyway if you have an idea what
1889it is (usually 0.45455 is a good guess for GIF images on PCs). Note
1890that file gammas are inverted from screen gammas. See the discussions
1891on gamma in the PNG specification for an excellent description of what
1892gamma is, and why all applications should support it. It is strongly
1893recommended that PNG viewers support gamma correction.
1894
1895 if (png_get_gAMA(png_ptr, info_ptr, &gamma))
1896 png_set_gamma(png_ptr, screen_gamma, gamma);
1897 else
1898 png_set_gamma(png_ptr, screen_gamma, 0.45455);
1899
1900If you need to reduce an RGB file to a paletted file, or if a paletted
1901file has more entries then will fit on your screen, png_set_dither()
1902will do that. Note that this is a simple match dither that merely
1903finds the closest color available. This should work fairly well with
1904optimized palettes, and fairly badly with linear color cubes. If you
1905pass a palette that is larger then maximum_colors, the file will
1906reduce the number of colors in the palette so it will fit into
1907maximum_colors. If there is a histogram, it will use it to make
1908more intelligent choices when reducing the palette. If there is no
1909histogram, it may not do as good a job.
1910
1911 if (color_type & PNG_COLOR_MASK_COLOR)
1912 {
1913 if (png_get_valid(png_ptr, info_ptr,
1914 PNG_INFO_PLTE))
1915 {
1916 png_uint_16p histogram = NULL;
1917
1918 png_get_hIST(png_ptr, info_ptr,
1919 &histogram);
1920 png_set_dither(png_ptr, palette, num_palette,
1921 max_screen_colors, histogram, 1);
1922 }
1923 else
1924 {
1925 png_color std_color_cube[MAX_SCREEN_COLORS] =
1926 { ... colors ... };
1927
1928 png_set_dither(png_ptr, std_color_cube,
1929 MAX_SCREEN_COLORS, MAX_SCREEN_COLORS,
1930 NULL,0);
1931 }
1932 }
1933
1934PNG files describe monochrome as black being zero and white being one.
1935The following code will reverse this (make black be one and white be
1936zero):
1937
1938 if (bit_depth == 1 && color_type == PNG_COLOR_TYPE_GRAY)
1939 png_set_invert_mono(png_ptr);
1940
1941This function can also be used to invert grayscale and gray-alpha images:
1942
1943 if (color_type == PNG_COLOR_TYPE_GRAY ||
1944 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1945 png_set_invert_mono(png_ptr);
1946
1947PNG files store 16 bit pixels in network byte order (big-endian,
1948ie. most significant bits first). This code changes the storage to the
1949other way (little-endian, i.e. least significant bits first, the
1950way PCs store them):
1951
1952 if (bit_depth == 16)
1953 png_set_swap(png_ptr);
1954
1955If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
1956need to change the order the pixels are packed into bytes, you can use:
1957
1958 if (bit_depth < 8)
1959 png_set_packswap(png_ptr);
1960
1961Finally, you can write your own transformation function if none of
1962the existing ones meets your needs. This is done by setting a callback
1963with
1964
1965 png_set_read_user_transform_fn(png_ptr,
1966 read_transform_fn);
1967
1968You must supply the function
1969
1970 void read_transform_fn(png_ptr ptr, row_info_ptr
1971 row_info, png_bytep data)
1972
1973See pngtest.c for a working example. Your function will be called
1974after all of the other transformations have been processed.
1975
1976You can also set up a pointer to a user structure for use by your
1977callback function, and you can inform libpng that your transform
1978function will change the number of channels or bit depth with the
1979function
1980
1981 png_set_user_transform_info(png_ptr, user_ptr,
1982 user_depth, user_channels);
1983
1984The user's application, not libpng, is responsible for allocating and
1985freeing any memory required for the user structure.
1986
1987You can retrieve the pointer via the function
1988png_get_user_transform_ptr(). For example:
1989
1990 voidp read_user_transform_ptr =
1991 png_get_user_transform_ptr(png_ptr);
1992
1993The last thing to handle is interlacing; this is covered in detail below,
1994but you must call the function here if you want libpng to handle expansion
1995of the interlaced image.
1996
1997 number_of_passes = png_set_interlace_handling(png_ptr);
1998
1999After setting the transformations, libpng can update your png_info
2000structure to reflect any transformations you've requested with this
2001call. This is most useful to update the info structure's rowbytes
2002field so you can use it to allocate your image memory. This function
2003will also update your palette with the correct screen_gamma and
2004background if these have been given with the calls above.
2005
2006 png_read_update_info(png_ptr, info_ptr);
2007
2008After you call png_read_update_info(), you can allocate any
2009memory you need to hold the image. The row data is simply
2010raw byte data for all forms of images. As the actual allocation
2011varies among applications, no example will be given. If you
2012are allocating one large chunk, you will need to build an
2013array of pointers to each row, as it will be needed for some
2014of the functions below.
2015
2016.SS Reading image data
2017
2018After you've allocated memory, you can read the image data.
2019The simplest way to do this is in one function call. If you are
2020allocating enough memory to hold the whole image, you can just
2021call png_read_image() and libpng will read in all the image data
2022and put it in the memory area supplied. You will need to pass in
2023an array of pointers to each row.
2024
2025This function automatically handles interlacing, so you don't need
2026to call png_set_interlace_handling() or call this function multiple
2027times, or any of that other stuff necessary with png_read_rows().
2028
2029 png_read_image(png_ptr, row_pointers);
2030
2031where row_pointers is:
2032
2033 png_bytep row_pointers[height];
2034
2035You can point to void or char or whatever you use for pixels.
2036
2037If you don't want to read in the whole image at once, you can
2038use png_read_rows() instead. If there is no interlacing (check
2039interlace_type == PNG_INTERLACE_NONE), this is simple:
2040
2041 png_read_rows(png_ptr, row_pointers, NULL,
2042 number_of_rows);
2043
2044where row_pointers is the same as in the png_read_image() call.
2045
2046If you are doing this just one row at a time, you can do this with
2047a single row_pointer instead of an array of row_pointers:
2048
2049 png_bytep row_pointer = row;
2050 png_read_row(png_ptr, row_pointer, NULL);
2051
2052If the file is interlaced (interlace_type != 0 in the IHDR chunk), things
2053get somewhat harder. The only current (PNG Specification version 1.2)
2054interlacing type for PNG is (interlace_type == PNG_INTERLACE_ADAM7)
2055is a somewhat complicated 2D interlace scheme, known as Adam7, that
2056breaks down an image into seven smaller images of varying size, based
2057on an 8x8 grid.
2058
2059libpng can fill out those images or it can give them to you "as is".
2060If you want them filled out, there are two ways to do that. The one
2061mentioned in the PNG specification is to expand each pixel to cover
2062those pixels that have not been read yet (the "rectangle" method).
2063This results in a blocky image for the first pass, which gradually
2064smooths out as more pixels are read. The other method is the "sparkle"
2065method, where pixels are drawn only in their final locations, with the
2066rest of the image remaining whatever colors they were initialized to
2067before the start of the read. The first method usually looks better,
2068but tends to be slower, as there are more pixels to put in the rows.
2069
2070If you don't want libpng to handle the interlacing details, just call
2071png_read_rows() seven times to read in all seven images. Each of the
2072images is a valid image by itself, or they can all be combined on an
20738x8 grid to form a single image (although if you intend to combine them
2074you would be far better off using the libpng interlace handling).
2075
2076The first pass will return an image 1/8 as wide as the entire image
2077(every 8th column starting in column 0) and 1/8 as high as the original
2078(every 8th row starting in row 0), the second will be 1/8 as wide
2079(starting in column 4) and 1/8 as high (also starting in row 0). The
2080third pass will be 1/4 as wide (every 4th pixel starting in column 0) and
20811/8 as high (every 8th row starting in row 4), and the fourth pass will
2082be 1/4 as wide and 1/4 as high (every 4th column starting in column 2,
2083and every 4th row starting in row 0). The fifth pass will return an
2084image 1/2 as wide, and 1/4 as high (starting at column 0 and row 2),
2085while the sixth pass will be 1/2 as wide and 1/2 as high as the original
2086(starting in column 1 and row 0). The seventh and final pass will be as
2087wide as the original, and 1/2 as high, containing all of the odd
2088numbered scanlines. Phew!
2089
2090If you want libpng to expand the images, call this before calling
2091png_start_read_image() or png_read_update_info():
2092
2093 if (interlace_type == PNG_INTERLACE_ADAM7)
2094 number_of_passes
2095 = png_set_interlace_handling(png_ptr);
2096
2097This will return the number of passes needed. Currently, this
2098is seven, but may change if another interlace type is added.
2099This function can be called even if the file is not interlaced,
2100where it will return one pass.
2101
2102If you are not going to display the image after each pass, but are
2103going to wait until the entire image is read in, use the sparkle
2104effect. This effect is faster and the end result of either method
2105is exactly the same. If you are planning on displaying the image
2106after each pass, the "rectangle" effect is generally considered the
2107better looking one.
2108
2109If you only want the "sparkle" effect, just call png_read_rows() as
2110normal, with the third parameter NULL. Make sure you make pass over
2111the image number_of_passes times, and you don't change the data in the
2112rows between calls. You can change the locations of the data, just
2113not the data. Each pass only writes the pixels appropriate for that
2114pass, and assumes the data from previous passes is still valid.
2115
2116 png_read_rows(png_ptr, row_pointers, NULL,
2117 number_of_rows);
2118
2119If you only want the first effect (the rectangles), do the same as
2120before except pass the row buffer in the third parameter, and leave
2121the second parameter NULL.
2122
2123 png_read_rows(png_ptr, NULL, row_pointers,
2124 number_of_rows);
2125
2126.SS Finishing a sequential read
2127
2128After you are finished reading the image through the
2129low-level interface, you can finish reading the file. If you are
2130interested in comments or time, which may be stored either before or
2131after the image data, you should pass the separate png_info struct if
2132you want to keep the comments from before and after the image
2133separate. If you are not interested, you can pass NULL.
2134
2135 png_read_end(png_ptr, end_info);
2136
2137When you are done, you can free all memory allocated by libpng like this:
2138
2139 png_destroy_read_struct(&png_ptr, &info_ptr,
2140 &end_info);
2141
2142It is also possible to individually free the info_ptr members that
2143point to libpng-allocated storage with the following function:
2144
2145 png_free_data(png_ptr, info_ptr, mask, seq)
2146 mask - identifies data to be freed, a mask
2147 containing the bitwise OR of one or
2148 more of
2149 PNG_FREE_PLTE, PNG_FREE_TRNS,
2150 PNG_FREE_HIST, PNG_FREE_ICCP,
2151 PNG_FREE_PCAL, PNG_FREE_ROWS,
2152 PNG_FREE_SCAL, PNG_FREE_SPLT,
2153 PNG_FREE_TEXT, PNG_FREE_UNKN,
2154 or simply PNG_FREE_ALL
2155 seq - sequence number of item to be freed
2156 (-1 for all items)
2157
2158This function may be safely called when the relevant storage has
2159already been freed, or has not yet been allocated, or was allocated
2160by the user and not by libpng, and will in those
2161cases do nothing. The "seq" parameter is ignored if only one item
2162of the selected data type, such as PLTE, is allowed. If "seq" is not
2163-1, and multiple items are allowed for the data type identified in
2164the mask, such as text or sPLT, only the n'th item in the structure
2165is freed, where n is "seq".
2166
2167The default behavior is only to free data that was allocated internally
2168by libpng. This can be changed, so that libpng will not free the data,
2169or so that it will free data that was allocated by the user with png_malloc()
2170or png_zalloc() and passed in via a png_set_*() function, with
2171
2172 png_data_freer(png_ptr, info_ptr, freer, mask)
2173 mask - which data elements are affected
2174 same choices as in png_free_data()
2175 freer - one of
2176 PNG_DESTROY_WILL_FREE_DATA
2177 PNG_SET_WILL_FREE_DATA
2178 PNG_USER_WILL_FREE_DATA
2179
2180This function only affects data that has already been allocated.
2181You can call this function after reading the PNG data but before calling
2182any png_set_*() functions, to control whether the user or the png_set_*()
2183function is responsible for freeing any existing data that might be present,
2184and again after the png_set_*() functions to control whether the user
2185or png_destroy_*() is supposed to free the data. When the user assumes
2186responsibility for libpng-allocated data, the application must use
2187png_free() to free it, and when the user transfers responsibility to libpng
2188for data that the user has allocated, the user must have used png_malloc()
2189or png_zalloc() to allocate it.
2190
2191If you allocated your row_pointers in a single block, as suggested above in
2192the description of the high level read interface, you must not transfer
2193responsibility for freeing it to the png_set_rows or png_read_destroy function,
2194because they would also try to free the individual row_pointers[i].
2195
2196If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword
2197separately, do not transfer responsibility for freeing text_ptr to libpng,
2198because when libpng fills a png_text structure it combines these members with
2199the key member, and png_free_data() will free only text_ptr.key. Similarly,
2200if you transfer responsibility for free'ing text_ptr from libpng to your
2201application, your application must not separately free those members.
2202
2203The png_free_data() function will turn off the "valid" flag for anything
2204it frees. If you need to turn the flag off for a chunk that was freed by your
2205application instead of by libpng, you can use
2206
2207 png_set_invalid(png_ptr, info_ptr, mask);
2208 mask - identifies the chunks to be made invalid,
2209 containing the bitwise OR of one or
2210 more of
2211 PNG_INFO_gAMA, PNG_INFO_sBIT,
2212 PNG_INFO_cHRM, PNG_INFO_PLTE,
2213 PNG_INFO_tRNS, PNG_INFO_bKGD,
2214 PNG_INFO_hIST, PNG_INFO_pHYs,
2215 PNG_INFO_oFFs, PNG_INFO_tIME,
2216 PNG_INFO_pCAL, PNG_INFO_sRGB,
2217 PNG_INFO_iCCP, PNG_INFO_sPLT,
2218 PNG_INFO_sCAL, PNG_INFO_IDAT
2219
2220For a more compact example of reading a PNG image, see the file example.c.
2221
2222.SS Reading PNG files progressively
2223
2224The progressive reader is slightly different then the non-progressive
2225reader. Instead of calling png_read_info(), png_read_rows(), and
2226png_read_end(), you make one call to png_process_data(), which calls
2227callbacks when it has the info, a row, or the end of the image. You
2228set up these callbacks with png_set_progressive_read_fn(). You don't
2229have to worry about the input/output functions of libpng, as you are
2230giving the library the data directly in png_process_data(). I will
2231assume that you have read the section on reading PNG files above,
2232so I will only highlight the differences (although I will show
2233all of the code).
2234
2235png_structp png_ptr;
2236png_infop info_ptr;
2237
2238 /* An example code fragment of how you would
2239 initialize the progressive reader in your
2240 application. */
2241 int
2242 initialize_png_reader()
2243 {
2244 png_ptr = png_create_read_struct
2245 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
2246 user_error_fn, user_warning_fn);
2247 if (!png_ptr)
2248 return (ERROR);
2249 info_ptr = png_create_info_struct(png_ptr);
2250 if (!info_ptr)
2251 {
2252 png_destroy_read_struct(&png_ptr, (png_infopp)NULL,
2253 (png_infopp)NULL);
2254 return (ERROR);
2255 }
2256
2257 if (setjmp(png_jmpbuf(png_ptr)))
2258 {
2259 png_destroy_read_struct(&png_ptr, &info_ptr,
2260 (png_infopp)NULL);
2261 return (ERROR);
2262 }
2263
2264 /* This one's new. You can provide functions
2265 to be called when the header info is valid,
2266 when each row is completed, and when the image
2267 is finished. If you aren't using all functions,
2268 you can specify NULL parameters. Even when all
2269 three functions are NULL, you need to call
2270 png_set_progressive_read_fn(). You can use
2271 any struct as the user_ptr (cast to a void pointer
2272 for the function call), and retrieve the pointer
2273 from inside the callbacks using the function
2274
2275 png_get_progressive_ptr(png_ptr);
2276
2277 which will return a void pointer, which you have
2278 to cast appropriately.
2279 */
2280 png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
2281 info_callback, row_callback, end_callback);
2282
2283 return 0;
2284 }
2285
2286 /* A code fragment that you call as you receive blocks
2287 of data */
2288 int
2289 process_data(png_bytep buffer, png_uint_32 length)
2290 {
2291 if (setjmp(png_jmpbuf(png_ptr)))
2292 {
2293 png_destroy_read_struct(&png_ptr, &info_ptr,
2294 (png_infopp)NULL);
2295 return (ERROR);
2296 }
2297
2298 /* This one's new also. Simply give it a chunk
2299 of data from the file stream (in order, of
2300 course). On machines with segmented memory
2301 models machines, don't give it any more than
2302 64K. The library seems to run fine with sizes
2303 of 4K. Although you can give it much less if
2304 necessary (I assume you can give it chunks of
2305 1 byte, I haven't tried less then 256 bytes
2306 yet). When this function returns, you may
2307 want to display any rows that were generated
2308 in the row callback if you don't already do
2309 so there.
2310 */
2311 png_process_data(png_ptr, info_ptr, buffer, length);
2312 return 0;
2313 }
2314
2315 /* This function is called (as set by
2316 png_set_progressive_read_fn() above) when enough data
2317 has been supplied so all of the header has been
2318 read.
2319 */
2320 void
2321 info_callback(png_structp png_ptr, png_infop info)
2322 {
2323 /* Do any setup here, including setting any of
2324 the transformations mentioned in the Reading
2325 PNG files section. For now, you _must_ call
2326 either png_start_read_image() or
2327 png_read_update_info() after all the
2328 transformations are set (even if you don't set
2329 any). You may start getting rows before
2330 png_process_data() returns, so this is your
2331 last chance to prepare for that.
2332 */
2333 }
2334
2335 /* This function is called when each row of image
2336 data is complete */
2337 void
2338 row_callback(png_structp png_ptr, png_bytep new_row,
2339 png_uint_32 row_num, int pass)
2340 {
2341 /* If the image is interlaced, and you turned
2342 on the interlace handler, this function will
2343 be called for every row in every pass. Some
2344 of these rows will not be changed from the
2345 previous pass. When the row is not changed,
2346 the new_row variable will be NULL. The rows
2347 and passes are called in order, so you don't
2348 really need the row_num and pass, but I'm
2349 supplying them because it may make your life
2350 easier.
2351
2352 For the non-NULL rows of interlaced images,
2353 you must call png_progressive_combine_row()
2354 passing in the row and the old row. You can
2355 call this function for NULL rows (it will just
2356 return) and for non-interlaced images (it just
2357 does the memcpy for you) if it will make the
2358 code easier. Thus, you can just do this for
2359 all cases:
2360 */
2361
2362 png_progressive_combine_row(png_ptr, old_row,
2363 new_row);
2364
2365 /* where old_row is what was displayed for
2366 previously for the row. Note that the first
2367 pass (pass == 0, really) will completely cover
2368 the old row, so the rows do not have to be
2369 initialized. After the first pass (and only
2370 for interlaced images), you will have to pass
2371 the current row, and the function will combine
2372 the old row and the new row.
2373 */
2374 }
2375
2376 void
2377 end_callback(png_structp png_ptr, png_infop info)
2378 {
2379 /* This function is called after the whole image
2380 has been read, including any chunks after the
2381 image (up to and including the IEND). You
2382 will usually have the same info chunk as you
2383 had in the header, although some data may have
2384 been added to the comments and time fields.
2385
2386 Most people won't do much here, perhaps setting
2387 a flag that marks the image as finished.
2388 */
2389 }
2390
2391
2392
2393.SH IV. Writing
2394
2395Much of this is very similar to reading. However, everything of
2396importance is repeated here, so you won't have to constantly look
2397back up in the reading section to understand writing.
2398
2399.SS Setup
2400
2401You will want to do the I/O initialization before you get into libpng,
2402so if it doesn't work, you don't have anything to undo. If you are not
2403using the standard I/O functions, you will need to replace them with
2404custom writing functions. See the discussion under Customizing libpng.
2405
2406 FILE *fp = fopen(file_name, "wb");
2407 if (!fp)
2408 {
2409 return (ERROR);
2410 }
2411
2412Next, png_struct and png_info need to be allocated and initialized.
2413As these can be both relatively large, you may not want to store these
2414on the stack, unless you have stack space to spare. Of course, you
2415will want to check if they return NULL. If you are also reading,
2416you won't want to name your read structure and your write structure
2417both "png_ptr"; you can call them anything you like, such as
2418"read_ptr" and "write_ptr". Look at pngtest.c, for example.
2419
2420 png_structp png_ptr = png_create_write_struct
2421 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
2422 user_error_fn, user_warning_fn);
2423 if (!png_ptr)
2424 return (ERROR);
2425
2426 png_infop info_ptr = png_create_info_struct(png_ptr);
2427 if (!info_ptr)
2428 {
2429 png_destroy_write_struct(&png_ptr,
2430 (png_infopp)NULL);
2431 return (ERROR);
2432 }
2433
2434If you want to use your own memory allocation routines,
2435define PNG_USER_MEM_SUPPORTED and use
2436png_create_write_struct_2() instead of png_create_write_struct():
2437
2438 png_structp png_ptr = png_create_write_struct_2
2439 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
2440 user_error_fn, user_warning_fn, (png_voidp)
2441 user_mem_ptr, user_malloc_fn, user_free_fn);
2442
2443After you have these structures, you will need to set up the
2444error handling. When libpng encounters an error, it expects to
2445longjmp() back to your routine. Therefore, you will need to call
2446setjmp() and pass the png_jmpbuf(png_ptr). If you
2447write the file from different routines, you will need to update
2448the png_jmpbuf(png_ptr) every time you enter a new routine that will
2449call a png_*() function. See your documentation of setjmp/longjmp
2450for your compiler for more information on setjmp/longjmp. See
2451the discussion on libpng error handling in the Customizing Libpng
2452section below for more information on the libpng error handling.
2453
2454 if (setjmp(png_jmpbuf(png_ptr)))
2455 {
2456 png_destroy_write_struct(&png_ptr, &info_ptr);
2457 fclose(fp);
2458 return (ERROR);
2459 }
2460 ...
2461 return;
2462
2463If you would rather avoid the complexity of setjmp/longjmp issues,
2464you can compile libpng with PNG_SETJMP_NOT_SUPPORTED, in which case
2465errors will result in a call to PNG_ABORT() which defaults to abort().
2466
2467Now you need to set up the output code. The default for libpng is to
2468use the C function fwrite(). If you use this, you will need to pass a
2469valid FILE * in the function png_init_io(). Be sure that the file is
2470opened in binary mode. Again, if you wish to handle writing data in
2471another way, see the discussion on libpng I/O handling in the Customizing
2472Libpng section below.
2473
2474 png_init_io(png_ptr, fp);
2475
2476If you are embedding your PNG into a datastream such as MNG, and don't
2477want libpng to write the 8-byte signature, or if you have already
2478written the signature in your application, use
2479
2480 png_set_sig_bytes(png_ptr, 8);
2481
2482to inform libpng that it should not write a signature.
2483
2484.SS Write callbacks
2485
2486At this point, you can set up a callback function that will be
2487called after each row has been written, which you can use to control
2488a progress meter or the like. It's demonstrated in pngtest.c.
2489You must supply a function
2490
2491 void write_row_callback(png_ptr, png_uint_32 row,
2492 int pass);
2493 {
2494 /* put your code here */
2495 }
2496
2497(You can give it another name that you like instead of "write_row_callback")
2498
2499To inform libpng about your function, use
2500
2501 png_set_write_status_fn(png_ptr, write_row_callback);
2502
2503You now have the option of modifying how the compression library will
2504run. The following functions are mainly for testing, but may be useful
2505in some cases, like if you need to write PNG files extremely fast and
2506are willing to give up some compression, or if you want to get the
2507maximum possible compression at the expense of slower writing. If you
2508have no special needs in this area, let the library do what it wants by
2509not calling this function at all, as it has been tuned to deliver a good
2510speed/compression ratio. The second parameter to png_set_filter() is
2511the filter method, for which the only valid values are 0 (as of the
2512July 1999 PNG specification, version 1.2) or 64 (if you are writing
2513a PNG datastream that is to be embedded in a MNG datastream). The third
2514parameter is a flag that indicates which filter type(s) are to be tested
2515for each scanline. See the PNG specification for details on the specific filter
2516types.
2517
2518
2519 /* turn on or off filtering, and/or choose
2520 specific filters. You can use either a single
2521 PNG_FILTER_VALUE_NAME or the bitwise OR of one
2522 or more PNG_FILTER_NAME masks. */
2523 png_set_filter(png_ptr, 0,
2524 PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE |
2525 PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB |
2526 PNG_FILTER_UP | PNG_FILTER_VALUE_UP |
2527 PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG |
2528 PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH|
2529 PNG_ALL_FILTERS);
2530
2531If an application
2532wants to start and stop using particular filters during compression,
2533it should start out with all of the filters (to ensure that the previous
2534row of pixels will be stored in case it's needed later), and then add
2535and remove them after the start of compression.
2536
2537If you are writing a PNG datastream that is to be embedded in a MNG
2538datastream, the second parameter can be either 0 or 64.
2539
2540The png_set_compression_*() functions interface to the zlib compression
2541library, and should mostly be ignored unless you really know what you are
2542doing. The only generally useful call is png_set_compression_level()
2543which changes how much time zlib spends on trying to compress the image
2544data. See the Compression Library (zlib.h and algorithm.txt, distributed
2545with zlib) for details on the compression levels.
2546
2547 /* set the zlib compression level */
2548 png_set_compression_level(png_ptr,
2549 Z_BEST_COMPRESSION);
2550
2551 /* set other zlib parameters */
2552 png_set_compression_mem_level(png_ptr, 8);
2553 png_set_compression_strategy(png_ptr,
2554 Z_DEFAULT_STRATEGY);
2555 png_set_compression_window_bits(png_ptr, 15);
2556 png_set_compression_method(png_ptr, 8);
2557 png_set_compression_buffer_size(png_ptr, 8192)
2558
2559extern PNG_EXPORT(void,png_set_zbuf_size)
2560
2561.SS Setting the contents of info for output
2562
2563You now need to fill in the png_info structure with all the data you
2564wish to write before the actual image. Note that the only thing you
2565are allowed to write after the image is the text chunks and the time
2566chunk (as of PNG Specification 1.2, anyway). See png_write_end() and
2567the latest PNG specification for more information on that. If you
2568wish to write them before the image, fill them in now, and flag that
2569data as being valid. If you want to wait until after the data, don't
2570fill them until png_write_end(). For all the fields in png_info and
2571their data types, see png.h. For explanations of what the fields
2572contain, see the PNG specification.
2573
2574Some of the more important parts of the png_info are:
2575
2576 png_set_IHDR(png_ptr, info_ptr, width, height,
2577 bit_depth, color_type, interlace_type,
2578 compression_type, filter_method)
2579 width - holds the width of the image
2580 in pixels (up to 2^31).
2581 height - holds the height of the image
2582 in pixels (up to 2^31).
2583 bit_depth - holds the bit depth of one of the
2584 image channels.
2585 (valid values are 1, 2, 4, 8, 16
2586 and depend also on the