Dealing with Premultiplied Alpha on iOS

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
- RGB channels represent the full intensity of the color.
- Alpha channel represents visibility.
- RGB and alpha are independent.
To fade out an object, decrease the alpha while keeping RGB fixed.
Premultiplied Alpha
- RGB channels are already scaled by alpha.
- Alpha still represents transparency but now affects RGB.
- Fully transparent pixels (α = 0) must have RGB = 0.
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.”

2. The Problem Encountered
During image processing on an app, a mismatch was observed between the Simulator and real device output:
- Simulator Output: R = 254, G = 254, B = 254, A = 254
- Real Device Output: R = 253, G = 253, B = 253, A = 254
💡 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

Apple provides CGImageAlphaInfo
, which includes values such as:
kCGImageAlphaFirst
(non-premultiplied ARGB)kCGImageAlphaPremultipliedLast
(RGB values are premultiplied)

5. From Premultiplied to Straight
Premultiplied Calculation:
Rₚ = R × A Gₚ = G × A Bₚ = B × A

Reverting to Straight:
R = Rₚ / A G = Gₚ / A B = Bₚ / A

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
- CGImageAlphaInfo – Apple Docs
- CoreImage Documentation
- Win2D Premultiplied Alpha
- Image Processing Book – University of Tartu
Originally published on Medium:
medium.com/@ezefranca