Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 17447)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 17660)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-04-17 23:43:02 $
+ *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-05-14 01:27:25 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -246,4 +246,5 @@
 static psImage *imageToDiskRepresentation(double *bscale, // Scaling applied
                                           double *bzero, // Zero point applied
+                                          long *blank, // Blank value (integer data)
                                           psFitsFloat *floatType, // Type of custom floating-point
                                           psFits *fits, // FITS file pointer
@@ -284,5 +285,5 @@
         if (newScaleZero) {
             // Choose an appropriate BSCALE and BZERO
-            if (!psFitsScaleDetermine(bscale, bzero, image, fits)) {
+            if (!psFitsScaleDetermine(bscale, bzero, blank, image, 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 --- setting to 1.0, 0.0");
@@ -365,8 +366,32 @@
     psAssert(!output->parent, "impossible");            // No parents means the buffer is contiguous
 
+    void *nullValue = NULL;             // Null value for data
+    float nullFloat = NAN;              // Null value for floating point
+    double nullDouble = NAN;            // Null value for double
+    switch (info->psDatatype) {
+      case PS_TYPE_F32:
+        nullValue = &nullFloat;
+        break;
+      case PS_TYPE_F64:
+        nullValue = &nullDouble;
+        break;
+      case PS_TYPE_U8:
+      case PS_TYPE_U16:
+      case PS_TYPE_U32:
+      case PS_TYPE_U64:
+      case PS_TYPE_S8:
+      case PS_TYPE_S16:
+      case PS_TYPE_S32:
+      case PS_TYPE_S64:
+        // Can't mark bad pixels any further than what is in the FITS image
+        break;
+      default:
+        psAbort("Unknown type: %x", info->psDatatype);
+    }
+
     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) {
+                         info->increment, nullValue, output->data.V[0], &anynull, &status) != 0) {
         psFitsError(status, true, "Reading FITS file failed.");
         return false;
@@ -498,6 +523,7 @@
 
     double bscale = 0.0, bzero = 0.0;   // Scale and zero point to put in header (*already* applied to data)
+    long blank = 0;                     // Blank (undefined) value for image
     psFitsFloat floatType;              // Custom floating-point convention type
-    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &floatType, fits, image,
+    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &blank, &floatType, fits, image,
                                                    NULL, true); // Image to write out
     if (!diskImage) {
@@ -576,4 +602,17 @@
     }
 
+    if (blank != 0) {
+        // Some quantisation has taken place --- record the blank ("magic") pixel value
+        char *keyword = (psFitsCompressionGetType(fits) != PS_FITS_COMPRESS_NONE &&
+                         (!fits->options || fits->options->conventions.compression)) ?
+            "ZBLANK" : "BLANK";         // Keyword for recording blank pixel value
+        fits_write_key_lng(fits->fd, keyword, blank, "Value for undefined pixels", &status);
+        fits_set_imgnull(fits->fd, blank, &status);
+        if (psFitsError(status, true, "Could not write BLANK header to file.")) {
+            psFree(diskImage);
+            return false;
+        }
+    }
+
     if (floatType != PS_FITS_FLOAT_NONE) {
         psFitsFloatImageSet(fits, floatType);
@@ -629,6 +668,7 @@
 
     double bscale = 0.0, bzero = 0.0;   // Scale and zero point to put in header (*already* applied to data)
+    long blank = 0;                     // Blank (undefined) value for image
     psFitsFloat floatType;              // Custom floating-point convention type
-    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &floatType, fits, input,
+    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &blank, &floatType, fits, input,
                                                    NULL, false); // Image to write out
     if (!diskImage) {
