Index: /trunk/Ohana/src/kapa2/src/SetColormap.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/SetColormap.c	(revision 34578)
+++ /trunk/Ohana/src/kapa2/src/SetColormap.c	(revision 34579)
@@ -24,4 +24,7 @@
   graphic = GetGraphic();
 
+  int NANValue = graphic[0].Npixels - 1;
+  int MaxValue = graphic[0].Npixels - 1;
+
   // the "fullcolor" colormap is uniquely defined for each image;
   // defer to the 'SetColorScale' step
@@ -35,7 +38,7 @@
   { 
       // XXX set pure green for now
-      graphic[0].cmap[graphic[0].Npixels-1].red = 0;
-      graphic[0].cmap[graphic[0].Npixels-1].green = 0xffff;
-      graphic[0].cmap[graphic[0].Npixels-1].blue = 0;
+      graphic[0].cmap[NANValue].red = 0;
+      graphic[0].cmap[NANValue].green = 0xffff;
+      graphic[0].cmap[NANValue].blue = 0;
   }  
 
@@ -73,9 +76,9 @@
 
   // red,green,blue are values in range 0x0000 to 0xffff
-  scale = 0xffff / (graphic[0].Npixels - 2);
+  scale = 0xffff / (float) (MaxValue - 1);
 
   /* greyscale */
   if ((!strcasecmp (name, "grayscale")) || (!strcasecmp (name, "greyscale"))) {
-    for (i = 0; i < graphic[0].Npixels - 1; i++) {  
+    for (i = 0; i < MaxValue; i++) {  
       SETVALUE (graphic[0].cmap[i].red,   0xffff - i*scale, 0, 0xffff);
       SETVALUE (graphic[0].cmap[i].green, 0xffff - i*scale, 0, 0xffff);
@@ -87,5 +90,5 @@
   /* -grayscale */
   if ((!strcasecmp (name, "-grayscale")) || (!strcasecmp (name, "-greyscale"))) {
-    for (i = 0; i < graphic[0].Npixels - 1; i++) {  
+    for (i = 0; i < MaxValue; i++) {  
       SETVALUE (graphic[0].cmap[i].red,   i*scale, 0, 0xffff);
       SETVALUE (graphic[0].cmap[i].green, i*scale, 0, 0xffff);
@@ -97,7 +100,7 @@
   /* heat */
   if (!strcasecmp (name, "Heat")) {
-    greenRef = 0.25*graphic[0].Npixels*scale*2.0;
-    blueRef  = 0.50*graphic[0].Npixels*scale*2.0;
-    for (i = 0; i < (int)(0.25*graphic[0].Npixels); i++) {  
+    greenRef = 0.25*MaxValue*scale*2.0;
+    blueRef  = 0.50*MaxValue*scale*2.0;
+    for (i = 0; i < (int)(0.25*MaxValue); i++) {  
       SETVALUE (graphic[0].cmap[i].red,   2*i*scale, 0, 0xffff);
       graphic[0].cmap[i].green = 0;
@@ -105,5 +108,5 @@
       graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
     }
-    for (i = 0.25*graphic[0].Npixels; i < 0.50*graphic[0].Npixels; i++) {  
+    for (i = 0.25*MaxValue; i < 0.50*MaxValue; i++) {  
       SETVALUE (graphic[0].cmap[i].red,   2*i*scale,            0, 0xffff);
       SETVALUE (graphic[0].cmap[i].green, 2*i*scale - greenRef, 0, 0xffff);
@@ -111,5 +114,5 @@
       graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
     }
-    for (i = (int)(0.50*graphic[0].Npixels); i < (int)(0.75*graphic[0].Npixels); i++) {  
+    for (i = (int)(0.50*MaxValue); i < (int)(0.75*MaxValue); i++) {  
       graphic[0].cmap[i].red = 0xffff;
       SETVALUE (graphic[0].cmap[i].green, 2*i*scale - greenRef, 0, 0xffff);
@@ -117,5 +120,5 @@
       graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
     }
-    for (i = (int)(0.75*graphic[0].Npixels); i < graphic[0].Npixels - 1; i++) {  
+    for (i = (int)(0.75*MaxValue); i < MaxValue; i++) {  
       graphic[0].cmap[i].red   = 0xffff;
       graphic[0].cmap[i].green = 0xffff;
@@ -127,8 +130,8 @@
   /* rainbow */
   if (!strcasecmp (name, "Rainbow")) {
-    redRef   = 0.25*graphic[0].Npixels*scale*4.0;
-    greenRef = 0.50*graphic[0].Npixels*scale*4.0;
-    blueRef  = 0.50*graphic[0].Npixels*scale*4.0;
-    for (i = 0; i < (int)(0.25*graphic[0].Npixels); i++) {  
+    redRef   = 0.25*MaxValue*scale*4.0;
+    greenRef = 0.50*MaxValue*scale*4.0;
+    blueRef  = 0.50*MaxValue*scale*4.0;
+    for (i = 0; i < (int)(0.25*MaxValue); i++) {  
       graphic[0].cmap[i].red   = 0;
       graphic[0].cmap[i].green = 0;
@@ -136,5 +139,5 @@
       graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
     }
-    for (i = (int)(0.25*graphic[0].Npixels); i < (int)(0.50*graphic[0].Npixels); i++) {  
+    for (i = (int)(0.25*MaxValue); i < (int)(0.50*MaxValue); i++) {  
       SETVALUE (graphic[0].cmap[i].red,   4*i*scale - redRef,  0, 0xffff);
       graphic[0].cmap[i].green = 0;
@@ -142,5 +145,5 @@
       graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
     }
-    for (i = (int)(0.50*graphic[0].Npixels); i < (int)(0.75*graphic[0].Npixels); i++) {  
+    for (i = (int)(0.50*MaxValue); i < (int)(0.75*MaxValue); i++) {  
       graphic[0].cmap[i].red  = 0xffff;
       SETVALUE (graphic[0].cmap[i].green,  4*i*scale - greenRef, 0, 0xffff);
@@ -148,9 +151,15 @@
       graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
     }
-    blueRef  = 0.75*graphic[0].Npixels*scale*4.0;
-    for (i = (int)(0.75*graphic[0].Npixels); i < graphic[0].Npixels - 1; i++) {  
+    // blue = blueScale * i + blueRef
+    // blue = (0xffff - 0.0)*(i - MinValue) / (MaxValue - MinValue) + 0.0
+    // blue = i * 0xffff / (MaxValue - MinValue) - 0xffff * MinValue / (MaxValue - MinValue)
+    float iMin = (int)(0.75*MaxValue);
+    float iMax = MaxValue - 1;
+    float dB = 0xffff / (iMax - iMin);
+    float oB = -0xffff * iMin / (iMax - iMin);
+    for (i = iMin; i < MaxValue; i++) {  
       graphic[0].cmap[i].red   = 0xffff;
       graphic[0].cmap[i].green = 0xffff;
-      SETVALUE (graphic[0].cmap[i].blue,  4*i*scale - blueRef, 0, 0xffff);
+      SETVALUE (graphic[0].cmap[i].blue,  i*dB + oB, 0, 0xffff);
       graphic[0].cmap[i].flags = DoRed | DoGreen | DoBlue;
     }
