Attached is a patch which adds coordinate transform support to
plimagefr, similar to what is available in the plshade*, plcont, plmap
and plmeridians functions.

This patch is rather large, mainly because it removes a lot of code.

- The dev_fastimg rendering path has been removed.  This was only used
[1] in the xwin driver, and does not work well with the custom
coordinate transform support added to plimagefr.
- The plimage functions have been restructured and hopefully
simplified in this process.
- An off-by-one bug was fixed during the code restructuring, which
would sometimes lead to a missing row and/or column of (scaled) pixels
in the plotted image.
- api.xml has been updated (hopefully correctly) to include the
addition of pltr and pltr_data parameters to plimagefr
- The interface to plimage has remained the same, only plimagefr and
related internal functions have been altered

[1] - There one file which I have found which references dev_fastimg
still: sys/win32/msdev/src/win3.cpp - I have not changed this as I
have no way to test the result.  It should just be a matter of
removing the appropriate line.  Similarly, examples/python/qplplot.py
calls plplot.plNoBufferNoPixmap() which looks like it calls one of the
removed functions from src/plimage.c.  This function was removed as
its only purpose, as far as I can tell, was part of the fastimg
rendering path.

This patch should apply cleanly against the latest PLplot SVN.  It
builds cleanly on my Debian Sid system building the C library and the
Cairo rendering devices.  C example 20 works as expected for xwin,
xcairo and pngcairo.

I realize that this is a large patch.  I can break it down in to
pieces if needed, but this is a complete patch as-is for the C portion
of PLplot.  None of the included language bindings have been altered.

Thank you for your time.

Hez

-- 
Hezekiah M. Carty
Graduate Research Assistant
University of Maryland
Department of Atmospheric and Oceanic Science
diff --git a/doc/docbook/src/api.xml b/doc/docbook/src/api.xml
index 5ec569c..bc7c204 100644
--- a/doc/docbook/src/api.xml
+++ b/doc/docbook/src/api.xml
@@ -5537,6 +5537,8 @@ device coordinates
         <paramdef><parameter>Dymax</parameter></paramdef>
         <paramdef><parameter>valuemin</parameter></paramdef>
         <paramdef><parameter>valuemax</parameter></paramdef>
+        <paramdef><parameter>pltr</parameter></paramdef>
+        <paramdef><parameter>pltr_data</parameter></paramdef>
       </funcprototype>
       </funcsynopsis>
     </para>
@@ -5620,6 +5622,42 @@ device coordinates
 	  </para>
 	</listitem>
       </varlistentry>
+      <varlistentry>
+	<term>
+	  <parameter>pltr</parameter>
+	  (<literal>void (*) (PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer)
+           </literal>, input)
+	</term>
+	<listitem>
+	  <para>
+	    Pointer to function that defines transformation between indices
+	    in array <literal><parameter>idata</parameter></literal> and the
+	    world coordinates (C only).  Transformation functions are
+	    provided in the PLplot library:
+	    &pltr0; for identity mapping, and &pltr1; and &pltr2; for
+	    arbitrary mappings respectively defined by one- and
+	    two-dimensional arrays.  In addition, user-supplied routines
+	    for the transformation can be used as well.  Examples of
+	    all of these approaches are given in
+	    <xref linkend="contour-plots-c"/>.
+	    The transformation function should
+	    have the form given by any of &pltr0;, &pltr1;, or &pltr2;.
+	  </para>
+	</listitem>
+      </varlistentry>
+      <varlistentry>
+	<term>
+	  <parameter>pltr_data</parameter>
+	  (<literal>PLPointer</literal>, input)
+	</term>
+	<listitem>
+	  <para>
+	    Extra parameter to help
+	    pass information to &pltr0;, &pltr1;, &pltr2;, or whatever routine
+	    that is externally supplied.
+	  </para>
+	</listitem>
+      </varlistentry>
     </variablelist>
 
     <para>
diff --git a/drivers/xwin.c b/drivers/xwin.c
index f561bca..5d6c871 100644
--- a/drivers/xwin.c
+++ b/drivers/xwin.c
@@ -193,7 +193,6 @@ static void  ConfigBufferingCmd (PLStream *pls, PLBufferingCB *ptr );
 static void  GetCursorCmd	(PLStream *pls, PLGraphicsIn *ptr);
 static void  FillPolygonCmd	(PLStream *pls);
 static void  XorMod		(PLStream *pls, PLINT *mod);
-static void  DrawImage          (PLStream *pls);
 
 /* Miscellaneous */
 
@@ -255,7 +254,6 @@ plD_init_xw(PLStream *pls)
     pls->dev_flush = 1;		/* Handle our own flushes */
     pls->dev_fill0 = 1;		/* Handle solid fills */
     pls->plbuf_write = 1; 	/* Activate plot buffer */
-    pls->dev_fastimg = 1;       /* is a fast image device */
     pls->dev_xor = 1;           /* device support xor mode */
 
 #ifndef HAVE_PTHREAD
@@ -731,14 +729,6 @@ plD_esc_xw(PLStream *pls, PLINT op, void *ptr)
 	ConfigBufferingCmd(pls, (PLBufferingCB *) ptr );
 	break;
 
