# include "Ximage.h"
# define XPIX(x,Rx,S) (x*S + Rx)
# define ZPIX(value,Rz1,Rz2) (value*Rz1 - Rz2)
# define FRAC(a) ((a) - (int)(a))

void CreateZoom32 (Layout *layout, Graphic *graphic, double x, double y) {

  int i, j, ii, jj;
  int i_start, i_end, j_start, j_end;
  int dropback;  /* this is a bit of a kludge... */
  int dx, dy, DX, DY;
  double expand, zoomscale, Rx, Ry;
  int expand_in, expand_out;
  unsigned int *out_pix, *out_pix2, *data;
  unsigned char  *in_pix,  *in_pix2;
  unsigned long pixel[256], pixvalue;
  unsigned long white;

  if (layout[0].matrix.size == 0) {  /* create a test pattern */
    REALLOCATE (layout[0].zoom.data, char, 4*layout[0].zoom.dx*layout[0].zoom.dy);
    layout[0].zoom.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
				       layout[0].zoom.data, layout[0].zoom.dx, layout[0].zoom.dy, 32, 0);
    return;
  }

  for (i = 0; i < 256; i++) { /* set up pixel array */
    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 = x - expand*(int)(0.5*(dx + 1)) + 1;
  Ry = y - expand*(int)(0.5*(dy + 1)) + 1;

  i_start = MIN (MAX (-Rx / expand, (1 - FRAC(Rx)) / expand), dx - expand_out + 1);
  j_start = MIN (MAX (-Ry / expand, (1 - FRAC(Ry)) / expand), dy - expand_out + 1);
  i_end   = MAX (MIN ((DX-Rx) / expand, dx - expand_out + 1), 0);
  j_end   = MAX (MIN ((DY-Ry) / expand, dy - expand_out + 1), 0);
  dropback = expand_out - (i_end - i_start) % expand_out;
  if ((i_end - i_start) % expand_out == 0) dropback = 0;

  out_pix = (unsigned int *) layout[0].zoom.data;
  data = (unsigned int *) layout[0].zoom.data;
  in_pix  = (unsigned char *) (layout[0].matrix.buffer) + DX*(int)MAX(Ry,0) + (int)MAX(Rx,0);

  /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/

  if ((i_end < i_start) || (j_end < j_start)) {
    for (j = 0; j < dx*dy; j++, out_pix++) 
      *out_pix = white;
    return;
  } 
  

  /**** fill in bottom area ****/
  for (j = 0; j < dx*j_start; j++, out_pix++) {
    out_pix[0] = white;
  }

  for (j = j_start; j < j_end; j+= expand_out, out_pix+=(expand_out-1)*dx, in_pix += expand_in*DX) {

    /**** fill in area to the left of the picture ****/
    for (jj = 0; jj < expand_out; jj++) { 
      out_pix2 = out_pix + jj*dx;
      for (i = 0; i < i_start; i++, out_pix2++) {
	out_pix[0] = white;
      }
    }
    out_pix += i_start;
    
    /*** fill in the picture region ***/
    in_pix2 = in_pix;
    if (expand_out == 1) {
      for (i = i_start; i < i_end; i++, in_pix2+= expand_in, out_pix++) {
	out_pix[0] = pixel[*in_pix2];
      }
    }
    else {
      for (i = i_start; i < i_end; i+= expand_out, in_pix2++, out_pix+= expand_out) { 
	pixvalue = *(pixel + *in_pix2);
	out_pix2 = out_pix;
	for (jj = 0; jj < expand_out; jj++, out_pix2+=(dx-expand_out)) {
	  for (ii = 0; ii < expand_out; ii++, out_pix2++) {
	    out_pix2[0] = pixvalue; 
	  }
	}
      }
    }
    out_pix -= dropback;

    
    /**** fill in area to the right of the picture ****/
    for (jj = 0; jj < expand_out; jj++) {
      out_pix2 = out_pix + jj*dx;
      for (i = i_end; i < dx; i++, out_pix2++) {
	out_pix2[0] = white;
      }
    }
    out_pix += (dx - i_end);
  } 
  
  if ((j_end - j_start) % expand_out > 0)
    out_pix -= (expand_out - (j_end - j_start) % expand_out);
  
  /**** fill in top area ****/
  for (j = 0; (j < dx*(dy - j_end)) && (out_pix - (unsigned int *)layout[0].zoom.data < dx*dy); j++, out_pix++) { 
    out_pix[0] = white;
  }

  out_pix = (unsigned int *)layout[0].zoom.data;
  layout[0].zoom.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
					layout[0].zoom.data, layout[0].zoom.dx, layout[0].zoom.dy, 32, 0);
  
}
