Index: /trunk/Ohana/src/kapa2/Makefile
===================================================================
--- /trunk/Ohana/src/kapa2/Makefile	(revision 16038)
+++ /trunk/Ohana/src/kapa2/Makefile	(revision 16039)
@@ -79,5 +79,5 @@
 $(SRC)/PSOverlay.$(ARCH).o                $(SRC)/SetChannel.$(ARCH).o         \
 $(SRC)/SetColorScale.$(ARCH).o            $(SRC)/ColorCube.$(ARCH).o          \
-$(SRC)/ColorHistogram.$(ARCH).o           $(SRC)/sort.$(ARCH).o
+$(SRC)/ColorHistogram.$(ARCH).o           
 
 OBJ  =  $(KAPA)
Index: /trunk/Ohana/src/kapa2/include/Ximage.h
===================================================================
--- /trunk/Ohana/src/kapa2/include/Ximage.h	(revision 16038)
+++ /trunk/Ohana/src/kapa2/include/Ximage.h	(revision 16039)
@@ -1,3 +1,11 @@
-# include <Xohana.h>
+# include <X11/Xatom.h>
+# include <X11/Xlib.h>
+# include <X11/Xresource.h>
+# include <X11/Xutil.h>
+# include <X11/cursorfont.h>
+# include <X11/keysym.h>
+# include <X11/keysymdef.h>
+
+# include <ohana.h>
 # include <dvo.h>
 # include <kapa.h>
Index: /trunk/Ohana/src/kapa2/include/structures.h
===================================================================
--- /trunk/Ohana/src/kapa2/include/structures.h	(revision 16038)
+++ /trunk/Ohana/src/kapa2/include/structures.h	(revision 16039)
@@ -1,68 +1,2 @@
-
-/**************** Graphic carries X info around ****************/
-typedef struct {
-  Display       *display;     // X display pointer
-  int            screen;      // X screen number
-  int            depth;
-  Window         window;
-  Visual        *visual;
-  int            visualclass; // is visual dynamic? (XXX change name?)
-  int            Nbits;	      // pixel depth in bits (8, 16, 24, 32)
-  GC             gc;
-  XFontStruct   *font;
-  Cursor         cursor;
-  int            x,  y;
-  unsigned int   dx, dy;
-  XColor        *cmap;
-  Colormap       colormap;
-  unsigned long *color;      // graph plotting colors
-  int            Ncolors;
-
-  unsigned long *pixels;      // image pixel colors
-  int Npixels;		      // number of pixels
-
-  int            ColorScaleMode; // single colormap for all images??
-  int nRed;
-  int nBlue;
-  int nGreen;
-
-  unsigned long  fore;	      // basic foreground color 
-  unsigned long  back;	      // basic background color
-
-  unsigned long  overlay_color[NOVERLAYS]; // image plotting colors 
-} Graphic;
-
-/**************** X related "widget" structures ****************/
-typedef struct {
-  int      x, y, dx, dy;      /* position and size */
-  int      width, height;     /* size of the bitmap */
-  char    *bitmap;            /* picture on button */
-  int    (*function_1) ();    /* mouse_button 1 function */
-  int    (*function_2) ();    /* mouse_button 2 function */
-  int    (*function_3) ();    /* mouse_button 3 function */
-} Button;
-
-typedef struct {
-  int      x, y, dx, dy;   /* position and size */
-  char    *label;          /* label on TextLine */
-  char     text[1024];     /* words of TextLine */
-  char     old_text[1024]; /* words of TextLine */
-  int      outline;        /* draw an outline?  */ 
-  int      cursor;         /* location of cursor (if selected) */
-  int    (*function) ();   /* textline function */
-} TextLine;
-
-typedef char STRING[1024];
-
-typedef struct {
-  int      x, y, dx, dy;   /* position and size */
-  STRING  *text;           /* words of TextLine */
-  int      Nlines;
-  int      outline;        /* draw an outline?  */ 
-  int      cursor_line;    /* cursor line */
-  int      cursor_x;       /* location of cursor (if selected) */
-  int      cursor_y;       /* location of cursor (if selected) */
-  int    (*function) ();   /* textline function */
-} TextBox;
 
 /*** 3C color cube histogram tree thingy ***/
@@ -80,4 +14,71 @@
 } CCNode;
 