-    case PLESC_IMAGE:
-	DrawImage(pls);
-	break;
-
-    case PLESC_IMAGEOPS:
-	imageops(pls, (PLINT *) ptr);
-	break;
-
     case PLESC_PL2DEVCOL:
 	PLColor_to_XColor(&pls->tmpcolor, (XColor *) ptr);
 	break;
@@ -3201,212 +3191,6 @@ GetImageErrorHandler(Display *display, XErrorEvent *error)
     return 1;
 }
 
-/*--------------------------------------------------------------------------*\
- * DrawImage()
- *
- * Fill polygon described in points pls->dev_x[] and pls->dev_y[].
- * Only solid color fill supported.
-\*--------------------------------------------------------------------------*/
-
-static void
-DrawImage(PLStream *pls)
-{
-    XwDev *dev = (XwDev *) pls->dev;
-    XwDisplay *xwd = (XwDisplay *) dev->xwd;
-    XImage *ximg = NULL;
-    XColor curcolor;
-    PLINT xmin, xmax, ymin, ymax, icol1;
-
-    int (*oldErrorHandler)();
-
-    float mlr, mtb;
-    float blt, brt, brb, blb;
-    float left, right;
-    int kx, ky;
-    int nx, ny, ix, iy;
-    int i, corners[4], r[4];
-
-    struct {
-	float x, y;
-    } Ppts[4];
-
-    CheckForEvents(pls);
-
-    xmin = dev->xscale * pls->imclxmin;
-    xmax = dev->xscale * pls->imclxmax;
-    ymin = dev->yscale * pls->imclymin;
-    ymax = dev->yscale * pls->imclymax;
-
-    nx = pls->dev_nptsX;
-    ny = pls->dev_nptsY;
-
-/* XGetImage() call fails if either the pixmap or window is not fully viewable! */
-    oldErrorHandler = XSetErrorHandler(GetImageErrorHandler);
-
-    XFlush(xwd->display);
-    if (dev->write_to_pixmap)
-	ximg = XGetImage( xwd->display, dev->pixmap, 0, 0, dev->width, dev->height,
-			  AllPlanes, ZPixmap);
-
-    if (dev->write_to_window)
-	ximg = XGetImage( xwd->display, dev->window, 0, 0, dev->width, dev->height,
-			  AllPlanes, ZPixmap);
-
-    XSetErrorHandler(oldErrorHandler);
-
-    if (ximg == NULL) {
-	plabort("Can't get image, the window must be partly off-screen, move it to fit screen");
-	return;
-    }
-
-    if (xwd->ncol1 == 0)
-	AllocCmap1(pls);
-    if (xwd->ncol1 < 2)
-	return;
-
-/* translate array for rotation */
-    switch ((int)(pls->diorot - 4.*floor(pls->diorot/4.))) {
-    case 0:
-	r[0]=0; r[1]=1; r[2]=2; r[3]=3; break;
-    case 1:
-	r[0]=1; r[1]=2; r[2]=3; r[3]=0; break;
-    case 2:
-	r[0]=2; r[1]=3; r[2]=0; r[3]=1; break;
-    case 3:
-	r[0]=3; r[1]=0; r[2]=1; r[3]=2;
-    }
-
-  /* after rotation and coordinate translation, each fill
-     lozangue will have coordinates (Ppts), slopes (m...)
-     and y intercepts (b...):
-
-           Ppts[3]
-             /\
-    mlr,blt /  \ mtb,brt
-           /    \
-   Ppts[0]<      > Ppts[2]
-           \    /
-    mtb,blt \  / mlr,brb
-             \/
-           Ppts[1]
-  */
-
-/* slope of left/right and top/bottom edges */
-    mlr = (dev->yscale * (pls->dev_iy[1] - pls->dev_iy[0])) /
-	(dev->xscale * (pls->dev_ix[1] - pls->dev_ix[0]));
-
-    mtb = (dev->yscale * (pls->dev_iy[ny] - pls->dev_iy[0])) /
-	(dev->xscale * (pls->dev_ix[ny] - pls->dev_ix[0]));
-
-    for(ix = 0; ix < nx-1; ix++) {
-	for(iy = 0; iy < ny-1; iy++) {
-	    corners[0] = ix*ny+iy; /* [ix][iy] */
-	    corners[1] = (ix+1)*ny+iy; /* [ix+1][iy] */
-	    corners[2] = (ix+1)*ny+iy+1; /* [ix+1][iy+1] */
-	    corners[3] = ix*ny+iy+1; /* [ix][iy+1] */
-
-	    for (i=0; i<4; i++) {
-		Ppts[i].x = dev->xscale * (pls->dev_ix[corners[r[i]]]);
-		Ppts[i].y = dev->yscale * (pls->dev_iy[corners[r[i]]]);
-	    }
-
-	/* if any corner is inside the draw area */
-	    if (Ppts[0].x >= xmin || Ppts[2].x <= xmax ||
-		Ppts[1].y >= ymin || Ppts[3].y <= ymax) {
-
-		Ppts[0].x = MAX(Ppts[0].x, xmin);
-		Ppts[2].x = MIN(Ppts[2].x, xmax);
-		Ppts[1].y = MAX(Ppts[1].y, ymin);
-		Ppts[3].y = MIN(Ppts[3].y, ymax);
-
-	    /* the Z array has size (nx-1)*(ny-1) */
-		icol1 = pls->dev_z[ix*(ny-1)+iy];
-
-	    /* only plot points within zmin/zmax range */
-		if (icol1 < pls->dev_zmin || icol1 > pls->dev_zmax)
-		    continue;
-
-		icol1 = icol1/(float)USHRT_MAX * (xwd->ncol1-1);
-		if (xwd->color)
-		    curcolor = xwd->cmap1[icol1];
-		else
-		    curcolor = xwd->fgcolor;
-
-	    /* Fill square between current and next points. */
-
-	    /* If the fill area is a single dot, accelerate the fill. */
-		if ( (fabs(Ppts[2].x - Ppts[0].x) == 1) &&
-		     (fabs(Ppts[3].y - Ppts[1].y) == 1)) {
-		    XPutPixel(ximg, Ppts[0].x, dev->height-1 - Ppts[0].y, curcolor.pixel);
-
-		/* integer rotate, accelerate */
-		} else if (pls->diorot == floor(pls->diorot)) {
-		    for( ky = Ppts[1].y; ky < Ppts[3].y; ky++)
-			for( kx = Ppts[0].x; kx < Ppts[2].x; kx++)
-			    XPutPixel(ximg, kx, dev->height-1 - ky, curcolor.pixel);
-
-		/* lozangue, scanline fill it */
-		} else {
-
-		/* y interception point of left/right top/bottom edges */
-		    blt = Ppts[0].y - mlr * Ppts[0].x;
-		    brb = Ppts[2].y - mlr * Ppts[2].x;
-
-		    brt = Ppts[2].y - mtb * Ppts[2].x;
-		    blb = Ppts[0].y - mtb * Ppts[0].x;
-
-		    for( ky = Ppts[1].y; ky < Ppts[3].y; ky++) {
-			left = MAX(((ky-blt)/mlr), ((ky-blb)/mtb));
-			right = MIN(((ky-brt)/mtb), ((ky-brb)/mlr));
-			for( kx = Ppts[0].x; kx < Ppts[2].x; kx++) {
-			    if (kx >= rint(left) && kx <= rint(right)) {
-				XPutPixel(ximg, kx, dev->height-1 - ky, curcolor.pixel);
-			    }
-			}
-		    }
-		}
-	    }
-	}
-    }
-
-    if (dev->write_to_pixmap)
-	XPutImage( xwd->display, dev->pixmap, dev->gc, ximg, 0, 0, 0, 0, dev->width, dev->height);
-
-    if (dev->write_to_window)
-	XPutImage( xwd->display, dev->window, dev->gc, ximg, 0, 0,
-		   0, 0, dev->width, dev->height);
-
-    XDestroyImage(ximg);
-}
-
-static void
-imageops(PLStream *pls, PLINT *ptr)
-{
-    XwDev *dev = (XwDev *) pls->dev;
-    XwDisplay *xwd = (XwDisplay *) dev->xwd;
-
-/* TODO: store/revert to/from previous state */
-
-    switch (*ptr) {
-    case ZEROW2D:
-	dev->write_to_window = 0;
-	break;
-
-    case ONEW2D:
-	dev->write_to_window = 1;
-	break;
-
-    case ZEROW2B:
-	dev->write_to_pixmap = 0;
-	break;
-
-    case ONEW2B:
-	XFlush(xwd->display);
-	dev->write_to_pixmap = 1;
-	break;
-    }
-}
-
 #else
 int
 pldummy_xwin()
