# include "Ximage.h"

static float slope = 1.0;
static float start = 0.0;
static int MaxValue = 255;

// XXX inline this if needed
static int PixelLookup(float value) {
  int out;
  out = MIN (MAX (slope * value - start, 0), MaxValue);
  return (out);
}

void Remap8 (Graphic *graphic, KapaImageWidget *image, Matrix *matrix) {

  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, pixelN;
  double expand, Rx, Ry, X, Y;
  int expand_in, expand_out;
  unsigned char *out_pix, *out_pix2;
  float *imdata, *in_pix,  *in_pix2;
  unsigned long pixel[256], pixvalue;
  unsigned long back;

  // local array for pixel values
  for (i = 0; i < 256; i++) {
    pixel[i] = graphic[0].cmap[i].pixel;
  }
  back = graphic[0].back;

  // define the color transform parameters
  MaxValue = graphic[0].Npixels - 1;
  if (image[0].range != 0.0) {
    slope = graphic[0].Npixels / image[0].range;
    start = graphic[0].Npixels * image[0].zero / image[0].range;
  } else {
    slope = 1.0;
    start = image[0].zero;
  }

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

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

  /* X,Y are the image coordinates of the first image pixel */
  X = MAX(0.5*(DX - dx*expand) - image[0].X, 0);
  if ((int)X != X) 
    X = (int) X + 1;
  else 
    X = (int) X;
  Y = MAX(0.5*(DY - dy*expand) - image[0].Y, 0);
  if ((int)Y != Y) 
    Y = (int) Y + 1;
  else 
    Y = (int) Y;

  /* Rx,Ry are the screen coordinates of the first image pixel */
  Rx = (X + image[0].X - 0.5*DX)/expand + 0.5*dx;
  Ry = (Y + image[0].Y - 0.5*DY)/expand + 0.5*dy;

  i_start = MIN (MAX (Rx, 0), dx - expand_out + 1);
  j_start = MIN (MAX (Ry, 0), dy - expand_out + 1);
  
  if (image[0].expand > 0) {
    i_end = MAX (MIN (i_start + ((int)(expand*(dx-i_start)))/expand, expand_out*(DX-X) + Rx), 0);
    j_end = MAX (MIN (j_start + ((int)(expand*(dy-j_start)))/expand, expand_out*(DY-Y) + Ry), 0);
  } else {
    i_end = MAX (MIN (dx, (DX-X)/expand + Rx), 0);
    j_end = MAX (MIN (dy, (DY-Y)/expand + Ry), 0);
  }    

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

  out_pix = (unsigned char *) image[0].picture.data;
  imdata  = (float *) matrix[0].buffer;
  in_pix  = &imdata[(int)(DX*(int)MAX(Y,0) + (int)MAX(X,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, out_pix+=(expand_out-1)*dx, in_pix += expand_in*DX) {

    /**** fill in area to the left of the picture ****/
    for (jj = 0; (i_start > 0) && (jj < expand_out); 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++) {
	pixelN = PixelLookup(*in_pix2);
	*out_pix = pixel[pixelN];
      }
    } else {
      for (i = i_start; i < i_end; i+= expand_out, in_pix2++, out_pix+= expand_out) { 
	pixelN   = PixelLookup(*in_pix2);
	pixvalue = pixel[pixelN];
	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 = 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 = back;
      }
    }
    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 char *)image[0].picture.data < dx*dy); j++, out_pix++) { 
    *out_pix = back;
  }
  image[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
					image[0].picture.data, image[0].picture.dx, image[0].picture.dy, 8, 0);
}

