Fractals/png

PNG features:
 * for png color depth can range from 1 to 64 bits per pixel
 * All PNG images contain a single static image
 * 1-, 2-, 4- and 8-bit palette support (like GIF)
 * 1-, 2-, 4-, 8- and 16-bit grayscale support
 * 8- and 16-bit-per-sample (that is, 24- and 48-bit) truecolor support
 * full alpha transparency in 8- and 16-bit modes
 * "palette-alpha" mode, effectively transforming normal RGB palette into RGBA
 * gamma correction for cross-platform "brightness" control

=png file versions= PNGN = PNG N bit_per_color ( = nr_of_channels * bits_per_channel)
 * PNG8 has 2^8 = 256 shades of gray or 256 indexed-color with 8-bit samples, usually means opaque or with GIF-like, binary transparency, but sometimes includes translucency
 * PNG16
 * gray:
 * Such images are typically used only in science, medicine, and the film industry
 * It splits the color space evenly to $$2^{16} = 65\ 536$$ shades of grey
 * Note that it may be converted down to 8-bit for display, when display devices don't support 16 bits. Any values greater then 256 will simply be clipped to 255.
 * PNG24 has a color depth of 8 x 3 channels ( RGB) which is 24 bits per pixel color  = $$(2^8)^3 = 2^{8*3}   = 2^{24} = 16\ 777\ 216$$ total colours and 2^8 = 256 shades of gray = Truecolor
 * PNG32 has a color depth of 8 x 4 channels ( RGBA) which is 32 bits per pixel color = $$(2^8)^4 = 2^{8*4}   = 2^{32} = 4\ 294\ 967\ 296$$ colors =  Truecolor and alpha
 * PNG48
 * RGBA: has a color depth of 12 x 4 channels ( RGBA) which is 48 bits per pixel color = $$(2^{12})^4 = 2^{12*4} = 2^{48} = 281\ 474\ 976\ 710\ 656$$ colors = Truecolor and alpha
 * RGB:
 * PNG64 has a color depth of 16 x 4 channels ( RGBA) which is 64 bits per pixel color = $$(2^{16})^4 = 2^{16*4} = 2^{64} = 18\ 446\ 744\ 073\ 709\ 551\ 616$$ colors

=Key words=
 * sample = intersection of a channel and a pixel in an image.
 * sample depth = number of bits used to represent a sample value. In an indexed-colour PNG image, samples are stored in the palette and thus the sample depth is always 8 by definition of the palette. In other types of PNG image it is the same as the bit depth
 * An tagged image is an image that is explicitly assigned a color profile, as defined by the image format. This is usually done by including an International Color Consortium (ICC) profile [ICC].

=File format=
 * A PNG file starts with an 8-byte signature ( file header)
 * After the header, comes a series of chunks

A chunk consists of four parts:
 * length (4 bytes, big-endian),
 * chunk type/name (4 bytes )
 * chunk data (length bytes)
 * CRC (cyclic redundancy code/checksum; 4 bytes ). The CRC is a network-byte-order CRC-32 computed over the chunk type and chunk data, but not the length.

file header


The first eight bytes of a PNG file is an 8-byte signature ( a magic signature) with the following (decimal) values"

DEC: 137 80 78 71 13 10 26 10 HEX: 89 50 4E 47 0D 0A 1A 0A ASC: NA  P  N  G \r \n NA \n

Critical chunks

 * must be the first chunk; it contains (in this order) the image's
 * width (4 bytes)
 * height (4 bytes)
 * bit depth (1 byte) Bit depth is a single-byte integer giving the number of bits per sample or per palette index (not per pixel). Valid values are 1, 2, 4, 8, and 16, although not all values are allowed for all colour types. See the table below
 * color type (1 byte, values 0, 2, 3, 4, or 6)
 * compression method (1 byte, value 0)
 * filter method (1 byte, value 0)
 * interlace method (1 byte, values 0 "no interlace" or 1 "Adam7 interlace") (13 data bytes total).
 * contains the palette: a list of colors.
 * contains the image, which may be split among multiple IDAT chunks. Such splitting increases filesize slightly, but makes it possible to generate a PNG in a streaming manner. The IDAT chunk contains the actual image data, which is the output stream of the compression algorithm.
 * marks the image end; the data field of the IEND chunk has 0 bytes/is empty.

