GIF image format

File

Bytes are in little-endian order.

GCT is Global Colour Table.

PurposeExampleDescription
Header47 49 46 38 37 61The string ‘GIF87a’ in ASCII.

Logical screen descriptor

PurposeExampleDescription
Width03 00Width of logical screen in pixels.
Height05 00Height of logical screen in pixels.
Colour fieldsbaParameters of the global colour table.
Background00Palette index of background colour.
Aspect ratio00Pixel aspect ratio.

The background value signifies the palette index of the colour to use for areas of the image for which there is no pixel data.

An aspect ratio of 0 signifies a pixel aspect ratio of 1:1.

The colour fields value comprises four packed fields, as follows:

BitPurposeDescription
0x7Global colour table flagIf set, a global colour table follows the logical screen descriptor.
0x6Colour resolutionNumber of bits per channel of the original colour data, minus 1.
0x5continuedcontinued
0x4continuedcontinued
0x3Sort flagIf set, indicates that the global colour table is sorted in descending order of frequency.
0x2Size of global colour tableThe global colour table has colours equal to one greater than this value, to the power of 2.
0x1continuedcontinued
0x0continuedcontinued

Global colour table

Contains a series of RGB triplets, eight bits per channel, one triplet for each colour in the table.

Image descriptor

There is one image descriptor per image within the file.

PurposeExampleDescription
Image separator2CMarks the start of image data.
Image left position00 00Coordinate of left column of the image.
Image top position00 00Coordinate of top row of the image.
Image width00 00Width of image in pixels.
Image height00 00Height of image in pixels.
Image fields00Parameters of the image.

The image fields value comprises five packed fields, as follows:

BitPurposeDescription
0x7Local colour table flagIf set, a local colour table follows this image descriptor.
0x6Interface flagIf set, the image is interlaced in a four-pass interlace pattern.
0x5Sort flagIf set, indicates that the local colour table is sorted in descending order of frequency.
0x4ReservedReserved.
0x3continuedcontinued
0x2Size of local colour tableThe local colour table has colours equal to one greater than this value, to the power of 2.
0x1continuedcontinued
0x0continuedcontinued

Table based image data

The clear code value is equal to 2 to the power of the code size. For a code size of 4, the clear code will be 0b10000. The first code of a data stream should be a clear code.

End of Information code is <Clear code>+1, and must be the final code emitted.

The first available compression code value is <Clear code>+2.

Output codes are variable length, starting at <Code size>+1 bits per code, up to 12 bits per code. Code length is increased by 1 whenever the code value would exceed the current code length.

After the output data stream has been created, it is grouped into blocks of up to 255 bytes each for output.

Trailer

PurposeExampleDescription
Trailer3BMarks the end of the file.