Additive blitting?

OK I give up :smiley: Googled, looked through the forum, and the answer eludes me…

I want to blit an image on the screen, then draw another image on top of it.

With the second image though, I want to use an ADD rop - in other words, the RGB of the second image is added to the destination (rather than the source replacing the dest, with transparency, as normal). To make things more complicated the second image has alpha, so it’s semi transparent.

Is there a nice easy render op mode I can set to do this? :smiley:

Thanks,

M

getGraphics().paint(…) :slight_smile:

This is quite easy. Refering to AlphaComposite class, add the setComposite(AlphaComposite) to the Graphics2D instance of the dest’ image then draw the source on it. I use AlphaComposite.SRC_OVER as default alpha-rendering mode. You’ll get an “ADD r. op.” as requested.
it looks like:

Image dest = ImageIO.read("myImageFile.bmp");
Image src = ImageIO.read("myImageFile2.bmp");
static Image addROP(Image dest, Image src) {
    JComponent obs = new JLabel("virtual observer");
    MediaTracker mt = new MediaTracker(obs);
    mt.addImage(dest, 0);
    mt.addImage(src, 0);
    try{ 
       mt.waitForAll(); 
       Graphics2D g2 = (Graphics2D)dest.getGraphics();
       g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
       g2.drawImage(src, null, obs);
       g2.dispose();
    }  catch(InterruptedException e) {} finally {
       return dest;
    }
}

consider the 2 images, they must be the same size or you should add an extra AffineTransform scaling-op to the drawImage method.

Ah thanks :slight_smile:

I looked into SRC_OVER, but I thought it just did a normal “destructive” blit…

I’ll bung that code in and see what happens :smiley:

Hmm, no different to a normal drawImage (as far as I can tell)…

Just to be clear, what I’m trying to do is, for example:

Source pixel RGB: 128, 128, 128
Dest pixel RGB: 64, 200, 64

Result: 192, 255, 192

Well, there is no additive blending in Java 2D. So you’ve got to use OpenGL, or write the additive blending code by hand.

PulpCore (http://www.interactivepulp.com/pulpcore/) does additive blending via software rendering, but it’s just for applets.

Oops, I meant drawImage(…) not paint lol; I remember using it way back and it did alpha blending using the alpha channels of the pixels. Also the MemoryImageSource’s newPixels() method does additive blending, using your alpha values taking the RGB components as pre multiplied values.

Not yet anyway… It’s planned for JDK 7 though in a new class called PhotoComposite, which will offer other PhotoShop-style blending modes as well. See this RFE:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5094232

Chris

[quote=“brackeen,post:6,topic:30198”]

But the MemoryImageSource does it in newPixels()!!!

Sorry, what do you mean? Do you mean bleding my hand - because that could be also done with BufferedImage (MemoryImageSource is more or less deprecated).

lg Clemens

Well yes and no. It takes pre-multiplied values so the RGB values would want to correspond to your alpha values (if you want to be pedantic with the pixel maths). Having said that if you just wanted to work with 50% transparency then you just write color = 0x80000000 | ((color&fefefe)>>1)! That premultiplies the pixel with 50% alpha in one shot - however BufferedImage allows for non-premultiplied RGBA values.

And deprecated is a harsh word for a class that isn’t yet deprecated :wink: