GIF image format
File
Bytes are in little-endian order.
GCT is Global Colour Table.
Header
Purpose | Example | Description |
---|---|---|
Header | 47 49 46 38 37 61 | The string ‘GIF87a’ in ASCII. |
Logical screen descriptor
Purpose | Example | Description |
---|---|---|
Width | 03 00 | Width of logical screen in pixels. |
Height | 05 00 | Height of logical screen in pixels. |
Colour fields | ba | Parameters of the global colour table. |
Background | 00 | Palette index of background colour. |
Aspect ratio | 00 | Pixel 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:
Bit | Purpose | Description |
---|---|---|
0x7 | Global colour table flag | If set, a global colour table follows the logical screen descriptor. |
0x6 | Colour resolution | Number of bits per channel of the original colour data, minus 1. |
0x5 | continued | continued |
0x4 | continued | continued |
0x3 | Sort flag | If set, indicates that the global colour table is sorted in descending order of frequency. |
0x2 | Size of global colour table | The global colour table has colours equal to one greater than this value, to the power of 2. |
0x1 | continued | continued |
0x0 | continued | continued |
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.
Purpose | Example | Description |
---|---|---|
Image separator | 2C | Marks the start of image data. |
Image left position | 00 00 | Coordinate of left column of the image. |
Image top position | 00 00 | Coordinate of top row of the image. |
Image width | 00 00 | Width of image in pixels. |
Image height | 00 00 | Height of image in pixels. |
Image fields | 00 | Parameters of the image. |
The image fields value comprises five packed fields, as follows:
Bit | Purpose | Description |
---|---|---|
0x7 | Local colour table flag | If set, a local colour table follows this image descriptor. |
0x6 | Interface flag | If set, the image is interlaced in a four-pass interlace pattern. |
0x5 | Sort flag | If set, indicates that the local colour table is sorted in descending order of frequency. |
0x4 | Reserved | Reserved. |
0x3 | continued | continued |
0x2 | Size of local colour table | The local colour table has colours equal to one greater than this value, to the power of 2. |
0x1 | continued | continued |
0x0 | continued | continued |
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
Purpose | Example | Description |
---|---|---|
Trailer | 3B | Marks the end of the file. |