文件头判断WEBP是否是动画

https://developers.google.com/speed/webp/docs/riff_container

要给rape.js增加判断WEBP是否为动画的功能。

根据Google官方的文档,动态WEBP属于Extended File Format,其文件头为:

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                   WebP file header (12 bytes)                 |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                      ChunkHeader('VP8X')                      |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|Rsv|I|L|E|X|A|R|                   Reserved                    |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|          Canvas Width Minus One               |             ...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

...  Canvas Height Minus One    |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

换算公式:1 byet = 2 hex = 8 bit

12字节WEBP文件头:RIFF **** WEBP (****=文件大小)

8字节ChunkHeader('VP8X'):VP8X ****(****=文件大小)

1字节(8bit)文件属性:

Reserved (Rsv): 2 bits

    SHOULD be 0.

ICC profile (I): 1 bit

    Set if the file contains an ICC profile.

Alpha (L): 1 bit

    Set if any of the frames of the image contain transparency information ("alpha").

EXIF metadata (E): 1 bit

    Set if the file contains EXIF metadata.

XMP metadata (X): 1 bit

    Set if the file contains XMP metadata.

Animation (A): 1 bit

    Set if this is an animated image. Data in 'ANIM' and 'ANMF' chunks should be used to control the animation.

Reserved (R): 1 bit

    SHOULD be 0.

后面Reserved开始部分就忽略了。

最终我一共要从WEBP读取21字节文件头,跳过前20字节。

最后1字节分解为8位bit(ASCII),从中取出倒数第二位,判断是否为动画即可。

 // 1 byet = 2 hex = 8 bit

52494646 56b20100 57454250 56503820 4ab20100 90 4a039d012aa80158023e250c8544a1884fc7ec0c01225b1a46662db46f

52494646 d6910000 57454250 56503858 0a000000 12 0000008f01008f0100414e494d06000000000000ff0000414e4d463e14

4B       4B       4B       V P 8 X  4B       R
C

不明觉厉

1