簡(jiǎn)要
兼容png格式,在png格式的基礎(chǔ)上加入額外的信息描述動(dòng)畫序列,在不支持apng的地方可以顯示第一幀內(nèi)容。幀間無(wú)損壓縮,有效降低存儲(chǔ)大小。
結(jié)構(gòu)
APNG與PNG相同,由若干個(gè)Chunk組成。每個(gè)Chunk的結(jié)構(gòu)如下:
Length(4B) + Type(4B) + Data + CRC(4B)
(refs: https://www.w3.org/TR/PNG/#5Chunk-layout)
Type 的四個(gè)字節(jié),分別為四個(gè)英文字母,大小寫分別代表不同的意義,除文檔已定義名字外可以自由命名(如 oRIg)
- 1字母:大寫/小寫 重要數(shù)據(jù)/附加數(shù)據(jù)
- 2字母:大寫/小寫 標(biāo)準(zhǔn)有定義的類型/自定義類型
- 3字母:大寫/小寫 未使用
- 4字母:大寫/小寫 非復(fù)制安全/復(fù)制安全 (對(duì)于編輯器來(lái)說(shuō)復(fù)制安全的數(shù)據(jù)可以直接復(fù)制)
Data 長(zhǎng)度為 Length
一般的PNG結(jié)構(gòu)形如:
PNGSignature(8B) + IHDR(25B) + IDAT + IEND(12B)
PNGSignature:固定值 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0aIHDR:PNG信息IDAT:圖像數(shù)據(jù)IEND:結(jié)束標(biāo)識(shí),數(shù)據(jù)長(zhǎng)度為0
`IHDR數(shù)據(jù)結(jié)構(gòu)`
Width 4 bytes
Height 4 bytes
Bit depth 1 byte
Colour type 1 byte
Compression method 1 byte
Filter method 1 byte
Interlace method 1 byte
(refs: https://www.w3.org/TR/PNG/#11IHDR)

png例子-頭部

png例子-尾部
一般的APNG結(jié)構(gòu)形如:
PNGSignature(8B) + IHDR(25B) + acTL + fcTL + IDAT + [ fcTL + fdAT ... ] + IEND(12B)
其中
第一幀:fcTL + IDAT
后續(xù)幀:fcTL + fdAT
acTL:動(dòng)畫控制塊(Animation Control Chunk),記錄總幀數(shù)和循環(huán)次數(shù),其中循環(huán)次數(shù)0表示無(wú)限循環(huán)fcTL:幀控制塊(Frame Control Chunk),記錄每一幀的信息,fdAT:幀數(shù)據(jù)塊(Frame Data Chunk),每幀圖像數(shù)據(jù)
`acTL數(shù)據(jù)結(jié)構(gòu)`
byte
0 num_frames (unsigned int) Number of frames
4 num_plays (unsigned int) Number of times to loop this APNG. 0 indicates infinite looping.
`fcTL數(shù)據(jù)結(jié)構(gòu)`
byte
0 sequence_number (unsigned int) Sequence number of the animation chunk, starting from 0
4 width (unsigned int) Width of the following frame
8 height (unsigned int) Height of the following frame
12 x_offset (unsigned int) X position at which to render the following frame
16 y_offset (unsigned int) Y position at which to render the following frame
20 delay_num (unsigned short) Frame delay fraction numerator
22 delay_den (unsigned short) Frame delay fraction denominator
24 dispose_op (byte) Type of frame area disposal to be done after rendering this frame
25 blend_op (byte) Type of frame area rendering for this frame
`fdAT數(shù)據(jù)結(jié)構(gòu)`
0 sequence_number unsigned int Sequence number of the animation chunk, starting from 0.
4 frame_data X bytes Frame data for this frame.
(refs: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/APNG)

apng例子-acTL(num_frames=8, num_plays=0)
相關(guān)資料
https://www.w3.org/TR/PNG - png格式文檔
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/APNG - apng格式文檔
https://wiki.mozilla.org/APNG_Specification - apng格式文檔
https://en.wikipedia.org/wiki/APNG
https://baike.baidu.com/item/APNG/3582613
https://aotu.io/notes/2016/11/07/apng/index.html - 寫得不錯(cuò)的apng介紹文章