# include "Ximage.h"

# define MY_SWAP_BYTE(W) { \
  char tmp, *X; \
  X = (char *) &W; \
  tmp = X[0]; X[0] = X[1]; X[1] = tmp; }

void Remap16 (Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix) {

  int i, j, ii, jj;
  int i_start, i_end, j_start, j_end;
  int dropback;
  int dx, dy, DX, DY;
  double expand, Ix, Iy;
  int expand_in, expand_out;
  unsigned short *out_pix, *out_pix2, *data;
  unsigned short *in_pix, *in_pix2;
  unsigned short *pixel, pixvalue;
  unsigned short back;
  int swap_client, swap_server, swap_bytes;

  ALLOCATE (pixel, unsigned short, graphic[0].Npixels);

# ifdef BYTE_SWAP
  swap_client = 1;
# else 
  swap_client = 0;
# endif  

  swap_server = ImageByteOrder (graphic[0].display);
  swap_bytes = !(swap_client ^ swap_server);

  // local array for pixel values (is this working??)
  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
    pixel[i] = 0xffff &  graphic[0].cmap[i].pixel;
    if (swap_bytes) MY_SWAP_BYTE (pixel[i]);
  }
  back = 0xffff & graphic[0].back;
  if (swap_bytes) MY_SWAP_BYTE (back);
  // XXX not certain this is the correct solution...

  // set up expansions
  assert ((picture[0].expand >= 1) || (picture[0].expand <= -2));
  expand = expand_in = expand_out = 1.0;
  if (picture[0].expand > 0) {
    expand = 1 / (1.0*picture[0].expand);
    expand_out = picture[0].expand;
    expand_in  = 1;
  }
  if (picture[0].expand < 0) {
    expand = fabs((double)picture[0].expand);
    expand_out = 1;
    expand_in  = -picture[0].expand;
  }

  // define the image boundaries
  dx = picture[0].dx;
  dy = picture[0].dy;
  DX = matrix[0].Naxis[0];
  DY = matrix[0].Naxis[1];

  // x, y are the closest lit screen pixel to 0,0
  Picture_Lower (&i_start, &j_start, matrix, picture);

  // x, y are the closest lit screen pixel to dx, dy
  Picture_Upper (&i_end, &j_end, matrix, picture);

  // Ix,Iy are the first displayed image pixel
  Picture_to_Image (&Ix, &Iy, i_start, j_start, picture);

  dropback = expand_out - (i_end - i_start) % expand_out;
  if ((i_end - i_start) % expand_out == 0) dropback = 0;

  data = out_pix = (unsigned short *) picture[0].data;
  in_pix  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];

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

  /**** fill in bottom area ****/
  for (j = 0; j < dx*j_start; j++, out_pix++) {
    *out_pix = back;
  }
  
  for (j = j_start; j < j_end; j+= expand_out, in_pix += expand_in*DX) {
    out_pix = &data[j*dx];

    /**** fill in area to the left of the picture ****/
    for (jj = 0; (i_start > 0) && (jj < expand_out) && (j + jj < dy); jj++) { 
      out_pix2 = out_pix + jj*dx;
      for (i = 0; i < i_start; i++, out_pix2++) {
	*out_pix2 = back;
      }
    }
    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 = 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) && (j + jj < dy); jj++, out_pix2+=(dx-expand_out)) {
	  for (ii = 0; ii < expand_out; ii++, out_pix2++) {
	    *out_pix2 = pixvalue; 
	  }
	}
      }
    }
    out_pix -= dropback;
    
    /**** fill in area to the right of the picture ****/
    for (jj = 0; (jj < expand_out) && (j + jj < dy); jj++) {
      out_pix2 = out_pix + jj*dx;
      for (i = i_end; i < dx; i++, out_pix2++) {
	*out_pix2 = back;
      }
    }
  } 
  
  /**** fill in top area ****/
  out_pix = &data[j_end*dx];
  for (j = 0; j < (dy - j_end); j++) {
    for (i = 0; i < dx; i++, out_pix++) { 
      *out_pix = back;
    }
  }

  picture[0].pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
					picture[0].data, picture[0].dx, picture[0].dy, 16, 0);

  free (pixel);
}
