first 8 bytes of a PNG
89 50 4e 47 0d 0a 1a 0a
- 4 bytes for data size
- 4 bytes for type code
- data
- 4 byte CRC
for type code the 5th bit of each byte -> upper/lower
- Ancillary bit : 0 (uppercase) = critical, 1 (lowercase) = ancillary.
- Private bit : 0 (uppercase) = public, 1 (lowercase) = private.
- Reserved bit : Must be 0 (uppercase) in files conforming to this version of PNG.
- Safe-to-copy bit : 0 (uppercase) = unsafe to copy, 1 (lowercase) = safe to copy. (1, 2, 3, 4)
The IHDR chunk must appear FIRST. It contains(bytes):
- Width : 4
- Height : 4
- Bit depth : 1
- Color type : 1
- Compression method : 1
- Filter method : 1
- Interlace method : 1
IDAT
解码: 先LZ77 每一行第一位定义filter type,
LZ77 uncompress后总共有height*width*pixelSize + height bytes
执行filter Algorithm
An additional "filter-type" byte is added to the beginning of every scanline (see Filtering). The filter-type byte is not considered part of the image data, but it is included in the datastream sent to the compression step.
end of image 00 00 00 00 49 45 4e 44 ae 42 60 82
gAMA Image gamma
cHRM Primary chromaticities
sRGB Standard RGB color space
iCCP Embedded ICC profile
tEXt Textual data
zTXt Compressed textual data
iTXt International textual data
bKGD Background color
pHYs Physical pixel dimensions
sBIT Significant bits
sPLT Suggested palette
hIST Palette histogram
tIME Image last-modification time
Filtering algorithms are applied to bytes,
filter types
- 0 None
- 1 Sub
- 2 Up
- 3 Average
- 4 Paeth
for decoder suggest An un-known chunk type is not to be treated as an error unless it is a critical chunk.