about summary refs log tree commit diff
path: root/converter
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-05-30 03:37:54 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-05-30 03:37:54 +0000
commitaa17eb4f8424ea6548af66c7dbf819aecd399d57 (patch)
tree80c5a6850037c4aa5fa8cf51ab647cb363fe4923 /converter
parent34bdbe6cca8c6083c29682661cc9d26912561c04 (diff)
downloadnetpbm-mirror-aa17eb4f8424ea6548af66c7dbf819aecd399d57.tar.gz
netpbm-mirror-aa17eb4f8424ea6548af66c7dbf819aecd399d57.tar.xz
netpbm-mirror-aa17eb4f8424ea6548af66c7dbf819aecd399d57.zip
Handle failed I/O
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3822 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter')
-rw-r--r--converter/other/cameratopam/Makefile2
-rw-r--r--converter/other/cameratopam/camera.c235
-rw-r--r--converter/other/cameratopam/canon.c24
-rw-r--r--converter/other/cameratopam/foveon.c102
-rw-r--r--converter/other/cameratopam/identify.c49
-rw-r--r--converter/other/cameratopam/ljpeg.c16
-rw-r--r--converter/other/cameratopam/stdio_nofail.c75
-rw-r--r--converter/other/cameratopam/stdio_nofail.h18
-rw-r--r--converter/other/cameratopam/util.c14
9 files changed, 314 insertions, 221 deletions
diff --git a/converter/other/cameratopam/Makefile b/converter/other/cameratopam/Makefile
index d6207aea..bb4c4cde 100644
--- a/converter/other/cameratopam/Makefile
+++ b/converter/other/cameratopam/Makefile
@@ -20,7 +20,7 @@ include $(BUILDDIR)/config.mk
 all: cameratopam
 
 ADDL_OBJECTS = util.o identify.o camera.o foveon.o decode.o \
-	canon.o ljpeg.o dng.o
+	canon.o ljpeg.o dng.o stdio_nofail.o
 
 OBJECTS = cameratopam.o $(ADDL_OBJECTS)
 
diff --git a/converter/other/cameratopam/camera.c b/converter/other/cameratopam/camera.c
index 439c9413..261196b3 100644
--- a/converter/other/cameratopam/camera.c
+++ b/converter/other/cameratopam/camera.c
@@ -24,6 +24,7 @@
 #include "bayer.h"
 #include "ljpeg.h"
 #include "dng.h"
+#include "stdio_nofail.h"
 
 #include "camera.h"
 
