Detailed explanation of png file format, get the modification time of the file

http://dev.gameres.com/Program/Visual/Other/PNGFormat.htm
http://www.360doc.com/content/11/0428/12/1016783_112894280.shtml
https://www.cnblogs.com/lidabo/p/3701197.html
http://blog.csdn.net/bisword/article/details/2777121
http://blog.csdn.net/hherima/article/details/45847043
http://twt35twt.iteye.com/blog/1351626

http://blog.csdn.net/Blues1021/article/details/45007943 (good)
http://blog.csdn.net/u014646950/article/details/51144476

code a png file, roughly understand the format of the png file

For a PNG file, the file header is always described by fixed-bit bytes:

Decimal 137 80 78 71 13 10 26 10
Hexadecimal 89 50 4E 47 0D 0A 1A 0A

The first byte 0x89 is beyond the range of ASCII characters, this is to prevent some software from treating PNG files as text files.
The rest of the file consists of more than 3 PNG data blocks (Chunks) in a specific order. Therefore, a standard PNG file structure should be as follows:

PNG file logo|PNG data block|...|PNG data block

PNG data block (Chunk)
PNG defines two types of data blocks,
one is called critical data block (critical chunk), which is a standard data block, and the
other is called auxiliary data block (ancillary chunks), which is Optional data block.


The key data block defines 4 standard data blocks, which must be included in each PNG file, and the PNG reading and writing software must also support these data blocks.
Although the PNG file specification does not require PNG codecs to encode and decode optional data blocks, the specification advocates support for optional data blocks.

The following table is the categories of data blocks in PNG, among which, we use (!) to distinguish the key data blocks.
Data chunks in PNG file format

Data chunks Symbol Data chunk names Multiple chunks Optional no Location constraints
IHDR(!) File header chunks NO First
cHRM base colour and white point chunks Are before PLTE and IDAT
gAMA images gamma data block is before PLTE and IDAT
sBIT sample significand data block is before PLTE and IDAT
PLTE(!) palette data block is before
IDAT bKGD background color data block is after PLTE and before
IDAT hIST Whether the image histogram data block is after PLTE and before
IDAT tRNS Whether the image transparency data block is after PLTE and before
IDAT oFFs (private common data block) Whether it is pHYs before
IDAT whether the physical pixel size data block is before
IDAT sCAL (private common data block) no is before
IDAT IDAT(!) image data block is continuous with other IDAT
tIME image last modified time data block is unlimited
tEXt text information data block is unlimited
zTXt compressed text data block is unlimited
fRAc (Private Common Data Block) Yes Yes Unlimited
gIFg (Private Common Data Block) Yes Yes Unlimited
gIFt (Private Common Data Block) Yes Yes Unlimited
gIFx (Private Common Data Block) Yes Yes Unlimited
IEND(!) Image end data No No last data block

Here to add an iCCP

For the sake of simplicity, we assume that in the PNG file we use, these 4 data blocks are stored in the above order and only appear once.

Data block structure:
In the PNG file, each data block consists of 4 parts, as follows:

Name byte description
Length (length) 4 bytes specify 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 data block type code Composed of ASCII letters (AZ and az)
Chunk Data (data block data) Variable-length storage data CRC (Cyclic Redundancy Check) specified by Chunk Type Code
4-byte storage for error cyclic redundancy code

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

IHDR
file header data block IHDR (header chunk): It contains the basic information of the image data stored in the PNG file, and should appear in the PNG data stream as the first data block, and there can only be one PNG data stream. File header data block.
The file header data block consists of 13 bytes, and its format is shown in the following table.

Field Name Bytes Description
Width 4 bytes Image width in pixels
Height 4 bytes Image height in pixels

Bit depth 1 byte Image depth:
Indexed color image: 1, 2, 4 or 8
Grayscale image: 1, 2, 4 , 8 or 16 Truecolor
image: 8 or 16

ColorType 1 byte ColorType:
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: With alpha channel True color image of data, 8 or 16

Compression method 1 byte compression method (LZ77 derived algorithm)

Filter method 1 byte Filter method

Interlace method 1 byte Interlacing method:
0: Non-interlacing
1: Adam7 (7-pass interlacing method developed by Adam M. Costello)

PLTE
palette data block PLTE (palette chunk) contains the color transformation data related to the indexed-color image (indexed-color image), it is only related to the indexed color image, and should be placed before the image data block (image data chunk) .

Color byte meaning
Red 1 byte 0 = black, 255 = red
Green 1 byte 0 = black, 255 = green
Blue 1 byte 0 = black, 255 = blue

For indexed images, the palette information is required, the color index of the palette starts from 0, then 1, 2..., the number of colors in the palette cannot exceed the number of colors specified in the color depth
(such as the image color When the depth is 4, the number of colors in the palette cannot exceed 2^4=16), otherwise, this will make the PNG image illegal.
Truecolor images and truecolor images with alpha channel data can also have palette data blocks, which are used by non-truecolor display programs to quantify the image data and display the image.

IDAT
image data block IDAT (image data chunk): It stores the actual data, and the data stream can contain multiple consecutive image data blocks.
IDAT stores the real data information of the image, so if we can understand the structure of IDAT, we can easily generate PNG images.


IEND
image end data IEND (image trailer chunk): It is used to mark the end of the PNG file or data stream, 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:
00 00 00 00 49 45 4E 44 AE 42 60 82
It is not difficult to understand that due to the definition of the data block structure, the IEND data The length of the block is always 0 (00 00 00 00, unless the information is added artificially), the data identifier is always IEND (49 45 4E 44), therefore, the CRC code is always AE 42 60 82

After getting a png file, you can put the auxiliary


Since PNG stipulates that except key data blocks, other auxiliary data blocks are optional parts, therefore, with this standard, we can reduce the size of PNG files by deleting all auxiliary data blocks in the png file, and
only keep IHDR, PLTE, IDAT and IEND data blocks.
(Of course, it should be noted that the PNG format can save the layers, text and other information in the image. Once these auxiliary data blocks are deleted, the image will lose its original editability.)


Because calculating the CRC takes some time, it is generally negligible for blocks with fewer bytes. For example, CRC check is performed on the IDAT data block.

==================================

Analysis of tIME:

According to the composition rules of data blocks, it can be known that

The data stored in the selected 7 bytes is the modification time of the file

The approximate storage method of guess time is:

1, 2 bytes for the year 0x070xE1 for 2017

3 bytes for month 0x04 means April

4 bytes are the day 0x05 means the 5th

When 5 bytes are 0x0A means 10

6 bytes are points 0x1E means 30 points

7 bytes are seconds 0x2C means 44 seconds

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325883990&siteId=291194637
Recommended