I have stored a RGB image in a BufferedImage, what I want to do is take each color (eg. red) and store it in a new BufferedImage. So at the end, I will have four BufferedImage, the original one and one for each color. Each BufferedImage for each color should be 8 bits per pixel and this 8 bits contain the color value.
This is what I did.
String fileName = "test.jpg";
File inFile = new File(fileName);
BufferedImage refImg = ImageIO.read(inFile);
BufferedImage redImage = new BufferedImage(refImg.getWidth(), refImg.getHeight(), BufferedImage.TYPE_BYTE_GRAY); // LINE 4
BufferedImage greenImage = new BufferedImage(refImg.getWidth(), refImg.getHeight(), refImg.getType());
BufferedImage blueImage = new BufferedImage(refImg.getWidth(), refImg.getHeight(), refImg.getType());
for (int i = 0; i < refImg.getWidth(); i++)
for (int j = 0; j < refImg.getHeight(); j++)
{
int rgb = refImg.getRGB(i, j);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
int ronly = (red << 16) | (0 << 8) | 0;
int gonly = (0 << 16) | (green << 8) | 0;
int bonly = (0 << 16) | (0 << 8) | blue;
redImage.setRGB(i, j, ronly);
greenImage.setRGB(i, j, gonly);
blueImage.setRGB(i, j, bonly);
}
File redOut = new File("testRed.jpg");
File greenOut = new File("testGreen.jpg");
File blueOut = new File("testBlue.jpg");
ImageIO.write(redImage, "jpg", redOut);
ImageIO.write(greenImage, "jpg", greenOut);
ImageIO.write(blueImage, "jpg", blueOut);
However, I am still in the RGB color model. When I changed the type of the bufferedImage for the red one to GRAY (LINE 4), I do not get the red component.
Any help or direction.
Color(int, boolean)to convert a packedintinto aColorobject, then I can usegetRed/Green/Blueto get the individual colors, you could then use something likenew Color(pixel.getRed(), 0, 0).getRGB()to create packedintvalue for the specific color. In my "quick" test, this worked fineBufferedImage.TYPE_BYTE_GRAYalso looks wrong, it probably should beBufferedImage.TYPE_INT_RGB.intconversion seems fine ;)