diff --git a/include/plplot.h b/include/plplot.h
index adf7e09..f0f93ee 100644
--- a/include/plplot.h
+++ b/include/plplot.h
@@ -192,19 +192,11 @@ typedef void* PLPointer;
 #define PLESC_CLEAR		18      /* RL: clear graphics region */
 #define PLESC_DASH		19	/* RL: draw dashed line */
 #define PLESC_HAS_TEXT		20	/* driver draws text */
-#define PLESC_IMAGE		21	/* handle image */
-#define PLESC_IMAGEOPS          22      /* plimage related operations */
 #define PLESC_PL2DEVCOL		23	/* convert PLColor to device color */
 #define PLESC_DEV2PLCOL		24	/* convert device color to PLColor */
 #define PLESC_SETBGFG		25	/* set BG, FG colors */
 #define PLESC_DEVINIT		26	/* alternate device initialization */
 
-/* image operations */
-#define ZEROW2B   1
-#define ZEROW2D   2
-#define ONEW2B    3
-#define ONEW2D    4
-
 /* Window parameter tags */
 
 #define PLSWIN_DEVICE		1	/* device coordinates */
@@ -1460,9 +1452,11 @@ c_plstripd(PLINT id);
 
 PLDLLIMPEXP void
 c_plimagefr(PLFLT **idata, PLINT nx, PLINT ny,
-	 PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
-	 PLFLT Dxmin, PLFLT Dxmax, PLFLT Dymin, PLFLT Dymax,
-         PLFLT valuemin, PLFLT valuemax);
+        PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
+        PLFLT Dxmin, PLFLT Dxmax, PLFLT Dymin, PLFLT Dymax,
+        PLFLT valuemin, PLFLT valuemax,
+        void (*pltr) (PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer),
+        PLPointer pltr_data);
 
 /* plots a 2d image (or a matrix too large for plshade() ) - colors
    automatically scaled */
