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:
- Byte 0: Image Separator (
0x2C) - Bytes 1–2: Image Left Position
- Bytes 3–4: Image Top Position
- Bytes 5–6: Image Width
- Bytes 7–8: Image Height
- Byte 9: Packed Fields (Local Color Table configuration and interlacing)
Encoding Width and Height
Image dimensions are defined across four contiguous bytes, from Byte 5 through Byte 8:
- Image Width (Bytes 5 and 6): Specifies the width of the image in pixels.
- Image Height (Bytes 7 and 8): Specifies the height of the image in pixels.
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:
- The decimal value
800is represented in hexadecimal as0x0320. - The least significant byte is
0x20. - The most significant byte is
0x03. - 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:
- Image Left Position (Bytes 1–2): The column number, in pixels, of the left edge of the image relative to the left edge of the Logical Screen.
- Image Top Position (Bytes 3–4): The row number, in pixels, of the top edge of the image relative to the top edge of the Logical Screen.
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.