Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/include/constants.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/include/constants.h	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/include/constants.h	(revision 37759)
@@ -10,5 +10,5 @@
  | PointerMotionMask)
 
-# define NCHANNELS 3
+# define NCHANNELS 10
 # define NPIXELS_DYNAMIC 128
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/include/structures.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/include/structures.h	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/include/structures.h	(revision 37759)
@@ -217,4 +217,5 @@
   KapaImageChannel *image;
   KapaImageChannel channel[NCHANNELS];
+  int currentChannel;
 } KapaImageWidget;
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/CreatePicture.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/CreatePicture.c	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/CreatePicture.c	(revision 37759)
@@ -40,7 +40,12 @@
 
   case 24:
+    
+    extra = 4 - (image[0].wide.dx * 3) % 4;
+    REALLOCATE (image[0].wide.data, char, image[0].wide.dy*(3*image[0].wide.dx+extra));
+
+    extra = 4 - (image[0].zoom.dx * 3) % 4;
+    REALLOCATE (image[0].zoom.data, char, image[0].zoom.dy*(3*image[0].zoom.dx+extra));
+
     extra = 4 - (image[0].picture.dx * 3) % 4;
-    REALLOCATE (image[0].wide.data, char, image[0].wide.dy*(3*image[0].wide.dx+extra));
-    REALLOCATE (image[0].zoom.data, char, image[0].zoom.dy*(3*image[0].zoom.dx+extra));
     REALLOCATE (image[0].picture.data, char, image[0].picture.dy*(3*image[0].picture.dx+extra));
     c = (unsigned char *) image[0].picture.data;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/Image.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/Image.c	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/Image.c	(revision 37759)
@@ -34,5 +34,7 @@
     InitImageChannel (&image[0].channel[i]);
   }
-  image[0].image = &image[0].channel[0];
+
+  image[0].currentChannel = 0;
+  image[0].image = &image[0].channel[image[0].currentChannel];
 
   image[0].nPixels = 0;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/InterpretKeys.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/InterpretKeys.c	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/InterpretKeys.c	(revision 37759)
@@ -89,24 +89,23 @@
   switch (keysym) {
 
-    case XK_F1:
-      image[0].image = &image[0].channel[0];
-      SetColorScale (graphic, image);
-      Remap (graphic, image);
-      Reorient (graphic, image, image[0].picture.Xc, image[0].picture.Yc, 0);
+# define SET_CHANNEL_CASE(NCHAN) \
+    case XK_F##NCHAN: \
+      image[0].image = &image[0].channel[NCHAN-1]; \
+      SetColorScale (graphic, image); \
+      Remap (graphic, image); \
+      Reorient (graphic, image, image[0].picture.Xc, image[0].picture.Yc, 0); \
       break;
 
-    case XK_F2:
-      image[0].image = &image[0].channel[1];
-      SetColorScale (graphic, image);
-      Remap (graphic, image);
-      Reorient (graphic, image, image[0].picture.Xc, image[0].picture.Yc, 0);
-      break;
-
-    case XK_F3:
-      image[0].image = &image[0].channel[2];
-      SetColorScale (graphic, image);
-      Remap (graphic, image);
-      Reorient (graphic, image, image[0].picture.Xc, image[0].picture.Yc, 0);
-      break;
+    // the number of entries here must match the value of NCHANNELS in contants.h
+    SET_CHANNEL_CASE(1);
+    SET_CHANNEL_CASE(2);
+    SET_CHANNEL_CASE(3);
+    SET_CHANNEL_CASE(4);
+    SET_CHANNEL_CASE(5);
+    SET_CHANNEL_CASE(6);
+    SET_CHANNEL_CASE(7);
+    SET_CHANNEL_CASE(8);
+    SET_CHANNEL_CASE(9);
+    SET_CHANNEL_CASE(10);
 
     case XK_KP_Home:
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/LoadPicture.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/LoadPicture.c	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/LoadPicture.c	(revision 37759)
@@ -55,9 +55,11 @@
   // choose expand for wide to guarantee we fit:
   wx = MAX ((header.Naxis[0] / (float) image[0].wide.dx), (header.Naxis[1] / (float) image[0].wide.dy));
-  if (wx > 1.0) {
-    image[0].wide.expand = -wx;
-  } else {
-    image[0].wide.expand = 1.0 / wx;
-  }    
+
+  // -4.002 -> -5
+  // image[0].wide.expand = (wx > 1.0) ? ceil (-wx) : floor (1.0 / wx);
+  image[0].wide.expand = (wx > 1.0) ? floor (-wx) : ceil (1.0 / wx);
+  fprintf (stderr, "%d : %f\n", image[0].wide.expand, wx);
+  // image[0].wide.expand = (wx > 1.0) ? ceil (-wx) : floor (1.0 / wx);
+
   image[0].wide.Xc = 0.5*header.Naxis[0];
   image[0].wide.Yc = 0.5*header.Naxis[1];
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/Remap32.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/Remap32.c	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/Remap32.c	(revision 37759)
@@ -37,5 +37,4 @@
   swap_client = 0;
 # endif  
-
   swap_server = ImageByteOrder (graphic[0].display);
   swap_bytes = !(swap_client ^ swap_server);
@@ -91,8 +90,15 @@
   in_pix  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];
 
