How GIF Image Descriptor Defines Dimensions

This article explains how individual frame dimensions and spatial coordinates are structured inside the Image Descriptor block of a GIF file. Readers will learn the exact byte offsets, data types, and endianness used to define an image's width and height, as well as how these parameters correlate with the overall canvas size defined in the Logical Screen Descriptor.

The GIF specification (both GIF87a and GIF89a) uses the Image Descriptor block to introduce the rendering parameters for an individual image or animation frame. Every Image Descriptor is exactly 10 bytes long and begins with the Image Separator byte, defined as hexadecimal 0x2C (ASCII comma ,).

The Byte Layout of the Image Descriptor

The 10 bytes of the Image Descriptor are allocated as follows:

Encoding Width and Height

Image dimensions are defined across four contiguous bytes, from Byte 5 through Byte 8:

Both values are stored as unsigned 16-bit integers (words). This allows for a theoretical maximum resolution of 65,535 × 65,535 pixels per image block.

Endianness: Little-Endian Format

The GIF specification mandates little-endian byte ordering for all multi-byte values. This means the least significant byte (LSB) precedes the most significant byte (MSB).

To decode the dimension value: \[\text{Dimension} = \text{Byte}_1 + (\text{Byte}_2 \times 256)\]

Example: If an image frame has a width of 800 pixels:

  1. The decimal value 800 is represented in hexadecimal as 0x0320.
  2. The least significant byte is 0x20.
  3. The most significant byte is 0x03.
  4. In the file stream at Bytes 5 and 6, the width is written as 20 03.

Spatial Placement and Offsets

Before the width and height fields, Bytes 1 through 4 define the frame's origin point relative to the global canvas:

Like the dimensions, these offsets are 16-bit unsigned little-endian integers.

Relation to the Logical Screen Descriptor

The Image Descriptor dimensions do not necessarily equal the dimensions of the entire GIF. The canvas boundary is defined earlier in the file within the Logical Screen Descriptor.

In single-frame images, the Image Descriptor's width and height usually match the Logical Screen's dimensions, with the left and top positions set to 0. However, in animated GIFs, individual frames can be smaller than the overall canvas. Sub-frame dimensions allow the file to update only the specific region of the screen that changes from one frame to the next, reducing the overall file size. The boundaries defined by the Image Descriptor (Left + Width, Top + Height) must not exceed the dimensions established by the Logical Screen Descriptor.