+/**************** Graphic carries X info around ****************/
+typedef struct {
+  Display       *display;     // X display pointer
+  int            screen;      // X screen number
+  int            depth;
+  Window         window;
+  Visual        *visual;
+  int            visualclass; // is visual dynamic? (XXX change name?)
+  int            Nbits;	      // pixel depth in bits (8, 16, 24, 32)
+  GC             gc;
+  XFontStruct   *font;
+  Cursor         cursor;
+  int            x,  y;
+  unsigned int   dx, dy;
+  CCNode        *cube;
+  XColor        *cmap;
+  Colormap       colormap;
+  unsigned long *color;      // graph plotting colors
+  int            Ncolors;
+
+  unsigned long *pixels;      // image pixel colors
+  int Npixels;		      // number of pixels
+
+  int            ColorScaleMode; // single colormap for all images??
+  int nRed;
+  int nBlue;
+  int nGreen;
+
+  unsigned long  fore;	      // basic foreground color 
+  unsigned long  back;	      // basic background color
+
+  unsigned long  overlay_color[NOVERLAYS]; // image plotting colors 
+} Graphic;
+
+/**************** X related "widget" structures ****************/
+typedef struct {
+  int      x, y, dx, dy;      /* position and size */
+  int      width, height;     /* size of the bitmap */
+  char    *bitmap;            /* picture on button */
+  int    (*function_1) ();    /* mouse_button 1 function */
+  int    (*function_2) ();    /* mouse_button 2 function */
+  int    (*function_3) ();    /* mouse_button 3 function */
+} Button;
+
+typedef struct {
+  int      x, y, dx, dy;   /* position and size */
+  char    *label;          /* label on TextLine */
+  char     text[1024];     /* words of TextLine */
+  char     old_text[1024]; /* words of TextLine */
+  int      outline;        /* draw an outline?  */ 
+  int      cursor;         /* location of cursor (if selected) */
+  int    (*function) ();   /* textline function */
+} TextLine;
+
+typedef char STRING[1024];
+
+typedef struct {
+  int      x, y, dx, dy;   /* position and size */
+  STRING  *text;           /* words of TextLine */
+  int      Nlines;
+  int      outline;        /* draw an outline?  */ 
+  int      cursor_line;    /* cursor line */
+  int      cursor_x;       /* location of cursor (if selected) */
+  int      cursor_y;       /* location of cursor (if selected) */
+  int    (*function) ();   /* textline function */
+} TextBox;
+
 /**************** general structures ****************/
 typedef struct {
Index: /trunk/Ohana/src/kapa2/src/ButtonFunctions.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 16038)
+++ /trunk/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 16039)
@@ -1,7 +1,3 @@
 # include "Ximage.h"
