Index: trunk/Ohana/src/kapa2/include/constants.h
===================================================================
--- trunk/Ohana/src/kapa2/include/constants.h	(revision 37412)
+++ trunk/Ohana/src/kapa2/include/constants.h	(revision 37807)
@@ -10,5 +10,5 @@
  | PointerMotionMask)
 
-# define NCHANNELS 3
+# define NCHANNELS 10
 # define NPIXELS_DYNAMIC 128
 
Index: trunk/Ohana/src/kapa2/include/structures.h
===================================================================
--- trunk/Ohana/src/kapa2/include/structures.h	(revision 37412)
+++ trunk/Ohana/src/kapa2/include/structures.h	(revision 37807)
@@ -217,4 +217,5 @@
   KapaImageChannel *image;
   KapaImageChannel channel[NCHANNELS];
+  int currentChannel;
 } KapaImageWidget;
 
Index: trunk/Ohana/src/kapa2/src/CreatePicture.c
===================================================================
--- trunk/Ohana/src/kapa2/src/CreatePicture.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/CreatePicture.c	(revision 37807)
@@ -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: trunk/Ohana/src/kapa2/src/CursorOps.c
===================================================================
--- trunk/Ohana/src/kapa2/src/CursorOps.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/CursorOps.c	(revision 37807)
@@ -100,8 +100,12 @@
   // Sx, Sy are the screen coordinates of the Ix,Iy pixel
   Image_to_Picture (&Sx, &Sy, Ix, Iy, picture);
+  
+  // Sx,Sy should be forced into the pixel
+  int Sxi = Sx + 0.5;
+  int Syi = Sy + 0.5;
 
   // i_start, j_start are now limited to valid screen coordinates
-  *i_start = MIN (MAX (Sx, 0), picture[0].dx);
-  *j_start = MIN (MAX (Sy, 0), picture[0].dy);
+  *i_start = MIN (MAX (Sxi, 0), picture[0].dx);
+  *j_start = MIN (MAX (Syi, 0), picture[0].dy);
 }
   
@@ -127,14 +131,19 @@
   Iy = MIN (MAX (Iy, 0), matrix[0].Naxis[1]);
 
-  // I_end, J_end are the last displayed image pixel 
-  // *I_end = Ix;
-  // *J_end = Iy;
-
   // Sx, Sy are the screen coordinates of the Ix,Iy pixel
   Image_to_Picture (&Sx, &Sy, Ix, Iy, picture);
 
+  // Sx,Sy should be forced into the pixel
+  int Sxi = Sx + 0.5;
+  int Syi = Sy + 0.5;
+
+  // double IxTest, IyTest;
+  // Picture_to_Image (&IxTest, &IyTest, Sx, Sy, picture);
+  // fprintf (stderr, "dx,dy: %d,%d : %f,%f : %f,%f : %f:%f\n", picture[0].dx, picture[0].dy, Ix, Iy, Sx, Sy, IxTest, IyTest);
+
   // i_start, j_start are now limited to valid screen coordinates
-  *i_end = MIN (MAX (Sx, 0), picture[0].dx);
-  *j_end = MIN (MAX (Sy, 0), picture[0].dy);
+  // XXX: i_end, j_end *should* be the last valid screen pixel plus 1
+  *i_end = MIN (MAX (Sxi, 0), picture[0].dx);
+  *j_end = MIN (MAX (Syi, 0), picture[0].dy);
 
   // round off error can leave us with a small number of extra pixels here.
Index: trunk/Ohana/src/kapa2/src/Graphs.c
===================================================================
--- trunk/Ohana/src/kapa2/src/Graphs.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/Graphs.c	(revision 37807)
@@ -57,12 +57,5 @@
   graph[0].data.flipnorth = FALSE;	// +North = +Y by default
 
