PNG file Structure Analysis---PNG parsing

Source: Internet
Author: User

In order to achieve more advanced applications, we must fully exploit the potential of PNG.

File structure of PNG

Depending on the definition of a PNG file, the header location of the file is always described by a bit-fixed byte:
Decimal number 137 80 78 71 13 10 26 10
Hexadecimal number 4E 0D 0A 1 a 0A
The first byte 0x89 is beyond the range of ASCII characters, in order to prevent some software from treating PNG files as text files. The remainder of the file is made up of more than 3 PNG data blocks (Chunk) in a specific order, so a standard PNG file structure should look like this:
PNG file Flags PNG data block ...... PNG data block

PNG data block (Chunk)

PNG defines two types of data blocks, one of which is called the Critical data Block (critical chunk), which is the standard block of data, and the other is called the auxiliary data Block (ancillary chunks), which is an optional block of data. Critical data blocks define 4 standard blocks, each of which must contain them, and PNG read-write software must support them. Although the PNG file specification does not require the PNG compiler to encode and decode an optional block of data, the specification advocates support for optional blocks of data.

The table below is the category of data blocks in PNG, where we use a dark background to differentiate the key chunks.

Data blocks in the PNG file format
Data block symbols Data block name Majority Data block Optional No Location restrictions
IHDR File header data block Whether Whether First piece
CHRM Base color and white point data block Whether Is Before Plte and Idat
GAMA Image Gamma Data block Whether Is Before Plte and Idat
Sbit Sample valid bit data block Whether Is Before Plte and Idat
Plte Palette Data Block Whether Is Before the Idat
Bkgd Background color data block Whether Is Before Idat after Plte
HIST Image histogram data block Whether Is Before Idat after Plte
Trns Image Transparent Data block Whether Is Before Idat after Plte
OFFs (Dedicated public data block) Whether Is Before the Idat
PHYs Physical pixel size data block Whether Is Before the Idat
SCAL (Dedicated public data block) Whether Is Before the Idat
IDAT Image data Block Is Whether Continuous with other Idat
Time Image last modified time data block Whether Is Unlimited
TEXt Text Information data block Is Is Unlimited
Ztxt Compressing text data blocks Is Is Unlimited
FRAc (Dedicated public data block) Is Is Unlimited
Gifg (Dedicated public data block) Is Is Unlimited
GIFt (Dedicated public data block) Is Is Unlimited
Gifx (Dedicated public data block) Is Is Unlimited
Iend Image End Data Whether Whether Last data block

For the sake of simplicity, let's assume that in the PNG file we're using, the 4 blocks are stored in the order above and only once.

Data block structure

In a PNG file, each chunk consists of 4 parts, as follows:

Name Number of bytes Description
Length (Long) 4 bytes Specifies the length of the data field in the data block, which is not longer than (231-1) bytes
Chunk Type code (data block type code) 4 bytes Block type codes are made up of ASCII letters (A-Z and A-Z)
Chunk data (block data) Variable length Stores data specified according to chunk Type code
CRC (cyclic redundancy detection) 4 bytes Storage of cyclic redundancy codes to detect errors

The values in the CRC (cyclic redundancy check) field are computed for the data in the chunk Type code domain and the chunk database domain. The CRC-specific algorithm is defined in ISO 3309 and ITU-T v.42, and its value is calculated as a polynomial generated by the following CRC code:

X32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1

CRC: a checking algorithm. Just to verify the correctness of the data.

Let's look at the structure of each key block in turn.

IHDRHeader chunk: It contains basic information about the image data stored in the PNG file and is to appear in the PNG data stream as the first block of data, and only one file header block in a PNG data stream. IHDR The file header data block consists of 13 bytes, which are formatted as shown in the following table.
Name of the domain Number of bytes Description
Width 4 bytes Image width, in pixels
Height 4 bytes Image height, in pixels
Bit depth 1 byte Image depth:
Indexed color images: 1,2,4 or 8
Grayscale Image: 1,2,4,8 or 16
True Color Image: 8 or 16
ColorType 1 byte Color type:
0: Grayscale image, 1,2,4,8 or 16
2: True Color image, 8 or 16
3: Indexed color image, 1,2,4 or 8
4: Grayscale image with alpha channel data, 8 or 16
6: True color image with alpha channel data, 8 or 16
Compression method 1 byte Compression method (LZ77 derivation algorithm)
Filter method 1 byte Filter method
Interlace method 1 byte Interlaced scanning method:
0: Non-interlaced scanning
1:ADAM7 (7-Times interlaced method developed by Adam M. Costello)
Since many of this article has been designed to use PNG in mobile phones, the requirements for PNG images used in MIDP1.0 are presented here:
    • In MIDP1.0, you can only use PNG images with version 1.0.
    • File size: MIDP supports PNG images of any size, however, in fact, if a picture is too large, it cannot be read because of memory exhaustion.
    • Color type: All color types are supported, although the display of these colors depends on the actual device display capability. Also, MIDP can support the alpha channel, but all alpha channel information is ignored and treated as an opaque color.
    • Color depth: All color depths can be supported.
    • Compression method: Only support deflate compression, which is identical to the way the jar files are compressed, so the decompression of PNG image data and the decompression of the jar file can use the same code.
    • Filter method: All 5 methods in PNG are supported.
    • Interlaced scanning: Although MIDP supports 0, 12 ways, however, when using interlaced scanning, MIDP does not really use interlaced scanning mode to display.
    • Plte Chunk: Support
    • IDAT Chunk: Image information must be used in one of the 5 ways of filtering (None, Sub, up, Average, Paeth)
    • Iend Chunk: This PNG image is considered a legitimate PNG image when the Iend data block is found.
    • Optional data blocks: MIDP can support the following secondary data blocks, however, this is not required.
