How GIF Encodes Sub-Rectangle Frames

The Graphics Interchange Format (GIF) achieves efficient animation by allowing individual frames to update only specific regions of the display rather than refreshing the entire canvas. This article explains the technical mechanics behind GIF sub-rectangle encoding, covering how the format defines the global canvas, positions localized frames via Image Descriptors, compresses localized pixel streams, and manages frame retention using Graphic Control Extensions.

The Logical Screen Descriptor

A GIF file begins by establishing a global canvas known as the Logical Screen. Defined within the Logical Screen Descriptor block, this sets the fixed width and height of the entire animation area. Every individual frame rendered thereafter exists within the boundary of this defined coordinate space, sharing a unified coordinate origin at the top-left corner (0, 0).

Image Descriptors and Coordinate Mapping

When a frame only modifies a tiny sub-rectangle of the full canvas, it uses an Image Descriptor block (identified by the byte value 0x2C). Instead of defining pixels for the entire screen, the Image Descriptor specifies four essential 16-bit parameters:

By specifying these values, the decoder maps the incoming pixel data strictly to the defined bounding box, leaving the rest of the canvas untouched during that frame's decoding pass.

Localized LZW Raster Data

Directly following the Image Descriptor (and an optional Local Color Table), the GIF stores the frame's image data encoded using LZW compression. Because the sub-image dimensions are restricted to the tiny rectangle:

Graphic Control Extension and Frame Blending

To seamlessly blend a sub-rectangle over previous frames, the GIF format relies on the Graphic Control Extension (GCE), which precedes the Image Descriptor:

By combining coordinate-based Image Descriptors, restricted LZW raster data, and Graphic Control Extensions, GIF encoders isolate localized screen changes and avoid redundant pixel storage.