# include "Ximage.h"

void ConvertPixmap8 (Layout *layout, FILE *f) {

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

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

  for (i = 0; i < layout[0].picture.dy; i++) {
    for (k = 0; k < layout[0].picture.dx; k++, buff++) {
      if (*buff == layout[0].white) 
	val = Nchar;
      else {
	for (m = 0; (layout[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*layout[0].picture.dx;
  }
  return;
}

void ConvertPixmap16 (Layout *layout, FILE *f) {

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

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

  for (i = 0; i < layout[0].picture.dy; i++) {
    for (k = 0; k < layout[0].picture.dx; k++, buff++) {
      if (*buff == layout[0].white) 
	val = Nchar;
      else {
	for (m = 0; (layout[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*layout[0].picture.dx;
  }
  return;
}

void ConvertPixmap24 (Layout *layout, FILE *f) {

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

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

  for (i = 0; i < dy; i++) {
    for (k = 0; k < dx; k++, buff+=3) {
      color = 0;
      byte = buff[2];
      color = (byte << 16);
      byte = buff[1];
      color |= (byte << 8);
      byte = buff[0];
      color |= byte;
      for (m = 0; (layout[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 ConvertPixmap32 (Layout *layout, FILE *f) {

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

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

  for (i = 0; i < layout[0].picture.dy; i++) {
    for (k = 0; k < layout[0].picture.dx; k++, buff++) {
      if (*buff == layout[0].white) 
	val = Nchar;
      else {
	for (m = 0; (layout[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*layout[0].picture.dx;
  }
  return;
}

# if (0)
void ConvertPixmap (Layout *layout, File *f) {
  
  /* set up expansions */
  expand = expand_in = expand_out = 1.0;
  if (layout[0].expand == 0)
    layout[0].expand = 1;
  if (layout[0].expand > 0) {
    expand = 1 / (1.0*layout[0].expand);
    expand_out = layout[0].expand;
    expand_in  = 1;
  }
  if (layout[0].expand < 0) {
    expand = fabs((double)layout[0].expand);
    expand_out = 1;
    expand_in  = -layout[0].expand;
  }

  dx = layout[0].picture.dx;
  dy = layout[0].picture.dy;
  DX = layout[0].matrix.Naxis[0];
  DY = layout[0].matrix.Naxis[1];

  /* X,Y are the image coordinates of the first image pixel */
  X = MAX(0.5*(DX - dx*expand) - layout[0].X, 0);
  if ((int)X != X) 
    X = (int) X + 1;
  else 
    X = (int) X;
  Y = MAX(0.5*(DY - dy*expand) - layout[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 + layout[0].X - 0.5*DX)/expand + 0.5*dx;
  Ry = (Y + layout[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 (layout[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;

  /* output line buffer */
  ALLOCATE (line_buffer, char, dx);

  in_pix_ref  = (unsigned char *) (layout[0].matrix.buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);

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

  Nchar = 255.0;
  Npix = layout[0].Npixels;
  frac = Nchar / Npix;

  /**** fill in bottom area ****/
  out_pix = line_buffer;
  for (i = 0; i < dx; i++, out_pix++) {
    *out_pix = WHITE;
  }
  for (j = 0; j < j_start; j++) {
    for (i = 0; i < dx; i++) {
      fprintf (f, "%02x", val);
      if ((Npt % 40) == 0) {
	fprintf (f, "\n");
      }
    }
  }
  
  /*** fill in the image data region ***/
  for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += expand_in*DX) {
    
    /* create one output image line */
    in_pix = in_pix_ref;
    out_pix = line_buffer;

    /**** fill in area to the left of the picture ****/
    for (i = 0; i < i_start; i++, out_pix+=3) {
      *out_pix = WHITE;
    }
    
    /*** fill in the picture region ***/
    for (i = i_start; i < i_end; i+=expand_out, in_pix+=expand_in) {
      for (ii = 0; ii < expand_out; ii++, out_pix++) {
	*out_pix = *in_pix*frac;
      }
    }
    
    /**** fill in area to the right of the picture ****/
    for (i = i_end; i < dx; i++, out_pix++) {
      *out_pix = WHITE;
    }

    /* write out the image line expand_out times */
    for (j = 0; j < expand_out; j++) {
      for (i = 0; i < dx; i++) {
	fprintf (f, "%02x", val);
	if ((Npt % 40) == 0) {
	  fprintf (f, "\n");
	}
      }
    }
  }

  /**** fill in top area ****/
  out_pix = line_buffer;
  for (i = 0; i < dx; i++, out_pix++) { 
    *out_pix = WHITE;
  }
  for (j = j_end; j < dy; j++) {
    for (i = 0; i < dx; i++) {
      fprintf (f, "%02x", val);
      if ((Npt % 40) == 0) {
	fprintf (f, "\n");
      }
    }
  }
}
# endif

