Index: /trunk/Ohana/src/kapa2/Makefile
===================================================================
--- /trunk/Ohana/src/kapa2/Makefile	(revision 13330)
+++ /trunk/Ohana/src/kapa2/Makefile	(revision 13331)
@@ -75,5 +75,7 @@
 $(SRC)/CheckButtons.$(ARCH).o             $(SRC)/InvertButton.$(ARCH).o       \
 $(SRC)/UpdatePointer.$(ARCH).o            $(SRC)/JPEGit24.$(ARCH).o           \
-$(SRC)/bDrawOverlay.$(ARCH).o
+$(SRC)/bDrawOverlay.$(ARCH).o             $(SRC)/ButtonFunctions.$(ARCH).o    \
+$(SRC)/PSimage.$(ARCH).o                  $(SRC)/PSPixmap.$(ARCH).o    \
+$(SRC)/PSOverlay.$(ARCH).o
 
 OBJ  =  $(KAPA)
Index: /trunk/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /trunk/Ohana/src/kapa2/include/prototypes.h	(revision 13330)
+++ /trunk/Ohana/src/kapa2/include/prototypes.h	(revision 13331)
@@ -39,4 +39,5 @@
 
 /* EventLoop */
+int           PScommand           PROTO(());
 int           CheckPipe           PROTO(());
 int           Reconfig            PROTO((XEvent *event));
@@ -44,5 +45,4 @@
 
 /* CheckPipe */
-int           PSit                PROTO(());
 int           PNGit               PROTO(());
 int           PPMit               PROTO(());
@@ -84,4 +84,5 @@
 
 /* PS drawing utilities */
+int           PSit                PROTO((char *filename, char *pagename, int scaleMode, int pageMode));
 int           PSFrame             PROTO((KapaGraphWidget *graph, FILE *f));
 int           PSObjects           PROTO((KapaGraphWidget *graph, FILE *f));
@@ -95,4 +96,10 @@
 void	      PSTick		  PROTO((FILE *f, double fx, double fy, double dfx, double dfy, int P, double min, double max, double value, int mode, int naxis));
 void          ClipLinePS          PROTO((double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0, FILE *f));
+int           PSimage    	  PROTO((KapaImageWidget *image, FILE *f));
+void 	      PSOverlay  	  PROTO((KapaImageWidget *image, int N, FILE *f, int extra));
+void 	      PSPixmap8  	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
+void 	      PSPixmap16 	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
+void 	      PSPixmap24 	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
+void 	      PSPixmap32 	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
 
 /* kapa bDraw Functions */
@@ -110,7 +117,4 @@
 bDrawBuffer  *bDrawIt		  PROTO(());
 void          bDrawGraph          PROTO((KapaGraphWidget *graph));
-
-# if (0)
-# endif
 
 /* misc support */
@@ -157,2 +161,16 @@
 int           InButton            PROTO((XButtonEvent *event, Button *button));
 int           InPicture           PROTO((XButtonEvent *event, Picture *picture));
