title: java : get differences between two images
body: <p>i am trying to get difference between two images ( same size ) i found this code :</p>
  
  <pre><code>            BufferedImage img1 = null;
              BufferedImage img2 = null;
  
              try{            
                  URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg");
                  URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");
                  img1 = ImageIO.read(url1);
                  img2 = ImageIO.read(url2);
               } catch (IOException e) {
                  e.printStackTrace();
               }
               int width1 = img1.getWidth(null);
               int width2 = img2.getWidth(null);
               int height1 = img1.getHeight(null);
               int height2 = img2.getHeight(null);
  
               if ((width1 != width2) || (height1 != height2)) {
                   System.err.println("Error: Images dimensions mismatch");
                   System.exit(1);
               }
  
               long diff = 0;
               for (int i = 0; i &lt; height1; i++) {
                   for (int j = 0; j &lt; width1; j++) {
                       int rgb1 = img1.getRGB(i, j);
                       int rgb2 = img2.getRGB(i, j);
                       int r1 = (rgb1 &gt;&gt; 16) &amp; 0xff;
                       int g1 = (rgb1 &gt;&gt;  8) &amp; 0xff;
                       int b1 = (rgb1      ) &amp; 0xff;
                       int r2 = (rgb2 &gt;&gt; 16) &amp; 0xff;
                       int g2 = (rgb2 &gt;&gt;  8) &amp; 0xff;
                       int b2 = (rgb2      ) &amp; 0xff;
                       diff += Math.abs(r1 - r2);
                       diff += Math.abs(g1 - g2);
                       diff += Math.abs(b1 - b2);
                   }
               }
               double n = width1 * height1 * 3;
               double p = diff / n / 255.0;
               System.out.println("diff percent: " + (p * 100.0));    `
  </code></pre>
  
  <p>it works fine for the two images given in the url , but when i changed the images given i get this exception :</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
  at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:299)
  at java.awt.image.BufferedImage.getRGB(BufferedImage.java:871)
  at Main.main(Main.java:77)
  </code></pre>
  
  <p>I changed the code to : </p>
  
  <pre><code>          File sourceimage1 = new File("C:\\lo.jpg");
            File sourceimage2 = new File("C:\\lo1.jpg");
            img1 = ImageIO.read(sourceimage1);
            img2 = ImageIO.read(sourceimage2);
  </code></pre>
  
  <p>the two images are black and white , and their dimensions are smaller than the two previous images <code>(lenna50 and lenna100)</code> 
  <strong>the lo.jpg and lo1.jpg are the same image to test the algorithm also they are in black and white</strong> 
  how can I change the code to make it work for any image dimension ?</p>