BKGD CHRM GAMA HIST ICCP itxt pHYs
Sbit SPLT SRGB tEXt time Trns ztxt PltePalette Data Block Plte (palette chunk) contains color transform data related to indexed color images (indexed-color image), which is only relevant to indexed color images and is placed before image data chunk. Plte data blocks are palette information that defines an image, and Plte can contain 1~256 palette information, each of which consists of 3 bytes:
Color Bytes Significance
Red 1 byte 0 = black, 255 = red
Green 1 byte 0 = black, 255 = Green
Blue 1 byte 0 = black, 255 = Blue
Therefore, the length of the palette should be a multiple of 3, otherwise it would be an illegal palette. For indexed images, palette information is required, the color index of the palette is numbered starting with 0, then 1, 2 ..., the color of the palette cannot exceed the number of colors specified in the color depth (as deep as 4, the number of colors in the palette may not exceed 2^4=16), otherwise this will result in the PNG image being illegal. True color images and true color images with alpha channel data can also have palette data blocks, which are designed to allow non-true color display programs to quantify image data to display the image. IDATImage data Block Idat: It stores the actual data, which can contain multiple sequential sequential image blocks in the data flow. Idat stores the real data information of the image, so if you can understand the structure of the idat, we can easily generate PNG images. iendImage end Data iend (Image trailer Chunk): It is used to mark a PNG file or the data stream has ended and must be placed at the end of the file. If we look closely at the PNG file, we will find that the 12 characters at the end of the file should always look like this: 4E 42 60 82 for the data block structure definition, the length of the iend data block is always 0 (00 00 00 00, unless artificially Add information), the data ID is always iend (4E 44), so the CRC is always AE 42 60 82. Case study PNGThe following is an image generated by fireworks, the image size is 8*8, for easy viewing, enlarge the image:

Open the file using UltraEdit32, as follows:
00000000~00000007:As you can see, the selected header 8 bytes is the identity of the PNG file. The next place is the IHDR data block: 00000008~00000020:
    • 0D Description IHDR Head block length is 13
    • IHDR logo
    • 00 00 00 08 Width of the image, 8 pixels
    • 00 00 00 08 High, 8 pixel image
    • 04 color Depth, 2^4=16, that is, this is a 16-color image (there may be no more than 16 colors, of course, if the number of colors does not exceed 8, 03 is more appropriate)
    • 03 Color type, indexed image
    • The PNG spec specifies that there will always be 0 (not 0 values reserved for future use of a better compression method), which means that the compression method (LZ77 derivation algorithm)
    • 00 Ibid.
    • 00 Non-interlaced scanning
    • A3 B8 CRC Checksum
00000021~0000002f:Optional data block sbit, color sampling rate, RGB are 256 (2^8=256) 00000030~00000062:Here is the palette information
    • 00 00 00 27 indicates that the color palette data is 39 bytes long and has a 13 number of colors
    • 4C Plte logo
    • FF FF 00 Color 0
    • FF ED 00 Color 1
    • ...... ......
    • B2 last color, 12
    • 5F F5 BB DD CRC check
00000063~000000C5:This section contains the Phys, text two types of data blocks a total of 3 blocks, because it is not very important, and therefore no longer described in detail.

000000c0~000000f8:The selected section above is the IDAT data block
    • 00 00 00 27 Data length is 39 bytes
    • Idat logo
    • 9C ... Compressed data, LZ77 derived compression method
    • DA A5 CRC Checksum
The compressed data section in Idat is described in more detail later. 000000f9~00000104:Iend data block, this part, as said above, usually should be the XX (XX) 4E 42 60 82 Now, we have been able to identify each block of data from a PNG file. Since PNG specifies that other auxiliary blocks are optional except for critical chunks, with this standard, we can reduce the size of PNG files by removing all of the auxiliary data blocks. (Of course, it is important to note that the PNG format can save information such as layers, text, and so on in the image, and once these secondary blocks are deleted, the image loses its original editable character.) The PNG file after the secondary data block is deleted, the file size is now 147 bytes, the original file size is 261 bytes, the file size is reduced, and the contents of the image are not affected.
    • As mentioned above, the IDAT data block is generated using the LZ77 compression algorithm, as it is limited by the phone processor

In fact, we can do some interesting things by changing the color value of the color palette, such as the flow effect of cloud/water, realize the fade effect of the image and so on.

Http://www.cnblogs.com/lidabo/p/3701197.html

PNG file Structure Analysis---PNG parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.