Index: trunk/Ohana/src/opihi/cmd.data/center.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/center.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/center.c	(revision 5846)
@@ -23,10 +23,7 @@
   y = atof (argv[2]);
   zoom = 0;
-  if (argc == 4) {
-    zoom = atof (argv[3]);
-  }
+  if (argc == 4) zoom = atof (argv[3]);
 
-  SendGraphCommand (Ximage, 4, "CENT");
-  SendGraphMessage (Ximage, "%8.3f %8.3f %8d ", x, y, zoom);
+  KiiCenter (Ximage, x, y, zoom);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/cursor.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/cursor.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/cursor.c	(revision 5846)
@@ -3,5 +3,5 @@
 int cursor (int argc, char **argv) {
 
-  char buffer[40], string[20], key[20], keyname[20];
+  char string[20], key[20];
   int i, N, Nsource, Source, IsImage;
   double X, Y, R, D, Z;
@@ -42,6 +42,5 @@
   if (IsImage) buf = SelectBuffer (GetImageName(), OLDBUFFER, FALSE);
   
-  sprintf (string, "CURS");
-  write (Source, string, 4); /* ask Source to look for the keystrokes */
+  KiiCursorOn (Source);
   
   oldsignal = signal (SIGINT, handle_interrupt);
@@ -49,48 +48,30 @@
   Z = -1.0;
   for (i = 0; ((i < N) || (N == 0)) && !interrupt; i++) {
-    bzero (buffer, 40);
-    read (Source, buffer, 40);
-    buffer[39] = 0;
-    sscanf (buffer, "%s %lf %lf", key, &X, &Y);
-    if (ispunct(key[0])) {
-      strcpy (keyname, "_");
-    } else {
-      strcpy (keyname, key);
-    }
-    sprintf (string, "X%s", keyname);
+    KiiCursorRead (Source, &X, &Y, key);
+
+    sprintf (string, "X%s", key);
     set_variable (string, X);
-    sprintf (string, "Y%s", keyname);
+    sprintf (string, "Y%s", key);
     set_variable (string, Y);
     set_str_variable ("KEY", key);
     
-    if (IsImage && (buf != NULL)) 
-      Z = fits_get_matrix_value (&buf[0].matrix, (int)X, (int)Y);
-
-    if (!IsImage) {
-      XY_to_RD (&R, &D, X, Y, &graphmode.coords);
-      sprintf (string, "R%s", keyname);
-      set_variable (string, R);
-      sprintf (string, "D%s", keyname);
-      set_variable (string, D);
-    }
-
-    /* I still don't like this, but I have to decide which to print... */
     if (IsImage) {
+      if (buf != NULL) { 
+	Z = fits_get_matrix_value (&buf[0].matrix, (int) X, (int) Y);
+      }
       fprintf (GetOutfile(), "%s %f %f  %f\n", key, X, Y, Z);
     } else {
+      XY_to_RD (&R, &D, X, Y, &graphmode.coords);
+      sprintf (string, "R%s", key);
+      set_variable (string, R);
+      sprintf (string, "D%s", key);
+      set_variable (string, D);
       fprintf (GetOutfile(), "%s %f %f\n", key, X, Y);
     }
-
-    if (!strcasecmp (key, "Q")) {
-      signal (SIGINT, (void *) oldsignal);
-      write (Source, "NCUR", 4); /* ask Source to stop looking for the keystrokes */
-      return (TRUE);
-    }
-
+    if (!strcasecmp (key, "Q")) break;
   }
 
   signal (SIGINT, oldsignal);
-  write (Source, "NCUR", 4); /* ask Source to look for the keystrokes */
+  KiiCursorOff (Source);
   return (TRUE);
-      
 }
