gif

How to determine whether a GIF image contains a graphic control extension


The 1990 CompuServe GIF89a specification defines a "graphic control extension" like so:

The Graphic Control Extension contains parameters used when processing a graphic rendering block. The scope of this extension is the first graphic rendering block to follow. The extension contains only one data sub-block.

This block is OPTIONAL; at most one Graphic Control Extension may precede a graphic rendering block. This is the only limit to the number of Graphic Control Extensions that may be contained in a Data Stream.

As this block is optional, how does one determine whether or not an arbitrary GIF image contains a graphics control extension?


Solution

  • As it turns out, the specification pretty plainly states that all extensions, including the Graphic Control, Plain Text, Comment, and Application extensions, are demarcated by the presence of a 0x21 at the start of the block.

    What threw me off is that GIFs contain a flag located in the Logical Screen Descriptor that denotes the presence of an optional Global Color Table block, but there is no such flag to denote the presence of a graphic control extension (not sure why). It either comes right after the Logical Screen Descriptor or it doesn't. Hope this helps someone down the road...