# include "Ximage.h"
# define XPIX(x,Rx,S) (x*S + Rx)

void
CreateZoom (layout, graphic, x, y)
Layout layout[];
Graphic graphic[];
double x, y;
{

  int i, j, ii, jj, X, Y;
  int zvalue;
  double value, expand, zero, range, zoomscale;
  double X0, Y0, Rx, Ry, Rz1, Rz2;
  int expand_int, dx, dy, DX, DY, Npixels, yfac;
  Visual *visual = CopyFromParent;
  unsigned long pixel;


  /* create a test pattern image to put in layout[0].picture */
  if (layout[0].matrix.size == 0) {
    REALLOCATE (layout[0].zoom.data, char, layout[0].zoom.dx*layout[0].zoom.dy);
    bzero (layout[0].zoom.data, layout[0].zoom.dx*layout[0].zoom.dy);
  }
  else {
    for (i = 0; i < 256; i++) 
      pixel[i] = layout[0].cmap[i].pixel;
    white = layout[0].white;
    
    zoomscale = MAX (5, layout[0].expand + 5);
    expand = 1 / zoomscale;
    expand_out = (int) zoomscale;
    expand_in  = 1;
    
    dx = layout[0].zoom.dx;
    dy = layout[0].zoom.dy;
    DX = layout[0].matrix.Naxis[0];
    DY = layout[0].matrix.Naxis[1];
    Rx = DX/2 - dx*expand/2.0 - X0;
    Ry = DY/2 - dy*expand/2.0 - Y0;  
    
    /* reassign as much for speed as clarity here */
    X0 = 0.5*layout[0].matrix.Naxis[0] - x;
    Y0 = 0.5*layout[0].matrix.Naxis[1] - y;
    dx = layout[0].picture.dx;
    dy = layout[0].picture.dy;
    DX = matrix[0].Naxis[0];
    DY = matrix[0].Naxis[1];
    Rx = DX/2 - dx*expand/2.0 - X0;
    Ry = DY/2 - dy*expand/2.0 - Y0;
    
    i_start = MAX (-Rx / expand, 0);
    j_start = MAX (-Ry / expand, 0);
    i_end   = MIN ((DX - Rx)/expand, dx - expand_out + 1);
    j_end   = MIN ((DY - Ry)/expand, dy - expand_out + 1);
    
    in_pix = (short *) matrix[0].buffer;
    out_pix = layout[0].zoom.data;
    for (j = 0; j < j_start; j++) {
      for (i = 0; i < dx; i++, out_pix++) {
	*out_pix = white;
      }
    }
    
  for (j = j_start; j < j_end; j+= expand_out) {
    Y = XPIX (j, Ry, expand);
    for (jj = 0; jj < expand_out; jj++) {
      out_pix = layout[0].zoom.data + (j+jj)*dx;  /* fencepost errors? */
      for (i = 0; i < i_start; i++, out_pix++) {
	*out_pix = white;
      }
    }
    out_pix = layout[0].zoom.data + j*dx + i_start;  
    X = XPIX (i_start, Rx, expand);
    in_pix2 = in_pix + DX*Y + X;
    for (i = i_start; i < i_end; i+= expand_out, in_pix2+= expand_in) { 
      zvalue = 0x003f & *in_pix2;
      for (ii = 0; ii < expand_out; ii++, out_pix++) {
	for (jj = 0; jj < expand_out; jj++) {
	  *(out_pix + jj*dx) = *(pixel + zvalue);
	}
      }
    }
    for (jj = 0; jj < expand_out; jj++) {
      out_pix = layout[0].zoom.data + (j+jj)*dx + i_end;  /* fencepost errors? */
      for (i = i_end; i < dx; i++, out_pix++) {
	*out_pix = white;
      }
    }
  }
  for (j = j_end; j < dy; j++) {
    out_pix = layout[0].picture.data + j*dx;
    for (i = 0; i < dx; i++, out_pix++) {
      *out_pix = white;
    }
  }
}
	
  /*** here, if expand is < 1 (fractional expansion), we sparsely sample the 
    original image, instead of box averaging - do we like this?  well, 
    it speeds things up alot! ***/




  layout[0].zoom.pix = XCreateImage (graphic[0].display, visual, graphic[0].depth, ZPixmap, 0, 
					layout[0].zoom.data, 
					layout[0].zoom.dx, layout[0].zoom.dy, 8, 0);
  
}

 