Index: trunk/Ohana/src/opihi/cmd.data/erase.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/erase.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/erase.c	(revision 5846)
@@ -3,5 +3,5 @@
 int erase (int argc, char **argv) {
   
-  int i, n, N;
+  int i, N;
   int Ximage, Nimage;
   
@@ -21,27 +21,16 @@
 
   for (i = 1; i < argc; i++) {
-    if (strcmp (argv[i], "0")   && strcmp (argv[i], "1")     && strcmp (argv[i], "2")   && strcmp (argv[i], "3") &&
-	strcmp (argv[i], "red") && strcmp (argv[i], "green") && strcmp (argv[i], "blue") && strcmp (argv[i], "yellow") &&
-	strcmp (argv[i], "all") && strcmp (argv[i], "tick") ) {
+    if (!(strcasecmp (argv[i], "all"))) {
+      KiiEraseOverlay (Ximage, "red");
+      KiiEraseOverlay (Ximage, "green");
+      KiiEraseOverlay (Ximage, "blue");
+      KiiEraseOverlay (Ximage, "yellow");
+      continue;
+    }
+    if (!KiiSelectOverlay (argv[i], &N)) {
       fprintf (stderr, "%s is not a valid overlay\n", argv[i]);
       return (FALSE);
     }
-    n = atof (argv[i]);
-    if (!(strcmp (argv[i], "all"))) {
-      for (n = 0; n < 4; n++) {
-	SendGraphCommand (Ximage, 4, "ERAS");
-	SendGraphCommand (Ximage, 16, "OVERLAY %7d ", n);
-      }
-      return (TRUE);
-    }
-    
-    if (!(strcmp (argv[i], "red"))) n = 0;
-    if (!(strcmp (argv[i], "green"))) n = 1;
-    if (!(strcmp (argv[i], "blue"))) n = 2;
-    if (!(strcmp (argv[i], "yellow"))) n = 3;
-    if (!(strcmp (argv[i], "tick"))) n = 4;
-    
-    SendGraphCommand (Ximage, 4, "ERAS");
-    SendGraphCommand (Ximage, 16, "OVERLAY %7d ", n);
+    KiiEraseOverlay (Ximage, argv[i]);
   }
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/jpeg.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/jpeg.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/jpeg.c	(revision 5846)
@@ -46,14 +46,13 @@
     if (!filename[0]) strcpy (filename, "Ximage.jpg");
     strcpy (type, "JPEG");
+    KiiJpeg (Source, filename);
   } else {
     if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
     if (!filename[0]) strcpy (filename, "Xgraph.png");
+    SendGraphCommand (Source, 4, type);
+    SendGraphCommand (Source, 16, "LEN: %11d", strlen(filename));
+    write (Source, filename, strlen(filename));
+    read (Source, buffer, 4);
   }
-
-  SendGraphCommand (Source, 4, type);
-  SendGraphCommand (Source, 16, "LEN: %11d", strlen(filename));
-  write (Source, filename, strlen(filename));
-  read (Source, buffer, 4);
-
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/load.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/load.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/load.c	(revision 5846)
@@ -3,13 +3,14 @@
 int load (int argc, char **argv) {
   
-  int N, n, Nline, ISCEL;
-  int Ximage, Nimage;
-  char *c, type[10], string[128];
+  int i, N, n, ISCEL;
+  int Ximage, Nimage, Noverlay, NOVERLAY;
+  char *c, type[10], string[128], line[1024];
   double x, y, dx, dy, x1, y1;
   double dra, ddec, ra1, dec1, ra, dec;
   FILE *f;
-  char *buffer, *line;
+  char *buffer;
   Coords coords;
   Buffer *buf;
+  KiiOverlay *overlay;
   
   Nimage = -1;
@@ -33,6 +34,4 @@
   }
   
-  if (!SelectOverlay (argv[1], &n)) return (FALSE);
-
   if (!strcmp (argv[2], "-")) {
     f = stdin;
@@ -50,12 +49,11 @@
   }
 
+  Noverlay = 0;
+  NOVERLAY = 1000;
+  ALLOCATE (overlay, KiiOverlay, NOVERLAY);
+
   ALLOCATE (buffer, char, 65536);  /* space for 512 lines of 128 bytes */
   bzero (buffer, 65536);
-  ALLOCATE (line, char, 1024);     /* space to read the file */
-  Nline = 0;
 
-  SendGraphCommand (Ximage, 4, "LOAD");
-  SendGraphCommand (Ximage, 16, "OVER %9d ", n);
-  
   dx = dy = 0;
   for (n = 0; scan_line (f, line) != EOF;) {
@@ -81,5 +79,5 @@
     case 3:
       if (strcmp (type, "TEXT")) {
-	fprintf (stderr, "syntactical error in line:\n   %s\n", line);
+	fprintf (stderr, "syntax error in line:\n   %s\n", line);
 	continue;
       }
@@ -113,26 +111,28 @@
       }
     }
-    if (!strcmp (type, "TEXT")) {
-      snprintf (&buffer[Nline*128], 128, "%15s %20.10f %20.10f %s ", type, x, y, string);
+    overlay[Noverlay].type = KiiOverlayTypeByName (type);
+    if (overlay[Noverlay].type == KII_OVERLAY_TEXT) {
+      overlay[Noverlay].text = strcreate (string);
     } else {
-      snprintf (&buffer[Nline*128], 128, "%15s %20.10f %20.10f %20.10f %20.10f ", type, x, y, dx, dy);
+      overlay[Noverlay].text = NULL;
     }
-    n++;
-    Nline ++;
-    if (Nline == 512) {
-      SendGraphCommand (Ximage, 16, "NLINES  %7d ", Nline);
-      write (Ximage, buffer, Nline*128);
-      bzero (buffer, 65536);
-      Nline = 0;
-    }
+    overlay[Noverlay].x = x;
+    overlay[Noverlay].y = y;
+    overlay[Noverlay].dx = dx;
+    overlay[Noverlay].dy = dy;
+    Noverlay++;
+    CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 1000);
   }
 
-  SendGraphCommand (Ximage, 16, "NLINES  %7d ", Nline);
-  write (Ximage, buffer, Nline*128);
-  SendGraphCommand (Ximage, 16, "DONE");
+  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
 
