Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/Makefile
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/Makefile	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/Makefile	(revision 29815)
@@ -42,4 +42,5 @@
 $(SRC)/bDrawObjects.$(ARCH).o	  	  $(SRC)/bDrawFrame.$(ARCH).o         \
 $(SRC)/bDrawLabels.$(ARCH).o              $(SRC)/bDrawIt.$(ARCH).o            \
+$(SRC)/bDrawImage.$(ARCH).o               \
 $(SRC)/PNGit.$(ARCH).o                    $(SRC)/PPMit.$(ARCH).o	      \
 $(SRC)/PSit.$(ARCH).o                     $(SRC)/CrossHairs.$(ARCH).o         \
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/include/prototypes.h	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/include/prototypes.h	(revision 29815)
@@ -40,7 +40,7 @@
 void          DrawYErrors         PROTO((KapaGraphWidget *graph, Gobjects *objects));
 void	      DrawTick		  PROTO((Graphic *graphic, Axis *axis, int P, TickMarkData *tick, int naxis));
-void	      AxisTickScale	  PROTO((Axis *axis, double *major, double *minor));
+void	      AxisTickScale	  PROTO((Axis *axis, double *major, double *minor, int *nsignif));
 TickMarkData *CreateAxisTicks     PROTO((Axis *axis, int *nticks));
-int           PrintTick           PROTO((char *string, double value, double min, double max));
+int           PrintTick           PROTO((char *string, double value, double min, double max, int nsignif));
 
 /* EventLoop */
@@ -68,4 +68,5 @@
 int           MoveSection         PROTO((int sock));
 int           DefineSection       PROTO((int sock));
+int           DefineSectionByImage PROTO((int sock));
 int           SetFont             PROTO((int sock));
 int           EraseCurrentPlot    PROTO((void));
@@ -127,17 +128,18 @@
 
 /* kapa bDraw Functions */
-int	      bDrawFrame	  PROTO((KapaGraphWidget *graph));
-int	      bDrawObjects	  PROTO((KapaGraphWidget *graph));
-void	      bDrawLabels	  PROTO((KapaGraphWidget *graph));
-void	      bDrawTextlines	  PROTO((KapaGraphWidget *graph));
-void	      bDrawConnect	  PROTO((KapaGraphWidget *graph, Gobjects *object));
-void	      bDrawHistogram	  PROTO((KapaGraphWidget *graph, Gobjects *object));
-void	      bDrawPoints	  PROTO((KapaGraphWidget *graph, Gobjects *object));
-void	      bDrawXErrors	  PROTO((KapaGraphWidget *graph, Gobjects *object));
-void	      bDrawYErrors	  PROTO((KapaGraphWidget *graph, Gobjects *object));
-void	      bDrawTick		  PROTO((Axis *axis, int P, TickMarkData *tick, int naxis));
-void	      bDrawClipLine	  PROTO((double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0));
-bDrawBuffer  *bDrawIt		  PROTO((void));
-void          bDrawGraph          PROTO((KapaGraphWidget *graph));
+int	      bDrawFrame	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph));
+int	      bDrawObjects	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph));
+void	      bDrawLabels	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph));
+void	      bDrawTextlines	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph));
+void	      bDrawConnect	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object));
+void	      bDrawHistogram	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object));
+void	      bDrawPoints	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object));
+void	      bDrawXErrors	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object));
+void	      bDrawYErrors	  PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object));
+void	      bDrawTick		  PROTO((bDrawBuffer *buffer, Axis *axis, int P, TickMarkData *tick, int naxis));
+void	      bDrawClipLine	  PROTO((bDrawBuffer *buffer, double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0));
+void          bDrawGraph          PROTO((bDrawBuffer *buffer, KapaGraphWidget *graph));
+int           bDrawImage          PROTO((bDrawBuffer *buffer, KapaImageWidget *image, Graphic *graphic));
+bDrawBuffer  *bDrawIt		  PROTO((png_color *palette, int Npalette, int Nbyte));
 
 /* misc support */
@@ -245,5 +247,5 @@
 int  	      GetActiveSocket (void);
 void 	      InvertButton (Graphic *graphic, Button *button);
-void 	      bDrawOverlay (KapaImageWidget *image, int N);
+void 	      bDrawOverlay (bDrawBuffer *buffer, KapaImageWidget *image, int N);
 
 /* color cube tools */
@@ -265,2 +267,5 @@
 int SetColorScale3D_CC (Graphic *graphic, KapaImageWidget *image);
 int SetColorCubeHistogram (void);
+int GetGraphBoundary (Section *section, double *x0, double *y0, double *x1, double *y1, int *dXm, int *dXp, int *dYm, int *dYp);
+int ResizeByImage (int sock);
+
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/include/structures.h
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/include/structures.h	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/include/structures.h	(revision 29815)
@@ -148,4 +148,5 @@
   int IsMajor;
   int IsLabel;
+  int nsignif;
 } TickMarkData;
 
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/CheckPipe.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/CheckPipe.c	(revision 29815)
@@ -137,4 +137,10 @@
   }
 
+  if (!strcmp (word, "ISIZ")) {
+    status = ResizeByImage (sock);
+    KiiSendCommand (sock, 4, "DONE");
+    FINISHED (TRUE);
+  }
+  
   if (!strcmp (word, "MOVE")) {
     status = Relocate (sock);
@@ -217,4 +223,10 @@
   if (!strcmp (word, "DSEC")) {
     status = DefineSection (sock);
+    KiiSendCommand (sock, 4, "DONE");
+    FINISHED (TRUE);
+  }
+  
+  if (!strcmp (word, "ISEC")) {
+    status = DefineSectionByImage (sock);
     KiiSendCommand (sock, 4, "DONE");
     FINISHED (TRUE);
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/DefineSection.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/DefineSection.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/DefineSection.c	(revision 29815)
@@ -40,2 +40,58 @@
   return (TRUE);
 }
+
+// define the section, but do not create a graph or image 
+int DefineSectionByImage (int sock) {
+  
+  int N, bg;
+  char name[128];
+  double x, y, dx, dy;
+  int dXm, dXp, dYm, dYp;
+  double x0, y0, x1, y1, expand;
+  Section *section;
+  Graphic *graphic;
+  KapaImageWidget *image;
+
+  KiiScanMessage (sock, "%s %lf %lf %d", name, &x, &y, &bg);
+
+  N = GetSectionByName (name);
+  section = GetSectionByNumber (N);
+  image = section->image;
+  if (!image) {
+    fprintf (stderr, "no image to define section\n");
+    return (TRUE);
+  }
+  SetActiveSectionByNumber (N);
+
+  graphic = GetGraphic ();
+
+  GetGraphBoundary (section, &x0, &y0, &x1, &y1, &dXm, &dXp, &dYm, &dYp);
+
+  expand = 1.0;
+  if (image[0].picture.expand > 0) {
+    expand = image[0].picture.expand;
+  }
+  if (image[0].picture.expand < 0) {
+    expand = 1.0 / fabs((double)image[0].picture.expand);
+  }
+
+  // pixel dimensions of the imaging region + boundary
+  dx = image[0].image[0].matrix.Naxis[0]*expand + x1;
+  dy = image[0].image[0].matrix.Naxis[1]*expand + y1;
+
+  section[0].x = x;
+  section[0].y = y;
+  section[0].bg = bg;
+  section[0].dx = dx / graphic[0].dx;
+  section[0].dy = dy / graphic[0].dy;
+
+  // if (section[0].x != x)   MoveSection = TRUE;
+  // if (section[0].y != y)   MoveSection = TRUE;
+  // if (section[0].dx != dx) MoveSection = TRUE;
+  // if (section[0].dy != dy) MoveSection = TRUE;
+
+  SetSectionSizes (section);
+  Refresh ();
+
+  return (TRUE);
+}
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/DrawFrame.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/DrawFrame.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/DrawFrame.c	(revision 29815)
@@ -5,6 +5,6 @@
 int DrawFrame (KapaGraphWidget *graph) {
   
-  int i, j, Nticks, P;
-  double fx, fy, dfx, dfy, lweight;
+  int i, j, Nticks, P, doffset;
+  double fx, fy, dfx, dfy, dx, dy, lweight;
   Graphic *graphic;
   TickMarkData *ticks;
@@ -15,12 +15,21 @@
   /* each axis is drawn independently, but ticks and labels are placed according to perpendicular distance. */
   for (i = 0; i < 4; i++) {
-    fx  = graph[0].axis[i].fx;
-    fy  = graph[0].axis[i].fy;
-    dfx = graph[0].axis[i].dfx;
-    dfy = graph[0].axis[i].dfy;
-    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);
-
     lweight = MAX (0, MIN (10, graph[0].axis[i].lweight));
     color = MAX (0, MIN (15, graph[0].axis[i].color));
+
+    /* temporarily assume rectilinear axes */
+    doffset = ((int)(lweight) % 2) ? 0.5*(lweight - 1) : 0.5*lweight;
+    if (i == 0) { dx = doffset; dy = 0.0; }
+    if (i == 2) { dx = doffset; dy = 0.0; }
+    if (i == 1) { dx = 0.0; dy = doffset; }
+    if (i == 3) { dx = 0.0; dy = doffset; }
+
+    fx  = graph[0].axis[i].fx - dx;
+    fy  = graph[0].axis[i].fy - dy;
+    dfx = graph[0].axis[i].dfx + 2*dx;
+    dfy = graph[0].axis[i].dfy + 2*dy;
+
+    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);
+    P *= (1 + 0.25*lweight);
 
     XSetLineAttributes (graphic->display, graphic->gc, lweight, LineSolid, CapNotLast, JoinMiter);
@@ -43,5 +52,5 @@
 }
 
