OpenGL How To Create Pre-Multiplied Alpha for use with OpenGL

This topic is 2284 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I feel like this should be a simple answer, but for the life of me I cannot find it.

I've been rendering my game using the alpha blend mode:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

but have been getting the gray outlines around a lot of my png images like Riruilo has outlined in this post on idevgames.com: http://www.idevgames...thread-966.html

What I gathered from that post is that to fix the problem I should convert all my images (currently in png format) to premultiplied alpha png's, and use the alpha blend mode:

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

So my question is this:

How do you convert a normal png to a premultiplied alpha png?

I had thought this would exist as a feature (like a checkbox) within Photoshop or GIMP or Paint.Net, but am unable to find it. I understand that one could convert the png's on loading into the correct format, but I would prefer to have them in the correct format before loading. Could anyone shed some light on this issue, I'm feeling very dumb being unable to find out this can be done.

Share on other sites
AFAIK the PNG standard explicitly requires non-premultiplied alpha. So image editors behave correctly if they do not allow you to save as premultiplied.
I had similar problem and resolved it by using separate color and mask images (thus transparent pixels had color as well), compositing them in game editor and converting to compressed texture format with included mipmaps.

Share on other sites
I'm afraid I don't think I understand your process. So if I have this right, you made 2 "pngs" (or whatever image format you used) for each image you have. One is the actual image itself, and the other is am image that represents all the alpha values of the first, with alpha represented as a shade between black and white? If this is what you have done (I think I'm getting something wrong), then why not just use a single png image and multiply the RGB values by the alpha value of that single image to get to "pre-multiplied alpha" state.

Just in case it matters, I should mention that I have many images with alpha values that fall between 0 and 1, and are not simply just opaque or transparent.

Share on other sites
Well, here's short tutorial about doing it in gimp (the wording might be incorrect, you need to know gimp).

Assumption:
You have an image with alpha value.

1. Load the image with gimp, call the layer "image"
2. Right-click the layer and select "mask from alpha"
3. Left click on alpha layer(white border around right thumbnail) => Ctrl+C => Ctrl+V
4. A new temporary layer exists, right click and choose "new layer", rename it to "alpha"
5. Create a new layer with black (0x000000) and call the layer "background"
6. Adjust the layer like this:
- alpha
- image
- background
7. right click the "image" layer and choose "merge with underlying layer"
8. right click the resulting layer and choose "create opaque alpha mask"
9. select alpha layer => Ctrl+C => select alpha channel of "image" layer => Ctrl+V => anchor
10. delete alpha layer
11. save file as png

This should leave you with the premultiplied image with alpha layer.

Share on other sites

I'm afraid I don't think I understand your process. So if I have this right, you made 2 "pngs" (or whatever image format you used) for each image you have. One is the actual image itself, and the other is am image that represents all the alpha values of the first, with alpha represented as a shade between black and white? If this is what you have done (I think I'm getting something wrong), then why not just use a single png image and multiply the RGB values by the alpha value of that single image to get to "pre-multiplied alpha" state.

Just in case it matters, I should mention that I have many images with alpha values that fall between 0 and 1, and are not simply just opaque or transparent.

Yes, I am using seaparate color images and grayscale masks (and thus non-premultiplied textures).
The reason is twofold:

1. It is easier to edit images this way. Although in theory GIMP should allow me to save color values for fully transparent pixels to png, these more than often get "lost" is some editing steps.
2. I want to have full control over how colors are mixed in mipmap creation. n certain textures (foliage). Premultiplied alpha means, that transparent pixels do not contribute any color to scaled down image, but for some textures (foliage) is is sometimes preferable to let these to "bleed" certain color into mipmap.

I am currently using edge detection filter on alpha while generating foliage mipmaps and this needs non-premultiplied colors anyways.

Share on other sites
Thank you both for your replies. I ended up using Ashaman73's approach using GIMP. Thanks a lot, it worked great! For anyone who is not familiar with GIMP, I will post his instructions but for GIMP 2.6:

1. Load the image with GIMP, In the layer window, name the image layer "image".
3. Create a new layer by hitting the "Create new layer" button in the bottom left of the layer window, call this layer "Alpha"
4. Left click on the alpha mask of the "image" layer (white border around right thumbnail) => Ctrl+C.
5. Left click on the "Alpha" layer => Ctrl+V.
6. Right click on the new temporary layer that is created, select "Anchor Layer".
7. Create a new layer with black (0x000000) and call the layer "background".
8. Order the layer like this (by left clicking and dragging the layers around):
- alpha
- image
- background
9. right click the "image" layer and choose "Merge Down"
10. right click the resulting layer and select "Add Layer Mask". Select "White (full opacity)", click add.
11. select the "Alpha" layer => Ctrl+C => select alpha mask of "background" layer => Ctrl+V.
12. Right click on the new temporary layer, select "Anchor Layer".
13. delete the "Alpha" layer.
14. save file as png.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631751
• Total Posts
3002087
×