about summary refs log tree commit diff
path: root/other
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2014-03-22 02:05:41 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2014-03-22 02:05:41 +0000
commit23845f8e2ef3a9277d81dddf2ae42d32182118b5 (patch)
tree1b37dd9db23f2c5b4d3a24a63f13ed44fa7df5f4 /other
parent18bd4b40fcf6854c8db23cf3c67982b31c262e92 (diff)
downloadnetpbm-mirror-23845f8e2ef3a9277d81dddf2ae42d32182118b5.tar.gz
netpbm-mirror-23845f8e2ef3a9277d81dddf2ae42d32182118b5.tar.xz
netpbm-mirror-23845f8e2ef3a9277d81dddf2ae42d32182118b5.zip
Add pamvalidate
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2162 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'other')
-rw-r--r--other/Makefile2
-rw-r--r--other/pamvalidate.c86
2 files changed, 87 insertions, 1 deletions
diff --git a/other/Makefile b/other/Makefile
index 23e60bc1..69562be0 100644
--- a/other/Makefile
+++ b/other/Makefile
@@ -25,7 +25,7 @@ endif
 
 PORTBINARIES = pamarith pambayer pamchannel pamdepth \
 	pamendian pamexec pamfix pamlookup pampick pamsplit \
-	pamstack pamsummcol pnmcolormap \
+	pamstack pamsummcol pamvalidate pnmcolormap \
 	ppmdcfont ppmddumpfont ppmdmkfont 
 
 BINARIES = $(PORTBINARIES)
diff --git a/other/pamvalidate.c b/other/pamvalidate.c
new file mode 100644
index 00000000..a7b08b8e
--- /dev/null
+++ b/other/pamvalidate.c
@@ -0,0 +1,86 @@
+/*=============================================================================
+                               pamvalidate
+===============================================================================
+  Part of the Netpbm package.
+
+  Copy PAM and PNM (i.e. PBM, PGM, or PPM) images from Standard Input
+  to Standard Output.  No output when input is invalid.
+
+  Contributed to the public domain by its author.
+=============================================================================*/
+
+#include <string.h>
+#include "pm_c_util.h"
+#include "pam.h"
+
+
+
+int
+main(int argc, const char * argv[]) {
+
+    FILE * const tmpfile = pm_tmpfile();
+    int        eof;     /* no more images in input stream */
+    struct pam inpam;   /* Input PAM image */
+    struct pam outpam;  /* Output PAM image */
+
+    pm_proginit(&argc, argv);
+
+    if (argc-1 != 0)
+        pm_error("Program takes no arguments.  Input is from Standard Input");
+
+    for (eof = FALSE; !eof; ) {
+        pnm_readpaminit(stdin, &inpam, PAM_STRUCT_SIZE(tuple_type));
+
+        outpam = inpam;  /* initial value */
+        outpam.file = tmpfile;
+
+        pnm_writepaminit(&outpam);
+
+        if (PNM_FORMAT_TYPE(inpam.format) == PBM_TYPE) {
+            /* Fast method for PBM */
+            unsigned char * const inrow = pbm_allocrow_packed(inpam.width);
+
+            unsigned int row;
+
+            for (row = 0; row < inpam.height; ++row) {
+                pbm_readpbmrow_packed(inpam.file, inrow, inpam.width,
+                                      inpam.format);
+                pbm_writepbmrow_packed(tmpfile, inrow, inpam.width, 0);
+            }
+            pbm_freerow(inrow);
+
+        } else {
+            /* General case.  Logic works for PBM */
+            tuple * const tuplerow = pnm_allocpamrow(&inpam);
+
+            unsigned int row;
+
+            for (row = 0; row < inpam.height; ++row) {
+                pnm_readpamrow(&inpam, tuplerow);
+                pnm_writepamrow(&outpam, tuplerow);
+            }
+            pnm_freepamrow(tuplerow);
+        }
+        pnm_nextimage(stdin, &eof);
+    }
+
+    fseek(tmpfile, 0, SEEK_SET);
+
+    while (!feof(tmpfile) && !ferror(tmpfile) && !ferror(stdout)) {
+        char buffer[4096];
+        size_t bytesReadCt;
+
+        bytesReadCt = fread(buffer, 1, sizeof(buffer), tmpfile);
+
+        if (ferror(tmpfile))
+            pm_error("Error reading from temporary file.  "
+                     "Incomplete output.  "    
+                     "Errno = %s (%d)", strerror(errno), errno);
+        else
+            fwrite(buffer, 1, bytesReadCt, stdout);
+    }
+    pm_close(tmpfile);
+    pm_close(stdout);
+
+    return 0;
+}