evan_tech

Previous Entry Share Next Entry
12:25 am, 23 Sep 05

premultiplied alpha

I've seen the term "premultiplied alpha" thrown around a lot -- basically, deciding whether the R,G,B components have been multiplied by alpha or not -- but a nice post by Bill Spitzak really made everything clear:
If you were not premultiplying, compositing color C over B with alpha a would be done with "Ca+B(1-a)".

In premultiplied images, the C pixel is replaced with A = Ca and the math is changed to "A+B(1-a)". Substituting Ca for A will show that this is identical.

So mathematically there is no difference.

Advantages of premultiplied:

1. This is what almost all rendering and painting programs produce. Correctly producing a non-premultiplied image would require tracking a "coverage" for each pixel that is seperate from the "transparency". Premultiplied allows you to combine these values very early.

2. Premultiplied images look correct when the rgb channels are viewed. Non-premultiplied images will have noisy and bright pixels at any antialiased edges.

3. Premultipled images are more convienent for other operations such as adding to simulate a double exposure.

4. Premultiplied images allow "glow" where the color is greater than the alpha.

5. Premultiplied over is available on Windows.

Advantages of non-premultiplied:

1. Supposedly this is what .png files are supposed to be, but everybody appears to be ignoring that.

2. The compositied result is always in the range 0-1, with no clamping needed.

3. An 8-bit image can antialias any 8-bit color accurately. A premultiplied image with 1/4 alpha, if added 4 times to get an opaque image, will only have 64 possible colors in 8 bits. This advantage is negated, however, because most implementations do not preserve intermediate results of more than 8 bits, thus the non-premultiplied version produces the same result.

I believe Cairo can do non-premultiplied, by setting the source pattern and the mask to the same image, then doing a copy operation. So you actually have both anyway.