-int PrintTick (char *string, double value, double min, double max) {
+int PrintTick (char *string, double value, double min, double max, int nsignif) {
 
   int Nexp = 0;
@@ -54,16 +63,23 @@
   }
 
-  double lvalue = log10(fabs(value));
-
-  if (isfinite(lvalue)) {
-    Nexp = fabs(lvalue);
-  } else {
-    Nexp = 0;
-  }
-  
+//   double lvalue = log10(fabs(value));
+//   if (isfinite(lvalue)) {
+//     Nexp = fabs(lvalue);
+//   } else {
+//     Nexp = 0;
+//   }
+  
+  Nexp = abs(nsignif);
+
   if (Nexp > 3) {
     Nchar = sprintf (string, "%.1e", value);
   } else {
-    Nchar = sprintf (string, "%.1f", value);
+    if (nsignif < 0) {
+      char format[16];
+      snprintf (format, 16, "%%.%df", -1 * nsignif);
+      Nchar = sprintf (string, format, value);
+    } else {
+      Nchar = sprintf (string, "%.1f", value);
+    }
   }
   return Nchar;
@@ -129,5 +145,5 @@
     yt = fy + (value-min)*dfy/(max - min) + dy;
 
-    PrintTick (string, value, min, max);
+    PrintTick (string, value, min, max, tick->nsignif);
     DrawRotText (xt, yt, string, pos, 0.0);
   }
@@ -136,5 +152,5 @@
 # define MIN_RANGE 1e-10
 
-void AxisTickScale (Axis *axis, double *major, double *minor) {
+void AxisTickScale (Axis *axis, double *major, double *minor, int *nsignif) {
 
   double range, lrange, factor, mantis, fmantis, power;
@@ -152,4 +168,6 @@
   }
   
+  // how many significant digits are needed?
+
   power = MAX(pow(10.0, factor), MIN_RANGE);
   fmantis = pow(10.0, mantis);
@@ -160,7 +178,9 @@
     *major = 0.5 * power;
     *minor = 0.1 * power;
+    *nsignif = factor - 1;
     if (axis[0].areticks == 1) {
       *major = 0.25 * power;
       *minor = 0.05 * power;
+      *nsignif = factor - 2;
     }	  
   }
@@ -168,7 +188,9 @@
     *major = 1.0 * power;
     *minor = 0.2 * power;
+    *nsignif = factor;
     if (axis[0].areticks == 1) {
       *major = 0.5 * power;
       *minor = 0.1 * power;
+      *nsignif = factor - 1;
     }	  
   }
@@ -176,7 +198,9 @@
     *major = 1.0 * power;
     *minor = 0.5 * power;
+    *nsignif = factor;
     if (axis[0].areticks == 1) {
       *major = 1.0 * power;
       *minor = 0.2 * power;
+      *nsignif = factor;
     }	  
   }
@@ -184,7 +208,9 @@
     *major = 2.0 * power;
     *minor = 0.5 * power;
+    *nsignif = factor;
     if (axis[0].areticks == 1) {
       *major = 1.0 * power;
       *minor = 0.5 * power;
+      *nsignif = factor;
     }	  
   }
@@ -192,7 +218,9 @@
     *major = 2.5 * power;
     *minor = 0.5 * power;
+      *nsignif = factor - 1;
     if (axis[0].areticks == 1) {
       *major = 2.0 * power;
       *minor = 0.5 * power;
+      *nsignif = factor;
     }	  
   }
@@ -203,5 +231,5 @@
   TickMarkData *ticks;
   double range, major, minor, first, value, dPixels, overshoot;
-  int i, NTICKS, nPixels, done, ifirst;
+  int i, NTICKS, nPixels, done, ifirst, nsignif;
 
   *nticks = 0;
@@ -223,5 +251,5 @@
   dPixels = nPixels / range; // axis pixel-scale
 
-  AxisTickScale (axis, &major, &minor);
+  AxisTickScale (axis, &major, &minor, &nsignif);
 
   // be a little generous 
@@ -256,4 +284,5 @@
     ticks[i].IsLabel = (ticks[i].IsMajor && axis->islabel);
     ticks[i].value = value;
+    ticks[i].nsignif = nsignif;
     if (range > 0) 
       value += minor;
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/JPEGit24.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/JPEGit24.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/JPEGit24.c	(revision 29815)
@@ -202,8 +202,7 @@
     palette = KapaPNGPalette (&Npalette);
 
-    buffer = bDrawBufferCreate (dx, dy);
-    bDrawSetBuffer (buffer);
+    buffer = bDrawBufferCreate (dx, dy, 1, palette, Npalette);
     for (i = 0; i < NOVERLAYS; i++) {
-      if (image[0].overlay[i].active) bDrawOverlay (image, i);
+      if (image[0].overlay[i].active) bDrawOverlay (buffer, image, i);
     }
 
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PNGit.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PNGit.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PNGit.c	(revision 29815)
@@ -6,7 +6,4 @@
    3) decision on output based on the existence of images or not
 */
-
-bDrawBuffer *bDrawBufferCreate8bitRGB (int Nx, int Ny);
-int bDrawImage (bDrawBuffer *buffer, Graphic *graphic);
 
 int PNGcommand (int sock) {
@@ -25,8 +22,9 @@
   png_structp png_ptr;
   png_infop info_ptr;
-  int status, Npalette;
+  int i, status, Npalette, Nsection;
   bDrawBuffer *buffer = NULL;
   Graphic *graphic = NULL;
   png_color *palette = NULL;
+  Section *section;
 
   graphic = GetGraphic();
@@ -68,14 +66,22 @@
   png_init_io (png_ptr, f);
 
+  palette = KapaPNGPalette (&Npalette);
+
+  // do we have an image in any of the sections?
+  Nsection = GetNumberOfSections ();
+  int haveImage = FALSE;
+  for (i = 0; !haveImage && (i < Nsection); i++) {
+    section = GetSectionByNumber (i);
+    haveImage = (section->image != NULL);
+  }
+
   /* see docs for write-row-callback to provide progress info */
-# define PALETTE 1
-  if (PALETTE) {
-    png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 
-    palette = KapaPNGPalette (&Npalette);
-    png_set_PLTE (png_ptr, info_ptr, palette, Npalette); 
-  } else {
+  if (haveImage) {
     // png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
     png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
     png_set_sRGB (png_ptr, info_ptr, PNG_sRGB_INTENT_ABSOLUTE);
+  } else {
+    png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 
+    png_set_PLTE (png_ptr, info_ptr, palette, Npalette); 
   }
 
@@ -87,9 +93,9 @@
   png_write_info (png_ptr, info_ptr);
 
-  if (PALETTE) {
-    buffer = bDrawIt ();
+  if (haveImage) {
+    buffer = bDrawIt (palette, Npalette, 3);
+    // bDrawImage (buffer, graphic);
   } else {
-    buffer = bDrawBufferCreate8bitRGB(graphic->dx, graphic->dy);
-    bDrawImage (buffer, graphic);
+    buffer = bDrawIt (palette, Npalette, 1);
   }
 
@@ -99,215 +105,6 @@
   
   bDrawBufferFree (buffer);
-  free (palette);
   fclose (f);
   return (TRUE);
 
 }
-
-/* For color images, I need to define the relationship between the drawing colors and the RGB values? */
-
-
-bDrawBuffer *bDrawBufferCreate8bitRGB (int Nx, int Ny) {
-
-  int i, j;
-  bDrawBuffer *buffer;
-
-  ALLOCATE (buffer, bDrawBuffer, 1);
-  buffer[0].Nx = Nx;
-  buffer[0].Ny = Ny;
-
-  ALLOCATE (buffer[0].pixels, bDrawColor *, Ny);
-  for (i = 0; i < Ny; i++) {
-    ALLOCATE (buffer[0].pixels[i], bDrawColor, 3*Nx);
-    for (j = 0; j < 3*Nx; j+=3) {
-      buffer[0].pixels[i][j+0] = 0xff;
-      buffer[0].pixels[i][j+1] = 0xff;
-      buffer[0].pixels[i][j+2] = 0xff;
-    }
-  }
-  return (buffer);
-}
-
-# define WHITE_R 255
-# define WHITE_G 255
-# define WHITE_B 255
-# define MY_SWAP_INT(A,B) { int tmp; tmp = A; A = B; B = tmp; }
-
-int bDrawImage (bDrawBuffer *buffer, Graphic *graphic) {
-  
-  Section *section;
-  KapaImageWidget *image;
-
-  int ii, i, j;
-  int i_start, i_end, j_start, j_end;
-  int I_start, J_start;
-  int dropback;  /* this is a bit of a kludge... */
-  int dx, dy, DX, DY, inDX, inDY;
-  int expand_in, expand_out;
-  double expand, Ix, Iy;
-  unsigned char *out_pix;
-  unsigned short *in_pix, *in_pix_ref;
-  unsigned char *pixel1, *pixel2, *pixel3;
-
-  bDrawColor *line_buffer;
-
-  section = GetActiveSection();
-  image   = section->image;
-  if (image == NULL) return (TRUE);
-
-  ALLOCATE (pixel1, unsigned char, graphic[0].Npixels);
-  ALLOCATE (pixel2, unsigned char, graphic[0].Npixels);
-  ALLOCATE (pixel3, unsigned char, graphic[0].Npixels);
-
-  /** cmap[i].pixel must be defined even if X is not used **/
-  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
-    pixel1[i] = graphic[0].cmap[i].red >> 8;
-    pixel2[i] = graphic[0].cmap[i].green >> 8;
-    pixel3[i] = graphic[0].cmap[i].blue >> 8;
-  }
-
-  assert ((image[0].picture.expand >= 1) || (image[0].picture.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;
-    expand_in  = 1;
-  }
-  if (image[0].picture.expand < 0) {
-    expand = fabs((double)image[0].picture.expand);
-    expand_out = 1;
-    expand_in  = -image[0].picture.expand;
-  }
-
-  dx = image[0].picture.dx;
-  dy = image[0].picture.dy;
-  DX = image[0].image[0].matrix.Naxis[0];
-  DY = image[0].image[0].matrix.Naxis[1];
-
-  // i_start, j_start are the closest lit screen pixel to 0,0
-  // I_start, J_start are the image pixel corresponding to i_start, j_start
-  Picture_Lower (&i_start, &j_start, &I_start, &J_start, &image[0].image[0].matrix, &image[0].picture);
-
-  // i_end, j_end are the closest lit screen pixel to dx, dy
-  // I_end, J_end are the image pixel corresponding to i_end, j_end
-  Picture_Upper (&i_end, &j_end, i_start, j_start, &image[0].image[0].matrix, &image[0].picture);
-
-  assert (i_start <= i_end);
-  assert (j_start <= j_end);
-
-  Ix = image[0].picture.flipx ? I_start - 1 : I_start;
-  Iy = image[0].picture.flipy ? J_start - 1 : J_start;
-
-  inDX = image[0].picture.flipx ? -1 : +1;
-  inDY = image[0].picture.flipy ? -1 : +1;
-
-  dropback = expand_out - (i_end - i_start) % expand_out;
-  if ((i_end - i_start) % expand_out == 0) dropback = 0;
-
-  ALLOCATE (line_buffer, bDrawColor, 3*dx);
-
-  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) **********/
-
-  /**** fill in bottom area ****/
-  out_pix = line_buffer;
-  for (i = 0; i < dx; i++, out_pix+=3) {
-    out_pix[0] = WHITE_R;
-    out_pix[1] = WHITE_G;
-    out_pix[2] = WHITE_B;
-  }
-  for (j = 0; j < j_start; j++) {
-    memcpy (buffer[0].pixels[j], line_buffer, 3*dx);
-  }
-  
-  /*** fill in the image data region ***/
-  for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += inDY*expand_in*DX) {
-    
-    /* create one output image line */
-    in_pix = in_pix_ref;
-    out_pix = line_buffer;
-
-    /**** fill in area to the left of the picture ****/
-    for (i = 0; i < i_start; i++, out_pix+=3) {
-      out_pix[0] = WHITE_R;
-      out_pix[1] = WHITE_G;
-      out_pix[2] = WHITE_B;
-    }
-    
-    /*** fill in the picture region ***/
-    for (i = i_start; i < i_end; i+=expand_out, in_pix += inDX*expand_in) {
-      for (ii = 0; ii < expand_out; ii++, out_pix+=3) {
-	out_pix[0] = pixel1[*in_pix];
-	out_pix[1] = pixel2[*in_pix];
-	out_pix[2] = pixel3[*in_pix];
-      }
-    }
-    
-    /**** fill in area to the right of the picture ****/
-    for (i = i_end; i < dx; i++, out_pix+=3) {
-      out_pix[0] = WHITE_R;
-      out_pix[1] = WHITE_G;
-      out_pix[2] = WHITE_B;
-    }
-
-    /* write out the image line expand_out times */
-    for (i = 0; i < expand_out; i++) {
-      memcpy (buffer[0].pixels[j + i], line_buffer, 3*dx);
-    }
-  }
-
-  /**** fill in top area ****/
-  out_pix = line_buffer;
-  for (i = 0; i < dx; i++, out_pix+=3) { 
-    out_pix[0] = WHITE_R;
-    out_pix[1] = WHITE_G;
-    out_pix[2] = WHITE_B;
-  }
-  for (j = j_end; j < dy; j++) {
-    memcpy (buffer[0].pixels[j], line_buffer, 3*dx);
-  }
-
-  free (pixel1);
-  free (pixel2);
-  free (pixel3);
-  free (line_buffer);
-
-  return (TRUE);
-}
-
-# if (0)
-
-  /* I need to write the overlay objects on the jpeg image.
-     if i can write / overwrite data in jpeg buffer, then do it here,
-     otherwise i need to create a temporary image buffer, then write the 
-     scanlines to that buffer */
-
-  {
-    int Npalette;
-    png_color *palette;
-    bDrawColor white, color;
-    bDrawBuffer *buffer;
-
-    palette = KapaPNGPalette (&Npalette);
-
-    buffer = bDrawBufferCreate (dx, dy);
-    bDrawSetBuffer (buffer);
-    for (i = 0; i < NOVERLAYS; i++) {
-      if (image[i].overlay[i].active) bDrawOverlay (image, i);
-    }
-
-    white = KapaColorByName ("white");
-    for (j = 0; j < dy; j++) {
-      for (i = 0; i < dx; i++) {
-	color = buffer[0].pixels[j][i];
-	if (color == white) continue;
-	image_buffer[j*3*dx + 3*i + 0] = palette[color].red;
-	image_buffer[j*3*dx + 3*i + 1] = palette[color].green;
-	image_buffer[j*3*dx + 3*i + 2] = palette[color].blue;
-      }
-    }
-    bDrawBufferFree (buffer);
-  }
-
-# endif
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PPMit.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PPMit.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PPMit.c	(revision 29815)
@@ -30,5 +30,5 @@
   fprintf (f, "255\n");
 