+
+/* Button Functions */
+int           greycolors	  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      puns		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      rainbow		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      Recenter		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      Rescale		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      RecenterRescale	  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      ToggleDEG		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      Overlay0		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      Overlay1		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      Overlay2		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      Overlay3		  PROTO((Graphic *graphic, KapaImageWidget *image));
+int	      PSfunction	  PROTO((Graphic *graphic, KapaImageWidget *image));
Index: /trunk/Ohana/src/kapa2/src/ButtonFunctions.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 13331)
+++ /trunk/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 13331)
@@ -0,0 +1,134 @@
+# include "Ximage.h"
+
+static char *GREY = "greyscale";
+static char *PUNS = "Puns";
+static char *RAINBOW = "Rainbow";
+
+int PSfunction (Graphic *graphic, KapaImageWidget *image) {
+
+  int status;
+
+  status = PSit ("kapa.ps", "default", TRUE, KAPA_PS_NEWPLOT);
+  return (status);
+}
+
+int greycolors (Graphic *graphic, KapaImageWidget *image) {
+  char *name;
+  name = GREY;
+  SetColormap (name);
+  CreateColorbar (image, graphic);
+  Remap (graphic, image, &image[0].matrix);
+  CreateZoom (image, graphic, 0, 0); 
+  Refresh ();
+  XFlush (graphic[0].display);
+  return (TRUE);
+}
+
+int puns (Graphic *graphic, KapaImageWidget *image) {
+  char *name;
+  name = PUNS;
+  SetColormap (name);
+  CreateColorbar (image, graphic);
+  Remap (graphic, image, &image[0].matrix);
+  CreateZoom (image, graphic, 0, 0); 
+  Refresh ();
+  XFlush (graphic[0].display);
+  return (TRUE);
+}
+
+int rainbow (Graphic *graphic, KapaImageWidget *image) {
+  char *name;
+  name = RAINBOW;
+  SetColormap (name);
+  CreateColorbar (image, graphic);
+  Remap (graphic, image, &image[0].matrix);
+  CreateZoom (image, graphic, 0, 0); 
+  Refresh ();
+  XFlush (graphic[0].display);
+  return (TRUE);
+}
+
+int Recenter (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].X = 0;
+  image[0].Y = 0;
+ 
+  Remap (graphic, image, &image[0].matrix);
+  Refresh ();
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+int Rescale (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].expand = 1;
+  Remap (graphic, image, &image[0].matrix);
+  Refresh ();
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+int RecenterRescale (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].X = 0;
+  image[0].Y = 0;
+  image[0].expand = 1;
+ 
+  Remap (graphic, image, &image[0].matrix);
+  Refresh ();
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+int ToggleDEG (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].DecimalDegrees = image[0].DecimalDegrees ^ TRUE;
+  StatusBox (graphic, image);
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+/*********** overlay_button functions ************/
+int Overlay0 (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].overlay[0].active = image[0].overlay[0].active ^ TRUE;
+  Refresh ();
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+int Overlay1 (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].overlay[1].active = image[0].overlay[1].active ^ TRUE;
+  Refresh ();
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+int Overlay2 (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].overlay[2].active = image[0].overlay[2].active ^ TRUE;
+  Refresh ();
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+int Overlay3 (Graphic *graphic, KapaImageWidget *image) {
+
+  image[0].overlay[3].active = image[0].overlay[3].active ^ TRUE;
+  Refresh ();
+  FlushDisplay (graphic[0].display);
+  return (TRUE);
+
+}
+
+/* this routine is NOT independent of the number of overlays */
+
+
Index: /trunk/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CheckPipe.c	(revision 13330)
+++ /trunk/Ohana/src/kapa2/src/CheckPipe.c	(revision 13331)
@@ -33,5 +33,5 @@
   
   if (!strcmp (buffer, "PSIT")) {
-    status = PSit ();
+    status = PScommand ();
     write (sock, "DONE", 4);
     return (status);
Index: unk/Ohana/src/kapa2/src/ConvertPixmap.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/ConvertPixmap.c	(revision 13330)
+++ 	(revision )
@@ -1,262 +1,0 @@
-# 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
-
Index: /trunk/Ohana/src/kapa2/src/EraseOverlay.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/EraseOverlay.c	(revision 13330)
+++ /trunk/Ohana/src/kapa2/src/EraseOverlay.c	(revision 13331)
@@ -13,5 +13,5 @@
   image = section->image;
 
-  KiiScanCommand (sock, 16, "%d", &N);
+  KiiScanCommand (sock, 16, "%*s %d", &N);
 
   if (N > NOVERLAYS) {
Index: /trunk/Ohana/src/kapa2/src/EventLoop.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/EventLoop.c	(revision 13330)
+++ /trunk/Ohana/src/kapa2/src/EventLoop.c	(revision 13331)
@@ -48,9 +48,7 @@
     if (LastEvent (display, Expose,          &event)) Refresh (1);
     if (LastEvent (display, MappingNotify,   &event)) XRefreshKeyboardMapping ((XMappingEvent *) &event);
-# if (0)
     if (LastEvent (display, MotionNotify,    &event)) UpdatePointer (graphic, (XMotionEvent *) &event);
     if (LastEvent (display, ButtonPress,     &event)) InterpretPresses (graphic, (XButtonEvent *) &event);
     if (LastEvent (display, KeyPress,        &event)) InterpretKeys (graphic, &event);
-# endif
 
     /* drop and ignore the following StructureNotifyMask events */
Index: /trunk/Ohana/src/kapa2/src/Image.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Image.c	(revision 13330)
+++ /trunk/Ohana/src/kapa2/src/Image.c	(revision 13331)
@@ -27,5 +27,5 @@
     ALLOCATE (image[0].overlay[i].objects, Object, 1);  /* allocate so later free will not crash! */
     image[0].overlay[i].active = FALSE;
-    image[0].overlay[i].color = graphic[0].overlay_color[0];
+    image[0].overlay[i].color = graphic[0].overlay_color[i];
   }
   image[0].matrix.size = 0; /* a flag to show there is no data in the matrix */
@@ -39,34 +39,34 @@
 
   InitButtonSize (&image[0].PS_button, PS_width, PS_height, PS_bits);
-  InitButtonFunc (&image[0].PS_button, PSit);
+  InitButtonFunc (&image[0].PS_button, PSfunction);
 
   InitButtonSize (&image[0].grey_button, grey_width, grey_height, grey_bits);
-//  InitButtonFunc (&image[0].grey_button, greycolors);
+  InitButtonFunc (&image[0].grey_button, greycolors);
 
   InitButtonSize (&image[0].rainbow_button, rainbow_width, rainbow_height, rainbow_bits);
-//  InitButtonFunc (&image[0].rainbow_button, rainbow);
+  InitButtonFunc (&image[0].rainbow_button, rainbow);
 
   InitButtonSize (&image[0].puns_button, puns_width, puns_height, puns_bits);
-//  InitButtonFunc (&image[0].puns_button, puns);
+  InitButtonFunc (&image[0].puns_button, puns);
 
   InitButtonSize (&image[0].recenter_button, recenter_width, recenter_height, recenter_bits);
-//  image[0].recenter_button.function_1 = Recenter;
-//  image[0].recenter_button.function_2 = RecenterRescale;
-//  image[0].recenter_button.function_3 = Rescale;
+  image[0].recenter_button.function_1 = Recenter;
+  image[0].recenter_button.function_2 = RecenterRescale;
+  image[0].recenter_button.function_3 = Rescale;
 
   InitButtonSize (&image[0].overlay_button[0], red_width, red_height, red_bits);
-//  InitButtonFunc (&image[0].overlay_button[0], Overlay0);
+  InitButtonFunc (&image[0].overlay_button[0], Overlay0);
 
   InitButtonSize (&image[0].overlay_button[1], green_width, green_height, green_bits);
-//  InitButtonFunc (&image[0].overlay_button[1], Overlay1);
+  InitButtonFunc (&image[0].overlay_button[1], Overlay1);
 
   InitButtonSize (&image[0].overlay_button[2], blue_width, blue_height, blue_bits);
-//  InitButtonFunc (&image[0].overlay_button[2], Overlay2);
+  InitButtonFunc (&image[0].overlay_button[2], Overlay2);
 
   InitButtonSize (&image[0].overlay_button[3], yellow_width, yellow_height, yellow_bits);
-//  InitButtonFunc (&image[0].overlay_button[3], Overlay3);
+  InitButtonFunc (&image[0].overlay_button[3], Overlay3);
 
   InitButtonSize (&image[0].hms_button, hms_width, hms_height, hms_bits);
-//  InitButtonFunc (&image[0].hms_button, ToggleDEG);
+  InitButtonFunc (&image[0].hms_button, ToggleDEG);
 
   return (image);
@@ -113,9 +113,10 @@
   CrossHairs (graphic, image);
 
+  for (i = 0; i < NOVERLAYS; i++) {
+    if (image[0].overlay[i].active) {
+      PaintOverlay (graphic, image, i);
+    }
+  }
 # if (0)
-  for (i = 0; i < NOVERLAYS; i++) {
-    if (OVERLAY[i])
-      PaintOverlay (graphic, image, i);
-  }
   PaintTickmarks (graphic, image);
 # endif
Index: /trunk/Ohana/src/kapa2/src/LoadOverlay.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/LoadOverlay.c	(revision 13330)
+++ /trunk/Ohana/src/kapa2/src/LoadOverlay.c	(revision 13331)
@@ -8,5 +8,5 @@
   double x, y, dx, dy;
   int Nin, bytes, status;
-  int i, N, NOBJECTS, Nobjects, Nstart, done;
+  int i, N, NOBJECTS, Nobjects, Nstart;
   Section *section;
   KapaImageWidget *image;
@@ -19,16 +19,12 @@
   ALLOCATE (buffer, char, 65536);  /* space for 512 lines of 128 bytes */
   bzero (buffer, 65536);
-  done = FALSE;
 
   KiiScanCommand (sock, 16, "%*s %d", &N);
   Nstart = image[0].overlay[N].Nobjects;
 
-  while (!done) {
+  while (1) {
     KiiScanCommand (sock, 16, "%s %d", word, &Nin);
     if (!strcmp (word, "DONE")) break;
-    if (strcmp (word, "NLINES")) { 
-      fprintf (stderr, "!"); 
-      continue;
-    }
+    if (strcmp (word, "NLINES")) abort();
 
     buff = buffer;
Index: /trunk/Ohana/src/kapa2/src/PSOverlay.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/PSOverlay.c	(revision 13331)
+++ /trunk/Ohana/src/kapa2/src/PSOverlay.c	(revision 13331)
@@ -0,0 +1,68 @@
+# include "Ximage.h"
+
+void PSOverlay (KapaImageWidget *image, int N, FILE *f, int extra) {
+
+  int i;
+  double X, Y, dX, dY;
+  int Xmin, Ymin, Xmax, Ymax;
+  double expand, X0, Y0;
+ 
+  expand = 1.0;
+  if (image[0].expand > 0) {
+    expand = 1 / (1.0*image[0].expand);
+  }
+  if (image[0].expand < 0) {
+    expand = fabs((double)image[0].expand);
+  }
+
+  Image_to_Screen (&X0, &Y0, 0.0, 0.0, image);
+  X0 -= image[0].picture.x;
+  Y0 -= image[0].picture.y;
+
+  Xmin = 0;
+  Ymin = 0;
+  Xmax = image[0].picture.dx;
+  Ymax = image[0].picture.dy;
+
+  for (i = 0; i < image[0].overlay[N].Nobjects; i++) {
+    X  = (image[0].overlay[N].objects[i].x)/expand + X0;
+    Y =  Ymax - (image[0].overlay[N].objects[i].y)/expand - Y0;
+    dX = (image[0].overlay[N].objects[i].dx)/expand;
+    dY = (image[0].overlay[N].objects[i].dy)/expand;
+    
+    if (!strcmp (image[0].overlay[N].objects[i].type, "LINE")) {
+      if (((X < Xmin) && (X + dX < Xmin)) || ((X > Xmax) && (X + dX > Xmax)) ||
+	  ((Y < Ymin) && (Y + dY < Ymin)) || ((Y > Ymax) && (Y + dY > Ymax))) {
+	continue;
+      }
+      fprintf (f, " %6.1f %6.1f %6.1f %6.1f L\n", X + extra, Y + extra, (X+dX + extra), (Y-dY + extra));
+      continue;
+    }
+    if (!strcmp (image[0].overlay[N].objects[i].type, "TEXT")) {
+      if (((X < Xmin) && (X + dX < Xmin)) || ((X > Xmax) && (X + dX > Xmax)) ||
+	  ((Y < Ymin) && (Y + dY < Ymin)) || ((Y > Ymax) && (Y + dY > Ymax))) {
+	continue;
+      }
+      fprintf (f, "(%s) %6.1f %6.1f T\n", image[0].overlay[N].objects[i].text, X + extra, Y + extra); 
+      continue;
+    }
+    if (!strcmp (image[0].overlay[N].objects[i].type, "BOX")) {
+      if (((X - 0.5*dX < Xmin) && (X + 0.5*dX < Xmin)) || ((X - 0.5*dX > Xmax) && (X + 0.5*dX > Xmax)) ||
+	  ((Y - 0.5*dY < Ymin) && (Y + 0.5*dY < Ymin)) || ((Y - 0.5*dY > Ymax) && (Y + 0.5*dY > Ymax))) {
+	continue;
+      }
+      fprintf (f, " %6.1f %6.1f %6.1f %6.1f B\n", (dX + 2*extra), (dY + 2*extra), (X - 0.5*dX - extra), (Y - 0.5*dY - extra));
+      continue;
+    }
+    if (!strcmp (image[0].overlay[N].objects[i].type, "CIRCLE")) {
+      if (((X - dX < Xmin) && (X + dX < Xmin)) || ((X - dX > Xmax) && (X + dX > Xmax)) ||
+	  ((Y - dY < Ymin) && (Y + dY < Ymin)) || ((Y - dY > Ymax) && (Y + dY > Ymax))) {
+	continue;
+      }
+      fprintf (f, " %6.1f %6.1f %6.1f C\n", X, Y, fabs(dX + extra));
+      continue;
+    }
+    fprintf (stderr, "don't know %s, skipping\n", image[0].overlay[N].objects[i].type);
+  }
+  
+}
Index: /trunk/Ohana/src/kapa2/src/PSPixmap.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/PSPixmap.c	(revision 13331)
+++ /trunk/Ohana/src/kapa2/src/PSPixmap.c	(revision 13331)
@@ -0,0 +1,144 @@
+# include "Ximage.h"
+
+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].slope;
+  start = 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].slope;
+  start = 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].slope;
+  start = 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].slope;
+  start = 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;
+}
Index: /trunk/Ohana/src/kapa2/src/PSimage.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/PSimage.c	(revision 13331)
+++ /trunk/Ohana/src/kapa2/src/PSimage.c	(revision 13331)
@@ -0,0 +1,61 @@
+# 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");
+  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);
+    }
+  }
+  
+  fprintf (f, "grestore %% end of picture\n");
+
+  return (TRUE);
+}
Index: /trunk/Ohana/src/kapa2/src/PSit.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/PSit.c	(revision 13330)
+++ /trunk/Ohana/src/kapa2/src/PSit.c	(revision 13331)
@@ -6,16 +6,24 @@
 static Graphic *graphic;
 
