PNG file interpretation (2): PNG format file structure and data structure interpretation - decoding PNG data

Army Zhou 2022-06-24 05:31:49 阅读数:363

pngfileinterpretationpngformat

PNG File identification

Before written 《JPEG/Exif/TIFF Format interpretation (1):JEPG Analysis of picture compression and storage principle 》,JPEG The file is ,FFD8 start ,FFD9 ending , Stored in the middle 0xFFE0~0xFFEF The data segment marked .

For one PNG The file is , Its header is always described by fixed bit bytes ,HEX: 89 50 4E 47 0D 0A 1A 0A

The first byte 0x89 Beyond the ASCII The range of characters , This is to avoid that some software will PNG Files are treated as text files . The rest of the file is by 3 More than one PNG A block of data (Chunk) Make up in a particular order , therefore , A standard PNG The file structure should be as follows :

PNG Document mark

PNG Data blocks

……

PNG Data blocks

And JPEG The format is as follows

SOI Marker

Marker XX size=SSSS

Marker YY size=TTTT

SOS Marker size=UUUU

Image stream

EOI Marker

FFD8

FFXX

SSSS

DDDD......

FFYY

TTTT

DDDD......

FFDA

UUUU

DDDD....

I I I I....

FFD9

what a striking similarity , But it's very different .png The data structure is better than jpeg complex

jpeg: Segment ID (FF)+ Segment type (1 byte )+ Segment length (2 byte )+ Data blocks

png: Segment length (4 byte )+ Segment type / Type mark (4 byte )+ Data blocks + Check code (4 byte )

png Every paragraph of , Call it a data block .

PNG Data blocks (Chunk)

PNG Two types of data blocks are defined ,

  • Key data blocks (critical chunk), This is the standard data block
  • Auxiliary data block (ancillary chunks), This is an optional data block .

Key data blocks define 4 A standard data block , Every PNG All files must contain them ,PNG Reading and writing software must also support these data blocks . although PNG The document specification does not require PNG The codec encodes and decodes optional data blocks , But the specification advocates supporting optional data blocks .

The following table is PNG Category of data block in , among , In the key data block section, we use a dark background to distinguish .

Data block symbols

Block name

Multiple data blocks

Optional no

Position limit

IHDR

Header data block

no

no

First block

cHRM

Base color and white dot data block

no

yes

stay PLTE and IDAT Before

gAMA

Images γ Data blocks

no

yes

stay PLTE and IDAT Before

sBIT

Sample significant bit data block

no

yes

stay PLTE and IDAT Before

PLTE

Palette data block

no

yes

stay IDAT Before

bKGD

Background color data block

no

yes

stay PLTE after IDAT Before

hIST

Image histogram data block

no

yes

stay PLTE after IDAT Before

tRNS

Image transparent data block

no

yes

stay PLTE after IDAT Before

oFFs

( Private public data block )

no

yes

stay IDAT Before

pHYs

Physical pixel size data block

no

yes

stay IDAT Before

sCAL

( Private public data block )

no

yes

stay IDAT Before

IDAT

Image data block

yes

no

And others IDAT continuity

tIME

Image last modified time data block

no

yes

unlimited

tEXt

Text message data block

yes

yes

unlimited

zTXt

Compress text data blocks

yes

yes

unlimited

fRAc

( Private public data block )

yes

yes

unlimited

gIFg

( Private public data block )

yes

yes

unlimited

gIFt

( Private public data block )

yes

yes

unlimited

gIFx

( Private public data block )

yes

yes

unlimited

IEND

Image end data

no

no

The last data block

At present, we only need to focus on the key data blocks marked in red .

There is... In the data block 4 Key data blocks :

  1. Header data block IHDR(header chunk): Contains basic information about images , Appears as the first data block and only once .
  2. Palette data block PLTE(palette chunk): Must be placed before the image data block .
  3. Image data block IDAT(image data chunk): Store the actual image data .PNG The data is allowed to contain a plurality of consecutive image data blocks .
  4. Image end data IEND(image trailer chunk): Put it at the end of the file , Express PNG End of data flow .

Data blocks are connected , About this :

PNG identifier

PNG Data blocks (IHDR)

PNG Data blocks ( Other types of data blocks )

PNG End data block (IEND)

That is, a piece of data is filled in according to a fixed format , The head and tail must , Fill the compressed data of the picture in the middle . So read png All data for , You need to interpret each data block

PNG Data block structure

PNG In file , Each data block consists of 4 Component composition , as follows :

name

Number of bytes

explain

