Table of contents
Overview of the PNG file format
The full name of PNG is Portable Network Graphic Format (PNG), which is a bitmap file (bitmap file) storage format, read as "ping".
A PNG file consists of two parts, namely the PNG file logo and data blocks (chunks).
The role of the PNG file flag is to identify whether the current file is a PNG file; data blocks are divided into two categories: critical data blocks (critical chunks) and auxiliary data blocks (ancillary chunks), critical data blocks are necessary in PNG files, and auxiliary data blocks are Data blocks are optional.
file structure
For a PNG file, its 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
After opening a PNG file as shown in the figure below, it can be observed that its file header is 89 50 4E 47 0D 0A 1A 0A, which also proves that the file is a PNG file.
PNG data block structure
PNG data blocks are divided into two categories: critical chunks and ancillary chunks.
The key data block consists of four parts: file header data block (IHDR), palette data block (PLTE), image data block (IDAT) and image end data (IEND). The palette data block (PLTE) is optional according to the color depth of the image.
The PNG file format specification specifies 10 auxiliary data blocks :
name |
---|
Background color data block bKGD(background color). |
Primary color and whiteness data block cHRM (primary chromaticities and white point). |
Image gamma data block gAMA (image gamma). |
Image histogram data block hIST(image histogram). |
Physical pixel size data block pHYs (physical pixel dimensions). |
Sample effective bit data block sBIT (significant bits). |
Text information data block tEXt (textual data). |
Image last modification time data block tIME (image last-modification time). |
Image transparency data block tRNS (transparency). |
Compressed text data block zTXt (compressed textual data). |
Data block composition structure:
name | Bytes | illustrate |
---|---|---|
Length (length) | 4 bytes | Specifies the length of the data field in the data block, and its length does not exceed (2^31-1) bytes |
Chunk Type Code (data block type code) | 4 bytes | The data block type code is a "data block symbol" consisting of ASCII letters (AZ and az) |
Chunk Data | variable length | Store data specified by Chunk Type Code |
CRC (Cyclic Redundancy Check) | 4 bytes | Stores the cyclic redundancy code used to detect whether there is an error |
Case Analysis
Open a PNG file, test.png
Open the file in binary mode using Visual Studio 2019
Key Data Block Analysis
(1) IHDR data block
00 00 00 0D -----indicates that the length of the IHDR header block is 13
49 48 44 52 -----IHDR identification
00 00 03 06 ------image width, 774 pixels
00 00 03 06 --- ---Image height, 774 pixels
08 ------------------Color depth, 2^8=256, that is, this is a 256-color image
02 ---- --------------Color type, grayscale image
00 ------------------PNG Spec stipulates that this is always 0 (not 0 The value is reserved for a better compression method in the future), indicating that the compression method (LZ77 derived algorithm)
00 ------------------PNG Spec stipulates that it is always 0 here ( The non-zero value is reserved for better compression methods in the future), indicating that the compression method (LZ77 derived algorithm)
00 ------------------ non-interlaced scanning
C7 A4 04 83 -----CRC check code
(2) PLTE palette data block
The PLTE data block is the palette information that defines the image. The PLTE can contain 1 to 256 palette information, and each palette information consists of 3 bytes.
In this example, if the color depth is 8, the number of colors does not exceed 2^8=256, and the length of the palette is 1216=192=364, which is a multiple of 3.
(3) IDAT image data block
E7 14 EE 56 -----------The data length is 3876908630 bytes
60 2F FD FA ----------- IDAT logo
20 20... --------------Compressed data
(4) IEND image end data
00 00 00 00 ------------- indicates that the length of the data part is 0, that is, this data block has no data part
49 45 4E 44 ------------- data type Code, 49 45 4E 44 is the IEND logo
AE 42 60 82 -------------CRC check code
Auxiliary data block analysis
(1) gAMA image y data block
(2) cHRM base color and white point data block
(3) bKGD background color data block
(4) pHYs physical pixel size data block
(5) tIME image last modification time data block
references
【1】Data compression job 2: PNG file format analysis_Liang Qirong's blog-CSDN blog
【2】PNG file format analysis_Messi0822's blog-CSDN blog
【3】Detailed PNG file structure-Angel_Kitty-博客园(cnblogs.com)