# include "Ximage.h"

int PSimage (KapaImageWidget *image, FILE *f) {
  
  int i;
  Graphic *graphic;

  if (!USE_XWINDOW) {
    fprintf (stderr, "PSimage not working yet for no X mode\n");
    return (TRUE);
  }

  graphic = GetGraphic();

  fprintf (f, " newpath 0 0 moveto %d 0 lineto %d %d lineto 0 %d lineto closepath clip\n\n", 
	   image[0].picture.dx, image[0].picture.dx, image[0].picture.dy, image[0].picture.dy);
  fprintf (f, "gsave %% encloses image\n");
  fprintf (f, "%d %d 8\n", image[0].picture.dx, image[0].picture.dy);
  fprintf (f, "[1 0 0 1 0 0]\n");
  fprintf (f, "{currentfile %d string readhexstring pop} image\n\n", image[0].picture.dx);

  /******** First we draw the picture itself ********/
  /* in !USE_XWINDOW, we'll have to change this to use the JPEG function */
  switch (graphic[0].Nbits) {
  case 8:
    PSPixmap8 (graphic, image, f);
    break;
  case 16:
    PSPixmap16 (graphic, image, f);
    break;
  case 24:
    PSPixmap24 (graphic, image, f);
    break;
  case 32:
    PSPixmap32 (graphic, image, f);
    break;
  }

  fprintf (f, "grestore %% end of image\n");
  fprintf (f, "stroke\n");

  // should we have a 'gsave / grestore' container here?
  fprintf (f, "%% plot overlay objects\n");
  fprintf (f, "1 setgray\n");

  for (i = 0; i < NOVERLAYS; i++) {
    if (image[0].overlay[i].active) {
      fprintf (f, "%% overlay %d\n", i);
      PSOverlay (image, i, f, 0);
    }
  }
  fprintf (f, "0 setgray\n");
  for (i = 0; i < NOVERLAYS; i++) {
    if (image[0].overlay[i].active) {
      fprintf (f, "%% overlay %d\n", i);
      PSOverlay (image, i, f, 1);
    }
  }

  return (TRUE);
}