-  graph[0].data.coords.pc1_1 = graph[0].data.coords.pc2_2 = 1.0;
-  graph[0].data.coords.pc1_2 = graph[0].data.coords.pc2_1 = 0.0;
-  graph[0].data.coords.crval1 = 0.0;
-  graph[0].data.coords.crval2 = 0.0;
-  graph[0].data.coords.crpix1 = 0.0;
-  graph[0].data.coords.crpix2 = 0.0;
-  graph[0].data.coords.cdelt1 = graph[0].data.coords.cdelt2 = 1.0;
-  strcpy (graph[0].data.coords.ctype, "DEC--LIN");
+  InitCoords (&graph[0].data.coords, "DEC--LIN");
   strcpy (graph[0].data.axis, "2222");
   strcpy (graph[0].data.ticks, "2222");
Index: trunk/Ohana/src/kapa2/src/Image.c
===================================================================
--- trunk/Ohana/src/kapa2/src/Image.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/Image.c	(revision 37807)
@@ -11,13 +11,5 @@
   channel->slope = 1;
 
-  channel->coords.pc1_1 = channel->coords.pc2_2 = 1.0;
-  channel->coords.pc1_2 = channel->coords.pc2_1 = 0.0;
-  channel->coords.crval1 = 0.0;
-  channel->coords.crval2 = 0.0;
-  channel->coords.crpix1 = 0.0;
-  channel->coords.crpix2 = 0.0;
-  channel->coords.cdelt1 = channel->coords.cdelt2 = 1.0;
-  strcpy (channel->coords.ctype, "DEC--LIN");
-  channel->coords.Npolyterms = 0;
+  InitCoords (&channel->coords, "DEC--LIN");
 
   channel->matrix.datasize = 0; /* a flag to show there is no data in the matrix */
@@ -42,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: trunk/Ohana/src/kapa2/src/InterpretKeys.c
===================================================================
--- trunk/Ohana/src/kapa2/src/InterpretKeys.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/InterpretKeys.c	(revision 37807)
@@ -90,24 +90,27 @@
   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].currentChannel = NCHAN-1; \
+      image[0].image = &image[0].channel[NCHAN-1]; \
+      SetColorScale (graphic, image); \
+      Reorient (graphic, image, image[0].picture.Xc, image[0].picture.Yc, 0); \
+      Screen_to_Image (&X, &Y, (double)(event[0].x + 0.5), (double)(event[0].y + 0.5), &image[0].picture); \
+      UpdateStatusBox (graphic, image, X, Y, 0.0, 1); \
       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;
+//    Remap (graphic, image); 
 
-    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: trunk/Ohana/src/kapa2/src/LoadPicture.c
===================================================================
--- trunk/Ohana/src/kapa2/src/LoadPicture.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/LoadPicture.c	(revision 37807)
@@ -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: trunk/Ohana/src/kapa2/src/Reconfig.c
===================================================================
--- trunk/Ohana/src/kapa2/src/Reconfig.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/Reconfig.c	(revision 37807)
@@ -15,5 +15,9 @@
 
   // if the new size is the same as the old size, do nothing.
-  if ((graphic->dx == NX) && (graphic->dy == NY)) return (TRUE);
+  if ((graphic->dx == NX) && (graphic->dy == NY)) {
+    if (USE_XWINDOW) XClearWindow (graphic->display, graphic->window);
+    Refresh ();
+    return (TRUE);
+  }
 
   // set the new window size
Index: trunk/Ohana/src/kapa2/src/Remap32.c
===================================================================
--- trunk/Ohana/src/kapa2/src/Remap32.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/Remap32.c	(revision 37807)
@@ -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: trunk/Ohana/src/kapa2/src/SetChannel.c
===================================================================
--- trunk/Ohana/src/kapa2/src/SetChannel.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/SetChannel.c	(revision 37807)
@@ -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);
 
@@ -61,5 +62,5 @@
 
   Refresh ();
-  XFlush (graphic[0].display);
+  if (USE_XWINDOW) XFlush (graphic[0].display);
 
   return (TRUE);
Index: trunk/Ohana/src/kapa2/src/UpdateStatusBox.c
===================================================================
--- trunk/Ohana/src/kapa2/src/UpdateStatusBox.c	(revision 37412)
+++ trunk/Ohana/src/kapa2/src/UpdateStatusBox.c	(revision 37807)
@@ -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);
