summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/libpng/libpng-manual.txt
diff options
context:
space:
mode:
Diffstat (limited to 'src/3rdparty/libpng/libpng-manual.txt')
-rw-r--r--src/3rdparty/libpng/libpng-manual.txt713
1 files changed, 528 insertions, 185 deletions
diff --git a/src/3rdparty/libpng/libpng-manual.txt b/src/3rdparty/libpng/libpng-manual.txt
index 77adf7a..c91b99c 100644
--- a/src/3rdparty/libpng/libpng-manual.txt
+++ b/src/3rdparty/libpng/libpng-manual.txt
@@ -1,6 +1,6 @@
libpng-manual.txt - A description on how to use and modify libpng
- libpng version 1.5.1 - February 3, 2011
+ libpng version 1.5.4 - July 7, 2011
Updated and distributed by Glenn Randers-Pehrson
<glennrp at users.sourceforge.net>
Copyright (c) 1998-2011 Glenn Randers-Pehrson
@@ -11,7 +11,7 @@ libpng-manual.txt - A description on how to use and modify libpng
Based on:
- libpng versions 0.97, January 1998, through 1.5.1 - February 3, 2011
+ libpng versions 0.97, January 1998, through 1.5.4 - July 7, 2011
Updated and distributed by Glenn Randers-Pehrson
Copyright (c) 1998-2011 Glenn Randers-Pehrson
@@ -198,9 +198,9 @@ reconfiguration of pnglibconf.h. To reconfigure pnglibconf.h it must either be
rebuilt from scripts/pnglibconf.dfa using awk or it must be edited by hand.
Hand editing is achieved by copying scripts/pnglibconf.h.prebuilt and changing
-the lines defining the supported features, paying very close attention to the
-'option' information in scripts/pnglibconf.dfa that describes those features and
-their requirements. This is easy to get wrong.
+the lines defining the supported features, paying very close attention to
+the 'option' information in scripts/pnglibconf.dfa that describes those
+features and their requirements. This is easy to get wrong.
B. Configuration using DFA_XTRA
@@ -348,21 +348,12 @@ create the structure, so your application should check for that.
return (ERROR);
}
- png_infop end_info = png_create_info_struct(png_ptr);
-
- if (!end_info)
- {
- png_destroy_read_struct(&png_ptr, &info_ptr,
- (png_infopp)NULL);
- return (ERROR);
- }
-
If you want to use your own memory allocation routines,
use a libpng that was built with PNG_USER_MEM_SUPPORTED defined, and use
png_create_read_struct_2() instead of png_create_read_struct():
png_structp png_ptr = png_create_read_struct_2
- (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
+ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
user_error_fn, user_warning_fn, (png_voidp)
user_mem_ptr, user_malloc_fn, user_free_fn);
@@ -374,7 +365,7 @@ handling and memory alloc/free functions.
When libpng encounters an error, it expects to longjmp back
to your routine. Therefore, you will need to call setjmp and pass
your png_jmpbuf(png_ptr). If you read the file from different
-routines, you will need to update the jmpbuf field every time you enter
+routines, you will need to update the longjmp buffer every time you enter
a new routine that will call a png_*() function.
See your documentation of setjmp/longjmp for your compiler for more
@@ -392,6 +383,9 @@ free any memory.
return (ERROR);
}
+Pass (png_infopp)NULL instead of &end_info if you didn't create
+an end_info structure.
+
If you would rather avoid the complexity of setjmp/longjmp issues,
you can compile libpng with PNG_NO_SETJMP, in which case
errors will result in a call to PNG_ABORT() which defaults to abort().
@@ -513,6 +507,19 @@ To inform libpng about your function, use
png_set_read_status_fn(png_ptr, read_row_callback);
+When this function is called the row has already been completely processed and
+the 'row' and 'pass' refer to the next row to be handled. For the
+non-interlaced case the row that was just handled is simply one less than the
+passed in row number, and pass will always be 0. For the interlaced case the
+same applies unless the row value is 0, in which case the row just handled was
+the last one from one of the preceding passes. Because interlacing may skip a
+pass you cannot be sure that the preceding pass is just 'pass-1', if you really
+need to know what the last pass is record (row,pass) from the callback and use
+the last recorded value each time.
+
+As with the user transform you can find the output row using the
+PNG_ROW_FROM_PASS_ROW macro.
+
Unknown-chunk handling
Now you get to set the way the library processes unknown chunks in the
@@ -590,7 +597,7 @@ large as 2^31-1 (0x7fffffff), or about 2.147 billion rows and columns.
Since very few applications really need to process such large images,
we have imposed an arbitrary 1-million limit on rows and columns.
Larger images will be rejected immediately with a png_error() call. If
-you wish to override this limit, you can use
+you wish to change this limit, you can use
png_set_user_limits(png_ptr, width_max, height_max);
@@ -600,6 +607,10 @@ anyway because of potential buffer overflow conditions).
You should put this statement after you create the PNG structure and
before calling png_read_info(), png_read_png(), or png_process_data().
+
+When writing a PNG datastream, put this statement before calling
+png_write_info() or png_write_png().
+
If you need to retrieve the limits that are being applied, use
width_max = png_get_user_width_max(png_ptr);
@@ -630,6 +641,234 @@ and you can retrieve the limit with
Any chunks that would cause either of these limits to be exceeded will
be ignored.
+Information about your system
+
+If you intend to display the PNG or to incorporate it in other image data you
+need to tell libpng information about your display or drawing surface so that
+libpng can convert the values in the image to match the display.
+
+From libpng-1.5.4 this information can be set before reading the PNG file
+header. In earlier versions png_set_gamma() existed but behaved incorrectly if
+called before the PNG file header had been read and png_set_alpha_mode() did not
+exist.
+
+If you need to support versions prior to libpng-1.5.4 test the version number
+and follow the procedures described in the appropriate manual page.
+
+You give libpng the encoding expected by your system expressed as a 'gamma'
+value. You can also specify a default encoding for the PNG file in
+case the required information is missing from the file. By default libpng
+assumes that the PNG data matches your system, to keep this default call:
+
+ png_set_gamma(png_ptr, screen_gamma, 1/screen_gamma/*file gamma*/);
+
+or you can use the fixed point equivalent:
+
+ png_set_gamma_fixed(png_ptr, PNG_FP_1*screen_gamma, PNG_FP_1/screen_gamma);
+
+If you don't know the gamma for you system it is probably 2.2 - a good
+approximation to the IEC standard for display systems (sRGB). If images are
+too contrasty or washed out you got the value wrong - check your system
+documentation!
+
+Many systems permit the system gamma to be changed via a lookup table in the
+display driver, a few systems, including older Macs, change the response by
+default. As of 1.5.4 three special values are available to handle common
+situations:
+
+ PNG_DEFAULT_sRGB: Indicates that the system conforms to the IEC 61966-2-1
+ standard. This matches almost all systems.
+ PNG_GAMMA_MAC_18: Indicates that the system is an older (pre Mac OS 10.6)
+ Apple Macintosh system with the default settings.
+ PNG_GAMMA_LINEAR: Just the fixed point value for 1.0 - indicates that the
+ system expects data with no gamma encoding.
+
+You would use the linear (unencoded) value if you need to process the pixel
+values further because this avoids the need to decode and reencode each
+component value whenever arithmetic is performed. A lot of graphics software
+uses linear values for this reason, often with higher precision component values
+to preserve overall accuracy.
+
+The second thing you may need to tell libpng about is how your system handles
+alpha channel information. Some, but not all, PNG files contain an alpha
+channel. To display these files correctly you need to compose the data onto a
+suitable background, as described in the PNG specification.
+
+Libpng only supports composing onto a single color (using png_set_background;
+see below.) Otherwise you must do the composition yourself and, in this case,
+you may need to call png_set_alpha_mode:
+
+ png_set_alpha_mode(png_ptr, mode, screen_gamma);
+
+The screen_gamma value is the same as the argument to png_set_gamma, however how
+it affects the output depends on the mode. png_set_alpha_mode() sets the file
+gamma default to 1/screen_gamma, so normally you don't need to call
+png_set_gamma. If you need different defaults call png_set_gamma() before
+png_set_alpha_mode() - if you call it after it will override the settings made
+by png_set_alpha_mode().
+
+The mode is as follows:
+
+ PNG_ALPHA_PNG: The data is encoded according to the PNG specification. Red,
+green and blue, or gray, components are gamma encoded color
+values and are not premultiplied by the alpha value. The
+alpha value is a linear measure of the contribution of the
+pixel to the corresponding final output pixel.
+
+You should normally use this format if you intend to perform
+color correction on the color values; most, maybe all, color
+correction software has no handling for the alpha channel and,
+anyway, the math to handle pre-multiplied component values is
+unnecessarily complex.
+
+Before you do any arithmetic on the component values you need
+to remove the gamma encoding and multiply out the alpha
+channel. See the PNG specification for more detail. It is
+important to note that when an image with an alpha channel is
+scaled, linear encoded, pre-multiplied component values must
+be used!
+
+The remaining modes assume you don't need to do any further color correction or
+that if you do your color correction software knows all about alpha (it
+probably doesn't!)
+
+ PNG_ALPHA_STANDARD: The data libpng produces is encoded in the standard way
+assumed by most correctly written graphics software.
+The gamma encoding will be removed by libpng and the
+linear component values will be pre-multiplied by the
+alpha channel.
+
+With this format the final image must be re-encoded to
+match the display gamma before the image is displayed.
+If your system doesn't do that, yet still seems to
+perform arithmetic on the pixels without decoding them,
+it is broken - check out the modes below.
+
+With PNG_ALPHA_STANDARD libpng always produces linear
+component values, whatever screen_gamma you supply. The
+screen_gamma value is, however, used as a default for
+the file gamma if the PNG file has no gamma information.
+
+If you call png_set_gamma() after png_set_alpha_mode() you
+will override the linear encoding. Instead the
+pre-multiplied pixel values will be gamma encoded but
+the alpha channel will still be linear. This may
+actually match the requirements of some broken software,
+but it is unlikely.
+
+While linear 8-bit data is often used it has
+insufficient precision for any image with a reasonable
+dynamic range. To avoid problems, and if your software
+supports it, use png_set_expand_16() to force all
+components to 16 bits.
+
+ PNG_ALPHA_OPTIMIZED: This mode is the same as PNG_ALPHA_STANDARD except that
+completely opaque pixels are gamma encoded according to
+the screen_gamma value. Pixels with alpha less than 1.0
+will still have linear components.
+
+Use this format if you have control over your
+compositing software and do don't do other arithmetic
+(such as scaling) on the data you get from libpng. Your
+compositing software can simply copy opaque pixels to
+the output but still has linear values for the
+non-opaque pixels.
+
+In normal compositing, where the alpha channel encodes
+partial pixel coverage (as opposed to broad area
+translucency), the inaccuracies of the 8-bit
+representation of non-opaque pixels are irrelevant.
+
+You can also try this format if your software is broken;
+it might look better.
+
+ PNG_ALPHA_BROKEN: This is PNG_ALPHA_STANDARD however all component values,
+including the alpha channel are gamma encoded. This is
+an appropriate format to try if your software, or more
+likely hardware, is totally broken: if it performs
+linear arithmetic directly on gamma encoded values.
+
+In most cases of broken software or hardware the bug in the final display
+manifests as a subtle halo around composited parts of the image. You may not
+even perceive this as a halo; the composited part of the image may simply appear
+separate from the background, as though it had been cut out of paper and pasted
+on afterward.
+
+If you don't have to deal with bugs in software or hardware, or if you can fix
+them, there are three recommended ways of using png_set_alpha_mode():
+
+ png_set_alpha_mode(png_ptr, PNG_ALPHA_PNG,
+ screen_gamma);
+
+You can do color correction on the result (libpng does not currently
+support color correction internally.) When you handle the alpha channel
+you need to undo the gamma encoding and multiply out the alpha.
+
+ png_set_alpha_mode(png_ptr, PNG_ALPHA_STANDARD,
+ screen_gamma);
+ png_set_expand_16(png_ptr);
+
+If you are using the high level interface don't call png_set_expand_16();
+instead pass PNG_TRANSFORM_EXPAND_16 to the interface.
+
+With this mode you can't do color correction, but you can do arithmetic,
+including composition and scaling, on the data without further processing.
+
+ png_set_alpha_mode(png_ptr, PNG_ALPHA_OPTIMIZED,
+ screen_gamma);
+
+You can avoid the expansion to 16-bit components with this mode, but you
+lose the ability to scale the image or perform other linear arithmetic.
+All you can do is compose the result onto a matching output. Since this
+mode is libpng specific you also need to write your own composition
+software.
+
+If you don't need, or can't handle, the alpha channel you can call
+png_set_background() to remove it by compositing against a fixed color. Don't
+call png_set_strip_alpha() to do this - it will leave spurious pixel values in
+transparent parts of this image.
+
+ png_set_background(png_ptr, &background_color,
+ PNG_BACKGROUND_GAMMA_SCREEN, 0, 1);
+
+The background_color is an RGB or grayscale value according to the data format
+libpng will produce for you. Because you don't yet know the format of the PNG
+file if you call png_set_background at this point you must arrange for the
+format produced by libpng to always have 8-bit or 16-bit components and then
+store the color as an 8-bit or 16-bit color as appropriate. The color contains
+separate gray and RGB component values, so you can let libpng produce gray or
+RGB output according to the input format, but low bit depth grayscale images
+must always be converted to at least 8-bit format. (Even low low bit depth
+grayscale images can't have an alpha channel they can have a transparent
+color!)
+
+You set the transforms you need later, either as flags to the high level
+interface or libpng API calls for the low level interface. For reference the
+settings and API calls required are:
+
+8-bit values:
+ PNG_TRANSFORM_SCALE_16 | PNG_EXPAND
+ png_set_expand(png_ptr); png_set_scale_16(png_ptr);
+
+ If you must get exactly the same inaccurate results
+ produced by default in versions prior to libpng-1.5.4,
+ use PNG_TRANSFORM_STRIP_16 and png_set_strip_16(png_ptr)
+ instead.
+
+16-bit values:
+ PNG_TRANSFORM_EXPAND_16
+ png_set_expand_16(png_ptr);
+
+In either case palette image data will be expanded to RGB. If you just want
+color data you can add PNG_TRANSFORM_GRAY_TO_RGB or png_set_gray_to_rgb(png_ptr)
+to the list.
+
+Calling png_set_background before the PNG file header is read will not work
+prior to libpng-1.5.4. Because the failure may result in unexpected warnings or
+errors it is therefore much safer to call png_set_background after the head has
+been read. Unfortunately this means that prior to libpng-1.5.4 it cannot be
+used with the high level interface.
+
The high-level read interface
At this point there are two ways to proceed; through the high-level
@@ -639,8 +878,10 @@ the entire image into memory, and (b) the input transformations
you want to do are limited to the following set:
PNG_TRANSFORM_IDENTITY No transformation
- PNG_TRANSFORM_STRIP_16 Strip 16-bit samples to
- 8 bits
+ PNG_TRANSFORM_SCALE_16 Strip 16-bit samples to
+ 8-bit accurately
+ PNG_TRANSFORM_STRIP_16 Chop 16-bit samples to
+ 8-bit less accurately
PNG_TRANSFORM_STRIP_ALPHA Discard the alpha channel
PNG_TRANSFORM_PACKING Expand 1, 2 and 4-bit
samples to bytes
@@ -659,6 +900,7 @@ you want to do are limited to the following set:
PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples
PNG_TRANSFORM_GRAY_TO_RGB Expand grayscale samples
to RGB (or GA to RGBA)
+ PNG_TRANSFORM_EXPAND_16 Expand samples to 16 bits
(This excludes setting a background color, doing gamma transformation,
quantizing, and setting filler.) If this is the case, simply do this:
@@ -727,6 +969,22 @@ call to png_read_info().
This will process all chunks up to but not including the image data.
+This also copies some of the data from the PNG file into the decode structure
+for use in later transformations. Important information copied in is:
+
+1) The PNG file gamma from the gAMA chunk. This overwrites the default value
+provided by an earlier call to png_set_gamma or png_set_alpha_mode.
+
+2) Prior to libpng-1.5.4 the background color from a bKGd chunk. This
+damages the information provided by an earlier call to png_set_background
+resulting in expected behavior. Libpng-1.5.4 no longer does this.
+
+3) The number of significant bits in each component value. Libpng uses this to
+optimize gamma handling by reducing the internal lookup table sizes.
+
+4) The transparent color information from a tRNS chunk. This can be modified by
+a later call to png_set_tRNS.
+
Querying the info structure
Functions are used to get the information from the info_ptr once it
@@ -1069,7 +1327,7 @@ forms:
72 dpi is stored as 0.28346 pixels/meter, and
when this is retrieved it is 71.9988 dpi, so
be sure to round the returned value appropriately
- if you want to display a reasonable-looking result.
+ if you want to display a reasonable-looking result.
The data from the oFFs chunk can be retrieved in several convenient
forms:
@@ -1090,7 +1348,7 @@ forms:
converted to microns and back without some loss
of precision.
-For more information, see the png_info definition in png.h and the
+For more information, see the
PNG specification for chunk contents. Be careful with trusting
rowbytes, as some of the transformations could increase the space
needed to hold a row (expand, filler, gray_to_rgb, etc.).
@@ -1125,17 +1383,20 @@ to handle any special transformations of the image data. The various
ways to transform the data will be described in the order that they
should occur. This is important, as some of these change the color
type and/or bit depth of the data, and some others only work on
-certain color types and bit depths. Even though each transformation
-checks to see if it has data that it can do something with, you should
-make sure to only enable a transformation if it will be valid for the
-data. For example, don't swap red and blue on grayscale data.
+certain color types and bit depths.
+
+Transformations you request are ignored if they don't have any meaning for a
+particular input data format. However some transformations can have an effect
+as a result of a previous transformation. If you specify a contradictory set of
+transformations, for example both adding and removing the alpha channel, you
+cannot predict the final result.
-The colors used for the background and transparency values should be
-supplied in the same format/depth as the current image data. They
-are stored in the same format/depth as the image data in a bKGD or tRNS
-chunk, so this is what libpng expects for this data. The colors are
-transformed to keep in sync with the image data when an application
-calls the png_read_update_info() routine (see below).
+The color used for the transparency values should be supplied in the same
+format/depth as the current image data. It is stored in the same format/depth
+as the image data in a tRNS chunk, so this is what libpng expects for this data.
+
+The color used for the background value depends on the need_expand argument as
+described below.
Data will be decoded into the supplied row buffers packed into bytes
unless the library has been told to transform it into another format.
@@ -1145,12 +1406,12 @@ byte, unless png_set_packing() is called. 8-bit RGB data will be stored
in RGB RGB RGB format unless png_set_filler() or png_set_add_alpha()
is called to insert filler bytes, either before or after each RGB triplet.
16-bit RGB data will be returned RRGGBB RRGGBB, with the most significant
-byte of the color value first, unless png_set_strip_16() is called to
+byte of the color value first, unless png_set_scale_16() is called to
transform it to regular RGB RGB triplets, or png_set_filler() or
png_set_add alpha() is called to insert filler bytes, either before or
after each RRGGBB triplet. Similarly, 8-bit or 16-bit grayscale data can
-be modified with
-png_set_filler(), png_set_add_alpha(), or png_set_strip_16().
+be modified with png_set_filler(), png_set_add_alpha(), png_set_strip_16(),
+or png_set_scale_16().
The following code transforms grayscale images of less than 8 to 8 bits,
changes paletted images to RGB, and adds a full alpha channel if there is
@@ -1161,13 +1422,13 @@ viewing application that wishes to treat all images in the same way.
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
- if (color_type == PNG_COLOR_TYPE_GRAY &&
- bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
-
if (png_get_valid(png_ptr, info_ptr,
PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
-These three functions are actually aliases for png_set_expand(), added
+ if (color_type == PNG_COLOR_TYPE_GRAY &&
+ bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
+
+The first two functions are actually aliases for png_set_expand(), added
in libpng version 1.0.4, with the function names expanded to improve code
readability. In some future version they may actually do different
things.
@@ -1175,56 +1436,84 @@ things.
As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() was
added. It expands the sample depth without changing tRNS to alpha.
-As of libpng version 1.5.1, not all possible expansions are supported.
+As of libpng version 1.5.2, png_set_expand_16() was added. It behaves as
+png_set_expand(), however, the resultant channels have 16 bits rather than 8.
+Use this when the output color or gray channels are made linear to avoid fairly
+severe accuracy loss.
+
+ if (bit_depth < 16)
+ png_set_expand_16(png_ptr);
+
+PNG can have files with 16 bits per channel. If you only can handle
+8 bits per channel, this will strip the pixels down to 8-bit.
+
+ if (bit_depth == 16)
+ png_set_scale_16(png_ptr);
+
+If you need to process the alpha channel on the image separately from the image
+data (for example if you convert it to a bitmap mask) it is possible to have
+libpng strip the channel leaving just RGB or gray data:
+
+ if (color_type & PNG_COLOR_MASK_ALPHA)
+ png_set_strip_alpha(png_ptr);
+
+If you strip the alpha channel you need to find some other way of dealing with
+the information. If, instead, you want to convert the image to an opaque
+version with no alpha channel use png_set_background; see below.
+
+As of libpng version 1.5.2, almost all useful expansions are supported, the
+major ommissions are conversion of grayscale to indexed images (which can be
+done trivially in the application) and conversion of indexed to grayscale (which
+can be done by a trivial manipulation of the palette.)
In the following table, the 01 means grayscale with depth<8, 31 means
indexed with depth<8, other numerals represent the color type, "T" means
the tRNS chunk is present, A means an alpha channel is present, and O
means tRNS or alpha is present but all pixels in the image are opaque.
- FROM 01 31 0 0T 0O 2 2T 2O 3 3T 3O 4A 4O 6A 6O
+ FROM 01 31 0 0T 0O 2 2T 2O 3 3T 3O 4A 4O 6A 6O
TO
- 01 -
- 31 -
- 0 1 -
- 0T -
- 0O -
- 2 GX -
- 2T -
- 2O -
- 3 1 -
- 3T -
- 3O -
- 4A T -
- 4O -
- 6A GX TX TX -
- 6O GX TX -
+ 01 - [G] - - - - - - - - - - - - -
+ 31 [Q] Q [Q] [Q] [Q] Q Q Q Q Q Q [Q] [Q] Q Q
+ 0 1 G + . . G G G G G G B B GB GB
+ 0T lt Gt t + . Gt G G Gt G G Bt Bt GBt GBt
+ 0O lt Gt t . + Gt Gt G Gt Gt G Bt Bt GBt GBt
+ 2 C P C C C + . . C - - CB CB B B
+ 2T Ct - Ct C C t + t - - - CBt CBt Bt Bt
+ 2O Ct - Ct C C t t + - - - CBt CBt Bt Bt
+ 3 [Q] p [Q] [Q] [Q] Q Q Q + . . [Q] [Q] Q Q
+ 3T [Qt] p [Qt][Q] [Q] Qt Qt Qt t + t [Qt][Qt] Qt Qt
+ 3O [Qt] p [Qt][Q] [Q] Qt Qt Qt t t + [Qt][Qt] Qt Qt
+ 4A lA G A T T GA GT GT GA GT GT + BA G GBA
+ 4O lA GBA A T T GA GT GT GA GT GT BA + GBA G
+ 6A CA PA CA C C A T tT PA P P C CBA + BA
+ 6O CA PBA CA C C A tT T PA P P CBA C BA +
Within the matrix,
+ "+" identifies entries where 'from' and 'to' are the same.
"-" means the transformation is not supported.
+ "." means nothing is necessary (a tRNS chunk can just be ignored).
+ "t" means the transformation is obtained by png_set_tRNS.
+ "A" means the transformation is obtained by png_set_add_alpha().
"X" means the transformation is obtained by png_set_expand().
"1" means the transformation is obtained by
- png_set_expand_gray_1_2_4_to_8
- "G" means the transformation is obtained by
- png_set_gray_to_rgb().
+ png_set_expand_gray_1_2_4_to_8() (and by png_set_expand() if there
+ is no transparency in the original or the final format).
+ "C" means the transformation is obtained by png_set_gray_to_rgb().
+ "G" means the transformation is obtained by png_set_rgb_to_gray().
"P" means the transformation is obtained by
png_set_expand_palette_to_rgb().
- "T" means the transformation is obtained by
- png_set_tRNS_to_alpha().
-
-PNG can have files with 16 bits per channel. If you only can handle
-8 bits per channel, this will strip the pixels down to 8 bit.
-
- if (bit_depth == 16)
- png_set_strip_16(png_ptr);
-
-If, for some reason, you don't need the alpha channel on an image,
-and you want to remove it rather than combining it with the background
-(but the image author certainly had in mind that you *would* combine
-it with the background, so that's what you should probably do):
-
- if (color_type & PNG_COLOR_MASK_ALPHA)
- png_set_strip_alpha(png_ptr);
+ "p" means the transformation is obtained by png_set_packing().
+ "Q" means the transformation is obtained by png_set_quantize().
+ "T" means the transformation is obtained by png_set_tRNS_to_alpha().
+ "B" means the transformation is obtained by png_set_background(), or
+ png_strip_alpha().
+
+When an entry has multiple transforms listed all are required to cause the
+right overall transformation. When two transforms are separated by a comma
+either will do the job. When transforms are enclosed in [] the transform should
+do the job but this is currently unimplemented - a different format will result
+if the suggested transformations are used.
In PNG files, the alpha channel in an image
is the level of opacity. If you need the alpha channel in an image to
@@ -1303,8 +1592,8 @@ with alpha.
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- png_set_rgb_to_gray_fixed(png_ptr, error_action,
- int red_weight, int green_weight);
+ png_set_rgb_to_gray(png_ptr, error_action, double red_weight,
+ double green_weight);
error_action = 1: silently do the conversion
@@ -1317,122 +1606,89 @@ with alpha.
image has any pixel where
red != green or red != blue
- red_weight: weight of red component times 100000
+ red_weight: weight of red component
- green_weight: weight of green component times 100000
+ green_weight: weight of green component
If either weight is negative, default
- weights (21268, 71514) are used.
+ weights are used.
+
+In the corresponding fixed point API the red_weight and green_weight values are
+simply scaled by 100,000:
+
+ png_set_rgb_to_gray(png_ptr, error_action, png_fixed_point red_weight,
+ png_fixed_point green_weight);
If you have set error_action = 1 or 2, you can
later check whether the image really was gray, after processing
the image rows, with the png_get_rgb_to_gray_status(png_ptr) function.
It will return a png_byte that is zero if the image was gray or
-1 if there were any non-gray pixels. bKGD and sBIT data
+1 if there were any non-gray pixels. Background and sBIT data
will be silently converted to grayscale, using the green channel
-data, regardless of the error_action setting.
-
-With red_weight+green_weight<=100000,
-the normalized graylevel is computed:
+data for sBIT, regardless of the error_action setting.
- int rw = red_weight * 65536;
- int gw = green_weight * 65536;
- int bw = 65536 - (rw + gw);
- gray = (rw*red + gw*green + bw*blue)/65536;
+The default values come from the PNG file cHRM chunk if present, otherwise the
+defaults correspond to the ITU-R recommendation 709, and also the sRGB color
+space, as recommended in the Charles Poynton's Colour FAQ,
+<http://www.poynton.com/>, in section 9:
-The default values approximate those recommended in the Charles
-Poynton's Color FAQ, <http://www.inforamp.net/~poynton/>
-Copyright (c) 1998-01-04 Charles Poynton <poynton at inforamp.net>
+ <http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC9>
Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
-Libpng approximates this with integers scaled by 32768:
-
- Y = (6968 * R + 23434 * G + 2366 * B)/32768
-
The calculation is done in a linear colorspace, if the image gamma
can be determined.
-If you have a grayscale and you are using png_set_expand_depth(),
-png_set_expand(), or png_set_gray_to_rgb to change to truecolor or to
-a higher bit-depth, you must either supply the background color as a gray
-value at the original file bit-depth (need_expand = 1) or else supply the
-background color as an RGB triplet at the final, expanded bit depth
-(need_expand = 0). Similarly, if you are reading a paletted image, you
-must either supply the background color as a palette index (need_expand = 1)
-or as an RGB triplet that may or may not be in the palette (need_expand = 0).
+The png_set_background() function has been described already, it tells libpng to
+composite images with alpha or simple transparency against the supplied
+background color. For compatibility with versions of libpng earlier than
+libpng-1.5.4 it is recommended that you call the function after reading the file
+header, even if you don't want to use the color in a bKGD chunk, if one exists.
+
+If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
+you may use this color, or supply another color more suitable for
+the current display (e.g., the background color from a web page). You
+need to tell libpng how the color is represented, both the format of the
+component values in the color (the number of bits) and the gamme encoding of the
+color. The function takes two arguments, background_gamma_mode and need_expand
+to convey this information, however only two combinations are like to be useful:
png_color_16 my_background;
png_color_16p image_background;
if (png_get_bKGD(png_ptr, info_ptr, &image_background))
png_set_background(png_ptr, image_background,
- PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
+ PNG_BACKGROUND_GAMMA_FILE, 1/*needs to be expanded*/, 1);
else
png_set_background(png_ptr, &my_background,
- PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
+ PNG_BACKGROUND_GAMMA_SCREEN, 0/*do not expand*/, 1);
-The png_set_background() function tells libpng to composite images
-with alpha or simple transparency against the supplied background
-color. If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
-you may use this color, or supply another color more suitable for
-the current display (e.g., the background color from a web page). You
-need to tell libpng whether the color is in the gamma space of the
-display (PNG_BACKGROUND_GAMMA_SCREEN for colors you supply), the file
-(PNG_BACKGROUND_GAMMA_FILE for colors from the bKGD chunk), or one
-that is neither of these gammas (PNG_BACKGROUND_GAMMA_UNIQUE - I don't
-know why anyone would use this, but it's here).
-
-To properly display PNG images on any kind of system, the application needs
-to know what the display gamma is. Ideally, the user will know this, and
-the application will allow them to set it. One method of allowing the user
-to set the display gamma separately for each system is to check for a
-SCREEN_GAMMA or DISPLAY_GAMMA environment variable, which will hopefully be
-correctly set.
-
-Note that display_gamma is the overall gamma correction required to produce
-pleasing results, which depends on the lighting conditions in the surrounding
-environment. In a dim or brightly lit room, no compensation other than
-the physical gamma exponent of the monitor is needed, while in a dark room
-a slightly smaller exponent is better.
-
- double gamma, screen_gamma;
-
- if (/* We have a user-defined screen
- gamma value */)
- {
- screen_gamma = user_defined_screen_gamma;
- }
- /* One way that applications can share the same
- screen gamma value */
- else if ((gamma_str = getenv("SCREEN_GAMMA"))
- != NULL)
- {
- screen_gamma = (double)atof(gamma_str);
- }
+The second call was described above - my_background is in the format of the
+final, display, output produced by libpng. Because you now know the format of
+the PNG it is possible to avoid the need to choose either 8-bit or 16-bit
+output and to retain palette images (the palette colors will be modified
+appropriately and the tRNS chunk removed.) However, if you are doing this,
+take great care not to ask for transformations without checking first that
+they apply!
- /* If we don't have another value */
- else
- {
- screen_gamma = 2.2; /* A good guess for a
- PC monitor in a bright office or a dim room */
+In the first call the background color has the original bit depth and color type
+of the PNG file. So, for palette images the color is supplied as a palette
+index and for low bit greyscale images the color is a reduced bit value in
+image_background->gray.
- screen_gamma = 2.0; /* A good guess for a
- PC monitor in a dark room */
+If you didn't call png_set_gamma() before reading the file header, for example
+if you need your code to remain compatible with older versions of libpng prior
+to libpng-1.5.4, this is the place to call it.
- screen_gamma = 1.7 or 1.0; /* A good
- guess for Mac systems */
- }
+Do not call it if you called png_set_alpha_mode(); doing so will damage the
+settings put in place by png_set_alpha_mode(). (If png_set_alpha_mode() is
+supported then you can certainly do png_set_gamma() before reading the PNG
+header.)
-The functions png_set_gamma() and its fixed point equivalent
-png_set_gamma_fixed() handle gamma transformations of the data.
-Pass both the file gamma and the current screen_gamma. If the file does
-not have a gamma value, you can pass one anyway if you have an idea what
-it is (usually 0.45455 is a good guess for GIF images on PCs). Note
-that file gammas are inverted from screen gammas. See the discussions
-on gamma in the PNG specification for an excellent description of what
-gamma is, and why all applications should support it. It is strongly
-recommended that PNG viewers support gamma correction.
+This API unconditionally sets the screen and file gamma values, so it will
+override the value in the PNG file unless it is called before the PNG file
+reading starts. For this reason you must always call it with the PNG file
+value when you call it in this position:
if (png_get_gAMA(png_ptr, info_ptr, &file_gamma))
png_set_gamma(png_ptr, screen_gamma, file_gamma);
@@ -1488,7 +1744,7 @@ This function can also be used to invert grayscale and gray-alpha images:
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_invert_mono(png_ptr);
-PNG files store 16 bit pixels in network byte order (big-endian,
+PNG files store 16-bit pixels in network byte order (big-endian,
ie. most significant bits first). This code changes the storage to the
other way (little-endian, i.e. least significant bits first, the
way PCs store them):
@@ -1511,7 +1767,7 @@ with
You must supply the function
- void read_transform_fn(png_structp png_ptr, row_info_ptr
+ void read_transform_fn(png_structp png_ptr, png_row_infop
row_info, png_bytep data)
See pngtest.c for a working example. Your function will be called
@@ -1530,6 +1786,14 @@ supported if user transforms are supported, secondly they may well return
unexpected results unless the row is actually being processed at the moment they
are called.
+With interlaced
+images the value returned is the row in the input sub-image image. Use
+PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to
+find the output pixel (x,y) given an interlaced sub-image pixel (row,col,pass).
+
+The discussion of interlace handling above contains more information on how to
+use these values.
+
You can also set up a pointer to a user structure for use by your
callback function, and you can inform libpng that your transform
function will change the number of channels or bit depth with the
@@ -1772,15 +2036,39 @@ low-level interface, you can finish reading the file. If you are
interested in comments or time, which may be stored either before or
after the image data, you should pass the separate png_info struct if
you want to keep the comments from before and after the image
-separate. If you are not interested, you can pass NULL.
+separate.
+
+ png_infop end_info = png_create_info_struct(png_ptr);
+
+ if (!end_info)
+ {
+ png_destroy_read_struct(&png_ptr, &info_ptr,
+ (png_infopp)NULL);
+ return (ERROR);
+ }
png_read_end(png_ptr, end_info);
+If you are not interested, you should still call png_read_end()
+but you can pass NULL, avoiding the need to create an end_info structure.
+
+ png_read_end(png_ptr, (png_infop)NULL);
+
+If you don't call png_read_end(), then your file pointer will be
+left pointing to the first chunk after the last IDAT, which is probably
+not what you want if you expect to read something beyond the end of
+the PNG datastream.
+
When you are done, you can free all memory allocated by libpng like this:
png_destroy_read_struct(&png_ptr, &info_ptr,
&end_info);
+or, if you didn't create an end_info structure,
+
+ png_destroy_read_struct(&png_ptr, &info_ptr,
+ (png_infopp)NULL);
+
It is also possible to individually free the info_ptr members that
point to libpng-allocated storage with the following function:
@@ -2183,6 +2471,20 @@ To inform libpng about your function, use
png_set_write_status_fn(png_ptr, write_row_callback);
+When this function is called the row has already been completely processed and
+it has also been written out. The 'row' and 'pass' refer to the next row to be
+handled. For the
+non-interlaced case the row that was just handled is simply one less than the
+passed in row number, and pass will always be 0. For the interlaced case the
+same applies unless the row value is 0, in which case the row just handled was
+the last one from one of the preceding passes. Because interlacing may skip a
+pass you cannot be sure that the preceding pass is just 'pass-1', if you really
+need to know what the last pass is record (row,pass) from the callback and use
+the last recorded value each time.
+
+As with the user transform you can find the output row using the
+PNG_ROW_FROM_PASS_ROW macro.
+
You now have the option of modifying how the compression library will
run. The following functions are mainly for testing, but may be useful
in some cases, like if you need to write PNG files extremely fast and
@@ -2227,11 +2529,13 @@ which changes how much time zlib spends on trying to compress the image
data. See the Compression Library (zlib.h and algorithm.txt, distributed
with zlib) for details on the compression levels.
- /* set the zlib compression level */
+ #include zlib.h
+
+ /* Set the zlib compression level */
png_set_compression_level(png_ptr,
Z_BEST_COMPRESSION);
- /* set other zlib parameters */
+ /* Set other zlib parameters for compressing IDAT */
png_set_compression_mem_level(png_ptr, 8);
png_set_compression_strategy(png_ptr,
Z_DEFAULT_STRATEGY);
@@ -2239,7 +2543,15 @@ with zlib) for details on the compression levels.
png_set_compression_method(png_ptr, 8);
png_set_compression_buffer_size(png_ptr, 8192)
-extern PNG_EXPORT(void,png_set_zbuf_size)
+ /* Set zlib parameters for text compression
+ * If you don't call these, the parameters
+ * fall back on those defined for IDAT chunks
+ */
+ png_set_text_compression_mem_level(png_ptr, 8);
+ png_set_text_compression_strategy(png_ptr,
+ Z_DEFAULT_STRATEGY);
+ png_set_text_compression_window_bits(png_ptr, 15);
+ png_set_text_compression_method(png_ptr, 8);
Setting the contents of info for output
@@ -2745,7 +3057,7 @@ is required by PNG.
png_set_shift(png_ptr, &sig_bit);
-PNG files store 16 bit pixels in network byte order (big-endian,
+PNG files store 16-bit pixels in network byte order (big-endian,
ie. most significant bits first). This code would be used if they are
supplied the other way (little-endian, i.e. least significant bits
first, the way PCs store them):
@@ -2779,8 +3091,8 @@ with
You must supply the function
- void write_transform_fn(png_structp png_ptr ptr,
- row_info_ptr row_info, png_bytep data)
+ void write_transform_fn(png_structp png_ptr, png_row_infop
+ row_info, png_bytep data)
See pngtest.c for a working example. Your function will be called
before any of the other transformations are processed. If supported
@@ -2788,9 +3100,15 @@ libpng also supplies an information routine that may be called from
your callback:
png_get_current_row_number(png_ptr);
+ png_get_current_pass_number(png_ptr);
-This returns the current row passed to the transform. Even with interlaced
-images the value returned is the row in the final output image.
+This returns the current row passed to the transform. With interlaced
+images the value returned is the row in the input sub-image image. Use
+PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to
+find the output pixel (x,y) given an interlaced sub-image pixel (row,col,pass).
+
+The discussion of interlace handling above contains more information on how to
+use these values.
You can also set up a pointer to a user structure for use by your
callback function.
@@ -3155,7 +3473,7 @@ the simpler ones to get an idea of how they work. Try to find a similar
transformation to the one you want to add and copy off of it. More details
can be found in the comments inside the code itself.
-Configuring for 16 bit platforms
+Configuring for 16-bit platforms
You will want to look into zconf.h to tell zlib (and thus libpng) that
it cannot allocate more then 64K at a time. Even if you can, the memory
@@ -3210,6 +3528,7 @@ specify no compression (Z_NO_COMPRESSION = 0), but this would create
files larger than just storing the raw bitmap. You can specify the
compression level by calling:
+ #include zlib.h
png_set_compression_level(png_ptr, level);
Another useful one is to reduce the memory level used by the library.
@@ -3220,12 +3539,14 @@ other things, lower levels will result in sections of incompressible
data being emitted in smaller stored blocks, with a correspondingly
larger relative overhead of up to 15% in the worst case.
+ #include zlib.h
png_set_compression_mem_level(png_ptr, level);
The other functions are for configuring zlib. They are not recommended
for normal use and may result in writing an invalid PNG file. See
zlib.h for more information on what these mean.
+ #include zlib.h
png_set_compression_strategy(png_ptr,
strategy);
@@ -3623,12 +3944,12 @@ and memset(), respectively.
The function png_set_gray_1_2_4_to_8() was removed. It has been
deprecated since libpng-1.0.18 and 1.2.9, when it was replaced with
png_set_expand_gray_1_2_4_to_8() because the former function also
-expanded palette images.
+expanded any tRNS chunk to an alpha channel.
Macros for png_get_uint_16, png_get_uint_32, and png_get_int_32
were added and are used by default instead of the corresponding
functions. Unfortunately,
-from libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
+from libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
function) incorrectly returned a value of type png_uint_32.
We changed the prototype for png_malloc() from
@@ -3658,7 +3979,8 @@ The png_zalloc() function no longer zeroes out the memory that it
allocates.
Support for dithering was disabled by default in libpng-1.4.0, because
-been well tested and doesn't actually "dither". The code was not
+it has not been well tested and doesn't actually "dither".
+The code was not
removed, however, and could be enabled by building libpng with
PNG_READ_DITHER_SUPPORTED defined. In libpng-1.4.2, this support
was reenabled, but the function was renamed png_set_quantize() to
@@ -3671,7 +3993,7 @@ We removed the trailing '.' from the warning and error messages.
X. Changes to Libpng from version 1.4.x to 1.5.x
-From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
+From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
function) incorrectly returned a value of type png_uint_32.
A. Changes that affect users of libpng
@@ -3708,7 +4030,8 @@ application code. (It is extremely rare for an application to do this.)
Any program that compiled against libpng 1.4 and did not use deprecated
features or access internal library structures should compile and work
-against libpng 1.5.
+against libpng 1.5, except for the change in the prototype for
+png_get_iCCP() and png_set_iCCP() API functions mentioned above.
libpng 1.5.0 adds PNG_ PASS macros to help in the reading and writing of
interlaced images. The macros return the number of rows and columns in
@@ -3716,10 +4039,11 @@ each pass and information that can be used to de-interlace and (if
absolutely necessary) interlace an image.
libpng 1.5.0 adds an API png_longjmp(png_ptr, value). This API calls
-the application provided png_longjmp_ptr on the internal, but application
+the application-provided png_longjmp_ptr on the internal, but application
initialized, jmpbuf. It is provided as a convenience to avoid the need
-to use the png_jmpbuf macro, which had the unnecessary side effect of
-resetting the internal png_longjmp_ptr value.
+initialized, longjmp buffer. It is provided as a convenience to avoid
+the need to use the png_jmpbuf macro, which had the unnecessary side
+effect of resetting the internal png_longjmp_ptr value.
libpng 1.5.0 includes a complete fixed point API. By default this is
present along with the corresponding floating point API. In general the
@@ -3762,7 +4086,7 @@ reset by pngusr.h or by explicit settings on the compiler command line.
These settings may produce compiler warnings or errors in 1.5.0 because
of macro redefinition.
-From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
+From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
function) incorrectly returned a value of type png_uint_32. libpng 1.5.0
is consistent with the implementation in 1.4.5 and 1.2.x (where the macro
did not exist.)
@@ -3773,6 +4097,25 @@ PNG_NO_USE_READ_MACROS before including png.h. Notice that this is
only supported from 1.5.0 -defining PNG_NO_USE_READ_MACROS prior to 1.5.0
will lead to a link failure.
+Prior to libpng-1.5.4, the zlib compressor used the same set of parameters
+when compressing the IDAT data and textual data such as zTXt and iCCP.
+In libpng-1.5.4 we reinitialized the zlib stream for each type of data.
+We added five png_set_text_*() functions for setting the parameters to
+use with textual data.
+
+Prior to libpng-1.5.4, the PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
+option was off by default, and slightly inaccurate scaling occurred.
+This option can no longer be turned off, and the choice of accurate
+or inaccurate 16-to-8 scaling is by using the new png_set_scale_16_to_8()
+API for accurate scaling or the old png_set_strip_16_to_8() API for simple
+chopping.
+
+Prior to libpng-1.5.4, the png_set_user_limits() function could only be
+used to reduce the width and height limits from the value of
+PNG_USER_WIDTH_MAX and PNG_USER_HEIGHT_MAX, although this document said
+that it could be used to override them. Now this function will reduce or
+increase the limits.
+
B. Changes to the build and configuration of libpng
Details of internal changes to the library code can be found in the CHANGES
@@ -4081,13 +4424,13 @@ Other rules can be inferred by inspecting the libpng source.
XIV. Y2K Compliance in libpng
-February 3, 2011
+July 7, 2011
Since the PNG Development group is an ad-hoc body, we can't make
an official declaration.
This is your unofficial assurance that libpng from version 0.71 and
-upward through 1.5.1 are Y2K compliant. It is my belief that earlier
+upward through 1.5.4 are Y2K compliant. It is my belief that earlier
versions were also Y2K compliant.
Libpng only has three year fields. One is a 2-byte unsigned integer that