PNG file structure analysis (I: Understand the PNG file storage format)
Preface
We all know that during the development of the mobile phone application program for j2's, we can use images in PNG format (even on some mobile phones, we can only use images in PNG format), although using images can add many highlights to our application, however, only images in PNG format are supported, which limits the possibility of further improvement (in fact, it should be said that the processing capability on the mobile phone platform is limited ). In midp2 or APIs provided by some vendors (such as Nokia), drawpixels/getpixels methods are provided. These methods further improve the flexibility of developers in processing images. However, we need to use. 0 implementation of this type of method is still whimsical, so in order to achieve more advanced applications, we must fully tap the potential of PNG.
PNG file structure
For a PNG file, the file header is always described by fixed Bytes:
Decimal number |
137 80 78 71 13 10 26 10 |
Hexadecimal number |
89 50 4E 47 0d 0a 1A 0a |
The first byte 0x89 is out of the ASCII character range to prevent some software from processing PNG files as text files. The remaining parts of the file are composed of more than three PNG data blocks (chunk) in a specific order. Therefore, a standard PNG file structure should be as follows:
PNG file flag |
PNG data block |
...... |
PNG data block |
PNG data block (chunk)
PNG defines two types of data blocks. One is critical chunk, which is a standard data block, and the other is ancillary chunks ), this is an optional data block. Key data blocks define four standard data blocks. Each PNG file must contain them. PNG read/write software must also support these data blocks. Although the PNG file specification does not require the PNG encoding/Decoding of optional data blocks, the specification advocates the support of optional data blocks.
The following table lists the types of data blocks in PNG. The key data blocks are distinguished by the dark background.
Data blocks in PNG file format |
Data Block symbol |
Data Block name |
Multiple Data blocks |
Optional |
Location restrictions |
Ihdr |
File header data block |
No |
No |
First |
CHRM |
Base color and white point data block |
No |
Yes |
Before plte and idat |
Gama |
Image gamma data block |
No |
Yes |
Before plte and idat |
Sbit |
Sample valid BIT data block |
No |
Yes |
Before plte and idat |
Plte |
Color Palette data block |
No |
Yes |
Before idat |
Bkgd |
Background color data block |
No |
Yes |
Before idat after plte |
Hist |
Image histogram data block |
No |
Yes |
Before idat after plte |
TRNS |
Transparent data block |
No |
Yes |
Before idat after plte |
Offs |
(Private Public Data blocks) |
No |
Yes |
Before idat |
Phys |
Physical pixel size data block |
No |
Yes |
Before idat |
Scal |
(Private Public Data blocks) |
No |
Yes |
Before idat |
Idat |
Image Data Block |
Yes |
No |
Continuous with other idat |
Time |
Last modification time data block of the image |
No |
Yes |
Unlimited |
Text |
Text Information Data Block |
Yes |
Yes |
Unlimited |
Ztxt |
Compressing text data blocks |
Yes |
Yes |
Unlimited |
Frac |
(Private Public Data blocks) |
Yes |
Yes |
Unlimited |
Gifg |
(Private Public Data blocks) |
Yes |
Yes |
Unlimited |
Gift |
(Private Public Data blocks) |
Yes |
Yes |
Unlimited |
Gifx |
(Private Public Data blocks) |
Yes |
Yes |
Unlimited |
Iend |
Image end data |
No |
No |
Last data block |
For the sake of simplicity, we assume that in the PNG file we use, the four data blocks are stored in the above Order, and only appear once.
Data Block Structure
In a PNG file, each data block consists of four parts:
Name |
Bytes |
Description |
Length) |
4 bytes |
Specifies the length of the data field in a data block. The length cannot exceed (231-1) bytes. |
Chunk type code (data block type code) |
4 bytes |
The data block type code consists of ASCII letters (A-Z and A-Z) |
Chunk data (data block data) |
|