I'm aware that enough has been said about Premultiplied Alpha vs Not-Premultiplied Alpha blending and compositing, so this article won't try to teach you about premultiplied alpha or preach its benefits or correctness. Although if you do need to learn about that or get a refreshed, then I recommend you visit Tom Forsyth's great and classic article, then come back here to continue reading.

In this article I just wanted to give a mathematical characterization to the problem of "black edges" when you render an image or texture that has black transparent background and some opaque pixels. As we might know from experience, things go wrong when using regular alpha blending and you start scaling up or down such image, although things also go wrong when any linear pixel operation happens, such as mipmapping, filtering with a low pass flilter such a blur, etc.

Now, we also know that one workaround is to dilate the color of the solid bits of the texture outwards to the black pixels. This might or might not be easy to do, but we also know it's unnecessary under premultiplied alpha, which is easy enough to do. In this article we'll explore why and how exactly both of these approaches work.

So for now let's assume you are doing the right thing here, that is, we are using premultiplied alpha blending and hence our blending equation for a texture color

**c**and alpha value

**a**and a background destination color

**d**is

B

_{p}(c,a,d) = c + (1-a)⋅d

as opposed to regular alpha blending

B

_{r}(c,a,d) = c⋅a + (1-a)⋅d

Note I used the p and r subscript to differentiate "premultiplied" and "regular" alpha-blending. Now let's also assume that we are doing some simple bilinear scaling of our texture, and keep the math simple let's assume this is 1D scaling only. In other words, any two consecutive texels of our texture will generate a gradient of colors and alphas during rendering, of the form

S(c) = c

_{1}+ x⋅(c

_{2}-c

_{1})

S(a) = a

_{1}+ x⋅(a

_{2}-a

_{1})

That is, the color

**c**will transition from c

_{1}to c

_{2}as x goes from 0 to 1 across the pixels of the destination buffer

**d**, and alpha

**a**will also transition from a

_{1}to a

_{2}, linearly. So far, pretty simple. And let's do things even simpler if we assume the background

**d**is of constant color, although everything we'll prove still works if we don't assume this.

So, now this is the key observation for this article: we expect scaling and then blending the texture into the screen (which is the normal order of operations) to result in the same picture as if we were blending the texture with the background first and then scaling the result. In other words, we want

B

_{p}(S(cp),S(a),d) = S( B

_{p}(cp,a,d) )

Or yet in other words, we want that blending a scaled image is the same as scaling a blended image.

### The math of Premultiplied Alpha Blending

So, let's expand the formulas and see if we can prove this to be true:

B

S(c) + (1-S(a))⋅d =

c

c

B

B