-  buffer = bDrawIt ();
+  buffer = bDrawIt (palette, Npalette, 1);
 
   ALLOCATE (line, char, 3*dx);
@@ -47,4 +47,3 @@
   bDrawBufferFree (buffer);
   return (TRUE);
-
 }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSFrame.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSFrame.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSFrame.c	(revision 29815)
@@ -4,6 +4,6 @@
 int PSFrame (KapaGraphWidget *graph, FILE *f) {
   
-  int i, j, Nticks, P;
-  double fx, fy, dfx, dfy, lweight;
+  int i, j, Nticks, P, doffset;
+  double fx, fy, dfx, dfy, dx, dy, lweight;
   Graphic *graphic;
   TickMarkData *ticks;
@@ -15,12 +15,21 @@
   fprintf (f, "1 setlinewidth\n");
   for (i = 0; i < 4; i++) {
-    fx  = graph[0].axis[i].fx;
-    fy  = graphic->dy - graph[0].axis[i].fy;
-    dfx = graph[0].axis[i].dfx;
-    dfy = -graph[0].axis[i].dfy;
-    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);
-
     lweight = MAX (0, MIN (10, graph[0].axis[i].lweight));
     color = MAX (0, MIN (15, graph[0].axis[i].color));
+
+    /* temporarily assume rectilinear axes */
+    doffset = ((int)(lweight) % 2) ? 0.5*(lweight - 1) : 0.5*lweight;
+    if (i == 0) { dx = doffset; dy = 0.0; }
+    if (i == 2) { dx = doffset; dy = 0.0; }
+    if (i == 1) { dx = 0.0; dy = doffset; }
+    if (i == 3) { dx = 0.0; dy = doffset; }
+
+    fx  = graph[0].axis[i].fx - dx;
+    fy  = graphic->dy - graph[0].axis[i].fy - dy;
+    dfx = graph[0].axis[i].dfx + 2*dx;
+    dfy = -graph[0].axis[i].dfy + 2*dy;
+
+    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);
+    P *= (1 + 0.25*lweight);
 
     fprintf (f, "%.1f setlinewidth\n", lweight);
@@ -95,5 +104,5 @@
     yt = fy + (value-min)*dfy/(max - min) + dy;
 
-    PrintTick (string, value, min, max);
+    PrintTick (string, value, min, max, tick->nsignif);
     PSRotText (f, xt, yt, string, pos, 0.0);
   }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSimage.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSimage.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSimage.c	(revision 29815)
@@ -13,7 +13,11 @@
   graphic = GetGraphic();
 
-  fprintf (f, " newpath 0 0 moveto %d 0 lineto %d %d lineto 0 %d lineto closepath clip\n\n", 
-	   image[0].picture.dx, image[0].picture.dx, image[0].picture.dy, image[0].picture.dy);
+  fprintf (f, " newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath\n\n", 
+	   (int) image[0].picture.x,                       graphic->dy - (int) image[0].picture.y, 
+	   (int) image[0].picture.x + image[0].picture.dx, graphic->dy - (int) image[0].picture.y, 
+	   (int) image[0].picture.x + image[0].picture.dx, graphic->dy - (int) image[0].picture.y - image[0].picture.dy, 
+	   (int) image[0].picture.x,                       graphic->dy - (int) image[0].picture.y - image[0].picture.dy); 
   fprintf (f, "gsave %% encloses image\n");
+  fprintf (f, "%d %d translate\n", (int) image[0].picture.x, graphic->dy - (int) image[0].picture.y - image[0].picture.dy);
   fprintf (f, "%d %d 8\n", image[0].picture.dx, image[0].picture.dy);
   fprintf (f, "[1 0 0 1 0 0]\n");
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSit.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSit.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/PSit.c	(revision 29815)
@@ -87,4 +87,7 @@
   for (i = 0; i < Nsection; i++) {
     section = GetSectionByNumber (i);
+    if (section->image) {
+      PSimage (section->image, f);
+    }
     if (section->graph) {
       PSFrame (section->graph, f); 
@@ -92,7 +95,4 @@
       PSLabels (section->graph, f);
       PSTextlines (section->graph, f);
-    }
-    if (section->image) {
-      PSimage (section->image, f);
     }
   }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/Resize.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/Resize.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/Resize.c	(revision 29815)
@@ -39,2 +39,66 @@
   return (TRUE);
 }