-  free (buffer);
-  free (line);
-  fprintf (stderr, "loaded %d objects\n", n);
+  for (i = 0; i < Noverlay; i++) {
+    if (overlay[i].text == NULL) continue;
+    free (overlay[i].text);
+  }
+  free (overlay);
+
+  fprintf (stderr, "loaded %d objects\n", Noverlay);
+
   if (f != stdin) {
     fclose (f);
Index: trunk/Ohana/src/opihi/cmd.data/point.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/point.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/point.c	(revision 5846)
@@ -3,10 +3,11 @@
 int point (int argc, char **argv) {
   
-  int N, n, ISCEL, ISPIX;
+  int N, celestial, pixscale;
   int Ximage, Nimage;
-  double x, y, dx, dy, x1, y1;
-  double dra, ddec, ra1, dec1, ra, dec;
+  double ra, dec, dra, ddec;
+  double x1, y1, ra1, dec1;
   Coords coords;
   Buffer *buf;
+  KiiOverlay overlay;
 
   Nimage = -1;
@@ -18,14 +19,14 @@
   if (!GetImage (&Ximage, &Nimage)) return (FALSE);
   
-  ISCEL = FALSE;
+  celestial = FALSE;
   if ((N = get_argument (argc, argv, "-c"))) {
     remove_argument (N, &argc, argv);
-    ISCEL = TRUE;
+    celestial = TRUE;
   }
   
-  ISPIX = FALSE;
-  if ((N = get_argument (argc, argv, "-p"))) {
+  pixscale = FALSE;
+  if ((N = get_argument (argc, argv, "-pixscale"))) {
     remove_argument (N, &argc, argv);
-    ISPIX = TRUE;
+    pixscale = TRUE;
   }
 
@@ -35,42 +36,35 @@
   }
   
-  if (!SelectOverlay (argv[1], &n)) 
-    return (FALSE);
-
-  if (ISCEL) {
+  if (celestial) {
     if ((buf = SelectBuffer (GetImageName(), OLDBUFFER, TRUE)) == NULL) return (FALSE);
     GetCoords (&coords, &buf[0].header);
   }
 
-  ra = atof(argv[3]);
-  dec = atof(argv[4]);
-  dra = atof(argv[5]);
-  ddec = atof(argv[6]);
-  if (ISCEL) {
-    RD_to_XY (&x, &y, ra, dec, &coords);
-    if (!ISPIX) {
+  if (celestial) {
+    ra   = atof(argv[3]);
+    dec  = atof(argv[4]);
+    dra  = atof(argv[5]);
+    ddec = atof(argv[6]);
+
+    fRD_to_XY (&overlay.x, &overlay.y, ra, dec, &coords);
+    if (pixscale) {
+      overlay.dx = atof(argv[5]);
+      overlay.dy = atof(argv[6]);
+    } else {
       ra1 = ra + dra;
       dec1 = dec + ddec;
       RD_to_XY (&x1, &y1, ra1, dec1, &coords);
-      dx = x1 - x;
-      dy = y1 - y;
-    } else {
-      dx = atof(argv[5]);
-      dy = atof(argv[6]);
+      overlay.dx = x1 - overlay.x;
+      overlay.dy = y1 - overlay.y;
     }
   }
   else {
-    x = ra;
-    y = dec;
-    dx = dra;
-    dy = ddec;
+    overlay.x  = atof(argv[3]);
+    overlay.y  = atof(argv[4]);
+    overlay.dx = atof(argv[5]);
+    overlay.dy = atof(argv[6]);
   }
-
-  SendGraphCommand (Ximage, 4, "LOAD");
-  SendGraphCommand (Ximage, 16, "OVERLAY %6d ", n);
-  SendGraphCommand (Ximage, 16, "NLINES %7d ", 1);
-  SendGraphCommand (Ximage, 128, "%15s %20.10f %20.10f %20.10f %20.10f ", argv[2], x, y, dx, dy);
-  SendGraphCommand (Ximage, 16, "DONE");
+  overlay.type = KiiOverlayTypeByName (argv[2]);
+  KiiLoadOverlay (Ximage, &overlay, 1, argv[1]);
   return (TRUE);
 }
-
Index: trunk/Ohana/src/opihi/cmd.data/ps.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/ps.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/ps.c	(revision 5846)
@@ -3,5 +3,5 @@
 int ps (int argc, char **argv) {
 
-  char filename[1024], buffer[64];
+  char filename[1024];
   int N, Source, Nsource, RawImage, NoScale, IsImage;
   
@@ -56,13 +56,5 @@
   
   /* tell Ximage/Xgraph to ps the image */
-  if (RawImage) SendGraphCommand (Source, 4, "PSRW"); 
-  if (NoScale)  SendGraphCommand (Source, 4, "PSNS");
-  if (!RawImage && !NoScale) SendGraphCommand (Source, 4, "PSIT");
-
-  /* I need to fix kapa and kii to read NBYTES, etc like other messages */
-  SendGraphCommand (Source, 16, "LEN: %11d", strlen(filename));
-  write (Source, filename, strlen(filename));
-  read (Source, buffer, 4);
-
+  KiiPS (Source, NoScale, RawImage, filename);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/resize.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/resize.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/resize.c	(revision 5846)
@@ -3,5 +3,5 @@
 int resize (int argc, char **argv) {
 
-  char buffer[128], buffer2[128], *end;
+  char *end;
   double NX, NY;
   int N, Source, Nsource, IsImage;
@@ -49,13 +49,6 @@
   if (!strcmp (end, "cm")) { NY *= 38; }
 
-  write (Source, "RSIZ", 4); /* tell Source to resize the image */
-
-  sprintf (buffer, "%d %d", (int) NX, (int) NY);
-  sprintf (buffer2, "NBYTES: %6d ", strlen (buffer));
-  write (Source, buffer2, 16);
-  write (Source, buffer, strlen (buffer));
-
+  KiiResize (Source, NX, NY);
   return (TRUE);
-
 }
 
Index: trunk/Ohana/src/opihi/cmd.data/save.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/save.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/save.c	(revision 5846)
@@ -3,5 +3,5 @@
 int save (int argc, char **argv) {
   
-  int N, n, ISCEL;
+  int N, celestial;
   int Ximage, Nimage;
   
@@ -14,8 +14,8 @@
   if (!GetImage (&Ximage, &Nimage)) return (FALSE);
 
-  ISCEL = FALSE;
+  celestial = FALSE;
   if ((N = get_argument (argc, argv, "-c"))) {
     remove_argument (N, &argc, argv);
-    ISCEL = TRUE;
+    celestial = TRUE;
   }
 
@@ -26,12 +26,5 @@
   }
 
-  if (!SelectOverlay (argv[1], &n)) return (FALSE);
-  
-  if (ISCEL) 
-    SendGraphCommand (Ximage, 4, "CSVE");
-  else 
-    SendGraphCommand (Ximage, 4, "SAVE");
-
-  SendGraphMessage (Ximage, "FILE: %d %s", n, argv[2]);
+  KiiSaveOverlay (Ximage, celestial, argv[1], argv[2]);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/tv.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/tv.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/tv.c	(revision 5846)
@@ -3,12 +3,9 @@
 int tv (int argc, char **argv) {
   
-  int N, j, Npix, Ncolors, size, ISLOG, NNcol;
-  char buffer[1024];
-  double a1, a2, max, min, zero, range;
-  float *in;
-  char *out, *outbuffer, *root;
-  int Ximage, Nimage;
+  int N, Ximage, Nimage;
   Coords coords;
   Buffer *buf;
+  KiiImage image;
+  KiiDisplayMode mode;
 
   Nimage = -1;
@@ -22,14 +19,13 @@
   /* shell exits on pipe close, FIX */
   if ((N = get_argument (argc, argv, "-kill"))) {
-    close (Ximage);
+    KiiClose (Ximage);
     Ximage = 0;
     return (TRUE);
   }
 
-  min = max = 0.0;
-  ISLOG = FALSE;
+  mode.logflux = FALSE;
   if ((N = get_argument (argc, argv, "-log"))) {
     remove_argument (N, &argc, argv);
-    ISLOG = TRUE;
+    mode.logflux = TRUE;
   }
 
@@ -39,55 +35,22 @@
   }
 
-  GetImageScale (&zero, &range);
+  GetImageScale (&mode.zero, &mode.range);
   if (argc == 4) {
-    zero = atof (argv[2]);
-    range = atof (argv[3]);
-    if (range == 0.0) range = 0.001;
-    SetImageScale (zero, range);
+    mode.zero = atof (argv[2]);
+    mode.range = atof (argv[3]);
+    if (mode.range == 0.0) mode.range = 0.001;
+    SetImageScale (mode.zero, mode.range);
   }
 
   if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
   GetCoords (&coords, &buf[0].header);
+  
+  image.data1d = (float *) buf[0].matrix.buffer;
+  image.Nx = buf[0].matrix.Naxis[0];
+  image.Ny = buf[0].matrix.Naxis[1];
+  image.file = buf[0].file;
+  image.name = buf[0].name;
 
-  Npix = buf[0].matrix.Naxis[0]*buf[0].matrix.Naxis[1];
-  size = Npix * sizeof (char);
-  in = (float *) (buf[0].matrix.buffer);
-  ALLOCATE (outbuffer, char, Npix);
-  out = outbuffer;
-
-  SendGraphCommand (Ximage, 4, "READ"); /* tell Ximage to look for the incoming image */
-
-  /* convert from (float) internal rep to 1 byte chars -- depends on how many colors Kii owns*/
-  read (Ximage, buffer, 16); 
-  sscanf (buffer, "%*s %d", &Ncolors); 
-
-  /* need to invert the logic if range < 0 */
-  /* define color table, */
-  NNcol = Ncolors - 1;
-  if (ISLOG) {
-    range = MAX (2, range);
-    a1 = Ncolors / log10 (range);
-    for (j = 0; j < Npix; j++, in++, out++) {
-      *out = (char) MIN (a1 * log10 (MAX (*in - zero, 1.0)), NNcol);
-    }
-  }
-  if (!ISLOG) {
-    a1 = Ncolors / range;
-    a2 = Ncolors * zero / range;
-    for (j = 0; j < Npix; j++, in++, out++) {
-      *out = (char) MIN (MAX (a1 * *in - a2, 0), NNcol);
-    }
-  }
-  
-  /* done with the conversion, now send Ximage the converted picture */
-  SendGraphMessage (Ximage, "%8d %8d 8 1 %f %f", buf[0].matrix.Naxis[0], buf[0].matrix.Naxis[1], 0.0, 1.0);
-  SendGraphMessage (Ximage, "%f %f %f %f %d ", zero, range, min, max, size);
-  SendGraphMessage (Ximage, "%f %f %g %g %g ", coords.crval1, coords.crpix1, coords.cdelt1, coords.pc1_1, coords.pc1_2);
-  SendGraphMessage (Ximage, "%f %f %g %g %g ", coords.crval2, coords.crpix2, coords.cdelt2, coords.pc2_1, coords.pc2_2);
-
-  root = filerootname (buf[0].file);
-  SendGraphMessage (Ximage, "%s %s %s ", coords.ctype, root, buf[0].name);
-  write (Ximage, outbuffer, size);
-  free (outbuffer);
+  KiiNewPicture1D (Ximage, &image, &mode, &coords);
 
   SetImageName (argv[1]);
Index: trunk/Ohana/src/opihi/cmd.data/tvcontour.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/tvcontour.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/tvcontour.c	(revision 5846)
@@ -42,34 +42,23 @@
 
 # define DUMP { \
-sprintf (&buffer[Nline*128], "%14s %20.10f %20.10f %20.10f %20.10f ", "LINE", Npix*x, Npix*y, Npix*dx, Npix*dy); \
-Nline ++; \
-if (Nline == 512) { \
-  SendGraphCommand (Ximage, 16, "NLINES  %7d ", Nline); \
-  write (Ximage, buffer, Nline*128); \
-  bzero (buffer, 65536); \
-  Nline = 0; \
-} \
-continue; }
-
-# define NUMP { \
-sprintf (&buffer[Nline*128], "%14s %20.10f %20.10f %20.10f %20.10f ", "LINE", Npix*x, Npix*y, Npix*dx, Npix*dy); \
-Nline ++; \
-if (Nline == 512) { \
-  SendGraphCommand (Ximage, 16, "NLINES  %7d ", Nline); \
-  write (Ximage, buffer, Nline*128); \
-  bzero (buffer, 65536); \
-  Nline = 0; \
-} }
+overlay[Noverlay].type = KII_OVERLAY_LINE; \
+overlay[Noverlay].x = Npix*x; \
+overlay[Noverlay].y = Npix*y; \
+overlay[Noverlay].dx = Npix*dx; \
+overlay[Noverlay].dy = Npix*dy; \
+Noverlay ++; \
+CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 1000); \
+}
 
 int tvcontour (int argc, char **argv) {
 
-  int i, j, ii, jj, n, Npix, Nx, Ny, Nline;
+  int i, j, ii, jj, Npix, Nx, Ny;
   float level, d00, d01, d10, d11, tmp;
   float x, y, dx, dy;
   float *v00, *v01, *v10, *v11;
   float *Vout, *Vin, *matrix;
-  char *buffer;
-  int Ximage, Nimage, N;
+  int Ximage, Nimage, N, Noverlay, NOVERLAY;
   Buffer *buf;
+  KiiOverlay *overlay;
   
   Nimage = -1;
@@ -87,5 +76,4 @@
 
   if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
-  if (!SelectOverlay (argv[2], &n)) return (FALSE);
 
   level = atof (argv[3]);
@@ -115,15 +103,12 @@
       }
     }
