Index: /trunk/psLib/psLib.kdevses
===================================================================
--- /trunk/psLib/psLib.kdevses	(revision 1318)
+++ /trunk/psLib/psLib.kdevses	(revision 1319)
@@ -2,50 +2,20 @@
 <!DOCTYPE KDevPrjSession>
 <KDevPrjSession>
- <DocsAndViews NumberOfDocuments="9" >
-  <Doc0 NumberOfViews="1" URL="file:/home/desonia/psLib/src/image/psImageExtraction.c" >
-   <View0 line="362" Type="???" >
-    <AdditionalSettings Top="2" Width="1270" Attach="1" Height="779" Left="2" MinMaxMode="0" />
+ <DocsAndViews NumberOfDocuments="3" >
+  <Doc0 NumberOfViews="1" URL="file:/home/desonia/psLib/src/image/psImageManip.c" >
+   <View0 line="0" Type="???" >
+    <AdditionalSettings Top="2" Width="1227" Attach="1" Height="794" Left="2" MinMaxMode="0" />
    </View0>
   </Doc0>
-  <Doc1 NumberOfViews="1" URL="file:/home/desonia/psLib/src/image/psImageExtraction.h" >
-   <View0 line="75" Type="???" >
-    <AdditionalSettings Top="2" Width="1526" Attach="1" Height="954" Left="2" MinMaxMode="0" />
+  <Doc1 NumberOfViews="1" URL="file:/home/desonia/psLib/test/image/tst_psImageManip.c" >
+   <View0 line="424" Type="???" >
+    <AdditionalSettings Top="2" Width="1227" Attach="1" Height="769" Left="2" MinMaxMode="0" />
    </View0>
   </Doc1>
-  <Doc2 NumberOfViews="1" URL="file:/home/desonia/psLib/src/collections/psVector.h" >
-   <View0 line="0" Type="???" >
-    <AdditionalSettings Top="2" Width="1243" Attach="1" Height="827" Left="2" MinMaxMode="0" />
+  <Doc2 NumberOfViews="1" URL="file:/home/desonia/psLib/src/astronomy/psCCD.h" >
+   <View0 line="93" Type="???" >
+    <AdditionalSettings Top="2" Width="1227" Attach="1" Height="769" Left="2" MinMaxMode="0" />
    </View0>
   </Doc2>
-  <Doc3 NumberOfViews="1" URL="file:/home/desonia/psLib/src/dataManip/psStats.h" >
-   <View0 line="0" Type="???" >
-    <AdditionalSettings Top="2" Width="1243" Attach="1" Height="827" Left="2" MinMaxMode="0" />
-   </View0>
-  </Doc3>
-  <Doc4 NumberOfViews="1" URL="file:/home/desonia/psLib/src/image/psImageManip.c" >
-   <View0 line="463" Type="???" >
-    <AdditionalSettings Top="2" Width="1196" Attach="1" Height="939" Left="2" MinMaxMode="0" />
-   </View0>
-  </Doc4>
-  <Doc5 NumberOfViews="1" URL="file:/home/desonia/psLib/src/collections/psType.h" >
-   <View0 line="120" Type="???" >
-    <AdditionalSettings Top="2" Width="1270" Attach="1" Height="779" Left="2" MinMaxMode="0" />
-   </View0>
-  </Doc5>
-  <Doc6 NumberOfViews="1" URL="file:/home/desonia/psLib/src/image/psImageIO.h" >
-   <View0 line="0" Type="???" >
-    <AdditionalSettings Top="2" Width="1270" Attach="1" Height="779" Left="2" MinMaxMode="0" />
-   </View0>
-  </Doc6>
-  <Doc7 NumberOfViews="1" URL="file:/home/desonia/psLib/src/image/psImageManip.h" >
-   <View0 line="0" Type="???" >
-    <AdditionalSettings Top="2" Width="1196" Attach="1" Height="939" Left="2" MinMaxMode="0" />
-   </View0>
-  </Doc7>
-  <Doc8 NumberOfViews="1" URL="file:/home/desonia/psLib/test/dataManip/tst_psImageStats03.c" >
-   <View0 line="35" Type="???" >
-    <AdditionalSettings Top="2" Width="1526" Attach="1" Height="942" Left="2" MinMaxMode="0" />
-   </View0>
-  </Doc8>
  </DocsAndViews>
  <pluginList>
