PNG file data structure image format

Source: Internet
Author: User

PNG is an image file storage format developed in the middle of 1990s. It aims to replace GIF and TIFF file formats.
Added some features not available in the GIF file format. Portable Network graphic format (PNG)
) The name is from the unofficial "PNG's not GIF". It is a bitmap file storage format and is read as "ping ". P
When Ng is used to store grayscale images, the depth of the grayscale image can be up to 16 bits. When the color image is stored, the depth of the color image can be up to 48 bits,
It can also store up to 16-bit alpha channel data. PNG uses the lossless data compression algorithm derived from lz77.

I. Data Structure

1. PNG data block
PNG defines two types of data blocks. One is key data blocks, which are standard data blocks and the other is auxiliary data.
Block. This is an optional data block. Key data blocks define five standard data blocks. Each PNG file must contain them.
You must also support these data blocks. For optional data blocks, the specifications only advocate support.
The following table lists the types of data blocks in PNG. * indicates the key data blocks:
----------------------------------------------------------------
Data Block symbol data block name multiple data blocks optional no location restrictions
----------------------------------------------------------------
* PNG file ID No first block
Ihdr * file header data block no second
Whether the CHRM base color and white dot data blocks are before plte and idat
Gama image gamma data block no before plte and idat
Sbit sample valid BIT data block no before plte and idat
Plte * color palette data block no before idat
Bkgd background color data block no before idat after plte
Hist image histogram data block no before idat after plte
TRNS image transparent data block no before idat after plte
Offs (private public data block) is not before idat
Phys physical pixel size data block no before idat
Scal (private public data block) is not before idat
Idat * Whether the image data block is continuous with other idat
The last modification time of the time image is unlimited.
Text data blocks are unrestricted.
Ztxt compressed text data blocks are unrestricted
Frac (dedicated public data block) is unrestricted
Gifg (dedicated public data block) is unrestricted
Gift (dedicated public data block) is unrestricted
Gifx (private public data block) is unrestricted
Iend * image end data block no last
--------------------------------------------------------------
Apart from key data blocks, other data blocks are dispensable.

2. Data Block Structure
In a PNG file, except for the PNG file identifier, all data blocks are composed of four parts, as shown in the following table:
----------------------------------------------------------------------
Description of the number of bytes of the Sn
----------------------------------------------------------------------
1 length 4 specify the length of part 3rd data fields
2 Data Block symbol 4 consists of the ASCII code of the data block symbol
3. data domains may not store data specified by chunk type code.
4 CRC Check 4, also known as cyclic redundancy check, is used to detect errors
----------------------------------------------------------------------
The value in Cyclic Redundancy detection is calculated for part 1 of the block symbols and Part 2 of the data domain. The specific algorithm is defined in
In ISO 3309 and ITU-T v.42, the value is calculated according to the following CRC code generation polynomial:
X32 + X26 + x23 + x22 + x16 + X12 + X11 + x10 + X8 + X7 + X5 + X4 + X2 + x + 1

The specific structure of 3.5 key data blocks.

① PNG file ID
It is fixed to 8 Bytes: 89 50 4E 47 0d 0a 1A 0a, and the first 4 bytes are the ASCII code of ". PNG.

② File header data block
It contains the basic information of the image data stored in the PNG file and needs to appear as the second data block in the PNG data stream,
A png data stream can have only one file header data block. The format is shown in the following table.
-----------------------------------------------------
Description of the number of domain name bytes
-----------------------------------------------------
Data domain length 4 specifies the length of the data domain, fixed to 00 00 0d
Data Block symbol 4 49 48 44 52, is the ihdr ASCII code
(The following 13 bytes are data fields)
Image Width 4 units: pixels
Image Height 4 units: pixels
Color Depth 1 grayscale image: 1, 2, 4, 8, or 16
True Color Image: 8 or 16
Indexed color images: 1, 2, 4, or 8
Grayscale Images with alpha channel data: 8 or 16
True Color Image with alpha channel data: 8 or 16
Color type 1 grayscale image: 0
True Color Image: 2
Indexed color image: 3
Grayscale Images with alpha channel data: 4
True Color Image with alpha channel data: 6
Compression Method 1 specifies that this byte is 0 (compressed using lz77 derived algorithm)
Filter Method 1 usually this byte is 0
Method 1 Non-interlaced scan: 0
Adam7 (7 times/second scan): 1
CRC Check 4
-----------------------------------------------------

