Understanding GIF Header Color Resolution Bit Depth

This article explains how the Graphics Interchange Format (GIF) specifies the color resolution bit depth inside the packed fields byte of its Logical Screen Descriptor. It covers the specific bitwise layout defined in the GIF87a and GIF89a specifications, details the mathematical formula used to determine bits per color channel, and clarifies how this field differs from the actual size of the Global Color Table.

The Logical Screen Descriptor

In the GIF file format, the Logical Screen Descriptor immediately follows the 6-byte header signature (GIF87a or GIF89a) and the screen dimensions. Located at byte offset 10 (the eleventh byte of the file), there is a single byte designated as the Packed Fields. This byte consolidates multiple configuration flags and values into individual bit ranges to conserve space.

Bit Allocation of the Packed Fields Byte

The Packed Fields byte is mapped from Most Significant Bit (MSB, Bit 7) to Least Significant Bit (LSB, Bit 0) as follows:

Extracting and Calculating Color Resolution

The color resolution occupies bits 4 through 6. To extract this 3-bit value programmatically, shift the packed byte right by 4 bits and apply a bitwise AND mask with 0x07 (binary 00000111):

\[\text{Raw Value} = (\text{Packed Byte} \gg 4) \ \& \ 0x07\]

The 3-bit binary value yields an integer ranging from 0 to 7. According to the official GIF specification, this raw value does not represent the literal bit depth directly; instead, it is an index where the actual color resolution in bits per primary color is computed as:

\[\text{Bits Per Color} = \text{Raw Value} + 1\]

Because the field can store values from 0 to 7, the calculated bit depth ranges from 1 to 8 bits per primary color component (Red, Green, Blue):

Purpose and Distinction from the Color Table Size

The Color Resolution field describes the capabilities of the original source hardware or color palette used to create the image, indicating how many bits of color precision were available per RGB channel before quantization.

It is functionally independent of Bits 0–2, which dictate the Size of the Global Color Table. While Bits 0–2 specify the total number of colors actually present in the palette (\(2^{\text{Size} + 1}\), up to 256 palette entries), the Color Resolution field in Bits 4–6 informs the decoder of the color depth of the primary colors used to render that palette. In modern decoders, this field is largely informational and rarely affects the rendering pipeline, as nearly all contemporary systems default to 8 bits per channel (24-bit true color).