diff --git a/include/plplotP.h b/include/plplotP.h
index 3d9f8fe..48cd8ca 100644
--- a/include/plplotP.h
+++ b/include/plplotP.h
@@ -874,7 +874,8 @@ plP_fill(short *x, short *y, PLINT npts);
 /* draw image */
 
 void
-plP_image(short *x, short *y, unsigned short *z, PLINT nx, PLINT ny, PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy, unsigned short zmin, unsigned short zmax);
+plP_image(PLFLT *z, PLINT nx, PLINT ny, PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy,
+        void (*pltr) (PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer), PLPointer pltr_data);
 
 /* End of page */
 
@@ -941,24 +942,14 @@ plP_getinitdriverlist(char *names);
 PLINT
 plP_checkdriverinit( char *names);
 
-  /* disable writing to plot buffer and pixmap */
-void
-NoBufferNoPixmap(void);
-
-  /* restart writing to plot buffer and pixmap */
-void
-RestoreWrite2BufferPixmap(void);
-
-void
-grimage(short *x, short *y, unsigned short *z, PLINT nx, PLINT ny);
-
 int PLDLLIMPEXP
 plInBuildTree();
 
 void
-plimageslow(short *x, short *y, unsigned short *data, PLINT nx, PLINT ny,
-	    PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy,
-	    unsigned short zmin,  unsigned short zmax);
+plimageslow(PLFLT *idata, PLINT nx, PLINT ny,
+        PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy,
+        void (*pltr) (PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer),
+        PLPointer pltr_data);
 
 typedef struct {
   PLFLT xmin, ymin, dx, dy;} IMG_DT;
diff --git a/include/plstrm.h b/include/plstrm.h
index eb414d9..22141a5 100644
--- a/include/plstrm.h
+++ b/include/plstrm.h
@@ -161,7 +161,6 @@ typedef struct {
  * dev_di	PLINT	Set if driver wants to handle DI commands
  * dev_flush	PLINT	Set if driver wants to handle flushes itself
  * dev_swin	PLINT	Set if driver wants to handle 'set window' commands
- * dev_fastimg  PLINT   Set if driver has fast image drawing capabilities
  * dev_xor      PLINT   Set if driver supports xor mode.
  * dev_clear    PLINT   Set if driver support clear.
  * termin	PLINT	Set for interactive devices
@@ -521,7 +520,7 @@ typedef struct {
     PLINT color, colorset;
     PLINT family, member, finc, fflen, bytemax, famadv;
     PLINT dev_fill0, dev_fill1, dev_dash, dev_di, dev_flush, dev_swin;
-    PLINT dev_text, dev_xor, dev_clear, dev_fastimg;
+    PLINT dev_text, dev_xor, dev_clear;
 
     char DevName[80];
     FILE *OutFile;
diff --git a/src/plbuf.c b/src/plbuf.c
index f95b4e4..da57a1b 100644
--- a/src/plbuf.c
+++ b/src/plbuf.c
@@ -236,37 +236,6 @@ plbuf_state(PLStream *pls, PLINT op)
     }
 }
 
-
-/*--------------------------------------------------------------------------*\
- * plbuf_image()
- *
- * write image described in points pls->dev_x[], pls->dev_y[], pls->dev_z[].
- *                      pls->nptsX, pls->nptsY.
-\*--------------------------------------------------------------------------*/
-
-static void
-plbuf_image(PLStream *pls, IMG_DT *img_dt)
-{
-    PLINT npts = pls->dev_nptsX * pls->dev_nptsY;
-
-    dbug_enter("plbuf_image");
-
-    wr_data(pls, &pls->dev_nptsX, sizeof(PLINT));
-    wr_data(pls, &pls->dev_nptsY, sizeof(PLINT));
-
-    wr_data(pls, &img_dt->xmin, sizeof(PLFLT));
-    wr_data(pls, &img_dt->ymin, sizeof(PLFLT));
-    wr_data(pls, &img_dt->dx, sizeof(PLFLT));
-    wr_data(pls, &img_dt->dy, sizeof(PLFLT));
-
-    wr_data(pls, &pls->dev_zmin, sizeof(short));
-    wr_data(pls, &pls->dev_zmax, sizeof(short));
-
-    wr_data(pls, pls->dev_ix, sizeof(short) * npts);
-    wr_data(pls, pls->dev_iy, sizeof(short) * npts);
-    wr_data(pls, pls->dev_z, sizeof(unsigned short) * (pls->dev_nptsX-1)*(pls->dev_nptsY-1));
-}
-
 /*--------------------------------------------------------------------------*\
  * plbuf_text()
  *
@@ -317,7 +286,6 @@ plbuf_text(PLStream *pls, EscText *text)
  *
  *	PLESC_FILL	Fill polygon
  *	PLESC_SWIN	Set plot window parameters
- *      PLESC_IMAGE     Draw image
  *      PLESC_HAS_TEXT  Draw PostScript text
 \*--------------------------------------------------------------------------*/
 
@@ -336,9 +304,6 @@ plbuf_esc(PLStream *pls, PLINT op, void *ptr)
     case PLESC_SWIN:
 	plbuf_swin(pls, (PLWindow *) ptr);
 	break;
-    case PLESC_IMAGE:
-	plbuf_image(pls, (IMG_DT *) ptr);
-	break;
     case PLESC_HAS_TEXT:
       if(ptr!=NULL) /* Check required by GCW driver, please don't remove */
 	  plbuf_text(pls, (EscText *) ptr);
