[s] how to create BufferedImage from alpha raster?

I want to separate the alpha channel of a BufferedImage (getAlphaRaster() ) and generate a new one which will look like a grey-scale image then.

My problem is that I do not find the right ColorModel which i can pass to the constructor of BufferedImage.

Has anyone done this before and can help?

I have solved the problem on my own. Here is how it goes:

First make sure the image really has a separate alpha channel. If image.getTransparency() == Transparency.TRANSLUCENT holds then image.getAlphaRaster() will not return null.

The generation of a BufferedImage which contains only the alpha channel requires ColorModel instance which matches the format of the alpha raster (the primary problem). Such a ColorModel can be created this way:

ColorModel grayscaleColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY),
new int[] {8}, false, false, ComponentColorModel.BITMASK, DataBuffer.TYPE_BYTE);

As you see the alpha channel is treated as a grayscale image. With that colormodel the BufferedImage can be created this way:

BufferedImage maskImage = new BufferedImage(grayscaleColorModel, image.getAlphaRaster(), false, new Hashtable());

Now we have an image which can be drawn into other (for iinstance RGBA) images.