Index: trunk/psLib/src/jpeg/psImageJpeg.c
===================================================================
--- trunk/psLib/src/jpeg/psImageJpeg.c	(revision 27144)
+++ trunk/psLib/src/jpeg/psImageJpeg.c	(revision 27145)
@@ -202,19 +202,27 @@
             if (isfinite(row[i])) {
                 pixel = PS_JPEG_SCALEVALUE(row[i],zero,scale);
-		outPix[0] = Rpix[pixel];
-		outPix[1] = Gpix[pixel];
-		outPix[2] = Bpix[pixel];
+                outPix[0] = Rpix[pixel];
+                outPix[1] = Gpix[pixel];
+                outPix[2] = Bpix[pixel];
             } else {
-		// XXX NAN value should be set per-color map
-		outPix[0] = 0xff;
-		outPix[1] = 0x00;
-		outPix[2] = 0xff;
-	    }
-        }
-        jpeg_write_scanlines(&cinfo, jpegLineList, 1);
+                // XXX NAN value should be set per-color map
+                outPix[0] = 0xff;
+                outPix[1] = 0x00;
+                outPix[2] = 0xff;
+            }
+        }
+        if (jpeg_write_scanlines(&cinfo, jpegLineList, 1) == 0) {
+            psError(PS_ERR_IO, true, "Unable to write line %d to JPEG", j);
+            psFree(jpegLine);
+            return false;
+        }
     }
 
     jpeg_finish_compress(&cinfo);
-    fclose(f);
+    if (fclose(f) == EOF) {
+        psError(PS_ERR_IO, true, "Failed to close %s", filename);
+        psFree(jpegLine);
+        return false;
+    }
     jpeg_destroy_compress(&cinfo);
 