+
+// resise the window so the image in the currently active window fills its section
+int ResizeByImage (int sock) {
+ 
+  int i, Nsection;
+  unsigned int NX, NY;
+  double dx, dy;
+  int dXm, dXp, dYm, dYp;
+  double x0, y0, x1, y1, expand;
+  Section *section;
+  Graphic *graphic;
+  KapaImageWidget *image;
+
+  graphic = GetGraphic();
+
+  section = GetActiveSection();
+
+  image = section->image;
+  if (!image) {
+    fprintf (stderr, "no image to define size\n");
+    return (TRUE);
+  }
+
+  GetGraphBoundary (section, &x0, &y0, &x1, &y1, &dXm, &dXp, &dYm, &dYp);
+
+  expand = 1.0;
+  if (image[0].picture.expand > 0) {
+    expand = image[0].picture.expand;
+  }
+  if (image[0].picture.expand < 0) {
+    expand = 1.0 / fabs((double)image[0].picture.expand);
+  }
+
+  // pixel dimensions of the imaging region + boundary
+  dx = image[0].image[0].matrix.Naxis[0]*expand + x1;
+  dy = image[0].image[0].matrix.Naxis[1]*expand + y1;
+
+  NX = dx / section[0].dx;
+  NY = dy / section[0].dy;
+
+  NX = MAX(NX, MIN_WIDTH); 
+  NY = MAX(NY, MIN_HEIGHT); 
+
+  // if the new size is the same as the old size, do nothing.
+  if ((graphic->dx == NX) && (graphic->dy == NY)) return (TRUE);
+
+  // set the new window size
+  graphic->dx = NX; 
+  graphic->dy = NY; 
+
+  if (USE_XWINDOW) XResizeWindow (graphic->display, graphic->window, NX, NY);
+
+  // reset the sizes for all sections
+  Nsection = GetNumberOfSections ();
+  for (i = 0; i < Nsection; i++) {
+      section = GetSectionByNumber (i);
+      SetSectionSizes (section);
+  }
+
+  if (USE_XWINDOW) XClearWindow (graphic->display, graphic->window);
+  Refresh ();
+
+  return (TRUE);
+}
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/SetGraphSize.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/SetGraphSize.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/SetGraphSize.c	(revision 29815)
@@ -5,13 +5,76 @@
 
 void SetGraphSize (Section *section) {
+
+  int dXm, dXp, dYm, dYp;
+  double x0, y0, x1, y1;
+  KapaGraphWidget *graph;
+  Graphic *graphic;
+
+  GetGraphBoundary(section, &x0, &y0, &x1, &y1, &dXm, &dXp, &dYm, &dYp);
+
+  if (section == NULL) return;
+  graph = section->graph;
+  if (graph == NULL) return;
+
+  graphic = GetGraphic ();
+
+  double X0 = graphic[0].dx * section[0].x  + x0;
+  double Y0 = graphic[0].dy * section[0].y  + y0;
+  double dX = graphic[0].dx * section[0].dx - x1;
+  double dY = graphic[0].dy * section[0].dy - y1;
+
+  /* define locations of coordinate axes */
+  graph[0].axis[0].fx  = X0;
+  graph[0].axis[0].fy  = graphic->dy - Y0;
+  graph[0].axis[0].dfx = dX;
+  graph[0].axis[0].dfy = 0;
+
+  graph[0].axis[1].fx  = X0;
+  graph[0].axis[1].fy  = graphic->dy - Y0;
+  graph[0].axis[1].dfx = 0;
+  graph[0].axis[1].dfy = -dY;
+
+  graph[0].axis[2].fx  = X0;
+  graph[0].axis[2].fy  = graphic->dy - Y0 - dY;
+  graph[0].axis[2].dfx = dX;
+  graph[0].axis[2].dfy = 0;
+
+  graph[0].axis[3].fx  = X0 + dX;
+  graph[0].axis[3].fy  = graphic->dy - Y0;
+  graph[0].axis[3].dfx = 0;
+  graph[0].axis[3].dfy = -dY;
+
+  /* define locations of axis labels */
+  graph[0].label[LABELLL].x = graph[0].axis[0].fx;
+  graph[0].label[LABELLL].y = graph[0].axis[0].fy + dXm;
+  graph[0].label[LABELX0].x = graph[0].axis[0].fx + 0.5*graph[0].axis[0].dfx;
+  graph[0].label[LABELX0].y = graph[0].axis[0].fy + dXm;
+  graph[0].label[LABELLR].x = graph[0].axis[0].fx + graph[0].axis[0].dfx;
+  graph[0].label[LABELLR].y = graph[0].axis[0].fy + dXm;
+
+  graph[0].label[LABELUL].x = graph[0].axis[2].fx;
+  graph[0].label[LABELUL].y = graph[0].axis[2].fy - dXp;
+  graph[0].label[LABELX1].x = graph[0].axis[2].fx + 0.5*graph[0].axis[2].dfx;
+  graph[0].label[LABELX1].y = graph[0].axis[2].fy - dXp;
+  graph[0].label[LABELUR].x = graph[0].axis[2].fx + graph[0].axis[2].dfx;
+  graph[0].label[LABELUR].y = graph[0].axis[2].fy - dXp;
+
+  graph[0].label[LABELY0].y = graph[0].axis[1].fy + 0.5*graph[0].axis[1].dfy;
+  graph[0].label[LABELY0].x = graph[0].axis[1].fx - dYm;
+
+  graph[0].label[LABELY1].y = graph[0].axis[3].fy + 0.5*graph[0].axis[3].dfy;
+  graph[0].label[LABELY1].x = graph[0].axis[3].fx + dYp;
+
+  return;
+}
+
+int GetGraphBoundary (Section *section, double *x0, double *y0, double *x1, double *y1, int *dXm, int *dXp, int *dYm, int *dYp) {
 
   int i, Nticks;
   int fontsize, Nc = 0;
   int textpad, textdY, WdY;
-  int dXm, dXp, dYm, dYp;
   double padXm, padXp, padYm, padYp;
   double minPADx, maxPADx, minPADy;
   double minPAD, maxPAD;
-  double X0, Y0, dX, dY;
   char string[64], *fontname;
   KapaGraphWidget *graph;
@@ -19,7 +82,17 @@
   TickMarkData *ticks;
 
-  if (section == NULL) return;
+  *x0 = 0.0;
+  *y0 = 0.0;
+  *x1 = 0.0;
+  *y1 = 0.0;
+
+  *dXm = 0;
+  *dXp = 0;
+  *dYm = 0;
+  *dYp = 0;
+
+  if (section == NULL) return FALSE;
   graph = section->graph;
-  if (graph == NULL) return;
+  if (graph == NULL) return FALSE;
 
   graphic = GetGraphic ();
@@ -40,7 +113,7 @@
   // these depend on (a) existence of tick labels and (b) auto-offset or not
   if (isnan(graph[0].axis[0].labelPad)) {
-    dXm = (graph[0].axis[0].islabel) ? maxPAD  : minPAD;
-  } else {
-    dXm = graph[0].axis[0].labelPad * fontsize;
+    *dXm = (graph[0].axis[0].islabel) ? maxPAD  : minPAD;
+  } else {
+    *dXm = graph[0].axis[0].labelPad * fontsize;
   }
   if (isnan(graph[0].axis[0].pad)) {
@@ -53,7 +126,7 @@
   // these depend on (a) existence of tick labels and (b) auto-offset or not
   if (isnan(graph[0].axis[2].labelPad)) {
-    dXp = (graph[0].axis[2].islabel) ? maxPAD : minPAD;
-  } else {
-    dXp = graph[0].axis[2].labelPad * fontsize;
+    *dXp = (graph[0].axis[2].islabel) ? maxPAD : minPAD;
+  } else {
+    *dXp = graph[0].axis[2].labelPad * fontsize;
   }
   if (isnan(graph[0].axis[2].pad)) {
@@ -73,5 +146,5 @@
       if (!ticks[i].IsMajor) continue;
 
-      int Nchar = PrintTick (string, ticks[i].value, graph[0].axis[1].min, graph[0].axis[1].max);
+      int Nchar = PrintTick (string, ticks[i].value, graph[0].axis[1].min, graph[0].axis[1].max, ticks[i].nsignif);
 
       Nc = MAX (Nc, Nchar);
@@ -80,7 +153,7 @@
   }
   if (isnan(graph[0].axis[1].labelPad)) {
-    dYm = (graph[0].axis[1].islabel) ? (0.7*(Nc + 1.5)*fontsize) : minPAD;
-  } else {
-    dYm = graph[0].axis[1].labelPad * fontsize;
+    *dYm = (graph[0].axis[1].islabel) ? (0.7*(Nc + 1.5)*fontsize) : minPAD;
+  } else {
+    *dYm = graph[0].axis[1].labelPad * fontsize;
   }
   if (isnan(graph[0].axis[1].pad)) {
@@ -98,5 +171,5 @@
     for (i = 0; i < Nticks; i++) {
       if (!ticks[i].IsMajor) continue;
-      int Nchar = PrintTick (string, ticks[i].value, graph[0].axis[3].min, graph[0].axis[3].max);
+      int Nchar = PrintTick (string, ticks[i].value, graph[0].axis[3].min, graph[0].axis[3].max, ticks[i].nsignif);
       Nc = MAX (Nc, Nchar);
     }
@@ -104,7 +177,7 @@
   }
   if (isnan(graph[0].axis[3].labelPad)) {
-    dYp = (graph[0].axis[3].islabel) ? (0.7*(Nc + 1.5)*fontsize) : minPAD;
-  } else {
-    dYp = graph[0].axis[3].labelPad * fontsize;
+    *dYp = (graph[0].axis[3].islabel) ? (0.7*(Nc + 1.5)*fontsize) : minPAD;
+  } else {
+    *dYp = graph[0].axis[3].labelPad * fontsize;
   }
   if (isnan(graph[0].axis[3].pad)) {
@@ -115,8 +188,8 @@
 
   /* basic size of the graph in Xwindow coordinates, but measured from lower-left corner */
-  X0 = graphic[0].dx * section[0].x + padYm;
-  Y0 = graphic[0].dy * section[0].y + padXm;
-  dX = graphic[0].dx * section[0].dx - padYm - padYp;
-  dY = graphic[0].dy * section[0].dy - padXm - padXp;
+  *x0 = padYm;
+  *y0 = padXm;
+  *x1 = padYm + padYp;
+  *y1 = padXm + padXp;
 
   // if we are tied to an image, make mods as needed
@@ -128,70 +201,24 @@
     switch (section->image->location) {
       case 0:
-	// no changes?
 	break;
       case 1:
-        Y0 = graphic[0].dy * section[0].y  + padXm         + 2*PAD1 + WdY + 2;
-        dY = graphic[0].dy * section[0].dy - padXm - padXp - 4*PAD1 - 1 - WdY - COLORPAD;
+        *y0 = padXm + 2*PAD1 + WdY + 2;
+        *y1 = padXm + padXp + 4*PAD1 + 1 + WdY + COLORPAD;
         break;
       case 3:
-        dY = graphic[0].dy * section[0].dy - padXm - padXp - 4*PAD1 - 1 - WdY - COLORPAD;
+        *y1 = padXm + padXp + 4*PAD1 + 1 + WdY + COLORPAD;
         break;
       case 2:
-        X0 = graphic[0].dx * section[0].x  + padYm         + 2*PAD1 + ZOOM_X;
-        dX = graphic[0].dx * section[0].dx - padYm - padYp - 3*PAD1 - ZOOM_X;
-	dY = graphic[0].dy * section[0].dy - padXm - padXp - 2*PAD1 - COLORPAD;
+        *x0 = padYm + 2*PAD1 + ZOOM_X;
+        *x1 = padYm + padYp + 3*PAD1 + ZOOM_X;
+	*y1 = padXm + padXp + 2*PAD1 + COLORPAD;
         break;
       case 4:
-        dX = graphic[0].dx * section[0].dx - padYm - padYp - 3*PAD1 - ZOOM_X;
-	dY = graphic[0].dy * section[0].dy - padXm - padXp - 2*PAD1 - COLORPAD;
+        *x1 = padYm + padYp + 3*PAD1 + ZOOM_X;
+	*y1 = padXm + padXp + 2*PAD1 + COLORPAD;
         break;
     }
   }
 
-  /* define locations of coordinate axes */
-  graph[0].axis[0].fx  = X0;
-  graph[0].axis[0].fy  = graphic->dy - Y0;
-  graph[0].axis[0].dfx = dX;
-  graph[0].axis[0].dfy = 0;
-
-  graph[0].axis[1].fx  = X0;
-  graph[0].axis[1].fy  = graphic->dy - Y0;
-  graph[0].axis[1].dfx = 0;
-  graph[0].axis[1].dfy = -dY;
-
-  graph[0].axis[2].fx  = X0;
-  graph[0].axis[2].fy  = graphic->dy - Y0 - dY;
-  graph[0].axis[2].dfx = dX;
-  graph[0].axis[2].dfy = 0;
-
-  graph[0].axis[3].fx  = X0 + dX;
-  graph[0].axis[3].fy  = graphic->dy - Y0;
-  graph[0].axis[3].dfx = 0;
-  graph[0].axis[3].dfy = -dY;
-
-  /* define locations of axis labels */
-  graph[0].label[LABELLL].x = graph[0].axis[0].fx;
-  graph[0].label[LABELLL].y = graph[0].axis[0].fy + dXm;
-  graph[0].label[LABELX0].x = graph[0].axis[0].fx + 0.5*graph[0].axis[0].dfx;
-  graph[0].label[LABELX0].y = graph[0].axis[0].fy + dXm;
-  graph[0].label[LABELLR].x = graph[0].axis[0].fx + graph[0].axis[0].dfx;
-  graph[0].label[LABELLR].y = graph[0].axis[0].fy + dXm;
-
-  graph[0].label[LABELUL].x = graph[0].axis[2].fx;
-  graph[0].label[LABELUL].y = graph[0].axis[2].fy - dXp;
-  graph[0].label[LABELX1].x = graph[0].axis[2].fx + 0.5*graph[0].axis[2].dfx;
-  graph[0].label[LABELX1].y = graph[0].axis[2].fy - dXp;
-  graph[0].label[LABELUR].x = graph[0].axis[2].fx + graph[0].axis[2].dfx;
-  graph[0].label[LABELUR].y = graph[0].axis[2].fy - dXp;
-
-  graph[0].label[LABELY0].y = graph[0].axis[1].fy + 0.5*graph[0].axis[1].dfy;
-  graph[0].label[LABELY0].x = graph[0].axis[1].fx - dYm;
-
-  graph[0].label[LABELY1].y = graph[0].axis[3].fy + 0.5*graph[0].axis[3].dfy;
-  graph[0].label[LABELY1].x = graph[0].axis[3].fx + dYp;
-
-  // fprintf (stderr, "section %s, %f,%f : %f,%f\n", section->name, X0, Y0, dX, dY);
-
-  /* these are wrong and have to be adjusted to sit in the corners */
-
+  return (TRUE);
 }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawFrame.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawFrame.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawFrame.c	(revision 29815)
@@ -2,8 +2,8 @@
 // bDrawLine is a function, not a macro like DrawLine
 
-int bDrawFrame (KapaGraphWidget *graph) {
+int bDrawFrame (bDrawBuffer *buffer, KapaGraphWidget *graph) {
   
-  int i, j, Nticks, P;
-  double fx, fy, dfx, dfy, lweight;
+  int i, j, Nticks, P, doffset;
+  double fx, fy, dfx, dfy, lweight, dx, dy;
   // Graphic graphic; is not needed
   TickMarkData *ticks;
@@ -14,19 +14,27 @@
   /* each axis is drawn independently */
   for (i = 0; i < 4; i++) {
-    fx  = graph[0].axis[i].fx;
-    fy  = graph[0].axis[i].fy;
-    dfx = graph[0].axis[i].dfx;
-    dfy = graph[0].axis[i].dfy;
-    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);
-
     lweight = MAX (0, MIN (10, graph[0].axis[i].lweight));
     color = MAX (0, MIN (15, graph[0].axis[i].color));
 
-    bDrawSetStyle (color, lweight, 0);
-    // function about sets solor and weight
+    /* temporarily assume rectilinear axes */
+    doffset = ((int)(lweight) % 2) ? 0.5*(lweight - 1) : 0.5*lweight;
+    if (i == 0) { dx = doffset; dy = 0.0; }
+    if (i == 2) { dx = doffset; dy = 0.0; }
+    if (i == 1) { dx = 0.0; dy = doffset; }
+    if (i == 3) { dx = 0.0; dy = doffset; }
+
+    fx  = graph[0].axis[i].fx - dx;
+    fy  = graph[0].axis[i].fy - dy;
+    dfx = graph[0].axis[i].dfx + 2*dx;
+    dfy = graph[0].axis[i].dfy + 2*dy;
+    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);
+    P *= (1 + 0.25*lweight);
+
+    bDrawSetStyle (buffer, color, lweight, 0);
+    // function about sets color and weight
     // bDrawRotTextInit does not exist
 
     if (graph[0].axis[i].isaxis) { 
-      bDrawLine (fx, fy, fx+dfx, fy+dfy); 
+      bDrawLine (buffer, fx, fy, fx+dfx, fy+dfy); 
     }
     
@@ -34,5 +42,10 @@
       ticks = CreateAxisTicks (&graph[0].axis[i], &Nticks);
       for (j = 0; j < Nticks; j++) {
-	bDrawTick (&graph[0].axis[i], P, &ticks[j], i);
+	bDrawTick (buffer, &graph[0].axis[i], P, &ticks[j], i);
+
+	// XXX DrawTick sets the style to (color, 0, 0) for the font
+	// should the font weight stay the same?
+	// XXX probably not needed now:
+	// bDrawSetStyle (buffer, color, lweight, 0);
       }
       FREE (ticks);
@@ -42,5 +55,5 @@
 }
 
-void bDrawTick (Axis *axis, int P, TickMarkData *tick, int naxis) {
+void bDrawTick (bDrawBuffer *buffer, Axis *axis, int P, TickMarkData *tick, int naxis) {
   
   double x, y, dx, dy;
@@ -75,5 +88,5 @@
   dy = dir*size*dfx*n;
   
-  bDrawLine (x, y, x+dx, y+dy);
+  bDrawLine (buffer, x, y, x+dx, y+dy);
 
   if (tick->IsLabel) {
@@ -94,6 +107,6 @@
     yt = fy + (value-min)*dfy/(max - min) + dy;
 
-    PrintTick (string, value, min, max);
-    bDrawRotText (xt, yt, string, pos, 0.0);
+    PrintTick (string, value, min, max, tick->nsignif);
+    bDrawRotText (buffer, xt, yt, string, pos, 0.0);
   }
 }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawImage.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawImage.c	(revision 29815)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawImage.c	(revision 29815)
@@ -0,0 +1,190 @@
+# include "Ximage.h"
+
+# define WHITE_R 255
+# define WHITE_G 255
+# define WHITE_B 255
+# define MY_SWAP_INT(A,B) { int tmp; tmp = A; A = B; B = tmp; }
+
+int bDrawImage (bDrawBuffer *buffer, KapaImageWidget *image, Graphic *graphic) {
+
+  int ii, i, j;
+  int i_start, i_end, j_start, j_end;
+  int I_start, J_start;
+  int dropback;  /* this is a bit of a kludge... */
+  int dx, dy, DX, DY, inDX, inDY, Xs, Ys;
+  int expand_in, expand_out;
+  double expand, Ix, Iy;
+  unsigned char *out_pix;
+  unsigned short *in_pix, *in_pix_ref;
+  unsigned char *pixel1, *pixel2, *pixel3;
+
+  bDrawColor *line_buffer;
+
+  if (image == NULL) return (TRUE);
+
+  ALLOCATE (pixel1, unsigned char, graphic[0].Npixels);
+  ALLOCATE (pixel2, unsigned char, graphic[0].Npixels);
+  ALLOCATE (pixel3, unsigned char, graphic[0].Npixels);
+
+  /** cmap[i].pixel must be defined even if X is not used **/
+  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
+    pixel1[i] = graphic[0].cmap[i].red >> 8;
+    pixel2[i] = graphic[0].cmap[i].green >> 8;
+    pixel3[i] = graphic[0].cmap[i].blue >> 8;
+  }
+
+  assert ((image[0].picture.expand >= 1) || (image[0].picture.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;
+    expand_in  = 1;
+  }
+  if (image[0].picture.expand < 0) {
+    expand = fabs((double)image[0].picture.expand);
+    expand_out = 1;
+    expand_in  = -image[0].picture.expand;
+  }
+
+  Xs = image[0].picture.x;
+  Ys = image[0].picture.y;
+  dx = image[0].picture.dx;
+  dy = image[0].picture.dy;
+  DX = image[0].image[0].matrix.Naxis[0];
+  DY = image[0].image[0].matrix.Naxis[1];
+
+  if (buffer[0].Nx < Xs + dx) {
+    fprintf (stderr, "invalid condition\n");
+    abort();
+  }
+  if (buffer[0].Ny < Ys + dy) {
+    fprintf (stderr, "invalid condition\n");
+    abort();
+  }
+
+  // i_start, j_start are the closest lit screen pixel to 0,0
+  // I_start, J_start are the image pixel corresponding to i_start, j_start
+  Picture_Lower (&i_start, &j_start, &I_start, &J_start, &image[0].image[0].matrix, &image[0].picture);
+
+  // i_end, j_end are the closest lit screen pixel to dx, dy
+  // I_end, J_end are the image pixel corresponding to i_end, j_end
+  Picture_Upper (&i_end, &j_end, i_start, j_start, &image[0].image[0].matrix, &image[0].picture);
+
+  assert (i_start <= i_end);
+  assert (j_start <= j_end);
+
+  Ix = image[0].picture.flipx ? I_start - 1 : I_start;
+  Iy = image[0].picture.flipy ? J_start - 1 : J_start;
+
+  inDX = image[0].picture.flipx ? -1 : +1;
+  inDY = image[0].picture.flipy ? -1 : +1;
+
+  dropback = expand_out - (i_end - i_start) % expand_out;
+  if ((i_end - i_start) % expand_out == 0) dropback = 0;
+
+  ALLOCATE (line_buffer, bDrawColor, 3*dx);
+
+  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) **********/
+
+  /**** fill in bottom area ****/
+  out_pix = line_buffer;
+  for (i = 0; i < dx; i++, out_pix+=3) {
+    out_pix[0] = WHITE_R;
+    out_pix[1] = WHITE_G;
+    out_pix[2] = WHITE_B;
+  }
+  for (j = 0; j < j_start; j++) {
+    memcpy (&buffer[0].pixels[j + Ys][3*Xs], line_buffer, 3*dx);
+  }
+  
+  /*** fill in the image data region ***/
+  for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += inDY*expand_in*DX) {
+    
+    /* create one output image line */
+    in_pix = in_pix_ref;
+    out_pix = line_buffer;
+
+    /**** fill in area to the left of the picture ****/
+    for (i = 0; i < i_start; i++, out_pix+=3) {
+      out_pix[0] = WHITE_R;
+      out_pix[1] = WHITE_G;
+      out_pix[2] = WHITE_B;
+    }
+    
+    /*** fill in the picture region ***/
+    for (i = i_start; i < i_end; i+=expand_out, in_pix += inDX*expand_in) {
+      for (ii = 0; ii < expand_out; ii++, out_pix+=3) {
+	out_pix[0] = pixel1[*in_pix];
+	out_pix[1] = pixel2[*in_pix];
+	out_pix[2] = pixel3[*in_pix];
+      }
+    }
+    
+    /**** fill in area to the right of the picture ****/
+    for (i = i_end; i < dx; i++, out_pix+=3) {
+      out_pix[0] = WHITE_R;
+      out_pix[1] = WHITE_G;
+      out_pix[2] = WHITE_B;
+    }
+
+    /* write out the image line expand_out times */
+    for (i = 0; i < expand_out; i++) {
+      memcpy (&buffer[0].pixels[j + i + Ys][3*Xs], line_buffer, 3*dx);
+    }
+  }
+
+  /**** fill in top area ****/
+  out_pix = line_buffer;
+  for (i = 0; i < dx; i++, out_pix+=3) { 
+    out_pix[0] = WHITE_R;
+    out_pix[1] = WHITE_G;
+    out_pix[2] = WHITE_B;
+  }
+  for (j = j_end; j < dy; j++) {
+    memcpy (&buffer[0].pixels[j + Ys][3*Xs], line_buffer, 3*dx);
+  }
+
+  free (pixel1);
+  free (pixel2);
+  free (pixel3);
+  free (line_buffer);
+
+  return (TRUE);
+}
+
+# if (0)
+
+  /* I need to write the overlay objects on the jpeg image.
+     if i can write / overwrite data in jpeg buffer, then do it here,
+     otherwise i need to create a temporary image buffer, then write the 
+     scanlines to that buffer */
+
+  {
+    int Npalette;
+    png_color *palette;
+    bDrawColor white, color;
+    bDrawBuffer *buffer;
+
+    palette = KapaPNGPalette (&Npalette);
+
+    buffer = bDrawBufferCreate (dx, dy, 1);
+    for (i = 0; i < NOVERLAYS; i++) {
+      if (image[i].overlay[i].active) bDrawOverlay (image, i);
+    }
+
+    white = KapaColorByName ("white");
+    for (j = 0; j < dy; j++) {
+      for (i = 0; i < dx; i++) {
+	color = buffer[0].pixels[j][i];
+	if (color == white) continue;
+	image_buffer[j*3*dx + 3*i + 0] = palette[color].red;
+	image_buffer[j*3*dx + 3*i + 1] = palette[color].green;
+	image_buffer[j*3*dx + 3*i + 2] = palette[color].blue;
+      }
+    }
+    bDrawBufferFree (buffer);
+  }
+
+# endif
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawIt.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawIt.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawIt.c	(revision 29815)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-bDrawBuffer *bDrawIt () {
+bDrawBuffer *bDrawIt (png_color *palette, int Npalette, int Nbyte) {
 
   int i, Nsection;
@@ -13,7 +13,6 @@
   black = KapaColorByName ("black");
 
-  buffer = bDrawBufferCreate (graphic->dx, graphic->dy);
-  bDrawSetBuffer (buffer);
-  bDrawSetStyle (black, 0, 0);
+  buffer = bDrawBufferCreate (graphic->dx, graphic->dy, Nbyte, palette, Npalette);
+  bDrawSetStyle (buffer, black, 0, 0);
   
   // reset the sizes for all sections
@@ -21,6 +20,6 @@
   for (i = 0; i < Nsection; i++) {
       section = GetSectionByNumber (i);
-      bDrawGraph (section->graph);
-      // bDrawImage (section->image);
+      bDrawImage (buffer, section->image, graphic);
+      bDrawGraph (buffer, section->graph);
   }
 
@@ -28,9 +27,9 @@
 }
 
-void bDrawGraph (KapaGraphWidget *graph) {
+void bDrawGraph (bDrawBuffer *buffer, KapaGraphWidget *graph) {
   if (graph == NULL) return;
-  bDrawFrame (graph); 
-  bDrawObjects (graph);
-  bDrawLabels (graph);
-  bDrawTextlines (graph);
+  bDrawFrame (buffer, graph); 
+  bDrawObjects (buffer, graph);
+  bDrawLabels (buffer, graph);
+  bDrawTextlines (buffer, graph);
 }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawLabels.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawLabels.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawLabels.c	(revision 29815)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
   
-void bDrawLabels (KapaGraphWidget *graph) {
+void bDrawLabels (bDrawBuffer *buffer, KapaGraphWidget *graph) {
   
   int i, pos, x, y, size;
@@ -25,5 +25,5 @@
       y = graph[0].label[i].y;
       SetRotFont (graph[0].label[i].font, graph[0].label[i].size); 
-      bDrawRotText (x, y, graph[0].label[i].text, pos, angle);
+      bDrawRotText (buffer, x, y, graph[0].label[i].text, pos, angle);
     }
   }
@@ -31,5 +31,5 @@
 }
 
-void bDrawTextlines (KapaGraphWidget *graph) {
+void bDrawTextlines (bDrawBuffer *buffer, KapaGraphWidget *graph) {
 
   int i, x, y, size;
@@ -44,5 +44,5 @@
       y = graph[0].textline[i].y;
       SetRotFont (graph[0].textline[i].font, graph[0].textline[i].size);
-      bDrawRotText (x, y, graph[0].textline[i].text, 5, angle);
+      bDrawRotText (buffer, x, y, graph[0].textline[i].text, 5, angle);
     }
   }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawObjects.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawObjects.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawObjects.c	(revision 29815)
@@ -1,10 +1,10 @@
 # include "Ximage.h"
 
-# define DrawLine(X1,Y1,X2,Y2) (bDrawLine ((X1), (Y1), (X2), (Y2)))
-# define DrawCircle(X1,Y1,R) (bDrawCircle ((X1), (Y1), (R)))
-# define DrawRectangle(X,Y,dX,dY) (bDrawRectOpen ((X-0.5*dX), (Y-0.5*dY), (X+0.5*dX), (Y+0.5*dY)))
-# define FillRectangle(X,Y,dX,dY) (bDrawRectFill ((X-0.5*dX), (Y-0.5*dY), (X+0.5*dX), (Y+0.5*dY)))
-# define FillTriangle(X1,Y1,X2,Y2,X3,Y3) (bDrawTriFill ((X1), (Y1), (X2), (Y2), (X3), (Y3)))
-# define OpenTriangle(X1,Y1,X2,Y2,X3,Y3) (bDrawTriOpen ((X1), (Y1), (X2), (Y2), (X3), (Y3)))
+# define DrawLine(BUF,X1,Y1,X2,Y2) (bDrawLine (BUF, (X1), (Y1), (X2), (Y2)))
+# define DrawCircle(BUF,X1,Y1,R) (bDrawCircle (BUF, (X1), (Y1), (R)))
+# define DrawRectangle(BUF,X,Y,dX,dY) (bDrawRectOpen (BUF, (X-0.5*dX), (Y-0.5*dY), (X+0.5*dX), (Y+0.5*dY)))
+# define FillRectangle(BUF,X,Y,dX,dY) (bDrawRectFill (BUF, (X-0.5*dX), (Y-0.5*dY), (X+0.5*dX), (Y+0.5*dY)))
+# define FillTriangle(BUF,X1,Y1,X2,Y2,X3,Y3) (bDrawTriFill (BUF, (X1), (Y1), (X2), (Y2), (X3), (Y3)))
+# define OpenTriangle(BUF,X1,Y1,X2,Y2,X3,Y3) (bDrawTriOpen (BUF, (X1), (Y1), (X2), (Y2), (X3), (Y3)))
 
 # define CONNECT 0
@@ -14,5 +14,5 @@
 static Graphic *graphic;
 
-int bDrawObjects (KapaGraphWidget *graph) {
+int bDrawObjects (bDrawBuffer *buffer, KapaGraphWidget *graph) {
   
   int i;
@@ -30,30 +30,30 @@
     type = graph[0].objects[i].ltype;    
     color = graph[0].objects[i].color;
-    bDrawSetStyle (color, weight, type);
+    bDrawSetStyle (buffer, color, weight, type);
 
     switch (graph[0].objects[i].style) {
       case CONNECT: 
-	bDrawConnect (graph, &graph[0].objects[i]);
+	bDrawConnect (buffer, graph, &graph[0].objects[i]);
 	break;
       case HISTOGRAM:
-	bDrawHistogram (graph, &graph[0].objects[i]);
+	bDrawHistogram (buffer, graph, &graph[0].objects[i]);
 	break;
       case POINTS:
-	bDrawPoints (graph, &graph[0].objects[i]);
+	bDrawPoints (buffer, graph, &graph[0].objects[i]);
 	break;
     }
 
     if (graph[0].objects[i].etype & 0x01) {
-      bDrawYErrors (graph, &graph[0].objects[i]);
+      bDrawYErrors (buffer, graph, &graph[0].objects[i]);
     }
     if (graph[0].objects[i].etype & 0x02) {
-      bDrawXErrors (graph, &graph[0].objects[i]);
-    }
-  }
-  bDrawSetStyle (black, 0, 0);
+      bDrawXErrors (buffer, graph, &graph[0].objects[i]);
+    }
+  }
+  bDrawSetStyle (buffer, black, 0, 0);
   return (TRUE);
 }
 
