How libavif Handles Premultiplied vs Straight Alpha

This article provides a technical overview of how the libavif library handles premultiplied versus straight alpha during color space conversions. It covers the internal representation of alpha in the avifImage structure, how the library converts between these two states to avoid compression artifacts, and the specific API functions developers use to manage alpha blending.

Alpha Representations in libavif

In digital imaging, alpha channels determine pixel transparency. “Straight” (or unassociated) alpha stores RGB color data independently of transparency. “Premultiplied” (or associated) alpha multiplies the RGB color values by the alpha value beforehand.

The AV1 container format (AVIF) natively encodes color and alpha as separate planes. Color is typically compressed as YUV (or RGB), while alpha is stored as an auxiliary monochrome plane. Because AV1 compression operates on these planes independently, libavif must carefully manage the transition between straight and premultiplied alpha to maintain color fidelity.

The alphaPremultiplied Flag

The core data structure in the library, avifImage, contains a boolean field named alphaPremultiplied.

This flag acts as metadata, telling the libavif encoder and decoder how to interpret the pixel data currently held in memory.

Handling Alpha During Encoding

When encoding an image to AVIF, using premultiplied alpha directly can lead to severe compression artifacts. During YUV chroma subsampling and lossy compression, the sharp color transitions at the edges of transparent objects in a premultiplied image can cause color bleeding, dark halos, or fringing.

To prevent this, libavif prefers to compress images using straight alpha. If an application inputs an image with premultiplied alpha, the library provides helper functions to “un-premultiply” the image back to straight alpha before the color channels are converted to YUV and encoded. This preserves the original color details of semi-transparent or fully transparent pixels during the lossy compression process.

Handling Alpha During Decoding

Upon decoding an AVIF file, libavif extracts the color and alpha planes as straight alpha. While straight alpha is ideal for storage and compression, many modern graphics APIs (such as WebGL, CoreGraphics, and various game engines) require premultiplied alpha for accurate, hardware-accelerated blending.

After decoding the straight alpha image, developers can instruct libavif to convert the output pixels to premultiplied alpha. This ensures that when the image is rendered, the edges of semi-transparent objects blend seamlessly with the background without displaying dark halos.

Key API Functions for Conversion

To facilitate these transitions, libavif exposes two primary conversion functions:

By utilizing these explicit conversion steps, libavif ensures that images retain high color accuracy during compression while remaining highly compatible with diverse rendering pipelines.