Premultiplied alpha question

From what I understand, for the OGL pipeline, Java2D caches images in VRAM with premultiplied alpha.

Is this also the case for the new D3D pipeline? For BufferedImages that often change contents, does this mean it’s better to use TYPE_INT_ARGB_PRE instead of TYPE_INT_ARGB?

Yes, the d3d pipeline also uses premultiplied format for cached images.

One thing you can do is use GraphicsConfiguration.createCompatibleImage(w,h, TRANSLUCENT)
since that image will use proper color model.

Dmitri

Cool, thanks trembovetski.

Well I’m still a bit confused because everywhere I’ve tried it, createCompatibleImage(w,h, TRANSLUCENT) returns a BufferedImage of type TYPE_INT_ARGB instead of TYPE_INT_ARGB_PRE.

I guess since VRAM caching typically on happens only once, the conversion time to premultiplied is negligible?

Hmm. That’s weird - the code says otherwise. Must be reading wrong code! =)

Yes, the conversion time is not a problem unless you touch the src image on every frame.

Dmitri

The ColorModel used for the translucent compatible image is created like this:
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
return new DirectColorModel(cs, 32,
0xff0000, 0xff00, 0xff, 0xff000000,
true, DataBuffer.TYPE_INT);

That looks like a premultiplied ARGB to me, so I’m surprised that it isn’t.

Dmitri

I could be missing something - this is the code I’m using to test:

 import java.awt.*;
import java.awt.image.BufferedImage;

public class CompatibleImageTest {
    public static void main(String[] args) {
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = e.getDefaultScreenDevice();
        GraphicsConfiguration[] configs = device.getConfigurations();
        for (GraphicsConfiguration config : configs) {
            BufferedImage image = config.createCompatibleImage(32, 32, Transparency.TRANSLUCENT);
            System.out.println("TYPE_INT_ARGB: " + (BufferedImage.TYPE_INT_ARGB == image.getType()));
            System.out.println("Premultiplied: " + image.getColorModel().isAlphaPremultiplied());
        }
    }
}

Maybe TYPE_INT_ARGB is intentional in this case? Because for 8-bit components, straight alpha is better for doing ColorConvertOp and such.

Either way, if all I’m doing is Porter-Duff compositing, it wouldn’t hurt to use TYPE_INT_ARGB_PRE, right? :slight_smile:

You are running it on 6u10, right? And the pipeline is enabled?

Because on my system your test reports a premultiplied color model,
and the buffered image type is 3 (which is TYPE_INT_ARGB_PRE),
as it supposed to be.

Dmitri

Ah, that’s it. I’m getting straight alpha on 1.6.0_03, and premultiplied alpha on 6u10 when the D3D pipeline is enabled.