blob: 602efd87269452022e0e4756f6021df3d8d8ad67 [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-Pehrson8f8fb6a1998-03-09 23:02:06 -06003 libpng version 1.0.0a March 9, 1998
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06004 Updated and distributed by Glenn Randers-Pehrson
5 <randeg@alumni.rpi.edu>
6 Copyright (c) 1998, 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-Pehrsoncbe52d81998-02-28 07:00:24 -060013 Updated and distributed by Andreas Dilger
14 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-Pehrsoncbe52d81998-02-28 07:00:24 -060025Introduction
Guy Schalnat0d580581995-07-20 02:43:20 -050026
27This file describes how to use and modify the PNG reference library
Guy Schalnat69b14481996-01-10 02:56:49 -060028(known as libpng) for your own use. There are five sections to this
29file: introduction, structures, reading, writing, and modification and
Guy Schalnat4ee97b01996-01-16 01:51:56 -060030configuration notes for various special platforms. In addition to this
Guy Schalnat69b14481996-01-10 02:56:49 -060031file, example.c is a good starting point for using the library, as
32it is heavily commented and should include everything most people
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060033will need. We assume that libpng is already installed; see the
34INSTALL file for instructions on how to install libpng.
Guy Schalnat0d580581995-07-20 02:43:20 -050035
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060036Libpng was written as a companion to the PNG specification, as a way
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060037of reducing the amount of time and effort it takes to support the PNG
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060038file format in application programs. The PNG specification is available
39as RFC 2083 <ftp://ftp.uu.net/graphics/png/documents/> and as a
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060040W3C Recommendation <http://www.w3.org/TR/REC.png.html>. Some
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060041additional chunks are described in the special-purpose public chunks
42documents at <ftp://ftp.uu.net/graphics/png/documents/>. Other information
43about PNG can be found at the PNG home page, <http://www.cdrom.com/pub/png/>.
44
45Most users will not have to modify the library significantly; advanced
46users may want to modify it more. All attempts were made to make it as
47complete as possible, while keeping the code easy to understand.
48Currently, this library only supports C. Support for other languages
49is being considered.
Guy Schalnat0d580581995-07-20 02:43:20 -050050
51Libpng has been designed to handle multiple sessions at one time,
52to be easily modifiable, to be portable to the vast majority of
Andreas Dilger47a0c421997-05-16 02:46:07 -050053machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy
54to use. The ultimate goal of libpng is to promote the acceptance of
Guy Schalnat0d580581995-07-20 02:43:20 -050055the PNG file format in whatever way possible. While there is still
Andreas Dilger47a0c421997-05-16 02:46:07 -050056work to be done (see the TODO file), libpng should cover the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060057majority of the needs of its users.
Guy Schalnat0d580581995-07-20 02:43:20 -050058
59Libpng uses zlib for its compression and decompression of PNG files.
60The zlib compression utility is a general purpose utility that is
Guy Schalnat4ee97b01996-01-16 01:51:56 -060061useful for more than PNG files, and can be used without libpng.
Guy Schalnat69b14481996-01-10 02:56:49 -060062See the documentation delivered with zlib for more details.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060063You can usually find the source files for the zlib utility wherever you
64find the libpng source files.
Guy Schalnat0d580581995-07-20 02:43:20 -050065
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060066Libpng is thread safe, provided the threads are using different
Guy Schalnat4ee97b01996-01-16 01:51:56 -060067instances of the structures. Each thread should have its own
68png_struct and png_info instances, and thus its own image.
69Libpng does not protect itself against two threads using the
70same instance of a structure.
71
Guy Schalnat0d580581995-07-20 02:43:20 -050072
Guy Schalnat69b14481996-01-10 02:56:49 -060073II. Structures
Guy Schalnat0d580581995-07-20 02:43:20 -050074
75There are two main structures that are important to libpng, png_struct
76and png_info. The first, png_struct, is an internal structure that
Guy Schalnate5a37791996-06-05 15:50:50 -050077will not, for the most part, be used by a user except as the first
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060078variable passed to every libpng function call.
Guy Schalnat0d580581995-07-20 02:43:20 -050079
80The png_info structure is designed to provide information about the
Andreas Dilger47a0c421997-05-16 02:46:07 -050081PNG file. At one time, the fields of png_info were intended to be
82directly accessible to the user. However, this tended to cause problems
83with applications using dynamically loaded libraries, and as a result
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060084a set of interface functions for png_info was developed. The fields
Andreas Dilger47a0c421997-05-16 02:46:07 -050085of png_info are still available for older applications, but it is
86suggested that applications use the new interfaces if at all possible.
Guy Schalnat0d580581995-07-20 02:43:20 -050087
Andreas Dilger47a0c421997-05-16 02:46:07 -050088The png.h header file is an invaluable reference for programming with libpng.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060089And while I'm on the topic, make sure you include the libpng header file:
Guy Schalnat0d580581995-07-20 02:43:20 -050090
91#include <png.h>
92
Guy Schalnat69b14481996-01-10 02:56:49 -060093III. Reading
94
Guy Schalnat0d580581995-07-20 02:43:20 -050095Reading PNG files:
96
Guy Schalnat69b14481996-01-10 02:56:49 -060097We'll now walk you through the possible functions to call when reading
98in a PNG file, briefly explaining the syntax and purpose of each one.
99See example.c and png.h for more detail. While Progressive reading
100is covered in the next section, you will still need some of the
101functions discussed in this section to read a PNG file.
Guy Schalnat6d764711995-12-19 03:22:19 -0600102
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600103You will want to do the I/O initialization(*) before you get into libpng,
104so if it doesn't work, you don't have much to undo. Of course, you
105will also want to insure that you are, in fact, dealing with a PNG
106file. Libpng provides a simple check to see if a file is a PNG file.
107To use it, pass in the first 1 to 8 bytes of the file, and it will
108return true or false (1 or 0) depending on whether the bytes could be
109part of a PNG file. Of course, the more bytes you pass in, the
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600110greater the accuracy of the prediction.
111
112If you are intending to keep the file pointer open for use in libpng,
113you must ensure you don't read more than 8 bytes from the beginning
114of the file, and you also have to make a call to png_set_sig_bytes_read()
115with the number of bytes you read from the beginning. Libpng will
116then only check the bytes (if any) that your program didn't read.
Guy Schalnat0d580581995-07-20 02:43:20 -0500117
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600118(*): If you are not using the standard I/O functions, you will need
Guy Schalnat69b14481996-01-10 02:56:49 -0600119to replace them with custom functions. See the discussion under
120Customizing libpng.
121
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600122
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600123 FILE *fp = fopen(file_name, "rb");
124 if (!fp)
125 {
126 return;
127 }
128 fread(header, 1, number, fp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600129 is_png = png_check_sig(header, 0, number);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600130 if (!is_png)
131 {
132 return;
133 }
134
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600135
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136Next, png_struct and png_info need to be allocated and initialized. In
137order to ensure that the size of these structures is correct even with a
138dynamically linked libpng, there are functions to initialize and
139allocate the structures. We also pass the library version, optional
140pointers to error handling functions, and a pointer to a data struct for
141use by the error functions, if necessary (the pointer and functions can
142be NULL if the default error handlers are to be used). See the section
143on Changes to Libpng below regarding the old initialization functions.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600144
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600145 png_structp png_ptr = png_create_read_struct
146 (PNG_LIBPNG_VER_STRING, (void *)user_error_ptr,
147 user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600148 if (!png_ptr)
149 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150
Guy Schalnate5a37791996-06-05 15:50:50 -0500151 png_infop info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600152 if (!info_ptr)
153 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600154 png_destroy_read_struct(&png_ptr,
155 (png_infopp)NULL, (png_infopp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500156 return;
157 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500158
Guy Schalnate5a37791996-06-05 15:50:50 -0500159 png_infop end_info = png_create_info_struct(png_ptr);
160 if (!end_info)
161 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600162 png_destroy_read_struct(&png_ptr, &info_ptr,
163 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600164 return;
165 }
166
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600167
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600168The error handling routines passed to png_create_read_struct() are only
169necessary if you are not using the libpng supplied error handling
170functions. When libpng encounters an error, it expects to longjmp back
171to your routine. Therefore, you will need to call setjmp and pass the
172jmpbuf field of your png_struct. If you read the file from different
173routines, you will need to update the jmpbuf field every time you enter
174a new routine that will call a png_ function.
175
176See your documentation of setjmp/longjmp for your compiler for more
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600177handling in the Customizing Libpng section below for more information on
178the libpng error handling. If an error occurs, and libpng longjmp's
179back to your setjmp, you will want to call png_destroy_read_struct() to
180free any memory.
Guy Schalnat0d580581995-07-20 02:43:20 -0500181
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 if (setjmp(png_ptr->jmpbuf))
183 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600184 png_destroy_read_struct(&png_ptr, &info_ptr,
185 &end_info);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600186 fclose(fp);
187 return;
188 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500189
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600190Now you need to set up the input code. The default for libpng is to
191use the C function fread(). If you use this, you will need to pass a
192valid FILE * in the function png_init_io(). Be sure that the file is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193opened in binary mode. If you wish to handle reading data in another
194way, you need not call the png_init_io() function, but you must then
195implement the libpng I/O methods discussed in the Customizing Libpng
196section below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500197
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600198 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500199
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600200If you had previously opened the file and read any of the signature from
201the beginning in order to see if this was a PNG file, you need to let
202libpng know that there are some bytes missing from the start of the file.
203
204 png_set_sig_bytes(png_ptr, number);
205
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600206At this point, you can set up a callback function that will be
207called after each row has been read, which you can use to control
208a progress meter or the like. It's demonstrated in pngtest.c.
209You must supply a function
210
211 void read_row_callback(png_ptr, png_uint_32 row, int pass);
212 {
213 /* put your code here */
214 }
215
216(You can give it another name that you like instead of "read_row_callback")
217
218To inform libpng about your function, use
219
220 png_set_read_status_fn(png_ptr, read_row_callback);
221
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600222In PNG files, the alpha channel in an image is the level of opacity.
223If you need the alpha channel in an image to be the level of transparency
224instead of opacity, you can invert the alpha channel (or the tRNS chunk
225data) after it's read, so that 0 is fully opaque and 255 (in 8-bit or
226paletted images) or 65535 (in 16-bit images) is fully transparent, with
227
228 png_set_invert_alpha(png_ptr);
229
230This has to appear here rather than later with the other transformations
231because the tRNS chunk data must be modified in the case of paletted images.
232If your image is not a paletted image, the tRNS data (which in such cases
233represents a single color to be rendered as transparent) won't be changed.
234
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600235Finally, you can write your own transformation function if none of
236the existing ones meets your needs. This is done by setting a callback
237with
238
239 png_set_read_user_transform_fn(png_ptr,
240 read_transform_fn);
241
242You must supply the function
243
244 void read_transform_fn(png_ptr ptr, row_info_ptr
245 row_info, png_bytep data)
246
247See pngtest.c for a working example. Your function will be called
248after all of the other transformations have been processed.
249
Guy Schalnat0d580581995-07-20 02:43:20 -0500250You are now ready to read all the file information up to the actual
251image data. You do this with a call to png_read_info().
252
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600253 png_read_info(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500254
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255Functions are used to get the information from the info_ptr:
Guy Schalnat69b14481996-01-10 02:56:49 -0600256
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600257 png_get_IHDR(png_ptr, info_ptr, &width, &height,
258 &bit_depth, &color_type, &interlace_type,
259 &compression_type, &filter_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600261 width - holds the width of the image
262 in pixels (up to 2^31).
263 height - holds the height of the image
264 in pixels (up to 2^31).
265 bit_depth - holds the bit depth of one of the
266 image channels. (valid values are
267 1, 2, 4, 8, 16 and depend also on
268 the color_type. See also
269 significant bits (sBIT) below).
270 color_type - describes which color/alpha channels
271 are present.
272 PNG_COLOR_TYPE_GRAY
273 (bit depths 1, 2, 4, 8, 16)
274 PNG_COLOR_TYPE_GRAY_ALPHA
275 (bit depths 8, 16)
276 PNG_COLOR_TYPE_PALETTE
277 (bit depths 1, 2, 4, 8)
278 PNG_COLOR_TYPE_RGB
279 (bit_depths 8, 16)
280 PNG_COLOR_TYPE_RGB_ALPHA
281 (bit_depths 8, 16)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282
283 PNG_COLOR_MASK_PALETTE
284 PNG_COLOR_MASK_COLOR
285 PNG_COLOR_MASK_ALPHA
286
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600287 filter_type - (must be PNG_FILTER_TYPE_BASE
288 for PNG 1.0)
289 compression_type - (must be PNG_COMPRESSION_TYPE_BASE
290 for PNG 1.0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600291 interlace_type - (PNG_INTERLACE_NONE or
292 PNG_INTERLACE_ADAM7)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600293 Any or all of interlace_type, compression_type, of
294 filter_type can be
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600295 NULL if you are not interested in their values.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296
297 channels = png_get_channels(png_ptr, info_ptr);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600298 channels - number of channels of info for the
299 color type (valid values are 1 (GRAY,
300 PALETTE), 2 (GRAY_ALPHA), 3 (RGB),
301 4 (RGB_ALPHA or RGB + filler byte))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600303 rowbytes - number of bytes needed to hold a row
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304
305 signature = png_get_signature(png_ptr, info_ptr);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600306 signature - holds the signature read from the
307 file (if any). The data is kept in
308 the same offset it would be if the
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600309 whole signature were read (i.e. if an
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600310 application had already read in 4
311 bytes of signature before starting
312 libpng, the remaining 4 bytes would
313 be in signature[4] through signature[7]
314 (see png_set_sig_bytes())).
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600315
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600316
317 width = png_get_image_width(png_ptr,
318 info_ptr);
319 height = png_get_image_height(png_ptr,
320 info_ptr);
321 bit_depth = png_get_bit_depth(png_ptr,
322 info_ptr);
323 color_type = png_get_color_type(png_ptr,
324 info_ptr);
325 filter_type = png_get_filter_type(png_ptr,
326 info_ptr);
327 compression_type = png_get_compression_type(png_ptr,
328 info_ptr);
329 interlace_type = png_get_interlace_type(png_ptr,
330 info_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600331
Guy Schalnat69b14481996-01-10 02:56:49 -0600332
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333These are also important, but their validity depends on whether the chunk
334has been read. The png_get_valid(png_ptr, info_ptr, PNG_INFO_<chunk>) and
335png_get_<chunk>(png_ptr, info_ptr, ...) functions return non-zero if the
336data has been read, or zero if it is missing. The parameters to the
337png_get_<chunk> are set directly if they are simple data types, or a pointer
338into the info_ptr is returned for any complex types.
Guy Schalnat69b14481996-01-10 02:56:49 -0600339
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600340 png_get_PLTE(png_ptr, info_ptr, &palette,
341 &num_palette);
342 palette - the palette for the file
343 (array of png_color)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600344 num_palette - number of entries in the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345
346 png_get_gAMA(png_ptr, info_ptr, &gamma);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600347 gamma - the gamma the file is written
348 at (PNG_INFO_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600350 png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
351 srgb_intent - the rendering intent (PNG_INFO_sRGB)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600352 The presence of the sRGB chunk
353 means that the pixel data is in the
354 sRGB color space. This chunk also
355 implies specific values of gAMA and
356 cHRM.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600357
Andreas Dilger47a0c421997-05-16 02:46:07 -0500358 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600359 sig_bit - the number of significant bits for
360 (PNG_INFO_sBIT) each of the gray,
361 red, green, and blue channels,
362 whichever are appropriate for the
363 given color type (png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600365 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
366 &trans_values);
367 trans - array of transparent entries for
368 palette (PNG_INFO_tRNS)
369 trans_values - transparent pixel for non-paletted
370 images (PNG_INFO_tRNS)
371 num_trans - number of transparent entries
372 (PNG_INFO_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500373
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600374 png_get_hIST(png_ptr, info_ptr, &hist);
375 (PNG_INFO_hIST)
376 hist - histogram of palette (array of
377 png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378
379 png_get_tIME(png_ptr, info_ptr, &mod_time);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600380 mod_time - time image was last modified
381 (PNG_VALID_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382
383 png_get_bKGD(png_ptr, info_ptr, &background);
Guy Schalnate5a37791996-06-05 15:50:50 -0500384 background - background color (PNG_VALID_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385
386 num_text = png_get_text(png_ptr, info_ptr, &text_ptr);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600387 text_ptr - array of png_text holding image
388 comments
389 text_ptr[i]->key - keyword for comment.
390 text_ptr[i]->text - text comments for current
391 keyword.
392 text_ptr[i]->compression - type of compression used
393 on "text" PNG_TEXT_COMPRESSION_NONE
394 or PNG_TEXT_COMPRESSION_zTXt
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600395 num_text - number of comments
Guy Schalnat69b14481996-01-10 02:56:49 -0600396
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600397 png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y,
398 &unit_type);
399 offset_x - positive offset from the left edge
400 of the screen
401 offset_y - positive offset from the top edge
402 of the screen
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
404
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600405 png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y,
406 &unit_type);
407 res_x - pixels/unit physical resolution in
408 x direction
409 res_y - pixels/unit physical resolution in
410 x direction
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600411 unit_type - PNG_RESOLUTION_UNKNOWN,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600412 PNG_RESOLUTION_METER
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600414The data from the pHYs chunk can be retrieved in several convenient
415forms:
416
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600417 res_x = png_get_x_pixels_per_meter(png_ptr,
418 info_ptr)
419 res_y = png_get_y_pixels_per_meter(png_ptr,
420 info_ptr)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600421 res_x_and_y = png_get_pixels_per_meter(png_ptr,
422 info_ptr)
423 aspect_ratio = png_get_pixel_aspect_ratio(png_ptr,
424 info_ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600425
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600426 (Each of these returns 0 [signifying "unknown"] if
427 the data is not present or if res_x is 0;
428 res_x_and_y is 0 if res_x != res_y)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600429
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430For more information, see the png_info definition in png.h and the
Guy Schalnat0d580581995-07-20 02:43:20 -0500431PNG specification for chunk contents. Be careful with trusting
432rowbytes, as some of the transformations could increase the space
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433needed to hold a row (expand, filler, gray_to_rgb, etc.).
434See png_read_update_info(), below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500435
Andreas Dilger47a0c421997-05-16 02:46:07 -0500436A quick word about text_ptr and num_text. PNG stores comments in
437keyword/text pairs, one pair per chunk, with no limit on the number
438of text chunks, and a 2^31 byte limit on their size. While there are
439suggested keywords, there is no requirement to restrict the use to these
440strings. It is strongly suggested that keywords and text be sensible
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600441to humans (that's the point), so don't use abbreviations. Non-printing
442symbols are not allowed. See the PNG specification for more details.
443There is also no requirement to have text after the keyword.
Guy Schalnat0d580581995-07-20 02:43:20 -0500444
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445Keywords should be limited to 79 Latin-1 characters without leading or
446trailing spaces, but non-consecutive spaces are allowed within the
447keyword. It is possible to have the same keyword any number of times.
448The text_ptr is an array of png_text structures, each holding pointer
449to a keyword and a pointer to a text string. Only the text string may
450be null. The keyword/text pairs are put into the array in the order
451that they are received. However, some or all of the text chunks may be
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600452after the image, so, to make sure you have read all the text chunks,
Guy Schalnat0d580581995-07-20 02:43:20 -0500453don't mess with these until after you read the stuff after the image.
454This will be mentioned again below in the discussion that goes with
455png_read_end().
456
Andreas Dilger47a0c421997-05-16 02:46:07 -0500457After you've read the header information, you can set up the library
458to handle any special transformations of the image data. The various
Guy Schalnat0d580581995-07-20 02:43:20 -0500459ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600460should occur. This is important, as some of these change the color
461type and/or bit depth of the data, and some others only work on
462certain color types and bit depths. Even though each transformation
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600463checks to see if it has data that it can do something with, you should
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600464make sure to only enable a transformation if it will be valid for the
465data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500466
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600467The colors used for the background and transparency values should be
468supplied in the same format/depth as the current image data. They
469are stored in the same format/depth as the image data in a bKGD or tRNS
470chunk, so this is what libpng expects for this data. The colors are
471transformed to keep in sync with the image data when an application
Andreas Dilger47a0c421997-05-16 02:46:07 -0500472calls the png_read_update_info() routine (see below).
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600473
Guy Schalnate5a37791996-06-05 15:50:50 -0500474Data will be decoded into the supplied row buffers packed into bytes
475unless the library has been told to transform it into another format.
476For example, 4 bit/pixel paletted or grayscale data will be returned
4772 pixels/byte with the leftmost pixel in the high-order bits of the
478byte, unless png_set_packing() is called. 8-bit RGB data will be stored
479in RGBRGBRGB format unless png_set_filler() is called to insert filler
480bytes, either before or after each RGB triplet. 16-bit RGB data will
481be returned RRGGBBRRGGBB, with the most significant byte of the color
482value first, unless png_set_strip_16() is called to transform it to
483regular RGBRGB triplets.
484
485The following code transforms grayscale images of less than 8 to 8 bits,
486changes paletted images to RGB, and adds a full alpha channel if there is
487transparency information in a tRNS chunk. This is most useful on
488grayscale images with bit depths of 2 or 4 or if there is a multiple-image
489viewing application that wishes to treat all images in the same way.
Guy Schalnat0d580581995-07-20 02:43:20 -0500490
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600491 if (color_type == PNG_COLOR_TYPE_PALETTE &&
492 bit_depth <= 8) png_set_expand(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500493
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600494 if (color_type == PNG_COLOR_TYPE_GRAY &&
495 bit_depth < 8) png_set_expand(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500496
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600497 if (png_get_valid(png_ptr, info_ptr,
498 PNG_INFO_tRNS)) png_set_expand(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500499
Guy Schalnate5a37791996-06-05 15:50:50 -0500500PNG can have files with 16 bits per channel. If you only can handle
5018 bits per channel, this will strip the pixels down to 8 bit.
502
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503 if (bit_depth == 16)
504 png_set_strip_16(png_ptr);
505
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600506The png_set_background() function tells libpng to composite images
507with alpha or simple transparency against the supplied background
508color. If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
509you may use this color, or supply another color more suitable for
510the current display (e.g., the background color from a web page). You
511need to tell libpng whether the color is in the gamma space of the
512display (PNG_BACKGROUND_GAMMA_SCREEN for colors you supply), the file
513(PNG_BACKGROUND_GAMMA_FILE for colors from the bKGD chunk), or one
514that is neither of these gammas (PNG_BACKGROUND_GAMMA_UNIQUE - I don't
515know why anyone would use this, but it's here).
516
Andreas Dilger47a0c421997-05-16 02:46:07 -0500517If, for some reason, you don't need the alpha channel on an image,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600518and you want to remove it rather than combining it with the background
519(but the image author certainly had in mind that you *would* combine
520it with the background, so that's what you should probably do):
Andreas Dilger47a0c421997-05-16 02:46:07 -0500521
522 if (color_type & PNG_COLOR_MASK_ALPHA)
523 png_set_strip_alpha(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500524
525PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
526they can, resulting in, for example, 8 pixels per byte for 1 bit
527files. This code expands to 1 pixel per byte without changing the
528values of the pixels:
529
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530 if (bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500531 png_set_packing(png_ptr);
532
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixels
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600534stored in a PNG image have been "scaled" or "shifted" up to the next
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600535higher possible bit depth (e.g. from 5 bits/sample in the range [0,31] to
Andreas Dilger47a0c421997-05-16 02:46:07 -05005368 bits/sample in the range [0, 255]). However, it is also possible to
537convert the PNG pixel data back to the original bit depth of the image.
538This call reduces the pixels back down to the original bit depth:
Guy Schalnate5a37791996-06-05 15:50:50 -0500539
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 png_color_16p sig_bit;
Guy Schalnate5a37791996-06-05 15:50:50 -0500541
Andreas Dilger47a0c421997-05-16 02:46:07 -0500542 if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
543 png_set_shift(png_ptr, sig_bit);
544
545PNG files store 3-color pixels in red, green, blue order. This code
Guy Schalnate5a37791996-06-05 15:50:50 -0500546changes the storage of the pixels to blue, green, red:
547
Andreas Dilger47a0c421997-05-16 02:46:07 -0500548 if (color_type == PNG_COLOR_TYPE_RGB ||
549 color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnate5a37791996-06-05 15:50:50 -0500550 png_set_bgr(png_ptr);
551
552PNG files store RGB pixels packed into 3 bytes. This code expands them
553into 4 bytes for windowing systems that need them in this format:
554
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600555 if (bit_depth == 8 && color_type ==
556 PNG_COLOR_TYPE_RGB) png_set_filler(png_ptr,
557 filler, PNG_FILLER_BEFORE);
Guy Schalnate5a37791996-06-05 15:50:50 -0500558
Andreas Dilger47a0c421997-05-16 02:46:07 -0500559where "filler" is the number to fill with, and the location is
Guy Schalnate5a37791996-06-05 15:50:50 -0500560either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
Andreas Dilger47a0c421997-05-16 02:46:07 -0500561you want the filler before the RGB or after. This transformation
562does not affect images that already have full alpha channels.
Guy Schalnate5a37791996-06-05 15:50:50 -0500563
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564If you are reading an image with an alpha channel, and you need the
565data as ARGB instead of the normal PNG format RGBA:
566
567 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
568 png_set_swap_alpha(png_ptr);
569
570For some uses, you may want a grayscale image to be represented as
Guy Schalnate5a37791996-06-05 15:50:50 -0500571RGB. This code will do that conversion:
572
Andreas Dilger47a0c421997-05-16 02:46:07 -0500573 if (color_type == PNG_COLOR_TYPE_GRAY ||
574 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
575 png_set_gray_to_rgb(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500576
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577If you have a grayscale and you are using png_set_expand() to change to
578a higher bit-depth you must indicate if the supplied background gray
579is supplied in the original file bit depth (need_expand = 1) or in the
580expanded bit depth (need_expand = 0). Similarly, if you are reading
Guy Schalnate5a37791996-06-05 15:50:50 -0500581a paletted image, you must indicate if you have supplied the background
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582as a palette index that needs to be expanded (need_expand = 1). You can
583also specify an RGB triplet that isn't in the palette when setting your
584background for a paletted image.
Guy Schalnat0d580581995-07-20 02:43:20 -0500585
Andreas Dilger47a0c421997-05-16 02:46:07 -0500586 png_color_16 my_background;
587 png_color_16p image_background;
Guy Schalnat0d580581995-07-20 02:43:20 -0500588
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600589 if (png_get_bKGD(png_ptr, info_ptr,
590 &image_background))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 png_set_background(png_ptr, image_background),
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600592 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500593 else
594 png_set_background(png_ptr, &my_background,
595 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
596
597To properly display PNG images on any kind of system, the application needs
598to know what the display gamma is. Ideally, the user will know this, and
599the application will allow them to set it. One method of allowing the user
600to set the display gamma separately for each system is to check for the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600601DISPLAY_GAMMA and VIEWING_GAMMA environment variables or for a SCREEN_GAMMA
602environment variable, which will hopefully be correctly set.
603
604Note that display_gamma is the gamma of your display, while screen_gamma is
605the overall gamma correction required to produce pleasing results,
606which depends on the lighting conditions in the surrounding environment.
607Screen_gamma is display_gamma/viewing_gamma, where viewing_gamma is
608the amount of additional gamma correction needed to compensate for
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600609a (viewing_gamma=1.25) environment. In a dim or brightly lit room, no
610compensation other than the display_gamma is needed (viewing_gamma=1.0).
Andreas Dilger47a0c421997-05-16 02:46:07 -0500611
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600612 if (/* We have a user-defined screen
613 gamma value */)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500614 {
615 screen_gamma = user_defined_screen_gamma;
616 }
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600617 /* One way that applications can share the same
618 screen gamma value */
619 else if ((gamma_str = getenv("SCREEN_GAMMA"))
620 != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500621 {
622 screen_gamma = atof(gamma_str);
623 }
624 /* If we don't have another value */
Guy Schalnat0d580581995-07-20 02:43:20 -0500625 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500626 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600627 screen_gamma = 2.2; /* A good guess for a
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600628 PC monitor in a bright office or a dim room */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600629 screen_gamma = 2.0; /* A good guess for a
630 PC monitor in a dark room */
631 screen_gamma = 1.7 or 1.0; /* A good
632 guess for Mac systems */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500633 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500634
Andreas Dilger47a0c421997-05-16 02:46:07 -0500635The png_set_gamma() function handles gamma transformations of the data.
636Pass both the file gamma and the current screen_gamma. If the file does
637not have a gamma value, you can pass one anyway if you have an idea what
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600638it is (usually 0.50 is a good guess for GIF images on PCs). Note
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600639that file gammas are inverted from screen gammas. See the discussions
Andreas Dilger47a0c421997-05-16 02:46:07 -0500640on gamma in the PNG specification for an excellent description of what
641gamma is, and why all applications should support it. It is strongly
642recommended that PNG viewers support gamma correction.
Guy Schalnat0d580581995-07-20 02:43:20 -0500643
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644 if (png_get_gAMA(png_ptr, info_ptr, &gamma))
645 png_set_gamma(png_ptr, screen_gamma, gamma);
Guy Schalnate5a37791996-06-05 15:50:50 -0500646 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600647 png_set_gamma(png_ptr, screen_gamma, 0.50);
Guy Schalnat0d580581995-07-20 02:43:20 -0500648
Guy Schalnate5a37791996-06-05 15:50:50 -0500649If you need to reduce an RGB file to a paletted file, or if a paletted
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600650file has more entries then will fit on your screen, png_set_dither()
651will do that. Note that this is a simple match dither that merely
652finds the closest color available. This should work fairly well with
653optimized palettes, and fairly badly with linear color cubes. If you
654pass a palette that is larger then maximum_colors, the file will
655reduce the number of colors in the palette so it will fit into
656maximum_colors. If there is a histogram, it will use it to make
Guy Schalnate5a37791996-06-05 15:50:50 -0500657more intelligent choices when reducing the palette. If there is no
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600658histogram, it may not do as good a job.
Guy Schalnat69b14481996-01-10 02:56:49 -0600659
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnat0d580581995-07-20 02:43:20 -0500661 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600662 if (png_get_valid(png_ptr, info_ptr,
663 PNG_INFO_PLTE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600664 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500665 png_color_16p histogram;
666
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600667 png_get_hIST(png_ptr, info_ptr,
668 &histogram);
669 png_set_dither(png_ptr, palette, num_palette,
670 max_screen_colors, histogram, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600671 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500672 else
673 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600674 png_color std_color_cube[MAX_SCREEN_COLORS] =
Guy Schalnat0d580581995-07-20 02:43:20 -0500675 { ... colors ... };
676
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600677 png_set_dither(png_ptr, std_color_cube,
678 MAX_SCREEN_COLORS, MAX_SCREEN_COLORS,
679 NULL,0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500680 }
681 }
682
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600683PNG files describe monochrome as black being zero and white being one.
684The following code will reverse this (make black be one and white be
685zero):
Guy Schalnat0d580581995-07-20 02:43:20 -0500686
Andreas Dilger47a0c421997-05-16 02:46:07 -0500687 if (bit_depth == 1 && color_type == PNG_COLOR_GRAY)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600688 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500689
Guy Schalnat69b14481996-01-10 02:56:49 -0600690PNG files store 16 bit pixels in network byte order (big-endian,
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600691ie. most significant bits first). This code changes the storage to the
692other way (little-endian, i.e. least significant bits first, the
Guy Schalnat69b14481996-01-10 02:56:49 -0600693way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -0500694
Andreas Dilger47a0c421997-05-16 02:46:07 -0500695 if (bit_depth == 16)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600696 png_set_swap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500697
Andreas Dilger47a0c421997-05-16 02:46:07 -0500698If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
699need to change the order the pixels are packed into bytes, you can use:
700
701 if (bit_depth < 8)
702 png_set_packswap(png_ptr);
703
Guy Schalnat69b14481996-01-10 02:56:49 -0600704The last thing to handle is interlacing; this is covered in detail below,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600705but you must call the function here if you want libpng to handle expansion
706of the interlaced image.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500707
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600708 number_of_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500709
Guy Schalnate5a37791996-06-05 15:50:50 -0500710After setting the transformations, libpng can update your png_info
711structure to reflect any transformations you've requested with this
Andreas Dilger47a0c421997-05-16 02:46:07 -0500712call. This is most useful to update the info structure's rowbytes
713field so you can use it to allocate your image memory. This function
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600714will also update your palette with the correct screen_gamma and
Guy Schalnate5a37791996-06-05 15:50:50 -0500715background if these have been given with the calls above.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500716
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600717 png_read_update_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500718
719After you call png_read_update_info(), you can allocate any
Guy Schalnate5a37791996-06-05 15:50:50 -0500720memory you need to hold the image. The row data is simply
721raw byte data for all forms of images. As the actual allocation
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500722varies among applications, no example will be given. If you
Guy Schalnate5a37791996-06-05 15:50:50 -0500723are allocating one large chunk, you will need to build an
724array of pointers to each row, as it will be needed for some
725of the functions below.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500726
727After you've allocated memory, you can read the image data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500728The simplest way to do this is in one function call. If you are
729allocating enough memory to hold the whole image, you can just
730call png_read_image() and libpng will read in all the image data
731and put it in the memory area supplied. You will need to pass in
732an array of pointers to each row.
733
734This function automatically handles interlacing, so you don't need
735to call png_set_interlace_handling() or call this function multiple
736times, or any of that other stuff necessary with png_read_rows().
737
738 png_read_image(png_ptr, row_pointers);
739
740where row_pointers is:
741
Guy Schalnate5a37791996-06-05 15:50:50 -0500742 png_bytep row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -0500743
744You can point to void or char or whatever you use for pixels.
745
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600746If you don't want to read in the whole image at once, you can
Guy Schalnat0d580581995-07-20 02:43:20 -0500747use png_read_rows() instead. If there is no interlacing (check
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600748interlace_type == PNG_INTERLACE_NONE), this is simple:
Guy Schalnat0d580581995-07-20 02:43:20 -0500749
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600750 png_read_rows(png_ptr, row_pointers, NULL,
751 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500752
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600753where row_pointers is the same as in the png_read_image() call.
Guy Schalnat0d580581995-07-20 02:43:20 -0500754
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600755If you are doing this just one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -0500756row_pointers:
757
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600758 png_bytep row_pointers = row;
Guy Schalnate5a37791996-06-05 15:50:50 -0500759 png_read_row(png_ptr, &row_pointers, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500760
Guy Schalnat69b14481996-01-10 02:56:49 -0600761If the file is interlaced (info_ptr->interlace_type != 0), things get
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762somewhat harder. The only current (PNG Specification version 1.0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600763interlacing type for PNG is (interlace_type == PNG_INTERLACE_ADAM7)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600764is a somewhat complicated 2D interlace scheme, known as Adam7, that
Andreas Dilger47a0c421997-05-16 02:46:07 -0500765breaks down an image into seven smaller images of varying size, based
766on an 8x8 grid.
Guy Schalnat0d580581995-07-20 02:43:20 -0500767
Guy Schalnate5a37791996-06-05 15:50:50 -0500768libpng can fill out those images or it can give them to you "as is".
769If you want them filled out, there are two ways to do that. The one
770mentioned in the PNG specification is to expand each pixel to cover
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600771those pixels that have not been read yet (the "rectangle" method).
772This results in a blocky image for the first pass, which gradually
773smooths out as more pixels are read. The other method is the "sparkle"
774method, where pixels are drawn only in their final locations, with the
775rest of the image remaining whatever colors they were initialized to
776before the start of the read. The first method usually looks better,
777but tends to be slower, as there are more pixels to put in the rows.
Guy Schalnate5a37791996-06-05 15:50:50 -0500778
779If you don't want libpng to handle the interlacing details, just call
780png_read_rows() seven times to read in all seven images. Each of the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600781images is a valid image by itself, or they can all be combined on an
Guy Schalnate5a37791996-06-05 15:50:50 -05007828x8 grid to form a single image (although if you intend to combine them
783you would be far better off using the libpng interlace handling).
784
785The first pass will return an image 1/8 as wide as the entire image
786(every 8th column starting in column 0) and 1/8 as high as the original
787(every 8th row starting in row 0), the second will be 1/8 as wide
788(starting in column 4) and 1/8 as high (also starting in row 0). The
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600789third pass will be 1/4 as wide (every 4th pixel starting in column 0) and
Guy Schalnate5a37791996-06-05 15:50:50 -05007901/8 as high (every 8th row starting in row 4), and the fourth pass will
791be 1/4 as wide and 1/4 as high (every 4th column starting in column 2,
792and every 4th row starting in row 0). The fifth pass will return an
793image 1/2 as wide, and 1/4 as high (starting at column 0 and row 2),
794while the sixth pass will be 1/2 as wide and 1/2 as high as the original
795(starting in column 1 and row 0). The seventh and final pass will be as
796wide as the original, and 1/2 as high, containing all of the odd
797numbered scanlines. Phew!
Guy Schalnat0d580581995-07-20 02:43:20 -0500798
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600799If you want libpng to expand the images, call this before calling
800png_start_read_image() or png_read_update_info():
Guy Schalnat0d580581995-07-20 02:43:20 -0500801
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600802 if (interlace_type == PNG_INTERLACE_ADAM7)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600803 number_of_passes
804 = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500805
806This will return the number of passes needed. Currently, this
807is seven, but may change if another interlace type is added.
808This function can be called even if the file is not interlaced,
Guy Schalnate5a37791996-06-05 15:50:50 -0500809where it will return one pass.
Guy Schalnat0d580581995-07-20 02:43:20 -0500810
811If you are not going to display the image after each pass, but are
812going to wait until the entire image is read in, use the sparkle
813effect. This effect is faster and the end result of either method
814is exactly the same. If you are planning on displaying the image
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600815after each pass, the "rectangle" effect is generally considered the
Guy Schalnat0d580581995-07-20 02:43:20 -0500816better looking one.
817
818If you only want the "sparkle" effect, just call png_read_rows() as
819normal, with the third parameter NULL. Make sure you make pass over
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600820the image number_of_passes times, and you don't change the data in the
Guy Schalnat0d580581995-07-20 02:43:20 -0500821rows between calls. You can change the locations of the data, just
822not the data. Each pass only writes the pixels appropriate for that
823pass, and assumes the data from previous passes is still valid.
824
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600825 png_read_rows(png_ptr, row_pointers, NULL,
826 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500827
828If you only want the first effect (the rectangles), do the same as
829before except pass the row buffer in the third parameter, and leave
830the second parameter NULL.
831
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600832 png_read_rows(png_ptr, NULL, row_pointers,
833 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500834
835After you are finished reading the image, you can finish reading
Guy Schalnate5a37791996-06-05 15:50:50 -0500836the file. If you are interested in comments or time, which may be
837stored either before or after the image data, you should pass the
Guy Schalnate5a37791996-06-05 15:50:50 -0500838separate png_info struct if you want to keep the comments from
839before and after the image separate. If you are not interested, you
840can pass NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -0500841
Guy Schalnate5a37791996-06-05 15:50:50 -0500842 png_read_end(png_ptr, end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500843
Guy Schalnate5a37791996-06-05 15:50:50 -0500844When you are done, you can free all memory allocated by libpng like this:
Guy Schalnat0d580581995-07-20 02:43:20 -0500845
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600846 png_destroy_read_struct(&png_ptr, &info_ptr,
847 &end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500848
Guy Schalnate5a37791996-06-05 15:50:50 -0500849For a more compact example of reading a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -0500850
851
Guy Schalnat6d764711995-12-19 03:22:19 -0600852Reading PNG files progressively:
853
854The progressive reader is slightly different then the non-progressive
855reader. Instead of calling png_read_info(), png_read_rows(), and
856png_read_end(), you make one call to png_process_data(), which calls
857callbacks when it has the info, a row, or the end of the image. You
858set up these callbacks with png_set_progressive_read_fn(). You don't
859have to worry about the input/output functions of libpng, as you are
860giving the library the data directly in png_process_data(). I will
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600861assume that you have read the section on reading PNG files above,
Guy Schalnat6d764711995-12-19 03:22:19 -0600862so I will only highlight the differences (although I will show
863all of the code).
864
865png_structp png_ptr;
866png_infop info_ptr;
867
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600868 /* An example code fragment of how you would
869 initialize the progressive reader in your
870 application. */
871 int
872 initialize_png_reader()
873 {
874 png_ptr = png_create_read_struct
875 (PNG_LIBPNG_VER_STRING, (void *)user_error_ptr,
876 user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600877 if (!png_ptr)
878 return -1;
Guy Schalnate5a37791996-06-05 15:50:50 -0500879 info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600880 if (!info_ptr)
881 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600882 png_destroy_read_struct(&png_ptr, (png_infopp)NULL,
883 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600884 return -1;
885 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600886
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600887 if (setjmp(png_ptr->jmpbuf))
888 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600889 png_destroy_read_struct(&png_ptr, &info_ptr,
890 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600891 return -1;
892 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600893
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600894 /* This one's new. You can provide functions
895 to be called when the header info is valid,
896 when each row is completed, and when the image
897 is finished. If you aren't using all functions,
898 you can specify a NULL parameter. You can use
899 any struct as the user_ptr (cast to a void pointer
900 for the function call), and retrieve the pointer
901 from inside the callbacks using the function
902
903 png_get_progressive_ptr(png_ptr);
904
905 which will return a void pointer, which you have
906 to cast appropriately.
Guy Schalnate5a37791996-06-05 15:50:50 -0500907 */
908 png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600909 info_callback, row_callback, end_callback);
Guy Schalnat6d764711995-12-19 03:22:19 -0600910
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600911 return 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600912 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600913
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600914 /* A code fragment that you call as you receive blocks
915 of data */
916 int
917 process_data(png_bytep buffer, png_uint_32 length)
918 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600919 if (setjmp(png_ptr->jmpbuf))
920 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600921 png_destroy_read_struct(&png_ptr, &info_ptr,
922 (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600923 return -1;
924 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600925
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600926 /* This one's new also. Simply give it a chunk
927 of data from the file stream (in order, of
928 course). On machines with segmented memory
929 models machines, don't give it any more than
930 64K. The library seems to run fine with sizes
931 of 4K. Although you can give it much less if
932 necessary (I assume you can give it chunks of
933 1 byte, I haven't tried less then 256 bytes
934 yet). When this function returns, you may
935 want to display any rows that were generated
936 in the row callback if you don't already do
937 so there.
Guy Schalnate5a37791996-06-05 15:50:50 -0500938 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600939 png_process_data(png_ptr, info_ptr, buffer, length);
940 return 0;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600941 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600942
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600943 /* This function is called (as set by
944 png_set_progressive_fn() above) when enough data
945 has been supplied so all of the header has been
946 read.
Guy Schalnate5a37791996-06-05 15:50:50 -0500947 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600948 void
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600949 info_callback(png_structp png_ptr, png_infop info)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600950 {
951 /* Do any setup here, including setting any of
952 the transformations mentioned in the Reading
953 PNG files section. For now, you _must_ call
954 either png_start_read_image() or
955 png_read_update_info() after all the
956 transformations are set (even if you don't set
957 any). You may start getting rows before
958 png_process_data() returns, so this is your
959 last chance to prepare for that.
Guy Schalnate5a37791996-06-05 15:50:50 -0500960 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600961 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600962
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600963 /* This function is called when each row of image
964 data is complete */
965 void
966 row_callback(png_structp png_ptr, png_bytep new_row,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600967 png_uint_32 row_num, int pass)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600968 {
969 /* If the image is interlaced, and you turned
970 on the interlace handler, this function will
971 be called for every row in every pass. Some
972 of these rows will not be changed from the
973 previous pass. When the row is not changed,
974 the new_row variable will be NULL. The rows
975 and passes are called in order, so you don't
976 really need the row_num and pass, but I'm
977 supplying them because it may make your life
978 easier.
Guy Schalnat6d764711995-12-19 03:22:19 -0600979
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600980 For the non-NULL rows of interlaced images,
981 you must call png_progressive_combine_row()
982 passing in the row and the old row. You can
983 call this function for NULL rows (it will just
984 return) and for non-interlaced images (it just
985 does the memcpy for you) if it will make the
986 code easier. Thus, you can just do this for
987 all cases:
Guy Schalnate5a37791996-06-05 15:50:50 -0500988 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600989
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600990 png_progressive_combine_row(png_ptr, old_row,
991 new_row);
Guy Schalnat6d764711995-12-19 03:22:19 -0600992
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600993 /* where old_row is what was displayed for
994 previous rows. Note that the first pass
995 (pass == 0, really) will completely cover
996 the old row, so the rows do not have to be
997 initialized. After the first pass (and only
998 for interlaced images), you will have to pass
999 the current row, and the function will combine
1000 the old row and the new row.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001001 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001002 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001003
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001004 void
1005 end_callback(png_structp png_ptr, png_infop info)
1006 {
1007 /* This function is called after the whole image
1008 has been read, including any chunks after the
1009 image (up to and including the IEND). You
1010 will usually have the same info chunk as you
1011 had in the header, although some data may have
1012 been added to the comments and time fields.
Guy Schalnat6d764711995-12-19 03:22:19 -06001013
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001014 Most people won't do much here, perhaps setting
1015 a flag that marks the image as finished.
Guy Schalnate5a37791996-06-05 15:50:50 -05001016 */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001017 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001018
1019
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001020
Guy Schalnat69b14481996-01-10 02:56:49 -06001021IV. Writing
Guy Schalnat0d580581995-07-20 02:43:20 -05001022
1023Much of this is very similar to reading. However, everything of
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001024importance is repeated here, so you won't have to constantly look
Guy Schalnat69b14481996-01-10 02:56:49 -06001025back up in the reading section to understand writing.
Guy Schalnat0d580581995-07-20 02:43:20 -05001026
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001027You will want to do the I/O initialization before you get into libpng,
Guy Schalnate5a37791996-06-05 15:50:50 -05001028so if it doesn't work, you don't have anything to undo. If you are not
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001029using the standard I/O functions, you will need to replace them with
Guy Schalnate5a37791996-06-05 15:50:50 -05001030custom writing functions. See the discussion under Customizing libpng.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001031
1032 FILE *fp = fopen(file_name, "wb");
1033 if (!fp)
1034 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001035 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001036 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001037
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001038Next, png_struct and png_info need to be allocated and initialized.
Guy Schalnate5a37791996-06-05 15:50:50 -05001039As these can be both relatively large, you may not want to store these
1040on the stack, unless you have stack space to spare. Of course, you
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001041will want to check if they return NULL. If you are also reading,
1042you won't want to name your read structure and your write structure
1043both "png_ptr"; you can call them anything you like, such as
1044"read_ptr" and "write_ptr". Look at pngtest.c, for example.
Guy Schalnat0d580581995-07-20 02:43:20 -05001045
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001046 png_structp png_ptr = png_create_write_struct
1047 (PNG_LIBPNG_VER_STRING, (void *)user_error_ptr,
1048 user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001049 if (!png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -05001050 return;
1051
1052 png_infop info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001053 if (!info_ptr)
1054 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001055 png_destroy_write_struct(&png_ptr,
1056 (png_infopp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -05001057 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001058 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001059
1060After you have these structures, you will need to set up the
1061error handling. When libpng encounters an error, it expects to
Guy Schalnate5a37791996-06-05 15:50:50 -05001062longjmp() back to your routine. Therefore, you will need to call
Guy Schalnat0d580581995-07-20 02:43:20 -05001063setjmp and pass the jmpbuf field of your png_struct. If you
1064write the file from different routines, you will need to update
1065the jmpbuf field every time you enter a new routine that will
1066call a png_ function. See your documentation of setjmp/longjmp
Guy Schalnat6d764711995-12-19 03:22:19 -06001067for your compiler for more information on setjmp/longjmp. See
Guy Schalnat69b14481996-01-10 02:56:49 -06001068the discussion on libpng error handling in the Customizing Libpng
1069section below for more information on the libpng error handling.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001070
1071 if (setjmp(png_ptr->jmpbuf))
1072 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001073 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001074 fclose(fp);
1075 return;
1076 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001077
Andreas Dilger47a0c421997-05-16 02:46:07 -05001078Now you need to set up the output code. The default for libpng is to
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001079use the C function fwrite(). If you use this, you will need to pass a
1080valid FILE * in the function png_init_io(). Be sure that the file is
1081opened in binary mode. Again, if you wish to handle writing data in
1082another way, see the discussion on libpng I/O handling in the Customizing
1083Libpng section below.
Guy Schalnat0d580581995-07-20 02:43:20 -05001084
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001085 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001086
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001087At this point, you can set up a callback function that will be
1088called after each row has been written, which you can use to control
1089a progress meter or the like. It's demonstrated in pngtest.c.
1090You must supply a function
1091
1092 void write_row_callback(png_ptr, png_uint_32 row, int pass);
1093 {
1094 /* put your code here */
1095 }
1096
1097(You can give it another name that you like instead of "write_row_callback")
1098
1099To inform libpng about your function, use
1100
1101 png_set_write_status_fn(png_ptr, write_row_callback);
1102
Guy Schalnate5a37791996-06-05 15:50:50 -05001103You now have the option of modifying how the compression library will
1104run. The following functions are mainly for testing, but may be useful
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001105in some cases, like if you need to write PNG files extremely fast and
Guy Schalnate5a37791996-06-05 15:50:50 -05001106are willing to give up some compression, or if you want to get the
1107maximum possible compression at the expense of slower writing. If you
1108have no special needs in this area, let the library do what it wants by
1109not calling this function at all, as it has been tuned to deliver a good
1110speed/compression ratio. The second parameter to png_set_filter() is
1111the filter method, for which the only valid value is '0' (as of the
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001112October 1996 PNG specification, version 1.0). The third parameter is a
1113flag that indicates
Guy Schalnate5a37791996-06-05 15:50:50 -05001114which filter type(s) are to be tested for each scanline. See the
1115Compression Library for details on the specific filter types.
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001116
Guy Schalnate5a37791996-06-05 15:50:50 -05001117
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001118 /* turn on or off filtering, and/or choose
1119 specific filters */
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 png_set_filter(png_ptr, 0,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001121 PNG_FILTER_NONE | PNG_FILTER_SUB |
1122 PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001123
1124The png_set_compression_???() functions interface to the zlib compression
1125library, and should mostly be ignored unless you really know what you are
1126doing. The only generally useful call is png_set_compression_level()
1127which changes how much time zlib spends on trying to compress the image
1128data. See the Compression Library for details on the compression levels.
1129
1130 /* set the zlib compression level */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001131 png_set_compression_level(png_ptr,
1132 Z_BEST_COMPRESSION);
Guy Schalnate5a37791996-06-05 15:50:50 -05001133
1134 /* set other zlib parameters */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001135 png_set_compression_mem_level(png_ptr, 8);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001136 png_set_compression_strategy(png_ptr,
1137 Z_DEFAULT_STRATEGY);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001138 png_set_compression_window_bits(png_ptr, 15);
1139 png_set_compression_method(png_ptr, 8);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001140
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001141You now need to fill in the png_info structure with all the data you
1142wish to write before the actual image. Note that the only thing you
1143are allowed to write after the image is the text chunks and the time
Guy Schalnate5a37791996-06-05 15:50:50 -05001144chunk (as of PNG Specification 1.0, anyway). See png_write_end() and
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001145the latest PNG specification for more information on that. If you
Guy Schalnate5a37791996-06-05 15:50:50 -05001146wish to write them before the image, fill them in now, and flag that
1147data as being valid. If you want to wait until after the data, don't
1148fill them until png_write_end(). For all the fields in png_info and
1149their data types, see png.h. For explanations of what the fields
1150contain, see the PNG specification.
Guy Schalnat69b14481996-01-10 02:56:49 -06001151
1152Some of the more important parts of the png_info are:
1153
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001154 png_set_IHDR(png_ptr, info_ptr, width, height,
1155 bit_depth, color_type, interlace_type,
1156 compression_type, filter_type)
1157 width - holds the width of the image
1158 in pixels (up to 2^31).
1159 height - holds the height of the image
1160 in pixels (up to 2^31).
1161 bit_depth - holds the bit depth of one of the
1162 image channels.
1163 (valid values are 1, 2, 4, 8, 16
1164 and depend also on the
1165 color_type. See also significant
1166 bits (sBIT) below).
1167 color_type - describes which color/alpha
1168 channels are present.
1169 PNG_COLOR_TYPE_GRAY
1170 (bit depths 1, 2, 4, 8, 16)
1171 PNG_COLOR_TYPE_GRAY_ALPHA
1172 (bit depths 8, 16)
1173 PNG_COLOR_TYPE_PALETTE
1174 (bit depths 1, 2, 4, 8)
1175 PNG_COLOR_TYPE_RGB
1176 (bit_depths 8, 16)
1177 PNG_COLOR_TYPE_RGB_ALPHA
1178 (bit_depths 8, 16)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001179
1180 PNG_COLOR_MASK_PALETTE
1181 PNG_COLOR_MASK_COLOR
1182 PNG_COLOR_MASK_ALPHA
1183
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001184 interlace_type - PNG_INTERLACE_NONE or
1185 PNG_INTERLACE_ADAM7
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001186 compression_type - (must be
1187 PNG_COMPRESSION_TYPE_DEFAULT)
1188 filter_type - (must be PNG_FILTER_TYPE_DEFAULT)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001190 png_set_PLTE(png_ptr, info_ptr, palette,
1191 num_palette);
1192 palette - the palette for the file
1193 (array of png_color)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001194 num_palette - number of entries in the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195
1196 png_set_gAMA(png_ptr, info_ptr, gamma);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001197 gamma - the gamma the image was created
1198 at (PNG_INFO_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001200 png_set_sRGB(png_ptr, info_ptr, srgb_intent);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001201 srgb_intent - the rendering intent
1202 (PNG_INFO_sRGB) The presence of
1203 the sRGB chunk means that the pixel
1204 data is in the sRGB color space.
1205 This chunk also implies specific
1206 values of gAMA and cHRM. Rendering
1207 intent is the CSS-1 property that
1208 has been defined by the International
1209 Color Consortium
1210 (http://www.color.org).
1211 It can be one of
1212 PNG_SRGB_INTENT_SATURATION,
1213 PNG_SRGB_INTENT_PERCEPTUAL,
1214 PNG_SRGB_INTENT_ABSOLUTE, or
1215 PNG_SRGB_INTENT_RELATIVE.
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001216
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001217
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001218 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
1219 srgb_intent);
1220 srgb_intent - the rendering intent
1221 (PNG_INFO_sRGB) The presence of the
1222 sRGB chunk means that the pixel
1223 data is in the sRGB color space.
1224 This function also causes gAMA and
1225 cHRM chunks with the specific values
1226 that are consistent with sRGB to be
1227 written.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001228
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 png_set_sBIT(png_ptr, info_ptr, sig_bit);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001230 sig_bit - the number of significant bits for
1231 (PNG_INFO_sBIT) each of the gray, red,
1232 green, and blue channels, whichever are
1233 appropriate for the given color type
1234 (png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001236 png_set_tRNS(png_ptr, info_ptr, trans, num_trans,
1237 trans_values);
1238 trans - array of transparent entries for
1239 palette (PNG_INFO_tRNS)
1240 trans_values - transparent pixel for non-paletted
1241 images (PNG_INFO_tRNS)
1242 num_trans - number of transparent entries
1243 (PNG_INFO_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001244
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001245 png_set_hIST(png_ptr, info_ptr, hist);
1246 (PNG_INFO_hIST)
1247 hist - histogram of palette (array of
1248 png_color_16)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249
1250 png_set_tIME(png_ptr, info_ptr, mod_time);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001251 mod_time - time image was last modified
1252 (PNG_VALID_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001253
1254 png_set_bKGD(png_ptr, info_ptr, background);
Guy Schalnate5a37791996-06-05 15:50:50 -05001255 background - background color (PNG_VALID_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256
1257 png_set_text(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001258 text_ptr - array of png_text holding image
1259 comments
1260 text_ptr[i]->key - keyword for comment.
1261 text_ptr[i]->text - text comments for current
1262 keyword.
1263 text_ptr[i]->compression - type of compression used
1264 on "text" PNG_TEXT_COMPRESSION_NONE or
1265 PNG_TEXT_COMPRESSION_zTXt
1266 num_text - number of comments in text_ptr
Andreas Dilger47a0c421997-05-16 02:46:07 -05001267
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001268 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y,
1269 unit_type);
1270 offset_x - positive offset from the left
1271 edge of the screen
1272 offset_y - positive offset from the top
1273 edge of the screen
1274 unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
Andreas Dilger47a0c421997-05-16 02:46:07 -05001275
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001276 png_set_pHYs(png_ptr, info_ptr, res_x, res_y,
1277 unit_type);
1278 res_x - pixels/unit physical resolution
1279 in x direction
1280 res_y - pixels/unit physical resolution
1281 in y direction
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001282 unit_type - PNG_RESOLUTION_UNKNOWN,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001283 PNG_RESOLUTION_METER
Guy Schalnat0d580581995-07-20 02:43:20 -05001284
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001285In PNG files, the alpha channel in an image is the level of opacity.
1286If your data is supplied as a level of transparency, you can invert the
1287alpha channel before you write it, so that 0 is fully transparent and 255
1288(in 8-bit or paletted images) or 65535 (in 16-bit images) is fully opaque,
1289with
1290
1291 png_set_invert_alpha(png_ptr);
1292
1293This must appear here instead of later with the other transformations
1294because in the case of paletted images the tRNS chunk data has to
1295be inverted before the tRNS chunk is written. If your image is not a
1296paletted image, the tRNS data (which in such cases represents a single
1297color to be rendered as transparent) won't be changed.
1298
Guy Schalnat0d580581995-07-20 02:43:20 -05001299A quick word about text and num_text. text is an array of png_text
1300structures. num_text is the number of valid structures in the array.
1301If you want, you can use max_text to hold the size of the array, but
1302libpng ignores it for writing (it does use it for reading). Each
1303png_text structure holds a keyword-text value, and a compression type.
1304The compression types have the same valid numbers as the compression
1305types of the image data. Currently, the only valid number is zero.
1306However, you can store text either compressed or uncompressed, unlike
1307images which always have to be compressed. So if you don't want the
Andreas Dilger47a0c421997-05-16 02:46:07 -05001308text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
1309Until text gets around 1000 bytes, it is not worth compressing it.
1310After the text has been written out to the file, the compression type
1311is set to PNG_TEXT_COMPRESSION_NONE_WR or PNG_TEXT_COMPRESSION_zTXt_WR,
1312so that it isn't written out again at the end (in case you are calling
1313png_write_end() with the same struct.
Guy Schalnate5a37791996-06-05 15:50:50 -05001314
1315The keywords that are given in the PNG Specification are:
1316
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001317 Title Short (one line) title or
1318 caption for image
1319 Author Name of image's creator
1320 Description Description of image (possibly long)
1321 Copyright Copyright notice
1322 Creation Time Time of original image creation
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001323 (usually RFC 1123 format, see below)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001324 Software Software used to create the image
1325 Disclaimer Legal disclaimer
1326 Warning Warning of nature of content
1327 Source Device used to create the image
1328 Comment Miscellaneous comment; conversion
1329 from other image format
Guy Schalnat0d580581995-07-20 02:43:20 -05001330
1331The keyword-text pairs work like this. Keywords should be short
1332simple descriptions of what the comment is about. Some typical
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001333keywords are found in the PNG specification, as is some recommendations
Guy Schalnat0d580581995-07-20 02:43:20 -05001334on keywords. You can repeat keywords in a file. You can even write
1335some text before the image and some after. For example, you may want
1336to put a description of the image before the image, but leave the
1337disclaimer until after, so viewers working over modem connections
1338don't have to wait for the disclaimer to go over the modem before
1339they start seeing the image. Finally, keywords should be full
Guy Schalnate5a37791996-06-05 15:50:50 -05001340words, not abbreviations. Keywords and text are in the ISO 8859-1
1341(Latin-1) character set (a superset of regular ASCII) and can not
1342contain NUL characters, and should not contain control or other
1343unprintable characters. To make the comments widely readable, stick
1344with basic ASCII, and avoid machine specific character set extensions
1345like the IBM-PC character set. The keyword must be present, but
Guy Schalnat0d580581995-07-20 02:43:20 -05001346you can leave off the text string on non-compressed pairs.
1347Compressed pairs must have a text string, as only the text string
1348is compressed anyway, so the compression would be meaningless.
1349
Guy Schalnat6d764711995-12-19 03:22:19 -06001350PNG supports modification time via the png_time structure. Two
Guy Schalnat0d580581995-07-20 02:43:20 -05001351conversion routines are proved, png_convert_from_time_t() for
1352time_t and png_convert_from_struct_tm() for struct tm. The
1353time_t routine uses gmtime(). You don't have to use either of
1354these, but if you wish to fill in the png_time structure directly,
1355you should provide the time in universal time (GMT) if possible
Guy Schalnat69b14481996-01-10 02:56:49 -06001356instead of your local time. Note that the year number is the full
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001357year (e.g. 1998, rather than 98 - PNG is year 2000 compliant!), and
Andreas Dilger47a0c421997-05-16 02:46:07 -05001358that months start with 1.
Guy Schalnat0d580581995-07-20 02:43:20 -05001359
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001360If you want to store the time of the original image creation, you should
1361use a plain tEXt chunk with the "Creation Time" keyword. This is
1362necessary because the "creation time" of a PNG image is somewhat vague,
1363depending on whether you mean the PNG file, the time the image was
1364created in a non-PNG format, a still photo from which the image was
1365scanned, or possibly the subject matter itself. In order to facilitate
1366machine-readable dates, it is recommended that the "Creation Time"
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001367tEXt chunk use RFC 1123 format dates (e.g. 22 May 1997 18:07:10 GMT"),
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001368although this isn't a requirement. Unlike the tIME chunk, the
1369"Creation Time" tEXt chunk is not expected to be automatically changed
1370by the software. To facilitate the use of RFC 1123 dates, a function
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001371png_convert_to_rfc1123(png_timep) is provided to convert from PNG
1372time to an RFC 1123 format string.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001373
Guy Schalnat0d580581995-07-20 02:43:20 -05001374You are now ready to write all the file information up to the actual
1375image data. You do this with a call to png_write_info().
1376
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001377 png_write_info(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001378
Guy Schalnate5a37791996-06-05 15:50:50 -05001379After you've written the file information, you can set up the library
1380to handle any special transformations of the image data. The various
Guy Schalnat0d580581995-07-20 02:43:20 -05001381ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001382should occur. This is important, as some of these change the color
1383type and/or bit depth of the data, and some others only work on
1384certain color types and bit depths. Even though each transformation
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001385checks to see if it has data that it can do something with, you should
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001386make sure to only enable a transformation if it will be valid for the
1387data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -05001388
Guy Schalnate5a37791996-06-05 15:50:50 -05001389PNG files store RGB pixels packed into 3 bytes. This code tells
1390the library to expect input data with 4 bytes per pixel
Guy Schalnat0d580581995-07-20 02:43:20 -05001391
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001392 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Guy Schalnat0d580581995-07-20 02:43:20 -05001393
Guy Schalnate5a37791996-06-05 15:50:50 -05001394where the 0 is the value that will be put in the 4th byte, and the
1395location is either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending
1396upon whether the filler byte is stored XRGB or RGBX.
Guy Schalnat0d580581995-07-20 02:43:20 -05001397
1398PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
1399they can, resulting in, for example, 8 pixels per byte for 1 bit files.
Guy Schalnat69b14481996-01-10 02:56:49 -06001400If the data is supplied at 1 pixel per byte, use this code, which will
Guy Schalnate5a37791996-06-05 15:50:50 -05001401correctly pack the pixels into a single byte:
Guy Schalnat0d580581995-07-20 02:43:20 -05001402
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001403 png_set_packing(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001404
1405PNG files reduce possible bit depths to 1, 2, 4, 8, and 16. If your
Guy Schalnate5a37791996-06-05 15:50:50 -05001406data is of another bit depth, you can write an sBIT chunk into the
1407file so that decoders can get the original data if desired.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001408
Guy Schalnate5a37791996-06-05 15:50:50 -05001409 /* Set the true bit depth of the image data */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001410 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001411 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001412 sig_bit.red = true_bit_depth;
1413 sig_bit.green = true_bit_depth;
1414 sig_bit.blue = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001415 }
1416 else
1417 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 sig_bit.gray = true_bit_depth;
1419 }
1420 if (color_type & PNG_COLOR_MASK_ALPHA)
1421 {
1422 sig_bit.alpha = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001423 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001424
Andreas Dilger47a0c421997-05-16 02:46:07 -05001425 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -05001426
Guy Schalnate5a37791996-06-05 15:50:50 -05001427If the data is stored in the row buffer in a bit depth other than
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001428one 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 -05001429this will scale the values to appear to be the correct bit depth as
1430is required by PNG.
1431
Andreas Dilger47a0c421997-05-16 02:46:07 -05001432 png_set_shift(png_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -05001433
Guy Schalnat69b14481996-01-10 02:56:49 -06001434PNG files store 16 bit pixels in network byte order (big-endian,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001435ie. most significant bits first). This code would be used if they are
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001436supplied the other way (little-endian, i.e. least significant bits
1437first, the way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -05001438
Andreas Dilger47a0c421997-05-16 02:46:07 -05001439 if (bit_depth > 8)
1440 png_set_swap(png_ptr);
1441
1442If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
1443need to change the order the pixels are packed into bytes, you can use:
1444
1445 if (bit_depth < 8)
1446 png_set_packswap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001447
Guy Schalnat69b14481996-01-10 02:56:49 -06001448PNG files store 3 color pixels in red, green, blue order. This code
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001449would be used if they are supplied as blue, green, red:
Guy Schalnat0d580581995-07-20 02:43:20 -05001450
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001451 png_set_bgr(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001452
Guy Schalnat69b14481996-01-10 02:56:49 -06001453PNG files describe monochrome as black being zero and white being
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001454one. This code would be used if the pixels are supplied with this reversed
Guy Schalnat69b14481996-01-10 02:56:49 -06001455(black being one and white being zero):
Guy Schalnat0d580581995-07-20 02:43:20 -05001456
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001457 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001458
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001459Finally, you can write your own transformation function if none of
1460the existing ones meets your needs. This is done by setting a callback
1461with
1462
1463 png_set_write_user_transform_fn(png_ptr,
1464 write_transform_fn);
1465
1466You must supply the function
1467
1468 void write_transform_fn(png_ptr ptr, row_info_ptr
1469 row_info, png_bytep data)
1470
1471See pngtest.c for a working example. Your function will be called
1472before any of the other transformations have been processed.
1473
Guy Schalnate5a37791996-06-05 15:50:50 -05001474It is possible to have libpng flush any pending output, either manually,
1475or automatically after a certain number of lines have been written. To
1476flush the output stream a single time call:
1477
1478 png_write_flush(png_ptr);
1479
1480and to have libpng flush the output stream periodically after a certain
1481number of scanlines have been written, call:
1482
1483 png_set_flush(png_ptr, nrows);
1484
1485Note that the distance between rows is from the last time png_write_flush()
1486was called, or the first row of the image if it has never been called.
1487So if you write 50 lines, and then png_set_flush 25, it will flush the
1488output on the next scanline, and every 25 lines thereafter, unless
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001489png_write_flush() is called before 25 more lines have been written.
Guy Schalnate5a37791996-06-05 15:50:50 -05001490If nrows is too small (less than about 10 lines for a 640 pixel wide
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001491RGB image) the image compression may decrease noticeably (although this
Guy Schalnate5a37791996-06-05 15:50:50 -05001492may be acceptable for real-time applications). Infrequent flushing will
1493only degrade the compression performance by a few percent over images
1494that do not use flushing.
1495
Guy Schalnat0d580581995-07-20 02:43:20 -05001496That's it for the transformations. Now you can write the image data.
1497The simplest way to do this is in one function call. If have the
1498whole image in memory, you can just call png_write_image() and libpng
1499will write the image. You will need to pass in an array of pointers to
1500each row. This function automatically handles interlacing, so you don't
1501need to call png_set_interlace_handling() or call this function multiple
1502times, or any of that other stuff necessary with png_write_rows().
1503
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001504 png_write_image(png_ptr, row_pointers);
Guy Schalnat0d580581995-07-20 02:43:20 -05001505
1506where row_pointers is:
1507
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001508 png_bytef *row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -05001509
1510You can point to void or char or whatever you use for pixels.
1511
1512If you can't want to write the whole image at once, you can
1513use png_write_rows() instead. If the file is not interlaced,
1514this is simple:
1515
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001516 png_write_rows(png_ptr, row_pointers,
1517 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -05001518
1519row_pointers is the same as in the png_write_image() call.
1520
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001521If you are just writing one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -05001522row_pointers:
1523
Guy Schalnate5a37791996-06-05 15:50:50 -05001524 png_bytep row_pointer = row;
Guy Schalnat0d580581995-07-20 02:43:20 -05001525
Guy Schalnate5a37791996-06-05 15:50:50 -05001526 png_write_row(png_ptr, &row_pointer);
Guy Schalnat0d580581995-07-20 02:43:20 -05001527
Guy Schalnat69b14481996-01-10 02:56:49 -06001528When the file is interlaced, things can get a good deal more
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001529complicated. The only currently (as of February 1998 -- PNG Specification
1530version 1.0, dated October 1996) defined interlacing scheme for PNG files
1531is the "Adam7" interlace scheme, that breaks down an
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001532image into seven smaller images of varying size. libpng will build
1533these images for you, or you can do them yourself. If you want to
1534build them yourself, see the PNG specification for details of which
1535pixels to write when.
Guy Schalnat0d580581995-07-20 02:43:20 -05001536
1537If you don't want libpng to handle the interlacing details, just
Guy Schalnate5a37791996-06-05 15:50:50 -05001538use png_set_interlace_handling() and call png_write_rows() the
1539correct number of times to write all seven sub-images.
Guy Schalnat0d580581995-07-20 02:43:20 -05001540
Guy Schalnat69b14481996-01-10 02:56:49 -06001541If you want libpng to build the sub-images, call this before you start
1542writing any rows:
Guy Schalnat0d580581995-07-20 02:43:20 -05001543
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001544 number_of_passes =
1545 png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001546
1547This will return the number of passes needed. Currently, this
1548is seven, but may change if another interlace type is added.
1549
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001550Then write the complete image number_of_passes times.
Guy Schalnat0d580581995-07-20 02:43:20 -05001551
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001552 png_write_rows(png_ptr, row_pointers,
1553 number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -05001554
1555As some of these rows are not used, and thus return immediately,
1556you may want to read about interlacing in the PNG specification,
1557and only update the rows that are actually used.
1558
1559After you are finished writing the image, you should finish writing
1560the file. If you are interested in writing comments or time, you should
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001561pass an appropriately filled png_info pointer. If you are not interested,
1562you can pass NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -05001563
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001564 png_write_end(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001565
1566When you are done, you can free all memory used by libpng like this:
1567
Guy Schalnate5a37791996-06-05 15:50:50 -05001568 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001569
Guy Schalnate5a37791996-06-05 15:50:50 -05001570You must free any data you allocated for info_ptr, such as comments,
1571palette, or histogram, before the call to png_destroy_write_struct();
Guy Schalnat0d580581995-07-20 02:43:20 -05001572
Guy Schalnate5a37791996-06-05 15:50:50 -05001573For a more compact example of writing a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -05001574
1575
Guy Schalnat69b14481996-01-10 02:56:49 -06001576V. Modifying/Customizing libpng:
Guy Schalnat0d580581995-07-20 02:43:20 -05001577
1578There are two issues here. The first is changing how libpng does
1579standard things like memory allocation, input/output, and error handling.
1580The second deals with more complicated things like adding new chunks,
1581adding new transformations, and generally changing how libpng works.
1582
Andreas Dilger47a0c421997-05-16 02:46:07 -05001583All of the memory allocation, input/output, and error handling in libpng
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001584goes through callbacks which are user settable. The default routines are
Andreas Dilger47a0c421997-05-16 02:46:07 -05001585in pngmem.c, pngrio.c, pngwio.c, and pngerror.c respectively. To change
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001586these functions, call the appropriate png_set_???_fn() function.
Guy Schalnat0d580581995-07-20 02:43:20 -05001587
Guy Schalnat6d764711995-12-19 03:22:19 -06001588Memory allocation is done through the functions png_large_malloc(),
Guy Schalnate5a37791996-06-05 15:50:50 -05001589png_malloc(), png_realloc(), png_large_free(), and png_free(). These
1590currently just call the standard C functions. The large functions must
1591handle exactly 64K, but they don't have to handle more than that. If
Andreas Dilger47a0c421997-05-16 02:46:07 -05001592your pointers can't access more then 64K at a time, you will want to set
1593MAXSEG_64K in zlib.h. Since it is unlikely that the method of handling
1594memory allocation on a platform will change between applications, these
1595functions must be modified in the library at compile time.
Guy Schalnat0d580581995-07-20 02:43:20 -05001596
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001597Input/Output in libpng is done through png_read() and png_write(),
Andreas Dilger47a0c421997-05-16 02:46:07 -05001598which currently just call fread() and fwrite(). The FILE * is stored in
1599png_struct and is initialized via png_init_io(). If you wish to change
1600the method of I/O, the library supplies callbacks that you can set
1601through the function png_set_read_fn() and png_set_write_fn() at run
1602time, instead of calling the png_init_io() function. These functions
1603also provide a void pointer that can be retrieved via the function
Guy Schalnat6d764711995-12-19 03:22:19 -06001604png_get_io_ptr(). For example:
Guy Schalnat0d580581995-07-20 02:43:20 -05001605
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001606 png_set_read_fn(png_structp png_ptr,
1607 voidp io_ptr, png_rw_ptr read_data_fn)
Guy Schalnat0f716451995-11-28 11:22:13 -06001608
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001609 png_set_write_fn(png_structp png_ptr,
1610 voidp io_ptr, png_rw_ptr write_data_fn,
1611 png_flush_ptr output_flush_fn);
Guy Schalnat0f716451995-11-28 11:22:13 -06001612
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001613 voidp io_ptr = png_get_io_ptr(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001614
Guy Schalnat69b14481996-01-10 02:56:49 -06001615The replacement I/O functions should have prototypes as follows:
1616
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001617 void user_read_data(png_structp png_ptr,
1618 png_bytep data, png_uint_32 length);
1619 void user_write_data(png_structp png_ptr,
1620 png_bytep data, png_uint_32 length);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001621 void user_flush_data(png_structp png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001622
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001623Supplying NULL for the read, write, or flush functions sets them back
1624to using the default C stream functions. It is an error to read from
1625a write stream, and vice versa.
Guy Schalnat6d764711995-12-19 03:22:19 -06001626
1627Error handling in libpng is done through png_error() and png_warning().
1628Errors handled through png_error() are fatal, meaning that png_error()
Andreas Dilger47a0c421997-05-16 02:46:07 -05001629should never return to its caller. Currently, this is handled via
Guy Schalnat6d764711995-12-19 03:22:19 -06001630setjmp() and longjmp(), but you could change this to do things like
Guy Schalnat69b14481996-01-10 02:56:49 -06001631exit() if you should wish. On non-fatal errors, png_warning() is called
1632to print a warning message, and then control returns to the calling code.
Guy Schalnate5a37791996-06-05 15:50:50 -05001633By default png_error() and png_warning() print a message on stderr via
1634fprintf() unless the library is compiled with PNG_NO_STDIO defined. If
Guy Schalnat69b14481996-01-10 02:56:49 -06001635you wish to change the behavior of the error functions, you will need to
Guy Schalnate5a37791996-06-05 15:50:50 -05001636set up your own message callbacks. These functions are normally supplied
1637at the time that the png_struct is created. It is also possible to change
1638these functions after png_create_???_struct() has been called by calling:
Guy Schalnat69b14481996-01-10 02:56:49 -06001639
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001640 png_set_error_fn(png_structp png_ptr,
1641 png_voidp error_ptr, png_error_ptr error_fn,
1642 png_error_ptr warning_fn);
Guy Schalnat69b14481996-01-10 02:56:49 -06001643
Guy Schalnate5a37791996-06-05 15:50:50 -05001644 png_voidp error_ptr = png_get_error_ptr(png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001645
Guy Schalnate5a37791996-06-05 15:50:50 -05001646If NULL is supplied for either error_fn or warning_fn, then the libpng
1647default function will be used, calling fprintf() and/or longjmp() if a
1648problem is encountered. The replacement error functions should have
1649parameters as follows:
Guy Schalnat69b14481996-01-10 02:56:49 -06001650
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001651 void user_error_fn(png_structp png_ptr,
1652 png_const_charp error_msg);
1653 void user_warning_fn(png_structp png_ptr,
1654 png_const_charp warning_msg);
Guy Schalnat69b14481996-01-10 02:56:49 -06001655
1656The motivation behind using setjmp() and longjmp() is the C++ throw and
1657catch exception handling methods. This makes the code much easier to write,
1658as there is no need to check every return code of every function call.
1659However, there are some uncertainties about the status of local variables
1660after a longjmp, so the user may want to be careful about doing anything after
Andreas Dilger47a0c421997-05-16 02:46:07 -05001661setjmp returns non-zero besides returning itself. Consult your compiler
Guy Schalnat69b14481996-01-10 02:56:49 -06001662documentation for more details.
Guy Schalnat0f716451995-11-28 11:22:13 -06001663
Guy Schalnat0d580581995-07-20 02:43:20 -05001664If you need to read or write custom chunks, you will need to get deeper
Guy Schalnate5a37791996-06-05 15:50:50 -05001665into the libpng code, as a mechanism has not yet been supplied for user
1666callbacks with custom chunks. First, read the PNG specification, and have
1667a first level of understanding of how it works. Pay particular attention
1668to the sections that describe chunk names, and look at how other chunks
1669were designed, so you can do things similarly. Second, check out the
1670sections of libpng that read and write chunks. Try to find a chunk that
1671is similar to yours and copy off of it. More details can be found in the
1672comments inside the code. A way of handling unknown chunks in a generic
1673method, potentially via callback functions, would be best.
Guy Schalnat0d580581995-07-20 02:43:20 -05001674
Guy Schalnate5a37791996-06-05 15:50:50 -05001675If you wish to write your own transformation for the data, look through
1676the part of the code that does the transformations, and check out some of
1677the simpler ones to get an idea of how they work. Try to find a similar
1678transformation to the one you want to add and copy off of it. More details
1679can be found in the comments inside the code itself.
Guy Schalnat0d580581995-07-20 02:43:20 -05001680
1681Configuring for 16 bit platforms:
1682
Guy Schalnate5a37791996-06-05 15:50:50 -05001683You may need to change the png_large_malloc() and png_large_free()
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001684routines in pngmem.c, as these are required to allocate 64K, although
Guy Schalnate5a37791996-06-05 15:50:50 -05001685there is already support for many of the common DOS compilers. Also,
1686you will want to look into zconf.h to tell zlib (and thus libpng) that
1687it cannot allocate more then 64K at a time. Even if you can, the memory
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001688won't be accessible. So limit zlib and libpng to 64K by defining MAXSEG_64K.
Guy Schalnat0d580581995-07-20 02:43:20 -05001689
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001690Configuring for DOS:
1691
1692For DOS users which only have access to the lower 640K, you will
1693have to limit zlib's memory usage via a png_set_compression_mem_level()
1694call. See zlib.h or zconf.h in the zlib library for more information.
1695
Guy Schalnat6d764711995-12-19 03:22:19 -06001696Configuring for Medium Model:
1697
1698Libpng's support for medium model has been tested on most of the popular
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001699compilers. Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets
Guy Schalnat69b14481996-01-10 02:56:49 -06001700defined, and FAR gets defined to far in pngconf.h, and you should be
Guy Schalnat6d764711995-12-19 03:22:19 -06001701all set. Everything in the library (except for zlib's structure) is
1702expecting far data. You must use the typedefs with the p or pp on
1703the end for pointers (or at least look at them and be careful). Make
1704note that the row's of data are defined as png_bytepp which is a
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001705unsigned char far * far *.
Guy Schalnat6d764711995-12-19 03:22:19 -06001706
Guy Schalnat0d580581995-07-20 02:43:20 -05001707Configuring for gui/windowing platforms:
1708
Guy Schalnate5a37791996-06-05 15:50:50 -05001709You will need to write new error and warning functions that use the GUI
1710interface, as described previously, and set them to be the error and
1711warning functions at the time that png_create_???_struct() is called,
1712in order to have them available during the structure initialization.
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001713They can be changed later via png_set_error_fn(). On some compilers,
Guy Schalnate5a37791996-06-05 15:50:50 -05001714you may also have to change the memory allocators (png_malloc, etc.).
Guy Schalnat6d764711995-12-19 03:22:19 -06001715
Guy Schalnat0d580581995-07-20 02:43:20 -05001716Configuring for compiler xxx:
1717
Guy Schalnat69b14481996-01-10 02:56:49 -06001718All includes for libpng are in pngconf.h. If you need to add/change/delete
Guy Schalnat0d580581995-07-20 02:43:20 -05001719an include, this is the place to do it. The includes that are not
1720needed outside libpng are protected by the PNG_INTERNAL definition,
1721which is only defined for those routines inside libpng itself. The
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001722files in libpng proper only include png.h, which includes pngconf.h.
1723
1724Configuring zlib:
1725
Guy Schalnate5a37791996-06-05 15:50:50 -05001726There are special functions to configure the compression. Perhaps the
1727most useful one changes the compression level, which currently uses
1728input compression values in the range 0 - 9. The library normally
Andreas Dilger47a0c421997-05-16 02:46:07 -05001729uses the default compression level (Z_DEFAULT_COMPRESSION = 6). Tests
1730have shown that for a large majority of images, compression values in
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001731the range 3-6 compress nearly as well as higher levels, and do so much
1732faster. For online applications it may be desirable to have maximum speed
Guy Schalnate5a37791996-06-05 15:50:50 -05001733(Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also
1734specify no compression (Z_NO_COMPRESSION = 0), but this would create
1735files larger than just storing the raw bitmap. You can specify the
1736compression level by calling:
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001737
Andreas Dilger47a0c421997-05-16 02:46:07 -05001738 png_set_compression_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001739
1740Another useful one is to reduce the memory level used by the library.
1741The memory level defaults to 8, but it can be lowered if you are
1742short on memory (running DOS, for example, where you only have 640K).
1743
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001744 png_set_compression_mem_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001745
Andreas Dilger47a0c421997-05-16 02:46:07 -05001746The other functions are for configuring zlib. They are not recommended
1747for normal use and may result in writing an invalid PNG file. See
1748zlib.h for more information on what these mean.
1749
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001750 png_set_compression_strategy(png_ptr,
1751 strategy);
1752 png_set_compression_window_bits(png_ptr,
1753 window_bits);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001754 png_set_compression_method(png_ptr, method);
1755
1756Controlling row filtering:
1757
1758If you want to control whether libpng uses filtering or not, which
1759filters are used, and how it goes about picking row filters, you
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001760can call one of these functions. The selection and configuration
1761of row filters can have a significant impact on the size and
1762encoding speed and a somewhat lesser impact on the decoding speed
1763of an image. Filtering is enabled by default for RGB and grayscale
1764images (with and without alpha), and for 8-bit paletted images, but
1765not for paletted images with bit depths less than 8 bits/pixel.
1766
1767The 'method' parameter sets the main filtering method, which is
1768currently only '0' in the PNG 1.0 specification. The 'filters'
1769parameter sets which filter(s), if any, should be used for each
1770scanline. Possible values are PNG_ALL_FILTERS and PNG_NO_FILTERS
1771to turn filtering on and off, respectively.
1772
Guy Schalnate5a37791996-06-05 15:50:50 -05001773Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB,
1774PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise
1775ORed together '|' to specify one or more filters to use. These
1776filters are described in more detail in the PNG specification. If
1777you intend to change the filter type during the course of writing
1778the image, you should start with flags set for all of the filters
1779you intend to use so that libpng can initialize its internal
1780structures appropriately for all of the filter types.
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001781
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001782 filters = PNG_FILTER_NONE | PNG_FILTER_SUB
1783 | PNG_FILTER_UP;
1784 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE,
1785 filters);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001786
Andreas Dilger47a0c421997-05-16 02:46:07 -05001787It is also possible to influence how libpng chooses from among the
1788available filters. This is done in two ways - by telling it how
1789important it is to keep the same filter for successive rows, and
1790by telling it the relative computational costs of the filters.
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001791
Andreas Dilger47a0c421997-05-16 02:46:07 -05001792 double weights[3] = {1.5, 1.3, 1.1},
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001793 costs[PNG_FILTER_VALUE_LAST] =
1794 {1.0, 1.3, 1.3, 1.5, 1.7};
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001795
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001796 png_set_filter_selection(png_ptr,
1797 PNG_FILTER_SELECTION_WEIGHTED, 3,
1798 weights, costs);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001799
1800The weights are multiplying factors which indicate to libpng that row
1801should be the same for successive rows unless another row filter is that
1802many times better than the previous filter. In the above example, if
1803the previous 3 filters were SUB, SUB, NONE, the SUB filter could have a
1804"sum of absolute differences" 1.5 x 1.3 times higher than other filters
1805and still be chosen, while the NONE filter could have a sum 1.1 times
1806higher than other filters and still be chosen. Unspecified weights are
1807taken to be 1.0, and the specified weights should probably be declining
1808like those above in order to emphasize recent filters over older filters.
1809
1810The filter costs specify for each filter type a relative decoding cost
1811to be considered when selecting row filters. This means that filters
1812with higher costs are less likely to be chosen over filters with lower
1813costs, unless their "sum of absolute differences" is that much smaller.
1814The costs do not necessarily reflect the exact computational speeds of
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001815the various filters, since this would unduly influence the final image
Andreas Dilger47a0c421997-05-16 02:46:07 -05001816size.
1817
1818Note that the numbers above were invented purely for this example and
1819are given only to help explain the function usage. Little testing has
1820been done to find optimum values for either the costs or the weights.
Guy Schalnat0d580581995-07-20 02:43:20 -05001821
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001822Removing unwanted object code:
1823
Guy Schalnat69b14481996-01-10 02:56:49 -06001824There are a bunch of #define's in pngconf.h that control what parts of
Andreas Dilger47a0c421997-05-16 02:46:07 -05001825libpng are compiled. All the defines end in _SUPPORTED. If you are
Guy Schalnate5a37791996-06-05 15:50:50 -05001826never going to use an ability, you can change the #define to #undef
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001827before recompiling libpng and save yourself code and data space.
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001828You can also turn a number of them off en masse with a compiler directive
1829that defines PNG_READ[or WRITE]_TRANSFORMS_NOT_SUPPORTED, or
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001830PNG_READ[or WRITE]_ANCILLARY_CHUNKS_NOT_SUPPORTED, or all four,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001831along with directives to turn on any of the capabilities that you do
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001832want. The PNG_READ[or WRITE]_TRANSFORMS_NOT_SUPPORTED directives disable
1833the extra transformations but still leave the library fully capable of reading
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001834and writing PNG files with all known public chunks [except for sPLT].
1835Use of the PNG_READ[or WRITE]_ANCILLARY_CHUNKS_NOT_SUPPORTED directive
1836produces a library that is incapable of reading or writing ancillary chunks.
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001837If you are not using the progressive reading capability, you can
1838turn that off with PNG_PROGRESSIVE_READ_NOT_SUPPORTED (don't confuse
1839this with the INTERLACING capability, which you'll still have).
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001840
1841All the reading and writing specific code are in separate files, so the
Guy Schalnate5a37791996-06-05 15:50:50 -05001842linker should only grab the files it needs. However, if you want to
1843make sure, or if you are building a stand alone library, all the
1844reading files start with pngr and all the writing files start with
1845pngw. The files that don't match either (like png.c, pngtrans.c, etc.)
1846are used for both reading and writing, and always need to be included.
1847The progressive reader is in pngpread.c
1848
1849If you are creating or distributing a dynamically linked library (a .so
Andreas Dilger47a0c421997-05-16 02:46:07 -05001850or DLL file), you should not remove or disable any parts of the library,
1851as this will cause applications linked with different versions of the
1852library to fail if they call functions not available in your library.
1853The size of the library itself should not be an issue, because only
1854those sections which are actually used will be loaded into memory.
Guy Schalnate5a37791996-06-05 15:50:50 -05001855
Guy Schalnate5a37791996-06-05 15:50:50 -05001856
Andreas Dilger47a0c421997-05-16 02:46:07 -05001857Changes to Libpng from version 0.88
Guy Schalnate5a37791996-06-05 15:50:50 -05001858
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001859It should be noted that versions of libpng later than 0.96 are not
1860distributed by the original libpng author, Guy Schalnat, nor by
1861Andreas Dilger, who had taken over from Guy during 1996 and 1997, and
1862distributed versions 0.89 through 0.96, but rather by another member
1863of the original PNG Group, Glenn Randers-Pehrson. Guy and Andreas are
1864still alive and well, but they have moved on to other things.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001865
1866The old libpng functions png_read_init(), png_write_init(),
1867png_info_init(), png_read_destroy(), and png_write_destory() have been
1868moved to PNG_INTERNAL in version 0.95 to discourage their use. The
1869preferred method of creating and initializing the libpng structures is
1870via the png_create_read_struct(), png_create_write_struct(), and
Guy Schalnate5a37791996-06-05 15:50:50 -05001871png_create_info_struct() because they isolate the size of the structures
Andreas Dilger47a0c421997-05-16 02:46:07 -05001872from the application, allow version error checking, and also allow the
1873use of custom error handling routines during the initialization, which
1874the old functions do not. The functions png_read_destroy() and
1875png_write_destroy() do not actually free the memory that libpng
1876allocated for these structs, but just reset the data structures, so they
1877can be used instead of png_destroy_read_struct() and
1878png_destroy_write_struct() if you feel there is too much system overhead
1879allocating and freeing the png_struct for each image read.
Guy Schalnate5a37791996-06-05 15:50:50 -05001880
1881Setting the error callbacks via png_set_message_fn() before
1882png_read_init() as was suggested in libpng-0.88 is no longer supported
1883because this caused applications which do not use custom error functions
1884to fail if the png_ptr was not initialized to zero. It is still possible
1885to set the error callbacks AFTER png_read_init(), or to change them with
1886png_set_error_fn(), which is essentially the same function, but with a
Andreas Dilger47a0c421997-05-16 02:46:07 -05001887new name to force compilation errors with applications that try to use
1888the old method.
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001889