Index: /branches/eam_branch_20071222/Ohana/src/kapa2/include/constants.h
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/include/constants.h	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/include/constants.h	(revision 16009)
@@ -14,5 +14,5 @@
 
 // XXX for the moment, this is set to match the values in SetColorScale3D_CC
-# define NPIXELS_STATIC 3000
+# define NPIXELS_STATIC 4600
 
 # define PAD1  3
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/include/prototypes.h	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/include/prototypes.h	(revision 16009)
@@ -71,4 +71,6 @@
 int           GetImageCoords      PROTO((int sock));
 int           GetImageRange       PROTO((int sock));
+int           SetChannel (int sock);
+int           SetColormapFromPipe (int sock);
 
 int           LoadVectorData      PROTO((int sock, KapaGraphWidget *graph, int N, char *type));
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/CheckPipe.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/CheckPipe.c	(revision 16009)
@@ -274,4 +274,10 @@
   }
 
+  if (!strcmp (word, "CMAP")) {
+    status = SetColormapFromPipe (sock);
+    KiiSendCommand (sock, 4, "DONE");
+    FINISHED (status);
+  }
+
   if (!strcmp (word, "SAVE")) {
     status = SaveOverlay (sock);
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/CreateZoom32.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/CreateZoom32.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/CreateZoom32.c	(revision 16009)
@@ -1,15 +1,4 @@
 # include "Ximage.h"
 # define FRAC(a) ((a) - (int)(a))
-
-static float slope = 1.0;
-static float start = 0.0;
-static int MaxValue = 255;
-
-// XXX inline this if needed
-static int PixelLookup(float value) {
-  int out;
-  out = MIN (MAX (slope * value - start, 0), MaxValue);
-  return (out);
-}
 
 void CreateZoom32 (KapaImageWidget *image, Graphic *graphic, double x, double y) {
@@ -22,6 +11,6 @@
   int expand_in, expand_out;
   unsigned int *out_pix, *out_pix2;
-  float *imdata, *in_pix, *in_pix2;
-  unsigned long pixel[256], pixvalue;
+  unsigned short *in_pix, *in_pix2;
+  unsigned long *pixel, pixvalue;
   unsigned long back;
 
@@ -33,18 +22,11 @@
   }
 
-  for (i = 0; i < 256; i++) { /* set up pixel array */
+  ALLOCATE (pixel, unsigned long, graphic[0].Npixels);
+
+  // local array for pixel values
+  for (i = 0; i < graphic[0].Npixels; i++) { 
     pixel[i] = graphic[0].cmap[i].pixel;
   }
   back = graphic[0].back;
-
-  // define the color transform parameters
-  MaxValue = graphic[0].Npixels - 1;
-  if (image[0].image[0].range != 0.0) {
-    slope = graphic[0].Npixels / image[0].image[0].range;
-    start = graphic[0].Npixels * image[0].image[0].zero / image[0].image[0].range;
-  } else {
-    slope = 1.0;
-    start = image[0].image[0].zero;
-  }
 
   zoomscale = MAX (5, image[0].expand + 5);
@@ -57,4 +39,6 @@
   DX = image[0].image[0].matrix.Naxis[0];
   DY = image[0].image[0].matrix.Naxis[1];
+  
+  /* Rx,Ry are the screen coordinates of the first image pixel */
   Rx = x - expand*(int)(0.5*(dx + 1)) + 1;
   Ry = y - expand*(int)(0.5*(dy + 1)) + 1;
@@ -64,10 +48,10 @@
   i_end   = MAX (MIN ((DX-Rx) / expand, dx - expand_out + 1), 0);
   j_end   = MAX (MIN ((DY-Ry) / expand, dy - expand_out + 1), 0);
+
   dropback = expand_out - (i_end - i_start) % expand_out;
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
 
   out_pix = (unsigned int *) image[0].zoom.data;
-  imdata  = (float *) image[0].image[0].matrix.buffer;
-  in_pix  = &imdata[DX*(int)MAX(Ry,0) + (int)MAX(Rx,0)];
+  in_pix  = &image[0].pixmap[DX*(int)MAX(Ry,0) + (int)MAX(Rx,0)];
 
   /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
@@ -78,9 +62,9 @@
     return;
   } 
-  
+ 
 
   /**** fill in bottom area ****/
   for (j = 0; j < dx*j_start; j++, out_pix++) {
-    out_pix[0] = back;
+    *out_pix = back;
   }
 
@@ -88,8 +72,8 @@
 
     /**** fill in area to the left of the picture ****/
-    for (jj = 0; jj < expand_out; jj++) { 
+    for (jj = 0; (i_start > 0) && (jj < expand_out); jj++) { 
       out_pix2 = out_pix + jj*dx;
       for (i = 0; i < i_start; i++, out_pix2++) {
-	out_pix[0] = back;
+	*out_pix2 = back;
       }
     }
@@ -100,16 +84,13 @@
     if (expand_out == 1) {
       for (i = i_start; i < i_end; i++, in_pix2+= expand_in, out_pix++) {
-	pixelN = PixelLookup(*in_pix2);
-	out_pix[0] = pixel[pixelN];
+	*out_pix = pixel[*in_pix2];
       }
-    }
-    else {
+    } else {
       for (i = i_start; i < i_end; i+= expand_out, in_pix2++, out_pix+= expand_out) { 
-	pixelN   = PixelLookup(*in_pix2);
-	pixvalue = pixel[pixelN];
+	pixvalue = pixel[*in_pix2];
 	out_pix2 = out_pix;
 	for (jj = 0; jj < expand_out; jj++, out_pix2+=(dx-expand_out)) {
 	  for (ii = 0; ii < expand_out; ii++, out_pix2++) {
-	    out_pix2[0] = pixvalue; 
+	    *out_pix2 = pixvalue; 
 	  }
 	}
@@ -117,5 +98,4 @@
     }
     out_pix -= dropback;
-
     
     /**** fill in area to the right of the picture ****/
@@ -123,5 +103,5 @@
       out_pix2 = out_pix + jj*dx;
       for (i = i_end; i < dx; i++, out_pix2++) {
-	out_pix2[0] = back;
+	*out_pix2 = back;
       }
     }
@@ -134,10 +114,10 @@
   /**** fill in top area ****/
   for (j = 0; (j < dx*(dy - j_end)) && (out_pix - (unsigned int *)image[0].zoom.data < dx*dy); j++, out_pix++) { 
-    out_pix[0] = back;
+    *out_pix = back;
   }
 
-  out_pix = (unsigned int *)image[0].zoom.data;
   image[0].zoom.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
 					image[0].zoom.data, image[0].zoom.dx, image[0].zoom.dy, 32, 0);
   
+  free (pixel);
 }
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/JPEGit24.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/JPEGit24.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/JPEGit24.c	(revision 16009)
@@ -37,6 +37,6 @@
   double expand, Rx, Ry, X, Y;
   unsigned char *out_pix;
