Re: [AD] premultiplied alpha

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Fri, 2010-10-29 at 16:52 -0400, Evert Glebbeek wrote:
> On 29 Oct 2010, at 16:36 , Elias Pschernig wrote:
> > Prompted by Trent, I was investigating the use of pre-multiplied alpha.
> > And as the ex_premulalpha I just committed shows, we probably should add
> > a way to use it in A5 or things will look wrong with filtering. My idea
> > so far is to just add a new bitmap flag, so you could do:
> > 
> > al_set_new_bitmap_flags(ALLEGRO_PREMULTIPLIED_ALPHA);
> > bitmap = al_load_bitmap("my.png");
> > 
> > It would cause the bitmap loader to multiply in the alpha, so where
> > right now it does:
> > 
> > r, g, b, a = png.r, png.g, png.b, png.a
> > 
> > it will then do this when the flag is set:
> > 
> > r, g, b = png.r * png.a, png.g * png.a, png.b * png.a
> > a = png.a
> 
> What will putpixel and getpixel calls do when applied to such a bitmap?

Well, if we don't do anything, then right now what you would do is where
you do this with non-premultiplied alpha:

al_putpixel(x, y, al_map_rgba_f(1, 0, 0, 0.5))

You instead do this with premultiplied:

al_putpixel(x, y, al_map_rgba_f(0.5, 0, 0, 0.5))

It seems to me that it would be confusing if either al_map_rgba_f or
al_putpixel do any pre-multiplying of their own... but would be
interesting to know if there's anything comparable in e.g. XNA.

> 
> > Another idea would be to use pre-multiplied alpha by default, as Shawn
> > Hargreaves does in XNA [1] :P But I don't really know enough about this
> > I guess, yesterday is the first time I understood where i can even make
> > a difference...
> 
> For what it's worth, we convert to pre-multiplied alpha when setting
> the application icon.
> Which prompts me to go and check what the native image loader on OS X
> does with the colour components when loading a bitmap with an alpha
> channel and make sure it does the right thing.
> 
> Other than that, is it essential to have this for 5.0? I guess it is
> if blending won't work otherwise. It doesn't seem like it would break
> the API to include it from 5.1 onward (except of course when making it
> the default, but we don't have to do that) so in that sense it's not
> that this is our one chance to "get it right".
> 

I think the flag for bitmap loading is important, otherwise you couldn't
use pre-multiplied alpha at all. And the way OpenGL and D3D filtering
work pre-multiplied appears to be a better choice.

As for making it the default I don't know. Maybe it's best to change the
default blend mode from ALPHA/INVERSE_ALPHA to ONE/ZERO. That way we
have no default any longer, everyone needs to call al_set_blender at
least once in each A5 program and decide the used blending for
themselves.

-- 
Elias Pschernig <elias.pschernig@xxxxxxxxxx>





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/