How to work with BufferedImage and YCbCr colorspace?
I need to translate colors in bitmap loaded to BufferedImage from RGB to YCbCr (luminance and 2 channels chrominance) and back after process.
I made it with functions used like rgb2ycbcr() in main method for each pixel, but it isn't so smart solution. I should use ColorSpace and ColorModel cla开发者_运维知识库sses to get BufferedImage with correct color space. It would be more flexible method, but I don't know how to do that.
I'm lost and I need some tips. Can somebody help me?
As I understood your question, you want to do the following:
Load RGB image -> process YCbCr image -> Use RGB image again
And you want us to help you, to make this process as seamless as possible. First and foremost you want us to give you a simple way to avoid the ->
(converting) parts.
Well I looked into the BufferedImage
documentation. It seems, as if there doesn't exist a way to change the ColorSpace
of an once created BufferedImage
.
You could create a new BufferedImage
with an YCbCr color space for that you can use the predefined ICC_ColorSpace
. Then you copy the data from your old image possibly via ColorSpace.fromRGB
to the YCbCr color space, do the image processing and then convert again via ColorSpace.toRGB
. This method requires you to fully convert the image before and after processing via existing methods. Furthermore you have to know, how ICC_ColorSpace
converts your image to YCbCr color space. Otherwise you can't know, which array indices corresponds to the same pixel.
If you just want to create a wrapper around the RGB-BufferedImage
that lets you manipulate this image, as if it was an YCbCr image, that isn't possible with BufferedImage
.
EDIT:
To convert the color space of a BufferedImage
use ColorConvertOp
. The code would look something like this:
ColorConvertOp cco = new ColorConvertOp(new YCbCrColorSpace(), null);
BufferedImage ycbcrImage = cco.filter( oldRGBImage, null );
This requires you to either write your own ColorSpace
class or you could download and use the classes mentioned here. If you just want to load a JPEG image you should use the predefined classes.
精彩评论