blob: 13af52e9ca041ca96eba25b271d2b02d12266ed6 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001libpng.txt - a description on how to use and modify libpng
2
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06003 libpng version 0.99
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06004 Updated and distributed by Glenn Randers-Pehrson <randeg@alumni.rpi.edu>
5 Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06006 January 30, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06007
8 based on:
9
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060010 libpng 1.0 beta 6 version 0.96
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060011 Updated and distributed by Andreas Dilger
Andreas Dilger47a0c421997-05-16 02:46:07 -050012 Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 May 28, 1997
Guy Schalnate5a37791996-06-05 15:50:50 -050014
Andreas Dilger47a0c421997-05-16 02:46:07 -050015 libpng 1.0 beta 2 - version 0.88
16 For conditions of distribution and use, see copyright notice in png.h
17 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
18 January 26, 1996
19
20 Updated/rewritten per request in the libpng FAQ
21 Copyright (c) 1995 Frank J. T. Wojcik
22 December 18, 1995 && January 20, 1996
Guy Schalnat69b14481996-01-10 02:56:49 -060023
24I. Introduction
Guy Schalnat0d580581995-07-20 02:43:20 -050025
26This file describes how to use and modify the PNG reference library
Guy Schalnat69b14481996-01-10 02:56:49 -060027(known as libpng) for your own use. There are five sections to this
28file: introduction, structures, reading, writing, and modification and
Guy Schalnat4ee97b01996-01-16 01:51:56 -060029configuration notes for various special platforms. In addition to this
Guy Schalnat69b14481996-01-10 02:56:49 -060030file, example.c is a good starting point for using the library, as
31it is heavily commented and should include everything most people
32will need.
Guy Schalnat0d580581995-07-20 02:43:20 -050033
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060034Libpng was written as a companion to the PNG specification, as a way
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060035of reducing the amount of time and effort it takes to support the PNG
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060036file format in application programs. The PNG specification is available
37as RFC 2083 <ftp://ftp.uu.net/graphics/png/documents/> and as a
38W3C Recommendation <http://www.w3.org/pub/WWW/TR/REC.png.html>. Some
39additional chunks are described in the special-purpose public chunks
40documents at <ftp://ftp.uu.net/graphics/png/documents/>. Other information
41about PNG can be found at the PNG home page, <http://www.cdrom.com/pub/png/>.
42
43Most users will not have to modify the library significantly; advanced
44users may want to modify it more. All attempts were made to make it as
45complete as possible, while keeping the code easy to understand.
46Currently, this library only supports C. Support for other languages
47is being considered.
Guy Schalnat0d580581995-07-20 02:43:20 -050048
49Libpng has been designed to handle multiple sessions at one time,
50to be easily modifiable, to be portable to the vast majority of
Andreas Dilger47a0c421997-05-16 02:46:07 -050051machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy
52to use. The ultimate goal of libpng is to promote the acceptance of
Guy Schalnat0d580581995-07-20 02:43:20 -050053the PNG file format in whatever way possible. While there is still
Andreas Dilger47a0c421997-05-16 02:46:07 -050054work to be done (see the TODO file), libpng should cover the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060055majority of the needs of its users.
Guy Schalnat0d580581995-07-20 02:43:20 -050056
57Libpng uses zlib for its compression and decompression of PNG files.
58The zlib compression utility is a general purpose utility that is
Guy Schalnat4ee97b01996-01-16 01:51:56 -060059useful for more than PNG files, and can be used without libpng.
Guy Schalnat69b14481996-01-10 02:56:49 -060060See the documentation delivered with zlib for more details.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060061You can usually find the source files for the zlib utility wherever you
62find the libpng source files.
Guy Schalnat0d580581995-07-20 02:43:20 -050063
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060064Libpng is thread safe, provided the threads are using different
Guy Schalnat4ee97b01996-01-16 01:51:56 -060065instances of the structures. Each thread should have its own
66png_struct and png_info instances, and thus its own image.
67Libpng does not protect itself against two threads using the
68same instance of a structure.
69
Guy Schalnat0d580581995-07-20 02:43:20 -050070
Guy Schalnat69b14481996-01-10 02:56:49 -060071
72II. Structures
Guy Schalnat0d580581995-07-20 02:43:20 -050073
74There are two main structures that are important to libpng, png_struct
75and png_info. The first, png_struct, is an internal structure that
Guy Schalnate5a37791996-06-05 15:50:50 -050076will not, for the most part, be used by a user except as the first
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060077variable passed to every libpng function call. It is not actually
78used in many of the functions; do not be alarmed about compiler
79warnings that say something to the effect that "png_ptr is not used."
Guy Schalnat0d580581995-07-20 02:43:20 -050080
81The png_info structure is designed to provide information about the
Andreas Dilger47a0c421997-05-16 02:46:07 -050082PNG file. At one time, the fields of png_info were intended to be
83directly accessible to the user. However, this tended to cause problems
84with applications using dynamically loaded libraries, and as a result
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060085a set of interface functions for png_info was developed. The fields
Andreas Dilger47a0c421997-05-16 02:46:07 -050086of png_info are still available for older applications, but it is
87suggested that applications use the new interfaces if at all possible.
Guy Schalnat0d580581995-07-20 02:43:20 -050088
Andreas Dilger47a0c421997-05-16 02:46:07 -050089The png.h header file is an invaluable reference for programming with libpng.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060090And while I'm on the topic, make sure you include the libpng header file:
Guy Schalnat0d580581995-07-20 02:43:20 -050091
92#include <png.h>
93
Guy Schalnat69b14481996-01-10 02:56:49 -060094
95
96III. Reading
97
Guy Schalnat0d580581995-07-20 02:43:20 -050098Reading PNG files:
99
Guy Schalnat69b14481996-01-10 02:56:49 -0600100We'll now walk you through the possible functions to call when reading
101in a PNG file, briefly explaining the syntax and purpose of each one.
102See example.c and png.h for more detail. While Progressive reading
103is covered in the next section, you will still need some of the
104functions discussed in this section to read a PNG file.
Guy Schalnat6d764711995-12-19 03:22:19 -0600105
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600106You will want to do the I/O initialization(*) before you get into libpng,
107so if it doesn't work, you don't have much to undo. Of course, you
108will also want to insure that you are, in fact, dealing with a PNG
109file. Libpng provides a simple check to see if a file is a PNG file.
110To use it, pass in the first 1 to 8 bytes of the file, and it will
111return true or false (1 or 0) depending on whether the bytes could be
112part of a PNG file. Of course, the more bytes you pass in, the
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600113greater the accuracy of the prediction.
114
115If you are intending to keep the file pointer open for use in libpng,
116you must ensure you don't read more than 8 bytes from the beginning
117of the file, and you also have to make a call to png_set_sig_bytes_read()
118with the number of bytes you read from the beginning. Libpng will
119then only check the bytes (if any) that your program didn't read.
Guy Schalnat0d580581995-07-20 02:43:20 -0500120
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600121(*): If you are not using the standard I/O functions, you will need
Guy Schalnat69b14481996-01-10 02:56:49 -0600122to replace them with custom functions. See the discussion under
123Customizing libpng.
124
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600125 FILE *fp = fopen(file_name, "rb");
126 if (!fp)
127 {
128 return;
129 }
130 fread(header, 1, number, fp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 is_png = png_check_sig(header, 0, number);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600132 if (!is_png)
133 {
134 return;
135 }
136
Andreas Dilger47a0c421997-05-16 02:46:07 -0500137Next, png_struct and png_info need to be allocated and initialized. In
138order to ensure that the size of these structures is correct even with a
139dynamically linked libpng, there are functions to initialize and
140allocate the structures. We also pass the library version, optional
141pointers to error handling functions, and a pointer to a data struct for
142use by the error functions, if necessary (the pointer and functions can
143be NULL if the default error handlers are to be used). See the section
144on Changes to Libpng below regarding the old initialization functions.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600145
Guy Schalnate5a37791996-06-05 15:50:50 -0500146 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
147 (void *)user_error_ptr, 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 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500154 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
155 return;
156 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500157
Guy Schalnate5a37791996-06-05 15:50:50 -0500158 png_infop end_info = png_create_info_struct(png_ptr);
159 if (!end_info)
160 {
161 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600162 return;
163 }
164
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600165The error handling routines passed to png_create_read_struct() are only
166necessary if you are not using the libpng supplied error handling
167functions. When libpng encounters an error, it expects to longjmp back
168to your routine. Therefore, you will need to call setjmp and pass the
169jmpbuf field of your png_struct. If you read the file from different
170routines, you will need to update the jmpbuf field every time you enter
171a new routine that will call a png_ function.
172
173See your documentation of setjmp/longjmp for your compiler for more
174information on setjmp/longjmp. See the discussion on libpng error
175handling in the Customizing Libpng section below for more information on
176the libpng error handling. If an error occurs, and libpng longjmp's
177back to your setjmp, you will want to call png_destroy_read_struct() to
178free any memory.
Guy Schalnat0d580581995-07-20 02:43:20 -0500179
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 if (setjmp(png_ptr->jmpbuf))
181 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500182 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600183 fclose(fp);
184 return;
185 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500186
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600187Now you need to set up the input code. The default for libpng is to
188use the C function fread(). If you use this, you will need to pass a
189valid FILE * in the function png_init_io(). Be sure that the file is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190opened in binary mode. If you wish to handle reading data in another
191way, you need not call the png_init_io() function, but you must then
192implement the libpng I/O methods discussed in the Customizing Libpng
193section below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500194
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600195 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500196
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600197If you had previously opened the file and read any of the signature from
198the beginning in order to see if this was a PNG file, you need to let
199libpng know that there are some bytes missing from the start of the file.
200
201 png_set_sig_bytes(png_ptr, number);
202
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600203In PNG files, the alpha channel in an image is the level of opacity.
204If you need the alpha channel in an image to be the level of transparency
205instead of opacity, you can invert the alpha channel (or the tRNS chunk
206data) after it's read, so that 0 is fully opaque and 255 (in 8-bit or
207paletted images) or 65535 (in 16-bit images) is fully transparent, with
208
209 png_set_invert_alpha(png_ptr);
210
211This has to appear here rather than later with the other transformations
212because the tRNS chunk data must be modified in the case of paletted images.
213If your image is not a paletted image, the tRNS data (which in such cases
214represents a single color to be rendered as transparent) won't be changed.
215
Guy Schalnat0d580581995-07-20 02:43:20 -0500216You are now ready to read all the file information up to the actual
217image data. You do this with a call to png_read_info().
218
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600219 png_read_info(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500220
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221Functions are used to get the information from the info_ptr:
Guy Schalnat69b14481996-01-10 02:56:49 -0600222
Andreas Dilger47a0c421997-05-16 02:46:07 -0500223 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
224 &interlace_type, &compression_type, &filter_type);
225
226 width - holds the width of the image in pixels (up to 2^31).
227 height - holds the height of the image in pixels (up to 2^31).
228 bit_depth - holds the bit depth of one of the image channels.
229 (valid values are 1, 2, 4, 8, 16 and depend also on the
230 color_type. See also significant bits (sBIT) below).
231 color_type - describes which color/alpha channels are present.
232 PNG_COLOR_TYPE_GRAY (bit depths 1, 2, 4, 8, 16)
233 PNG_COLOR_TYPE_GRAY_ALPHA (bit depths 8, 16)
234 PNG_COLOR_TYPE_PALETTE (bit depths 1, 2, 4, 8)
235 PNG_COLOR_TYPE_RGB (bit_depths 8, 16)
236 PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16)
237
238 PNG_COLOR_MASK_PALETTE
239 PNG_COLOR_MASK_COLOR
240 PNG_COLOR_MASK_ALPHA
241
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 filter_type - (must be PNG_FILTER_TYPE_BASE for PNG 1.0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600243 compression_type - (must be PNG_COMPRESSION_TYPE_BASE for PNG 1.0)
244 interlace_type - (PNG_INTERLACE_TYPE_NONE or PNG_INTERLACE_TYPE_ADAM7)
245 Any or all of interlace_type, compression_type, of filter_type can be
246 NULL if you are not interested in their values.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500247
248 channels = png_get_channels(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600249 channels - number of channels of info for the color type
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 (valid values are 1 (GRAY, PALETTE), 2 (GRAY_ALPHA),
251 3 (RGB), 4 (RGB_ALPHA or RGB + filler byte))
252 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600253 rowbytes - number of bytes needed to hold a row
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254
255 signature = png_get_signature(png_ptr, info_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600256 signature - holds the signature read from the file (if any). The
257 data is kept in the same offset it would be if the
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 whole signature were read (ie if an application had
259 already read in 4 bytes of signature before staring
260 libpng, the remaining 4 bytes would be in signature[4]
261 through signature[7] (see png_set_sig_bytes())).
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600262
263Information from each if the IHDR fields can be retrieve separately as well:
264
265 width = png_get_image_width(png_ptr, info_ptr);
266 height = png_get_image_height(png_ptr, info_ptr);
267 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
268 color_type = png_get_color_type(png_ptr, info_ptr);
269 filter_type = png_get_filter_type(png_ptr, info_ptr);
270 compression_type = png_get_compression_type(png_ptr, info_ptr);
271 interlace_type = png_get_interlace_type(png_ptr, info_ptr);
272
Guy Schalnat69b14481996-01-10 02:56:49 -0600273
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274These are also important, but their validity depends on whether the chunk
275has been read. The png_get_valid(png_ptr, info_ptr, PNG_INFO_<chunk>) and
276png_get_<chunk>(png_ptr, info_ptr, ...) functions return non-zero if the
277data has been read, or zero if it is missing. The parameters to the
278png_get_<chunk> are set directly if they are simple data types, or a pointer
279into the info_ptr is returned for any complex types.
Guy Schalnat69b14481996-01-10 02:56:49 -0600280
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281 png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
282 palette - the palette for the file (array of png_color)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600283 num_palette - number of entries in the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -0500284
285 png_get_gAMA(png_ptr, info_ptr, &gamma);
Guy Schalnate5a37791996-06-05 15:50:50 -0500286 gamma - the gamma the file is written at (PNG_INFO_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500287
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600288 png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
289 srgb_intent - the rendering intent (PNG_INFO_sRGB)
290 The presence of the sRGB chunk means that the pixel
291 data is in the sRGB color space. This chunk also
292 implies specific values of gAMA and cHRM.
293
Andreas Dilger47a0c421997-05-16 02:46:07 -0500294 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
295 sig_bit - the number of significant bits for (PNG_INFO_sBIT)
296 the gray, red, green, and blue channels, whichever
297 are appropriate for the given color type (png_color_16)
298
299 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values);
300 trans - array of transparent entries for palette (PNG_INFO_tRNS)
Guy Schalnate5a37791996-06-05 15:50:50 -0500301 trans_values - transparent pixel for non-paletted images (PNG_INFO_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302 num_trans - number of transparent entries (PNG_INFO_tRNS)
303
304 png_get_hIST(png_ptr, info_ptr, &hist); (PNG_INFO_hIST)
305 hist - histogram of palette (array of png_color_16)
306
307 png_get_tIME(png_ptr, info_ptr, &mod_time);
Guy Schalnate5a37791996-06-05 15:50:50 -0500308 mod_time - time image was last modified (PNG_VALID_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309
310 png_get_bKGD(png_ptr, info_ptr, &background);
Guy Schalnate5a37791996-06-05 15:50:50 -0500311 background - background color (PNG_VALID_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312
313 num_text = png_get_text(png_ptr, info_ptr, &text_ptr);
314 text_ptr - array of png_text holding image comments
315 text_ptr[i]->key - keyword for comment.
316 text_ptr[i]->text - text comments for current keyword.
317 text_ptr[i]->compression - type of compression used on "text"
318 PNG_TEXT_COMPRESSION_NONE or
319 PNG_TEXT_COMPRESSION_zTXt
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600320 num_text - number of comments
Guy Schalnat69b14481996-01-10 02:56:49 -0600321
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y, &unit_type);
323 offset_x - positive offset from the left edge of the screen
324 offset_y - positive offset from the top edge of the screen
325 unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
326
327 png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, &unit_type);
328 res_x - pixels/unit physical resolution in x direction
329 res_y - pixels/unit physical resolution in x direction
330 unit_type - PNG_RESOLUTION_UNKOWN, PNG_RESOLUTION_METER
331
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600332The data from the pHYs chunk can be retrieved in several convenient
333forms:
334
335 res_x_and_y = png_get_pixels_per_meter(png_ptr, info_ptr)
336 aspect_ratio = png_get_pixel_aspect_ratio(png_ptr, info_ptr)
337
338 (Each of these returns 0 [signifying "unknown"] if the data is not
339 present or if res_x is 0; res_x_and_y is 0 if res_x != res_y)
340
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341For more information, see the png_info definition in png.h and the
Guy Schalnat0d580581995-07-20 02:43:20 -0500342PNG specification for chunk contents. Be careful with trusting
343rowbytes, as some of the transformations could increase the space
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344needed to hold a row (expand, filler, gray_to_rgb, etc.).
345See png_read_update_info(), below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500346
Andreas Dilger47a0c421997-05-16 02:46:07 -0500347A quick word about text_ptr and num_text. PNG stores comments in
348keyword/text pairs, one pair per chunk, with no limit on the number
349of text chunks, and a 2^31 byte limit on their size. While there are
350suggested keywords, there is no requirement to restrict the use to these
351strings. It is strongly suggested that keywords and text be sensible
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600352to humans (that's the point), so don't use abbreviations. Non-printing
353symbols are not allowed. See the PNG specification for more details.
354There is also no requirement to have text after the keyword.
Guy Schalnat0d580581995-07-20 02:43:20 -0500355
Andreas Dilger47a0c421997-05-16 02:46:07 -0500356Keywords should be limited to 79 Latin-1 characters without leading or
357trailing spaces, but non-consecutive spaces are allowed within the
358keyword. It is possible to have the same keyword any number of times.
359The text_ptr is an array of png_text structures, each holding pointer
360to a keyword and a pointer to a text string. Only the text string may
361be null. The keyword/text pairs are put into the array in the order
362that they are received. However, some or all of the text chunks may be
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600363after the image, so, to make sure you have read all the text chunks,
Guy Schalnat0d580581995-07-20 02:43:20 -0500364don't mess with these until after you read the stuff after the image.
365This will be mentioned again below in the discussion that goes with
366png_read_end().
367
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368After you've read the header information, you can set up the library
369to handle any special transformations of the image data. The various
Guy Schalnat0d580581995-07-20 02:43:20 -0500370ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600371should occur. This is important, as some of these change the color
372type and/or bit depth of the data, and some others only work on
373certain color types and bit depths. Even though each transformation
374checks to see if it has data that it can do somthing with, you should
375make sure to only enable a transformation if it will be valid for the
376data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500377
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600378The colors used for the background and transparency values should be
379supplied in the same format/depth as the current image data. They
380are stored in the same format/depth as the image data in a bKGD or tRNS
381chunk, so this is what libpng expects for this data. The colors are
382transformed to keep in sync with the image data when an application
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383calls the png_read_update_info() routine (see below).
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600384
Guy Schalnate5a37791996-06-05 15:50:50 -0500385Data will be decoded into the supplied row buffers packed into bytes
386unless the library has been told to transform it into another format.
387For example, 4 bit/pixel paletted or grayscale data will be returned
3882 pixels/byte with the leftmost pixel in the high-order bits of the
389byte, unless png_set_packing() is called. 8-bit RGB data will be stored
390in RGBRGBRGB format unless png_set_filler() is called to insert filler
391bytes, either before or after each RGB triplet. 16-bit RGB data will
392be returned RRGGBBRRGGBB, with the most significant byte of the color
393value first, unless png_set_strip_16() is called to transform it to
394regular RGBRGB triplets.
395
396The following code transforms grayscale images of less than 8 to 8 bits,
397changes paletted images to RGB, and adds a full alpha channel if there is
398transparency information in a tRNS chunk. This is most useful on
399grayscale images with bit depths of 2 or 4 or if there is a multiple-image
400viewing application that wishes to treat all images in the same way.
Guy Schalnat0d580581995-07-20 02:43:20 -0500401
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600402 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600403 png_set_expand(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500404
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
406 png_set_expand(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500407
Andreas Dilger47a0c421997-05-16 02:46:07 -0500408 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
409 png_set_expand(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500410
Guy Schalnate5a37791996-06-05 15:50:50 -0500411PNG can have files with 16 bits per channel. If you only can handle
4128 bits per channel, this will strip the pixels down to 8 bit.
413
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 if (bit_depth == 16)
415 png_set_strip_16(png_ptr);
416
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600417The png_set_background() function tells libpng to composite images
418with alpha or simple transparency against the supplied background
419color. If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
420you may use this color, or supply another color more suitable for
421the current display (e.g., the background color from a web page). You
422need to tell libpng whether the color is in the gamma space of the
423display (PNG_BACKGROUND_GAMMA_SCREEN for colors you supply), the file
424(PNG_BACKGROUND_GAMMA_FILE for colors from the bKGD chunk), or one
425that is neither of these gammas (PNG_BACKGROUND_GAMMA_UNIQUE - I don't
426know why anyone would use this, but it's here).
427
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428If, for some reason, you don't need the alpha channel on an image,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600429and you want to remove it rather than combining it with the background
430(but the image author certainly had in mind that you *would* combine
431it with the background, so that's what you should probably do):
Andreas Dilger47a0c421997-05-16 02:46:07 -0500432
433 if (color_type & PNG_COLOR_MASK_ALPHA)
434 png_set_strip_alpha(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500435
436PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
437they can, resulting in, for example, 8 pixels per byte for 1 bit
438files. This code expands to 1 pixel per byte without changing the
439values of the pixels:
440
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 if (bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500442 png_set_packing(png_ptr);
443
Andreas Dilger47a0c421997-05-16 02:46:07 -0500444PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixels
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600445stored in a PNG image have been "scaled" or "shifted" up to the next
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446higher possible bit depth (eg from 5 bits/sample in the range [0,31] to
4478 bits/sample in the range [0, 255]). However, it is also possible to
448convert the PNG pixel data back to the original bit depth of the image.
449This call reduces the pixels back down to the original bit depth:
Guy Schalnate5a37791996-06-05 15:50:50 -0500450
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 png_color_16p sig_bit;
Guy Schalnate5a37791996-06-05 15:50:50 -0500452
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
454 png_set_shift(png_ptr, sig_bit);
455
456PNG files store 3-color pixels in red, green, blue order. This code
Guy Schalnate5a37791996-06-05 15:50:50 -0500457changes the storage of the pixels to blue, green, red:
458
Andreas Dilger47a0c421997-05-16 02:46:07 -0500459 if (color_type == PNG_COLOR_TYPE_RGB ||
460 color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnate5a37791996-06-05 15:50:50 -0500461 png_set_bgr(png_ptr);
462
463PNG files store RGB pixels packed into 3 bytes. This code expands them
464into 4 bytes for windowing systems that need them in this format:
465
Andreas Dilger47a0c421997-05-16 02:46:07 -0500466 if (bit_depth == 8 && color_type == PNG_COLOR_TYPE_RGB)
467 png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE);
Guy Schalnate5a37791996-06-05 15:50:50 -0500468
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469where "filler" is the number to fill with, and the location is
Guy Schalnate5a37791996-06-05 15:50:50 -0500470either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471you want the filler before the RGB or after. This transformation
472does not affect images that already have full alpha channels.
Guy Schalnate5a37791996-06-05 15:50:50 -0500473
Andreas Dilger47a0c421997-05-16 02:46:07 -0500474If you are reading an image with an alpha channel, and you need the
475data as ARGB instead of the normal PNG format RGBA:
476
477 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
478 png_set_swap_alpha(png_ptr);
479
480For some uses, you may want a grayscale image to be represented as
Guy Schalnate5a37791996-06-05 15:50:50 -0500481RGB. This code will do that conversion:
482
Andreas Dilger47a0c421997-05-16 02:46:07 -0500483 if (color_type == PNG_COLOR_TYPE_GRAY ||
484 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
485 png_set_gray_to_rgb(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500486
Andreas Dilger47a0c421997-05-16 02:46:07 -0500487If you have a grayscale and you are using png_set_expand() to change to
488a higher bit-depth you must indicate if the supplied background gray
489is supplied in the original file bit depth (need_expand = 1) or in the
490expanded bit depth (need_expand = 0). Similarly, if you are reading
Guy Schalnate5a37791996-06-05 15:50:50 -0500491a paletted image, you must indicate if you have supplied the background
Andreas Dilger47a0c421997-05-16 02:46:07 -0500492as a palette index that needs to be expanded (need_expand = 1). You can
493also specify an RGB triplet that isn't in the palette when setting your
494background for a paletted image.
Guy Schalnat0d580581995-07-20 02:43:20 -0500495
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 png_color_16 my_background;
497 png_color_16p image_background;
Guy Schalnat0d580581995-07-20 02:43:20 -0500498
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
500 png_set_background(png_ptr, image_background),
501 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
502 else
503 png_set_background(png_ptr, &my_background,
504 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
505
506To properly display PNG images on any kind of system, the application needs
507to know what the display gamma is. Ideally, the user will know this, and
508the application will allow them to set it. One method of allowing the user
509to set the display gamma separately for each system is to check for the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600510DISPLAY_GAMMA and VIEWING_GAMMA environment variables or for a SCREEN_GAMMA
511environment variable, which will hopefully be correctly set.
512
513Note that display_gamma is the gamma of your display, while screen_gamma is
514the overall gamma correction required to produce pleasing results,
515which depends on the lighting conditions in the surrounding environment.
516Screen_gamma is display_gamma/viewing_gamma, where viewing_gamma is
517the amount of additional gamma correction needed to compensate for
518a dim (viewing_gamma=1.125) or dark (viewing_gamma=1.25) environment.
519In a brightly lit room, no compensation other than the display_gamma
520is needed (viewing_gamma=1.0).
Andreas Dilger47a0c421997-05-16 02:46:07 -0500521
522 if (/* We have a user-defined screen gamma value */)
523 {
524 screen_gamma = user_defined_screen_gamma;
525 }
526 /* One way that applications can share the same screen gamma value */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600527 else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 {
529 screen_gamma = atof(gamma_str);
530 }
531 /* If we don't have another value */
Guy Schalnat0d580581995-07-20 02:43:20 -0500532 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600534 screen_gamma = 2.5; /* A good guess for a PC monitor in a bright office */
535 screen_gamma = 2.2; /* A good guess for a PC monitor in a dim room */
536 screen_gamma = 2.0; /* A good guess for a PC monitor in a dark room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500537 screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */
538 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500539
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540The png_set_gamma() function handles gamma transformations of the data.
541Pass both the file gamma and the current screen_gamma. If the file does
542not have a gamma value, you can pass one anyway if you have an idea what
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600543it is (usually 0.50 is a good guess for GIF images on PCs). Note
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600544that file gammas are inverted from screen gammas. See the discussions
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545on gamma in the PNG specification for an excellent description of what
546gamma is, and why all applications should support it. It is strongly
547recommended that PNG viewers support gamma correction.
Guy Schalnat0d580581995-07-20 02:43:20 -0500548
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 if (png_get_gAMA(png_ptr, info_ptr, &gamma))
550 png_set_gamma(png_ptr, screen_gamma, gamma);
Guy Schalnate5a37791996-06-05 15:50:50 -0500551 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600552 png_set_gamma(png_ptr, screen_gamma, 0.50);
Guy Schalnat0d580581995-07-20 02:43:20 -0500553
Guy Schalnate5a37791996-06-05 15:50:50 -0500554If you need to reduce an RGB file to a paletted file, or if a paletted
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600555file has more entries then will fit on your screen, png_set_dither()
556will do that. Note that this is a simple match dither that merely
557finds the closest color available. This should work fairly well with
558optimized palettes, and fairly badly with linear color cubes. If you
559pass a palette that is larger then maximum_colors, the file will
560reduce the number of colors in the palette so it will fit into
561maximum_colors. If there is a histogram, it will use it to make
Guy Schalnate5a37791996-06-05 15:50:50 -0500562more intelligent choices when reducing the palette. If there is no
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600563histogram, it may not do as good a job.
Guy Schalnat69b14481996-01-10 02:56:49 -0600564
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500567 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600568 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 png_color_16p histogram;
570
571 png_get_hIST(png_ptr, info_ptr, &histogram);
572 png_set_dither(png_ptr, palette, num_palette, max_screen_colors,
573 histogram, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600574 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 else
576 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600577 png_color std_color_cube[MAX_SCREEN_COLORS] =
Guy Schalnat0d580581995-07-20 02:43:20 -0500578 { ... colors ... };
579
580 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600581 MAX_SCREEN_COLORS, NULL,0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500582 }
583 }
584
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600585PNG files describe monochrome as black being zero and white being one.
586The following code will reverse this (make black be one and white be
587zero):
Guy Schalnat0d580581995-07-20 02:43:20 -0500588
Andreas Dilger47a0c421997-05-16 02:46:07 -0500589 if (bit_depth == 1 && color_type == PNG_COLOR_GRAY)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600590 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500591
Guy Schalnat69b14481996-01-10 02:56:49 -0600592PNG files store 16 bit pixels in network byte order (big-endian,
593ie. most significant bits first). This code chages the storage to the
594other way (little-endian, ie. least significant bits first, eg. the
595way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -0500596
Andreas Dilger47a0c421997-05-16 02:46:07 -0500597 if (bit_depth == 16)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600598 png_set_swap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500599
Andreas Dilger47a0c421997-05-16 02:46:07 -0500600If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
601need to change the order the pixels are packed into bytes, you can use:
602
603 if (bit_depth < 8)
604 png_set_packswap(png_ptr);
605
Guy Schalnat69b14481996-01-10 02:56:49 -0600606The last thing to handle is interlacing; this is covered in detail below,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600607but you must call the function here if you want libpng to handle expansion
608of the interlaced image.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500609
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600610 number_of_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500611
Guy Schalnate5a37791996-06-05 15:50:50 -0500612After setting the transformations, libpng can update your png_info
613structure to reflect any transformations you've requested with this
Andreas Dilger47a0c421997-05-16 02:46:07 -0500614call. This is most useful to update the info structure's rowbytes
615field so you can use it to allocate your image memory. This function
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600616will also update your palette with the correct screen_gamma and
Guy Schalnate5a37791996-06-05 15:50:50 -0500617background if these have been given with the calls above.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500618
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600619 png_read_update_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500620
621After you call png_read_update_info(), you can allocate any
Guy Schalnate5a37791996-06-05 15:50:50 -0500622memory you need to hold the image. The row data is simply
623raw byte data for all forms of images. As the actual allocation
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500624varies among applications, no example will be given. If you
Guy Schalnate5a37791996-06-05 15:50:50 -0500625are allocating one large chunk, you will need to build an
626array of pointers to each row, as it will be needed for some
627of the functions below.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500628
629After you've allocated memory, you can read the image data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500630The simplest way to do this is in one function call. If you are
631allocating enough memory to hold the whole image, you can just
632call png_read_image() and libpng will read in all the image data
633and put it in the memory area supplied. You will need to pass in
634an array of pointers to each row.
635
636This function automatically handles interlacing, so you don't need
637to call png_set_interlace_handling() or call this function multiple
638times, or any of that other stuff necessary with png_read_rows().
639
640 png_read_image(png_ptr, row_pointers);
641
642where row_pointers is:
643
Guy Schalnate5a37791996-06-05 15:50:50 -0500644 png_bytep row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -0500645
646You can point to void or char or whatever you use for pixels.
647
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600648If you don't want to read in the whole image at once, you can
Guy Schalnat0d580581995-07-20 02:43:20 -0500649use png_read_rows() instead. If there is no interlacing (check
Andreas Dilger47a0c421997-05-16 02:46:07 -0500650interlace_type == PNG_INTERLACE_TYPE_NONE), this is simple:
Guy Schalnat0d580581995-07-20 02:43:20 -0500651
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600652 png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500653
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600654where row_pointers is the same as in the png_read_image() call.
Guy Schalnat0d580581995-07-20 02:43:20 -0500655
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600656If you are doing this just one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -0500657row_pointers:
658
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600659 png_bytep row_pointers = row;
Guy Schalnate5a37791996-06-05 15:50:50 -0500660 png_read_row(png_ptr, &row_pointers, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500661
Guy Schalnat69b14481996-01-10 02:56:49 -0600662If the file is interlaced (info_ptr->interlace_type != 0), things get
Andreas Dilger47a0c421997-05-16 02:46:07 -0500663somewhat harder. The only current (PNG Specification version 1.0)
664interlacing type for PNG is (interlace_type == PNG_INTERLACE_TYPE_ADAM7)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600665is a somewhat complicated 2D interlace scheme, known as Adam7, that
Andreas Dilger47a0c421997-05-16 02:46:07 -0500666breaks down an image into seven smaller images of varying size, based
667on an 8x8 grid.
Guy Schalnat0d580581995-07-20 02:43:20 -0500668
Guy Schalnate5a37791996-06-05 15:50:50 -0500669libpng can fill out those images or it can give them to you "as is".
670If you want them filled out, there are two ways to do that. The one
671mentioned in the PNG specification is to expand each pixel to cover
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600672those pixels that have not been read yet (the "rectangle" method).
673This results in a blocky image for the first pass, which gradually
674smooths out as more pixels are read. The other method is the "sparkle"
675method, where pixels are drawn only in their final locations, with the
676rest of the image remaining whatever colors they were initialized to
677before the start of the read. The first method usually looks better,
678but tends to be slower, as there are more pixels to put in the rows.
Guy Schalnate5a37791996-06-05 15:50:50 -0500679
680If you don't want libpng to handle the interlacing details, just call
681png_read_rows() seven times to read in all seven images. Each of the
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600682images is a valid image by itself, or they can all be combined on an
Guy Schalnate5a37791996-06-05 15:50:50 -05006838x8 grid to form a single image (although if you intend to combine them
684you would be far better off using the libpng interlace handling).
685
686The first pass will return an image 1/8 as wide as the entire image
687(every 8th column starting in column 0) and 1/8 as high as the original
688(every 8th row starting in row 0), the second will be 1/8 as wide
689(starting in column 4) and 1/8 as high (also starting in row 0). The
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600690third pass will be 1/4 as wide (every 4th pixel starting in column 0) and
Guy Schalnate5a37791996-06-05 15:50:50 -05006911/8 as high (every 8th row starting in row 4), and the fourth pass will
692be 1/4 as wide and 1/4 as high (every 4th column starting in column 2,
693and every 4th row starting in row 0). The fifth pass will return an
694image 1/2 as wide, and 1/4 as high (starting at column 0 and row 2),
695while the sixth pass will be 1/2 as wide and 1/2 as high as the original
696(starting in column 1 and row 0). The seventh and final pass will be as
697wide as the original, and 1/2 as high, containing all of the odd
698numbered scanlines. Phew!
Guy Schalnat0d580581995-07-20 02:43:20 -0500699
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600700If you want libpng to expand the images, call this before calling
701png_start_read_image() or png_read_update_info():
Guy Schalnat0d580581995-07-20 02:43:20 -0500702
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703 if (interlace_type == PNG_INTERLACE_TYPE_ADAM7)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600704 number_of_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500705
706This will return the number of passes needed. Currently, this
707is seven, but may change if another interlace type is added.
708This function can be called even if the file is not interlaced,
Guy Schalnate5a37791996-06-05 15:50:50 -0500709where it will return one pass.
Guy Schalnat0d580581995-07-20 02:43:20 -0500710
711If you are not going to display the image after each pass, but are
712going to wait until the entire image is read in, use the sparkle
713effect. This effect is faster and the end result of either method
714is exactly the same. If you are planning on displaying the image
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600715after each pass, the "rectangle" effect is generally considered the
Guy Schalnat0d580581995-07-20 02:43:20 -0500716better looking one.
717
718If you only want the "sparkle" effect, just call png_read_rows() as
719normal, with the third parameter NULL. Make sure you make pass over
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600720the image number_of_passes times, and you don't change the data in the
Guy Schalnat0d580581995-07-20 02:43:20 -0500721rows between calls. You can change the locations of the data, just
722not the data. Each pass only writes the pixels appropriate for that
723pass, and assumes the data from previous passes is still valid.
724
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600725 png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500726
727If you only want the first effect (the rectangles), do the same as
728before except pass the row buffer in the third parameter, and leave
729the second parameter NULL.
730
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600731 png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500732
733After you are finished reading the image, you can finish reading
Guy Schalnate5a37791996-06-05 15:50:50 -0500734the file. If you are interested in comments or time, which may be
735stored either before or after the image data, you should pass the
736info_ptr pointer from the png_read_info() call, or you can pass a
737separate png_info struct if you want to keep the comments from
738before and after the image separate. If you are not interested, you
739can pass NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -0500740
Guy Schalnate5a37791996-06-05 15:50:50 -0500741 png_read_end(png_ptr, end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500742
Guy Schalnate5a37791996-06-05 15:50:50 -0500743When you are done, you can free all memory allocated by libpng like this:
Guy Schalnat0d580581995-07-20 02:43:20 -0500744
Guy Schalnate5a37791996-06-05 15:50:50 -0500745 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500746
Guy Schalnate5a37791996-06-05 15:50:50 -0500747For a more compact example of reading a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -0500748
749
Guy Schalnat6d764711995-12-19 03:22:19 -0600750Reading PNG files progressively:
751
752The progressive reader is slightly different then the non-progressive
753reader. Instead of calling png_read_info(), png_read_rows(), and
754png_read_end(), you make one call to png_process_data(), which calls
755callbacks when it has the info, a row, or the end of the image. You
756set up these callbacks with png_set_progressive_read_fn(). You don't
757have to worry about the input/output functions of libpng, as you are
758giving the library the data directly in png_process_data(). I will
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600759assume that you have read the section on reading PNG files above,
Guy Schalnat6d764711995-12-19 03:22:19 -0600760so I will only highlight the differences (although I will show
761all of the code).
762
763png_structp png_ptr;
764png_infop info_ptr;
765
Guy Schalnate5a37791996-06-05 15:50:50 -0500766/* An example code fragment of how you would initialize the progressive
767 reader in your application. */
Guy Schalnat6d764711995-12-19 03:22:19 -0600768int
769initialize_png_reader()
770{
Guy Schalnate5a37791996-06-05 15:50:50 -0500771 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
772 (void *)user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600773 if (!png_ptr)
774 return -1;
Guy Schalnate5a37791996-06-05 15:50:50 -0500775 info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600776 if (!info_ptr)
777 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500778 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600779 return -1;
780 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600781
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600782 if (setjmp(png_ptr->jmpbuf))
783 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500784 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600785 return -1;
786 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600787
Guy Schalnate5a37791996-06-05 15:50:50 -0500788 /* This one's new. You can provide functions to be called
789 when the header info is valid, when each row is completed,
790 and when the image is finished. If you aren't using all
791 functions, you can specify a NULL parameter. You can use
792 any struct as the user_ptr (cast to a void pointer for the
793 function call), and retrieve the pointer from inside the
794 callbacks using the function png_get_progressive_ptr(png_ptr);
795 which will return a void pointer, which you have to cast
796 appropriately.
797 */
798 png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600799 info_callback, row_callback, end_callback);
Guy Schalnat6d764711995-12-19 03:22:19 -0600800
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600801 return 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600802}
803
Guy Schalnate5a37791996-06-05 15:50:50 -0500804/* A code fragment that you call as you recieve blocks of data */
Guy Schalnat6d764711995-12-19 03:22:19 -0600805int
806process_data(png_bytep buffer, png_uint_32 length)
807{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600808 if (setjmp(png_ptr->jmpbuf))
809 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500810 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600811 return -1;
812 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600813
Guy Schalnate5a37791996-06-05 15:50:50 -0500814 /* This one's new also. Simply give it a chunk of data
815 from the file stream (in order, of course). On machines
816 with segmented memory models machines, don't give it any
817 more than 64K. The library seems to run fine with sizes
818 of 4K. Although you can give it much less if necessary
819 (I assume you can give it chunks of 1 byte, I haven't
820 tried less then 256 bytes yet). When this function returns,
821 you may want to display any rows that were generated in the
822 row callback if you don't already do so there.
823 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600824 png_process_data(png_ptr, info_ptr, buffer, length);
825 return 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600826}
827
Guy Schalnate5a37791996-06-05 15:50:50 -0500828/* This function is called (as set by png_set_progressive_fn() above)
829 when enough data has been supplied so all of the header has been read.
830 */
831void
Guy Schalnat6d764711995-12-19 03:22:19 -0600832info_callback(png_structp png_ptr, png_infop info)
833{
Guy Schalnate5a37791996-06-05 15:50:50 -0500834 /* Do any setup here, including setting any of the transformations
835 mentioned in the Reading PNG files section. For now, you _must_
836 call either png_start_read_image() or png_read_update_info()
837 after all the transformations are set (even if you don't set
838 any). You may start getting rows before png_process_data()
839 returns, so this is your last chance to prepare for that.
840 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600841}
842
Guy Schalnate5a37791996-06-05 15:50:50 -0500843/* This function is called when each row of image data is complete */
844void
Guy Schalnat6d764711995-12-19 03:22:19 -0600845row_callback(png_structp png_ptr, png_bytep new_row,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600846 png_uint_32 row_num, int pass)
Guy Schalnat6d764711995-12-19 03:22:19 -0600847{
Guy Schalnate5a37791996-06-05 15:50:50 -0500848 /* If the image is interlaced, and you turned on the interlace
849 handler, this function will be called for every row in every pass.
850 Some of these rows will not be changed from the previous pass.
851 When the row is not changed, the new_row variable will be NULL.
852 The rows and passes are called in order, so you don't really
853 need the row_num and pass, but I'm supplying them because it
854 may make your life easier.
Guy Schalnat6d764711995-12-19 03:22:19 -0600855
Guy Schalnate5a37791996-06-05 15:50:50 -0500856 For the non-NULL rows of interlaced images, you must call
857 png_progressive_combine_row() passing in the row and the
858 old row. You can call this function for NULL rows (it will
859 just return) and for non-interlaced images (it just does the
860 memcpy for you) if it will make the code easier. Thus, you
861 can just do this for all cases:
862 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600863
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600864 png_progressive_combine_row(png_ptr, old_row, new_row);
Guy Schalnat6d764711995-12-19 03:22:19 -0600865
Guy Schalnate5a37791996-06-05 15:50:50 -0500866 /* where old_row is what was displayed for previous rows. Note
867 that the first pass (pass == 0, really) will completely cover
868 the old row, so the rows do not have to be initialized. After
869 the first pass (and only for interlaced images), you will have
870 to pass the current row, and the function will combine the
871 old row and the new row.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600872 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600873}
874
Guy Schalnate5a37791996-06-05 15:50:50 -0500875void
Guy Schalnat6d764711995-12-19 03:22:19 -0600876end_callback(png_structp png_ptr, png_infop info)
877{
Guy Schalnate5a37791996-06-05 15:50:50 -0500878 /* This function is called after the whole image has been read,
879 including any chunks after the image (up to and including
880 the IEND). You will usually have the same info chunk as you
881 had in the header, although some data may have been added
882 to the comments and time fields.
Guy Schalnat6d764711995-12-19 03:22:19 -0600883
Guy Schalnate5a37791996-06-05 15:50:50 -0500884 Most people won't do much here, perhaps setting a flag that
885 marks the image as finished.
886 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600887}
888
889
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600890
Guy Schalnat69b14481996-01-10 02:56:49 -0600891IV. Writing
Guy Schalnat0d580581995-07-20 02:43:20 -0500892
893Much of this is very similar to reading. However, everything of
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600894importance is repeated here, so you won't have to constantly look
Guy Schalnat69b14481996-01-10 02:56:49 -0600895back up in the reading section to understand writing.
Guy Schalnat0d580581995-07-20 02:43:20 -0500896
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600897You will want to do the I/O initialization before you get into libpng,
Guy Schalnate5a37791996-06-05 15:50:50 -0500898so if it doesn't work, you don't have anything to undo. If you are not
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600899using the standard I/O functions, you will need to replace them with
Guy Schalnate5a37791996-06-05 15:50:50 -0500900custom writing functions. See the discussion under Customizing libpng.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600901
902 FILE *fp = fopen(file_name, "wb");
903 if (!fp)
904 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500905 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600906 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500907
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600908Next, png_struct and png_info need to be allocated and initialized.
Guy Schalnate5a37791996-06-05 15:50:50 -0500909As these can be both relatively large, you may not want to store these
910on the stack, unless you have stack space to spare. Of course, you
911will want to check if they return NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -0500912
Guy Schalnate5a37791996-06-05 15:50:50 -0500913 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
914 (void *)user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600915 if (!png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500916 return;
917
918 png_infop info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600919 if (!info_ptr)
920 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500921 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
922 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600923 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500924
925After you have these structures, you will need to set up the
926error handling. When libpng encounters an error, it expects to
Guy Schalnate5a37791996-06-05 15:50:50 -0500927longjmp() back to your routine. Therefore, you will need to call
Guy Schalnat0d580581995-07-20 02:43:20 -0500928setjmp and pass the jmpbuf field of your png_struct. If you
929write the file from different routines, you will need to update
930the jmpbuf field every time you enter a new routine that will
931call a png_ function. See your documentation of setjmp/longjmp
Guy Schalnat6d764711995-12-19 03:22:19 -0600932for your compiler for more information on setjmp/longjmp. See
Guy Schalnat69b14481996-01-10 02:56:49 -0600933the discussion on libpng error handling in the Customizing Libpng
934section below for more information on the libpng error handling.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600935
936 if (setjmp(png_ptr->jmpbuf))
937 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500938 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600939 fclose(fp);
940 return;
941 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500942
Andreas Dilger47a0c421997-05-16 02:46:07 -0500943Now you need to set up the output code. The default for libpng is to
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600944use the C function fwrite(). If you use this, you will need to pass a
945valid FILE * in the function png_init_io(). Be sure that the file is
946opened in binary mode. Again, if you wish to handle writing data in
947another way, see the discussion on libpng I/O handling in the Customizing
948Libpng section below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500949
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600950 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500951
Guy Schalnate5a37791996-06-05 15:50:50 -0500952You now have the option of modifying how the compression library will
953run. The following functions are mainly for testing, but may be useful
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600954in some cases, like if you need to write PNG files extremely fast and
Guy Schalnate5a37791996-06-05 15:50:50 -0500955are willing to give up some compression, or if you want to get the
956maximum possible compression at the expense of slower writing. If you
957have no special needs in this area, let the library do what it wants by
958not calling this function at all, as it has been tuned to deliver a good
959speed/compression ratio. The second parameter to png_set_filter() is
960the filter method, for which the only valid value is '0' (as of the
96106/96 PNG specification. The third parameter is a flag that indicates
962which filter type(s) are to be tested for each scanline. See the
963Compression Library for details on the specific filter types.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500964
Guy Schalnate5a37791996-06-05 15:50:50 -0500965
966 /* turn on or off filtering, and/or choose specific filters */
967 png_set_filter(png_ptr, 0,
968 PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_PAETH);
969
970The png_set_compression_???() functions interface to the zlib compression
971library, and should mostly be ignored unless you really know what you are
972doing. The only generally useful call is png_set_compression_level()
973which changes how much time zlib spends on trying to compress the image
974data. See the Compression Library for details on the compression levels.
975
976 /* set the zlib compression level */
977 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
978
979 /* set other zlib parameters */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600980 png_set_compression_mem_level(png_ptr, 8);
981 png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
982 png_set_compression_window_bits(png_ptr, 15);
983 png_set_compression_method(png_ptr, 8);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500984
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600985You now need to fill in the png_info structure with all the data you
986wish to write before the actual image. Note that the only thing you
987are allowed to write after the image is the text chunks and the time
Guy Schalnate5a37791996-06-05 15:50:50 -0500988chunk (as of PNG Specification 1.0, anyway). See png_write_end() and
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600989the latest PNG specification for more information on that. If you
Guy Schalnate5a37791996-06-05 15:50:50 -0500990wish to write them before the image, fill them in now, and flag that
991data as being valid. If you want to wait until after the data, don't
992fill them until png_write_end(). For all the fields in png_info and
993their data types, see png.h. For explanations of what the fields
994contain, see the PNG specification.
Guy Schalnat69b14481996-01-10 02:56:49 -0600995
996Some of the more important parts of the png_info are:
997
Andreas Dilger47a0c421997-05-16 02:46:07 -0500998 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600999 interlace_type, compression_type, filter_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001000 width - holds the width of the image in pixels (up to 2^31).
1001 height - holds the height of the image in pixels (up to 2^31).
1002 bit_depth - holds the bit depth of one of the image channels.
1003 (valid values are 1, 2, 4, 8, 16 and depend also on the
1004 color_type. See also significant bits (sBIT) below).
1005 color_type - describes which color/alpha channels are present.
1006 PNG_COLOR_TYPE_GRAY (bit depths 1, 2, 4, 8, 16)
1007 PNG_COLOR_TYPE_GRAY_ALPHA (bit depths 8, 16)
1008 PNG_COLOR_TYPE_PALETTE (bit depths 1, 2, 4, 8)
1009 PNG_COLOR_TYPE_RGB (bit_depths 8, 16)
1010 PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16)
1011
1012 PNG_COLOR_MASK_PALETTE
1013 PNG_COLOR_MASK_COLOR
1014 PNG_COLOR_MASK_ALPHA
1015
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001016 interlace_type - PNG_INTERLACE_TYPE_NONE or PNG_INTERLACE_TYPE_ADAM7
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017 compression_type - (must be PNG_COMPRESSION_TYPE_DEFAULT for PNG 1.0)
1018 filter_type - (must be PNG_FILTER_TYPE_DEFAULT for PNG 1.0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001019
1020 png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
1021 palette - the palette for the file (array of png_color)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001022 num_palette - number of entries in the palette
Andreas Dilger47a0c421997-05-16 02:46:07 -05001023
1024 png_set_gAMA(png_ptr, info_ptr, gamma);
1025 gamma - the gamma the image was created at (PNG_INFO_gAMA)
1026
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001027 png_set_sRGB(png_ptr, info_ptr, srgb_intent);
1028 srgb_intent - the rendering intent (PNG_INFO_sRGB)
1029 The presence of the sRGB chunk means that the pixel
1030 data is in the sRGB color space. This chunk also
1031 implies specific values of gAMA and cHRM.
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001032 Rendering intent is the CSS-1 property that has been
1033 defined by the International Color Consortium
1034 (http://www.color.org). It can be one of
1035 PNG_SRGB_INTENT_SATURATION, PNG_SRGB_INTENT_PERCEPTUAL,
1036 PNG_SRGB_INTENT_ABSOLUTE, or PNG_SRGB_INTENT_RELATIVE.
1037
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001038
1039 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, srgb_intent);
1040 srgb_intent - the rendering intent (PNG_INFO_sRGB)
1041 The presence of the sRGB chunk means that the pixel
1042 data is in the sRGB color space. This chunk also
1043 causes gAMA and cHRM chunks with the specific values
1044 that are consistent with sRGB to be written.
1045
Andreas Dilger47a0c421997-05-16 02:46:07 -05001046 png_set_sBIT(png_ptr, info_ptr, sig_bit);
1047 sig_bit - the number of significant bits for (PNG_INFO_sBIT)
1048 the gray, red, green, and blue channels, whichever
1049 are appropriate for the given color type (png_color_16)
1050
1051 png_set_tRNS(png_ptr, info_ptr, trans, num_trans, trans_values);
1052 trans - array of transparent entries for palette (PNG_INFO_tRNS)
Guy Schalnate5a37791996-06-05 15:50:50 -05001053 trans_values - transparent pixel for non-paletted images (PNG_INFO_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001054 num_trans - number of transparent entries (PNG_INFO_tRNS)
1055
1056 png_set_hIST(png_ptr, info_ptr, hist); (PNG_INFO_hIST)
1057 hist - histogram of palette (array of png_color_16)
1058
1059 png_set_tIME(png_ptr, info_ptr, mod_time);
Guy Schalnate5a37791996-06-05 15:50:50 -05001060 mod_time - time image was last modified (PNG_VALID_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061
1062 png_set_bKGD(png_ptr, info_ptr, background);
Guy Schalnate5a37791996-06-05 15:50:50 -05001063 background - background color (PNG_VALID_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001064
1065 png_set_text(png_ptr, info_ptr, text_ptr, num_text);
1066 text_ptr - array of png_text holding image comments
1067 text_ptr[i]->key - keyword for comment.
1068 text_ptr[i]->text - text comments for current keyword.
1069 text_ptr[i]->compression - type of compression used on "text"
1070 PNG_TEXT_COMPRESSION_NONE or
1071 PNG_TEXT_COMPRESSION_zTXt
1072 num_text - number of comments in text_ptr
1073
1074 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
1075 offset_x - positive offset from the left edge of the screen
1076 offset_y - positive offset from the top edge of the screen
1077 unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
1078
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001079 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001080 res_x - pixels/unit physical resolution in x direction
1081 res_y - pixels/unit physical resolution in x direction
1082 unit_type - PNG_RESOLUTION_UNKOWN, PNG_RESOLUTION_METER
Guy Schalnat0d580581995-07-20 02:43:20 -05001083
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001084In PNG files, the alpha channel in an image is the level of opacity.
1085If your data is supplied as a level of transparency, you can invert the
1086alpha channel before you write it, so that 0 is fully transparent and 255
1087(in 8-bit or paletted images) or 65535 (in 16-bit images) is fully opaque,
1088with
1089
1090 png_set_invert_alpha(png_ptr);
1091
1092This must appear here instead of later with the other transformations
1093because in the case of paletted images the tRNS chunk data has to
1094be inverted before the tRNS chunk is written. If your image is not a
1095paletted image, the tRNS data (which in such cases represents a single
1096color to be rendered as transparent) won't be changed.
1097
Guy Schalnat0d580581995-07-20 02:43:20 -05001098A quick word about text and num_text. text is an array of png_text
1099structures. num_text is the number of valid structures in the array.
1100If you want, you can use max_text to hold the size of the array, but
1101libpng ignores it for writing (it does use it for reading). Each
1102png_text structure holds a keyword-text value, and a compression type.
1103The compression types have the same valid numbers as the compression
1104types of the image data. Currently, the only valid number is zero.
1105However, you can store text either compressed or uncompressed, unlike
1106images which always have to be compressed. So if you don't want the
Andreas Dilger47a0c421997-05-16 02:46:07 -05001107text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
1108Until text gets around 1000 bytes, it is not worth compressing it.
1109After the text has been written out to the file, the compression type
1110is set to PNG_TEXT_COMPRESSION_NONE_WR or PNG_TEXT_COMPRESSION_zTXt_WR,
1111so that it isn't written out again at the end (in case you are calling
1112png_write_end() with the same struct.
Guy Schalnate5a37791996-06-05 15:50:50 -05001113
1114The keywords that are given in the PNG Specification are:
1115
1116 Title Short (one line) title or caption for image
1117 Author Name of image's creator
1118 Description Description of image (possibly long)
1119 Copyright Copyright notice
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001120 Creation Time Time of original image creation (usually
1121 RFC 1123 format, see below)
Guy Schalnate5a37791996-06-05 15:50:50 -05001122 Software Software used to create the image
1123 Disclaimer Legal disclaimer
1124 Warning Warning of nature of content
1125 Source Device used to create the image
1126 Comment Miscellaneous comment; conversion from other
1127 image format
Guy Schalnat0d580581995-07-20 02:43:20 -05001128
1129The keyword-text pairs work like this. Keywords should be short
1130simple descriptions of what the comment is about. Some typical
1131keywords are found in the PNG specification, as is some recomendations
1132on keywords. You can repeat keywords in a file. You can even write
1133some text before the image and some after. For example, you may want
1134to put a description of the image before the image, but leave the
1135disclaimer until after, so viewers working over modem connections
1136don't have to wait for the disclaimer to go over the modem before
1137they start seeing the image. Finally, keywords should be full
Guy Schalnate5a37791996-06-05 15:50:50 -05001138words, not abbreviations. Keywords and text are in the ISO 8859-1
1139(Latin-1) character set (a superset of regular ASCII) and can not
1140contain NUL characters, and should not contain control or other
1141unprintable characters. To make the comments widely readable, stick
1142with basic ASCII, and avoid machine specific character set extensions
1143like the IBM-PC character set. The keyword must be present, but
Guy Schalnat0d580581995-07-20 02:43:20 -05001144you can leave off the text string on non-compressed pairs.
1145Compressed pairs must have a text string, as only the text string
1146is compressed anyway, so the compression would be meaningless.
1147
Guy Schalnat6d764711995-12-19 03:22:19 -06001148PNG supports modification time via the png_time structure. Two
Guy Schalnat0d580581995-07-20 02:43:20 -05001149conversion routines are proved, png_convert_from_time_t() for
1150time_t and png_convert_from_struct_tm() for struct tm. The
1151time_t routine uses gmtime(). You don't have to use either of
1152these, but if you wish to fill in the png_time structure directly,
1153you should provide the time in universal time (GMT) if possible
Guy Schalnat69b14481996-01-10 02:56:49 -06001154instead of your local time. Note that the year number is the full
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155year (ie 1996, rather than 96 - PNG is year 2000 compliant!), and
1156that months start with 1.
Guy Schalnat0d580581995-07-20 02:43:20 -05001157
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001158If you want to store the time of the original image creation, you should
1159use a plain tEXt chunk with the "Creation Time" keyword. This is
1160necessary because the "creation time" of a PNG image is somewhat vague,
1161depending on whether you mean the PNG file, the time the image was
1162created in a non-PNG format, a still photo from which the image was
1163scanned, or possibly the subject matter itself. In order to facilitate
1164machine-readable dates, it is recommended that the "Creation Time"
1165tEXt chunk use RFC 1123 format dates (eg 22 May 1997 18:07:10 GMT"),
1166although this isn't a requirement. Unlike the tIME chunk, the
1167"Creation Time" tEXt chunk is not expected to be automatically changed
1168by the software. To facilitate the use of RFC 1123 dates, a function
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001169png_convert_to_rfc1123(png_timep) is provided to convert from PNG
1170time to an RFC 1123 format string.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001171
Guy Schalnat0d580581995-07-20 02:43:20 -05001172You are now ready to write all the file information up to the actual
1173image data. You do this with a call to png_write_info().
1174
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001175 png_write_info(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001176
Guy Schalnate5a37791996-06-05 15:50:50 -05001177After you've written the file information, you can set up the library
1178to handle any special transformations of the image data. The various
Guy Schalnat0d580581995-07-20 02:43:20 -05001179ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001180should occur. This is important, as some of these change the color
1181type and/or bit depth of the data, and some others only work on
1182certain color types and bit depths. Even though each transformation
1183checks to see if it has data that it can do somthing with, you should
1184make sure to only enable a transformation if it will be valid for the
1185data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -05001186
Guy Schalnate5a37791996-06-05 15:50:50 -05001187PNG files store RGB pixels packed into 3 bytes. This code tells
1188the library to expect input data with 4 bytes per pixel
Guy Schalnat0d580581995-07-20 02:43:20 -05001189
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001190 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Guy Schalnat0d580581995-07-20 02:43:20 -05001191
Guy Schalnate5a37791996-06-05 15:50:50 -05001192where the 0 is the value that will be put in the 4th byte, and the
1193location is either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending
1194upon whether the filler byte is stored XRGB or RGBX.
Guy Schalnat0d580581995-07-20 02:43:20 -05001195
1196PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
1197they can, resulting in, for example, 8 pixels per byte for 1 bit files.
Guy Schalnat69b14481996-01-10 02:56:49 -06001198If the data is supplied at 1 pixel per byte, use this code, which will
Guy Schalnate5a37791996-06-05 15:50:50 -05001199correctly pack the pixels into a single byte:
Guy Schalnat0d580581995-07-20 02:43:20 -05001200
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001201 png_set_packing(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001202
1203PNG files reduce possible bit depths to 1, 2, 4, 8, and 16. If your
Guy Schalnate5a37791996-06-05 15:50:50 -05001204data is of another bit depth, you can write an sBIT chunk into the
1205file so that decoders can get the original data if desired.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001206
Guy Schalnate5a37791996-06-05 15:50:50 -05001207 /* Set the true bit depth of the image data */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001208 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001209 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001210 sig_bit.red = true_bit_depth;
1211 sig_bit.green = true_bit_depth;
1212 sig_bit.blue = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001213 }
1214 else
1215 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001216 sig_bit.gray = true_bit_depth;
1217 }
1218 if (color_type & PNG_COLOR_MASK_ALPHA)
1219 {
1220 sig_bit.alpha = true_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001221 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001222
Andreas Dilger47a0c421997-05-16 02:46:07 -05001223 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -05001224
Guy Schalnate5a37791996-06-05 15:50:50 -05001225If the data is stored in the row buffer in a bit depth other than
1226one supported by PNG (ie 3 bit data in the range 0-7 for a 4-bit PNG),
1227this will scale the values to appear to be the correct bit depth as
1228is required by PNG.
1229
Andreas Dilger47a0c421997-05-16 02:46:07 -05001230 png_set_shift(png_ptr, &sig_bit);
Guy Schalnat0d580581995-07-20 02:43:20 -05001231
Guy Schalnat69b14481996-01-10 02:56:49 -06001232PNG files store 16 bit pixels in network byte order (big-endian,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001233ie. most significant bits first). This code would be used if they are
1234supplied the other way (little-endian, ie. least significant bits
1235first, eg. the way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -05001236
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 if (bit_depth > 8)
1238 png_set_swap(png_ptr);
1239
1240If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
1241need to change the order the pixels are packed into bytes, you can use:
1242
1243 if (bit_depth < 8)
1244 png_set_packswap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001245
Guy Schalnat69b14481996-01-10 02:56:49 -06001246PNG files store 3 color pixels in red, green, blue order. This code
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001247would be used if they are supplied as blue, green, red:
Guy Schalnat0d580581995-07-20 02:43:20 -05001248
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001249 png_set_bgr(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001250
Guy Schalnat69b14481996-01-10 02:56:49 -06001251PNG files describe monochrome as black being zero and white being
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001252one. This code would be used if the pixels are supplied with this reversed
Guy Schalnat69b14481996-01-10 02:56:49 -06001253(black being one and white being zero):
Guy Schalnat0d580581995-07-20 02:43:20 -05001254
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001255 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001256
Guy Schalnate5a37791996-06-05 15:50:50 -05001257It is possible to have libpng flush any pending output, either manually,
1258or automatically after a certain number of lines have been written. To
1259flush the output stream a single time call:
1260
1261 png_write_flush(png_ptr);
1262
1263and to have libpng flush the output stream periodically after a certain
1264number of scanlines have been written, call:
1265
1266 png_set_flush(png_ptr, nrows);
1267
1268Note that the distance between rows is from the last time png_write_flush()
1269was called, or the first row of the image if it has never been called.
1270So if you write 50 lines, and then png_set_flush 25, it will flush the
1271output on the next scanline, and every 25 lines thereafter, unless
1272png_write_flush()ls is called before 25 more lines have been written.
1273If nrows is too small (less than about 10 lines for a 640 pixel wide
1274RGB image) the image compression may decrease noticably (although this
1275may be acceptable for real-time applications). Infrequent flushing will
1276only degrade the compression performance by a few percent over images
1277that do not use flushing.
1278
Guy Schalnat0d580581995-07-20 02:43:20 -05001279That's it for the transformations. Now you can write the image data.
1280The simplest way to do this is in one function call. If have the
1281whole image in memory, you can just call png_write_image() and libpng
1282will write the image. You will need to pass in an array of pointers to
1283each row. This function automatically handles interlacing, so you don't
1284need to call png_set_interlace_handling() or call this function multiple
1285times, or any of that other stuff necessary with png_write_rows().
1286
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001287 png_write_image(png_ptr, row_pointers);
Guy Schalnat0d580581995-07-20 02:43:20 -05001288
1289where row_pointers is:
1290
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001291 png_bytef *row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -05001292
1293You can point to void or char or whatever you use for pixels.
1294
1295If you can't want to write the whole image at once, you can
1296use png_write_rows() instead. If the file is not interlaced,
1297this is simple:
1298
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001299 png_write_rows(png_ptr, row_pointers, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -05001300
1301row_pointers is the same as in the png_write_image() call.
1302
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001303If you are just writing one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -05001304row_pointers:
1305
Guy Schalnate5a37791996-06-05 15:50:50 -05001306 png_bytep row_pointer = row;
Guy Schalnat0d580581995-07-20 02:43:20 -05001307
Guy Schalnate5a37791996-06-05 15:50:50 -05001308 png_write_row(png_ptr, &row_pointer);
Guy Schalnat0d580581995-07-20 02:43:20 -05001309
Guy Schalnat69b14481996-01-10 02:56:49 -06001310When the file is interlaced, things can get a good deal more
Guy Schalnate5a37791996-06-05 15:50:50 -05001311complicated. The only currently (as of 6/96 -- PNG Specification
1312version 1.0) defined interlacing scheme for PNG files is a
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001313compilcated interlace scheme, known as Adam7, that breaks down an
1314image into seven smaller images of varying size. libpng will build
1315these images for you, or you can do them yourself. If you want to
1316build them yourself, see the PNG specification for details of which
1317pixels to write when.
Guy Schalnat0d580581995-07-20 02:43:20 -05001318
1319If you don't want libpng to handle the interlacing details, just
Guy Schalnate5a37791996-06-05 15:50:50 -05001320use png_set_interlace_handling() and call png_write_rows() the
1321correct number of times to write all seven sub-images.
Guy Schalnat0d580581995-07-20 02:43:20 -05001322
Guy Schalnat69b14481996-01-10 02:56:49 -06001323If you want libpng to build the sub-images, call this before you start
1324writing any rows:
Guy Schalnat0d580581995-07-20 02:43:20 -05001325
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001326 number_of_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001327
1328This will return the number of passes needed. Currently, this
1329is seven, but may change if another interlace type is added.
1330
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001331Then write the complete image number_of_passes times.
Guy Schalnat0d580581995-07-20 02:43:20 -05001332
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001333 png_write_rows(png_ptr, row_pointers, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -05001334
1335As some of these rows are not used, and thus return immediately,
1336you may want to read about interlacing in the PNG specification,
1337and only update the rows that are actually used.
1338
1339After you are finished writing the image, you should finish writing
1340the file. If you are interested in writing comments or time, you should
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001341pass an appropriately filled png_info pointer. If you are not interested,
1342you can pass NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -05001343
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001344 png_write_end(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001345
1346When you are done, you can free all memory used by libpng like this:
1347
Guy Schalnate5a37791996-06-05 15:50:50 -05001348 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001349
Guy Schalnate5a37791996-06-05 15:50:50 -05001350You must free any data you allocated for info_ptr, such as comments,
1351palette, or histogram, before the call to png_destroy_write_struct();
Guy Schalnat0d580581995-07-20 02:43:20 -05001352
Guy Schalnate5a37791996-06-05 15:50:50 -05001353For a more compact example of writing a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -05001354
1355
Guy Schalnat69b14481996-01-10 02:56:49 -06001356V. Modifying/Customizing libpng:
Guy Schalnat0d580581995-07-20 02:43:20 -05001357
1358There are two issues here. The first is changing how libpng does
1359standard things like memory allocation, input/output, and error handling.
1360The second deals with more complicated things like adding new chunks,
1361adding new transformations, and generally changing how libpng works.
1362
Andreas Dilger47a0c421997-05-16 02:46:07 -05001363All of the memory allocation, input/output, and error handling in libpng
1364goes through callbacks which are user setable. The default routines are
1365in pngmem.c, pngrio.c, pngwio.c, and pngerror.c respectively. To change
1366these functions, call the approprate png_set_???_fn() function.
Guy Schalnat0d580581995-07-20 02:43:20 -05001367
Guy Schalnat6d764711995-12-19 03:22:19 -06001368Memory allocation is done through the functions png_large_malloc(),
Guy Schalnate5a37791996-06-05 15:50:50 -05001369png_malloc(), png_realloc(), png_large_free(), and png_free(). These
1370currently just call the standard C functions. The large functions must
1371handle exactly 64K, but they don't have to handle more than that. If
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372your pointers can't access more then 64K at a time, you will want to set
1373MAXSEG_64K in zlib.h. Since it is unlikely that the method of handling
1374memory allocation on a platform will change between applications, these
1375functions must be modified in the library at compile time.
Guy Schalnat0d580581995-07-20 02:43:20 -05001376
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001377Input/Output in libpng is done through png_read() and png_write(),
Andreas Dilger47a0c421997-05-16 02:46:07 -05001378which currently just call fread() and fwrite(). The FILE * is stored in
1379png_struct and is initialized via png_init_io(). If you wish to change
1380the method of I/O, the library supplies callbacks that you can set
1381through the function png_set_read_fn() and png_set_write_fn() at run
1382time, instead of calling the png_init_io() function. These functions
1383also provide a void pointer that can be retrieved via the function
Guy Schalnat6d764711995-12-19 03:22:19 -06001384png_get_io_ptr(). For example:
Guy Schalnat0d580581995-07-20 02:43:20 -05001385
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001386 png_set_read_fn(png_structp png_ptr, voidp io_ptr,
1387 png_rw_ptr read_data_fn)
Guy Schalnat0f716451995-11-28 11:22:13 -06001388
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001389 png_set_write_fn(png_structp png_ptr, voidp io_ptr,
1390 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn);
Guy Schalnat0f716451995-11-28 11:22:13 -06001391
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001392 voidp io_ptr = png_get_io_ptr(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001393
Guy Schalnat69b14481996-01-10 02:56:49 -06001394The replacement I/O functions should have prototypes as follows:
1395
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001396 void user_read_data(png_structp png_ptr, png_bytep data,
1397 png_uint_32 length);
1398 void user_write_data(png_structp png_ptr, png_bytep data,
1399 png_uint_32 length);
1400 void user_flush_data(png_structp png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001401
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001402Supplying NULL for the read, write, or flush functions sets them back
1403to using the default C stream functions. It is an error to read from
1404a write stream, and vice versa.
Guy Schalnat6d764711995-12-19 03:22:19 -06001405
1406Error handling in libpng is done through png_error() and png_warning().
1407Errors handled through png_error() are fatal, meaning that png_error()
Andreas Dilger47a0c421997-05-16 02:46:07 -05001408should never return to its caller. Currently, this is handled via
Guy Schalnat6d764711995-12-19 03:22:19 -06001409setjmp() and longjmp(), but you could change this to do things like
Guy Schalnat69b14481996-01-10 02:56:49 -06001410exit() if you should wish. On non-fatal errors, png_warning() is called
1411to print a warning message, and then control returns to the calling code.
Guy Schalnate5a37791996-06-05 15:50:50 -05001412By default png_error() and png_warning() print a message on stderr via
1413fprintf() unless the library is compiled with PNG_NO_STDIO defined. If
Guy Schalnat69b14481996-01-10 02:56:49 -06001414you wish to change the behavior of the error functions, you will need to
Guy Schalnate5a37791996-06-05 15:50:50 -05001415set up your own message callbacks. These functions are normally supplied
1416at the time that the png_struct is created. It is also possible to change
1417these functions after png_create_???_struct() has been called by calling:
Guy Schalnat69b14481996-01-10 02:56:49 -06001418
Guy Schalnate5a37791996-06-05 15:50:50 -05001419 png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
1420 png_error_ptr error_fn, png_error_ptr warning_fn);
Guy Schalnat69b14481996-01-10 02:56:49 -06001421
Guy Schalnate5a37791996-06-05 15:50:50 -05001422 png_voidp error_ptr = png_get_error_ptr(png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001423
Guy Schalnate5a37791996-06-05 15:50:50 -05001424If NULL is supplied for either error_fn or warning_fn, then the libpng
1425default function will be used, calling fprintf() and/or longjmp() if a
1426problem is encountered. The replacement error functions should have
1427parameters as follows:
Guy Schalnat69b14481996-01-10 02:56:49 -06001428
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001429 void user_error_fn(png_structp png_ptr, png_const_charp error_msg);
1430 void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg);
Guy Schalnat69b14481996-01-10 02:56:49 -06001431
1432The motivation behind using setjmp() and longjmp() is the C++ throw and
1433catch exception handling methods. This makes the code much easier to write,
1434as there is no need to check every return code of every function call.
1435However, there are some uncertainties about the status of local variables
1436after a longjmp, so the user may want to be careful about doing anything after
Andreas Dilger47a0c421997-05-16 02:46:07 -05001437setjmp returns non-zero besides returning itself. Consult your compiler
Guy Schalnat69b14481996-01-10 02:56:49 -06001438documentation for more details.
Guy Schalnat0f716451995-11-28 11:22:13 -06001439
Guy Schalnat0d580581995-07-20 02:43:20 -05001440If you need to read or write custom chunks, you will need to get deeper
Guy Schalnate5a37791996-06-05 15:50:50 -05001441into the libpng code, as a mechanism has not yet been supplied for user
1442callbacks with custom chunks. First, read the PNG specification, and have
1443a first level of understanding of how it works. Pay particular attention
1444to the sections that describe chunk names, and look at how other chunks
1445were designed, so you can do things similarly. Second, check out the
1446sections of libpng that read and write chunks. Try to find a chunk that
1447is similar to yours and copy off of it. More details can be found in the
1448comments inside the code. A way of handling unknown chunks in a generic
1449method, potentially via callback functions, would be best.
Guy Schalnat0d580581995-07-20 02:43:20 -05001450
Guy Schalnate5a37791996-06-05 15:50:50 -05001451If you wish to write your own transformation for the data, look through
1452the part of the code that does the transformations, and check out some of
1453the simpler ones to get an idea of how they work. Try to find a similar
1454transformation to the one you want to add and copy off of it. More details
1455can be found in the comments inside the code itself.
Guy Schalnat0d580581995-07-20 02:43:20 -05001456
1457Configuring for 16 bit platforms:
1458
Guy Schalnate5a37791996-06-05 15:50:50 -05001459You may need to change the png_large_malloc() and png_large_free()
1460routines in pngmem.c, as these are requred to allocate 64K, although
1461there is already support for many of the common DOS compilers. Also,
1462you will want to look into zconf.h to tell zlib (and thus libpng) that
1463it cannot allocate more then 64K at a time. Even if you can, the memory
1464won't be accessable. So limit zlib and libpng to 64K by defining MAXSEG_64K.
Guy Schalnat0d580581995-07-20 02:43:20 -05001465
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001466Configuring for DOS:
1467
1468For DOS users which only have access to the lower 640K, you will
1469have to limit zlib's memory usage via a png_set_compression_mem_level()
1470call. See zlib.h or zconf.h in the zlib library for more information.
1471
Guy Schalnat6d764711995-12-19 03:22:19 -06001472Configuring for Medium Model:
1473
1474Libpng's support for medium model has been tested on most of the popular
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001475compilers. Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets
Guy Schalnat69b14481996-01-10 02:56:49 -06001476defined, and FAR gets defined to far in pngconf.h, and you should be
Guy Schalnat6d764711995-12-19 03:22:19 -06001477all set. Everything in the library (except for zlib's structure) is
1478expecting far data. You must use the typedefs with the p or pp on
1479the end for pointers (or at least look at them and be careful). Make
1480note that the row's of data are defined as png_bytepp which is a
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001481unsigned char far * far *.
Guy Schalnat6d764711995-12-19 03:22:19 -06001482
Guy Schalnat0d580581995-07-20 02:43:20 -05001483Configuring for gui/windowing platforms:
1484
Guy Schalnate5a37791996-06-05 15:50:50 -05001485You will need to write new error and warning functions that use the GUI
1486interface, as described previously, and set them to be the error and
1487warning functions at the time that png_create_???_struct() is called,
1488in order to have them available during the structure initialization.
1489They can be changed later via png_set_error_fn(). On some compliers,
1490you may also have to change the memory allocators (png_malloc, etc.).
Guy Schalnat6d764711995-12-19 03:22:19 -06001491
Guy Schalnat0d580581995-07-20 02:43:20 -05001492Configuring for compiler xxx:
1493
Guy Schalnat69b14481996-01-10 02:56:49 -06001494All includes for libpng are in pngconf.h. If you need to add/change/delete
Guy Schalnat0d580581995-07-20 02:43:20 -05001495an include, this is the place to do it. The includes that are not
1496needed outside libpng are protected by the PNG_INTERNAL definition,
1497which is only defined for those routines inside libpng itself. The
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001498files in libpng proper only include png.h, which includes pngconf.h.
1499
1500Configuring zlib:
1501
Guy Schalnate5a37791996-06-05 15:50:50 -05001502There are special functions to configure the compression. Perhaps the
1503most useful one changes the compression level, which currently uses
1504input compression values in the range 0 - 9. The library normally
Andreas Dilger47a0c421997-05-16 02:46:07 -05001505uses the default compression level (Z_DEFAULT_COMPRESSION = 6). Tests
1506have shown that for a large majority of images, compression values in
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001507the range 3-6 compress nearly as well as higher levels, and do so much
1508faster. For online applications it may be desirable to have maximum speed
Guy Schalnate5a37791996-06-05 15:50:50 -05001509(Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also
1510specify no compression (Z_NO_COMPRESSION = 0), but this would create
1511files larger than just storing the raw bitmap. You can specify the
1512compression level by calling:
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001513
Andreas Dilger47a0c421997-05-16 02:46:07 -05001514 png_set_compression_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001515
1516Another useful one is to reduce the memory level used by the library.
1517The memory level defaults to 8, but it can be lowered if you are
1518short on memory (running DOS, for example, where you only have 640K).
1519
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001520 png_set_compression_mem_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001521
Andreas Dilger47a0c421997-05-16 02:46:07 -05001522The other functions are for configuring zlib. They are not recommended
1523for normal use and may result in writing an invalid PNG file. See
1524zlib.h for more information on what these mean.
1525
1526 png_set_compression_strategy(png_ptr, strategy);
1527 png_set_compression_window_bits(png_ptr, window_bits);
1528 png_set_compression_method(png_ptr, method);
1529
1530Controlling row filtering:
1531
1532If you want to control whether libpng uses filtering or not, which
1533filters are used, and how it goes about picking row filters, you
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001534can call one of these functions. The selection and configuration
1535of row filters can have a significant impact on the size and
1536encoding speed and a somewhat lesser impact on the decoding speed
1537of an image. Filtering is enabled by default for RGB and grayscale
1538images (with and without alpha), and for 8-bit paletted images, but
1539not for paletted images with bit depths less than 8 bits/pixel.
1540
1541The 'method' parameter sets the main filtering method, which is
1542currently only '0' in the PNG 1.0 specification. The 'filters'
1543parameter sets which filter(s), if any, should be used for each
1544scanline. Possible values are PNG_ALL_FILTERS and PNG_NO_FILTERS
1545to turn filtering on and off, respectively.
1546
Guy Schalnate5a37791996-06-05 15:50:50 -05001547Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB,
1548PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise
1549ORed together '|' to specify one or more filters to use. These
1550filters are described in more detail in the PNG specification. If
1551you intend to change the filter type during the course of writing
1552the image, you should start with flags set for all of the filters
1553you intend to use so that libpng can initialize its internal
1554structures appropriately for all of the filter types.
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001555
Andreas Dilger47a0c421997-05-16 02:46:07 -05001556 filters = PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP;
1557 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, filters);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001558
Andreas Dilger47a0c421997-05-16 02:46:07 -05001559It is also possible to influence how libpng chooses from among the
1560available filters. This is done in two ways - by telling it how
1561important it is to keep the same filter for successive rows, and
1562by telling it the relative computational costs of the filters.
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001563
Andreas Dilger47a0c421997-05-16 02:46:07 -05001564 double weights[3] = {1.5, 1.3, 1.1},
1565 costs[PNG_FILTER_VALUE_LAST] = {1.0, 1.3, 1.3, 1.5, 1.7};
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001566
Andreas Dilger47a0c421997-05-16 02:46:07 -05001567 png_set_filter_selection(png_ptr, PNG_FILTER_SELECTION_WEIGHTED,
1568 3, weights, costs);
1569
1570The weights are multiplying factors which indicate to libpng that row
1571should be the same for successive rows unless another row filter is that
1572many times better than the previous filter. In the above example, if
1573the previous 3 filters were SUB, SUB, NONE, the SUB filter could have a
1574"sum of absolute differences" 1.5 x 1.3 times higher than other filters
1575and still be chosen, while the NONE filter could have a sum 1.1 times
1576higher than other filters and still be chosen. Unspecified weights are
1577taken to be 1.0, and the specified weights should probably be declining
1578like those above in order to emphasize recent filters over older filters.
1579
1580The filter costs specify for each filter type a relative decoding cost
1581to be considered when selecting row filters. This means that filters
1582with higher costs are less likely to be chosen over filters with lower
1583costs, unless their "sum of absolute differences" is that much smaller.
1584The costs do not necessarily reflect the exact computational speeds of
1585the various filters, since this would unduely influence the final image
1586size.
1587
1588Note that the numbers above were invented purely for this example and
1589are given only to help explain the function usage. Little testing has
1590been done to find optimum values for either the costs or the weights.
Guy Schalnat0d580581995-07-20 02:43:20 -05001591
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001592Removing unwanted object code:
1593
Guy Schalnat69b14481996-01-10 02:56:49 -06001594There are a bunch of #define's in pngconf.h that control what parts of
Andreas Dilger47a0c421997-05-16 02:46:07 -05001595libpng are compiled. All the defines end in _SUPPORTED. If you are
Guy Schalnate5a37791996-06-05 15:50:50 -05001596never going to use an ability, you can change the #define to #undef
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001597before recompiling libpng and save yourself code and data space.
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001598You can also turn them off en masse with a compiler directive that
1599defines PNG_READ[or WRITE]_NOT_FULLY_SUPPORTED, or
1600PNG_READ[or WRITE]_ANCILLARY_CHUNKS_NOT_SUPPORTED, or all four,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001601along with directives to turn on any of the capabilities that you do
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001602want. The NOT_FULLY_SUPPORTED directives disable the extra
1603transformations but still leave the library fully capable of reading
1604and writing PNG files with all known public chunks [except for sPLT].
1605Use of the PNG_READ[or WRITE]_ANCILLARY_CHUNKS_NOT_SUPPORTED directive
1606produces a library that is incapable of reading or writing ancillary chunks.
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001607
1608All the reading and writing specific code are in separate files, so the
Guy Schalnate5a37791996-06-05 15:50:50 -05001609linker should only grab the files it needs. However, if you want to
1610make sure, or if you are building a stand alone library, all the
1611reading files start with pngr and all the writing files start with
1612pngw. The files that don't match either (like png.c, pngtrans.c, etc.)
1613are used for both reading and writing, and always need to be included.
1614The progressive reader is in pngpread.c
1615
1616If you are creating or distributing a dynamically linked library (a .so
Andreas Dilger47a0c421997-05-16 02:46:07 -05001617or DLL file), you should not remove or disable any parts of the library,
1618as this will cause applications linked with different versions of the
1619library to fail if they call functions not available in your library.
1620The size of the library itself should not be an issue, because only
1621those sections which are actually used will be loaded into memory.
Guy Schalnate5a37791996-06-05 15:50:50 -05001622
Guy Schalnate5a37791996-06-05 15:50:50 -05001623
Andreas Dilger47a0c421997-05-16 02:46:07 -05001624Changes to Libpng from version 0.88
Guy Schalnate5a37791996-06-05 15:50:50 -05001625
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001626It should be noted that versions of libpng later than 0.96 are not
1627distributed by the original libpng author, Guy Schalnat, nor by
1628Andreas Dilger, who had taken over from Guy during 1996 and 1997, and
1629distributed versions 0.89 through 0.96, but rather by another member
1630of the original PNG Group, Glenn Randers-Pehrson. Guy and Andreas are
1631still alive and well, but they have moved on to other things.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001632
1633The old libpng functions png_read_init(), png_write_init(),
1634png_info_init(), png_read_destroy(), and png_write_destory() have been
1635moved to PNG_INTERNAL in version 0.95 to discourage their use. The
1636preferred method of creating and initializing the libpng structures is
1637via the png_create_read_struct(), png_create_write_struct(), and
Guy Schalnate5a37791996-06-05 15:50:50 -05001638png_create_info_struct() because they isolate the size of the structures
Andreas Dilger47a0c421997-05-16 02:46:07 -05001639from the application, allow version error checking, and also allow the
1640use of custom error handling routines during the initialization, which
1641the old functions do not. The functions png_read_destroy() and
1642png_write_destroy() do not actually free the memory that libpng
1643allocated for these structs, but just reset the data structures, so they
1644can be used instead of png_destroy_read_struct() and
1645png_destroy_write_struct() if you feel there is too much system overhead
1646allocating and freeing the png_struct for each image read.
Guy Schalnate5a37791996-06-05 15:50:50 -05001647
1648Setting the error callbacks via png_set_message_fn() before
1649png_read_init() as was suggested in libpng-0.88 is no longer supported
1650because this caused applications which do not use custom error functions
1651to fail if the png_ptr was not initialized to zero. It is still possible
1652to set the error callbacks AFTER png_read_init(), or to change them with
1653png_set_error_fn(), which is essentially the same function, but with a
Andreas Dilger47a0c421997-05-16 02:46:07 -05001654new name to force compilation errors with applications that try to use
1655the old method.