BufferedImage swap red and blue channel

3061 views java
3

My goal is to swap the red and blue channels of a java BufferedImage.

Is there any way to achieve this other than inefficiently iterating over each pixel value und swapping the channels? I was thinking of some bitwise magic or some integrated function I don't know of.

Any help is appreciated.

answered question

1 Answer

2

Maybe you create an new instance of BufferedImage with the same WritableRaster raster and the same properties, but with a ColorModel with the colors swaped:

e.g:

ColorModel swappedColorModel = new DirectColorModel(24,
                                              0x000000ff,   // Red -> Blue
                                              0x0000ff00,   // Green
                                              0x00ff0000,   // Blue -> Red
                                              0x0           // Alpha
                                              );
BufferedImage swapedColorImage = new BufferedImage (swappedColorModel,
                                                    originalImage.getRaster(),
                                                    swappedColorModel.isAlphaPremultiplied(),
                                                    properties);

I didn't tried this code

posted this

Have an answer?

JD

Please login first before posting an answer.