S(B

Or in short

So indeed, blending interpolated pixels is the same as interpolating blended pixels. Fantastic, pre-multiplied alpha blending works!

B

_{p}(S(c),S(a),d) =S(c) + (1-S(a))⋅d =

c

_{1}+ x⋅(c_{2}-c_{1}) + d⋅( 1 - a_{1}- x⋅(a_{2}-a_{1}) ) =c

_{1}+ d⋅(1-a_{1}) + x⋅[ c_{2}- c_{1}- d⋅(a_{2}-a_{1}) ] =B

_{p}(c_{1},a_{1},d) + x⋅[ c_{2}- d⋅a_{2}+ d - (c_{1}- d⋅a_{1}+ d) ] =B

_{p}(c_{1},a_{1},d) + x⋅[ B_{p}(c_{2},a_{2},d) - B_{p}(c_{1},a_{1},d) ] =S(B

_{p}(c,a,d))Or in short

B

_{p}(S(c),S(a),d) = S(B_{p}(c,a,d))So indeed, blending interpolated pixels is the same as interpolating blended pixels. Fantastic, pre-multiplied alpha blending works!

### The math of Regular Alpha Blending

Now, let's see why and how regular alpha blending does not work. Recall our blending equations is now B

B

S(c)S(a) + d⋅(1-S(a)) =

[c

c

B

B

S(B

S(B

S(B

Summarizing, we have that

That is, the blending of interpolated pixels is the same as the interpolation of the blended pixels PLUS some weird stuff that depends on the pixels we are interpolating and the interpolation factor x. And that's naturally a problem. But let's analyze that “weird stuff” a bit to see how exactly things get wrong here and it produces a black border rather than some other thing.

_{r}(c,a,d) = c⋅a + (1-a)⋅d, soB

_{r}(S(c),S(a),d) =S(c)S(a) + d⋅(1-S(a)) =

[c

_{1}+x⋅(c_{2}-c_{1})]⋅[a_{1}+x⋅(a_{2}-a_{1})] + d⋅[1 - a_{1}- x⋅(a_{2}-a_{1})] =c

_{1}a_{1}+d⋅(1-a_{1}) + c_{1}x⋅(a_{2}-a_{1}) + a_{1}x⋅(c_{2}-c_{1}) + x^{2}⋅(c_{2}-c_{1})(a_{2}-a_{1}) - x⋅d⋅(a_{2}-a_{1}) =B

_{r}(c_{1},a_{1},d) + x⋅[ c_{1}a_{2}+ a_{1}c_{2}- a_{1}c_{1}-a_{2}c_{2}+ B(c_{2},a_{2},d) - B(c_{1},a_{1},d) ] + x^{2}⋅(c_{2}-c_{1})(a_{2}-a_{1}) =B

_{r}(c_{1},a_{1},d) + x⋅[B_{r}(c_{2},a_{2},d) - B_{r}(c_{1},a_{1},d)] x⋅[ c_{1}a_{2}+ a_{1}c_{2}- a_{1}c_{1}-a_{2}c_{2}] + x^{2}⋅(c_{2}-c_{1})(a_{2}-a_{1}) =S(B

_{r}(c,a,d)) + x⋅( c_{1}a_{2}+ a_{1}c_{2}- a_{1}c_{1}-a_{2}c_{2}) + x^{2}⋅(c_{2}-c_{1})(a_{2}-a_{1}) =S(B

_{r}(c,a,d)) + x⋅[ c_{1}a_{2}+ a_{1}c_{2}- a_{1}c_{1}-a_{2}c_{2}+ x⋅(c_{2}a_{2}-c_{2}a_{1}-c_{1}a_{2}+c_{1}a_{1})] =S(B

_{r}(c,a,d)) + x(1-x)⋅(c_{1}a_{2}+ a_{1}c_{2}- a_{1}c_{1}- a_{2}c_{2})Summarizing, we have that

B

_{r}(S(c),S(a),d) = S(B_{r}(c,a,d)) + x(1-x)⋅(c_{1}a_{2}+ a_{1}c_{2}- a_{1}c_{1}- a_{2}c_{2})That is, the blending of interpolated pixels is the same as the interpolation of the blended pixels PLUS some weird stuff that depends on the pixels we are interpolating and the interpolation factor x. And that's naturally a problem. But let's analyze that “weird stuff” a bit to see how exactly things get wrong here and it produces a black border rather than some other thing.

### The error term

For example, the shape of the problem is a parabola: x⋅(1-x), and this parabola will cancel when x=0 or x=1, which is exactly at the exact pixel positions we are interpolating between. So, the colors of the pixels will be alright at exact pixel locations, it's between pixel locations that things will look wrong, and this will peak at the center of the parabola which happens at x=0.5, right between the two pixels.

We know this wrongness often manifest as the infamous dark edge, so let's see why that is the shape and color of our error:

Recall the error is

e = x⋅(1-x)⋅(c

_{1}⋅a

_{2}+ a

_{1}⋅c

_{2}- a

_{1}⋅c

_{1}- a

_{2}⋅c

_{2})

So first we note that the error will be zero for all x, meaning that everything wil be correct even between pixels, if a

_{1}=a

_{2}

Sin in that case c

_{1}⋅a

_{2}cancels out with -c

_{1}⋅a

_{1}and c

_{2}⋅a

_{1}cancels out with -c

_{2}⋅a

_{2}.

So, we won't see rendering artifacts in the solid parts of the texture, nor in the fully transparent areas of the texture. It's in the transitions between opaque and transparent that the error term e will be non-zero. This checks with experience:

Similarly, if the alpha channel does change but the color channel doesn't, then c

_{1}=c

_{2}and in that case c

_{1}⋅a

_{1}cancels out with -c

_{2}⋅a

_{1}and c

_{1}⋅a

_{2}cancels out with c

_{2}⋅a

_{2}, and so e=0 and then too we won't see an error. This corresponds to the hack of dilating the pixel colors of the billboards outwards into the transparent areas. Again, the math checks with what we know from experience.

The failure is when either the alpha channel changes (ie, a

_{1}!= a

_{2}) and the color channel changes (ie, c

_{1}!= c

_{2}), because then nothing cancels out, e is always bigger than zero except for the exact pixel center x=0 and x=1, and so we see the error.

Furthermore, assuming we are transitioning for example from a green leave (c

e = -x⋅(1-x)⋅c

while if we are transitioning the other way from a black transparent background (c

e = -x⋅(1-x)⋅c

In both cases the error is a negative color contribution to the rendering, stronger the brighter our leaf color is. In the worst case scenario, right in the middle of the transition, at x=0.5, this error will be:

e = -c

For example, in the pure green to white transition in the picture below, this means the error is -255/4 and therefore the color at the darkest point in the transition will be 255 - 255/4 = 191 instead of the expected 255, which matches the empirical measurement.

I probably don't need to justify myself if I claim that non-premultipled alpha blending will go horribly wrong in the case of general 2D bilinear interpolation given that just the 1D linear interpolation misbehaves.

_{1}=green, a_{1}=1) to a black transparent background (c_{2}=black, a_{2}=0), then the error term will be:e = -x⋅(1-x)⋅c

_{1}while if we are transitioning the other way from a black transparent background (c

_{1}=black, a_{1}=zero) to the leaf (c_{2}=green, a_{2}=1) we gete = -x⋅(1-x)⋅c

_{2}In both cases the error is a negative color contribution to the rendering, stronger the brighter our leaf color is. In the worst case scenario, right in the middle of the transition, at x=0.5, this error will be:

e = -c

_{2}/4For example, in the pure green to white transition in the picture below, this means the error is -255/4 and therefore the color at the darkest point in the transition will be 255 - 255/4 = 191 instead of the expected 255, which matches the empirical measurement.

I probably don't need to justify myself if I claim that non-premultipled alpha blending will go horribly wrong in the case of general 2D bilinear interpolation given that just the 1D linear interpolation misbehaves.

I'm aware there's no actionable advice in this article, other than keep doing what you probably were already doing in terms of premultiplied blending, but I hope you enjoyed the little mathematical take on it.