-void bDrawConnect (KapaGraphWidget *graph, Gobjects *object) {
+void bDrawConnect (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
   
   int i;
@@ -91,10 +91,10 @@
     sx1 = x[i]*mxi + y[i]*mxj + bx;
     sy1 = x[i]*myi + y[i]*myj + by;
-    bDrawClipLine (sx0, sy0, sx1, sy1, X0, Y0, X1, Y1);
+    bDrawClipLine (buffer, sx0, sy0, sx1, sy1, X0, Y0, X1, Y1);
     sx0 = sx1; sy0 = sy1;
   }
 }
 
-void bDrawClipLine (double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0) {
+void bDrawClipLine (bDrawBuffer *buffer, double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0) {
 
   /* skip line segement if both points are beyond box */
@@ -145,9 +145,9 @@
     y1 = Y1;
   }
-  DrawLine (x0, y0, x1, y1);
+  DrawLine (buffer, x0, y0, x1, y1);
 }
 
 /*******/
-void bDrawHistogram (KapaGraphWidget *graph, Gobjects *object) {
+void bDrawHistogram (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
 
   int i;
@@ -192,7 +192,7 @@
     sy1 = MAX (MIN (sy1, Y0), Y1);
     sxa = 0.5*(sx0 + sx1);
-    DrawLine (sx0, sy0, sxa, sy0);
-    DrawLine (sxa, sy0, sxa, sy1);
-    DrawLine (sxa, sy1, sx1, sy1);
+    DrawLine (buffer, sx0, sy0, sxa, sy0);
+    DrawLine (buffer, sxa, sy0, sxa, sy1);
+    DrawLine (buffer, sxa, sy1, sx1, sy1);
     sx0 = sx1; sy0 = sy1;
   }
@@ -200,5 +200,5 @@
 
 /*******/
-void bDrawPoints (KapaGraphWidget *graph, Gobjects *object) {
+void bDrawPoints (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
  
   int i;
@@ -233,5 +233,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  FillRectangle (sx, sy, 2*d*z[i], 2*d*z[i]);
+	  FillRectangle (buffer, sx, sy, 2*d*z[i], 2*d*z[i]);
 	}
       }
@@ -245,5 +245,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawRectangle (sx, sy, 2*d*z[i], 2*d*z[i]);
+	  DrawRectangle (buffer, sx, sy, 2*d*z[i], 2*d*z[i]);
 	}
       }
