An image is a vector of pixels xi with i = 1 ... npixel with either one grayscale channel xi = (gri) or three (R,G,B) colour component channels xi = (ri, gi, bi) and (optionally) one alpha transparency channel αi. All values are from the interval [0, 1].
In computers these are usually represented in integer values:
gri = Gri / Maxval
ri = Ri / Maxval
gi = Gi / Maxval
bi = Bi / Maxval
αi = Αi / Maxval
with X ∈ [0, Maxval] (X = Gr, R, G, B, Α).
Given an original image x0,i (without an alpha channel), and a specific background colour xb, the task of alpha channel reconstruction is the construction of a new image x1,i with an alpha channel α1,i, such that:
1. the new image composed over the specified background colour is identical to the original image, and
2. all pixels with a colour identical to the given background colour become fully transparent,
3. all colours which are at the edge of the colourspace and are not the background colour become fully opaque,
4. all remaining colours are associated with an alpha value which is proportional to their distance in the colourspace from the background colour.
The goal of alpha channel reconstruction is to create an image which can easily be composed over a different background while still looking nice.
First, calculate the relative colourspace distance for each component:
The total distance (and thus the alpha value) is the maximum of the above values for each pixel:
For grayscale:
For RGB:
From α we can calculate the new image channels:
In integer representation:
For RGB:
Implementation notes
[edit]
The method can be implemented with integer arithmetics if the maximum value range is larger or equal to Maxval² + ½Maxval. With the operation \ (integer division),
The latter can be implemented using unsigned integer arithmetics as follows:
Alternative methods of transparency reconstruction
[edit]
A method commonly implemented (simple) image editing software is binary transparency, which fullfills points 1-3 of the above, but not point 4:
In integer representation: