Index: /branches/eam_branch_20071222/Ohana/src/kapa2/Makefile
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/Makefile	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/Makefile	(revision 15935)
@@ -77,5 +77,6 @@
 $(SRC)/bDrawOverlay.$(ARCH).o             $(SRC)/ButtonFunctions.$(ARCH).o    \
 $(SRC)/PSimage.$(ARCH).o                  $(SRC)/PSPixmap.$(ARCH).o           \
-$(SRC)/PSOverlay.$(ARCH).o                $(SRC)/SetChannel.$(ARCH).o
+$(SRC)/PSOverlay.$(ARCH).o                $(SRC)/SetChannel.$(ARCH).o         \
+$(SRC)/SetColorScale.$(ARCH).o
 
 OBJ  =  $(KAPA)
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/include/constants.h
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/include/constants.h	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/include/constants.h	(revision 15935)
@@ -31,13 +31,20 @@
 # define LABEL_MAXLEN 128
 
+typedef enum {
+    KAPA_SCALE_1D,
+    KAPA_SCALE_3D
+} KapaColorScaleMode;
+
 /* label names */
-# define LABELX0 0
-# define LABELY0 1
-# define LABELX1 2
-# define LABELY1 3
-# define LABELUL 4
-# define LABELUR 5
-# define LABELLL 6
-# define LABELLR 7
+typedef enum {
+  LABELX0,
+  LABELY0,
+  LABELX1,
+  LABELY1,
+  LABELUL,
+  LABELUR,
+  LABELLL,
+  LABELLR
+} KapaLabelMode;
 
 /* EVENT_MASK consists of:
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/include/prototypes.h	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/include/prototypes.h	(revision 15935)
@@ -140,4 +140,7 @@
 
 int           Center              PROTO(());
+void          SetColorScale       PROTO((Graphic *graphic, KapaImageWidget *image));
+void          SetColorScale1D     PROTO((Graphic *graphic, KapaImageWidget *image));
+void          SetColorScale3D     PROTO((Graphic *graphic, KapaImageWidget *image));
 void          Remap               PROTO((Graphic *graphic, KapaImageWidget *image));
 void          Remap8              PROTO((Graphic *graphic, KapaImageWidget *image, Matrix *matrix));
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/include/structures.h
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/include/structures.h	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/include/structures.h	(revision 15935)
@@ -17,8 +17,13 @@
   Colormap       colormap;
   unsigned long *color;      // graph plotting colors
-  int Ncolors;
+  int            Ncolors;
 
-  unsigned long *pixels;     // image pixel colors
+  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 
@@ -171,4 +176,6 @@
 
   unsigned short   *pixmap;   // lookup table for image pixel value to pixel index
+  int      nPixels;
+ 
   KapaImageChannel *image;
   KapaImageChannel channel[NCHANNELS];
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/ButtonFunctions.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 15935)
@@ -18,4 +18,5 @@
   SetColormap (name);
   CreateColorbar (image, graphic);
+  SetColorScale (graphic, image);
   Remap (graphic, image);
   CreateZoom (image, graphic, 0, 0); 
@@ -30,4 +31,5 @@
   SetColormap (name);
   CreateColorbar (image, graphic);
+  SetColorScale (graphic, image);
   Remap (graphic, image);
   CreateZoom (image, graphic, 0, 0); 
@@ -40,6 +42,7 @@
   char *name;
   name = RAINBOW;
-  SetColormap (name);
+  SetColormap ("fullcolor");
   CreateColorbar (image, graphic);
+  SetColorScale (graphic, image);
   Remap (graphic, image);
   CreateZoom (image, graphic, 0, 0); 
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/Image.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/Image.c	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/Image.c	(revision 15935)
@@ -33,4 +33,10 @@
   }
   image[0].image = &image[0].channel[0];
+
+  image[0].nPixels = 0;
+  ALLOCATE (image[0].pixmap, unsigned short, 1);  /* allocate so later free will not crash! */
+
+  // XXXX this has been moved to graphic, which may be wrong
+  // image[0].ColorScaleMode = KAPA_SCALE_1D;
 
   for (i = 0; i < NOVERLAYS; i++) {
@@ -165,5 +171,8 @@
     free (image[0].overlay[i].objects);
   }
-  free (image[0].image[0].matrix.buffer);
+  for (i = 0; i < NCHANNELS; i++) {
+    free (image[0].channel[i].matrix.buffer);
+  }
+  free (image[0].pixmap);
   free (image[0].picture.data);
   free (image[0].cmapbar.data);
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/LoadPicture.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/LoadPicture.c	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/LoadPicture.c	(revision 15935)
@@ -63,4 +63,5 @@
   if (!USE_XWINDOW) return (TRUE);
 
+  SetColorScale (graphic, image);
   Remap (graphic, image);
   if (DEBUG) fprintf (stderr, "remapped image\n");
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/Remap32.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/Remap32.c	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/Remap32.c	(revision 15935)
@@ -1,18 +1,3 @@
 # include "Ximage.h"
-
-static float slope = 1.0;
-static float start = 0.0;
-static int MaxValue = 255;
-
-// XXX inline this if needed
-# define PixelLookup(VALUE) (int)(MIN (MAX (slope * VALUE - start, 0), MaxValue))
-
-# if  (0)
-static int PixelLookup(float value) {
-  int out;
-  out = MIN (MAX (slope * value - start, 0), MaxValue);
-  return (out);
-}
-# endif
 
 void Remap32 (Graphic *graphic, KapaImageWidget *image, Matrix *matrix) {
@@ -25,5 +10,5 @@
   int expand_in, expand_out;
   unsigned int *out_pix, *out_pix2;
-  float *imdata, *in_pix, *in_pix2;
+  unsigned short *in_pix, *in_pix2;
   unsigned long *pixel, pixvalue;
   unsigned long back;
@@ -36,14 +21,4 @@
   }
   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;
-  }
 
   // set up expansions
@@ -98,6 +73,5 @@
 
   out_pix = (unsigned int *) image[0].picture.data;
-  imdata  = (float *) matrix[0].buffer;
-  in_pix  = &imdata[DX*(int)MAX(Y,0) + (int)MAX(X,0)];
+  in_pix  = &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) **********/
@@ -122,12 +96,9 @@
     if (expand_out == 1) {
       for (i = i_start; i < i_end; i++, in_pix2+= expand_in, out_pix++) {
-	pixelN = PixelLookup(*in_pix2);
-	*out_pix = 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)) {
@@ -148,5 +119,4 @@
     }
     out_pix += (dx - i_end);
-    
   } 
   
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/Reorient.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/Reorient.c	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/Reorient.c	(revision 15935)
@@ -4,4 +4,10 @@
 
   if (image[0].expand == 0) image[0].expand = 1;
