Index: /trunk/Ohana/src/kapa2/Makefile
===================================================================
--- /trunk/Ohana/src/kapa2/Makefile	(revision 16255)
+++ /trunk/Ohana/src/kapa2/Makefile	(revision 16256)
@@ -61,6 +61,4 @@
 $(SRC)/DrawButton.$(ARCH).o               $(SRC)/DrawBitmap.$(ARCH).o         \
 $(SRC)/hh_hms.$(ARCH).o	                  $(SRC)/CreateZoom.$(ARCH).o         \
-$(SRC)/CreateZoom8.$(ARCH).o              $(SRC)/CreateZoom16.$(ARCH).o       \
-$(SRC)/CreateZoom24.$(ARCH).o             $(SRC)/CreateZoom32.$(ARCH).o       \
 $(SRC)/CreatePicture.$(ARCH).o            $(SRC)/CreateColorbar.$(ARCH).o     \
 $(SRC)/MakeColormap.$(ARCH).o		  $(SRC)/SetColormap.$(ARCH).o        \
@@ -81,4 +79,7 @@
 $(SRC)/ColorHistogram.$(ARCH).o           
 
+#$(SRC)/CreateZoom8.$(ARCH).o              $(SRC)/CreateZoom16.$(ARCH).o       \
+#$(SRC)/CreateZoom24.$(ARCH).o             $(SRC)/CreateZoom32.$(ARCH).o       \
+
 OBJ  =  $(KAPA)
 
Index: /trunk/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /trunk/Ohana/src/kapa2/include/prototypes.h	(revision 16255)
+++ /trunk/Ohana/src/kapa2/include/prototypes.h	(revision 16256)
@@ -146,8 +146,8 @@
 int           SetColorScale3D     PROTO((Graphic *graphic, KapaImageWidget *image));
 void          Remap               PROTO((Graphic *graphic, KapaImageWidget *image));
-void          Remap8              PROTO((Graphic *graphic, KapaImageWidget *image, Matrix *matrix));
-void          Remap16             PROTO((Graphic *graphic, KapaImageWidget *image, Matrix *matrix));
-void          Remap24             PROTO((Graphic *graphic, KapaImageWidget *image, Matrix *matrix));
-void          Remap32             PROTO((Graphic *graphic, KapaImageWidget *image, Matrix *matrix));
+void          Remap8              PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
+void          Remap16             PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
+void          Remap24             PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
+void          Remap32             PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
 int           LoadPicture         PROTO((int sock));
 
@@ -171,6 +171,10 @@
 void          PaintTickmarks      PROTO((Graphic *graphic, KapaImageWidget *image));
 
+void          Picture_to_Image    PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
 void          Screen_to_Image     PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
+void          Image_to_Picture    PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
 void          Image_to_Screen     PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
+void          Picture_Lower 	  PROTO((int *i_start, int *j_start, Matrix *matrix, Picture *picture));
+void          Picture_Upper 	  PROTO((int *i_end, int *j_end, Matrix *matrix, Picture *picture));
 
 void          DragColorbar        PROTO((Graphic *graphic, KapaImageWidget *image, XButtonEvent *mouse_event));
@@ -214,11 +218,11 @@
 void 	      CreatePicture (KapaImageWidget *image, Graphic *graphic);
 void 	      CreateColorbar (KapaImageWidget *image, Graphic *graphic);
-void 	      CreateZoom (KapaImageWidget *image, Graphic *graphic, double x, double y);
+void 	      CreateZoom (Graphic *graphic, KapaImageWidget *image);
 void 	      UpdateStatusBox (Graphic *graphic, KapaImageWidget *image, double x, double y, double z, int mode);
 
-void 	      CreateZoom8  (KapaImageWidget *image, Graphic *graphic, double x, double y);
-void 	      CreateZoom16 (KapaImageWidget *image, Graphic *graphic, double x, double y);
-void 	      CreateZoom24 (KapaImageWidget *image, Graphic *graphic, double x, double y);
-void 	      CreateZoom32 (KapaImageWidget *image, Graphic *graphic, double x, double y);
+// void 	      CreateZoom8  (KapaImageWidget *image, Graphic *graphic, double x, double y);
+// void 	      CreateZoom16 (KapaImageWidget *image, Graphic *graphic, double x, double y);
+// void 	      CreateZoom24 (KapaImageWidget *image, Graphic *graphic, double x, double y);
+// void 	      CreateZoom32 (KapaImageWidget *image, Graphic *graphic, double x, double y);
 
 int  	      GetActiveSocket ();
Index: /trunk/Ohana/src/kapa2/include/structures.h
===================================================================
--- /trunk/Ohana/src/kapa2/include/structures.h	(revision 16255)
+++ /trunk/Ohana/src/kapa2/include/structures.h	(revision 16256)
@@ -94,6 +94,5 @@
   int      DX, DY;	      // size of displayed picture (must be updated with new images...)
   int      expand;	      // zoomscale
-  double   X,  Y;	      // offset of image center
-  double   Xo, Yo;	      // reference image pixel (picture has X - Xo at center)
+  double   X,  Y;	      // center of image in picture
   XImage  *pix;
   char    *data;
Index: /trunk/Ohana/src/kapa2/src/ButtonFunctions.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/ButtonFunctions.c	(revision 16256)
@@ -14,5 +14,5 @@
   SetColorScale (graphic, image);
   Remap (graphic, image);
-  CreateZoom (image, graphic, 0, 0); 
+  CreateZoom (graphic, image); 
   Refresh ();
   XFlush (graphic[0].display);
@@ -25,5 +25,5 @@
   SetColorScale (graphic, image);
   Remap (graphic, image);
-  CreateZoom (image, graphic, 0, 0); 
+  CreateZoom (graphic, image); 
   Refresh ();
   XFlush (graphic[0].display);
@@ -36,5 +36,5 @@
   SetColorScale (graphic, image);
   Remap (graphic, image);
-  CreateZoom (image, graphic, 0, 0); 
+  CreateZoom (graphic, image); 
   Refresh ();
   XFlush (graphic[0].display);
