GIMP Selection Boundary and Mask Data Structures

The GNU Image Manipulation Program (GIMP) internally handles user selections through a dual approach: a raster-based channel mask representing pixel coverage and a derived vector segment array representing the visible boundary. Rather than relying on a simple 1-bit monochrome array, GIMP stores the selection mask as a full 8-bit grayscale channel managed by a sparse, tile-based buffer (GeglBuffer), while representing the active selection boundaries (the "marching ants") as an array of line segment structures (GimpBoundSeg). When strict binary clipping regions are required, GIMP translates these bounds into rectangular sets using cairo_region_t.

The Core Selection Mask: GimpChannel and GeglBuffer

At the core of GIMP's architecture, an active selection is not strictly a binary mask, but an 8-bit alpha mask stored within a specialized channel object (GimpSelection, which inherits from GimpChannel). This design allows GIMP to support anti-aliasing, sub-pixel accuracy, and feathered selections:

  • Pixel Format: Each pixel in the selection mask is an 8-bit unsigned integer (guint8) ranging from 0 (completely unselected) to 255 (completely selected). When a user creates a hard-edged selection, the values are restricted to 0 and 255, effectively operating as a binary mask.
  • Storage Engine (GeglBuffer): Modern GIMP (version 2.10 and later) stores channel data using the GEGL (Generic Graphical Library) engine. A GeglBuffer organizes pixel data into a sparse, tiled grid—typically 128×128 pixel tiles. This tile-based data structure avoids allocating contiguous memory for empty (unselected) canvas regions and supports copy-on-write functionality for undo/redo stacks.

Boundary Representation: GimpBoundSeg

While the raster mask governs pixel manipulation, GIMP needs a separate data structure to compute and render the selection boundary (the animated dashed line known as "marching ants").

To generate boundaries:

  1. GIMP scans the underlying selection mask using an edge-tracing algorithm.
  2. The threshold is evaluated at the midpoint (value 127/128) to locate the boundary of the mask.
  3. The boundary is converted into an allocated array of GimpBoundSeg (Boundary Segment) structures.

Each GimpBoundSeg structure records discrete segment coordinates:

  • Starting coordinate (x1, y1)
  • Ending coordinate (x2, y2)
  • Directional flags indicating whether the segment is horizontal or vertical, and which side contains the selected pixels.

This array of boundary segments enables rapid rendering of the marching ants on the canvas without repeatedly scanning the full raster mask on every frame redraw.

Geometric Binary Clipping: cairo_region_t

When GIMP requires a discrete binary representation for bounding-box queries, canvas invalidation, or fast clipping, it converts the selection boundaries into a cairo_region_t.

This structure represents a 2D binary shape as a sorted array of non-overlapping, axis-aligned rectangles (based on the Pixman/X11 region implementation). It allows GIMP to perform boolean operations (union, intersection, subtraction) with minimal computational overhead during screen redraws and tool operations.