+  unsigned short *in_base = image[0].pixmap;
+  OUT_TYPE *out_base = (OUT_TYPE *) picture[0].data;
+
+  int Ninmax = matrix[0].Naxis[0] * matrix[0].Naxis[1];
+  int Noutmax = picture[0].dx * picture[0].dy;
+
   /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
 
   /**** fill in bottom area ****/
   for (j = 0; j < dx*j_start; j++, out_pix++) {
+    myAssert (out_pix - out_base < Noutmax, "too far out");
     *out_pix = back;
   }
@@ -105,4 +111,5 @@
       out_pix2 = out_pix + jj*dx;
       for (i = 0; i < i_start; i++, out_pix2++) {
+	myAssert (out_pix2 - out_base < Noutmax, "too far out");
 	*out_pix2 = back;
       }
@@ -114,12 +121,16 @@
     if (expand_out == 1) {
       for (i = i_start; i < i_end; i++, in_pix2 += inDX*expand_in, out_pix++) {
+	myAssert (out_pix - out_base < Noutmax, "too far out");
+	myAssert (in_pix2 - in_base < Ninmax, "too far in");
 	*out_pix = pixel[*in_pix2];
       }
     } else {
       for (i = i_start; i < i_end; i+= expand_out, in_pix2 += inDX, out_pix+= expand_out) { 
+	myAssert (in_pix2 - in_base < Ninmax, "too far in");
 	pixvalue = pixel[*in_pix2];
 	out_pix2 = out_pix;
 	for (jj = 0; (jj < expand_out) && (j + jj < dy); jj++, out_pix2+=(dx-expand_out)) {
 	  for (ii = 0; ii < expand_out; ii++, out_pix2++) {
+	    myAssert (out_pix2 - out_base < Noutmax, "too far out");
 	    *out_pix2 = pixvalue;
 	  }
@@ -133,4 +144,5 @@
       out_pix2 = out_pix + jj*dx;
       for (i = i_end; i < dx; i++, out_pix2++) {
+	myAssert (out_pix2 - out_base < Noutmax, "too far out");
 	*out_pix2 = back;
       }
@@ -142,4 +154,5 @@
   for (j = 0; j < dy - j_end; j++) {
     for (i = 0; i < dx; i++, out_pix ++) {
+      myAssert (out_pix - out_base < Noutmax, "too far out");
       *out_pix = back;
     }
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/SetChannel.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/SetChannel.c	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/SetChannel.c	(revision 37759)
@@ -21,5 +21,6 @@
   if (Nchannel >= NCHANNELS) return (TRUE);
   
-  image[0].image = &image[0].channel[Nchannel];
+  image[0].currentChannel = Nchannel;
+  image[0].image = &image[0].channel[image[0].currentChannel];
   SetColorScale (graphic, image);
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/UpdateStatusBox.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/UpdateStatusBox.c	(revision 37758)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/kapa2/src/UpdateStatusBox.c	(revision 37759)
@@ -20,6 +20,6 @@
   
     bzero (line, 100);
-    snprintf (line, 100, "(%d x %d) @ %d                                         ", 
-	     image[0].picture.dx, image[0].picture.dy, image[0].picture.expand); 
+    snprintf (line, 100, "(%d x %d) @ %d   ch: %d                                     ", 
+	      image[0].picture.dx, image[0].picture.dy, image[0].picture.expand, image[0].currentChannel+1); 
     XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc, 
 		 image[0].text_x + PAD1, image[0].text_y + 4*textpad + 4*PAD1, line, 25);