@@ -44,6 +44,6 @@
 int Recenter (Graphic *graphic, KapaImageWidget *image) {
 
-  image[0].picture.X = 0;
-  image[0].picture.Y = 0;
+  image[0].picture.X = 0.5*image[0].image[0].matrix.Naxis[0];
+  image[0].picture.Y = 0.5*image[0].image[0].matrix.Naxis[1];
  
   Remap (graphic, image);
@@ -66,6 +66,6 @@
 int RecenterRescale (Graphic *graphic, KapaImageWidget *image) {
 
-  image[0].picture.X = 0;
-  image[0].picture.Y = 0;
+  image[0].picture.X = 0.5*image[0].image[0].matrix.Naxis[0];
+  image[0].picture.Y = 0.5*image[0].image[0].matrix.Naxis[1];
   image[0].picture.expand = 1;
  
Index: /trunk/Ohana/src/kapa2/src/Center.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Center.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/Center.c	(revision 16256)
@@ -16,6 +16,6 @@
   if (image == NULL) return (TRUE);
 
-  image[0].picture.Xo = 0.5*image[0].image[0].matrix.Naxis[0] - X;
-  image[0].picture.Yo = 0.5*image[0].image[0].matrix.Naxis[1] - Y;
+  image[0].picture.X = X;
+  image[0].picture.Y = Y;
   if ((zoom != 0) && (zoom != -1)) {
     image[0].picture.expand = zoom;
Index: /trunk/Ohana/src/kapa2/src/CreatePicture.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CreatePicture.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/CreatePicture.c	(revision 16256)
@@ -55,6 +55,6 @@
 
   case 32:
-    REALLOCATE (image[0].picture.data, char, (4*image[0].picture.dx*image[0].picture.dy + 32));
-    memset (image[0].picture.data, 0xbd, 4*image[0].picture.dx*image[0].picture.dy + 30);
+    REALLOCATE (image[0].zoom.data,    char, 4*image[0].zoom.dx*image[0].zoom.dy);
+    REALLOCATE (image[0].picture.data, char, 4*image[0].picture.dx*image[0].picture.dy);
     l = (unsigned int *) image[0].picture.data;
     for (i = 0; i < (image[0].picture.dx*image[0].picture.dy); i++, l++)
Index: /trunk/Ohana/src/kapa2/src/CreateZoom.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CreateZoom.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/CreateZoom.c	(revision 16256)
@@ -1,21 +1,18 @@
 # include "Ximage.h"
 
-void CreateZoom (KapaImageWidget *image, Graphic *graphic, double x, double y) {
+void CreateZoom (Graphic *graphic, KapaImageWidget *image) {
 
   switch (graphic[0].Nbits) {
   case 8:
-    CreateZoom8 (image, graphic, x, y);
+    Remap8  (graphic, image, &image->zoom, &image->image->matrix);
     break;
-
   case 16:
-    CreateZoom16 (image, graphic, x, y);
+    Remap16 (graphic, image, &image->zoom, &image->image->matrix);
     break;
-
   case 24:
-    CreateZoom24 (image, graphic, x, y);
+    Remap24 (graphic, image, &image->zoom, &image->image->matrix);
     break;
-
   case 32:
-    CreateZoom32 (image, graphic, x, y);
+    Remap32 (graphic, image, &image->zoom, &image->image->matrix);
     break;
   }    
Index: /trunk/Ohana/src/kapa2/src/CreateZoom16.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CreateZoom16.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/CreateZoom16.c	(revision 16256)
@@ -1,15 +1,3 @@
 # include "Ximage.h"
-# define FRAC(a) ((a) - (int)(a))
-
-static float slope = 1.0;
-static float start = 0.0;
-static int MaxValue = 255;
-
-// XXX inline this if needed
-static int PixelLookup(float value) {
-  int out;
-  out = MIN (MAX (slope * value - start, 0), MaxValue);
-  return (out);
-}
 
 void CreateZoom16 (KapaImageWidget *image, Graphic *graphic, double x, double y) {
Index: /trunk/Ohana/src/kapa2/src/CreateZoom24.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CreateZoom24.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/CreateZoom24.c	(revision 16256)
@@ -1,15 +1,3 @@
 # include "Ximage.h"
-# define FRAC(a) ((a) - (int)(a))
-
-static float slope = 1.0;
-static float start = 0.0;
-static int MaxValue = 255;
-
-// XXX inline this if needed
-static int PixelLookup(float value) {
-  int out;
-  out = MIN (MAX (slope * value - start, 0), MaxValue);
-  return (out);
-}
 
 void CreateZoom24 (KapaImageWidget *image, Graphic *graphic, double x, double y) {
Index: /trunk/Ohana/src/kapa2/src/CreateZoom32.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CreateZoom32.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/CreateZoom32.c	(revision 16256)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# define FRAC(a) ((a) - (int)(a))
 
 void CreateZoom32 (KapaImageWidget *image, Graphic *graphic, double x, double y) {
@@ -8,5 +7,5 @@
   int dropback;  /* this is a bit of a kludge... */
   int dx, dy, DX, DY;
-  double expand, Sx, Sy, Ix, Iy;
+  double expand, Ix, Iy;
   int expand_in, expand_out;
   unsigned int *out_pix, *out_pix2;
@@ -50,31 +49,12 @@
   DY = image[0].image[0].matrix.Naxis[1];
   
-  // Ix, Iy are the image coordinates of the 0,0 screen pixel
-  Screen_to_Image (&Ix, &Iy, 0.0, 0.0, &image[0].picture);
+  // x, y are the closest lit screen pixel to 0,0
+  Picture_Lower (&i_start, &j_start, &image[0].image[0].matrix, &image[0].zoom);
 
-  // Ix, Iy are now limited to valid image coordinates
-  Ix = MIN (MAX (Ix, 0), DX);
-  Iy = MIN (MAX (Iy, 0), DY);
+  // x, y are the closest lit screen pixel to dx, dy
+  Picture_Upper (&i_end, &j_end, &image[0].image[0].matrix, &image[0].zoom);
 
-  // Sx, Sy are the screen coordinates of the Ix,Iy pixel
-  Image_to_Screen (&Sx, &Sy, Ix,  Iy,  &image[0].picture);
-
-  // i_start, j_start are now limited to valid screen coordinates
-  i_start = MIN (MAX (Sx, 0), dx);
-  j_start = MIN (MAX (Sy, 0), dy);
-
-  // Ix, Iy are the image coordinates of the 0,0 screen pixel
-  Screen_to_Image (&Ix, &Iy, dx, dy, &image[0].picture);
-
-  // Ix, Iy are now limited to valid image coordinates
-  Ix = MIN (MAX (Ix, 0), DX);
-  Iy = MIN (MAX (Iy, 0), DY);
-
-  // Sx, Sy are the screen coordinates of the Ix,Iy pixel
-  Image_to_Screen (&Sx, &Sy, Ix,  Iy,  &image[0].picture);
-
-  // i_end, j_end are now limited to valid screen coordinates
-  i_end = MIN (MAX (Sx, 0), dx);
-  j_end = MIN (MAX (Sy, 0), dy);
+  // Ix,Iy are the first displayed image pixel
+  Picture_to_Image (&Ix, &Iy, i_start, j_start, &image[0].zoom);
 
   dropback = expand_out - (i_end - i_start) % expand_out;
@@ -92,5 +72,4 @@
     return;
   } 
- 
 
   /**** fill in bottom area ****/
@@ -117,4 +96,5 @@
       }
     } else {
+      // equiv to : out_pix += (i_end - i_start) + [expand_out - (i_end - i_start) % expand_out]
       for (i = i_start; i < i_end; i+= expand_out, in_pix2++, out_pix+= expand_out) { 
 	pixvalue = pixel[*in_pix2];
@@ -137,13 +117,18 @@
     }
     out_pix += (dx - i_end);
+    assert (out_pix - (unsigned int *)image[0].zoom.data <= dx*dy);
   } 
+  assert (out_pix - (unsigned int *)image[0].zoom.data <= dx*dy);
   
   if ((j_end - j_start) % expand_out > 0)
     out_pix -= (expand_out - (j_end - j_start) % expand_out);
   
+  assert (out_pix - (unsigned int *)image[0].zoom.data <= dx*dy);
+
   /**** fill in top area ****/
   for (j = 0; (j < dx*(dy - j_end)) && (out_pix - (unsigned int *)image[0].zoom.data < dx*dy); j++, out_pix++) { 
     *out_pix = back;
   }
+  assert (out_pix - (unsigned int *)image[0].zoom.data <= dx*dy);
 
   image[0].zoom.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
Index: /trunk/Ohana/src/kapa2/src/CreateZoom8.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CreateZoom8.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/CreateZoom8.c	(revision 16256)
@@ -1,15 +1,3 @@
 # include "Ximage.h"
-# define FRAC(a) ((a) - (int)(a))
-
-static float slope = 1.0;
-static float start = 0.0;
-static int MaxValue = 255;
-
-// XXX inline this if needed
-static int PixelLookup(float value) {
-  int out;
-  out = MIN (MAX (slope * value - start, 0), MaxValue);
-  return (out);
-}
 
 void CreateZoom8 (KapaImageWidget *image, Graphic *graphic, double x, double y) {
Index: /trunk/Ohana/src/kapa2/src/CursorOps.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CursorOps.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/CursorOps.c	(revision 16256)
@@ -1,7 +1,8 @@
 # include "Ximage.h"
 
-void Screen_to_Image (double *x1, double *y1, double x2, double y2, Picture *picture) {
+// input coordinates are relative to the picture bounding box
+void Picture_to_Image (double *x1, double *y1, double x2, double y2, Picture *picture) {
 
-  double xs, ys, expand;
+  double expand;
 
   expand = 1.0;
@@ -12,16 +13,24 @@
   }
   
-  // pixel coordinates in picture frame
-  xs = x2 - picture[0].x;
-  ys = y2 - picture[0].y;
-
-  // picture[0].X,Y is the offset relative to picture[0].Xo,Yo
-  *x1 = expand*(xs - 0.5*picture[0].dx) + picture[0].Xo - picture[0].X;
-  *y1 = expand*(ys - 0.5*picture[0].dy) + picture[0].Yo - picture[0].Y;
+  // picture[0].X,Y is the image coordinate in the center of the picture
+  *x1 = expand*(x2 - 0.5*picture[0].dx) + picture[0].X;
+  *y1 = expand*(y2 - 0.5*picture[0].dy) + picture[0].Y;
 }
 
-void Image_to_Screen (double *x1, double *y1, double x2, double y2, Picture *picture) {
+// input coordinates are relative to the X window
+void Screen_to_Image (double *x1, double *y1, double x2, double y2, Picture *picture) {
 
-  double xs, ys, expand;
+  double xp, yp;
+
+  // pixel coordinates in picture frame
+  xp = x2 - picture[0].x;
+  yp = y2 - picture[0].y;
+
+  Picture_to_Image (x1, y1, xp, yp, picture);
+}
+
+void Image_to_Picture (double *x1, double *y1, double x2, double y2, Picture *picture) {
+
+  double expand;
 
   /* notice that here, expand is the reciprocal of the expand above */
@@ -34,10 +43,64 @@
   
   // pixel coordinates in picture frame
-  xs = expand*(x2 - picture[0].Xo + picture[0].X) + 0.5*picture[0].dx;
-  ys = expand*(y2 - picture[0].Yo + picture[0].Y) + 0.5*picture[0].dy;
+  *x1 = expand*(x2 - picture[0].X) + 0.5*picture[0].dx;
+  *y1 = expand*(y2 - picture[0].Y) + 0.5*picture[0].dy;
+}
+
+void Image_to_Screen (double *x1, double *y1, double x2, double y2, Picture *picture) {
+
+  double xp, yp;
+
+  Image_to_Picture (&xp, &yp, x2, y2, picture);
 
   // pixel coordinates in screen frame
-  *x1 = xs + picture[0].x;
-  *y1 = ys + picture[0].y;
+  *x1 = xp + picture[0].x;
+  *y1 = yp + picture[0].y;
 }
 
+// input coordinates are relative to the picture bounding box
+void Picture_Lower (int *i_start, int *j_start, Matrix *matrix, Picture *picture) {
+
+  double Ix, Iy, Sx, Sy;
+
+  // Ix, Iy are the image coordinates of the specified screen pixel
+  Picture_to_Image (&Ix, &Iy, 0.0, 0.0, picture);
+
+  // Ix, Iy are now limited to valid image coordinates
+  Ix = MIN (MAX (Ix, 0), matrix[0].Naxis[0]);
+  Iy = MIN (MAX (Iy, 0), matrix[0].Naxis[1]);
+
+  // round up to nearest pixel boundary
+  if (Ix > (int)(Ix)) Ix = (int)(Ix) + 1;
+  if (Iy > (int)(Iy)) Iy = (int)(Iy) + 1;
+
+  // Sx, Sy are the screen coordinates of the Ix,Iy pixel
+  Image_to_Picture (&Sx, &Sy, Ix, Iy, picture);
+
+  // 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);
+}
+  
+// input coordinates are relative to the picture bounding box
+void Picture_Upper (int *i_end, int *j_end, Matrix *matrix, Picture *picture) {
+
+  double Ix, Iy, Sx, Sy;
+
+  // Ix, Iy are the image coordinates of the specified screen pixel
+  Picture_to_Image (&Ix, &Iy, picture[0].dx, picture[0].dy, picture);
+
+  // Ix, Iy are now limited to valid image coordinates
+  Ix = MIN (MAX (Ix, 0), matrix[0].Naxis[0]);
+  Iy = MIN (MAX (Iy, 0), matrix[0].Naxis[1]);
+
+  // round down to nearest pixel boundary
+  if (Ix > (int)(Ix)) Ix = (int)(Ix);
+  if (Iy > (int)(Iy)) Iy = (int)(Iy);
+
+  // Sx, Sy are the screen coordinates of the Ix,Iy pixel
+  Image_to_Picture (&Sx, &Sy, Ix, Iy, picture);
+
+  // 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);
+}
Index: /trunk/Ohana/src/kapa2/src/Image.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Image.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/Image.c	(revision 16256)
@@ -50,18 +50,12 @@
   image[0].picture.X      = 0.0;
   image[0].picture.Y      = 0.0;
-  image[0].picture.Xo     = 0.0;
-  image[0].picture.Yo     = 0.0;
   image[0].picture.expand = 1;
 
   image[0].zoom.X      	  = 0.0;
   image[0].zoom.Y      	  = 0.0;
-  image[0].zoom.Xo     	  = 0.0;
-  image[0].zoom.Yo     	  = 0.0;
   image[0].zoom.expand 	  = +5;
 
   image[0].wide.X      	  = 0.0;
   image[0].wide.Y      	  = 0.0;
-  image[0].wide.Xo     	  = 0.0;
-  image[0].wide.Yo     	  = 0.0;
   image[0].wide.expand 	  = -5;
 
Index: /trunk/Ohana/src/kapa2/src/InterpretKeys.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/InterpretKeys.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/InterpretKeys.c	(revision 16256)
@@ -118,12 +118,10 @@
     case XK_End:
       image[0].picture.expand = 1;
-      Reorient (graphic, image, 0, 0, 0);
+      Reorient (graphic, image, 0.5*image[0].image[0].matrix.Naxis[0], 0.5*image[0].image[0].matrix.Naxis[1], 0);
       break;
     case XK_KP_Enter:
     case XK_KP_Begin:
     case XK_Return:
-      Screen_to_Image (&X, &Y, (double)event[0].x, (double)event[0].y, &image[0].picture);
-      X = 0.5*image[0].picture.DX - X;
-      Y = 0.5*image[0].picture.DY - Y;
+      Screen_to_Image (&X, &Y, event[0].x + 0.5, event[0].y + 0.5, &image[0].picture);
       Reorient (graphic, image, X, Y, 0);
       break;
Index: /trunk/Ohana/src/kapa2/src/InterpretPresses.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/InterpretPresses.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/InterpretPresses.c	(revision 16256)
@@ -34,5 +34,5 @@
       if (event[0].x > image[0].picture.x + image[0].picture.dx) goto skip_cursor;
       if (event[0].y > image[0].picture.y + image[0].picture.dy) goto skip_cursor;
-      Screen_to_Image (&X, &Y, (double)event[0].x, (double)event[0].y, &image[0].picture);
+      Screen_to_Image (&X, &Y, event[0].x + 0.5, event[0].y + 0.5, &image[0].picture);
 
       XY_to_RD (&R, &D, X, Y, &image[0].image[0].coords);
@@ -101,7 +101,5 @@
   double X, Y;
 
-  Screen_to_Image (&X, &Y, (double)mouse_event[0].x, (double)mouse_event[0].y, &image[0].picture);
-  X = 0.5*image[0].image[0].matrix.Naxis[0] - X;
-  Y = 0.5*image[0].image[0].matrix.Naxis[1] - Y;
+  Screen_to_Image (&X, &Y, mouse_event[0].x + 0.5, mouse_event[0].y + 0.5, &image[0].picture);
 
   switch (mouse_event[0].button) {
Index: /trunk/Ohana/src/kapa2/src/JPEGit24.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/JPEGit24.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/JPEGit24.c	(revision 16256)
@@ -8,6 +8,4 @@
 // XXX this currently writes out the jpeg for just the active image
 int JPEGit24 (int sock) {
-
-# if (0)
 
   struct jpeg_compress_struct cinfo;
@@ -26,5 +24,5 @@
   int quality;
   int expand_in, expand_out;
-  double expand, Rx, Ry, X, Y;
+  double expand, Ix, Iy;
   unsigned char *out_pix;
   unsigned short *in_pix, *in_pix_ref;
@@ -95,30 +93,12 @@
   DY = image[0].image[0].matrix.Naxis[1];
 
-  /* X,Y are the image coordinates of the first image pixel */
-  X = MAX(0.5*(DX - dx*expand) - image[0].X, 0);
-  if ((int)X != X) 
-    X = (int) X + 1;
-  else 
-    X = (int) X;
-  Y = MAX(0.5*(DY - dy*expand) - image[0].Y, 0);
-  if ((int)Y != Y) 
-    Y = (int) Y + 1;
-  else 
-    Y = (int) Y;
-
-  /* Rx,Ry are the screen coordinates of the first image pixel */
-  Rx = (X + image[0].X - 0.5*DX)/expand + 0.5*dx;
-  Ry = (Y + image[0].Y - 0.5*DY)/expand + 0.5*dy;
-
-  i_start = MIN (MAX (Rx, 0), dx - expand_out + 1);
-  j_start = MIN (MAX (Ry, 0), dy - expand_out + 1);
-  
-  if (image[0].expand > 0) {
-    i_end = MAX (MIN (i_start + ((int)(expand*(dx-i_start)))/expand, expand_out*(DX-X) + Rx), 0);
-    j_end = MAX (MIN (j_start + ((int)(expand*(dy-j_start)))/expand, expand_out*(DY-Y) + Ry), 0);
-  } else {
-    i_end = MAX (MIN (dx, (DX-X)/expand + Rx), 0);
-    j_end = MAX (MIN (dy, (DY-Y)/expand + Ry), 0);
-  }    
+  // x, y are the closest lit screen pixel to 0,0
+  Picture_Lower (&i_start, &j_start, &image[0].image[0].matrix, &image[0].picture);
+
+  // x, y are the closest lit screen pixel to dx, dy
+  Picture_Upper (&i_end, &j_end, &image[0].image[0].matrix, &image[0].picture);
+
+  // Ix,Iy are the first displayed image pixel
+  Picture_to_Image (&Ix, &Iy, i_start, j_start, &image[0].picture);
 
   dropback = expand_out - (i_end - i_start) % expand_out;
@@ -129,5 +109,5 @@
   ALLOCATE (line_buffer, JSAMPLE, 3*dx);
 
-  in_pix_ref  = &image[0].pixmap[DX*(int)MAX(Y,0) + (int)MAX(X,0)];
+  in_pix_ref  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];
 
   /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
@@ -233,6 +213,4 @@
   jpeg_destroy_compress (&cinfo);
 
-
-# endif
   return (TRUE);
 }
Index: /trunk/Ohana/src/kapa2/src/LoadPicture.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/LoadPicture.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/LoadPicture.c	(revision 16256)
@@ -9,4 +9,5 @@
   KapaImageWidget *image;
   Graphic *graphic;
+  double Xoffset, Yoffset;
 
   graphic = GetGraphic ();
@@ -19,4 +20,12 @@
   
   KiiSendMessage (sock, "%d", graphic->Npixels);
+
+  // when we load a new picture, use the same orientation as the old picture
+  Xoffset = 0.0;
+  Yoffset = 0.0;
+  if (image[0].image[0].matrix.size) {
+    Xoffset = image[0].picture.X - 0.5*image[0].image[0].matrix.Naxis[0];
+    Yoffset = image[0].picture.Y - 0.5*image[0].image[0].matrix.Naxis[1];
+  }
 
   gfits_init_header (&header);
@@ -34,6 +43,6 @@
 
   // reference point for image is the center pixel
-  image[0].picture.Xo = 0.5*header.Naxis[0];
-  image[0].picture.Yo = 0.5*header.Naxis[1];
+  image[0].picture.X = 0.5*header.Naxis[0] + Xoffset;
+  image[0].picture.Y = 0.5*header.Naxis[1] + Yoffset;
 
   fcntl (sock, F_SETFL, O_NONBLOCK);  
Index: /trunk/Ohana/src/kapa2/src/PSPixmap.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/PSPixmap.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/PSPixmap.c	(revision 16256)
@@ -1,3 +1,5 @@
 # include "Ximage.h"
+
+// XXX this stuff has been broken by the conversion to the pixmap stuff
 
 void PSPixmap8 (Graphic *graphic, KapaImageWidget *image, FILE *f) {
Index: /trunk/Ohana/src/kapa2/src/Remap.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Remap.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/Remap.c	(revision 16256)
@@ -5,14 +5,14 @@
   switch (graphic[0].Nbits) {
   case 8:
-    Remap8  (graphic, image, &image->image->matrix);
+    Remap8  (graphic, image, &image->picture, &image->image->matrix);
     break;
   case 16:
-    Remap16 (graphic, image, &image->image->matrix);
+    Remap16 (graphic, image, &image->picture, &image->image->matrix);
     break;
   case 24:
-    Remap24 (graphic, image, &image->image->matrix);
+    Remap24 (graphic, image, &image->picture, &image->image->matrix);
     break;
   case 32:
-    Remap32 (graphic, image, &image->image->matrix);
+    Remap32 (graphic, image, &image->picture, &image->image->matrix);
     break;
   }
Index: /trunk/Ohana/src/kapa2/src/Remap16.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Remap16.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/Remap16.c	(revision 16256)
@@ -1,92 +1,58 @@
 # include "Ximage.h"
 
-void Remap16 (Graphic *graphic, KapaImageWidget *image, Matrix *matrix) {
+void Remap16 (Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix) {
 
-# if (0)
   int i, j, ii, jj;
   int i_start, i_end, j_start, j_end;
-  int dropback;  /* this is a bit of a kludge... */
+  int dropback;
   int dx, dy, DX, DY;
-  double expand, Sx, Sy, Ix, Iy;
+  double expand, Ix, Iy;
   int expand_in, expand_out;
-  unsigned char *out_pix, *out_pix2, *data;
+  unsigned short *out_pix, *out_pix2, *data;
   unsigned short *in_pix, *in_pix2;
-  unsigned char *pixel1, *pixel2;
-  unsigned char pixvalue1, pixvalue2;
-  unsigned char back1, back2;
+  unsigned short *pixel, pixvalue;
+  unsigned short back;
 
-  ALLOCATE (pixel1, unsigned char, graphic[0].Npixels);
-  ALLOCATE (pixel2, unsigned char, graphic[0].Npixels);
+  ALLOCATE (pixel, unsigned short, graphic[0].Npixels);
 
   // local array for pixel values (is this working??)
   for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
-    pixel1[i] = 0x00ff &  graphic[0].cmap[i].pixel;
-    pixel2[i] = 0x00ff & (graphic[0].cmap[i].pixel >> 8);
+    pixel[i] = 0xffff &  graphic[0].cmap[i].pixel;
   }
-  back1 = 0x00ff & (graphic[0].back >> 0);
-  back2 = 0x00ff & (graphic[0].back >> 8);
-
-  // 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;
-  }
+  back = 0xffff & graphic[0].back;
 
   // set up expansions
+  assert ((picture[0].expand >= 1) || (picture[0].expand <= -2));
   expand = expand_in = expand_out = 1.0;
-  if (image[0].expand == 0) /* set up expansions */
-    image[0].expand = 1;
-  if (image[0].expand > 0) {
-    expand = 1 / (1.0*image[0].expand);
-    expand_out = image[0].expand;
+  if (picture[0].expand > 0) {
+    expand = 1 / (1.0*picture[0].expand);
+    expand_out = picture[0].expand;
     expand_in  = 1;
   }
-  if (image[0].expand < 0) {
-    expand = fabs((double)image[0].expand);
+  if (picture[0].expand < 0) {
+    expand = fabs((double)picture[0].expand);
     expand_out = 1;
-    expand_in  = -image[0].expand;
+    expand_in  = -picture[0].expand;
   }
 
   // define the image boundaries
-  dx = image[0].picture.dx;
-  dy = image[0].picture.dy;
+  dx = picture[0].dx;
+  dy = picture[0].dy;
   DX = matrix[0].Naxis[0];
   DY = matrix[0].Naxis[1];
 
-  /* X,Y are the image coordinates of the first image pixel */
-  Ix = MAX(0.5*(DX - dx*expand) - image[0].X, 0);
-  if ((int)Ix != Ix) 
-    Ix = (int) Ix + 1;
-  else 
-    Ix = (int) Ix;
-  Iy = MAX(0.5*(DY - dy*expand) - image[0].Y, 0);
-  if ((int)Iy != Iy) 
-    Iy = (int) Iy + 1;
-  else 
-    Iy = (int) Iy;
+  // x, y are the closest lit screen pixel to 0,0
+  Picture_Lower (&i_start, &j_start, matrix, picture);
 
-  /* Sx,Sy are the screen coordinates of the first image pixel */
-  Sx = (Ix + image[0].X - 0.5*DX)/expand + 0.5*dx;
-  Sy = (Iy + image[0].Y - 0.5*DY)/expand + 0.5*dy;
+  // x, y are the closest lit screen pixel to dx, dy
+  Picture_Upper (&i_end, &j_end, matrix, picture);
 
-  i_start = MIN (MAX (Sx, 0), dx - expand_out + 1);
-  j_start = MIN (MAX (Sy, 0), dy - expand_out + 1);
-  
-  if (image[0].expand > 0) {
-    i_end = MAX (MIN (i_start + ((int)(expand*(dx-i_start)))/expand, expand_out*(DX-Ix) + Sx), 0);
-    j_end = MAX (MIN (j_start + ((int)(expand*(dy-j_start)))/expand, expand_out*(DY-Iy) + Sy), 0);
-  } else {
-    i_end = MAX (MIN (dx, (DX-Ix)/expand + Sx), 0);
-    j_end = MAX (MIN (dy, (DY-Iy)/expand + Sy), 0);
-  }    
+  // Ix,Iy are the first displayed image pixel
+  Picture_to_Image (&Ix, &Iy, i_start, j_start, picture);
 
   dropback = expand_out - (i_end - i_start) % expand_out;
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
 
-  data = out_pix = (unsigned char *) image[0].picture.data;
+  data = out_pix = (unsigned short *) picture[0].data;
   in_pix  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];
 
@@ -94,78 +60,59 @@
 
   /**** fill in bottom area ****/
-  for (j = 0; j < j_start; j++) {
-    for (i = 0; i < dx; i++, out_pix+=2) {
-      out_pix[0] = back1;
-      out_pix[1] = back2;
-    }
+  for (j = 0; j < dx*j_start; j++, out_pix++) {
+    *out_pix = back;
   }
   
   for (j = j_start; j < j_end; j+= expand_out, in_pix += expand_in*DX) {
-    out_pix = &data[2*j*dx];
+    out_pix = &data[j*dx];
 
     /**** fill in area to the left of the picture ****/
     for (jj = 0; (i_start > 0) && (jj < expand_out); jj++) { 
-      out_pix2 = out_pix + 2*jj*dx;
-      for (i = 0; i < i_start; i++, out_pix2+=2) {
-	out_pix2[0] = back1;
-	out_pix2[1] = back2;
+      out_pix2 = out_pix + jj*dx;
+      for (i = 0; i < i_start; i++, out_pix2++) {
+	*out_pix2 = back;
       }
     }
-    out_pix += 2*i_start;
+    out_pix += i_start;
     
     /*** fill in the picture region ***/
     in_pix2 = in_pix;
     if (expand_out == 1) {
-      for (i = i_start; i < i_end; i++, in_pix2+= expand_in, out_pix+=2) {
-	out_pix[0] = pixel1[*in_pix2];
-	out_pix[1] = pixel2[*in_pix2];
+      for (i = i_start; i < i_end; i++, in_pix2+= expand_in, out_pix++) {
+	*out_pix = pixel[*in_pix2];
       }
-    }
-    else {
-      for (i = i_start; i < i_end; i+= expand_out, in_pix2++, out_pix+= 2*expand_out) { 
-	pixvalue1 = pixel1[*in_pix2];
-	pixvalue2 = pixel2[*in_pix2];
+    } else {
+      for (i = i_start; i < i_end; i+= expand_out, in_pix2++, out_pix+= expand_out) { 
+	pixvalue = pixel[*in_pix2];
 	out_pix2 = out_pix;
-	for (jj = 0; jj < expand_out; jj++, out_pix2+=2*(dx-expand_out)) {
-	  for (ii = 0; ii < expand_out; ii++, out_pix2+=2) {
-	    out_pix2[0] = pixvalue1; 
-	    out_pix2[1] = pixvalue2; 
+	for (jj = 0; jj < expand_out; jj++, out_pix2+=(dx-expand_out)) {
+	  for (ii = 0; ii < expand_out; ii++, out_pix2++) {
+	    *out_pix2 = pixvalue; 
 	  }
 	}
       }
     }
-    out_pix -= 2*dropback;
+    out_pix -= dropback;
     
     /**** fill in area to the right of the picture ****/
     for (jj = 0; jj < expand_out; jj++) {
-      out_pix2 = out_pix + 2*jj*dx;
-      for (i = i_end; i < dx; i++, out_pix2+=2) {
-	out_pix2[0] = back1;
-	out_pix2[1] = back2;
+      out_pix2 = out_pix + jj*dx;
+      for (i = i_end; i < dx; i++, out_pix2++) {
+	*out_pix2 = back;
       }
     }
   } 
   
-  /*
-  if ((j_end - j_start) % expand_out > 0)
-    out_pix -= expand_out - (j_end - j_start) % expand_out;
-  */
-  
-  out_pix = &data[2*j_end*dx];
   /**** fill in top area ****/
+  out_pix = &data[j_end*dx];
   for (j = 0; j < (dy - j_end); j++) {
-    for (i = 0; i < dx; i++, out_pix+=2) { 
-      out_pix[0] = back1;
-      out_pix[1] = back2;
+    for (i = 0; i < dx; i++, out_pix++) { 
+      *out_pix = back;
     }
   }
 
-  image[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
-					image[0].picture.data, image[0].picture.dx, image[0].picture.dy, 16, 0);
+  picture[0].pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
+					picture[0].data, picture[0].dx, picture[0].dy, 16, 0);
 
-  free (pixel1);
-  free (pixel2);
-
-# endif
+  free (pixel);
 }
-
Index: /trunk/Ohana/src/kapa2/src/Remap24.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Remap24.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/Remap24.c	(revision 16256)
@@ -1,12 +1,11 @@
 # include "Ximage.h"
 
-void Remap24 (Graphic *graphic, KapaImageWidget *image, Matrix *matrix) {
+void Remap24 (Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix) {
 
-# if (0)
   int i, j, ii, jj;
   int i_start, i_end, j_start, j_end;
-  int dropback, extra;  /* this is a bit of a kludge... */
+  int dropback, extra;
   int dx, dy, DX, DY;
-  double expand, Sx, Sy, Ix, Iy;
+  double expand, Ix, Iy;
   int expand_in, expand_out;
   unsigned char *out_pix, *out_pix2, *data;
@@ -31,55 +30,40 @@
 
   // set up expansions
+  assert ((picture[0].expand >= 1) || (picture[0].expand <= -2));
   expand = expand_in = expand_out = 1.0;
-  if (image[0].expand == 0) /* set up expansions */
-    image[0].expand = 1;
-  if (image[0].expand > 0) {
-    expand = 1 / (1.0*image[0].expand);
-    expand_out = image[0].expand;
+  if (picture[0].expand == 0) /* set up expansions */
+    picture[0].expand = 1;
+  if (picture[0].expand > 0) {
+    expand = 1 / (1.0*picture[0].expand);
+    expand_out = picture[0].expand;
     expand_in  = 1;
   }
-  if (image[0].expand < 0) {
-    expand = fabs((double)image[0].expand);
+  if (picture[0].expand < 0) {
+    expand = fabs((double)picture[0].expand);
     expand_out = 1;
-    expand_in  = -image[0].expand;
+    expand_in  = -picture[0].expand;
   }
 
-  dx = image[0].picture.dx;
-  dy = image[0].picture.dy;
+  dx = picture[0].dx;
+  dy = picture[0].dy;
   DX = matrix[0].Naxis[0];
   DY = matrix[0].Naxis[1];
+
+  // each row is padded to a 4-byte word
   extra = 4 - (dx * 3) % 4;
 
-  /* X,Y are the image coordinates of the first image pixel */
-  Ix = MAX(0.5*(DX - dx*expand) - image[0].X, 0);
-  if ((int)Ix != Ix) 
-    Ix = (int) Ix + 1;
-  else 
-    Ix = (int) Ix;
-  Iy = MAX(0.5*(DY - dy*expand) - image[0].Y, 0);
-  if ((int)Iy != Iy) 
-    Iy = (int) Iy + 1;
-  else 
-    Iy = (int) Iy;
+  // x, y are the closest lit screen pixel to 0,0
+  Picture_Lower (&i_start, &j_start, matrix, picture);
 
-  /* Sx,Sy are the screen coordinates of the first image pixel */
-  Sx = (Ix + image[0].X - 0.5*DX)/expand + 0.5*dx;
-  Sy = (Iy + image[0].Y - 0.5*DY)/expand + 0.5*dy;
+  // x, y are the closest lit screen pixel to dx, dy
+  Picture_Upper (&i_end, &j_end, matrix, picture);
 
-  i_start = MIN (MAX (Sx, 0), dx - expand_out + 1);
-  j_start = MIN (MAX (Sy, 0), dy - expand_out + 1);
-  
-  if (image[0].expand > 0) {
-    i_end = MAX (MIN (i_start + ((int)(expand*(dx-i_start)))/expand, expand_out*(DX-Ix) + Sx), 0);
-    j_end = MAX (MIN (j_start + ((int)(expand*(dy-j_start)))/expand, expand_out*(DY-Iy) + Sy), 0);
-  } else {
-    i_end = MAX (MIN (dx, (DX-Ix)/expand + Sx), 0);
-    j_end = MAX (MIN (dy, (DY-Iy)/expand + Sy), 0);
-  }    
+  // Ix,Iy are the first displayed image pixel
+  Picture_to_Image (&Ix, &Iy, i_start, j_start, picture);
 
   dropback = expand_out - (i_end - i_start) % expand_out;
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
-
-  data = out_pix = (unsigned char *) image[0].picture.data;
+  
+  data = out_pix = (unsigned char *) picture[0].data;
   in_pix  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];
 
@@ -146,11 +130,6 @@
   } 
   
-  /*
-  if ((j_end - j_start) % expand_out > 0)
-    out_pix -= expand_out - (j_end - j_start) % expand_out;
-  */
-  
+  /**** fill in top area ****/
   out_pix = &data[j_end*(3*dx+extra)];
-  /**** fill in top area ****/
   for (j = 0; j < (dy - j_end); j++) {
     for (i = 0; i < dx; i++, out_pix+=3) { 
@@ -162,12 +141,9 @@
   }
 
-  image[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
-					image[0].picture.data, image[0].picture.dx, image[0].picture.dy, 32, 0);
+  picture[0].pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
+					picture[0].data, picture[0].dx, picture[0].dy, 32, 0);
 
   free (pixel1);
   free (pixel2);
   free (pixel3);
-
-# endif
 }
-
Index: /trunk/Ohana/src/kapa2/src/Remap32.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Remap32.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/Remap32.c	(revision 16256)
@@ -1,11 +1,11 @@
 # include "Ximage.h"
 
-void Remap32 (Graphic *graphic, KapaImageWidget *image, Matrix *matrix) {
+void Remap32 (Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix) {
 
   int i, j, ii, jj;
   int i_start, i_end, j_start, j_end;
-  int dropback;  /* this is a bit of a kludge... */
+  int dropback;
   int dx, dy, DX, DY;
-  double expand, Sx, Sy, Ix, Iy;
+  double expand, Ix, Iy;
   int expand_in, expand_out;
   unsigned int *out_pix, *out_pix2;
@@ -23,55 +23,36 @@
 
   // set up expansions
-  assert ((image[0].picture.expand >= 1) || (image[0].picture.expand <= -2));
+  assert ((picture[0].expand >= 1) || (picture[0].expand <= -2));
   expand = expand_in = expand_out = 1.0;
-  if (image[0].picture.expand > 0) {
-    expand = 1 / (1.0*image[0].picture.expand);
-    expand_out = image[0].picture.expand;
+  if (picture[0].expand > 0) {
+    expand = 1 / (1.0*picture[0].expand);
+    expand_out = picture[0].expand;
     expand_in  = 1;
   }
-  if (image[0].picture.expand < 0) {
-    expand = fabs((double)image[0].picture.expand);
+  if (picture[0].expand < 0) {
+    expand = fabs((double)picture[0].expand);
     expand_out = 1;
-    expand_in  = -image[0].picture.expand;
+    expand_in  = -picture[0].expand;
   }
 
   // define the image sizes
-  dx = image[0].picture.dx;
-  dy = image[0].picture.dy;
+  dx = picture[0].dx;
+  dy = picture[0].dy;
   DX = matrix[0].Naxis[0];
   DY = matrix[0].Naxis[1];
 
-  // Ix, Iy are the image coordinates of the 0,0 screen pixel
-  Screen_to_Image (&Ix, &Iy, 0.0, 0.0, &image[0].picture);
+  // x, y are the closest lit screen pixel to 0,0
+  Picture_Lower (&i_start, &j_start, matrix, picture);
 
-  // Ix, Iy are now limited to valid image coordinates
-  Ix = MIN (MAX (Ix, 0), DX);
-  Iy = MIN (MAX (Iy, 0), DY);
+  // x, y are the closest lit screen pixel to dx, dy
+  Picture_Upper (&i_end, &j_end, matrix, picture);
 
-  // Sx, Sy are the screen coordinates of the Ix,Iy pixel
-  Image_to_Screen (&Sx, &Sy, Ix,  Iy,  &image[0].picture);
-
-  // i_start, j_start are now limited to valid screen coordinates
-  i_start = MIN (MAX (Sx, 0), dx);
-  j_start = MIN (MAX (Sy, 0), dy);
-
-  // Ix, Iy are the image coordinates of the 0,0 screen pixel
-  Screen_to_Image (&Ix, &Iy, dx, dy, &image[0].picture);
-
-  // Ix, Iy are now limited to valid image coordinates
-  Ix = MIN (MAX (Ix, 0), DX);
-  Iy = MIN (MAX (Iy, 0), DY);
-
-  // Sx, Sy are the screen coordinates of the Ix,Iy pixel
-  Image_to_Screen (&Sx, &Sy, Ix,  Iy,  &image[0].picture);
-
-  // i_end, j_end are now limited to valid screen coordinates
-  i_end = MIN (MAX (Sx, 0), dx);
-  j_end = MIN (MAX (Sy, 0), dy);
+  // Ix,Iy are the first displayed image pixel
+  Picture_to_Image (&Ix, &Iy, i_start, j_start, picture);
 
   dropback = expand_out - (i_end - i_start) % expand_out;
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
 
-  out_pix = (unsigned int *) image[0].picture.data;
+  out_pix = (unsigned int *) picture[0].data;
   in_pix  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];
 
@@ -127,10 +108,10 @@
   
   /**** fill in top area ****/
-  for (j = 0; (j < dx*(dy - j_end)) && (out_pix - (unsigned int *)image[0].picture.data < dx*dy); j++, out_pix++) { 
+  for (j = 0; (j < dx*(dy - j_end)) && (out_pix - (unsigned int *)picture[0].data < dx*dy); j++, out_pix++) { 
     *out_pix = back;
   }
 
-  image[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
-					image[0].picture.data, image[0].picture.dx, image[0].picture.dy, 32, 0);
+  picture[0].pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
+					picture[0].data, picture[0].dx, picture[0].dy, 32, 0);
 
   free (pixel);
Index: /trunk/Ohana/src/kapa2/src/Remap8.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/Remap8.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/Remap8.c	(revision 16256)
@@ -1,78 +1,60 @@
 # include "Ximage.h"
 
-void Remap8 (Graphic *graphic, KapaImageWidget *image, Matrix *matrix) {
+void Remap8 (Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix) {
 
-# if (0)
   int i, j, ii, jj;
   int i_start, i_end, j_start, j_end;
-  int dropback;  /* this is a bit of a kludge... */
-  int dx, dy, DX, DY, pixelN;
-  double expand, Sx, Sy, Ix, Iy;
+  int dropback;
+  int dx, dy, DX, DY;
+  double expand, Ix, Iy;
   int expand_in, expand_out;
   unsigned char *out_pix, *out_pix2;
   unsigned short *in_pix,  *in_pix2;
-  unsigned long *pixel, pixvalue;
-  unsigned long back;
+  unsigned char *pixel, pixvalue;
+  unsigned char back;
 
-  ALLOCATE (pixel, unsigned long, graphic[0].Npixels);
+  ALLOCATE (pixel, unsigned char, graphic[0].Npixels);
 
   // local array for pixel values
   for (i = 0; i < graphic[0].Npixels; i++) {
-    pixel[i] = graphic[0].cmap[i].pixel;
+    pixel[i] = 0xff & graphic[0].cmap[i].pixel;
   }
-  back = graphic[0].back;
+  back = 0xff & graphic[0].back;
 
   // set up expansions
+  assert ((picture[0].expand >= 1) || (picture[0].expand <= -2));
   expand = expand_in = expand_out = 1.0;
-  if (image[0].expand == 0) /* set up expansions */
-    image[0].expand = 1;
-  if (image[0].expand > 0) {
-    expand = 1 / (1.0*image[0].expand);
-    expand_out = image[0].expand;
+  if (picture[0].expand == 0) /* set up expansions */
+    picture[0].expand = 1;
+  if (picture[0].expand > 0) {
+    expand = 1 / (1.0*picture[0].expand);
+    expand_out = picture[0].expand;
     expand_in  = 1;
   }
-  if (image[0].expand < 0) {
-    expand = fabs((double)image[0].expand);
+  if (picture[0].expand < 0) {
+    expand = fabs((double)picture[0].expand);
     expand_out = 1;
-    expand_in  = -image[0].expand;
+    expand_in  = -picture[0].expand;
   }
 
   // define the image boundaries
-  dx = image[0].picture.dx;
-  dy = image[0].picture.dy;
+  dx = picture[0].dx;
+  dy = picture[0].dy;
   DX = matrix[0].Naxis[0];
   DY = matrix[0].Naxis[1];
 
-  /* X,Y are the image coordinates of the first image pixel */
-  Ix = MAX(0.5*(DX - dx*expand) - image[0].X, 0);
-  if ((int)Ix != Ix) 
-    Ix = (int) Ix + 1;
-  else 
-    Ix = (int) Ix;
-  Iy = MAX(0.5*(DY - dy*expand) - image[0].Y, 0);
-  if ((int)Iy != Iy) 
-    Iy = (int) Iy + 1;
-  else 
-    Iy = (int) Iy;
+  // x, y are the closest lit screen pixel to 0,0
+  Picture_Lower (&i_start, &j_start, matrix, picture);
 
-  /* Sx,Sy are the screen coordinates of the first image pixel */
-  Sx = (Ix + image[0].X - 0.5*DX)/expand + 0.5*dx;
-  Sy = (Iy + image[0].Y - 0.5*DY)/expand + 0.5*dy;
+  // x, y are the closest lit screen pixel to dx, dy
+  Picture_Upper (&i_end, &j_end, matrix, picture);
 
-  i_start = MIN (MAX (Sx, 0), dx - expand_out + 1);
-  j_start = MIN (MAX (Sy, 0), dy - expand_out + 1);
-  
-  if (image[0].expand > 0) {
-    i_end = MAX (MIN (i_start + ((int)(expand*(dx-i_start)))/expand, expand_out*(DX-Ix) + Sx), 0);
-    j_end = MAX (MIN (j_start + ((int)(expand*(dy-j_start)))/expand, expand_out*(DY-Iy) + Sy), 0);
-  } else {
-    i_end = MAX (MIN (dx, (DX-Ix)/expand + Sx), 0);
-    j_end = MAX (MIN (dy, (DY-Iy)/expand + Sy), 0);
-  }    
+  // Ix,Iy are the first displayed image pixel
+  Picture_to_Image (&Ix, &Iy, i_start, j_start, picture);
 
   dropback = expand_out - (i_end - i_start) % expand_out;
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
 
-  out_pix = (unsigned char *) image[0].picture.data;
+  out_pix = (unsigned char *) picture[0].data;
   in_pix  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];
 
@@ -80,6 +62,7 @@
 
   /**** fill in bottom area ****/
-  for (j = 0; j < dx*j_start; j++, out_pix++) 
+  for (j = 0; j < dx*j_start; j++, out_pix++) {
     *out_pix = back;
+  }
   
   for (j = j_start; j < j_end; j+= expand_out, out_pix+=(expand_out-1)*dx, in_pix += expand_in*DX) {
@@ -128,11 +111,9 @@
   
   /**** fill in top area ****/
-  for (j = 0; (j < dx*(dy - j_end)) && (out_pix - (unsigned char *)image[0].picture.data < dx*dy); j++, out_pix++) { 
+  for (j = 0; (j < dx*(dy - j_end)) && (out_pix - (unsigned char *)picture[0].data < dx*dy); j++, out_pix++) { 
     *out_pix = back;
   }
-  image[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
-					image[0].picture.data, image[0].picture.dx, image[0].picture.dy, 8, 0);
-
-# endif
+  picture[0].pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
+					picture[0].data, picture[0].dx, picture[0].dy, 8, 0);
+  free (pixel);
 }
-
Index: /trunk/Ohana/src/kapa2/src/SetImageData.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/SetImageData.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/SetImageData.c	(revision 16256)
@@ -125,6 +125,6 @@
   image = section->image;
 
-  Screen_to_Image (&Xmin, &Ymin, 0.0, 0.0, &image[0].picture);
-  Screen_to_Image (&Xmax, &Ymax, image[0].picture.dx, image[0].picture.dy, &image[0].picture);
+  Picture_to_Image (&Xmin, &Ymin, 0.0, 0.0, &image[0].picture);
+  Picture_to_Image (&Xmax, &Ymax, image[0].picture.dx, image[0].picture.dy, &image[0].picture);
 
   KiiSendMessage (sock, "%g %g %g %g", Xmin, Xmax, Ymin, Ymax);
Index: /trunk/Ohana/src/kapa2/src/SetImageSize.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/SetImageSize.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/SetImageSize.c	(revision 16256)
@@ -319,5 +319,5 @@
     CreatePicture (image, graphic);
     CreateColorbar (image, graphic);
-    CreateZoom (image, graphic, 0, 0); 
+    CreateZoom (graphic, image); 
   }
   Remap (graphic, image);
Index: /trunk/Ohana/src/kapa2/src/StatusBox.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/StatusBox.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/StatusBox.c	(revision 16256)
@@ -11,10 +11,10 @@
     y = 0.5*image[0].image[0].matrix.Naxis[1];
     // z = -1;
-    image[0].zoom.x = x;
-    image[0].zoom.y = y;
+    image[0].zoom.X = x;
+    image[0].zoom.Y = y;
     // image[0].z = z;
   } else {
-    x = image[0].zoom.x;
-    y = image[0].zoom.y;
+    x = image[0].zoom.X;
+    y = image[0].zoom.Y;
     // z = image[0].z;
   }
Index: /trunk/Ohana/src/kapa2/src/UpdatePointer.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/UpdatePointer.c	(revision 16255)
+++ /trunk/Ohana/src/kapa2/src/UpdatePointer.c	(revision 16256)
@@ -19,5 +19,5 @@
 
     data = (float *) image[0].image[0].matrix.buffer;
-    Screen_to_Image (&x, &y, (double)event[0].x, (double)event[0].y, &image[0].picture);
+    Screen_to_Image (&x, &y, event[0].x + 0.5, event[0].y + 0.5, &image[0].picture);
 
     z = -1;
@@ -35,5 +35,5 @@
     XFlush (graphic[0].display);
       
-    CreateZoom (image, graphic, x, y);  
+    CreateZoom (graphic, image);  
     XPutImage (graphic[0].display, graphic[0].window, graphic[0].gc,
 	       image[0].zoom.pix, 0, 0, 