-  float *imdata, *in_pix, *in_pix_ref;
-  unsigned char pixel1[256], pixel2[256], pixel3[256];
+  unsigned short *in_pix, *in_pix_ref;
+  unsigned char *pixel1, *pixel2, *pixel3;
   char filename[1024];
   FILE *f;
@@ -75,19 +75,13 @@
   jpeg_start_compress (&cinfo, TRUE);
 
+  ALLOCATE (pixel1, unsigned char, graphic[0].Npixels);
+  ALLOCATE (pixel2, unsigned char, graphic[0].Npixels);
+  ALLOCATE (pixel3, unsigned char, graphic[0].Npixels);
+
   /** cmap[i].pixel must be defined even if X is not used **/
-  for (i = 0; i < 256; i++) { /* set up pixel array */
+  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
     pixel1[i] = graphic[0].cmap[i].red >> 8;
     pixel2[i] = graphic[0].cmap[i].green >> 8;
     pixel3[i] = graphic[0].cmap[i].blue >> 8;
-  }
-
-  // define the color transform parameters
-  MaxValue = graphic[0].Npixels - 1;
-  if (image[0].image[0].range != 0.0) {
-    slope = graphic[0].Npixels / image[0].image[0].range;
-    start = graphic[0].Npixels * image[0].image[0].zero / image[0].image[0].range;
-  } else {
-    slope = 1.0;
-    start = image[0].image[0].zero;
   }
 
@@ -145,6 +139,5 @@
   ALLOCATE (line_buffer, JSAMPLE, 3*dx);
 
-  imdata = (float *) image[0].image[0].matrix.buffer;
-  in_pix_ref = &imdata[DX*(int)MAX(Y,0) + (int)MAX(X,0)];
+  in_pix_ref  = &image[0].pixmap[DX*(int)MAX(Y,0) + (int)MAX(X,0)];
 
   /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
