Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
If it is possible to read the image pixel by pixel, then each pixel is RRGGBB in hex representation. Accordingly, to get the red channel, you need to apply a mask of 110000 to each pixel. Each channel has its own mask - green 001100 and blue - 000011.
To assemble it back - sum the three corresponding pixels from the three representations
public static BufferedImage[] splitToRGB(BufferedImage original) {
BufferedImage R = new BufferedImage(
original.getWidth(), original.getHeight(),
BufferedImage.TYPE_INT_RGB
);
BufferedImage G = new BufferedImage(
original.getWidth(), original.getHeight(),
BufferedImage.TYPE_INT_RGB
);
BufferedImage B = new BufferedImage(
original.getWidth(), original.getHeight(),
BufferedImage.TYPE_INT_RGB
);
for (int x = 0; x < original.getWidth(); x++)
for (int y = 0; y < original.getHeight(); y++) {
final int rgb = original.getRGB(x, y);
R.setRGB(x, y, rgb & 0xff0000);
G.setRGB(x, y, rgb & 0xff00);
B.setRGB(x, y, rgb & 0xff);
}
return new BufferedImage[]{R, G, B};
}
public static BufferedImage mergeRGB(BufferedImage R, BufferedImage G, BufferedImage B) {
BufferedImage original = new BufferedImage(
R.getWidth(), R.getHeight(),
BufferedImage.TYPE_INT_RGB
);
for (int x = 0; x < original.getWidth(); x++)
for (int y = 0; y < original.getHeight(); y++) {
final int rgb = R.getRGB(x, y) | G.getRGB(x, y) | B.getRGB(x, y);
original.setRGB(x, y, rgb);
}
return original;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question