Logical Screen Descriptor in GIF Files
The Logical Screen Descriptor is a mandatory 7-byte metadata block positioned immediately after the file header in both GIF87a and GIF89a specifications. It defines the overall display dimensions, color table characteristics, background color, and pixel proportions required to correctly render the image sequence.
Byte Breakdown of the Logical Screen Descriptor
The 7 bytes of the Logical Screen Descriptor contain the following structural information:
1. Logical Screen Width (Bytes 1–2)
A 16-bit unsigned integer stored in little-endian byte order. It specifies the total width of the canvas or display area in pixels required to render the GIF. Individual image frames within the file cannot exceed this boundary.
2. Logical Screen Height (Bytes 3–4)
A 16-bit unsigned integer stored in little-endian byte order. It specifies the total height of the canvas or display area in pixels required to display the image.
3. Packed Fields (Byte 5)
A single byte containing four distinct bitfields that dictate how colors are managed:
- Global Color Table Flag (Bit 7): A 1-bit flag
indicating whether a Global Color Table immediately follows the Logical
Screen Descriptor. If set to
1, a global palette is present; if0, no global palette is provided. - Color Resolution (Bits 4–6): A 3-bit value representing the color resolution of the original image palette minus one. It indicates the number of bits per primary color minus one, allowing reconstruction of the original dynamic range.
- Sort Flag (Bit 3): A 1-bit flag indicating whether
the colors in the Global Color Table are sorted in decreasing order of
importance. A value of
1indicates sorted colors, which aids rendering on displays with limited color capabilities. - Size of Global Color Table (Bits 0–2): A 3-bit
value used to calculate the number of entries in the Global Color Table.
The number of color entries is calculated using the formula \(2^{(N + 1)}\), where \(N\) is the value of these 3 bits. This
yields between 2 and 256 color entries (taking up between 6 and 768
bytes). If the Global Color Table Flag is set to
0, this value is ignored.
4. Background Color Index (Byte 6)
An 8-bit index pointing to a specific entry in the Global Color Table. The color at this index is used for portions of the screen not covered by an image frame or when transparent pixels are cleared. If no Global Color Table exists, this byte is ignored.
5. Pixel Aspect Ratio (Byte 7)
An 8-bit value that approximates the aspect ratio of the pixels in
the original image. If the byte value is 0, no aspect ratio
information is given, and square pixels (1:1) are assumed. If the value
is non-zero, the actual aspect ratio is computed using the formula:
\[\text{Aspect Ratio} = \frac{\text{Value} + 15}{64}\]