blob: 0abf51c0eda1703bcaf40e012878fecc73f62ab0 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001libpng.txt - a description on how to use and modify libpng
2
Guy Schalnate5a37791996-06-05 15:50:50 -05003 libpng 1.0 beta 3 - version 0.89
4 Updated and distributed by Andreas Dilger <adilger@enel.ucalgary.ca>,
5 based on:
6
7 libpng 1.0 beta 2 - version 0.88
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06008 For conditions of distribution and use, see copyright notice in png.h
9 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
Guy Schalnate5a37791996-06-05 15:50:50 -050010 May 24, 1996
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060011 Updated/rewritten per request in the libpng FAQ
12 Copyright (c) 1995 Frank J. T. Wojcik
13 December 18, 1995 && January 20, 1996
Guy Schalnat69b14481996-01-10 02:56:49 -060014
15I. Introduction
Guy Schalnat0d580581995-07-20 02:43:20 -050016
17This file describes how to use and modify the PNG reference library
Guy Schalnat69b14481996-01-10 02:56:49 -060018(known as libpng) for your own use. There are five sections to this
19file: introduction, structures, reading, writing, and modification and
Guy Schalnat4ee97b01996-01-16 01:51:56 -060020configuration notes for various special platforms. In addition to this
Guy Schalnat69b14481996-01-10 02:56:49 -060021file, example.c is a good starting point for using the library, as
22it is heavily commented and should include everything most people
23will need.
Guy Schalnat0d580581995-07-20 02:43:20 -050024
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060025Libpng was written as a companion to the PNG specification, as a way
26to reduce the amount of time and effort it takes to support the PNG
27file format in application programs. Most users will not have to
28modify the library significantly; advanced users may want to modify it
29more. All attempts were made to make it as complete as possible,
30while keeping the code easy to understand. Currently, this library
Guy Schalnat0d580581995-07-20 02:43:20 -050031only supports C. Support for other languages is being considered.
32
33Libpng has been designed to handle multiple sessions at one time,
34to be easily modifiable, to be portable to the vast majority of
35machines (ANSI, K&R, 16 bit, 32 bit) available, and to be easy to
36use. The ultimate goal of libpng is to promote the acceptance of
37the PNG file format in whatever way possible. While there is still
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060038work to be done (see the pngtodo.txt file), libpng should cover the
Guy Schalnat0d580581995-07-20 02:43:20 -050039majority of the needs of it's users.
40
41Libpng uses zlib for its compression and decompression of PNG files.
42The zlib compression utility is a general purpose utility that is
Guy Schalnat4ee97b01996-01-16 01:51:56 -060043useful for more than PNG files, and can be used without libpng.
Guy Schalnat69b14481996-01-10 02:56:49 -060044See the documentation delivered with zlib for more details.
Guy Schalnat0d580581995-07-20 02:43:20 -050045
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060046Libpng is thread safe, provided the threads are using different
Guy Schalnat4ee97b01996-01-16 01:51:56 -060047instances of the structures. Each thread should have its own
48png_struct and png_info instances, and thus its own image.
49Libpng does not protect itself against two threads using the
50same instance of a structure.
51
Guy Schalnat0d580581995-07-20 02:43:20 -050052
Guy Schalnat69b14481996-01-10 02:56:49 -060053
54II. Structures
Guy Schalnat0d580581995-07-20 02:43:20 -050055
56There are two main structures that are important to libpng, png_struct
57and png_info. The first, png_struct, is an internal structure that
Guy Schalnate5a37791996-06-05 15:50:50 -050058will not, for the most part, be used by a user except as the first
59variable passed to every png function call.
Guy Schalnat0d580581995-07-20 02:43:20 -050060
61The png_info structure is designed to provide information about the
Guy Schalnat69b14481996-01-10 02:56:49 -060062png file. All of its fields are intended to be examined or modified
Guy Schalnat0d580581995-07-20 02:43:20 -050063by the user. See png.h for a good description of the png_info fields.
Guy Schalnat69b14481996-01-10 02:56:49 -060064png.h is also an invaluable reference for programming with libpng.
Guy Schalnat0d580581995-07-20 02:43:20 -050065
Guy Schalnat6d764711995-12-19 03:22:19 -060066And while I'm on the topic, make sure you include the png header file:
Guy Schalnat0d580581995-07-20 02:43:20 -050067
68#include <png.h>
69
Guy Schalnat69b14481996-01-10 02:56:49 -060070
71
72III. Reading
73
Guy Schalnat0d580581995-07-20 02:43:20 -050074Reading PNG files:
75
Guy Schalnat69b14481996-01-10 02:56:49 -060076We'll now walk you through the possible functions to call when reading
77in a PNG file, briefly explaining the syntax and purpose of each one.
78See example.c and png.h for more detail. While Progressive reading
79is covered in the next section, you will still need some of the
80functions discussed in this section to read a PNG file.
Guy Schalnat6d764711995-12-19 03:22:19 -060081
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060082You will want to do the I/O initialization(*) before you get into libpng,
83so if it doesn't work, you don't have much to undo. Of course, you
84will also want to insure that you are, in fact, dealing with a PNG
85file. Libpng provides a simple check to see if a file is a PNG file.
86To use it, pass in the first 1 to 8 bytes of the file, and it will
87return true or false (1 or 0) depending on whether the bytes could be
88part of a PNG file. Of course, the more bytes you pass in, the
89greater the accuracy of the prediction. If you pass in more then
Guy Schalnate5a37791996-06-05 15:50:50 -050090eight bytes, libpng will only look at the first eight bytes. However,
91because libpng automatically checks the file header, this is not often
92necessary, and you should pass a newly opened file pointer to libpng
93when reading a file.
Guy Schalnat0d580581995-07-20 02:43:20 -050094
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060095(*): If you are not using the standard I/O functions, you will need
Guy Schalnat69b14481996-01-10 02:56:49 -060096to replace them with custom functions. See the discussion under
97Customizing libpng.
98
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060099 FILE *fp = fopen(file_name, "rb");
100 if (!fp)
101 {
102 return;
103 }
104 fread(header, 1, number, fp);
105 is_png = png_check_sig(header, number);
106 if (!is_png)
107 {
108 return;
109 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500110 fclose(fp);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600111
112Next, png_struct and png_info need to be allocated and initialized.
Guy Schalnate5a37791996-06-05 15:50:50 -0500113In order to ensure that the size of these structures is correct even
114with a dynamically linked libpng, there are functions to initialize
115and allocate the structures. We also pass the library version, and
116optionally pointers to error handling functions (these can be NULL
117if the default error handlers are to be used). See the section on
118Changes to Libpng below regarding the old initialization functions.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600119
Guy Schalnate5a37791996-06-05 15:50:50 -0500120 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
121 (void *)user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600122 if (!png_ptr)
123 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500124 png_infop info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600125 if (!info_ptr)
126 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500127 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
128 return;
129 }
130 png_infop end_info = png_create_info_struct(png_ptr);
131 if (!end_info)
132 {
133 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600134 return;
135 }
136
Guy Schalnate5a37791996-06-05 15:50:50 -0500137The error handling routines passed to png_create_read_struct()
138are only necessary if you are not using the libpng supplied
139error handling functions. When libpng encounters an error,
140it expects to longjmp back to your routine. Therefore, you
141will need to call setjmp and pass the jmpbuf field of your
142png_struct. If you read the file from different routines, you
143will need to update the jmpbuf field every time you enter a new
144routine that will call a png_ function. See your documentation
145of setjmp/longjmp for your compiler for more information on
146setjmp/longjmp. See the discussion on libpng error handling
147in the Customizing Libpng section below for more information on
148the libpng error handling. If an error occurs, and libpng
149longjmp's back to your setjmp, you will want to call
150png_destroy_read_struct() to free any memory.
Guy Schalnat0d580581995-07-20 02:43:20 -0500151
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600152 if (setjmp(png_ptr->jmpbuf))
153 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500154 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600155 fclose(fp);
156 return;
157 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500158
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600159Now you need to set up the input code. The default for libpng is to
160use the C function fread(). If you use this, you will need to pass a
161valid FILE * in the function png_init_io(). Be sure that the file is
162opened in binary mode. Again, if you wish to handle reading data in
163another way, see the discussion on libpng I/O handling in the Customizing
164Libpng section below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500165
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600166 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500167
168You are now ready to read all the file information up to the actual
169image data. You do this with a call to png_read_info().
170
171 png_read_info(png_ptr, info_ptr);
172
173The png_info structure is now filled in with all the data necessary
Guy Schalnate5a37791996-06-05 15:50:50 -0500174to read the file. Some of the more important parts of the info_ptr are:
Guy Schalnat69b14481996-01-10 02:56:49 -0600175
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600176 width - holds the width of the file
177 height - holds the height of the file
178 bit_depth - holds the bit depth of one of the image channels
179 color_type - describes the channels and what they mean
180 (see the PNG_COLOR_TYPE_ macros for more information)
181 channels - number of channels of info for the color type
182 pixel_depth - bits per pixel, the result of multiplying the
183 bit_depth times the channels
184 rowbytes - number of bytes needed to hold a row
185 interlace_type - currently 0 for none, 1 for interlaced
186 valid - this details which optional chunks were found in the
Guy Schalnate5a37791996-06-05 15:50:50 -0500187 file to see if a chunk was present, AND '&' valid with
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188 the appropriate PNG_INFO_<chunk name> define.
Guy Schalnat69b14481996-01-10 02:56:49 -0600189
190These are also important, but their validity depends on whether a
191corresponding chunk exists. Use valid (see above) to ensure that what
192you're doing with these values makes sense.
193
Guy Schalnate5a37791996-06-05 15:50:50 -0500194 palette - the palette for the file (PNG_INFO_PLTE)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600195 num_palette - number of entries in the palette
Guy Schalnate5a37791996-06-05 15:50:50 -0500196 gamma - the gamma the file is written at (PNG_INFO_gAMA)
197 sig_bit - the number of significant bits (PNG_INFO_sBIT)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600198 for the gray, red, green, and blue channels, whichever
199 are appropriate for the given color type.
Guy Schalnate5a37791996-06-05 15:50:50 -0500200 trans_values - transparent pixel for non-paletted images (PNG_INFO_tRNS)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600201 trans - array of transparent entries for paletted images
202 num_trans - number of transparent entries
Guy Schalnate5a37791996-06-05 15:50:50 -0500203 hist - histogram of palette (PNG_INFO_hIST)
204 mod_time - time image was last modified (PNG_VALID_tIME)
205 background - background color (PNG_VALID_bKGD)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600206 text - text comments in the file.
207 num_text - number of comments
Guy Schalnat69b14481996-01-10 02:56:49 -0600208
Guy Schalnat0d580581995-07-20 02:43:20 -0500209for more information, see the png_info definition in png.h and the
210PNG specification for chunk contents. Be careful with trusting
211rowbytes, as some of the transformations could increase the space
Guy Schalnate5a37791996-06-05 15:50:50 -0500212needed to hold a row (expand, RGBX, XRGB, gray_to_rgb, etc.).
Guy Schalnat69b14481996-01-10 02:56:49 -0600213See png_update_info(), below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500214
215A quick word about text and num_text. PNG stores comments in
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600216keyword/text pairs, one pair per chunk. While there are suggested
217keywords, there is no requirement to restrict the use to these
218strings. There is a requirement to have at least one character for a
219keyword. It is strongly suggested that keywords be sensible to humans
220(that's the point), so don't use abbreviations. See the png
221specification for more details. There is also no requirement to have
222text after the keyword.
Guy Schalnat0d580581995-07-20 02:43:20 -0500223
Guy Schalnate5a37791996-06-05 15:50:50 -0500224Keywords should be limited to 80 characters without leading or trailing
225spaces, but non-consecutive spaces are allowed within the keyword. It is
226possible to have the same keyword any number of times. The text field
227is an array of png_text structures, each holding pointer to a keyword
Guy Schalnat0d580581995-07-20 02:43:20 -0500228and a pointer to a text string. Only the text string may be null.
229The keyword/text pairs are put into the array in the order that
230they are received. However, some or all of the text chunks may be
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600231after the image, so, to make sure you have read all the text chunks,
Guy Schalnat0d580581995-07-20 02:43:20 -0500232don't mess with these until after you read the stuff after the image.
233This will be mentioned again below in the discussion that goes with
234png_read_end().
235
236After you've read the file information, you can set up the library to
237handle any special transformations of the image data. The various
238ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600239should occur. This is important, as some of these change the color
240type and/or bit depth of the data, and some others only work on
241certain color types and bit depths. Even though each transformation
242checks to see if it has data that it can do somthing with, you should
243make sure to only enable a transformation if it will be valid for the
244data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500245
Guy Schalnate5a37791996-06-05 15:50:50 -0500246Data will be decoded into the supplied row buffers packed into bytes
247unless the library has been told to transform it into another format.
248For example, 4 bit/pixel paletted or grayscale data will be returned
2492 pixels/byte with the leftmost pixel in the high-order bits of the
250byte, unless png_set_packing() is called. 8-bit RGB data will be stored
251in RGBRGBRGB format unless png_set_filler() is called to insert filler
252bytes, either before or after each RGB triplet. 16-bit RGB data will
253be returned RRGGBBRRGGBB, with the most significant byte of the color
254value first, unless png_set_strip_16() is called to transform it to
255regular RGBRGB triplets.
256
257The following code transforms grayscale images of less than 8 to 8 bits,
258changes paletted images to RGB, and adds a full alpha channel if there is
259transparency information in a tRNS chunk. This is most useful on
260grayscale images with bit depths of 2 or 4 or if there is a multiple-image
261viewing application that wishes to treat all images in the same way.
Guy Schalnat0d580581995-07-20 02:43:20 -0500262
263 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
264 info_ptr->bit_depth < 8)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600265 png_set_expand(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500266
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600267 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Guy Schalnat0d580581995-07-20 02:43:20 -0500268 info_ptr->bit_depth < 8)
269 png_set_expand(png_ptr);
270
271 if (info_ptr->valid & PNG_INFO_tRNS)
272 png_set_expand(png_ptr);
273
Guy Schalnate5a37791996-06-05 15:50:50 -0500274PNG can have files with 16 bits per channel. If you only can handle
2758 bits per channel, this will strip the pixels down to 8 bit.
276
277 if (info_ptr->bit_depth == 16)
278 png_set_strip_16(png_ptr);
279
280PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
281they can, resulting in, for example, 8 pixels per byte for 1 bit
282files. This code expands to 1 pixel per byte without changing the
283values of the pixels:
284
285 if (info_ptr->bit_depth < 8)
286 png_set_packing(png_ptr);
287
288PNG files have possible bit depths of 1, 2, 4, 8, and 16. It is then
289required that values be "scaled" or "shifted" up to the bit depth used
290in the file (ie from 5 bits/sample in the range [0,31] to 8 bits/sample
291in the range [0, 255]). However, they also provide a way to describe
292the true bit depth of the image. See the PNG specification for details.
293This call reduces the pixels back down to the true bit depth:
294
295 if (info_ptr->valid & PNG_INFO_sBIT)
296 png_set_shift(png_ptr, &(info_ptr->sig_bit));
297
298PNG files store 3 color pixels in red, green, blue order. This code
299changes the storage of the pixels to blue, green, red:
300
301 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
302 info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
303 png_set_bgr(png_ptr);
304
305PNG files store RGB pixels packed into 3 bytes. This code expands them
306into 4 bytes for windowing systems that need them in this format:
307
308 if (info_ptr->bit_depth == 8 &&
309 info_ptr->color_type == PNG_COLOR_TYPE_RGB)
310 png_set_filler(png_ptr, filler_byte, PNG_FILLER_BEFORE);
311
312where filler_byte is the number to fill with, and the location is
313either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
314you want the filler before the RGB or after.
315
316For some uses, you may want a gray-scale image to be represented as
317RGB. This code will do that conversion:
318
319 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
320 info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
321 png_set_gray_to_rgb(png_ptr);
322
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600323The following code handles alpha and transparency by replacing it with
Guy Schalnate5a37791996-06-05 15:50:50 -0500324a background value. If there was a valid bKGD in the file, you can use
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600325it if you want. However, you can replace it with your own if you want
326also. If there wasn't one in the file, you must supply a color. If
327libpng is doing gamma correction, you will need to tell libpng where
328the background came from so it can do the appropriate gamma
Guy Schalnate5a37791996-06-05 15:50:50 -0500329correction. If you have a grayscale and you are using png_set_expand()
330to change to a higher bit-depth you must indicate if the background gray
331needs to be expanded to the new bit-depth. Similarly, if you are reading
332a paletted image, you must indicate if you have supplied the background
333index that needs to be expanded to RGB values. You can always specify
334RGB color values directly when setting your background for paletted images.
Guy Schalnat0d580581995-07-20 02:43:20 -0500335
Guy Schalnate5a37791996-06-05 15:50:50 -0500336 png_color_16 my_background;
Guy Schalnat0d580581995-07-20 02:43:20 -0500337
338 if (info_ptr->valid & PNG_INFO_bKGD)
339 png_set_backgrond(png_ptr, &(info_ptr->background),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600340 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500341 else
342 png_set_background(png_ptr, &my_background,
Guy Schalnat6d764711995-12-19 03:22:19 -0600343 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500344
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600345The following code handles gamma transformations of the data. Pass
346both the file gamma and the desired screen gamma. If the file does
347not have a gamma value, you can pass one anyway if you wish. Note
348that file gammas are inverted from screen gammas. See the discussions
349on gamma in the PNG specification for more information. It is
350strongly recommended that viewers support gamma correction.
Guy Schalnat0d580581995-07-20 02:43:20 -0500351
352 if (info_ptr->valid & PNG_INFO_gAMA)
353 png_set_gamma(png_ptr, screen_gamma, info_ptr->gamma);
Guy Schalnate5a37791996-06-05 15:50:50 -0500354 else
355 png_set_gamma(png_ptr, screen_gamma, 0.45);
Guy Schalnat0d580581995-07-20 02:43:20 -0500356
Guy Schalnate5a37791996-06-05 15:50:50 -0500357If you need to reduce an RGB file to a paletted file, or if a paletted
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600358file has more entries then will fit on your screen, png_set_dither()
359will do that. Note that this is a simple match dither that merely
360finds the closest color available. This should work fairly well with
361optimized palettes, and fairly badly with linear color cubes. If you
362pass a palette that is larger then maximum_colors, the file will
363reduce the number of colors in the palette so it will fit into
364maximum_colors. If there is a histogram, it will use it to make
Guy Schalnate5a37791996-06-05 15:50:50 -0500365more intelligent choices when reducing the palette. If there is no
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600366histogram, it may not do as good a job.
Guy Schalnat69b14481996-01-10 02:56:49 -0600367
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600368 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnat0d580581995-07-20 02:43:20 -0500369 {
370 if (info_ptr->valid & PNG_INFO_PLTE)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600371 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 png_set_dither(png_ptr, info_ptr->palette,
373 info_ptr->num_palette, max_screen_colors,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600374 info_ptr->histogram, 1);
375 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500376 else
377 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600378 png_color std_color_cube[MAX_SCREEN_COLORS] =
Guy Schalnat0d580581995-07-20 02:43:20 -0500379 { ... colors ... };
380
381 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600382 MAX_SCREEN_COLORS, NULL,0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500383 }
384 }
385
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600386PNG files describe monochrome as black being zero and white being one.
387The following code will reverse this (make black be one and white be
388zero):
Guy Schalnat0d580581995-07-20 02:43:20 -0500389
390 if (info_ptr->bit_depth == 1 &&
391 info_ptr->color_type == PNG_COLOR_GRAY)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600392 png_set_invert_mono(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500393
Guy Schalnat69b14481996-01-10 02:56:49 -0600394PNG files store 16 bit pixels in network byte order (big-endian,
395ie. most significant bits first). This code chages the storage to the
396other way (little-endian, ie. least significant bits first, eg. the
397way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -0500398
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600399 if (info_ptr->bit_depth == 16)
400 png_set_swap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500401
Guy Schalnat69b14481996-01-10 02:56:49 -0600402The last thing to handle is interlacing; this is covered in detail below,
403but you must call the function here.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500404
405 if (info_ptr->interlace_type)
406 number_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500407
Guy Schalnate5a37791996-06-05 15:50:50 -0500408After setting the transformations, libpng can update your png_info
409structure to reflect any transformations you've requested with this
410call. This is most useful to update the info structures rowbytes
411field, so you can use it to allocate your image memory. This function
412will also update your palette with the correct display gamma and
413background if these have been given with the calls above.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500414
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600415 png_read_update_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500416
417After you call png_read_update_info(), you can allocate any
Guy Schalnate5a37791996-06-05 15:50:50 -0500418memory you need to hold the image. The row data is simply
419raw byte data for all forms of images. As the actual allocation
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500420varies among applications, no example will be given. If you
Guy Schalnate5a37791996-06-05 15:50:50 -0500421are allocating one large chunk, you will need to build an
422array of pointers to each row, as it will be needed for some
423of the functions below.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500424
425After you've allocated memory, you can read the image data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500426The simplest way to do this is in one function call. If you are
427allocating enough memory to hold the whole image, you can just
428call png_read_image() and libpng will read in all the image data
429and put it in the memory area supplied. You will need to pass in
430an array of pointers to each row.
431
432This function automatically handles interlacing, so you don't need
433to call png_set_interlace_handling() or call this function multiple
434times, or any of that other stuff necessary with png_read_rows().
435
436 png_read_image(png_ptr, row_pointers);
437
438where row_pointers is:
439
Guy Schalnate5a37791996-06-05 15:50:50 -0500440 png_bytep row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -0500441
442You can point to void or char or whatever you use for pixels.
443
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600444If you don't want to read int the whole image at once, you can
Guy Schalnat0d580581995-07-20 02:43:20 -0500445use png_read_rows() instead. If there is no interlacing (check
446info_ptr->interlace_type), this is simple:
447
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600448 png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500449
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600450where row_pointers is the same as in the png_read_image() call.
Guy Schalnat0d580581995-07-20 02:43:20 -0500451
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600452If you are doing this just one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -0500453row_pointers:
454
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600455 png_bytep row_pointers = row;
Guy Schalnate5a37791996-06-05 15:50:50 -0500456 png_read_row(png_ptr, &row_pointers, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500457
Guy Schalnat69b14481996-01-10 02:56:49 -0600458If the file is interlaced (info_ptr->interlace_type != 0), things get
Guy Schalnate5a37791996-06-05 15:50:50 -0500459somewhat harder. The only currently (as of 6/96 -- PNG
460Specification version 1.0) defined interlacing scheme for PNG files
461(info_ptr->interlace_type == 1) is a someewhat complicated 2D interlace
462scheme, known as Adam7, that breaks down an image into seven smaller
463images of varying size, based on an 8x8 grid.
Guy Schalnat0d580581995-07-20 02:43:20 -0500464
Guy Schalnate5a37791996-06-05 15:50:50 -0500465libpng can fill out those images or it can give them to you "as is".
466If you want them filled out, there are two ways to do that. The one
467mentioned in the PNG specification is to expand each pixel to cover
468those pixels that have not been read yet. This results in a blocky
469image for the first pass, which gradually smoothes out as more pixels
470are read. The other method is the "sparkle" method, where pixels are
471draw only in their final locations, with the rest of the image remaining
472whatever colors they were initialized to before the start of the read.
473The first method usually looks better, but tends to be slower, as there
474are more pixels to put in the rows.
475
476If you don't want libpng to handle the interlacing details, just call
477png_read_rows() seven times to read in all seven images. Each of the
478images are valid images by themselves, or they can be combined on an
4798x8 grid to form a single image (although if you intend to combine them
480you would be far better off using the libpng interlace handling).
481
482The first pass will return an image 1/8 as wide as the entire image
483(every 8th column starting in column 0) and 1/8 as high as the original
484(every 8th row starting in row 0), the second will be 1/8 as wide
485(starting in column 4) and 1/8 as high (also starting in row 0). The
486third pass will be 1/4 as wide (every 4th pixel starting in row 0) and
4871/8 as high (every 8th row starting in row 4), and the fourth pass will
488be 1/4 as wide and 1/4 as high (every 4th column starting in column 2,
489and every 4th row starting in row 0). The fifth pass will return an
490image 1/2 as wide, and 1/4 as high (starting at column 0 and row 2),
491while the sixth pass will be 1/2 as wide and 1/2 as high as the original
492(starting in column 1 and row 0). The seventh and final pass will be as
493wide as the original, and 1/2 as high, containing all of the odd
494numbered scanlines. Phew!
Guy Schalnat0d580581995-07-20 02:43:20 -0500495
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600496If you want libpng to expand the images, call this before calling
497png_start_read_image() or png_read_update_info():
Guy Schalnat0d580581995-07-20 02:43:20 -0500498
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600499 if (info_ptr->interlace_type)
500 number_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500501
502This will return the number of passes needed. Currently, this
503is seven, but may change if another interlace type is added.
504This function can be called even if the file is not interlaced,
Guy Schalnate5a37791996-06-05 15:50:50 -0500505where it will return one pass.
Guy Schalnat0d580581995-07-20 02:43:20 -0500506
507If you are not going to display the image after each pass, but are
508going to wait until the entire image is read in, use the sparkle
509effect. This effect is faster and the end result of either method
510is exactly the same. If you are planning on displaying the image
511after each pass, the rectangle effect is generally considered the
512better looking one.
513
514If you only want the "sparkle" effect, just call png_read_rows() as
515normal, with the third parameter NULL. Make sure you make pass over
516the image number_passes times, and you don't change the data in the
517rows between calls. You can change the locations of the data, just
518not the data. Each pass only writes the pixels appropriate for that
519pass, and assumes the data from previous passes is still valid.
520
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600521 png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500522
523If you only want the first effect (the rectangles), do the same as
524before except pass the row buffer in the third parameter, and leave
525the second parameter NULL.
526
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600527 png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500528
529After you are finished reading the image, you can finish reading
Guy Schalnate5a37791996-06-05 15:50:50 -0500530the file. If you are interested in comments or time, which may be
531stored either before or after the image data, you should pass the
532info_ptr pointer from the png_read_info() call, or you can pass a
533separate png_info struct if you want to keep the comments from
534before and after the image separate. If you are not interested, you
535can pass NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -0500536
Guy Schalnate5a37791996-06-05 15:50:50 -0500537 png_read_end(png_ptr, end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500538
Guy Schalnate5a37791996-06-05 15:50:50 -0500539When you are done, you can free all memory allocated by libpng like this:
Guy Schalnat0d580581995-07-20 02:43:20 -0500540
Guy Schalnate5a37791996-06-05 15:50:50 -0500541 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
Guy Schalnat0d580581995-07-20 02:43:20 -0500542
Guy Schalnate5a37791996-06-05 15:50:50 -0500543For a more compact example of reading a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -0500544
545
Guy Schalnat6d764711995-12-19 03:22:19 -0600546Reading PNG files progressively:
547
548The progressive reader is slightly different then the non-progressive
549reader. Instead of calling png_read_info(), png_read_rows(), and
550png_read_end(), you make one call to png_process_data(), which calls
551callbacks when it has the info, a row, or the end of the image. You
552set up these callbacks with png_set_progressive_read_fn(). You don't
553have to worry about the input/output functions of libpng, as you are
554giving the library the data directly in png_process_data(). I will
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600555assume that you have read the section on reading PNG files above,
Guy Schalnat6d764711995-12-19 03:22:19 -0600556so I will only highlight the differences (although I will show
557all of the code).
558
559png_structp png_ptr;
560png_infop info_ptr;
561
Guy Schalnate5a37791996-06-05 15:50:50 -0500562/* An example code fragment of how you would initialize the progressive
563 reader in your application. */
Guy Schalnat6d764711995-12-19 03:22:19 -0600564int
565initialize_png_reader()
566{
Guy Schalnate5a37791996-06-05 15:50:50 -0500567 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
568 (void *)user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600569 if (!png_ptr)
570 return -1;
Guy Schalnate5a37791996-06-05 15:50:50 -0500571 info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600572 if (!info_ptr)
573 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500574 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600575 return -1;
576 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600577
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600578 if (setjmp(png_ptr->jmpbuf))
579 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500580 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600581 return -1;
582 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600583
Guy Schalnate5a37791996-06-05 15:50:50 -0500584 /* This one's new. You can provide functions to be called
585 when the header info is valid, when each row is completed,
586 and when the image is finished. If you aren't using all
587 functions, you can specify a NULL parameter. You can use
588 any struct as the user_ptr (cast to a void pointer for the
589 function call), and retrieve the pointer from inside the
590 callbacks using the function png_get_progressive_ptr(png_ptr);
591 which will return a void pointer, which you have to cast
592 appropriately.
593 */
594 png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600595 info_callback, row_callback, end_callback);
Guy Schalnat6d764711995-12-19 03:22:19 -0600596
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600597 return 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600598}
599
Guy Schalnate5a37791996-06-05 15:50:50 -0500600/* A code fragment that you call as you recieve blocks of data */
Guy Schalnat6d764711995-12-19 03:22:19 -0600601int
602process_data(png_bytep buffer, png_uint_32 length)
603{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600604 if (setjmp(png_ptr->jmpbuf))
605 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500606 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600607 return -1;
608 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600609
Guy Schalnate5a37791996-06-05 15:50:50 -0500610 /* This one's new also. Simply give it a chunk of data
611 from the file stream (in order, of course). On machines
612 with segmented memory models machines, don't give it any
613 more than 64K. The library seems to run fine with sizes
614 of 4K. Although you can give it much less if necessary
615 (I assume you can give it chunks of 1 byte, I haven't
616 tried less then 256 bytes yet). When this function returns,
617 you may want to display any rows that were generated in the
618 row callback if you don't already do so there.
619 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600620 png_process_data(png_ptr, info_ptr, buffer, length);
621 return 0;
Guy Schalnat6d764711995-12-19 03:22:19 -0600622}
623
Guy Schalnate5a37791996-06-05 15:50:50 -0500624/* This function is called (as set by png_set_progressive_fn() above)
625 when enough data has been supplied so all of the header has been read.
626 */
627void
Guy Schalnat6d764711995-12-19 03:22:19 -0600628info_callback(png_structp png_ptr, png_infop info)
629{
Guy Schalnate5a37791996-06-05 15:50:50 -0500630 /* Do any setup here, including setting any of the transformations
631 mentioned in the Reading PNG files section. For now, you _must_
632 call either png_start_read_image() or png_read_update_info()
633 after all the transformations are set (even if you don't set
634 any). You may start getting rows before png_process_data()
635 returns, so this is your last chance to prepare for that.
636 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600637}
638
Guy Schalnate5a37791996-06-05 15:50:50 -0500639/* This function is called when each row of image data is complete */
640void
Guy Schalnat6d764711995-12-19 03:22:19 -0600641row_callback(png_structp png_ptr, png_bytep new_row,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600642 png_uint_32 row_num, int pass)
Guy Schalnat6d764711995-12-19 03:22:19 -0600643{
Guy Schalnate5a37791996-06-05 15:50:50 -0500644 /* If the image is interlaced, and you turned on the interlace
645 handler, this function will be called for every row in every pass.
646 Some of these rows will not be changed from the previous pass.
647 When the row is not changed, the new_row variable will be NULL.
648 The rows and passes are called in order, so you don't really
649 need the row_num and pass, but I'm supplying them because it
650 may make your life easier.
Guy Schalnat6d764711995-12-19 03:22:19 -0600651
Guy Schalnate5a37791996-06-05 15:50:50 -0500652 For the non-NULL rows of interlaced images, you must call
653 png_progressive_combine_row() passing in the row and the
654 old row. You can call this function for NULL rows (it will
655 just return) and for non-interlaced images (it just does the
656 memcpy for you) if it will make the code easier. Thus, you
657 can just do this for all cases:
658 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600659
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600660 png_progressive_combine_row(png_ptr, old_row, new_row);
Guy Schalnat6d764711995-12-19 03:22:19 -0600661
Guy Schalnate5a37791996-06-05 15:50:50 -0500662 /* where old_row is what was displayed for previous rows. Note
663 that the first pass (pass == 0, really) will completely cover
664 the old row, so the rows do not have to be initialized. After
665 the first pass (and only for interlaced images), you will have
666 to pass the current row, and the function will combine the
667 old row and the new row.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600668 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600669}
670
Guy Schalnate5a37791996-06-05 15:50:50 -0500671void
Guy Schalnat6d764711995-12-19 03:22:19 -0600672end_callback(png_structp png_ptr, png_infop info)
673{
Guy Schalnate5a37791996-06-05 15:50:50 -0500674 /* This function is called after the whole image has been read,
675 including any chunks after the image (up to and including
676 the IEND). You will usually have the same info chunk as you
677 had in the header, although some data may have been added
678 to the comments and time fields.
Guy Schalnat6d764711995-12-19 03:22:19 -0600679
Guy Schalnate5a37791996-06-05 15:50:50 -0500680 Most people won't do much here, perhaps setting a flag that
681 marks the image as finished.
682 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600683}
684
685
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600686
Guy Schalnat69b14481996-01-10 02:56:49 -0600687IV. Writing
Guy Schalnat0d580581995-07-20 02:43:20 -0500688
689Much of this is very similar to reading. However, everything of
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600690importance is repeated here, so you won't have to constantly look
Guy Schalnat69b14481996-01-10 02:56:49 -0600691back up in the reading section to understand writing.
Guy Schalnat0d580581995-07-20 02:43:20 -0500692
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600693You will want to do the I/O initialization before you get into libpng,
Guy Schalnate5a37791996-06-05 15:50:50 -0500694so if it doesn't work, you don't have anything to undo. If you are not
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600695using the standard I/O functions, you will need to replace them with
Guy Schalnate5a37791996-06-05 15:50:50 -0500696custom writing functions. See the discussion under Customizing libpng.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600697
698 FILE *fp = fopen(file_name, "wb");
699 if (!fp)
700 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500701 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600702 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500703
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600704Next, png_struct and png_info need to be allocated and initialized.
Guy Schalnate5a37791996-06-05 15:50:50 -0500705As these can be both relatively large, you may not want to store these
706on the stack, unless you have stack space to spare. Of course, you
707will want to check if they return NULL.
Guy Schalnat0d580581995-07-20 02:43:20 -0500708
Guy Schalnate5a37791996-06-05 15:50:50 -0500709 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
710 (void *)user_error_ptr, user_error_fn, user_warning_fn);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600711 if (!png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500712 return;
713
714 png_infop info_ptr = png_create_info_struct(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600715 if (!info_ptr)
716 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500717 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
718 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600719 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500720
721After you have these structures, you will need to set up the
722error handling. When libpng encounters an error, it expects to
Guy Schalnate5a37791996-06-05 15:50:50 -0500723longjmp() back to your routine. Therefore, you will need to call
Guy Schalnat0d580581995-07-20 02:43:20 -0500724setjmp and pass the jmpbuf field of your png_struct. If you
725write the file from different routines, you will need to update
726the jmpbuf field every time you enter a new routine that will
727call a png_ function. See your documentation of setjmp/longjmp
Guy Schalnat6d764711995-12-19 03:22:19 -0600728for your compiler for more information on setjmp/longjmp. See
Guy Schalnat69b14481996-01-10 02:56:49 -0600729the discussion on libpng error handling in the Customizing Libpng
730section below for more information on the libpng error handling.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600731
732 if (setjmp(png_ptr->jmpbuf))
733 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500734 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600735 fclose(fp);
736 return;
737 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500738
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600739Now you need to set up the input code. The default for libpng is to
740use the C function fwrite(). If you use this, you will need to pass a
741valid FILE * in the function png_init_io(). Be sure that the file is
742opened in binary mode. Again, if you wish to handle writing data in
743another way, see the discussion on libpng I/O handling in the Customizing
744Libpng section below.
Guy Schalnat0d580581995-07-20 02:43:20 -0500745
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600746 png_init_io(png_ptr, fp);
Guy Schalnat0d580581995-07-20 02:43:20 -0500747
Guy Schalnate5a37791996-06-05 15:50:50 -0500748You now have the option of modifying how the compression library will
749run. The following functions are mainly for testing, but may be useful
750in some cases, like if you need to write png files extremely fast and
751are willing to give up some compression, or if you want to get the
752maximum possible compression at the expense of slower writing. If you
753have no special needs in this area, let the library do what it wants by
754not calling this function at all, as it has been tuned to deliver a good
755speed/compression ratio. The second parameter to png_set_filter() is
756the filter method, for which the only valid value is '0' (as of the
75706/96 PNG specification. The third parameter is a flag that indicates
758which filter type(s) are to be tested for each scanline. See the
759Compression Library for details on the specific filter types.
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500760
Guy Schalnate5a37791996-06-05 15:50:50 -0500761
762 /* turn on or off filtering, and/or choose specific filters */
763 png_set_filter(png_ptr, 0,
764 PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_PAETH);
765
766The png_set_compression_???() functions interface to the zlib compression
767library, and should mostly be ignored unless you really know what you are
768doing. The only generally useful call is png_set_compression_level()
769which changes how much time zlib spends on trying to compress the image
770data. See the Compression Library for details on the compression levels.
771
772 /* set the zlib compression level */
773 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
774
775 /* set other zlib parameters */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600776 png_set_compression_mem_level(png_ptr, 8);
777 png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
778 png_set_compression_window_bits(png_ptr, 15);
779 png_set_compression_method(png_ptr, 8);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500780
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600781You now need to fill in the png_info structure with all the data you
782wish to write before the actual image. Note that the only thing you
783are allowed to write after the image is the text chunks and the time
Guy Schalnate5a37791996-06-05 15:50:50 -0500784chunk (as of PNG Specification 1.0, anyway). See png_write_end() and
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600785the latest PNG specification for more information on that. If you
Guy Schalnate5a37791996-06-05 15:50:50 -0500786wish to write them before the image, fill them in now, and flag that
787data as being valid. If you want to wait until after the data, don't
788fill them until png_write_end(). For all the fields in png_info and
789their data types, see png.h. For explanations of what the fields
790contain, see the PNG specification.
Guy Schalnat69b14481996-01-10 02:56:49 -0600791
792Some of the more important parts of the png_info are:
793
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600794 width - holds the width of the file
795 height - holds the height of the file
796 bit_depth - holds the bit depth of one of the image channels
797 color_type - describes the channels and what they mean
798 see the PNG_COLOR_TYPE_ defines for more information
Guy Schalnate5a37791996-06-05 15:50:50 -0500799 interlace_type - allowed values are 0 for none, 1 for interlaced
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600800 valid - this describes which optional chunks to write to the
801 file. Note that if you are writing a
802 PNG_COLOR_TYPE_PALETTE file, the PLTE chunk is not
803 optional, but must still be marked for writing. To
Guy Schalnate5a37791996-06-05 15:50:50 -0500804 mark chunks for writing, logical OR '|' valid with
805 the appropriate PNG_INFO_<chunk name> define.
806 palette - the palette for the file (PNG_INFO_PLTE)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600807 num_palette - number of entries in the palette
Guy Schalnate5a37791996-06-05 15:50:50 -0500808 gamma - the gamma the file is written at (PNG_INFO_gAMA)
809 sig_bit - the number of significant bits (PNG_INFO_sBIT)
810 for the gray, red, green, and blue channels, whichever
811 are appropriate for the given color type.
812 trans_values - transparent pixel for non-paletted images (PNG_INFO_tRNS)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600813 trans - array of transparent entries for paletted images
814 num_trans - number of transparent entries
Guy Schalnate5a37791996-06-05 15:50:50 -0500815 hist - histogram of palette (PNG_INFO_hIST)
816 mod_time - time image was last modified (PNG_VALID_tIME)
817 background - background color (PNG_VALID_bKGD)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600818 text - text comments in the file.
819 num_text - number of comments
Guy Schalnat0d580581995-07-20 02:43:20 -0500820
821A quick word about text and num_text. text is an array of png_text
822structures. num_text is the number of valid structures in the array.
823If you want, you can use max_text to hold the size of the array, but
824libpng ignores it for writing (it does use it for reading). Each
825png_text structure holds a keyword-text value, and a compression type.
826The compression types have the same valid numbers as the compression
827types of the image data. Currently, the only valid number is zero.
828However, you can store text either compressed or uncompressed, unlike
829images which always have to be compressed. So if you don't want the
830text compressed, set the compression type to -1. Until text gets
Guy Schalnate5a37791996-06-05 15:50:50 -0500831around 1000 bytes, it is not worth compressing it.
832
833The keywords that are given in the PNG Specification are:
834
835 Title Short (one line) title or caption for image
836 Author Name of image's creator
837 Description Description of image (possibly long)
838 Copyright Copyright notice
839 Creation Time Time of original image creation
840 Software Software used to create the image
841 Disclaimer Legal disclaimer
842 Warning Warning of nature of content
843 Source Device used to create the image
844 Comment Miscellaneous comment; conversion from other
845 image format
Guy Schalnat0d580581995-07-20 02:43:20 -0500846
847The keyword-text pairs work like this. Keywords should be short
848simple descriptions of what the comment is about. Some typical
849keywords are found in the PNG specification, as is some recomendations
850on keywords. You can repeat keywords in a file. You can even write
851some text before the image and some after. For example, you may want
852to put a description of the image before the image, but leave the
853disclaimer until after, so viewers working over modem connections
854don't have to wait for the disclaimer to go over the modem before
855they start seeing the image. Finally, keywords should be full
Guy Schalnate5a37791996-06-05 15:50:50 -0500856words, not abbreviations. Keywords and text are in the ISO 8859-1
857(Latin-1) character set (a superset of regular ASCII) and can not
858contain NUL characters, and should not contain control or other
859unprintable characters. To make the comments widely readable, stick
860with basic ASCII, and avoid machine specific character set extensions
861like the IBM-PC character set. The keyword must be present, but
Guy Schalnat0d580581995-07-20 02:43:20 -0500862you can leave off the text string on non-compressed pairs.
863Compressed pairs must have a text string, as only the text string
864is compressed anyway, so the compression would be meaningless.
865
Guy Schalnat6d764711995-12-19 03:22:19 -0600866PNG supports modification time via the png_time structure. Two
Guy Schalnat0d580581995-07-20 02:43:20 -0500867conversion routines are proved, png_convert_from_time_t() for
868time_t and png_convert_from_struct_tm() for struct tm. The
869time_t routine uses gmtime(). You don't have to use either of
870these, but if you wish to fill in the png_time structure directly,
871you should provide the time in universal time (GMT) if possible
Guy Schalnat69b14481996-01-10 02:56:49 -0600872instead of your local time. Note that the year number is the full
Guy Schalnate5a37791996-06-05 15:50:50 -0500873year (ie 1996, rather than 96), and that months start with 1.
Guy Schalnat0d580581995-07-20 02:43:20 -0500874
875You are now ready to write all the file information up to the actual
876image data. You do this with a call to png_write_info().
877
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600878 png_write_info(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500879
Guy Schalnate5a37791996-06-05 15:50:50 -0500880After you've written the file information, you can set up the library
881to handle any special transformations of the image data. The various
Guy Schalnat0d580581995-07-20 02:43:20 -0500882ways to transform the data will be described in the order that they
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600883should occur. This is important, as some of these change the color
884type and/or bit depth of the data, and some others only work on
885certain color types and bit depths. Even though each transformation
886checks to see if it has data that it can do somthing with, you should
887make sure to only enable a transformation if it will be valid for the
888data. For example, don't swap red and blue on grayscale data.
Guy Schalnat0d580581995-07-20 02:43:20 -0500889
Guy Schalnate5a37791996-06-05 15:50:50 -0500890PNG files store RGB pixels packed into 3 bytes. This code tells
891the library to expect input data with 4 bytes per pixel
Guy Schalnat0d580581995-07-20 02:43:20 -0500892
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600893 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Guy Schalnat0d580581995-07-20 02:43:20 -0500894
Guy Schalnate5a37791996-06-05 15:50:50 -0500895where the 0 is the value that will be put in the 4th byte, and the
896location is either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending
897upon whether the filler byte is stored XRGB or RGBX.
Guy Schalnat0d580581995-07-20 02:43:20 -0500898
899PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
900they can, resulting in, for example, 8 pixels per byte for 1 bit files.
Guy Schalnat69b14481996-01-10 02:56:49 -0600901If the data is supplied at 1 pixel per byte, use this code, which will
Guy Schalnate5a37791996-06-05 15:50:50 -0500902correctly pack the pixels into a single byte:
Guy Schalnat0d580581995-07-20 02:43:20 -0500903
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600904 png_set_packing(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500905
906PNG files reduce possible bit depths to 1, 2, 4, 8, and 16. If your
Guy Schalnate5a37791996-06-05 15:50:50 -0500907data is of another bit depth, you can write an sBIT chunk into the
908file so that decoders can get the original data if desired.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600909
910 /* Do this before png_write_info() */
911 info_ptr->valid |= PNG_INFO_sBIT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500912
Guy Schalnate5a37791996-06-05 15:50:50 -0500913 /* Set the true bit depth of the image data */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600914 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
915 {
916 info_ptr->sig_bit.red = true_bit_depth;
917 info_ptr->sig_bit.green = true_bit_depth;
918 info_ptr->sig_bit.blue = true_bit_depth;
919 }
920 else
921 {
922 info_ptr->sig_bit.gray = true_bit_depth;
923 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500924
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600925 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
926 {
927 info_ptr->sig_bit.alpha = true_bit_depth;
928 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500929
Guy Schalnate5a37791996-06-05 15:50:50 -0500930If the data is stored in the row buffer in a bit depth other than
931one supported by PNG (ie 3 bit data in the range 0-7 for a 4-bit PNG),
932this will scale the values to appear to be the correct bit depth as
933is required by PNG.
934
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600935 png_set_shift(png_ptr, &(info_ptr->sig_bit));
Guy Schalnat0d580581995-07-20 02:43:20 -0500936
Guy Schalnat69b14481996-01-10 02:56:49 -0600937PNG files store 16 bit pixels in network byte order (big-endian,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600938ie. most significant bits first). This code would be used if they are
939supplied the other way (little-endian, ie. least significant bits
940first, eg. the way PCs store them):
Guy Schalnat0d580581995-07-20 02:43:20 -0500941
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600942 png_set_swap(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500943
Guy Schalnat69b14481996-01-10 02:56:49 -0600944PNG files store 3 color pixels in red, green, blue order. This code
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600945would be used if they are supplied as blue, green, red:
Guy Schalnat0d580581995-07-20 02:43:20 -0500946
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600947 png_set_bgr(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500948
Guy Schalnat69b14481996-01-10 02:56:49 -0600949PNG files describe monochrome as black being zero and white being
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600950one. This code would be used if the pixels are supplied with this reversed
Guy Schalnat69b14481996-01-10 02:56:49 -0600951(black being one and white being zero):
Guy Schalnat0d580581995-07-20 02:43:20 -0500952
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600953 png_set_invert(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500954
Guy Schalnate5a37791996-06-05 15:50:50 -0500955It is possible to have libpng flush any pending output, either manually,
956or automatically after a certain number of lines have been written. To
957flush the output stream a single time call:
958
959 png_write_flush(png_ptr);
960
961and to have libpng flush the output stream periodically after a certain
962number of scanlines have been written, call:
963
964 png_set_flush(png_ptr, nrows);
965
966Note that the distance between rows is from the last time png_write_flush()
967was called, or the first row of the image if it has never been called.
968So if you write 50 lines, and then png_set_flush 25, it will flush the
969output on the next scanline, and every 25 lines thereafter, unless
970png_write_flush()ls is called before 25 more lines have been written.
971If nrows is too small (less than about 10 lines for a 640 pixel wide
972RGB image) the image compression may decrease noticably (although this
973may be acceptable for real-time applications). Infrequent flushing will
974only degrade the compression performance by a few percent over images
975that do not use flushing.
976
Guy Schalnat0d580581995-07-20 02:43:20 -0500977That's it for the transformations. Now you can write the image data.
978The simplest way to do this is in one function call. If have the
979whole image in memory, you can just call png_write_image() and libpng
980will write the image. You will need to pass in an array of pointers to
981each row. This function automatically handles interlacing, so you don't
982need to call png_set_interlace_handling() or call this function multiple
983times, or any of that other stuff necessary with png_write_rows().
984
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600985 png_write_image(png_ptr, row_pointers);
Guy Schalnat0d580581995-07-20 02:43:20 -0500986
987where row_pointers is:
988
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600989 png_bytef *row_pointers[height];
Guy Schalnat0d580581995-07-20 02:43:20 -0500990
991You can point to void or char or whatever you use for pixels.
992
993If you can't want to write the whole image at once, you can
994use png_write_rows() instead. If the file is not interlaced,
995this is simple:
996
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600997 png_write_rows(png_ptr, row_pointers, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -0500998
999row_pointers is the same as in the png_write_image() call.
1000
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001001If you are just writing one row at a time, you can do this with
Guy Schalnat0d580581995-07-20 02:43:20 -05001002row_pointers:
1003
Guy Schalnate5a37791996-06-05 15:50:50 -05001004 png_bytep row_pointer = row;
Guy Schalnat0d580581995-07-20 02:43:20 -05001005
Guy Schalnate5a37791996-06-05 15:50:50 -05001006 png_write_row(png_ptr, &row_pointer);
Guy Schalnat0d580581995-07-20 02:43:20 -05001007
Guy Schalnat69b14481996-01-10 02:56:49 -06001008When the file is interlaced, things can get a good deal more
Guy Schalnate5a37791996-06-05 15:50:50 -05001009complicated. The only currently (as of 6/96 -- PNG Specification
1010version 1.0) defined interlacing scheme for PNG files is a
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001011compilcated interlace scheme, known as Adam7, that breaks down an
1012image into seven smaller images of varying size. libpng will build
1013these images for you, or you can do them yourself. If you want to
1014build them yourself, see the PNG specification for details of which
1015pixels to write when.
Guy Schalnat0d580581995-07-20 02:43:20 -05001016
1017If you don't want libpng to handle the interlacing details, just
Guy Schalnate5a37791996-06-05 15:50:50 -05001018use png_set_interlace_handling() and call png_write_rows() the
1019correct number of times to write all seven sub-images.
Guy Schalnat0d580581995-07-20 02:43:20 -05001020
Guy Schalnat69b14481996-01-10 02:56:49 -06001021If you want libpng to build the sub-images, call this before you start
1022writing any rows:
Guy Schalnat0d580581995-07-20 02:43:20 -05001023
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001024 number_passes = png_set_interlace_handling(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001025
1026This will return the number of passes needed. Currently, this
1027is seven, but may change if another interlace type is added.
1028
Guy Schalnate5a37791996-06-05 15:50:50 -05001029Then write the complete image number_passes times.
Guy Schalnat0d580581995-07-20 02:43:20 -05001030
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001031 png_write_rows(png_ptr, row_pointers, number_of_rows);
Guy Schalnat0d580581995-07-20 02:43:20 -05001032
1033As some of these rows are not used, and thus return immediately,
1034you may want to read about interlacing in the PNG specification,
1035and only update the rows that are actually used.
1036
1037After you are finished writing the image, you should finish writing
1038the file. If you are interested in writing comments or time, you should
1039pass the an appropriately filled png_info pointer. If you
Guy Schalnate5a37791996-06-05 15:50:50 -05001040are not interested, you can pass NULL. If you have written text at
1041the beginning and are not writing more at the end, you should set
1042info_ptr->num_text = 0, or the text will be written again here.
Guy Schalnat0d580581995-07-20 02:43:20 -05001043
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001044 png_write_end(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001045
1046When you are done, you can free all memory used by libpng like this:
1047
Guy Schalnate5a37791996-06-05 15:50:50 -05001048 png_destroy_write_struct(&png_ptr, &info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001049
Guy Schalnate5a37791996-06-05 15:50:50 -05001050You must free any data you allocated for info_ptr, such as comments,
1051palette, or histogram, before the call to png_destroy_write_struct();
Guy Schalnat0d580581995-07-20 02:43:20 -05001052
Guy Schalnate5a37791996-06-05 15:50:50 -05001053For a more compact example of writing a PNG image, see the file example.c.
Guy Schalnat0d580581995-07-20 02:43:20 -05001054
1055
Guy Schalnat69b14481996-01-10 02:56:49 -06001056V. Modifying/Customizing libpng:
Guy Schalnat0d580581995-07-20 02:43:20 -05001057
1058There are two issues here. The first is changing how libpng does
1059standard things like memory allocation, input/output, and error handling.
1060The second deals with more complicated things like adding new chunks,
1061adding new transformations, and generally changing how libpng works.
1062
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001063All of the memory allocation, input/output, and error handling in
1064libpng goes through callbacks which are user setable. The default
Guy Schalnate5a37791996-06-05 15:50:50 -05001065routines are in pngmem.c, pngrio.c, pngwio.c, and pngerror.c respectively.
1066To change these functions, call the approprate png_set_???_fn() function.
Guy Schalnat0d580581995-07-20 02:43:20 -05001067
Guy Schalnat6d764711995-12-19 03:22:19 -06001068Memory allocation is done through the functions png_large_malloc(),
Guy Schalnate5a37791996-06-05 15:50:50 -05001069png_malloc(), png_realloc(), png_large_free(), and png_free(). These
1070currently just call the standard C functions. The large functions must
1071handle exactly 64K, but they don't have to handle more than that. If
1072your pointers can't access more then 64K at a time, you will want to
1073set MAXSEG_64K in zlib.h. Since it is unlikely that the method of
1074handling memory allocation on a platform will change between applications,
1075these functions must be modified in the library at compile time.
Guy Schalnat0d580581995-07-20 02:43:20 -05001076
Guy Schalnat6d764711995-12-19 03:22:19 -06001077Input/Output in libpng is done throught png_read() and png_write(), which
1078currently just call fread() and fwrite(). The FILE * is stored in
1079png_struct, and is initialized via png_init_io(). If you wish to change
Guy Schalnat69b14481996-01-10 02:56:49 -06001080the method of I/O, the library supplies callbacks that you can set through
1081the function png_set_read_fn() and png_set_write_fn() at run time. These
1082functions also provide a void pointer that can be retrieved via the function
Guy Schalnat6d764711995-12-19 03:22:19 -06001083png_get_io_ptr(). For example:
Guy Schalnat0d580581995-07-20 02:43:20 -05001084
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001085 png_set_read_fn(png_structp png_ptr, voidp io_ptr,
1086 png_rw_ptr read_data_fn)
Guy Schalnat0f716451995-11-28 11:22:13 -06001087
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001088 png_set_write_fn(png_structp png_ptr, voidp io_ptr,
1089 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn);
Guy Schalnat0f716451995-11-28 11:22:13 -06001090
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001091 voidp io_ptr = png_get_io_ptr(png_ptr);
Guy Schalnat6d764711995-12-19 03:22:19 -06001092
Guy Schalnat69b14481996-01-10 02:56:49 -06001093The replacement I/O functions should have prototypes as follows:
1094
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001095 void user_read_data(png_structp png_ptr, png_bytep data,
1096 png_uint_32 length);
1097 void user_write_data(png_structp png_ptr, png_bytep data,
1098 png_uint_32 length);
1099 void user_flush_data(png_structp png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001100
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001101Supplying NULL for the read, write, or flush functions sets them back
1102to using the default C stream functions. It is an error to read from
1103a write stream, and vice versa.
Guy Schalnat6d764711995-12-19 03:22:19 -06001104
1105Error handling in libpng is done through png_error() and png_warning().
1106Errors handled through png_error() are fatal, meaning that png_error()
1107should never return to it's caller. Currently, this is handled via
1108setjmp() and longjmp(), but you could change this to do things like
Guy Schalnat69b14481996-01-10 02:56:49 -06001109exit() if you should wish. On non-fatal errors, png_warning() is called
1110to print a warning message, and then control returns to the calling code.
Guy Schalnate5a37791996-06-05 15:50:50 -05001111By default png_error() and png_warning() print a message on stderr via
1112fprintf() unless the library is compiled with PNG_NO_STDIO defined. If
Guy Schalnat69b14481996-01-10 02:56:49 -06001113you wish to change the behavior of the error functions, you will need to
Guy Schalnate5a37791996-06-05 15:50:50 -05001114set up your own message callbacks. These functions are normally supplied
1115at the time that the png_struct is created. It is also possible to change
1116these functions after png_create_???_struct() has been called by calling:
Guy Schalnat69b14481996-01-10 02:56:49 -06001117
Guy Schalnate5a37791996-06-05 15:50:50 -05001118 png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
1119 png_error_ptr error_fn, png_error_ptr warning_fn);
Guy Schalnat69b14481996-01-10 02:56:49 -06001120
Guy Schalnate5a37791996-06-05 15:50:50 -05001121 png_voidp error_ptr = png_get_error_ptr(png_ptr);
Guy Schalnat69b14481996-01-10 02:56:49 -06001122
Guy Schalnate5a37791996-06-05 15:50:50 -05001123If NULL is supplied for either error_fn or warning_fn, then the libpng
1124default function will be used, calling fprintf() and/or longjmp() if a
1125problem is encountered. The replacement error functions should have
1126parameters as follows:
Guy Schalnat69b14481996-01-10 02:56:49 -06001127
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001128 void user_error_fn(png_struct png_ptr, png_const_charp error_msg);
1129 void user_warning_fn(png_struct png_ptr, png_const_charp warning_msg);
Guy Schalnat69b14481996-01-10 02:56:49 -06001130
1131The motivation behind using setjmp() and longjmp() is the C++ throw and
1132catch exception handling methods. This makes the code much easier to write,
1133as there is no need to check every return code of every function call.
1134However, there are some uncertainties about the status of local variables
1135after a longjmp, so the user may want to be careful about doing anything after
Guy Schalnat0d580581995-07-20 02:43:20 -05001136setjmp returns non zero besides returning itself. Consult your compiler
Guy Schalnat69b14481996-01-10 02:56:49 -06001137documentation for more details.
Guy Schalnat0f716451995-11-28 11:22:13 -06001138
Guy Schalnat0d580581995-07-20 02:43:20 -05001139If you need to read or write custom chunks, you will need to get deeper
Guy Schalnate5a37791996-06-05 15:50:50 -05001140into the libpng code, as a mechanism has not yet been supplied for user
1141callbacks with custom chunks. First, read the PNG specification, and have
1142a first level of understanding of how it works. Pay particular attention
1143to the sections that describe chunk names, and look at how other chunks
1144were designed, so you can do things similarly. Second, check out the
1145sections of libpng that read and write chunks. Try to find a chunk that
1146is similar to yours and copy off of it. More details can be found in the
1147comments inside the code. A way of handling unknown chunks in a generic
1148method, potentially via callback functions, would be best.
Guy Schalnat0d580581995-07-20 02:43:20 -05001149
Guy Schalnate5a37791996-06-05 15:50:50 -05001150If you wish to write your own transformation for the data, look through
1151the part of the code that does the transformations, and check out some of
1152the simpler ones to get an idea of how they work. Try to find a similar
1153transformation to the one you want to add and copy off of it. More details
1154can be found in the comments inside the code itself.
Guy Schalnat0d580581995-07-20 02:43:20 -05001155
1156Configuring for 16 bit platforms:
1157
Guy Schalnate5a37791996-06-05 15:50:50 -05001158You may need to change the png_large_malloc() and png_large_free()
1159routines in pngmem.c, as these are requred to allocate 64K, although
1160there is already support for many of the common DOS compilers. Also,
1161you will want to look into zconf.h to tell zlib (and thus libpng) that
1162it cannot allocate more then 64K at a time. Even if you can, the memory
1163won't be accessable. So limit zlib and libpng to 64K by defining MAXSEG_64K.
Guy Schalnat0d580581995-07-20 02:43:20 -05001164
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001165Configuring for DOS:
1166
1167For DOS users which only have access to the lower 640K, you will
1168have to limit zlib's memory usage via a png_set_compression_mem_level()
1169call. See zlib.h or zconf.h in the zlib library for more information.
1170
Guy Schalnat6d764711995-12-19 03:22:19 -06001171Configuring for Medium Model:
1172
1173Libpng's support for medium model has been tested on most of the popular
Guy Schalnat69b14481996-01-10 02:56:49 -06001174complers. Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets
1175defined, and FAR gets defined to far in pngconf.h, and you should be
Guy Schalnat6d764711995-12-19 03:22:19 -06001176all set. Everything in the library (except for zlib's structure) is
1177expecting far data. You must use the typedefs with the p or pp on
1178the end for pointers (or at least look at them and be careful). Make
1179note that the row's of data are defined as png_bytepp which is a
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001180unsigned char far * far *.
Guy Schalnat6d764711995-12-19 03:22:19 -06001181
Guy Schalnat0d580581995-07-20 02:43:20 -05001182Configuring for gui/windowing platforms:
1183
Guy Schalnate5a37791996-06-05 15:50:50 -05001184You will need to write new error and warning functions that use the GUI
1185interface, as described previously, and set them to be the error and
1186warning functions at the time that png_create_???_struct() is called,
1187in order to have them available during the structure initialization.
1188They can be changed later via png_set_error_fn(). On some compliers,
1189you may also have to change the memory allocators (png_malloc, etc.).
Guy Schalnat6d764711995-12-19 03:22:19 -06001190
Guy Schalnat0d580581995-07-20 02:43:20 -05001191Configuring for compiler xxx:
1192
Guy Schalnat69b14481996-01-10 02:56:49 -06001193All includes for libpng are in pngconf.h. If you need to add/change/delete
Guy Schalnat0d580581995-07-20 02:43:20 -05001194an include, this is the place to do it. The includes that are not
1195needed outside libpng are protected by the PNG_INTERNAL definition,
1196which is only defined for those routines inside libpng itself. The
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001197files in libpng proper only include png.h, which includes pngconf.h.
1198
1199Configuring zlib:
1200
Guy Schalnate5a37791996-06-05 15:50:50 -05001201There are special functions to configure the compression. Perhaps the
1202most useful one changes the compression level, which currently uses
1203input compression values in the range 0 - 9. The library normally
1204uses the default compression level (Z_DEFAULT_COMPRESSION = 6), but if
1205speed is not critical it is possible to configure it for maximum
1206compression (Z_BEST_COMPRESSION = 9) to generate smaller PNG files.
1207For online applications it may be desirable to have maximum speed
1208(Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also
1209specify no compression (Z_NO_COMPRESSION = 0), but this would create
1210files larger than just storing the raw bitmap. You can specify the
1211compression level by calling:
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001212
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001213 png_set_compression_mem_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001214
1215Another useful one is to reduce the memory level used by the library.
1216The memory level defaults to 8, but it can be lowered if you are
1217short on memory (running DOS, for example, where you only have 640K).
1218
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001219 png_set_compression_mem_level(png_ptr, level);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001220
1221If you want to control whether libpng uses filtering or not, you
Guy Schalnate5a37791996-06-05 15:50:50 -05001222can call this function. Filtering is enabled by default for RGB
1223and grayscale images (with and without alpha), and for 8-bit
1224paletted images, but not for paletted images with bit depths less
1225than 8 bits/pixel. The 'method' parameter sets the main filtering
1226method, which is currently only '0' in the PNG 1.0 specification.
1227The 'filters' parameter sets which filter(s), if any, should be
1228used for each scanline. Possible values are PNG_ALL_FILTERS and
1229PNG_NO_FILTERS to turn filtering on and off, respectively.
1230Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB,
1231PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise
1232ORed together '|' to specify one or more filters to use. These
1233filters are described in more detail in the PNG specification. If
1234you intend to change the filter type during the course of writing
1235the image, you should start with flags set for all of the filters
1236you intend to use so that libpng can initialize its internal
1237structures appropriately for all of the filter types.
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001238
Guy Schalnate5a37791996-06-05 15:50:50 -05001239 png_set_filter(png_ptr, method, filters);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001240
1241The other functions are for configuring zlib. They are not recommended
Guy Schalnate5a37791996-06-05 15:50:50 -05001242for normal use and may result in writing an invalid PNG file. See
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001243zlib.h for more information on what these mean.
1244
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001245 png_set_compression_strategy(png_ptr, strategy);
1246 png_set_compression_window_bits(png_ptr, window_bits);
1247 png_set_compression_method(png_ptr, method);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001248
Guy Schalnate5a37791996-06-05 15:50:50 -05001249Except for png_set_filter(), all of these are just controlling zlib,
1250so see the zlib documentation (zlib.h and zconf.h) for more information.
Guy Schalnat0d580581995-07-20 02:43:20 -05001251
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001252Removing unwanted object code:
1253
Guy Schalnat69b14481996-01-10 02:56:49 -06001254There are a bunch of #define's in pngconf.h that control what parts of
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001255libpng are compiled. All the defines end in _SUPPORT. If you are
Guy Schalnate5a37791996-06-05 15:50:50 -05001256never going to use an ability, you can change the #define to #undef
1257before recompiling libpng and save yourself code and data space. All
1258the reading and writing specific code are in seperate files, so the
1259linker should only grab the files it needs. However, if you want to
1260make sure, or if you are building a stand alone library, all the
1261reading files start with pngr and all the writing files start with
1262pngw. The files that don't match either (like png.c, pngtrans.c, etc.)
1263are used for both reading and writing, and always need to be included.
1264The progressive reader is in pngpread.c
1265
1266If you are creating or distributing a dynamically linked library (a .so
1267or DLL file), you should not remove or disable any parts of the
1268library, as this will cause applications linked with different versions
1269of the library to fail if they call functions not available in your
1270library. The size of the library itself should not be an issue, because
1271only those sections which are actually used will be loaded into memory.
1272
1273Changes to Libpng from version 0.88 to version 0.89
1274
1275It should be noted that version 0.89 of libpng is not distributed by
1276the original author, Guy Schalnat, but rather Andreas Dilger, although
1277all of the copyright messages have been left in Guy's name.
1278
1279The old libpng functions png_read_init(), png_write_init() and
1280png_info_init() still exist in the 0.89 version of the library, as
1281do png_read_destroy() and png_write_destroy(). The preferred method
1282of creating and initializing the libpng structures is via the
1283png_create_read_struct(), png_create_write_struct(), and
1284png_create_info_struct() because they isolate the size of the structures
1285from the application, allow version error checking, and also allow
1286the use of custom error handling routines during the initialization,
1287which the old functions do not. The functions png_read_destroy() and
1288png_write_destroy() do not actually free the memory that libpng allocated
1289for these structs, but just reset the data structures, so they can be
1290used instead of png_destroy_read_struct() and png_destroy_write_struct()
1291if you feel there is too much system overhead allocating and freeing the
1292png_struct for each image read.
1293
1294Setting the error callbacks via png_set_message_fn() before
1295png_read_init() as was suggested in libpng-0.88 is no longer supported
1296because this caused applications which do not use custom error functions
1297to fail if the png_ptr was not initialized to zero. It is still possible
1298to set the error callbacks AFTER png_read_init(), or to change them with
1299png_set_error_fn(), which is essentially the same function, but with a
1300new name to force compilation errors with the new library.
Guy Schalnat6d764711995-12-19 03:22:19 -06001301