# include "Ximage.h"

// XXX this stuff has been broken by the conversion to the pixmap stuff

void PSPixmap8 (Graphic *graphic, KapaImageWidget *image, FILE *f) {

  int i, k, m, val;
  double Nchar, Npix, start, slope, frac;
  unsigned char *buff;
  unsigned long back;

  Nchar = 255.0;
  Npix = graphic[0].Npixels;
  frac = Nchar / Npix;
  /* start at the last line, print lines in decending order */
  buff = (unsigned char *)image[0].picture.data + image[0].picture.dx*(image[0].picture.dy - 1);
  slope = image[0].image[0].slope;
  start = image[0].image[0].start;
  back  = graphic[0].back;

  for (i = 0; i < image[0].picture.dy; i++) {
    for (k = 0; k < image[0].picture.dx; k++, buff++) {
      if (*buff == back) 
	val = Nchar;
      else {
	for (m = 0; (graphic[0].cmap[m].pixel != *buff) && (m < Npix); m++);
	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
      }
      fprintf (f, "%02x", val);
      if (!((k+1) % 40)) fprintf (f, "\n"); 
    }
    fprintf (f, "\n");
    buff -= 2*image[0].picture.dx;
  }
  return;
}

void PSPixmap16 (Graphic *graphic, KapaImageWidget *image, FILE *f) {

  int i, k, m, val;
  double Nchar, Npix, start, slope, frac;
  unsigned short *buff;
  unsigned long back;

  Nchar = 255.0;
  Npix = graphic[0].Npixels;
  frac = Nchar / Npix;
  /* start at the last line, print lines in decending order */
  buff = (unsigned short *)image[0].picture.data + image[0].picture.dx*(image[0].picture.dy - 1);
  slope = image[0].image[0].slope;
  start = image[0].image[0].start;
  back  = graphic[0].back;

  for (i = 0; i < image[0].picture.dy; i++) {
    for (k = 0; k < image[0].picture.dx; k++, buff++) {
      if (*buff == back) 
	val = Nchar;
      else {
	for (m = 0; (graphic[0].cmap[m].pixel != *buff) && (m < Npix); m++);
	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
      }
      fprintf (f, "%02x", val);
      if (!((k+1) % 40)) fprintf (f, "\n"); 
    }
    fprintf (f, "\n");
    buff -= 2*image[0].picture.dx;
  }
  return;
}

void PSPixmap24 (Graphic *graphic, KapaImageWidget *image, FILE *f) {

  int i, k, m, dx, dy, val, extra;
  unsigned char *buff;
  unsigned long color, byte;
  double Nchar, Npix, start, slope, frac;
  unsigned long back;

  Nchar = 255.0;
  Npix = graphic[0].Npixels;
  frac = Nchar / Npix;
  dx = image[0].picture.dx;
  dy = image[0].picture.dy;
  extra = 4 - (dx * 3) % 4;
  /* start at the last line, print lines in decending order */
  buff = (unsigned char *)&image[0].picture.data[(dy - 1)*(3*dx + extra)];
  slope = image[0].image[0].slope;
  start = image[0].image[0].start;
  back  = graphic[0].back;

  for (i = 0; i < dy; i++) {
    for (k = 0; k < dx; k++, buff+=3) {
      if (*buff == back) {
	val = Nchar;
      } else {
	color = 0;
	byte = buff[2];
	color = (byte << 16);
	byte = buff[1];
	color |= (byte << 8);
	byte = buff[0];
	color |= byte;
	for (m = 0; (graphic[0].cmap[m].pixel != color) && (m < Npix); m++);
	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
      }
      fprintf (f, "%02x", val);
      if (!((k+1) % 40)) fprintf (f, "\n"); 
    }
    fprintf (f, "\n");
    buff -= 2*3*dx + extra;
  }
  return;
}

void PSPixmap32 (Graphic *graphic, KapaImageWidget *image, FILE *f) {

  int i, k, m, val;
  double Nchar, Npix, start, slope, frac;
  unsigned int *buff;
  unsigned long back;

  Nchar = 255.0;
  Npix = graphic[0].Npixels;
  frac = Nchar / Npix;
  /* start at the last line, print lines in decending order */
  buff = (unsigned int *)image[0].picture.data + image[0].picture.dx*(image[0].picture.dy - 1);
  slope = image[0].image[0].slope;
  start = image[0].image[0].start;
  back  = graphic[0].back;

  for (i = 0; i < image[0].picture.dy; i++) {
    for (k = 0; k < image[0].picture.dx; k++, buff++) {
      if (*buff == back) 
	val = Nchar;
      else {
	for (m = 0; (graphic[0].cmap[m].pixel != *buff) && (m < Npix); m++);
	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
      }
      fprintf (f, "%02x", val);
      if (!((k+1) % 40)) fprintf (f, "\n"); 
    }
    fprintf (f, "\n");
    buff -= 2*image[0].picture.dx;
  }
  return;
}

# if (0)
// XXX needs work!
void PSPixmap32_RGB (Graphic *graphic, KapaImageWidget *image, FILE *f) {

  int i, k, m, val;
  double Nchar, Npix, start, slope, frac;
  unsigned int *buff;
  unsigned long back;
  unsigned char 

  ALLOCATE (pixelR, unsigned char, graphic[0].Npixels);
  ALLOCATE (pixelG, unsigned char, graphic[0].Npixels);
  ALLOCATE (pixelB, unsigned char, graphic[0].Npixels);

  /** cmap[i].pixel must be defined even if X is not used **/
  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
    pixelR[i] = graphic[0].cmap[i].red >> 8;
    pixelG[i] = graphic[0].cmap[i].green >> 8;
    pixelB[i] = graphic[0].cmap[i].blue >> 8;
  }

  for (i = 0; i < image[0].picture.dy; i++) {
    for (k = 0; k < image[0].picture.dx; k++, buff++) {
      if (*buff == back) 
	val = Nchar;
      else {
	for (m = 0; (graphic[0].cmap[m].pixel != *buff) && (m < Npix); m++);
	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
      }
      fprintf (f, "%02x", val);
      if (!((k+1) % 40)) fprintf (f, "\n"); 
    }
    fprintf (f, "\n");
    buff -= 2*image[0].picture.dx;
  }
  return;
}
# endif
