I made a program to separate red blue and green components of a image but the code below gives an error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318) at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888) at rgb.Rgb.main(Rgb.java:46):
Here is the source code:
public static void main(String[] args) { String type = "jpg"; BufferedImage img = null; try { img = ImageIO.read(new File("d:\\a.jpg")); System.out.println(img.getWidth() + " " + img.getHeight()); } catch (IOException ex) { Logger.getLogger(Rgb.class.getName()).log(Level.SEVERE, null, ex); } BufferedImage rp, gp, bp; rp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB); bp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB); gp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB); for (int i = 1; i <= img.getHeight(); i++) { for (int j = 1; j <= img.getWidth(); j++) { int pixel = img.getRGB(i, j); int alpha = pixel & 0xff000000; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; rp.setRGB(i, j, alpha | (red << 16)); gp.setRGB(i, j, alpha | (green << 8)); bp.setRGB(i, j, alpha | blue); } } try { ImageIO.write(rp, type, new File("d:\\red.jpg")); ImageIO.write(gp, type, new File("d:\\green.jpg")); ImageIO.write(bp, type, new File("d:\\blue.jpg")); } catch (IOException ex) { Logger.getLogger(Rgb.class.getName()).log(Level.SEVERE, null, ex); }