Premultiplied alpha halos

Member
Posts: 156
Joined: 2002.10
Post: #1
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
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
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.
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #3
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)
Quote this message in a reply
Member
Posts: 156
Joined: 2002.10
Post: #4
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
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #5
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  png without alpha fine, png with alpha, nothing dave05 6 6,721 Jun 11, 2005 10:31 AM
Last Post: dave05
  How can I load a PNG without premultiplied alpha? Prime 6 7,233 Feb 15, 2005 08:52 PM
Last Post: arekkusu
  Premultiplied alpha trouble Fenris 21 11,348 Nov 8, 2004 12:47 AM
Last Post: aaronsullivan