GIF

The GIF (Graphics Interchange Format) is a graphics format using Lemple-Ziv-Welch (LZW) compression. It supports maximally 8-bit images (256 colors or 256 shades of gray). It has two versions: 0,87 and 0,89a. The GIF enables storing of animated gifs; these are individual images displayed at certain intervals. This format is highly widespread, thanks to the Internet, because it enabled a subsequent displaying of an image already after reading of 1/8 of all data, while other formats required the whole file to be read before displaying. However, this format had become over time rather dated, and it was necessary to design a new format (PNG). A reason for this was also the fact that the company (CompuServe Incorporated), which designed it, started to exercise copyright rights after some time.
The basic GIF scheme:

1 GIF Signature  
2 Screen Descriptor
3 Global Color Table
   
4 Image Descriptor
5 Local Color Table
6 Raster Data
   
7 GIF End

The sections 4 through 6 can be repeated, with the number of repetitions being given with the number of images in a GIF file.

GIF Signature and Screen Descriptor :
 

Offset

 Size  Meaning
0-2 3 byte Identification: GIF
3-5 3 byte Release 89a ? July 1989, 87a ? May 1987
6-7 2 byte Width of the image in points
8-9 2 byte Height of the image in points
10 1 byte Global Color Table Flag  1 bit
 Color Resolution   3 bits
 Sort Flag     1 bit
 Size of Global Color Table   3 bits
11 1 byte Background Color Index
12 1 byte Pixel Aspect Ratio
13-? ? Global Color Table  

Details :

Global Color Table Flag (1 bit)

    This flag identifies the Global Color Table. If the flag is on, then this Table follows immediately after the section Screen Descriptor. In addition, this flag represents the color of the background. If the bit is on, then the value of the Background Color Index field is used as the color of the background.

 Values, which it can take :

0 A Global Color Table will not follow; so Background Color Index is not relevant.
1 A Global Color Table follows immediately from the 13th byte, and the Background Color Index is relevant

Color Resolution (3 bits)

    This number represents the number of primary colors in the original picture minus 1. It represents the size of the whole pallet from which we can select colors. For instance, if the value in this field is 3, then the pallet of the original picture has 4 bits, and for every primary color required for representation of a picture. This value identifies the size of the original pallet.

Sort Flag (1 bit)

    Indicates whether the Global Color Table is sorted. If this bit is on, the Global Color Table is sorted. This means: the most frequent colors are prior to those occurring with a smaller frequency. 

This sort flag can be interpreted:
 

0 Not sorted
1 Sorted in accordance with descending importance, the most important color is the first.

Size of Global Color Table (3 bits)

    If this flag is set on 1, then the value in this field is used for calculation of the number of bytes contained in the Global Color Table. It determines the size of the Global Color Table plus 2. If there is no Global Color Table, the decoder (an application program) can select the best graphics mode for displaying graphics data.

Background Color Index (1 byte)

    The Background Color Index for the Global Color Table for color of background. The background color is used for those pixels in the screen whose flags are hidden in this image. If this flag has 0, then this field is zero, and the Background Color Index is not relevant.

Pixel Aspect Ratio (1 byte)

    A factor used in calculating the approximation of the ratio of pixel sides in the original image. If the value according to the Pixel Aspect Ratio is 0, then the approximation starts based on the basis of the following formula:

            Aspect Ratio = (Pixel Aspect Ratio + 15) / 64

    The Pixel Aspect Ratio is defined as the proportion of the width and height of pixels. The value range in this field enables us to specify the widest pixel from 4:1 to the tallest pixel from 1:4 with an increment of 1/64.

Values of Pixel Aspect Ratio :
 

0 No information is given on aspect ratio.
1..255 The value will be used in calculating the Aspect Ratio.

Global Color Table

      The Global Color Table contains a sequence of three bytes representing RGB values in an individual color. This Table is used in an image without colors from a local color table, a Plain Text Extension. Its presence is identified by the flag of the Local Color Table, setting at 1. The table is immediately after the GIF header of the file, and its value is equal to the following.

        3 x 2^(VSize of the Global Color Table + 1).

In every GIF file, at least one Global Color Table must be present.
 
The arrangement of a Global Color Table in a GIB file is as follows:

Offset Color  Color Index
0 Red index 0
1 Green index 0
2 Blue index 0
3 Red index 1
4 Green index 1
5 Blue index 1
6 Red index 2
7 Green index 2
8 Blue index 2
... ... ...
767 Blue index 255

Naturally, the size of the table depends on the colors used. In the case of using a 256-color pallet, the table has 768 values. In the case of 8 colors, the table has 24 values, meaning 24 bytes.

GIF End

    GIF end is identified by hexadecimal value 0x3B (at the end of the file).

Head sample of 256 colors GIF

Head sample of 32 colors GIF

Head sample of 16 colors GIF

Head sample of 8 colors GIF

Head sample of 2 colors GIF


Picture Sample: beginning of the GIF file (8 colors 24x13)

Copyright (c) 1999-2017 Juraj Štugel. www.netgraphics.sk