GIF Interlacing Explained: The Four-Pass Scheme
GIF interlacing is a technique designed to progressively render an image over slow connections by reorganizing the order in which horizontal scan lines are transmitted and displayed. Instead of drawing the image sequentially from top to bottom, an interlaced GIF divides all horizontal pixel rows into four distinct passes of increasing density. This allows a decoder or web browser to quickly display a low-resolution silhouette of the entire image and progressively sharpen the details as subsequent passes arrive.
The Mechanics of the Four Passes
In a standard, non-interlaced GIF, rows are stored sequentially from row 0 through row N - 1 (where N is the image height). In an interlaced GIF, the rows are extracted and stored using a fixed mathematical pattern across four passes:
Pass 1: Begins at Row 0 and samples every 8th row.
Rows rendered: 0, 8, 16, 24, 32, etc.
Purpose: This pass provides the initial, very coarse preview of the image. While it delivers only 12.5% of the total row data, filling the missing rows with duplicates of the transmitted rows creates an immediate, blocky representation of the entire composition.Pass 2: Begins at Row 4 and samples every 8th row.
Rows rendered: 4, 12, 20, 28, 36, etc.
Purpose: By targeting the exact midpoint between the lines drawn in Pass 1, this pass doubles the vertical resolution. The viewer can now distinguish major shapes, color boundaries, and large text.Pass 3: Begins at Row 2 and samples every 4th row.
Rows rendered: 2, 6, 10, 14, 18, etc.
Purpose: This pass fills the gaps between the existing lines from Passes 1 and 2, bringing the total rendered lines to half of the final image. Most visual elements become clearly identifiable at this stage.Pass 4: Begins at Row 1 and samples every 2nd row (every odd-numbered row).
Rows rendered: 1, 3, 5, 7, 9, etc.
Purpose: This final pass provides the remaining 50% of the image data, filling in all missing single-pixel gaps to complete the full-resolution graphic.
Decoding and Display
When a decoder renders an interlaced GIF progressively, it does not leave empty scan lines between passes. Instead, it temporarily duplicates each decoded row into the empty rows beneath it:
- After Pass 1: Each line is repeated down 7 times (occupying an 8-pixel tall band).
- After Pass 2: Each line is repeated down 3 times (occupying a 4-pixel tall band).
- After Pass 3: Each line is repeated down 1 time (occupying a 2-pixel tall band).
- After Pass 4: The exact pixel rows are written directly without row duplication, revealing the final output.
This interlacing scheme ensures that all horizontal scan lines are visited exactly once across the four passes, restructuring the transmission order entirely without adding any extra pixel overhead.