diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2019-06-28 23:07:55 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2019-06-28 23:07:55 +0000 |
commit | 11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32 (patch) | |
tree | 7c40f096dd973943ef563ec87b2a68d8205db4fb /other/pamvalidate.c | |
parent | 89c6ec14eb7514630aea5abc4b90b51d1473d33a (diff) | |
download | netpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.tar.gz netpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.tar.xz netpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.zip |
Promote Stable to Super_stable
git-svn-id: http://svn.code.sf.net/p/netpbm/code/super_stable@3640 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'other/pamvalidate.c')
-rw-r--r-- | other/pamvalidate.c | 86 |
1 files changed, 86 insertions, 0 deletions
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; +} |