Reputation: 68
I separate the colors of my image in three others BufferedImage and when i put them together again, i only got blue. When i test for the red image really get red color i see that it is blue too and it's exactly the same thing with the green. Here's my code.
public void modifyImage(BufferedImage image) {
BufferedImage green = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
BufferedImage blue = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
BufferedImage red = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for(int y= 0; y < image.getHeight(); y ++){
for(int x = 0; x < image.getWidth(); x++){
red.setRGB(x, y, new Color(image.getRGB(x, y)).getRed());
green.setRGB(x, y, new Color(image.getRGB(x, y)).getGreen());
blue.setRGB(x, y, new Color(image.getRGB(x, y)).getBlue());
}
}
BufferedImage finalImage = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_RGB);
for(int y= 0; y < image.getHeight(); y ++){
for(int x = 0; x < image.getWidth(); x++){
finalImage.setRGB(x, y, new Color(new Color(red.getRGB(x, y)).getRed(),new Color(green.getRGB(x, y)).getGreen(),new Color(blue.getRGB(x, y)).getBlue()).getRGB());
}
}
this.image = green;
}
So where is my mistake? What did i miss?
Upvotes: 2
Views: 395
Reputation: 111219
The Color
methods getRed
, getGreen
, and getBlue
return the intensity of that color component as a number from 0 to 255. If you interpret these numbers as packed RGB colors, they fill only the blue color component, so that's why you only get blue. You would need to do a bitwise right shift to get the color component to its right place.
Then again, you don't need to use these methods to extract colors in the first place, just use masks:
red.setRGB(x, y, image.getRGB(x, y)&0xFF0000);
green.setRGB(x, y, image.getRGB(x, y)&0x00FF00);
blue.setRGB(x, y, image.getRGB(x, y)&0x0000FF);
Upvotes: 4