-int PSit () {
+int PScommand () {
 
-  int i, Nsection, pageMode, scaleMode;
+  int status, scaleMode, pageMode;
+  char filename[1024], pagename[1024];
+
+  /* expect a line telling the number of bytes and a filename */
+  KiiScanMessage (sock, "%s %s %d %d", filename, pagename, &scaleMode, &pageMode);
+  status = PSit (filename, pagename, scaleMode, pageMode);
+  return (status);
+}
+
+int PSit (char *filename, char *pagename, int scaleMode, int pageMode) {
+
+  int i, Nsection;
   double scale;
   FILE *f;
-  char filename[1024], pagename[1024], *version;
+  char *version;
   Section *section;
 
   graphic = GetGraphic();
-
-  /* expect a line telling the number of bytes and a filename */
-  KiiScanMessage (sock, "%s %s %d %d", filename, pagename, &scaleMode, &pageMode);
 
   if (pageMode == KAPA_PS_NEWPAGE) {
@@ -76,4 +84,5 @@
   }
 
+  Nsection = GetNumberOfSections ();
   for (i = 0; i < Nsection; i++) {
     section = GetSectionByNumber (i);
@@ -85,5 +94,5 @@
     }
     if (section->image) {
-      // PSimage ();
+      PSimage (section->image, f);
     }
   }
