Java Convert ColorSpace of BufferedImage to CS_GRAY without using ConvertColorOp

Posted by gmatt on Stack Overflow See other posts from Stack Overflow or by gmatt
Published on 2009-05-20T08:39:55Z Indexed on 2010/05/10 17:14 UTC
Read the original article Hit count: 288

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about bufferedimage