how to do premultiplied alpha

hello,
maybe this might be offtopic here, since it is not related to jogl, but i think you guys might know the answer:

i read a lot that in order to use textures with alpha channels correctly you should premultiply the alpha channel. (aka premultiplied alpha)
does anyone know how to do that in bufferedimages? if yes, how?

currently the edges of my textures look like this:

http://www.embege.com/misc/dark-blending.jpg

http://www.embege.com/misc/dark-blending.jpg

i wanna get rid of that dark edge.

thanks a lot!

I only learned the distinction about premultiplied alpha fairly recently. There is a brief writeup in the javadoc for the JOGL Texture class. Basically, assuming you’re trying to composite a bunch of textures which come from BufferedImages using OpenGL:

[] Use a BufferedImage format with premultiplied alpha; TYPE_INT_ARGB_PRE is a good choice. If your source BufferedImages aren’t in this format, convert them by manually creating a BufferedImage of the correct type, width and height, and use Graphics.drawImage() to put the initial image into the premultiplied one. Then use the TextureIO classes to convert the BufferedImage into a Texture.
[
] Use glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA).

If you use a texture environment mode where the current color has an effect, such as GL_MODULATE, then you also need to premultiply the alpha component into the red, green, and blue components. For example:


    float a = ...;
    float r = r * a;
    float g = g * a;
    float b = b * a;
    gl.glColor4f(r, g, b, a);

That should be it.

thanks Ken,
that completely solves my problem!
i am using the blendfunc you suggest and in case i modulate with an alpha other than 1 i also multiply with the alpha.

thanks a lot!

Ken, given the above two quotes (one from you and one from the Texture javadocs) I’m confused as what to do. Your remark seems to suggest that you need to do the pre-multiplying yourself while the javadocs seem to suggest it happens automatically?

You’re right, there’s a problem with the semantics of the TextureIO classes here. For now assume that premultiplied alpha is only used for premultiplied BufferedImage types. I realize this defeats some of the purpose of the TextureIO classes. We’ll look into how to fix this more generally.