-  }
-  else {
+  } else {
     fprintf (stderr, "using scale of 1\n");
     matrix = (float *)(buf[0].matrix.buffer);
   }
 
-  SendGraphCommand (Ximage, 4, "LOAD"); /* force Ximage to look for the incoming image */
-  SendGraphCommand (Ximage, 16, "OVERLAY %7d ", n);
-  ALLOCATE (buffer, char, 65536);  /* space for 512 lines of 128 bytes */
-  bzero (buffer, 65536);
-  Nline = 0;
+  Noverlay = 0;
+  NOVERLAY = 1000;
+  ALLOCATE (overlay, KiiOverlay, NOVERLAY);
 
   v00 = matrix;
@@ -150,18 +135,22 @@
 	  LL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 <= 0)) { /* -  */
 	  HZ;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 > 0)) { /* /  */
 	  UL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 > 0)) { /* \\  */
 	  LL;
-	  NUMP;
+	  DUMP;
 	  UR;
 	  DUMP;
+	  continue;
 	}
       }
@@ -171,18 +160,22 @@
 	  LL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 > 0)) { /* -  */
 	  HZ;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 <= 0)) { /* /  */
 	  UL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 <= 0)) { /* //  */
 	  UL;
-	  NUMP;
+	  DUMP;
 	  LR;
 	  DUMP;
+	  continue;
 	}
       }
