Index: /trunk/psLib/src/Makefile.am
===================================================================
--- /trunk/psLib/src/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 lib_LTLIBRARIES = libpslib.la
 
-libpslib_la_CPPFLAGS = $(SRCINC)
+libpslib_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) 
 libpslib_la_LIBADD = $(SRCSUBLIBS)
 libpslib_la_DEPENDENCIES = $(SRCSUBLIBS)
Index: /trunk/psLib/src/astro/Makefile.am
===================================================================
--- /trunk/psLib/src/astro/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/astro/Makefile.am	(revision 8412)
@@ -5,5 +5,5 @@
 noinst_LTLIBRARIES = libpslibastro.la
 
-libpslibastro_la_CPPFLAGS = $(SRCINC)
+libpslibastro_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC)
 libpslibastro_la_SOURCES = \
 	psTime.c \
Index: /trunk/psLib/src/db/Makefile.am
===================================================================
--- /trunk/psLib/src/db/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/db/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibdb.la
 
-libpslibdb_la_CPPFLAGS = $(SRCINC) $(MYSQL_CFLAGS)
+libpslibdb_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(MYSQL_CFLAGS)
 libpslibdb_la_SOURCES = \
 	psDB.c
Index: /trunk/psLib/src/fft/Makefile.am
===================================================================
--- /trunk/psLib/src/fft/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/fft/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibfft.la
 
-libpslibfft_la_CPPFLAGS = $(SRCINC) $(FFTW3_CFLAGS)
+libpslibfft_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(FFTW3_CFLAGS)
 libpslibfft_la_SOURCES = \
 	psImageFFT.c \
Index: /trunk/psLib/src/fits/Makefile.am
===================================================================
--- /trunk/psLib/src/fits/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/fits/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibfits.la
 
-libpslibfits_la_CFLAGS = $(SRCINC) $(CFITSIO_CFLAGS) $(AM_CFLAGS)
+libpslibfits_la_CFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(CFITSIO_CFLAGS) $(AM_CFLAGS)
 libpslibfits_la_SOURCES = \
 	psFits.c \
Index: /trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- /trunk/psLib/src/fits/psFitsImage.c	(revision 8411)
+++ /trunk/psLib/src/fits/psFitsImage.c	(revision 8412)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-08-08 23:32:23 $
+ *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-08-17 22:15:17 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -14,8 +14,10 @@
 
 #include <unistd.h>
-
+#include <assert.h>
+#include <string.h>
+
+#include "psAssert.h"
 #include "psFits.h"
 #include "psFitsImage.h"
-#include "string.h"
 #include "psError.h"
 
@@ -31,158 +33,229 @@
 #define MAX_STRING_LENGTH 256  // maximum length string for FITS routines
 