@@ -562,14 +527,10 @@ rdbuf_state(PLStream *pls)
  *
  *	PLESC_FILL	Fill polygon
  *	PLESC_SWIN	Set plot window parameters
- *      PLESC_IMAGE     Draw image
  *      PLESC_HAS_TEXT  Draw PostScript text
 \*--------------------------------------------------------------------------*/
 
 static void
-rdbuf_image(PLStream *pls);
-
-static void
 rdbuf_text(PLStream *pls);
 
 static void
@@ -588,9 +549,6 @@ rdbuf_esc(PLStream *pls)
     case PLESC_SWIN:
 	rdbuf_swin(pls);
 	break;
-    case PLESC_IMAGE:
-	rdbuf_image(pls);
-	break;
     case PLESC_HAS_TEXT:
         rdbuf_text(pls);
 	break;
@@ -619,54 +577,6 @@ rdbuf_fill(PLStream *pls)
 }
 
 /*--------------------------------------------------------------------------*\
- * rdbuf_image()
- *
- * .
-\*--------------------------------------------------------------------------*/
-
-static void
-rdbuf_image(PLStream *pls)
-{
-    short *dev_ix, *dev_iy;
-    unsigned short *dev_z, dev_zmin, dev_zmax;
-    PLINT nptsX,nptsY, npts;
-    PLFLT xmin, ymin, dx, dy;
-
-    dbug_enter("rdbuf_image");
-
-    rd_data(pls, &nptsX, sizeof(PLINT));
-    rd_data(pls, &nptsY, sizeof(PLINT));
-    npts = nptsX*nptsY;
-
-    rd_data(pls, &xmin, sizeof(PLFLT));
-    rd_data(pls, &ymin, sizeof(PLFLT));
-    rd_data(pls, &dx, sizeof(PLFLT));
-    rd_data(pls, &dy, sizeof(PLFLT));
-
-    rd_data(pls, &dev_zmin, sizeof(short));
-    rd_data(pls, &dev_zmax, sizeof(short));
-
-	/* NOTE:  Even though for memory buffered version all the data is in memory,
-	 * we still allocate and copy the data because I think that method works
-	 * better in a multithreaded environment.  I could be wrong.
-	 */
-    if (((dev_ix=(short *)malloc(npts*sizeof(short)))==NULL)||
-        ((dev_iy=(short *)malloc(npts*sizeof(short)))==NULL)||
-        ((dev_z=(unsigned short *)malloc((nptsX-1)*(nptsY-1)*sizeof(unsigned short)))==NULL))
-        plexit("rdbuf_image: Insufficient memory");
-
-    rd_data(pls, dev_ix, sizeof(short) * npts);
-    rd_data(pls, dev_iy, sizeof(short) * npts);
-    rd_data(pls, dev_z, sizeof(unsigned short) * (nptsX-1)*(nptsY-1));
-
-    plP_image(dev_ix, dev_iy, dev_z, nptsX, nptsY, xmin, ymin, dx, dy, dev_zmin, dev_zmax);
-
-    free(dev_ix);
-    free(dev_iy);
-    free(dev_z);
-}
-
-/*--------------------------------------------------------------------------*\
  * rdbuf_swin()
  *
  * Set up plot window parameters.
diff --git a/src/plcore.c b/src/plcore.c
index 27b5317..209e092 100644
--- a/src/plcore.c
+++ b/src/plcore.c
@@ -3422,76 +3422,17 @@ return(ret);
  *
  * Author: Alessandro Mirone, Nov 2001
  *
- *
+ * Updated by Hezekiah Carty, Mar 2008.
+ *   - Added support for pltr callback
+ *   - Removed the "fastimg" rendering path
  *
 \*--------------------------------------------------------------------------*/
 
 void
