views:

460

answers:

1

I know its possible to convert an image to CS_GRAY using

public static BufferedImage getGrayBufferedImage(BufferedImage image) {
 BufferedImageOp op = new ColorConvertOp(ColorSpace
   .getInstance(ColorSpace.CS_GRAY), null);
 BufferedImage sourceImgGray = op.filter(image, null);

 return sourceImgGray;
}

however, this is a chokepoint of my entire program. I need to do this often, on 800x600 pixel images and takes about 200-300ms for this operation to complete, on average. I know I can do this alot faster by using one for loop to loop through the image data and set it right away. The code above on the other hand constructs a brand new 800x600 BufferedImage that is gray scale. I would rather just transform the image I pass in.

Does any one know how to do this with a for loop and given that the image is RGB color space?

A: 

ColorConvertOp.filter takes two parameters. The second parameter is also a BufferedImage, which will be the destination. If you pass a correct BufferedImage to the filter method it saves you from the hassle to create a fresh BufferedImage.

ablaeul