Notes · work · research

Music

Loading…

Open in Apple Music ↗
Ezequiel Santos
iOS Developer & Creative Technologist
  • News
  • Blog
  • About
  • Stuff
⌕/

Dealing with Premultiplied Alpha on iOS

Wednesday, 15 July 2020
AlphaCoreImageRGBiOS
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

  • 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.”
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:

  • 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

CGImageAlphaInfo Docs
Figure 2: CGImageAlphaInfo Reference from Apple

Apple provides CGImageAlphaInfo, which includes values such as:

  • kCGImageAlphaFirst (non-premultiplied ARGB)
  • kCGImageAlphaPremultipliedLast (RGB values are premultiplied)
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

Previous: Visualizing Real-Time Map Rotation with GPS Coordinates on iOS through User Experience Design
Next: Playing with Custom Modifiers in SwiftUI
Discussion

Comments

Read or join the conversation with a GitHub account.

2013– Ezequiel Santos

Built with Swift and Publish.Palette: Flexoki