-plP_image(short *x, short *y, unsigned short *z , PLINT nx, PLINT ny, PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy, unsigned short zmin, unsigned short zmax)
+plP_image(PLFLT *z , PLINT nx, PLINT ny, PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy,
+          void (*pltr) (PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer), PLPointer pltr_data)
 {
-  PLINT i, npts;
-  short *xscl, *yscl;
-  int   plbuf_write;
-
   plsc->page_status = DRAWING;
 
-  if (plsc->dev_fastimg == 0) {
-    plimageslow(x, y, z, nx-1, ny-1,
-         xmin, ymin, dx, dy, zmin, zmax);
-    return ;
-  }
-
-  if (plsc->plbuf_write) {
-    IMG_DT img_dt;
-
-    img_dt.xmin=xmin;
-    img_dt.ymin=ymin;
-    img_dt.dx=dx;
-    img_dt.dy=dy;
-
-    plsc->dev_ix = x;
-    plsc->dev_iy = y;
-    plsc->dev_z = z;
-    plsc->dev_nptsX = nx;
-    plsc->dev_nptsY = ny;
-    plsc->dev_zmin = zmin;
-    plsc->dev_zmax = zmax;
-
-    plbuf_esc(plsc, PLESC_IMAGE, &img_dt);
-  }
-
-  /* avoid re-saving plot buffer while in plP_esc() */
-  plbuf_write = plsc->plbuf_write;
-  plsc->plbuf_write = 0;
-
-  npts = nx*ny;
-  if (plsc->difilt) { /* isn't this odd? when replaying the plot buffer, e.g., when resizing the window, difilt() is caled again! the plot buffer should already contain the transformed data--it would save a lot of time! (and allow for differently oriented plots when in multiplot mode) */
-    PLINT clpxmi, clpxma, clpymi, clpyma;
-
-    if (((xscl = (short *) malloc(nx*ny*sizeof(short)))==NULL)||
-        ((yscl = (short *) malloc(nx*ny*sizeof(short)))==NULL))
-       {
-         plexit("plP_image: Insufficient memory");
-       }
-
-    for (i = 0; i < npts; i++) {
-      xscl[i] = x[i];
-      yscl[i] = y[i];
-    }
-    sdifilt(xscl, yscl, npts, &clpxmi, &clpxma, &clpymi, &clpyma);
-    plsc->imclxmin = clpxmi;
-    plsc->imclymin = clpymi;
-    plsc->imclxmax = clpxma;
-    plsc->imclymax = clpyma;
-    grimage(xscl, yscl, z, nx, ny);
-    free(xscl);
-    free(yscl);
-  } else {
-    plsc->imclxmin = plsc->phyxmi;
-    plsc->imclymin = plsc->phyymi;
-    plsc->imclxmax = plsc->phyxma;
-    plsc->imclymax = plsc->phyyma;
-    grimage(x, y, z, nx, ny );
-  }
-  plsc->plbuf_write = plbuf_write;
+  plimageslow(z, nx, ny, xmin, ymin, dx, dy, pltr, pltr_data);
 }
diff --git a/src/plimage.c b/src/plimage.c
index 67be368..5421911 100644
--- a/src/plimage.c
+++ b/src/plimage.c
@@ -2,6 +2,7 @@
  *
  * Author: Alessandro Mirone, Nov 2001
  * Adapted: Joao Cardoso
+ * Updated: Hezekiah Carty 2008
  *
  * Copyright (C) 2004  Alan W. Irwin
  *
@@ -24,117 +25,69 @@
 
 #include "plplotP.h"
 
-/* Get better names, those are too criptic!
- *
- * ZEROW2B: zero writing to buffer ?
- * ZEROW2D: zero writing to display ?
- * ONEW2B: set writing to buffer ?
- * ONEW2D: set writing to display ?
- */
-
-void
-NoBufferNoPixmap()
-{
-  PLINT op = ZEROW2B;
-
-  plsc->plbuf_write = 0; /* TODO: store previous state */
-  plP_esc(PLESC_EXPOSE, NULL);
-  plP_esc(PLESC_IMAGEOPS, &op);
-}
-
-void
-RestoreWrite2BufferPixmap()
-{
-  PLINT op = ONEW2B;
-
-  plsc->plbuf_write = 1; /* TODO: revert from previous state */
-  plP_esc(PLESC_IMAGEOPS, &op);
-}
-
-void
-disabledisplay()
-{
-  PLINT op = ZEROW2D;
-
-  plP_esc(PLESC_IMAGEOPS, &op);
-}
-
-void
-enabledisplay()
-{
-  PLINT op = ONEW2D;
-
-  plP_esc(PLESC_IMAGEOPS, &op);
-  plP_esc(PLESC_EXPOSE, NULL);
-}
-
-
+#define COLOR_MIN 0.0
+#define COLOR_MAX 1.0
+#define COLOR_NO_PLOT (-1.0)
 
+/*
+ * NOTE: The plshade* functions require that both pltr and pltr_data are set
+ * in order for pltr to be used.  plimageslow does NOT require this, so it is
+ * up to the user to make sure pltr_data is something non-NULL if pltr
+ * requires it.
+ * Plottable values in idata must be scaled between COLOR_MIN and COLOR_MAX.
+ */
 void
