Index: branches/ipp-magic-v0/Ohana/src/kii/Makefile
===================================================================
--- branches/elixir/Ohana/src/kii/Makefile	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/Makefile	(revision 21062)
@@ -1,9 +1,9 @@
-include ../../Configure
+default: kii
+help:
+	@echo "make options: kii (default)"
+
+include ../../Makefile.System
 HOME 	=	$(ROOT)/src/kii
-PROGRAM =       kii
-
-default: $(PROGRAM)
-
-INC	=	$(HOME)/include
+LIB	=	$(HOME)/lib
 BIN	=	$(HOME)/bin
 BDIR	=	$(HOME)/button
@@ -15,14 +15,14 @@
 SDIR	=	$(HOME)/setup
 ZDIR	=	$(HOME)/zoom
-DESTBIN	=	$(LBIN)
-DESTLIB	=	$(LLIB)
-DESTINC	=	$(LINC)
-DESTMAN	=	$(LMAN)
-#
-INCS	= 	-I$(INC) -I$(LINC) -I$(XINC) -I$(LINC)/$(ARCH)
-LIBS	= 	-L$(LLIB) $(XLIB) -lohana -lFITS -ljpeg -lX11 -lsocket -lnsl -lm
-# LIBS	= 	-L$(LLIB) $(XLIB) -lohana -lFITS -ljpeg -lX11 -lsocket -lnsl -lm -static
-CFLAGS  =	-o $*.$(ARCH).o $(INCS) 
-CCFLAGS	=	$(INCS) $(LIBS) 
+INC	=	$(HOME)/include
+include ../../Makefile.Common
+
+# programs may add their own internal requirements here
+FULL_CFLAGS   = $(BASE_CFLAGS)
+FULL_CPPFLAGS = $(BASE_CPPFLAGS)
+FULL_LDFLAGS  = -lkapa -ldvo -lFITS -lohana $(BASE_LDFLAGS)
+
+kii: $(BIN)/kii.$(ARCH)
+install: $(DESTBIN)/kii
 
 # local source / object files ########################
@@ -32,5 +32,5 @@
 $(BDIR)/DrawButton.$(ARCH).o       	  $(BDIR)/InvertButton.$(ARCH).o       \
 $(BDIR)/FlashButton.$(ARCH).o      	  $(BDIR)/PSit.$(ARCH).o		\
-$(BDIR)/JPEGit.$(ARCH).o
+$(BDIR)/JPEGit.$(ARCH).o		  $(BDIR)/ConvertPixmap.$(ARCH).o
 
 COBJ = \
@@ -50,6 +50,7 @@
 $(ODIR)/EraseOverlay.$(ARCH).o            $(ODIR)/PaintOverlay.$(ARCH).o       \
 $(ODIR)/LoadOverlay.$(ARCH).o   	  $(ODIR)/SaveOverlay.$(ARCH).o        \
-$(ODIR)/DrawOverlay.$(ARCH).o		  $(ODIR)/CSaveOverlay.$(ARCH).o 	\
-$(ODIR)/PaintTickmarks.$(ARCH).o          $(ODIR)/LoadTickmarks.$(ARCH).o
+$(ODIR)/DrawOverlay.$(ARCH).o		  $(ODIR)/CSaveOverlay.$(ARCH).o       \
+$(ODIR)/PaintTickmarks.$(ARCH).o          $(ODIR)/LoadTickmarks.$(ARCH).o      \
+$(ODIR)/bDrawOverlay.$(ARCH).o
 
 POBJ = \
@@ -57,9 +58,9 @@
 $(PDIR)/NewPicture.$(ARCH).o        	  $(PDIR)/Center.$(ARCH).o             \
 $(PDIR)/PositionPictures.$(ARCH).o  	  $(PDIR)/StatusBox.$(ARCH).o          \
-$(PDIR)/cursor.$(ARCH).o 							\
 $(PDIR)/CursorOps.$(ARCH).o 		  $(PDIR)/Resize.$(ARCH).o 		\
 $(PDIR)/Remap8.$(ARCH).o 		  $(PDIR)/Remap24.$(ARCH).o 		\
 $(PDIR)/Remap.$(ARCH).o             	  $(PDIR)/Remap32.$(ARCH).o		\
-$(PDIR)/Remap16.$(ARCH).o
+$(PDIR)/Remap16.$(ARCH).o		  $(PDIR)/JPEGit24.$(ARCH).o            \
+$(PDIR)/cursor.$(ARCH).o
 
 SOBJ = \
@@ -75,5 +76,6 @@
 $(SDIR)/LoadFont.$(ARCH).o          	  $(SDIR)/TopWindow.$(ARCH).o          \
 $(SDIR)/MakeColormap.$(ARCH).o      	  $(SDIR)/Ximage.$(ARCH).o             \
-$(SDIR)/MakeCursor.$(ARCH).o              $(SDIR)/CheckVisual.$(ARCH).o
+$(SDIR)/MakeCursor.$(ARCH).o              $(SDIR)/CheckVisual.$(ARCH).o	       \
+$(SDIR)/args.$(ARCH).o
 
 ZOBJ = \
@@ -83,5 +85,4 @@
 $(ZDIR)/CreateZoom8.$(ARCH).o             $(ZDIR)/CreateZoom24.$(ARCH).o	\
 $(ZDIR)/CreateZoom16.$(ARCH).o
-
 
 OBJ  =  $(BOBJ) $(COBJ) $(EOBJ) $(MOBJ) $(OOBJ) $(POBJ) $(SOBJ) $(ZOBJ)
@@ -93,32 +94,3 @@
 	$(INC)/prototypes.h
 
-#### nothing program-dependant below ####
-# dependancy rules for binary code ##########################
-$(PROGRAM): $(BIN)/$(PROGRAM).$(ARCH)
-
-$(BIN)/$(PROGRAM).$(ARCH): $(OBJ)
-	$(CC) $(OBJ) -o $(BIN)/$(PROGRAM).$(ARCH) $(CCFLAGS)
-
-install: $(DESTBIN)/$(PROGRAM)
-
-$(DESTBIN)/$(PROGRAM): $(BIN)/$(PROGRAM).$(ARCH)
-	rm -f $(DESTBIN)/$(PROGRAM)
-	cp $(BIN)/$(PROGRAM).$(ARCH) $(DESTBIN)/$(PROGRAM)
-
-# utilities #################################################
-clean:
-	rm -f $(BIN)/*.$(ARCH)
-	rm -f `find . -name "*.o"`
-	rm -f `find . -name "*~"`
-	rm -f `find . -name "#*"`
-
-dist: clean
-	rm -f */*.*.o
-	rm -f $(BIN)/$(PROGRAM).*
-
-.SUFFIXES: .$(ARCH).o
-
-.c.$(ARCH).o:
-	$(CC) $(CFLAGS) -c $<
-
-
+$(BIN)/kii.$(ARCH): $(OBJ)
Index: branches/ipp-magic-v0/Ohana/src/kii/bin/.cvsignore
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/bin/.cvsignore	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/bin/.cvsignore	(revision 21062)
@@ -0,0 +1,4 @@
+*.linux *.lin64 *.sol *.sun *.sid *.hp *.irix
+*.linrh
+*.darwin
+*.darwin_x86
Index: branches/ipp-magic-v0/Ohana/src/kii/button/ButtonFunctions.c
===================================================================
--- branches/elixir/Ohana/src/kii/button/ButtonFunctions.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/button/ButtonFunctions.c	(revision 21062)
@@ -12,4 +12,6 @@
   Remap (graphic, layout, &layout[0].matrix);
   CreateZoom (layout, graphic, 0, 0); 
+  Refresh (graphic, layout, 0);
+  XFlush (graphic[0].display);
   return (TRUE);
 }
@@ -22,4 +24,6 @@
   Remap (graphic, layout, &layout[0].matrix);
   CreateZoom (layout, graphic, 0, 0); 
+  Refresh (graphic, layout, 0);
+  XFlush (graphic[0].display);
   return (TRUE);
 }
@@ -32,4 +36,6 @@
   Remap (graphic, layout, &layout[0].matrix);
   CreateZoom (layout, graphic, 0, 0); 
+  Refresh (graphic, layout, 0);
+  XFlush (graphic[0].display);
   return (TRUE);
 }
Index: branches/ipp-magic-v0/Ohana/src/kii/button/ConvertPixmap.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/button/ConvertPixmap.c	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/button/ConvertPixmap.c	(revision 21062)
@@ -0,0 +1,262 @@
+# 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: branches/ipp-magic-v0/Ohana/src/kii/button/PSit.c
===================================================================
--- branches/elixir/Ohana/src/kii/button/PSit.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/button/PSit.c	(revision 21062)
@@ -1,25 +1,24 @@
 # include "Ximage.h"
