To premultiply or not to premultiply alpha

Monkey Forums/Monkey Programming/To premultiply or not to premultiply alpha

Grey Alien(Posted 2013) [#1]
OK I'm confused about something...

Currently all my sprites are normal pngs from photoshop, not pre-multiplied alpha.

However, when I looked in mojo.glfw and mojo.ios and mojo.android they all use this:
glEnable( GL_BLEND );
glBlendFunc( GL_ONE,GL_ONE_MINUS_SRC_ALPHA );

As I understand it, that is the blend mode for Pre-multiplied alpha sprites, otherwise it would be (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

So how come when I view my normal pngs they look fine? If the renderer is expecting pre-multiplied pngs then they should all look wrong shouldn't they?

Anyway, that aside, as the mojo code is expecting pre-multiplied pngs, should I be doing that via running them through a tool or something (doing at runtime would be slow)?

To add to the confusion apparently XCode pre-multiplies (and reverses the RGB) of pngs when it builds for iOS, but according to this post it doesn't do it for the data folder: http://www.monkeycoder.co.nz/Community/posts.php?topic=2464 but the article that post refers to seems to infer it happens to everything: http://iphonedevelopment.blogspot.co.nz/2008/10/iphone-optimized-pngs.html

So yeah I'm pretty confused right now. Any experts on this in da house?

Thanks in advance.

PS. I understand what pre-multiplied alpha is and why it's better for most situations, so I don't need an explanation of that. I'm confused as to if I should be doing the pre-multiplication myself for GLFW and iOS, or not.


AdamRedwoods(Posted 2013) [#2]
it's done at runtime in mojo. iOS doesn't seem to do it, so you are correct on the pre-premultiplying(?).

you'll find it during the gxtkSurface::SetData in the native GLFW code, or in Android's case at texture bind time (which may explain the slight pause when a new image is drawn on Android).


marksibly(Posted 2013) [#3]
You don't need to do any alpha pre-multiplication, it's all handled for you behind the scenes.

On iOS, whether or not the image has been 'optimized', or is in the data folder or whatever doesn't matter - the OS always makes sure it's premultiplied when you load it (I guess optimizing makes this step faster). There is some premultiplication code in iOS mojo, but it's only for 'WritePixels'.


Grey Alien(Posted 2013) [#4]
Thanks for the replies guys! Much appreciated.

I wonder how iOS knows if an image has already been optimised before it loads it? Maybe there's some identifier in the header?