title: How to zoom out an image by Bilinear Interpolation in java?
body: <p>I am trying to read an image, zoom it in to 80*60 and then zoom out the resulted image 5 times by bilinear Interpolation method. But I get this error :    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4800 . Can anyone help me please? </p>
  
  <p>this is what I have done : </p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.Image;
  import java.awt.RenderingHints;
  import java.awt.image.BufferedImage;
  import java.awt.image.DataBufferByte;
  import java.awt.image.WritableRaster;
  import java.io.ByteArrayInputStream;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  
  import javax.imageio.ImageIO;
  
  public class BiInterpolationTest {
  
      public static int zh;
      public static int zw;
  
  
      public static void main(String[] args) throws IOException {
          // TODO Auto-generated method stub
  
  
          int[][] savedImage;
  
          File  f = new File ("F:\\Java\\Gray Scale Images\\3.jpg"); 
          savedImage = readimage(f);
          BufferedImage grayImage = new BufferedImage(savedImage.length, savedImage[0].length, BufferedImage.TYPE_BYTE_GRAY);
  
          for (int i =0 ; i&lt;savedImage.length ; i ++){
          for (int j=0 ; j&lt;savedImage[0].length ; j++){
                  int rgb = savedImage[i][j];
                  rgb = (rgb&lt;&lt;16)|(rgb&lt;&lt;8)|(rgb);
                  grayImage.setRGB(i, j, rgb);
  
                  BufferedImage zoomin =ScaledImage(grayImage, 80,60);
                   zh = zoomin.getHeight();
                   zw = zoomin.getWidth();
  
  
          byte[] tempArr;
              tempArr = extractBytes(zoomin);
                  // TODO Auto-generated catch block
  
          byte [] zoomout;
          zoomout = bilineraInterpolation(tempArr , 5); 
          InputStream in = new ByteArrayInputStream(zoomout);
          BufferedImage bImageInterpolated;
          try {
              bImageInterpolated = ImageIO.read(in);
              ImageIO.write(bImageInterpolated, "jpg", new File(
                      "F:/new-darksouls.jpg"));
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          }
  
          }
  
      }
  
          public static BufferedImage ScaledImage(Image img, int w , int h){
  
          BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_BYTE_GRAY);
          Graphics2D g2 = resizedImage.createGraphics();
          g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
          g2.drawImage(img, 0, 0, w, h, null);
          g2.dispose();
          return resizedImage;
          }
          /////////////////////////////////////
          public static byte[] extractBytes (BufferedImage Image) throws IOException {
  
               BufferedImage bufferedImage = new BufferedImage(Image.getHeight(), Image.getWidth(), BufferedImage.TYPE_BYTE_GRAY);
  
               // get DataBufferBytes from Raster
               WritableRaster raster = bufferedImage .getRaster();
               DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();
  
               return ( data.getData() );
              }    
  
  
       public static int[][] readimage(File filename){
              BufferedImage img;
  
              try {
                  img = ImageIO.read(filename);
  
                  // Gray_scaled Image output
  
                  int width = img.getWidth();
                  int height = img.getHeight();
                  ImagePro.fw=width;
                  ImagePro.fh = height;
                  int [][] readimageVal = new int [width][height];
                  for (int i = 0; i&lt;height ; i++){
                      for (int j =0  ; j&lt;width ; j++){
  
                          Color c = new Color(img.getRGB(j, i));
                             int r= (int)(c.getRed() * 0.299)&amp;0xff;
                             int g = (int)(c.getGreen() * 0.587)&amp;0xff;
                             int b = (int)(c.getBlue() *0.114)&amp;0xff;
                             int avg = ((r+b+g));
  
                          readimageVal[j][i] = avg;
                              }
                              }
                  return readimageVal;
  
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              return null;
          }
  
  
       public static byte[] bilineraInterpolation(byte[] ImgData, int ratio) {
  
         int wf = zw*ratio;
         int hf = zh*ratio;
         byte[] tempArr = new byte[wf*hf] ;
         int A, B, C, D, x, y, index, g ;
         float x_ratio = ((float)(zw))/wf ;
         float y_ratio = ((float)(zh))/hf ;
         float x_diff, y_diff ;
         int in = 0 ;
         for (int i=0;i&lt;hf;i++) {
             for (int j=0;j&lt;wf;j++) {
                 x = (int)(x_ratio * j) ;
                 y = (int)(y_ratio * i) ;
                 x_diff = (x_ratio * j) - x ;
                 y_diff = (y_ratio * i) - y ;
                 index = y*zw+x ;
  
                 // range is 0 to 255 thus bitwise AND with 0xff
                 A = ImgData[index] &amp; 0xff ;
                 B = ImgData[index+1] &amp; 0xff ;
                 C = ImgData[index+zw] &amp; 0xff ;
                 D = ImgData[index+zw+1] &amp; 0xff ;
  
                          g = (int)(
                         A*(1-x_diff)*(1-y_diff) +  B*(x_diff)*(1-y_diff) +
                         C*(y_diff)*(1-x_diff)   +  D*(x_diff*y_diff)
                         ) ;
  
                 tempArr[in++] = (byte) g ;                                   
             }
         }
         return tempArr ;
     }
       }
  </code></pre>