Premultiplied alpha halos
Just started layering up some transparent quads using OpenGL. The images are PNGs exported from Photoshop, loaded with SDL_image. I'm using the (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) options to my blend function, but I get nasty white halos around the edges of my image. Googling around a bit suggests that this is due to premultiplied alpha channels, but I have struggled to find a coherent explanation, and more important, a solution to this problem.
Anyone here know about this, and have any suggestions?
Cheers
- Iain
Anyone here know about this, and have any suggestions?
Cheers
- Iain
It's actually the other way around. Premultiplied alpha helps solve the halo problem.
Premultiplied alpha works like this: Instead of using R, G, B, A as normal, you use R*A, G*A, B*A, A. So, any non-opaque pixels's color values are pulled toward zero. You can do this either as preprocessing on the image file itself, or at runtime when you load it. So, if you premultiply alpha and switch your blendFunc to (GL_ONE, GL_ONE_MINUS_SRC_ALPHA), you should theoretically stop seeing halos.
I think the exact specifics of why this works are floating around and old thread on this forum somewhere, but the above is at least what you need to know to implement it.
Premultiplied alpha works like this: Instead of using R, G, B, A as normal, you use R*A, G*A, B*A, A. So, any non-opaque pixels's color values are pulled toward zero. You can do this either as preprocessing on the image file itself, or at runtime when you load it. So, if you premultiply alpha and switch your blendFunc to (GL_ONE, GL_ONE_MINUS_SRC_ALPHA), you should theoretically stop seeing halos.
I think the exact specifics of why this works are floating around and old thread on this forum somewhere, but the above is at least what you need to know to implement it.
I know I was involved in a thread here that had a solution. I would find it for you but there is a 60 second wait in between searches and I didn't find it on my first two attempts and I'm not going to keep waiting 60 seconds... (this needs changed)
Thanks for the help guys - I just wrote a pre-multiplication step into my image loading code, and changed the blend function to GL_ONE, GL_ONE_MINUS_SRC_ALPHA, and bingo, nasty halos are gone.
Cheers
- Iain
Cheers
- Iain
It works because of the way linear filtering works. Say you have black image on a white background with the alpha masking out the background. If the pixels aren't perfectly aligned, you'll get some grey-ish pixels with a medium transparency.
Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
A thread with an explanation is here: http://www.idevgames.com/forum/showthread.php?t=7560 but I don't think that's the original...
I just use google with site:idevgames.com to search the forums. No 60-second restriction, and it gives better results, too.
I just use google with site:idevgames.com to search the forums. No 60-second restriction, and it gives better results, too.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| png without alpha fine, png with alpha, nothing | dave05 | 6 | 5,662 |
Jun 11, 2005 10:31 AM Last Post: dave05 |
|
| How can I load a PNG without premultiplied alpha? | Prime | 6 | 5,505 |
Feb 15, 2005 08:52 PM Last Post: arekkusu |
|
| Premultiplied alpha trouble | Fenris | 21 | 9,612 |
Nov 8, 2004 12:47 AM Last Post: aaronsullivan |
|