③ Palette data blocks
Contains color transformation data related to indexed color images. It is only related to indexed color images and must be placed in image data blocks.
Before. It defines the color palette information of an image. Its data structure is as follows:
----------------------------------------------------------------------
Description of the number of domain name bytes
----------------------------------------------------------------------
Data domain length 4 specify the data domain Length
Data Block symbol 4 50 4C 54 45, which is the ASCII code of "plte"
There are no more than n color palette in the data field. There are 3 × n Bytes of length, up to 3 × 256 bytes.
CRC Check 4
----------------------------------------------------------------------
Each color palette consists of three bytes indicating the red, green, and blue information. Therefore, the length of the color palette should be a multiple of three. Otherwise
Is an invalid color palette.
For indexed images, the color palette information is required. The color index of the palette starts from 0, followed by 1, 2 ......, Color
The number of colors in the palette cannot exceed the number of colors specified by the "color depth" in the data block of the file header (for example, when the color depth is 4, the colors in the palette
The color number cannot exceed 2 ^ 4 = 16). Otherwise, the PNG image is invalid.
Real-color images and true-color images with alpha channel data can have color palette data blocks to facilitate non-real-Color Display programs.
To quantify the image data and display the image, or do not use a color palette.

④ Image data block
It stores actual data and can contain multiple consecutive image data blocks in the data stream. The data structure is as follows:
--------------------------------------------------------
Description of the number of domain name bytes
--------------------------------------------------------
Data domain length 4 specify the data domain Length
Data Block symbol 4 49 44 41 54, which is the ASCII code of "idat"
The data domain may not store the real data information of the image (Compressed)
CRC Check 4
--------------------------------------------------------
Since image data blocks are compressed data, if you can master the compression and decompression methods, you can easily
Convert a PNG Image to another type of image.

⑤ Image ending Block
It is used to mark the PNG file or the data stream has ended and must be placed at the end of the file. The data structure is as follows:
--------------------------------------------------------
Description of the number of domain name bytes
--------------------------------------------------------
Data domain length 4: 0: 00 00 00
Data Block symbol 4 49 45 4E 44, which is an ascii code of "iend"
Data domain
CRC verification 4 AE 42 60 82
--------------------------------------------------------

2. A specific example
Create an 8x8 pixel image, fill in red, and save it as PNG. All the data of this image is as follows (note:
Device differences, your data may be slightly different ):

000000: 89 50 4E 47 0d 0a 1A 0a 00 00 00 0d 49 48 44 52
000010: 00 00 08 00 00 08 08 02 00 00 4B 6D 29
000020: DC 00 00 00 01 73 52 47 42 00 AE ce 1C E9 00 00
000030: 00 04 67 41 4D 41 00 00 B1 8f 0b FC 61 05 00 00
000040: 00 20 63 48 52 4D 00 00 7A 26 00 80 84 00 00
000050: Fa 00 00 00 80 E8 00 00 75 30 00 00 EA 60 00 00
000060: 3A 98 00 00 17 70 9C Ba 51 3C 00 00 00 17 49 44
000070: 41 54 18 57 63 fc cf 80 03 00 25 B0 22 EC A2 20
000080: 63 06 52 07 00 11 99 38 C9 dB F8 06 FD 00 00 00
000090: 00 49 45 4E 44 AE 42 60 82

Analysis:

000000-000007: These 8 bytes are identified as PNG files.

000008-000020: The data block of the file header, where:
00 00 00 0d: The data domain length is 13 bytes.
49 48 44 52: file header data block symbol "ihdr"
00 00 00 08: Image Width 8 pixels
00 00 00 08: Image Height 8 pixels
Color Depth
02: true color
00: Use Compression
00: filter method, usually 0
00: Non-interlace Scan
4B 6D 29 DC: CRC verification code

201721-00002d: optional data block sRGB

%2e-%3d: optional data block Gama (image gamma data block)

20173e-000069: optional data block CHRM (base color and white point data block)

Ipv6a-rj8c: idat data block, where:
00 00 00 17: The data domain length is 23 bytes.
49 44 41 54: idat data block ID "idat"
18 57 63... : Compressed Data
DB F8 06 FD: CRC Check Code

20178d-000098: iend data block. As mentioned above, it is fixed to 00 00 00 00 49 45 4E 44 AE 42 60 82

As PNG specifies that all other auxiliary data blocks except key data blocks are optional, we can use the hex editor
Add/delete experiments on the data (hex Editor: http://bbs.pfan.cn/post-249666.html)
  
1. Image slimming: using the delete function of the hex editor, the auxiliary data blocks 20-000069 are deleted.
To reduce the size of 153 bytes to 80 bytes, without affecting the image content! Try it. Have you succeeded?
However, you must note that the PNG format can save layer and text information in the image. Once the data is deleted, the image will be lost.
The original editability.

2. Add text information: add such a string "PNG Image", the ASCII code of the string is 50 4E 47 cd bc C6 AC, a total of 7
So the data domain length should be 00 00 00 07, the data block symbol is "text", the ASCII is 74 45 58 74, as for CRC
Verification code. We can write 4 bytes at will. Let's write Ba 2D 29 CC! All these information is:
00 00 00 07 74 45 58 74 4E 47 cd bc C6 AC Ba 2D 29 CC
19 bytes in total. Since text data blocks can appear between any data blocks, we use the insert function of the hex editor to insert these 19 words.
Insert it from section 000068, save it as, and open it with the code in the attachment. We can't help but see the image and see
Seven characters of text!

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.