@@ -193,12 +186,15 @@
 	  LR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 > 0)) {
 	  UR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 > 0)) {
 	  VT;
 	  DUMP;
+	  continue;
 	}
       }
@@ -208,12 +204,15 @@
 	  LR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 <= 0)) {
 	  UR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 <= 0)) {
 	  VT;
 	  DUMP;
+	  continue;
 	}
       }
@@ -235,5 +234,5 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       x = i + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
     }
   }
@@ -248,5 +247,5 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       x = i + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
     }
   }
@@ -261,5 +260,5 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       y = j + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
     }
   }
@@ -274,15 +273,10 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       y = j + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
     }
   }
   
-  if (Nline > 0) {
-    SendGraphCommand (Ximage, 16, "NLINES  %7d ", Nline); 
-    write (Ximage, buffer, Nline*128); 
-  }
-
-  free (buffer);
-  SendGraphCommand (Ximage, 16, "DONE"); 
+  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[2]);
+  free (overlay);
 
   if (Npix != 1) free (matrix);
Index: trunk/Ohana/src/opihi/cmd.data/tvgrid.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/tvgrid.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/tvgrid.c	(revision 5846)
@@ -4,5 +4,5 @@
 int tvgrid (int argc, char **argv) {
   
-  int ncolor, ndig1, ndig2, NX, NY, connect;
+  int ndig1, ndig2, NX, NY, connect;
   int tDEC, tRA;
   double ra, dec, ra0, dec0, ra1, dec1;
@@ -11,6 +11,7 @@
   char format[16];
   Coords coords;
-  int Ximage, Nimage, N;
+  int Ximage, Nimage, N, Noverlay, NOVERLAY;
   Buffer *buf;
+  KiiOverlay *overlay;
 
   Nimage = -1;
@@ -23,23 +24,8 @@
   
   if (argc != 3) {
-    fprintf (stderr, "USAGE: load (overlay) (buffer)\n");
+    fprintf (stderr, "USAGE: tvgrid (overlay) (buffer)\n");
     fprintf (stderr, " (overlay) may be: red, green, blue, yellow\n");
     return (FALSE);
   }
-  if (strcmp (argv[1], "0")   && strcmp (argv[1], "1")     && strcmp (argv[1], "2")   && strcmp (argv[1], "3") &&
-      strcmp (argv[1], "red") && strcmp (argv[1], "green") && strcmp (argv[1], "blue") && strcmp (argv[1], "yellow")) {
-    fprintf (stderr, "valid overlays may be: 0, red, 1, green, 2, blue, 3, yellow\n");
-    return (FALSE);
-  }
-
-  ncolor = atof (argv[1]);
-  if (!(strcmp (argv[1], "red"))) 
-    ncolor = 0;
-  if (!(strcmp (argv[1], "green"))) 
-    ncolor = 1;
-  if (!(strcmp (argv[1], "blue"))) 
-    ncolor = 2;
-  if (!(strcmp (argv[1], "yellow"))) 
-    ncolor = 3;
 
   if ((buf = SelectBuffer (argv[2], OLDBUFFER, TRUE)) == NULL) return (FALSE);
@@ -50,8 +36,8 @@
   fprintf (stderr, "%f %f  %f %f\n", ra0, dec0, ra1, dec1);
   
-  /* tell KII to expect overlay data */
-  SendGraphCommand (Ximage, 4, "LOAD"); 
-  SendGraphCommand (Ximage, 16, "OVERLAY: %7d", ncolor);
-  
+  Noverlay = 0;
+  NOVERLAY = 1000;
+  ALLOCATE (overlay, KiiOverlay, NOVERLAY);
+
   dDEC = fabs(dec1 - dec0);
   tDEC = log10(dDEC) - log10(5.0);
@@ -90,5 +76,13 @@
       RD_to_XY (&x1, &y1, ra, dec, &coords);
       if ((x1 >= 0) && (x1 < NX) && (y1 >= 0) && (y1 < NY)) {
-	if (connect) SendGraphCommand (Ximage, 128, "%15s %20.10f %20.10f %20.10f %20.10f ", "LINE", x0, y0, (x1 - x0), (y1 - y0));
+	if (connect) {
+	  overlay[Noverlay].type = KII_OVERLAY_LINE;
+	  overlay[Noverlay].x = x0;
+	  overlay[Noverlay].y = y0;
+	  overlay[Noverlay].dx = x1 - x0;
+	  overlay[Noverlay].dy = y1 - y0;
+	  Noverlay ++;
+	  CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 1000);
+	}
 	x0 = x1;
 	y0 = y1;
@@ -132,5 +126,13 @@
       RD_to_XY (&x1, &y1, ra, dec, &coords);
       if ((x1 >= 0) && (x1 < NX) && (y1 >= 0) && (y1 < NY)) {
-	if (connect) SendGraphCommand (Ximage, 128, "%15s %20.10f %20.10f %20.10f %20.10f ", "LINE", x0, y0, (x1 - x0), (y1 - y0));
+	if (connect) {
+	  overlay[Noverlay].type = KII_OVERLAY_LINE;
+	  overlay[Noverlay].x = x0;
+	  overlay[Noverlay].y = y0;
+	  overlay[Noverlay].dx = x1 - x0;
+	  overlay[Noverlay].dy = y1 - y0;
+	  Noverlay ++;
+	  CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 1000);
+	}
 	x0 = x1;
 	y0 = y1;
@@ -142,5 +144,6 @@
   }
 