+
+  if ((image[0].X == X) && (image[0].Y == Y) && (mode == 0)) {
+    Refresh ();
+    XFlush (graphic[0].display);
+    return;
+  }
 
   switch (mode) {
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetChannel.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetChannel.c	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetChannel.c	(revision 15935)
@@ -25,4 +25,5 @@
   if (!USE_XWINDOW) return (TRUE);
 
+  SetColorScale (graphic, image);
   Remap (graphic, image);
   if (DEBUG) fprintf (stderr, "remapped image\n");
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColorScale.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColorScale.c	(revision 15935)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColorScale.c	(revision 15935)
@@ -0,0 +1,130 @@
+# include "Ximage.h"
+
+void SetColorScale (Graphic *graphic, KapaImageWidget *image) {
+
+  switch (graphic[0].ColorScaleMode) {
+    case KAPA_SCALE_1D:
+      SetColorScale1D (graphic, image);
+      break;
+    case KAPA_SCALE_3D:
+      SetColorScale3D (graphic, image);
+      break;
+    default:
+      fprintf (stderr, "programming error in kapa: unknown color scale mode\n");
+      return;
+  }
+  return;
+}
+
+void SetColorScale1D (Graphic *graphic, KapaImageWidget *image) {
+  int i, j, DX, DY, value, nPixels;
+  float *iData;
+  unsigned short *oData;
+  float slope;
+  float start;
+  unsigned short MaxValue;
+  Matrix *matrix;
+
+  // 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;
+  }
+
+  matrix = &image->image->matrix;
+
+  DX = matrix[0].Naxis[0];
+  DY = matrix[0].Naxis[1];
+
+  nPixels = DX*DY;
+  if (image[0].nPixels != nPixels) {
+    REALLOCATE (image[0].pixmap, unsigned short, nPixels);
+    image[0].nPixels = nPixels;
+  }
+
+  oData = image[0].pixmap;
+  iData = (float *) matrix[0].buffer;
+
+  // convert pixel data values to pixel index values (0 - Npixel)
+  for (i = 0; i < nPixels; i++, iData++, oData++) {
+    value = *iData * slope - start;
+    if (value < 0) value = 0;
+    if (value > MaxValue) value = MaxValue;
+    *oData = value;
+  }
+}
+
+// in 3D we use channels 0,1,2 to choose the pixel from the cube
+void SetColorScale3D (Graphic *graphic, KapaImageWidget *image) {
+  int i, j, DX, DY, value, nPixels, rValue, gValue, bValue;
+  float *rData, *bData, *gData;
+  unsigned short *oData;
+  float redSlope, blueSlope, greenSlope;
+  float redStart, blueStart, greenStart;
+
+  // define the color transform parameters
+  unsigned short maxRed = graphic[0].nRed - 1;
+  unsigned short maxBlue = graphic[0].nBlue - 1;
+  unsigned short maxGreen = graphic[0].nGreen - 1;
+
+  // set start & slope for red (channel 0)
+  if (image[0].channel[0].range != 0.0) {
+    redSlope = graphic[0].nRed / image[0].channel[0].range;
+    redStart = graphic[0].nRed * image[0].channel[0].zero / image[0].channel[0].range;
+  } else {
+    redSlope = 1.0;
+    redStart = image[0].channel[0].zero;
+  }
+  // set start & slope for blue (channel 0)
+  if (image[0].channel[1].range != 0.0) {
+    blueSlope = graphic[0].nBlue / image[0].channel[1].range;
+    blueStart = graphic[0].nBlue * image[0].channel[1].zero / image[0].channel[1].range;
+  } else {
+    blueSlope = 1.0;
+    blueStart = image[0].channel[1].zero;
+  }
+  // set start & slope for green (channel 0)
+  if (image[0].channel[2].range != 0.0) {
+    greenSlope = graphic[0].nGreen / image[0].channel[2].range;
+    greenStart = graphic[0].nGreen * image[0].channel[2].zero / image[0].channel[2].range;
+  } else {
+    greenSlope = 1.0;
+    greenStart = image[0].channel[2].zero;
+  }
+
+  DX = image[0].channel[0].matrix.Naxis[0];
+  DY = image[0].channel[0].matrix.Naxis[1];
+  // check on equal size for channel[1] and channel[2]
+
+  nPixels = DX*DY;
+  if (image[0].nPixels != nPixels) {
+    REALLOCATE (image[0].pixmap, unsigned short, nPixels);
+    image[0].nPixels = nPixels;
+  }
+
+  oData = image[0].pixmap;
+  rData = (float *) image[0].channel[0].matrix.buffer;
+  bData = (float *) image[0].channel[1].matrix.buffer;
+  gData = (float *) image[0].channel[2].matrix.buffer;
+
+  // convert pixel data values to pixel index values (0 - Npixel)
+  for (i = 0; i < nPixels; i++, rData++, bData++, gData++, oData++) {
+    rValue = *rData * redSlope   - redStart;
+    if (rValue < 0) rValue = 0;
+    if (rValue > maxRed) rValue = maxRed;
+
+    bValue = *bData * blueSlope  - blueStart;
+    if (bValue < 0) bValue = 0;
+    if (bValue > maxBlue) bValue = maxBlue;
+
+    gValue = *gData * greenSlope - greenStart;
+    if (gValue < 0) gValue = 0;
+    if (gValue > maxGreen) gValue = maxGreen;
+
+    *oData = gValue + bValue * (maxGreen + 1) + rValue * (maxGreen + 1) * (maxBlue + 1);
+  }
+}
Index: /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColormap.c
===================================================================
--- /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColormap.c	(revision 15934)
+++ /branches/eam_branch_20071222/Ohana/src/kapa2/src/SetColormap.c	(revision 15935)
@@ -13,9 +13,40 @@
 int SetColormap (char *name) {
 
-  int i, ref1, ref2;
+  int i, red, blue, green;
   float scale, blueRef, redRef, greenRef;
   Graphic *graphic;
 
   graphic = GetGraphic();
+
+  // very simple color model: evenly spaced cube 
+  if (!strcasecmp (name, "fullcolor")) {
+      graphic[0].nRed  = pow (graphic[0].Npixels, 0.333);
+      graphic[0].nBlue = pow (graphic[0].Npixels, 0.333);
+      graphic[0].nGreen = graphic[0].Npixels / (graphic[0].nRed * graphic[0].nBlue);
+
+      // red,green,blue are values in range 0x0000 to 0xffff
+      float redScale = 0xffff / (graphic[0].nRed - 1);
+      float blueScale = 0xffff / (graphic[0].nBlue - 1);
+      float greenScale = 0xffff / (graphic[0].nGreen - 1);
+
+      i = 0;
+      for (red = 0; red < graphic[0].nRed; red++) {  
+	  for (blue = 0; blue < graphic[0].nBlue; blue++) {  
+	      for (green = 0; green < graphic[0].nGreen; green++, i++) {  
+		  SETVALUE (graphic[0].cmap[i].red,   red*redScale, 0, 0xffff);
+		  SETVALUE (graphic[0].cmap[i].blue,  blue*blueScale, 0, 0xffff);
+		  SETVALUE (graphic[0].cmap[i].green, green*greenScale, 0, 0xffff);
+		  graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
+	      }
+	  }
+      }
+
+      // all other modes are 1D: set the flag:
+      graphic[0].ColorScaleMode = KAPA_SCALE_3D;
+      goto store_colors;
+  }
+
+  // all other modes are 1D: set the flag:
+  graphic[0].ColorScaleMode = KAPA_SCALE_1D;
 
   // red,green,blue are values in range 0x0000 to 0xffff