@@ -257,6 +257,6 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx - d*z[i], sy, sx + d*z[i], sy);
-	  DrawLine (sx, sy - d*z[i], sx, sy + d*z[i]);
+	  DrawLine (buffer, sx - d*z[i], sy, sx + d*z[i], sy);
+	  DrawLine (buffer, sx, sy - d*z[i], sx, sy + d*z[i]);
 	}
       }
@@ -270,6 +270,6 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx + d*z[i], sy - d*z[i], sx - d*z[i], sy + d*z[i]);
-	  DrawLine (sx - d*z[i], sy - d*z[i], sx + d*z[i], sy + d*z[i]);
+	  DrawLine (buffer, sx + d*z[i], sy - d*z[i], sx - d*z[i], sy + d*z[i]);
+	  DrawLine (buffer, sx - d*z[i], sy - d*z[i], sx + d*z[i], sy + d*z[i]);
 	}
       }
@@ -283,5 +283,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  FillTriangle (sx - d*z[i], sy - 0.58*d*z[i], sx + d*z[i], sy - 0.58*d*z[i], sx, sy + 1.15*d*z[i]);
+	  FillTriangle (buffer, sx - d*z[i], sy - 0.58*d*z[i], sx + d*z[i], sy - 0.58*d*z[i], sx, sy + 1.15*d*z[i]);
 	}
       }