-/* Notice: we are using a fixed border size of 10 pt */
-void ConvertPixmap8  (Layout *layout, FILE *f);
-void ConvertPixmap16 (Layout *layout, FILE *f);
-void ConvertPixmap24 (Layout *layout, FILE *f);
-void ConvertPixmap32 (Layout *layout, FILE *f);
+# define PADDING 10
+# define XOFFSET 60
+# define YOFFSET 60
+
+static char *name = "$Name: not supported by cvs2svn $";
 
 int PSit (Graphic *graphic, Layout *layout, int Raw) {
   
   FILE *f;
-  int i;
+  int i, pageMode, scaleMode;
   double scale;
-  char filename[1024];
-  int Nbytes, status;
+  char filename[1024], pagename[1024], *version;
 
   /* expect a line telling the number of bytes and a filename */
-  status = read (layout[0].Ximage, filename, 16);
-  filename[16] = 0;
-  sscanf (filename, "%*s %d", &Nbytes);
-  status = read (layout[0].Ximage, filename, Nbytes);
-  filename[status] = 0; /* make the string easy to parse */
+  KiiScanMessage (layout[0].Ximage, "%s %s %d %d", filename, pagename, &scaleMode, &pageMode);
 
-  f = fopen (filename, "w");
+  if (pageMode == KAPA_PS_NEWPAGE) {
+    f = fopen (filename, "a+");
+  } else {
+    f = fopen (filename, "w");
+  }
   if (f == (FILE *) NULL) {
     fprintf (stderr, "failed to open %s for output\n", filename);
@@ -27,17 +26,40 @@
   }
   
-  scale = MIN ((500.0/(double)layout[0].picture.dx), (700.0/(double)layout[0].picture.dy));
-  if (!Raw) {
-    fprintf (f, "%%!PS-Adobe-2.0 EPSF-2.0\n");
-    fprintf (f, "%%%%Title: %s\n", layout[0].file);
-    fprintf (f, "%%%%Creator: Ki'i (0.95)\n");
-    fprintf (f, "%%%%BoundingBox: 10 40 %d %d \n", (int)(scale*layout[0].picture.dx + 30), (int)(scale*layout[0].picture.dy + 60));
-    fprintf (f, "%%%%Pages: 1\n");
-    fprintf (f, "%%%%DocumentFonts:\n");
-    fprintf (f, "%%%%EndComments\n");
-    fprintf (f, "%%%%EndProlog\n");
-    fprintf (f, "%%%%Page: 1 1\n\n");
+  if (!USE_XWINDOW) {
+    fprintf (stderr, "PS not working yet\n");
+    return (TRUE);
+  }
 
+  if (scaleMode) {
+    scale = MIN ((500.0/(double)layout[0].picture.dx), (700.0/(double)layout[0].picture.dy));
+  } else {
+    scale = 72.0 / 96.0; /* ratio of screen pixels to points */
   }
+
+  switch (pageMode) {
+    case KAPA_PS_NEWPLOT:
+      fprintf (f, "%%!PS-Adobe-2.0 EPSF-2.0\n");
+      fprintf (f, "%%%%Title: %s\n", filename);
+      version = strip_version (name);
+      fprintf (f, "%%%%Creator: Ki'i (%s)\n", version);
+      free (version);
+      fprintf (f, "%%%%BoundingBox: %d %d %.0f %.0f\n", 
+	       XOFFSET - PADDING, YOFFSET - PADDING, 
+	       scale*layout[0].picture.dx + PADDING + XOFFSET, 
+	       scale*layout[0].picture.dy + PADDING + YOFFSET);
+      fprintf (f, "%%%%Pages: 1\n");
+      fprintf (f, "%%%%DocumentFonts:\n");
+      fprintf (f, "%%%%EndComments\n");
+      fprintf (f, "%%%%EndProlog\n");
+      fprintf (f, "%%%%Page: %s\n\n", pagename);
+      break;
+
+    case KAPA_PS_NEWPAGE:
+      fprintf (f, "%%%%Page: %s\n\n", pagename);
+      break;
+
+    case KAPA_PS_RAWPAGE:
+      break;
+  } 
 
   fprintf (f, "gsave %% encloses picture\n");
@@ -50,6 +72,6 @@
   fprintf (f, "/L {newpath moveto lineto stroke} def\n\n");
 
-  if (!Raw) {
-    fprintf (f, " 20 50 translate\n");
+  if (pageMode != KAPA_PS_RAWPAGE) {
+    fprintf (f, " %d %d translate\n", XOFFSET, YOFFSET);
     fprintf (f, "  %f  %f scale\n", scale, scale);
   }
@@ -63,4 +85,5 @@
 
   /******** 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:
@@ -99,5 +122,5 @@
   fprintf (f, "grestore %% end of picture\n");
 
-  if (!Raw) fprintf (f, "showpage\n");
+  if (pageMode != KAPA_PS_RAWPAGE) fprintf (f, "showpage\n");
 
   fclose (f);
@@ -107,132 +130,2 @@
 }
 
-void ConvertPixmap8 (Layout *layout, FILE *f) {
-
-  int i, k, m, val;
-  double Nchar, Npix, start, slope, frac;
-  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 = &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;
-}
Index: branches/ipp-magic-v0/Ohana/src/kii/colorbar/CreateColorbar.c
===================================================================
--- branches/elixir/Ohana/src/kii/colorbar/CreateColorbar.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/colorbar/CreateColorbar.c	(revision 21062)
@@ -28,5 +28,5 @@
   case 16:
     REALLOCATE (layout[0].cmapbar.data, char, 2*dy*dx);
-    out8 = layout[0].cmapbar.data;
+    out8 = (unsigned char *) layout[0].cmapbar.data;
     for (i = 0; i < dx; i++) {
       pixvalue = layout[0].cmap[(int)(i*layout[0].Npixels/dx)].pixel;
@@ -44,5 +44,5 @@
     extra = 4 - (dx * 3) % 4;
     REALLOCATE (layout[0].cmapbar.data, char, dy*(3*dx + extra));
-    out8 = layout[0].cmapbar.data;
+    out8 = (unsigned char *) layout[0].cmapbar.data;
     for (i = 0; i < dx; i++) {
       pixvalue = layout[0].cmap[(int)(i*layout[0].Npixels/dx)].pixel;
Index: branches/ipp-magic-v0/Ohana/src/kii/colorbar/DragColorbar.c
===================================================================
--- branches/elixir/Ohana/src/kii/colorbar/DragColorbar.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/colorbar/DragColorbar.c	(revision 21062)
@@ -17,5 +17,5 @@
   slope = layout[0].slope;
   start = layout[0].start;
-  oldfrac_x = 10;
+  oldfrac_x = oldfrac_y = 10;
   
   while (1) {
Index: branches/ipp-magic-v0/Ohana/src/kii/colorbar/SetColormap.c
===================================================================
--- branches/elixir/Ohana/src/kii/colorbar/SetColormap.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/colorbar/SetColormap.c	(revision 21062)
@@ -1,3 +1,6 @@
 # include "Ximage.h"
+
+/*** this function uses only private colormaps.  it should try the standard colormap
+     first to get better control over the smooth greyscales ***/
 
 int SetColormap (Graphic *graphic, Layout *layout, char *name) {
@@ -84,4 +87,5 @@
 
  store_colors:
+  if (!USE_XWINDOW) return (TRUE);
   if (graphic[0].visualclass) {
     XStoreColors(graphic[0].display, graphic[0].colormap, layout[0].cmap, layout[0].Npixels);
Index: branches/ipp-magic-v0/Ohana/src/kii/doc/ChangeLog.txt
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/doc/ChangeLog.txt	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/doc/ChangeLog.txt	(revision 21062)
@@ -0,0 +1,34 @@
+
+- kii 1.6 : 2006.08.23
+  * converted to gfits APIs (forces libfits 1.6)
+  * added keyboard events
+  * removed items moved to libkapa
+  * added test scripts
+
+- kii 1.5
+  * fixed bug in event loop which missed certain mask types
+  * added arrows and up/down keys to window events
+  * added ReorientOnButton / changed Reorient
+  * fixed errors wrt bDraw / fonts
+
+- kii 1.4
+  * major work to allow noX operation
+  * added bDraw functions
+  * integrated with libkapa
+
+kii-1-3: released 2005.10.20
+  - changed cursor interaction (returns name for keys)
+  - minor changes to use new libohana (v1.5) / libfits (v1.4)
+
+kii-1-2:
+  - cleaned up the event loop to reduce CPU spinning
+
+kii-1-1: 
+  - cleaned up the update / refresh process to correctly handle
+    uncovered portions.
+  - cleaned up the event loop to handle slow traffic better
+  - cleanup up the cursor event loop in the same way
+  - fixed crash on non-printing cursor keys
+
+kii-1-0:
+  imported to CVS 
Index: branches/ipp-magic-v0/Ohana/src/kii/doc/VERSIONS
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/doc/VERSIONS	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/doc/VERSIONS	(revision 21062)
@@ -0,0 +1,9 @@
+
+tag names used by kapa:
+
+TAG         : Comment
+kii-1-0     : first version under CVS
+
+USES
+libfits-1-0
+libohana-1-0
Index: branches/ipp-magic-v0/Ohana/src/kii/doc/notes.txt
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/doc/notes.txt	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/doc/notes.txt	(revision 21062)
@@ -0,0 +1,29 @@
+
+2005.12.05
+
+  I have been making some improvements to Kapa / Kii, and am
+  planning on further advances.  Here are some of the features
+  I'd like to add:
+
+  - library function calls (see libfuncs.txt)
+
+  - named plotting styles (line, histogram, points)
+  - add y-based histogram 
+  - move point-to-point to a style
+  - explore standard colormaps to get better greyscale
+  - merge kapa and kii common code?
+  - kapa / kii device Number in title bar
+
+  - complete kapa plotting features in kii 
+  - axis labels on kii image
+  - kapa (and kii) axis labels, frame in color
+  - image-plotting in kapa
+
+  - three selectable kii image buffers
+  - true-color kii representation 
+  
+  - color PS for kii (currently only B&W)
+  - refresh picture after new colormap
+  - DVO regions corresponding to Kapa 'sections' on any window
+
+  - push argv/argc entries all into args, push options into globals.
Index: branches/ipp-magic-v0/Ohana/src/kii/event/CheckPipe.c
===================================================================
--- branches/elixir/Ohana/src/kii/event/CheckPipe.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/event/CheckPipe.c	(revision 21062)
@@ -1,12 +1,15 @@
 # include "Ximage.h"
 # define STRCONST(A) ((int)(0x1000000*A[0] + 0x10000*A[1] + 0x100*A[2] + 0x1*A[3]))
+# define DEBUG 0
+
+static int ActiveCursor = FALSE;
 
 int CheckPipe (Graphic *graphic, Layout *layout) {
 
   int status;
-  char buffer[1024];
+  char buffer[20];
 
   /* attempt to read 4 bytes from stdin */
-
+  bzero (buffer, 20);
   status = read (layout[0].Ximage, buffer, 4);
   buffer[4] = 0;
@@ -24,5 +27,5 @@
 
   case 4:
-/*    fprintf (stderr, "got signal %s from pipe\n", buffer);    */
+    if (DEBUG) fprintf (stderr, "got signal %s from pipe\n", buffer);
     break;
 
@@ -32,8 +35,18 @@
     break;
   }
+  
+  /* In Active Cursor mode, we can only receive the NCUR signal */
+  if (ActiveCursor) {
+    if (!strcmp (buffer, "NCUR")) {
+      ActiveCursor = FALSE;
+      return (2);
+    }
+    fprintf (stderr, "unknown signal (AC) %s\n", buffer);
+    return (TRUE);
+  }
 
-  
+  /* If Active Cursor mode is off, we can receive other signal */
   if (!strcmp (buffer, "QUIT")) {
-    /* fprintf (stderr, "Goodbye!\n"); */
+    if (DEBUG) fprintf (stderr, "Goodbye!\n");
     return (FALSE);
   }
@@ -45,6 +58,12 @@
 
   if (!strcmp (buffer, "CURS")) {
+    ActiveCursor = TRUE;
     status = cursor (graphic, layout);
     return (status);
+  }
+
+  if (!strcmp (buffer, "NCUR")) {
+    fprintf (stderr, "error: not in active cursor mode\n");
+    return (TRUE);
   }
 
@@ -70,4 +89,5 @@
 
   if (!strcmp (buffer, "PSIT")) {
+    /** this one needs work : ConvertPixmapN fails for !USE_XWINDOW */
     status = PSit (graphic, layout, 0);
     write (layout[0].Ximage, "DONE", 4);
@@ -76,4 +96,5 @@
 
   if (!strcmp (buffer, "PSRW")) {
+    /** this one needs work : ConvertPixmapN fails for !USE_XWINDOW */
     status = PSit (graphic, layout, 1);
     write (layout[0].Ximage, "DONE", 4);
@@ -82,5 +103,5 @@
 
   if (!strcmp (buffer, "JPEG")) {
-    status = JPEGit (graphic, layout);
+    status = JPEGit24 (graphic, layout);
     write (layout[0].Ximage, "DONE", 4);
     return (status);
@@ -102,4 +123,5 @@
   }
 
+  /* is this no longer used ?
   if (!strcmp (buffer, "NPIX")) {
     sprintf (buffer, "NPIX:   %8d", layout[0].Npixels);
@@ -107,4 +129,5 @@
     return (TRUE);
   }
+  */
 
   fprintf (stderr, "unknown signal %s\n", buffer);
Index: branches/ipp-magic-v0/Ohana/src/kii/event/EventLoop.c
===================================================================
--- branches/elixir/Ohana/src/kii/event/EventLoop.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/event/EventLoop.c	(revision 21062)
@@ -1,12 +1,17 @@
 # include "Ximage.h"
-# define MAX_STRING 100
-/* WARNING: this is defined because the HP UX installation i have does not
-seem to know this structure, though it should */
-# ifdef DUMMY
-struct Ktimeval {
-  unsigned long tv_sec;         /* seconds */
-  long          tv_usec;        /* and microseconds */
-};
-# endif
+
+/* # define IgnoreMask (long) (ClientMessage | ButtonReleaseMask) */
+# define IgnoreMask (long) (~(StructureNotifyMask | SubstructureNotifyMask | ExposureMask | KeyPressMask | ButtonPressMask | PointerMotionMask))
+
+int LastEvent (Display *display, int type, XEvent *event) {
+
+  int found;
+
+  found = FALSE;
+  while (XCheckTypedEvent (display, type, event)) {
+    found = TRUE;
+  }
+  return (found);
+}
 
 int EventLoop (Graphic *graphic, Layout *layout) {
@@ -14,8 +19,7 @@
   XEvent          event;
   int             status;
+  Display        *display;
   
-  /* gettimeofday (&then, (struct timeval *) NULL); */
-
-  Refresh (graphic, layout, 1);
+  if (USE_XWINDOW) Refresh (graphic, layout, 1);
 
   status = TRUE;
@@ -25,4 +29,9 @@
       return (FALSE);
     
+    if (!USE_XWINDOW) {
+      usleep (50000);
+      continue;
+    }
+
     if (XEventsQueued (graphic[0].display, QueuedAfterFlush) < 1) {
       usleep (10000);
@@ -30,65 +39,42 @@
     }
 
-    if (XCheckMaskEvent (graphic[0].display, EVENT_MASK, &event)) {
+    display = graphic[0].display;
 
-      switch (event.type)  {
-	
-      case MotionNotify:
-	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
-	  UpdatePointer (graphic, layout, (XMotionEvent *) &event);
-	}
-	break;
-      case ButtonPress:
-	status = InterpretPresses (graphic, layout, (XButtonEvent *) &event);
-	break;
-	
-      case Expose:
-	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
-	  Refresh (graphic, layout, 1);
-	}
-	break;
-	/* if (event.xexpose.count == 0) */
-	
-      case ConfigureNotify:
-	status = Reconfig (graphic, layout, &event);
-	/*
-	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
-	  status = Reconfig (graphic, layout, &event);
-	}
-	*/
-	break;
-	
-      case KeyPress:
-	status = InterpretKeys (graphic, layout, &event);
-	break;
-	
-      case MappingNotify:
-	if (XEventsQueued (graphic[0].display, QueuedAlready) < 2) {
-	  XRefreshKeyboardMapping ((XMappingEvent *) &event);
-	}
-	break;
-	
-      }
-    }
+    /* grab the last entry for these events */
+    if (LastEvent (display, ConfigureNotify, &event)) Reconfig (graphic, layout, &event);
+    if (LastEvent (display, CirculateNotify, &event)) Reconfig (graphic, layout, &event);
+    if (LastEvent (display, Expose,          &event)) Refresh (graphic, layout, 1);
+    if (LastEvent (display, MotionNotify,    &event)) UpdatePointer (graphic, layout, (XMotionEvent *) &event);
+    if (LastEvent (display, ButtonPress,     &event)) InterpretPresses (graphic, layout, (XButtonEvent *) &event);
+    if (LastEvent (display, KeyPress,        &event)) InterpretKeys (graphic, layout, &event);
+
+    if (LastEvent (display, MappingNotify,   &event)) XRefreshKeyboardMapping ((XMappingEvent *) &event);
+
+    /* drop and ignore the following StructureNotifyMask events */
+    LastEvent (display, GravityNotify, &event);
+    LastEvent (display, ReparentNotify, &event);
+    LastEvent (display, MapNotify, &event);
+    LastEvent (display, UnmapNotify, &event);
+
+    /* remove those events we will ignore */
+    while (XCheckMaskEvent (display, IgnoreMask, &event)) continue;
+
+    /* events to remove which have no mask component */
+    while (XCheckTypedEvent (display, MappingNotify, &event)) continue;
+    while (XCheckTypedEvent (display, ClientMessage, &event)) continue;
+    while (XCheckTypedEvent (display, SelectionClear, &event)) continue;
+    while (XCheckTypedEvent (display, SelectionNotify, &event)) continue;
+    while (XCheckTypedEvent (display, SelectionRequest, &event)) continue;
   }
   return (status);
 }
 
-/*     
-while (XPending (graphic[0].display) {
-if (readfds && display_mask) { 
-*/
-/*
-  while (1) {
-    fprintf (stderr, "window in EventLoop: %x  %d\n", graphic[0].display, graphic[0].window);
-    XNextEvent (graphic[0].display, &event);
-    if (event.type == ClientMessage) {
-      fprintf (stderr, "choice 1\n");
-    }
-    
-    if (event.xclient.send_event) {
-      fprintf (stderr, "choice 2\n");
-    }
-  }
-*/
-  
+# if (0)
+/* WARNING: this is defined because the HP UX installation i have does not
+seem to know this structure, though it should.  Clearly a need for autoconf.. */
+struct Ktimeval {
+  unsigned long tv_sec;         /* seconds */
+  long          tv_usec;        /* and microseconds */
+};
+# endif
+
Index: branches/ipp-magic-v0/Ohana/src/kii/event/InterpretKeys.c
===================================================================
--- branches/elixir/Ohana/src/kii/event/InterpretKeys.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/event/InterpretKeys.c	(revision 21062)
@@ -1,44 +1,82 @@
 # include "Ximage.h"
 
-int 
-InterpretKeys (graphic, layout, event)
-Graphic        graphic[];
-Layout         layout[];
-XEvent        *event;
-{
+int InterpretKeys (Graphic *graphic, Layout *layout, XEvent *event) {
 
-  int    status;
+  double X, Y, offset;
+  int    modstate;
+  char   string[10];
   KeySym keysym;
-  char            string[10];
   XComposeStatus  composestatus;
+  XKeyEvent *keyevent;
 
-  XLookupString ((XKeyEvent *)event, string, 9, &keysym, &composestatus);
+  keyevent = (XKeyEvent *) event;
+  XLookupString (keyevent, string, 9, &keysym, &composestatus);
+  modstate = keyevent[0].state;
+
+  // offset is in image pixels: 
+  // 0.5 image pixels is 1 screen pixel for expand == +2
+  // 2.0 image pixels is 1 screen pixel for expand == -2
+  if (layout[0].expand == 0) layout[0].expand = 1;
+  offset = (layout[0].expand > 0) ? 1.0 / layout[0].expand : -layout[0].expand;
+  if (modstate & ControlMask) offset *= 100;
 
   switch (keysym) {
 
-  case XK_Tab:
-    MOVE_POINTER = MOVE_POINTER ^ TRUE;
-    if (MOVE_POINTER) UpdatePointer (graphic, layout, (XMotionEvent *)event);
-    status = TRUE;
-    break;
+    case XK_KP_Home:
+    case XK_Home:
+      layout[0].expand = 1;
+      Reorient (graphic, layout, layout[0].X, layout[0].Y, 0);
+      break;
+    case XK_KP_End:
+    case XK_End:
+      layout[0].expand = 1;
+      Reorient (graphic, layout, 0, 0, 0);
+      break;
+    case XK_KP_Enter:
+    case XK_KP_Begin:
+    case XK_Return:
+      Screen_to_Image (&X, &Y, (double)keyevent[0].x, (double)keyevent[0].y, layout);
+      X = 0.5*layout[0].matrix.Naxis[0] - X;
+      Y = 0.5*layout[0].matrix.Naxis[1] - Y;
+      Reorient (graphic, layout, X, Y, 0);
+      break;
+    case XK_Prior:
+    case XK_KP_Prior:
+      Reorient (graphic, layout, layout[0].X, layout[0].Y, +1);
+      break;
+    case XK_Next:
+    case XK_KP_Next:
+      Reorient (graphic, layout, layout[0].X, layout[0].Y, -1);
+      break;
+    case XK_Up:
+    case XK_KP_Up:
+      Reorient (graphic, layout, layout[0].X, layout[0].Y + offset, 0);
+      break;
+    case XK_Down:
+    case XK_KP_Down:
+      Reorient (graphic, layout, layout[0].X, layout[0].Y - offset, 0);
+      break;
+    case XK_Left:
+    case XK_KP_Left:
+      Reorient (graphic, layout, layout[0].X + offset, layout[0].Y, 0);
+      break;
+    case XK_Right:
+    case XK_KP_Right:
+      Reorient (graphic, layout, layout[0].X - offset, layout[0].Y, 0);
+      break;
 
-  default:
-    status = TRUE;
+    case XK_Tab:
+      MOVE_POINTER = MOVE_POINTER ^ TRUE;
+      if (MOVE_POINTER) UpdatePointer (graphic, layout, (XMotionEvent *)event);
+      break;
   }
 
-  return (status);
-
+  return (TRUE);
 }
 
+/* other keys we might want to use:
 
+  
 
-
-/************* the stuff below this line is old - remove eventually */
-/*
-  case XK_Right:
-  case XK_Left:
-  this is all old, but a good example for putting in the cursor motion.
-
-    break;
 */
 
Index: branches/ipp-magic-v0/Ohana/src/kii/event/InterpretPresses.c
===================================================================
--- branches/elixir/Ohana/src/kii/event/InterpretPresses.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/event/InterpretPresses.c	(revision 21062)
@@ -10,5 +10,5 @@
   
   if ((event[0].type == ButtonPress) && InPicture (event, &layout[0].picture)) {
-    Reorient (graphic, layout, event);
+    ReorientOnButton (graphic, layout, event);
   }
 
@@ -50,2 +50,24 @@
 }
 
+void ReorientOnButton (Graphic *graphic, Layout *layout, XButtonEvent *mouse_event) {
+
+  double X, Y;
+
+  Screen_to_Image (&X, &Y, (double)mouse_event[0].x, (double)mouse_event[0].y, layout);
+  X = 0.5*layout[0].matrix.Naxis[0] - X;
+  Y = 0.5*layout[0].matrix.Naxis[1] - Y;
+
+  switch (mouse_event[0].button) {
+    case 1:
+      Reorient (graphic, layout, X, Y, 0);
+      break;
+    case 2:
+      Reorient (graphic, layout, X, Y, -1);
+      break;
+    case 3:
+      Reorient (graphic, layout, X, Y, +1);
+      break;
+    default:
+      break;
+  }
+}
Index: branches/ipp-magic-v0/Ohana/src/kii/event/Reconfig.c
===================================================================
--- branches/elixir/Ohana/src/kii/event/Reconfig.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/event/Reconfig.c	(revision 21062)
@@ -11,4 +11,5 @@
   graphic[0].dy = MAX(event[0].xconfigure.height, MIN_HEIGHT);
 
+  XClearWindow (graphic[0].display, graphic[0].window);
   PositionPictures (layout, graphic);
   CreateColorbar (layout, graphic);
Index: branches/ipp-magic-v0/Ohana/src/kii/include/Ximage.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/Ximage.h	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/include/Ximage.h	(revision 21062)
@@ -1,5 +1,8 @@
 # include <Xohana.h>
-# include <loneos.h>
+# include <dvo.h>
+# include <kapa.h>
 # include "constants.h"
 # include "structures.h"
 # include "prototypes.h"
+
+/* dvo.h is used only for Coords */
Index: branches/ipp-magic-v0/Ohana/src/kii/include/constants.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/constants.h	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/include/constants.h	(revision 21062)
@@ -14,5 +14,4 @@
 */
 
-# define EVENT_MASK (long) (ButtonPressMask | ClientMessage | ButtonReleaseMask | KeyPressMask | ExposureMask | StructureNotifyMask | PointerMotionMask)
 # define DEFAULT_CURSOR XC_crosshair
 # define BORDER_WIDTH 2
@@ -26,2 +25,34 @@
 int DECIMAL_DEG;
 int DEBUG;
+int FOREGROUND;
+int USE_XWINDOW;
+int MAP_WINDOW;
+char *NAME_WINDOW;
+
+# define EVENT_MASK (long) \
+(ButtonPressMask \
+ | ClientMessage \
+ | ButtonReleaseMask \
+ | KeyPressMask \
+ | ExposureMask \
+ | StructureNotifyMask \
+ | PointerMotionMask)
+
+/* EVENT_MASK consists of:
+
+ExposureMask        : Expose
+StructureNotifyMask : CirculateNotify | 
+                      ConfigureNotify | 
+                      DestroyNotify   | 
+		      GravityNotify   | 
+		      MapNotify       |
+		      ReparentNotify  |
+		      UnmapNotify
+ButtonPressMask     : ButtonPress
+ButtonReleaseMask   : ButtonRelease
+KeyPressMask        : KeyPress
+PointerMotionMask   : MotionNotify
+(always)            : ClientMessage 
+(always)            : MappingNotify
+
+*/
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R0.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R0.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R0_width 9
-#define R0_height 6
-static char R0_bits[] = {
-   0x00, 0x00, 0x7c, 0x00, 0x82, 0x00, 0x01, 0x01, 0x82, 0x00, 0x7c, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R1.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R1.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R1_width 9
-#define R1_height 6
-static char R1_bits[] = {
-   0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xff, 0x01, 0x02, 0x01, 0x04, 0x01};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R2.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R2.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R2_width 9
-#define R2_height 6
-static char R2_bits[] = {
-   0x00, 0x00, 0x0e, 0x01, 0x11, 0x01, 0x21, 0x01, 0x41, 0x01, 0x86, 0x01};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R3.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R3.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R3_width 9
-#define R3_height 6
-static char R3_bits[] = {
-   0x00, 0x00, 0xe3, 0x00, 0x15, 0x01, 0x19, 0x01, 0x11, 0x01, 0x81, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R4.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R4.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R4_width 9
-#define R4_height 6
-static char R4_bits[] = {
-   0x00, 0x00, 0x40, 0x00, 0xff, 0x01, 0x44, 0x00, 0x58, 0x00, 0x60, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R5.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R5.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R5_width 9
-#define R5_height 6
-static char R5_bits[] = {
-   0x00, 0x00, 0xf1, 0x00, 0x09, 0x01, 0x09, 0x01, 0x11, 0x01, 0x9f, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R6.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R6.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R6_width 9
-#define R6_height 6
-static char R6_bits[] = {
-   0x00, 0x00, 0xe2, 0x00, 0x11, 0x01, 0x11, 0x01, 0x11, 0x01, 0xfe, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R7.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R7.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R7_width 9
-#define R7_height 6
-static char R7_bits[] = {
-   0x00, 0x00, 0x03, 0x00, 0x0d, 0x00, 0x31, 0x00, 0xc1, 0x01, 0x01, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R8.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R8.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R8_width 9
-#define R8_height 6
-static char R8_bits[] = {
-   0x00, 0x00, 0xee, 0x00, 0x11, 0x01, 0x11, 0x01, 0x11, 0x01, 0xee, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/R9.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/R9.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define R9_width 9
-#define R9_height 6
-static char R9_bits[] = {
-   0x00, 0x00, 0xfe, 0x00, 0x11, 0x01, 0x11, 0x01, 0x11, 0x01, 0x8e, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/Rd.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/Rd.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define Rd_width 9
-#define Rd_height 6
-static char Rd_bits[] = {
-   0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0x00, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/Re.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/Re.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define Re_width 9
-#define Re_height 6
-static char Re_bits[] = {
-   0x00, 0x00, 0xb0, 0x00, 0x28, 0x01, 0x28, 0x01, 0x28, 0x01, 0xf0, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/Rm.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/Rm.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define Rm_width 9
-#define Rm_height 6
-static char Rm_bits[] = {
-   0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/icons/Rp.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/icons/Rp.h	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-#define Rp_width 9
-#define Rp_height 6
-static char Rp_bits[] = {
-   0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x7c, 0x00, 0x10, 0x00, 0x10, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/prototypes.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/prototypes.h	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/include/prototypes.h	(revision 21062)
@@ -1,2 +1,7 @@
+
+int args (int *argc, char **argv);
+
+void bDrawOverlay (Layout *layout, int N);
+
 int SetColormap (Graphic *graphic, Layout *layout, char *name);
 void FlushDisplay (Display *display);
@@ -6,4 +11,5 @@
 int PSit (Graphic *graphic, Layout *layout, int Raw);
 int JPEGit (Graphic *graphic, Layout *layout);
+int JPEGit24 (Graphic *graphic, Layout *layout);
 int Resize (Graphic *graphic, Layout *layout);
 int Center (Graphic *graphic, Layout *layout);
@@ -23,4 +29,10 @@
 void CreateZoom32 (Layout *layout, Graphic *graphic, double x, double y);
 
+void ConvertPixmap8  (Layout *layout, FILE *f);
+void ConvertPixmap16 (Layout *layout, FILE *f);
+void ConvertPixmap24 (Layout *layout, FILE *f);
+void ConvertPixmap32 (Layout *layout, FILE *f);
+
+
 /***** Prototypes for image ***************/
 
@@ -31,5 +43,6 @@
 void          Remap               PROTO((Graphic *, Layout *, Matrix  *));
 int           NewPicture          PROTO((Graphic *, Layout *));
-void          Reorient            PROTO((Graphic *, Layout *, XButtonEvent *));
+void          Reorient            PROTO((Graphic *, Layout *, double, double, int));
+void          ReorientOnButton    PROTO((Graphic *, Layout *, XButtonEvent *));
 int           UpdatePointer       PROTO((Graphic *, Layout *, XMotionEvent *));
 void          CrossHairs          PROTO((Graphic *, Layout *));
Index: branches/ipp-magic-v0/Ohana/src/kii/include/rotletters.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/rotletters.h	(revision 2465)
+++ 	(revision )
@@ -1,56 +1,0 @@
-#define R0_width 9
-#define R0_height 6
-static char R0_bits[] = {
-   0x00, 0x00, 0x7c, 0x00, 0x82, 0x00, 0x01, 0x01, 0x82, 0x00, 0x7c, 0x00};
-#define R1_width 9
-#define R1_height 6
-static char R1_bits[] = {
-   0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xff, 0x01, 0x02, 0x01, 0x04, 0x01};
-#define R2_width 9
-#define R2_height 6
-static char R2_bits[] = {
-   0x00, 0x00, 0x0e, 0x01, 0x11, 0x01, 0x21, 0x01, 0x41, 0x01, 0x86, 0x01};
-#define R3_width 9
-#define R3_height 6
-static char R3_bits[] = {
-   0x00, 0x00, 0xe3, 0x00, 0x15, 0x01, 0x19, 0x01, 0x11, 0x01, 0x81, 0x00};
-#define R4_width 9
-#define R4_height 6
-static char R4_bits[] = {
-   0x00, 0x00, 0x40, 0x00, 0xff, 0x01, 0x44, 0x00, 0x58, 0x00, 0x60, 0x00};
-#define R5_width 9
-#define R5_height 6
-static char R5_bits[] = {
-   0x00, 0x00, 0xf1, 0x00, 0x09, 0x01, 0x09, 0x01, 0x11, 0x01, 0x9f, 0x00};
-#define R6_width 9
-#define R6_height 6
-static char R6_bits[] = {
-   0x00, 0x00, 0xe2, 0x00, 0x11, 0x01, 0x11, 0x01, 0x11, 0x01, 0xfe, 0x00};
-#define R7_width 9
-#define R7_height 6
-static char R7_bits[] = {
-   0x00, 0x00, 0x03, 0x00, 0x0d, 0x00, 0x31, 0x00, 0xc1, 0x01, 0x01, 0x00};
-#define R8_width 9
-#define R8_height 6
-static char R8_bits[] = {
-   0x00, 0x00, 0xee, 0x00, 0x11, 0x01, 0x11, 0x01, 0x11, 0x01, 0xee, 0x00};
-#define R9_width 9
-#define R9_height 6
-static char R9_bits[] = {
-   0x00, 0x00, 0xfe, 0x00, 0x11, 0x01, 0x11, 0x01, 0x11, 0x01, 0x8e, 0x00};
-#define Rd_width 9
-#define Rd_height 6
-static char Rd_bits[] = {
-   0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0xc0, 0x01, 0x80, 0x00, 0x00, 0x00};
-#define Re_width 9
-#define Re_height 6
-static char Re_bits[] = {
-   0x00, 0x00, 0xb0, 0x00, 0x28, 0x01, 0x28, 0x01, 0x28, 0x01, 0xf0, 0x00};
-#define Rm_width 9
-#define Rm_height 6
-static char Rm_bits[] = {
-   0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00};
-#define Rp_width 9
-#define Rp_height 6
-static char Rp_bits[] = {
-   0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x7c, 0x00, 0x10, 0x00, 0x10, 0x00};
Index: branches/ipp-magic-v0/Ohana/src/kii/include/structures.h
===================================================================
--- branches/elixir/Ohana/src/kii/include/structures.h	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/include/structures.h	(revision 21062)
@@ -67,5 +67,5 @@
   int      dx, dy, x, y;
   XImage  *pix;
-  char    *data;
+  char *data;
 } Picture;
 
@@ -124,5 +124,4 @@
 } Layout;
 
-
 /* this routine is independent of the number of overlays */
 
Index: branches/ipp-magic-v0/Ohana/src/kii/notes
===================================================================
--- branches/elixir/Ohana/src/kii/notes	(revision 2465)
+++ 	(revision )
@@ -1,4 +1,0 @@
-1/31/94: totally revamped the overlay function -- made two overlays,
-red and green.  Added two buttons to toggle them on and off.  Made
-functions for mana to "load" and "save" files with overlay objects
-defined.  Made contour function for mana.  
Index: branches/ipp-magic-v0/Ohana/src/kii/overlay/DrawOverlay.c
===================================================================
--- branches/elixir/Ohana/src/kii/overlay/DrawOverlay.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/overlay/DrawOverlay.c	(revision 21062)
@@ -9,4 +9,5 @@
   double expand, X0, Y0;
  
+  expand = 1.0;
   if (layout[0].expand > 0) {
     expand = 1 / (1.0*layout[0].expand);
Index: branches/ipp-magic-v0/Ohana/src/kii/overlay/EraseOverlay.c
===================================================================
--- branches/elixir/Ohana/src/kii/overlay/EraseOverlay.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/overlay/EraseOverlay.c	(revision 21062)
@@ -6,4 +6,5 @@
   int i, status, N;
 
+  fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  
   status = read (layout[0].Ximage, buffer, 16); 
   buffer[16] = 0; 
@@ -23,5 +24,7 @@
   }
 
-  Refresh (graphic, layout, 0);
+  if (USE_XWINDOW) Refresh (graphic, layout, 0);
+
+  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
   return (TRUE);
 }
Index: branches/ipp-magic-v0/Ohana/src/kii/overlay/LoadOverlay.c
===================================================================
--- branches/elixir/Ohana/src/kii/overlay/LoadOverlay.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/overlay/LoadOverlay.c	(revision 21062)
@@ -88,10 +88,12 @@
   OVERLAY[N] = TRUE;
   /*  Refresh (graphic, layout, 0); */
-  for (i = 0; i < NOVERLAYS; i++) {
-    if (OVERLAY[i])
-      PaintOverlay (graphic, layout, i);
+  if (USE_XWINDOW) {
+    for (i = 0; i < NOVERLAYS; i++) {
+      if (OVERLAY[i])
+	PaintOverlay (graphic, layout, i);
+    }
+    PaintTickmarks (graphic, layout);
+    XFlush (graphic[0].display);
   }
-  PaintTickmarks (graphic, layout);
-  XFlush (graphic[0].display);
   fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
   return (TRUE);
Index: branches/ipp-magic-v0/Ohana/src/kii/overlay/LoadTickmarks.c
===================================================================
--- branches/elixir/Ohana/src/kii/overlay/LoadTickmarks.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/overlay/LoadTickmarks.c	(revision 21062)
@@ -55,5 +55,6 @@
   layout[0].tickmarks.Nobjects = Nobjects;
 
-  Refresh (graphic, layout, 0);
+  if (USE_XWINDOW) Refresh (graphic, layout, 0);
+
   fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
   return (TRUE);
Index: branches/ipp-magic-v0/Ohana/src/kii/overlay/PaintOverlay.c
===================================================================
--- branches/elixir/Ohana/src/kii/overlay/PaintOverlay.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/overlay/PaintOverlay.c	(revision 21062)
@@ -11,4 +11,5 @@
   XSetForeground (graphic[0].display, graphic[0].gc, layout[0].overlay[N].color);
   
+  expand = 1.0;
   if (layout[0].expand > 0) {
     expand = 1 / (1.0*layout[0].expand);
Index: branches/ipp-magic-v0/Ohana/src/kii/overlay/RotFont.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/overlay/RotFont.c	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/overlay/RotFont.c	(revision 21062)
@@ -0,0 +1,124 @@
+# include "Ximage.h"
+# include "alphabet.h"
+  
+static int Nrotfonts;
+static FontSet *RotFonts;
+
+static char currentname[64];
+static int  currentsize;
+static double currentscale;
+static RotFont *currentfont;
+
+void InitRotFonts () {
+
+  int i, Nhardwired;
+
+  Nhardwired = sizeof (HardwiredFonts) / sizeof (FontSet);
+  
+  Nrotfonts = Nhardwired;
+  ALLOCATE (RotFonts, FontSet, Nrotfonts);
+  
+  for (i = 0; i < Nhardwired; i++) {
+    RotFonts[i] = HardwiredFonts[i];
+  }
+
+  currentfont = RotFonts[DEFFONT].font;
+  currentscale = 1.0;
+  strcpy (currentname, RotFonts[DEFFONT].name);
+  currentsize = RotFonts[DEFFONT].size;
+}
+
+int SetRotFont (char *name, int size) {
+  
+  int i, nsize, msize, bsize, bigger, dsize, match, good;
+
+  bigger = good = match = -1;
+  dsize = 10000;
+  bsize = 10000;
+  for (i = 0; i < Nrotfonts; i++) {
+    if (!strcasecmp (RotFonts[i].name, name)) {
+      good = i;
+      nsize = abs (RotFonts[i].size - size);
+      if (nsize < dsize) {
+	match = i;
+	dsize = nsize;
+      }
+      msize = RotFonts[i].size - size;
+      if ((msize < bsize) && (msize >= 0)) {
+	bigger = i;
+	bsize = msize;
+      }
+    }
+  }
+  
+  if ((match == -1) && (good != -1)) match = good;
+  if (bigger != -1) match = bigger;
+  if (match != -1) {
+    currentfont = RotFonts[match].font;
+    currentscale = (double) size / RotFonts[match].size;
+    currentsize = size;
+    strcpy (currentname, name);
+    return (TRUE);
+  } else {
+    fprintf (stderr, "no matching font\n");
+    return (FALSE);
+  }
+
+}
+  
+char *GetRotFont (int *size) {
+
+  *size = currentsize;
+  return (currentname);
+
+}
+
+RotFont *GetRotFontData (double *scale) {
+  *scale = currentscale;
+  return (currentfont);
+}
+
+int RotStrlen (char *c) {
+
+  int i, N, dX, code;
+  double scale; 
+  
+  scale = currentscale;
+
+  /* find string length */
+  dX = 0;
+
+  code = FALSE;
+  for (i = 0; i < strlen (c); i++) {
+    N = (int)(c[i]);
+    /* skip non-printing characters */
+    if ((N < 0) || (N >= NROT)) continue;
+
+    /* check for special characters */
+    if (!code) {
+      if (N == 94) { /* super-script */
+	scale *= 0.8;
+	continue;
+      }
+      if (N == 95) { /* sub-script */
+	scale *= 0.8;
+	continue;
+      }
+      if (N == 124) { /* normal-script */
+	scale = currentscale;
+	continue;
+      }
+      if (N == 92) { /* backslash */
+	code = TRUE;
+	continue;
+      } 
+      if (N == 38) { /* font-code */
+	i++;
+	continue;
+      }
+    }
+    code = FALSE;
+    dX += scale*currentfont[N].dx + 1;
+  }
+  return (dX);
+}
Index: branches/ipp-magic-v0/Ohana/src/kii/overlay/bDrawOverlay.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/overlay/bDrawOverlay.c	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/overlay/bDrawOverlay.c	(revision 21062)
@@ -0,0 +1,93 @@
+# include "Ximage.h"
+# define INFRONT 4
+
+static char name[4][16] = {"red", "green", "blue", "yellow"};
+
+void bDrawOverlay (Layout *layout, int N) {
+
+  int i;
+  int dX, dY, dx, dy;
+  int X, Y, Xmin, Ymin, Xmax, Ymax, Xrange, Yrange;
+  double expand, X0, Y0;
+  bDrawColor color;
+ 
+  /* translate color to bDrawColors : layout[0].overlay[N].color */
+  color = KapaColorByName (name[N]);
+  bDrawSetStyle (color, 0, 0);
+  
+  expand = 1.0;
+  if (layout[0].expand > 0) {
+    expand = 1 / (1.0*layout[0].expand);
+  }
+  if (layout[0].expand < 0) {
+    expand = fabs((double)layout[0].expand);
+  }
+  /* 
+  X = 0.5*layout[0].matrix.Naxis[0] - expand*((int)(0.5*layout[0].picture.dx + 0.5) - 0.0) - layout[0].X;
+  Y = 0.5*layout[0].matrix.Naxis[1] - expand*((int)(0.5*layout[0].picture.dy + 0.5) - 0.0) - layout[0].Y;  
+  X0 =  (layout[0].X + 1 - (int)(0.5*layout[0].matrix.Naxis[0] + 0.5) - X + (int)X)/expand + layout[0].picture.x + 0.5*layout[0].picture.dx;
+  Y0 =  (layout[0].Y + 1 - (int)(0.5*layout[0].matrix.Naxis[1] + 0.5) - Y + (int)Y)/expand + layout[0].picture.y + 0.5*layout[0].picture.dy;
+  */
+
+  Image_to_Screen (&X0, &Y0, 0.0, 0.0, layout);
+  X0 -= layout[0].picture.x;
+  Y0 -= layout[0].picture.y;
+
+  Xmin = 0;
+  Ymin = 0;
+  Xmax = layout[0].picture.dx;
+  Ymax = layout[0].picture.dy;
+  Xrange = layout[0].picture.dx;
+  Yrange = layout[0].picture.dy;
+
+  for (i = 0; i < layout[0].overlay[N].Nobjects; i++) {
+    if (N == INFRONT) {
+      X  = layout[0].overlay[N].objects[i].x * Xrange;
+      Y  = layout[0].overlay[N].objects[i].y * Yrange;
+      dX = layout[0].overlay[N].objects[i].dx * Xrange;
+      dY = layout[0].overlay[N].objects[i].dy * Yrange;
+    } else {
+      X  = layout[0].overlay[N].objects[i].x/expand + X0;
+      Y =  layout[0].overlay[N].objects[i].y/expand + Y0;
+      dX = layout[0].overlay[N].objects[i].dx/expand;
+      dY = layout[0].overlay[N].objects[i].dy/expand;
+    }
+    if ((X + dX < Xmin) || (X - dX > Xmax) ||
+	(Y + dY < Ymin) || (Y - dY > Ymax)) {
+      continue;
+    }
+
+    /* for a LINE, (x, y) is the start, (dx, dy) is the distance to end
+       for a CIRCLE (x, y) is the center, (dx, dy) is the radius 
+       for a BOX (x, y) is the center, (dx, dy) is the width */
+
+    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "LINE")) {
+      bDrawLine (X, Y, (X+dX), (Y+dY));
+      continue;
+    }
+    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "TEXT")) {
+      bDrawRotText (X, Y, layout[0].overlay[N].objects[i].text, 8, 0.0);
+      continue;
+    }
+    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "BOX")) {
+      dx = MAX (abs(dX),2) / 2;
+      dy = MAX (abs(dY),2) / 2;
+      bDrawRectOpen ((X-dx), (Y-dy), (X+dx), (Y+dy));
+      // bDrawRectOpen ((X-dx), (Y-dy), (X), (Y));
+       continue;
+    }
+    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "CIRCLE")) {
+      dx = MAX (abs(dX),2);
+      dy = MAX (abs(dY),2);
+      bDrawArc (X, Y, dx, dy, 0, 360);
+      continue;
+    }
+    fprintf (stderr, "don't know %s, skipping (PO)\n", layout[0].overlay[N].objects[i].type);
+  }
+  
+  /* translate color to bDrawColors : layout[0].overlay[N].color */
+  bDrawSetStyle (color, 0, 0);
+}
+
+/* this routine is independent of the number of overlays */
+
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/Center.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/Center.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/Center.c	(revision 21062)
@@ -7,4 +7,5 @@
   double X, Y;
 
+  fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  
   status = read (layout[0].Ximage, buffer, 16); 
   buffer[16] = 0; 
@@ -20,8 +21,11 @@
   }
 
-  Remap (graphic, layout, &layout[0].matrix);
-  Refresh (graphic, layout, 0);
+  if (USE_XWINDOW) {
+    Remap (graphic, layout, &layout[0].matrix);
+    Refresh (graphic, layout, 0);
+    XFlush (graphic[0].display);
+  }
 
-  XFlush (graphic[0].display);
+  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
   return (TRUE);
 }
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/CreatePicture.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/CreatePicture.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/CreatePicture.c	(revision 21062)
@@ -4,5 +4,5 @@
 
   int i, j, extra;
-  char *c;
+  unsigned char *c;
   unsigned int *l;
   unsigned int start, start1, start2, start3;
@@ -13,5 +13,5 @@
   case 8:
     REALLOCATE (layout[0].picture.data, char, layout[0].picture.dx*layout[0].picture.dy);
-    c = layout[0].picture.data;
+    c = (unsigned char *) layout[0].picture.data;
     for (i = 0; i < (layout[0].picture.dx*layout[0].picture.dy); i++, c++)
       *c = start;
@@ -22,5 +22,5 @@
   case 16:
     REALLOCATE (layout[0].picture.data, char, 2*layout[0].picture.dy*layout[0].picture.dx);
-    c = layout[0].picture.data;
+    c = (unsigned char *) layout[0].picture.data;
     start1 = 0x0000ff & (start);
     start2 = 0x0000ff & (start >> 8);
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/CursorOps.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/CursorOps.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/CursorOps.c	(revision 21062)
@@ -5,4 +5,5 @@
   double expand;
 
+  expand = 1.0;
   if (layout[0].expand > 0) {
     expand = 1 / (1.0*layout[0].expand);
@@ -22,4 +23,5 @@
 
   /* notice that here, expand is the reciprocal of the expand above */
+  expand = 1.0;
   if (layout[0].expand > 0) {
     expand = abs(layout[0].expand);
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/JPEGit24.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/picture/JPEGit24.c	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/JPEGit24.c	(revision 21062)
@@ -0,0 +1,225 @@
+# include "Ximage.h"
+# include "jpeglib.h"
+
+# define WHITE_R 255
+# define WHITE_G 255
+# define WHITE_B 255
+
+int JPEGit24 (Graphic *graphic, Layout *layout) {
+
+  struct jpeg_compress_struct cinfo;
+  struct jpeg_error_mgr jerr;
+  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
+  JSAMPLE *image_buffer;	/* Points to data for current line */
+  JSAMPLE *line_buffer;	        /* Points to data for current line */
+
+  int ii, i, j;
+  int i_start, i_end, j_start, j_end;
+  int dropback;  /* this is a bit of a kludge... */
+  int dx, dy, DX, DY;
+  int status, Nbytes, quality;
+  int expand_in, expand_out;
+  double expand, Rx, Ry, X, Y;
+  unsigned char *out_pix, *in_pix, *in_pix_ref;
+  unsigned char pixel1[256], pixel2[256], pixel3[256];
+  char filename[1024];
+  FILE *f;
+
+  /* expect a line telling the number of bytes and a filename */
+  status = read (layout[0].Ximage, filename, 16);
+  filename[16] = 0;
+  sscanf (filename, "%*s %d", &Nbytes);
+  status = read (layout[0].Ximage, filename, Nbytes);
+  filename[status] = 0; /* make the string easy to parse */
+
+  /***** JPEG init calls */
+  cinfo.err = jpeg_std_error (&jerr);
+  jpeg_create_compress (&cinfo);
+
+  f = fopen (filename, "w");
+  if (f == (FILE *) NULL) {
+    fprintf (stderr, "Kii: failed to open %s for output\n", filename);
+    return (TRUE);
+  }
+  jpeg_stdio_dest(&cinfo, f);
+  
+  quality = 75;
+  cinfo.image_width = layout[0].picture.dx; 	/* image width and height, in pixels */
+  cinfo.image_height = layout[0].picture.dy;
+# ifdef GREYSCALE
+  cinfo.input_components = 1;		        /* # of color components per pixel */
+  cinfo.in_color_space = JCS_GRAYSCALE; 	/* colorspace of input image */
+# else 
+  cinfo.input_components = 3;		        
+  cinfo.in_color_space = JCS_RGB; 	
+# endif
+  jpeg_set_defaults (&cinfo);
+  jpeg_set_quality (&cinfo, quality, TRUE       /* limit to baseline-JPEG values */);
+  jpeg_start_compress (&cinfo, TRUE);
+
+  /** cmap[i].pixel must be defined even if X is not used **/
+  for (i = 0; i < 256; i++) { /* set up pixel array */
+    pixel1[i] = layout[0].cmap[i].red >> 8;
+    pixel2[i] = layout[0].cmap[i].green >> 8;
+    pixel3[i] = layout[0].cmap[i].blue >> 8;
+  }
+
+  expand = expand_in = expand_out = 1.0;
+  if (layout[0].expand == 0) /* set up expansions */
+    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 (image_buffer, JSAMPLE, 3*dx*dy);
+  ALLOCATE (line_buffer, JSAMPLE, 3*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) **********/
+
+  /**** fill in bottom area ****/
+  out_pix = line_buffer;
+  for (i = 0; i < dx; i++, out_pix+=3) {
+    out_pix[0] = WHITE_R;
+    out_pix[1] = WHITE_G;
+    out_pix[2] = WHITE_B;
+  }
+  for (j = 0; j < j_start; j++) {
+    memcpy (&image_buffer[j*3*dx], line_buffer, 3*dx);
+  }
+  
+  /*** 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[0] = WHITE_R;
+      out_pix[1] = WHITE_G;
+      out_pix[2] = WHITE_B;
+    }
+    
+    /*** 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+=3) {
+	out_pix[0] = pixel1[*in_pix];
+	out_pix[1] = pixel2[*in_pix];
+	out_pix[2] = pixel3[*in_pix];
+      }
+    }
+    
+    /**** fill in area to the right of the picture ****/
+    for (i = i_end; i < dx; i++, out_pix+=3) {
+      out_pix[0] = WHITE_R;
+      out_pix[1] = WHITE_G;
+      out_pix[2] = WHITE_B;
+    }
+
+    /* write out the image line expand_out times */
+    for (i = 0; i < expand_out; i++) {
+      memcpy (&image_buffer[(j + i)*3*dx], line_buffer, 3*dx);
+    }
+  }
+
+  /**** fill in top area ****/
+  out_pix = line_buffer;
+  for (i = 0; i < dx; i++, out_pix+=3) { 
+    out_pix[0] = WHITE_R;
+    out_pix[1] = WHITE_G;
+    out_pix[2] = WHITE_B;
+  }
+  for (j = j_end; j < dy; j++) {
+    memcpy (&image_buffer[j*3*dx], line_buffer, 3*dx);
+  }
+
+
+  /* I need to write the overlay objects on the jpeg image.
+     if i can write / overwrite data in jpeg buffer, then do it here,
+     otherwise i need to create a temporary image buffer, then write the 
+     scanlines to that buffer */
+
+  {
+    int Npalette;
+    png_color *palette;
+    bDrawColor white, color;
+    bDrawBuffer *buffer;
+
+    palette = KapaPNGPalette (&Npalette);
+
+    buffer = bDrawBufferCreate (dx, dy);
+    bDrawSetBuffer (buffer);
+    for (i = 0; i < NOVERLAYS; i++) {
+      if (OVERLAY[i]) bDrawOverlay (layout, i);
+    }
+
+    white = KapaColorByName ("white");
+    for (j = 0; j < dy; j++) {
+      for (i = 0; i < dx; i++) {
+	color = buffer[0].pixels[j][i];
+	if (color == white) continue;
+	image_buffer[j*3*dx + 3*i + 0] = palette[color].red;
+	image_buffer[j*3*dx + 3*i + 1] = palette[color].green;
+	image_buffer[j*3*dx + 3*i + 2] = palette[color].blue;
+      }
+    }
+    bDrawBufferFree (buffer);
+  }
+
+  for (i = 0; i < dy; i++) {
+    row_pointer[0] = &image_buffer[i*3*dx];
+    (void) jpeg_write_scanlines (&cinfo, row_pointer, 1);
+  }
+
+  jpeg_finish_compress (&cinfo);
+  fclose (f);
+  jpeg_destroy_compress (&cinfo);
+
+  return (TRUE);
+}
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/NewPicture.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/NewPicture.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/NewPicture.c	(revision 21062)
@@ -1,44 +1,31 @@
 # include "Ximage.h"
-# define SCAN \
-  status = read (layout[0].Ximage, buffer, 16); \
-  buffer[16] = 0; \
-  sscanf (buffer, "%*s %d", &Nbytes); \
-  status = read (layout[0].Ximage, buffer, Nbytes); \
-  buffer[status] = 0; /* make the string easy to parse */ \
-  (DEBUG) && fprintf (stderr, "buffer: <%s>\n", buffer); \
 
 int NewPicture (Graphic *graphic, Layout *layout) {
 
   Header header;
-  char buffer[1024], *buff;
-  int status, bytes_left, Nbytes;
+  char *buff;
+  int status, bytes_left, socket;
  
-  fits_free_matrix (&layout[0].matrix);
-  sprintf (buffer, "NCOLOR: %6d ", layout[0].Npixels);
-  write (layout[0].Ximage, buffer, 16);
+  socket = layout[0].Ximage;
 
-  fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  
+  KiiSendMessage (socket, "%d", layout[0].Npixels);
+
+  fcntl (socket, F_SETFL, !O_NONBLOCK);  
 
   header.Naxes = 2;
-  SCAN;
-  sscanf (buffer, "%d %d %d %d %lf %lf",
-	  &header.Naxis[0], &header.Naxis[1], &header.bitpix, 
-	  &header.unsign,   &header.bzero,    &header.bscale);
 
-  SCAN;
-  sscanf (buffer, "%lf %lf %lf %lf %d", &layout[0].zero, &layout[0].range, &layout[0].min,  &layout[0].max, &header.size);
+  KiiScanMessage (socket, "%d %d %d %d %lf %lf", &header.Naxis[0], &header.Naxis[1]);
+  KiiScanMessage (socket, "%d %d %d %d %lf %lf", &header.bitpix, &header.unsign, &header.bzero, &header.bscale);
+  KiiScanMessage (socket, "%lf %lf",  &layout[0].zero, &layout[0].range);
+  KiiScanMessage (socket, "%lf %lf %d", &layout[0].min,  &layout[0].max, &header.size);
+  KiiScanMessage (socket, "%lf %f %f %f %f", &layout[0].coords.crval1, &layout[0].coords.crpix1, &layout[0].coords.cdelt1, &layout[0].coords.pc1_1, &layout[0].coords.pc1_2);
+  KiiScanMessage (socket, "%lf %f %f %f %f", &layout[0].coords.crval2, &layout[0].coords.crpix2, &layout[0].coords.cdelt2, &layout[0].coords.pc2_1, &layout[0].coords.pc2_2);
+	  
+  KiiScanMessage (socket, "%s %s %s", layout[0].coords.ctype, layout[0].file, layout[0].buffer_name);
 
-  SCAN;
-  sscanf (buffer, "%lf %f %f %f %f", &layout[0].coords.crval1, &layout[0].coords.crpix1, &layout[0].coords.cdelt1, &layout[0].coords.pc1_1, &layout[0].coords.pc1_2);
-	  
-  SCAN;
-  sscanf (buffer, "%lf %f %f %f %f", &layout[0].coords.crval2, &layout[0].coords.crpix2, &layout[0].coords.cdelt2, &layout[0].coords.pc2_1, &layout[0].coords.pc2_2);
-	  
-  SCAN;
-  sscanf (buffer, "%s %s %s", layout[0].coords.ctype, layout[0].file, layout[0].buffer_name);
+  gfits_free_matrix (&layout[0].matrix);
+  gfits_create_matrix (&header, &layout[0].matrix);
 
-  fits_create_matrix (&header, &layout[0].matrix);
-
-  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
+  fcntl (socket, F_SETFL, O_NONBLOCK);  
 
   status = 1;
@@ -47,5 +34,5 @@
   layout[0].matrix.size = 0;
   while (bytes_left > 0) {
-    status = read (layout[0].Ximage, buff, bytes_left);
+    status = read (socket, buff, bytes_left);
     if (status == 0) {  /* No more pipe */
       fprintf (stderr, "error: pipe closed\n");
@@ -59,5 +46,5 @@
   }
 
-  (DEBUG) && fprintf (stderr, "read %d bytes\n", layout[0].matrix.size);
+  if (DEBUG) fprintf (stderr, "read %d bytes\n", layout[0].matrix.size);
   /* it it not obvious this condition should kill kii, but ... */
   if (layout[0].matrix.size != header.size) {  
@@ -66,9 +53,10 @@
   }
 
+  if (!USE_XWINDOW) return (TRUE);
+
   Remap (graphic, layout, &layout[0].matrix);
-  (DEBUG) && fprintf (stderr, "remapped image\n");
+  if (DEBUG) fprintf (stderr, "remapped image\n");
   Refresh (graphic, layout, 0);
-  
-  (DEBUG) && fprintf (stderr, "refreshed\n");
+  if (DEBUG) fprintf (stderr, "refreshed\n");
   XFlush (graphic[0].display);
 
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/Remap16.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/Remap16.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/Remap16.c	(revision 21062)
@@ -26,4 +26,5 @@
   white2 = 0x00ff & (white >> 8);
 
+  expand = expand_in = expand_out = 1.0;
   if (layout[0].expand == 0) /* set up expansions */
     layout[0].expand = 1;
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/Remap24.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/Remap24.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/Remap24.c	(revision 21062)
@@ -27,4 +27,5 @@
   white3 = 0x0000ff & (white >> 16);
 
+  expand = expand_in = expand_out = 1.0;
   if (layout[0].expand == 0) /* set up expansions */
     layout[0].expand = 1;
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/Remap32.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/Remap32.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/Remap32.c	(revision 21062)
@@ -20,4 +20,5 @@
   white = layout[0].white;
 
+  expand = expand_in = expand_out = 1.0;
   if (layout[0].expand == 0) /* set up expansions */
     layout[0].expand = 1;
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/Remap8.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/Remap8.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/Remap8.c	(revision 21062)
@@ -20,4 +20,5 @@
   white = layout[0].white;
 
+  expand = expand_in = expand_out = 1.0;
   if (layout[0].expand == 0) /* set up expansions */
     layout[0].expand = 1;
@@ -67,6 +68,6 @@
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
 
-  out_pix = layout[0].picture.data;
-  in_pix  = (unsigned char *) (matrix[0].buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);
+  out_pix = (unsigned char *) layout[0].picture.data;
+  in_pix  = (unsigned char *) matrix[0].buffer + DX*(int)MAX(Y,0) + (int)MAX(X,0);
 
   /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/Reorient.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/Reorient.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/Reorient.c	(revision 21062)
@@ -1,26 +1,17 @@
 # include "Ximage.h"
 
-void Reorient (Graphic *graphic, Layout *layout, XButtonEvent *mouse_event) {
-
-  int x, y, dummy2;
-  double X0, Y0;
-  unsigned int dummy3;
-  Window dummy1;
+void Reorient (Graphic *graphic, Layout *layout, double X, double Y, int mode) {
 
   if (layout[0].expand == 0)
     layout[0].expand = 1;
 
-  Screen_to_Image (&X0, &Y0, (double)mouse_event[0].x, (double)mouse_event[0].y, layout);
-  X0 = 0.5*layout[0].matrix.Naxis[0] - X0;
-  Y0 = 0.5*layout[0].matrix.Naxis[1] - Y0;
-
-  switch (mouse_event[0].button) {
-  case 1:
-    if ((layout[0].X != X0) || (layout[0].Y != Y0)) {
-      layout[0].X = X0;
-      layout[0].Y = Y0;
+  switch (mode) {
+  case 0:
+    if ((layout[0].X != X) || (layout[0].Y != Y)) {
+      layout[0].X = X;
+      layout[0].Y = Y;
     }
     break;
-  case 2: 
+  case -1: 
     layout[0].expand--;
     if ((layout[0].expand == 0) || (layout[0].expand == -1))
@@ -31,8 +22,8 @@
       return;
     }
-    layout[0].X = X0;
-    layout[0].Y = Y0;
+    layout[0].X = X;
+    layout[0].Y = Y;
     break;
-  case 3:
+  case +1:
     layout[0].expand++;
     if ((layout[0].expand == 0) || (layout[0].expand == -1))
@@ -43,6 +34,6 @@
       return;
     }
-    layout[0].X = X0;
-    layout[0].Y = Y0;
+    layout[0].X = X;
+    layout[0].Y = Y;
     break;
   }
@@ -51,5 +42,8 @@
   Refresh (graphic, layout, 0);
  
-# ifndef NOWARP  /* the "warp" function seems to be a problem on SUNs for some reason */
+  XFlush (graphic[0].display);
+}
+
+# if (0) /* the "warp" function seems to be a problem on SUNs for some reason */
   XQueryPointer (graphic[0].display, graphic[0].window, &dummy1, &dummy1, &dummy2, &dummy2, &x, &y, &dummy3); 
   if ((x > layout[0].picture.x) && (x < layout[0].picture.x + layout[0].picture.dx) &&
@@ -62,4 +56,2 @@
 # endif
 
-  XFlush (graphic[0].display);
-}
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/Resize.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/Resize.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/Resize.c	(revision 21062)
@@ -1,20 +1,9 @@
 # include "Ximage.h"
-# define SCAN \
-  status = read (layout[0].Ximage, buffer, 16); \
-  buffer[16] = 0; \
-  sscanf (buffer, "%*s %d", &Nbytes); \
-  status = read (layout[0].Ximage, buffer, Nbytes); \
-  buffer[status] = 0; /* make the string easy to parse */ \
-  (DEBUG) && fprintf (stderr, "buffer: <%s>\n", buffer); \
-
 
 int Resize (Graphic *graphic, Layout *layout) {
  
-  int status, Nbytes;
   unsigned int NX, NY;
-  char buffer[128];
 
-  SCAN;
-  sscanf (buffer, "%d %d", &NX, &NY);
+  KiiScanMessage (layout[0].Ximage, "%d %d", &NX, &NY);
 
   NX += ZOOM_X + 3*PAD1 + 25;
@@ -24,18 +13,18 @@
     return (TRUE);
 
-  /*  graphic[0].dx = MAX(NX,  MIN_WIDTH); 
-  graphic[0].dy = MAX(NY, MIN_HEIGHT); */
   graphic[0].dx = MAX(NX, 50); 
   graphic[0].dy = MAX(NY, 50); 
 
-  XResizeWindow (graphic[0].display, graphic[0].window, NX, NY);
+  if (USE_XWINDOW) XResizeWindow (graphic[0].display, graphic[0].window, NX, NY);
 
   PositionPictures (layout, graphic);
-  CreateColorbar (layout, graphic);
-  CreatePicture (layout, graphic);
-  Remap (graphic, layout, &layout[0].matrix); 
 
-  XClearWindow (graphic[0].display, graphic[0].window);
-  Refresh (graphic, layout, 1);
+  if (USE_XWINDOW) {
+    CreateColorbar (layout, graphic);
+    CreatePicture (layout, graphic);
+    Remap (graphic, layout, &layout[0].matrix); 
+    XClearWindow (graphic[0].display, graphic[0].window);
+    Refresh (graphic, layout, 1);
+  }
 
   return (TRUE);
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/TestPicture.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/TestPicture.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/TestPicture.c	(revision 21062)
@@ -12,5 +12,5 @@
   double frac;
 
-  fits_free_matrix (&layout[0].matrix);
+  gfits_free_matrix (&layout[0].matrix);
 
   header.Naxes = 2;
@@ -27,5 +27,5 @@
   layout[0].max = layout[0].Npixels;
   
-  fits_create_matrix (&header, &layout[0].matrix);
+  gfits_create_matrix (&header, &layout[0].matrix);
 
   frac = layout[0].Npixels / (512.0*512.0);
Index: branches/ipp-magic-v0/Ohana/src/kii/picture/cursor.c
===================================================================
--- branches/elixir/Ohana/src/kii/picture/cursor.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/picture/cursor.c	(revision 21062)
@@ -1,86 +1,82 @@
 # include "Ximage.h"
+
+# define IgnoreMask (long) (~(StructureNotifyMask | SubstructureNotifyMask | ExposureMask | KeyPressMask | ButtonPressMask | PointerMotionMask))
+
+int LastEvent (Display *display, int type, XEvent *event);
 
 int cursor (Graphic *graphic, Layout *layout) {
 
+  Display        *display;
   XEvent          event;
   KeySym          keysym;
-  int             status, xstatus, value;
+  int             status;
   XComposeStatus  composestatus;
-  char            string[10], line[500];
+  char            string[10], line[40], *name;
   double          x, y;
-  char            buffer[1024];
   
   while (1) {
     
-    status = read (layout[0].Ximage, buffer, 4);
-    buffer[4] = 0;
-    
-    switch (status) {
-    case -1:
-      /* no input from pipe: continue */
-      break;
-      
-    case 0:
-      fprintf (stderr, "pipe has died!\n");
-      return (FALSE);
-      break;
+    status = CheckPipe (graphic, layout);
+    if (status == 0) return (FALSE);
+    if (status == 2) return (TRUE);
+    if (status == 1) goto events;
 
-    case 4:
-      if (!strcmp (buffer, "NCUR")) {  /* stop reading cursor */
-	return (TRUE);
-      }
-      break;
-
-    default:
-      fprintf (stderr, "weird signal: too many or few bytes!  %d\n", status);
-      return (TRUE);
-      break;
+  events:
+    if (XEventsQueued (graphic[0].display, QueuedAfterFlush) < 1) {
+      usleep (10000);
+      continue;
     }
 
-    if ((xstatus = XCheckMaskEvent (graphic[0].display, EVENT_MASK, &event))) {
-      
-      switch (event.type)  {
+    display = graphic[0].display;
+
+    /* only do the last entry for these events */
+    /* this ignores the return status from these functions */
+    if (LastEvent (display, ConfigureNotify, &event)) Reconfig (graphic, layout, &event);
+    if (LastEvent (display, Expose,          &event)) Refresh (graphic, layout, 1);
+    if (LastEvent (display, MappingNotify,   &event)) XRefreshKeyboardMapping ((XMappingEvent *) &event);
+    if (LastEvent (display, MotionNotify,    &event)) UpdatePointer (graphic, layout, (XMotionEvent *) &event);
+    if (LastEvent (display, ButtonPress,     &event)) InterpretPresses (graphic, layout, (XButtonEvent *) &event);
+    if (LastEvent (display, KeyPress,        &event)) {
+      status = XLookupString ((XKeyEvent *) &event, string, 9, &keysym, &composestatus);
+
+      if (event.xkey.x < layout[0].picture.x) continue;
+      if (event.xkey.x > layout[0].picture.x + layout[0].picture.dx) continue;
+      if (event.xkey.y < layout[0].picture.y) continue;
+      if (event.xkey.y > layout[0].picture.y + layout[0].picture.dy) continue;
 	
-      case ButtonPress:
-	status = InterpretPresses (graphic, layout, (XButtonEvent *) &event);
-	break;
-	
-      case Expose:
-	if (event.xexpose.count == 0)
-	  Refresh (graphic, layout, 1);
-	break;
-	
-      case ConfigureNotify:
-	status =  Reconfig (graphic, layout, &event);
-	break;
-	
-      case MappingNotify:
-	XRefreshKeyboardMapping ((XMappingEvent *) &event);
-	break;
-	
-      case MotionNotify:
-	if (XPending (graphic[0].display) < 2) {
-	  UpdatePointer (graphic, layout, (XMotionEvent *) &event);
-	}
-	break;
+      Screen_to_Image (&x, &y, (double)event.xkey.x, (double)event.xkey.y, layout);
+      name = XKeysymToString (keysym);
 
-      case KeyPress:
-	value = XLookupString ((XKeyEvent *) &event, string, 9, &keysym, &composestatus);
+      // skip the following keys: 
+      if (!strcmp (name, "Shift_L")) continue;
+      if (!strcmp (name, "Shift_R")) continue;
+      if (!strcmp (name, "Control_L")) continue;
+      if (!strcmp (name, "Control_R")) continue;
+      if (!strcmp (name, "Alt_L")) continue;
+      if (!strcmp (name, "Alt_R")) continue;
+      if (!strcmp (name, "Super_L")) continue;
+      if (!strcmp (name, "Super_R")) continue;
+      if (!strcmp (name, "Caps_Lock")) continue;
+      if (!strcmp (name, "Pause")) continue;
+      if (!strcmp (name, "Continue")) continue;
+      if (!strcmp (name, "Num_Lock")) continue;
+      if (!strcmp (name, "Scroll_Lock")) continue;
+      if (!strcmp (name, "Print")) continue;
+      if (!strcmp (name, "(null)")) continue;
 
-	if ((event.xkey.x >= layout[0].picture.x) && (event.xkey.x <= layout[0].picture.x + layout[0].picture.dx) &&
-	    (event.xkey.y >= layout[0].picture.y) && (event.xkey.y <= layout[0].picture.y + layout[0].picture.dy)) {
+      snprintf (line, 40, "%12s %12.6f %12.6f ", name, x, y);
+      write (layout[0].Ximage, line, 40);
+    }
 
-	  Screen_to_Image (&x, &y, (double)event.xkey.x, (double)event.xkey.y, layout);
+    /* remove those events we will ignore */
+    while (XCheckMaskEvent (display, IgnoreMask, &event)) continue;
 
-	  if (isprint(string[0]))
-	    sprintf (line, "%c %6.1f %6.1f ", string[0], x, y);
-	  else {
-	    sprintf (line, "X %6.1f %6.1f ", x, y);
-	    break;
-	  }
-	  write (layout[0].Ximage, line, 16);
-	}
-      }
-    }
+    /* events to remove which have no mask component */
+    while (XCheckTypedEvent (display, MappingNotify, &event)) continue;
+    while (XCheckTypedEvent (display, ClientMessage, &event)) continue;
+    while (XCheckTypedEvent (display, SelectionClear, &event)) continue;
+    while (XCheckTypedEvent (display, SelectionNotify, &event)) continue;
+    while (XCheckTypedEvent (display, SelectionRequest, &event)) continue;
   }
+  return (FALSE);
 }
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/CheckVisual.c
===================================================================
--- branches/elixir/Ohana/src/kii/setup/CheckVisual.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/CheckVisual.c	(revision 21062)
@@ -1,5 +1,4 @@
 # include "Ximage.h"
 # define NPIXELS 64
-# define DEBUG 1
 
 /* DirectColor doesn't seem to work, even though it is available:
@@ -143,4 +142,5 @@
 
   if (!def) {
+    if (DEBUG) fprintf (stderr, "allocated private colormap\n");
     graphic[0].colormap = XCreateColormap (graphic[0].display, RootWindow (graphic[0].display, graphic[0].screen), graphic[0].visual, AllocNone);
   }
@@ -194,6 +194,3 @@
     }
   }
-  
-
 }
-
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/CloseDisplay.c
===================================================================
--- branches/elixir/Ohana/src/kii/setup/CloseDisplay.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/CloseDisplay.c	(revision 21062)
@@ -2,8 +2,8 @@
 
 /************** CloseDisplay *************/
-void
-CloseDisplay (graphic)
-Graphic *graphic;
-{
+void CloseDisplay (Graphic *graphic) {
+
+  XFlush (graphic[0].display);
+  XFreeFont (graphic[0].display, graphic[0].font); 
   XFreeGC (graphic[0].display, graphic[0].gc);
   XDestroySubwindows (graphic[0].display, graphic[0].window);
@@ -11,4 +11,5 @@
   XFlush (graphic[0].display);
   XCloseDisplay (graphic[0].display);
+  return;
 }
 
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/DefineLayout.c
===================================================================
--- branches/elixir/Ohana/src/kii/setup/DefineLayout.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/DefineLayout.c	(revision 21062)
@@ -1,33 +1,18 @@
 # include "Ximage.h"
+
+static char default_colormap[] = "grayscale";
 
 void DefineLayout (Layout *layout, Graphic *graphic, int argc, char **argv) {
 
-  int i, status, N;
-  struct sockaddr_un Address;
-  char temp[100];
-
-  DEBUG = FALSE;
-  if ((N = get_argument (argc, argv, "-debug"))) {
-    remove_argument(N, &argc, argv);
-    DEBUG = TRUE;
-  }
+  int i, N;
+  char *colormap;
 
   /** initiate connection with mana **/
-  if (!DEBUG) {	
+  if (!FOREGROUND) {
     if (argc < 2) {
       fprintf (stderr, "socket path not specified\n");
       exit (0);
     }
-    strcpy (Address.sun_path, argv[1]);
-    Address.sun_family = AF_UNIX;
-    layout[0].Ximage = socket (AF_UNIX, SOCK_STREAM, 0);
-    status = connect (layout[0].Ximage, (struct sockaddr *) &Address, (socklen_t) sizeof (Address));
-    if (status < 0) {
-      fprintf (stderr, "kii - unsuccessful connection: %d\n", status);
-      exit (0);
-    }
-    fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
-    sprintf (temp, "rm -f %s", argv[1]);
-    system (temp);
+    layout[0].Ximage = KiiWait (argv[1]);
   }
 
@@ -35,5 +20,15 @@
   
   /* get the display colors */
-  MakeColormap (graphic, layout, argc, argv);
+  if (USE_XWINDOW) {
+    MakeColormap (graphic, layout, argc, argv);
+  } else {
+    colormap = default_colormap;
+    if ((N = get_argument (argc, argv, "-cm"))) {
+      remove_argument (N, &argc, argv);
+      colormap = argv[N];
+    }
+    layout[0].Npixels = 256;
+    SetColormap (graphic, layout, colormap);
+  }
 
   /** set up a bunch of default things **/
@@ -57,9 +52,10 @@
   ALLOCATE (layout[0].zoom.data, char, 1);      /* allocate so later free will not crash! */
 
-  CreatePicture (layout, graphic);
-  CreateColorbar (layout, graphic);
-  CreateZoom (layout, graphic, 0, 0); 
+  if (USE_XWINDOW) {
+    CreatePicture (layout, graphic);
+    CreateColorbar (layout, graphic);
+    CreateZoom (layout, graphic, 0, 0); 
+  }
 }
-
 
 /* this routine is independent of the number of overlays */
@@ -67,3 +63,2 @@
   /* cmap = the mapping of value to color in kii
      colormap = the set of pixel -> color representation in X */
-
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/DrawBitmap.c
===================================================================
--- branches/elixir/Ohana/src/kii/setup/DrawBitmap.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/DrawBitmap.c	(revision 21062)
@@ -11,4 +11,7 @@
   int i, j, byte_line, byte, bit, flag;
   unsigned long int black, white;
+
+  black = graphic[0].black;
+  white = graphic[0].white;
 
   if (mode == 0) {
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/NameWindow.c
===================================================================
--- branches/elixir/Ohana/src/kii/setup/NameWindow.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/NameWindow.c	(revision 21062)
@@ -2,22 +2,11 @@
 
 /************** NameWindow *************/
-void
-NameWindow (graphic, Name)
-Graphic     graphic[];
-char       *Name;
-{
+void NameWindow (Graphic *graphic, char *name) {
 
-  char       *name;
   char       *class_name;
   char       *class_type;
   XClassHint *classhints;
 
-  name = strrchr (Name, '/');
-  if (name != NULL) 
-    name ++;
-  else 
-    name = Name;
   class_type = class_name = name;
-
 
   classhints = XAllocClassHint ();
@@ -33,3 +22,2 @@
   XSetIconName (graphic[0].display, graphic[0].window, name);
 }
-
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/SetUpWindow.c
===================================================================
--- branches/elixir/Ohana/src/kii/setup/SetUpWindow.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/SetUpWindow.c	(revision 21062)
@@ -5,6 +5,6 @@
 void SetUpWindow (Graphic *graphic, int *argc, char **argv) {
 
-  int N;
   Icon icon;
+  char *name;
 
   icon.width = icon_width;
@@ -21,13 +21,14 @@
   SetNormalHints (graphic);
   SetWMHints (graphic, &icon);
-  NameWindow (graphic, "Ki'i");
 
-  if ((N = get_argument (*argc, argv, "-nomap"))) {
-    remove_argument(N, argc, argv);
+  if (NAME_WINDOW == NULL) {
+    NameWindow (graphic, "Ki'i");
   } else {
-    MapWindow (graphic);
-  }  
+    ALLOCATE (name, char, strlen(NAME_WINDOW) + 10);
+    sprintf (name, "Ki'i %s", NAME_WINDOW);
+    NameWindow (graphic, name);
+    free (name);
+  }
+
+  if (MAP_WINDOW) MapWindow (graphic);
 }
-
-
-
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/Ximage.c
===================================================================
--- branches/elixir/Ohana/src/kii/setup/Ximage.c	(revision 2465)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/Ximage.c	(revision 21062)
@@ -6,16 +6,23 @@
   Layout  layout;
   
+  args (&argc, argv);
+
   bzero (&graphic, sizeof(Graphic));
   bzero (&layout, sizeof(Layout));
 
-  SetUpDisplay (&graphic, &argc, argv);
-  SetUpWindow (&graphic, &argc, argv);
+  if (USE_XWINDOW) SetUpDisplay (&graphic, &argc, argv);
+  if (USE_XWINDOW) {
+    SetUpWindow (&graphic, &argc, argv);
+  } else {
+    graphic.x = 10;
+    graphic.y = 10;
+    graphic.dx = 512 + 2*PAD1 + PAD1 + ZOOM_X;
+    graphic.dy = 512 + 2*PAD1 + PAD2 + COLORPAD;
+  }
 
   DefineLayout (&layout, &graphic, argc, argv);
   EventLoop (&graphic, &layout);
 
-  XFlush (graphic.display);
-  XFreeFont (graphic.display, graphic.font); 
-  CloseDisplay (&graphic);
+  if (USE_XWINDOW) CloseDisplay (&graphic);
   exit (0);
 }
Index: branches/ipp-magic-v0/Ohana/src/kii/setup/args.c
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/setup/args.c	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/setup/args.c	(revision 21062)
@@ -0,0 +1,39 @@
+# include "Ximage.h"
+
+int args (int *argc, char **argv) {
+
+  int N;
+
+  MAP_WINDOW = TRUE;
+  if ((N = get_argument (*argc, argv, "-nomap"))) {
+    remove_argument(N, argc, argv);
+    MAP_WINDOW = FALSE;
+  }
+
+  NAME_WINDOW = NULL;
+  if ((N = get_argument (*argc, argv, "-name"))) {
+    remove_argument(N, argc, argv);
+    NAME_WINDOW = strcreate (argv[N]);
+    remove_argument(N, argc, argv);
+  }
+
+  USE_XWINDOW = TRUE;
+  if ((N = get_argument (*argc, argv, "-noX"))) {
+    remove_argument(N, argc, argv);
+    USE_XWINDOW = FALSE;
+  }
+
+  DEBUG = FALSE;
+  if ((N = get_argument (*argc, argv, "-debug"))) {
+    remove_argument(N, argc, argv);
+    DEBUG = TRUE;
+  }
+
+  FOREGROUND = FALSE;
+  if ((N = get_argument (*argc, argv, "-fg"))) {
+    remove_argument(N, argc, argv);
+    FOREGROUND = TRUE;
+  }
+
+  return (TRUE);
+}
Index: branches/ipp-magic-v0/Ohana/src/kii/test/jpeg.sh
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/test/jpeg.sh	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/test/jpeg.sh	(revision 21062)
@@ -0,0 +1,17 @@
+
+macro kii-test
+  mcreate z 512 512
+  set a = xramp (z)
+  tv a 0 512
+
+  load red sample.reg
+
+  create x 64 512 64
+  vload red x x -type circle -size 5
+  vload blue x x -type box -size 3
+  vload yellow x x -type line -size 10
+
+  jpeg -name test.png
+  ps -name test.ps
+end
+
Index: branches/ipp-magic-v0/Ohana/src/kii/test/regions.sh
===================================================================
--- branches/ipp-magic-v0/Ohana/src/kii/test/regions.sh	(revision 21062)
+++ branches/ipp-magic-v0/Ohana/src/kii/test/regions.sh	(revision 21062)
@@ -0,0 +1,32 @@
+
+$KII = kii -noX
+
+macro go
+ mcreate a 512 512
+ set b = yramp (a)
+ tv b 0 512
+ create x 0 512 64
+ set y = 1.1*x
+ vload red x x
+ vload red x y
+ set y = x + 10
+ vload blue -type line x y
+ set y = x + 20
+ vload green -type circle x y -size 3.0
+ set y = x + 30
+ vload yellow -type box x y -size 5.0
+
+ for i 1 6
+  vload red -type box x y -size $i
+  vload blue -type circle x y -size $i
+ end
+
+ jpeg -name test.jpg
+
+ resize 512 512
+ center 256 256 1
+ jpeg -name test2.jpg
+
+ center 256 256 3
+ jpeg -name test3.jpg
+end