-
-static char *GREY = "greyscale";
-static char *HEAT = "Heat";
-static char *RAINBOW = "Rainbow";
 
 int PSfunction (Graphic *graphic, KapaImageWidget *image) {
@@ -14,7 +10,5 @@
 
 int greycolors (Graphic *graphic, KapaImageWidget *image) {
-  char *name;
-  name = GREY;
-  SetColormap (name);
+  SetColormap ("greyscale");
   CreateColorbar (image, graphic);
   SetColorScale (graphic, image);
@@ -27,7 +21,5 @@
 
 int heat (Graphic *graphic, KapaImageWidget *image) {
-  char *name;
-  name = HEAT;
-  SetColormap ("ruffcolor");
+  SetColormap ("heat");
   CreateColorbar (image, graphic);
   SetColorScale (graphic, image);
@@ -40,7 +32,5 @@
 
 int rainbow (Graphic *graphic, KapaImageWidget *image) {
-  char *name;
-  name = RAINBOW;
-  SetColormap ("fullcolor");
+  SetColormap ("rainbow");
   CreateColorbar (image, graphic);
   SetColorScale (graphic, image);
Index: /trunk/Ohana/src/kapa2/src/InterpretKeys.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/InterpretKeys.c	(revision 16038)
+++ /trunk/Ohana/src/kapa2/src/InterpretKeys.c	(revision 16039)
@@ -91,4 +91,6 @@
     case XK_F1:
       image[0].image = &image[0].channel[0];
+      SetColorScale (graphic, image);
+      Remap (graphic, image);
       Reorient (graphic, image, image[0].X, image[0].Y, 0);
       break;
@@ -96,4 +98,6 @@
     case XK_F2:
       image[0].image = &image[0].channel[1];
+      SetColorScale (graphic, image);
+      Remap (graphic, image);
       Reorient (graphic, image, image[0].X, image[0].Y, 0);
       break;
@@ -101,4 +105,6 @@
     case XK_F3:
       image[0].image = &image[0].channel[2];
+      SetColorScale (graphic, image);
+      Remap (graphic, image);
       Reorient (graphic, image, image[0].X, image[0].Y, 0);
       break;
Index: /trunk/Ohana/src/kapa2/src/SetColorScale.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/SetColorScale.c	(revision 16038)
+++ /trunk/Ohana/src/kapa2/src/SetColorScale.c	(revision 16039)
@@ -148,9 +148,17 @@
 
 // in 3D we use channels 0,1,2 to choose the pixel from the cube
-int SetColorScale3D_CC (Graphic *graphic, KapaImageWidget *image) {
+int SetColorCubeHistogram () {
 
   int i, DX, DY, Nvalues, NVALUES, Npixels, Nmin;
   float *values, *colors;
   CCNode *cube;
+  Graphic *graphic;
+  Section *section;
+  KapaImageWidget *image;
+
+  graphic = GetGraphic ();
+  section = GetActiveSection();
+  if (section->image == NULL) return (FALSE);
+  image = section->image;
 
   DX = image[0].channel[KAPA_CHANNEL_RED].matrix.Naxis[0];
@@ -162,5 +170,10 @@
 
   // create the top-level cube
-  cube = CCNodeAlloc();
+  if (graphic[0].cube != NULL) {
+    CCNodeFree (graphic[0].cube);
+  }
+  graphic[0].cube = CCNodeAlloc();
+
+  cube = graphic[0].cube;
   cube->min[CC_X] = cube->min[CC_Y] = cube->min[CC_Z] = 0.0;
   cube->max[CC_X] = cube->max[CC_Y] = cube->max[CC_Z] = 1.0;
@@ -185,5 +198,5 @@
   
     // select top N cells
-    sort (values, Nvalues);
+    fsort (values, Nvalues);
 
     // split nodes with more than value[n]
@@ -214,4 +227,16 @@
     }
   }
+  return TRUE;
+}
+
+// in 3D we use channels 0,1,2 to choose the pixel from the cube
+int SetColorScale3D_CC (Graphic *graphic, KapaImageWidget *image) {
+
+  int i, DX, DY, Nvalues, NVALUES, Npixels, Nmin;
+  float *values, *colors;
+  CCNode *cube;
+
+  // create the top-level cube
+  cube = graphic[0].cube;
   CCNodeSetColorPixels (image, cube);
   return TRUE;
Index: /trunk/Ohana/src/kapa2/src/SetColormap.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/SetColormap.c	(revision 16038)
+++ /trunk/Ohana/src/kapa2/src/SetColormap.c	(revision 16039)
@@ -23,5 +23,5 @@
   if (!strcasecmp (name, "fullcolor")) {
     graphic[0].ColorScaleMode = KAPA_SCALE_3D_FULL;
-    return TRUE;
+    if (SetColorCubeHistogram ()) return TRUE;
   }
 
Index: /trunk/Ohana/src/kapa2/src/SetUpGraphic.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/SetUpGraphic.c	(revision 16038)
+++ /trunk/Ohana/src/kapa2/src/SetUpGraphic.c	(revision 16039)
@@ -21,4 +21,5 @@
     ALLOCATE (graphic[0].cmap,   XColor,        NPIXELS_STATIC);
     graphic[0].Npixels = NPIXELS_STATIC;
+    graphic[0].cube = NULL;
     return;
   }
Index: /trunk/Ohana/src/kapa2/test/input
===================================================================
--- /trunk/Ohana/src/kapa2/test/input	(revision 16038)
+++ /trunk/Ohana/src/kapa2/test/input	(revision 16039)
@@ -5,9 +5,9 @@
   set y = yramp(a)
   set z = sqrt((x-256)^2 + (y-256)^2)
-  tvchannel 0
+  tvchannel 1
   tv x 0 512
-  tvchannel 1
+  tvchannel 2
   tv y 0 512
-  tvchannel 2
+  tvchannel 3
   tv z 0 512
 end
@@ -20,9 +20,13 @@
   set G = g*(g >= 0) + (g + 256)*(g < 0)
   set B = b*(b >= 0) + (b + 256)*(b < 0)
-  tvchannel 0
+  tvchannel 1
   tv R 0 256
-  tvchannel 1
+  tvchannel 2
   tv G 0 256
-  tvchannel 2
+  tvchannel 3
   tv B 0 256
+
+  tvcolor ruffcolor
+  sleep 1
+  tvcolor fullcolor
 end
