[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Pre-multiplied alpha



Having a way to export to premultiplied alpha would be useful in
pygame. With any system that does bilinear filtering or compositing
premultiplied is better.

Along the lines of what Greg was saying, compositing transparent
layers (i.e. blending RGBA src to an RGBA dest)  is one of those
things that OpenGL and DirectX can do pretty well for you with
premultiplied, but can't do accurately if it's not premultiplied.
Basically to properly composite non-premultiplied alpha colors you
need to do multiple multiplies and a divide. If you are doing
everything in premultiplied space on the other hand, the compositing
is simple enough to be done with standard blend modes.

Basically you can render your color & alpha modes as src*ONE +
dest*(ONE_MINUS_SRC_ALPHA) and compositing works just right with
premultiplied source and dest. Even cooler, the same thing works for
going premult RGBA source to RGB dest.

The whole thing was laid out as part of "Compositing Digital Images"
paper by Porter & Duff (often referred to as Porter-Duff compositing
modes). Here's a link:
http://keithp.com/~keithp/porterduff/p253-porter.pdf

Here's some guys blog post that lays a lot of this stuff out easier to
read than the original paper (in my opinion):
http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D


---
as an aside... one thing that always bugged me about using
premultiplied alpha in OpenGL, is that openGL doesn't seem to have a
texture format that is explicitly marked as premultiplied by the
application... so it seems that if the card had a bilinear filter that
interpolated non-premultiplied correctly (i.e. made translucent black
blend with opaque yellow to make half-translucent yellow instead of
half-translucent brown) than if you used premult textures, than in
that same case the card would end up doubling the color values (i.e.
it would take opaque brown, and blend it with translucent black to
produce half-translucent yellow)

In other words, I can tell openGL how to blend properly with
premultiplied alpha, but it doesn't seem to have a way for me to say
how to interpolate colors correctly, and I never know if a card was
made to expect pre-multiplied or not...


On 8/4/07, Greg Ewing <greg.ewing@xxxxxxxxxxxxxxxx> wrote:
> Don't some OpenGL effects require using premultiplied alpha?
> If that's the case, it could be worth having a function
> in pygame for easily creating premultiplied alpha surfaces.
>
> --
> Greg
>