Every extension starts with the byte 21, then a label that says which one
it is, then its data. Fixed fields go first, as a sub-block with a length byte, and any
further data follows as sub-blocks: length bytes of 1 to 255, ended by a zero-length one,
the block terminator.
| Label | Extension | Length |
|---|---|---|
F9 | Graphic control | 8 bytes |
FE | Comment | varies |
01 | Plain text | varies |
FF | Application | varies |
Graphic control extension
This extension gives the next image its delay, its transparent colour and what becomes of it afterwards. It applies to the first image or plain text block after it, and at most one may come before each of those. Other extensions may sit between it and its target. The extension holds one data sub-block, and its scope ends with that graphic. The clock for the delay starts as soon as the graphic is drawn. The specification asks that disposal method 3 be used sparingly, for small areas, as it makes the decoder store the area; a decoder that cannot is advised to restore to the background colour. The transparent index is present, and used, if and only if the transparent flag is 1: a pixel with that index leaves the display unchanged.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | introducer | 21. |
| 1 | 1 | u8 | label | F9. |
| 2 | 1 | u8 | block size | 4, the fixed bytes that follow. |
| 3 | 1 | u8 | flags | See below. |
| 4 | 2 | u16 | delay | The delay in hundredths of a second before the next frame. 0 means no delay. |
| 6 | 1 | u8 | transparent index | The colour index that stands for transparent, if the flag is set. |
| 7 | 1 | u8 | terminator | 00. |
| Bits | Name | Meaning |
|---|---|---|
| 7–5 | reserved | Should be 0. |
| 4–2 | disposal method | What happens to this frame next: 0 not specified, 1 leave it, 2 restore to background, 3 restore to previous. 4 to 7 are not defined. See Animation. |
| 1 | user input | 1 means processing waits for input from the user (a carriage return, a mouse click, as the application decides), or until the delay ends, whichever comes first. The specification asks encoders not to set it without a delay; without one the decoder should wait indefinitely, and it may sound the bell (07) to alert the user. |
| 0 | transparent colour | 1 if the transparent index is used: pixels of that index leave the canvas unchanged. |
00000019 21 F9 04 00 00 00 00 00
Comment extension
| Offset | Size | Name | Description |
|---|---|---|---|
| 0 | 1 | introducer | 21. |
| 1 | 1 | label | FE. |
| 2 | … | comment | Data sub-blocks holding the text, then the terminator. |
The text is not part of the picture and readers do not draw it. The specification recommends 7-bit ASCII meant for humans, not control information for custom processing. A reader may ignore the comment or keep it, but it must never disturb the decoding. Any number may appear, anywhere a block can begin; the specification recommends the start or the end of the stream so they do not split control and data blocks. A comment has no scope and no extension can modify it.
Plain text extension
Text to draw on the canvas in a fixed grid of character cells. It is a graphic block, so a graphic control extension can precede it. Almost no reader draws it: a built file with one was decoded by Chrome, Pillow, macOS’s ImageIO and ffmpeg, and none drew the text. The explorer lists the grid, its cell size, the two colours with their values and the text, and does not draw it. No real file with a plain text extension was found among more than 32,000 GIFs.
What the specification says about it: the text is 7-bit printable ASCII, drawn as mono-spaced characters, one per cell, from the upper left cell left to right and top to bottom, until the data ends or the grid is full. The grid is a whole number of cells; fractional cells are discarded, so encoders must size the grid exactly. The block needs a global colour table (or one saved from an earlier stream) for its two colours, and a graphic control extension can modify it. Font and size are left to the decoder. The data is taken as preformatted; a character below 20 or above F7 should be shown as a space. For the widest compatibility the cell should be about 8 × 8 or 8 × 16; anything unusual is better as an image. Any number of these blocks may appear.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | introducer | 21. |
| 1 | 1 | u8 | label | 01. |
| 2 | 1 | u8 | block size | 12. |
| 3 | 2 | u16 | text grid left | The canvas column of the grid. |
| 5 | 2 | u16 | text grid top | The canvas row of the grid. |
| 7 | 2 | u16 | text grid width | The grid’s width in pixels. |
| 9 | 2 | u16 | text grid height | The grid’s height in pixels. |
| 11 | 1 | u8 | cell width | The width of a character in pixels. |
| 12 | 1 | u8 | cell height | The height of a character in pixels. |
| 13 | 1 | u8 | foreground | The global colour table index of the text. |
| 14 | 1 | u8 | background | The global colour table index behind the text. |
| 15 | … | text | Data sub-blocks of the characters, then the terminator. |
Application extension
Data for a particular application, identified by name, so that other readers can skip it. The identifier is eight printable ASCII characters naming the application; the authentication code is three bytes by which a program may prove the extension is its own. The block has no scope and no extension can modify it. The specification recommends that an application embedding GIF define its own block around the whole stream instead, since application extensions are overhead for every other reader. The cover sheet of the specification says developers could register identifiers in a courtesy directory kept by CompuServe’s PICS forum; it was not official and guaranteed nothing.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | introducer | 21. |
| 1 | 1 | u8 | label | FF. |
| 2 | 1 | u8 | block size | 11. |
| 3 | 8 | ASCII | identifier | Eight characters naming the application. |
| 11 | 3 | authentication | Three bytes the application can use to check the extension. | |
| 14 | … | data | Data sub-blocks that only the application defines, then the terminator. |
Looping: NETSCAPE2.0
GIF89a has no field that says an animation should repeat. Netscape Navigator 2.0
added it with an application extension, and it is now what almost all readers
look for. The identifier and code spell NETSCAPE2.0 and it holds one
sub-block of 3 bytes: the byte 1, then a 16-bit loop count.
21 FF 0B 4E 45 54 53 43 41 50 45 32 2E 30 03 01 00 00 00
A loop count of 0 means loop forever. A count of n means
the animation plays n further times after the first (so n + 1 plays
in all). Checked against Firefox’s source (FrameAnimator.cpp: the counter is
set to n after the first full pass and counts down once per further pass).
The extension is not part of either GIF specification.
Some writers add a second sub-block that starts with the byte 2, a buffering extension; its layout is unverified here (no source opened).
Other programs write the same
block as ANIMEXTS1.0, which the explorer reads the same way.
XMP metadata
Adobe stores XMP as an application extension with the identifier XMP Data
and code XMP, but not in sub-blocks: the UTF-8 packet comes straight after
the identifier, with no length bytes in it. To let a reader that does not know this
walk past it as if it were sub-blocks, a 258-byte magic trailer follows the packet:
01, then FF down to 00, then 00. A
reader taking the packet’s bytes for length bytes lands somewhere in the trailer, and
the ramp carries it to a real terminator.
ICC colour profiles
An ICC profile is stored as an application extension with the identifier
ICCRGBG1012 (ICCRGBG1 and code 012), and the
profile in ordinary data sub-blocks.
Other extensions
A reader that does not know a label must still skip the extension, by reading its sub-blocks to the terminator. The same applies to any application extension it does not recognise.
The specification divides the labels into three ranges, so that a decoder can tell
what scope an extension it does not know has. Labels 00 to 7F
are graphic rendering blocks (the plain text extension, 01, is one); labels
80 to F9 are control blocks, which apply to the graphic
rendering block that follows them (the graphic control extension, F9, is one);
labels FA to FF are special purpose blocks, which have no
effect on the scope of the others (the comment, FE, and application,
FF, extensions are two). Among more than 32,000 real GIF files none held an
extension with any other label, so the explorer’s handling of one was checked only on
built files.
Not described
What this page leaves out, and what the explorer does not do with extensions.
Known, not yet covered
- Reference: the application extensions other than NETSCAPE2.0 (and ANIMEXTS1.0), ICC and XMP that real files here carry: ImageMagick, ADOBE:IR1.0 (Photoshop image resources), MGKIPTC0000 (IPTC), GIFCONnb1.0, MSOFFICE9.0 and PIANYGIF1.0. Open source readers such as ImageMagick handle several of them.
- Reference: the NETSCAPE2.0 buffering sub-block: browser or giflib source would settle it; not checked yet.
- Reference: how the text of a plain text extension should be drawn (font, colours, clipping): the GIF89a specification defines the grid but leaves the drawing open. No real file here has the extension; it was checked on built files only.
- Viewer: plain text is listed with its grid, cell size, colours and text but is not drawn; no decoder tried (Chrome, Pillow, ImageIO, ffmpeg) draws it.
- Viewer: an ICC profile (ISO 15076) is joined from its sub-blocks and offered as a file; its contents are not interpreted or applied to the colours. XMP is offered as a file and not parsed.
- Viewer: the user input flag is shown and not acted on; the animation is played by the browser.
- Viewer: application extensions other than those above are shown as sub-block bytes with the identifier and size.
Unknown
- Reference and viewer: extension labels other than F9, FE, 01 and FF: the specification gives only the label ranges and none of the 32,958 real files has one. The viewer shows the label and the sub-blocks as labelled bytes.
- Reference and viewer: a malformed XMP block (two real files write the packet without its start or without the magic trailer): no source describes it. The viewer shows plain sub-blocks.