Index: /trunk/psLib/src/image/psImageManip.c
===================================================================
--- /trunk/psLib/src/image/psImageManip.c	(revision 1318)
+++ /trunk/psLib/src/image/psImageManip.c	(revision 1319)
@@ -10,6 +10,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-07-24 02:00:21 $
+ *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-07-29 01:20:10 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -276,4 +276,5 @@
 
     if (input == NULL) {
+        psError(__func__,"Can not perform clip on NULL image");
         return 0;
     }
@@ -301,5 +302,6 @@
                 psError(__func__, "Specified vmin (%g%+gi) is outside of image's " \
                         typename " pixel range", \
-                        realfcn(vmin),imagfcn(vmin)); \
+                        creal(vmin),cimag(vmin)); \
+                break; \
             } \
             if (realfcn(vmax) > PS_MAX_##type || imagfcn(vmax) > PS_MAX_##type || \
@@ -307,5 +309,6 @@
                 psError(__func__, "Specified vmax (%g%+gi) is outside of image's " \
                         typename " pixel range", \
-                        realfcn(vmax),imagfcn(vmax)); \
+                        creal(vmax),cimag(vmax)); \
+                break; \
             } \
             for (unsigned int row = 0;row<numRows;row++) { \
@@ -313,8 +316,8 @@
                 for (unsigned int col = 0; col < numCols; col++) { \
                     if ( (realfcn(inputRow[col]) > realMax) || (imagfcn(inputRow[col]) > imagMax) ) { \
+                        inputRow[col] = (ps##type)vmax; \
+                        numClipped++; \
+                    } else if ( (realfcn(inputRow[col]) < realMin) || (imagfcn(inputRow[col]) < imagMin) ){ \
                         inputRow[col] = (ps##type)vmin; \
-                        numClipped++; \
-                    } else if ( (realfcn(inputRow[col]) < realMin) || (imagfcn(inputRow[col]) < imagMax) ){ \
-                        inputRow[col] = (ps##type)vmax; \
                         numClipped++; \
                     } \
Index: /trunk/psLib/test/image/tst_psImageManip.c
===================================================================
--- /trunk/psLib/test/image/tst_psImageManip.c	(revision 1318)
+++ /trunk/psLib/test/image/tst_psImageManip.c	(revision 1319)
@@ -6,6 +6,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-07-21 23:38:27 $
+ *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-07-29 01:20:10 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -17,4 +17,6 @@
 #include <stdlib.h>
 #include <string.h>                    // for memset
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #include "psTest.h"
@@ -24,4 +26,5 @@
 static int testImageClip(void);
 static int testImageClipNAN(void);
+static int testImageClipComplexRegion(void);
 static int testImageOverlay(void);
 static int testImageRebin(void);
@@ -32,4 +35,5 @@
                               {testImageClip,571,"psImageClip",0,false},
                               {testImageClipNAN,572,"psImageClipNAN",0,false},
+                              {testImageClipComplexRegion,673,"psImageClipComplexRegion",0,false},
                               {testImageOverlay,573,"psImageOverlay",0,false},
                               {testImageRebin,559,"psImageRebin",0,false},
@@ -287,4 +291,257 @@
 }
 
+int testImageClipComplexRegion(void)
+{
+    psImage* img = NULL;
+    unsigned int c = 1024;
+    unsigned int r = 2048;
+    int numClipped = 0;
+    int retVal;
+
+    psLogMsg(__func__,PS_LOG_INFO,
+             "psImageClipNaN shall modified pixel values of NaN with a specified value");
+
+    /*
+    1. Create a complex image with a wide range of complex values
+
+    2. call psImageClipComplexRegion with min and max where there is at least 
+       2 pixels in the image above) that is:
+        a) real(p) < real(min) && complex(p) < complex(min),
+        b) real(p) < real(min) && complex(min) < complex(p) < complex(max)
+        c) real(min) < real(p) < real(max) && complex(p) < complex(min)
+        d) real(min) < real(p) < real(max) && complex(min) < complex(p) < complex(max)
+        e) real(p) > real(max) && complex(min) < complex(p) < complex(max)
+        f) real(pmin) < real(p) < real(max) && complex(p) > complex(max)
+        g) real(p) > real(max) && complex(p) > complex(max)
+        h) real(p) < real(min) && complex(p) > complex(max)
+        i) real(p) > real(max) && complex(p) < complex(min)
+
+    3. verify that All pixels in case (a), (b), and (c) have the value vmin
+
+    4. verify that all pixels in case (d) are unchanged from input
+
+    5. verify that all pixels in case (e), (f), (g), (h), and (i) have the 
+       value vmax
+
+    */
+
+    #define testImageClipComplexByType(datatype,MIN,MAX) /* datatype must be complex */ \
+    /* create image */ \
+    img = psImageAlloc(c,r,PS_TYPE_##datatype); \
+    for (unsigned row=0;row<r;row++) { \
+        ps##datatype* imgRow = img->data.datatype[row]; \
+        for (unsigned col=0;col<c;col++) { \
+            imgRow[col] = row+I*col; \
+        } \
+    } \
+    \
+    retVal = psImageClipComplexRegion(img,MIN,-1.0-1.0*I,MAX,-2.0-2.0*I); \
+    \
+    numClipped = 0; \
+    for (unsigned row=0;row<r;row++) { \
+        ps##datatype* imgRow = img->data.datatype[row]; \
+        for (unsigned col=0;col<c;col++) { \
+            ps##datatype value = (ps##datatype)(row+I*col); \
+            if ( (row > creal(MAX)) || (col > cimag(MAX)) ) { \
+                numClipped++; \
+                value = -2.0-2.0*I; \
+            } else if ((row < creal(MIN)) || (col < cimag(MIN)) ) { \
+                numClipped++; \
+                value = -1.0-1.0*I; \
+            } \
+            if (cabs(imgRow[col]-value) > FLT_EPSILON) { \
+                psError(__func__,"Pixel value is not as expected (%g%+gi vs %g%+gi) at %d,%d", \
+                        creal(imgRow[col]),cimag(imgRow[col]),creal(value),cimag(value),col,row); \
+                return 1; \
+            } \
+        } \
+    } \
+    if (retVal != numClipped) { \
+        psError(__func__,"Expected %d clips, but got %d", \
+                numClipped,retVal); \
+        return 2; \
+    } \
+    psFree(img);
+
+    complex double min = ((double)r)/5.0+I*((double)c)/4.0;
+    complex double max = ((double)r)/3.0+I*((double)c)/2.0;
+
+    psLogMsg(__func__,PS_LOG_INFO,"Testing clipping at %g%+gi to %g%+gi for psC32",
+             creal(min),cimag(min),creal(max),cimag(max));
+
+    testImageClipComplexByType(C32,min,max);
+
+    psLogMsg(__func__,PS_LOG_INFO,"Testing clipping at %g%+gi to %g%+gi for psC64",
+             creal(min),cimag(min),creal(max),cimag(max));
+    testImageClipComplexByType(C64,min,max);
+
+    //  6. Call psImageClipComplexRegion with NULL input parameter; should error
+    //     but not stop execution.
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(NULL,0,0,0,0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for clips of a NULL image.");
+        return 3;
+    }
+
+
+    img = psImageAlloc(c,r,PS_TYPE_C32);
+    for (unsigned row=0;row<r;row++) {
+        psC32* imgRow = img->data.C32[row];
+        for (unsigned col=0;col<c;col++) {
+            imgRow[col] = row+I*col;
+        }
+    }
+
+    //  7. Call psImageClipComplexRegion with min > max; should error and return 0,
+    //     but not stop execution
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,10.0+I*1.0,-1.0,5.0+5.0*I,-2.0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for creal(min)>creal(max).");
+        return 3;
+    }
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,1.0+I*10.0,-1.0,5.0+5.0*I,-2.0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for cimag(min)>cimag(max).");
+        return 3;
+    }
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,10.0+I*10.0,-1.0,5.0+5.0*I,-2.0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for min>max.");
+        return 3;
+    }
+
+    //  8. Call psImageClipComplexRegion with the follow vmin/vmax values; each
+    //     should error and return 0, but not stop execution
+    //      a) vmin < datatype region's minimum
+    //      b) vmax < datatype region's minimum
+    //      c) vmin > datatype region's maximum
+    //      d) vmax > datatype region's maximum
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      -2.0*(double)FLT_MAX,
+                                      5.0+5.0*I,
+                                      0.0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmin not in datatype range.");
+        return 80;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      2.0*(double)FLT_MAX,
+                                      5.0+5.0*I,
+                                      0.0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmin not in datatype range.");
+        return 81;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      -2.0*(double)FLT_MAX*I,
+                                      5.0+5.0*I,
+                                      0.0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmin not in datatype range.");
+        return 82;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      2.0*(double)FLT_MAX*I,
+                                      5.0+5.0*I,
+                                      0.0);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmin not in datatype range.");
+        return 83;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      0.0,
+                                      5.0+5.0*I,
+                                      -2.0*(double)FLT_MAX);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmax not in datatype range.");
+        return 84;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      0.0,
+                                      5.0+5.0*I,
+                                      2.0*(double)FLT_MAX);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmax not in datatype range.");
+        return 85;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      0.0,
+                                      5.0+5.0*I,
+                                      -2.0*(double)FLT_MAX*I);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmax not in datatype range.");
+        return 87;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error:");
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      0.0,
+                                      5.0+5.0*I,
+                                      2.0*(double)FLT_MAX*I);
+    if (retVal != 0) {
+        psError(__func__,"Expected zero return for vmax not in datatype range.");
+        return 88;
+    }
+
+
+    // now check if vmin > vmax is OK
+    for (unsigned row=0;row<r;row++) {
+        psC32* imgRow = img->data.C32[row];
+        for (unsigned col=0;col<c;col++) {
+            imgRow[col] = row+I*col;
+        }
+    }
+    retVal = psImageClipComplexRegion(img,
+                                      1.0+I*1.0,
+                                      10.0,
+                                      5.0+5.0*I,
+                                      0.0);
+    if (retVal == 0) {
+        psError(__func__,"Didn't expect zero return for vmin > vmax.");
+        return 83;
+    }
+
+
+    psFree(img);
+    img = NULL;
+
+    //  9. Call psImageClipComplexRegion with the max value out of datatype's
+    //     range; should clip as expected (see step 1-5). Repeat with min value
+    //     out of datatype's range.
+
+    testImageClipComplexByType(C32,-(double)FLT_MAX*2.0-I*(double)FLT_MAX*2.0,10.0+I*10.0);
+    testImageClipComplexByType(C32,10.0+I*10.0,(double)FLT_MAX*2.0+I*(double)FLT_MAX*2.0);
+
+    return 0;
+}
+
 int testImageOverlay(void)
 {
@@ -858,4 +1115,5 @@
 
     // write results of various rotates to a file and verify with truth images
+    mkdir("temp",0777);
     remove
         ("temp/fOut.fits");
@@ -874,6 +1132,12 @@
         }
         sOut = psImageRotate(sOut,sImg,rot,-1.0,PS_INTERPOLATE_FLAT);
-        psImageWriteSection(fOut,0,0,0,NULL,index,"temp/fOut.fits");
-        psImageWriteSection(sOut,0,0,0,NULL,index,"temp/sOut.fits");
+        if (! psImageWriteSection(fOut,0,0,0,NULL,index,"temp/fOut.fits") ) {
+            psError(__func__,"Can not write to temp/fOut.fits, so why continue!?");
+            return 20;
+        }
+        if (! psImageWriteSection(sOut,0,0,0,NULL,index,"temp/sOut.fits") ) {
+            psError(__func__,"Can not write to temp/sOut.fits, so why continue!?");
+            return 21;
+        }
 
         // now, let's compare this with the verified file