-psImage* psFitsReadImage(const psFits* fits,    // the psFits object
+// Information required to read a FITS file
+typedef struct
+{
+    int nAxis;                          // Number of axes
+    int bitPix;                         // Bits per pixel
+    long nAxes[3];                      // Length of each axis
+    long firstPixel[3];                 // lower-left corner of image subset
+    long lastPixel[3];                  // upper-right corner of image subset
+    long increment[3];                  // increment for image subset
+    int fitsDatatype;                   // cfitsio data type
+    int psDatatype;                     // psLib data type
+}
+p_psFitsReadInfo;
+
+static p_psFitsReadInfo *p_psFitsReadInfoAlloc(const psFits *fits, // The FITS file handle
+        psRegion region, // Region to read
+        int z // z-plane to read in cube
+                                              )
+{
+    PS_ASSERT_PTR_NON_NULL(fits, NULL);
+    PS_ASSERT_INT_NONNEGATIVE(z, NULL);
+
+    p_psFitsReadInfo *info = psAlloc(sizeof(p_psFitsReadInfo));
+    memset(info, 0, sizeof(p_psFitsReadInfo));
+
+    int status = 0;                     // CFITSIO status
+    char fitsErr[80];                   // CFITSIO error message string
+
+    // check to see if we even are positioned on an image HDU
+    int hduType;                        // Type of HDU
+    if (fits_get_hdu_type(fits->fd, &hduType, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                _("Could not determine the HDU type. CFITSIO Error: %s"),
+                fitsErr);
+        goto bad;
+    }
+    if (hduType != IMAGE_HDU) {
+        psError(PS_ERR_IO, true,
+                _("Current FITS HDU type must be an image."));
+        goto bad;
+    }
+
+    // Get the data type 'bitPix' from the FITS image
+    if (fits_get_img_equivtype(fits->fd, &info->bitPix, &status) != 0) {
+        fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                _("Could not determine image data type. CFITSIO Error: %s"),
+                fitsErr);
+        goto bad;
+    }
+
+    /* Get the dimensions 'nAxis' from the FITS image */
+    if (fits_get_img_dim(fits->fd, &info->nAxis, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                _("Could not determine image dimensions. CFITSIO Error: %s"),
+                fitsErr);
+        goto bad;
+    }
+
+    /* Validate the number of axis */
+    if ((info->nAxis < 2) || (info->nAxis > 3)) {
+        psError(PS_ERR_IO, true,
+                _("Image number of dimensions, %d, is not valid.  Only two or three dimensions supported for FITS I/O."),
+                info->nAxis);
+        goto bad;
+    }
+
+    /* Get the Image size from the FITS file */
+    if (fits_get_img_size(fits->fd, info->nAxis, info->nAxes, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                _("Could not determine image size. CFITSIO Error: %s"),
+                fitsErr);
+        goto bad;
+    }
+
+    info->firstPixel[0] = region.x0 + 1;
+    info->firstPixel[1] = region.y0 + 1;
+    info->firstPixel[2] = z + 1;
+
+    if (region.x1 > 0) {
+        info->lastPixel[0] = region.x1;
+    } else {
+        info->lastPixel[0] = info->nAxes[0] + region.x1; // n.b., region.x1 < 0
+    }
+    if (region.y1 > 0) {
+        info->lastPixel[1] = region.y1;
+    } else {
+        info->lastPixel[1] = info->nAxes[1] + region.y1; // n.b., region.y1 < 0
+    }
+    info->lastPixel[2] = z + 1;
+
+    info->increment[0] = 1;
+    info->increment[1] = 1;
+    info->increment[2] = 1;
+
+    switch (info->bitPix) {
+    case BYTE_IMG:
+        info->psDatatype = PS_TYPE_U8;
+        info->fitsDatatype = TBYTE;
+        break;
+    case SBYTE_IMG:
+        info->psDatatype = PS_TYPE_S8;
+        info->fitsDatatype = TSBYTE;
+        break;
+    case USHORT_IMG:
+        info->psDatatype = PS_TYPE_U16;
+        info->fitsDatatype = TUSHORT;
+        break;
+    case SHORT_IMG:
+        info->psDatatype = PS_TYPE_S16;
+        info->fitsDatatype = TSHORT;
+        break;
+    case ULONG_IMG:
+        info->psDatatype = PS_TYPE_U32;
+        info->fitsDatatype = TUINT;
+        break;
+    case LONG_IMG:
+        info->psDatatype = PS_TYPE_S32;
+        info->fitsDatatype = TINT;
+        break;
+    case LONGLONG_IMG:
+        info->psDatatype = PS_TYPE_S64;
+        info->fitsDatatype = TLONGLONG;
+        break;
+    case FLOAT_IMG:
+        info->psDatatype = PS_TYPE_F32;
+        info->fitsDatatype = TFLOAT;
+        break;
+    case DOUBLE_IMG:
+        info->psDatatype = PS_TYPE_F64;
+        info->fitsDatatype = TDOUBLE;
+        break;
+    default:
+        psError(PS_ERR_IO, true,
+                _("FITS image type, BITPIX=%d, is not supported."),
+                info->bitPix);
+        goto bad;
+    }
+
+    return info;
+
+    // Common error cleanup
+bad:
+    psFree(info);
+    return NULL;
+}
+
+// Read into an extant image of just the right size
+static bool fitsReadImage(psImage *output,   // Output image
+                          const psFits *fits, // FITS file handle
+                          p_psFitsReadInfo *info // Info on how to read
+                         )
+{
+    // n.b., this assumes contiguous image buffer
+    assert(output);
+    assert(fits);
+    assert(info);
+    assert(output->numCols == info->lastPixel[0] - info->firstPixel[0] + 1);
+    assert(output->numRows == info->lastPixel[1] - info->firstPixel[1] + 1); // Right size
+    assert(!output->parent);            // No parents means the buffer is contiguous
+
+    int anynull = 0;                    // Are there any NULLs in the data?
+    int status = 0;                     // cfitsio status
+    if (fits_read_subset(fits->fd, info->fitsDatatype, info->firstPixel, info->lastPixel,
+                         info->increment, NULL, output->data.V[0], &anynull, &status) != 0) {
+        char fitsErr[80];               // CFITSIO error message string
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                _("Reading FITS file failed. CFITSIO Error: %s"),
+                fitsErr);
+        return false;
+    }
+
+    return true;
+}
+
+psImage* psFitsReadImage(const psFits *fits, // the psFits object
                          psRegion region, // the region in the FITS image to read
-                         int z)           // the z-plane in the FITS image cube to read
-{
-    psS32 status = 0;           /* CFITSIO file vars */
-    psS32 nAxis = 0;
-    psS32 anynull = 0;
-    psS32 bitPix = 0;           /* Pixel type */
-    long nAxes[3];
-    long firstPixel[3];         /* lower-left corner of image subset */
-    long lastPixel[3];          /* upper-right corner of image subset */
-    long increment[3];          /* increment for image subset */
-    char fitsErr[80] = "";      /* CFITSIO error message string */
-    psS32 fitsDatatype = 0;
-    psS32 datatype = 0;
-
-    if (fits == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psFits object can not NULL."));
+                         int z          // the z-plane in the FITS image cube to read
+                        )
+{
+    p_psFitsReadInfo *info = p_psFitsReadInfoAlloc(fits, region, z);
+
+    psImage *output = psImageAlloc(info->lastPixel[0] - info->firstPixel[0] + 1,
+                                   info->lastPixel[1] - info->firstPixel[1] + 1,
+                                   info->psDatatype);
+
+    if (!fitsReadImage(output, fits, info)) {
+        psFree(info);
+        psFree(output);
         return NULL;
     }
 
-    // check to see if we even are positioned on an image HDU
-    int hdutype;
-    if ( fits_get_hdu_type(fits->fd,&hdutype, &status) != 0) {
-        char fitsErr[MAX_STRING_LENGTH];
-        (void)fits_get_errstatus(status, fitsErr);
-        psError(PS_ERR_IO, true,
-                _("Could not determine the HDU type. CFITSIO Error: %s"),
-                fitsErr);
+    psFree(info);
+    return output;
+}
+
+psImage* psFitsReadImageBuffer(psImage *output, // Output image buffer
+                               const psFits *fits,    // the psFits object
+                               psRegion region, // the region in the FITS image to read
+                               int z           // the z-plane in the FITS image cube to read
+                              )
+{
+    if (output && output->parent) {
+        psError(PS_ERR_IO, true, "Unable to read into a buffer for a child image.\n");
         return NULL;
     }
-    if (hdutype != IMAGE_HDU) {
-        psError(PS_ERR_IO, true,
-                _("Current FITS HDU type must be an image."));
+
+    p_psFitsReadInfo *info = p_psFitsReadInfoAlloc(fits, region, z);
+
+    output = psImageRecycle(output, info->lastPixel[0] - info->firstPixel[0] + 1,
+                            info->lastPixel[1] - info->firstPixel[1] + 1,
+                            info->psDatatype);
+
+    if (!fitsReadImage(output, fits, info)) {
+        psFree(info);
+        psFree(output);
         return NULL;
     }
 
-    /* Get the data type 'bitPix' from the FITS image */
-    if (fits_get_img_equivtype(fits->fd, &bitPix, &status) != 0) {
-        fits_get_errstatus(status, fitsErr);
-        psError(PS_ERR_IO, true,
-                _("Could not determine image data type. CFITSIO Error: %s"),
-                fitsErr);
-        return NULL;
-    }
-
-    /* Get the dimensions 'nAxis' from the FITS image */
-    if (fits_get_img_dim(fits->fd, &nAxis, &status) != 0) {
-        (void)fits_get_errstatus(status, fitsErr);
-        psError(PS_ERR_IO, true,
-                _("Could not determine image dimensions. CFITSIO Error: %s"),
-                fitsErr);
-        return NULL;
-    }
-
-    /* Validate the number of axis */
-    if ((nAxis < 2) || (nAxis > 3)) {
-        psError(PS_ERR_IO, true,
-                _("Image number of dimensions, %d, is not valid.  Only two or three dimensions supported for FITS I/O."),
-                nAxis);
-        return NULL;
-    }
-
-    /* Get the Image size from the FITS file */
-    if (fits_get_img_size(fits->fd, nAxis, nAxes, &status) != 0) {
-        (void)fits_get_errstatus(status, fitsErr);
-        psError(PS_ERR_IO, true,
-                _("Could not determine image size. CFITSIO Error: %s"),
-                fitsErr);
-        return NULL;
-    }
-
-    firstPixel[0] = region.x0 + 1;
-    firstPixel[1] = region.y0 + 1;
-    firstPixel[2] = z + 1;
-
-    if (region.x1 > 0) {
-        lastPixel[0] = region.x1;
-    } else {
-        lastPixel[0] = nAxes[0] + region.x1; // n.b., region.x1 < 0
-    }
-    if (region.y1 > 0) {
-        lastPixel[1] = region.y1;
-    } else {
-        lastPixel[1] = nAxes[1] + region.y1; // n.b., region.y1 < 0
-    }
-    lastPixel[2] = z + 1;
-
-    increment[0] = 1;
-    increment[1] = 1;
-    increment[2] = 1;
-
-    switch (bitPix) {
-    case BYTE_IMG:
-        datatype = PS_TYPE_U8;
-        fitsDatatype = TBYTE;
-        break;
-    case SBYTE_IMG:
-        datatype = PS_TYPE_S8;
-        fitsDatatype = TSBYTE;
-        break;
-    case USHORT_IMG:
-        datatype = PS_TYPE_U16;
-        fitsDatatype = TUSHORT;
-        break;
-    case SHORT_IMG:
-        datatype = PS_TYPE_S16;
-        fitsDatatype = TSHORT;
-        break;
-    case ULONG_IMG:
-        datatype = PS_TYPE_U32;
-        fitsDatatype = TUINT;
-        break;
-    case LONG_IMG:
-        datatype = PS_TYPE_S32;
-        fitsDatatype = TINT;
-        break;
-    case LONGLONG_IMG:
-        datatype = PS_TYPE_S64;
-        fitsDatatype = TLONGLONG;
-        break;
-    case FLOAT_IMG:
-        datatype = PS_TYPE_F32;
-        fitsDatatype = TFLOAT;
-        break;
-    case DOUBLE_IMG:
-        datatype = PS_TYPE_F64;
-        fitsDatatype = TDOUBLE;
-        break;
-    default:
-        psError(PS_ERR_IO, true,
-                _("FITS image type, BITPIX=%d, is not supported."),
-                bitPix);
-        return NULL;
-    }
-
-    psImage *output = psImageAlloc(lastPixel[0]-firstPixel[0]+1,
-                                   lastPixel[1]-firstPixel[1]+1,
-                                   datatype);
-
-    // n.b., this assumes contiguous image buffer
-    if (fits_read_subset(fits->fd, fitsDatatype, firstPixel, lastPixel, increment,
-                         NULL, output->data.V[0], &anynull, &status) != 0) {
-        psFree(output);
-        (void)fits_get_errstatus(status, fitsErr);
-        psError(PS_ERR_IO, true,
-                _("Reading FITS file failed. CFITSIO Error: %s"),
-                fitsErr);
-        return NULL;
-    }
-
+    psFree(info);
     return output;
-
 }
 
@@ -329,6 +402,6 @@
 
     // check to see if we are positioned on an image HDU
-    int hdutype;
-    if ( fits_get_hdu_type(fits->fd,&hdutype, &status) != 0) {
+    int hduType;
+    if ( fits_get_hdu_type(fits->fd, &hduType, &status) != 0) {
         char fitsErr[MAX_STRING_LENGTH];
         (void)fits_get_errstatus(status, fitsErr);
@@ -338,5 +411,5 @@
         return NULL;
     }
-    if (hdutype != IMAGE_HDU) {
+    if (hduType != IMAGE_HDU) {
         psError(PS_ERR_IO, true,
                 _("Current FITS HDU type must be an image."));
Index: /trunk/psLib/src/fits/psFitsImage.h
===================================================================
--- /trunk/psLib/src/fits/psFitsImage.h	(revision 8411)
+++ /trunk/psLib/src/fits/psFitsImage.h	(revision 8412)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-13 22:28:02 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-08-17 22:15:17 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -36,4 +36,11 @@
     int z                              ///< the z-plane in the FITS image cube to read
 );
+
+// Read an image into an extant buffer
+psImage* psFitsReadImageBuffer(psImage *output, // Output image buffer
+                               const psFits *fits,    // the psFits object
+                               psRegion region, // the region in the FITS image to read
+                               int z           // the z-plane in the FITS image cube to read
+                              );
 
 /** Writes an image, given the desired region and z-plane.
Index: /trunk/psLib/src/imageops/Makefile.am
===================================================================
--- /trunk/psLib/src/imageops/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/imageops/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibimageops.la
 
-libpslibimageops_la_CPPFLAGS = $(SRCINC) $(GSL_CFLAGS)
+libpslibimageops_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(GSL_CFLAGS)
 libpslibimageops_la_SOURCES = \
 	psImageBackground.c \
Index: /trunk/psLib/src/jpeg/Makefile.am
===================================================================
--- /trunk/psLib/src/jpeg/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/jpeg/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibjpeg.la
 
-libpslibjpeg_la_CPPFLAGS = $(SRCINC) $(JPEG_CFLAGS)
+libpslibjpeg_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(JPEG_CFLAGS)
 libpslibjpeg_la_SOURCES = \
 	psImageJpeg.c
Index: /trunk/psLib/src/math/Makefile.am
===================================================================
--- /trunk/psLib/src/math/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/math/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibmath.la
 
-libpslibmath_la_CPPFLAGS = $(SRCINC) $(GSL_CFLAGS)
+libpslibmath_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(GSL_CFLAGS)
 libpslibmath_la_SOURCES = \
 	psUnaryOp.c \
Index: /trunk/psLib/src/mathtypes/Makefile.am
===================================================================
--- /trunk/psLib/src/mathtypes/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/mathtypes/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibmathtypes.la
 
-libpslibmathtypes_la_CPPFLAGS = $(SRCINC)
+libpslibmathtypes_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC)
 libpslibmathtypes_la_SOURCES = \
 	psImage.c \
Index: /trunk/psLib/src/sys/Makefile.am
===================================================================
--- /trunk/psLib/src/sys/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/sys/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibsys.la
 
-libpslibsys_la_CPPFLAGS = $(SRCINC) $(CFITSIO_CFLAGS)
+libpslibsys_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(CFITSIO_CFLAGS)
 libpslibsys_la_SOURCES = \
 	psAbort.c \
Index: /trunk/psLib/src/types/Makefile.am
===================================================================
--- /trunk/psLib/src/types/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/types/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibtypes.la
 
-libpslibtypes_la_CPPFLAGS = $(SRCINC) $(CFITSIO_CFLAGS)
+libpslibtypes_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(CFITSIO_CFLAGS)
 libpslibtypes_la_SOURCES = \
 	psArray.c \
Index: /trunk/psLib/src/xml/Makefile.am
===================================================================
--- /trunk/psLib/src/xml/Makefile.am	(revision 8411)
+++ /trunk/psLib/src/xml/Makefile.am	(revision 8412)
@@ -3,5 +3,5 @@
 noinst_LTLIBRARIES = libpslibxml.la
 
-libpslibxml_la_CPPFLAGS = $(SRCINC) $(XML_CFLAGS)
+libpslibxml_la_CPPFLAGS = $(PSLIB_CFLAGS) $(SRCINC) $(XML_CFLAGS)
 libpslibxml_la_SOURCES = \
 	psXML.c