Length ( length )

4 byte

Specifies the length of the data field in the data block , Its length does not exceed (231-1) byte

Chunk Type Code ( Block type code )

4 byte

The block type code consists of ASCII Letter (A-Z and a-z) form

Chunk Data ( Block data )

Variable length

Store according to Chunk Type Code Specified data

CRC ( Cyclic redundancy detection )

4 byte

Store the cyclic redundancy code used to detect whether there is an error

  • Length It's worth dividing :length In itself ,Chunk Type Code,CRC Outer length , That is to say Chunk Data The length of .
  • CRC: A verification algorithm . Only used to verify the correctness of data

CRC(cyclic redundancy check) The value in the field is right Chunk Type Code Domain and Chunk Data The data in the domain is calculated .

CRC The specific algorithm is defined in ISO 3309 and ITU-T V.42 in , Its value is as follows CRC Code generation polynomial for calculation :

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

below , Let's take a look at the structure of each key data block in turn .

Header data block IHDR

Header data block IHDR(header chunk): It's a bag contain PNG Basic information of image data stored in the file , And as the first data block to appear in PNG In the data stream , And one PNG There can only be one file header block in the data stream .

The file header data block is created by 13 Byte composition , Its format is shown in the following table .

The name of the domain

Number of bytes

explain

Width

4 bytes

The width of the image , In pixels

Height

4 bytes

Height of the image , In pixels

Bit depth

1 byte

Image depth : Index 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 162: True color image ,8 or 163: Index color images ,1,2,4 or 84: belt α Gray image of channel data ,8 or 166: belt α True color image of channel data ,8 or 16

Comdivssion method

1 byte

Compression method (LZ77 Derivative algorithm )

Filter method

1 byte

Filter method

Interlace method

1 byte

Interlaced scanning method :0: Non interlaced scanning 1: Adam7( from Adam M. Costello Developed 7 The method of interlaced scanning )