@@ -295,5 +295,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  OpenTriangle (sx - d*z[i], sy + 0.58*d*z[i], sx, sy - 1.15*d*z[i], sx + d*z[i], sy + 0.58*d*z[i]);
+	  OpenTriangle (buffer, sx - d*z[i], sy + 0.58*d*z[i], sx, sy - 1.15*d*z[i], sx + d*z[i], sy + 0.58*d*z[i]);
 	}
       }
@@ -307,7 +307,7 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx, sy, sx - d*z[i], sy + 0.58*d*z[i]);
-	  DrawLine (sx, sy, sx + d*z[i], sy + 0.58*d*z[i]);
-	  DrawLine (sx, sy, sx,          sy - 1.15*d*z[i]);
+	  DrawLine (buffer, sx, sy, sx - d*z[i], sy + 0.58*d*z[i]);
+	  DrawLine (buffer, sx, sy, sx + d*z[i], sy + 0.58*d*z[i]);
+	  DrawLine (buffer, sx, sy, sx,          sy - 1.15*d*z[i]);
 	}
       }
@@ -321,5 +321,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawCircle (sx, sy, d*z[i]);
+	  DrawCircle (buffer, sx, sy, d*z[i]);
 	}
       }
@@ -333,9 +333,9 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx + 0.00*d*z[i], sy - 1.00*d*z[i], sx + 0.95*d*z[i], sy - 0.31*d*z[i]);
-	  DrawLine (sx + 0.95*d*z[i], sy - 0.31*d*z[i], sx + 0.58*d*z[i], sy + 0.81*d*z[i]);
-	  DrawLine (sx + 0.58*d*z[i], sy + 0.81*d*z[i], sx - 0.58*d*z[i], sy + 0.81*d*z[i]);
-	  DrawLine (sx - 0.58*d*z[i], sy + 0.81*d*z[i], sx - 0.95*d*z[i], sy - 0.31*d*z[i]);
-	  DrawLine (sx - 0.95*d*z[i], sy - 0.31*d*z[i], sx + 0.00*d*z[i], sy - 1.00*d*z[i]);
+	  DrawLine (buffer, sx + 0.00*d*z[i], sy - 1.00*d*z[i], sx + 0.95*d*z[i], sy - 0.31*d*z[i]);
+	  DrawLine (buffer, sx + 0.95*d*z[i], sy - 0.31*d*z[i], sx + 0.58*d*z[i], sy + 0.81*d*z[i]);
+	  DrawLine (buffer, sx + 0.58*d*z[i], sy + 0.81*d*z[i], sx - 0.58*d*z[i], sy + 0.81*d*z[i]);
+	  DrawLine (buffer, sx - 0.58*d*z[i], sy + 0.81*d*z[i], sx - 0.95*d*z[i], sy - 0.31*d*z[i]);
+	  DrawLine (buffer, sx - 0.95*d*z[i], sy - 0.31*d*z[i], sx + 0.00*d*z[i], sy - 1.00*d*z[i]);
 	}
       }
@@ -349,11 +349,11 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx -      d*z[i], sy,               sx - 0.50*d*z[i], sy + 0.87*d*z[i]);
-	  DrawLine (sx - 0.50*d*z[i], sy + 0.87*d*z[i], sx + 0.50*d*z[i], sy + 0.87*d*z[i]);
-	  DrawLine (sx + 0.50*d*z[i], sy + 0.87*d*z[i], sx +      d*z[i], sy);
-
-	  DrawLine (sx +      d*z[i], sy,               sx + 0.50*d*z[i], sy - 0.87*d*z[i]);
-	  DrawLine (sx + 0.50*d*z[i], sy - 0.87*d*z[i], sx - 0.50*d*z[i], sy - 0.87*d*z[i]);
-	  DrawLine (sx - 0.50*d*z[i], sy - 0.87*d*z[i], sx -      d*z[i], sy);
+	  DrawLine (buffer, sx -      d*z[i], sy,               sx - 0.50*d*z[i], sy + 0.87*d*z[i]);
+	  DrawLine (buffer, sx - 0.50*d*z[i], sy + 0.87*d*z[i], sx + 0.50*d*z[i], sy + 0.87*d*z[i]);
+	  DrawLine (buffer, sx + 0.50*d*z[i], sy + 0.87*d*z[i], sx +      d*z[i], sy);
+
+	  DrawLine (buffer, sx +      d*z[i], sy,               sx + 0.50*d*z[i], sy - 0.87*d*z[i]);
+	  DrawLine (buffer, sx + 0.50*d*z[i], sy - 0.87*d*z[i], sx - 0.50*d*z[i], sy - 0.87*d*z[i]);
+	  DrawLine (buffer, sx - 0.50*d*z[i], sy - 0.87*d*z[i], sx -      d*z[i], sy);
 	}
       }
@@ -366,5 +366,5 @@
 	sx2 = x[i+1]*mxi + y[i+1]*mxj + bx;
 	sy2 = x[i+1]*myi + y[i+1]*myj + by;
-	DrawLine (sx1, sy1, sx2, sy2);
+	DrawLine (buffer, sx1, sy1, sx2, sy2);
       }
     }
@@ -380,5 +380,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  FillRectangle (sx, sy, 2*d, 2*d);
+	  FillRectangle (buffer, sx, sy, 2*d, 2*d);
 	}
       }
@@ -392,5 +392,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawRectangle (sx, sy, 2*d, 2*d);
+	  DrawRectangle (buffer, sx, sy, 2*d, 2*d);
 	}
       }
@@ -404,6 +404,6 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx - d, sy, sx + d, sy);
-	  DrawLine (sx, sy - d, sx, sy + d);
+	  DrawLine (buffer, sx - d, sy, sx + d, sy);
+	  DrawLine (buffer, sx, sy - d, sx, sy + d);
 	}
       }
@@ -417,6 +417,6 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx + d, sy - d, sx - d, sy + d);
-	  DrawLine (sx - d, sy - d, sx + d, sy + d);
+	  DrawLine (buffer, sx + d, sy - d, sx - d, sy + d);
+	  DrawLine (buffer, sx - d, sy - d, sx + d, sy + d);
 	}
       }
