User:Federhalter/Alpha channel reconstruction

Definitions:
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, Α).

Problem
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.

Solution
First, calculate the relative colourspace distance for each component:

$$\delta_{x,i} = \begin{cases} \frac{x_b - x_{0,i}}{x_b}, & \text{for } x_{0,i} < x_b, \\ 0, & \text{for } x_{0,i} = x_b, \\ \frac{x_{0,i} - x_b}{1 - x_b}, & \text{for } x_{0,i} > x_b.\\ \end{cases} $$

The total distance (and thus the alpha value) is the maximum of the above values for each pixel:

$$\alpha_i = max(\delta_{X,i})$$

For grayscale:

$$\alpha_i = \delta_{gr,i}$$

For RGB: $$\alpha_i = max(\delta_r, \delta_g, \delta_b)_i$$

From α we can calculate the new image channels:

$$x_{1,i} = \begin{cases} \frac{x_{0,i} - x_b}{\alpha_i} + x_b, & \text{for } \alpha_i > 0, \\ x_b, & \text{for } \alpha_i = 0. \end{cases} $$

In integer representation:

$$ \Delta_{X,i} = \begin{cases} \frac{Maxval \cdot (X_b - X_{0,i})}{X_b}, & \text{for } X_{0,i} < X_b, \\ 0, & \text{for } X_{0,i} = X_b, \\ \frac{Maxval \cdot (X_{0,i} - X_b)}{Maxval - X_b}, & \text{for } X_{0,i} > X_b. \end{cases} $$

$$\Alpha_i = \Delta_{Gr,i}$$

For RGB: $$\Alpha_i = max(\Delta_R, \Delta_G, \Delta_B)_i$$

$$X_{1,i} = \begin{cases} X_b + \frac{Maxval \cdot (X_{0,i} - X_b)}{\Alpha_i}, & \text{for } \Alpha_i > 0, \\ X_b, & \text{for } \Alpha_i = 0. \end{cases} $$

Implementation notes
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),

$$ \Delta_{X,i} = \begin{cases} (Maxval \cdot (X_b - X_{0,i}) + X_b \backslash 2) \backslash X_b, & \text{for } X_{0,i} < X_b, \\ 0, & \text{for } X_{0,i} = X_b, \\ (Maxval \cdot (X_{0,i} - X_b) + (Maxval - X_b) \backslash 2) \backslash (Maxval - X_b), & \text{for } X_{0,i} > X_b. \end{cases} $$

$$X_{1,i} = \begin{cases} X_b + (Maxval \cdot (X_{0,i} - X_b) + \Alpha_i \backslash 2) \backslash \Alpha_i, & \text{for } \Alpha_i > 0, \\ X_b, & \text{for } \Alpha_i = 0. \end{cases} $$

The latter can be implemented using unsigned integer arithmetics as follows:

$$X_{1,i} = \begin{cases} X_b - (Maxval \cdot (X_b - X_{0,i}) + \Alpha_i \backslash 2) \backslash \Alpha_i, & \text{for } \Alpha_i > 0 \land X_{0,i} < X_b, \\ X_b + (Maxval \cdot (X_{0,i} - X_b) + \Alpha_i \backslash 2) \backslash \Alpha_i, & \text{for } \Alpha_i > 0 \land X_{0,i} \ge X_b, \\ X_b, & \text{for } \Alpha_i = 0. \end{cases} $$

Alternative methods of transparency reconstruction
A method commonly implemented (simple) image editing software is binary transparency, which fullfills points 1-3 of the above, but not point 4:

$$x_{1,i} = x_{0,i}$$

$$ \alpha_{x,i} = \begin{cases} 1, & \text{for } x_{0,i} \ne x_b, \\ 0, & \text{for } x_{0,i} = x_b. \end{cases} $$

In integer representation:

$$X_{1,i} = X_{0,i}$$

$$ \Alpha_{X,i} = \begin{cases} Maxval, & \text{for } X_{0,i} \ne X_b, \\ 0, & \text{for } X_{0,i} = X_b. \end{cases} $$