import java.awt.*;

public class ImArray {
  int height, width;
  int im[], oim[];
  Image image;

  ImArray(int h, int w, int d[]){
    height = h;
    width = w;
    im = d;
    oim = null;
  }

  ImArray(int h, int w, int d[], int i[]){
    height = h;
    width = w;
    im = d;
    oim = i;
  }

  ImArray(int h, int w, int d[], int i[], Image jim){
    height = h;
    width = w;
    im = d;
    oim = i;
    image = jim;
  }

  ImArray shrink(int s){
    int nh = ((int) (height/s));
    int nw = ((int) (width/s));
    int[] nd = new int[(nh+1)*(nw+1)];
    for(int x=0;x<nw;x++){
      for(int y=0;y<nh;y++){
	nd[x+(nw*y)] = 0;
      }
    }    
    for(int x=0;x<width;x++){
      for(int y=0;y<height;y++){
	nd[((int) (x/s)) + (((int) (y/s))*nw)]  += im[x+(width*y)]/(s*s);
      }
    }
    return new ImArray(nh, nw, nd);
  }

  public static double imageDifference(ImArray im1, ImArray im2, cornerTransform ct){
    double diff = 0;
    double pixct = 0;

    for(int ctx=0;ctx<(im1.width);ctx++){
      for(int cty=0;cty<(im1.height);cty++){
	int ttx, tty;
	ttx = ct.x(ctx, cty);
	tty = ct.y(ctx, cty);
	if((ttx >= 0) && (tty >= 0) &&
	   (ttx < im2.width) && (tty < im2.height)){
	  if((ctx > (.9*im1.width)) || (cty > (.9*im1.height)) ||
	     (ctx < (.1*im1.width)) || (cty < (.1*im1.height))){
	    diff += 2*Math.abs(im1.im[ctx+(cty*im1.width)]-im2.im[ttx+(tty*im2.width)]);
	    pixct+=2;
	  }
	  else {
	    diff += Math.abs(im1.im[ctx+(cty*im1.width)]-im2.im[ttx+(tty*im2.width)]);
	    pixct++;
	  }
	}
	else{
	  diff += 0;
	}
      }
    }
    return diff/pixct;
  }


}