@@ -177,9 +170,8 @@
     /*** fill in the picture region ***/
     for (i = i_start; i < i_end; i+=expand_out, in_pix+=expand_in) {
-      pixelN = PixelLookup(*in_pix);
       for (ii = 0; ii < expand_out; ii++, out_pix+=3) {
-	out_pix[0] = pixel1[pixelN];
-	out_pix[1] = pixel2[pixelN];
-	out_pix[2] = pixel3[pixelN];
+	out_pix[0] = pixel1[*in_pix];
+	out_pix[1] = pixel2[*in_pix];
+	out_pix[2] = pixel3[*in_pix];
       }
     }
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/PSPixmap.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/PSPixmap.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/PSPixmap.c	(revision 16009)
@@ -143,2 +143,41 @@
   return;
 }
+
+# if (0)
+// XXX needs work!
+void PSPixmap32_RGB (Graphic *graphic, KapaImageWidget *image, FILE *f) {
+
+  int i, k, m, val;
+  double Nchar, Npix, start, slope, frac;
+  unsigned int *buff;
+  unsigned long back;
+  unsigned char 
+
+  ALLOCATE (pixelR, unsigned char, graphic[0].Npixels);
+  ALLOCATE (pixelG, unsigned char, graphic[0].Npixels);
+  ALLOCATE (pixelB, unsigned char, graphic[0].Npixels);
+
+  /** cmap[i].pixel must be defined even if X is not used **/
+  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
+    pixelR[i] = graphic[0].cmap[i].red >> 8;
+    pixelG[i] = graphic[0].cmap[i].green >> 8;
+    pixelB[i] = graphic[0].cmap[i].blue >> 8;
+  }
+
+  for (i = 0; i < image[0].picture.dy; i++) {
+    for (k = 0; k < image[0].picture.dx; k++, buff++) {
+      if (*buff == back) 
+	val = Nchar;
+      else {
+	for (m = 0; (graphic[0].cmap[m].pixel != *buff) && (m < Npix); m++);
+	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
+      }
+      fprintf (f, "%02x", val);
+      if (!((k+1) % 40)) fprintf (f, "\n"); 
+    }
+    fprintf (f, "\n");
+    buff -= 2*image[0].picture.dx;
+  }
+  return;
+}
+# endif
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/Remap32.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/Remap32.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/Remap32.c	(revision 16009)
@@ -42,4 +42,5 @@
   DX = matrix[0].Naxis[0];
   DY = matrix[0].Naxis[1];
+
   /* X,Y are the image coordinates of the first image pixel */
   X = MAX(0.5*(DX - dx*expand) - image[0].X, 0);
@@ -78,6 +79,7 @@
 
   /**** fill in bottom area ****/
-  for (j = 0; j < dx*j_start; j++, out_pix++) 
+  for (j = 0; j < dx*j_start; j++, out_pix++) {
     *out_pix = back;
+  }
   
   for (j = j_start; j < j_end; j+= expand_out, out_pix+=(expand_out-1)*dx, in_pix += expand_in*DX) {
@@ -128,4 +130,5 @@
     *out_pix = back;
   }
+
   image[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
 					image[0].picture.data, image[0].picture.dx, image[0].picture.dy, 32, 0);
@@ -133,3 +136,2 @@
   free (pixel);
 }
-
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetChannel.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetChannel.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetChannel.c	(revision 16009)
@@ -34,2 +34,31 @@
   return (TRUE);
 }
+
+int SetColormapFromPipe (int sock) {
+  
+  Graphic *graphic;
+  Section *section;
+  KapaImageWidget *image;
+  char colormap[256];
+  int status;
+
+  graphic = GetGraphic ();
+  section = GetActiveSection();
+  if (section->image == NULL) {
+    section->image = InitImageWidget ();
+    SetSectionSizes (section);
+  }
+  image = section->image;
+
+  KiiScanMessage (sock, "%s", colormap);
+
+  status = SetColormap (colormap);
+  if (!status) fprintf (stderr, "unknown colormap %s\n", colormap);
+
+  SetColorScale (graphic, image);
+  Remap (graphic, image);
+  Refresh ();
+  XFlush (graphic[0].display);
+
+  return (TRUE);
+}
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColorScale.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColorScale.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColorScale.c	(revision 16009)
@@ -170,7 +170,9 @@
   CCSplitNodeIterate (cube, 0, 2);
 