-plimageslow(short *x, short *y, unsigned short *data, PLINT nx, PLINT ny,
-	    PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy,
-	    unsigned short zmin, unsigned short zmax)
+plimageslow(PLFLT *idata, PLINT nx, PLINT ny,
+            PLFLT xmin, PLFLT ymin, PLFLT dx, PLFLT dy,
+            void (*pltr) (PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer),
+            PLPointer pltr_data)
 {
+  // Indices
   PLINT ix, iy, i;
+  // Float coordinates
   PLFLT xf[4], yf[4];
-  short xs[5], ys[5];
+  // Translated (by pltr) coordinates
+  PLFLT tx, ty;
+  // The corners of a single filled region
   int corners[4];
-  unsigned short col;
+  // The color to use in the fill
+  PLFLT color;
 
   for (ix = 0; ix < nx ; ix++) {
     for (iy = 0; iy < ny ; iy++) {
-
-      col = data[ix*ny+iy];
-      /* only plot points within zmin/zmax range */
-      if (col < zmin || col > zmax)
-	continue;
-
-      plcol1(col/(float)USHRT_MAX);
-
-      if (plsc->plbuf_read == 1) {
-	/* buffer read, is a replot to a slow device. */
-
-	corners[0] = ix*(ny+1)+iy;       /* [ix][iy] */
-	corners[1] = (ix+1)*(ny+1)+iy;   /* [ix+1][iy] */
-	corners[2] = (ix+1)*(ny+1)+iy+1; /* [ix+1][iy+1] */
-	corners[3] = ix*(ny+1)+iy+1;     /* [ix][iy+1] */
-
-	for (i = 0; i < 4; i++) {
-	  xs[i] = x[corners[i]];
-	  ys[i] = y[corners[i]];
-	}
-	xs[4] = xs[0]; ys[4] = ys[0];
-	plP_fill(xs, ys, 5);
-
-      } else {
-
-	xf[0] = xf[1] = ix;
-	xf[2] = xf[3] = ix+1;
-	yf[0] = yf[3] = iy;
-	yf[1] = yf[2] = iy+1;
-
-	for (i = 0; i < 4; i++) {
-	  xf[i] = xmin + xf[i]*dx;
-	  yf[i] = ymin + yf[i]*dy;
-	}
-	plfill(4, xf, yf);
+      // Only plot values within in appropriate range
+      color = idata[ix * ny + iy];
+      if (color == COLOR_NO_PLOT)
+        continue;
+
+      // The color value has to be scaled to 0.0 -> 1.0 plcol1 color values
+      plcol1(color / COLOR_MAX);
+
+      xf[0] = xf[1] = ix;
+      xf[2] = xf[3] = ix + 1;
+      yf[0] = yf[3] = iy;
+      yf[1] = yf[2] = iy + 1;
+
+      if (pltr) {
+        for (i = 0; i < 4; i++) {
+          // Translate the points
+          (*pltr) (xf[i], yf[i], &tx, &ty, pltr_data);
+          xf[i] = tx;
+          yf[i] = ty;
+        }
+      }
+      else {
+        for (i = 0; i < 4; i++) {
+          // Automatic translation to the specified plot area
+          xf[i] = xmin + xf[i] * dx;
+          yf[i] = ymin + yf[i] * dy;
+        }
       }
+      plfill(4, xf, yf);
     }
   }
 }
 
-void
-grimage(short *x, short *y, unsigned short *z, PLINT nx, PLINT ny)
-{
-    plsc->dev_ix = x;
-    plsc->dev_iy = y;
-    plsc->dev_z = z;
-    plsc->dev_nptsX = nx;
-    plsc->dev_nptsY = ny;
-
-    plP_esc(PLESC_IMAGE, NULL);
-}
-
 /*-------------------------------------------------------------------------*\
  * plimagefr
  *
@@ -170,19 +123,18 @@ grimage(short *x, short *y, unsigned short *z, PLINT nx, PLINT ny)
  *       image content.
  *
 \*-------------------------------------------------------------------------*/
-
 void
 c_plimagefr(PLFLT **idata, PLINT nx, PLINT ny,
          PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
          PLFLT Dxmin, PLFLT Dxmax, PLFLT Dymin, PLFLT Dymax,
-         PLFLT valuemin, PLFLT valuemax)
+         PLFLT valuemin, PLFLT valuemax,
+         void (*pltr) (PLFLT, PLFLT, PLFLT *, PLFLT *, PLPointer),
+         PLPointer pltr_data)
 {
   PLINT nnx, nny, ix, iy, ixx, iyy, xm, ym;
   PLFLT dx, dy;
-  // Zf holds transformed image pixel values
-  // szmin and szmax are zmin and zmax scaled to unsigned short values
-  unsigned short *Zf, szmin, szmax;
-  short *Xf, *Yf;
+  // z holds scaled image pixel values
+  PLFLT *z;
   // This is used when looping through the image array, checking to
   // make sure the values are within an acceptable range.
   PLFLT datum;
@@ -202,86 +154,70 @@ c_plimagefr(PLFLT **idata, PLINT nx, PLINT ny,
     return;
   }
 
+  if (Dxmax < Dxmin || xmax < xmin || Dymax < Dymin || ymax < ymin) {
+    plabort("plimage: All (Dxmin < Dxmax) and (Dymin < Dymax) and (xmin < xmax) and (ymin < ymax) must hold.");
+    return;
+  }
+
+  // dx and dy are the plot-coordinates pixel sizes for an untransformed image
   dx = (xmax - xmin) / (nx - 1);
   dy = (ymax - ymin) / (ny - 1);
-  nnx = (Dxmax-Dxmin)/dx + 1;
-  nny = (Dymax-Dymin)/dy + 1;
 
-  if ((Zf = (unsigned short *) malloc(nny*nnx*sizeof(unsigned short)))==NULL)
-    {
-      plexit("plimage: Insufficient memory");
-    }
+  // Pixel dimensions of the (Dxmin, Dymin) to (Dxmax, Dymax) box
+  nnx = (Dxmax - Dxmin) / dx + 1;
+  nny = (Dymax - Dymin) / dy + 1;
 
-  xm = floor((Dxmin-xmin)/dx); ym = floor((Dymin-ymin)/dy);
+  if ((z = (PLFLT *) malloc(nny * nnx * sizeof(PLFLT))) == NULL) {
+    plexit("plimage: Insufficient memory");
+  }
+
+  // Offsets for the idata indices to select
+  // (Dxmin, Dymin) to (Dxmax, Dymax)
+  xm = floor((Dxmin - xmin) / dx);
+  ym = floor((Dymin - ymin) / dy);
 
-  // Go through the image values and scale them to fit in an
-  // unsigned short range.
+  // If no acceptable data range is given, then set the min/max data range
+  // to include all of the given data.
+  if (zmin == zmax) {
+    // Find the minimum and maximum values in the image, and make sure
+    plMinMax2dGrid(idata, nx, ny, &zmax, &zmin);
+  }
+
+  // Go through the image values and scale them to fit in
+  // the COLOR_MIN to COLOR_MAX range.
   // Any values greater than valuemax are set to valuemax,
   // and values less than valuemin are set to valuemin.
-  ixx=-1;
-  for (ix=xm; ix<xm+nnx; ix++) {
+  // Any values outside of zmin to zmax are flagged so they
+  // are not plotted.
+  ixx = -1;
+  for (ix = xm; ix < xm + nnx; ix++) {
     ixx++; iyy=0;
-    for (iy=ym; iy<ym+nny; iy++) {
+    for (iy = ym; iy < ym + nny; iy++) {
       datum = idata[ix][iy];
-      if (datum < valuemin) {
-        datum = valuemin;
+      if (datum < zmin || datum > zmax) {
+        // Set to a guaranteed-not-to-plot value
+        z[ixx * nny + iyy++] = COLOR_NO_PLOT;
       }
-      else if (datum > valuemax) {
-        datum = valuemax;
+      else {
+        if (datum < valuemin) {
+          datum = valuemin;
+        }
+        else if (datum > valuemax) {
+          datum = valuemax;
+        }
+        // Set to a value scaled between COLOR_MIN and COLOR_MAX
+        z[ixx * nny + iyy++] = (datum - valuemin) / (valuemax - valuemin);
       }
-      Zf[ixx*nny+iyy++] =
-        (datum - valuemin) / (valuemax - valuemin) * USHRT_MAX;
     }
   }
 
-  if (zmin == zmax) {
-    zmin = valuemin;
-    zmax = valuemax;
-  }
-  else {
-    if (zmin < valuemin)
-      zmin = valuemin;
-    if (zmax > valuemax)
-      zmax = valuemax;
-  }
-
-  // The value range to plot, scaled to unsigned short values
-  szmin = (zmin - valuemin) / (valuemax - valuemin) * USHRT_MAX;
-  szmax = (zmax - valuemin) / (valuemax - valuemin) * USHRT_MAX;
-
-  xmin = Dxmin;  xmax = Dxmax;
-  ymin = Dymin;  ymax = Dymax;
+  plP_image(z, nnx, nny, Dxmin, Dymin, dx, dy, pltr, pltr_data);
 
-  /* The X and Y arrays has size nnx*nny */
-  nnx++; nny++;
-
-  if (((Xf = (short *) malloc(nny*nnx*sizeof(short)))==NULL)||
-      ((Yf = (short *) malloc(nny*nnx*sizeof(short)))==NULL))
-      {
-        plexit("plimage: Insufficient memory");
-      }
-
-  /* adjust the step for the X/Y arrays */
-  dx = dx*(nx-1)/nx;
-  dy = dy*(ny-1)/ny;
-
-  for (ix = 0; ix < nnx; ix++) {
-    for (iy = 0; iy < nny; iy++) {
-      Xf[ix*nny+iy] =  plP_wcpcx(xmin + ix*dx);
-      Yf[ix*nny+iy] =  plP_wcpcy(ymin + iy*dy);
-    }
-  }
-
-  plP_image(Xf, Yf, Zf, nnx, nny, xmin, ymin, dx, dy, szmin, szmax);
-
-  free(Xf);
-  free(Yf);
-  free(Zf);
+  free(z);
 }
 
 /*-------------------------------------------------------------------------*\
  * plimage
- *           (***** SUBJECT TO CHANGE ******)
  *
  * arguments are
  *   idata: array containing image data
@@ -302,11 +238,10 @@ c_plimagefr(PLFLT **idata, PLINT nx, PLINT ny,
  *       inside the [Dxmin->Dxmax]X[Dymin->Dymax] window
  *
 \*-------------------------------------------------------------------------*/
-
 void
 c_plimage(PLFLT **idata, PLINT nx, PLINT ny,
-	PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
-	PLFLT Dxmin, PLFLT Dxmax, PLFLT Dymin, PLFLT Dymax)
+          PLFLT xmin, PLFLT xmax, PLFLT ymin, PLFLT ymax, PLFLT zmin, PLFLT zmax,
+          PLFLT Dxmin, PLFLT Dxmax, PLFLT Dymin, PLFLT Dymax)
 {
   PLINT ix, iy;
   PLFLT data_min, data_max, iz;
@@ -319,14 +254,14 @@ c_plimage(PLFLT **idata, PLINT nx, PLINT ny,
     for (iy = 0; iy < ny; iy++) {
       iz = idata[ix][iy];
       if (data_max < iz)
-	data_max = iz;
+        data_max = iz;
       if (data_min > iz)
-	data_min = iz;
+        data_min = iz;
     }
   }
 
   // Call plimagefr with the value -> color range mapped to the minimum
   // and maximum values in idata.
   plimagefr(idata, nx, ny, xmin, xmax, ymin, ymax, zmin, zmax,
-           Dxmin, Dxmax, Dymin, Dymax, data_min, data_max);
+            Dxmin, Dxmax, Dymin, Dymax, data_min, data_max, NULL, NULL);
 }
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel

Reply via email to