Java: extract Alpha Channel from BufferedImage?

I don't believe there's a single method call to do this; you would have to get all the image data and mask off the alpha byte for each pixel. So for example, use getRGB() to get the ARGB pixels for the image. The most significant byte is the alpha value.So for each pixel in the array you get from getRGB() int alpha = (pixel >> 24) & 0xFF.

I don't believe there's a single method call to do this; you would have to get all the image data and mask off the alpha byte for each pixel. So for example, use getRGB() to get the ARGB pixels for the image. The most significant byte is the alpha value.So for each pixel in the array you get from getRGB(), int alpha = (pixel >> 24) & 0xFF.

Ok, thanks. How would I mask off the alpha byte from one pixel? – clamp May 30 at 12:59 Added a little more detail.

– Ernest Friedman-ll May 30 at 13:06 I think this is possible without manually looping over the pixels which might result in better performance on some platforms, see my answer. – Waldheinz May 30 at 13:11.

Not tested but contains the main points. Public Image alpha2gray(BufferedImage src) { if (src.getType()! = BufferedImage.

TYPE_INT_ARGB) throw new RuntimeException("Wrong image type. "); int w = src.getWidth(); int h = src.getHeight(); int srcBuffer = src.getData(). GetPixels(0, 0, w, h, null); int dstbuffer = new intw * h; for (int i=0; i> 24) & 0xff; dstBufferi = a | a >> 8 | a >> 16; } return Toolkit.

GetDefaultToolkit(). CreateImage(new MemoryImageSource(w, h, pix, 0, w)); }.

You could grab the Raster from the BufferedImage, and then create a child Raster of this one which contains only the band you're interested in (the bandList parameter). From this child raster you can create a new BufferedImage with a suitable ColorModel which would only contain the grayscale aplpha mask. The benefit of doing it this way instead of manually iterating over the pixels is that the runtime has chance to get an idea of what you are doing, and thus this might get accelerated by exploiting the hardware capabilities.

Honestly I doubt it will be accelerated with current JVMs, but who knows what the future brings?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions