Ezequiel Santos
iOS Developer & Creative Technologist

Dealing with Premultiplied Alpha on iOS

Photo by Paweł Czerwiński on Unsplash

Understanding the difference between straight and premultiplied alpha and how to deal with them on iOS image processing.

Abstract

In computer graphics, color transparency can be represented in two main ways: straight alpha and premultiplied alpha. This article explains the conceptual difference between the two models and outlines a real-world debugging process for resolving discrepancies in image pixel values on iOS, especially between the Simulator and physical devices.

1. Conceptual Foundations

Straight (Linear) Alpha

To fade out an object, decrease the alpha while keeping RGB fixed.

Premultiplied Alpha

In this case, RGB and alpha are mathematically linked.
“In digital imaging, a pixel is a physical point in a raster image or the smallest addressable element in an all points addressable display device; so it is the smallest controllable element of a picture represented on the screen. The RGB color model is an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors.”
Premultiplied Alpha Pixel Grid
Figure 1: Representation of an image with 3x3 pixels, with the 4 channels: Red (R), Green (G), Blue(B) and Alpha (represented in purple)

2. The Problem Encountered

During image processing on an app, a mismatch was observed between the Simulator and real device output:

💡 Root cause: Simulator uses straight alpha, device uses premultiplied alpha.

3. First Fix Attempt

To read pixel color values, this Swift extension was used:

However, it did not account for premultiplied alpha.

4. CGImageAlphaInfo

CGImageAlphaInfo Docs
Figure 2: CGImageAlphaInfo Reference from Apple

Apple provides CGImageAlphaInfo, which includes values such as:

AlphaInfo Options
Figure 3: AlphaInfo Variants

5. From Premultiplied to Straight

Premultiplied Calculation:

Rₚ = R × A
Gₚ = G × A
Bₚ = B × A
Premultiplied Code
Figure 4: Premultiplied Calculation Pseudocode

Reverting to Straight:

R = Rₚ / A
G = Gₚ / A
B = Bₚ / A
Straight Code
Figure 5: Straight Color Recovery

6. Final Fix: Alpha-Aware Swift Extension

Conclusion

This is a simple but illustrative example of how misunderstandings in alpha formats can cause discrepancies in image processing. Always verify whether you’re working in straight or premultiplied alpha mode.

Start your image processing journey with Apple’s CoreImage: CoreImage Documentation

References

  1. CGImageAlphaInfo – Apple Docs
  2. CoreImage Documentation
  3. Win2D Premultiplied Alpha
  4. Image Processing Book – University of Tartu

Originally published on Medium:
medium.com/@ezefranca