# Alpha blending - Mac Programming

This bit me last night; don't let it bite you. When preparing images for compositing, pre-multiply r, g, and b with alpha. There are two means of blending commonly used on the Mac, interpolated and pre-multiplied. (Well, there's also a third, but it isn't used much.) With interpolated blending, a source color (c_s) painted over a colored destination (c_d) with source alpha (a_s) gives a final color (c_f): c_f = a_s * c_s + (1 - a_s) * c_d In premultiplied blending, it's c_f = c_s + (1 - a_s) * c_d Which means that c_s must be premultiplied with ...

1. ## Alpha blending

This bit me last night; don't let it bite you.

When preparing images for compositing, pre-multiply r, g, and b with alpha.

There are two means of blending commonly used on the Mac, interpolated
and pre-multiplied. (Well, there's also a third, but it isn't used much.)

With interpolated blending, a source color (c_s) painted over a colored
destination (c_d) with source alpha (a_s) gives a final color (c_f):

c_f = a_s * c_s + (1 - a_s) * c_d

In premultiplied blending, it's

c_f = c_s + (1 - a_s) * c_d

Which means that c_s must be premultiplied with c_a beforehand
relative to the c_s in the interpolated case.

It's obvious why Apple chose this, because making c_s bigger than
that lets you put specular highlights on glass. But, like many things
that are obvious, it isn't necessarily obvious when you're working on
something else.
Eric Pepke Guest

2. ## Re: Alpha blending

In <ef37f531.0309041713.10abdd41posting.google.com > Eric Pepke wrote:
> This bit me last night; don't let it bite you.
>
> When preparing images for compositing, pre-multiply r, g, and b with
> alpha.
>
> There are two means of blending commonly used on the Mac, interpolated
> and pre-multiplied. (Well, there's also a third, but it isn't used
> much.)
>
> With interpolated blending, a source color (c_s) painted over a
> colored destination (c_d) with source alpha (a_s) gives a final color (
> c_f):
>
> c_f = a_s * c_s + (1 - a_s) * c_d
>
> In premultiplied blending, it's
>
> c_f = c_s + (1 - a_s) * c_d
>
> Which means that c_s must be premultiplied with c_a beforehand
> relative to the c_s in the interpolated case.
>
> It's obvious why Apple chose this, because making c_s bigger than
> that lets you put specular highlights on glass. But, like many things
> that are obvious, it isn't necessarily obvious when you're working on
> something else.
Is this related to the stuff in the docs that says stuff like "Cocoa and
Quartz expect bitmaps with alpha to have the color values premultiplied"?
m.

--
matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
REALbasic: The Definitive Guide! 2nd edition!
[url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
Subscribe to TidBITS. It's free and smart.
matt neuburg Guest

3. ## Re: Alpha blending

matt neuburg <matttidbits.com> wrote in message news:<20030905074827438-0700news.la.sbcglobal.net>...
> Is this related to the stuff in the docs that says stuff like "Cocoa and
> Quartz expect bitmaps with alpha to have the color values premultiplied"?
> m.
Yes. But the only reference I've seen is in [NSBitmapImageRep
getBitmapDataPlanes], which I only found afterward.
Eric Pepke Guest

4. ## Re: Alpha blending

in article google.com, Eric Pepke at
org wrote on 05/09/2003 04:13:

Please clarify, how exactly? And what exactly do you mean
by "specular highlights", white?

Why can't you do the same with the first method (interpolated
blending), e.g. like this:

a_s = 1 (fully opaque)
c_s = 1 (white)
c_f = a_s * c_s + (1 - a_s) * c_d = 1 * 1 + (1 - 1) * c_d = 1 (white)

Mike

--
Mike Kluev

Mike Guest

5. ## Re: Alpha blending

Mike Kluev <-DELETE-.org> writes:

>
> Please clarify, how exactly? And what exactly do you mean
> by "specular highlights", white?
>
> Why can't you do the same with the first method (interpolated
> blending), e.g. like this:
>
> a_s = 1 (fully opaque)
> c_s = 1 (white)
> c_f = a_s * c_s + (1 - a_s) * c_d = 1 * 1 + (1 - 1) * c_d = 1 (white)[/ref]

The difference is that a real highlight isn't opaque. Imagine looking
through a window from a dark room. Does the view get darker when you
turn the lights on? It doesn't, because there's no way the light
hitting the front of the glass can attenuate the light coming through

Another example is a flame: it adds light without really obscuring
what's behind it. That's why flames don't cast shadows.

--
-Stephen H. Westin
Any information or opinions in this message are mine: they do not
represent the position of Cornell University or any of its sponsors.
Stephen Guest

6. ## Re: Alpha blending

in article cornell.edu, Stephen H. Westin at
westin*cornell.edu wrote on 29/10/2003 20:06:

>>
>> Please clarify, how exactly? And what exactly do you mean
>> by "specular highlights", white?
>>
>> Why can't you do the same with the first method (interpolated
>> blending), e.g. like this:
>>
>> a_s = 1 (fully opaque)
>> c_s = 1 (white)
>> c_f = a_s * c_s + (1 - a_s) * c_d = 1 * 1 + (1 - 1) * c_d = 1 (white)[/ref]
>
> The difference is that a real highlight isn't opaque. Imagine looking
> through a window from a dark room. Does the view get darker when you
> turn the lights on? It doesn't, because there's no way the light
> hitting the front of the glass can attenuate the light coming through
>
> Another example is a flame: it adds light without really obscuring
> what's behind it. That's why flames don't cast shadows.[/ref]

Let's see. Let a_s be 0.5 (half transparent) and c_s be 1 (white).

c_f = 0.5 * 1 + 0.5 * c_d = (1 + c_d)/2 >= c_d
(because 0 <= c_d <= 1)

So, source image is not fully opaque and result is not getting
darker than it was. Am I missing something? Give some concrete
example (numeric values of c_s, a_s, c_d) that shows advantage
of premultiplying (i.e. what is the destination color c_d and
what result do we want to get c_f).

--
Mike Kluev

Mike Guest

7. ## Re: Alpha blending

Mike Kluev <-DELETE-.org> wrote in message news:<BBC6C0CD.7891%-DELETE-.org>...

(shrug) The cardinal rule in graphics is, if it looks good, do it.
Try it if you like. The highlights just don't look as good; they
look more like a smear than a highlight. I've probably tried
every conceivable alpha combination going back to the SGI days:
some look good, and some don't.
Eric Guest

8. ## Re: Alpha blending

In Article google.com, Eric Pepke wrote:

>
> (shrug) The cardinal rule in graphics is, if it looks good, do it.
> Try it if you like. The highlights just don't look as good; they
> look more like a smear than a highlight. I've probably tried
> every conceivable alpha combination going back to the SGI days:
> some look good, and some don't.[/ref]

Not sure how to try. The icon of "Preview" app has some descent
(for me anyway) highlight on glass. This icon is almost fully
opaque where this highlight is (checked with Icon Browser), so
the "traditional" (interpolated) blending should work:

c_f = a_s * c_s + (1 - a_s) * c_d = 1 * 1 + (1 - 1) * c_d = 1

If someone knows the answer to the below, please post it here:
[/ref]

--
Mike Kluev

Mike Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•