【速成】APNG - 格式結(jié)構(gòu)詳解

簡(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, 0x0a
  • IHDR: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介紹文章

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容