Index: trunk/psModules/src/camera/pmFPA_JPEG.c
===================================================================
--- trunk/psModules/src/camera/pmFPA_JPEG.c	(revision 19648)
+++ trunk/psModules/src/camera/pmFPA_JPEG.c	(revision 19649)
@@ -5,6 +5,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-09-23 00:12:30 $
+ *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-09-23 01:46:02 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -134,13 +134,13 @@
 
     if (file->type != PM_FPA_FILE_JPEG) {
-        psError(PS_ERR_UNKNOWN, true, "warning: type mismatch");
-        return false;
-    }
-
-    psTrace("psModules.camera", 3, "writing jpeg for readout %zd\n", (size_t) readout);
-
-    psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, "PPIMAGE");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, true, "missing recipe PPIMAGE in config data");
+        psError(PS_ERR_UNKNOWN, true, "File is not of type JPEG");
+        return false;
+    }
+
+    psTrace("psModules.camera", 3, "writing jpeg for readout %p\n", readout);
+
+    psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, "JPEG");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "Unable to find JPEG recipe");
         return false;
     }
@@ -148,12 +148,7 @@
     psMetadata *options = psMetadataLookupMetadata(&status, recipe, file->name);
     if (!status) {
-        psError(PS_ERR_UNKNOWN, true, "missing %s options in PPIMAGE recipe", file->name);
-        return false;
-    }
-
-    float min = 0;
-    float max = 0;                 // Minimum and maximum for stretch
-    float mean = 0;
-    float delta = 0;
+        psError(PS_ERR_UNKNOWN, true, "Unable to find options for file %s in JPEG recipe", file->name);
+        return false;
+    }
 
     // measure the image statistics for scaling
@@ -162,14 +157,16 @@
     stats->nSubsample = 10000;
     psMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config); // Value to mask
+    float mean = 0, delta = 0;          //
     if (!psImageBackground(stats, NULL, readout->image, readout->mask, maskVal, rng)) {
         psStats *statsAlt = psStatsAlloc(PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
         stats->nSubsample = 10000;
         if (!psImageBackground(stats, NULL, readout->image, readout->mask, maskVal, rng)) {
-            psLogMsg("jpeg", PS_LOG_WARN, "Unable to measure statistics for image, writing blank jpeg\n");
+            psLogMsg("psModules.jpeg", PS_LOG_WARN,
+                     "Unable to measure statistics for image; writing blank jpeg");
             mean = 0;
             delta = 1;
         } else {
             mean = statsAlt->sampleMean;
-            delta = 2*statsAlt->sampleStdev;
+            delta = 2.0 * statsAlt->sampleStdev;
         }
         psFree(statsAlt);
@@ -181,46 +178,56 @@
     psFree(stats);
 
-    char *colormapName = psMemIncrRefCounter (psMetadataLookupStr (NULL, options, "COLORMAP"));
-    if (!colormapName)
-        colormapName = psStringCopy ("-greyscale");
-    char *mode = psMemIncrRefCounter (psMetadataLookupStr (NULL, options, "SCALE.MODE"));
-    if (!mode)
-        mode = psStringCopy ("RANGE");
-    float fmin = psMetadataLookupF32 (&status, options, "SCALE.MIN");
-    if (!status)
+    char *colormapName = psMetadataLookupStr(NULL, options, "COLORMAP"); // Name of colour map
+    if (!colormapName) {
+        colormapName = "-greyscale";
+    }
+    char *mode = psMetadataLookupStr(NULL, options, "SCALE.MODE"); // Mode for scaling image
+    if (!mode) {
+        mode = "RANGE";
+    }
+    float fmin = psMetadataLookupF32(&status, options, "SCALE.MIN"); // Minimum value/faction for scaling
+    if (!status) {
         fmin = -3.0;
-    float fmax = psMetadataLookupF32 (&status, options, "SCALE.MAX");
-    if (!status)
+    }
+    float fmax = psMetadataLookupF32(&status, options, "SCALE.MAX"); // Maximum value/faction for scaling
+    if (!status) {
         fmax = +6.0;
-
-    if (!strcasecmp (mode, "RANGE")) {
+    }
+
+    float min = 0, max = 0;             // Minimum and maximum for stretch
+
+    if (!strcasecmp(mode, "RANGE")) {
         min = mean + fmin*delta;
         max = mean + fmax*delta;
-    }
-    if (!strcasecmp (mode, "FRACTION")) {
+    } else if (!strcasecmp(mode, "FRACTION")) {
         min = fmin*mean;
         max = fmax*mean;
-    }
-    if (!strcasecmp (mode, "VALUE")) {
+    } else if (!strcasecmp(mode, "VALUE")) {
         min = fmin;
         max = fmax;
+    } else {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised scaling mode: %s", mode);
+        return false;
     }
 
     if (!isfinite(min) || !isfinite(max)) {
-        psLogMsg("jpeg", PS_LOG_WARN, "The stretch parameters are not both finite, writing blank jpeg\n");
+        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);
+    psImageJpegColormap *map = psImageJpegColormapSet(NULL, colormapName);
     if (!map) {
-        map = psImageJpegColormapSet (NULL, "-greyscale");
-    }
-
-    psImageJpeg (map, readout->image, file->filename, min, max);
+        map = psImageJpegColormapSet(NULL, "-greyscale");
+    }
+
+    if (!psImageJpeg(map, readout->image, file->filename, min, max)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to write JPEG image");
+        psFree(map);
+        return false;
+    }
 
     psFree(map);
-    psFree(colormapName);
-    psFree(mode);
-    return true;
-}
+    return true;
+}