-  /*
+  # if (0)
+  /* write the labels on the axes */
   dDEC = fabs(dec1 - dec0) / 100;
   for (ra = dRA * ((int)(MIN(ra0, ra1)/dRA) + 1); ra < MAX(ra0, ra1); ra += dRA) {
@@ -159,38 +162,9 @@
     }
   }
-  */
+  # endif
 
-  SendGraphCommand (Ximage, 128, "DONE");
+  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
+  free (overlay);
+
   return (TRUE);
 }
-
-/*
-  write (Ximage, "LOAD", 4); 
-  sprintf (buffer, "OVERLAY: %7d", n);
-  write (Ximage, buffer, 16);
-  
-  for (n = 0; scan_line (f, buffer) != EOF; n++) {
-    c = strchr (buffer, '#');
-    if (c != (char *) NULL) 
-      *c = 0;  
-    while ((c = strchr (buffer, '(')) != (char *) NULL) 
-      *c = ' ';
-    while ((c = strchr (buffer, ')')) != (char *) NULL) 
-      *c = ' ';
-    while ((c = strchr (buffer, ',')) != (char *) NULL) 
-      *c = ' ';
-    N = sscanf (buffer, "%s %lf %lf %lf %lf", type, &x, &y, &dx, &dy);
-    if (N > 3) {
-      if (N == 4)
-	dy = dx;
-      sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", type, x, y, dx, dy);
-      write (Ximage, buffer, 128);
-    }
-  }
-  fprintf (stderr, "loaded %d objects\n", n);
-  sprintf (buffer, "DONE");
-  write (Ximage, buffer, 128);
-  if (f != stdin) {
-    fclose (f);
-  }
-*/
Index: trunk/Ohana/src/opihi/cmd.data/vcontour.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/vcontour.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/vcontour.c	(revision 5846)
@@ -42,23 +42,12 @@
 
 # define DUMP { \
