Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 36537)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 37050)
@@ -345,14 +345,14 @@
         if (newScaleZero) {
             // Choose an appropriate BSCALE and BZERO
- 	  if (!psFitsScaleDetermine(bscale, bzero, boffset, bsoften, blank, image, mask, maskVal, fits)) {
-                // We can't have the write dying for this reason --- try to save it somehow!
-                psWarning("Unable to determine BSCALE and BZERO for image --- refusing to quantise.");
-                psErrorClear();
-                return psMemIncrRefCounter((psImage*)image);
-            }
+	    if (!psFitsScaleDetermine(bscale, bzero, boffset, bsoften, blank, image, mask, maskVal, fits)) {
+		// We can't have the write dying for this reason --- try to save it somehow!
+		psWarning("Unable to determine BSCALE and BZERO for image --- refusing to quantise.");
+		psErrorClear();
+		return psMemIncrRefCounter((psImage*)image);
+	    }
         } else {
-            // Don't want to muck with the current BSCALE and BZERO.  Get the current values and use those.
-            int status = 0;                 // Status of cfitsio
-            if (fits_read_key_dbl(fits->fd, "BSCALE", bscale, NULL, &status) && status != KEY_NO_EXIST) {
+	    // Don't want to muck with the current BSCALE and BZERO.  Get the current values and use those.
+	    int status = 0;                 // Status of cfitsio
+	    if (fits_read_key_dbl(fits->fd, "BSCALE", bscale, NULL, &status) && status != KEY_NO_EXIST) {
                 psFitsError(status, true, "Unable to read header.");
                 return NULL;
@@ -371,5 +371,25 @@
         }
 
-        return psFitsScaleForDisk(image, fits, *bscale, *bzero, *boffset, *bsoften, rng);
+	// XXX this test triggers the pswarp memory corruption problem.  you need to include "psThread.h" above to activate this
+	// for (int nx = 0; nx < 3; nx++) { 
+	//     psList *pending = psThreadGetPendingQueue();
+	//     if (pending && pending->head) {
+	// 	fprintf (stderr, "thread jobs are still pending...\n");
+	//     }
+	//     // XXX can we mess up the memory?
+	//     psImage *temp1 = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);
+	//     fprintf (stderr, "corruption check set 1\n");
+	//     psMemCheckCorruption (stderr, true);
+	//     psImageInit (temp1, 2.0);
+	//     psFree (temp1);
+	//     fprintf (stderr, "corruption check set 2\n");
+	//     psMemCheckCorruption (stderr, true);
+	// }
+
+	// fprintf (stderr, "corruption check psFitsInsertImageWithMask\n");
+	// psMemCheckCorruption (stderr, true);
+
+        psImage *out = psFitsScaleForDisk(image, fits, *bscale, *bzero, *boffset, *bsoften, rng);
+        return out;
     }
 
@@ -638,6 +658,8 @@
     long blank = 0;                     // Blank (undefined) value for image
     psFitsFloat floatType;              // Custom floating-point convention type
-    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &boffset, &bsoften, &blank, &floatType, fits, image,
-                                                   mask, maskVal, NULL, true); // Image to write out
+
+    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &boffset, &bsoften, &blank, &floatType, 
+						   fits, image, mask, maskVal, NULL, true); // Image to write out
+
     if (!diskImage) {
         psError(PS_ERR_UNKNOWN, false, "Unable to convert image to desired disk format.");
@@ -666,4 +688,5 @@
         goto INSERT_DONE;
     }
+
     if (cfitsioBzero != 0.0) {
         psAssert(bzero == 0.0 && bscale == 1.0,
@@ -840,4 +863,5 @@
         }
     }
+
     if (psFitsError(status, true, "Could not write image to file.")) {
         success = false;
@@ -856,4 +880,5 @@
  INSERT_DONE:
     psFree(diskImage);
+
     if (compress) {
         // Restore compression state
@@ -906,4 +931,5 @@
     psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &boffset, &bsoften, &blank, &floatType, fits, input,
                                                    mask, maskVal, NULL, false); // Image to write out
+    
     if (!diskImage) {
         psError(PS_ERR_UNKNOWN, false, "Unable to convert image to desired disk format.");
@@ -932,4 +958,5 @@
         goto UPDATE_DONE;
     }
+
     if (cfitsioBzero != 0.0) {
         psAssert(bzero == 0.0 && bscale == 1.0,
@@ -979,5 +1006,5 @@
         goto UPDATE_DONE;
     }
-
+    
     // We only want cfitsio to do the scale and zero if the type conversion requires it (e.g., input type is
     // an unsigned integer type).  In all other cases, we have already converted the image to use the
