Index: trunk/psModules/src/camera/pmFPAMaskWeight.c
===================================================================
--- trunk/psModules/src/camera/pmFPAMaskWeight.c	(revision 29543)
+++ trunk/psModules/src/camera/pmFPAMaskWeight.c	(revision 29544)
@@ -476,6 +476,29 @@
 }
 
-
-
+// find any pixels which are not already masked (with maskTest) which are not valid and raise maskSet bits
+bool pmReadoutMaskInvalid (const pmReadout *readout, psImageMaskType maskTest, psImageMaskType maskSet) {
+
+    if (!readout) return true;
+
+    psImage *image = readout->image;
+    psImage *mask  = readout->mask;
+    psImage *variance = readout->variance;
+    for (int y = 0; y < image->numRows; y++) {
+        for (int x = 0; x < image->numCols; x++) {
+            if (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskTest) continue;
+            bool valid = false;
+            valid = isfinite(image->data.F32[y][x]);
+            if (valid && variance) {
+                valid &= isfinite(variance->data.F32[y][x]);
+            }
+            if (valid) continue;
+            mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskSet;
+        }
+    }
+
+    return true;
+}
+
+// raise maskVal for any invalid pixels
 bool pmReadoutMaskNonfinite(pmReadout *readout, psImageMaskType maskVal)
 {
Index: trunk/psModules/src/camera/pmFPAMaskWeight.h
===================================================================
--- trunk/psModules/src/camera/pmFPAMaskWeight.h	(revision 29543)
+++ trunk/psModules/src/camera/pmFPAMaskWeight.h	(revision 29544)
@@ -99,4 +99,7 @@
     );
 
+// find any pixels which are not already masked (with maskTest) which are not valid and raise maskSet bits
+bool pmReadoutMaskInvalid (const pmReadout *readout, psImageMaskType maskTest, psImageMaskType maskSet);
+
 /// Apply a mask to the image and variance map
 ///
Index: trunk/psModules/src/camera/pmFPA_JPEG.c
===================================================================
--- trunk/psModules/src/camera/pmFPA_JPEG.c	(revision 29543)
+++ trunk/psModules/src/camera/pmFPA_JPEG.c	(revision 29544)
@@ -179,8 +179,14 @@
     psFree(stats);
 
+    // default options are: no flip in X or Y, scale bar on bottom
+    psImageJpegOptions *jpegOptions = psImageJpegOptionsAlloc();
+
     char *colormapName = psMetadataLookupStr(NULL, options, "COLORMAP"); // Name of colour map
     if (!colormapName) {
         colormapName = "-greyscale";
     }
+    psImageJpegColormapSet(jpegOptions, colormapName);
+
+    // set up the scale options
     char *mode = psMetadataLookupStr(NULL, options, "SCALE.MODE"); // Mode for scaling image
     if (!mode) {
@@ -195,40 +201,51 @@
         fmax = +6.0;
     }
-
-    float min = 0, max = 0;             // Minimum and maximum for stretch
-
     if (!strcasecmp(mode, "RANGE")) {
-        min = mean + fmin*delta;
-        max = mean + fmax*delta;
+        jpegOptions->min = mean + fmin*delta;
+        jpegOptions->max = mean + fmax*delta;
     } else if (!strcasecmp(mode, "FRACTION")) {
-        min = fmin*mean;
-        max = fmax*mean;
+        jpegOptions->min = fmin*mean;
+        jpegOptions->max = fmax*mean;
     } else if (!strcasecmp(mode, "VALUE")) {
-        min = fmin;
-        max = fmax;
+	jpegOptions->min = fmin;
+        jpegOptions->max = fmax;
     } else {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised scaling mode: %s", mode);
         return false;
     }
-
-    if (!isfinite(min) || !isfinite(max)) {
+    if (!isfinite(jpegOptions->min) || !isfinite(jpegOptions->max)) {
         psLogMsg("psModules.jpeg", PS_LOG_WARN,
                  "The stretch parameters are not both finite --- writing blank jpeg");
-        min = 0;
-        max = 1;
-    }
-
-    psImageJpegColormap *map = psImageJpegColormapSet(NULL, colormapName);
-    if (!map) {
-        map = psImageJpegColormapSet(NULL, "-greyscale");
-    }
-
-    if (!psImageJpeg(map, readout->image, file->filename, min, max)) {
+        jpegOptions->min = 0;
+        jpegOptions->max = 1;
+    }
+
+    jpegOptions->showScale = PS_JPEG_SHOWSCALE_NONE;
+    jpegOptions->xFlip = false;
+    jpegOptions->yFlip = false;
+    
+    char *userOptions = psMetadataLookupStr(&status, options, "OPTIONS"); // Mode for scaling image
+    if (userOptions) {
+	// just use strstr for now 
+	if (strcasestr(userOptions, "+SB")) {
+	    jpegOptions->showScale = PS_JPEG_SHOWSCALE_BOTTOM;
+	} 
+	if (strcasestr(userOptions, "-X")) {
+	    jpegOptions->xFlip = true;
+	} 
+	if (strcasestr(userOptions, "-Y")) {
+	    jpegOptions->yFlip = true;
+	} 
+    }
+
+    // NOTE: we can overlay the location of the stars from the readout by passing a bDrawBuffer to this function
+
+    if (!psImageJpeg(jpegOptions, readout->image, NULL, file->filename)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to write JPEG image");
-        psFree(map);
-        return false;
-    }
-
-    psFree(map);
-    return true;
-}
+        psFree(jpegOptions);
+        return false;
+    }
+
+    psFree(jpegOptions);
+    return true;
+}