-sprintf (&buffer[Nline*128], "%14s %20.10f %20.10f %20.10f %20.10f \0", "LINE", Npix*x, Npix*y, Npix*dx, Npix*dy); \
-Nline ++; \
-if (Nline == 512) { \
-  SendGraphCommand (Ximage, 16, "NLINES  %7d \0", Nline); \
-  write (Ximage, buffer, Nline*128); \
-  bzero (buffer, 65536); \
-  Nline = 0; \
-} \
-continue; }
-
-# define NUMP { \
-sprintf (&buffer[Nline*128], "%14s %20.10f %20.10f %20.10f %20.10f \0", "LINE", Npix*x, Npix*y, Npix*dx, Npix*dy); \
-Nline ++; \
-if (Nline == 512) { \
-  SendGraphCommand (Ximage, 16, "NLINES  %7d \0", Nline); \
-  write (Ximage, buffer, Nline*128); \
-  bzero (buffer, 65536); \
-  Nline = 0; \
-} }
+overlay[Noverlay].type = KII_OVERLAY_LINE; \
+overlay[Noverlay].x = Npix*x; \
+overlay[Noverlay].y = Npix*y; \
+overlay[Noverlay].dx = Npix*dx; \
+overlay[Noverlay].dy = Npix*dy; \
+Noverlay ++; \
+CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 1000); \
+}
 
 int vcontour (int argc, char **argv) {
@@ -110,4 +99,8 @@
   /* not really finished */
 
+  Noverlay = 0;
+  NOVERLAY = 1000;
+  ALLOCATE (overlay, KiiOverlay, NOVERLAY);
+
   v01 = matrix + 1;
   v10 = matrix + Nx;
@@ -132,18 +125,22 @@
 	  LL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 <= 0)) { /* -  */
 	  HZ;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 > 0)) { /* /  */
 	  UL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 > 0)) { /* \\  */
 	  LL;
-	  NUMP;
+	  DUMP;
 	  UR;
 	  DUMP;
+	  continue;
 	}
       }
@@ -153,18 +150,22 @@
 	  LL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 > 0)) { /* -  */
 	  HZ;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 <= 0)) { /* /  */
 	  UL;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 <= 0)) { /* //  */
 	  UL;
-	  NUMP;
+	  DUMP;
 	  LR;
 	  DUMP;
+	  continue;
 	}
       }
@@ -175,12 +176,15 @@
 	  LR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 > 0)) {
 	  UR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 > 0)) {
 	  VT;
 	  DUMP;
+	  continue;
 	}
       }
