Sortix cross-volatile manual
This manual documents Sortix cross-volatile. You can instead view this document in the latest official manual.
LIBPNG(3) | Library Functions Manual | LIBPNG(3) |
NAME
libpng - Portable Network Graphics (PNG) Reference Library 1.6.43SYNOPSIS
#include <png.h>DESCRIPTION
The libpng library supports encoding, decoding, and various manipulations of the Portable Network Graphics (PNG) format image files. It uses the zlib(3) compression library. Following is a copy of the libpng-manual.txt file that accompanies libpng.LIBPNG.TXT
libpng-manual.txt - A description on how to use and modify libpngCopyright (c) 2018-2024 Cosmin Truta
Copyright (c) 1998-2018 Glenn Randers-Pehrson
This document is released under the libpng license.
For conditions of distribution and use, see the disclaimer
and license in png.h
Based on:
libpng version 1.6.36, December 2018, through 1.6.43 - February 2024
Updated and distributed by Cosmin Truta
Copyright (c) 2018-2024 Cosmin Truta
libpng versions 0.97, January 1998, through 1.6.35 - July 2018
Updated and distributed by Glenn Randers-Pehrson
Copyright (c) 1998-2018 Glenn Randers-Pehrson
libpng 1.0 beta 6 - version 0.96 - May 28, 1997
Updated and distributed by Andreas Dilger
Copyright (c) 1996, 1997 Andreas Dilger
libpng 1.0 beta 2 - version 0.88 - January 26, 1996
For conditions of distribution and use, see copyright
notice in png.h. Copyright (c) 1995, 1996 Guy Eric
Schalnat, Group 42, Inc.
Updated/rewritten per request in the libpng FAQ
Copyright (c) 1995, 1996 Frank J. T. Wojcik
December 18, 1995 & January 20, 1996
TABLE OF CONTENTS
I. Introduction
II. Structures
III. Reading
IV. Writing
V. Simplified API
VI. Modifying/Customizing libpng
VII. MNG support
VIII. Changes to Libpng from version 0.88
IX. Changes to Libpng from version 1.0.x to 1.2.x
X. Changes to Libpng from version 1.0.x/1.2.x to 1.4.x
XI. Changes to Libpng from version 1.4.x to 1.5.x
XII. Changes to Libpng from version 1.5.x to 1.6.x
XIII. Detecting libpng
XIV. Source code repository
XV. Coding style
I. Introduction
This file describes how to use and modify the PNG reference library (known as libpng) for your own use. In addition to this file, example.c is a good starting point for using the library, as it is heavily commented and should include everything most people will need. We assume that libpng is already installed; see the INSTALL file for instructions on how to configure and install libpng.II. Structures
There are two main structures that are important to libpng, png_struct and png_info. Both are internal structures that are no longer exposed in the libpng interface (as of libpng 1.5.0).Types
The png.h header file defines a number of integral types used by the APIs. Most of these are fairly obvious; for example types corresponding to integers of particular sizes and types for passing color values.Configuration
The main header file function declarations are frequently protected by C preprocessing directives of the form:#ifdef PNG_feature_SUPPORTED
declare-function
#endif
...
#ifdef PNG_feature_SUPPORTED
use-function
#endif
III. Reading
We'll now walk you through the possible functions to call when reading in a PNG file sequentially, briefly explaining the syntax and purpose of each one. See example.c and png.h for more detail. While progressive reading is covered in the next section, you will still need some of the functions discussed in this section to read a PNG file.Setup
You will want to do the I/O initialization(*) before you get into libpng, so if it doesn't work, you don't have much to undo. Of course, you will also want to insure that you are, in fact, dealing with a PNG file. Libpng provides a simple check to see if a file is a PNG file. To use it, pass in the first 1 to 8 bytes of the file to the function png_sig_cmp(), and it will return 0 (false) if the bytes match the corresponding bytes of the PNG signature, or nonzero (true) otherwise. Of course, the more bytes you pass in, the greater the accuracy of the prediction.FILE *fp = fopen(file_name, "rb");
if (!fp)
{
return ERROR;
}
if (fread(header, 1, number, fp) != number)
{
return ERROR;
}
is_png = (png_sig_cmp(header, 0, number) == 0);
if (!is_png)
{
return NOT_PNG;
}
png_structp png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
user_error_fn, user_warning_fn);
if (!png_ptr)
return ERROR;
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
return ERROR;
}
png_structp png_ptr = png_create_read_struct_2
(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);
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return ERROR;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, number);
png_set_compression_buffer_size(png_ptr, buffer_size);
png_set_crc_action(png_ptr, crit_action, ancil_action);
PNG_CRC_DEFAULT 0 error/quit
PNG_CRC_ERROR_QUIT 1 error/quit
PNG_CRC_WARN_USE 3 warn/use data
PNG_CRC_QUIET_USE 4 quiet/use data
PNG_CRC_NO_CHANGE 5 use the current value
PNG_CRC_DEFAULT 0 error/quit
PNG_CRC_ERROR_QUIT 1 error/quit
PNG_CRC_WARN_DISCARD 2 warn/discard data
PNG_CRC_WARN_USE 3 warn/use data
PNG_CRC_QUIET_USE 4 quiet/use data
PNG_CRC_NO_CHANGE 5 use the current value
Setting up callback code
You can set up a callback function to handle any unknown chunks in the input stream. You must supply the functionread_chunk_callback(png_structp png_ptr,
png_unknown_chunkp chunk)
{
/* The unknown chunk structure contains your
chunk data, along with similar data for any other
unknown chunks: */
png_byte name[5];
png_byte *data;
size_t size;
/* Note that libpng has already taken care of
the CRC handling */
/* put your code here. Search for your chunk in the
unknown chunk structure, process it, and return one
of the following: */
return -n; /* chunk had an error */
return 0; /* did not recognize */
return n; /* success */
}
png_set_read_user_chunk_fn(png_ptr, user_chunk_ptr,
read_chunk_callback);
png_get_user_chunk_ptr(png_ptr);
void read_row_callback(png_structp png_ptr,
png_uint_32 row, int pass)
{
/* put your code here */
}
png_set_read_status_fn(png_ptr, read_row_callback);
Unknown-chunk handling
Now you get to set the way the library processes unknown chunks in the input PNG stream. Both known and unknown chunks will be read. Normal behavior is that known chunks will be parsed into information in various info_ptr members while unknown chunks will be discarded. This behavior can be wasteful if your application will never use some known chunk types. To change this, you can call:png_set_keep_unknown_chunks(png_ptr, keep,
chunk_list, num_chunks);
keep - 0: default unknown chunk handling
1: ignore; do not keep
2: keep only if safe-to-copy
3: keep even if unsafe-to-copy
You can use these definitions:
PNG_HANDLE_CHUNK_AS_DEFAULT 0
PNG_HANDLE_CHUNK_NEVER 1
PNG_HANDLE_CHUNK_IF_SAFE 2
PNG_HANDLE_CHUNK_ALWAYS 3
chunk_list - list of chunks affected (a byte string,
five bytes per chunk, NULL or ' ' if
num_chunks is positive; ignored if
numchunks <= 0).
num_chunks - number of chunks affected; if 0, all
unknown chunks are affected. If positive,
only the chunks in the list are affected,
and if negative all unknown chunks and
all known chunks except for the IHDR,
PLTE, tRNS, IDAT, and IEND chunks are
affected.
png_byte vpAg[5]={118, 112, 65, 103, (png_byte) ' '};
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
png_byte unused_chunks[]=
{
104, 73, 83, 84, (png_byte) ' ', /* hIST */
105, 84, 88, 116, (png_byte) ' ', /* iTXt */
112, 67, 65, 76, (png_byte) ' ', /* pCAL */
115, 67, 65, 76, (png_byte) ' ', /* sCAL */
115, 80, 76, 84, (png_byte) ' ', /* sPLT */
116, 73, 77, 69, (png_byte) ' ', /* tIME */
};
#endif
...
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
/* ignore all unknown chunks
* (use global setting "2" for libpng16 and earlier):
*/
png_set_keep_unknown_chunks(read_ptr, 2, NULL, 0);
/* except for vpAg: */
png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1);
/* also ignore unused known chunks: */
png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks,
(int)(sizeof unused_chunks)/5);
#endif
User limits
The PNG specification allows the width and height of an image to be as large as 2^(31-1 (0x7fffffff), or about 2.147 billion rows and columns. For safety, libpng imposes a default limit of 1 million rows and columns. Larger images will be rejected immediately with a png_error() call. If you wish to change these limits, you can usepng_set_user_limits(png_ptr, width_max, height_max);
width_max = png_get_user_width_max(png_ptr);
height_max = png_get_user_height_max(png_ptr);
png_set_chunk_cache_max(png_ptr, user_chunk_cache_max);
chunk_cache_max = png_get_chunk_cache_max(png_ptr);
png_set_chunk_malloc_max(png_ptr, user_chunk_malloc_max);
chunk_malloc_max = png_get_chunk_malloc_max(png_ptr);
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.png_set_gamma(png_ptr, screen_gamma, output_gamma);
png_set_gamma_fixed(png_ptr, PNG_FP_1*screen_gamma,
PNG_FP_1*output_gamma);
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.
#if PNG_LIBPNG_VER >= 10504
png_set_alpha_mode(png_ptr, mode, screen_gamma);
#else
png_set_gamma(png_ptr, screen_gamma, 1.0/screen_gamma);
#endif
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.
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.
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.
PNG_ALPHA_BROKEN: This is PNG_ALPHA_STANDARD; however, all component values, including the alpha channel are gamma encoded. This is broken because, in practice, no implementation that uses this choice correctly undoes the encoding before handling alpha composition. Use this choice only if other serious errors in the software or hardware you use mandate it. 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.
png_set_alpha_mode(png_ptr, PNG_ALPHA_PNG,
screen_gamma);
png_set_alpha_mode(png_ptr, PNG_ALPHA_STANDARD,
screen_gamma);
png_set_expand_16(png_ptr);
png_set_alpha_mode(png_ptr, PNG_ALPHA_OPTIMIZED,
screen_gamma);
png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB);
PNG_ALPHA_PNG 0 /* according to the PNG standard */
PNG_ALPHA_STANDARD 1 /* according to Porter/Duff */
PNG_ALPHA_ASSOCIATED 1 /* as above; this is the normal practice */
PNG_ALPHA_PREMULTIPLIED 1 /* as above */
PNG_ALPHA_OPTIMIZED 2 /* 'PNG' for opaque pixels, else 'STANDARD' */
PNG_ALPHA_BROKEN 3 /* the alpha channel is gamma encoded */
png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC);
png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_GAMMA_LINEAR);
png_set_expand_16(pp);
png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_DEFAULT_sRGB);
png_set_alpha_mode(pp, PNG_ALPHA_OPTIMIZED, PNG_DEFAULT_sRGB);
Other cases
png_set_alpha_mode(pp, PNG_ALPHA_BROKEN, PNG_DEFAULT_sRGB);
png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB);
png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC);
png_set_background(png_ptr, &background_color,
PNG_BACKGROUND_GAMMA_SCREEN, 0, 1);
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.
PNG_TRANSFORM_EXPAND_16
png_set_expand_16(png_ptr);
The high-level read interface
At this point there are two ways to proceed; through the high-level read interface, or through a sequence of low-level read operations. You can use the high-level interface if (a) you are willing to read 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_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
PNG_TRANSFORM_PACKSWAP Change order of packed
pixels to LSB first
PNG_TRANSFORM_EXPAND Perform set_expand()
PNG_TRANSFORM_INVERT_MONO Invert monochrome images
PNG_TRANSFORM_SHIFT Normalize pixels to the
sBIT depth
PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA
to BGRA
PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA
to AG
PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity
to transparency
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
png_read_png(png_ptr, info_ptr, png_transforms, NULL)
row_pointers = png_get_rows(png_ptr, info_ptr);
png_bytep row_pointers[height];
if (height > PNG_UINT_32_MAX / (sizeof (png_bytep)))
png_error(png_ptr,
"Image is too tall to process in memory");
if (width > PNG_UINT_32_MAX / pixel_size)
png_error(png_ptr,
"Image is too wide to process in memory");
row_pointers = png_malloc(png_ptr,
height*(sizeof (png_bytep)));
for (int i = 0; i < height, i++)
row_pointers[i] = NULL; /* security precaution */
for (int i = 0; i < height, i++)
row_pointers[i] = png_malloc(png_ptr,
width*pixel_size);
png_set_rows(png_ptr, info_ptr, &row_pointers);
/* Guard against integer overflow */
if (height > PNG_SIZE_MAX/(width*pixel_size))
png_error(png_ptr, "image_data buffer would be too large");
png_bytep buffer = png_malloc(png_ptr,
height*width*pixel_size);
for (int i = 0; i < height, i++)
row_pointers[i] = buffer + i*width*pixel_size;
png_set_rows(png_ptr, info_ptr, &row_pointers);
The low-level read interface
If you are going the low-level route, you are now ready to read all the file information up to the actual image data. You do this with a call to png_read_info().png_read_info(png_ptr, info_ptr);
Querying the info structure
Functions are used to get the information from the info_ptr once it has been read. Note that these fields may not be completely filled in until png_read_end() has read the chunk data following the image.png_get_IHDR(png_ptr, info_ptr, &width, &height,
&bit_depth, &color_type, &interlace_type,
&compression_type, &filter_method);
width - holds the width of the image
in pixels (up to 2^31).
height - holds the height of the image
in pixels (up to 2^31).
bit_depth - holds the bit depth of one of the
image channels. (valid values are
1, 2, 4, 8, 16 and depend also on
the color_type. See also
significant bits (sBIT) below).
color_type - describes which color/alpha channels
are present.
PNG_COLOR_TYPE_GRAY
(bit depths 1, 2, 4, 8, 16)
PNG_COLOR_TYPE_GRAY_ALPHA
(bit depths 8, 16)
PNG_COLOR_TYPE_PALETTE
(bit depths 1, 2, 4, 8)
PNG_COLOR_TYPE_RGB
(bit_depths 8, 16)
PNG_COLOR_TYPE_RGB_ALPHA
(bit_depths 8, 16)
PNG_COLOR_MASK_PALETTE
PNG_COLOR_MASK_COLOR
PNG_COLOR_MASK_ALPHA
interlace_type - (PNG_INTERLACE_NONE or
PNG_INTERLACE_ADAM7)
compression_type - (must be PNG_COMPRESSION_TYPE_BASE
for PNG 1.0)
filter_method - (must be PNG_FILTER_TYPE_BASE
for PNG 1.0, and can also be
PNG_INTRAPIXEL_DIFFERENCING if
the PNG datastream is embedded in
a MNG-1.0 datastream)
Any of width, height, color_type, bit_depth,
interlace_type, compression_type, or filter_method can
be NULL if you are not interested in their values.
Note that png_get_IHDR() returns 32-bit data into
the application's width and height variables.
This is an unsafe situation if these are not png_uint_32
variables. In such situations, the
png_get_image_width() and png_get_image_height()
functions described below are safer.
width = png_get_image_width(png_ptr,
info_ptr);
height = png_get_image_height(png_ptr,
info_ptr);
bit_depth = png_get_bit_depth(png_ptr,
info_ptr);
color_type = png_get_color_type(png_ptr,
info_ptr);
interlace_type = png_get_interlace_type(png_ptr,
info_ptr);
compression_type = png_get_compression_type(png_ptr,
info_ptr);
filter_method = png_get_filter_type(png_ptr,
info_ptr);
channels = png_get_channels(png_ptr, info_ptr);
channels - number of channels of info for the
color type (valid values are 1 (GRAY,
PALETTE), 2 (GRAY_ALPHA), 3 (RGB),
4 (RGB_ALPHA or RGB + filler byte))
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
rowbytes - number of bytes needed to hold a row
This value, the bit_depth, color_type,
and the number of channels can change
if you use transforms such as
png_set_expand(). See
png_read_update_info(), below.
signature = png_get_signature(png_ptr, info_ptr);
signature - holds the signature read from the
file (if any). The data is kept in
the same offset it would be if the
whole signature were read (i.e. if an
application had already read in 4
bytes of signature before starting
libpng, the remaining 4 bytes would
be in signature[4] through signature[7]
(see png_set_sig_bytes())).
png_get_PLTE(png_ptr, info_ptr, &palette,
&num_palette);
palette - the palette for the file
(array of png_color)
num_palette - number of entries in the palette
png_get_gAMA(png_ptr, info_ptr, &file_gamma);
png_get_gAMA_fixed(png_ptr, info_ptr, &int_file_gamma);
file_gamma - the gamma at which the file is
written (PNG_INFO_gAMA)
int_file_gamma - 100,000 times the gamma at which the
file is written
png_get_cHRM(png_ptr, info_ptr, &white_x, &white_y, &red_x,
&red_y, &green_x, &green_y, &blue_x, &blue_y)
png_get_cHRM_XYZ(png_ptr, info_ptr, &red_X, &red_Y, &red_Z,
&green_X, &green_Y, &green_Z, &blue_X, &blue_Y,
&blue_Z)
png_get_cHRM_fixed(png_ptr, info_ptr, &int_white_x,
&int_white_y, &int_red_x, &int_red_y,
&int_green_x, &int_green_y, &int_blue_x,
&int_blue_y)
png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, &int_red_X, &int_red_Y,
&int_red_Z, &int_green_X, &int_green_Y,
&int_green_Z, &int_blue_X, &int_blue_Y,
&int_blue_Z)
{white,red,green,blue}_{x,y}
A color space encoding specified using the
chromaticities of the end points and the
white point. (PNG_INFO_cHRM)
{red,green,blue}_{X,Y,Z}
A color space encoding specified using the
encoding end points - the CIE tristimulus
specification of the intended color of the red,
green and blue channels in the PNG RGB data.
The white point is simply the sum of the three
end points. (PNG_INFO_cHRM)
png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
srgb_intent - the rendering intent (PNG_INFO_sRGB)
The presence of the sRGB chunk
means that the pixel data is in the
sRGB color space. This chunk also
implies specific values of gAMA and
cHRM.
png_get_iCCP(png_ptr, info_ptr, &name,
&compression_type, &profile, &proflen);
name - The profile name.
compression_type - The compression type; always
PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
You may give NULL to this argument to
ignore it.
profile - International Color Consortium color
profile data. May contain NULs.
proflen - length of profile data in bytes.
png_get_sBIT(png_ptr, info_ptr, &sig_bit);
sig_bit - the number of significant bits for
(PNG_INFO_sBIT) each of the gray,
red, green, and blue channels,
whichever are appropriate for the
given color type (png_color_16)
png_get_tRNS(png_ptr, info_ptr, &trans_alpha,
&num_trans, &trans_color);
trans_alpha - array of alpha (transparency)
entries for palette (PNG_INFO_tRNS)
num_trans - number of transparent entries
(PNG_INFO_tRNS)
trans_color - graylevel or color sample values of
the single transparent color for
non-paletted images (PNG_INFO_tRNS)
png_get_eXIf_1(png_ptr, info_ptr, &num_exif, &exif);
exif - Exif profile (array of png_byte)
(PNG_INFO_eXIf)
png_get_hIST(png_ptr, info_ptr, &hist);
hist - histogram of palette (array of
png_uint_16) (PNG_INFO_hIST)
png_get_tIME(png_ptr, info_ptr, &mod_time);
mod_time - time image was last modified
(PNG_INFO_tIME)
png_get_bKGD(png_ptr, info_ptr, &background);
background - background color (of type
png_color_16p) (PNG_INFO_bKGD)
valid 16-bit red, green and blue
values, regardless of color_type
num_comments = png_get_text(png_ptr, info_ptr,
&text_ptr, &num_text);
num_comments - number of comments
text_ptr - array of png_text holding image
comments
text_ptr[i].compression - type of compression used
on "text" PNG_TEXT_COMPRESSION_NONE
PNG_TEXT_COMPRESSION_zTXt
PNG_ITXT_COMPRESSION_NONE
PNG_ITXT_COMPRESSION_zTXt
text_ptr[i].key - keyword for comment. Must contain
1-79 characters.
text_ptr[i].text - text comments for current
keyword. Can be empty.
text_ptr[i].text_length - length of text string,
after decompression, 0 for iTXt
text_ptr[i].itxt_length - length of itxt string,
after decompression, 0 for tEXt/zTXt
text_ptr[i].lang - language of comment (empty
string for unknown).
text_ptr[i].lang_key - keyword in UTF-8
(empty string for unknown).
Note that the itxt_length, lang, and lang_key
members of the text_ptr structure only exist when the
library is built with iTXt chunk support. Prior to
libpng-1.4.0 the library was built by default without
iTXt support. Also note that when iTXt is supported,
they contain NULL pointers when the "compression"
field contains PNG_TEXT_COMPRESSION_NONE or
PNG_TEXT_COMPRESSION_zTXt.
num_text - number of comments (same as
num_comments; you can put NULL here
to avoid the duplication)
Note while png_set_text() will accept text, language,
and translated keywords that can be NULL pointers, the
structure returned by png_get_text will always contain
regular zero-terminated C strings. They might be
empty strings but they will never be NULL pointers.
num_spalettes = png_get_sPLT(png_ptr, info_ptr,
&palette_ptr);
num_spalettes - number of sPLT chunks read.
palette_ptr - array of palette structures holding
contents of one or more sPLT chunks
read.
png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y,
&unit_type);
offset_x - positive offset from the left edge
of the screen (can be negative)
offset_y - positive offset from the top edge
of the screen (can be negative)
unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y,
&unit_type);
res_x - pixels/unit physical resolution in
x direction
res_y - pixels/unit physical resolution in
x direction
unit_type - PNG_RESOLUTION_UNKNOWN,
PNG_RESOLUTION_METER
png_get_sCAL(png_ptr, info_ptr, &unit, &width,
&height)
unit - physical scale units (an integer)
width - width of a pixel in physical scale units
height - height of a pixel in physical scale units
(width and height are doubles)
png_get_sCAL_s(png_ptr, info_ptr, &unit, &width,
&height)
unit - physical scale units (an integer)
width - width of a pixel in physical scale units
(expressed as a string)
height - height of a pixel in physical scale units
(width and height are strings like "2.54")
num_unknown_chunks = png_get_unknown_chunks(png_ptr,
info_ptr, &unknowns)
unknowns - array of png_unknown_chunk
structures holding unknown chunks
unknowns[i].name - name of unknown chunk
unknowns[i].data - data of unknown chunk
unknowns[i].size - size of unknown chunk's data
unknowns[i].location - position of chunk in file
The value of "i" corresponds to the order in which the
chunks were read from the PNG file or inserted with the
png_set_unknown_chunks() function.
The value of "location" is a bitwise "or" of
PNG_HAVE_IHDR (0x01)
PNG_HAVE_PLTE (0x02)
PNG_AFTER_IDAT (0x08)
res_x = png_get_x_pixels_per_meter(png_ptr,
info_ptr)
res_y = png_get_y_pixels_per_meter(png_ptr,
info_ptr)
res_x_and_y = png_get_pixels_per_meter(png_ptr,
info_ptr)
res_x = png_get_x_pixels_per_inch(png_ptr,
info_ptr)
res_y = png_get_y_pixels_per_inch(png_ptr,
info_ptr)
res_x_and_y = png_get_pixels_per_inch(png_ptr,
info_ptr)
aspect_ratio = png_get_pixel_aspect_ratio(png_ptr,
info_ptr)
Each of these returns 0 [signifying "unknown"] if
the data is not present or if res_x is 0;
res_x_and_y is 0 if res_x != res_y
Note that because of the way the resolutions are
stored internally, the inch conversions won't
come out to exactly even number. For example,
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.
x_offset = png_get_x_offset_microns(png_ptr, info_ptr);
y_offset = png_get_y_offset_microns(png_ptr, info_ptr);
x_offset = png_get_x_offset_inches(png_ptr, info_ptr);
y_offset = png_get_y_offset_inches(png_ptr, info_ptr);
Each of these returns 0 [signifying "unknown" if both
x and y are 0] if the data is not present or if the
chunk is present but the unit is the pixel. The
remark about inexact inch conversions applies here
as well, because a value in inches can't always be
converted to microns and back without some loss
of precision.
Input transformations
After you've read the header information, you can set up the library 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.if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if (bit_depth < 16)
png_set_expand_16(png_ptr);
if (bit_depth == 16)
{ #if PNG_LIBPNG_VER >= 10504
png_set_scale_16(png_ptr); #else
png_set_strip_16(png_ptr); #endif
}
if (color_type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(png_ptr);
FROM 01 31 0 0T 0O 2 2T 2O 3 3T 3O 4A 4O 6A 6O
TO
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 +
"+" 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() (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().
"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().
png_set_invert_alpha(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
png_color_8p sig_bit;
if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
png_set_shift(png_ptr, sig_bit);
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_bgr(png_ptr);
if (color_type == PNG_COLOR_TYPE_RGB)
png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE);
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY)
png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_swap_alpha(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_rgb_to_gray(png_ptr, error_action,
(double)red_weight, (double)green_weight);
error_action = 1: silently do the conversion
error_action = 2: issue a warning if the original
image has any pixel where
red != green or red != blue
error_action = 3: issue an error and abort the
conversion if the original
image has any pixel where
red != green or red != blue
red_weight: weight of red component
green_weight: weight of green component
If either weight is negative, default
weights are used.
png_set_rgb_to_gray(png_ptr, error_action,
(png_fixed_point)red_weight,
(png_fixed_point)green_weight);
Y = 0.2126 * R + 0.7152 * G + 0.0722 * B
Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
Y = (6968 * R + 23434 * G + 2366 * B)/32768
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/*needs to be expanded*/, 1);
else
png_set_background(png_ptr, &my_background,
PNG_BACKGROUND_GAMMA_SCREEN, 0/*do not expand*/, 1);
if (png_get_gAMA(png_ptr, info_ptr, &file_gamma))
png_set_gamma(png_ptr, screen_gamma, file_gamma);
else
png_set_gamma(png_ptr, screen_gamma, 0.45455);
if (color_type & PNG_COLOR_MASK_COLOR)
{
if (png_get_valid(png_ptr, info_ptr,
PNG_INFO_PLTE))
{
png_uint_16p histogram = NULL;
png_get_hIST(png_ptr, info_ptr,
&histogram);
png_set_quantize(png_ptr, palette, num_palette,
max_screen_colors, histogram, 1);
}
else
{
png_color std_color_cube[MAX_SCREEN_COLORS] =
{ ... colors ... };
png_set_quantize(png_ptr, std_color_cube,
MAX_SCREEN_COLORS, MAX_SCREEN_COLORS,
NULL,0);
}
}
if (bit_depth == 1 && color_type == PNG_COLOR_TYPE_GRAY)
png_set_invert_mono(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_invert_mono(png_ptr);
if (bit_depth == 16)
png_set_swap(png_ptr);
if (bit_depth < 8)
png_set_packswap(png_ptr);
png_set_read_user_transform_fn(png_ptr,
read_transform_fn);
void read_transform_fn(png_structp png_ptr, png_row_infop
row_info, png_bytep data)
png_get_current_pass_number(png_structp png_ptr);
png_get_current_row_number(png_structp png_ptr);
png_set_user_transform_info(png_ptr, user_ptr,
user_depth, user_channels);
voidp read_user_transform_ptr =
png_get_user_transform_ptr(png_ptr);
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
/* Guard against integer overflow */
if (number_of_rows > PNG_SIZE_MAX/(width*pixel_size))
png_error(png_ptr, "image_data buffer would be too large");
Reading image data
After you've allocated memory, you can read the image data. The simplest way to do this is in one function call. If you are allocating enough memory to hold the whole image, you can just call png_read_image() and libpng will read in all the image data and put it in the memory area supplied. You will need to pass in an array of pointers to each row.png_read_image(png_ptr, row_pointers);
png_bytep row_pointers[height];
png_read_rows(png_ptr, row_pointers, NULL,
number_of_rows);
png_bytep row_pointer = row;
png_read_row(png_ptr, row_pointer, NULL);
if (interlace_type == PNG_INTERLACE_ADAM7)
number_of_passes
= png_set_interlace_handling(png_ptr);
png_read_rows(png_ptr, row_pointers, NULL,
number_of_rows);
or
png_read_row(png_ptr, row_pointers, NULL);
png_read_rows(png_ptr, NULL, row_pointers,
number_of_rows);
or
png_read_row(png_ptr, NULL, row_pointers);
png_uint_32 width = PNG_PASS_COLS(image_width, pass_number);
png_uint_32 height = PNG_PASS_ROWS(image_height, pass_number);
png_uint_32 x = PNG_PASS_START_COL(pass);
png_uint_32 y = PNG_PASS_START_ROW(pass);
png_uint_32 xStep = 1U << PNG_PASS_COL_SHIFT(pass);
png_uint_32 yStep = 1U << PNG_PASS_ROW_SHIFT(pass);
png_uint_32 input_y = 0;
png_uint_32 output_y = PNG_PASS_START_ROW(pass);
while (output_y < output_image_height)
{
png_uint_32 input_x = 0;
png_uint_32 output_x = PNG_PASS_START_COL(pass);
while (output_x < output_image_width)
{
image[output_y][output_x] =
subimage[pass][input_y][input_x++];
output_x += xStep;
}
++input_y;
output_y += yStep;
}
png_uint_32 output_x = PNG_COL_FROM_PASS_COL(input_x, pass);
png_uint_32 output_y = PNG_ROW_FROM_PASS_ROW(input_y, pass);
int col_in_pass = PNG_COL_IN_INTERLACE_PASS(output_x, pass);
int row_in_pass = PNG_ROW_IN_INTERLACE_PASS(output_y, pass);
Finishing a sequential read
After you are finished reading the image through the low-level interface, you can finish reading the file.png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return ERROR;
}
png_read_end(png_ptr, end_info);
png_read_end(png_ptr, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
png_free_data(png_ptr, info_ptr, mask, seq)
mask - identifies data to be freed, a mask
containing the bitwise OR of one or
more of
PNG_FREE_PLTE, PNG_FREE_TRNS,
PNG_FREE_HIST, PNG_FREE_ICCP,
PNG_FREE_PCAL, PNG_FREE_ROWS,
PNG_FREE_SCAL, PNG_FREE_SPLT,
PNG_FREE_TEXT, PNG_FREE_UNKN,
or simply PNG_FREE_ALL
seq - sequence number of item to be freed
(-1 for all items)
png_data_freer(png_ptr, info_ptr, freer, mask)
freer - one of
PNG_DESTROY_WILL_FREE_DATA
PNG_SET_WILL_FREE_DATA
PNG_USER_WILL_FREE_DATA
mask - which data elements are affected
same choices as in png_free_data()
png_set_invalid(png_ptr, info_ptr, mask);
mask - identifies the chunks to be made invalid,
containing the bitwise OR of one or
more of
PNG_INFO_gAMA, PNG_INFO_sBIT,
PNG_INFO_cHRM, PNG_INFO_PLTE,
PNG_INFO_tRNS, PNG_INFO_bKGD,
PNG_INFO_eXIf,
PNG_INFO_hIST, PNG_INFO_pHYs,
PNG_INFO_oFFs, PNG_INFO_tIME,
PNG_INFO_pCAL, PNG_INFO_sRGB,
PNG_INFO_iCCP, PNG_INFO_sPLT,
PNG_INFO_sCAL, PNG_INFO_IDAT
Reading PNG files progressively
The progressive reader is slightly different from the non-progressive reader. Instead of calling png_read_info(), png_read_rows(), and png_read_end(), you make one call to png_process_data(), which calls callbacks when it has the info, a row, or the end of the image. You set up these callbacks with png_set_progressive_read_fn(). You don't have to worry about the input/output functions of libpng, as you are giving the library the data directly in png_process_data(). I will assume that you have read the section on reading PNG files above, so I will only highlight the differences (although I will show all of the code)./* An example code fragment of how you would
initialize the progressive reader in your
application. */
int
initialize_png_reader()
{
png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
user_error_fn, user_warning_fn);
if (!png_ptr)
return ERROR;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
return ERROR;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return ERROR;
}
/* This one's new. You can provide functions
to be called when the header info is valid,
when each row is completed, and when the image
is finished. If you aren't using all functions,
you can specify NULL parameters. Even when all
three functions are NULL, you need to call
png_set_progressive_read_fn(). You can use
any struct as the user_ptr (cast to a void pointer
for the function call), and retrieve the pointer
from inside the callbacks using the function
png_get_progressive_ptr(png_ptr);
which will return a void pointer, which you have
to cast appropriately.
*/
png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
info_callback, row_callback, end_callback);
return 0;
}
/* A code fragment that you call as you receive blocks
of data */
int
process_data(png_bytep buffer, png_uint_32 length)
{
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return ERROR;
}
/* This one's new also. Simply give it a chunk
of data from the file stream (in order, of
course). On machines with segmented memory
models machines, don't give it any more than
64K. The library seems to run fine with sizes
of 4K. Although you can give it much less if
necessary (I assume you can give it chunks of
1 byte, I haven't tried less than 256 bytes
yet). When this function returns, you may
want to display any rows that were generated
in the row callback if you don't already do
so there.
*/
png_process_data(png_ptr, info_ptr, buffer, length);
/* At this point you can call png_process_data_skip if
you want to handle data the library will skip yourself;
it simply returns the number of bytes to skip (and stops
libpng skipping that number of bytes on the next
png_process_data call).
return 0;
}
/* This function is called (as set by
png_set_progressive_read_fn() above) when enough data
has been supplied so all of the header has been
read.
*/
void
info_callback(png_structp png_ptr, png_infop info)
{
/* Do any setup here, including setting any of
the transformations mentioned in the Reading
PNG files section. For now, you _must_ call
either png_start_read_image() or
png_read_update_info() after all the
transformations are set (even if you don't set
any). You may start getting rows before
png_process_data() returns, so this is your
last chance to prepare for that.
This is where you turn on interlace handling,
assuming you don't want to do it yourself.
If you need to you can stop the processing of
your original input data at this point by calling
png_process_data_pause. This returns the number
of unprocessed bytes from the last png_process_data
call - it is up to you to ensure that the next call
sees these bytes again. If you don't want to bother
with this you can get libpng to cache the unread
bytes by setting the 'save' parameter (see png.h) but
then libpng will have to copy the data internally.
*/
}
/* This function is called when each row of image
data is complete */
void
row_callback(png_structp png_ptr, png_bytep new_row,
png_uint_32 row_num, int pass)
{
/* If the image is interlaced, and you turned
on the interlace handler, this function will
be called for every row in every pass. Some
of these rows will not be changed from the
previous pass. When the row is not changed,
the new_row variable will be NULL. The rows
and passes are called in order, so you don't
really need the row_num and pass, but I'm
supplying them because it may make your life
easier.
If you did not turn on interlace handling then
the callback is called for each row of each
sub-image when the image is interlaced. In this
case 'row_num' is the row in the sub-image, not
the row in the output image as it is in all other
cases.
For the non-NULL rows of interlaced images when
you have switched on libpng interlace handling,
you must call png_progressive_combine_row()
passing in the row and the old row. You can
call this function for NULL rows (it will just
return) and for non-interlaced images (it just
does the memcpy for you) if it will make the
code easier. Thus, you can just do this for
all cases if you switch on interlace handling;
*/
png_progressive_combine_row(png_ptr, old_row,
new_row);
/* where old_row is what was displayed
previously for the row. Note that the first
pass (pass == 0, really) will completely cover
the old row, so the rows do not have to be
initialized. After the first pass (and only
for interlaced images), you will have to pass
the current row, and the function will combine
the old row and the new row.
You can also call png_process_data_pause in this
callback - see above.
*/
}
void
end_callback(png_structp png_ptr, png_infop info)
{
/* This function is called after the whole image
has been read, including any chunks after the
image (up to and including the IEND). You
will usually have the same info chunk as you
had in the header, although some data may have
been added to the comments and time fields.
Most people won't do much here, perhaps setting
a flag that marks the image as finished.
*/
}
IV. Writing
Much of this is very similar to reading. However, everything of importance is repeated here, so you won't have to constantly look back up in the reading section to understand writing.Setup
You will want to do the I/O initialization before you get into libpng, so if it doesn't work, you don't have anything to undo. If you are not using the standard I/O functions, you will need to replace them with custom writing functions. See the discussion under Customizing libpng.FILE *fp = fopen(file_name, "wb");
if (!fp)
return ERROR;
png_structp png_ptr = png_create_write_struct
(PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
user_error_fn, user_warning_fn);
if (!png_ptr)
return ERROR;
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, NULL);
return ERROR;
}
png_structp png_ptr = png_create_write_struct_2
(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);
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return ERROR;
}
...
return;
png_set_check_for_invalid_index(png_ptr, 0);
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
Write callbacks
At this point, you can set up a callback function that will be called after each row has been written, which you can use to control a progress meter or the like. It's demonstrated in pngtest.c. You must supply a functionvoid write_row_callback(png_structp png_ptr, png_uint_32 row,
int pass)
{
/* put your code here */
}
png_set_write_status_fn(png_ptr, write_row_callback);
/* turn on or off filtering, and/or choose
specific filters. You can use either a single
PNG_FILTER_VALUE_NAME or the bitwise OR of one
or more PNG_FILTER_NAME masks.
*/
png_set_filter(png_ptr, 0,
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE |
PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB |
PNG_FILTER_UP | PNG_FILTER_VALUE_UP |
PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG |
PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH|
PNG_ALL_FILTERS | PNG_FAST_FILTERS);
#include zlib.h
/* Set the zlib compression level */
png_set_compression_level(png_ptr,
Z_BEST_COMPRESSION);
/* Set other zlib parameters for compressing IDAT */
png_set_compression_mem_level(png_ptr, 8);
png_set_compression_strategy(png_ptr,
Z_DEFAULT_STRATEGY);
png_set_compression_window_bits(png_ptr, 15);
png_set_compression_method(png_ptr, 8);
png_set_compression_buffer_size(png_ptr, 8192)
/* 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
You now need to fill in the png_info structure with all the data you wish to write before the actual image. Note that the only thing you are allowed to write after the image is the text chunks and the time chunk (as of PNG Specification 1.2, anyway). See png_write_end() and the latest PNG specification for more information on that. If you wish to write them before the image, fill them in now, and flag that data as being valid. If you want to wait until after the data, don't fill them until png_write_end(). For all the fields in png_info and their data types, see png.h. For explanations of what the fields contain, see the PNG specification.png_set_IHDR(png_ptr, info_ptr, width, height,
bit_depth, color_type, interlace_type,
compression_type, filter_method)
width - holds the width of the image
in pixels (up to 2^31).
height - holds the height of the image
in pixels (up to 2^31).
bit_depth - holds the bit depth of one of the
image channels.
(valid values are 1, 2, 4, 8, 16
and depend also on the
color_type. See also significant
bits (sBIT) below).
color_type - describes which color/alpha
channels are present.
PNG_COLOR_TYPE_GRAY
(bit depths 1, 2, 4, 8, 16)
PNG_COLOR_TYPE_GRAY_ALPHA
(bit depths 8, 16)
PNG_COLOR_TYPE_PALETTE
(bit depths 1, 2, 4, 8)
PNG_COLOR_TYPE_RGB
(bit_depths 8, 16)
PNG_COLOR_TYPE_RGB_ALPHA
(bit_depths 8, 16)
PNG_COLOR_MASK_PALETTE
PNG_COLOR_MASK_COLOR
PNG_COLOR_MASK_ALPHA
interlace_type - PNG_INTERLACE_NONE or
PNG_INTERLACE_ADAM7
compression_type - (must be
PNG_COMPRESSION_TYPE_DEFAULT)
filter_method - (must be PNG_FILTER_TYPE_DEFAULT
or, if you are writing a PNG to
be embedded in a MNG datastream,
can also be
PNG_INTRAPIXEL_DIFFERENCING)
png_set_PLTE(png_ptr, info_ptr, palette,
num_palette);
palette - the palette for the file
(array of png_color)
num_palette - number of entries in the palette
png_set_gAMA(png_ptr, info_ptr, file_gamma);
png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
file_gamma - the gamma at which the image was
created (PNG_INFO_gAMA)
int_file_gamma - 100,000 times the gamma at which
the image was created
png_set_cHRM(png_ptr, info_ptr, white_x, white_y, red_x, red_y,
green_x, green_y, blue_x, blue_y)
png_set_cHRM_XYZ(png_ptr, info_ptr, red_X, red_Y, red_Z, green_X,
green_Y, green_Z, blue_X, blue_Y, blue_Z)
png_set_cHRM_fixed(png_ptr, info_ptr, int_white_x, int_white_y,
int_red_x, int_red_y, int_green_x, int_green_y,
int_blue_x, int_blue_y)
png_set_cHRM_XYZ_fixed(png_ptr, info_ptr, int_red_X, int_red_Y,
int_red_Z, int_green_X, int_green_Y, int_green_Z,
int_blue_X, int_blue_Y, int_blue_Z)
{white,red,green,blue}_{x,y}
A color space encoding specified using the chromaticities
of the end points and the white point.
{red,green,blue}_{X,Y,Z}
A color space encoding specified using the encoding end
points - the CIE tristimulus specification of the intended
color of the red, green and blue channels in the PNG RGB
data. The white point is simply the sum of the three end
points.
png_set_sRGB(png_ptr, info_ptr, srgb_intent);
srgb_intent - the rendering intent
(PNG_INFO_sRGB) The presence of
the sRGB chunk means that the pixel
data is in the sRGB color space.
This chunk also implies specific
values of gAMA and cHRM. Rendering
intent is the CSS-1 property that
has been defined by the International
Color Consortium
(http://www.color.org).
It can be one of
PNG_sRGB_INTENT_SATURATION,
PNG_sRGB_INTENT_PERCEPTUAL,
PNG_sRGB_INTENT_ABSOLUTE, or
PNG_sRGB_INTENT_RELATIVE.
png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
srgb_intent);
srgb_intent - the rendering intent
(PNG_INFO_sRGB) The presence of the
sRGB chunk means that the pixel
data is in the sRGB color space.
This function also causes gAMA and
cHRM chunks with the specific values
that are consistent with sRGB to be
written.
png_set_iCCP(png_ptr, info_ptr, name, compression_type,
profile, proflen);
name - The profile name.
compression_type - The compression type; always
PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
You may give NULL to this argument to
ignore it.
profile - International Color Consortium color
profile data. May contain NULs.
proflen - length of profile data in bytes.
png_set_sBIT(png_ptr, info_ptr, sig_bit);
sig_bit - the number of significant bits for
(PNG_INFO_sBIT) each of the gray, red,
green, and blue channels, whichever are
appropriate for the given color type
(png_color_16)
png_set_tRNS(png_ptr, info_ptr, trans_alpha,
num_trans, trans_color);
trans_alpha - array of alpha (transparency)
entries for palette (PNG_INFO_tRNS)
num_trans - number of transparent entries
(PNG_INFO_tRNS)
trans_color - graylevel or color sample values
(in order red, green, blue) of the
single transparent color for
non-paletted images (PNG_INFO_tRNS)
png_set_eXIf_1(png_ptr, info_ptr, num_exif, exif);
exif - Exif profile (array of png_byte)
(PNG_INFO_eXIf)
png_set_hIST(png_ptr, info_ptr, hist);
hist - histogram of palette (array of
png_uint_16) (PNG_INFO_hIST)
png_set_tIME(png_ptr, info_ptr, mod_time);
mod_time - time image was last modified
(PNG_INFO_tIME)
png_set_bKGD(png_ptr, info_ptr, background);
background - background color (of type
png_color_16p) (PNG_INFO_bKGD)
png_set_text(png_ptr, info_ptr, text_ptr, num_text);
text_ptr - array of png_text holding image
comments
text_ptr[i].compression - type of compression used
on "text" PNG_TEXT_COMPRESSION_NONE
PNG_TEXT_COMPRESSION_zTXt
PNG_ITXT_COMPRESSION_NONE
PNG_ITXT_COMPRESSION_zTXt
text_ptr[i].key - keyword for comment. Must contain
1-79 characters.
text_ptr[i].text - text comments for current
keyword. Can be NULL or empty.
text_ptr[i].text_length - length of text string,
after decompression, 0 for iTXt
text_ptr[i].itxt_length - length of itxt string,
after decompression, 0 for tEXt/zTXt
text_ptr[i].lang - language of comment (NULL or
empty for unknown).
text_ptr[i].translated_keyword - keyword in UTF-8 (NULL
or empty for unknown).
Note that the itxt_length, lang, and lang_key
members of the text_ptr structure only exist when the
library is built with iTXt chunk support. Prior to
libpng-1.4.0 the library was built by default without
iTXt support. Also note that when iTXt is supported,
they contain NULL pointers when the "compression"
field contains PNG_TEXT_COMPRESSION_NONE or
PNG_TEXT_COMPRESSION_zTXt.
num_text - number of comments
png_set_sPLT(png_ptr, info_ptr, &palette_ptr,
num_spalettes);
palette_ptr - array of png_sPLT_struct structures
to be added to the list of palettes
in the info structure.
num_spalettes - number of palette structures to be
added.
png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y,
unit_type);
offset_x - positive offset from the left
edge of the screen
offset_y - positive offset from the top
edge of the screen
unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
png_set_pHYs(png_ptr, info_ptr, res_x, res_y,
unit_type);
res_x - pixels/unit physical resolution
in x direction
res_y - pixels/unit physical resolution
in y direction
unit_type - PNG_RESOLUTION_UNKNOWN,
PNG_RESOLUTION_METER
png_set_sCAL(png_ptr, info_ptr, unit, width, height)
unit - physical scale units (an integer)
width - width of a pixel in physical scale units
height - height of a pixel in physical scale units
(width and height are doubles)
png_set_sCAL_s(png_ptr, info_ptr, unit, width, height)
unit - physical scale units (an integer)
width - width of a pixel in physical scale units
expressed as a string
height - height of a pixel in physical scale units
(width and height are strings like "2.54")
png_set_unknown_chunks(png_ptr, info_ptr, &unknowns,
num_unknowns)
unknowns - array of png_unknown_chunk
structures holding unknown chunks
unknowns[i].name - name of unknown chunk
unknowns[i].data - data of unknown chunk
unknowns[i].size - size of unknown chunk's data
unknowns[i].location - position to write chunk in file
0: do not write chunk
PNG_HAVE_IHDR: before PLTE
PNG_HAVE_PLTE: before IDAT
PNG_AFTER_IDAT: after IDAT
Title Short (one line) title or
caption for image
Author Name of image's creator
Description Description of image (possibly long)
Copyright Copyright notice
Creation Time Time of original image creation
(usually RFC 1123 format, see below)
Software Software used to create the image
Disclaimer Legal disclaimer
Warning Warning of nature of content
Source Device used to create the image
Comment Miscellaneous comment; conversion
from other image format
Writing unknown chunks
You can use the png_set_unknown_chunks function to queue up private chunks for writing. You give it a chunk name, location, raw data, and a size. You also must use png_set_keep_unknown_chunks() to ensure that libpng will handle them. That's all there is to it. The chunks will be written by the next following png_write_info_before_PLTE, png_write_info, or png_write_end function, depending upon the specified location. Any chunks previously read into the info structure's unknown-chunk list will also be written out in a sequence that satisfies the PNG specification's ordering rules.#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
/* Set unknown chunk data */
png_unknown_chunk unk_chunk[2];
strcpy((char *) unk_chunk[0].name, "prVt";
unk_chunk[0].data = (unsigned char *) "PRIVATE DATA";
unk_chunk[0].size = strlen(unk_chunk[0].data)+1;
unk_chunk[0].location = PNG_HAVE_IHDR;
strcpy((char *) unk_chunk[1].name, "miNE";
unk_chunk[1].data = (unsigned char *) "MY CHUNK DATA";
unk_chunk[1].size = strlen(unk_chunk[0].data)+1;
unk_chunk[1].location = PNG_AFTER_IDAT;
png_set_unknown_chunks(write_ptr, write_info_ptr,
unk_chunk, 2);
/* Needed because miNE is not safe-to-copy */
png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS,
(png_bytep) "miNE", 1);
# if PNG_LIBPNG_VER < 10600
/* Deal with unknown chunk location bug in 1.5.x and earlier */
png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
png_set_unknown_chunk_location(png, info, 1, PNG_AFTER_IDAT);
# endif
# if PNG_LIBPNG_VER < 10500
/* PNG_AFTER_IDAT writes two copies of the chunk prior to libpng-1.5.0,
* one before IDAT and another after IDAT, so don't use it; only use
* PNG_HAVE_IHDR location. This call resets the location previously
* set by assignment and png_set_unknown_chunk_location() for chunk 1.
*/
png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
# endif
#endif
The high-level write interface
At this point there are two ways to proceed; through the high-level write interface, or through a sequence of low-level write operations. You can use the high-level interface if your image data is present in the info structure. All defined output transformations are permitted, enabled by the following masks.PNG_TRANSFORM_IDENTITY No transformation
PNG_TRANSFORM_PACKING Pack 1, 2 and 4-bit samples
PNG_TRANSFORM_PACKSWAP Change order of packed
pixels to LSB first
PNG_TRANSFORM_INVERT_MONO Invert monochrome images
PNG_TRANSFORM_SHIFT Normalize pixels to the
sBIT depth
PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA
to BGRA
PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA
to AG
PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity
to transparency
PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples
PNG_TRANSFORM_STRIP_FILLER Strip out filler
bytes (deprecated).
PNG_TRANSFORM_STRIP_FILLER_BEFORE Strip out leading
filler bytes
PNG_TRANSFORM_STRIP_FILLER_AFTER Strip out trailing
filler bytes
png_write_png(png_ptr, info_ptr, png_transforms, NULL)
The low-level write interface
If you are going the low-level route instead, you are now ready to write all the file information up to the actual image data. You do this with a call to png_write_info().png_write_info(png_ptr, info_ptr);
png_set_invert_alpha(png_ptr);
png_write_info_before_PLTE(png_ptr, info_ptr);
png_set_unknown_chunks(png_ptr, info_ptr, ...);
png_write_info(png_ptr, info_ptr);
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
png_set_packing(png_ptr);
/* Set the true bit depth of the image data */
if (color_type & PNG_COLOR_MASK_COLOR)
{
sig_bit.red = true_bit_depth;
sig_bit.green = true_bit_depth;
sig_bit.blue = true_bit_depth;
}
else
{
sig_bit.gray = true_bit_depth;
}
if (color_type & PNG_COLOR_MASK_ALPHA)
{
sig_bit.alpha = true_bit_depth;
}
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
png_set_shift(png_ptr, &sig_bit);
if (bit_depth > 8)
png_set_swap(png_ptr);
if (bit_depth < 8)
png_set_packswap(png_ptr);
png_set_bgr(png_ptr);
png_set_invert_mono(png_ptr);
png_set_write_user_transform_fn(png_ptr,
write_transform_fn);
void write_transform_fn(png_structp png_ptr, png_row_infop
row_info, png_bytep data)
png_get_current_row_number(png_ptr);
png_get_current_pass_number(png_ptr);
png_set_user_transform_info(png_ptr, user_ptr, 0, 0);
voidp write_user_transform_ptr =
png_get_user_transform_ptr(png_ptr);
png_write_flush(png_ptr);
png_set_flush(png_ptr, nrows);
Writing the image data
That's it for the transformations. Now you can write the image data. The simplest way to do this is in one function call. If you have the whole image in memory, you can just call png_write_image() and libpng will write the image. You will need to pass in an array of pointers to each row. This function automatically handles interlacing, so you don't need to call png_set_interlace_handling() or call this function multiple times, or any of that other stuff necessary with png_write_rows().png_write_image(png_ptr, row_pointers);
png_byte *row_pointers[height];
png_write_rows(png_ptr, row_pointers,
number_of_rows);
png_bytep row_pointer = row;
png_write_row(png_ptr, row_pointer);
number_of_passes = png_set_interlace_handling(png_ptr);
png_write_rows(png_ptr, row_pointers, number_of_rows);
Finishing a sequential write
After you are finished writing the image, you should finish writing the file. If you are interested in writing comments or time, you should pass an appropriately filled png_info pointer. If you are not interested, you can pass NULL.png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
png_free_data(png_ptr, info_ptr, mask, seq)
mask - identifies data to be freed, a mask
containing the bitwise OR of one or
more of
PNG_FREE_PLTE, PNG_FREE_TRNS,
PNG_FREE_HIST, PNG_FREE_ICCP,
PNG_FREE_PCAL, PNG_FREE_ROWS,
PNG_FREE_SCAL, PNG_FREE_SPLT,
PNG_FREE_TEXT, PNG_FREE_UNKN,
or simply PNG_FREE_ALL
seq - sequence number of item to be freed
(-1 for all items)
png_data_freer(png_ptr, info_ptr, freer, mask)
freer - one of
PNG_DESTROY_WILL_FREE_DATA
PNG_SET_WILL_FREE_DATA
PNG_USER_WILL_FREE_DATA
mask - which data elements are affected
same choices as in png_free_data()
png_data_freer(read_ptr, read_info_ptr,
PNG_USER_WILL_FREE_DATA,
PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
png_data_freer(write_ptr, write_info_ptr,
PNG_DESTROY_WILL_FREE_DATA,
PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
V. Simplified API
The simplified API, which became available in libpng-1.6.0, hides the details of both libpng and the PNG file format itself. It allows PNG files to be read into a very limited number of in-memory bitmap formats or to be written from the same formats. If these formats do not accommodate your needs then you can, and should, use the more sophisticated APIs above - these support a wide variety of in-memory formats and a wide variety of sophisticated transformations to those formats as well as a wide variety of APIs to manipulate ancillary information.1) Declare a 'png_image' structure (see below) on the stack, set the
version field to PNG_IMAGE_VERSION and the 'opaque' pointer to NULL
(this is REQUIRED, your program may crash if you don't do it.)
2) Call the appropriate png_image_begin_read... function.
3) Set the png_image 'format' member to the required sample format.
4) Allocate a buffer for the image and, if required, the color-map.
5) Call png_image_finish_read to read the image and, if required, the
color-map into your buffers.
1) Declare a 'png_image' structure on the stack and memset()
it to all zero.
2) Initialize the members of the structure that describe the
image, setting the 'format' member to the format of the
image samples.
3) Call the appropriate png_image_write... function with a
pointer to the image and, if necessary, the color-map to write
the PNG data.
png_controlp opaque Initialize to NULL, free with png_image_free
png_uint_32 version Set to PNG_IMAGE_VERSION
png_uint_32 width Image width in pixels (columns)
png_uint_32 height Image height in pixels (rows)
png_uint_32 format Image format as defined below
png_uint_32 flags A bit mask containing informational flags
png_uint_32 colormap_entries; Number of entries in the color-map
png_uint_32 warning_or_error;
char message[64];
0 - no warning or error
1 - warning
2 - error
3 - error preceded by warning
1: A single gray or luminance channel (G).
2: A gray/luminance channel and an alpha channel (GA).
3: Three red, green, blue color channels (RGB).
4: Three color channels and an alpha channel (RGBA).
a) As a small integer, value 0..255, contained in a single byte. For the alpha channel the original value is simply value/255. For the color or luminance channels the value is encoded according to the sRGB specification and matches the 8-bit format expected by typical display devices.
b) As a value in the range 0..65535, contained in a 2-byte integer, in the native byte order of the platform on which the application is running. All channels can be converted to the original value by dividing by 65535; all channels are linear. Color channels use the RGB encoding (RGB end-points) of the sRGB specification. This encoding is identified by the PNG_FORMAT_FLAG_LINEAR flag below.
PNG_SIMPLIFIED_{READ,WRITE}_{BGR,AFIRST}_SUPPORTED
PNG_FORMAT_FLAG_ALPHA format with an alpha channel
PNG_FORMAT_FLAG_COLOR color format: otherwise grayscale
PNG_FORMAT_FLAG_LINEAR 2-byte channels else 1-byte
PNG_FORMAT_FLAG_COLORMAP image data is color-mapped
PNG_FORMAT_FLAG_BGR BGR colors, else order is RGB
PNG_FORMAT_FLAG_AFIRST alpha channel comes first
PNG_FORMAT_GRAY
PNG_FORMAT_GA
PNG_FORMAT_AG
PNG_FORMAT_RGB
PNG_FORMAT_BGR
PNG_FORMAT_RGBA
PNG_FORMAT_ARGB
PNG_FORMAT_BGRA
PNG_FORMAT_ABGR
PNG_FORMAT_LINEAR_Y
PNG_FORMAT_LINEAR_Y_ALPHA
PNG_FORMAT_LINEAR_RGB
PNG_FORMAT_LINEAR_RGB_ALPHA
PNG_FORMAT_RGB_COLORMAP
PNG_FORMAT_BGR_COLORMAP
PNG_FORMAT_RGBA_COLORMAP
PNG_FORMAT_ARGB_COLORMAP
PNG_FORMAT_BGRA_COLORMAP
PNG_FORMAT_ABGR_COLORMAP
PNG_IMAGE_SAMPLE_CHANNELS(fmt)
Returns the total number of channels in a given format: 1..4
PNG_IMAGE_SAMPLE_COMPONENT_SIZE(fmt)
Returns the size in bytes of a single component of a pixel or color-map
entry (as appropriate) in the image: 1 or 2.
PNG_IMAGE_SAMPLE_SIZE(fmt)
This is the size of the sample data for one sample. If the image is
color-mapped it is the size of one color-map entry (and image pixels are
one byte in size), otherwise it is the size of one image pixel.
PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(fmt)
The maximum size of the color-map required by the format expressed in a
count of components. This can be used to compile-time allocate a
color-map:
png_uint_16 colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(linear_fmt)];
png_byte colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(sRGB_fmt)];
Alternatively use the PNG_IMAGE_COLORMAP_SIZE macro below to use the
information from one of the png_image_begin_read_ APIs and dynamically
allocate the required memory.
PNG_IMAGE_COLORMAP_SIZE(fmt)
The size of the color-map required by the format; this is the size of the
color-map buffer passed to the png_image_{read,write}_colormap APIs. It is
a fixed number determined by the format so can easily be allocated on the
stack if necessary.
PNG_IMAGE_PIXEL_CHANNELS(fmt)
The number of separate channels (components) in a pixel; 1 for a
color-mapped image.
PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt) The size, in bytes, of each component in a pixel; 1 for a color-mapped
image.
PNG_IMAGE_PIXEL_SIZE(fmt)
The size, in bytes, of a complete pixel; 1 for a color-mapped image.
PNG_IMAGE_ROW_STRIDE(image)
Returns the total number of components in a single row of the image; this
is the minimum 'row stride', the minimum count of components between each
row. For a color-mapped image this is the minimum number of bytes in a
row.
If you need the stride measured in bytes, row_stride_bytes is
PNG_IMAGE_ROW_STRIDE(image) * PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)
plus any padding bytes that your application might need, for example
to start the next row on a 4-byte boundary.
PNG_IMAGE_BUFFER_SIZE(image, row_stride)
Return the size, in bytes, of an image buffer given a png_image and a row
stride - the number of components to leave space for in each row.
PNG_IMAGE_SIZE(image)
Return the size, in bytes, of the image in memory given just a png_image;
the row stride is the minimum stride required for the image.
PNG_IMAGE_COLORMAP_SIZE(image)
Return the size, in bytes, of the color-map of this image. If the image
format is not a color-map format this will return a size sufficient for
256 entries in the given format; check PNG_FORMAT_FLAG_COLORMAP if
you don't want to allocate a color-map in this case.
PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB == 0x01
This indicates that the RGB values of the in-memory bitmap do not
correspond to the red, green and blue end-points defined by sRGB.
PNG_IMAGE_FLAG_FAST == 0x02
On write emphasise speed over compression; the resultant PNG file will be
larger but will be produced significantly faster, particular for large
images. Do not use this option for images which will be distributed, only
used it when producing intermediate files that will be read back in
repeatedly. For a typical 24-bit image the option will double the read
speed at the cost of increasing the image size by 25%, however for many
more compressible images the PNG file can be 10 times larger with only a
slight speed gain.
PNG_IMAGE_FLAG_16BIT_sRGB == 0x04
On read if the image is a 16-bit per component image and there is no gAMA
or sRGB chunk assume that the components are sRGB encoded. Notice that
images output by the simplified API always have gamma information; setting
this flag only affects the interpretation of 16-bit images from an
external source. It is recommended that the application expose this flag
to the user; the user can normally easily recognize the difference between
linear and sRGB encoding. This flag has no effect on write - the data
passed to the write APIs must have the correct encoding (as defined
above.)
If the flag is not set (the default) input 16-bit per component data is
assumed to be linear.
NOTE: the flag can only be set after the png_image_begin_read_ call,
because that call initializes the 'flags' field.
The png_image passed to the read APIs must have been initialized by setting
the png_controlp field 'opaque' to NULL (or, better, memset the whole thing.)
int png_image_begin_read_from_file( png_imagep image,
const char *file_name)
The named file is opened for read and the image header
is filled in from the PNG header in the file.
int png_image_begin_read_from_stdio (png_imagep image,
FILE* file)
The PNG header is read from the stdio FILE object.
int png_image_begin_read_from_memory(png_imagep image,
png_const_voidp memory, size_t size)
The PNG header is read from the given memory buffer.
int png_image_finish_read(png_imagep image,
png_colorp background, void *buffer,
png_int_32 row_stride, void *colormap));
Finish reading the image into the supplied buffer and
clean up the png_image structure.
row_stride is the step, in png_byte or png_uint_16 units
as appropriate, between adjacent rows. A positive stride
indicates that the top-most row is first in the buffer -
the normal top-down arrangement. A negative stride
indicates that the bottom-most row is first in the buffer.
background need only be supplied if an alpha channel must
be removed from a png_byte format and the removal is to be
done by compositing on a solid color; otherwise it may be
NULL and any composition will be done directly onto the
buffer. The value is an sRGB color to use for the
background, for grayscale output the green channel is used.
For linear output removing the alpha channel is always done
by compositing on black.
void png_image_free(png_imagep image)
Free any data allocated by libpng in image->opaque,
setting the pointer to NULL. May be called at any time
after the structure is initialized.
version: must be set to PNG_IMAGE_VERSION
opaque: must be initialized to NULL
width: image width in pixels
height: image height in rows
format: the format of the data you wish to write
flags: set to 0 unless one of the defined flags applies; set
PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB for color format images
where the RGB values do not correspond to the colors in sRGB.
colormap_entries: set to the number of entries in the color-map (0 to 256)
int png_image_write_to_file, (png_imagep image,
const char *file, int convert_to_8bit, const void *buffer,
png_int_32 row_stride, const void *colormap));
Write the image to the named file.
int png_image_write_to_memory (png_imagep image, void *memory,
png_alloc_size_t * PNG_RESTRICT memory_bytes,
int convert_to_8_bit, const void *buffer, ptrdiff_t row_stride,
const void *colormap));
Write the image to memory.
int png_image_write_to_stdio(png_imagep image, FILE *file,
int convert_to_8_bit, const void *buffer,
png_int_32 row_stride, const void *colormap)
Write the image to the given (FILE*).
VI. Modifying/Customizing libpng
There are two issues here. The first is changing how libpng does standard things like memory allocation, input/output, and error handling. The second deals with more complicated things like adding new chunks, adding new transformations, and generally changing how libpng works. Both of those are compile-time issues; that is, they are generally determined at the time the code is written, and there is rarely a need to provide the user with a means of changing them.mem_ptr = png_get_mem_ptr(png_ptr);
png_voidp malloc_fn(png_structp png_ptr,
png_alloc_size_t size);
void free_fn(png_structp png_ptr, png_voidp ptr);
png_set_read_fn(png_structp read_ptr,
voidp read_io_ptr, png_rw_ptr read_data_fn)
png_set_write_fn(png_structp write_ptr,
voidp write_io_ptr, png_rw_ptr write_data_fn,
png_flush_ptr output_flush_fn);
voidp read_io_ptr = png_get_io_ptr(read_ptr);
voidp write_io_ptr = png_get_io_ptr(write_ptr);
void user_read_data(png_structp png_ptr,
png_bytep data, size_t length);
void user_write_data(png_structp png_ptr,
png_bytep data, size_t length);
void user_flush_data(png_structp png_ptr);
png_set_error_fn(png_structp png_ptr,
png_voidp error_ptr, png_error_ptr error_fn,
png_error_ptr warning_fn);
void user_error_fn(png_structp png_ptr,
png_const_charp error_msg);
void user_warning_fn(png_structp png_ptr,
png_const_charp warning_msg);
png_voidp error_ptr = png_get_error_ptr(png_ptr);
png_set_benign_errors (png_ptr, int allowed);
allowed: 0: treat png_benign_error() as an error.
1: treat png_benign_error() as a warning.
Custom chunks
If you need to read or write custom chunks, you may need to get deeper into the libpng code. The library now has mechanisms for storing and writing chunks of unknown type; you can even declare callbacks for custom chunks. However, this may not be good enough if the library code itself needs to know about interactions between your chunk and existing `intrinsic' chunks.Configuring for gui/windowing platforms:
You will need to write new error and warning functions that use the GUI interface, as described previously, and set them to be the error and warning functions at the time that png_create_*_struct() is called, in order to have them available during the structure initialization. They can be changed later via png_set_error_fn(). On some compilers, you may also have to change the memory allocators (png_malloc, etc.).Configuring zlib:
There are special functions to configure the compression. Perhaps the most useful one changes the compression level, which currently uses input compression values in the range 0 - 9. The library normally uses the default compression level (Z_DEFAULT_COMPRESSION = 6). Tests have shown that for a large majority of images, compression values in the range 3-6 compress nearly as well as higher levels, and do so much faster. For online applications it may be desirable to have maximum speed (Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also 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);
#include zlib.h
png_set_compression_mem_level(png_ptr, level);
#include zlib.h
png_set_compression_strategy(png_ptr,
strategy);
png_set_compression_window_bits(png_ptr,
window_bits);
png_set_compression_method(png_ptr, method);
png_set_compression_buffer_size(png_ptr, size);
#include zlib.h
#if PNG_LIBPNG_VER >= 10504
png_set_text_compression_level(png_ptr, level);
png_set_text_compression_mem_level(png_ptr, level);
png_set_text_compression_strategy(png_ptr,
strategy);
png_set_text_compression_window_bits(png_ptr,
window_bits);
png_set_text_compression_method(png_ptr, method);
#endif
Controlling row filtering
If you want to control whether libpng uses filtering or not, which filters are used, and how it goes about picking row filters, you can call one of these functions. The selection and configuration of row filters can have a significant impact on the size and encoding speed and a somewhat lesser impact on the decoding speed of an image. Filtering is enabled by default for RGB and grayscale images (with and without alpha), but not for paletted images nor for any images with bit depths less than 8 bits/pixel.filters = PNG_NO_FILTERS;
filters = PNG_ALL_FILTERS;
filters = PNG_FAST_FILTERS;
or
filters = PNG_FILTER_NONE | PNG_FILTER_SUB |
PNG_FILTER_UP | PNG_FILTER_AVG |
PNG_FILTER_PAETH;
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE,
filters);
The second parameter can also be
PNG_INTRAPIXEL_DIFFERENCING if you are
writing a PNG to be embedded in a MNG
datastream. This parameter must be the
same as the value of filter_method used
in png_set_IHDR().
Requesting debug printout
The macro definition PNG_DEBUG can be used to request debugging printout. Set it to an integer value in the range 0 to 3. Higher numbers result in increasing amounts of debugging information. The information is printed to the "stderr" file, unless another file name is specified in the PNG_DEBUG_FILE macro definition.png_debug(level, message)
png_debug1(level, message, p1)
png_debug2(level, message, p1, p2)
png_debug1(2, "foo=%d", foo);
if (PNG_DEBUG > 2)
fprintf(PNG_DEBUG_FILE, "foo=%d\n", foo);
#ifdef PNG_DEBUG
fprintf(stderr, ...);
#endif
VII. MNG support
The MNG specification (available at http://www.libpng.org/pub/mng) allows certain extensions to PNG for PNG images that are embedded in MNG datastreams. Libpng can support some of these extensions. To enable them, use the png_permit_mng_features() function:feature_set = png_permit_mng_features(png_ptr, mask)
mask is a png_uint_32 containing the bitwise OR of the
features you want to enable. These include
PNG_FLAG_MNG_EMPTY_PLTE
PNG_FLAG_MNG_FILTER_64
PNG_ALL_MNG_FEATURES
feature_set is a png_uint_32 that is the bitwise AND of
your mask with the set of MNG features that is
supported by the version of libpng that you are using.
VIII. Changes to Libpng from version 0.88
It should be noted that versions of libpng later than 0.96 are not distributed by the original libpng author, Guy Schalnat, nor by Andreas Dilger, who had taken over from Guy during 1996 and 1997, and distributed versions 0.89 through 0.96, but rather by another member of the original PNG Group, Glenn Randers-Pehrson. Guy and Andreas are still alive and well, but they have moved on to other things.png_uint_32 libpng_vn = png_access_version_number();
png_uint_32 application_vn = PNG_LIBPNG_VER;
IX. Changes to Libpng from version 1.0.x to 1.2.x
Support for user memory management was enabled by default. To accomplish this, the functions png_create_read_struct_2(), png_create_write_struct_2(), png_set_mem_fn(), png_get_mem_ptr(), png_malloc_default(), and png_free_default() were added.PNG_ASM_FLAG_MMX_SUPPORT_COMPILED
PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
PNG_ASM_FLAG_MMX_READ_COMBINE_ROW
PNG_ASM_FLAG_MMX_READ_INTERLACE
PNG_ASM_FLAG_MMX_READ_FILTER_SUB
PNG_ASM_FLAG_MMX_READ_FILTER_UP
PNG_ASM_FLAG_MMX_READ_FILTER_AVG
PNG_ASM_FLAG_MMX_READ_FILTER_PAETH
PNG_ASM_FLAGS_INITIALIZED
PNG_MMX_READ_FLAGS
PNG_MMX_FLAGS
PNG_MMX_WRITE_FLAGS
PNG_MMX_FLAGS
png_get_mmx_flagmask()
png_set_mmx_thresholds()
png_get_asm_flags()
png_get_mmx_bitdepth_threshold()
png_get_mmx_rowbytes_threshold()
png_set_asm_flags()
PNG_READ_TRANSFORMS_NOT_SUPPORTED
PNG_PROGRESSIVE_READ_NOT_SUPPORTED
PNG_NO_SEQUENTIAL_READ_SUPPORTED
PNG_WRITE_TRANSFORMS_NOT_SUPPORTED
PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED
PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED
PNG_NO_READ_TRANSFORMS
PNG_NO_PROGRESSIVE_READ
PNG_NO_SEQUENTIAL_READ
PNG_NO_WRITE_TRANSFORMS
PNG_NO_READ_ANCILLARY_CHUNKS
PNG_NO_WRITE_ANCILLARY_CHUNKS
png_check_sig(sig, num) was replaced with
png_sig_cmp(sig, 0, num) == 0 It has been deprecated since libpng-0.90.
png_set_gray_1_2_4_to_8() which also expands tRNS to alpha was replaced with
png_set_expand_gray_1_2_4_to_8() which does not. It has been deprecated since libpng-1.0.18 and 1.2.9.
X. Changes to Libpng from version 1.0.x/1.2.x to 1.4.x
Private libpng prototypes and macro definitions were moved from png.h and pngconf.h into a new pngpriv.h header file.#define png_voidp_NULL (png_voidp)NULL were eliminated. If you used these in your application, just use NULL instead.
png_malloc(png_structp png_ptr, png_uint_32 size) to
png_malloc(png_structp png_ptr, png_alloc_size_t size)
XI. 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 function) incorrectly returned a value of type png_uint_32. The incorrect macro was removed from libpng-1.4.5.png_set_check_for_invalid_index(png_ptr, allowed);
allowed - one of
0: disable benign error (accept the
invalid data without warning).
1: enable benign error (treat the
invalid data as an error or a
warning).
int max_palette = png_get_palette_max(png_ptr, info_ptr);
/* code that uses the inch conversion APIs. */ #endif
max possible default
png_user_width_max 0x7fffffff 1,000,000
png_user_height_max 0x7fffffff 1,000,000
png_user_chunk_cache_max 0 (unlimited) 1000
png_user_chunk_malloc_max 0 (unlimited) 8,000,000
XII. Changes to Libpng from version 1.5.x to 1.6.x
A "simplified API" has been added (see documentation in png.h and a simple example in contrib/examples/pngtopng.c). The new publicly visible API includes the following:macros:
PNG_FORMAT_*
PNG_IMAGE_*
structures:
png_control
png_image
read functions
png_image_begin_read_from_file()
png_image_begin_read_from_stdio()
png_image_begin_read_from_memory()
png_image_finish_read()
png_image_free()
write functions
png_image_write_to_file()
png_image_write_to_memory()
png_image_write_to_stdio()
png_info_init_3()
png_convert_to_rfc1123() which has been replaced
with png_convert_to_rfc1123_buffer()
png_malloc_default()
png_free_default()
png_reset_zstream()
png_get_io_chunk_name(), which has been replaced
with png_get_io_chunk_type(). The new
function returns a 32-bit integer instead of
a string.
The png_sizeof(), png_strlen(), png_memcpy(), png_memcmp(), and
png_memset() macros are no longer used in the libpng sources and
have been removed. These had already been made invisible to applications
(i.e., defined in the private pngpriv.h header file) since libpng-1.5.0.
png_structp became png_structrp or png_const_structrp
png_infop became png_inforp or png_const_inforp where "rp" indicates a "restricted pointer".
#if defined(PNG_SKIP_sRGB_CHECK_PROFILE) && defined(PNG_SET_OPTION_SUPPORTED)
png_set_option(png_ptr, PNG_SKIP_sRGB_CHECK_PROFILE,
PNG_OPTION_ON);
#endif
png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW,
PNG_OPTION_ON);
default spec limit
png_user_width_max 1,000,000 2,147,483,647
png_user_height_max 1,000,000 2,147,483,647
png_user_chunk_cache_max 128 unlimited
png_user_chunk_malloc_max 8,000,000 unlimited
XIII. Detecting libpng
The png_get_io_ptr() function has been present since libpng-0.88, has never changed, and is unaffected by conditional compilation macros. It is the best choice for use in configure scripts for detecting the presence of any libpng version since 0.88. In an autoconf "configure.in" you could useAC_CHECK_LIB(png, png_get_io_ptr, ...)
XV. Source code repository
Since about February 2009, version 1.2.34, libpng has been under "git" source control. The git repository was built from old libpng-x.y.z.tar.gz files going back to version 0.70. You can access the git repository (read only) athttps://github.com/pnggroup/libpng or
https://git.code.sf.net/p/libpng/code.git
https://github.com/pnggroup/libpng or
https://sourceforge.net/p/libpng/code/ci/libpng16/tree/
https://libpng.sourceforge.io/
https://github.com/pnggroup/libpng/pulls
XV. Coding style
Our coding style is similar to the "Allman" style (See https://en.wikipedia.org/wiki/Indent_style#Allman_style), with curly braces on separate lines:if (condition)
{
action;
}
else if (another condition)
{
another action;
}
if (condition)
return 0;
#ifndef PNG_NO_FEATURE
# ifndef PNG_FEATURE_SUPPORTED
# define PNG_FEATURE_SUPPORTED
# endif
#endif
/* Single-line comment */
statement;
/* This is a multiple-line
* comment.
*/
statement;
statement; /* comment */
/* This is a public function that is visible to
* application programmers. It does thus-and-so.
*/
void PNGAPI
png_exported_function(png_ptr, png_info, foo)
{
body;
}
/* Maintainer: Put new public prototypes here ... */
void /* PRIVATE */
png_non_exported_function(png_ptr, png_info, foo)
{
body;
}
/* Maintainer: Put new private prototypes here ^ */
(sizeof (png_uint_32))
(sizeof array)
for (i = 2; i > 0; --i)
y[i] = a(x) + (int)b;
NOTE
Note about libpng version numbers:source png.h png.h shared-lib
version string int version
------- ------ ----- ----------
0.89c "1.0 beta 3" 0.89 89 1.0.89
0.90 "1.0 beta 4" 0.90 90 0.90 [should have been 2.0.90]
0.95 "1.0 beta 5" 0.95 95 0.95 [should have been 2.0.95]
0.96 "1.0 beta 6" 0.96 96 0.96 [should have been 2.0.96]
0.97b "1.00.97 beta 7" 1.00.97 97 1.0.1 [should have been 2.0.97]
0.97c 0.97 97 2.0.97
0.98 0.98 98 2.0.98
0.99 0.99 98 2.0.99
0.99a-m 0.99 99 2.0.99
1.00 1.00 100 2.1.0 [100 should be 10000]
1.0.0 (from here on, the 100 2.1.0 [100 should be 10000]
1.0.1 png.h string is 10001 2.1.0
1.0.1a-e identical to the 10002 from here on, the shared library
1.0.2 source version) 10002 is 2.V where V is the source code
1.0.2a-b 10003 version, except as noted.
1.0.3 10003
1.0.3a-d 10004
1.0.4 10004
1.0.4a-f 10005
1.0.5 (+ 2 patches) 10005
1.0.5a-d 10006
1.0.5e-r 10100 (not source compatible)
1.0.5s-v 10006 (not binary compatible)
1.0.6 (+ 3 patches) 10006 (still binary incompatible)
1.0.6d-f 10007 (still binary incompatible)
1.0.6g 10007
1.0.6h 10007 10.6h (testing xy.z so-numbering)
1.0.6i 10007 10.6i
1.0.6j 10007 2.1.0.6j (incompatible with 1.0.0)
1.0.7beta11-14 DLLNUM 10007 2.1.0.7beta11-14 (binary compatible)
1.0.7beta15-18 1 10007 2.1.0.7beta15-18 (binary compatible)
1.0.7rc1-2 1 10007 2.1.0.7rc1-2 (binary compatible)
1.0.7 1 10007 (still compatible)
...
1.0.69 10 10069 10.so.0.69[.0]
...
1.2.59 13 10259 12.so.0.59[.0]
...
1.4.20 14 10420 14.so.0.20[.0]
...
1.5.30 15 10530 15.so.15.30[.0]
...
1.6.35 16 10635 16.so.16.35[.0]
SEE ALSO
png(5)- The PNG (Portable Network Graphics) format specification.
-
http://www.libpng.org/pub/png/libpng.html (canonical home page)
-
https://zlib.net (canonical home page)
AUTHORS
This man page: Initially created by Glenn Randers-Pehrson. Maintained by Cosmin Truta.February 23, 2024 |