Pre-Multiplied Alpha
 
 
 

The following is a discussion of the concept of pre-multiplied alpha as used by 3ds Max. A 32-bit bitmap file contains four channels of data: red, green, blue, and alpha. The first three provide color information to the pixels, while the alpha channel provides transparency information. There are two methods of storing alpha in a bitmap -- pre-multiplied and non-pre-multiplied.

To composite an image that is in non-pre-multiplied format, the alpha must be multiplied by each of the R,G, and B channels before adding it in to the color of the background image. This provides the correct transparency effect, but must be done each time you composite. With pre-multiplied alpha, you store the R,G, and B components with the alpha already multiplied in, so compositing is more efficient.

However, this is not the only reason that 3ds Max stores images in the pre-multiplied format. When you render an image, you typically want the edges of the objects to be anti-aliased. This effect is achieved by determining the fractional coverage of pixels on the edge of the object, and then adjusting the alpha of the pixel to reflect this. For example, a pixel which is 30% covered by the object will have an alpha of 0.30. To anti-alias the edges, the alpha must be pre-multiplied to darken these edge pixels. (This is basically equivalent to compositing the image over a black image). So it is natural, in a sense, for rendered images to have pre-multiplied alpha. If you didn't pre-multiply in the alpha of a rendered image, then just looking at the RGB you would see jaggies on the edges of objects: you'd have to composite it against black using the alpha channel whenever you wanted to display it.

Pre-multiplied alpha works as follows: If you have an image A which is partially transparent, and you want to composite it over an image B, the alpha channel of A tells you at each pixel how much of A and B to mix in. If A's alpha is pre-multiplied (as it always is in 3ds Max) then the formula is:

color = A + (1-A.alpha)*B

Alpha can also be thought of as the "opacity" of A at a given pixel.

If image A was stored with Non-Pre-Multiplied Alpha (NPMA) then the formula for compositing would be:

color = A.alpha*A + (1-A.alpha)*B

To understand why pre-multiplied alpha is used consider an anti-aliased edge of an object, rendered against a black background. The pixels along the edge will have an alpha less than 1.0, and when the alpha is multiplied in, it will make the edge look smooth (i.e. anti-aliased). If you don't pre-multiply the alpha, the RGB image displayed as-is (with out taking into account alpha) looks jagged: multiplying by the alpha is what anti-aliases the image.

This begs the question "Why would you ever use non-pre-multiplied alpha?". Say you have an image without an alpha channel. You want to create an alpha channel to mask out all but a certain object, but want to leave the original image unchanged: you may be using the same image at other places in your rendering. In this case, you would want to paint an alpha channel using an image editor (such as Photoshop) which would mask out the image, and combine it using the non-pre-multiplied alpha formula.

3ds Max's Mask texture map allows you to do this. Mask lets you combine a texture with an mask channel, where the mask channel acts as a non pre-multiplied alpha channel. The mask texture multiplies all four channels of the map with the mask channel, so what is passed up the pipeline is RGBA with pre-multiplied alpha.