@@ -190,12 +194,15 @@
 	  LR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 > 0) && (d11 <= 0)) {
 	  UR;
 	  DUMP;
+	  continue;
 	}
 	if ((d01 <= 0) && (d11 <= 0)) {
 	  VT;
 	  DUMP;
+	  continue;
 	}
       }
@@ -217,5 +224,6 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       x = i + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
+      continue;
     }
   }
@@ -230,5 +238,6 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       x = i + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
+      continue;
     }
   }
@@ -243,5 +252,6 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       y = j + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
+      continue;
     }
   }
@@ -256,15 +266,12 @@
     if (((*v00 > level) && (*v01 <= level)) || ((*v00 <= level) && (*v01 > level))) {
       y = j + (level - *v01)/(*v01 - *v00);
-      NUMP;
+      DUMP;
+      continue;
     }
   }
   
-  if (Nline > 0) {
-    SendGraphCommand (Ximage, 16, "NLINES  %7d \0", Nline); 
-    write (Ximage, buffer, Nline*128); 
-  }
-
-  free (buffer);
-  SendGraphCommand (Ximage, 16, "DONE");
+  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[2]);
+  free (overlay);
+
   if (Npix != 1) free (matrix);
   fprintf (stderr, "\n");
Index: trunk/Ohana/src/opihi/cmd.data/vload.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/vload.c	(revision 5702)
+++ trunk/Ohana/src/opihi/cmd.data/vload.c	(revision 5846)
@@ -1,13 +1,10 @@
 # include "data.h"
-
-# define NPTYPE 3
-static char ptype[NPTYPE][8] = {"BOX", "LINE", "CIRCLE"};
 
 int vload (int argc, char **argv) {
   
-  int i, N, n, Nline;
-  int Ximage, Nimage;
+  int i, N, Noverlay;
+  int Ximage, Nimage, type;
   double dx, dy, size;
-  char *buffer, *type;
+  KiiOverlay *overlay;
   Vector *vecx, *vecy;
   
@@ -20,18 +17,13 @@
   if (!GetImage (&Ximage, &Nimage)) return (FALSE);
 
-  type = ptype[0];
+  type = KII_OVERLAY_BOX;
   if ((N = get_argument (argc, argv, "-type"))) {
     remove_argument (N, &argc, argv);
-    for (i = 0; i < NPTYPE; i++) {
-      if (!strcasecmp (argv[N], ptype[i])) {
-	type = ptype[i];
-	break;
-      }
-    }
-    if (i == NPTYPE) {
+    type = KiiOverlayTypeByName (argv[N]);
+    remove_argument (N, &argc, argv);
+    if (!type) {
       fprintf (stderr, "unknown Kii point type %s\n", argv[N]);
       return (FALSE);
     }
-    remove_argument (N, &argc, argv);
   }
 
@@ -48,5 +40,9 @@
   }
   
-  if (!SelectOverlay (argv[1], &n)) return (FALSE);
+  if (type == KII_OVERLAY_CIRCLE) {
+    dx = dy = size / 2;
+  } else {
+    dx = dy = size;
+  }    
 
   if ((vecx = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
@@ -57,33 +53,18 @@
   }
 
-  ALLOCATE (buffer, char, 65536);  /* space for 512 lines of 128 bytes */
-  bzero (buffer, 65536);
-  Nline = 0;
+  Noverlay = vecx[0].Nelements;
+  ALLOCATE (overlay, KiiOverlay, Noverlay);
 
-  SendGraphCommand (Ximage, 4, "LOAD");
-  SendGraphCommand (Ximage, 16, "OVER %9d ", n);
-
-  if (type == ptype[2]) {
-    dx = dy = size / 2;
-  } else {
-    dx = dy = size;
-  }    
-  for (i = 0; i < vecx[0].Nelements; i++) {
-    snprintf (&buffer[Nline*128], 128, "%15s %20.10f %20.10f %20.10f %20.10f ", type, 
-	      vecx[0].elements[i]+0.5, vecy[0].elements[i]+0.5, dx, dy);
-    Nline ++;
-    if (Nline == 512) {
-      SendGraphCommand (Ximage, 16, "NLINES  %7d ", Nline);
-      write (Ximage, buffer, Nline*128);
-      bzero (buffer, 65536);
-      Nline = 0;
-    }
+  for (i = 0; i < Noverlay; i++) {
+    overlay[i].type = type;
+    overlay[i].text = NULL;
+    overlay[i].x = vecx[0].elements[i]+0.5;
+    overlay[i].y = vecy[0].elements[i]+0.5;
+    overlay[i].dx = dx;
+    overlay[i].dy = dy;
   }
 
-  SendGraphCommand (Ximage, 16, "NLINES  %7d ", Nline);
-  write (Ximage, buffer, Nline*128);
-  SendGraphCommand (Ximage, 16, "DONE");
-
-  free (buffer);
+  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
+  free (overlay);
   return (TRUE);
 }
