这些字节有什么作用?

What do these bytes do?

这是在 Gimp 中制作并以最少信息导出的黑色 1x1 PNG 的十六进制转储:

89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
00 00 00 01 00 00 00 01 08 02 00 00 00 90 77 53
DE 00 00 00 0C 49 44 41 54 08 D7 63 60 60 60 00
00 00 04 00 01 27 34 27 0A 00 00 00 00 49 45 4E
44 AE 42 60 82

现在读完 the specification 我很确定其中大部分是什么意思,除了 IHDR 和 IDAT 块之间的字节 30-34:90 77 53 DE

谁能教教我?

这些数字是前一个块的 CRC 校验和。见 the official specification: 5 Datastream structure for a general overview, and in particular 5.3 Chunk layout.

计算 CRC,并将其附加到每个单独的块:

A four-byte CRC (Cyclic Redundancy Code) calculated on the preceding bytes in the chunk, including the chunk type field and chunk data fields, but not including the length field. The CRC can be used to check for corruption of the data. The CRC is always present, even for chunks containing no data.

这是您的 1x1 像素图像,逐字节注释。在 IHDRIDATIEND 每个块的数据之后是前面数据的 CRC。

File: test.png
89 50 4E 47 0D 0A 1A 0A
  Header 0x89 "PNG" CR LF ^Z LF checks out okay
===========
00 00 00 0D
49 48 44 52
00 00 00 01 00 00 00 01 08 02 00 00 00
90 77 53 DE
  block:  "IHDR", 13 bytes [49484452]
  Width:              1
  Height:             1
  Bit depth:          8
  Color type:         2 = Color
  (Bits per pixel: 8)
  (Bytes per pixel: 3)
  Compression method: 0
  Filter method:      0
  Interlace method:   0 (none)
  CRC: 907753DE
===========
00 00 00 0C
49 44 41 54
08 D7 63 60 60 60 00 00 00 04 00 01
27 34 27 0A
  block:  "IDAT", 12 bytes [49444154]
  expanded result: 4 (as expected)
  (Row   0 Filter:0)
  decompresses into
  00 00 00 00
  CRC: 2734270A
===========
00 00 00 00
49 45 4E 44
AE 42 60 82
  block:  "IEND", 0 bytes [49454E44]
  CRC: AE426082

IDAT数据解压缩为四个0:第一个是行过滤器(0,表示'none'),接下来的3个字节是一个像素的红色、绿色、蓝色值。