# include <kapa_internal.h>

int KiiNewPicture1D (int fd, KiiImage *image, KiiDisplayMode *mode, Coords *coords) {

  int i;
  int Npix, Ncolors, NNcolors, size;
  float *in, min, max;
  char *out, *outbuffer, *root;
  double a1, a2;

  Npix = image[0].Nx*image[0].Ny;

  KiiSendCommand (fd, 4, "READ"); /* tell Ximage to look for the incoming image */
  KiiScanMessage (fd, "%d", &Ncolors);

  ALLOCATE (outbuffer, char, Npix);
  out = outbuffer;
  in = image[0].data1d;

  /* need to invert the logic if range < 0 */

  /* define color table, */
  NNcolors = Ncolors - 1;
  if (mode[0].logflux) {
    mode[0].range = MAX (2, mode[0].range);
    a1 = Ncolors / log10 (mode[0].range);
    a2 = mode[0].zero;
    for (i = 0; i < Npix; i++, in++, out++) {
      *out = (char) MIN (a1 * log10 (MAX (*in - a2, 1.0)), NNcolors);
    }
  } else {
    a1 = Ncolors / mode[0].range;
    a2 = Ncolors * mode[0].zero / mode[0].range;
    for (i = 0; i < Npix; i++, in++, out++) {
      *out = (char) MIN (MAX (a1 * *in - a2, 0), NNcolors);
    }
  }
  
  /* these are for a future upgrade */
  min = max = 0.0;
  size = Npix*sizeof(char);

  /* done with the conversion, now send Ximage the converted picture */
  KiiSendMessage (fd, "%8d %8d", image[0].Nx, image[0].Ny);
  KiiSendMessage (fd, "8 1 0.0 1.0");
  KiiSendMessage (fd, "%f %f ", mode[0].zero, mode[0].range);
  KiiSendMessage (fd, "%f %f %d ", min, max, size);
  KiiSendMessage (fd, "%f %f %g %g %g ", coords[0].crval1, coords[0].crpix1, coords[0].cdelt1, coords[0].pc1_1, coords[0].pc1_2);
  KiiSendMessage (fd, "%f %f %g %g %g ", coords[0].crval2, coords[0].crpix2, coords[0].cdelt2, coords[0].pc2_1, coords[0].pc2_2);

  /* send the filename */
  root = filerootname (image[0].file);
  KiiSendMessage (fd, "%s %s %s ", coords[0].ctype, root, image[0].name);
  free (root);

  /* send the image data */
  write (fd, outbuffer, size);
  free (outbuffer);

  return (TRUE);
}

int KiiNewPicture2D (int fd, KiiImage *image, KiiDisplayMode *mode, Coords *coords) {

  int i, j;
  int Npix, Ncolors, NNcolors, size;
  float *in, min, max;
  char *out, *outbuffer, *root;
  char buffer[1024];
  double a1, a2;

  Npix = image[0].Nx*image[0].Ny;

  KiiSendCommand (fd, 4, "READ"); /* tell Ximage to look for the incoming image */

  /* convert from (float *) to 1 byte chars -- depends on how many colors Kii owns */
  /* use this response as a handshake test */
  read (fd, buffer, 16); 
  sscanf (buffer, "%*s %d", &Ncolors); 

  ALLOCATE (outbuffer, char, Npix);
  out = outbuffer;

  /* need to invert the logic if range < 0 */

  /* define color table, */
  NNcolors = Ncolors - 1;
  if (mode[0].logflux) {
    mode[0].range = MAX (2, mode[0].range);
    a1 = Ncolors / log10 (mode[0].range);
    a2 = mode[0].zero;
    for (j = 0; j < image[0].Ny; j++) {
      in = image[0].data2d[j];
      for (i = 0; i < image[0].Nx; i++, in++, out++) {
	*out = (char) MIN (a1 * log10 (MAX (*in - a2, 1.0)), NNcolors);
      }
    }
  } else {
    a1 = Ncolors / mode[0].range;
    a2 = Ncolors * mode[0].zero / mode[0].range;
    for (j = 0; j < image[0].Ny; j++) {
      in = image[0].data2d[j];
      for (i = 0; i < image[0].Nx; i++, in++, out++) {
	*out = (char) MIN (MAX (a1 * *in - a2, 0), NNcolors);
      }
    }
  }
  
  /* these are for a future upgrade */
  min = max = 0.0;
  size = Npix*sizeof(char);

  /* done with the conversion, now send Ximage the converted picture */
  KiiSendMessage (fd, "%8d %8d 8 1 %f %f", image[0].Nx, image[0].Ny, 0.0, 1.0);
  KiiSendMessage (fd, "%f %f %f %f %d ", mode[0].zero, mode[0].range, min, max, size);
  KiiSendMessage (fd, "%f %f %g %g %g ", coords[0].crval1, coords[0].crpix1, coords[0].cdelt1, coords[0].pc1_1, coords[0].pc1_2);
  KiiSendMessage (fd, "%f %f %g %g %g ", coords[0].crval2, coords[0].crpix2, coords[0].cdelt2, coords[0].pc2_1, coords[0].pc2_2);

  /* send the filename */
  root = filerootname (image[0].file);
  KiiSendMessage (fd, "%s %s %s ", coords[0].ctype, root, image[0].name);

  /* send the image data */
  write (fd, outbuffer, size);
  free (outbuffer);

  return (TRUE);
}

/* this function should be broken into pieces: 
   KiiSendImage
   KiiSendCoords (default to 0 otherwise)
   KiiSendFilename
*/