Because we are studying on mobile phones PNG, therefore , First of all, let's see MIDP1.0 For the used PNG Picture requirements :

  • stay MIDP1.0 in , We can only use 1.0 Version of PNG picture . also , So PNG There are special requirements for key data blocks :IHDR
  • file size :MIDP Support any size of PNG picture , However , actually , If a picture is too big , Will be unable to read due to memory exhaustion .
  • ColorType/ Color type : All color types are supported , Although the display of these colors depends on the display ability of the actual equipment . meanwhile ,MIDP Can also support alpha passageway , however , be-all alpha Channel information is ignored and treated as opaque colors .
  • Bit depth/ Color depth : All color depths can be supported .
  • Comdivssion method/ Compression method : Only compression mode is supported 0(deflate Compression way ), This sum jar Files are compressed in exactly the same way , therefore ,PNG Image data decompression and jar The same code can be used to decompress the file .( In fact, that's why J2ME Good support PNG The reason for the image :))
  • Filter method/ Filter method : Although in PNG Only methods are defined in the white paper 0, But all 5 Both methods are supported !
  • Interlaced scanning : although MIDP Support 0、1 Two ways , However , When interlacing is used ,MIDP It doesn't really use interlacing to display .
  • PLTE chunk: Support
  • IDAT chunk: Image information must use 5 One of the three filtering methods 0 (None, Sub, Up, Average, Paeth)
  • IEND chunk: When IEND When the data block is found , This PNG Images are considered legal PNG Images .
  • Optional data block :MIDP The following auxiliary data blocks can be supported , However , This is not necessary .

More about , You can refer to http://www.w3.org/TR/REC-png.html

Open a... With a hex viewer PNG file :

The analysis is as follows :

Hexadecimal

explain

00 00 00 0D

Block length 13 byte

49 48 44 52

Block type code “IHDR” Of ASCII Letter

00 00 04 1D

The width of the image 1053

00 00 02 B3

Height of the image 691

08

Image depth 8

06

belt α True color image of channel data

00

Compression method

00

Filter method

00

Interlaced scanning method :00 Non interlaced scanning

52 C3 75 3A

CRC ( Cyclic redundancy detection )

Data blocks - Palette data block PLTE

Palette data block PLTE(palette chunk) Contains color images with index (indexed-color image) Related color transformation data , it Related only to indexed color images , And put it in the image data block (image data chunk) Before .

PLTE Data blocks are palette information that defines an image ,PLTE Can contain 1~256 Palette information , Each palette information from 3 Byte composition :

Color

byte

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 3 Multiple , otherwise , This will be an illegal palette .

For index images , Palette information is required , Color index of palette from 0 Numbered starting , And then there was 1、2……, The number of colors in the palette cannot exceed the number of colors specified in the color depth ( If the color depth of the image is 4 When , The number of colors in the palette cannot exceed 2^4=16), otherwise , This will lead to PNG The image is illegal .

True color images and bands α True color images of channel data can also have palette data blocks , The purpose is to facilitate non true color display program to use it to quantize image data , So that the image .

Open an index image with a hex viewer PNG file :

The analysis is as follows :

Hexadecimal

explain

00 00 00 27

Block length 39 byte

50 4C 54 45

Block type code “PLTE” Of ASCII Letter

B7 00 34 FF 99 00 60 00 73 FF 0F 00 FF ED 00 09 00 B2 FF 66 00 FF 3B 00 E2 00 15 8B 00 54 FF C1 00 33 00 99 FF FF 00

Palette color 13 individual

48 29 75 2C

CRC ( Cyclic redundancy detection )

Preview colors in the palette :

pHYs

Physical pixel data block , It represents the pixel size of the picture , Or aspect ratio , The results are as follows

Pixels per unit, X axis

4 bytes (PNG unsigned integer)

Pixels per unit, Y axis

4 bytes (PNG unsigned integer)

Unit specifier

1 byte

unit specifier Is defined as follows :

  • 0:unit is unknown
  • 1:unit is the metre

Data blocks - Image data block IDAT

Image data block IDAT(image data chunk): It stores the actual data , The data stream may contain a plurality of consecutive image data blocks .

IDAT Store the real data information of the image , therefore , If you can understand IDAT Structure , We can easily generate PNG Images .

Open a... With a hex viewer Index image PNG file :

Hexadecimal

explain

00 00 00 D3

Block length 211 byte

49 44 41 54

Block type code “IDAT” Of ASCII Letter

78 9C ......

Compressed data 211 byte ,LZ77 Derived compression method

52 98 5D 9D

CRC ( Cyclic redundancy detection )

Data blocks - Image end data IEND

Image end data IEND(image trailer chunk): It's used to mark PNG The file or data stream is over , And it must be placed at the end of the file .

If we observe carefully PNG file , We will find that , The end of the document 12 A character always looks like this :

00 00 00 00 49 45 4E 44 AE 42 60 82

It's not hard to understand , Because of the definition of data block structure ,IEND The length of the data block is always 0(00 00 00 00, Except for adding information ), Data identification is always IEND(49 45 4E 44), therefore ,CRC Code is always AE 42 60 82.

IHDR cHRM pHYs IEND

thus , We have been able to learn from a PNG The data blocks are identified in the file . because PNG In addition to key data blocks , Other auxiliary data blocks are optional , therefore , With this standard , We can reduce... By deleting all auxiliary data blocks PNG File size .( Of course , It should be noted that ,PNG The format can save the layers in the image 、 Text and other information , Once these auxiliary data blocks are deleted , The image will lose its original editability .)

After deleting the auxiliary data block PNG file , Now the file size is 147 byte , The original file size is 261 byte , After the file size is reduced , It does not affect the content of the image .

Actually , We can do some interesting things by changing the color value of the palette , For example, realize the cloud / The flow effect of water waves , Achieve the fade in and fade out effect of the image, etc , Here it is , It may be more direct to give you a link to see :http://blog.csdn.net/flyingghost/archive/2005/01/13/251110.aspx, I am inspired by this article .

As mentioned above ,IDAT The data block uses LZ77 Generated by compression algorithm , Due to the limited capacity of mobile phone processor , therefore , If we're generating IDAT Data blocks are still used LZ77 Compression algorithm , Will greatly reduce efficiency , therefore , For efficiency , Only uncompressed LZ77 Algorithm , About LZ77 The specific implementation of the algorithm , This article is not intended to delve into , If you are right about LZ77 Algorithm JAVA Interested in implementing , You can refer to the following two sites :

Reference article :

Introduction to audio and video -11-PNG File format details https://www.cnblogs.com/binglingziyu/p/audio-video-basic-11-png-file-format-detail.html

PNG File structure https://www.cnblogs.com/Yuuki-/p/7868858.html

http://read.newbooks.com.cn/info/173555.html

Reprint This station article 《PNG Document interpretation (2):PNG Interpretation of format file structure and data structure — decode PNG data 》, Please indicate the source :https://www.zhoulujun.cn/html/theory/multimedia/CG-CV-IP/8410.html

copyright:author[Army Zhou],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/20210807220831575r.html