1 | libpng.txt - A description on how to use and modify libpng
|
---|
2 |
|
---|
3 | libpng version 1.2.54 - November 12, 2015
|
---|
4 | Updated and distributed by Glenn Randers-Pehrson
|
---|
5 | <glennrp at users.sourceforge.net>
|
---|
6 | Copyright (c) 1998-2014 Glenn Randers-Pehrson
|
---|
7 |
|
---|
8 | This document is released under the libpng license.
|
---|
9 | For conditions of distribution and use, see the disclaimer
|
---|
10 | and license in png.h
|
---|
11 |
|
---|
12 | Based on:
|
---|
13 |
|
---|
14 | libpng versions 0.97, January 1998, through 1.2.54 - November 12, 2015
|
---|
15 | Updated and distributed by Glenn Randers-Pehrson
|
---|
16 | Copyright (c) 1998-2014 Glenn Randers-Pehrson
|
---|
17 |
|
---|
18 | libpng 1.0 beta 6 version 0.96 May 28, 1997
|
---|
19 | Updated and distributed by Andreas Dilger
|
---|
20 | Copyright (c) 1996, 1997 Andreas Dilger
|
---|
21 |
|
---|
22 | libpng 1.0 beta 2 - version 0.88 January 26, 1996
|
---|
23 | For conditions of distribution and use, see copyright
|
---|
24 | notice in png.h. Copyright (c) 1995, 1996 Guy Eric
|
---|
25 | Schalnat, Group 42, Inc.
|
---|
26 |
|
---|
27 | Updated/rewritten per request in the libpng FAQ
|
---|
28 | Copyright (c) 1995, 1996 Frank J. T. Wojcik
|
---|
29 | December 18, 1995 & January 20, 1996
|
---|
30 |
|
---|
31 | I. Introduction
|
---|
32 |
|
---|
33 | This file describes how to use and modify the PNG reference library
|
---|
34 | (known as libpng) for your own use. There are five sections to this
|
---|
35 | file: introduction, structures, reading, writing, and modification and
|
---|
36 | configuration notes for various special platforms. In addition to this
|
---|
37 | file, example.c is a good starting point for using the library, as
|
---|
38 | it is heavily commented and should include everything most people
|
---|
39 | will need. We assume that libpng is already installed; see the
|
---|
40 | INSTALL file for instructions on how to install libpng.
|
---|
41 |
|
---|
42 | For examples of libpng usage, see the files "example.c", "pngtest.c",
|
---|
43 | and the files in the "contrib" directory, all of which are included in
|
---|
44 | the libpng distribution.
|
---|
45 |
|
---|
46 | Libpng was written as a companion to the PNG specification, as a way
|
---|
47 | of reducing the amount of time and effort it takes to support the PNG
|
---|
48 | file format in application programs.
|
---|
49 |
|
---|
50 | The PNG specification (second edition), November 2003, is available as
|
---|
51 | a W3C Recommendation and as an ISO Standard (ISO/IEC 15948:2003 (E)) at
|
---|
52 | <http://www.w3.org/TR/2003/REC-PNG-20031110/
|
---|
53 | The W3C and ISO documents have identical technical content.
|
---|
54 |
|
---|
55 | The PNG-1.2 specification is available at
|
---|
56 | <http://png-mng.sourceforge.net/pub/png/spec/1.2/>.
|
---|
57 | It is technically equivalent
|
---|
58 | to the PNG specification (second edition) but has some additional material.
|
---|
59 |
|
---|
60 | The PNG-1.0 specification is available as RFC 2083
|
---|
61 | <http://png-mng.sourceforge.net/pub/png/spec/1.0/> and as a
|
---|
62 | W3C Recommendation <http://www.w3.org/TR/REC-png-961001>.
|
---|
63 |
|
---|
64 | Some additional chunks are described in the special-purpose public chunks
|
---|
65 | documents at <http://www.libpng.org/pub/png/spec/register/>
|
---|
66 |
|
---|
67 | Other information
|
---|
68 | about PNG, and the latest version of libpng, can be found at the PNG home
|
---|
69 | page, <http://www.libpng.org/pub/png/>.
|
---|
70 |
|
---|
71 | Most users will not have to modify the library significantly; advanced
|
---|
72 | users may want to modify it more. All attempts were made to make it as
|
---|
73 | complete as possible, while keeping the code easy to understand.
|
---|
74 | Currently, this library only supports C. Support for other languages
|
---|
75 | is being considered.
|
---|
76 |
|
---|
77 | Libpng has been designed to handle multiple sessions at one time,
|
---|
78 | to be easily modifiable, to be portable to the vast majority of
|
---|
79 | machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy
|
---|
80 | to use. The ultimate goal of libpng is to promote the acceptance of
|
---|
81 | the PNG file format in whatever way possible. While there is still
|
---|
82 | work to be done (see the TODO file), libpng should cover the
|
---|
83 | majority of the needs of its users.
|
---|
84 |
|
---|
85 | Libpng uses zlib for its compression and decompression of PNG files.
|
---|
86 | Further information about zlib, and the latest version of zlib, can
|
---|
87 | be found at the zlib home page, <http://zlib.net/>.
|
---|
88 | The zlib compression utility is a general purpose utility that is
|
---|
89 | useful for more than PNG files, and can be used without libpng.
|
---|
90 | See the documentation delivered with zlib for more details.
|
---|
91 | You can usually find the source files for the zlib utility wherever you
|
---|
92 | find the libpng source files.
|
---|
93 |
|
---|
94 | Libpng is thread safe, provided the threads are using different
|
---|
95 | instances of the structures. Each thread should have its own
|
---|
96 | png_struct and png_info instances, and thus its own image.
|
---|
97 | Libpng does not protect itself against two threads using the
|
---|
98 | same instance of a structure.
|
---|
99 |
|
---|
100 | II. Structures
|
---|
101 |
|
---|
102 | There are two main structures that are important to libpng, png_struct
|
---|
103 | and png_info. The first, png_struct, is an internal structure that
|
---|
104 | will not, for the most part, be used by a user except as the first
|
---|
105 | variable passed to every libpng function call.
|
---|
106 |
|
---|
107 | The png_info structure is designed to provide information about the
|
---|
108 | PNG file. At one time, the fields of png_info were intended to be
|
---|
109 | directly accessible to the user. However, this tended to cause problems
|
---|
110 | with applications using dynamically loaded libraries, and as a result
|
---|
111 | a set of interface functions for png_info (the png_get_*() and png_set_*()
|
---|
112 | functions) was developed. The fields of png_info are still available for
|
---|
113 | older applications, but it is suggested that applications use the new
|
---|
114 | interfaces if at all possible.
|
---|
115 |
|
---|
116 | Applications that do make direct access to the members of png_struct (except
|
---|
117 | for png_ptr->jmpbuf) must be recompiled whenever the library is updated,
|
---|
118 | and applications that make direct access to the members of png_info must
|
---|
119 | be recompiled if they were compiled or loaded with libpng version 1.0.6,
|
---|
120 | in which the members were in a different order. In version 1.0.7, the
|
---|
121 | members of the png_info structure reverted to the old order, as they were
|
---|
122 | in versions 0.97c through 1.0.5. Starting with version 2.0.0, both
|
---|
123 | structures are going to be hidden, and the contents of the structures will
|
---|
124 | only be accessible through the png_get/png_set functions.
|
---|
125 |
|
---|
126 | The png.h header file is an invaluable reference for programming with libpng.
|
---|
127 | And while I'm on the topic, make sure you include the libpng header file:
|
---|
128 |
|
---|
129 | #include <png.h>
|
---|
130 |
|
---|
131 | III. Reading
|
---|
132 |
|
---|
133 | We'll now walk you through the possible functions to call when reading
|
---|
134 | in a PNG file sequentially, briefly explaining the syntax and purpose
|
---|
135 | of each one. See example.c and png.h for more detail. While
|
---|
136 | progressive reading is covered in the next section, you will still
|
---|
137 | need some of the functions discussed in this section to read a PNG
|
---|
138 | file.
|
---|
139 |
|
---|
140 | Setup
|
---|
141 |
|
---|
142 | You will want to do the I/O initialization(*) before you get into libpng,
|
---|
143 | so if it doesn't work, you don't have much to undo. Of course, you
|
---|
144 | will also want to insure that you are, in fact, dealing with a PNG
|
---|
145 | file. Libpng provides a simple check to see if a file is a PNG file.
|
---|
146 | To use it, pass in the first 1 to 8 bytes of the file to the function
|
---|
147 | png_sig_cmp(), and it will return 0 (false) if the bytes match the
|
---|
148 | corresponding bytes of the PNG signature, or nonzero (true) otherwise.
|
---|
149 | Of course, the more bytes you pass in, the greater the accuracy of the
|
---|
150 | prediction.
|
---|
151 |
|
---|
152 | If you are intending to keep the file pointer open for use in libpng,
|
---|
153 | you must ensure you don't read more than 8 bytes from the beginning
|
---|
154 | of the file, and you also have to make a call to png_set_sig_bytes()
|
---|
155 | with the number of bytes you read from the beginning. Libpng will
|
---|
156 | then only check the bytes (if any) that your program didn't read.
|
---|
157 |
|
---|
158 | (*): If you are not using the standard I/O functions, you will need
|
---|
159 | to replace them with custom functions. See the discussion under
|
---|
160 | Customizing libpng.
|
---|
161 |
|
---|
162 |
|
---|
163 | FILE *fp = fopen(file_name, "rb");
|
---|
164 | if (!fp)
|
---|
165 | {
|
---|
166 | return (ERROR);
|
---|
167 | }
|
---|
168 | fread(header, 1, number, fp);
|
---|
169 | is_png = !png_sig_cmp(header, 0, number);
|
---|
170 | if (!is_png)
|
---|
171 | {
|
---|
172 | return (NOT_PNG);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | Next, png_struct and png_info need to be allocated and initialized. In
|
---|
177 | order to ensure that the size of these structures is correct even with a
|
---|
178 | dynamically linked libpng, there are functions to initialize and
|
---|
179 | allocate the structures. We also pass the library version, optional
|
---|
180 | pointers to error handling functions, and a pointer to a data struct for
|
---|
181 | use by the error functions, if necessary (the pointer and functions can
|
---|
182 | be NULL if the default error handlers are to be used). See the section
|
---|
183 | on Changes to Libpng below regarding the old initialization functions.
|
---|
184 | The structure allocation functions quietly return NULL if they fail to
|
---|
185 | create the structure, so your application should check for that.
|
---|
186 |
|
---|
187 | png_structp png_ptr = png_create_read_struct
|
---|
188 | (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
|
---|
189 | user_error_fn, user_warning_fn);
|
---|
190 | if (!png_ptr)
|
---|
191 | return (ERROR);
|
---|
192 |
|
---|
193 | png_infop info_ptr = png_create_info_struct(png_ptr);
|
---|
194 | if (!info_ptr)
|
---|
195 | {
|
---|
196 | png_destroy_read_struct(&png_ptr,
|
---|
197 | (png_infopp)NULL, (png_infopp)NULL);
|
---|
198 | return (ERROR);
|
---|
199 | }
|
---|
200 |
|
---|
201 | png_infop end_info = png_create_info_struct(png_ptr);
|
---|
202 | if (!end_info)
|
---|
203 | {
|
---|
204 | png_destroy_read_struct(&png_ptr, &info_ptr,
|
---|
205 | (png_infopp)NULL);
|
---|
206 | return (ERROR);
|
---|
207 | }
|
---|
208 |
|
---|
209 | If you want to use your own memory allocation routines,
|
---|
210 | define PNG_USER_MEM_SUPPORTED and use
|
---|
211 | png_create_read_struct_2() instead of png_create_read_struct():
|
---|
212 |
|
---|
213 | png_structp png_ptr = png_create_read_struct_2
|
---|
214 | (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
|
---|
215 | user_error_fn, user_warning_fn, (png_voidp)
|
---|
216 | user_mem_ptr, user_malloc_fn, user_free_fn);
|
---|
217 |
|
---|
218 | The error handling routines passed to png_create_read_struct()
|
---|
219 | and the memory alloc/free routines passed to png_create_struct_2()
|
---|
220 | are only necessary if you are not using the libpng supplied error
|
---|
221 | handling and memory alloc/free functions.
|
---|
222 |
|
---|
223 | When libpng encounters an error, it expects to longjmp back
|
---|
224 | to your routine. Therefore, you will need to call setjmp and pass
|
---|
225 | your png_jmpbuf(png_ptr). If you read the file from different
|
---|
226 | routines, you will need to update the jmpbuf field every time you enter
|
---|
227 | a new routine that will call a png_*() function.
|
---|
228 |
|
---|
229 | See your documentation of setjmp/longjmp for your compiler for more
|
---|
230 | information on setjmp/longjmp. See the discussion on libpng error
|
---|
231 | handling in the Customizing Libpng section below for more information
|
---|
232 | on the libpng error handling. If an error occurs, and libpng longjmp's
|
---|
233 | back to your setjmp, you will want to call png_destroy_read_struct() to
|
---|
234 | free any memory.
|
---|
235 |
|
---|
236 | if (setjmp(png_jmpbuf(png_ptr)))
|
---|
237 | {
|
---|
238 | png_destroy_read_struct(&png_ptr, &info_ptr,
|
---|
239 | &end_info);
|
---|
240 | fclose(fp);
|
---|
241 | return (ERROR);
|
---|
242 | }
|
---|
243 |
|
---|
244 | If you would rather avoid the complexity of setjmp/longjmp issues,
|
---|
245 | you can compile libpng with PNG_SETJMP_NOT_SUPPORTED, in which case
|
---|
246 | errors will result in a call to PNG_ABORT() which defaults to abort().
|
---|
247 |
|
---|
248 | Now you need to set up the input code. The default for libpng is to
|
---|
249 | use the C function fread(). If you use this, you will need to pass a
|
---|
250 | valid FILE * in the function png_init_io(). Be sure that the file is
|
---|
251 | opened in binary mode. If you wish to handle reading data in another
|
---|
252 | way, you need not call the png_init_io() function, but you must then
|
---|
253 | implement the libpng I/O methods discussed in the Customizing Libpng
|
---|
254 | section below.
|
---|
255 |
|
---|
256 | png_init_io(png_ptr, fp);
|
---|
257 |
|
---|
258 | If you had previously opened the file and read any of the signature from
|
---|
259 | the beginning in order to see if this was a PNG file, you need to let
|
---|
260 | libpng know that there are some bytes missing from the start of the file.
|
---|
261 |
|
---|
262 | png_set_sig_bytes(png_ptr, number);
|
---|
263 |
|
---|
264 | Setting up callback code
|
---|
265 |
|
---|
266 | You can set up a callback function to handle any unknown chunks in the
|
---|
267 | input stream. You must supply the function
|
---|
268 |
|
---|
269 | read_chunk_callback(png_ptr ptr,
|
---|
270 | png_unknown_chunkp chunk);
|
---|
271 | {
|
---|
272 | /* The unknown chunk structure contains your
|
---|
273 | chunk data, along with similar data for any other
|
---|
274 | unknown chunks: */
|
---|
275 |
|
---|
276 | png_byte name[5];
|
---|
277 | png_byte *data;
|
---|
278 | png_size_t size;
|
---|
279 |
|
---|
280 | /* Note that libpng has already taken care of
|
---|
281 | the CRC handling */
|
---|
282 |
|
---|
283 | /* put your code here. Search for your chunk in the
|
---|
284 | unknown chunk structure, process it, and return one
|
---|
285 | of the following: */
|
---|
286 |
|
---|
287 | return (-n); /* chunk had an error */
|
---|
288 | return (0); /* did not recognize */
|
---|
289 | return (n); /* success */
|
---|
290 | }
|
---|
291 |
|
---|
292 | (You can give your function another name that you like instead of
|
---|
293 | "read_chunk_callback")
|
---|
294 |
|
---|
295 | To inform libpng about your function, use
|
---|
296 |
|
---|
297 | png_set_read_user_chunk_fn(png_ptr, user_chunk_ptr,
|
---|
298 | read_chunk_callback);
|
---|
299 |
|
---|
300 | This names not only the callback function, but also a user pointer that
|
---|
301 | you can retrieve with
|
---|
302 |
|
---|
303 | png_get_user_chunk_ptr(png_ptr);
|
---|
304 |
|
---|
305 | If you call the png_set_read_user_chunk_fn() function, then all unknown
|
---|
306 | chunks will be saved when read, in case your callback function will need
|
---|
307 | one or more of them. This behavior can be changed with the
|
---|
308 | png_set_keep_unknown_chunks() function, described below.
|
---|
309 |
|
---|
310 | At this point, you can set up a callback function that will be
|
---|
311 | called after each row has been read, which you can use to control
|
---|
312 | a progress meter or the like. It's demonstrated in pngtest.c.
|
---|
313 | You must supply a function
|
---|
314 |
|
---|
315 | void read_row_callback(png_ptr ptr, png_uint_32 row,
|
---|
316 | int pass);
|
---|
317 | {
|
---|
318 | /* put your code here */
|
---|
319 | }
|
---|
320 |
|
---|
321 | (You can give it another name that you like instead of "read_row_callback")
|
---|
322 |
|
---|
323 | To inform libpng about your function, use
|
---|
324 |
|
---|
325 | png_set_read_status_fn(png_ptr, read_row_callback);
|
---|
326 |
|
---|
327 | Unknown-chunk handling
|
---|
328 |
|
---|
329 | Now you get to set the way the library processes unknown chunks in the
|
---|
330 | input PNG stream. Both known and unknown chunks will be read. Normal
|
---|
331 | behavior is that known chunks will be parsed into information in
|
---|
332 | various info_ptr members while unknown chunks will be discarded. This
|
---|
333 | behavior can be wasteful if your application will never use some known
|
---|
334 | chunk types. To change this, you can call:
|
---|
335 |
|
---|
336 | png_set_keep_unknown_chunks(png_ptr, keep,
|
---|
337 | chunk_list, num_chunks);
|
---|
338 | keep - 0: default unknown chunk handling
|
---|
339 | 1: ignore; do not keep
|
---|
340 | 2: keep only if safe-to-copy
|
---|
341 | 3: keep even if unsafe-to-copy
|
---|
342 | You can use these definitions:
|
---|
343 | PNG_HANDLE_CHUNK_AS_DEFAULT 0
|
---|
344 | PNG_HANDLE_CHUNK_NEVER 1
|
---|
345 | PNG_HANDLE_CHUNK_IF_SAFE 2
|
---|
346 | PNG_HANDLE_CHUNK_ALWAYS 3
|
---|
347 | chunk_list - list of chunks affected (a byte string,
|
---|
348 | five bytes per chunk, NULL or '\0' if
|
---|
349 | num_chunks is 0)
|
---|
350 | num_chunks - number of chunks affected; if 0, all
|
---|
351 | unknown chunks are affected. If nonzero,
|
---|
352 | only the chunks in the list are affected
|
---|
353 |
|
---|
354 | Unknown chunks declared in this way will be saved as raw data onto a
|
---|
355 | list of png_unknown_chunk structures. If a chunk that is normally
|
---|
356 | known to libpng is named in the list, it will be handled as unknown,
|
---|
357 | according to the "keep" directive. If a chunk is named in successive
|
---|
358 | instances of png_set_keep_unknown_chunks(), the final instance will
|
---|
359 | take precedence. The IHDR and IEND chunks should not be named in
|
---|
360 | chunk_list; if they are, libpng will process them normally anyway.
|
---|
361 |
|
---|
362 | Here is an example of the usage of png_set_keep_unknown_chunks(),
|
---|
363 | where the private "vpAg" chunk will later be processed by a user chunk
|
---|
364 | callback function:
|
---|
365 |
|
---|
366 | png_byte vpAg[5]={118, 112, 65, 103, (png_byte) '\0'};
|
---|
367 |
|
---|
368 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
|
---|
369 | png_byte unused_chunks[]=
|
---|
370 | {
|
---|
371 | 104, 73, 83, 84, (png_byte) '\0', /* hIST */
|
---|
372 | 105, 84, 88, 116, (png_byte) '\0', /* iTXt */
|
---|
373 | 112, 67, 65, 76, (png_byte) '\0', /* pCAL */
|
---|
374 | 115, 67, 65, 76, (png_byte) '\0', /* sCAL */
|
---|
375 | 115, 80, 76, 84, (png_byte) '\0', /* sPLT */
|
---|
376 | 116, 73, 77, 69, (png_byte) '\0', /* tIME */
|
---|
377 | };
|
---|
378 | #endif
|
---|
379 |
|
---|
380 | ...
|
---|
381 |
|
---|
382 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
|
---|
383 | /* ignore all unknown chunks: */
|
---|
384 | png_set_keep_unknown_chunks(read_ptr, 1, NULL, 0);
|
---|
385 | /* except for vpAg: */
|
---|
386 | png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1);
|
---|
387 | /* also ignore unused known chunks: */
|
---|
388 | png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks,
|
---|
389 | (int)sizeof(unused_chunks)/5);
|
---|
390 | #endif
|
---|
391 |
|
---|
392 | User limits
|
---|
393 |
|
---|
394 | The PNG specification allows the width and height of an image to be as
|
---|
395 | large as 2^31-1 (0x7fffffff), or about 2.147 billion rows and columns.
|
---|
396 | Since very few applications really need to process such large images,
|
---|
397 | we have imposed an arbitrary 1-million limit on rows and columns.
|
---|
398 | Larger images will be rejected immediately with a png_error() call. If
|
---|
399 | you wish to override this limit, you can use
|
---|
400 |
|
---|
401 | png_set_user_limits(png_ptr, width_max, height_max);
|
---|
402 |
|
---|
403 | to set your own limits, or use width_max = height_max = 0x7fffffffL
|
---|
404 | to allow all valid dimensions (libpng may reject some very large images
|
---|
405 | anyway because of potential buffer overflow conditions).
|
---|
406 |
|
---|
407 | You should put this statement after you create the PNG structure and
|
---|
408 | before calling png_read_info(), png_read_png(), or png_process_data().
|
---|
409 | If you need to retrieve the limits that are being applied, use
|
---|
410 |
|
---|
411 | width_max = png_get_user_width_max(png_ptr);
|
---|
412 | height_max = png_get_user_height_max(png_ptr);
|
---|
413 |
|
---|
414 | The PNG specification sets no limit on the number of ancillary chunks
|
---|
415 | allowed in a PNG datastream. You can impose a limit on the total number
|
---|
416 | of sPLT, tEXt, iTXt, zTXt, and unknown chunks that will be stored, with
|
---|
417 |
|
---|
418 | png_set_chunk_cache_max(png_ptr, user_chunk_cache_max);
|
---|
419 |
|
---|
420 | where 0x7fffffffL means unlimited. You can retrieve this limit with
|
---|
421 |
|
---|
422 | chunk_cache_max = png_get_chunk_cache_max(png_ptr);
|
---|
423 |
|
---|
424 | This limit also applies to the number of buffers that can be allocated
|
---|
425 | by png_decompress_chunk() while decompressing iTXt, zTXt, and iCCP chunks.
|
---|
426 |
|
---|
427 | The high-level read interface
|
---|
428 |
|
---|
429 | At this point there are two ways to proceed; through the high-level
|
---|
430 | read interface, or through a sequence of low-level read operations.
|
---|
431 | You can use the high-level interface if (a) you are willing to read
|
---|
432 | the entire image into memory, and (b) the input transformations
|
---|
433 | you want to do are limited to the following set:
|
---|
434 |
|
---|
435 | PNG_TRANSFORM_IDENTITY No transformation
|
---|
436 | PNG_TRANSFORM_STRIP_16 Strip 16-bit samples to
|
---|
437 | 8 bits
|
---|
438 | PNG_TRANSFORM_STRIP_ALPHA Discard the alpha channel
|
---|
439 | PNG_TRANSFORM_PACKING Expand 1, 2 and 4-bit
|
---|
440 | samples to bytes
|
---|
441 | PNG_TRANSFORM_PACKSWAP Change order of packed
|
---|
442 | pixels to LSB first
|
---|
443 | PNG_TRANSFORM_EXPAND Perform set_expand()
|
---|
444 | PNG_TRANSFORM_INVERT_MONO Invert monochrome images
|
---|
445 | PNG_TRANSFORM_SHIFT Normalize pixels to the
|
---|
446 | sBIT depth
|
---|
447 | PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA
|
---|
448 | to BGRA
|
---|
449 | PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA
|
---|
450 | to AG
|
---|
451 | PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity
|
---|
452 | to transparency
|
---|
453 | PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples
|
---|
454 | PNG_TRANSFORM_GRAY_TO_RGB Expand grayscale samples
|
---|
455 | to RGB (or GA to RGBA)
|
---|
456 |
|
---|
457 | (This excludes setting a background color, doing gamma transformation,
|
---|
458 | dithering, and setting filler.) If this is the case, simply do this:
|
---|
459 |
|
---|
460 | png_read_png(png_ptr, info_ptr, png_transforms, NULL)
|
---|
461 |
|
---|
462 | where png_transforms is an integer containing the bitwise OR of some
|
---|
463 | set of transformation flags. This call is equivalent to png_read_info(),
|
---|
464 | followed the set of transformations indicated by the transform mask,
|
---|
465 | then png_read_image(), and finally png_read_end().
|
---|
466 |
|
---|
467 | (The final parameter of this call is not yet used. Someday it might point
|
---|
468 | to transformation parameters required by some future input transform.)
|
---|
469 |
|
---|
470 | You must use png_transforms and not call any png_set_transform() functions
|
---|
471 | when you use png_read_png().
|
---|
472 |
|
---|
473 | After you have called png_read_png(), you can retrieve the image data
|
---|
474 | with
|
---|
475 |
|
---|
476 | row_pointers = png_get_rows(png_ptr, info_ptr);
|
---|
477 |
|
---|
478 | where row_pointers is an array of pointers to the pixel data for each row:
|
---|
479 |
|
---|
480 | png_bytep row_pointers[height];
|
---|
481 |
|
---|
482 | If you know your image size and pixel size ahead of time, you can allocate
|
---|
483 | row_pointers prior to calling png_read_png() with
|
---|
484 |
|
---|
485 | if (height > PNG_UINT_32_MAX/png_sizeof(png_byte))
|
---|
486 | png_error (png_ptr,
|
---|
487 | "Image is too tall to process in memory");
|
---|
488 | if (width > PNG_UINT_32_MAX/pixel_size)
|
---|
489 | png_error (png_ptr,
|
---|
490 | "Image is too wide to process in memory");
|
---|
491 | row_pointers = png_malloc(png_ptr,
|
---|
492 | height*png_sizeof(png_bytep));
|
---|
493 | for (int i=0; i<height, i++)
|
---|
494 | row_pointers[i]=NULL; /* security precaution */
|
---|
495 | for (int i=0; i<height, i++)
|
---|
496 | row_pointers[i]=png_malloc(png_ptr,
|
---|
497 | width*pixel_size);
|
---|
498 | png_set_rows(png_ptr, info_ptr, &row_pointers);
|
---|
499 |
|
---|
500 | Alternatively you could allocate your image in one big block and define
|
---|
501 | row_pointers[i] to point into the proper places in your block.
|
---|
502 |
|
---|
503 | If you use png_set_rows(), the application is responsible for freeing
|
---|
504 | row_pointers (and row_pointers[i], if they were separately allocated).
|
---|
505 |
|
---|
506 | If you don't allocate row_pointers ahead of time, png_read_png() will
|
---|
507 | do it, and it'll be free'ed when you call png_destroy_*().
|
---|
508 |
|
---|
509 | The low-level read interface
|
---|
510 |
|
---|
511 | If you are going the low-level route, you are now ready to read all
|
---|
512 | the file information up to the actual image data. You do this with a
|
---|
513 | call to png_read_info().
|
---|
514 |
|
---|
515 | png_read_info(png_ptr, info_ptr);
|
---|
516 |
|
---|
517 | This will process all chunks up to but not including the image data.
|
---|
518 |
|
---|
519 | Querying the info structure
|
---|
520 |
|
---|
521 | Functions are used to get the information from the info_ptr once it
|
---|
522 | has been read. Note that these fields may not be completely filled
|
---|
523 | in until png_read_end() has read the chunk data following the image.
|
---|
524 |
|
---|
525 | png_get_IHDR(png_ptr, info_ptr, &width, &height,
|
---|
526 | &bit_depth, &color_type, &interlace_type,
|
---|
527 | &compression_type, &filter_method);
|
---|
528 |
|
---|
529 | width - holds the width of the image
|
---|
530 | in pixels (up to 2^31).
|
---|
531 | height - holds the height of the image
|
---|
532 | in pixels (up to 2^31).
|
---|
533 | bit_depth - holds the bit depth of one of the
|
---|
534 | image channels. (valid values are
|
---|
535 | 1, 2, 4, 8, 16 and depend also on
|
---|
536 | the color_type. See also
|
---|
537 | significant bits (sBIT) below).
|
---|
538 | color_type - describes which color/alpha channels
|
---|
539 | are present.
|
---|
540 | PNG_COLOR_TYPE_GRAY
|
---|
541 | (bit depths 1, 2, 4, 8, 16)
|
---|
542 | PNG_COLOR_TYPE_GRAY_ALPHA
|
---|
543 | (bit depths 8, 16)
|
---|
544 | PNG_COLOR_TYPE_PALETTE
|
---|
545 | (bit depths 1, 2, 4, 8)
|
---|
546 | PNG_COLOR_TYPE_RGB
|
---|
547 | (bit_depths 8, 16)
|
---|
548 | PNG_COLOR_TYPE_RGB_ALPHA
|
---|
549 | (bit_depths 8, 16)
|
---|
550 |
|
---|
551 | PNG_COLOR_MASK_PALETTE
|
---|
552 | PNG_COLOR_MASK_COLOR
|
---|
553 | PNG_COLOR_MASK_ALPHA
|
---|
554 |
|
---|
555 | filter_method - (must be PNG_FILTER_TYPE_BASE
|
---|
556 | for PNG 1.0, and can also be
|
---|
557 | PNG_INTRAPIXEL_DIFFERENCING if
|
---|
558 | the PNG datastream is embedded in
|
---|
559 | a MNG-1.0 datastream)
|
---|
560 | compression_type - (must be PNG_COMPRESSION_TYPE_BASE
|
---|
561 | for PNG 1.0)
|
---|
562 | interlace_type - (PNG_INTERLACE_NONE or
|
---|
563 | PNG_INTERLACE_ADAM7)
|
---|
564 |
|
---|
565 | Any or all of interlace_type, compression_type, or
|
---|
566 | filter_method can be NULL if you are
|
---|
567 | not interested in their values.
|
---|
568 |
|
---|
569 | Note that png_get_IHDR() returns 32-bit data into
|
---|
570 | the application's width and height variables.
|
---|
571 | This is an unsafe situation if these are 16-bit
|
---|
572 | variables. In such situations, the
|
---|
573 | png_get_image_width() and png_get_image_height()
|
---|
574 | functions described below are safer.
|
---|
575 |
|
---|
576 | width = png_get_image_width(png_ptr,
|
---|
577 | info_ptr);
|
---|
578 | height = png_get_image_height(png_ptr,
|
---|
579 | info_ptr);
|
---|
580 | bit_depth = png_get_bit_depth(png_ptr,
|
---|
581 | info_ptr);
|
---|
582 | color_type = png_get_color_type(png_ptr,
|
---|
583 | info_ptr);
|
---|
584 | filter_method = png_get_filter_type(png_ptr,
|
---|
585 | info_ptr);
|
---|
586 | compression_type = png_get_compression_type(png_ptr,
|
---|
587 | info_ptr);
|
---|
588 | interlace_type = png_get_interlace_type(png_ptr,
|
---|
589 | info_ptr);
|
---|
590 |
|
---|
591 | channels = png_get_channels(png_ptr, info_ptr);
|
---|
592 | channels - number of channels of info for the
|
---|
593 | color type (valid values are 1 (GRAY,
|
---|
594 | PALETTE), 2 (GRAY_ALPHA), 3 (RGB),
|
---|
595 | 4 (RGB_ALPHA or RGB + filler byte))
|
---|
596 | rowbytes = png_get_rowbytes(png_ptr, info_ptr);
|
---|
597 | rowbytes - number of bytes needed to hold a row
|
---|
598 |
|
---|
599 | signature = png_get_signature(png_ptr, info_ptr);
|
---|
600 | signature - holds the signature read from the
|
---|
601 | file (if any). The data is kept in
|
---|
602 | the same offset it would be if the
|
---|
603 | whole signature were read (i.e. if an
|
---|
604 | application had already read in 4
|
---|
605 | bytes of signature before starting
|
---|
606 | libpng, the remaining 4 bytes would
|
---|
607 | be in signature[4] through signature[7]
|
---|
608 | (see png_set_sig_bytes())).
|
---|
609 |
|
---|
610 | These are also important, but their validity depends on whether the chunk
|
---|
611 | has been read. The png_get_valid(png_ptr, info_ptr, PNG_INFO_<chunk>) and
|
---|
612 | png_get_<chunk>(png_ptr, info_ptr, ...) functions return non-zero if the
|
---|
613 | data has been read, or zero if it is missing. The parameters to the
|
---|
614 | png_get_<chunk> are set directly if they are simple data types, or a
|
---|
615 | pointer into the info_ptr is returned for any complex types.
|
---|
616 |
|
---|
617 | png_get_PLTE(png_ptr, info_ptr, &palette,
|
---|
618 | &num_palette);
|
---|
619 | palette - the palette for the file
|
---|
620 | (array of png_color)
|
---|
621 | num_palette - number of entries in the palette
|
---|
622 |
|
---|
623 | png_get_gAMA(png_ptr, info_ptr, &gamma);
|
---|
624 | gamma - the gamma the file is written
|
---|
625 | at (PNG_INFO_gAMA)
|
---|
626 |
|
---|
627 | png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
|
---|
628 | srgb_intent - the rendering intent (PNG_INFO_sRGB)
|
---|
629 | The presence of the sRGB chunk
|
---|
630 | means that the pixel data is in the
|
---|
631 | sRGB color space. This chunk also
|
---|
632 | implies specific values of gAMA and
|
---|
633 | cHRM.
|
---|
634 |
|
---|
635 | png_get_iCCP(png_ptr, info_ptr, &name,
|
---|
636 | &compression_type, &profile, &proflen);
|
---|
637 | name - The profile name.
|
---|
638 | compression - The compression type; always
|
---|
639 | PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
|
---|
640 | You may give NULL to this argument to
|
---|
641 | ignore it.
|
---|
642 | profile - International Color Consortium color
|
---|
643 | profile data. May contain NULs.
|
---|
644 | proflen - length of profile data in bytes.
|
---|
645 |
|
---|
646 | png_get_sBIT(png_ptr, info_ptr, &sig_bit);
|
---|
647 | sig_bit - the number of significant bits for
|
---|
648 | (PNG_INFO_sBIT) each of the gray,
|
---|
649 | red, green, and blue channels,
|
---|
650 | whichever are appropriate for the
|
---|
651 | given color type (png_color_16)
|
---|
652 |
|
---|
653 | png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
|
---|
654 | &trans_values);
|
---|
655 | trans - array of transparent
|
---|
656 | entries for palette (PNG_INFO_tRNS)
|
---|
657 | trans_values - graylevel or color sample values of
|
---|
658 | the single transparent color for
|
---|
659 | non-paletted images (PNG_INFO_tRNS)
|
---|
660 | num_trans - number of transparent entries
|
---|
661 | (PNG_INFO_tRNS)
|
---|
662 |
|
---|
663 | png_get_hIST(png_ptr, info_ptr, &hist);
|
---|
664 | (PNG_INFO_hIST)
|
---|
665 | hist - histogram of palette (array of
|
---|
666 | png_uint_16)
|
---|
667 |
|
---|
668 | png_get_tIME(png_ptr, info_ptr, &mod_time);
|
---|
669 | mod_time - time image was last modified
|
---|
670 | (PNG_VALID_tIME)
|
---|
671 |
|
---|
672 | png_get_bKGD(png_ptr, info_ptr, &background);
|
---|
673 | background - background color (PNG_VALID_bKGD)
|
---|
674 | valid 16-bit red, green and blue
|
---|
675 | values, regardless of color_type
|
---|
676 |
|
---|
677 | num_comments = png_get_text(png_ptr, info_ptr,
|
---|
678 | &text_ptr, &num_text);
|
---|
679 | num_comments - number of comments
|
---|
680 | text_ptr - array of png_text holding image
|
---|
681 | comments
|
---|
682 | text_ptr[i].compression - type of compression used
|
---|
683 | on "text" PNG_TEXT_COMPRESSION_NONE
|
---|
684 | PNG_TEXT_COMPRESSION_zTXt
|
---|
685 | PNG_ITXT_COMPRESSION_NONE
|
---|
686 | PNG_ITXT_COMPRESSION_zTXt
|
---|
687 | text_ptr[i].key - keyword for comment. Must contain
|
---|
688 | 1-79 characters.
|
---|
689 | text_ptr[i].text - text comments for current
|
---|
690 | keyword. Can be empty.
|
---|
691 | text_ptr[i].text_length - length of text string,
|
---|
692 | after decompression, 0 for iTXt
|
---|
693 | text_ptr[i].itxt_length - length of itxt string,
|
---|
694 | after decompression, 0 for tEXt/zTXt
|
---|
695 | text_ptr[i].lang - language of comment (empty
|
---|
696 | string for unknown).
|
---|
697 | text_ptr[i].lang_key - keyword in UTF-8
|
---|
698 | (empty string for unknown).
|
---|
699 | Note that the itxt_length, lang, and lang_key
|
---|
700 | members of the text_ptr structure only exist
|
---|
701 | when the library is built with iTXt chunk support.
|
---|
702 |
|
---|
703 | num_text - number of comments (same as
|
---|
704 | num_comments; you can put NULL here
|
---|
705 | to avoid the duplication)
|
---|
706 | Note while png_set_text() will accept text, language,
|
---|
707 | and translated keywords that can be NULL pointers, the
|
---|
708 | structure returned by png_get_text will always contain
|
---|
709 | regular zero-terminated C strings. They might be
|
---|
710 | empty strings but they will never be NULL pointers.
|
---|
711 |
|
---|
712 | num_spalettes = png_get_sPLT(png_ptr, info_ptr,
|
---|
713 | &palette_ptr);
|
---|
714 | palette_ptr - array of palette structures holding
|
---|
715 | contents of one or more sPLT chunks
|
---|
716 | read.
|
---|
717 | num_spalettes - number of sPLT chunks read.
|
---|
718 |
|
---|
719 | png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y,
|
---|
720 | &unit_type);
|
---|
721 | offset_x - positive offset from the left edge
|
---|
722 | of the screen
|
---|
723 | offset_y - positive offset from the top edge
|
---|
724 | of the screen
|
---|
725 | unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
|
---|
726 |
|
---|
727 | png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y,
|
---|
728 | &unit_type);
|
---|
729 | res_x - pixels/unit physical resolution in
|
---|
730 | x direction
|
---|
731 | res_y - pixels/unit physical resolution in
|
---|
732 | x direction
|
---|
733 | unit_type - PNG_RESOLUTION_UNKNOWN,
|
---|
734 | PNG_RESOLUTION_METER
|
---|
735 |
|
---|
736 | png_get_sCAL(png_ptr, info_ptr, &unit, &width,
|
---|
737 | &height)
|
---|
738 | unit - physical scale units (an integer)
|
---|
739 | width - width of a pixel in physical scale units
|
---|
740 | height - height of a pixel in physical scale units
|
---|
741 | (width and height are doubles)
|
---|
742 |
|
---|
743 | png_get_sCAL_s(png_ptr, info_ptr, &unit, &width,
|
---|
744 | &height)
|
---|
745 | unit - physical scale units (an integer)
|
---|
746 | width - width of a pixel in physical scale units
|
---|
747 | height - height of a pixel in physical scale units
|
---|
748 | (width and height are strings like "2.54")
|
---|
749 |
|
---|
750 | num_unknown_chunks = png_get_unknown_chunks(png_ptr,
|
---|
751 | info_ptr, &unknowns)
|
---|
752 | unknowns - array of png_unknown_chunk
|
---|
753 | structures holding unknown chunks
|
---|
754 | unknowns[i].name - name of unknown chunk
|
---|
755 | unknowns[i].data - data of unknown chunk
|
---|
756 | unknowns[i].size - size of unknown chunk's data
|
---|
757 | unknowns[i].location - position of chunk in file
|
---|
758 |
|
---|
759 | The value of "i" corresponds to the order in which the
|
---|
760 | chunks were read from the PNG file or inserted with the
|
---|
761 | png_set_unknown_chunks() function.
|
---|
762 |
|
---|
763 | The data from the pHYs chunk can be retrieved in several convenient
|
---|
764 | forms:
|
---|
765 |
|
---|
766 | res_x = png_get_x_pixels_per_meter(png_ptr,
|
---|
767 | info_ptr)
|
---|
768 | res_y = png_get_y_pixels_per_meter(png_ptr,
|
---|
769 | info_ptr)
|
---|
770 | res_x_and_y = png_get_pixels_per_meter(png_ptr,
|
---|
771 | info_ptr)
|
---|
772 | res_x = png_get_x_pixels_per_inch(png_ptr,
|
---|
773 | info_ptr)
|
---|
774 | res_y = png_get_y_pixels_per_inch(png_ptr,
|
---|
775 | info_ptr)
|
---|
776 | res_x_and_y = png_get_pixels_per_inch(png_ptr,
|
---|
777 | info_ptr)
|
---|
778 | aspect_ratio = png_get_pixel_aspect_ratio(png_ptr,
|
---|
779 | info_ptr)
|
---|
780 |
|
---|
781 | (Each of these returns 0 [signifying "unknown"] if
|
---|
782 | the data is not present or if res_x is 0;
|
---|
783 | res_x_and_y is 0 if res_x != res_y)
|
---|
784 |
|
---|
785 | The data from the oFFs chunk can be retrieved in several convenient
|
---|
786 | forms:
|
---|
787 |
|
---|
788 | x_offset = png_get_x_offset_microns(png_ptr, info_ptr);
|
---|
789 | y_offset = png_get_y_offset_microns(png_ptr, info_ptr);
|
---|
790 | x_offset = png_get_x_offset_inches(png_ptr, info_ptr);
|
---|
791 | y_offset = png_get_y_offset_inches(png_ptr, info_ptr);
|
---|
792 |
|
---|
793 | (Each of these returns 0 [signifying "unknown" if both
|
---|
794 | x and y are 0] if the data is not present or if the
|
---|
795 | chunk is present but the unit is the pixel)
|
---|
796 |
|
---|
797 | For more information, see the png_info definition in png.h and the
|
---|
798 | PNG specification for chunk contents. Be careful with trusting
|
---|
799 | rowbytes, as some of the transformations could increase the space
|
---|
800 | needed to hold a row (expand, filler, gray_to_rgb, etc.).
|
---|
801 | See png_read_update_info(), below.
|
---|
802 |
|
---|
803 | A quick word about text_ptr and num_text. PNG stores comments in
|
---|
804 | keyword/text pairs, one pair per chunk, with no limit on the number
|
---|
805 | of text chunks, and a 2^31 byte limit on their size. While there are
|
---|
806 | suggested keywords, there is no requirement to restrict the use to these
|
---|
807 | strings. It is strongly suggested that keywords and text be sensible
|
---|
808 | to humans (that's the point), so don't use abbreviations. Non-printing
|
---|
809 | symbols are not allowed. See the PNG specification for more details.
|
---|
810 | There is also no requirement to have text after the keyword.
|
---|
811 |
|
---|
812 | Keywords should be limited to 79 Latin-1 characters without leading or
|
---|
813 | trailing spaces, but non-consecutive spaces are allowed within the
|
---|
814 | keyword. It is possible to have the same keyword any number of times.
|
---|
815 | The text_ptr is an array of png_text structures, each holding a
|
---|
816 | pointer to a language string, a pointer to a keyword and a pointer to
|
---|
817 | a text string. The text string, language code, and translated
|
---|
818 | keyword may be empty or NULL pointers. The keyword/text
|
---|
819 | pairs are put into the array in the order that they are received.
|
---|
820 | However, some or all of the text chunks may be after the image, so, to
|
---|
821 | make sure you have read all the text chunks, don't mess with these
|
---|
822 | until after you read the stuff after the image. This will be
|
---|
823 | mentioned again below in the discussion that goes with png_read_end().
|
---|
824 |
|
---|
825 | Input transformations
|
---|
826 |
|
---|
827 | After you've read the header information, you can set up the library
|
---|
828 | to handle any special transformations of the image data. The various
|
---|
829 | ways to transform the data will be described in the order that they
|
---|
830 | should occur. This is important, as some of these change the color
|
---|
831 | type and/or bit depth of the data, and some others only work on
|
---|
832 | certain color types and bit depths. Even though each transformation
|
---|
833 | checks to see if it has data that it can do something with, you should
|
---|
834 | make sure to only enable a transformation if it will be valid for the
|
---|
835 | data. For example, don't swap red and blue on grayscale data.
|
---|
836 |
|
---|
837 | The colors used for the background and transparency values should be
|
---|
838 | supplied in the same format/depth as the current image data. They
|
---|
839 | are stored in the same format/depth as the image data in a bKGD or tRNS
|
---|
840 | chunk, so this is what libpng expects for this data. The colors are
|
---|
841 | transformed to keep in sync with the image data when an application
|
---|
842 | calls the png_read_update_info() routine (see below).
|
---|
843 |
|
---|
844 | Data will be decoded into the supplied row buffers packed into bytes
|
---|
845 | unless the library has been told to transform it into another format.
|
---|
846 | For example, 4 bit/pixel paletted or grayscale data will be returned
|
---|
847 | 2 pixels/byte with the leftmost pixel in the high-order bits of the
|
---|
848 | byte, unless png_set_packing() is called. 8-bit RGB data will be stored
|
---|
849 | in RGB RGB RGB format unless png_set_filler() or png_set_add_alpha()
|
---|
850 | is called to insert filler bytes, either before or after each RGB triplet.
|
---|
851 | 16-bit RGB data will be returned RRGGBB RRGGBB, with the most significant
|
---|
852 | byte of the color value first, unless png_set_strip_16() is called to
|
---|
853 | transform it to regular RGB RGB triplets, or png_set_filler() or
|
---|
854 | png_set_add alpha() is called to insert filler bytes, either before or
|
---|
855 | after each RRGGBB triplet. Similarly, 8-bit or 16-bit grayscale data can
|
---|
856 | be modified with
|
---|
857 | png_set_filler(), png_set_add_alpha(), or png_set_strip_16().
|
---|
858 |
|
---|
859 | The following code transforms grayscale images of less than 8 to 8 bits,
|
---|
860 | changes paletted images to RGB, and adds a full alpha channel if there is
|
---|
861 | transparency information in a tRNS chunk. This is most useful on
|
---|
862 | grayscale images with bit depths of 2 or 4 or if there is a multiple-image
|
---|
863 | viewing application that wishes to treat all images in the same way.
|
---|
864 |
|
---|
865 | if (color_type == PNG_COLOR_TYPE_PALETTE)
|
---|
866 | png_set_palette_to_rgb(png_ptr);
|
---|
867 |
|
---|
868 | if (color_type == PNG_COLOR_TYPE_GRAY &&
|
---|
869 | bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
|
---|
870 |
|
---|
871 | if (png_get_valid(png_ptr, info_ptr,
|
---|
872 | PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
|
---|
873 |
|
---|
874 | These three functions are actually aliases for png_set_expand(), added
|
---|
875 | in libpng version 1.0.4, with the function names expanded to improve code
|
---|
876 | readability. In some future version they may actually do different
|
---|
877 | things.
|
---|
878 |
|
---|
879 | As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() was
|
---|
880 | added. It expands the sample depth without changing tRNS to alpha.
|
---|
881 |
|
---|
882 | As of libpng version 1.2.54, not all possible expansions are supported.
|
---|
883 |
|
---|
884 | In the following table, the 01 means grayscale with depth<8, 31 means
|
---|
885 | indexed with depth<8, other numerals represent the color type, "T" means
|
---|
886 | the tRNS chunk is present, A means an alpha channel is present, and O
|
---|
887 | means tRNS or alpha is present but all pixels in the image are opaque.
|
---|
888 |
|
---|
889 | FROM 01 31 0 0T 0O 2 2T 2O 3 3T 3O 4A 4O 6A 6O
|
---|
890 | TO
|
---|
891 | 01 -
|
---|
892 | 31 -
|
---|
893 | 0 1 -
|
---|
894 | 0T -
|
---|
895 | 0O -
|
---|
896 | 2 GX -
|
---|
897 | 2T -
|
---|
898 | 2O -
|
---|
899 | 3 1 -
|
---|
900 | 3T -
|
---|
901 | 3O -
|
---|
902 | 4A T -
|
---|
903 | 4O -
|
---|
904 | 6A GX TX TX -
|
---|
905 | 6O GX TX -
|
---|
906 |
|
---|
907 | Within the matrix,
|
---|
908 | "-" means the transformation is not supported.
|
---|
909 | "X" means the transformation is obtained by png_set_expand().
|
---|
910 | "1" means the transformation is obtained by
|
---|
911 | png_set_expand_gray_1_2_4_to_8
|
---|
912 | "G" means the transformation is obtained by
|
---|
913 | png_set_gray_to_rgb().
|
---|
914 | "P" means the transformation is obtained by
|
---|
915 | png_set_expand_palette_to_rgb().
|
---|
916 | "T" means the transformation is obtained by
|
---|
917 | png_set_tRNS_to_alpha().
|
---|
918 |
|
---|
919 | PNG can have files with 16 bits per channel. If you only can handle
|
---|
920 | 8 bits per channel, this will strip the pixels down to 8 bit.
|
---|
921 |
|
---|
922 | if (bit_depth == 16)
|
---|
923 | png_set_strip_16(png_ptr);
|
---|
924 |
|
---|
925 | If, for some reason, you don't need the alpha channel on an image,
|
---|
926 | and you want to remove it rather than combining it with the background
|
---|
927 | (but the image author certainly had in mind that you *would* combine
|
---|
928 | it with the background, so that's what you should probably do):
|
---|
929 |
|
---|
930 | if (color_type & PNG_COLOR_MASK_ALPHA)
|
---|
931 | png_set_strip_alpha(png_ptr);
|
---|
932 |
|
---|
933 | In PNG files, the alpha channel in an image
|
---|
934 | is the level of opacity. If you need the alpha channel in an image to
|
---|
935 | be the level of transparency instead of opacity, you can invert the
|
---|
936 | alpha channel (or the tRNS chunk data) after it's read, so that 0 is
|
---|
937 | fully opaque and 255 (in 8-bit or paletted images) or 65535 (in 16-bit
|
---|
938 | images) is fully transparent, with
|
---|
939 |
|
---|
940 | png_set_invert_alpha(png_ptr);
|
---|
941 |
|
---|
942 | The PNG format only supports pixels with postmultiplied alpha.
|
---|
943 | If you want to replace the pixels, after reading them, with pixels
|
---|
944 | that have premultiplied color samples, you can do this with
|
---|
945 |
|
---|
946 | png_set_premultiply_alpha(png_ptr);
|
---|
947 |
|
---|
948 | If you do this, any input with a tRNS chunk will be expanded to
|
---|
949 | have an alpha channel.
|
---|
950 |
|
---|
951 | PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
|
---|
952 | they can, resulting in, for example, 8 pixels per byte for 1 bit
|
---|
953 | files. This code expands to 1 pixel per byte without changing the
|
---|
954 | values of the pixels:
|
---|
955 |
|
---|
956 | if (bit_depth < 8)
|
---|
957 | png_set_packing(png_ptr);
|
---|
958 |
|
---|
959 | PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixels
|
---|
960 | stored in a PNG image have been "scaled" or "shifted" up to the next
|
---|
961 | higher possible bit depth (e.g. from 5 bits/sample in the range [0,31]
|
---|
962 | to 8 bits/sample in the range [0, 255]). However, it is also possible
|
---|
963 | to convert the PNG pixel data back to the original bit depth of the
|
---|
964 | image. This call reduces the pixels back down to the original bit depth:
|
---|
965 |
|
---|
966 | png_color_8p sig_bit;
|
---|
967 |
|
---|
968 | if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
|
---|
969 | png_set_shift(png_ptr, sig_bit);
|
---|
970 |
|
---|
971 | PNG files store 3-color pixels in red, green, blue order. This code
|
---|
972 | changes the storage of the pixels to blue, green, red:
|
---|
973 |
|
---|
974 | if (color_type == PNG_COLOR_TYPE_RGB ||
|
---|
975 | color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
---|
976 | png_set_bgr(png_ptr);
|
---|
977 |
|
---|
978 | PNG files store RGB pixels packed into 3 or 6 bytes. This code expands them
|
---|
979 | into 4 or 8 bytes for windowing systems that need them in this format:
|
---|
980 |
|
---|
981 | if (color_type == PNG_COLOR_TYPE_RGB)
|
---|
982 | png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE);
|
---|
983 |
|
---|
984 | where "filler" is the 8 or 16-bit number to fill with, and the location is
|
---|
985 | either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
|
---|
986 | you want the filler before the RGB or after. This transformation
|
---|
987 | does not affect images that already have full alpha channels. To add an
|
---|
988 | opaque alpha channel, use filler=0xff or 0xffff and PNG_FILLER_AFTER which
|
---|
989 | will generate RGBA pixels.
|
---|
990 |
|
---|
991 | Note that png_set_filler() does not change the color type. If you want
|
---|
992 | to do that, you can add a true alpha channel with
|
---|
993 |
|
---|
994 | if (color_type == PNG_COLOR_TYPE_RGB ||
|
---|
995 | color_type == PNG_COLOR_TYPE_GRAY)
|
---|
996 | png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
|
---|
997 |
|
---|
998 | where "filler" contains the alpha value to assign to each pixel.
|
---|
999 | This function was added in libpng-1.2.7.
|
---|
1000 |
|
---|
1001 | If you are reading an image with an alpha channel, and you need the
|
---|
1002 | data as ARGB instead of the normal PNG format RGBA:
|
---|
1003 |
|
---|
1004 | if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
---|
1005 | png_set_swap_alpha(png_ptr);
|
---|
1006 |
|
---|
1007 | For some uses, you may want a grayscale image to be represented as
|
---|
1008 | RGB. This code will do that conversion:
|
---|
1009 |
|
---|
1010 | if (color_type == PNG_COLOR_TYPE_GRAY ||
|
---|
1011 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
---|
1012 | png_set_gray_to_rgb(png_ptr);
|
---|
1013 |
|
---|
1014 | Conversely, you can convert an RGB or RGBA image to grayscale or grayscale
|
---|
1015 | with alpha.
|
---|
1016 |
|
---|
1017 | if (color_type == PNG_COLOR_TYPE_RGB ||
|
---|
1018 | color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
---|
1019 | png_set_rgb_to_gray_fixed(png_ptr, error_action,
|
---|
1020 | int red_weight, int green_weight);
|
---|
1021 |
|
---|
1022 | error_action = 1: silently do the conversion
|
---|
1023 | error_action = 2: issue a warning if the original
|
---|
1024 | image has any pixel where
|
---|
1025 | red != green or red != blue
|
---|
1026 | error_action = 3: issue an error and abort the
|
---|
1027 | conversion if the original
|
---|
1028 | image has any pixel where
|
---|
1029 | red != green or red != blue
|
---|
1030 |
|
---|
1031 | red_weight: weight of red component times 100000
|
---|
1032 | green_weight: weight of green component times 100000
|
---|
1033 | If either weight is negative, default
|
---|
1034 | weights (21268, 71514) are used.
|
---|
1035 |
|
---|
1036 | If you have set error_action = 1 or 2, you can
|
---|
1037 | later check whether the image really was gray, after processing
|
---|
1038 | the image rows, with the png_get_rgb_to_gray_status(png_ptr) function.
|
---|
1039 | It will return a png_byte that is zero if the image was gray or
|
---|
1040 | 1 if there were any non-gray pixels. bKGD and sBIT data
|
---|
1041 | will be silently converted to grayscale, using the green channel
|
---|
1042 | data, regardless of the error_action setting.
|
---|
1043 |
|
---|
1044 | With red_weight+green_weight<=100000,
|
---|
1045 | the normalized graylevel is computed:
|
---|
1046 |
|
---|
1047 | int rw = red_weight * 65536;
|
---|
1048 | int gw = green_weight * 65536;
|
---|
1049 | int bw = 65536 - (rw + gw);
|
---|
1050 | gray = (rw*red + gw*green + bw*blue)/65536;
|
---|
1051 |
|
---|
1052 | The default values come from the PNG file cHRM chunk if present; otherwise, the
|
---|
1053 | defaults correspond to the ITU-R recommendation 709, and also the sRGB color
|
---|
1054 | space, as recommended in the Charles Poynton's Colour FAQ,
|
---|
1055 | Copyright (c) 2006-11-28 Charles Poynton, in section 9:
|
---|
1056 |
|
---|
1057 | <http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC9>
|
---|
1058 |
|
---|
1059 | Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
|
---|
1060 |
|
---|
1061 | Libpng approximates this with
|
---|
1062 |
|
---|
1063 | Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
|
---|
1064 |
|
---|
1065 | which can be expressed with integers as
|
---|
1066 |
|
---|
1067 | Y = (6969 * R + 23434 * G + 2365 * B)/32768
|
---|
1068 |
|
---|
1069 | The calculation is done in a linear colorspace, if the image gamma
|
---|
1070 | is known.
|
---|
1071 |
|
---|
1072 | If you have a grayscale and you are using png_set_expand_depth(),
|
---|
1073 | png_set_expand(), or png_set_gray_to_rgb to change to truecolor or to
|
---|
1074 | a higher bit-depth, you must either supply the background color as a gray
|
---|
1075 | value at the original file bit-depth (need_expand = 1) or else supply the
|
---|
1076 | background color as an RGB triplet at the final, expanded bit depth
|
---|
1077 | (need_expand = 0). Similarly, if you are reading a paletted image, you
|
---|
1078 | must either supply the background color as a palette index (need_expand = 1)
|
---|
1079 | or as an RGB triplet that may or may not be in the palette (need_expand = 0).
|
---|
1080 |
|
---|
1081 | png_color_16 my_background;
|
---|
1082 | png_color_16p image_background;
|
---|
1083 |
|
---|
1084 | if (png_get_bKGD(png_ptr, info_ptr, &image_background))
|
---|
1085 | png_set_background(png_ptr, image_background,
|
---|
1086 | PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
|
---|
1087 | else
|
---|
1088 | png_set_background(png_ptr, &my_background,
|
---|
1089 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
|
---|
1090 |
|
---|
1091 | The png_set_background() function tells libpng to composite images
|
---|
1092 | with alpha or simple transparency against the supplied background
|
---|
1093 | color. If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
|
---|
1094 | you may use this color, or supply another color more suitable for
|
---|
1095 | the current display (e.g., the background color from a web page). You
|
---|
1096 | need to tell libpng whether the color is in the gamma space of the
|
---|
1097 | display (PNG_BACKGROUND_GAMMA_SCREEN for colors you supply), the file
|
---|
1098 | (PNG_BACKGROUND_GAMMA_FILE for colors from the bKGD chunk), or one
|
---|
1099 | that is neither of these gammas (PNG_BACKGROUND_GAMMA_UNIQUE - I don't
|
---|
1100 | know why anyone would use this, but it's here).
|
---|
1101 |
|
---|
1102 | To properly display PNG images on any kind of system, the application needs
|
---|
1103 | to know what the display gamma is. Ideally, the user will know this, and
|
---|
1104 | the application will allow them to set it. One method of allowing the user
|
---|
1105 | to set the display gamma separately for each system is to check for a
|
---|
1106 | SCREEN_GAMMA or DISPLAY_GAMMA environment variable, which will hopefully be
|
---|
1107 | correctly set.
|
---|
1108 |
|
---|
1109 | Note that display_gamma is the overall gamma correction required to produce
|
---|
1110 | pleasing results, which depends on the lighting conditions in the surrounding
|
---|
1111 | environment. In a dim or brightly lit room, no compensation other than
|
---|
1112 | the physical gamma exponent of the monitor is needed, while in a dark room
|
---|
1113 | a slightly smaller exponent is better.
|
---|
1114 |
|
---|
1115 | double gamma, screen_gamma;
|
---|
1116 |
|
---|
1117 | if (/* We have a user-defined screen
|
---|
1118 | gamma value */)
|
---|
1119 | {
|
---|
1120 | screen_gamma = user_defined_screen_gamma;
|
---|
1121 | }
|
---|
1122 | /* One way that applications can share the same
|
---|
1123 | screen gamma value */
|
---|
1124 | else if ((gamma_str = getenv("SCREEN_GAMMA"))
|
---|
1125 | != NULL)
|
---|
1126 | {
|
---|
1127 | screen_gamma = (double)atof(gamma_str);
|
---|
1128 | }
|
---|
1129 | /* If we don't have another value */
|
---|
1130 | else
|
---|
1131 | {
|
---|
1132 | screen_gamma = 2.2; /* A good guess for a
|
---|
1133 | PC monitor in a bright office or a dim room */
|
---|
1134 | screen_gamma = 2.0; /* A good guess for a
|
---|
1135 | PC monitor in a dark room */
|
---|
1136 | screen_gamma = 1.7 or 1.0; /* A good
|
---|
1137 | guess for Mac systems */
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | The png_set_gamma() function handles gamma transformations of the data.
|
---|
1141 | Pass both the file gamma and the current screen_gamma. If the file does
|
---|
1142 | not have a gamma value, you can pass one anyway if you have an idea what
|
---|
1143 | it is (usually 0.45455 is a good guess for GIF images on PCs). Note
|
---|
1144 | that file gammas are inverted from screen gammas. See the discussions
|
---|
1145 | on gamma in the PNG specification for an excellent description of what
|
---|
1146 | gamma is, and why all applications should support it. It is strongly
|
---|
1147 | recommended that PNG viewers support gamma correction.
|
---|
1148 |
|
---|
1149 | if (png_get_gAMA(png_ptr, info_ptr, &gamma))
|
---|
1150 | png_set_gamma(png_ptr, screen_gamma, gamma);
|
---|
1151 | else
|
---|
1152 | png_set_gamma(png_ptr, screen_gamma, 0.45455);
|
---|
1153 |
|
---|
1154 | If you need to reduce an RGB file to a paletted file, or if a paletted
|
---|
1155 | file has more entries then will fit on your screen, png_set_dither()
|
---|
1156 | will do that. Note that this is a simple match dither that merely
|
---|
1157 | finds the closest color available. This should work fairly well with
|
---|
1158 | optimized palettes, and fairly badly with linear color cubes. If you
|
---|
1159 | pass a palette that is larger then maximum_colors, the file will
|
---|
1160 | reduce the number of colors in the palette so it will fit into
|
---|
1161 | maximum_colors. If there is a histogram, it will use it to make
|
---|
1162 | more intelligent choices when reducing the palette. If there is no
|
---|
1163 | histogram, it may not do as good a job.
|
---|
1164 |
|
---|
1165 | if (color_type & PNG_COLOR_MASK_COLOR)
|
---|
1166 | {
|
---|
1167 | if (png_get_valid(png_ptr, info_ptr,
|
---|
1168 | PNG_INFO_PLTE))
|
---|
1169 | {
|
---|
1170 | png_uint_16p histogram = NULL;
|
---|
1171 |
|
---|
1172 | png_get_hIST(png_ptr, info_ptr,
|
---|
1173 | &histogram);
|
---|
1174 | png_set_dither(png_ptr, palette, num_palette,
|
---|
1175 | max_screen_colors, histogram, 1);
|
---|
1176 | }
|
---|
1177 | else
|
---|
1178 | {
|
---|
1179 | png_color std_color_cube[MAX_SCREEN_COLORS] =
|
---|
1180 | { ... colors ... };
|
---|
1181 |
|
---|
1182 | png_set_dither(png_ptr, std_color_cube,
|
---|
1183 | MAX_SCREEN_COLORS, MAX_SCREEN_COLORS,
|
---|
1184 | NULL,0);
|
---|
1185 | }
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | PNG files describe monochrome as black being zero and white being one.
|
---|
1189 | The following code will reverse this (make black be one and white be
|
---|
1190 | zero):
|
---|
1191 |
|
---|
1192 | if (bit_depth == 1 && color_type == PNG_COLOR_TYPE_GRAY)
|
---|
1193 | png_set_invert_mono(png_ptr);
|
---|
1194 |
|
---|
1195 | This function can also be used to invert grayscale and gray-alpha images:
|
---|
1196 |
|
---|
1197 | if (color_type == PNG_COLOR_TYPE_GRAY ||
|
---|
1198 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
---|
1199 | png_set_invert_mono(png_ptr);
|
---|
1200 |
|
---|
1201 | PNG files store 16 bit pixels in network byte order (big-endian,
|
---|
1202 | ie. most significant bits first). This code changes the storage to the
|
---|
1203 | other way (little-endian, i.e. least significant bits first, the
|
---|
1204 | way PCs store them):
|
---|
1205 |
|
---|
1206 | if (bit_depth == 16)
|
---|
1207 | png_set_swap(png_ptr);
|
---|
1208 |
|
---|
1209 | If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
|
---|
1210 | need to change the order the pixels are packed into bytes, you can use:
|
---|
1211 |
|
---|
1212 | if (bit_depth < 8)
|
---|
1213 | png_set_packswap(png_ptr);
|
---|
1214 |
|
---|
1215 | Finally, you can write your own transformation function if none of
|
---|
1216 | the existing ones meets your needs. This is done by setting a callback
|
---|
1217 | with
|
---|
1218 |
|
---|
1219 | png_set_read_user_transform_fn(png_ptr,
|
---|
1220 | read_transform_fn);
|
---|
1221 |
|
---|
1222 | You must supply the function
|
---|
1223 |
|
---|
1224 | void read_transform_fn(png_ptr ptr, row_info_ptr
|
---|
1225 | row_info, png_bytep data)
|
---|
1226 |
|
---|
1227 | See pngtest.c for a working example. Your function will be called
|
---|
1228 | after all of the other transformations have been processed.
|
---|
1229 |
|
---|
1230 | You can also set up a pointer to a user structure for use by your
|
---|
1231 | callback function, and you can inform libpng that your transform
|
---|
1232 | function will change the number of channels or bit depth with the
|
---|
1233 | function
|
---|
1234 |
|
---|
1235 | png_set_user_transform_info(png_ptr, user_ptr,
|
---|
1236 | user_depth, user_channels);
|
---|
1237 |
|
---|
1238 | The user's application, not libpng, is responsible for allocating and
|
---|
1239 | freeing any memory required for the user structure.
|
---|
1240 |
|
---|
1241 | You can retrieve the pointer via the function
|
---|
1242 | png_get_user_transform_ptr(). For example:
|
---|
1243 |
|
---|
1244 | voidp read_user_transform_ptr =
|
---|
1245 | png_get_user_transform_ptr(png_ptr);
|
---|
1246 |
|
---|
1247 | The last thing to handle is interlacing; this is covered in detail below,
|
---|
1248 | but you must call the function here if you want libpng to handle expansion
|
---|
1249 | of the interlaced image.
|
---|
1250 |
|
---|
1251 | number_of_passes = png_set_interlace_handling(png_ptr);
|
---|
1252 |
|
---|
1253 | After setting the transformations, libpng can update your png_info
|
---|
1254 | structure to reflect any transformations you've requested with this
|
---|
1255 | call. This is most useful to update the info structure's rowbytes
|
---|
1256 | field so you can use it to allocate your image memory. This function
|
---|
1257 | will also update your palette with the correct screen_gamma and
|
---|
1258 | background if these have been given with the calls above.
|
---|
1259 |
|
---|
1260 | png_read_update_info(png_ptr, info_ptr);
|
---|
1261 |
|
---|
1262 | After you call png_read_update_info(), you can allocate any
|
---|
1263 | memory you need to hold the image. The row data is simply
|
---|
1264 | raw byte data for all forms of images. As the actual allocation
|
---|
1265 | varies among applications, no example will be given. If you
|
---|
1266 | are allocating one large chunk, you will need to build an
|
---|
1267 | array of pointers to each row, as it will be needed for some
|
---|
1268 | of the functions below.
|
---|
1269 |
|
---|
1270 | Reading image data
|
---|
1271 |
|
---|
1272 | After you've allocated memory, you can read the image data.
|
---|
1273 | The simplest way to do this is in one function call. If you are
|
---|
1274 | allocating enough memory to hold the whole image, you can just
|
---|
1275 | call png_read_image() and libpng will read in all the image data
|
---|
1276 | and put it in the memory area supplied. You will need to pass in
|
---|
1277 | an array of pointers to each row.
|
---|
1278 |
|
---|
1279 | This function automatically handles interlacing, so you don't need
|
---|
1280 | to call png_set_interlace_handling() or call this function multiple
|
---|
1281 | times, or any of that other stuff necessary with png_read_rows().
|
---|
1282 |
|
---|
1283 | png_read_image(png_ptr, row_pointers);
|
---|
1284 |
|
---|
1285 | where row_pointers is:
|
---|
1286 |
|
---|
1287 | png_bytep row_pointers[height];
|
---|
1288 |
|
---|
1289 | You can point to void or char or whatever you use for pixels.
|
---|
1290 |
|
---|
1291 | If you don't want to read in the whole image at once, you can
|
---|
1292 | use png_read_rows() instead. If there is no interlacing (check
|
---|
1293 | interlace_type == PNG_INTERLACE_NONE), this is simple:
|
---|
1294 |
|
---|
1295 | png_read_rows(png_ptr, row_pointers, NULL,
|
---|
1296 | number_of_rows);
|
---|
1297 |
|
---|
1298 | where row_pointers is the same as in the png_read_image() call.
|
---|
1299 |
|
---|
1300 | If you are doing this just one row at a time, you can do this with
|
---|
1301 | a single row_pointer instead of an array of row_pointers:
|
---|
1302 |
|
---|
1303 | png_bytep row_pointer = row;
|
---|
1304 | png_read_row(png_ptr, row_pointer, NULL);
|
---|
1305 |
|
---|
1306 | If the file is interlaced (interlace_type != 0 in the IHDR chunk), things
|
---|
1307 | get somewhat harder. The only current (PNG Specification version 1.2)
|
---|
1308 | interlacing type for PNG is (interlace_type == PNG_INTERLACE_ADAM7)
|
---|
1309 | is a somewhat complicated 2D interlace scheme, known as Adam7, that
|
---|
1310 | breaks down an image into seven smaller images of varying size, based
|
---|
1311 | on an 8x8 grid.
|
---|
1312 |
|
---|
1313 | libpng can fill out those images or it can give them to you "as is".
|
---|
1314 | If you want them filled out, there are two ways to do that. The one
|
---|
1315 | mentioned in the PNG specification is to expand each pixel to cover
|
---|
1316 | those pixels that have not been read yet (the "rectangle" method).
|
---|
1317 | This results in a blocky image for the first pass, which gradually
|
---|
1318 | smooths out as more pixels are read. The other method is the "sparkle"
|
---|
1319 | method, where pixels are drawn only in their final locations, with the
|
---|
1320 | rest of the image remaining whatever colors they were initialized to
|
---|
1321 | before the start of the read. The first method usually looks better,
|
---|
1322 | but tends to be slower, as there are more pixels to put in the rows.
|
---|
1323 |
|
---|
1324 | If you don't want libpng to handle the interlacing details, just call
|
---|
1325 | png_read_rows() seven times to read in all seven images. Each of the
|
---|
1326 | images is a valid image by itself, or they can all be combined on an
|
---|
1327 | 8x8 grid to form a single image (although if you intend to combine them
|
---|
1328 | you would be far better off using the libpng interlace handling).
|
---|
1329 |
|
---|
1330 | The first pass will return an image 1/8 as wide as the entire image
|
---|
1331 | (every 8th column starting in column 0) and 1/8 as high as the original
|
---|
1332 | (every 8th row starting in row 0), the second will be 1/8 as wide
|
---|
1333 | (starting in column 4) and 1/8 as high (also starting in row 0). The
|
---|
1334 | third pass will be 1/4 as wide (every 4th pixel starting in column 0) and
|
---|
1335 | 1/8 as high (every 8th row starting in row 4), and the fourth pass will
|
---|
1336 | be 1/4 as wide and 1/4 as high (every 4th column starting in column 2,
|
---|
1337 | and every 4th row starting in row 0). The fifth pass will return an
|
---|
1338 | image 1/2 as wide, and 1/4 as high (starting at column 0 and row 2),
|
---|
1339 | while the sixth pass will be 1/2 as wide and 1/2 as high as the original
|
---|
1340 | (starting in column 1 and row 0). The seventh and final pass will be as
|
---|
1341 | wide as the original, and 1/2 as high, containing all of the odd
|
---|
1342 | numbered scanlines. Phew!
|
---|
1343 |
|
---|
1344 | If you want libpng to expand the images, call this before calling
|
---|
1345 | png_start_read_image() or png_read_update_info():
|
---|
1346 |
|
---|
1347 | if (interlace_type == PNG_INTERLACE_ADAM7)
|
---|
1348 | number_of_passes
|
---|
1349 | = png_set_interlace_handling(png_ptr);
|
---|
1350 |
|
---|
1351 | This will return the number of passes needed. Currently, this
|
---|
1352 | is seven, but may change if another interlace type is added.
|
---|
1353 | This function can be called even if the file is not interlaced,
|
---|
1354 | where it will return one pass.
|
---|
1355 |
|
---|
1356 | If you are not going to display the image after each pass, but are
|
---|
1357 | going to wait until the entire image is read in, use the sparkle
|
---|
1358 | effect. This effect is faster and the end result of either method
|
---|
1359 | is exactly the same. If you are planning on displaying the image
|
---|
1360 | after each pass, the "rectangle" effect is generally considered the
|
---|
1361 | better looking one.
|
---|
1362 |
|
---|
1363 | If you only want the "sparkle" effect, just call png_read_rows() as
|
---|
1364 | normal, with the third parameter NULL. Make sure you make pass over
|
---|
1365 | the image number_of_passes times, and you don't change the data in the
|
---|
1366 | rows between calls. You can change the locations of the data, just
|
---|
1367 | not the data. Each pass only writes the pixels appropriate for that
|
---|
1368 | pass, and assumes the data from previous passes is still valid.
|
---|
1369 |
|
---|
1370 | png_read_rows(png_ptr, row_pointers, NULL,
|
---|
1371 | number_of_rows);
|
---|
1372 |
|
---|
1373 | If you only want the first effect (the rectangles), do the same as
|
---|
1374 | before except pass the row buffer in the third parameter, and leave
|
---|
1375 | the second parameter NULL.
|
---|
1376 |
|
---|
1377 | png_read_rows(png_ptr, NULL, row_pointers,
|
---|
1378 | number_of_rows);
|
---|
1379 |
|
---|
1380 | Finishing a sequential read
|
---|
1381 |
|
---|
1382 | After you are finished reading the image through the
|
---|
1383 | low-level interface, you can finish reading the file. If you are
|
---|
1384 | interested in comments or time, which may be stored either before or
|
---|
1385 | after the image data, you should pass the separate png_info struct if
|
---|
1386 | you want to keep the comments from before and after the image
|
---|
1387 | separate. If you are not interested, you can pass NULL.
|
---|
1388 |
|
---|
1389 | png_read_end(png_ptr, end_info);
|
---|
1390 |
|
---|
1391 | When you are done, you can free all memory allocated by libpng like this:
|
---|
1392 |
|
---|
1393 | png_destroy_read_struct(&png_ptr, &info_ptr,
|
---|
1394 | &end_info);
|
---|
1395 |
|
---|
1396 | It is also possible to individually free the info_ptr members that
|
---|
1397 | point to libpng-allocated storage with the following function:
|
---|
1398 |
|
---|
1399 | png_free_data(png_ptr, info_ptr, mask, seq)
|
---|
1400 | mask - identifies data to be freed, a mask
|
---|
1401 | containing the bitwise OR of one or
|
---|
1402 | more of
|
---|
1403 | PNG_FREE_PLTE, PNG_FREE_TRNS,
|
---|
1404 | PNG_FREE_HIST, PNG_FREE_ICCP,
|
---|
1405 | PNG_FREE_PCAL, PNG_FREE_ROWS,
|
---|
1406 | PNG_FREE_SCAL, PNG_FREE_SPLT,
|
---|
1407 | PNG_FREE_TEXT, PNG_FREE_UNKN,
|
---|
1408 | or simply PNG_FREE_ALL
|
---|
1409 | seq - sequence number of item to be freed
|
---|
1410 | (-1 for all items)
|
---|
1411 |
|
---|
1412 | This function may be safely called when the relevant storage has
|
---|
1413 | already been freed, or has not yet been allocated, or was allocated
|
---|
1414 | by the user and not by libpng, and will in those cases do nothing.
|
---|
1415 | The "seq" parameter is ignored if only one item of the selected data
|
---|
1416 | type, such as PLTE, is allowed. If "seq" is not -1, and multiple items
|
---|
1417 | are allowed for the data type identified in the mask, such as text or
|
---|
1418 | sPLT, only the n'th item in the structure is freed, where n is "seq".
|
---|
1419 |
|
---|
1420 | The default behavior is only to free data that was allocated internally
|
---|
1421 | by libpng. This can be changed, so that libpng will not free the data,
|
---|
1422 | or so that it will free data that was allocated by the user with png_malloc()
|
---|
1423 | or png_zalloc() and passed in via a png_set_*() function, with
|
---|
1424 |
|
---|
1425 | png_data_freer(png_ptr, info_ptr, freer, mask)
|
---|
1426 | mask - which data elements are affected
|
---|
1427 | same choices as in png_free_data()
|
---|
1428 | freer - one of
|
---|
1429 | PNG_DESTROY_WILL_FREE_DATA
|
---|
1430 | PNG_SET_WILL_FREE_DATA
|
---|
1431 | PNG_USER_WILL_FREE_DATA
|
---|
1432 |
|
---|
1433 | This function only affects data that has already been allocated.
|
---|
1434 | You can call this function after reading the PNG data but before calling
|
---|
1435 | any png_set_*() functions, to control whether the user or the png_set_*()
|
---|
1436 | function is responsible for freeing any existing data that might be present,
|
---|
1437 | and again after the png_set_*() functions to control whether the user
|
---|
1438 | or png_destroy_*() is supposed to free the data. When the user assumes
|
---|
1439 | responsibility for libpng-allocated data, the application must use
|
---|
1440 | png_free() to free it, and when the user transfers responsibility to libpng
|
---|
1441 | for data that the user has allocated, the user must have used png_malloc()
|
---|
1442 | or png_zalloc() to allocate it.
|
---|
1443 |
|
---|
1444 | If you allocated your row_pointers in a single block, as suggested above in
|
---|
1445 | the description of the high level read interface, you must not transfer
|
---|
1446 | responsibility for freeing it to the png_set_rows or png_read_destroy function,
|
---|
1447 | because they would also try to free the individual row_pointers[i].
|
---|
1448 |
|
---|
1449 | If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword
|
---|
1450 | separately, do not transfer responsibility for freeing text_ptr to libpng,
|
---|
1451 | because when libpng fills a png_text structure it combines these members with
|
---|
1452 | the key member, and png_free_data() will free only text_ptr.key. Similarly,
|
---|
1453 | if you transfer responsibility for free'ing text_ptr from libpng to your
|
---|
1454 | application, your application must not separately free those members.
|
---|
1455 |
|
---|
1456 | The png_free_data() function will turn off the "valid" flag for anything
|
---|
1457 | it frees. If you need to turn the flag off for a chunk that was freed by
|
---|
1458 | your application instead of by libpng, you can use
|
---|
1459 |
|
---|
1460 | png_set_invalid(png_ptr, info_ptr, mask);
|
---|
1461 | mask - identifies the chunks to be made invalid,
|
---|
1462 | containing the bitwise OR of one or
|
---|
1463 | more of
|
---|
1464 | PNG_INFO_gAMA, PNG_INFO_sBIT,
|
---|
1465 | PNG_INFO_cHRM, PNG_INFO_PLTE,
|
---|
1466 | PNG_INFO_tRNS, PNG_INFO_bKGD,
|
---|
1467 | PNG_INFO_hIST, PNG_INFO_pHYs,
|
---|
1468 | PNG_INFO_oFFs, PNG_INFO_tIME,
|
---|
1469 | PNG_INFO_pCAL, PNG_INFO_sRGB,
|
---|
1470 | PNG_INFO_iCCP, PNG_INFO_sPLT,
|
---|
1471 | PNG_INFO_sCAL, PNG_INFO_IDAT
|
---|
1472 |
|
---|
1473 | For a more compact example of reading a PNG image, see the file example.c.
|
---|
1474 |
|
---|
1475 | Reading PNG files progressively
|
---|
1476 |
|
---|
1477 | The progressive reader is slightly different then the non-progressive
|
---|
1478 | reader. Instead of calling png_read_info(), png_read_rows(), and
|
---|
1479 | png_read_end(), you make one call to png_process_data(), which calls
|
---|
1480 | callbacks when it has the info, a row, or the end of the image. You
|
---|
1481 | set up these callbacks with png_set_progressive_read_fn(). You don't
|
---|
1482 | have to worry about the input/output functions of libpng, as you are
|
---|
1483 | giving the library the data directly in png_process_data(). I will
|
---|
1484 | assume that you have read the section on reading PNG files above,
|
---|
1485 | so I will only highlight the differences (although I will show
|
---|
1486 | all of the code).
|
---|
1487 |
|
---|
1488 | png_structp png_ptr;
|
---|
1489 | png_infop info_ptr;
|
---|
1490 |
|
---|
1491 | /* An example code fragment of how you would
|
---|
1492 | initialize the progressive reader in your
|
---|
1493 | application. */
|
---|
1494 | int
|
---|
1495 | initialize_png_reader()
|
---|
1496 | {
|
---|
1497 | png_ptr = png_create_read_struct
|
---|
1498 | (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
|
---|
1499 | user_error_fn, user_warning_fn);
|
---|
1500 | if (!png_ptr)
|
---|
1501 | return (ERROR);
|
---|
1502 | info_ptr = png_create_info_struct(png_ptr);
|
---|
1503 | if (!info_ptr)
|
---|
1504 | {
|
---|
1505 | png_destroy_read_struct(&png_ptr, (png_infopp)NULL,
|
---|
1506 | (png_infopp)NULL);
|
---|
1507 | return (ERROR);
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | if (setjmp(png_jmpbuf(png_ptr)))
|
---|
1511 | {
|
---|
1512 | png_destroy_read_struct(&png_ptr, &info_ptr,
|
---|
1513 | (png_infopp)NULL);
|
---|
1514 | return (ERROR);
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 | /* This one's new. You can provide functions
|
---|
1518 | to be called when the header info is valid,
|
---|
1519 | when each row is completed, and when the image
|
---|
1520 | is finished. If you aren't using all functions,
|
---|
1521 | you can specify NULL parameters. Even when all
|
---|
1522 | three functions are NULL, you need to call
|
---|
1523 | png_set_progressive_read_fn(). You can use
|
---|
1524 | any struct as the user_ptr (cast to a void pointer
|
---|
1525 | for the function call), and retrieve the pointer
|
---|
1526 | from inside the callbacks using the function
|
---|
1527 |
|
---|
1528 | png_get_progressive_ptr(png_ptr);
|
---|
1529 |
|
---|
1530 | which will return a void pointer, which you have
|
---|
1531 | to cast appropriately.
|
---|
1532 | */
|
---|
1533 | png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
|
---|
1534 | info_callback, row_callback, end_callback);
|
---|
1535 |
|
---|
1536 | return 0;
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | /* A code fragment that you call as you receive blocks
|
---|
1540 | of data */
|
---|
1541 | int
|
---|
1542 | process_data(png_bytep buffer, png_uint_32 length)
|
---|
1543 | {
|
---|
1544 | if (setjmp(png_jmpbuf(png_ptr)))
|
---|
1545 | {
|
---|
1546 | png_destroy_read_struct(&png_ptr, &info_ptr,
|
---|
1547 | (png_infopp)NULL);
|
---|
1548 | return (ERROR);
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | /* This one's new also. Simply give it a chunk
|
---|
1552 | of data from the file stream (in order, of
|
---|
1553 | course). On machines with segmented memory
|
---|
1554 | models machines, don't give it any more than
|
---|
1555 | 64K. The library seems to run fine with sizes
|
---|
1556 | of 4K. Although you can give it much less if
|
---|
1557 | necessary (I assume you can give it chunks of
|
---|
1558 | 1 byte, I haven't tried less then 256 bytes
|
---|
1559 | yet). When this function returns, you may
|
---|
1560 | want to display any rows that were generated
|
---|
1561 | in the row callback if you don't already do
|
---|
1562 | so there.
|
---|
1563 | */
|
---|
1564 | png_process_data(png_ptr, info_ptr, buffer, length);
|
---|
1565 | return 0;
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | /* This function is called (as set by
|
---|
1569 | png_set_progressive_read_fn() above) when enough data
|
---|
1570 | has been supplied so all of the header has been
|
---|
1571 | read.
|
---|
1572 | */
|
---|
1573 | void
|
---|
1574 | info_callback(png_structp png_ptr, png_infop info)
|
---|
1575 | {
|
---|
1576 | /* Do any setup here, including setting any of
|
---|
1577 | the transformations mentioned in the Reading
|
---|
1578 | PNG files section. For now, you _must_ call
|
---|
1579 | either png_start_read_image() or
|
---|
1580 | png_read_update_info() after all the
|
---|
1581 | transformations are set (even if you don't set
|
---|
1582 | any). You may start getting rows before
|
---|
1583 | png_process_data() returns, so this is your
|
---|
1584 | last chance to prepare for that.
|
---|
1585 | */
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | /* This function is called when each row of image
|
---|
1589 | data is complete */
|
---|
1590 | void
|
---|
1591 | row_callback(png_structp png_ptr, png_bytep new_row,
|
---|
1592 | png_uint_32 row_num, int pass)
|
---|
1593 | {
|
---|
1594 | /* If the image is interlaced, and you turned
|
---|
1595 | on the interlace handler, this function will
|
---|
1596 | be called for every row in every pass. Some
|
---|
1597 | of these rows will not be changed from the
|
---|
1598 | previous pass. When the row is not changed,
|
---|
1599 | the new_row variable will be NULL. The rows
|
---|
1600 | and passes are called in order, so you don't
|
---|
1601 | really need the row_num and pass, but I'm
|
---|
1602 | supplying them because it may make your life
|
---|
1603 | easier.
|
---|
1604 |
|
---|
1605 | For the non-NULL rows of interlaced images,
|
---|
1606 | you must call png_progressive_combine_row()
|
---|
1607 | passing in the row and the old row. You can
|
---|
1608 | call this function for NULL rows (it will just
|
---|
1609 | return) and for non-interlaced images (it just
|
---|
1610 | does the memcpy for you) if it will make the
|
---|
1611 | code easier. Thus, you can just do this for
|
---|
1612 | all cases:
|
---|
1613 | */
|
---|
1614 |
|
---|
1615 | png_progressive_combine_row(png_ptr, old_row,
|
---|
1616 | new_row);
|
---|
1617 |
|
---|
1618 | /* where old_row is what was displayed for
|
---|
1619 | previously for the row. Note that the first
|
---|
1620 | pass (pass == 0, really) will completely cover
|
---|
1621 | the old row, so the rows do not have to be
|
---|
1622 | initialized. After the first pass (and only
|
---|
1623 | for interlaced images), you will have to pass
|
---|
1624 | the current row, and the function will combine
|
---|
1625 | the old row and the new row.
|
---|
1626 | */
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | void
|
---|
1630 | end_callback(png_structp png_ptr, png_infop info)
|
---|
1631 | {
|
---|
1632 | /* This function is called after the whole image
|
---|
1633 | has been read, including any chunks after the
|
---|
1634 | image (up to and including the IEND). You
|
---|
1635 | will usually have the same info chunk as you
|
---|
1636 | had in the header, although some data may have
|
---|
1637 | been added to the comments and time fields.
|
---|
1638 |
|
---|
1639 | Most people won't do much here, perhaps setting
|
---|
1640 | a flag that marks the image as finished.
|
---|
1641 | */
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 |
|
---|
1645 |
|
---|
1646 | IV. Writing
|
---|
1647 |
|
---|
1648 | Much of this is very similar to reading. However, everything of
|
---|
1649 | importance is repeated here, so you won't have to constantly look
|
---|
1650 | back up in the reading section to understand writing.
|
---|
1651 |
|
---|
1652 | Setup
|
---|
1653 |
|
---|
1654 | You will want to do the I/O initialization before you get into libpng,
|
---|
1655 | so if it doesn't work, you don't have anything to undo. If you are not
|
---|
1656 | using the standard I/O functions, you will need to replace them with
|
---|
1657 | custom writing functions. See the discussion under Customizing libpng.
|
---|
1658 |
|
---|
1659 | FILE *fp = fopen(file_name, "wb");
|
---|
1660 | if (!fp)
|
---|
1661 | {
|
---|
1662 | return (ERROR);
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | Next, png_struct and png_info need to be allocated and initialized.
|
---|
1666 | As these can be both relatively large, you may not want to store these
|
---|
1667 | on the stack, unless you have stack space to spare. Of course, you
|
---|
1668 | will want to check if they return NULL. If you are also reading,
|
---|
1669 | you won't want to name your read structure and your write structure
|
---|
1670 | both "png_ptr"; you can call them anything you like, such as
|
---|
1671 | "read_ptr" and "write_ptr". Look at pngtest.c, for example.
|
---|
1672 |
|
---|
1673 | png_structp png_ptr = png_create_write_struct
|
---|
1674 | (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
|
---|
1675 | user_error_fn, user_warning_fn);
|
---|
1676 | if (!png_ptr)
|
---|
1677 | return (ERROR);
|
---|
1678 |
|
---|
1679 | png_infop info_ptr = png_create_info_struct(png_ptr);
|
---|
1680 | if (!info_ptr)
|
---|
1681 | {
|
---|
1682 | png_destroy_write_struct(&png_ptr,
|
---|
1683 | (png_infopp)NULL);
|
---|
1684 | return (ERROR);
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | If you want to use your own memory allocation routines,
|
---|
1688 | define PNG_USER_MEM_SUPPORTED and use
|
---|
1689 | png_create_write_struct_2() instead of png_create_write_struct():
|
---|
1690 |
|
---|
1691 | png_structp png_ptr = png_create_write_struct_2
|
---|
1692 | (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
|
---|
1693 | user_error_fn, user_warning_fn, (png_voidp)
|
---|
1694 | user_mem_ptr, user_malloc_fn, user_free_fn);
|
---|
1695 |
|
---|
1696 | After you have these structures, you will need to set up the
|
---|
1697 | error handling. When libpng encounters an error, it expects to
|
---|
1698 | longjmp() back to your routine. Therefore, you will need to call
|
---|
1699 | setjmp() and pass the png_jmpbuf(png_ptr). If you
|
---|
1700 | write the file from different routines, you will need to update
|
---|
1701 | the png_jmpbuf(png_ptr) every time you enter a new routine that will
|
---|
1702 | call a png_*() function. See your documentation of setjmp/longjmp
|
---|
1703 | for your compiler for more information on setjmp/longjmp. See
|
---|
1704 | the discussion on libpng error handling in the Customizing Libpng
|
---|
1705 | section below for more information on the libpng error handling.
|
---|
1706 |
|
---|
1707 | if (setjmp(png_jmpbuf(png_ptr)))
|
---|
1708 | {
|
---|
1709 | png_destroy_write_struct(&png_ptr, &info_ptr);
|
---|
1710 | fclose(fp);
|
---|
1711 | return (ERROR);
|
---|
1712 | }
|
---|
1713 | ...
|
---|
1714 | return;
|
---|
1715 |
|
---|
1716 | If you would rather avoid the complexity of setjmp/longjmp issues,
|
---|
1717 | you can compile libpng with PNG_SETJMP_NOT_SUPPORTED, in which case
|
---|
1718 | errors will result in a call to PNG_ABORT() which defaults to abort().
|
---|
1719 |
|
---|
1720 | Now you need to set up the output code. The default for libpng is to
|
---|
1721 | use the C function fwrite(). If you use this, you will need to pass a
|
---|
1722 | valid FILE * in the function png_init_io(). Be sure that the file is
|
---|
1723 | opened in binary mode. Again, if you wish to handle writing data in
|
---|
1724 | another way, see the discussion on libpng I/O handling in the Customizing
|
---|
1725 | Libpng section below.
|
---|
1726 |
|
---|
1727 | png_init_io(png_ptr, fp);
|
---|
1728 |
|
---|
1729 | If you are embedding your PNG into a datastream such as MNG, and don't
|
---|
1730 | want libpng to write the 8-byte signature, or if you have already
|
---|
1731 | written the signature in your application, use
|
---|
1732 |
|
---|
1733 | png_set_sig_bytes(png_ptr, 8);
|
---|
1734 |
|
---|
1735 | to inform libpng that it should not write a signature.
|
---|
1736 |
|
---|
1737 | Write callbacks
|
---|
1738 |
|
---|
1739 | At this point, you can set up a callback function that will be
|
---|
1740 | called after each row has been written, which you can use to control
|
---|
1741 | a progress meter or the like. It's demonstrated in pngtest.c.
|
---|
1742 | You must supply a function
|
---|
1743 |
|
---|
1744 | void write_row_callback(png_ptr, png_uint_32 row,
|
---|
1745 | int pass);
|
---|
1746 | {
|
---|
1747 | /* put your code here */
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | (You can give it another name that you like instead of "write_row_callback")
|
---|
1751 |
|
---|
1752 | To inform libpng about your function, use
|
---|
1753 |
|
---|
1754 | png_set_write_status_fn(png_ptr, write_row_callback);
|
---|
1755 |
|
---|
1756 | You now have the option of modifying how the compression library will
|
---|
1757 | run. The following functions are mainly for testing, but may be useful
|
---|
1758 | in some cases, like if you need to write PNG files extremely fast and
|
---|
1759 | are willing to give up some compression, or if you want to get the
|
---|
1760 | maximum possible compression at the expense of slower writing. If you
|
---|
1761 | have no special needs in this area, let the library do what it wants by
|
---|
1762 | not calling this function at all, as it has been tuned to deliver a good
|
---|
1763 | speed/compression ratio. The second parameter to png_set_filter() is
|
---|
1764 | the filter method, for which the only valid values are 0 (as of the
|
---|
1765 | July 1999 PNG specification, version 1.2) or 64 (if you are writing
|
---|
1766 | a PNG datastream that is to be embedded in a MNG datastream). The third
|
---|
1767 | parameter is a flag that indicates which filter type(s) are to be tested
|
---|
1768 | for each scanline. See the PNG specification for details on the specific
|
---|
1769 | filter types.
|
---|
1770 |
|
---|
1771 |
|
---|
1772 | /* turn on or off filtering, and/or choose
|
---|
1773 | specific filters. You can use either a single
|
---|
1774 | PNG_FILTER_VALUE_NAME or the bitwise OR of one
|
---|
1775 | or more PNG_FILTER_NAME masks. */
|
---|
1776 | png_set_filter(png_ptr, 0,
|
---|
1777 | PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE |
|
---|
1778 | PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB |
|
---|
1779 | PNG_FILTER_UP | PNG_FILTER_VALUE_UP |
|
---|
1780 | PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG |
|
---|
1781 | PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH|
|
---|
1782 | PNG_ALL_FILTERS);
|
---|
1783 |
|
---|
1784 | If an application
|
---|
1785 | wants to start and stop using particular filters during compression,
|
---|
1786 | it should start out with all of the filters (to ensure that the previous
|
---|
1787 | row of pixels will be stored in case it's needed later), and then add
|
---|
1788 | and remove them after the start of compression.
|
---|
1789 |
|
---|
1790 | If you are writing a PNG datastream that is to be embedded in a MNG
|
---|
1791 | datastream, the second parameter can be either 0 or 64.
|
---|
1792 |
|
---|
1793 | The png_set_compression_*() functions interface to the zlib compression
|
---|
1794 | library, and should mostly be ignored unless you really know what you are
|
---|
1795 | doing. The only generally useful call is png_set_compression_level()
|
---|
1796 | which changes how much time zlib spends on trying to compress the image
|
---|
1797 | data. See the Compression Library (zlib.h and algorithm.txt, distributed
|
---|
1798 | with zlib) for details on the compression levels.
|
---|
1799 |
|
---|
1800 | /* set the zlib compression level */
|
---|
1801 | png_set_compression_level(png_ptr,
|
---|
1802 | Z_BEST_COMPRESSION);
|
---|
1803 |
|
---|
1804 | /* set other zlib parameters */
|
---|
1805 | png_set_compression_mem_level(png_ptr, 8);
|
---|
1806 | png_set_compression_strategy(png_ptr,
|
---|
1807 | Z_DEFAULT_STRATEGY);
|
---|
1808 | png_set_compression_window_bits(png_ptr, 15);
|
---|
1809 | png_set_compression_method(png_ptr, 8);
|
---|
1810 | png_set_compression_buffer_size(png_ptr, 8192)
|
---|
1811 |
|
---|
1812 | extern PNG_EXPORT(void,png_set_zbuf_size)
|
---|
1813 |
|
---|
1814 | Setting the contents of info for output
|
---|
1815 |
|
---|
1816 | You now need to fill in the png_info structure with all the data you
|
---|
1817 | wish to write before the actual image. Note that the only thing you
|
---|
1818 | are allowed to write after the image is the text chunks and the time
|
---|
1819 | chunk (as of PNG Specification 1.2, anyway). See png_write_end() and
|
---|
1820 | the latest PNG specification for more information on that. If you
|
---|
1821 | wish to write them before the image, fill them in now, and flag that
|
---|
1822 | data as being valid. If you want to wait until after the data, don't
|
---|
1823 | fill them until png_write_end(). For all the fields in png_info and
|
---|
1824 | their data types, see png.h. For explanations of what the fields
|
---|
1825 | contain, see the PNG specification.
|
---|
1826 |
|
---|
1827 | Some of the more important parts of the png_info are:
|
---|
1828 |
|
---|
1829 | png_set_IHDR(png_ptr, info_ptr, width, height,
|
---|
1830 | bit_depth, color_type, interlace_type,
|
---|
1831 | compression_type, filter_method)
|
---|
1832 | width - holds the width of the image
|
---|
1833 | in pixels (up to 2^31).
|
---|
1834 | height - holds the height of the image
|
---|
1835 | in pixels (up to 2^31).
|
---|
1836 | bit_depth - holds the bit depth of one of the
|
---|
1837 | image channels.
|
---|
1838 | (valid values are 1, 2, 4, 8, 16
|
---|
1839 | and depend also on the
|
---|
1840 | color_type. See also significant
|
---|
1841 | bits (sBIT) below).
|
---|
1842 | color_type - describes which color/alpha
|
---|
1843 | channels are present.
|
---|
1844 | PNG_COLOR_TYPE_GRAY
|
---|
1845 | (bit depths 1, 2, 4, 8, 16)
|
---|
1846 | PNG_COLOR_TYPE_GRAY_ALPHA
|
---|
1847 | (bit depths 8, 16)
|
---|
1848 | PNG_COLOR_TYPE_PALETTE
|
---|
1849 | (bit depths 1, 2, 4, 8)
|
---|
1850 | PNG_COLOR_TYPE_RGB
|
---|
1851 | (bit_depths 8, 16)
|
---|
1852 | PNG_COLOR_TYPE_RGB_ALPHA
|
---|
1853 | (bit_depths 8, 16)
|
---|
1854 |
|
---|
1855 | PNG_COLOR_MASK_PALETTE
|
---|
1856 | PNG_COLOR_MASK_COLOR
|
---|
1857 | PNG_COLOR_MASK_ALPHA
|
---|
1858 |
|
---|
1859 | interlace_type - PNG_INTERLACE_NONE or
|
---|
1860 | PNG_INTERLACE_ADAM7
|
---|
1861 | compression_type - (must be
|
---|
1862 | PNG_COMPRESSION_TYPE_DEFAULT)
|
---|
1863 | filter_method - (must be PNG_FILTER_TYPE_DEFAULT
|
---|
1864 | or, if you are writing a PNG to
|
---|
1865 | be embedded in a MNG datastream,
|
---|
1866 | can also be
|
---|
1867 | PNG_INTRAPIXEL_DIFFERENCING)
|
---|
1868 |
|
---|
1869 | If you call png_set_IHDR(), the call must appear before any of the
|
---|
1870 | other png_set_*() functions, because they might require access to some of
|
---|
1871 | the IHDR settings. The remaining png_set_*() functions can be called
|
---|
1872 | in any order.
|
---|
1873 |
|
---|
1874 | If you wish, you can reset the compression_type, interlace_type, or
|
---|
1875 | filter_method later by calling png_set_IHDR() again; if you do this, the
|
---|
1876 | width, height, bit_depth, and color_type must be the same in each call.
|
---|
1877 |
|
---|
1878 | png_set_PLTE(png_ptr, info_ptr, palette,
|
---|
1879 | num_palette);
|
---|
1880 | palette - the palette for the file
|
---|
1881 | (array of png_color)
|
---|
1882 | num_palette - number of entries in the palette
|
---|
1883 |
|
---|
1884 | png_set_gAMA(png_ptr, info_ptr, gamma);
|
---|
1885 | gamma - the gamma the image was created
|
---|
1886 | at (PNG_INFO_gAMA)
|
---|
1887 |
|
---|
1888 | png_set_sRGB(png_ptr, info_ptr, srgb_intent);
|
---|
1889 | srgb_intent - the rendering intent
|
---|
1890 | (PNG_INFO_sRGB) The presence of
|
---|
1891 | the sRGB chunk means that the pixel
|
---|
1892 | data is in the sRGB color space.
|
---|
1893 | This chunk also implies specific
|
---|
1894 | values of gAMA and cHRM. Rendering
|
---|
1895 | intent is the CSS-1 property that
|
---|
1896 | has been defined by the International
|
---|
1897 | Color Consortium
|
---|
1898 | (http://www.color.org).
|
---|
1899 | It can be one of
|
---|
1900 | PNG_sRGB_INTENT_SATURATION,
|
---|
1901 | PNG_sRGB_INTENT_PERCEPTUAL,
|
---|
1902 | PNG_sRGB_INTENT_ABSOLUTE, or
|
---|
1903 | PNG_sRGB_INTENT_RELATIVE.
|
---|
1904 |
|
---|
1905 |
|
---|
1906 | png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
|
---|
1907 | srgb_intent);
|
---|
1908 | srgb_intent - the rendering intent
|
---|
1909 | (PNG_INFO_sRGB) The presence of the
|
---|
1910 | sRGB chunk means that the pixel
|
---|
1911 | data is in the sRGB color space.
|
---|
1912 | This function also causes gAMA and
|
---|
1913 | cHRM chunks with the specific values
|
---|
1914 | that are consistent with sRGB to be
|
---|
1915 | written.
|
---|
1916 |
|
---|
1917 | png_set_iCCP(png_ptr, info_ptr, name, compression_type,
|
---|
1918 | profile, proflen);
|
---|
1919 | name - The profile name.
|
---|
1920 | compression - The compression type; always
|
---|
1921 | PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
|
---|
1922 | You may give NULL to this argument to
|
---|
1923 | ignore it.
|
---|
1924 | profile - International Color Consortium color
|
---|
1925 | profile data. May contain NULs.
|
---|
1926 | proflen - length of profile data in bytes.
|
---|
1927 |
|
---|
1928 | png_set_sBIT(png_ptr, info_ptr, sig_bit);
|
---|
1929 | sig_bit - the number of significant bits for
|
---|
1930 | (PNG_INFO_sBIT) each of the gray, red,
|
---|
1931 | green, and blue channels, whichever are
|
---|
1932 | appropriate for the given color type
|
---|
1933 | (png_color_16)
|
---|
1934 |
|
---|
1935 | png_set_tRNS(png_ptr, info_ptr, trans, num_trans,
|
---|
1936 | trans_values);
|
---|
1937 | trans - array of transparent
|
---|
1938 | entries for palette (PNG_INFO_tRNS)
|
---|
1939 | trans_values - graylevel or color sample values
|
---|
1940 | (in order red, green, blue) of the
|
---|
1941 | single transparent color for
|
---|
1942 | non-paletted images (PNG_INFO_tRNS)
|
---|
1943 | num_trans - number of transparent entries
|
---|
1944 | (PNG_INFO_tRNS)
|
---|
1945 |
|
---|
1946 | png_set_hIST(png_ptr, info_ptr, hist);
|
---|
1947 | (PNG_INFO_hIST)
|
---|
1948 | hist - histogram of palette (array of
|
---|
1949 | png_uint_16)
|
---|
1950 |
|
---|
1951 | png_set_tIME(png_ptr, info_ptr, mod_time);
|
---|
1952 | mod_time - time image was last modified
|
---|
1953 | (PNG_VALID_tIME)
|
---|
1954 |
|
---|
1955 | png_set_bKGD(png_ptr, info_ptr, background);
|
---|
1956 | background - background color (PNG_VALID_bKGD)
|
---|
1957 |
|
---|
1958 | png_set_text(png_ptr, info_ptr, text_ptr, num_text);
|
---|
1959 | text_ptr - array of png_text holding image
|
---|
1960 | comments
|
---|
1961 | text_ptr[i].compression - type of compression used
|
---|
1962 | on "text" PNG_TEXT_COMPRESSION_NONE
|
---|
1963 | PNG_TEXT_COMPRESSION_zTXt
|
---|
1964 | PNG_ITXT_COMPRESSION_NONE
|
---|
1965 | PNG_ITXT_COMPRESSION_zTXt
|
---|
1966 | text_ptr[i].key - keyword for comment. Must contain
|
---|
1967 | 1-79 characters.
|
---|
1968 | text_ptr[i].text - text comments for current
|
---|
1969 | keyword. Can be NULL or empty.
|
---|
1970 | text_ptr[i].text_length - length of text string,
|
---|
1971 | after decompression, 0 for iTXt
|
---|
1972 | text_ptr[i].itxt_length - length of itxt string,
|
---|
1973 | after decompression, 0 for tEXt/zTXt
|
---|
1974 | text_ptr[i].lang - language of comment (NULL or
|
---|
1975 | empty for unknown).
|
---|
1976 | text_ptr[i].translated_keyword - keyword in UTF-8 (NULL
|
---|
1977 | or empty for unknown).
|
---|
1978 | Note that the itxt_length, lang, and lang_key
|
---|
1979 | members of the text_ptr structure only exist
|
---|
1980 | when the library is built with iTXt chunk support.
|
---|
1981 |
|
---|
1982 | num_text - number of comments
|
---|
1983 |
|
---|
1984 | png_set_sPLT(png_ptr, info_ptr, &palette_ptr,
|
---|
1985 | num_spalettes);
|
---|
1986 | palette_ptr - array of png_sPLT_struct structures
|
---|
1987 | to be added to the list of palettes
|
---|
1988 | in the info structure.
|
---|
1989 | num_spalettes - number of palette structures to be
|
---|
1990 | added.
|
---|
1991 |
|
---|
1992 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y,
|
---|
1993 | unit_type);
|
---|
1994 | offset_x - positive offset from the left
|
---|
1995 | edge of the screen
|
---|
1996 | offset_y - positive offset from the top
|
---|
1997 | edge of the screen
|
---|
1998 | unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
|
---|
1999 |
|
---|
2000 | png_set_pHYs(png_ptr, info_ptr, res_x, res_y,
|
---|
2001 | unit_type);
|
---|
2002 | res_x - pixels/unit physical resolution
|
---|
2003 | in x direction
|
---|
2004 | res_y - pixels/unit physical resolution
|
---|
2005 | in y direction
|
---|
2006 | unit_type - PNG_RESOLUTION_UNKNOWN,
|
---|
2007 | PNG_RESOLUTION_METER
|
---|
2008 |
|
---|
2009 | png_set_sCAL(png_ptr, info_ptr, unit, width, height)
|
---|
2010 | unit - physical scale units (an integer)
|
---|
2011 | width - width of a pixel in physical scale units
|
---|
2012 | height - height of a pixel in physical scale units
|
---|
2013 | (width and height are doubles)
|
---|
2014 |
|
---|
2015 | png_set_sCAL_s(png_ptr, info_ptr, unit, width, height)
|
---|
2016 | unit - physical scale units (an integer)
|
---|
2017 | width - width of a pixel in physical scale units
|
---|
2018 | height - height of a pixel in physical scale units
|
---|
2019 | (width and height are strings like "2.54")
|
---|
2020 |
|
---|
2021 | png_set_unknown_chunks(png_ptr, info_ptr, &unknowns,
|
---|
2022 | num_unknowns)
|
---|
2023 | unknowns - array of png_unknown_chunk
|
---|
2024 | structures holding unknown chunks
|
---|
2025 | unknowns[i].name - name of unknown chunk
|
---|
2026 | unknowns[i].data - data of unknown chunk
|
---|
2027 | unknowns[i].size - size of unknown chunk's data
|
---|
2028 | unknowns[i].location - position to write chunk in file
|
---|
2029 | 0: do not write chunk
|
---|
2030 | PNG_HAVE_IHDR: before PLTE
|
---|
2031 | PNG_HAVE_PLTE: before IDAT
|
---|
2032 | PNG_AFTER_IDAT: after IDAT
|
---|
2033 |
|
---|
2034 | The "location" member is set automatically according to
|
---|
2035 | what part of the output file has already been written.
|
---|
2036 | You can change its value after calling png_set_unknown_chunks()
|
---|
2037 | as demonstrated in pngtest.c. Within each of the "locations",
|
---|
2038 | the chunks are sequenced according to their position in the
|
---|
2039 | structure (that is, the value of "i", which is the order in which
|
---|
2040 | the chunk was either read from the input file or defined with
|
---|
2041 | png_set_unknown_chunks).
|
---|
2042 |
|
---|
2043 | A quick word about text and num_text. text is an array of png_text
|
---|
2044 | structures. num_text is the number of valid structures in the array.
|
---|
2045 | Each png_text structure holds a language code, a keyword, a text value,
|
---|
2046 | and a compression type.
|
---|
2047 |
|
---|
2048 | The compression types have the same valid numbers as the compression
|
---|
2049 | types of the image data. Currently, the only valid number is zero.
|
---|
2050 | However, you can store text either compressed or uncompressed, unlike
|
---|
2051 | images, which always have to be compressed. So if you don't want the
|
---|
2052 | text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
|
---|
2053 | Because tEXt and zTXt chunks don't have a language field, if you
|
---|
2054 | specify PNG_TEXT_COMPRESSION_NONE or PNG_TEXT_COMPRESSION_zTXt
|
---|
2055 | any language code or translated keyword will not be written out.
|
---|
2056 |
|
---|
2057 | Until text gets around 1000 bytes, it is not worth compressing it.
|
---|
2058 | After the text has been written out to the file, the compression type
|
---|
2059 | is set to PNG_TEXT_COMPRESSION_NONE_WR or PNG_TEXT_COMPRESSION_zTXt_WR,
|
---|
2060 | so that it isn't written out again at the end (in case you are calling
|
---|
2061 | png_write_end() with the same struct.
|
---|
2062 |
|
---|
2063 | The keywords that are given in the PNG Specification are:
|
---|
2064 |
|
---|
2065 | Title Short (one line) title or
|
---|
2066 | caption for image
|
---|
2067 | Author Name of image's creator
|
---|
2068 | Description Description of image (possibly long)
|
---|
2069 | Copyright Copyright notice
|
---|
2070 | Creation Time Time of original image creation
|
---|
2071 | (usually RFC 1123 format, see below)
|
---|
2072 | Software Software used to create the image
|
---|
2073 | Disclaimer Legal disclaimer
|
---|
2074 | Warning Warning of nature of content
|
---|
2075 | Source Device used to create the image
|
---|
2076 | Comment Miscellaneous comment; conversion
|
---|
2077 | from other image format
|
---|
2078 |
|
---|
2079 | The keyword-text pairs work like this. Keywords should be short
|
---|
2080 | simple descriptions of what the comment is about. Some typical
|
---|
2081 | keywords are found in the PNG specification, as is some recommendations
|
---|
2082 | on keywords. You can repeat keywords in a file. You can even write
|
---|
2083 | some text before the image and some after. For example, you may want
|
---|
2084 | to put a description of the image before the image, but leave the
|
---|
2085 | disclaimer until after, so viewers working over modem connections
|
---|
2086 | don't have to wait for the disclaimer to go over the modem before
|
---|
2087 | they start seeing the image. Finally, keywords should be full
|
---|
2088 | words, not abbreviations. Keywords and text are in the ISO 8859-1
|
---|
2089 | (Latin-1) character set (a superset of regular ASCII) and can not
|
---|
2090 | contain NUL characters, and should not contain control or other
|
---|
2091 | unprintable characters. To make the comments widely readable, stick
|
---|
2092 | with basic ASCII, and avoid machine specific character set extensions
|
---|
2093 | like the IBM-PC character set. The keyword must be present, but
|
---|
2094 | you can leave off the text string on non-compressed pairs.
|
---|
2095 | Compressed pairs must have a text string, as only the text string
|
---|
2096 | is compressed anyway, so the compression would be meaningless.
|
---|
2097 |
|
---|
2098 | PNG supports modification time via the png_time structure. Two
|
---|
2099 | conversion routines are provided, png_convert_from_time_t() for
|
---|
2100 | time_t and png_convert_from_struct_tm() for struct tm. The
|
---|
2101 | time_t routine uses gmtime(). You don't have to use either of
|
---|
2102 | these, but if you wish to fill in the png_time structure directly,
|
---|
2103 | you should provide the time in universal time (GMT) if possible
|
---|
2104 | instead of your local time. Note that the year number is the full
|
---|
2105 | year (e.g. 1998, rather than 98 - PNG is year 2000 compliant!), and
|
---|
2106 | that months start with 1.
|
---|
2107 |
|
---|
2108 | If you want to store the time of the original image creation, you should
|
---|
2109 | use a plain tEXt chunk with the "Creation Time" keyword. This is
|
---|
2110 | necessary because the "creation time" of a PNG image is somewhat vague,
|
---|
2111 | depending on whether you mean the PNG file, the time the image was
|
---|
2112 | created in a non-PNG format, a still photo from which the image was
|
---|
2113 | scanned, or possibly the subject matter itself. In order to facilitate
|
---|
2114 | machine-readable dates, it is recommended that the "Creation Time"
|
---|
2115 | tEXt chunk use RFC 1123 format dates (e.g. "22 May 1997 18:07:10 GMT"),
|
---|
2116 | although this isn't a requirement. Unlike the tIME chunk, the
|
---|
2117 | "Creation Time" tEXt chunk is not expected to be automatically changed
|
---|
2118 | by the software. To facilitate the use of RFC 1123 dates, a function
|
---|
2119 | png_convert_to_rfc1123(png_timep) is provided to convert from PNG
|
---|
2120 | time to an RFC 1123 format string.
|
---|
2121 |
|
---|
2122 | Writing unknown chunks
|
---|
2123 |
|
---|
2124 | You can use the png_set_unknown_chunks function to queue up chunks
|
---|
2125 | for writing. You give it a chunk name, raw data, and a size; that's
|
---|
2126 | all there is to it. The chunks will be written by the next following
|
---|
2127 | png_write_info_before_PLTE, png_write_info, or png_write_end function.
|
---|
2128 | Any chunks previously read into the info structure's unknown-chunk
|
---|
2129 | list will also be written out in a sequence that satisfies the PNG
|
---|
2130 | specification's ordering rules.
|
---|
2131 |
|
---|
2132 | The high-level write interface
|
---|
2133 |
|
---|
2134 | At this point there are two ways to proceed; through the high-level
|
---|
2135 | write interface, or through a sequence of low-level write operations.
|
---|
2136 | You can use the high-level interface if your image data is present
|
---|
2137 | in the info structure. All defined output
|
---|
2138 | transformations are permitted, enabled by the following masks.
|
---|
2139 |
|
---|
2140 | PNG_TRANSFORM_IDENTITY No transformation
|
---|
2141 | PNG_TRANSFORM_PACKING Pack 1, 2 and 4-bit samples
|
---|
2142 | PNG_TRANSFORM_PACKSWAP Change order of packed
|
---|
2143 | pixels to LSB first
|
---|
2144 | PNG_TRANSFORM_INVERT_MONO Invert monochrome images
|
---|
2145 | PNG_TRANSFORM_SHIFT Normalize pixels to the
|
---|
2146 | sBIT depth
|
---|
2147 | PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA
|
---|
2148 | to BGRA
|
---|
2149 | PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA
|
---|
2150 | to AG
|
---|
2151 | PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity
|
---|
2152 | to transparency
|
---|
2153 | PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples
|
---|
2154 | PNG_TRANSFORM_STRIP_FILLER Strip out filler
|
---|
2155 | bytes (deprecated).
|
---|
2156 | PNG_TRANSFORM_STRIP_FILLER_BEFORE Strip out leading
|
---|
2157 | filler bytes
|
---|
2158 | PNG_TRANSFORM_STRIP_FILLER_AFTER Strip out trailing
|
---|
2159 | filler bytes
|
---|
2160 |
|
---|
2161 | If you have valid image data in the info structure (you can use
|
---|
2162 | png_set_rows() to put image data in the info structure), simply do this:
|
---|
2163 |
|
---|
2164 | png_write_png(png_ptr, info_ptr, png_transforms, NULL)
|
---|
2165 |
|
---|
2166 | where png_transforms is an integer containing the bitwise OR of some set of
|
---|
2167 | transformation flags. This call is equivalent to png_write_info(),
|
---|
2168 | followed the set of transformations indicated by the transform mask,
|
---|
2169 | then png_write_image(), and finally png_write_end().
|
---|
2170 |
|
---|
2171 | (The final parameter of this call is not yet used. Someday it might point
|
---|
2172 | to transformation parameters required by some future output transform.)
|
---|
2173 |
|
---|
2174 | You must use png_transforms and not call any png_set_transform() functions
|
---|
2175 | when you use png_write_png().
|
---|
2176 |
|
---|
2177 | The low-level write interface
|
---|
2178 |
|
---|
2179 | If you are going the low-level route instead, you are now ready to
|
---|
2180 | write all the file information up to the actual image data. You do
|
---|
2181 | this with a call to png_write_info().
|
---|
2182 |
|
---|
2183 | png_write_info(png_ptr, info_ptr);
|
---|
2184 |
|
---|
2185 | Note that there is one transformation you may need to do before
|
---|
2186 | png_write_info(). In PNG files, the alpha channel in an image is the
|
---|
2187 | level of opacity. If your data is supplied as a level of transparency,
|
---|
2188 | you can invert the alpha channel before you write it, so that 0 is
|
---|
2189 | fully transparent and 255 (in 8-bit or paletted images) or 65535
|
---|
2190 | (in 16-bit images) is fully opaque, with
|
---|
2191 |
|
---|
2192 | png_set_invert_alpha(png_ptr);
|
---|
2193 |
|
---|
2194 | This must appear before png_write_info() instead of later with the
|
---|
2195 | other transformations because in the case of paletted images the tRNS
|
---|
2196 | chunk data has to be inverted before the tRNS chunk is written. If
|
---|
2197 | your image is not a paletted image, the tRNS data (which in such cases
|
---|
2198 | represents a single color to be rendered as transparent) won't need to
|
---|
2199 | be changed, and you can safely do this transformation after your
|
---|
2200 | png_write_info() call.
|
---|
2201 |
|
---|
2202 | If you need to write a private chunk that you want to appear before
|
---|
2203 | the PLTE chunk when PLTE is present, you can write the PNG info in
|
---|
2204 | two steps, and insert code to write your own chunk between them:
|
---|
2205 |
|
---|
2206 | png_write_info_before_PLTE(png_ptr, info_ptr);
|
---|
2207 | png_set_unknown_chunks(png_ptr, info_ptr, ...);
|
---|
2208 | png_write_info(png_ptr, info_ptr);
|
---|
2209 |
|
---|
2210 | After you've written the file information, you can set up the library
|
---|
2211 | to handle any special transformations of the image data. The various
|
---|
2212 | ways to transform the data will be described in the order that they
|
---|
2213 | should occur. This is important, as some of these change the color
|
---|
2214 | type and/or bit depth of the data, and some others only work on
|
---|
2215 | certain color types and bit depths. Even though each transformation
|
---|
2216 | checks to see if it has data that it can do something with, you should
|
---|
2217 | make sure to only enable a transformation if it will be valid for the
|
---|
2218 | data. For example, don't swap red and blue on grayscale data.
|
---|
2219 |
|
---|
2220 | PNG files store RGB pixels packed into 3 or 6 bytes. This code tells
|
---|
2221 | the library to strip input data that has 4 or 8 bytes per pixel down
|
---|
2222 | to 3 or 6 bytes (or strip 2 or 4-byte grayscale+filler data to 1 or 2
|
---|
2223 | bytes per pixel).
|
---|
2224 |
|
---|
2225 | png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
|
---|
2226 |
|
---|
2227 | where the 0 is unused, and the location is either PNG_FILLER_BEFORE or
|
---|
2228 | PNG_FILLER_AFTER, depending upon whether the filler byte in the pixel
|
---|
2229 | is stored XRGB or RGBX.
|
---|
2230 |
|
---|
2231 | PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
|
---|
2232 | they can, resulting in, for example, 8 pixels per byte for 1 bit files.
|
---|
2233 | If the data is supplied at 1 pixel per byte, use this code, which will
|
---|
2234 | correctly pack the pixels into a single byte:
|
---|
2235 |
|
---|
2236 | png_set_packing(png_ptr);
|
---|
2237 |
|
---|
2238 | PNG files reduce possible bit depths to 1, 2, 4, 8, and 16. If your
|
---|
2239 | data is of another bit depth, you can write an sBIT chunk into the
|
---|
2240 | file so that decoders can recover the original data if desired.
|
---|
2241 |
|
---|
2242 | /* Set the true bit depth of the image data */
|
---|
2243 | if (color_type & PNG_COLOR_MASK_COLOR)
|
---|
2244 | {
|
---|
2245 | sig_bit.red = true_bit_depth;
|
---|
2246 | sig_bit.green = true_bit_depth;
|
---|
2247 | sig_bit.blue = true_bit_depth;
|
---|
2248 | }
|
---|
2249 | else
|
---|
2250 | {
|
---|
2251 | sig_bit.gray = true_bit_depth;
|
---|
2252 | }
|
---|
2253 | if (color_type & PNG_COLOR_MASK_ALPHA)
|
---|
2254 | {
|
---|
2255 | sig_bit.alpha = true_bit_depth;
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | png_set_sBIT(png_ptr, info_ptr, &sig_bit);
|
---|
2259 |
|
---|
2260 | If the data is stored in the row buffer in a bit depth other than
|
---|
2261 | one supported by PNG (e.g. 3 bit data in the range 0-7 for a 4-bit PNG),
|
---|
2262 | this will scale the values to appear to be the correct bit depth as
|
---|
2263 | is required by PNG.
|
---|
2264 |
|
---|
2265 | png_set_shift(png_ptr, &sig_bit);
|
---|
2266 |
|
---|
2267 | PNG files store 16 bit pixels in network byte order (big-endian,
|
---|
2268 | ie. most significant bits first). This code would be used if they are
|
---|
2269 | supplied the other way (little-endian, i.e. least significant bits
|
---|
2270 | first, the way PCs store them):
|
---|
2271 |
|
---|
2272 | if (bit_depth > 8)
|
---|
2273 | png_set_swap(png_ptr);
|
---|
2274 |
|
---|
2275 | If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
|
---|
2276 | need to change the order the pixels are packed into bytes, you can use:
|
---|
2277 |
|
---|
2278 | if (bit_depth < 8)
|
---|
2279 | png_set_packswap(png_ptr);
|
---|
2280 |
|
---|
2281 | PNG files store 3 color pixels in red, green, blue order. This code
|
---|
2282 | would be used if they are supplied as blue, green, red:
|
---|
2283 |
|
---|
2284 | png_set_bgr(png_ptr);
|
---|
2285 |
|
---|
2286 | PNG files describe monochrome as black being zero and white being
|
---|
2287 | one. This code would be used if the pixels are supplied with this reversed
|
---|
2288 | (black being one and white being zero):
|
---|
2289 |
|
---|
2290 | png_set_invert_mono(png_ptr);
|
---|
2291 |
|
---|
2292 | Finally, you can write your own transformation function if none of
|
---|
2293 | the existing ones meets your needs. This is done by setting a callback
|
---|
2294 | with
|
---|
2295 |
|
---|
2296 | png_set_write_user_transform_fn(png_ptr,
|
---|
2297 | write_transform_fn);
|
---|
2298 |
|
---|
2299 | You must supply the function
|
---|
2300 |
|
---|
2301 | void write_transform_fn(png_ptr ptr, row_info_ptr
|
---|
2302 | row_info, png_bytep data)
|
---|
2303 |
|
---|
2304 | See pngtest.c for a working example. Your function will be called
|
---|
2305 | before any of the other transformations are processed.
|
---|
2306 |
|
---|
2307 | You can also set up a pointer to a user structure for use by your
|
---|
2308 | callback function.
|
---|
2309 |
|
---|
2310 | png_set_user_transform_info(png_ptr, user_ptr, 0, 0);
|
---|
2311 |
|
---|
2312 | The user_channels and user_depth parameters of this function are ignored
|
---|
2313 | when writing; you can set them to zero as shown.
|
---|
2314 |
|
---|
2315 | You can retrieve the pointer via the function png_get_user_transform_ptr().
|
---|
2316 | For example:
|
---|
2317 |
|
---|
2318 | voidp write_user_transform_ptr =
|
---|
2319 | png_get_user_transform_ptr(png_ptr);
|
---|
2320 |
|
---|
2321 | It is possible to have libpng flush any pending output, either manually,
|
---|
2322 | or automatically after a certain number of lines have been written. To
|
---|
2323 | flush the output stream a single time call:
|
---|
2324 |
|
---|
2325 | png_write_flush(png_ptr);
|
---|
2326 |
|
---|
2327 | and to have libpng flush the output stream periodically after a certain
|
---|
2328 | number of scanlines have been written, call:
|
---|
2329 |
|
---|
2330 | png_set_flush(png_ptr, nrows);
|
---|
2331 |
|
---|
2332 | Note that the distance between rows is from the last time png_write_flush()
|
---|
2333 | was called, or the first row of the image if it has never been called.
|
---|
2334 | So if you write 50 lines, and then png_set_flush 25, it will flush the
|
---|
2335 | output on the next scanline, and every 25 lines thereafter, unless
|
---|
2336 | png_write_flush() is called before 25 more lines have been written.
|
---|
2337 | If nrows is too small (less than about 10 lines for a 640 pixel wide
|
---|
2338 | RGB image) the image compression may decrease noticeably (although this
|
---|
2339 | may be acceptable for real-time applications). Infrequent flushing will
|
---|
2340 | only degrade the compression performance by a few percent over images
|
---|
2341 | that do not use flushing.
|
---|
2342 |
|
---|
2343 | Writing the image data
|
---|
2344 |
|
---|
2345 | That's it for the transformations. Now you can write the image data.
|
---|
2346 | The simplest way to do this is in one function call. If you have the
|
---|
2347 | whole image in memory, you can just call png_write_image() and libpng
|
---|
2348 | will write the image. You will need to pass in an array of pointers to
|
---|
2349 | each row. This function automatically handles interlacing, so you don't
|
---|
2350 | need to call png_set_interlace_handling() or call this function multiple
|
---|
2351 | times, or any of that other stuff necessary with png_write_rows().
|
---|
2352 |
|
---|
2353 | png_write_image(png_ptr, row_pointers);
|
---|
2354 |
|
---|
2355 | where row_pointers is:
|
---|
2356 |
|
---|
2357 | png_byte *row_pointers[height];
|
---|
2358 |
|
---|
2359 | You can point to void or char or whatever you use for pixels.
|
---|
2360 |
|
---|
2361 | If you don't want to write the whole image at once, you can
|
---|
2362 | use png_write_rows() instead. If the file is not interlaced,
|
---|
2363 | this is simple:
|
---|
2364 |
|
---|
2365 | png_write_rows(png_ptr, row_pointers,
|
---|
2366 | number_of_rows);
|
---|
2367 |
|
---|
2368 | row_pointers is the same as in the png_write_image() call.
|
---|
2369 |
|
---|
2370 | If you are just writing one row at a time, you can do this with
|
---|
2371 | a single row_pointer instead of an array of row_pointers:
|
---|
2372 |
|
---|
2373 | png_bytep row_pointer = row;
|
---|
2374 |
|
---|
2375 | png_write_row(png_ptr, row_pointer);
|
---|
2376 |
|
---|
2377 | When the file is interlaced, things can get a good deal more complicated.
|
---|
2378 | The only currently (as of the PNG Specification version 1.2, dated July
|
---|
2379 | 1999) defined interlacing scheme for PNG files is the "Adam7" interlace
|
---|
2380 | scheme, that breaks down an image into seven smaller images of varying
|
---|
2381 | size. libpng will build these images for you, or you can do them
|
---|
2382 | yourself. If you want to build them yourself, see the PNG specification
|
---|
2383 | for details of which pixels to write when.
|
---|
2384 |
|
---|
2385 | If you don't want libpng to handle the interlacing details, just
|
---|
2386 | use png_set_interlace_handling() and call png_write_rows() the
|
---|
2387 | correct number of times to write all seven sub-images.
|
---|
2388 |
|
---|
2389 | If you want libpng to build the sub-images, call this before you start
|
---|
2390 | writing any rows:
|
---|
2391 |
|
---|
2392 | number_of_passes =
|
---|
2393 | png_set_interlace_handling(png_ptr);
|
---|
2394 |
|
---|
2395 | This will return the number of passes needed. Currently, this is seven,
|
---|
2396 | but may change if another interlace type is added.
|
---|
2397 |
|
---|
2398 | Then write the complete image number_of_passes times.
|
---|
2399 |
|
---|
2400 | png_write_rows(png_ptr, row_pointers,
|
---|
2401 | number_of_rows);
|
---|
2402 |
|
---|
2403 | As some of these rows are not used, and thus return immediately, you may
|
---|
2404 | want to read about interlacing in the PNG specification, and only update
|
---|
2405 | the rows that are actually used.
|
---|
2406 |
|
---|
2407 | Finishing a sequential write
|
---|
2408 |
|
---|
2409 | After you are finished writing the image, you should finish writing
|
---|
2410 | the file. If you are interested in writing comments or time, you should
|
---|
2411 | pass an appropriately filled png_info pointer. If you are not interested,
|
---|
2412 | you can pass NULL.
|
---|
2413 |
|
---|
2414 | png_write_end(png_ptr, info_ptr);
|
---|
2415 |
|
---|
2416 | When you are done, you can free all memory used by libpng like this:
|
---|
2417 |
|
---|
2418 | png_destroy_write_struct(&png_ptr, &info_ptr);
|
---|
2419 |
|
---|
2420 | It is also possible to individually free the info_ptr members that
|
---|
2421 | point to libpng-allocated storage with the following function:
|
---|
2422 |
|
---|
2423 | png_free_data(png_ptr, info_ptr, mask, seq)
|
---|
2424 | mask - identifies data to be freed, a mask
|
---|
2425 | containing the bitwise OR of one or
|
---|
2426 | more of
|
---|
2427 | PNG_FREE_PLTE, PNG_FREE_TRNS,
|
---|
2428 | PNG_FREE_HIST, PNG_FREE_ICCP,
|
---|
2429 | PNG_FREE_PCAL, PNG_FREE_ROWS,
|
---|
2430 | PNG_FREE_SCAL, PNG_FREE_SPLT,
|
---|
2431 | PNG_FREE_TEXT, PNG_FREE_UNKN,
|
---|
2432 | or simply PNG_FREE_ALL
|
---|
2433 | seq - sequence number of item to be freed
|
---|
2434 | (-1 for all items)
|
---|
2435 |
|
---|
2436 | This function may be safely called when the relevant storage has
|
---|
2437 | already been freed, or has not yet been allocated, or was allocated
|
---|
2438 | by the user and not by libpng, and will in those cases do nothing.
|
---|
2439 | The "seq" parameter is ignored if only one item of the selected data
|
---|
2440 | type, such as PLTE, is allowed. If "seq" is not -1, and multiple items
|
---|
2441 | are allowed for the data type identified in the mask, such as text or
|
---|
2442 | sPLT, only the n'th item in the structure is freed, where n is "seq".
|
---|
2443 |
|
---|
2444 | If you allocated data such as a palette that you passed in to libpng
|
---|
2445 | with png_set_*, you must not free it until just before the call to
|
---|
2446 | png_destroy_write_struct().
|
---|
2447 |
|
---|
2448 | The default behavior is only to free data that was allocated internally
|
---|
2449 | by libpng. This can be changed, so that libpng will not free the data,
|
---|
2450 | or so that it will free data that was allocated by the user with png_malloc()
|
---|
2451 | or png_zalloc() and passed in via a png_set_*() function, with
|
---|
2452 |
|
---|
2453 | png_data_freer(png_ptr, info_ptr, freer, mask)
|
---|
2454 | mask - which data elements are affected
|
---|
2455 | same choices as in png_free_data()
|
---|
2456 | freer - one of
|
---|
2457 | PNG_DESTROY_WILL_FREE_DATA
|
---|
2458 | PNG_SET_WILL_FREE_DATA
|
---|
2459 | PNG_USER_WILL_FREE_DATA
|
---|
2460 |
|
---|
2461 | For example, to transfer responsibility for some data from a read structure
|
---|
2462 | to a write structure, you could use
|
---|
2463 |
|
---|
2464 | png_data_freer(read_ptr, read_info_ptr,
|
---|
2465 | PNG_USER_WILL_FREE_DATA,
|
---|
2466 | PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
|
---|
2467 | png_data_freer(write_ptr, write_info_ptr,
|
---|
2468 | PNG_DESTROY_WILL_FREE_DATA,
|
---|
2469 | PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
|
---|
2470 |
|
---|
2471 | thereby briefly reassigning responsibility for freeing to the user but
|
---|
2472 | immediately afterwards reassigning it once more to the write_destroy
|
---|
2473 | function. Having done this, it would then be safe to destroy the read
|
---|
2474 | structure and continue to use the PLTE, tRNS, and hIST data in the write
|
---|
2475 | structure.
|
---|
2476 |
|
---|
2477 | This function only affects data that has already been allocated.
|
---|
2478 | You can call this function before calling after the png_set_*() functions
|
---|
2479 | to control whether the user or png_destroy_*() is supposed to free the data.
|
---|
2480 | When the user assumes responsibility for libpng-allocated data, the
|
---|
2481 | application must use
|
---|
2482 | png_free() to free it, and when the user transfers responsibility to libpng
|
---|
2483 | for data that the user has allocated, the user must have used png_malloc()
|
---|
2484 | or png_zalloc() to allocate it.
|
---|
2485 |
|
---|
2486 | If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword
|
---|
2487 | separately, do not transfer responsibility for freeing text_ptr to libpng,
|
---|
2488 | because when libpng fills a png_text structure it combines these members with
|
---|
2489 | the key member, and png_free_data() will free only text_ptr.key. Similarly,
|
---|
2490 | if you transfer responsibility for free'ing text_ptr from libpng to your
|
---|
2491 | application, your application must not separately free those members.
|
---|
2492 | For a more compact example of writing a PNG image, see the file example.c.
|
---|
2493 |
|
---|
2494 | V. Modifying/Customizing libpng:
|
---|
2495 |
|
---|
2496 | There are two issues here. The first is changing how libpng does
|
---|
2497 | standard things like memory allocation, input/output, and error handling.
|
---|
2498 | The second deals with more complicated things like adding new chunks,
|
---|
2499 | adding new transformations, and generally changing how libpng works.
|
---|
2500 | Both of those are compile-time issues; that is, they are generally
|
---|
2501 | determined at the time the code is written, and there is rarely a need
|
---|
2502 | to provide the user with a means of changing them.
|
---|
2503 |
|
---|
2504 | Memory allocation, input/output, and error handling
|
---|
2505 |
|
---|
2506 | All of the memory allocation, input/output, and error handling in libpng
|
---|
2507 | goes through callbacks that are user-settable. The default routines are
|
---|
2508 | in pngmem.c, pngrio.c, pngwio.c, and pngerror.c, respectively. To change
|
---|
2509 | these functions, call the appropriate png_set_*_fn() function.
|
---|
2510 |
|
---|
2511 | Memory allocation is done through the functions png_malloc(), png_calloc(),
|
---|
2512 | and png_free(). These currently just call the standard C functions.
|
---|
2513 | png_calloc() calls png_malloc() and then png_memset() to clear the newly
|
---|
2514 | allocated memory to zero. If your pointers can't access more then 64K
|
---|
2515 | at a time, you will want to set MAXSEG_64K in zlib.h. Since it is
|
---|
2516 | unlikely that the method of handling memory allocation on a platform
|
---|
2517 | will change between applications, these functions must be modified in
|
---|
2518 | the library at compile time. If you prefer to use a different method
|
---|
2519 | of allocating and freeing data, you can use png_create_read_struct_2() or
|
---|
2520 | png_create_write_struct_2() to register your own functions as described
|
---|
2521 | above. These functions also provide a void pointer that can be retrieved
|
---|
2522 | via
|
---|
2523 |
|
---|
2524 | mem_ptr=png_get_mem_ptr(png_ptr);
|
---|
2525 |
|
---|
2526 | Your replacement memory functions must have prototypes as follows:
|
---|
2527 |
|
---|
2528 | png_voidp malloc_fn(png_structp png_ptr,
|
---|
2529 | png_size_t size);
|
---|
2530 | void free_fn(png_structp png_ptr, png_voidp ptr);
|
---|
2531 |
|
---|
2532 | Your malloc_fn() must return NULL in case of failure. The png_malloc()
|
---|
2533 | function will normally call png_error() if it receives a NULL from the
|
---|
2534 | system memory allocator or from your replacement malloc_fn().
|
---|
2535 |
|
---|
2536 | Your free_fn() will never be called with a NULL ptr, since libpng's
|
---|
2537 | png_free() checks for NULL before calling free_fn().
|
---|
2538 |
|
---|
2539 | Input/Output in libpng is done through png_read() and png_write(),
|
---|
2540 | which currently just call fread() and fwrite(). The FILE * is stored in
|
---|
2541 | png_struct and is initialized via png_init_io(). If you wish to change
|
---|
2542 | the method of I/O, the library supplies callbacks that you can set
|
---|
2543 | through the function png_set_read_fn() and png_set_write_fn() at run
|
---|
2544 | time, instead of calling the png_init_io() function. These functions
|
---|
2545 | also provide a void pointer that can be retrieved via the function
|
---|
2546 | png_get_io_ptr(). For example:
|
---|
2547 |
|
---|
2548 | png_set_read_fn(png_structp read_ptr,
|
---|
2549 | voidp read_io_ptr, png_rw_ptr read_data_fn)
|
---|
2550 |
|
---|
2551 | png_set_write_fn(png_structp write_ptr,
|
---|
2552 | voidp write_io_ptr, png_rw_ptr write_data_fn,
|
---|
2553 | png_flush_ptr output_flush_fn);
|
---|
2554 |
|
---|
2555 | voidp read_io_ptr = png_get_io_ptr(read_ptr);
|
---|
2556 | voidp write_io_ptr = png_get_io_ptr(write_ptr);
|
---|
2557 |
|
---|
2558 | The replacement I/O functions must have prototypes as follows:
|
---|
2559 |
|
---|
2560 | void user_read_data(png_structp png_ptr,
|
---|
2561 | png_bytep data, png_size_t length);
|
---|
2562 | void user_write_data(png_structp png_ptr,
|
---|
2563 | png_bytep data, png_size_t length);
|
---|
2564 | void user_flush_data(png_structp png_ptr);
|
---|
2565 |
|
---|
2566 | The user_read_data() function is responsible for detecting and
|
---|
2567 | handling end-of-data errors.
|
---|
2568 |
|
---|
2569 | Supplying NULL for the read, write, or flush functions sets them back
|
---|
2570 | to using the default C stream functions, which expect the io_ptr to
|
---|
2571 | point to a standard *FILE structure. It is probably a mistake
|
---|
2572 | to use NULL for one of write_data_fn and output_flush_fn but not both
|
---|
2573 | of them, unless you have built libpng with PNG_NO_WRITE_FLUSH defined.
|
---|
2574 | It is an error to read from a write stream, and vice versa.
|
---|
2575 |
|
---|
2576 | Error handling in libpng is done through png_error() and png_warning().
|
---|
2577 | Errors handled through png_error() are fatal, meaning that png_error()
|
---|
2578 | should never return to its caller. Currently, this is handled via
|
---|
2579 | setjmp() and longjmp() (unless you have compiled libpng with
|
---|
2580 | PNG_SETJMP_NOT_SUPPORTED, in which case it is handled via PNG_ABORT()),
|
---|
2581 | but you could change this to do things like exit() if you should wish.
|
---|
2582 |
|
---|
2583 | On non-fatal errors, png_warning() is called
|
---|
2584 | to print a warning message, and then control returns to the calling code.
|
---|
2585 | By default png_error() and png_warning() print a message on stderr via
|
---|
2586 | fprintf() unless the library is compiled with PNG_NO_CONSOLE_IO defined
|
---|
2587 | (because you don't want the messages) or PNG_NO_STDIO defined (because
|
---|
2588 | fprintf() isn't available). If you wish to change the behavior of the error
|
---|
2589 | functions, you will need to set up your own message callbacks. These
|
---|
2590 | functions are normally supplied at the time that the png_struct is created.
|
---|
2591 | It is also possible to redirect errors and warnings to your own replacement
|
---|
2592 | functions after png_create_*_struct() has been called by calling:
|
---|
2593 |
|
---|
2594 | png_set_error_fn(png_structp png_ptr,
|
---|
2595 | png_voidp error_ptr, png_error_ptr error_fn,
|
---|
2596 | png_error_ptr warning_fn);
|
---|
2597 |
|
---|
2598 | png_voidp error_ptr = png_get_error_ptr(png_ptr);
|
---|
2599 |
|
---|
2600 | If NULL is supplied for either error_fn or warning_fn, then the libpng
|
---|
2601 | default function will be used, calling fprintf() and/or longjmp() if a
|
---|
2602 | problem is encountered. The replacement error functions should have
|
---|
2603 | parameters as follows:
|
---|
2604 |
|
---|
2605 | void user_error_fn(png_structp png_ptr,
|
---|
2606 | png_const_charp error_msg);
|
---|
2607 | void user_warning_fn(png_structp png_ptr,
|
---|
2608 | png_const_charp warning_msg);
|
---|
2609 |
|
---|
2610 | The motivation behind using setjmp() and longjmp() is the C++ throw and
|
---|
2611 | catch exception handling methods. This makes the code much easier to write,
|
---|
2612 | as there is no need to check every return code of every function call.
|
---|
2613 | However, there are some uncertainties about the status of local variables
|
---|
2614 | after a longjmp, so the user may want to be careful about doing anything
|
---|
2615 | after setjmp returns non-zero besides returning itself. Consult your
|
---|
2616 | compiler documentation for more details. For an alternative approach, you
|
---|
2617 | may wish to use the "cexcept" facility (see http://cexcept.sourceforge.net).
|
---|
2618 |
|
---|
2619 | Custom chunks
|
---|
2620 |
|
---|
2621 | If you need to read or write custom chunks, you may need to get deeper
|
---|
2622 | into the libpng code. The library now has mechanisms for storing
|
---|
2623 | and writing chunks of unknown type; you can even declare callbacks
|
---|
2624 | for custom chunks. However, this may not be good enough if the
|
---|
2625 | library code itself needs to know about interactions between your
|
---|
2626 | chunk and existing `intrinsic' chunks.
|
---|
2627 |
|
---|
2628 | If you need to write a new intrinsic chunk, first read the PNG
|
---|
2629 | specification. Acquire a first level of understanding of how it works.
|
---|
2630 | Pay particular attention to the sections that describe chunk names,
|
---|
2631 | and look at how other chunks were designed, so you can do things
|
---|
2632 | similarly. Second, check out the sections of libpng that read and
|
---|
2633 | write chunks. Try to find a chunk that is similar to yours and use
|
---|
2634 | it as a template. More details can be found in the comments inside
|
---|
2635 | the code. It is best to handle unknown chunks in a generic method,
|
---|
2636 | via callback functions, instead of by modifying libpng functions.
|
---|
2637 |
|
---|
2638 | If you wish to write your own transformation for the data, look through
|
---|
2639 | the part of the code that does the transformations, and check out some of
|
---|
2640 | the simpler ones to get an idea of how they work. Try to find a similar
|
---|
2641 | transformation to the one you want to add and copy off of it. More details
|
---|
2642 | can be found in the comments inside the code itself.
|
---|
2643 |
|
---|
2644 | Configuring for 16 bit platforms
|
---|
2645 |
|
---|
2646 | You will want to look into zconf.h to tell zlib (and thus libpng) that
|
---|
2647 | it cannot allocate more then 64K at a time. Even if you can, the memory
|
---|
2648 | won't be accessible. So limit zlib and libpng to 64K by defining MAXSEG_64K.
|
---|
2649 |
|
---|
2650 | Configuring for DOS
|
---|
2651 |
|
---|
2652 | For DOS users who only have access to the lower 640K, you will
|
---|
2653 | have to limit zlib's memory usage via a png_set_compression_mem_level()
|
---|
2654 | call. See zlib.h or zconf.h in the zlib library for more information.
|
---|
2655 |
|
---|
2656 | Configuring for Medium Model
|
---|
2657 |
|
---|
2658 | Libpng's support for medium model has been tested on most of the popular
|
---|
2659 | compilers. Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets
|
---|
2660 | defined, and FAR gets defined to far in pngconf.h, and you should be
|
---|
2661 | all set. Everything in the library (except for zlib's structure) is
|
---|
2662 | expecting far data. You must use the typedefs with the p or pp on
|
---|
2663 | the end for pointers (or at least look at them and be careful). Make
|
---|
2664 | note that the rows of data are defined as png_bytepp, which is an
|
---|
2665 | unsigned char far * far *.
|
---|
2666 |
|
---|
2667 | Configuring for gui/windowing platforms:
|
---|
2668 |
|
---|
2669 | You will need to write new error and warning functions that use the GUI
|
---|
2670 | interface, as described previously, and set them to be the error and
|
---|
2671 | warning functions at the time that png_create_*_struct() is called,
|
---|
2672 | in order to have them available during the structure initialization.
|
---|
2673 | They can be changed later via png_set_error_fn(). On some compilers,
|
---|
2674 | you may also have to change the memory allocators (png_malloc, etc.).
|
---|
2675 |
|
---|
2676 | Configuring for compiler xxx:
|
---|
2677 |
|
---|
2678 | All includes for libpng are in pngconf.h. If you need to add, change
|
---|
2679 | or delete an include, this is the place to do it.
|
---|
2680 | The includes that are not needed outside libpng are protected by the
|
---|
2681 | PNG_INTERNAL definition, which is only defined for those routines inside
|
---|
2682 | libpng itself. The files in libpng proper only include png.h, which
|
---|
2683 | includes pngconf.h.
|
---|
2684 |
|
---|
2685 | Configuring zlib:
|
---|
2686 |
|
---|
2687 | There are special functions to configure the compression. Perhaps the
|
---|
2688 | most useful one changes the compression level, which currently uses
|
---|
2689 | input compression values in the range 0 - 9. The library normally
|
---|
2690 | uses the default compression level (Z_DEFAULT_COMPRESSION = 6). Tests
|
---|
2691 | have shown that for a large majority of images, compression values in
|
---|
2692 | the range 3-6 compress nearly as well as higher levels, and do so much
|
---|
2693 | faster. For online applications it may be desirable to have maximum speed
|
---|
2694 | (Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also
|
---|
2695 | specify no compression (Z_NO_COMPRESSION = 0), but this would create
|
---|
2696 | files larger than just storing the raw bitmap. You can specify the
|
---|
2697 | compression level by calling:
|
---|
2698 |
|
---|
2699 | png_set_compression_level(png_ptr, level);
|
---|
2700 |
|
---|
2701 | Another useful one is to reduce the memory level used by the library.
|
---|
2702 | The memory level defaults to 8, but it can be lowered if you are
|
---|
2703 | short on memory (running DOS, for example, where you only have 640K).
|
---|
2704 | Note that the memory level does have an effect on compression; among
|
---|
2705 | other things, lower levels will result in sections of incompressible
|
---|
2706 | data being emitted in smaller stored blocks, with a correspondingly
|
---|
2707 | larger relative overhead of up to 15% in the worst case.
|
---|
2708 |
|
---|
2709 | png_set_compression_mem_level(png_ptr, level);
|
---|
2710 |
|
---|
2711 | The other functions are for configuring zlib. They are not recommended
|
---|
2712 | for normal use and may result in writing an invalid PNG file. See
|
---|
2713 | zlib.h for more information on what these mean.
|
---|
2714 |
|
---|
2715 | png_set_compression_strategy(png_ptr,
|
---|
2716 | strategy);
|
---|
2717 | png_set_compression_window_bits(png_ptr,
|
---|
2718 | window_bits);
|
---|
2719 | png_set_compression_method(png_ptr, method);
|
---|
2720 | png_set_compression_buffer_size(png_ptr, size);
|
---|
2721 |
|
---|
2722 | Controlling row filtering
|
---|
2723 |
|
---|
2724 | If you want to control whether libpng uses filtering or not, which
|
---|
2725 | filters are used, and how it goes about picking row filters, you
|
---|
2726 | can call one of these functions. The selection and configuration
|
---|
2727 | of row filters can have a significant impact on the size and
|
---|
2728 | encoding speed and a somewhat lesser impact on the decoding speed
|
---|
2729 | of an image. Filtering is enabled by default for RGB and grayscale
|
---|
2730 | images (with and without alpha), but not for paletted images nor
|
---|
2731 | for any images with bit depths less than 8 bits/pixel.
|
---|
2732 |
|
---|
2733 | The 'method' parameter sets the main filtering method, which is
|
---|
2734 | currently only '0' in the PNG 1.2 specification. The 'filters'
|
---|
2735 | parameter sets which filter(s), if any, should be used for each
|
---|
2736 | scanline. Possible values are PNG_ALL_FILTERS and PNG_NO_FILTERS
|
---|
2737 | to turn filtering on and off, respectively.
|
---|
2738 |
|
---|
2739 | Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB,
|
---|
2740 | PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise
|
---|
2741 | ORed together with '|' to specify one or more filters to use.
|
---|
2742 | These filters are described in more detail in the PNG specification.
|
---|
2743 | If you intend to change the filter type during the course of writing
|
---|
2744 | the image, you should start with flags set for all of the filters
|
---|
2745 | you intend to use so that libpng can initialize its internal
|
---|
2746 | structures appropriately for all of the filter types. (Note that this
|
---|
2747 | means the first row must always be adaptively filtered, because libpng
|
---|
2748 | currently does not allocate the filter buffers until png_write_row()
|
---|
2749 | is called for the first time.)
|
---|
2750 |
|
---|
2751 | filters = PNG_FILTER_NONE | PNG_FILTER_SUB
|
---|
2752 | PNG_FILTER_UP | PNG_FILTER_AVG |
|
---|
2753 | PNG_FILTER_PAETH | PNG_ALL_FILTERS;
|
---|
2754 |
|
---|
2755 | png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE,
|
---|
2756 | filters);
|
---|
2757 | The second parameter can also be
|
---|
2758 | PNG_INTRAPIXEL_DIFFERENCING if you are
|
---|
2759 | writing a PNG to be embedded in a MNG
|
---|
2760 | datastream. This parameter must be the
|
---|
2761 | same as the value of filter_method used
|
---|
2762 | in png_set_IHDR().
|
---|
2763 |
|
---|
2764 | Removing unwanted object code
|
---|
2765 |
|
---|
2766 | There are a bunch of #define's in pngconf.h that control what parts of
|
---|
2767 | libpng are compiled. All the defines end in _SUPPORTED. If you are
|
---|
2768 | never going to use a capability, you can change the #define to #undef
|
---|
2769 | before recompiling libpng and save yourself code and data space, or
|
---|
2770 | you can turn off individual capabilities with defines that begin with
|
---|
2771 | PNG_NO_.
|
---|
2772 |
|
---|
2773 | You can also turn all of the transforms and ancillary chunk capabilities
|
---|
2774 | off en masse with compiler directives that define
|
---|
2775 | PNG_NO_READ[or WRITE]_TRANSFORMS, or PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS,
|
---|
2776 | or all four,
|
---|
2777 | along with directives to turn on any of the capabilities that you do
|
---|
2778 | want. The PNG_NO_READ[or WRITE]_TRANSFORMS directives disable the extra
|
---|
2779 | transformations but still leave the library fully capable of reading
|
---|
2780 | and writing PNG files with all known public chunks. Use of the
|
---|
2781 | PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS directive produces a library
|
---|
2782 | that is incapable of reading or writing ancillary chunks. If you are
|
---|
2783 | not using the progressive reading capability, you can turn that off
|
---|
2784 | with PNG_NO_PROGRESSIVE_READ (don't confuse this with the INTERLACING
|
---|
2785 | capability, which you'll still have).
|
---|
2786 |
|
---|
2787 | All the reading and writing specific code are in separate files, so the
|
---|
2788 | linker should only grab the files it needs. However, if you want to
|
---|
2789 | make sure, or if you are building a stand alone library, all the
|
---|
2790 | reading files start with pngr and all the writing files start with
|
---|
2791 | pngw. The files that don't match either (like png.c, pngtrans.c, etc.)
|
---|
2792 | are used for both reading and writing, and always need to be included.
|
---|
2793 | The progressive reader is in pngpread.c
|
---|
2794 |
|
---|
2795 | If you are creating or distributing a dynamically linked library (a .so
|
---|
2796 | or DLL file), you should not remove or disable any parts of the library,
|
---|
2797 | as this will cause applications linked with different versions of the
|
---|
2798 | library to fail if they call functions not available in your library.
|
---|
2799 | The size of the library itself should not be an issue, because only
|
---|
2800 | those sections that are actually used will be loaded into memory.
|
---|
2801 |
|
---|
2802 | Requesting debug printout
|
---|
2803 |
|
---|
2804 | The macro definition PNG_DEBUG can be used to request debugging
|
---|
2805 | printout. Set it to an integer value in the range 0 to 3. Higher
|
---|
2806 | numbers result in increasing amounts of debugging information. The
|
---|
2807 | information is printed to the "stderr" file, unless another file
|
---|
2808 | name is specified in the PNG_DEBUG_FILE macro definition.
|
---|
2809 |
|
---|
2810 | When PNG_DEBUG > 0, the following functions (macros) become available:
|
---|
2811 |
|
---|
2812 | png_debug(level, message)
|
---|
2813 | png_debug1(level, message, p1)
|
---|
2814 | png_debug2(level, message, p1, p2)
|
---|
2815 |
|
---|
2816 | in which "level" is compared to PNG_DEBUG to decide whether to print
|
---|
2817 | the message, "message" is the formatted string to be printed,
|
---|
2818 | and p1 and p2 are parameters that are to be embedded in the string
|
---|
2819 | according to printf-style formatting directives. For example,
|
---|
2820 |
|
---|
2821 | png_debug1(2, "foo=%d", foo);
|
---|
2822 |
|
---|
2823 | is expanded to
|
---|
2824 |
|
---|
2825 | if(PNG_DEBUG > 2)
|
---|
2826 | fprintf(PNG_DEBUG_FILE, "foo=%d\n", foo);
|
---|
2827 |
|
---|
2828 | When PNG_DEBUG is defined but is zero, the macros aren't defined, but you
|
---|
2829 | can still use PNG_DEBUG to control your own debugging:
|
---|
2830 |
|
---|
2831 | #ifdef PNG_DEBUG
|
---|
2832 | fprintf(stderr, ...
|
---|
2833 | #endif
|
---|
2834 |
|
---|
2835 | When PNG_DEBUG = 1, the macros are defined, but only png_debug statements
|
---|
2836 | having level = 0 will be printed. There aren't any such statements in
|
---|
2837 | this version of libpng, but if you insert some they will be printed.
|
---|
2838 |
|
---|
2839 | VI. MNG support
|
---|
2840 |
|
---|
2841 | The MNG specification (available at http://www.libpng.org/pub/mng) allows
|
---|
2842 | certain extensions to PNG for PNG images that are embedded in MNG datastreams.
|
---|
2843 | Libpng can support some of these extensions. To enable them, use the
|
---|
2844 | png_permit_mng_features() function:
|
---|
2845 |
|
---|
2846 | feature_set = png_permit_mng_features(png_ptr, mask)
|
---|
2847 | mask is a png_uint_32 containing the bitwise OR of the
|
---|
2848 | features you want to enable. These include
|
---|
2849 | PNG_FLAG_MNG_EMPTY_PLTE
|
---|
2850 | PNG_FLAG_MNG_FILTER_64
|
---|
2851 | PNG_ALL_MNG_FEATURES
|
---|
2852 | feature_set is a png_uint_32 that is the bitwise AND of
|
---|
2853 | your mask with the set of MNG features that is
|
---|
2854 | supported by the version of libpng that you are using.
|
---|
2855 |
|
---|
2856 | It is an error to use this function when reading or writing a standalone
|
---|
2857 | PNG file with the PNG 8-byte signature. The PNG datastream must be wrapped
|
---|
2858 | in a MNG datastream. As a minimum, it must have the MNG 8-byte signature
|
---|
2859 | and the MHDR and MEND chunks. Libpng does not provide support for these
|
---|
2860 | or any other MNG chunks; your application must provide its own support for
|
---|
2861 | them. You may wish to consider using libmng (available at
|
---|
2862 | http://www.libmng.com) instead.
|
---|
2863 |
|
---|
2864 | VII. Changes to Libpng from version 0.88
|
---|
2865 |
|
---|
2866 | It should be noted that versions of libpng later than 0.96 are not
|
---|
2867 | distributed by the original libpng author, Guy Schalnat, nor by
|
---|
2868 | Andreas Dilger, who had taken over from Guy during 1996 and 1997, and
|
---|
2869 | distributed versions 0.89 through 0.96, but rather by another member
|
---|
2870 | of the original PNG Group, Glenn Randers-Pehrson. Guy and Andreas are
|
---|
2871 | still alive and well, but they have moved on to other things.
|
---|
2872 |
|
---|
2873 | The old libpng functions png_read_init(), png_write_init(),
|
---|
2874 | png_info_init(), png_read_destroy(), and png_write_destroy() have been
|
---|
2875 | moved to PNG_INTERNAL in version 0.95 to discourage their use. These
|
---|
2876 | functions will be removed from libpng version 2.0.0.
|
---|
2877 |
|
---|
2878 | The preferred method of creating and initializing the libpng structures is
|
---|
2879 | via the png_create_read_struct(), png_create_write_struct(), and
|
---|
2880 | png_create_info_struct() because they isolate the size of the structures
|
---|
2881 | from the application, allow version error checking, and also allow the
|
---|
2882 | use of custom error handling routines during the initialization, which
|
---|
2883 | the old functions do not. The functions png_read_destroy() and
|
---|
2884 | png_write_destroy() do not actually free the memory that libpng
|
---|
2885 | allocated for these structs, but just reset the data structures, so they
|
---|
2886 | can be used instead of png_destroy_read_struct() and
|
---|
2887 | png_destroy_write_struct() if you feel there is too much system overhead
|
---|
2888 | allocating and freeing the png_struct for each image read.
|
---|
2889 |
|
---|
2890 | Setting the error callbacks via png_set_message_fn() before
|
---|
2891 | png_read_init() as was suggested in libpng-0.88 is no longer supported
|
---|
2892 | because this caused applications that do not use custom error functions
|
---|
2893 | to fail if the png_ptr was not initialized to zero. It is still possible
|
---|
2894 | to set the error callbacks AFTER png_read_init(), or to change them with
|
---|
2895 | png_set_error_fn(), which is essentially the same function, but with a new
|
---|
2896 | name to force compilation errors with applications that try to use the old
|
---|
2897 | method.
|
---|
2898 |
|
---|
2899 | Support for the sCAL, iCCP, iTXt, and sPLT chunks was added at libpng-1.0.6;
|
---|
2900 | however, iTXt support was not enabled by default.
|
---|
2901 |
|
---|
2902 | Starting with version 1.0.7, you can find out which version of the library
|
---|
2903 | you are using at run-time:
|
---|
2904 |
|
---|
2905 | png_uint_32 libpng_vn = png_access_version_number();
|
---|
2906 |
|
---|
2907 | The number libpng_vn is constructed from the major version, minor
|
---|
2908 | version with leading zero, and release number with leading zero,
|
---|
2909 | (e.g., libpng_vn for version 1.0.7 is 10007).
|
---|
2910 |
|
---|
2911 | You can also check which version of png.h you used when compiling your
|
---|
2912 | application:
|
---|
2913 |
|
---|
2914 | png_uint_32 application_vn = PNG_LIBPNG_VER;
|
---|
2915 |
|
---|
2916 | VIII. Changes to Libpng from version 1.0.x to 1.2.x
|
---|
2917 |
|
---|
2918 | Support for user memory management was enabled by default. To
|
---|
2919 | accomplish this, the functions png_create_read_struct_2(),
|
---|
2920 | png_create_write_struct_2(), png_set_mem_fn(), png_get_mem_ptr(),
|
---|
2921 | png_malloc_default(), and png_free_default() were added.
|
---|
2922 |
|
---|
2923 | Support for the iTXt chunk has been enabled by default as of
|
---|
2924 | version 1.2.41.
|
---|
2925 |
|
---|
2926 | Support for certain MNG features was enabled.
|
---|
2927 |
|
---|
2928 | Support for numbered error messages was added. However, we never got
|
---|
2929 | around to actually numbering the error messages. The function
|
---|
2930 | png_set_strip_error_numbers() was added (Note: the prototype for this
|
---|
2931 | function was inadvertently removed from png.h in PNG_NO_ASSEMBLER_CODE
|
---|
2932 | builds of libpng-1.2.15. It was restored in libpng-1.2.36).
|
---|
2933 |
|
---|
2934 | The png_malloc_warn() function was added at libpng-1.2.3. This issues
|
---|
2935 | a png_warning and returns NULL instead of aborting when it fails to
|
---|
2936 | acquire the requested memory allocation.
|
---|
2937 |
|
---|
2938 | Support for setting user limits on image width and height was enabled
|
---|
2939 | by default. The functions png_set_user_limits(), png_get_user_width_max(),
|
---|
2940 | and png_get_user_height_max() were added at libpng-1.2.6.
|
---|
2941 |
|
---|
2942 | The png_set_add_alpha() function was added at libpng-1.2.7.
|
---|
2943 |
|
---|
2944 | The function png_set_expand_gray_1_2_4_to_8() was added at libpng-1.2.9.
|
---|
2945 | Unlike png_set_gray_1_2_4_to_8(), the new function does not expand the
|
---|
2946 | tRNS chunk to alpha. The png_set_gray_1_2_4_to_8() function is
|
---|
2947 | deprecated.
|
---|
2948 |
|
---|
2949 | A number of macro definitions in support of runtime selection of
|
---|
2950 | assembler code features (especially Intel MMX code support) were
|
---|
2951 | added at libpng-1.2.0:
|
---|
2952 |
|
---|
2953 | PNG_ASM_FLAG_MMX_SUPPORT_COMPILED
|
---|
2954 | PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
|
---|
2955 | PNG_ASM_FLAG_MMX_READ_COMBINE_ROW
|
---|
2956 | PNG_ASM_FLAG_MMX_READ_INTERLACE
|
---|
2957 | PNG_ASM_FLAG_MMX_READ_FILTER_SUB
|
---|
2958 | PNG_ASM_FLAG_MMX_READ_FILTER_UP
|
---|
2959 | PNG_ASM_FLAG_MMX_READ_FILTER_AVG
|
---|
2960 | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH
|
---|
2961 | PNG_ASM_FLAGS_INITIALIZED
|
---|
2962 | PNG_MMX_READ_FLAGS
|
---|
2963 | PNG_MMX_FLAGS
|
---|
2964 | PNG_MMX_WRITE_FLAGS
|
---|
2965 | PNG_MMX_FLAGS
|
---|
2966 |
|
---|
2967 | We added the following functions in support of runtime
|
---|
2968 | selection of assembler code features:
|
---|
2969 |
|
---|
2970 | png_get_mmx_flagmask()
|
---|
2971 | png_set_mmx_thresholds()
|
---|
2972 | png_get_asm_flags()
|
---|
2973 | png_get_mmx_bitdepth_threshold()
|
---|
2974 | png_get_mmx_rowbytes_threshold()
|
---|
2975 | png_set_asm_flags()
|
---|
2976 |
|
---|
2977 | We replaced all of these functions with simple stubs in libpng-1.2.20,
|
---|
2978 | when the Intel assembler code was removed due to a licensing issue.
|
---|
2979 |
|
---|
2980 | These macros are deprecated:
|
---|
2981 |
|
---|
2982 | PNG_READ_TRANSFORMS_NOT_SUPPORTED
|
---|
2983 | PNG_PROGRESSIVE_READ_NOT_SUPPORTED
|
---|
2984 | PNG_NO_SEQUENTIAL_READ_SUPPORTED
|
---|
2985 | PNG_WRITE_TRANSFORMS_NOT_SUPPORTED
|
---|
2986 | PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED
|
---|
2987 | PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED
|
---|
2988 |
|
---|
2989 | They have been replaced, respectively, by:
|
---|
2990 |
|
---|
2991 | PNG_NO_READ_TRANSFORMS
|
---|
2992 | PNG_NO_PROGRESSIVE_READ
|
---|
2993 | PNG_NO_SEQUENTIAL_READ
|
---|
2994 | PNG_NO_WRITE_TRANSFORMS
|
---|
2995 | PNG_NO_READ_ANCILLARY_CHUNKS
|
---|
2996 | PNG_NO_WRITE_ANCILLARY_CHUNKS
|
---|
2997 |
|
---|
2998 | PNG_MAX_UINT was replaced with PNG_UINT_31_MAX. It has been
|
---|
2999 | deprecated since libpng-1.0.16 and libpng-1.2.6.
|
---|
3000 |
|
---|
3001 | The function
|
---|
3002 | png_check_sig(sig, num)
|
---|
3003 | was replaced with
|
---|
3004 | !png_sig_cmp(sig, 0, num)
|
---|
3005 | It has been deprecated since libpng-0.90.
|
---|
3006 |
|
---|
3007 | The function
|
---|
3008 | png_set_gray_1_2_4_to_8()
|
---|
3009 | which also expands tRNS to alpha was replaced with
|
---|
3010 | png_set_expand_gray_1_2_4_to_8()
|
---|
3011 | which does not. It has been deprecated since libpng-1.0.18 and 1.2.9.
|
---|
3012 |
|
---|
3013 | IX. (Omitted)
|
---|
3014 |
|
---|
3015 |
|
---|
3016 | Starting with libpng-1.2.54, attempting to set an over-length
|
---|
3017 | PLTE chunk is an error. Previously this requirement of the PNG specification
|
---|
3018 | was not enforced, and the palette was always limited to 256 entries.
|
---|
3019 | An over-length PLTE chunk found in an input PNG is silently truncated.
|
---|
3020 |
|
---|
3021 | X. Detecting libpng
|
---|
3022 |
|
---|
3023 | The png_get_io_ptr() function has been present since libpng-0.88, has never
|
---|
3024 | changed, and is unaffected by conditional compilation macros. It is the
|
---|
3025 | best choice for use in configure scripts for detecting the presence of any
|
---|
3026 | libpng version since 0.88. In an autoconf "configure.in" you could use
|
---|
3027 |
|
---|
3028 | AC_CHECK_LIB(png, png_get_io_ptr, ...
|
---|
3029 |
|
---|
3030 | XI. Source code repository
|
---|
3031 |
|
---|
3032 | Since about February 2009, version 1.2.34, libpng has been under "git" source
|
---|
3033 | control. The git repository was built from old libpng-x.y.z.tar.gz files
|
---|
3034 | going back to version 0.70. You can access the git repository (read only)
|
---|
3035 | at
|
---|
3036 |
|
---|
3037 | git://git.code.sf.net/p/libpng/code
|
---|
3038 |
|
---|
3039 | or you can browse it with a web browser by selecting the "code" button at
|
---|
3040 |
|
---|
3041 | https://sourceforge.net/projects/libpng/
|
---|
3042 |
|
---|
3043 | Patches can be sent to glennrp at users.sourceforge.net or to
|
---|
3044 | png-mng-implement at lists.sourceforge.net or you can upload them to
|
---|
3045 | the libpng bug tracker at
|
---|
3046 |
|
---|
3047 | http://libpng.sourceforge.net
|
---|
3048 |
|
---|
3049 | XII. Coding style
|
---|
3050 |
|
---|
3051 | Our coding style is similar to the "Allman" style
|
---|
3052 | (See http://en.wikipedia.org/wiki/Indent_style#Allman_style), with curly
|
---|
3053 | braces on separate lines:
|
---|
3054 |
|
---|
3055 | if (condition)
|
---|
3056 | {
|
---|
3057 | action;
|
---|
3058 | }
|
---|
3059 |
|
---|
3060 | else if (another condition)
|
---|
3061 | {
|
---|
3062 | another action;
|
---|
3063 | }
|
---|
3064 |
|
---|
3065 | The braces can be omitted from simple one-line actions:
|
---|
3066 |
|
---|
3067 | if (condition)
|
---|
3068 | return (0);
|
---|
3069 |
|
---|
3070 | We use 3-space indentation, except for continued statements which
|
---|
3071 | are usually indented the same as the first line of the statement
|
---|
3072 | plus four more spaces.
|
---|
3073 |
|
---|
3074 | For macro definitions we use 2-space indentation, always leaving the "#"
|
---|
3075 | in the first column.
|
---|
3076 |
|
---|
3077 | #ifndef PNG_NO_FEATURE
|
---|
3078 | # ifndef PNG_FEATURE_SUPPORTED
|
---|
3079 | # define PNG_FEATURE_SUPPORTED
|
---|
3080 | # endif
|
---|
3081 | #endif
|
---|
3082 |
|
---|
3083 | Comments appear with the leading "/*" at the same indentation as
|
---|
3084 | the statement that follows the comment:
|
---|
3085 |
|
---|
3086 | /* Single-line comment */
|
---|
3087 | statement;
|
---|
3088 |
|
---|
3089 | /* Multiple-line
|
---|
3090 | * comment
|
---|
3091 | */
|
---|
3092 | statement;
|
---|
3093 |
|
---|
3094 | Very short comments can be placed at the end of the statement
|
---|
3095 | to which they pertain:
|
---|
3096 |
|
---|
3097 | statement; /* comment */
|
---|
3098 |
|
---|
3099 | We don't use C++ style ("//") comments. We have, however,
|
---|
3100 | used them in the past in some now-abandoned MMX assembler
|
---|
3101 | code.
|
---|
3102 |
|
---|
3103 | Functions and their curly braces are not indented, and
|
---|
3104 | exported functions are marked with PNGAPI:
|
---|
3105 |
|
---|
3106 | /* This is a public function that is visible to
|
---|
3107 | * application programers. It does thus-and-so.
|
---|
3108 | */
|
---|
3109 | void PNGAPI
|
---|
3110 | png_exported_function(png_ptr, png_info, foo)
|
---|
3111 | {
|
---|
3112 | body;
|
---|
3113 | }
|
---|
3114 |
|
---|
3115 | The prototypes for all exported functions appear in png.h,
|
---|
3116 | above the comment that says
|
---|
3117 |
|
---|
3118 | /* Maintainer: Put new public prototypes here ... */
|
---|
3119 |
|
---|
3120 | We mark all non-exported functions with "/* PRIVATE */"":
|
---|
3121 |
|
---|
3122 | void /* PRIVATE */
|
---|
3123 | png_non_exported_function(png_ptr, png_info, foo)
|
---|
3124 | {
|
---|
3125 | body;
|
---|
3126 | }
|
---|
3127 |
|
---|
3128 | The prototypes for non-exported functions (except for those in
|
---|
3129 | pngtest) appear in
|
---|
3130 | the PNG_INTERNAL section of png.h
|
---|
3131 | above the comment that says
|
---|
3132 |
|
---|
3133 | /* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */
|
---|
3134 |
|
---|
3135 | The names of all exported functions and variables begin
|
---|
3136 | with "png_", and all publicly visible C preprocessor
|
---|
3137 | macros begin with "PNG".
|
---|
3138 |
|
---|
3139 | We put a space after each comma and after each semicolon
|
---|
3140 | in "for" statments, and we put spaces before and after each
|
---|
3141 | C binary operator and after "for" or "while". We don't
|
---|
3142 | put a space between a typecast and the expression being
|
---|
3143 | cast, nor do we put one between a function name and the
|
---|
3144 | left parenthesis that follows it:
|
---|
3145 |
|
---|
3146 | for (i = 2; i > 0; --i)
|
---|
3147 | y[i] = a(x) + (int)b;
|
---|
3148 |
|
---|
3149 | We prefer #ifdef and #ifndef to #if defined() and if !defined()
|
---|
3150 | when there is only one macro being tested.
|
---|
3151 |
|
---|
3152 | We do not use the TAB character for indentation in the C sources.
|
---|
3153 |
|
---|
3154 | Lines do not exceed 80 characters.
|
---|
3155 |
|
---|
3156 | Other rules can be inferred by inspecting the libpng source.
|
---|
3157 |
|
---|
3158 | XIII. Y2K Compliance in libpng
|
---|
3159 |
|
---|
3160 | Since the PNG Development group is an ad-hoc body, we can't make
|
---|
3161 | an official declaration.
|
---|
3162 |
|
---|
3163 | This is your unofficial assurance that libpng from version 0.71 and
|
---|
3164 | upward through 1.2.54 are Y2K compliant. It is my belief that earlier
|
---|
3165 | versions were also Y2K compliant.
|
---|
3166 |
|
---|
3167 | Libpng only has three year fields. One is a 2-byte unsigned integer that
|
---|
3168 | will hold years up to 65535. The other two hold the date in text
|
---|
3169 | format, and will hold years up to 9999.
|
---|
3170 |
|
---|
3171 | The integer is
|
---|
3172 | "png_uint_16 year" in png_time_struct.
|
---|
3173 |
|
---|
3174 | The strings are
|
---|
3175 | "png_charp time_buffer" in png_struct and
|
---|
3176 | "near_time_buffer", which is a local character string in png.c.
|
---|
3177 |
|
---|
3178 | There are seven time-related functions:
|
---|
3179 |
|
---|
3180 | png_convert_to_rfc_1123() in png.c
|
---|
3181 | (formerly png_convert_to_rfc_1152() in error)
|
---|
3182 | png_convert_from_struct_tm() in pngwrite.c, called
|
---|
3183 | in pngwrite.c
|
---|
3184 | png_convert_from_time_t() in pngwrite.c
|
---|
3185 | png_get_tIME() in pngget.c
|
---|
3186 | png_handle_tIME() in pngrutil.c, called in pngread.c
|
---|
3187 | png_set_tIME() in pngset.c
|
---|
3188 | png_write_tIME() in pngwutil.c, called in pngwrite.c
|
---|
3189 |
|
---|
3190 | All appear to handle dates properly in a Y2K environment. The
|
---|
3191 | png_convert_from_time_t() function calls gmtime() to convert from system
|
---|
3192 | clock time, which returns (year - 1900), which we properly convert to
|
---|
3193 | the full 4-digit year. There is a possibility that applications using
|
---|
3194 | libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
|
---|
3195 | function, or that they are incorrectly passing only a 2-digit year
|
---|
3196 | instead of "year - 1900" into the png_convert_from_struct_tm() function,
|
---|
3197 | but this is not under our control. The libpng documentation has always
|
---|
3198 | stated that it works with 4-digit years, and the APIs have been
|
---|
3199 | documented as such.
|
---|
3200 |
|
---|
3201 | The tIME chunk itself is also Y2K compliant. It uses a 2-byte unsigned
|
---|
3202 | integer to hold the year, and can hold years as large as 65535.
|
---|
3203 |
|
---|
3204 | zlib, upon which libpng depends, is also Y2K compliant. It contains
|
---|
3205 | no date-related code.
|
---|
3206 |
|
---|
3207 |
|
---|
3208 | Glenn Randers-Pehrson
|
---|
3209 | libpng maintainer
|
---|
3210 | PNG Development Group
|
---|