@@ -94,8 +95,8 @@ adobe_dng_load_raw_lj(Image const image) {
     unsigned short *rp;
 
     while (1) {
-        save = ftell(ifp);
-        fseek (ifp, get4(ifp), SEEK_SET);
+        save = ftell_nofail(ifp);
+        fseek_nofail (ifp, get4(ifp), SEEK_SET);
         if (!ljpeg_start (ifp, &jh)) break;
         if (trow >= raw_height) break;
         if (jh.high > raw_height-trow)
@@ -113,7 +114,7 @@ adobe_dng_load_raw_lj(Image const image) {
                 adobeCopyPixel(image,
                                trow+jrow, tcol+jcol, &rp, use_secondary);
         }
-        fseek (ifp, save+4, SEEK_SET);
+        fseek_nofail (ifp, save+4, SEEK_SET);
         if ((tcol += twide) >= raw_width) {
             tcol = 0;
             trow += jh.high;
@@ -157,14 +158,14 @@ nikon_compressed_load_raw(Image const image) {
     init_decoder();
     make_decoder (nikon_tree, 0);
 
-    fseek (ifp, nikon_curve_offset, SEEK_SET);
+    fseek_nofail (ifp, nikon_curve_offset, SEEK_SET);
     read_shorts (ifp, vpred, 4);
     csize = get2(ifp);
     curve = calloc (csize, sizeof *curve);
     merror (curve, "nikon_compressed_load_raw()");
     read_shorts (ifp, curve, csize);
 
-    fseek (ifp, data_offset, SEEK_SET);
+    fseek_nofail (ifp, data_offset, SEEK_SET);
     getbits(ifp, -1);
 
     for (row=0; row < height; row++)
@@ -197,8 +198,8 @@ nikon_load_raw(Image const image) {
     if (model[0] == 'E') {
       row = irow * 2 % height + irow / (height/2);
       if (row == 1 && atoi(model+1) < 5000) {
-    fseek (ifp, 0, SEEK_END);
-    fseek (ifp, ftell(ifp)/2, SEEK_SET);
+    fseek_nofail (ifp, 0, SEEK_END);
+    fseek_nofail (ifp, ftell_nofail(ifp)/2, SEEK_SET);
     getbits(ifp, -1);
       }
     }
@@ -227,8 +228,8 @@ nikon_is_compressed()
     return 0;
   if (strcmp(model,"D100"))
     return 1;
-  fseek (ifp, data_offset, SEEK_SET);
-  fread (test, 1, 256, ifp);
+  fseek_nofail (ifp, data_offset, SEEK_SET);
+  fread_nofail (test, 1, 256, ifp);
   for (i=15; i < 256; i+=16)
     if (test[i]) return 1;
   return 0;
@@ -244,9 +245,9 @@ nikon_e990()
   const unsigned char often[] = { 0x00, 0x55, 0xaa, 0xff };
 
   memset (histo, 0, sizeof histo);
-  fseek (ifp, 2064*1540*3/4, SEEK_SET);
+  fseek_nofail (ifp, 2064*1540*3/4, SEEK_SET);
   for (i=0; i < 2000; i++)
-    histo[fgetc(ifp)]++;
+    histo[fgetc_nofail(ifp)]++;
   for (i=0; i < 4; i++)
     if (histo[often[i]] > 400)
       return 1;
@@ -262,9 +263,9 @@ nikon_e2100()
   unsigned char t[12];
   int i;
 
-  fseek (ifp, 0, SEEK_SET);
+  fseek_nofail (ifp, 0, SEEK_SET);
   for (i=0; i < 1024; i++) {
-    fread (t, 1, 12, ifp);
+    fread_nofail (t, 1, 12, ifp);
     if (((t[2] & t[4] & t[7] & t[9]) >> 4
     & t[1] & t[6] & t[8] & t[11] & 3) != 3)
       return 0;
@@ -281,8 +282,8 @@ pentax_optio33()
   int i, sum[] = { 0, 0 };
   unsigned char tail[952];
 
-  fseek (ifp, -sizeof tail, SEEK_END);
-  fread (tail, 1, sizeof tail, ifp);
+  fseek_nofail (ifp, -sizeof tail, SEEK_END);
+  fread_nofail (tail, 1, sizeof tail, ifp);
   for (i=0; i < sizeof tail; i++)
     sum[(i>>2) & 1] += tail[i];
   return sum[0] < sum[1]*4;
@@ -297,8 +298,8 @@ minolta_z2()
   int i;
   char tail[424];
 
-  fseek (ifp, -sizeof tail, SEEK_END);
-  fread (tail, 1, sizeof tail, ifp);
+  fseek_nofail (ifp, -sizeof tail, SEEK_END);
+  fread_nofail (tail, 1, sizeof tail, ifp);
   for (i=0; i < sizeof tail; i++)
     if (tail[i]) return 1;
   return 0;
@@ -313,10 +314,10 @@ nikon_e2100_load_raw(Image const image) {
 
   for (row=0; row <= height; row+=2) {
     if (row == height) {
-      fseek (ifp, ((width==1616) << 13) - (-ftell(ifp) & -2048), SEEK_SET);
+      fseek_nofail (ifp, ((width==1616) << 13) - (-ftell_nofail(ifp) & -2048), SEEK_SET);
       row = 1;
     }
-    fread (data, 1, width*3/2, ifp);
+    fread_nofail (data, 1, width*3/2, ifp);
     for (dp=data, pix=pixel; pix < pixel+width; dp+=12, pix+=8) {
       pix[0] = (dp[2] >> 4) + (dp[ 3] << 4);
       pix[1] = (dp[2] << 8) +  dp[ 1];
@@ -356,7 +357,7 @@ fuji_s2_load_raw(Image const image) {
   unsigned short pixel[2944];
   int row, col, r, c;
 
-  fseek (ifp, (2944*24+32)*2, SEEK_CUR);
+  fseek_nofail (ifp, (2944*24+32)*2, SEEK_CUR);
   for (row=0; row < 2144; row++) {
     read_shorts(ifp, pixel, 2944);
     for (col=0; col < 2880; col++) {
@@ -372,7 +373,7 @@ fuji_s3_load_raw(Image const image) {
   unsigned short pixel[4352];
   int row, col, r, c;
 
-  fseek (ifp, (4352*2+32)*2, SEEK_CUR);
+  fseek_nofail (ifp, (4352*2+32)*2, SEEK_CUR);
   for (row=0; row < 1440; row++) {
     read_shorts(ifp, pixel, 4352);
     for (col=0; col < 4288; col++) {
@@ -408,7 +409,7 @@ fuji_common_load_raw(Image        const image,
 void
 fuji_s5000_load_raw(Image const image) {
 
-  fseek (ifp, (1472*4+24)*2, SEEK_CUR);
+  fseek_nofail (ifp, (1472*4+24)*2, SEEK_CUR);
   fuji_common_load_raw(image, 1472, 1423, 2152);
 }
 
@@ -449,7 +450,7 @@ rollei_load_raw(Image const image) {
   unsigned iten=0, isix, i, buffer=0, row, col, todo[16];
 
   isix = raw_width * raw_height * 5 / 8;
-  while (fread (pixel, 1, 10, ifp) == 10) {
+  while (fread_nofail (pixel, 1, 10, ifp) == 10) {
     for (i=0; i < 10; i+=2) {
       todo[i]   = iten++;
       todo[i+1] = pixel[i] << 8 | pixel[i+1];
@@ -474,11 +475,11 @@ phase_one_load_raw(Image const image) {
   int row, col, a, b;
   unsigned short *pixel, akey, bkey;
 
-  fseek (ifp, 8, SEEK_CUR);
-  fseek (ifp, get4(ifp) + 296, SEEK_CUR);
+  fseek_nofail (ifp, 8, SEEK_CUR);
+  fseek_nofail (ifp, get4(ifp) + 296, SEEK_CUR);
   akey = get2(ifp);
   bkey = get2(ifp);
-  fseek (ifp, data_offset + 12 + top_margin*raw_width*2, SEEK_SET);
+  fseek_nofail (ifp, data_offset + 12 + top_margin*raw_width*2, SEEK_SET);
   pixel = calloc (raw_width, sizeof *pixel);
   merror (pixel, "phase_one_load_raw()");
   for (row=0; row < height; row++) {
@@ -501,7 +502,7 @@ ixpress_load_raw(Image const image) {
   int row, col;
 
   order = 0x4949;
-  fseek (ifp, 304 + 6*2*4090, SEEK_SET);
+  fseek_nofail (ifp, 304 + 6*2*4090, SEEK_SET);
   for (row=height; --row >= 0; ) {
     read_shorts(ifp, pixel, 4090);
     for (col=0; col < width; col++)
@@ -567,7 +568,7 @@ olympus_e300_load_raw(Image const image) {
   merror (data, "olympus_e300_load_raw()");
   pixel = (unsigned short *) (data + dwide);
   for (row=0; row < height; row++) {
-    fread (data, 1, dwide, ifp);
+    fread_nofail (data, 1, dwide, ifp);
     for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=3, pix+=2) {
       if (((dp-data) & 15) == 15) dp++;
       pix[0] = dp[1] << 8 | dp[0];
@@ -586,7 +587,7 @@ olympus_cseries_load_raw(Image const image) {
   for (irow=0; irow < height; irow++) {
     row = irow * 2 % height + irow / (height/2);
     if (row < 2) {
-      fseek (ifp, data_offset - row*(-width*height*3/4 & -2048), SEEK_SET);
+      fseek_nofail (ifp, data_offset - row*(-width*height*3/4 & -2048), SEEK_SET);
       getbits(ifp, -1);
     }
     for (col=0; col < width; col++)
@@ -602,7 +603,7 @@ eight_bit_load_raw(Image const image) {
   pixel = calloc (raw_width, sizeof *pixel);
   merror (pixel, "eight_bit_load_raw()");
   for (row=0; row < height; row++) {
-    fread (pixel, 1, raw_width, ifp);
+    fread_nofail (pixel, 1, raw_width, ifp);
     for (col=0; col < width; col++)
       BAYER(row,col) = pixel[col];
   }
@@ -617,7 +618,7 @@ casio_qv5700_load_raw(Image const image) {
   int row, col;
 
   for (row=0; row < height; row++) {
-    fread (data, 1, 3232, ifp);
+    fread_nofail (data, 1, 3232, ifp);
     for (dp=data, pix=pixel; dp < data+3220; dp+=5, pix+=4) {
       pix[0] = (dp[0] << 2) + (dp[1] >> 6);
       pix[1] = (dp[1] << 4) + (dp[2] >> 4);
@@ -783,7 +784,7 @@ fill_input_buffer (j_decompress_ptr cinfo)
   static char jpeg_buffer[4096];
   size_t nbytes;
 
-  nbytes = fread (jpeg_buffer, 1, 4096, ifp);
+  nbytes = fread_nofail (jpeg_buffer, 1, 4096, ifp);
   swab (jpeg_buffer, jpeg_buffer, nbytes);
   cinfo->src->next_input_byte = jpeg_buffer;
   cinfo->src->bytes_in_buffer = nbytes;
@@ -840,7 +841,7 @@ kodak_dc120_load_raw(Image const image)
   int row, shift, col;
 
   for (row=0; row < height; row++) {
-    fread (pixel, 848, 1, ifp);
+    fread_nofail (pixel, 848, 1, ifp);
     shift = row * mul[row & 3] + add[row & 3];
     for (col=0; col < width; col++)
       BAYER(row,col) = (unsigned short) pixel[(col + shift) % 848];
@@ -878,7 +879,7 @@ kodak_easy_load_raw(Image const image)
   pixel = calloc (raw_width, sizeof *pixel);
   merror (pixel, "kodak_easy_load_raw()");
   for (row=0; row < height; row++) {
-    fread (pixel, 1, raw_width, ifp);
+    fread_nofail (pixel, 1, raw_width, ifp);
     for (col=0; col < raw_width; col++) {
       icol = col - left_margin;
       if (icol < width)
@@ -912,21 +913,21 @@ kodak_compressed_load_raw(Image const image)
       if ((col & 255) == 0) {       /* Get the bit-lengths of the */
     len = width - col;      /* next 256 pixel values      */
     if (len > 256) len = 256;
-    save = ftell(ifp);
+    save = ftell_nofail(ifp);
     for (israw=i=0; i < len; i+=2) {
-      c = fgetc(ifp);
+      c = fgetc_nofail(ifp);
       if ((blen[i+0] = c & 15) > 12 ||
           (blen[i+1] = c >> 4) > 12 )
         israw = 1;
     }
     bitbuf = bits = pred[0] = pred[1] = 0;
     if (len % 8 == 4) {
-      bitbuf  = fgetc(ifp) << 8;
-      bitbuf += fgetc(ifp);
+      bitbuf  = fgetc_nofail(ifp) << 8;
+      bitbuf += fgetc_nofail(ifp);
       bits = 16;
     }
     if (israw)
-      fseek (ifp, save, SEEK_SET);
+      fseek_nofail (ifp, save, SEEK_SET);
       }
       if (israw) {          /* If the data is not compressed */
     switch (col & 7) {
@@ -944,7 +945,7 @@ kodak_compressed_load_raw(Image const image)
     len = blen[col & 255];      /* Number of bits for this pixel */
     if (bits < len) {       /* Got enough bits in the buffer? */
       for (i=0; i < 32; i+=8)
-        bitbuf += (INT64) fgetc(ifp) << (bits+(i^8));
+        bitbuf += (INT64) fgetc_nofail(ifp) << (bits+(i^8));
       bits += 32;
     }
     diff = bitbuf & (0xffff >> (16-len));  /* Pull bits from buffer */
@@ -976,14 +977,14 @@ kodak_yuv_load_raw(Image const image)
     len = (width - col + 1) * 3 & -4;
     if (len > 384) len = 384;
     for (i=0; i < len; ) {
-      c = fgetc(ifp);
+      c = fgetc_nofail(ifp);
       blen[i++] = c & 15;
       blen[i++] = c >> 4;
     }
     li = bitbuf = bits = y[1] = y[3] = cb = cr = 0;
     if (len % 8 == 4) {
-      bitbuf  = fgetc(ifp) << 8;
-      bitbuf += fgetc(ifp);
+      bitbuf  = fgetc_nofail(ifp) << 8;
+      bitbuf += fgetc_nofail(ifp);
       bits = 16;
     }
       }
@@ -991,7 +992,7 @@ kodak_yuv_load_raw(Image const image)
     len = blen[li++];
     if (bits < len) {
       for (i=0; i < 32; i+=8)
-        bitbuf += (INT64) fgetc(ifp) << (bits+(i^8));
+        bitbuf += (INT64) fgetc_nofail(ifp) << (bits+(i^8));
       bits += 32;
     }
     diff = bitbuf & (0xffff >> (16-len));
@@ -1062,20 +1063,20 @@ sony_load_raw(Image const image)
   struct pixel * pixelrow;
   unsigned i, key, row, col;
 
-  fseek (ifp, 200896, SEEK_SET);
-  fseek (ifp, (unsigned) fgetc(ifp)*4 - 1, SEEK_CUR);
+  fseek_nofail (ifp, 200896, SEEK_SET);
+  fseek_nofail (ifp, (unsigned) fgetc_nofail(ifp)*4 - 1, SEEK_CUR);
   order = 0x4d4d;
   key = get4(ifp);
-  fseek (ifp, 164600, SEEK_SET);
-  fread (head, 1, 40, ifp);
+  fseek_nofail (ifp, 164600, SEEK_SET);
+  fread_nofail (head, 1, 40, ifp);
   sony_decrypt ((void *) head, 10, 1, key);
   for (i=26; i-- > 22; )
     key = key << 8 | head[i];
-  fseek (ifp, data_offset, SEEK_SET);
+  fseek_nofail (ifp, data_offset, SEEK_SET);
   MALLOCARRAY(pixelrow, raw_width);
   merror (pixelrow, "sony_load_raw()");
   for (row=0; row < height; row++) {
-    fread (pixelrow, 2, raw_width, ifp);
+    fread_nofail (pixelrow, 2, raw_width, ifp);
     sony_decrypt ((void *) pixelrow, raw_width/2, !row, key);
     for (col=9; col < left_margin; col++)
       black += pixelrow[col].bytes[0] * 256 + pixelrow[col].bytes[1];
@@ -1095,14 +1096,14 @@ parse_minolta(FILE * const ifp)
 {
   int save, tag, len, offset, high=0, wide=0;
 
-  fseek (ifp, 4, SEEK_SET);
+  fseek_nofail (ifp, 4, SEEK_SET);
   offset = get4(ifp) + 8;
-  while ((save=ftell(ifp)) < offset) {
+  while ((save=ftell_nofail(ifp)) < offset) {
     tag = get4(ifp);
     len = get4(ifp);
     switch (tag) {
       case 0x505244:                /* PRD */
-    fseek (ifp, 8, SEEK_CUR);
+    fseek_nofail (ifp, 8, SEEK_CUR);
     high = get2(ifp);
     wide = get2(ifp);
     break;
@@ -1114,9 +1115,9 @@ parse_minolta(FILE * const ifp)
     camera_blue = get2(ifp) / camera_blue;
     break;
       case 0x545457:                /* TTW */
-    parse_tiff(ifp, ftell(ifp));
+    parse_tiff(ifp, ftell_nofail(ifp));
     }
-    fseek (ifp, save+len+8, SEEK_SET);
+    fseek_nofail (ifp, save+len+8, SEEK_SET);
   }
   raw_height = high;
   raw_width  = wide;
@@ -1168,24 +1169,24 @@ parse_ciff(FILE * const ifp,
       strcmp(model,"Canon PowerShot S70") &&
       strcmp(model,"Canon PowerShot Pro1"))
     key[0] = key[1] = 0;
-  fseek (ifp, offset+length-4, SEEK_SET);
+  fseek_nofail (ifp, offset+length-4, SEEK_SET);
   tboff = get4(ifp) + offset;
-  fseek (ifp, tboff, SEEK_SET);
+  fseek_nofail (ifp, tboff, SEEK_SET);
   nrecs = get2(ifp);
   for (i = 0; i < nrecs; i++) {
     type = get2(ifp);
     len  = get4(ifp);
     roff = get4(ifp);
     aoff = offset + roff;
-    save = ftell(ifp);
+    save = ftell_nofail(ifp);
     if (type == 0x080a) {       /* Get the camera make and model */
-      fseek (ifp, aoff, SEEK_SET);
-      fread (make, 64, 1, ifp);
-      fseek (ifp, aoff+strlen(make)+1, SEEK_SET);
-      fread (model, 64, 1, ifp);
+      fseek_nofail (ifp, aoff, SEEK_SET);
+      fread_nofail (make, 64, 1, ifp);
+      fseek_nofail (ifp, aoff+strlen(make)+1, SEEK_SET);
+      fread_nofail (model, 64, 1, ifp);
     }
     if (type == 0x102a) {       /* Find the White Balance index */
-      fseek (ifp, aoff+14, SEEK_SET);   /* 0=auto, 1=daylight, 2=cloudy ... */
+      fseek_nofail (ifp, aoff+14, SEEK_SET);   /* 0=auto, 1=daylight, 2=cloudy ... */
       wbi = get2(ifp);
       if (((!strcmp(model,"Canon EOS DIGITAL REBEL") ||
         !strcmp(model,"Canon EOS 300D DIGITAL"))) && wbi == 6)
@@ -1194,19 +1195,19 @@ parse_ciff(FILE * const ifp,
     if (type == 0x102c) {       /* Get white balance (G2) */
       if (!strcmp(model,"Canon PowerShot G1") ||
       !strcmp(model,"Canon PowerShot Pro90 IS")) {
-    fseek (ifp, aoff+120, SEEK_SET);
+    fseek_nofail (ifp, aoff+120, SEEK_SET);
     white[0][1] = get2(ifp);
     white[0][0] = get2(ifp);
     white[1][0] = get2(ifp);
     white[1][1] = get2(ifp);
       } else {
-    fseek (ifp, aoff+100, SEEK_SET);
+    fseek_nofail (ifp, aoff+100, SEEK_SET);
     goto common;
       }
     }
     if (type == 0x0032) {       /* Get white balance (D30 & G3) */
       if (!strcmp(model,"Canon EOS D30")) {
-    fseek (ifp, aoff+72, SEEK_SET);
+    fseek_nofail (ifp, aoff+72, SEEK_SET);
 common:
     camera_red   = get2(ifp) ^ key[0];
     camera_red   =(get2(ifp) ^ key[1]) / camera_red;
@@ -1214,13 +1215,13 @@ common:
     camera_blue /= get2(ifp) ^ key[1];
       } else if (!strcmp(model,"Canon PowerShot G6") ||
          !strcmp(model,"Canon PowerShot S70")) {
-    fseek (ifp, aoff+96 + remap_s70[wbi]*8, SEEK_SET);
+    fseek_nofail (ifp, aoff+96 + remap_s70[wbi]*8, SEEK_SET);
     goto common;
       } else if (!strcmp(model,"Canon PowerShot Pro1")) {
-    fseek (ifp, aoff+96 + wbi*8, SEEK_SET);
+    fseek_nofail (ifp, aoff+96 + wbi*8, SEEK_SET);
     goto common;
       } else {
-    fseek (ifp, aoff+80 + (wbi < 6 ? remap[wbi]*8 : 0), SEEK_SET);
+    fseek_nofail (ifp, aoff+80 + (wbi < 6 ? remap[wbi]*8 : 0), SEEK_SET);
     if (!camera_red)
       goto common;
       }
@@ -1228,38 +1229,38 @@ common:
     if (type == 0x10a9) {       /* Get white balance (D60) */
       if (!strcmp(model,"Canon EOS 10D"))
     wbi = remap_10d[wbi];
-      fseek (ifp, aoff+2 + wbi*8, SEEK_SET);
+      fseek_nofail (ifp, aoff+2 + wbi*8, SEEK_SET);
       camera_red  = get2(ifp);
       camera_red /= get2(ifp);
       camera_blue = get2(ifp);
       camera_blue = get2(ifp) / camera_blue;
     }
     if (type == 0x1030 && (wbi == 6 || wbi == 15)) {
-      fseek (ifp, aoff, SEEK_SET);  /* Get white sample */
+      fseek_nofail (ifp, aoff, SEEK_SET);  /* Get white sample */
       ciff_block_1030();
     }
     if (type == 0x1031) {       /* Get the raw width and height */
-      fseek (ifp, aoff+2, SEEK_SET);
+      fseek_nofail (ifp, aoff+2, SEEK_SET);
       raw_width  = get2(ifp);
       raw_height = get2(ifp);
     }
     if (type == 0x180e) {       /* Get the timestamp */
-      fseek (ifp, aoff, SEEK_SET);
+      fseek_nofail (ifp, aoff, SEEK_SET);
       timestamp = get4(ifp);
     }
     if (type == 0x580e)
       timestamp = len;
     if (type == 0x1810) {       /* Get the rotation */
-      fseek (ifp, aoff+12, SEEK_SET);
+      fseek_nofail (ifp, aoff+12, SEEK_SET);
       flip = get4(ifp);
     }
     if (type == 0x1835) {       /* Get the decoder table */
-      fseek (ifp, aoff, SEEK_SET);
+      fseek_nofail (ifp, aoff, SEEK_SET);
       crw_init_tables (get4(ifp));
     }
     if (type >> 8 == 0x28 || type >> 8 == 0x30) /* Get sub-tables */
       parse_ciff(ifp, aoff, len);
-    fseek (ifp, save, SEEK_SET);
+    fseek_nofail (ifp, save, SEEK_SET);
   }
   if (wbi == 0 && !strcmp(model,"Canon EOS D30"))
     camera_red = -1;            /* Use my auto WB for this photo */
@@ -1273,7 +1274,7 @@ parse_rollei(FILE * const ifp)
   struct tm t;
   time_t ts;
 
-  fseek (ifp, 0, SEEK_SET);
+  fseek_nofail (ifp, 0, SEEK_SET);
   do {
     fgets (line, 128, ifp);
     if ((val = strchr(line,'=')))
@@ -1314,13 +1315,13 @@ parse_mos(FILE * const ifp,
     char data[40];
     int skip, from, i, neut[4];
 
-    fseek (ifp, offset, SEEK_SET);
+    fseek_nofail (ifp, offset, SEEK_SET);
     while (1) {
-        fread (data, 1, 8, ifp);
+        fread_nofail (data, 1, 8, ifp);
         if (strcmp(data,"PKTS")) break;
-        fread (data, 1, 40, ifp);
+        fread_nofail (data, 1, 40, ifp);
         skip = get4(ifp);
-        from = ftell(ifp);
+        from = ftell_nofail(ifp);
         if (!strcmp(data,"NeutObj_neutrals")) {
             for (i=0; i < 4; i++)
                 fscanf (ifp, "%d", neut+i);
@@ -1328,7 +1329,7 @@ parse_mos(FILE * const ifp,
             camera_blue = (float) neut[2] / neut[3];
         }
         parse_mos(ifp, from);
-        fseek (ifp, skip+from, SEEK_SET);
+        fseek_nofail (ifp, skip+from, SEEK_SET);
     }
 }
 
@@ -1369,43 +1370,43 @@ parse_makernote(FILE * const ifp)
    its own byte-order!), or it might just be a table.
  */
   sorder = order;
-  fread (buf, 1, 10, ifp);
+  fread_nofail (buf, 1, 10, ifp);
   if (!strncmp (buf,"KC" ,2) ||     /* these aren't TIFF format */
       !strncmp (buf,"MLY",3)) return;
   if (!strcmp (buf,"Nikon")) {
-    base = ftell(ifp);
+    base = ftell_nofail(ifp);
     order = get2(ifp);
     if (get2(ifp) != 42) goto quit;
     offset = get4(ifp);
-    fseek (ifp, offset-8, SEEK_CUR);
+    fseek_nofail (ifp, offset-8, SEEK_CUR);
   } else if (!strncmp (buf,"FUJIFILM",8) ||
          !strcmp  (buf,"Panasonic")) {
     order = 0x4949;
-    fseek (ifp,  2, SEEK_CUR);
+    fseek_nofail (ifp,  2, SEEK_CUR);
   } else if (!strcmp (buf,"OLYMP") ||
          !strcmp (buf,"LEICA") ||
          !strcmp (buf,"EPSON"))
-    fseek (ifp, -2, SEEK_CUR);
+    fseek_nofail (ifp, -2, SEEK_CUR);
   else if (!strcmp (buf,"AOC") ||
        !strcmp (buf,"QVC"))
-    fseek (ifp, -4, SEEK_CUR);
-  else fseek (ifp, -10, SEEK_CUR);
+    fseek_nofail (ifp, -4, SEEK_CUR);
+  else fseek_nofail (ifp, -10, SEEK_CUR);
 
   entries = get2(ifp);
   while (entries--) {
     tag  = get2(ifp);
     type = get2(ifp);
     len  = get4(ifp);
-    save = ftell(ifp);
+    save = ftell_nofail(ifp);
     if (len * size[type < 13 ? type:0] > 4)
-      fseek (ifp, get4(ifp)+base, SEEK_SET);
+      fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
 
     if (tag == 0xc && len == 4) {
       camera_red  = getrat();
       camera_blue = getrat();
     }
     if (tag == 0x14 && len == 2560 && type == 7) {
-      fseek (ifp, 1248, SEEK_CUR);
+      fseek_nofail (ifp, 1248, SEEK_CUR);
       goto get2_256;
     }
     if (strstr(make,"PENTAX")) {
@@ -1413,19 +1414,19 @@ parse_makernote(FILE * const ifp)
       if (tag == 0x1c) tag = 0x1017;
     }
     if (tag == 0x8c)
-      nikon_curve_offset = ftell(ifp) + 2112;
+      nikon_curve_offset = ftell_nofail(ifp) + 2112;
     if (tag == 0x96)
-      nikon_curve_offset = ftell(ifp) + 2;
+      nikon_curve_offset = ftell_nofail(ifp) + 2;
     if (tag == 0x97) {
       if (!strcmp(model,"NIKON D100 ")) {
-    fseek (ifp, 72, SEEK_CUR);
+    fseek_nofail (ifp, 72, SEEK_CUR);
     camera_red  = get2(ifp) / 256.0;
     camera_blue = get2(ifp) / 256.0;
       } else if (!strcmp(model,"NIKON D2H")) {
-    fseek (ifp, 10, SEEK_CUR);
+    fseek_nofail (ifp, 10, SEEK_CUR);
     goto get2_rggb;
       } else if (!strcmp(model,"NIKON D70")) {
-    fseek (ifp, 20, SEEK_CUR);
+    fseek_nofail (ifp, 20, SEEK_CUR);
     camera_red  = get2(ifp);
     camera_red /= get2(ifp);
     camera_blue = get2(ifp);
@@ -1449,12 +1450,12 @@ parse_makernote(FILE * const ifp)
       black = (get4(ifp)+get4(ifp)+get4(ifp)+get4(ifp))/4;
     }
     if (tag == 0xe80 && len == 256 && type == 7) {
-      fseek (ifp, 48, SEEK_CUR);
+      fseek_nofail (ifp, 48, SEEK_CUR);
       camera_red  = get2(ifp) * 508 * 1.078 / 0x10000;
       camera_blue = get2(ifp) * 382 * 1.173 / 0x10000;
     }
     if (tag == 0xf00 && len == 614 && type == 7) {
-      fseek (ifp, 188, SEEK_CUR);
+      fseek_nofail (ifp, 188, SEEK_CUR);
       goto get2_256;
     }
     if (tag == 0x1017)
@@ -1468,14 +1469,14 @@ get2_256:
       camera_blue = get2(ifp) / 256.0;
     }
     if (tag == 0x4001) {
-      fseek (ifp, strstr(model,"EOS-1D") ? 68:50, SEEK_CUR);
+      fseek_nofail (ifp, strstr(model,"EOS-1D") ? 68:50, SEEK_CUR);
 get2_rggb:
       camera_red  = get2(ifp);
       camera_red /= get2(ifp);
       camera_blue = get2(ifp);
       camera_blue = get2(ifp) / camera_blue;
     }
-    fseek (ifp, save+4, SEEK_SET);
+    fseek_nofail (ifp, save+4, SEEK_SET);
   }
 quit:
   order = sorder;
@@ -1514,8 +1515,8 @@ parse_exif(FILE * const ifp, int base)
     /* type = */ get2(ifp);
     len  = get4(ifp);
     val  = get4(ifp);
-    save = ftell(ifp);
-    fseek (ifp, base+val, SEEK_SET);
+    save = ftell_nofail(ifp);
+    fseek_nofail (ifp, base+val, SEEK_SET);
     if (tag == 0x9003 || tag == 0x9004)
       get_timestamp(ifp);
     if (tag == 0x927c) {
@@ -1524,7 +1525,7 @@ parse_exif(FILE * const ifp, int base)
       else
     parse_makernote(ifp);
     }
-    fseek (ifp, save, SEEK_SET);
+    fseek_nofail (ifp, save, SEEK_SET);
   }
 }
 
@@ -1550,10 +1551,10 @@ parse_tiff_ifd(FILE * const ifp, int base, int level)
     tag  = get2(ifp);
     type = get2(ifp);
     len  = get4(ifp);
-    save = ftell(ifp);
+    save = ftell_nofail(ifp);
     if (tag > 50700 && tag < 50800) done = 1;
     if (len * size[type < 13 ? type:0] > 4)
-      fseek (ifp, get4(ifp)+base, SEEK_SET);
+      fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
     switch (tag) {
       case 0x11:
     camera_red  = get4(ifp) / 256.0;
@@ -1604,7 +1605,7 @@ parse_tiff_ifd(FILE * const ifp, int base, int level)
     break;
       case 0x144:           /* TileOffsets */
     if (level) {
-      data_offset = ftell(ifp);
+      data_offset = ftell_nofail(ifp);
     } else {
       strcpy (make, "Leaf");
       data_offset = get4(ifp);
@@ -1614,10 +1615,10 @@ parse_tiff_ifd(FILE * const ifp, int base, int level)
     if (len > 2 && !is_dng && !strcmp(make,"Kodak"))
         len = 2;
     while (len--) {
-      i = ftell(ifp);
-      fseek (ifp, get4(ifp)+base, SEEK_SET);
+      i = ftell_nofail(ifp);
+      fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
       if (parse_tiff_ifd(ifp, base, level+1)) break;
-      fseek (ifp, i+4, SEEK_SET);
+      fseek_nofail (ifp, i+4, SEEK_SET);
     }
     break;
       case 33405:           /* Model2 */
@@ -1625,7 +1626,7 @@ parse_tiff_ifd(FILE * const ifp, int base, int level)
     break;
       case 33422:           /* CFAPattern */
     if ((plen=len) > 16) plen = 16;
-    fread (cfa_pat, 1, plen, ifp);
+    fread_nofail (cfa_pat, 1, plen, ifp);
     for (colors=cfa=i=0; i < plen; i++) {
       colors += !(cfa & (1 << cfa_pat[i]));
       cfa |= 1 << cfa_pat[i];
@@ -1634,7 +1635,7 @@ parse_tiff_ifd(FILE * const ifp, int base, int level)
     if (cfa == 072) memcpy (cfa_pc,"\005\003\004\001",4);   /* GMCY */
     goto guess_cfa_pc;
       case 34665:           /* EXIF tag */
-    fseek (ifp, get4(ifp)+base, SEEK_SET);
+    fseek_nofail (ifp, get4(ifp)+base, SEEK_SET);
     parse_exif(ifp, base);
     break;
       case 50706:           /* DNGVersion */
@@ -1644,7 +1645,7 @@ parse_tiff_ifd(FILE * const ifp, int base, int level)
       case 50710:           /* CFAPlaneColor */
     if (len > 4) len = 4;
     colors = len;
-    fread (cfa_pc, 1, colors, ifp);
+    fread_nofail (cfa_pc, 1, colors, ifp);
 guess_cfa_pc:
     FORC4 tab[cfa_pc[c]] = c;
     for (i=16; i--; )
@@ -1701,7 +1702,7 @@ guess_cfa_pc:
     xyz[1] = getrat();
     xyz[2] = 1 - xyz[0] - xyz[1];
     }
-    fseek (ifp, save+4, SEEK_SET);
+    fseek_nofail (ifp, save+4, SEEK_SET);
   }
   for (i=0; i < colors; i++)
     FORC4 cc[i][c] *= ab[i];
@@ -1729,16 +1730,16 @@ parse_tiff(FILE * const ifp, int base)
 {
   int doff;
 
-  fseek (ifp, base, SEEK_SET);
+  fseek_nofail (ifp, base, SEEK_SET);
   order = get2(ifp);
   if (order != 0x4949 && order != 0x4d4d) return;
   get2(ifp);
   while ((doff = get4(ifp))) {
-    fseek (ifp, doff+base, SEEK_SET);
+    fseek_nofail (ifp, doff+base, SEEK_SET);
     if (parse_tiff_ifd(ifp, base, 0)) break;
   }
   if (!is_dng && !strncmp(make,"Kodak",5)) {
-    fseek (ifp, 12+base, SEEK_SET);
+    fseek_nofail (ifp, 12+base, SEEK_SET);
     parse_tiff_ifd(ifp, base, 2);
   }
 }
diff --git a/converter/other/cameratopam/canon.c b/converter/other/cameratopam/canon.c
index 96a6210b..cbf5ece0 100644
--- a/converter/other/cameratopam/canon.c
+++ b/converter/other/cameratopam/canon.c
@@ -6,9 +6,10 @@
 #include "decode.h"
 #include "bayer.h"
 #include "canon.h"
+#include "stdio_nofail.h"
 
 
-void 
+void
 canon_600_load_raw(Image const image) {
     unsigned char  data[1120], *dp;
     unsigned short pixel[896], *pix;
@@ -16,7 +17,7 @@ canon_600_load_raw(Image const image) {
 
     for (irow=orow=0; irow < height; irow++)
     {
-        fread (data, 1120, 1, ifp);
+        fread_nofail (data, 1120, 1, ifp);
         for (dp=data, pix=pixel; dp < data+1120; dp+=10, pix+=8)
         {
             pix[0] = (dp[0] << 2) + (dp[1] >> 6    );
@@ -48,7 +49,7 @@ canon_a5_load_raw(Image const image) {
     int row, col;
 
     for (row=0; row < height; row++) {
-        fread (data, raw_width * 10 / 8, 1, ifp);
+        fread_nofail (data, raw_width * 10 / 8, 1, ifp);
         for (dp=data, pix=pixel; pix < pixel+raw_width; dp+=10, pix+=8)
         {
             pix[0] = (dp[1] << 2) + (dp[0] >> 6);
@@ -84,8 +85,8 @@ canon_has_lowbits()
     unsigned char test[0x4000];
     int ret=1, i;
 
-    fseek (ifp, 0, SEEK_SET);
-    fread (test, 1, sizeof test, ifp);
+    fseek_nofail (ifp, 0, SEEK_SET);
+    fread_nofail (test, 1, sizeof test, ifp);
     for (i=540; i < sizeof test - 1; i++)
         if (test[i] == 0xff) {
             if (test[i+1]) return 1;
@@ -96,7 +97,7 @@ canon_has_lowbits()
 
 
 
-void 
+void
 canon_compressed_load_raw(Image const image) {
     unsigned short *pixel, *prow;
     int lowbits, i, row, r, col, save, val;
@@ -110,7 +111,7 @@ canon_compressed_load_raw(Image const image) {
         pm_error("Unable to allocate space for %u pixels", raw_width*8);
     lowbits = canon_has_lowbits();
     if (!lowbits) maximum = 0x3ff;
-    fseek (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
+    fseek_nofail (ifp, 540 + lowbits*raw_height*raw_width/4, SEEK_SET);
     zero_after_ff = 1;
     getbits(ifp, -1);
     for (row = 0; row < raw_height; row += 8) {
@@ -141,17 +142,17 @@ canon_compressed_load_raw(Image const image) {
             }
         }
         if (lowbits) {
-            save = ftell(ifp);
-            fseek (ifp, 26 + row*raw_width/4, SEEK_SET);
+            save = ftell_nofail(ifp);
+            fseek_nofail (ifp, 26 + row*raw_width/4, SEEK_SET);
             for (prow=pixel, i=0; i < raw_width*2; i++) {
-                c = fgetc(ifp);
+                c = fgetc_nofail(ifp);
                 for (r=0; r < 8; r+=2, prow++) {
                     val = (*prow << 2) + ((c >> r) & 3);
                     if (raw_width == 2672 && val < 512) val += 2;
                     *prow = val;
                 }
             }
-            fseek (ifp, save, SEEK_SET);
+            fseek_nofail (ifp, save, SEEK_SET);
         }
         for (r=0; r < 8; r++) {
             irow = row - top_margin + r;
@@ -169,4 +170,3 @@ canon_compressed_load_raw(Image const image) {
     if (raw_width > width)
         black /= (raw_width - width) * height;
 }
-
diff --git a/converter/other/cameratopam/foveon.c b/converter/other/cameratopam/foveon.c
index 992f3883..5a26777b 100644
--- a/converter/other/cameratopam/foveon.c
+++ b/converter/other/cameratopam/foveon.c
@@ -2,7 +2,6 @@
 
 #define _XOPEN_SOURCE 500  /* get M_PI in math.h */
 
-#include <stdio.h>
 #include <assert.h>
 #include <string.h>
 #include <float.h>
@@ -13,6 +12,7 @@
 #include "global_variables.h"
 #include "decode.h"
 #include "foveon.h"
+#include "stdio_nofail.h"
 
 #if HAVE_INT64
    typedef int64_t INT64;
@@ -32,13 +32,13 @@
 
 
 
-static char *  
-foveon_gets(int    const offset, 
-            char * const str, 
+static char *
+foveon_gets(int    const offset,
+            char * const str,
             int    const len) {
 
     unsigned int i;
-    fseek (ifp, offset, SEEK_SET);
+    fseek_nofail (ifp, offset, SEEK_SET);
     for (i=0; i < len-1; ++i) {
         /* It certains seems wrong that we're reading a 16 bit integer
            and assigning it to char, but that's what Dcraw does.
@@ -55,7 +55,7 @@ foveon_gets(int    const offset,
 
 
 
-void 
+void
 parse_foveon(FILE * const ifp) {
     long fliplong;
     long pos;
@@ -63,14 +63,14 @@ parse_foveon(FILE * const ifp) {
     long junk;
     long entries;
 
-    fseek (ifp, 36, SEEK_SET);
+    fseek_nofail (ifp, 36, SEEK_SET);
     pm_readlittlelong(ifp, &fliplong);
     flip = fliplong;
-    fseek (ifp, -4, SEEK_END);
+    fseek_nofail (ifp, -4, SEEK_END);
     pm_readlittlelong(ifp, &pos);
-    fseek (ifp, pos, SEEK_SET);
+    fseek_nofail (ifp, pos, SEEK_SET);
     pm_readlittlelong(ifp, &magic);
-    if (magic != 0x64434553) 
+    if (magic != 0x64434553)
         return; /* SECd */
     pm_readlittlelong(ifp, &junk);
     pm_readlittlelong(ifp, &entries);
@@ -85,17 +85,17 @@ parse_foveon(FILE * const ifp) {
         pm_readlittlelong(ifp, &off);
         pm_readlittlelong(ifp, &len);
         pm_readlittlelong(ifp, &tag);
-            
-        save = ftell(ifp);
-        fseek (ifp, off, SEEK_SET);
+
+        save = ftell_nofail(ifp);
+        fseek_nofail (ifp, off, SEEK_SET);
         pm_readlittlelong(ifp, &sec_);
         if (sec_ != (0x20434553 | (tag << 24))) return;
         switch (tag) {
         case 0x47414d49:          /* IMAG */
-            if (data_offset) 
+            if (data_offset)
                 break;
             data_offset = off + 28;
-            fseek (ifp, 12, SEEK_CUR);
+            fseek_nofail (ifp, 12, SEEK_CUR);
             {
                 long wlong, hlong;
                 pm_readlittlelong(ifp, &wlong);
@@ -113,7 +113,7 @@ parse_foveon(FILE * const ifp) {
         case 0x504f5250:          /* PROP */
             pm_readlittlelong(ifp, &junk);
             pm_readlittlelong(ifp, &pent);
-            fseek (ifp, 12, SEEK_CUR);
+            fseek_nofail (ifp, 12, SEEK_CUR);
             off += pent*8 + 24;
             if (pent > 256) pent=256;
             for (i=0; i < pent*2; i++) {
@@ -133,14 +133,14 @@ parse_foveon(FILE * const ifp) {
                     timestamp = atoi (foveon_gets (poff[i][1], name, 64));
             }
         }
-        fseek (ifp, save, SEEK_SET);
+        fseek_nofail (ifp, save, SEEK_SET);
     }
     is_foveon = 1;
 }
 
 
 
-void  
+void
 foveon_coeff(int * const useCoeffP,
              float       coeff[3][4]) {
 
@@ -159,8 +159,8 @@ foveon_coeff(int * const useCoeffP,
 
 
 
-static void  
-foveon_decoder (unsigned int const huff[1024], 
+static void
+foveon_decoder (unsigned int const huff[1024],
                 unsigned int const code) {
 
     struct decode *cur;
@@ -180,7 +180,7 @@ foveon_decoder (unsigned int const huff[1024],
             }
         }
     }
-    if ((len = code >> 27) > 26) 
+    if ((len = code >> 27) > 26)
         return;
     code2 = (len+1) << 27 | (code & 0x3ffffff) << 1;
 
@@ -192,14 +192,14 @@ foveon_decoder (unsigned int const huff[1024],
 
 
 
-static void  
+static void
 foveon_load_camf() {
     unsigned int i, val;
     long key;
 
-    fseek (ifp, meta_offset, SEEK_SET);
+    fseek_nofail (ifp, meta_offset, SEEK_SET);
     pm_readlittlelong(ifp, &key);
-    fread (meta_data, 1, meta_length, ifp);
+    fread_nofail (meta_data, 1, meta_length, ifp);
     for (i=0; i < meta_length; i++) {
         key = (key * 1597 + 51749) % 244944;
         assert(have64BitArithmetic);
@@ -210,7 +210,7 @@ foveon_load_camf() {
 
 
 
-void  
+void
 foveon_load_raw(Image const image) {
 
     struct decode *dindex;
@@ -234,14 +234,14 @@ foveon_load_raw(Image const image) {
     for (row=0; row < height; row++) {
         long junk;
         memset (pred, 0, sizeof pred);
-        if (!bit) 
+        if (!bit)
             pm_readlittlelong(ifp, &junk);
         for (col=bit=0; col < width; col++) {
             FORC3 {
                 for (dindex=first_decode; dindex->branch[0]; ) {
                     if ((bit = (bit-1) & 31) == 31)
                         for (i=0; i < 4; i++)
-                            bitbuf = (bitbuf << 8) + fgetc(ifp);
+                            bitbuf = (bitbuf << 8) + fgetc_nofail(ifp);
                     dindex = dindex->branch[bitbuf >> bit & 1];
                 }
                 pred[c] += diff[dindex->leaf];
@@ -262,8 +262,8 @@ sget4(char const s[]) {
 
 
 
-static char *  
-foveon_camf_param (const char * const block, 
+static char *
+foveon_camf_param (const char * const block,
                    const char * const param) {
     unsigned idx, num;
     char *pos, *cp, *dp;
@@ -287,8 +287,8 @@ foveon_camf_param (const char * const block,
 
 
 
-static void *  
-foveon_camf_matrix (int                dim[3], 
+static void *
+foveon_camf_matrix (int                dim[3],
                     const char * const name) {
 
     unsigned i, idx, type, ndim, size, *mat;
@@ -325,9 +325,9 @@ foveon_camf_matrix (int                dim[3],
 
 
 
-static int  
-foveon_fixed (void *       const ptr, 
-              int          const size, 
+static int
+foveon_fixed (void *       const ptr,
+              int          const size,
               const char * const name) {
     void *dp;
     int dim[3];
@@ -345,7 +345,7 @@ static float  foveon_avg (unsigned short *pix, int range[2], float cfilt)
     float val, min=FLT_MAX, max=-FLT_MAX, sum=0;
 
     for (i=range[0]; i <= range[1]; i++) {
-        sum += val = 
+        sum += val =
             (short)pix[i*4] + ((short)pix[i*4]-(short)pix[(i-1)*4]) * cfilt;
         if (min > val) min = val;
         if (max < val) max = val;
@@ -390,11 +390,11 @@ static int  foveon_apply_curve (short *curve, int i)
     return i < 0 ? -(unsigned short)curve[1-i] : (unsigned short)curve[1+i];
 }
 
-void  
+void
 foveon_interpolate(Image const image,
                    float coeff[3][4]) {
 
-    static const short hood[] = { 
+    static const short hood[] = {
         -1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1 };
     short *pix, prev[3], *curve[8], (*shrink)[3];
     float cfilt=0.8, ddft[3][3][2], ppm[3][3][3];
@@ -441,7 +441,7 @@ foveon_interpolate(Image const image,
         for (i=0; i < 3; i++)
             FORC3 diag[c][i] = LAST(1,1)*LAST(2,2) - LAST(1,2)*LAST(2,1);
 #undef LAST
-        FORC3 div[c] = 
+        FORC3 div[c] =
             diag[c][0]*0.3127 + diag[c][1]*0.329 + diag[c][2]*0.3583;
     }
     num = 0;
@@ -494,11 +494,11 @@ foveon_interpolate(Image const image,
     for (row=1; row < height-1; row++) {
         FORC3 if (last[1][c] > last[0][c]) {
             if (last[1][c] > last[2][c])
-                black[row][c] = 
+                black[row][c] =
                     (last[0][c] > last[2][c]) ? last[0][c]:last[2][c];
         } else
             if (last[1][c] < last[2][c])
-                black[row][c] = 
+                black[row][c] =
                     (last[0][c] < last[2][c]) ? last[0][c]:last[2][c];
         memmove (last, last+1, 2*sizeof last[0]);
         memcpy (last[2], black[row+1], sizeof last[2]);
@@ -547,8 +547,8 @@ foveon_interpolate(Image const image,
                 diff = pix[c] - prev[c];
                 prev[c] = pix[c];
                 ipix[c] = pix[c] + floor ((diff + (diff*diff >> 14)) * cfilt
-                                          - ddft[0][c][1] 
-                                          - ddft[0][c][0] 
+                                          - ddft[0][c][1]
+                                          - ddft[0][c][0]
                                             * ((float) col/width - 0.5)
                                           - black[row][c] );
             }
@@ -563,7 +563,7 @@ foveon_interpolate(Image const image,
                         val += ppm[c][i][j] * work[i][j];
                 ipix[c] = floor ((ipix[c] + floor(val)) *
                                  ( sgrow[col/sgx  ][c] * (sgx - col%sgx) +
-                                   sgrow[col/sgx+1][c] * (col%sgx) ) / sgx / 
+                                   sgrow[col/sgx+1][c] * (col%sgx) ) / sgx /
                                  div[c]);
                 if (ipix[c] > 32000) ipix[c] = 32000;
                 pix[c] = ipix[c];
@@ -584,7 +584,7 @@ foveon_interpolate(Image const image,
             memset (fsum, 0, sizeof fsum);
             for (sum=j=0; j < 8; j++)
                 if (badpix[i] & (1 << j)) {
-                    FORC3 fsum[c] += 
+                    FORC3 fsum[c] +=
                         image[(row+hood[j*2])*width+col+hood[j*2+1]][c];
                     sum++;
                 }
@@ -665,8 +665,8 @@ foveon_interpolate(Image const image,
         pix = (short*)image[row*width+2];
         for (col=2; col < width-2; col++) {
             FORC3 dev[c] = -foveon_apply_curve (curve[7], pix[c] -
-                                                ((smrow[1][col][c] + 
-                                                  2*smrow[2][col][c] + 
+                                                ((smrow[1][col][c] +
+                                                  2*smrow[2][col][c] +
                                                   smrow[3][col][c]) >> 2));
             sum = (dev[0] + dev[1] + dev[2]) >> 3;
             FORC3 pix[c] += dev[c] - sum;
@@ -695,7 +695,7 @@ foveon_interpolate(Image const image,
             if (sum < 0) sum = 0;
             j = total[3] > 375 ? (sum << 16) / total[3] : sum * 174;
             FORC3 pix[c] += foveon_apply_curve (curve[6],
-                                                ((j*total[c] + 0x8000) >> 16) 
+                                                ((j*total[c] + 0x8000) >> 16)
                                                 - pix[c]);
             pix += 4;
         }
@@ -732,7 +732,7 @@ foveon_interpolate(Image const image,
                     shrink[row*(width/4)+col][c] = ipix[c] >> 4;
                 else
                     shrink[row*(width/4)+col][c] =
-                        (shrink[(row+1)*(width/4)+col][c]*1840 + 
+                        (shrink[(row+1)*(width/4)+col][c]*1840 +
                          ipix[c]*141 + 2048) >> 12;
         }
     }
@@ -742,7 +742,7 @@ foveon_interpolate(Image const image,
         if ((row & 3) == 0)
             for (col = width & ~3 ; col--; )
                 FORC3 smrow[0][col][c] = ipix[c] =
-                    (shrink[(row/4)*(width/4)+col/4][c]*1485 + 
+                    (shrink[(row/4)*(width/4)+col/4][c]*1485 +
                      ipix[c]*6707 + 4096) >> 13;
 
         /* Then smooth left-to-right */
@@ -757,7 +757,7 @@ foveon_interpolate(Image const image,
         else
             for (col=0; col < (width & ~3); col++)
                 FORC3 smrow[2][col][c] =
-                    (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096) 
+                    (smrow[2][col][c]*6707 + smrow[1][col][c]*1485 + 4096)
                         >> 13;
 
         /* Adjust the chroma toward the smooth values */
@@ -770,7 +770,7 @@ foveon_interpolate(Image const image,
             for (sum=c=0; c < 3; c++) {
                 ipix[c] = foveon_apply_curve(
                     curve[c+3],
-                    ((smrow[2][col][c] * j + 0x8000) >> 16) - 
+                    ((smrow[2][col][c] * j + 0x8000) >> 16) -
                     image[row*width+col][c]);
                 sum += ipix[c];
             }
diff --git a/converter/other/cameratopam/identify.c b/converter/other/cameratopam/identify.c
index 7e77adb2..b76913d1 100644
--- a/converter/other/cameratopam/identify.c
+++ b/converter/other/cameratopam/identify.c
@@ -13,6 +13,7 @@
 #include "dng.h"
 #include "ljpeg.h"
 #include "camera.h"
+#include "stdio_nofail.h"
 
 #include "identify.h"
 
@@ -38,7 +39,7 @@ static const char *memmem_internal (const char *haystack, size_t haystacklen,
 
 
 
-static void 
+static void
 adobeCoeff(const char * const make,
            const char * const model) {
     /*
@@ -47,7 +48,7 @@ adobeCoeff(const char * const make,
     struct CoeffTableEntry {
         const char * prefix;
         short trans[12];
-    }; 
+    };
 
     static struct CoeffTableEntry const table[] = {
         { "Canon EOS D2000C",
@@ -330,17 +331,17 @@ identify(FILE *       const ifp,
 
     order = get2(ifp);
     hlen = get4(ifp);
-    fseek (ifp, 0, SEEK_SET);
-    fread (head, 1, 32, ifp);
-    fseek (ifp, 0, SEEK_END);
-    fsize = ftell(ifp);
+    fseek_nofail (ifp, 0, SEEK_SET);
+    fread_nofail (head, 1, 32, ifp);
+    fseek_nofail (ifp, 0, SEEK_END);
+    fsize = ftell_nofail(ifp);
     if ((c = (char*)memmem_internal(head, 32, "MMMMRawT", 8))) {
         strcpy (make, "Phase One");
         data_offset = c - head;
-        fseek (ifp, data_offset + 8, SEEK_SET);
-        fseek (ifp, get4(ifp) + 136, SEEK_CUR);
+        fseek_nofail (ifp, data_offset + 8, SEEK_SET);
+        fseek_nofail (ifp, get4(ifp) + 136, SEEK_CUR);
         raw_width = get4(ifp);
-        fseek (ifp, 12, SEEK_CUR);
+        fseek_nofail (ifp, 12, SEEK_CUR);
         raw_height = get4(ifp);
     } else if (order == 0x4949 || order == 0x4d4d) {
         if (!memcmp (head+6, "HEAPCCDR", 8)) {
@@ -355,14 +356,14 @@ identify(FILE *       const ifp,
         parse_minolta(ifp);
     else if (!memcmp (head, "\xff\xd8\xff\xe1", 4) &&
              !memcmp (head+6, "Exif", 4)) {
-        fseek (ifp, 4, SEEK_SET);
-        fseek (ifp, 4 + get2(ifp), SEEK_SET);
-        if (fgetc(ifp) != 0xff)
+        fseek_nofail (ifp, 4, SEEK_SET);
+        fseek_nofail (ifp, 4 + get2(ifp), SEEK_SET);
+        if (fgetc_nofail(ifp) != 0xff)
             parse_tiff(ifp, 12);
     } else if (!memcmp (head, "BM", 2)) {
         data_offset = 0x1000;
         order = 0x4949;
-        fseek (ifp, 38, SEEK_SET);
+        fseek_nofail (ifp, 38, SEEK_SET);
         if (get4(ifp) == 2834 && get4(ifp) == 2834) {
             strcpy (model, "BMQ");
             flip = 3;
@@ -373,7 +374,7 @@ identify(FILE *       const ifp,
     nucore:
         strcpy (make, "Nucore");
         order = 0x4949;
-        fseek (ifp, 10, SEEK_SET);
+        fseek_nofail (ifp, 10, SEEK_SET);
         data_offset += get4(ifp);
         get4(ifp);
         raw_width  = get4(ifp);
@@ -385,16 +386,16 @@ identify(FILE *       const ifp,
     } else if (!memcmp (head+25, "ARECOYK", 7)) {
         strcpy (make, "Contax");
         strcpy (model,"N Digital");
-        fseek (ifp, 60, SEEK_SET);
+        fseek_nofail (ifp, 60, SEEK_SET);
         camera_red  = get4(ifp);
         camera_red /= get4(ifp);
         camera_blue = get4(ifp);
         camera_blue = get4(ifp) / camera_blue;
     } else if (!memcmp (head, "FUJIFILM", 8)) {
         long data_offset_long;
-        fseek (ifp, 84, SEEK_SET);
+        fseek_nofail (ifp, 84, SEEK_SET);
         parse_tiff(ifp, get4(ifp)+12);
-        fseek (ifp, 100, SEEK_SET);
+        fseek_nofail (ifp, 100, SEEK_SET);
         pm_readbiglong(ifp, &data_offset_long);
         data_offset = data_offset_long;
     } else if (!memcmp (head, "DSC-Image", 9))
@@ -736,13 +737,13 @@ identify(FILE *       const ifp,
         width  = 2312;
         raw_width = 2336;
         data_offset = 4034;
-        fseek (ifp, 2032, SEEK_SET);
+        fseek_nofail (ifp, 2032, SEEK_SET);
         goto konica_400z;
     } else if (!strcmp(model,"Digital Camera KD-510Z")) {
         data_offset = 4032;
         pre_mul[0] = 1.297;
         pre_mul[2] = 1.438;
-        fseek (ifp, 2032, SEEK_SET);
+        fseek_nofail (ifp, 2032, SEEK_SET);
         goto konica_510z;
     } else if (!strcasecmp(make,"MINOLTA")) {
         load_raw = unpacked_load_raw;
@@ -766,19 +767,19 @@ identify(FILE *       const ifp,
                 data_offset = 5056;
                 pre_mul[0] = 1.602;
                 pre_mul[2] = 1.441;
-                fseek (ifp, 2078, SEEK_SET);
+                fseek_nofail (ifp, 2078, SEEK_SET);
                 height = 1716;
                 width  = 2304;
             } else if (model[8] == '5') {
                 data_offset = 4016;
-                fseek (ifp, 1936, SEEK_SET);
+                fseek_nofail (ifp, 1936, SEEK_SET);
             konica_510z:
                 height = 1956;
                 width  = 2607;
                 raw_width = 2624;
             } else if (model[8] == '6') {
                 data_offset = 4032;
-                fseek (ifp, 2030, SEEK_SET);
+                fseek_nofail (ifp, 2030, SEEK_SET);
                 height = 2136;
                 width  = 2848;
             }
@@ -884,7 +885,7 @@ identify(FILE *       const ifp,
         pre_mul[0] = 1.963;
         pre_mul[2] = 1.430;
     } else if (!strcmp(make,"Sinar") && !memcmp(head,"8BPS",4)) {
-        fseek (ifp, 14, SEEK_SET);
+        fseek_nofail (ifp, 14, SEEK_SET);
         height = get4(ifp);
         width  = get4(ifp);
         filters = 0x61616161;
@@ -1198,7 +1199,7 @@ dng_skip:
             for (i=0; i < 3; i++)
                 coeff[i][3] = coeff[i][1] /= 2;
     }
-    fseek (ifp, data_offset, SEEK_SET);
+    fseek_nofail (ifp, data_offset, SEEK_SET);
 
     *loadRawFnP = load_raw;
 
diff --git a/converter/other/cameratopam/ljpeg.c b/converter/other/cameratopam/ljpeg.c
index 331d258c..35f50f4b 100644
--- a/converter/other/cameratopam/ljpeg.c
+++ b/converter/other/cameratopam/ljpeg.c
@@ -2,7 +2,6 @@
 #define _BSD_SOURCE    /* Make sure string.h containst strcasecmp() */
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <limits.h>
 
 #include "pm.h"
@@ -11,6 +10,7 @@
 #include "util.h"
 #include "decode.h"
 #include "bayer.h"
+#include "stdio_nofail.h"
 
 #include "ljpeg.h"
 
@@ -20,7 +20,7 @@
    enough to decode Canon, Kodak and Adobe DNG images.
  */
 
-int  
+int
 ljpeg_start(FILE *         const ifP,
             struct jhead * const jhP) {
 
@@ -30,12 +30,12 @@ ljpeg_start(FILE *         const ifP,
     init_decoder();
     for (i=0; i < 4; i++)
         jhP->huff[i] = free_decode;
-    fread (data, 2, 1, ifP);
+    fread_nofail (data, 2, 1, ifP);
     if (data[0] != 0xff || data[1] != 0xd8) return 0;
     do {
         unsigned int len;
 
-        fread (data, 2, 2, ifP);
+        fread_nofail (data, 2, 2, ifP);
         tag =  data[0] << 8 | data[1];
         len = data[2] << 8 | data[3];
 
@@ -45,7 +45,7 @@ ljpeg_start(FILE *         const ifP,
             unsigned int const dataLen = len - 2;
 
             if (tag <= 0xff00 || dataLen > 255) return 0;
-            fread (data, 1, dataLen, ifP);
+            fread_nofail (data, 1, dataLen, ifP);
             switch (tag) {
             case 0xffc3:
                 jhP->bits = data[0];
@@ -73,7 +73,7 @@ ljpeg_start(FILE *         const ifP,
 
 
 
-int 
+int
 ljpeg_diff(FILE *          const ifP,
            struct decode * const dindexHeadP) {
 
@@ -111,7 +111,7 @@ ljpeg_row(FILE *         const ifP,
 
 
 
-void  
+void
 lossless_jpeg_load_raw(Image  const image) {
 
     int jwide, jrow, jcol, val, jidx, i, row, col;
@@ -161,5 +161,3 @@ lossless_jpeg_load_raw(Image  const image) {
     if (!strcasecmp(make,"KODAK"))
         black = min;
 }
-
-
diff --git a/converter/other/cameratopam/stdio_nofail.c b/converter/other/cameratopam/stdio_nofail.c
new file mode 100644
index 00000000..0ccfc5b4
--- /dev/null
+++ b/converter/other/cameratopam/stdio_nofail.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <netpbm/pm.h>
+
+#include "stdio_nofail.h"
+
+
+
+size_t
+fread_nofail(void * const ptr,
+             size_t const size,
+             size_t const nmemb,
+             FILE * const streamP) {
+
+    size_t rc;
+
+    rc = fread(ptr, size, nmemb, streamP);
+
+    if (rc < 0)
+        pm_error("File read failed.  Errno=%d (%s)", errno, strerror(errno));
+
+    return rc;
+}
+
+
+
+int
+fgetc_nofail(FILE * streamP) {
+
+    int rc;
+
+    rc = fgetc(streamP);
+
+    if (rc < 0)
+        pm_error("File read failed.  Errno=%d (%s)", errno, strerror(errno));
+
+    return rc;
+}
+
+
+
+int
+fseek_nofail(FILE * const streamP,
+             long   const offset,
+             int    const whence) {
+
+    int rc;
+
+    rc = fseek(streamP, offset, whence);
+
+    if (rc < 0)
+        pm_error("File read failed.  Errno=%d (%s)", errno, strerror(errno));
+
+    return rc;
+}
+
+
+
+long
+ftell_nofail(FILE * const streamP) {
+
+    long rc;
+
+    rc = ftell(streamP);
+
+    if (rc < 0)
+        pm_error("File read failed.  Errno=%d (%s)", errno, strerror(errno));
+
+    return rc;
+}
+
+
+
diff --git a/converter/other/cameratopam/stdio_nofail.h b/converter/other/cameratopam/stdio_nofail.h
new file mode 100644
index 00000000..9d6fcea5
--- /dev/null
+++ b/converter/other/cameratopam/stdio_nofail.h
@@ -0,0 +1,18 @@
+#include <stdio.h>
+
+size_t
+fread_nofail(void * const ptr,
+             size_t const size,
+             size_t const nmemb,
+             FILE * const streamP);
+
+int
+fgetc_nofail(FILE * const streamP);
+
+int
+fseek_nofail(FILE * const streamP,
+             long   const offset,
+             int    const whence);
+
+long
+ftell_nofail(FILE * const streamP);
diff --git a/converter/other/cameratopam/util.c b/converter/other/cameratopam/util.c
index b3ccf7e9..8781473a 100644
--- a/converter/other/cameratopam/util.c
+++ b/converter/other/cameratopam/util.c
@@ -1,10 +1,10 @@
 #define _XOPEN_SOURCE   /* Make sure unistd.h contains swab() */
 #include <unistd.h>
-#include <stdio.h>
 
 #include "pm.h"
 #include "global_variables.h"
 #include "util.h"
+#include "stdio_nofail.h"
 
 #ifndef LONG_BITS
 #define LONG_BITS (8 * sizeof(long))
@@ -18,7 +18,7 @@ get2(FILE * const ifp)
 {
     unsigned char a, b;
 
-    a = fgetc(ifp);  b = fgetc(ifp);
+    a = fgetc_nofail(ifp);  b = fgetc_nofail(ifp);
 
     if (order == 0x4949)      /* "II" means little-endian */
         return a | b << 8;
@@ -34,8 +34,8 @@ get4(FILE * const ifp)
 {
     unsigned char a, b, c, d;
 
-    a = fgetc(ifp);  b = fgetc(ifp);
-    c = fgetc(ifp);  d = fgetc(ifp);
+    a = fgetc_nofail(ifp);  b = fgetc_nofail(ifp);
+    c = fgetc_nofail(ifp);  d = fgetc_nofail(ifp);
 
     if (order == 0x4949)
         return a | b << 8 | c << 16 | d << 24;
@@ -49,7 +49,7 @@ get4(FILE * const ifp)
 void
 read_shorts (FILE * const ifp, unsigned short *pixel, int count)
 {
-    fread (pixel, 2, count, ifp);
+    fread_nofail (pixel, 2, count, ifp);
     if ((order == 0x4949) == (BYTE_ORDER == BIG_ENDIAN))
         swab (pixel, pixel, count*2);
 }
@@ -73,10 +73,10 @@ getbits (FILE * const ifp, int nbits)
         vbits -= nbits;
     }
     while (vbits < LONG_BITS - 7) {
-        c = fgetc(ifp);
+        c = fgetc_nofail(ifp);
         bitbuf = (bitbuf << 8) + c;
         if (c == 0xff && zero_after_ff)
-            fgetc(ifp);
+            fgetc_nofail(ifp);
         vbits += 8;
     }
     return ret;