GIF Image Descriptor Packed Fields Bit Structure
In the Graphics Interchange Format (GIF) specification—both GIF87a and GIF89a—the Image Descriptor block introduces each individual image frame. This block contains a critical one-byte configuration field known as the Packed Fields byte, located at offset 9 of the 10-byte descriptor. This article details the specific 8-bit layout of this byte, breaking down how each bit controls local color palettes, interlacing schemes, and palette sizes.
Image Descriptor Overview
The Image Descriptor consists of 10 consecutive bytes arranged as follows:
- Byte 0: Image Separator (
0x2C) - Bytes 1–2: Image Left Position (16-bit unsigned integer, little-endian)
- Bytes 3–4: Image Top Position (16-bit unsigned integer, little-endian)
- Bytes 5–6: Image Width (16-bit unsigned integer, little-endian)
- Bytes 7–8: Image Height (16-bit unsigned integer, little-endian)
- Byte 9: Packed Fields (1 byte)
Bit Structure of the Packed Fields Byte
The Packed Fields byte assigns distinct operational flags and values across its 8 bits, read from most significant bit (Bit 7) to least significant bit (Bit 0):
Bit: 7 6 5 4 3 2 1 0
Field: M I S R R <-- N -->
Bit 7: Local Color Table Flag (M)
- Value 0: No Local Color Table follows this descriptor. The frame relies on the Global Color Table defined in the Logical Screen Descriptor.
- Value 1: A Local Color Table immediately follows this Image Descriptor and precedes the image raster data.
Bit 6: Interlace Flag (I)
- Value 0: The image data is sequentially arranged from top to bottom.
- Value 1: The image data is encoded using a
four-pass interlacing scheme:
- Pass 1: Every 8th row, starting with row 0.
- Pass 2: Every 8th row, starting with row 4.
- Pass 3: Every 4th row, starting with row 2.
- Pass 4: Every 2nd row, starting with row 1.
Bit 5: Sort Flag (S)
- Value 0: The color palette entries are not ordered by importance.
- Value 1: The Local Color Table entries are sorted in order of decreasing frequency of use to assist decoders with limited color hardware.
Bits 4–3: Reserved (R)
- These 2 bits are reserved for future specification use. Decoders
typically ignore these bits, and encoders should set them to
00.
Bits 2–0: Size of Local Color Table (N)
- A 3-bit unsigned integer representing the exponent used to calculate the number of RGB triplets in the Local Color Table.
- If Bit 7 is set to
1, the total number of colors in the local palette is computed using the formula: \[\text{Palette Entries} = 2^{(N + 1)}\] - The total byte size of the Local Color Table in the file stream is: \[\text{Table Byte Size} = 3 \times 2^{(N + 1)}\]
- Bit value mappings:
000(\(0\)): 2 colors (6 bytes)001(\(1\)): 4 colors (12 bytes)010(\(2\)): 8 colors (24 bytes)011(\(3\)): 16 colors (48 bytes)100(\(4\)): 32 colors (96 bytes)101(\(5\)): 64 colors (192 bytes)110(\(6\)): 128 colors (384 bytes)111(\(7\)): 256 colors (768 bytes)
If Bit 7 is set to 0, the value of Bits 2–0 is ignored
by standard decoders.