How GIF Decoders Read Pixel Aspect Ratio
The Graphics Interchange Format (GIF) specification includes a dedicated byte in its header to define the shape of individual pixels, enabling displays with non-square pixels to render images correctly. A GIF decoder interprets this pixel aspect ratio byte located in the Logical Screen Descriptor by applying a specific mathematical formula to calculate the intended physical proportion of the pixels. While defined primarily in the GIF89a standard, modern software decoders often handle this field with varying degrees of compliance, frequently defaulting to square pixels due to contemporary display standards.
Location in the GIF Stream
The Pixel Aspect Ratio (PAR) is stored within the Logical
Screen Descriptor, which immediately follows the 6-byte GIF
file signature (GIF87a or GIF89a). The
descriptor spans 7 bytes:
- Bytes 1–2: Logical Screen Width
- Bytes 3–4: Logical Screen Height
- Byte 5: Packed Fields (color table flags and resolution)
- Byte 6: Background Color Index
- Byte 7: Pixel Aspect Ratio
The 7th byte is an unsigned 8-bit integer with an absolute value ranging from 0 to 255.
The Interpretation Formula
According to the GIF89a specification, the value of the Pixel Aspect Ratio byte is interpreted as follows:
- Zero Value (0): If the value is
0, the encoder did not specify an aspect ratio. The decoder assumes standard square pixels (a 1:1 aspect ratio). - Non-Zero Value (1 to 255): If the value is non-zero, the decoder calculates the actual aspect ratio using the standard formula:
\[\text{Actual Aspect Ratio} = \frac{\text{Pixel Aspect Ratio Value} + 15}{64}\]
In this formula, the resulting aspect ratio represents the quotient of the pixel's width over its height (\(\text{Width} / \text{Height}\)):
- A value of 49 yields: \((49 + 15) / 64 = 64 / 64 = 1.0\) (square pixels).
- Values less than 49 describe pixels that are taller than they are wide (tall pixels).
- Values greater than 49 describe pixels that are wider than they are tall (wide pixels).
The formula allows representation of pixel aspect ratios ranging from \(16/64\) (\(0.25\)) up to \(270/64\) (\(4.21875\)) in increments of \(1/64\).
How Decoders Apply the Value
When rendering the image, a fully compliant decoder adjusts the displayed dimensions of the canvas rather than modifying the raw pixel grid:
- Scaling the Output: The decoder maps the raw pixel data onto a display grid scaled by the computed ratio. If the actual aspect ratio is greater than 1.0, the decoder stretches the image horizontally or compresses it vertically by that factor.
- Historical Hardware Compatibility: This mechanism originally allowed images authored on non-square pixel displays (such as CGA, EGA, or early Macintosh modes) to render with correct proportions on different hardware.
Modern Decoder Implementation
In modern implementations, including major web browsers and image
viewers, the pixel aspect ratio byte is almost universally ignored.
Because modern displays use square pixels, decoders typically treat any
non-zero value as if it were 0, rendering the image using a
strict 1:1 pixel grid to prevent distortion and unexpected layout
shifts.