blob: a68f33556dca0f8822e2e50b63014f511650bd2f [file] [log] [blame]
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001libpng.txt - A description on how to use and modify libpng
Guy Schalnat0d580581995-07-20 02:43:20 -05002
Glenn Randers-Pehrsonf8b008c1999-09-18 10:54:36 -05003 libpng version 1.0.4 - September 18, 1999
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06004 Updated and distributed by Glenn Randers-Pehrson
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05005 <randeg@alum.rpi.edu>
Glenn Randers-Pehrson87544ac1999-01-13 22:06:39 -06006 Copyright (c) 1998, 1999 Glenn Randers-Pehrson
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06007 For conditions of distribution and use, see copyright
8 notice in png.h.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060010 based on:
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060011
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060012 libpng 1.0 beta 6 version 0.96 May 28, 1997
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060013 Updated and distributed by Andreas Dilger
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060014 Copyright (c) 1996, 1997 Andreas Dilger
Guy Schalnate5a37791996-06-05 15:50:50 -050015
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060016 libpng 1.0 beta 2 - version 0.88 January 26, 1996
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060017 For conditions of distribution and use, see copyright
18 notice in png.h. Copyright (c) 1995, 1996 Guy Eric
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060019 Schalnat, Group 42, Inc.
Andreas Dilger47a0c421997-05-16 02:46:07 -050020
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060021 Updated/rewritten per request in the libpng FAQ
22 Copyright (c) 1995 Frank J. T. Wojcik
23 December 18, 1995 && January 20, 1996
Guy Schalnat69b14481996-01-10 02:56:49 -060024
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050025 *
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050026I. Introduction
Guy Schalnat0d580581995-07-20 02:43:20 -050027
28This file describes how to use and modify the PNG reference library
Guy Schalnat69b14481996-01-10 02:56:49 -060029(known as libpng) for your own use. There are five sections to this
30file: introduction, structures, reading, writing, and modification and
Guy Schalnat4ee97b01996-01-16 01:51:56 -060031configuration notes for various special platforms. In addition to this
Guy Schalnat69b14481996-01-10 02:56:49 -060032file, example.c is a good starting point for using the library, as
33it is heavily commented and should include everything most people
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060034will need. We assume that libpng is already installed; see the
35INSTALL file for instructions on how to install libpng.
Guy Schalnat0d580581995-07-20 02:43:20 -050036
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060037Libpng was written as a companion to the PNG specification, as a way
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060038of reducing the amount of time and effort it takes to support the PNG
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050039file format in application programs.
40
41The PNG-1.2 specification is available at <http://www.cdrom.com/png>.
42
43The PNG-1.0 specification is available
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060044as RFC 2083 <ftp://ftp.uu.net/graphics/png/documents/> and as a
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060045W3C Recommendation <http://www.w3.org/TR/REC.png.html>. Some
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060046additional chunks are described in the special-purpose public chunks
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050047documents at <ftp://ftp.uu.net/graphics/png/documents/>.
48
49Other information
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060050about PNG, and the latest version of libpng, can be found at the PNG home
51page, <http://www.cdrom.com/pub/png/>.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060052
53Most users will not have to modify the library significantly; advanced
54users may want to modify it more. All attempts were made to make it as
55complete as possible, while keeping the code easy to understand.
56Currently, this library only supports C. Support for other languages
57is being considered.
Guy Schalnat0d580581995-07-20 02:43:20 -050058
59Libpng has been designed to handle multiple sessions at one time,
60to be easily modifiable, to be portable to the vast majority of
Andreas Dilger47a0c421997-05-16 02:46:07 -050061machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy
62to use. The ultimate goal of libpng is to promote the acceptance of
Guy Schalnat0d580581995-07-20 02:43:20 -050063the PNG file format in whatever way possible. While there is still
Andreas Dilger47a0c421997-05-16 02:46:07 -050064work to be done (see the TODO file), libpng should cover the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060065majority of the needs of its users.
Guy Schalnat0d580581995-07-20 02:43:20 -050066
67Libpng uses zlib for its compression and decompression of PNG files.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060068Further information about zlib, and the latest version of zlib, can
69be found at the zlib home page, <http://www.cdrom.com/pub/infozip/zlib/>.
Guy Schalnat0d580581995-07-20 02:43:20 -050070The zlib compression utility is a general purpose utility that is
Guy Schalnat4ee97b01996-01-16 01:51:56 -060071useful for more than PNG files, and can be used without libpng.
Guy Schalnat69b14481996-01-10 02:56:49 -060072See the documentation delivered with zlib for more details.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060073You can usually find the source files for the zlib utility wherever you
74find the libpng source files.
Guy Schalnat0d580581995-07-20 02:43:20 -050075
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060076Libpng is thread safe, provided the threads are using different
Guy Schalnat4ee97b01996-01-16 01:51:56 -060077instances of the structures. Each thread should have its own
78png_struct and png_info instances, and thus its own image.
79Libpng does not protect itself against two threads using the
80same instance of a structure.
81
Guy Schalnat0d580581995-07-20 02:43:20 -050082
Guy Schalnat69b14481996-01-10 02:56:49 -060083II. Structures
Guy Schalnat0d580581995-07-20 02:43:20 -050084
85There are two main structures that are important to libpng, png_struct
86and png_info. The first, png_struct, is an internal structure that
Guy Schalnate5a37791996-06-05 15:50:50 -050087will not, for the most part, be used by a user except as the first
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060088variable passed to every libpng function call.
Guy Schalnat0d580581995-07-20 02:43:20 -050089
90The png_info structure is designed to provide information about the
Andreas Dilger47a0c421997-05-16 02:46:07 -050091PNG file. At one time, the fields of png_info were intended to be
92directly accessible to the user. However, this tended to cause problems
93with applications using dynamically loaded libraries, and as a result
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060094a set of interface functions for png_info was developed. The fields
Andreas Dilger47a0c421997-05-16 02:46:07 -050095of png_info are still available for older applications, but it is
96suggested that applications use the new interfaces if at all possible.
Guy Schalnat0d580581995-07-20 02:43:20 -050097
Andreas Dilger47a0c421997-05-16 02:46:07 -050098The png.h header file is an invaluable reference for programming with libpng.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060099And while I'm on the topic, make sure you include the libpng header file:
Guy Schalnat0d580581995-07-20 02:43:20 -0500100
101#include <png.h>
102
Guy Schalnat69b14481996-01-10 02:56:49 -0600103III. Reading
104
Guy Schalnat0d580581995-07-20 02:43:20 -0500105Reading PNG files:
106
Guy Schalnat69b14481996-01-10 02:56:49 -0600107We'll now walk you through the possible functions to call when reading
108in a PNG file, briefly explaining the syntax and purpose of each one.
109See example.c and png.h for more detail. While Progressive reading
110is covered in the next section, you will still need some of the
111functions discussed in this section to read a PNG file.
Guy Schalnat6d764711995-12-19 03:22:19 -0600112
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600113You will want to do the I/O initialization(*) before you get into libpng,
114so if it doesn't work, you don't have much to undo. Of course, you
115will also want to insure that you are, in fact, dealing with a PNG
116file. Libpng provides a simple check to see if a file is a PNG file.
117To use it, pass in the first 1 to 8 bytes of the file, and it will
118return true or false (1 or 0) depending on whether the bytes could be
119part of a PNG file. Of course, the more bytes you pass in, the
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600120greater the accuracy of the prediction.
121
122If you are intending to keep the file pointer open for use in libpng,
123you must ensure you don't read more than 8 bytes from the beginning
124of the file, and you also have to make a call to png_set_sig_bytes_read()
125with the number of bytes you read from the beginning. Libpng will
126then only check the bytes (if any) that your program didn't read.
Guy Schalnat0d580581995-07-20 02:43:20 -0500127
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600128(*): If you are not using the standard I/O functions, you will need
Guy Schalnat69b14481996-01-10 02:56:49 -0600129to replace them with custom functions. See the discussion under
130Customizing libpng.
131
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600132
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600133 FILE *fp = fopen(file_name, "rb");
134 if (!fp)
135 {
136 return;
137 }
138 fread(header, 1, number, fp);
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -0600139 is_png = !png_sig_cmp(header, 0, number);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600140 if (!is_png)
141 {
142 return;
143 }
144
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600145
Andreas Dilger47a0c421997-05-16 02:46:07 -0500146Next, png_struct and png_info need to be allocated and initialized. In
147order to ensure that the size of these structures is correct even with a
148dynamically linked libpng, there are functions to initialize and
149allocate the structures. We also pass the library version, optional
150pointers to error handling functions, and a pointer to a data struct for
151use by the error functions, if necessary (the pointer and functions can
152be NULL if the default error handlers are to be used). See the section
153on Changes to Libpng below regarding the old initialization functions.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600155 png_structp png_ptr = png_create_read_struct
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500156 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600157 user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600158 if (!png_ptr)
159 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500160
Guy Schalnate5a37791996-06-05 15:50:50 -0500161 png_infop info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600162 if (!info_ptr)
163 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600164 png_destroy_read_struct(&png_ptr,
165 (png_infopp)NULL, (png_infopp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500166 return;
167 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168
Guy Schalnate5a37791996-06-05 15:50:50 -0500169 png_infop end_info = png_create_info_struct(png_ptr);
170 if (!end_info)
171 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600172 png_destroy_read_struct(&png_ptr, &info_ptr,
173 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600174 return;
175 }
176
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500177If you want to use your own memory allocation routines,
178define PNG_USER_MEM_SUPPORTED and use
179png_create_read_struct_2() instead of png_create_read_struct():
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600180
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500181 png_structp png_ptr = png_create_read_struct_2
182 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
183 user_error_fn, user_warning_fn, (png_voidp)
184 user_mem_ptr, user_malloc_fn, user_free_fn);
185
186The error handling routines passed to png_create_read_struct()
187and the memory alloc/free routines passed to png_create_struct_2()
188are only necessary if you are not using the libpng supplied error
189handling and memory alloc/free functions.
190
191When libpng encounters an error, it expects to longjmp back
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600192to your routine. Therefore, you will need to call setjmp and pass
193your png_ptr->jmpbuf. If you read the file from different
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600194routines, you will need to update the jmpbuf field every time you enter
195a new routine that will call a png_ function.
196
197See your documentation of setjmp/longjmp for your compiler for more
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198handling in the Customizing Libpng section below for more information on
199the libpng error handling. If an error occurs, and libpng longjmp's
200back to your setjmp, you will want to call png_destroy_read_struct() to
201free any memory.
Guy Schalnat0d580581995-07-20 02:43:20 -0500202
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600203 if (setjmp(png_ptr->jmpbuf))
204 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600205 png_destroy_read_struct(&png_ptr, &info_ptr,
206 &end_info);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600207 fclose(fp);
208 return;
209 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500210
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600211Now you need to set up the input code. The default for libpng is to
212use the C function fread(). If you use this, you will need to pass a
213valid FILE * in the function png_init_io(). Be sure that the file is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214opened in binary mode. If you wish to handle reading data in another
215way, you need not call the png_init_io() function, but you must then
216implement the libpng I/O methods discussed in the Customizing Libpng
217section below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500218
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500220
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600221If you had previously opened the file and read any of the signature from
222the beginning in order to see if this was a PNG file, you need to let
223libpng know that there are some bytes missing from the start of the file.
224
225 png_set_sig_bytes(png_ptr, number);
226
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600227At this point, you can set up a callback function that will be
228called after each row has been read, which you can use to control
229a progress meter or the like. It's demonstrated in pngtest.c.
230You must supply a function
231
232 void read_row_callback(png_ptr, png_uint_32 row, int pass);
233 {
234 /* put your code here */
235 }
236
237(You can give it another name that you like instead of "read_row_callback")
238
239To inform libpng about your function, use
240
241 png_set_read_status_fn(png_ptr, read_row_callback);
242
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600243In PNG files, the alpha channel in an image is the level of opacity.
244If you need the alpha channel in an image to be the level of transparency
245instead of opacity, you can invert the alpha channel (or the tRNS chunk
246data) after it's read, so that 0 is fully opaque and 255 (in 8-bit or
247paletted images) or 65535 (in 16-bit images) is fully transparent, with
248
249 png_set_invert_alpha(png_ptr);
250
251This has to appear here rather than later with the other transformations
252because the tRNS chunk data must be modified in the case of paletted images.
253If your image is not a paletted image, the tRNS data (which in such cases
254represents a single color to be rendered as transparent) won't be changed.
255
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600256Finally, you can write your own transformation function if none of
257the existing ones meets your needs. This is done by setting a callback
258with
259
260 png_set_read_user_transform_fn(png_ptr,
261 read_transform_fn);
262
263You must supply the function
264
265 void read_transform_fn(png_ptr ptr, row_info_ptr
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600266 row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600267
268See pngtest.c for a working example. Your function will be called
269after all of the other transformations have been processed.
270
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500271You can also set up a pointer to a user structure for use by your
272callback function, and you can inform libpng that your transform
273function will change the number of channels or bit depth with the
274function
275
276 png_set_user_transform_info(png_ptr, user_ptr,
277 user_depth, user_channels);
278
279
280You can retrieve the pointer via the function
281png_get_user_transform_ptr(). For example:
282
283 voidp read_user_transform_ptr =
284 png_get_user_transform_ptr(png_ptr);
285
Guy Schalnat0d580581995-07-20 02:43:20 -0500286You are now ready to read all the file information up to the actual
287image data. You do this with a call to png_read_info().
288
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600289 png_read_info(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500290
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291Functions are used to get the information from the info_ptr:
Guy Schalnat69b14481996-01-10 02:56:49 -0600292
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600293 png_get_IHDR(png_ptr, info_ptr, &width, &height,
294 &bit_depth, &color_type, &interlace_type,
295 &compression_type, &filter_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600297 width - holds the width of the image
298 in pixels (up to 2^31).
299 height - holds the height of the image
300 in pixels (up to 2^31).
301 bit_depth - holds the bit depth of one of the
302 image channels. (valid values are
303 1, 2, 4, 8, 16 and depend also on
304 the color_type. See also
305 significant bits (sBIT) below).
306 color_type - describes which color/alpha channels
307 are present.
308 PNG_COLOR_TYPE_GRAY
309 (bit depths 1, 2, 4, 8, 16)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600310 PNG_COLOR_TYPE_GRAY_ALPHA
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600311 (bit depths 8, 16)
312 PNG_COLOR_TYPE_PALETTE
313 (bit depths 1, 2, 4, 8)
314 PNG_COLOR_TYPE_RGB
315 (bit_depths 8, 16)
316 PNG_COLOR_TYPE_RGB_ALPHA
317 (bit_depths 8, 16)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318
319 PNG_COLOR_MASK_PALETTE
320 PNG_COLOR_MASK_COLOR
321 PNG_COLOR_MASK_ALPHA
322
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600323 filter_type - (must be PNG_FILTER_TYPE_BASE
324 for PNG 1.0)
325 compression_type - (must be PNG_COMPRESSION_TYPE_BASE
326 for PNG 1.0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600327 interlace_type - (PNG_INTERLACE_NONE or
328 PNG_INTERLACE_ADAM7)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600329 Any or all of interlace_type, compression_type, of
330 filter_type can be
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600331 NULL if you are not interested in their values.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332
333 channels = png_get_channels(png_ptr, info_ptr);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600334 channels - number of channels of info for the
335 color type (valid values are 1 (GRAY,
336 PALETTE), 2 (GRAY_ALPHA), 3 (RGB),
337 4 (RGB_ALPHA or RGB + filler byte))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600339 rowbytes - number of bytes needed to hold a row
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340
341 signature = png_get_signature(png_ptr, info_ptr);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600342 signature - holds the signature read from the
343 file (if any). The data is kept in
344 the same offset it would be if the
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600345 whole signature were read (i.e. if an
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600346 application had already read in 4
347 bytes of signature before starting
348 libpng, the remaining 4 bytes would
349 be in signature[4] through signature[7]
350 (see png_set_sig_bytes())).
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600351
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600352
353 width = png_get_image_width(png_ptr,
354 info_ptr);
355 height = png_get_image_height(png_ptr,
356 info_ptr);
357 bit_depth = png_get_bit_depth(png_ptr,
358 info_ptr);
359 color_type = png_get_color_type(png_ptr,
360 info_ptr);
361 filter_type = png_get_filter_type(png_ptr,
362 info_ptr);
363 compression_type = png_get_compression_type(png_ptr,
364 info_ptr);
365 interlace_type = png_get_interlace_type(png_ptr,
366 info_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600367
Guy Schalnat69b14481996-01-10 02:56:49 -0600368
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369These are also important, but their validity depends on whether the chunk
370has been read. The png_get_valid(png_ptr, info_ptr, PNG_INFO_<chunk>) and
371png_get_<chunk>(png_ptr, info_ptr, ...) functions return non-zero if the
372data has been read, or zero if it is missing. The parameters to the
373png_get_<chunk> are set directly if they are simple data types, or a pointer
374into the info_ptr is returned for any complex types.
Guy Schalnat69b14481996-01-10 02:56:49 -0600375
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600376 png_get_PLTE(png_ptr, info_ptr, &palette,
377 &num_palette);
378 palette - the palette for the file
379 (array of png_color)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600380 num_palette - number of entries in the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381
382 png_get_gAMA(png_ptr, info_ptr, &gamma);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600383 gamma - the gamma the file is written
384 at (PNG_INFO_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600386 png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
387 srgb_intent - the rendering intent (PNG_INFO_sRGB)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600388 The presence of the sRGB chunk
389 means that the pixel data is in the
390 sRGB color space. This chunk also
391 implies specific values of gAMA and
392 cHRM.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600393
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600395 sig_bit - the number of significant bits for
396 (PNG_INFO_sBIT) each of the gray,
397 red, green, and blue channels,
398 whichever are appropriate for the
399 given color type (png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600401 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
402 &trans_values);
403 trans - array of transparent entries for
404 palette (PNG_INFO_tRNS)
405 trans_values - transparent pixel for non-paletted
406 images (PNG_INFO_tRNS)
407 num_trans - number of transparent entries
408 (PNG_INFO_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600410 png_get_hIST(png_ptr, info_ptr, &hist);
411 (PNG_INFO_hIST)
412 hist - histogram of palette (array of
413 png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414
415 png_get_tIME(png_ptr, info_ptr, &mod_time);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600416 mod_time - time image was last modified
417 (PNG_VALID_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500418
419 png_get_bKGD(png_ptr, info_ptr, &background);
Guy Schalnate5a37791996-06-05 15:50:50 -0500420 background - background color (PNG_VALID_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421
422 num_text = png_get_text(png_ptr, info_ptr, &text_ptr);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600423 text_ptr - array of png_text holding image
424 comments
425 text_ptr[i]->key - keyword for comment.
426 text_ptr[i]->text - text comments for current
427 keyword.
428 text_ptr[i]->compression - type of compression used
429 on "text" PNG_TEXT_COMPRESSION_NONE
430 or PNG_TEXT_COMPRESSION_zTXt
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600431 num_text - number of comments
Guy Schalnat69b14481996-01-10 02:56:49 -0600432
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600433 png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y,
434 &unit_type);
435 offset_x - positive offset from the left edge
436 of the screen
437 offset_y - positive offset from the top edge
438 of the screen
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439 unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
440
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600441 png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y,
442 &unit_type);
443 res_x - pixels/unit physical resolution in
444 x direction
445 res_y - pixels/unit physical resolution in
446 x direction
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600447 unit_type - PNG_RESOLUTION_UNKNOWN,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600448 PNG_RESOLUTION_METER
Andreas Dilger47a0c421997-05-16 02:46:07 -0500449
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600450The data from the pHYs chunk can be retrieved in several convenient
451forms:
452
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600453 res_x = png_get_x_pixels_per_meter(png_ptr,
454 info_ptr)
455 res_y = png_get_y_pixels_per_meter(png_ptr,
456 info_ptr)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600457 res_x_and_y = png_get_pixels_per_meter(png_ptr,
458 info_ptr)
459 aspect_ratio = png_get_pixel_aspect_ratio(png_ptr,
460 info_ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600461
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600462 (Each of these returns 0 [signifying "unknown"] if
463 the data is not present or if res_x is 0;
464 res_x_and_y is 0 if res_x != res_y)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600465
Andreas Dilger47a0c421997-05-16 02:46:07 -0500466For more information, see the png_info definition in png.h and the
Guy Schalnat0d580581995-07-20 02:43:20 -0500467PNG specification for chunk contents. Be careful with trusting
468rowbytes, as some of the transformations could increase the space
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469needed to hold a row (expand, filler, gray_to_rgb, etc.).
470See png_read_update_info(), below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500471
Andreas Dilger47a0c421997-05-16 02:46:07 -0500472A quick word about text_ptr and num_text. PNG stores comments in
473keyword/text pairs, one pair per chunk, with no limit on the number
474of text chunks, and a 2^31 byte limit on their size. While there are
475suggested keywords, there is no requirement to restrict the use to these
476strings. It is strongly suggested that keywords and text be sensible
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600477to humans (that's the point), so don't use abbreviations. Non-printing
478symbols are not allowed. See the PNG specification for more details.
479There is also no requirement to have text after the keyword.
Guy Schalnat0d580581995-07-20 02:43:20 -0500480
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481Keywords should be limited to 79 Latin-1 characters without leading or
482trailing spaces, but non-consecutive spaces are allowed within the
483keyword. It is possible to have the same keyword any number of times.
484The text_ptr is an array of png_text structures, each holding pointer
485to a keyword and a pointer to a text string. Only the text string may
486be null. The keyword/text pairs are put into the array in the order
487that they are received. However, some or all of the text chunks may be
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600488after the image, so, to make sure you have read all the text chunks,
Guy Schalnat0d580581995-07-20 02:43:20 -0500489don't mess with these until after you read the stuff after the image.
490This will be mentioned again below in the discussion that goes with
491png_read_end().
492
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493After you've read the header information, you can set up the library
494to handle any special transformations of the image data. The various
Guy Schalnat0d580581995-07-20 02:43:20 -0500495ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600496should occur. This is important, as some of these change the color
497type and/or bit depth of the data, and some others only work on
498certain color types and bit depths. Even though each transformation
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600499checks to see if it has data that it can do something with, you should
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600500make sure to only enable a transformation if it will be valid for the
501data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500502
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600503The colors used for the background and transparency values should be
504supplied in the same format/depth as the current image data. They
505are stored in the same format/depth as the image data in a bKGD or tRNS
506chunk, so this is what libpng expects for this data. The colors are
507transformed to keep in sync with the image data when an application
Andreas Dilger47a0c421997-05-16 02:46:07 -0500508calls the png_read_update_info() routine (see below).
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600509
Guy Schalnate5a37791996-06-05 15:50:50 -0500510Data will be decoded into the supplied row buffers packed into bytes
511unless the library has been told to transform it into another format.
512For example, 4 bit/pixel paletted or grayscale data will be returned
5132 pixels/byte with the leftmost pixel in the high-order bits of the
514byte, unless png_set_packing() is called. 8-bit RGB data will be stored
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500515in RGB RGB RGB format unless png_set_filler() is called to insert filler
Guy Schalnate5a37791996-06-05 15:50:50 -0500516bytes, either before or after each RGB triplet. 16-bit RGB data will
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500517be returned RRGGBB RRGGBB, with the most significant byte of the color
Guy Schalnate5a37791996-06-05 15:50:50 -0500518value first, unless png_set_strip_16() is called to transform it to
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500519regular RGB RGB triplets, or png_set_filler() is called to insert
520filler bytes, either before or after each RRGGBB triplet. Similarly,
5218-bit or 16-bit grayscale data can be modified with png_set_filler()
522or png_set_strip_16().
Guy Schalnate5a37791996-06-05 15:50:50 -0500523
524The following code transforms grayscale images of less than 8 to 8 bits,
525changes paletted images to RGB, and adds a full alpha channel if there is
526transparency information in a tRNS chunk. This is most useful on
527grayscale images with bit depths of 2 or 4 or if there is a multiple-image
528viewing application that wishes to treat all images in the same way.
Guy Schalnat0d580581995-07-20 02:43:20 -0500529
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600530 if (color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500531 bit_depth <= 8) png_set_palette_to_rgb(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500532
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600533 if (color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500534 bit_depth < 8) png_set_gray_1_2_4_to_8(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500535
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600536 if (png_get_valid(png_ptr, info_ptr,
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500537 PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
538
539These three functions are actually aliases for png_set_expand(), added
540in libpng version 1.0.4, with the function names expanded to improve code
541readability. In some future version they may actually do different
542things.
Guy Schalnat0d580581995-07-20 02:43:20 -0500543
Guy Schalnate5a37791996-06-05 15:50:50 -0500544PNG can have files with 16 bits per channel. If you only can handle
5458 bits per channel, this will strip the pixels down to 8 bit.
546
Andreas Dilger47a0c421997-05-16 02:46:07 -0500547 if (bit_depth == 16)
548 png_set_strip_16(png_ptr);
549
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600550The png_set_background() function tells libpng to composite images
551with alpha or simple transparency against the supplied background
552color. If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
553you may use this color, or supply another color more suitable for
554the current display (e.g., the background color from a web page). You
555need to tell libpng whether the color is in the gamma space of the
556display (PNG_BACKGROUND_GAMMA_SCREEN for colors you supply), the file
557(PNG_BACKGROUND_GAMMA_FILE for colors from the bKGD chunk), or one
558that is neither of these gammas (PNG_BACKGROUND_GAMMA_UNIQUE - I don't
559know why anyone would use this, but it's here).
560
Andreas Dilger47a0c421997-05-16 02:46:07 -0500561If, for some reason, you don't need the alpha channel on an image,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600562and you want to remove it rather than combining it with the background
563(but the image author certainly had in mind that you *would* combine
564it with the background, so that's what you should probably do):
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565
566 if (color_type & PNG_COLOR_MASK_ALPHA)
567 png_set_strip_alpha(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500568
569PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
570they can, resulting in, for example, 8 pixels per byte for 1 bit
571files. This code expands to 1 pixel per byte without changing the
572values of the pixels:
573
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 if (bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500575 png_set_packing(png_ptr);
576
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixels
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600578stored in a PNG image have been "scaled" or "shifted" up to the next
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600579higher possible bit depth (e.g. from 5 bits/sample in the range [0,31] to
Andreas Dilger47a0c421997-05-16 02:46:07 -05005808 bits/sample in the range [0, 255]). However, it is also possible to
581convert the PNG pixel data back to the original bit depth of the image.
582This call reduces the pixels back down to the original bit depth:
Guy Schalnate5a37791996-06-05 15:50:50 -0500583
Andreas Dilger47a0c421997-05-16 02:46:07 -0500584 png_color_16p sig_bit;
Guy Schalnate5a37791996-06-05 15:50:50 -0500585
Andreas Dilger47a0c421997-05-16 02:46:07 -0500586 if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
587 png_set_shift(png_ptr, sig_bit);
588
589PNG files store 3-color pixels in red, green, blue order. This code
Guy Schalnate5a37791996-06-05 15:50:50 -0500590changes the storage of the pixels to blue, green, red:
591
Andreas Dilger47a0c421997-05-16 02:46:07 -0500592 if (color_type == PNG_COLOR_TYPE_RGB ||
593 color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnate5a37791996-06-05 15:50:50 -0500594 png_set_bgr(png_ptr);
595
596PNG files store RGB pixels packed into 3 bytes. This code expands them
597into 4 bytes for windowing systems that need them in this format:
598
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600599 if (bit_depth == 8 && color_type ==
600 PNG_COLOR_TYPE_RGB) png_set_filler(png_ptr,
601 filler, PNG_FILLER_BEFORE);
Guy Schalnate5a37791996-06-05 15:50:50 -0500602
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500603where "filler" is the 8 or 16-bit number to fill with, and the location is
Guy Schalnate5a37791996-06-05 15:50:50 -0500604either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605you want the filler before the RGB or after. This transformation
606does not affect images that already have full alpha channels.
Guy Schalnate5a37791996-06-05 15:50:50 -0500607
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608If you are reading an image with an alpha channel, and you need the
609data as ARGB instead of the normal PNG format RGBA:
610
611 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
612 png_set_swap_alpha(png_ptr);
613
614For some uses, you may want a grayscale image to be represented as
Guy Schalnate5a37791996-06-05 15:50:50 -0500615RGB. This code will do that conversion:
616
Andreas Dilger47a0c421997-05-16 02:46:07 -0500617 if (color_type == PNG_COLOR_TYPE_GRAY ||
618 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
619 png_set_gray_to_rgb(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500620
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600621Conversely, you can convert an RGB or RGBA image to grayscale or grayscale
622with alpha. This is intended for conversion of images that really are
623gray (red == green == blue), so the function simply strips out the red
624and blue channels, leaving the green channel in the gray position.
625
626 if (color_type == PNG_COLOR_TYPE_RGB ||
627 color_type == PNG_COLOR_TYPE_RGB_ALPHA)
628 png_set_rgb_to_gray(png_ptr, error_action,
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500629 double red_weight, double green_weight);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600630
631 error_action = 1: silently do the conversion
632 error_action = 2: issue a warning if the original
633 image has any pixel where
634 red != green or red != blue
635 error_action = 3: issue an error and abort the
636 conversion if the original
637 image has any pixel where
638 red != green or red != blue
639
640 red_weight: weight of red component
641 (NULL -> default 54/256)
642 green_weight: weight of green component
643 (NULL -> default 183/256)
644
645If you have set error_action = 1 or 2, you can
646later check whether the image really was gray, after processing
647the image rows, with the png_get_rgb_to_gray_status(png_ptr) function.
648It will return a png_byte that is zero if the image was gray or
6491 if there were any non-gray pixels. bKGD and sBIT data
650will be silently converted to grayscale, using the green channel
651data, regardless of the error_action setting.
652
653With 0.0<=red_weight+green_weight<=1.0,
654the normalized graylevel is computed:
655
656 int rw = red_weight * 256;
657 int gw = green_weight * 256;
658 int bw = 256 - (rw + gw);
659 gray = (rw*red + gw*green + bw*blue)/256;
660
661The default values approximate those recommended in the Charles
662Poynton's Color FAQ, <http://www.inforamp.net/~poynton/>
663Copyright (c) 1998-01-04 Charles Poynton poynton@inforamp.net
664
665 Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
666
667Libpng approximates this with
668
669 Y = 0.211 * R + 0.715 * G + 0.074 * B
670
671which can be expressed with integers as
672
673 Y = (54 * R + 183 * G + 19 * B)/256
674
675The calculation is done in a linear colorspace, if the image gamma
676is known.
677
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500678If you have a grayscale and you are using png_set_expand_depth() or
679png_set_expand() to change to
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500680a higher bit-depth, you must either supply the background color as a gray
681value at the original file bit-depth (need_expand = 1) or else supply the
682background color as an RGB triplet at the final, expanded bit depth
683(need_expand = 0). Similarly, if you are reading a paletted image, you
684must either supply the background color as a palette index (need_expand = 1)
685or as an RGB triplet that may or may not be in the palette (need_expand = 0).
Guy Schalnat0d580581995-07-20 02:43:20 -0500686
Andreas Dilger47a0c421997-05-16 02:46:07 -0500687 png_color_16 my_background;
688 png_color_16p image_background;
Guy Schalnat0d580581995-07-20 02:43:20 -0500689
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500690 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
691 png_set_background(png_ptr, image_background,
692 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500693 else
694 png_set_background(png_ptr, &my_background,
695 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
696
697To properly display PNG images on any kind of system, the application needs
698to know what the display gamma is. Ideally, the user will know this, and
699the application will allow them to set it. One method of allowing the user
700to set the display gamma separately for each system is to check for the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600701DISPLAY_GAMMA and VIEWING_GAMMA environment variables or for a SCREEN_GAMMA
702environment variable, which will hopefully be correctly set.
703
704Note that display_gamma is the gamma of your display, while screen_gamma is
705the overall gamma correction required to produce pleasing results,
706which depends on the lighting conditions in the surrounding environment.
707Screen_gamma is display_gamma/viewing_gamma, where viewing_gamma is
708the amount of additional gamma correction needed to compensate for
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600709a (viewing_gamma=1.25) environment. In a dim or brightly lit room, no
710compensation other than the display_gamma is needed (viewing_gamma=1.0).
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500712 double gamma, screen_gamma;
713
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600714 if (/* We have a user-defined screen
715 gamma value */)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 {
717 screen_gamma = user_defined_screen_gamma;
718 }
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600719 /* One way that applications can share the same
720 screen gamma value */
721 else if ((gamma_str = getenv("SCREEN_GAMMA"))
722 != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500724 screen_gamma = (double)atof(gamma_str);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500725 }
726 /* If we don't have another value */
Guy Schalnat0d580581995-07-20 02:43:20 -0500727 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500728 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600729 screen_gamma = 2.2; /* A good guess for a
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600730 PC monitor in a bright office or a dim room */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600731 screen_gamma = 2.0; /* A good guess for a
732 PC monitor in a dark room */
733 screen_gamma = 1.7 or 1.0; /* A good
734 guess for Mac systems */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500735 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500736
Andreas Dilger47a0c421997-05-16 02:46:07 -0500737The png_set_gamma() function handles gamma transformations of the data.
738Pass both the file gamma and the current screen_gamma. If the file does
739not have a gamma value, you can pass one anyway if you have an idea what
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600740it is (usually 0.45455 is a good guess for GIF images on PCs). Note
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600741that file gammas are inverted from screen gammas. See the discussions
Andreas Dilger47a0c421997-05-16 02:46:07 -0500742on gamma in the PNG specification for an excellent description of what
743gamma is, and why all applications should support it. It is strongly
744recommended that PNG viewers support gamma correction.
Guy Schalnat0d580581995-07-20 02:43:20 -0500745
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746 if (png_get_gAMA(png_ptr, info_ptr, &gamma))
747 png_set_gamma(png_ptr, screen_gamma, gamma);
Guy Schalnate5a37791996-06-05 15:50:50 -0500748 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600749 png_set_gamma(png_ptr, screen_gamma, 0.45455);
Guy Schalnat0d580581995-07-20 02:43:20 -0500750
Guy Schalnate5a37791996-06-05 15:50:50 -0500751If you need to reduce an RGB file to a paletted file, or if a paletted
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600752file has more entries then will fit on your screen, png_set_dither()
753will do that. Note that this is a simple match dither that merely
754finds the closest color available. This should work fairly well with
755optimized palettes, and fairly badly with linear color cubes. If you
756pass a palette that is larger then maximum_colors, the file will
757reduce the number of colors in the palette so it will fit into
758maximum_colors. If there is a histogram, it will use it to make
Guy Schalnate5a37791996-06-05 15:50:50 -0500759more intelligent choices when reducing the palette. If there is no
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600760histogram, it may not do as good a job.
Guy Schalnat69b14481996-01-10 02:56:49 -0600761
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnat0d580581995-07-20 02:43:20 -0500763 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600764 if (png_get_valid(png_ptr, info_ptr,
765 PNG_INFO_PLTE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600766 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500767 png_color_16p histogram;
768
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600769 png_get_hIST(png_ptr, info_ptr,
770 &histogram);
771 png_set_dither(png_ptr, palette, num_palette,
772 max_screen_colors, histogram, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600773 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500774 else
775 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600776 png_color std_color_cube[MAX_SCREEN_COLORS] =
Guy Schalnat0d580581995-07-20 02:43:20 -0500777 { ... colors ... };
778
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600779 png_set_dither(png_ptr, std_color_cube,
780 MAX_SCREEN_COLORS, MAX_SCREEN_COLORS,
781 NULL,0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500782 }
783 }
784
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600785PNG files describe monochrome as black being zero and white being one.
786The following code will reverse this (make black be one and white be
787zero):
Guy Schalnat0d580581995-07-20 02:43:20 -0500788
Andreas Dilger47a0c421997-05-16 02:46:07 -0500789 if (bit_depth == 1 && color_type == PNG_COLOR_GRAY)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600790 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500791
Guy Schalnat69b14481996-01-10 02:56:49 -0600792PNG files store 16 bit pixels in network byte order (big-endian,
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600793ie. most significant bits first). This code changes the storage to the
794other way (little-endian, i.e. least significant bits first, the
Guy Schalnat69b14481996-01-10 02:56:49 -0600795way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -0500796
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 if (bit_depth == 16)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600798 png_set_swap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500799
Andreas Dilger47a0c421997-05-16 02:46:07 -0500800If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
801need to change the order the pixels are packed into bytes, you can use:
802
803 if (bit_depth < 8)
804 png_set_packswap(png_ptr);
805
Guy Schalnat69b14481996-01-10 02:56:49 -0600806The last thing to handle is interlacing; this is covered in detail below,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600807but you must call the function here if you want libpng to handle expansion
808of the interlaced image.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500809
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600810 number_of_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500811
Guy Schalnate5a37791996-06-05 15:50:50 -0500812After setting the transformations, libpng can update your png_info
813structure to reflect any transformations you've requested with this
Andreas Dilger47a0c421997-05-16 02:46:07 -0500814call. This is most useful to update the info structure's rowbytes
815field so you can use it to allocate your image memory. This function
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600816will also update your palette with the correct screen_gamma and
Guy Schalnate5a37791996-06-05 15:50:50 -0500817background if these have been given with the calls above.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500818
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600819 png_read_update_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500820
821After you call png_read_update_info(), you can allocate any
Guy Schalnate5a37791996-06-05 15:50:50 -0500822memory you need to hold the image. The row data is simply
823raw byte data for all forms of images. As the actual allocation
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500824varies among applications, no example will be given. If you
Guy Schalnate5a37791996-06-05 15:50:50 -0500825are allocating one large chunk, you will need to build an
826array of pointers to each row, as it will be needed for some
827of the functions below.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500828
829After you've allocated memory, you can read the image data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500830The simplest way to do this is in one function call. If you are
831allocating enough memory to hold the whole image, you can just
832call png_read_image() and libpng will read in all the image data
833and put it in the memory area supplied. You will need to pass in
834an array of pointers to each row.
835
836This function automatically handles interlacing, so you don't need
837to call png_set_interlace_handling() or call this function multiple
838times, or any of that other stuff necessary with png_read_rows().
839
840 png_read_image(png_ptr, row_pointers);
841
842where row_pointers is:
843
Guy Schalnate5a37791996-06-05 15:50:50 -0500844 png_bytep row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -0500845
846You can point to void or char or whatever you use for pixels.
847
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600848If you don't want to read in the whole image at once, you can
Guy Schalnat0d580581995-07-20 02:43:20 -0500849use png_read_rows() instead. If there is no interlacing (check
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600850interlace_type == PNG_INTERLACE_NONE), this is simple:
Guy Schalnat0d580581995-07-20 02:43:20 -0500851
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600852 png_read_rows(png_ptr, row_pointers, NULL,
853 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500854
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600855where row_pointers is the same as in the png_read_image() call.
Guy Schalnat0d580581995-07-20 02:43:20 -0500856
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600857If you are doing this just one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -0500858row_pointers:
859
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600860 png_bytep row_pointers = row;
Guy Schalnate5a37791996-06-05 15:50:50 -0500861 png_read_row(png_ptr, &row_pointers, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500862
Guy Schalnat69b14481996-01-10 02:56:49 -0600863If the file is interlaced (info_ptr->interlace_type != 0), things get
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500864somewhat harder. The only current (PNG Specification version 1.2)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600865interlacing type for PNG is (interlace_type == PNG_INTERLACE_ADAM7)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600866is a somewhat complicated 2D interlace scheme, known as Adam7, that
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867breaks down an image into seven smaller images of varying size, based
868on an 8x8 grid.
Guy Schalnat0d580581995-07-20 02:43:20 -0500869
Guy Schalnate5a37791996-06-05 15:50:50 -0500870libpng can fill out those images or it can give them to you "as is".
871If you want them filled out, there are two ways to do that. The one
872mentioned in the PNG specification is to expand each pixel to cover
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600873those pixels that have not been read yet (the "rectangle" method).
874This results in a blocky image for the first pass, which gradually
875smooths out as more pixels are read. The other method is the "sparkle"
876method, where pixels are drawn only in their final locations, with the
877rest of the image remaining whatever colors they were initialized to
878before the start of the read. The first method usually looks better,
879but tends to be slower, as there are more pixels to put in the rows.
Guy Schalnate5a37791996-06-05 15:50:50 -0500880
881If you don't want libpng to handle the interlacing details, just call
882png_read_rows() seven times to read in all seven images. Each of the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600883images is a valid image by itself, or they can all be combined on an
Guy Schalnate5a37791996-06-05 15:50:50 -05008848x8 grid to form a single image (although if you intend to combine them
885you would be far better off using the libpng interlace handling).
886
887The first pass will return an image 1/8 as wide as the entire image
888(every 8th column starting in column 0) and 1/8 as high as the original
889(every 8th row starting in row 0), the second will be 1/8 as wide
890(starting in column 4) and 1/8 as high (also starting in row 0). The
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600891third pass will be 1/4 as wide (every 4th pixel starting in column 0) and
Guy Schalnate5a37791996-06-05 15:50:50 -05008921/8 as high (every 8th row starting in row 4), and the fourth pass will
893be 1/4 as wide and 1/4 as high (every 4th column starting in column 2,
894and every 4th row starting in row 0). The fifth pass will return an
895image 1/2 as wide, and 1/4 as high (starting at column 0 and row 2),
896while the sixth pass will be 1/2 as wide and 1/2 as high as the original
897(starting in column 1 and row 0). The seventh and final pass will be as
898wide as the original, and 1/2 as high, containing all of the odd
899numbered scanlines. Phew!
Guy Schalnat0d580581995-07-20 02:43:20 -0500900
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600901If you want libpng to expand the images, call this before calling
902png_start_read_image() or png_read_update_info():
Guy Schalnat0d580581995-07-20 02:43:20 -0500903
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600904 if (interlace_type == PNG_INTERLACE_ADAM7)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600905 number_of_passes
906 = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500907
908This will return the number of passes needed. Currently, this
909is seven, but may change if another interlace type is added.
910This function can be called even if the file is not interlaced,
Guy Schalnate5a37791996-06-05 15:50:50 -0500911where it will return one pass.
Guy Schalnat0d580581995-07-20 02:43:20 -0500912
913If you are not going to display the image after each pass, but are
914going to wait until the entire image is read in, use the sparkle
915effect. This effect is faster and the end result of either method
916is exactly the same. If you are planning on displaying the image
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600917after each pass, the "rectangle" effect is generally considered the
Guy Schalnat0d580581995-07-20 02:43:20 -0500918better looking one.
919
920If you only want the "sparkle" effect, just call png_read_rows() as
921normal, with the third parameter NULL. Make sure you make pass over
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600922the image number_of_passes times, and you don't change the data in the
Guy Schalnat0d580581995-07-20 02:43:20 -0500923rows between calls. You can change the locations of the data, just
924not the data. Each pass only writes the pixels appropriate for that
925pass, and assumes the data from previous passes is still valid.
926
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600927 png_read_rows(png_ptr, row_pointers, NULL,
928 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500929
930If you only want the first effect (the rectangles), do the same as
931before except pass the row buffer in the third parameter, and leave
932the second parameter NULL.
933
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600934 png_read_rows(png_ptr, NULL, row_pointers,
935 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500936
937After you are finished reading the image, you can finish reading
Guy Schalnate5a37791996-06-05 15:50:50 -0500938the file. If you are interested in comments or time, which may be
939stored either before or after the image data, you should pass the
Guy Schalnate5a37791996-06-05 15:50:50 -0500940separate png_info struct if you want to keep the comments from
941before and after the image separate. If you are not interested, you
942can pass NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -0500943
Guy Schalnate5a37791996-06-05 15:50:50 -0500944 png_read_end(png_ptr, end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500945
Guy Schalnate5a37791996-06-05 15:50:50 -0500946When you are done, you can free all memory allocated by libpng like this:
Guy Schalnat0d580581995-07-20 02:43:20 -0500947
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600948 png_destroy_read_struct(&png_ptr, &info_ptr,
949 &end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500950
Guy Schalnate5a37791996-06-05 15:50:50 -0500951For a more compact example of reading a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -0500952
953
Guy Schalnat6d764711995-12-19 03:22:19 -0600954Reading PNG files progressively:
955
956The progressive reader is slightly different then the non-progressive
957reader. Instead of calling png_read_info(), png_read_rows(), and
958png_read_end(), you make one call to png_process_data(), which calls
959callbacks when it has the info, a row, or the end of the image. You
960set up these callbacks with png_set_progressive_read_fn(). You don't
961have to worry about the input/output functions of libpng, as you are
962giving the library the data directly in png_process_data(). I will
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600963assume that you have read the section on reading PNG files above,
Guy Schalnat6d764711995-12-19 03:22:19 -0600964so I will only highlight the differences (although I will show
965all of the code).
966
967png_structp png_ptr;
968png_infop info_ptr;
969
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600970 /* An example code fragment of how you would
971 initialize the progressive reader in your
972 application. */
973 int
974 initialize_png_reader()
975 {
976 png_ptr = png_create_read_struct
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500977 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600978 user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600979 if (!png_ptr)
980 return -1;
Guy Schalnate5a37791996-06-05 15:50:50 -0500981 info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600982 if (!info_ptr)
983 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600984 png_destroy_read_struct(&png_ptr, (png_infopp)NULL,
985 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600986 return -1;
987 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600988
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600989 if (setjmp(png_ptr->jmpbuf))
990 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600991 png_destroy_read_struct(&png_ptr, &info_ptr,
992 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600993 return -1;
994 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600995
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600996 /* This one's new. You can provide functions
997 to be called when the header info is valid,
998 when each row is completed, and when the image
999 is finished. If you aren't using all functions,
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001000 you can specify NULL parameters. Even when all
1001 three functions are NULL, you need to call
1002 png_set_progressive_read_fn(). You can use
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001003 any struct as the user_ptr (cast to a void pointer
1004 for the function call), and retrieve the pointer
1005 from inside the callbacks using the function
1006
1007 png_get_progressive_ptr(png_ptr);
1008
1009 which will return a void pointer, which you have
1010 to cast appropriately.
Guy Schalnate5a37791996-06-05 15:50:50 -05001011 */
1012 png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001013 info_callback, row_callback, end_callback);
Guy Schalnat6d764711995-12-19 03:22:19 -06001014
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001015 return 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001016 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001017
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001018 /* A code fragment that you call as you receive blocks
1019 of data */
1020 int
1021 process_data(png_bytep buffer, png_uint_32 length)
1022 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001023 if (setjmp(png_ptr->jmpbuf))
1024 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001025 png_destroy_read_struct(&png_ptr, &info_ptr,
1026 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001027 return -1;
1028 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001029
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001030 /* This one's new also. Simply give it a chunk
1031 of data from the file stream (in order, of
1032 course). On machines with segmented memory
1033 models machines, don't give it any more than
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001034 64K. The library seems to run fine with sizes
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001035 of 4K. Although you can give it much less if
1036 necessary (I assume you can give it chunks of
1037 1 byte, I haven't tried less then 256 bytes
1038 yet). When this function returns, you may
1039 want to display any rows that were generated
1040 in the row callback if you don't already do
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001041 so there.
Guy Schalnate5a37791996-06-05 15:50:50 -05001042 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001043 png_process_data(png_ptr, info_ptr, buffer, length);
1044 return 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001045 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001046
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001047 /* This function is called (as set by
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001048 png_set_progressive_read_fn() above) when enough data
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001049 has been supplied so all of the header has been
1050 read.
Guy Schalnate5a37791996-06-05 15:50:50 -05001051 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001052 void
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001053 info_callback(png_structp png_ptr, png_infop info)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001054 {
1055 /* Do any setup here, including setting any of
1056 the transformations mentioned in the Reading
1057 PNG files section. For now, you _must_ call
1058 either png_start_read_image() or
1059 png_read_update_info() after all the
1060 transformations are set (even if you don't set
1061 any). You may start getting rows before
1062 png_process_data() returns, so this is your
1063 last chance to prepare for that.
Guy Schalnate5a37791996-06-05 15:50:50 -05001064 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001065 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001066
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001067 /* This function is called when each row of image
1068 data is complete */
1069 void
1070 row_callback(png_structp png_ptr, png_bytep new_row,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001071 png_uint_32 row_num, int pass)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001072 {
1073 /* If the image is interlaced, and you turned
1074 on the interlace handler, this function will
1075 be called for every row in every pass. Some
1076 of these rows will not be changed from the
1077 previous pass. When the row is not changed,
1078 the new_row variable will be NULL. The rows
1079 and passes are called in order, so you don't
1080 really need the row_num and pass, but I'm
1081 supplying them because it may make your life
1082 easier.
Guy Schalnat6d764711995-12-19 03:22:19 -06001083
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001084 For the non-NULL rows of interlaced images,
1085 you must call png_progressive_combine_row()
1086 passing in the row and the old row. You can
1087 call this function for NULL rows (it will just
1088 return) and for non-interlaced images (it just
1089 does the memcpy for you) if it will make the
1090 code easier. Thus, you can just do this for
1091 all cases:
Guy Schalnate5a37791996-06-05 15:50:50 -05001092 */
Guy Schalnat6d764711995-12-19 03:22:19 -06001093
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001094 png_progressive_combine_row(png_ptr, old_row,
1095 new_row);
Guy Schalnat6d764711995-12-19 03:22:19 -06001096
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001097 /* where old_row is what was displayed for
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05001098 previously for the row. Note that the first
1099 pass (pass == 0, really) will completely cover
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001100 the old row, so the rows do not have to be
1101 initialized. After the first pass (and only
1102 for interlaced images), you will have to pass
1103 the current row, and the function will combine
1104 the old row and the new row.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001105 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001106 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001107
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001108 void
1109 end_callback(png_structp png_ptr, png_infop info)
1110 {
1111 /* This function is called after the whole image
1112 has been read, including any chunks after the
1113 image (up to and including the IEND). You
1114 will usually have the same info chunk as you
1115 had in the header, although some data may have
1116 been added to the comments and time fields.
Guy Schalnat6d764711995-12-19 03:22:19 -06001117
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001118 Most people won't do much here, perhaps setting
1119 a flag that marks the image as finished.
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001121 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001122
1123
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001124
Guy Schalnat69b14481996-01-10 02:56:49 -06001125IV. Writing
Guy Schalnat0d580581995-07-20 02:43:20 -05001126
1127Much of this is very similar to reading. However, everything of
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001128importance is repeated here, so you won't have to constantly look
Guy Schalnat69b14481996-01-10 02:56:49 -06001129back up in the reading section to understand writing.
Guy Schalnat0d580581995-07-20 02:43:20 -05001130
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001131You will want to do the I/O initialization before you get into libpng,
Guy Schalnate5a37791996-06-05 15:50:50 -05001132so if it doesn't work, you don't have anything to undo. If you are not
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001133using the standard I/O functions, you will need to replace them with
Guy Schalnate5a37791996-06-05 15:50:50 -05001134custom writing functions. See the discussion under Customizing libpng.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001135
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001136 FILE *fp = fopen(file_name, "wb");
1137 if (!fp)
1138 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001139 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001140 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001141
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001142Next, png_struct and png_info need to be allocated and initialized.
Guy Schalnate5a37791996-06-05 15:50:50 -05001143As these can be both relatively large, you may not want to store these
1144on the stack, unless you have stack space to spare. Of course, you
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001145will want to check if they return NULL. If you are also reading,
1146you won't want to name your read structure and your write structure
1147both "png_ptr"; you can call them anything you like, such as
1148"read_ptr" and "write_ptr". Look at pngtest.c, for example.
Guy Schalnat0d580581995-07-20 02:43:20 -05001149
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001150 png_structp png_ptr = png_create_write_struct
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001151 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001152 user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001153 if (!png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -05001154 return;
1155
1156 png_infop info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001157 if (!info_ptr)
1158 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001159 png_destroy_write_struct(&png_ptr,
1160 (png_infopp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -05001161 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001162 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001163
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001164If you want to use your own memory allocation routines,
1165define PNG_USER_MEM_SUPPORTED and use
1166png_create_write_struct_2() instead of png_create_read_struct():
1167
1168 png_structp png_ptr = png_create_write_struct_2
1169 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
1170 user_error_fn, user_warning_fn, (png_voidp)
1171 user_mem_ptr, user_malloc_fn, user_free_fn);
1172
Guy Schalnat0d580581995-07-20 02:43:20 -05001173After you have these structures, you will need to set up the
1174error handling. When libpng encounters an error, it expects to
Guy Schalnate5a37791996-06-05 15:50:50 -05001175longjmp() back to your routine. Therefore, you will need to call
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001176setjmp() and pass the png_ptr->jmpbuf. If you
Guy Schalnat0d580581995-07-20 02:43:20 -05001177write the file from different routines, you will need to update
1178the jmpbuf field every time you enter a new routine that will
1179call a png_ function. See your documentation of setjmp/longjmp
Guy Schalnat6d764711995-12-19 03:22:19 -06001180for your compiler for more information on setjmp/longjmp. See
Guy Schalnat69b14481996-01-10 02:56:49 -06001181the discussion on libpng error handling in the Customizing Libpng
1182section below for more information on the libpng error handling.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001183
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001184 if (setjmp(png_ptr->jmpbuf))
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001185 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001186 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001187 fclose(fp);
1188 return;
1189 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001190 ...
1191 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001192
Andreas Dilger47a0c421997-05-16 02:46:07 -05001193Now you need to set up the output code. The default for libpng is to
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001194use the C function fwrite(). If you use this, you will need to pass a
1195valid FILE * in the function png_init_io(). Be sure that the file is
1196opened in binary mode. Again, if you wish to handle writing data in
1197another way, see the discussion on libpng I/O handling in the Customizing
1198Libpng section below.
Guy Schalnat0d580581995-07-20 02:43:20 -05001199
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001200 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001201
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001202At this point, you can set up a callback function that will be
1203called after each row has been written, which you can use to control
1204a progress meter or the like. It's demonstrated in pngtest.c.
1205You must supply a function
1206
1207 void write_row_callback(png_ptr, png_uint_32 row, int pass);
1208 {
1209 /* put your code here */
1210 }
1211
1212(You can give it another name that you like instead of "write_row_callback")
1213
1214To inform libpng about your function, use
1215
1216 png_set_write_status_fn(png_ptr, write_row_callback);
1217
Guy Schalnate5a37791996-06-05 15:50:50 -05001218You now have the option of modifying how the compression library will
1219run. The following functions are mainly for testing, but may be useful
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001220in some cases, like if you need to write PNG files extremely fast and
Guy Schalnate5a37791996-06-05 15:50:50 -05001221are willing to give up some compression, or if you want to get the
1222maximum possible compression at the expense of slower writing. If you
1223have no special needs in this area, let the library do what it wants by
1224not calling this function at all, as it has been tuned to deliver a good
1225speed/compression ratio. The second parameter to png_set_filter() is
1226the filter method, for which the only valid value is '0' (as of the
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001227July 1999 PNG specification, version 1.2). The third parameter is a
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001228flag that indicates which filter type(s) are to be tested for each
1229scanline. See the Compression Library for details on the specific filter
1230types.
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001231
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001232
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001233 /* turn on or off filtering, and/or choose
1234 specific filters */
Guy Schalnate5a37791996-06-05 15:50:50 -05001235 png_set_filter(png_ptr, 0,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001236 PNG_FILTER_NONE | PNG_FILTER_SUB |
1237 PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001238
1239The png_set_compression_???() functions interface to the zlib compression
1240library, and should mostly be ignored unless you really know what you are
1241doing. The only generally useful call is png_set_compression_level()
1242which changes how much time zlib spends on trying to compress the image
1243data. See the Compression Library for details on the compression levels.
1244
1245 /* set the zlib compression level */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001246 png_set_compression_level(png_ptr,
1247 Z_BEST_COMPRESSION);
Guy Schalnate5a37791996-06-05 15:50:50 -05001248
1249 /* set other zlib parameters */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001250 png_set_compression_mem_level(png_ptr, 8);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001251 png_set_compression_strategy(png_ptr,
1252 Z_DEFAULT_STRATEGY);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001253 png_set_compression_window_bits(png_ptr, 15);
1254 png_set_compression_method(png_ptr, 8);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001255
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001256You now need to fill in the png_info structure with all the data you
1257wish to write before the actual image. Note that the only thing you
1258are allowed to write after the image is the text chunks and the time
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001259chunk (as of PNG Specification 1.2, anyway). See png_write_end() and
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001260the latest PNG specification for more information on that. If you
Guy Schalnate5a37791996-06-05 15:50:50 -05001261wish to write them before the image, fill them in now, and flag that
1262data as being valid. If you want to wait until after the data, don't
1263fill them until png_write_end(). For all the fields in png_info and
1264their data types, see png.h. For explanations of what the fields
1265contain, see the PNG specification.
Guy Schalnat69b14481996-01-10 02:56:49 -06001266
1267Some of the more important parts of the png_info are:
1268
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001269 png_set_IHDR(png_ptr, info_ptr, width, height,
1270 bit_depth, color_type, interlace_type,
1271 compression_type, filter_type)
1272 width - holds the width of the image
1273 in pixels (up to 2^31).
1274 height - holds the height of the image
1275 in pixels (up to 2^31).
1276 bit_depth - holds the bit depth of one of the
1277 image channels.
1278 (valid values are 1, 2, 4, 8, 16
1279 and depend also on the
1280 color_type. See also significant
1281 bits (sBIT) below).
1282 color_type - describes which color/alpha
1283 channels are present.
1284 PNG_COLOR_TYPE_GRAY
1285 (bit depths 1, 2, 4, 8, 16)
1286 PNG_COLOR_TYPE_GRAY_ALPHA
1287 (bit depths 8, 16)
1288 PNG_COLOR_TYPE_PALETTE
1289 (bit depths 1, 2, 4, 8)
1290 PNG_COLOR_TYPE_RGB
1291 (bit_depths 8, 16)
1292 PNG_COLOR_TYPE_RGB_ALPHA
1293 (bit_depths 8, 16)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294
1295 PNG_COLOR_MASK_PALETTE
1296 PNG_COLOR_MASK_COLOR
1297 PNG_COLOR_MASK_ALPHA
1298
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001299 interlace_type - PNG_INTERLACE_NONE or
1300 PNG_INTERLACE_ADAM7
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001301 compression_type - (must be
1302 PNG_COMPRESSION_TYPE_DEFAULT)
1303 filter_type - (must be PNG_FILTER_TYPE_DEFAULT)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001304
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001305 png_set_PLTE(png_ptr, info_ptr, palette,
1306 num_palette);
1307 palette - the palette for the file
1308 (array of png_color)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001309 num_palette - number of entries in the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -05001310
1311 png_set_gAMA(png_ptr, info_ptr, gamma);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001312 gamma - the gamma the image was created
1313 at (PNG_INFO_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001314
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001315 png_set_sRGB(png_ptr, info_ptr, srgb_intent);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001316 srgb_intent - the rendering intent
1317 (PNG_INFO_sRGB) The presence of
1318 the sRGB chunk means that the pixel
1319 data is in the sRGB color space.
1320 This chunk also implies specific
1321 values of gAMA and cHRM. Rendering
1322 intent is the CSS-1 property that
1323 has been defined by the International
1324 Color Consortium
1325 (http://www.color.org).
1326 It can be one of
1327 PNG_SRGB_INTENT_SATURATION,
1328 PNG_SRGB_INTENT_PERCEPTUAL,
1329 PNG_SRGB_INTENT_ABSOLUTE, or
1330 PNG_SRGB_INTENT_RELATIVE.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001331
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001332
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001333 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
1334 srgb_intent);
1335 srgb_intent - the rendering intent
1336 (PNG_INFO_sRGB) The presence of the
1337 sRGB chunk means that the pixel
1338 data is in the sRGB color space.
1339 This function also causes gAMA and
1340 cHRM chunks with the specific values
1341 that are consistent with sRGB to be
1342 written.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001343
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 png_set_sBIT(png_ptr, info_ptr, sig_bit);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001345 sig_bit - the number of significant bits for
1346 (PNG_INFO_sBIT) each of the gray, red,
1347 green, and blue channels, whichever are
1348 appropriate for the given color type
1349 (png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001350
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001351 png_set_tRNS(png_ptr, info_ptr, trans, num_trans,
1352 trans_values);
1353 trans - array of transparent entries for
1354 palette (PNG_INFO_tRNS)
1355 trans_values - transparent pixel for non-paletted
1356 images (PNG_INFO_tRNS)
1357 num_trans - number of transparent entries
1358 (PNG_INFO_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001359
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001360 png_set_hIST(png_ptr, info_ptr, hist);
1361 (PNG_INFO_hIST)
1362 hist - histogram of palette (array of
1363 png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001364
1365 png_set_tIME(png_ptr, info_ptr, mod_time);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001366 mod_time - time image was last modified
1367 (PNG_VALID_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001368
1369 png_set_bKGD(png_ptr, info_ptr, background);
Guy Schalnate5a37791996-06-05 15:50:50 -05001370 background - background color (PNG_VALID_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001371
1372 png_set_text(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001373 text_ptr - array of png_text holding image
1374 comments
1375 text_ptr[i]->key - keyword for comment.
1376 text_ptr[i]->text - text comments for current
1377 keyword.
1378 text_ptr[i]->compression - type of compression used
1379 on "text" PNG_TEXT_COMPRESSION_NONE or
1380 PNG_TEXT_COMPRESSION_zTXt
1381 num_text - number of comments in text_ptr
Andreas Dilger47a0c421997-05-16 02:46:07 -05001382
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001383 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y,
1384 unit_type);
1385 offset_x - positive offset from the left
1386 edge of the screen
1387 offset_y - positive offset from the top
1388 edge of the screen
1389 unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
Andreas Dilger47a0c421997-05-16 02:46:07 -05001390
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001391 png_set_pHYs(png_ptr, info_ptr, res_x, res_y,
1392 unit_type);
1393 res_x - pixels/unit physical resolution
1394 in x direction
1395 res_y - pixels/unit physical resolution
1396 in y direction
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001397 unit_type - PNG_RESOLUTION_UNKNOWN,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001398 PNG_RESOLUTION_METER
Guy Schalnat0d580581995-07-20 02:43:20 -05001399
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001400In PNG files, the alpha channel in an image is the level of opacity.
1401If your data is supplied as a level of transparency, you can invert the
1402alpha channel before you write it, so that 0 is fully transparent and 255
1403(in 8-bit or paletted images) or 65535 (in 16-bit images) is fully opaque,
1404with
1405
1406 png_set_invert_alpha(png_ptr);
1407
1408This must appear here instead of later with the other transformations
1409because in the case of paletted images the tRNS chunk data has to
1410be inverted before the tRNS chunk is written. If your image is not a
1411paletted image, the tRNS data (which in such cases represents a single
1412color to be rendered as transparent) won't be changed.
1413
Guy Schalnat0d580581995-07-20 02:43:20 -05001414A quick word about text and num_text. text is an array of png_text
1415structures. num_text is the number of valid structures in the array.
1416If you want, you can use max_text to hold the size of the array, but
1417libpng ignores it for writing (it does use it for reading). Each
1418png_text structure holds a keyword-text value, and a compression type.
1419The compression types have the same valid numbers as the compression
1420types of the image data. Currently, the only valid number is zero.
1421However, you can store text either compressed or uncompressed, unlike
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001422images, which always have to be compressed. So if you don't want the
Andreas Dilger47a0c421997-05-16 02:46:07 -05001423text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
1424Until text gets around 1000 bytes, it is not worth compressing it.
1425After the text has been written out to the file, the compression type
1426is set to PNG_TEXT_COMPRESSION_NONE_WR or PNG_TEXT_COMPRESSION_zTXt_WR,
1427so that it isn't written out again at the end (in case you are calling
1428png_write_end() with the same struct.
Guy Schalnate5a37791996-06-05 15:50:50 -05001429
1430The keywords that are given in the PNG Specification are:
1431
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001432 Title Short (one line) title or
1433 caption for image
1434 Author Name of image's creator
1435 Description Description of image (possibly long)
1436 Copyright Copyright notice
1437 Creation Time Time of original image creation
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001438 (usually RFC 1123 format, see below)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001439 Software Software used to create the image
1440 Disclaimer Legal disclaimer
1441 Warning Warning of nature of content
1442 Source Device used to create the image
1443 Comment Miscellaneous comment; conversion
1444 from other image format
Guy Schalnat0d580581995-07-20 02:43:20 -05001445
1446The keyword-text pairs work like this. Keywords should be short
1447simple descriptions of what the comment is about. Some typical
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001448keywords are found in the PNG specification, as is some recommendations
Guy Schalnat0d580581995-07-20 02:43:20 -05001449on keywords. You can repeat keywords in a file. You can even write
1450some text before the image and some after. For example, you may want
1451to put a description of the image before the image, but leave the
1452disclaimer until after, so viewers working over modem connections
1453don't have to wait for the disclaimer to go over the modem before
1454they start seeing the image. Finally, keywords should be full
Guy Schalnate5a37791996-06-05 15:50:50 -05001455words, not abbreviations. Keywords and text are in the ISO 8859-1
1456(Latin-1) character set (a superset of regular ASCII) and can not
1457contain NUL characters, and should not contain control or other
1458unprintable characters. To make the comments widely readable, stick
1459with basic ASCII, and avoid machine specific character set extensions
1460like the IBM-PC character set. The keyword must be present, but
Guy Schalnat0d580581995-07-20 02:43:20 -05001461you can leave off the text string on non-compressed pairs.
1462Compressed pairs must have a text string, as only the text string
1463is compressed anyway, so the compression would be meaningless.
1464
Guy Schalnat6d764711995-12-19 03:22:19 -06001465PNG supports modification time via the png_time structure. Two
Guy Schalnat0d580581995-07-20 02:43:20 -05001466conversion routines are proved, png_convert_from_time_t() for
1467time_t and png_convert_from_struct_tm() for struct tm. The
1468time_t routine uses gmtime(). You don't have to use either of
1469these, but if you wish to fill in the png_time structure directly,
1470you should provide the time in universal time (GMT) if possible
Guy Schalnat69b14481996-01-10 02:56:49 -06001471instead of your local time. Note that the year number is the full
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001472year (e.g. 1998, rather than 98 - PNG is year 2000 compliant!), and
Andreas Dilger47a0c421997-05-16 02:46:07 -05001473that months start with 1.
Guy Schalnat0d580581995-07-20 02:43:20 -05001474
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001475If you want to store the time of the original image creation, you should
1476use a plain tEXt chunk with the "Creation Time" keyword. This is
1477necessary because the "creation time" of a PNG image is somewhat vague,
1478depending on whether you mean the PNG file, the time the image was
1479created in a non-PNG format, a still photo from which the image was
1480scanned, or possibly the subject matter itself. In order to facilitate
1481machine-readable dates, it is recommended that the "Creation Time"
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001482tEXt chunk use RFC 1123 format dates (e.g. "22 May 1997 18:07:10 GMT"),
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001483although this isn't a requirement. Unlike the tIME chunk, the
1484"Creation Time" tEXt chunk is not expected to be automatically changed
1485by the software. To facilitate the use of RFC 1123 dates, a function
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001486png_convert_to_rfc1123(png_timep) is provided to convert from PNG
1487time to an RFC 1123 format string.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001488
Guy Schalnat0d580581995-07-20 02:43:20 -05001489You are now ready to write all the file information up to the actual
1490image data. You do this with a call to png_write_info().
1491
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001492 png_write_info(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001493
Guy Schalnate5a37791996-06-05 15:50:50 -05001494After you've written the file information, you can set up the library
1495to handle any special transformations of the image data. The various
Guy Schalnat0d580581995-07-20 02:43:20 -05001496ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001497should occur. This is important, as some of these change the color
1498type and/or bit depth of the data, and some others only work on
1499certain color types and bit depths. Even though each transformation
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001500checks to see if it has data that it can do something with, you should
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001501make sure to only enable a transformation if it will be valid for the
1502data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -05001503
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001504PNG files store RGB pixels packed into 3 or 6 bytes. This code tells
1505the library to expand the input data to 4 or 8 bytes per pixel
1506(or expand 1 or 2-byte grayscale data to 2 or 4 bytes per pixel).
Guy Schalnat0d580581995-07-20 02:43:20 -05001507
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001508 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Guy Schalnat0d580581995-07-20 02:43:20 -05001509
Guy Schalnate5a37791996-06-05 15:50:50 -05001510where the 0 is the value that will be put in the 4th byte, and the
1511location is either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending
1512upon whether the filler byte is stored XRGB or RGBX.
Guy Schalnat0d580581995-07-20 02:43:20 -05001513
1514PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
1515they can, resulting in, for example, 8 pixels per byte for 1 bit files.
Guy Schalnat69b14481996-01-10 02:56:49 -06001516If the data is supplied at 1 pixel per byte, use this code, which will
Guy Schalnate5a37791996-06-05 15:50:50 -05001517correctly pack the pixels into a single byte:
Guy Schalnat0d580581995-07-20 02:43:20 -05001518
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001519 png_set_packing(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001520
1521PNG files reduce possible bit depths to 1, 2, 4, 8, and 16. If your
Guy Schalnate5a37791996-06-05 15:50:50 -05001522data is of another bit depth, you can write an sBIT chunk into the
1523file so that decoders can get the original data if desired.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001524
Guy Schalnate5a37791996-06-05 15:50:50 -05001525 /* Set the true bit depth of the image data */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001526 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001527 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001528 sig_bit.red = true_bit_depth;
1529 sig_bit.green = true_bit_depth;
1530 sig_bit.blue = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001531 }
1532 else
1533 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001534 sig_bit.gray = true_bit_depth;
1535 }
1536 if (color_type & PNG_COLOR_MASK_ALPHA)
1537 {
1538 sig_bit.alpha = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001539 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001540
Andreas Dilger47a0c421997-05-16 02:46:07 -05001541 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -05001542
Guy Schalnate5a37791996-06-05 15:50:50 -05001543If the data is stored in the row buffer in a bit depth other than
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001544one supported by PNG (e.g. 3 bit data in the range 0-7 for a 4-bit PNG),
Guy Schalnate5a37791996-06-05 15:50:50 -05001545this will scale the values to appear to be the correct bit depth as
1546is required by PNG.
1547
Andreas Dilger47a0c421997-05-16 02:46:07 -05001548 png_set_shift(png_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -05001549
Guy Schalnat69b14481996-01-10 02:56:49 -06001550PNG files store 16 bit pixels in network byte order (big-endian,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001551ie. most significant bits first). This code would be used if they are
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001552supplied the other way (little-endian, i.e. least significant bits
1553first, the way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -05001554
Andreas Dilger47a0c421997-05-16 02:46:07 -05001555 if (bit_depth > 8)
1556 png_set_swap(png_ptr);
1557
1558If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
1559need to change the order the pixels are packed into bytes, you can use:
1560
1561 if (bit_depth < 8)
1562 png_set_packswap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001563
Guy Schalnat69b14481996-01-10 02:56:49 -06001564PNG files store 3 color pixels in red, green, blue order. This code
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001565would be used if they are supplied as blue, green, red:
Guy Schalnat0d580581995-07-20 02:43:20 -05001566
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001567 png_set_bgr(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001568
Guy Schalnat69b14481996-01-10 02:56:49 -06001569PNG files describe monochrome as black being zero and white being
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001570one. This code would be used if the pixels are supplied with this reversed
Guy Schalnat69b14481996-01-10 02:56:49 -06001571(black being one and white being zero):
Guy Schalnat0d580581995-07-20 02:43:20 -05001572
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001573 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001574
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001575Finally, you can write your own transformation function if none of
1576the existing ones meets your needs. This is done by setting a callback
1577with
1578
1579 png_set_write_user_transform_fn(png_ptr,
1580 write_transform_fn);
1581
1582You must supply the function
1583
1584 void write_transform_fn(png_ptr ptr, row_info_ptr
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001585 row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001586
1587See pngtest.c for a working example. Your function will be called
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001588before any of the other transformations are processed.
1589
1590You can also set up a pointer to a user structure for use by your
1591callback function.
1592
1593 png_set_user_transform_info(png_ptr, user_ptr, 0, 0);
1594
1595The user_channels and user_depth parameters of this function are ignored
1596when writing; you can set them to zero as shown.
1597
1598You can retrieve the pointer via the function
1599png_get_user_transform_ptr(). For example:
1600
1601 voidp write_user_transform_ptr =
1602 png_get_user_transform_ptr(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001603
Guy Schalnate5a37791996-06-05 15:50:50 -05001604It is possible to have libpng flush any pending output, either manually,
1605or automatically after a certain number of lines have been written. To
1606flush the output stream a single time call:
1607
1608 png_write_flush(png_ptr);
1609
1610and to have libpng flush the output stream periodically after a certain
1611number of scanlines have been written, call:
1612
1613 png_set_flush(png_ptr, nrows);
1614
1615Note that the distance between rows is from the last time png_write_flush()
1616was called, or the first row of the image if it has never been called.
1617So if you write 50 lines, and then png_set_flush 25, it will flush the
1618output on the next scanline, and every 25 lines thereafter, unless
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001619png_write_flush() is called before 25 more lines have been written.
Guy Schalnate5a37791996-06-05 15:50:50 -05001620If nrows is too small (less than about 10 lines for a 640 pixel wide
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001621RGB image) the image compression may decrease noticeably (although this
Guy Schalnate5a37791996-06-05 15:50:50 -05001622may be acceptable for real-time applications). Infrequent flushing will
1623only degrade the compression performance by a few percent over images
1624that do not use flushing.
1625
Guy Schalnat0d580581995-07-20 02:43:20 -05001626That's it for the transformations. Now you can write the image data.
1627The simplest way to do this is in one function call. If have the
1628whole image in memory, you can just call png_write_image() and libpng
1629will write the image. You will need to pass in an array of pointers to
1630each row. This function automatically handles interlacing, so you don't
1631need to call png_set_interlace_handling() or call this function multiple
1632times, or any of that other stuff necessary with png_write_rows().
1633
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001634 png_write_image(png_ptr, row_pointers);
Guy Schalnat0d580581995-07-20 02:43:20 -05001635
1636where row_pointers is:
1637
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001638 png_byte *row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -05001639
1640You can point to void or char or whatever you use for pixels.
1641
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001642If you don't want to write the whole image at once, you can
Guy Schalnat0d580581995-07-20 02:43:20 -05001643use png_write_rows() instead. If the file is not interlaced,
1644this is simple:
1645
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001646 png_write_rows(png_ptr, row_pointers,
1647 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -05001648
1649row_pointers is the same as in the png_write_image() call.
1650
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001651If you are just writing one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -05001652row_pointers:
1653
Guy Schalnate5a37791996-06-05 15:50:50 -05001654 png_bytep row_pointer = row;
Guy Schalnat0d580581995-07-20 02:43:20 -05001655
Guy Schalnate5a37791996-06-05 15:50:50 -05001656 png_write_row(png_ptr, &row_pointer);
Guy Schalnat0d580581995-07-20 02:43:20 -05001657
Guy Schalnat69b14481996-01-10 02:56:49 -06001658When the file is interlaced, things can get a good deal more
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001659complicated. The only currently (as of August 1999 -- PNG Specification
1660version 1.2, dated July 1999) defined interlacing scheme for PNG files
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001661is the "Adam7" interlace scheme, that breaks down an
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001662image into seven smaller images of varying size. libpng will build
1663these images for you, or you can do them yourself. If you want to
1664build them yourself, see the PNG specification for details of which
1665pixels to write when.
Guy Schalnat0d580581995-07-20 02:43:20 -05001666
1667If you don't want libpng to handle the interlacing details, just
Guy Schalnate5a37791996-06-05 15:50:50 -05001668use png_set_interlace_handling() and call png_write_rows() the
1669correct number of times to write all seven sub-images.
Guy Schalnat0d580581995-07-20 02:43:20 -05001670
Guy Schalnat69b14481996-01-10 02:56:49 -06001671If you want libpng to build the sub-images, call this before you start
1672writing any rows:
Guy Schalnat0d580581995-07-20 02:43:20 -05001673
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001674 number_of_passes =
1675 png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001676
1677This will return the number of passes needed. Currently, this
1678is seven, but may change if another interlace type is added.
1679
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001680Then write the complete image number_of_passes times.
Guy Schalnat0d580581995-07-20 02:43:20 -05001681
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001682 png_write_rows(png_ptr, row_pointers,
1683 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -05001684
1685As some of these rows are not used, and thus return immediately,
1686you may want to read about interlacing in the PNG specification,
1687and only update the rows that are actually used.
1688
1689After you are finished writing the image, you should finish writing
1690the file. If you are interested in writing comments or time, you should
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001691pass an appropriately filled png_info pointer. If you are not interested,
1692you can pass NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -05001693
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001694 png_write_end(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001695
1696When you are done, you can free all memory used by libpng like this:
1697
Guy Schalnate5a37791996-06-05 15:50:50 -05001698 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001699
Guy Schalnate5a37791996-06-05 15:50:50 -05001700You must free any data you allocated for info_ptr, such as comments,
1701palette, or histogram, before the call to png_destroy_write_struct();
Guy Schalnat0d580581995-07-20 02:43:20 -05001702
Guy Schalnate5a37791996-06-05 15:50:50 -05001703For a more compact example of writing a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -05001704
Guy Schalnat69b14481996-01-10 02:56:49 -06001705V. Modifying/Customizing libpng:
Guy Schalnat0d580581995-07-20 02:43:20 -05001706
1707There are two issues here. The first is changing how libpng does
1708standard things like memory allocation, input/output, and error handling.
1709The second deals with more complicated things like adding new chunks,
1710adding new transformations, and generally changing how libpng works.
1711
Andreas Dilger47a0c421997-05-16 02:46:07 -05001712All of the memory allocation, input/output, and error handling in libpng
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001713goes through callbacks that are user settable. The default routines are
Andreas Dilger47a0c421997-05-16 02:46:07 -05001714in pngmem.c, pngrio.c, pngwio.c, and pngerror.c respectively. To change
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001715these functions, call the appropriate png_set_???_fn() function.
Guy Schalnat0d580581995-07-20 02:43:20 -05001716
Guy Schalnat6d764711995-12-19 03:22:19 -06001717Memory allocation is done through the functions png_large_malloc(),
Guy Schalnate5a37791996-06-05 15:50:50 -05001718png_malloc(), png_realloc(), png_large_free(), and png_free(). These
1719currently just call the standard C functions. The large functions must
1720handle exactly 64K, but they don't have to handle more than that. If
Andreas Dilger47a0c421997-05-16 02:46:07 -05001721your pointers can't access more then 64K at a time, you will want to set
1722MAXSEG_64K in zlib.h. Since it is unlikely that the method of handling
1723memory allocation on a platform will change between applications, these
1724functions must be modified in the library at compile time.
Guy Schalnat0d580581995-07-20 02:43:20 -05001725
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001726Input/Output in libpng is done through png_read() and png_write(),
Andreas Dilger47a0c421997-05-16 02:46:07 -05001727which currently just call fread() and fwrite(). The FILE * is stored in
1728png_struct and is initialized via png_init_io(). If you wish to change
1729the method of I/O, the library supplies callbacks that you can set
1730through the function png_set_read_fn() and png_set_write_fn() at run
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001731time, instead of calling the png_init_io() function.
1732These functions
Andreas Dilger47a0c421997-05-16 02:46:07 -05001733also provide a void pointer that can be retrieved via the function
Guy Schalnat6d764711995-12-19 03:22:19 -06001734png_get_io_ptr(). For example:
Guy Schalnat0d580581995-07-20 02:43:20 -05001735
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001736 png_set_read_fn(png_structp read_ptr,
1737 voidp read_io_ptr, png_rw_ptr read_data_fn)
Guy Schalnat0f716451995-11-28 11:22:13 -06001738
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001739 png_set_write_fn(png_structp write_ptr,
1740 voidp write_io_ptr, png_rw_ptr write_data_fn,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001741 png_flush_ptr output_flush_fn);
Guy Schalnat0f716451995-11-28 11:22:13 -06001742
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001743 voidp read_io_ptr = png_get_io_ptr(read_ptr);
1744 voidp write_io_ptr = png_get_io_ptr(write_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001745
Guy Schalnat69b14481996-01-10 02:56:49 -06001746The replacement I/O functions should have prototypes as follows:
1747
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001748 void user_read_data(png_structp png_ptr,
1749 png_bytep data, png_uint_32 length);
1750 void user_write_data(png_structp png_ptr,
1751 png_bytep data, png_uint_32 length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001752 void user_flush_data(png_structp png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001753
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001754Supplying NULL for the read, write, or flush functions sets them back
1755to using the default C stream functions. It is an error to read from
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001756a write stream, and vice versa.
Guy Schalnat6d764711995-12-19 03:22:19 -06001757
1758Error handling in libpng is done through png_error() and png_warning().
1759Errors handled through png_error() are fatal, meaning that png_error()
Andreas Dilger47a0c421997-05-16 02:46:07 -05001760should never return to its caller. Currently, this is handled via
Guy Schalnat6d764711995-12-19 03:22:19 -06001761setjmp() and longjmp(), but you could change this to do things like
Guy Schalnat69b14481996-01-10 02:56:49 -06001762exit() if you should wish. On non-fatal errors, png_warning() is called
1763to print a warning message, and then control returns to the calling code.
Guy Schalnate5a37791996-06-05 15:50:50 -05001764By default png_error() and png_warning() print a message on stderr via
1765fprintf() unless the library is compiled with PNG_NO_STDIO defined. If
Guy Schalnat69b14481996-01-10 02:56:49 -06001766you wish to change the behavior of the error functions, you will need to
Guy Schalnate5a37791996-06-05 15:50:50 -05001767set up your own message callbacks. These functions are normally supplied
1768at the time that the png_struct is created. It is also possible to change
1769these functions after png_create_???_struct() has been called by calling:
Guy Schalnat69b14481996-01-10 02:56:49 -06001770
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001771 png_set_error_fn(png_structp png_ptr,
1772 png_voidp error_ptr, png_error_ptr error_fn,
1773 png_error_ptr warning_fn);
Guy Schalnat69b14481996-01-10 02:56:49 -06001774
Guy Schalnate5a37791996-06-05 15:50:50 -05001775 png_voidp error_ptr = png_get_error_ptr(png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001776
Guy Schalnate5a37791996-06-05 15:50:50 -05001777If NULL is supplied for either error_fn or warning_fn, then the libpng
1778default function will be used, calling fprintf() and/or longjmp() if a
1779problem is encountered. The replacement error functions should have
1780parameters as follows:
Guy Schalnat69b14481996-01-10 02:56:49 -06001781
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001782 void user_error_fn(png_structp png_ptr,
1783 png_const_charp error_msg);
1784 void user_warning_fn(png_structp png_ptr,
1785 png_const_charp warning_msg);
Guy Schalnat69b14481996-01-10 02:56:49 -06001786
1787The motivation behind using setjmp() and longjmp() is the C++ throw and
1788catch exception handling methods. This makes the code much easier to write,
1789as there is no need to check every return code of every function call.
1790However, there are some uncertainties about the status of local variables
1791after a longjmp, so the user may want to be careful about doing anything after
Andreas Dilger47a0c421997-05-16 02:46:07 -05001792setjmp returns non-zero besides returning itself. Consult your compiler
Guy Schalnat69b14481996-01-10 02:56:49 -06001793documentation for more details.
Guy Schalnat0f716451995-11-28 11:22:13 -06001794
Guy Schalnat0d580581995-07-20 02:43:20 -05001795If you need to read or write custom chunks, you will need to get deeper
Guy Schalnate5a37791996-06-05 15:50:50 -05001796into the libpng code, as a mechanism has not yet been supplied for user
1797callbacks with custom chunks. First, read the PNG specification, and have
1798a first level of understanding of how it works. Pay particular attention
1799to the sections that describe chunk names, and look at how other chunks
1800were designed, so you can do things similarly. Second, check out the
1801sections of libpng that read and write chunks. Try to find a chunk that
1802is similar to yours and copy off of it. More details can be found in the
1803comments inside the code. A way of handling unknown chunks in a generic
1804method, potentially via callback functions, would be best.
Guy Schalnat0d580581995-07-20 02:43:20 -05001805
Guy Schalnate5a37791996-06-05 15:50:50 -05001806If you wish to write your own transformation for the data, look through
1807the part of the code that does the transformations, and check out some of
1808the simpler ones to get an idea of how they work. Try to find a similar
1809transformation to the one you want to add and copy off of it. More details
1810can be found in the comments inside the code itself.
Guy Schalnat0d580581995-07-20 02:43:20 -05001811
1812Configuring for 16 bit platforms:
1813
Guy Schalnate5a37791996-06-05 15:50:50 -05001814You may need to change the png_large_malloc() and png_large_free()
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001815routines in pngmem.c, as these are required to allocate 64K, although
Guy Schalnate5a37791996-06-05 15:50:50 -05001816there is already support for many of the common DOS compilers. Also,
1817you will want to look into zconf.h to tell zlib (and thus libpng) that
1818it cannot allocate more then 64K at a time. Even if you can, the memory
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001819won't be accessible. So limit zlib and libpng to 64K by defining MAXSEG_64K.
Guy Schalnat0d580581995-07-20 02:43:20 -05001820
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001821Configuring for DOS:
1822
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001823For DOS users who only have access to the lower 640K, you will
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001824have to limit zlib's memory usage via a png_set_compression_mem_level()
1825call. See zlib.h or zconf.h in the zlib library for more information.
1826
Guy Schalnat6d764711995-12-19 03:22:19 -06001827Configuring for Medium Model:
1828
1829Libpng's support for medium model has been tested on most of the popular
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001830compilers. Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets
Guy Schalnat69b14481996-01-10 02:56:49 -06001831defined, and FAR gets defined to far in pngconf.h, and you should be
Guy Schalnat6d764711995-12-19 03:22:19 -06001832all set. Everything in the library (except for zlib's structure) is
1833expecting far data. You must use the typedefs with the p or pp on
1834the end for pointers (or at least look at them and be careful). Make
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001835note that the row's of data are defined as png_bytepp, which is an
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001836unsigned char far * far *.
Guy Schalnat6d764711995-12-19 03:22:19 -06001837
Guy Schalnat0d580581995-07-20 02:43:20 -05001838Configuring for gui/windowing platforms:
1839
Guy Schalnate5a37791996-06-05 15:50:50 -05001840You will need to write new error and warning functions that use the GUI
1841interface, as described previously, and set them to be the error and
1842warning functions at the time that png_create_???_struct() is called,
1843in order to have them available during the structure initialization.
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001844They can be changed later via png_set_error_fn(). On some compilers,
Guy Schalnate5a37791996-06-05 15:50:50 -05001845you may also have to change the memory allocators (png_malloc, etc.).
Guy Schalnat6d764711995-12-19 03:22:19 -06001846
Guy Schalnat0d580581995-07-20 02:43:20 -05001847Configuring for compiler xxx:
1848
Guy Schalnat69b14481996-01-10 02:56:49 -06001849All includes for libpng are in pngconf.h. If you need to add/change/delete
Guy Schalnat0d580581995-07-20 02:43:20 -05001850an include, this is the place to do it. The includes that are not
1851needed outside libpng are protected by the PNG_INTERNAL definition,
1852which is only defined for those routines inside libpng itself. The
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001853files in libpng proper only include png.h, which includes pngconf.h.
1854
1855Configuring zlib:
1856
Guy Schalnate5a37791996-06-05 15:50:50 -05001857There are special functions to configure the compression. Perhaps the
1858most useful one changes the compression level, which currently uses
1859input compression values in the range 0 - 9. The library normally
Andreas Dilger47a0c421997-05-16 02:46:07 -05001860uses the default compression level (Z_DEFAULT_COMPRESSION = 6). Tests
1861have shown that for a large majority of images, compression values in
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001862the range 3-6 compress nearly as well as higher levels, and do so much
1863faster. For online applications it may be desirable to have maximum speed
Guy Schalnate5a37791996-06-05 15:50:50 -05001864(Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also
1865specify no compression (Z_NO_COMPRESSION = 0), but this would create
1866files larger than just storing the raw bitmap. You can specify the
1867compression level by calling:
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001868
Andreas Dilger47a0c421997-05-16 02:46:07 -05001869 png_set_compression_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001870
1871Another useful one is to reduce the memory level used by the library.
1872The memory level defaults to 8, but it can be lowered if you are
1873short on memory (running DOS, for example, where you only have 640K).
1874
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001875 png_set_compression_mem_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001876
Andreas Dilger47a0c421997-05-16 02:46:07 -05001877The other functions are for configuring zlib. They are not recommended
1878for normal use and may result in writing an invalid PNG file. See
1879zlib.h for more information on what these mean.
1880
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001881 png_set_compression_strategy(png_ptr,
1882 strategy);
1883 png_set_compression_window_bits(png_ptr,
1884 window_bits);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001885 png_set_compression_method(png_ptr, method);
1886
1887Controlling row filtering:
1888
1889If you want to control whether libpng uses filtering or not, which
1890filters are used, and how it goes about picking row filters, you
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001891can call one of these functions. The selection and configuration
1892of row filters can have a significant impact on the size and
1893encoding speed and a somewhat lesser impact on the decoding speed
1894of an image. Filtering is enabled by default for RGB and grayscale
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001895images (with and without alpha), but not for paletted images nor
1896for any images with bit depths less than 8 bits/pixel.
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001897
1898The 'method' parameter sets the main filtering method, which is
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001899currently only '0' in the PNG 1.2 specification. The 'filters'
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001900parameter sets which filter(s), if any, should be used for each
1901scanline. Possible values are PNG_ALL_FILTERS and PNG_NO_FILTERS
1902to turn filtering on and off, respectively.
1903
Guy Schalnate5a37791996-06-05 15:50:50 -05001904Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB,
1905PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise
1906ORed together '|' to specify one or more filters to use. These
1907filters are described in more detail in the PNG specification. If
1908you intend to change the filter type during the course of writing
1909the image, you should start with flags set for all of the filters
1910you intend to use so that libpng can initialize its internal
1911structures appropriately for all of the filter types.
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001912
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001913 filters = PNG_FILTER_NONE | PNG_FILTER_SUB
1914 | PNG_FILTER_UP;
1915 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE,
1916 filters);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001917
Andreas Dilger47a0c421997-05-16 02:46:07 -05001918It is also possible to influence how libpng chooses from among the
1919available filters. This is done in two ways - by telling it how
1920important it is to keep the same filter for successive rows, and
1921by telling it the relative computational costs of the filters.
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001922
Andreas Dilger47a0c421997-05-16 02:46:07 -05001923 double weights[3] = {1.5, 1.3, 1.1},
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001924 costs[PNG_FILTER_VALUE_LAST] =
1925 {1.0, 1.3, 1.3, 1.5, 1.7};
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001926
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001927 png_set_filter_selection(png_ptr,
1928 PNG_FILTER_SELECTION_WEIGHTED, 3,
1929 weights, costs);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001930
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001931The weights are multiplying factors that indicate to libpng that the
1932row filter should be the same for successive rows unless another row filter
1933is that many times better than the previous filter. In the above example,
1934if the previous 3 filters were SUB, SUB, NONE, the SUB filter could have a
Andreas Dilger47a0c421997-05-16 02:46:07 -05001935"sum of absolute differences" 1.5 x 1.3 times higher than other filters
1936and still be chosen, while the NONE filter could have a sum 1.1 times
1937higher than other filters and still be chosen. Unspecified weights are
1938taken to be 1.0, and the specified weights should probably be declining
1939like those above in order to emphasize recent filters over older filters.
1940
1941The filter costs specify for each filter type a relative decoding cost
1942to be considered when selecting row filters. This means that filters
1943with higher costs are less likely to be chosen over filters with lower
1944costs, unless their "sum of absolute differences" is that much smaller.
1945The costs do not necessarily reflect the exact computational speeds of
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001946the various filters, since this would unduly influence the final image
Andreas Dilger47a0c421997-05-16 02:46:07 -05001947size.
1948
1949Note that the numbers above were invented purely for this example and
1950are given only to help explain the function usage. Little testing has
1951been done to find optimum values for either the costs or the weights.
Guy Schalnat0d580581995-07-20 02:43:20 -05001952
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001953Removing unwanted object code:
1954
Guy Schalnat69b14481996-01-10 02:56:49 -06001955There are a bunch of #define's in pngconf.h that control what parts of
Andreas Dilger47a0c421997-05-16 02:46:07 -05001956libpng are compiled. All the defines end in _SUPPORTED. If you are
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001957never going to use a capability, you can change the #define to #undef
1958before recompiling libpng and save yourself code and data space, or
1959you can turn off individual capabilities with defines that begin with
1960PNG_NO_.
1961
1962You can also turn all of the transforms and ancillary chunk capabilities
1963off en masse with compiler directives that define
1964PNG_NO_READ[or WRITE]_TRANSFORMS, or PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS,
1965or all four,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001966along with directives to turn on any of the capabilities that you do
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001967want. The PNG_NO_READ[or WRITE]_TRANSFORMS directives disable
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001968the extra transformations but still leave the library fully capable of reading
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001969and writing PNG files with all known public chunks [except for sPLT].
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001970Use of the PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS directive
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001971produces a library that is incapable of reading or writing ancillary chunks.
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001972If you are not using the progressive reading capability, you can
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001973turn that off with PNG_NO_PROGRESSIVE_READ (don't confuse
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001974this with the INTERLACING capability, which you'll still have).
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001975
1976All the reading and writing specific code are in separate files, so the
Guy Schalnate5a37791996-06-05 15:50:50 -05001977linker should only grab the files it needs. However, if you want to
1978make sure, or if you are building a stand alone library, all the
1979reading files start with pngr and all the writing files start with
1980pngw. The files that don't match either (like png.c, pngtrans.c, etc.)
1981are used for both reading and writing, and always need to be included.
1982The progressive reader is in pngpread.c
1983
1984If you are creating or distributing a dynamically linked library (a .so
Andreas Dilger47a0c421997-05-16 02:46:07 -05001985or DLL file), you should not remove or disable any parts of the library,
1986as this will cause applications linked with different versions of the
1987library to fail if they call functions not available in your library.
1988The size of the library itself should not be an issue, because only
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001989those sections that are actually used will be loaded into memory.
Guy Schalnate5a37791996-06-05 15:50:50 -05001990
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001991Requesting debug printout:
Guy Schalnate5a37791996-06-05 15:50:50 -05001992
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001993The macro definition PNG_DEBUG can be used to request debugging
1994printout. Set it to an integer value in the range 0 to 3. Higher
1995numbers result in increasing amounts of debugging information. The
1996information is printed to the "stderr" file, unless another file
1997name is specified in the PNG_DEBUG_FILE macro definition.
1998
1999When PNG_DEBUG > 0, the following functions (macros) become available:
2000
2001 png_debug(level, message)
2002 png_debug1(level, message, p1)
2003 png_debug2(level, message, p1, p2)
2004
2005in which "level" is compared to PNG_DEBUG to decide whether to print
2006the message, "message" is the formatted string to be printed,
2007and p1 and p2 are parameters that are to be embedded in the string
2008according to printf-style formatting directives. For example,
2009
2010 png_debug1(2, "foo=%d\n", foo);
2011
2012is expanded to
2013
2014 if(PNG_DEBUG > 2)
2015 fprintf(PNG_DEBUG_FILE, "foo=%d\n", foo);
2016
2017When PNG_DEBUG is defined but is zero, the macros aren't defined, but you
2018can still use PNG_DEBUG to control your own debugging:
2019
2020 #ifdef PNG_DEBUG
2021 fprintf(stderr, ...
2022 #endif
2023
2024When PNG_DEBUG = 1, the macros are defined, but only png_debug statements
2025having level = 0 will be printed. There aren't any such statements in
2026this version of libpng, but if you insert some they will be printed.
2027
2028VI. Changes to Libpng from version 0.88
Guy Schalnate5a37791996-06-05 15:50:50 -05002029
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002030It should be noted that versions of libpng later than 0.96 are not
2031distributed by the original libpng author, Guy Schalnat, nor by
2032Andreas Dilger, who had taken over from Guy during 1996 and 1997, and
2033distributed versions 0.89 through 0.96, but rather by another member
2034of the original PNG Group, Glenn Randers-Pehrson. Guy and Andreas are
2035still alive and well, but they have moved on to other things.
Andreas Dilger47a0c421997-05-16 02:46:07 -05002036
2037The old libpng functions png_read_init(), png_write_init(),
2038png_info_init(), png_read_destroy(), and png_write_destory() have been
2039moved to PNG_INTERNAL in version 0.95 to discourage their use. The
2040preferred method of creating and initializing the libpng structures is
2041via the png_create_read_struct(), png_create_write_struct(), and
Guy Schalnate5a37791996-06-05 15:50:50 -05002042png_create_info_struct() because they isolate the size of the structures
Andreas Dilger47a0c421997-05-16 02:46:07 -05002043from the application, allow version error checking, and also allow the
2044use of custom error handling routines during the initialization, which
2045the old functions do not. The functions png_read_destroy() and
2046png_write_destroy() do not actually free the memory that libpng
2047allocated for these structs, but just reset the data structures, so they
2048can be used instead of png_destroy_read_struct() and
2049png_destroy_write_struct() if you feel there is too much system overhead
2050allocating and freeing the png_struct for each image read.
Guy Schalnate5a37791996-06-05 15:50:50 -05002051
2052Setting the error callbacks via png_set_message_fn() before
2053png_read_init() as was suggested in libpng-0.88 is no longer supported
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002054because this caused applications that do not use custom error functions
Guy Schalnate5a37791996-06-05 15:50:50 -05002055to fail if the png_ptr was not initialized to zero. It is still possible
2056to set the error callbacks AFTER png_read_init(), or to change them with
2057png_set_error_fn(), which is essentially the same function, but with a
Andreas Dilger47a0c421997-05-16 02:46:07 -05002058new name to force compilation errors with applications that try to use
2059the old method.
Glenn Randers-Pehrson87544ac1999-01-13 22:06:39 -06002060
2061VII. Y2K Compliance in libpng
2062
2063January 13, 1999
2064
2065Since the PNG Development group is an ad-hoc body, we can't make
2066an official declaration.
2067
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002068This is your unofficial assurance that libpng from version 0.71 and
2069upward through 1.0.4 are Y2K compliant. It is my belief that earlier
2070versions were also Y2K compliant.
Glenn Randers-Pehrson87544ac1999-01-13 22:06:39 -06002071
2072Libpng only has three year fields. One is a 2-byte unsigned integer that
2073will hold years up to 65535. The other two hold the date in text
2074format, and will hold years up to 9999.
2075
2076The integer is
2077 "png_uint_16 year" in png_time_struct.
2078
2079The strings are
2080 "png_charp time_buffer" in png_struct and
2081 "near_time_buffer", which is a local character string in png.c.
2082
2083There are seven time-related functions:
2084
2085 png_convert_to_rfc_1123() in png.c
2086 (formerly png_convert_to_rfc_1152() in error)
2087 png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c
2088 png_convert_from_time_t() in pngwrite.c
2089 png_get_tIME() in pngget.c
2090 png_handle_tIME() in pngrutil.c, called in pngread.c
2091 png_set_tIME() in pngset.c
2092 png_write_tIME() in pngwutil.c, called in pngwrite.c
2093
2094All appear to handle dates properly in a Y2K environment. The
2095png_convert_from_time_t() function calls gmtime() to convert from system
2096clock time, which returns (year - 1900), which we properly convert to
2097the full 4-digit year. There is a possibility that applications using
2098libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002099function, or that they are incorrectly passing only a 2-digit year
2100instead of "year - 1900" into the png_convert_from_struct_tm() function,
2101but this is not under our control. The libpng documentation has always
2102stated that it works with 4-digit years, and the APIs have been
2103documented as such.
Glenn Randers-Pehrson87544ac1999-01-13 22:06:39 -06002104
2105The tIME chunk itself is also Y2K compliant. It uses a 2-byte unsigned
2106integer to hold the year, and can hold years as large as 65535.
2107
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002108zlib, upon which libpng depends, is also Y2K compliant. It contains
2109no date-related code.
2110
Glenn Randers-Pehrson87544ac1999-01-13 22:06:39 -06002111
2112 Glenn Randers-Pehrson
2113 libpng maintainer
2114 PNG Development Group