Loading a separate alpha channel

I’ve been programming for a long time, but just recently back to Java (since I left it in 1.1 4-5 years ago). Anyway, I’ve figured out the bulk of stuff in J2D, except for one thing:

Is there any canned way to load a separate file (or separate part of the same file) as the alpha channel of another? Say that I have 2 jpg files – one which contains an image, and one which contains the mask of that image… is there an easy way to use the mask image as the alpha channel of the original? I can write the function myself (it wouldn’t even be that hard to do – just pull in both files, and then transfer the grayscale mask image pixel-by-pixel as the alpha channel of the original image), but it seems like it would be extremely slow.

Keep in mind that I know I can use image formats such as GIF (which I don’t want to use due to the color depth) or PNG, which contain their own alpha information, but I wanted to know if there would be an easy way to get this to work (if for no other reason than to have all of my options open).

Thanks,
Chris

[quote]but it seems like it would be extremely slow.
[/quote]
You’ll be surprised…

With some low-lever access to bufferedimages you can easily replace the alpha channel (or any other channel) with a single call. getAlphaRaster() is a start.

Anyway the standard java image loaders doesn’t give you such granularity and I’ve never seen something similar in other third party libraries.

Thanks, javazoid – it worked. For anyone interested, I had two images – testpic is a straight RGB JPEG, and testpic-mask is a grayscale one. It was a simple matter of creating a new image ABGR, drawing the RGB image to this new image, and then catching the alpha raster of the destination image, and copying the raster of the mask image to that, (of course, I cheated because I knew the dimensions and color depths of all images involved, but it won’t be too hard to make a function from):

try
{
bi = (BufferedImage)ImageIO.read(new File(“testpic.jpg”));
bim = (BufferedImage)ImageIO.read(new File(“testpic-mask.jpg”));
}
catch (Exception e)
{
e.printStackTrace();
}

bif = new BufferedImage(100,100,BufferedImage.TYPE_4BYTE_ABGR);

Graphics2D bg = bif.createGraphics();
bg.drawImage(bi,null,0,0);

WritableRaster rs = (WritableRaster)bif.getAlphaRaster();
rs.setDataElements(0,0,bim.getRaster());

Rise From Your Grave

I found this thread and would like to use this method for a Qix clone, but I’m having a little trouble on my implementation. Here’s my terrible code:

try{
            src = ImageIO.read(new File(victim));
            img = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            img.getGraphics().drawImage(src,0,0,null);
            imgalpha = f**k.getAlphaRaster();
            
            alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            alpha.getGraphics().setColor(Color.white);
            alpha.getGraphics().fillOval(25, 25, 100, 400);
            fimgalpha.setDataElements(0, 0, alpha.getRaster());
            
 } catch (Exception e)  {
               System.out.println("YOU FAILURE.");
            e.printStackTrace(System.out);
}

Which - to my newbie eyes - looks like your code above, but it gives unexpected results.

The alpha looks like it’s being distorted. The white circle is where the alpha should be.

edit: almost forgot, trying this with any other image type (like TYPE_INT_ARGB) doesn’t seem to work at all, though I can’t understand why.

Nevermind. I r smart. S-M-R-T.
Wrong image type on the alpha mask. That’s what I get for using copy/paste on everything.