Ancillary chunks

 * gives the default background color. It is intended for use when there is no better choice available, such as in standalone image viewers (but not web browsers; see below for more details).
 * gives the chromaticity coordinates of the display primaries and white point.
 * is for storing digital signatures.
 * stores Exif metadata.
 * specifies gamma. The gAMA chunk contains only 4 bytes, and its value represents the gamma value multiplied by 100,000; for example, the gamma value 1/3.4 calculates to 29411.7647059 ((1/3.4)*(100,000)) and is converted to an integer (29412) for storage.
 * can store the histogram, or total amount of each color in the image.
 * is an ICC color profile
 * contains a keyword and UTF-8 text, with encodings for possible compression and translations marked with language tag. The Extensible Metadata Platform (XMP) uses this chunk with a keyword 'XML:com.adobe.xmp'
 * holds the intended pixel size (or pixel aspect ratio); the pHYs contains "Pixels per unit, X axis" (4 bytes), "Pixels per unit, Y axis" (4 bytes), and "Unit specifier" (1 byte) for a total of 9 bytes.
 * (significant bits) indicates the color-accuracy of the source data; this chunk contains a total of between 1 and 5 bytes, depending on the color type.
 * suggests a palette to use if the full range of colors is unavailable.
 * indicates that the standard sRGB color space is used; the sRGB chunk contains only 1 byte, which is used for "rendering intent" (4 values&mdash;0, 1, 2, and 3&mdash;are defined for rendering intent).
 * stereo-image indicator chunk for stereoscopic images.
 * can store text that can be represented in ISO/IEC 8859-1, with one key-value pair for each chunk. The "key" must be between 1 and 79 characters long. Separator is a null character. The "value" can be any length, including zero up to the maximum permissible chunk size minus the length of the keyword and separator. Neither "key" nor "value" can contain null character. Leading or trailing spaces are also disallowed.
 * stores the time that the image was last changed.
 * contains transparency information. For indexed images, it stores alpha channel values for one or more palette entries. For truecolor and grayscale images, it stores a single pixel value that is to be regarded as fully transparent.
 * contains compressed text (and a compression method marker) with the same limits as.

Rendering intent values (Value	Name	Description)
 * 0	= Perceptual	for images preferring good adaptation to the output device gamut at the expense of colorimetric accuracy, such as photographs.
 * 1	= Relative colorimetric	for images requiring colour appearance matching (relative to the output device white point), such as logos.
 * 2	= Saturation	for images preferring preservation of saturation at the expense of hue and lightness, such as charts and graphs.
 * 3	= Absolute colorimetric	for images requiring preservation of absolute colorimetry, such as previews of images destined for a different output device (proofs).

= Examples =

Displayed in the fashion of hex editors, with on the left side byte values shown in hex format, and on the right side their equivalent characters from w:ISO-8859-1 with unrecognized and control characters replaced with periods. Additionally the PNG signature and individual chunks are marked with colors. Note they are easy to identify because of their human readable type names (in this example PNG, IHDR, IDAT, and IEND).

PNG Signature(8 bytes): 89 50 4E 47 0D 0A 1A 0A =======Chunk start======= IHDR Chunk: IHDR chunk length(4 bytes): 00 00 00 0D IHDR chunk type(Identifies chunk type to be IHDR): 49 48 44 52 Image width in pixels(variable 4): xx xx xx xx       Image height in pixels(variable 4): xx xx xx xx        Flags in the chunk(variable 5 bytes): xx xx xx xx xx        CRC checksum(variable 4 bytes): xx xx xx xx     =======Chunk end=======

read png
Read png with od program

od -c test.png | head -4 Example output: 0000000 211  P   N   G  \r  \n 032  \n  \0  \0  \0 004   C   g   B   I 0000020   P  \0     002   + 325 263 177  \0  \0  \0  \r   I   H   D   R 0000040  \0  \0  \0   `  \0  \0  \0   `  \b 006  \0  \0  \0 342 230   w 0000060   8  \0  \0  \0       c   H   R   M  \0  \0   z   %  \0  \0 200

Programs:
 * pngcheck a command-line program by Greg Roelofs

pngcheck -c *.png pngcheck -cvt *.png

=code=
 * ImageMagic creating png in polish wikibooks
 * rpng is a simple and easy-to-use C library to manage png chunks by raysan5

octave
Octave code that creates array ( memory image) and saves it to the file

=collection of images=
 * PngSuite created by Willem van Schaik  The "official" test-suite for PNG
 * Rich Franzen's PNG Gallery
 * 16-bit integer ProPhotoRGB png : saturated-colors.png

=Creating png files=
 * Image Magic

=Checking png files=

file p.png

ICC embaded profile
 * PNG-ICC-tests
 * results

=Editing png files=
 * hex editors:
 * editing of pixel data in a hex editor is pretty much impossible because of of the compression algorithm
 * one can edit header,
 * use an online crc calculator to calculate the checksum for the png
 * use TS/JS
 * pngcheck

Hexdump

hexdump --canonical p.png

To show the output of the PNG header as described by the official libpng spec
 * to process the PNG file in 8-byte chunks
 * the PNG spec is documented in decimal, which is represented by %d according to the hexdump documentation
 * make the output perfect by adding a blank space after each integer

hexdump -n8 -e'8/1 "%d ""\n"' p.png

ICC profile tasks
 * Extract ICC profile from the file
 * To remove an ICC profile
 * To embed an ICC prolile

Extract ICC profile from the file:

exiftool -icc_profile -b -w icc somefile.png

To remove an ICC profile

pngcrush -rem iCCP foo.png result.png

=See also=
 * png-embedded-malicious-payload-hidden-in-a-png-file by THIAGO MARQUES
 * png compatybility
 * pngquant

=References=