-  // need to allocate at least 1184 pixels
-  // 64, 288, 512, 736, 960, 1184
-  for (i = 0; i < 8; i++) {
+  // need to allocate at least 64 + NPASS*7*NSPLIT pixels (overestimate if 64 < NSPLIT)
+# define NPASS 5
+# define NSPLIT 128
+
+  for (i = 0; i < NPASS; i++) {
     // generate histogram
     ColorHistogram (image, cube);
@@ -186,5 +188,5 @@
 
     // split nodes with more than value[n]
-    Nmin = MAX (0, Nvalues - 50);
+    Nmin = MAX (0, Nvalues - NSPLIT);
     CCNodeDivideLimit (cube, values[Nmin]);
     free (values);
Index: /branches/eam_branch_20071222/Ohana/src/libkapa/include/kapa.h
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/libkapa/include/kapa.h	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/libkapa/include/kapa.h	(revision 16009)
@@ -125,4 +125,5 @@
 /* KiiPicture.c */
 int KiiSetChannel (int fd, int channel);
+int KiiSetColormap (int fd, char *colormap);
 int KiiNewPicture1D (int fd, KiiImage *image, KapaImageData *data, Coords *coords);
 int KiiNewPicture2D (int fd, KiiImage *image, KapaImageData *data, Coords *coords);
Index: /branches/eam_branch_20071222/Ohana/src/libkapa/src/KiiPicture.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/libkapa/src/KiiPicture.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/libkapa/src/KiiPicture.c	(revision 16009)
@@ -5,4 +5,13 @@
   KiiSendCommand (fd, 4, "CHAN"); /* tell kapa to look for the incoming image */
   KiiSendMessage (fd, "%1d", channel);
+  
+  KiiWaitAnswer (fd, "DONE");
+  return (TRUE);
+}
+
+int KiiSetColormap (int fd, char *colormap) {
+
+  KiiSendCommand (fd, 4, "CMAP"); /* tell kapa to look for the incoming image */
+  KiiSendMessage (fd, "%s", colormap);
   
   KiiWaitAnswer (fd, "DONE");
Index: /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/Makefile
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/Makefile	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/Makefile	(revision 16009)
@@ -114,4 +114,5 @@
 $(SRC)/tv.$(ARCH).o		   \
 $(SRC)/tvchannel.$(ARCH).o	   \
+$(SRC)/tvcolors.$(ARCH).o	   \
 $(SRC)/tvcontour.$(ARCH).o	   \
 $(SRC)/tvgrid.$(ARCH).o	   \
Index: /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/init.c	(revision 16008)
+++ /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/init.c	(revision 16009)
@@ -97,4 +97,5 @@
 int tv               PROTO((int, char **));
 int tvchannel        PROTO((int, char **));
+int tvcolors         PROTO((int, char **));
 int tvcontour        PROTO((int, char **));
 int tvgrid           PROTO((int, char **));
@@ -216,4 +217,5 @@
   {"tv",      	   tv,		     "display an image on the Kii window"},
   {"tvchannel",	   tvchannel,	     "set the current tv channel"},
+  {"tvcolors",	   tvcolors,	     "set the tv colormap"},
   {"tvcontour",	   tvcontour,	     "send contour to overlay"},
   {"tvgrid",	   tvgrid,	     "wait until return is typed"},
Index: /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/tvcolors.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/tvcolors.c	(revision 16009)
+++ /branches/eam_branch_20071222/Ohana/src/opihi/cmd.data/tvcolors.c	(revision 16009)
@@ -0,0 +1,26 @@
+# include "data.h"
+
+int tvcolors (int argc, char **argv) {
+  
+  int N, kapa, Nchannel;
+  char *name;
+  KapaImageData data;
+
+  name = NULL;
+  if ((N = get_argument (argc, argv, "-n"))) {
+    remove_argument (N, &argc, argv);
+    name = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if (!GetImage (&data, &kapa, name)) return (FALSE);
+  FREE (name);
+
+  // use the currently-set zero,range values if not supplied
+  if (argc != 2) {
+    gprint (GP_ERR, "USAGE: tvcolors (colormap)\n");
+    return (FALSE);
+  }
+
+  KiiSetColormap (kapa, argv[1]);
+  return (TRUE);
+}