@@ -430,5 +430,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  FillTriangle (sx - d, sy - 0.58*d, sx + d, sy - 0.58*d, sx, sy + 1.15*d);
+	  FillTriangle (buffer, sx - d, sy - 0.58*d, sx + d, sy - 0.58*d, sx, sy + 1.15*d);
 	}
       }
@@ -442,5 +442,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  OpenTriangle (sx - d, sy + 0.58*d, sx + d, sy + 0.58*d, sx, sy - 1.15*d);
+	  OpenTriangle (buffer, sx - d, sy + 0.58*d, sx + d, sy + 0.58*d, sx, sy - 1.15*d);
 	}
       }
@@ -454,7 +454,7 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx, sy, sx - d, sy - 0.58*d);
-	  DrawLine (sx, sy, sx + d, sy - 0.58*d);
-	  DrawLine (sx, sy, sx,     sy + 1.15*d);
+	  DrawLine (buffer, sx, sy, sx - d, sy - 0.58*d);
+	  DrawLine (buffer, sx, sy, sx + d, sy - 0.58*d);
+	  DrawLine (buffer, sx, sy, sx,     sy + 1.15*d);
 	}
       }
@@ -468,5 +468,5 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawCircle (sx, sy, d);
+	  DrawCircle (buffer, sx, sy, d);
 	}
       }
@@ -480,9 +480,9 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx + 0.00*d, sy - 1.00*d, sx + 0.95*d, sy - 0.31*d);
-	  DrawLine (sx + 0.95*d, sy - 0.31*d, sx + 0.58*d, sy + 0.81*d);
-	  DrawLine (sx + 0.58*d, sy + 0.81*d, sx - 0.58*d, sy + 0.81*d);
-	  DrawLine (sx - 0.58*d, sy + 0.81*d, sx - 0.95*d, sy - 0.31*d);
-	  DrawLine (sx - 0.95*d, sy - 0.31*d, sx + 0.00*d, sy - 1.00*d);
+	  DrawLine (buffer, sx + 0.00*d, sy - 1.00*d, sx + 0.95*d, sy - 0.31*d);
+	  DrawLine (buffer, sx + 0.95*d, sy - 0.31*d, sx + 0.58*d, sy + 0.81*d);
+	  DrawLine (buffer, sx + 0.58*d, sy + 0.81*d, sx - 0.58*d, sy + 0.81*d);
+	  DrawLine (buffer, sx - 0.58*d, sy + 0.81*d, sx - 0.95*d, sy - 0.31*d);
+	  DrawLine (buffer, sx - 0.95*d, sy - 0.31*d, sx + 0.00*d, sy - 1.00*d);
 	}
       }
@@ -496,10 +496,10 @@
 	    (sy < graph[0].axis[1].fy) && (sy > graph[0].axis[1].fy + graph[0].axis[1].dfy))
 	{
-	  DrawLine (sx - 1.00*d, sy + 0.00*d, sx - 0.50*d, sy + 0.87*d);
-	  DrawLine (sx - 0.50*d, sy + 0.87*d, sx + 0.50*d, sy + 0.87*d);
-	  DrawLine (sx + 0.50*d, sy + 0.87*d, sx + 1.00*d, sy + 0.00*d);
-	  DrawLine (sx + 1.00*d, sy + 0.00*d, sx + 0.50*d, sy - 0.87*d);
-	  DrawLine (sx + 0.50*d, sy - 0.87*d, sx - 0.50*d, sy - 0.87*d);
-	  DrawLine (sx - 0.50*d, sy - 0.87*d, sx - 1.00*d, sy + 0.00*d);
+	  DrawLine (buffer, sx - 1.00*d, sy + 0.00*d, sx - 0.50*d, sy + 0.87*d);
+	  DrawLine (buffer, sx - 0.50*d, sy + 0.87*d, sx + 0.50*d, sy + 0.87*d);
+	  DrawLine (buffer, sx + 0.50*d, sy + 0.87*d, sx + 1.00*d, sy + 0.00*d);
+	  DrawLine (buffer, sx + 1.00*d, sy + 0.00*d, sx + 0.50*d, sy - 0.87*d);
+	  DrawLine (buffer, sx + 0.50*d, sy - 0.87*d, sx - 0.50*d, sy - 0.87*d);
+	  DrawLine (buffer, sx - 0.50*d, sy - 0.87*d, sx - 1.00*d, sy + 0.00*d);
 	}
       }
@@ -512,5 +512,5 @@
 	sx2 = x[i+1]*mxi + y[i+1]*mxj + bx;
 	sy2 = x[i+1]*myi + y[i+1]*myj + by;
-	DrawLine (sx1, sy1, sx2, sy2);
+	DrawLine (buffer, sx1, sy1, sx2, sy2);
       }
     }
@@ -519,5 +519,5 @@
     
 /*******/
-void bDrawXErrors (KapaGraphWidget *graph, Gobjects *object) {
+void bDrawXErrors (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
   
   int i, bar;
@@ -553,9 +553,9 @@
 	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
     {
-      DrawLine (sx0, sy0, sx1, sy1);
+      DrawLine (buffer, sx0, sy0, sx1, sy1);
       if (bar) {
 	sx10 = sy1 - sz;
 	sx11 = sy1 + sz;
-	DrawLine (sx1, sx10, sx1, sx11);
+	DrawLine (buffer, sx1, sx10, sx1, sx11);
       }
     }
@@ -570,9 +570,9 @@
 	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
     {
-      DrawLine (sx0, sy0, sx1, sy1);
+      DrawLine (buffer, sx0, sy0, sx1, sy1);
       if (bar) {
 	sx10 = sy1 - sz;
 	sx11 = sy1 + sz;
-	DrawLine (sx1, sx10, sx1, sx11);
+	DrawLine (buffer, sx1, sx10, sx1, sx11);
       }
     }
@@ -581,5 +581,5 @@
     
 /*******/
-void bDrawYErrors (KapaGraphWidget *graph, Gobjects *object) {
+void bDrawYErrors (bDrawBuffer *buffer, KapaGraphWidget *graph, Gobjects *object) {
   
   int i, bar;
@@ -615,9 +615,9 @@
 	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
     {
-      DrawLine (sx0, sy0, sx1, sy1);
+      DrawLine (buffer, sx0, sy0, sx1, sy1);
       if (bar) {
 	sx10 = sx1 - sz;
 	sx11 = sx1 + sz;
-	DrawLine (sx10, sy1, sx11, sy1);
+	DrawLine (buffer, sx10, sy1, sx11, sy1);
       }
     }
@@ -632,9 +632,9 @@
 	 (sy1 < graph[0].axis[1].fy) && (sy1 > graph[0].axis[1].fy + graph[0].axis[1].dfy)))
     {
-      DrawLine (sx0, sy0, sx1, sy1);
+      DrawLine (buffer, sx0, sy0, sx1, sy1);
       if (bar) {
 	sx10 = sx1 - sz;
 	sx11 = sx1 + sz;
-	DrawLine (sx10, sy1, sx11, sy1);
+	DrawLine (buffer, sx10, sy1, sx11, sy1);
       }
     }
Index: /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawOverlay.c
===================================================================
--- /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawOverlay.c	(revision 29814)
+++ /branches/eam_branches/ipp-20101103/Ohana/src/kapa2/src/bDrawOverlay.c	(revision 29815)
@@ -4,5 +4,5 @@
 static char name[4][16] = {"red", "green", "blue", "yellow"};
 
-void bDrawOverlay (KapaImageWidget *image, int N) {
+void bDrawOverlay (bDrawBuffer *buffer, KapaImageWidget *image, int N) {
 
   int i;
@@ -14,5 +14,5 @@
   /* translate color to bDrawColors : image[0].overlay[N].color */
   color = KapaColorByName (name[N]);
-  bDrawSetStyle (color, 0, 0);
+  bDrawSetStyle (buffer, color, 0, 0);
   
   expand = 1.0;
@@ -57,14 +57,14 @@
     switch (image[0].overlay[N].objects[i].type) {
       case KII_OVERLAY_LINE:
-	bDrawLine (X, Y, (X+dX), (Y+dY));
+	bDrawLine (buffer, X, Y, (X+dX), (Y+dY));
 	break;
       case KII_OVERLAY_TEXT:
-	bDrawRotText (X, Y, image[0].overlay[N].objects[i].text, 8, 0.0);
+	bDrawRotText (buffer, X, Y, image[0].overlay[N].objects[i].text, 8, 0.0);
 	break;
       case KII_OVERLAY_BOX:
 	dx = MAX (abs(dX),2) / 2;
 	dy = MAX (abs(dY),2) / 2;
-	bDrawRectOpen ((X-dx), (Y-dy), (X+dx), (Y+dy));
-	// bDrawRectOpen ((X-dx), (Y-dy), (X), (Y));
+	bDrawRectOpen (buffer, (X-dx), (Y-dy), (X+dx), (Y+dy));
+	// bDrawRectOpen (buffer, (X-dx), (Y-dy), (X), (Y));
 	break;
       case KII_OVERLAY_CIRCLE:
@@ -72,5 +72,5 @@
 	dy = MAX (abs(dY),2);
 	if (image[0].overlay[N].objects[i].angle == 0.0) {
-	  bDrawArc (X, Y, dx, dy, 0, 360);
+	  bDrawArc (buffer, X, Y, dx, dy, 0, 360);
 	} else {
 	  // moderately-stupid rotated ellipse drawing:
@@ -88,5 +88,5 @@
 	    x1 = X + pX*dx*cos(t)*cs - pX*dy*sin(t)*sn;
 	    y1 = Y + pY*dx*cos(t)*sn + pY*dy*sin(t)*cs;
-	    bDrawLine (x0, y0, x1, y1);
+	    bDrawLine (buffer, x0, y0, x1, y1);
 	    x0 = x1;
 	    y0 = y1;
@@ -101,5 +101,5 @@
   
   /* translate color to bDrawColors : image[0].overlay[N].color */
-  bDrawSetStyle (color, 0, 0);
+  bDrawSetStyle (buffer, color, 0, 0);
 }
 
