about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--GNUmakefile26
-rw-r--r--Makefile.common7
-rw-r--r--Makefile.config.in2
-rw-r--r--Makefile.version4
-rw-r--r--buildtools/README.pkg10
-rwxr-xr-xbuildtools/configure.pl67
-rwxr-xr-xbuildtools/installnetpbm.pl14
-rw-r--r--converter/other/Makefile2
-rw-r--r--converter/other/bmptopnm.c85
-rw-r--r--converter/other/pamtopam.c57
-rw-r--r--converter/other/pnmtopalm/gen_palm_colormap.c33
-rw-r--r--converter/other/pnmtopalm/palmcolor8.map458
-rw-r--r--converter/other/pnmtorast.c139
-rw-r--r--converter/other/rast.h30
-rw-r--r--converter/other/tifftopnm.c11
-rw-r--r--converter/ppm/ppmtompeg/Makefile14
-rw-r--r--converter/ppm/ppmtompeg/gethostname_win32.c402
-rw-r--r--converter/ppm/ppmtompeg/specifics.c45
-rw-r--r--doc/HISTORY57
-rw-r--r--editor/pamenlarge.c243
-rw-r--r--editor/pamflip.c2
-rw-r--r--editor/pamscale.c2
-rw-r--r--editor/pnmcat.c16
-rw-r--r--editor/pnmmontage.c460
-rw-r--r--generator/pbmmake.c12
-rw-r--r--lib/Makefile10
-rw-r--r--lib/bitio.h4
-rw-r--r--lib/colorname.h2
-rw-r--r--lib/fileio.c12
-rw-r--r--lib/libpam.h2
-rw-r--r--lib/libpbm1.c28
-rw-r--r--lib/libpbm2.c25
-rw-r--r--lib/libpbm3.c1
-rw-r--r--lib/libpgm1.c5
-rw-r--r--lib/libpgm2.c1
-rw-r--r--lib/libpm.c972
-rw-r--r--lib/libpnm2.c3
-rw-r--r--lib/libpnm3.c3
-rw-r--r--lib/libppm2.c1
-rw-r--r--lib/libppmcmap.c1
-rw-r--r--lib/pam.h4
-rw-r--r--lib/pammap.h4
-rw-r--r--lib/pbm.h11
-rw-r--r--lib/pbmfont.h2
-rw-r--r--lib/pgm.h5
-rw-r--r--lib/pm.h19
-rw-r--r--lib/pm_gamma.h2
-rw-r--r--lib/pmfileio.c948
-rw-r--r--lib/pnm.h9
-rw-r--r--lib/ppm.h7
-rw-r--r--lib/ppmcmap.h40
-rw-r--r--lib/util/Makefile2
-rw-r--r--lib/util/mallocvar.h2
-rw-r--r--lib/util/nstring.c40
-rw-r--r--lib/util/nstring.h5
-rw-r--r--other/pamarith.c431
56 files changed, 3139 insertions, 1660 deletions
diff --git a/GNUmakefile b/GNUmakefile
index 0103aa9f..33a01b43 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -145,11 +145,11 @@ endif
 MAJOR := $(NETPBM_MAJOR_RELEASE)
 MINOR := $(NETPBM_MINOR_RELEASE)
 POINT := $(NETPBM_POINT_RELEASE)
-version.h:
+version.h: $(SRCDIR)/Makefile.version
 	@rm -f $@
-	@echo "/* Generated by make file rule */" >$@
+	@echo "/* Generated by make file rule */" >>$@
 	@echo "#define NETPBM_VERSION" \
-	  \"Netpbm $(MAJOR).$(MINOR).$(POINT)"\"" >$@
+	  \"Netpbm $(MAJOR).$(MINOR).$(POINT)"\"" >>$@
 
 
 .PHONY: install
@@ -346,11 +346,14 @@ $(PKGDIR)/bin/doc.url: $(PKGDIR)/bin
 install-dev: install.hdr install.staticlib install.lib install.sharedlibstub
 
 .PHONY: install.hdr
-install.hdr: $(PKGDIR)/include
-	$(MAKE) -C lib -f $(SRCDIR)/lib/Makefile \
-	    SRCDIR=$(SRCDIR) BUILDDIR=$(BUILDDIR) install.hdr
+install.hdr: lib/install.hdr $(PKGDIR)/include/netpbm
 	$(INSTALL) -c -m $(INSTALL_PERM_HDR) \
-	    $(BUILDDIR)/pm_config.h $(PKGDIR)/include
+	    $(BUILDDIR)/pm_config.h $(PKGDIR)/include/netpbm
+
+.PHONY: lib/install.hdr
+lib/install.hdr:
+	$(MAKE) -C $(dir $@) -f $(SRCDIR)/lib/Makefile \
+	    SRCDIR=$(SRCDIR) BUILDDIR=$(BUILDDIR) $(notdir $@)
 
 ifeq ($(STATICLIB_TOO),y)
 BUILD_STATIC = y
@@ -387,8 +390,17 @@ distclean: localdistclean
 .PHONY: localdistclean
 localdistclean: localclean
 	-rm -f `find -type l`
+	-rm -f TAGS
 	-rm -f Makefile.config
 
+# 'tags' generates/updates an Emacs tags file, anmed TAGS, in the current
+# directory.  Use with Emacs command 'find-tag'.
+
+.PHONY: tags
+tags:
+	find . -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.hpp" | \
+	  etags -
+
 # The following endif is for the else block that contains virtually the
 # whole file, for the test of the existence of CURDIR.
 endif
diff --git a/Makefile.common b/Makefile.common
index 16bb58ef..c626f226 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -36,7 +36,6 @@
 # INSTALL: command to use to copy files to where they belong
 # INSTALL_PERM_BIN: file permissions for installed binaries
 # INSTALL_PERM_LIB: ...same for libraries
-# INSTALL_PERM_HDR: ...same for headers
 # INSTALL_PERM_MAN: ...same for man pages
 # MERGE_OBJECTS: list of object files that go into the merged executable
 #   from the current directory (not subdirectories).  All of these are to
@@ -139,10 +138,16 @@ IMPORTINC_LIB_FILES := $(IMPORTINC_LIB_HEADERS:%=importinc/%)
 IMPORTINC_LIB_UTIL_FILES := $(IMPORTINC_LIB_UTIL_HEADERS:%=importinc/%)
 
 importinc: \
+  importinc/netpbm \
   $(IMPORTINC_ROOT_FILES) \
   $(IMPORTINC_LIB_FILES) \
   $(IMPORTINC_LIB_UTIL_FILES) \
 
+importinc/netpbm:
+	mkdir -p importinc
+	rm -f $@
+	$(SYMLINK) . $@
+
 $(IMPORTINC_ROOT_FILES):importinc/%:$(BUILDDIR)/%
 	mkdir -p importinc
 	rm -f $@
diff --git a/Makefile.config.in b/Makefile.config.in
index 2cf49fa6..1f7a44cb 100644
--- a/Makefile.config.in
+++ b/Makefile.config.in
@@ -292,7 +292,7 @@ LDRELOC = NONE
 # know why -- history seems to be repeating itself.  2005.02.23.
 
 CFLAGS_SHLIB = 
-# Solaris or SunOS with gcc, and NetBSD:
+# Gcc:
 #CFLAGS_SHLIB = -fpic
 #CFLAGS_SHLIB = -fPIC
 # Sun compiler:
diff --git a/Makefile.version b/Makefile.version
index 31809399..eb0b63ef 100644
--- a/Makefile.version
+++ b/Makefile.version
@@ -1,4 +1,4 @@
 NETPBM_MAJOR_RELEASE = 10
-NETPBM_MINOR_RELEASE = 40
-NETPBM_POINT_RELEASE = 06
+NETPBM_MINOR_RELEASE = 41
+NETPBM_POINT_RELEASE = 00
 
diff --git a/buildtools/README.pkg b/buildtools/README.pkg
index b2a6b0ef..e544cbb7 100644
--- a/buildtools/README.pkg
+++ b/buildtools/README.pkg
@@ -69,6 +69,16 @@ The parts to be installed are:
     that is in the default search path of your compiler.  Typical
     directories for this are /usr/include and /usr/local/include.
 
+    All of the files are meant to be named like <netpbm/xxx.h> for C
+    #include purposes, which means on an ordinary system, they are
+    installed in a directory named "netpbm".  Thus, they are organized
+    this way in the package.  BUT: until Netpbm 10.41 (December 2007),
+    they were packaged for, and customarily used, just as <xxx.h>.
+    Therefore, for backward compatibility, it is a good idea to make
+    symbolic links from the parent directory into the netpbm/
+    directory.  But as that may pollute your namespace, you may prefer
+    just to make all users migrate to the <netpbm/xxx.h> form.
+
   Data Files
 
     These are files that you can use for various purposes as input to
diff --git a/buildtools/configure.pl b/buildtools/configure.pl
index 3a0592ad..9cb1313a 100755
--- a/buildtools/configure.pl
+++ b/buildtools/configure.pl
@@ -157,22 +157,14 @@ sub chooseTestCompiler($$) {
 
     my $cc;
 
-    if (!defined($compiler)) {
-        if ($ENV{'CC'}) {
-            $cc = $ENV{'CC'};
-        } else {
-            if (commandExists('cc')) {
-                $cc = 'cc';
-            } elsif (commandExists("gcc")) {
-                $cc = 'gcc';
-            }
-        }
-    } elsif ($compiler eq 'cc') {
-        $cc = "cc";
-    } elsif ($compiler eq 'gcc') {
-        $cc = 'gcc';
+    if ($ENV{'CC'}) {
+        $cc = $ENV{'CC'};
     } else {
-        die("Internal error: invalid value \"$compiler\" for \$compiler");
+        if (commandExists('cc')) {
+            $cc = 'cc';
+        } elsif (commandExists("gcc")) {
+            $cc = 'gcc';
+        }
     }
     $$testCcR = $cc;
 }
@@ -469,7 +461,8 @@ sub getCompiler($$) {
 #    need different options.
 #
 #  - There are basically two choices on any system:  native compiler or
-#    GNU compiler.  That's all this program recognizes, anyway.
+#    GNU compiler.  That's all this program recognizes, anyway.  On some,
+#    native _is_ GNU, and we return 'gcc'.
 #
 #  - A user may well have various compilers.  Different releases, using
 #    different standard libraries, for different target machines, etc.
@@ -488,15 +481,24 @@ sub getCompiler($$) {
 #
 # The value this subroutine returns is NOT the command name to invoke the
 # compiler.  It is simply "cc" to mean native compiler or "gcc" to mean
-# GNU compiler or undefined to express no preference.
+# GNU compiler.
 #-----------------------------------------------------------------------------
-    my %gccCapablePlatform = ("SOLARIS" => 1,
-                              "TRU64"   => 1,
-                              "SCO"     => 1,
-                              "AIX"     => 1,
-                              "HP"      => 1);
-
-    if ($gccCapablePlatform{$platform}) {
+    my %gccOptionalPlatform = ("SOLARIS" => 1,
+                               "TRU64"   => 1,
+                               "SCO"     => 1,
+                               "AIX"     => 1,
+                               "HP"      => 1);
+
+    my %gccUsualPlatform = ("GNU"     => 1,
+                            "NETBSD"  => 1,
+                            "OPENBSD" => 1,
+                            "FREEBSD" => 1,
+                            "DARWIN"  => 1,
+                            );
+
+    if ($gccUsualPlatform{$platform}) {
+        $$compilerR = "gcc";
+    } elsif ($gccOptionalPlatform{$platform}) {
         print("GNU compiler or native operating system compiler (cc)?\n");
         print("\n");
 
@@ -529,6 +531,8 @@ sub getCompiler($$) {
                   "makefile variable or install 'gcc'\n");
         }
         print("\n");
+    } else {
+        $$compilerR = 'cc';
     }
 }
 
@@ -1623,7 +1627,7 @@ sub printNoLibxml2Warning() {
 
 WARNING: You appear not to have Libxml2 installed ('xml2-config' does not
 exist in your program search PATH).  If this is the case at build time,
-the build will skip building 'svtgtopam'.
+the build will skip building 'svgtopam'.
 
 EOF
 }
@@ -2056,7 +2060,7 @@ if ($platform eq "GNU") {
         # above does NOT work for HP native compiler.
     }
 } elsif ($platform eq "AIX") {
-    push(@Makefile_config, 'LDFLAGS = -L /usr/pubsw/lib', "\n");
+    push(@Makefile_config, 'LDFLAGS += -L /usr/pubsw/lib', "\n");
     if ($compiler eq "cc") {
         # Yes, the -L option implies the runtime as well as linktime library
         # search path.  There's no way to specify runtime path independently.
@@ -2114,8 +2118,6 @@ if ($platform eq "GNU") {
          '-shared -Wl,--image-base=0x10000000 -Wl,--enable-auto-import', "\n");
 } elsif ($platform eq "BEOS") {
     push(@Makefile_config, "LDSHLIB = -nostart\n");
-} elsif ($platform eq "NETBSD") {
-    push(@Makefile_config, 'CFLAGS_SHLIB = -fpic', "\n");
 } elsif ($platform eq "OPENBSD") {
     # vedge@vedge.com.ar says on 2001.04.29 that there are a ton of 
     # undefined symbols in the Fiasco stuff on OpenBSD.  So we'll just
@@ -2132,13 +2134,12 @@ if ($platform eq "GNU") {
     if ($compiler eq "cc") {
         push(@Makefile_config, "CFLAGS = -O\n");
         push(@Makefile_config, "CFLAGS_SHLIB = -O -K pic\n");
-        push(@Makefile_config, "LD_SHLIB = -G\n");
+        push(@Makefile_config, "LDSHLIB = -G\n");
         push(@Makefile_config, "SHLIB_CLIB =\n");
     } else {
         makeCompilerGcc(\@Makefile_config);
+        push(@Makefile_config, "LDSHLIB = -shared\n"); 
     }
-    push(@Makefile_config, "CFLAGS_SHLIB = -fPIC\n");
-    push(@Makefile_config, "LDSHLIB = -shared\n"); 
     push(@Makefile_config, "NETWORKLD = -lsocket -lresolve\n");
 } elsif ($platform eq "DARWIN") {
     push(@Makefile_config, "CC = cc -no-cpp-precomp\n");
@@ -2192,6 +2193,10 @@ if (!$flex_result) {
     }
 }
 
+if ($compiler eq 'gcc') {
+    push(@Makefile_config, "CFLAGS_SHLIB += -fPIC\n");
+}
+
 if (defined($tiffhdr_dir)) {
     push(@Makefile_config, "TIFFHDR_DIR = $tiffhdr_dir\n");
 }
diff --git a/buildtools/installnetpbm.pl b/buildtools/installnetpbm.pl
index f6ab7ca0..25376ec3 100755
--- a/buildtools/installnetpbm.pl
+++ b/buildtools/installnetpbm.pl
@@ -554,7 +554,19 @@ sub installHeader($$$) {
               "failed.\n");
         print("cp exit code is $rc\n");
     } else {
-        print("done.\n");
+        # Install symbolic links for backward compatibility (because the
+        # netpbm/ subdirectory wasn't used before Netpbm 10.41 (December
+        # 2007).
+
+        my $rc = system("cd $hdrDir; ln -s netpbm/* .");
+
+        if ($rc != 0) {
+            print("Failed to create backward compatibilty symlinks from " .
+                  "$hdrDir into $hdrDir/netpbm\n");
+            print("ln exit code is $rc\n");
+        } else {
+            print("done.\n");
+        }
     }
     $$includedirR = $hdrDir;
 }
diff --git a/converter/other/Makefile b/converter/other/Makefile
index 976c215a..3e2c2faa 100644
--- a/converter/other/Makefile
+++ b/converter/other/Makefile
@@ -81,7 +81,7 @@ PORTBINARIES =  bmptopnm fitstopnm \
 		gemtopnm giftopnm hdifftopam infotopam \
 		pamtodjvurle pamtofits pamtogif \
 		pamtohdiff pamtohtmltbl pamtooctaveimg \
-		pamtopfm pamtopnm pamtouil \
+		pamtopam pamtopfm pamtopnm pamtouil \
 		pamtoxvmini \
 		pbmtopgm pfmtopam \
 	        pgmtopbm pgmtoppm ppmtopgm pnmtoddif \
diff --git a/converter/other/bmptopnm.c b/converter/other/bmptopnm.c
index 4d29e4d3..aaaf59c1 100644
--- a/converter/other/bmptopnm.c
+++ b/converter/other/bmptopnm.c
@@ -17,6 +17,11 @@
  in supporting documentation.  This software is provided "as is"
  without express or implied warranty.
 
+ Note: From mid-2003 to mid-2007, this program would crash on any 16
+ bit BMP without transparency and no one reported it.  Before that, it
+ refused to even try to read a 16 bit BMP.  I conclude that esentially
+ nobody is using 16 bit BMP.
+
 *****************************************************************************/
 #include <string.h>
 #include <limits.h>
@@ -42,6 +47,10 @@ struct bitPosition {
 
        Example: if 16 bits are laid out as XRRRRRGGGGGBBBBB then the shift
        count for the R component is 10 and the mask is 0000000000011111.
+
+       A 'mask' of zero denotes absence of any bits; e.g. in the example
+       above, the mask for the transparency component is zero because there
+       is no transparency component .  'shift' is arbitrary in that case.
     */
     unsigned int shift;
         /* How many bits right you have to shift the value to get the subject
@@ -303,6 +312,36 @@ readOs2InfoHeader(FILE *                 const ifP,
 
 
 static void
+validateCompression(unsigned long const compression,
+                    enum rowOrder const rowOrder,
+                    unsigned int  const cBitCount) {
+    
+    if (compression != COMP_RGB && compression != COMP_BITFIELDS &&
+        compression != COMP_RLE4 && compression != COMP_RLE8 ) 
+        pm_error("Input has unknown encoding.  "
+                 "Compression type code = %ld.  The only ones we know "
+                 "are RGB (%u), BITFIELDS (%u), "
+                 "RLE4 (%u), and RLE8 (%u)",
+                 compression, COMP_RGB, COMP_BITFIELDS,
+                 COMP_RLE4, COMP_RLE8);
+                     
+    if ((compression == COMP_RLE4 || compression == COMP_RLE8) &&
+        rowOrder == TOPDOWN )                        
+        pm_error("Invalid BMP header.  Claims image is top-down and also "
+                 "compressed, which is an impossible combination.");
+
+    if ( (compression == COMP_RLE4 && cBitCount !=4) ||
+         (compression == COMP_RLE8 && cBitCount !=8) ) 
+        pm_error("Invalid BMP header.  " 
+                 "Compression type (%s) disagrees with "
+                 "number of bits per pixel (%u).",
+                 compression == COMP_RLE4 ? "RLE4" : "RLE8",
+                 cBitCount);
+}
+
+
+
+static void
 readWindowsBasic40ByteInfoHeader(FILE *                 const ifP,
                                  struct bmpInfoHeader * const headerP) {
 /*----------------------------------------------------------------------------
@@ -335,22 +374,9 @@ readWindowsBasic40ByteInfoHeader(FILE *                 const ifP,
         unsigned long int const compression = GetLong(ifP);
 
         headerP->bitFields = (compression == COMP_BITFIELDS);
-    
-        if (compression != COMP_RGB && compression != COMP_BITFIELDS &&
-            compression != COMP_RLE4 && compression != COMP_RLE8 ) 
-            pm_error("Input is compressed.  Unsupported encoding method.\n"
-                     "Compression type code = %ld", compression);
-                     
-        if ( (compression == COMP_RLE4 || compression == COMP_RLE8) &&
-              headerP->rowOrder == TOPDOWN )                        
-            pm_error("Invalid BMP header.  Top-down images cannot be compressed.");
 
-        if ( (compression == COMP_RLE4 && headerP->cBitCount !=4) ||
-             (compression == COMP_RLE8 && headerP->cBitCount !=8) )                        
-            pm_error("Invalid BMP header.\n" 
-                     "Compression type (%s) disagrees with number of bits per pixel (%u).",
-                      compression == COMP_RLE4 ? "RLE4" : "RLE8",
-                      headerP->cBitCount);
+        validateCompression(compression, headerP->rowOrder,
+                            headerP->cBitCount);
 
         headerP->compression = compression;             
     }
@@ -458,6 +484,10 @@ defaultPixelformat(unsigned int const bitCount) {
 
     switch (bitCount) {
     case 16:
+        /* This layout is sometimes called "RGB555".  A document from
+           Microsoft says this is the default (when the "compression"
+           field of the header says COMP_BITFIELDS).
+        */
         retval.conventionalBgr = FALSE;
         retval.red.shift = 10;
         retval.grn.shift = 5;
@@ -495,6 +525,11 @@ readV4InfoHeaderExtension(FILE *                 const ifP,
                           struct bmpInfoHeader * const headerP) {
 
     if (headerP->bitFields) {
+        /* A document from Microsoft says on Windows 95 there is no
+           transparency plane and (red, green, blue) must be either
+           (5,5,5) or (5,6,5) for 16 bit and (8,8,8) for 32 bit.
+           It calls these RGB555, RGB565, RGB888.
+        */
         headerP->pixelformat.red = bitPositionFromMask(GetLong(ifP));
         headerP->pixelformat.grn = bitPositionFromMask(GetLong(ifP));
         headerP->pixelformat.blu = bitPositionFromMask(GetLong(ifP));
@@ -650,16 +685,16 @@ extractBitFields(unsigned int       const rasterval,
         (rasterval >> pixelformat.blu.shift) & pixelformat.blu.mask;
     unsigned int const abits = 
         (rasterval >> pixelformat.trn.shift) & pixelformat.trn.mask;
-    
-    *rP = pixelformat.red.mask ?
-              (unsigned int) rbits * maxval / pixelformat.red.mask : 0;
-    *gP = pixelformat.grn.mask ?
-              (unsigned int) gbits * maxval / pixelformat.grn.mask : 0;
-    *bP = pixelformat.blu.mask ?
-              (unsigned int) bbits * maxval / pixelformat.blu.mask : 0;
-    *aP = pixelformat.trn.mask ?
-              (unsigned int) abits * maxval / pixelformat.trn.mask : 0;
-}
+
+    *rP = pixelformat.red.mask > 0 ?
+        (unsigned int) rbits * maxval / pixelformat.red.mask : 0;
+    *gP = pixelformat.grn.mask > 0 ?
+        (unsigned int) gbits * maxval / pixelformat.grn.mask : 0;
+    *bP = pixelformat.blu.mask > 0 ?
+        (unsigned int) bbits * maxval / pixelformat.blu.mask : 0;
+    *aP = pixelformat.trn.mask > 0 ?
+        (unsigned int) abits * maxval / pixelformat.trn.mask : 0;
+}        
 
 
 
diff --git a/converter/other/pamtopam.c b/converter/other/pamtopam.c
new file mode 100644
index 00000000..cae54060
--- /dev/null
+++ b/converter/other/pamtopam.c
@@ -0,0 +1,57 @@
+/*=============================================================================
+                               pamtopam
+===============================================================================
+  Part of the Netpbm package.
+
+  Copy PAM and PNM (i.e. PBM, PGM, or PPM) images from Standard Input
+  to Standard Output (while converting PNM images to PAM)
+
+  By Paul Bolle October 2007.
+
+  Contributed to the public domain by its author.
+=============================================================================*/
+
+#include "pm_c_util.h"
+#include "pam.h"
+
+int
+main(int argc, const char * argv[]) {
+
+    bool       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");
+
+    eof = FALSE;
+    while (!eof) {
+        pnm_readpaminit(stdin, &inpam, PAM_STRUCT_SIZE(tuple_type));
+
+        outpam = inpam;
+        outpam.file = stdout;
+        outpam.format = PAM_FORMAT;
+
+        pnm_writepaminit(&outpam);
+
+        {
+            tuple * tuplerow;
+
+            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);
+    }
+
+    return 0;
+}
diff --git a/converter/other/pnmtopalm/gen_palm_colormap.c b/converter/other/pnmtopalm/gen_palm_colormap.c
index b71854b2..c7172c6b 100644
--- a/converter/other/pnmtopalm/gen_palm_colormap.c
+++ b/converter/other/pnmtopalm/gen_palm_colormap.c
@@ -1,9 +1,10 @@
 /* gen_palm_colormap.c - generate a ppm file containing the default Palm colormap
  *
- * Bill Janssen  <bill@janssen.org>
+ * Based on an earlier version by Bill Janssen  <bill@janssen.org>
  */
 
-#include "pnm.h"
+#include "ppm.h"
+#include "pm_c_util.h"
 
 #include "palm.h"
 
@@ -11,19 +12,33 @@ int
 main(int     argc,
      char ** argv) {
 
-    Colormap const defaultMap = palmcolor_build_default_8bit_colormap();
-
+    Colormap defaultMap;
     unsigned int i;
+    pixel pix;
     
-    printf("P3\n%d 1\n255\n", defaultMap->ncolors);
+    defaultMap = palmcolor_build_default_8bit_colormap();
+    qsort (defaultMap->color_entries, defaultMap->ncolors,
+           sizeof(Color_s), palmcolor_compare_indices);
+
+    ppm_writeppminit(stdout, 256, 1, 255, TRUE);
 
     for (i = 0; i < defaultMap->ncolors; ++i) {
         Color_s const current = defaultMap->color_entries[i];
 
-        printf("%u %u %u\n",
-               (unsigned char)(current >> 16),
-               (unsigned char)(current >>  8),
-               (unsigned char)(current >>  0));
+        PPM_ASSIGN(pix,
+                   (current >> 16) & 0xff,
+                   (current >>  8) & 0xff,
+                   (current >>  0) & 0xff);
+
+        ppm_writeppmrow(stdout, &pix, 1, 255, TRUE);
+    }
+
+    /* palmcolor_build_default_8bit_colormap() builds a map of the 231 default
+     * palm colors and 1 extra black pixel. Add another 24 extra black pixels
+     * as per spec. */
+    PPM_ASSIGN(pix, 0, 0, 0);
+    for (i = 0; i < 256 - defaultMap->ncolors; ++i) {
+        ppm_writeppmrow(stdout, &pix, 1, 255, TRUE);
     }
 
     return 0;
diff --git a/converter/other/pnmtopalm/palmcolor8.map b/converter/other/pnmtopalm/palmcolor8.map
index 2e054616..a4840118 100644
--- a/converter/other/pnmtopalm/palmcolor8.map
+++ b/converter/other/pnmtopalm/palmcolor8.map
@@ -1,235 +1,259 @@
 P3
-232 1
+256 1
 255
-0 0 0
-0 0 0
-0 0 51
-0 0 102
-0 0 153
-0 0 204
+255 255 255
+255 204 255
+255 153 255
+255 102 255
+255 51 255
+255 0 255
+255 255 204
+255 204 204
+255 153 204
+255 102 204
+255 51 204
+255 0 204
+255 255 153
+255 204 153
+255 153 153
+255 102 153
+255 51 153
+255 0 153
+204 255 255
+204 204 255
+204 153 255
+204 102 255
+204 51 255
+204 0 255
+204 255 204
+204 204 204
+204 153 204
+204 102 204
+204 51 204
+204 0 204
+204 255 153
+204 204 153
+204 153 153
+204 102 153
+204 51 153
+204 0 153
+153 255 255
+153 204 255
+153 153 255
+153 102 255
+153 51 255
+153 0 255
+153 255 204
+153 204 204
+153 153 204
+153 102 204
+153 51 204
+153 0 204
+153 255 153
+153 204 153
+153 153 153
+153 102 153
+153 51 153
+153 0 153
+102 255 255
+102 204 255
+102 153 255
+102 102 255
+102 51 255
+102 0 255
+102 255 204
+102 204 204
+102 153 204
+102 102 204
+102 51 204
+102 0 204
+102 255 153
+102 204 153
+102 153 153
+102 102 153
+102 51 153
+102 0 153
+51 255 255
+51 204 255
+51 153 255
+51 102 255
+51 51 255
+51 0 255
+51 255 204
+51 204 204
+51 153 204
+51 102 204
+51 51 204
+51 0 204
+51 255 153
+51 204 153
+51 153 153
+51 102 153
+51 51 153
+51 0 153
+0 255 255
+0 204 255
+0 153 255
+0 102 255
+0 51 255
 0 0 255
-0 51 0
-0 51 51
-0 51 102
-0 51 153
+0 255 204
+0 204 204
+0 153 204
+0 102 204
 0 51 204
-0 51 255
-0 102 0
-0 102 51
-0 102 102
+0 0 204
+0 255 153
+0 204 153
+0 153 153
 0 102 153
-0 102 204
-0 102 255
-0 128 0
-0 128 128
-0 153 0
-0 153 51
+0 51 153
+0 0 153
+255 255 102
+255 204 102
+255 153 102
+255 102 102
+255 51 102
+255 0 102
+255 255 51
+255 204 51
+255 153 51
+255 102 51
+255 51 51
+255 0 51
+255 255 0
+255 204 0
+255 153 0
+255 102 0
+255 51 0
+255 0 0
+204 255 102
+204 204 102
+204 153 102
+204 102 102
+204 51 102
+204 0 102
+204 255 51
+204 204 51
+204 153 51
+204 102 51
+204 51 51
+204 0 51
+204 255 0
+204 204 0
+204 153 0
+204 102 0
+204 51 0
+204 0 0
+153 255 102
+153 204 102
+153 153 102
+153 102 102
+153 51 102
+153 0 102
+153 255 51
+153 204 51
+153 153 51
+153 102 51
+153 51 51
+153 0 51
+153 255 0
+153 204 0
+153 153 0
+153 102 0
+153 51 0
+153 0 0
+102 255 102
+102 204 102
+102 153 102
+102 102 102
+102 51 102
+102 0 102
+102 255 51
+102 204 51
+102 153 51
+102 102 51
+102 51 51
+102 0 51
+102 255 0
+102 204 0
+102 153 0
+102 102 0
+102 51 0
+102 0 0
+51 255 102
+51 204 102
+51 153 102
+51 102 102
+51 51 102
+51 0 102
+51 255 51
+51 204 51
+51 153 51
+51 102 51
+51 51 51
+51 0 51
+51 255 0
+51 204 0
+51 153 0
+51 102 0
+51 51 0
+51 0 0
+0 255 102
+0 204 102
 0 153 102
-0 153 153
-0 153 204
-0 153 255
-0 204 0
+0 102 102
+0 51 102
+0 0 102
+0 255 51
 0 204 51
-0 204 102
-0 204 153
-0 204 204
-0 204 255
+0 153 51
+0 102 51
+0 51 51
+0 0 51
 0 255 0
-0 255 51
-0 255 102
-0 255 153
-0 255 204
-0 255 255
+0 204 0
+0 153 0
+0 102 0
+0 51 0
 17 17 17
 34 34 34
-51 0 0
-51 0 51
-51 0 102
-51 0 153
-51 0 204
-51 0 255
-51 51 0
-51 51 51
-51 51 102
-51 51 153
-51 51 204
-51 51 255
-51 102 0
-51 102 51
-51 102 102
-51 102 153
-51 102 204
-51 102 255
-51 153 0
-51 153 51
-51 153 102
-51 153 153
-51 153 204
-51 153 255
-51 204 0
-51 204 51
-51 204 102
-51 204 153
-51 204 204
-51 204 255
-51 255 0
-51 255 51
-51 255 102
-51 255 153
-51 255 204
-51 255 255
 68 68 68
 85 85 85
-102 0 0
-102 0 51
-102 0 102
-102 0 153
-102 0 204
-102 0 255
-102 51 0
-102 51 51
-102 51 102
-102 51 153
-102 51 204
-102 51 255
-102 102 0
-102 102 51
-102 102 102
-102 102 153
-102 102 204
-102 102 255
-102 153 0
-102 153 51
-102 153 102
-102 153 153
-102 153 204
-102 153 255
-102 204 0
-102 204 51
-102 204 102
-102 204 153
-102 204 204
-102 204 255
-102 255 0
-102 255 51
-102 255 102
-102 255 153
-102 255 204
-102 255 255
 119 119 119
-128 0 0
-128 0 128
 136 136 136
-153 0 0
-153 0 51
-153 0 102
-153 0 153
-153 0 204
-153 0 255
-153 51 0
-153 51 51
-153 51 102
-153 51 153
-153 51 204
-153 51 255
-153 102 0
-153 102 51
-153 102 102
-153 102 153
-153 102 204
-153 102 255
-153 153 0
-153 153 51
-153 153 102
-153 153 153
-153 153 204
-153 153 255
-153 204 0
-153 204 51
-153 204 102
-153 204 153
-153 204 204
-153 204 255
-153 255 0
-153 255 51
-153 255 102
-153 255 153
-153 255 204
-153 255 255
 170 170 170
 187 187 187
-192 192 192
-204 0 0
-204 0 51
-204 0 102
-204 0 153
-204 0 204
-204 0 255
-204 51 0
-204 51 51
-204 51 102
-204 51 153
-204 51 204
-204 51 255
-204 102 0
-204 102 51
-204 102 102
-204 102 153
-204 102 204
-204 102 255
-204 153 0
-204 153 51
-204 153 102
-204 153 153
-204 153 204
-204 153 255
-204 204 0
-204 204 51
-204 204 102
-204 204 153
-204 204 204
-204 204 255
-204 255 0
-204 255 51
-204 255 102
-204 255 153
-204 255 204
-204 255 255
 221 221 221
 238 238 238
-255 0 0
-255 0 51
-255 0 102
-255 0 153
-255 0 204
-255 0 255
-255 51 0
-255 51 51
-255 51 102
-255 51 153
-255 51 204
-255 51 255
-255 102 0
-255 102 51
-255 102 102
-255 102 153
-255 102 204
-255 102 255
-255 153 0
-255 153 51
-255 153 102
-255 153 153
-255 153 204
-255 153 255
-255 204 0
-255 204 51
-255 204 102
-255 204 153
-255 204 204
-255 204 255
-255 255 0
-255 255 51
-255 255 102
-255 255 153
-255 255 204
-255 255 255
+192 192 192
+128 0 0
+128 0 128
+0 128 0
+0 128 128
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
+0 0 0
diff --git a/converter/other/pnmtorast.c b/converter/other/pnmtorast.c
index 7d1ae05a..605e815c 100644
--- a/converter/other/pnmtorast.c
+++ b/converter/other/pnmtorast.c
@@ -15,15 +15,79 @@
 #include "mallocvar.h"
 
 #define MAXCOLORS 256
-static colormap_t* make_pr_colormap ARGS(( colorhist_vector chv, int colors ));
-static colormap_t* make_gray_pr_colormap ARGS(( void ));
-static colormap_t* alloc_pr_colormap ARGS(( void ));
+
+
+
+static colormap_t *
+alloc_pr_colormap(void) {
+
+    colormap_t* pr_colormapP;
+
+    MALLOCVAR(pr_colormapP);
+    if ( pr_colormapP == NULL )
+        pm_error( "out of memory" );
+    pr_colormapP->type = RMT_EQUAL_RGB;
+    pr_colormapP->length = MAXCOLORS;
+    MALLOCARRAY(pr_colormapP->map[0], MAXCOLORS);
+    MALLOCARRAY(pr_colormapP->map[1], MAXCOLORS);
+    MALLOCARRAY(pr_colormapP->map[2], MAXCOLORS);
+    if ( pr_colormapP->map[0] == NULL || 
+         pr_colormapP->map[1] == NULL ||
+         pr_colormapP->map[2] == NULL )
+        pm_error( "out of memory" );
+
+    return pr_colormapP;
+}
+
+
+
+static colormap_t*
+make_pr_colormap(colorhist_vector const chv,
+                 int              const colors) {
+
+    colormap_t* pr_colormapP;
+    int i;
+
+    pr_colormapP = alloc_pr_colormap( );
+
+    for ( i = 0; i < colors; ++i )
+    {
+        pr_colormapP->map[0][i] = PPM_GETR( chv[i].color );
+        pr_colormapP->map[1][i] = PPM_GETG( chv[i].color );
+        pr_colormapP->map[2][i] = PPM_GETB( chv[i].color );
+    }
+    for ( ; i < MAXCOLORS; ++i )
+        pr_colormapP->map[0][i] = pr_colormapP->map[1][i] =
+            pr_colormapP->map[2][i] = 0;
+
+    return pr_colormapP;
+}
+
+
+
+static colormap_t *
+make_gray_pr_colormap(void) {
+
+    colormap_t* pr_colormapP;
+    int i;
+
+    pr_colormapP = alloc_pr_colormap( );
+
+    for ( i = 0; i < MAXCOLORS; ++i )
+    {
+        pr_colormapP->map[0][i] = i;
+        pr_colormapP->map[1][i] = i;
+        pr_colormapP->map[2][i] = i;
+    }
+
+    return pr_colormapP;
+}
+
+
 
 int
-main( argc, argv )
-    int argc;
-    char* argv[];
-{
+main(int argc, char ** argv) {
+
     FILE* ifp;
     xel** xels;
     xel* xelrow;
@@ -247,64 +311,3 @@ main( argc, argv )
     exit( 0 );
 }
 
-static colormap_t*
-make_pr_colormap( chv, colors )
-    colorhist_vector chv;
-    int colors;
-{
-    colormap_t* pr_colormapP;
-    int i;
-
-    pr_colormapP = alloc_pr_colormap( );
-
-    for ( i = 0; i < colors; ++i )
-    {
-        pr_colormapP->map[0][i] = PPM_GETR( chv[i].color );
-        pr_colormapP->map[1][i] = PPM_GETG( chv[i].color );
-        pr_colormapP->map[2][i] = PPM_GETB( chv[i].color );
-    }
-    for ( ; i < MAXCOLORS; ++i )
-        pr_colormapP->map[0][i] = pr_colormapP->map[1][i] =
-            pr_colormapP->map[2][i] = 0;
-
-    return pr_colormapP;
-}
-
-static colormap_t*
-make_gray_pr_colormap( )
-{
-    colormap_t* pr_colormapP;
-    int i;
-
-    pr_colormapP = alloc_pr_colormap( );
-
-    for ( i = 0; i < MAXCOLORS; ++i )
-    {
-        pr_colormapP->map[0][i] = i;
-        pr_colormapP->map[1][i] = i;
-        pr_colormapP->map[2][i] = i;
-    }
-
-    return pr_colormapP;
-}
-
-static colormap_t*
-alloc_pr_colormap( )
-{
-    colormap_t* pr_colormapP;
-
-    MALLOCVAR(pr_colormapP);
-    if ( pr_colormapP == NULL )
-        pm_error( "out of memory" );
-    pr_colormapP->type = RMT_EQUAL_RGB;
-    pr_colormapP->length = MAXCOLORS;
-    MALLOCARRAY(pr_colormapP->map[0], MAXCOLORS);
-    MALLOCARRAY(pr_colormapP->map[1], MAXCOLORS);
-    MALLOCARRAY(pr_colormapP->map[2], MAXCOLORS);
-    if ( pr_colormapP->map[0] == NULL || 
-         pr_colormapP->map[1] == NULL ||
-         pr_colormapP->map[2] == NULL )
-        pm_error( "out of memory" );
-
-    return pr_colormapP;
-}
diff --git a/converter/other/rast.h b/converter/other/rast.h
index 1854e495..e79896ea 100644
--- a/converter/other/rast.h
+++ b/converter/other/rast.h
@@ -97,15 +97,33 @@ typedef struct {
 
 /* And the routine definitions. */
 
-struct pixrect* mem_create ARGS(( int w, int h, int depth ));
-void mem_free ARGS(( struct pixrect* p ));
+struct pixrect *
+mem_create(int const w,
+           int const h,
+           int const depth);
 
-int pr_dump ARGS(( struct pixrect* p, FILE* out, colormap_t* colormap, int type, int copy_flag ));
+void
+mem_free(struct pixrect * const p);
 
-int pr_load_header ARGS(( FILE* in, struct rasterfile* hP ));
+int
+pr_dump(struct pixrect * const p,
+        FILE *           const out,
+        colormap_t *     const colormap,
+        int              const type,
+        int              const copy_flag);
 
-int pr_load_colormap ARGS(( FILE* in, struct rasterfile* hP, colormap_t* colormap ));
+int
+pr_load_header(FILE *              const in,
+               struct rasterfile * const hP);
 
-struct pixrect* pr_load_image ARGS(( FILE* in, struct rasterfile* hP, colormap_t* colormap ));
+int
+pr_load_colormap(FILE *              const in,
+                 struct rasterfile * const hP,
+                 colormap_t *        const colormap);
+
+struct pixrect *
+pr_load_image(FILE *              const in,
+              struct rasterfile * const hP,
+              colormap_t *        const colormap);
 
 #endif
diff --git a/converter/other/tifftopnm.c b/converter/other/tifftopnm.c
index ce17b7e1..e228651a 100644
--- a/converter/other/tifftopnm.c
+++ b/converter/other/tifftopnm.c
@@ -233,8 +233,8 @@ read_directory(TIFF * const tif,
         pm_error("Input Tiff file is invalid.  It has no IMAGELENGTH tag.");
 
     if (headerdump) {
-        pm_message( "%dx%dx%d image", *cols_p, *rows_p, *bps_p * *spp_p );
-        pm_message( "%d bits/sample, %d samples/pixel", *bps_p, *spp_p );
+        pm_message( "%ux%ux%u image", *cols_p, *rows_p, *bps_p * *spp_p );
+        pm_message( "%u bits/sample, %d samples/pixel", *bps_p, *spp_p );
     }
 }
 
@@ -266,11 +266,12 @@ readscanline(TIFF *         const tif,
     /* The TIFFReadScanline man page doesn't tell the format of its
        'buf' return value, but it is exactly the same format as the 'buf'
        input to TIFFWriteScanline.  The man page for that doesn't say 
-       anything either, but the source code for Pnmtotiff contains a
+       anything either, but the source code for Pamtotiff contains a
        specification.
     */
 
     rc = TIFFReadScanline(tif, scanbuf, row, plane);
+
     if (rc < 0)
         pm_error( "Unable to read row %d, plane %d of input Tiff image.  "
                   "TIFFReadScanline() failed.",
@@ -291,7 +292,7 @@ readscanline(TIFF *         const tif,
 
         for (sample = 0, bitsleft=8, inP=scanbuf; 
              sample < cols*spp; 
-             sample++) {
+             ++sample) {
             if (bitsleft == 0) {
                 ++inP; 
                 bitsleft = 8;
@@ -322,7 +323,7 @@ readscanline(TIFF *         const tif,
            bytes of each sample appear in a TIFF file, which is
            contrary to the TIFF spec.  
         */
-        uint16 * const scanbuf16 = (uint16 *) scanbuf;
+        const uint16 * const scanbuf16 = (const uint16 *) scanbuf;
         unsigned int sample;
 
         for (sample = 0; sample < cols*spp; ++sample)
diff --git a/converter/ppm/ppmtompeg/Makefile b/converter/ppm/ppmtompeg/Makefile
index e5ad6c58..7e8f0b95 100644
--- a/converter/ppm/ppmtompeg/Makefile
+++ b/converter/ppm/ppmtompeg/Makefile
@@ -50,14 +50,20 @@ MP_ENCODE_OBJS = \
 
 MP_OTHER_OBJS = mpeg.o subsample.o param.o rgbtoycc.o \
 	readframe.o combine.o jrevdct.o frame.o fsize.o frametype.o \
-	specifics.o rate.o opts.o input.o gethostname.o
+	specifics.o rate.o opts.o input.o
 ifeq ($(OMIT_NETWORK),y)
-  MP_PARALLEL_OBJS = noparallel.o
+  MP_OTHER_OBJS += noparallel.o
 else
-  MP_PARALLEL_OBJS = parallel.o psocket.o
+  MP_OTHER_OBJS += parallel.o psocket.o
 endif
+ifeq ($(WIN32),y)
+  MP_OTHER_OBJS += gethostname_win32.o
+else
+  MP_OTHER_OBJS += gethostname.o
+endif
+
 NONMAIN_OBJS = $(MP_BASE_OBJS) $(MP_OTHER_OBJS) $(MP_ENCODE_OBJS) \
-	      $(MP_PARALLEL_OBJS) $(JPEG_MODULE).o
+	      	$(JPEG_MODULE).o
 OBJECTS = ppmtompeg.o $(NONMAIN_OBJS)
 MERGE_OBJECTS = ppmtompeg.o2 $(NONMAIN_OBJS)
 MP_INCLUDE = mproto.h mtypes.h huff.h bitio.h
diff --git a/converter/ppm/ppmtompeg/gethostname_win32.c b/converter/ppm/ppmtompeg/gethostname_win32.c
new file mode 100644
index 00000000..56a8dbfc
--- /dev/null
+++ b/converter/ppm/ppmtompeg/gethostname_win32.c
@@ -0,0 +1,402 @@
+// define this macro for activating debugging version
+//#define GETHOSTNAME_LOCAL_DEBUG     1
+
+#include <windows.h>
+#include <tchar.h>
+#include <stdarg.h>
+
+#ifndef GETHOSTNAME_LOCAL_DEBUG
+#include "pm.h"
+#include "gethostname.h"
+#endif
+
+#define BUFSIZE 80
+
+typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
+typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
+
+typedef struct {
+    char  str[256];
+    int   level;
+} push_string_t;
+
+static void
+pushString(push_string_t *p, char *fmt, ...)
+{
+    va_list args;
+
+    va_start(args, fmt);
+    p->level += _vsnprintf(p->str + p->level, sizeof(p->str)-p->level, fmt, args);
+    va_end(args);
+}
+
+#if _WIN32_WINNT < 0x0600
+/*
+ * Reference available here:
+ *
+ * GetProductInfo() Function
+ * http://msdn2.microsoft.com/en-us/library/ms724358.aspx
+ */
+#define PRODUCT_BUSINESS                        0x00000006  // Business Edition
+#define PRODUCT_BUSINESS_N                      0x00000010  // Business Edition
+#define PRODUCT_CLUSTER_SERVER                  0x00000012  // Cluster Server Edition
+#define PRODUCT_DATACENTER_SERVER               0x00000008  // Server Datacenter Edition (full installation)
+#define PRODUCT_DATACENTER_SERVER_CORE          0x0000000C  // Server Datacenter Edition (core installation)
+#define PRODUCT_ENTERPRISE                      0x00000004  // Enterprise Edition
+#define PRODUCT_ENTERPRISE_N                    0x0000001B  // Enterprise Edition
+#define PRODUCT_ENTERPRISE_SERVER               0x0000000A  // Server Enterprise Edition (full installation)
+#define PRODUCT_ENTERPRISE_SERVER_CORE          0x0000000E  // Server Enterprise Edition (core installation)
+#define PRODUCT_ENTERPRISE_SERVER_IA64          0x0000000F  // Server Enterprise Edition for Itanium-based Systems
+#define PRODUCT_HOME_BASIC                      0x00000002  // Home Basic Edition
+#define PRODUCT_HOME_BASIC_N                    0x00000005  // Home Basic Edition
+#define PRODUCT_HOME_PREMIUM                    0x00000003  // Home Premium Edition
+#define PRODUCT_HOME_PREMIUM_N                  0x0000001A  // Home Premium Edition
+#define PRODUCT_HOME_SERVER                     0x00000013  // Home Server Edition
+#define PRODUCT_SERVER_FOR_SMALLBUSINESS        0x00000018  // Server for Small Business Edition
+#define PRODUCT_SMALLBUSINESS_SERVER            0x00000009  // Small Business Server
+#define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM    0x00000019  // Small Business Server Premium Edition
+#define PRODUCT_STANDARD_SERVER                 0x00000007  // Server Standard Edition (full installation)
+#define PRODUCT_STANDARD_SERVER_CORE            0x0000000D  // Server Standard Edition (core installation)
+#define PRODUCT_STARTER                         0x0000000B  // Starter Edition
+#define PRODUCT_STORAGE_ENTERPRISE_SERVER       0x00000017  // Storage Server Enterprise Edition
+#define PRODUCT_STORAGE_EXPRESS_SERVER          0x00000014  // Storage Server Express Edition
+#define PRODUCT_STORAGE_STANDARD_SERVER         0x00000015  // Storage Server Standard Edition
+#define PRODUCT_STORAGE_WORKGROUP_SERVER        0x00000016  // Storage Server Workgroup Edition
+#define PRODUCT_UNDEFINED                       0x00000000  // An unknown product
+#define PRODUCT_ULTIMATE                        0x00000001  // Ultimate Edition
+#define PRODUCT_ULTIMATE_N                      0x0000001C  // Ultimate Edition
+#define PRODUCT_WEB_SERVER                      0x00000011  // Web Server Edition (full installation)
+#define PRODUCT_WEB_SERVER_CORE                 0x0000001D  // Web Server Edition (core installation)
+#endif
+
+static BOOL
+get_string_version(push_string_t *str)
+{
+    OSVERSIONINFOEX osvi;
+    SYSTEM_INFO si;
+    PGPI pGPI;
+    PGNSI pGNSI;
+    BOOL bOsVersionInfoEx;
+    DWORD dwType;
+
+    ZeroMemory(&si, sizeof(SYSTEM_INFO));
+    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
+
+    // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
+    // If that fails, try using the OSVERSIONINFO structure.
+    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+    if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
+    {
+        osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
+        if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
+            return FALSE;
+    }
+
+    // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
+    pGNSI = (PGNSI)
+            GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), 
+            "GetNativeSystemInfo");
+    if (NULL != pGNSI)
+        pGNSI(&si);
+    else
+        GetSystemInfo(&si);
+
+    switch (osvi.dwPlatformId)
+    {
+    // Test for the Windows NT product family.
+    case VER_PLATFORM_WIN32_NT:
+        // Test for the specific product.
+        if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0)
+        {
+            if (osvi.wProductType == VER_NT_WORKSTATION)
+                pushString(str, "Windows Vista ");
+            else
+                pushString(str, "Windows Server 2008 ");
+
+            pGPI = (PGPI) GetProcAddress(
+                GetModuleHandle(TEXT("kernel32.dll")),
+                "GetProductInfo");
+
+            pGPI( 6, 0, 0, 0, &dwType);
+            switch (dwType)
+            {
+            case PRODUCT_ULTIMATE:
+                pushString(str, "Ultimate Edition");
+                break;
+            case PRODUCT_HOME_PREMIUM:
+                pushString(str, "Home Premium Edition");
+                break;
+            case PRODUCT_HOME_BASIC:
+                pushString(str, "Home Basic Edition");
+                break;
+            case PRODUCT_ENTERPRISE:
+                pushString(str, "Enterprise Edition");
+                break;
+            case PRODUCT_BUSINESS:
+                pushString(str, "Business Edition");
+                break;
+            case PRODUCT_STARTER:
+                pushString(str, "Starter Edition");
+                break;
+            case PRODUCT_CLUSTER_SERVER:
+                pushString(str, "Cluster Server Edition");
+                break;
+            case PRODUCT_DATACENTER_SERVER:
+                pushString(str, "Datacenter Edition");
+                break;
+            case PRODUCT_DATACENTER_SERVER_CORE:
+                pushString(str, "Datacenter Edition (core installation)");
+                break;
+            case PRODUCT_ENTERPRISE_SERVER:
+                pushString(str, "Enterprise Edition");
+                break;
+            case PRODUCT_ENTERPRISE_SERVER_CORE:
+                pushString(str, "Enterprise Edition (core installation)");
+                break;
+            case PRODUCT_ENTERPRISE_SERVER_IA64:
+                pushString(str, "Enterprise Edition for Itanium-based Systems");
+                break;
+            case PRODUCT_SMALLBUSINESS_SERVER:
+                pushString(str, "Small Business Server");
+                break;
+            case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
+                pushString(str, "Small Business Server Premium Edition");
+                break;
+            case PRODUCT_STANDARD_SERVER:
+                pushString(str, "Standard Edition");
+                break;
+            case PRODUCT_STANDARD_SERVER_CORE:
+                pushString(str, "Standard Edition (core installation)");
+                break;
+            case PRODUCT_WEB_SERVER:
+                pushString(str, "Web Server Edition");
+                break;
+            }
+            if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
+                pushString(str,  ", 64-bit");
+            else
+            if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL)
+                pushString(str, ", 32-bit");
+            else
+                /* space for optional build number */
+                pushString(str, " ");
+        }
+        else
+        if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
+        {
+            if( GetSystemMetrics(SM_SERVERR2) )
+                pushString(str, "Microsoft Windows Server 2003 \"R2\" ");
+            else
+            if ( osvi.wSuiteMask==VER_SUITE_STORAGE_SERVER )
+                pushString(str, "Windows Storage Server 2003 ");
+            else
+            if( osvi.wProductType == VER_NT_WORKSTATION &&
+                si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
+                pushString(str, "Microsoft Windows XP Professional x64 Edition ");
+            else
+                pushString(str, "Microsoft Windows Server 2003, ");
+
+            // Test for the server type.
+            if ( osvi.wProductType != VER_NT_WORKSTATION )
+            {
+                switch (si.wProcessorArchitecture)
+                {
+                case PROCESSOR_ARCHITECTURE_IA64:
+                    if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
+                       pushString(str, "Datacenter Edition for Itanium-based Systems ");
+                    else
+                    if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
+                       pushString(str, "Enterprise Edition for Itanium-based Systems ");
+                    break;
+
+                case PROCESSOR_ARCHITECTURE_AMD64:
+                    if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
+                        pushString(str, "Datacenter x64 Edition ");
+                    else
+                    if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
+                        pushString(str, "Enterprise x64 Edition ");
+                    else
+                        pushString(str, "Standard x64 Edition ");
+                    break;
+
+                default:
+                    if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
+                        pushString(str, "Compute Cluster Edition ");
+                    else
+                    if ( osvi.wSuiteMask & VER_SUITE_DATACENTER)
+                        pushString(str, "Datacenter Edition ");
+                    else
+                    if ( osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
+                        pushString(str, "Enterprise Edition ");
+                    else
+                    if ( osvi.wSuiteMask & VER_SUITE_BLADE)
+                        pushString(str, "Web Edition ");
+                    else
+                        pushString(str, "Standard Edition ");
+                    break;
+                }
+            }
+        }
+        else
+        if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
+        {
+            pushString(str, "Microsoft Windows XP ");
+            if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
+                pushString(str, "Home Edition ");
+            else
+                pushString(str, "Professional ");
+        }
+        else
+        if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
+        {
+            pushString(str, "Microsoft Windows 2000 ");
+            if (osvi.wProductType == VER_NT_WORKSTATION)
+                pushString(str, "Professional ");
+            else 
+            if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
+                pushString(str, "Datacenter Server ");
+            else
+            if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
+                pushString(str, "Advanced Server ");
+            else
+                pushString(str, "Server ");
+        } else
+        if ( osvi.dwMajorVersion <= 4 )
+            pushString(str, "Microsoft Windows NT ");
+
+        // Test for specific product on Windows NT 4.0 SP6 and later.
+        if (bOsVersionInfoEx)
+        {
+            // Test for the workstation type.
+            switch (osvi.wProductType)
+            {
+            case VER_NT_WORKSTATION:
+                if (si.wProcessorArchitecture!=PROCESSOR_ARCHITECTURE_AMD64 &&
+                    osvi.dwMajorVersion == 4)
+                    pushString(str, "Workstation 4.0 ");
+                break;
+
+            case VER_NT_SERVER:
+            case VER_NT_DOMAIN_CONTROLLER:
+                if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
+                    pushString(str, "Server 4.0, Enterprise Edition ");
+                else
+                    pushString(str, "Server 4.0 ");
+                break;
+            }
+        }
+        // Test for specific product on Windows NT 4.0 SP5 and earlier
+        else  
+        {
+            HKEY hKey;
+            TCHAR szProductType[BUFSIZE];
+            DWORD dwBufLen=BUFSIZE*sizeof(TCHAR);
+            LONG lRet;
+
+            lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+                TEXT("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),
+                0, KEY_QUERY_VALUE, &hKey);
+            if (lRet != ERROR_SUCCESS)
+                return FALSE;
+
+            lRet = RegQueryValueEx(hKey, TEXT("ProductType"), NULL, NULL,
+                (LPBYTE) szProductType, &dwBufLen);
+            RegCloseKey( hKey );
+
+            if ((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE*sizeof(TCHAR)))
+                return FALSE;
+
+            if (lstrcmpi(TEXT("WINNT"), szProductType) == 0)
+                pushString(str, "Workstation ");
+            else
+            if (lstrcmpi(TEXT("LANMANNT"), szProductType) == 0)
+                pushString(str, "Server ");
+            else
+            if (lstrcmpi( TEXT("SERVERNT"), szProductType) == 0)
+                pushString(str, "Advanced Server ");
+            else
+                pushString(str, "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion);
+        }
+
+        // Display service pack (if any) and build number.
+        if (osvi.dwMajorVersion == 4 && 
+            lstrcmpi(osvi.szCSDVersion, TEXT("Service Pack 6")) == 0)
+        { 
+            HKEY hKey;
+            LONG lRet;
+
+            // Test for SP6 versus SP6a.
+            lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
+                TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"),
+                0, KEY_QUERY_VALUE, &hKey );
+            if( lRet == ERROR_SUCCESS )
+                pushString(str, "Service Pack 6a (Build %d)\n", osvi.dwBuildNumber & 0xFFFF );         
+            else
+                // Windows NT 4.0 prior to SP6a
+                pushString(str, "%s (Build %d)\n", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
+
+             RegCloseKey( hKey );
+        }
+        else // not Windows NT 4.0 
+            pushString(str, "%s (Build %d)\n", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
+
+        break;
+
+    // Test for the Windows Me/98/95.
+    case VER_PLATFORM_WIN32_WINDOWS:
+        if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
+        {
+            pushString(str, "Microsoft Windows 95");
+            if (osvi.szCSDVersion[1]=='C' || osvi.szCSDVersion[1]=='B')
+                pushString(str, " OSR2");
+        }
+        else
+        if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
+        {
+            pushString(str, "Microsoft Windows 98");
+            if (osvi.szCSDVersion[1]=='A' || osvi.szCSDVersion[1]=='B')
+                pushString(str, " SE");
+        } 
+        else
+        if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
+            pushString(str, "Microsoft Windows Millennium Edition\n");
+        break;
+
+    case VER_PLATFORM_WIN32s:
+        pushString(str, "Microsoft Win32s\n");
+        break;
+    }
+    return TRUE; 
+}
+
+const char *
+GetHostName(void)
+{
+/*----------------------------------------------------------------------------
+   Return the host name of this system.
+-----------------------------------------------------------------------------*/
+    push_string_t str;
+
+    ZeroMemory(&str, sizeof(str));
+    if (!get_string_version(&str)) {
+#ifndef GETHOSTNAME_LOCAL_DEBUG
+        pm_error("Unable to find out host name.");
+#endif
+        pushString(&str, "unknown");
+    }
+    return (const char *)_strdup(str.str);
+}
+
+#ifdef GETHOSTNAME_LOCAL_DEBUG
+int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst,
+                   LPSTR lpsCmdLine, int nCmdShow)
+{
+    char *hostName = (char *)GetHostName();
+
+    /* compile as ascii only (not UNICODE) */
+    MessageBox(NULL, hostName, TEXT("GetHostName"), MB_OK);
+    free(hostName);
+
+    return 0;
+}
+#endif
diff --git a/converter/ppm/ppmtompeg/specifics.c b/converter/ppm/ppmtompeg/specifics.c
index c7bbfb5b..38e8fc43 100644
--- a/converter/ppm/ppmtompeg/specifics.c
+++ b/converter/ppm/ppmtompeg/specifics.c
@@ -47,12 +47,6 @@
 #include <string.h>
 #include "prototypes.h"
 
-/*====================*
- * System Information *
- *====================*/
-
-#define CPP_LOC "/lib/cpp"
-
 /*==================*
  * GLOBAL VARIABLES *
  *==================*/
@@ -153,26 +147,27 @@ static char version = -1;
  *
  *================================================================
  */
-void Specifics_Init()
-{
-  char command[1100];
-  FILE *specificsFP;
-  
-  sprintf(command, "/bin/rm -f %s.cpp", specificsFile);
-  system(command);
-  sprintf(command, "%s -P %s %s %s.cpp",
-	  CPP_LOC, specificsDefines, specificsFile, specificsFile);
-  system(command);
-  strcat(specificsFile, ".cpp");
-  if ((specificsFP = fopen(specificsFile, "r")) == NULL) {
-    fprintf(stderr, "Error with specifics file, cannot open %s\n", specificsFile);
-    exit(1);
-  }
-  printf("Specifics file: %s\n", specificsFile);
-  Parse_Specifics_File(specificsFP);
-  sprintf(command, "/bin/rm -f %s.cpp", specificsFile);
-  system(command);
+void
+Specifics_Init() {
 
+    char command[1100];
+    FILE *specificsFP;
+  
+    sprintf(command, "rm -f %s.cpp", specificsFile);
+    system(command);
+    sprintf(command, "cpp -P %s %s %s.cpp",
+            specificsDefines, specificsFile, specificsFile);
+    system(command);
+    strcat(specificsFile, ".cpp");
+    if ((specificsFP = fopen(specificsFile, "r")) == NULL) {
+        fprintf(stderr, "Error with specifics file, cannot open %s\n",
+                specificsFile);
+        exit(1);
+    }
+    printf("Specifics file: %s\n", specificsFile);
+    Parse_Specifics_File(specificsFP);
+    sprintf(command, "rm -f %s.cpp", specificsFile);
+    system(command);
 }
 
 
diff --git a/doc/HISTORY b/doc/HISTORY
index 62ec9c70..e38c7b71 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -4,23 +4,20 @@ Netpbm.
 CHANGE HISTORY 
 --------------
 
-07.12.17 BJH  Release 10.40.06
+07.12.27 BJH  Release 10.41.00
 
-              pamperspective: Eliminate build-time symbol conflict
-              with 'nearest' on AIX.
+              pamenlarge: much faster for PBM.  Thanks Prophet of the
+              Way <afu@wta.att.ne.jp>.
 
-07.12.14 BJH  Release 10.40.05
+              pamenlarge: remove arithmetic overflow.
 
-              set _XOPEN_SOURCE=600 so jpeg2ktopam and pamtojpeg2k
-              compile on AIX (_XOPEN_SOURCE=500 keeps int_fast32_t from
-              being defined).
-
-07.12.11 BJH  Release 10.40.04
+              pnmmontage: new search algorithm to choose square
+              output quickly.  Thanks "Nic Roets" <nroets@gmail.com>.
 
-              pamtotiff: fix incorrect pixels with PNM maxval != TIFF
-              maxval.
+              bmptopnm: fix crash with 16 bit images.
 
-07.11.26 BJH  Release 10.40.03
+              pamlcolor8.map: Make it a 256 color map sorted by index,
+              per Palm's spec.  Thanks Paul Bolle <pebolle@tiscali.nl>.
 
               xwdtopnm: deal with too-large bits per pixel value in XWD 
               header.
@@ -28,14 +25,34 @@ CHANGE HISTORY
               pamditherbw, pamsharpness, pamsharpmask, pamtopfm:
               fix crash due to write to arbitrary memory.
 
-07.11.13 BJH  Release 10.40.02
+              pamtotiff: fix incorrect pixels with PNM maxval != TIFF
+              maxval.
 
               pnmcrop: fix -verbose message about background color with
               -white.
 
-07.10.12 BJH  Release 10.40.01
+              pbmmake: handle ridiculously large height, width arguments.
 
-              bmptopnm: fix crash with 16 bit images.
+              pnmcat: fix arithmetic overflow.
+
+              libnetpbm: Add arithmetic overflow protection to PBM
+              routines, like PGM/PPM/PNM have had for a long time.
+
+              libnetpbm: make all row free operations go through
+              pm_freerow(); change row buffer type from char * to void *
+              for pm_allocrow(), pm_freerow().
+
+              set _XOPEN_SOURCE=600 so jpeg2ktopam and pamtojpeg2k
+              compile on AIX (_XOPEN_SOURCE=500 keeps int_fast32_t from
+              being defined).
+
+              pamperspective: Eliminate build-time symbol conflict
+              with 'nearest' on AIX.
+
+              Fix bug: ppmdraw.h, ppmdfont.h not installed.
+
+              package, install: install interface header files as
+              <netpbm/xxx.h> instead of just <xxx.h>.
 
 07.09.26 BJH  Release 10.40.00
 
@@ -572,7 +589,7 @@ CHANGE HISTORY
               for superspeed operations.  Thanks
               Prophet of the Way <afu@wta.att.ne.jp>.
 
-              pm_make_tempfile(): Use TEMP and TMP environment variables if
+              pm_make_tmpfile(): Use TEMP and TMP environment variables if
               TMPDIR not set.
 
               pm_make_tempfile(): improve error message.
@@ -1133,6 +1150,8 @@ CHANGE HISTORY
               ppmforge: fix bug: crash due to wild pointer with -night.
               Thanks John Walker <kelvin@fourmilab.ch>.
 
+              libnetpbm: add pm_make_tmpfile().
+
 05.01.01 BJH  Release 10.26
 
               pnmhistmap: Add -dots, -nmax, -red, -green, -blue, -lval,
@@ -1228,7 +1247,9 @@ CHANGE HISTORY
               pamcut: major speedup.  Thanks Prophet of the Way
               <afu@wta.att.ne.jp> (Akira Urushibata ("Douso")).
 
-              Add pnm_getopacity().
+              libnetpbm: Add pnm_getopacity().
+
+              libnetpbm: Add pnm_applyopacityrown(), pnm_unapplyopacityrown().
 
               libnetpbm: "pam" read and write routines much more
               efficient.  Thanks Prophet of the Way
@@ -1567,6 +1588,8 @@ CHANGE HISTORY
               Stefan Nordhausen <nordhaus@informatik.hu-berlin.de>.
               
               tifftopnm: Do better validation of number of channels.
+
+              libnetpbm: Add pm_tmpfile().
               
               libnetpbm: Add "normalized" (floating point) read/write
               routines:  pnm_readpamrown(), pnm_writepamrown(), 
diff --git a/editor/pamenlarge.c b/editor/pamenlarge.c
index 15b91b4f..b3039424 100644
--- a/editor/pamenlarge.c
+++ b/editor/pamenlarge.c
@@ -5,8 +5,10 @@
   author.
 =============================================================================*/
 
-#include "pam.h"
-#include "mallocvar.h"
+#include "netpbm/mallocvar.h"
+#include "netpbm/pm_c_util.h"
+#include "netpbm/pam.h"
+#include "netpbm/pbm.h"
 
 struct cmdlineInfo {
     /* All the information the user supplied in the command line,
@@ -19,7 +21,8 @@ struct cmdlineInfo {
 
 
 static void
-parseCommandLine(int argc, char ** const argv,
+parseCommandLine(int                  const argc,
+                 const char **        const argv,
                  struct cmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    Note that the file spec array we return is stored in the storage that
@@ -69,45 +72,231 @@ makeOutputRowMap(tuple **     const outTupleRowP,
 
 
 
-int
-main(int    argc, 
-     char * argv[]) {
+static void
+validateComputableDimensions(unsigned int const width,
+                             unsigned int const height,
+                             unsigned int const scaleFactor) {
+/*----------------------------------------------------------------------------
+   Make sure that multiplication for output image width and height do not
+   overflow.
+   See validateComputetableSize() in libpam.c
+   and pbm_readpbminitrest() in libpbm2.c
+-----------------------------------------------------------------------------*/
+    unsigned int const maxWidthHeight = INT_MAX - 2;
+    unsigned int const maxScaleFactor = maxWidthHeight / MAX(height, width);
 
-    struct cmdlineInfo cmdline;
-    FILE * ifP;
-    struct pam inpam;
-    struct pam outpam; 
-    tuple * tuplerow;
-    tuple * newtuplerow;
-    int row;
+    if (scaleFactor > maxScaleFactor)
+       pm_error("Scale factor '%u' too large.  "
+                "The maximum for this %u x %u input image is %u.",
+                scaleFactor, width, height, maxScaleFactor);
+}
 
-    pnm_init(&argc, argv);
 
-    parseCommandLine(argc, argv, &cmdline);
 
-    ifP = pm_openr(cmdline.inputFilespec);
- 
-    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
+static void
+enlargePbmRowHorizontally(struct pam *          const inpamP,
+                          const unsigned char * const inrow,
+                          unsigned int          const inColChars,
+                          unsigned int          const outColChars,
+                          unsigned int          const scaleFactor,
+                          unsigned char *       const outrow) {
+
+    static unsigned char const dbl[16] = { 
+        0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33, 0x3C, 0x3F, 
+        0xC0, 0xC3, 0xCC, 0xCF, 0xF0, 0xF3, 0xFC, 0xFF };
+
+    static unsigned char const trp1[8] = { 
+        0x00, 0x03, 0x1C, 0x1F, 0xE0, 0xE3, 0xFC, 0xFF };
+        
+    static unsigned char const trp2[16] = { 
+        0x00, 0x01, 0x0E, 0x0F, 0x70, 0x71, 0x7E, 0x7F,
+        0x80, 0x81, 0x8E, 0x8F, 0xF0, 0xF1, 0xFE, 0xFF };
+
+    static unsigned char const trp3[8] = { 
+        0x00, 0x07, 0x38, 0x3F, 0xC0, 0xC7, 0xF8, 0xFF };
+
+    static unsigned char const quin2[8] = {
+        0x00, 0x01, 0x3E, 0x3F, 0xC0, 0xC1, 0xFE, 0xFF };
+
+    static unsigned char const quin4[8] = {
+        0x00, 0x03, 0x7C, 0x7F, 0x80, 0x83, 0xFC, 0xFF };
+
+    static unsigned int const pair[4] = { 0x0000, 0x00FF, 0xFF00, 0xFFFF };
+
+    unsigned int colChar;
+
+    switch (scaleFactor) {
+    case 1:  break; /* outrow set to inrow */
+    case 2:  /* Make outrow using prefabricated parts (same for 3, 5). */ 
+        for (colChar = 0; colChar < inColChars; ++colChar) { 
+            outrow[colChar*2]  = dbl[(inrow[colChar] & 0xF0) >> 4];
+            outrow[colChar*2+1]= dbl[inrow[colChar] & 0x0F];
+            /* Possible outrow overrun by one byte. */
+        }
+        break;   
+    case 3:
+        for (colChar = 0; colChar < inColChars; ++colChar) { 
+            outrow[colChar*3]  = trp1[(inrow[colChar] & 0xF0) >> 5];
+            outrow[colChar*3+1]= trp2[(inrow[colChar] >> 2) & 0x0F];
+            outrow[colChar*3+2]= trp3[inrow[colChar] & 0x07];
+        }
+        break;  
+    case 5:
+        for (colChar = 0; colChar < inColChars; ++colChar) { 
+            outrow[colChar*5]  = pair[ (inrow[colChar] >>6) & 0x03 ] >> 5; 
+            outrow[colChar*5+1]= quin2[(inrow[colChar] >>4) & 0x07 ]; 
+            outrow[colChar*5+2]= pair[ (inrow[colChar] >>3) & 0x03 ] >> 4; 
+            outrow[colChar*5+3]= quin4[(inrow[colChar] >>1) & 0x07 ];
+            outrow[colChar*5+4]= pair[ inrow[colChar] & 0x03 ] >>3; 
+        }
+        break;
+    case 4: default:
+        /*  Unlike the above cases, we iterate through outrow.  The color
+            composition of each outrow byte is computed by consulting
+            a single bit or two consecutive bits in inrow. 
+            Color changes never happen twice in a single outrow byte.
+        */
+        for (colChar = 0; colChar < outColChars; ++colChar) {
+            unsigned int const mult = scaleFactor;
+            unsigned int const mod = colChar % mult;
+            unsigned int const bit = (mod*8)/mult;
+            /* source bit position, leftmost=0 */
+            unsigned int const offset = mult - (mod*8)%mult;
+            /* number of outrow bits derived from the same
+               "source" inrow bit, starting at and to the right
+               of leftmost bit of outrow byte, inclusive
+            */
+
+            if (offset >= 8)  /* Bits in outrow byte are all 1 or 0 */
+                outrow[colChar] =
+                    (inrow[colChar/mult] >> (7-bit) & 0x01) * 0xFF;
+            else           /* Two inrow bits influence this outrow byte */ 
+                outrow[colChar] = (unsigned char)
+                    (pair[inrow[colChar/mult] >> (6-bit) & 0x03] >> offset)
+                    & 0xFF;
+        }
+    }
+}
+
+
+
+static void
+enlargePbm(struct pam * const inpamP,
+           unsigned int const scaleFactor,
+           FILE *       const ofP) {
+
+    unsigned char * inrow;
+    unsigned char * outrow;
+
+    unsigned int row;
+
+    unsigned int const outcols = inpamP->width * scaleFactor;
+    unsigned int const outrows = inpamP->height * scaleFactor;
+    unsigned int const inColChars  = pbm_packed_bytes(inpamP->width);
+    unsigned int const outColChars = pbm_packed_bytes(outcols);
+
+    inrow  = pbm_allocrow_packed(inpamP->width);
+    
+    if (scaleFactor == 1)
+        outrow = inrow;
+    else  
+        outrow = pbm_allocrow_packed(outcols + 32);
+            /* The 32 (=4 bytes) is to allow writes beyond outrow data
+               end when scaleFactor is 2, 3, 5.  (max 4 bytes when
+               scaleFactor is 5)
+            */
 
-    outpam = inpam; 
-    outpam.file   = stdout;
-    outpam.width  = inpam.width * cmdline.scaleFactor;
-    outpam.height = inpam.height * cmdline.scaleFactor; 
+    pbm_writepbminit(ofP, outcols, outrows, 0);
+    
+    for (row = 0; row < inpamP->height; ++row) {
+        unsigned int i;
+
+        pbm_readpbmrow_packed(inpamP->file, inrow, inpamP->width,
+                              inpamP->format);
+
+        if (inpamP->width % 8 > 0) {  /* clean final partial byte */ 
+            inrow[inColChars-1] >>= 8 - inpamP->width % 8;
+            inrow[inColChars-1] <<= 8 - inpamP->width % 8;
+        }
+
+        enlargePbmRowHorizontally(inpamP, inrow, inColChars, outColChars,
+                                  scaleFactor, outrow);
+
+        for (i = 0; i < scaleFactor; ++i)  
+            pbm_writepbmrow_packed(ofP, outrow, outcols, 0);
+    }
+    
+    if (outrow != inrow)
+        pbm_freerow(outrow);
+
+    pbm_freerow(inrow);
+}
+
+
+
+static void
+enlargeGeneral(struct pam * const inpamP,
+               unsigned int const scaleFactor,
+               FILE *       const ofP) {
+/*----------------------------------------------------------------------------
+   Enlarge the input image described by *pamP.
+
+   Assume the dimensions won't cause an arithmetic overflow.
+
+   This works on all kinds of images, but is slower than enlargePbm on
+   PBM.
+-----------------------------------------------------------------------------*/
+    struct pam outpam; 
+    tuple * tuplerow;
+    tuple * newtuplerow;
+    unsigned int row;
+
+    outpam = *inpamP; 
+    outpam.file   = ofP;
+    outpam.width  = inpamP->width  * scaleFactor;
+    outpam.height = inpamP->height * scaleFactor; 
 
     pnm_writepaminit(&outpam);
 
-    tuplerow = pnm_allocpamrow(&inpam);
+    tuplerow = pnm_allocpamrow(inpamP);
 
-    makeOutputRowMap(&newtuplerow, &outpam, &inpam, tuplerow);
+    makeOutputRowMap(&newtuplerow, &outpam, inpamP, tuplerow);
 
-    for (row = 0; row < inpam.height; ++row) {
-        pnm_readpamrow(&inpam, tuplerow);
-        pnm_writepamrowmult(&outpam, newtuplerow, cmdline.scaleFactor);
+    for (row = 0; row < inpamP->height; ++row) {
+        pnm_readpamrow(inpamP, tuplerow);
+        pnm_writepamrowmult(&outpam, newtuplerow, scaleFactor);
     }
 
     free(newtuplerow);
 
     pnm_freepamrow(tuplerow);
+}
+
+
+
+int
+main(int           argc, 
+     const char ** argv) {
+
+    struct cmdlineInfo cmdline;
+    FILE * ifP;
+    struct pam inpam;
+
+    pm_proginit(&argc, argv);
+
+    parseCommandLine(argc, argv, &cmdline);
+
+    ifP = pm_openr(cmdline.inputFilespec);
+ 
+    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
+    
+    validateComputableDimensions(inpam.width, inpam.height,
+                                 cmdline.scaleFactor); 
+    
+    if (PNM_FORMAT_TYPE(inpam.format) == PBM_TYPE)
+        enlargePbm(&inpam, cmdline.scaleFactor, stdout);
+    else
+        enlargeGeneral(&inpam, cmdline.scaleFactor, stdout);
 
     pm_close(ifP);
     pm_close(stdout);
diff --git a/editor/pamflip.c b/editor/pamflip.c
index 0c2aabb4..94e6056c 100644
--- a/editor/pamflip.c
+++ b/editor/pamflip.c
@@ -25,7 +25,7 @@
    functions apply, it is capable of handling all cases.  (Only that it
    is slow, and uses more memory.)  In the same manner, transformPbm() is
    capable of handling all pbm transformations and transformRowByRowNonPbm()
-   transformRowsBottomTomNonPbm() are capable of handling pbm.
+   transformRowsBottomTopNonPbm() are capable of handling pbm.
 
 
    There is some fancy virtual memory management in transformGen() to avoid
diff --git a/editor/pamscale.c b/editor/pamscale.c
index 229fce42..ad7c319b 100644
--- a/editor/pamscale.c
+++ b/editor/pamscale.c
@@ -2131,7 +2131,7 @@ main(int argc, char **argv ) {
         scaleWithoutMixing(&inpam, &outpam, xscale, yscale);
     } else if (!cmdline.filterFunction) {
         if (cmdline.verbose)
-            pm_message("Using regular rescaling method");
+            pm_message("Using simple pixel mixing rescaling method");
         scaleWithMixing(&inpam, &outpam, xscale, yscale, 
                         cmdline.linear, cmdline.verbose);
     } else {
diff --git a/editor/pnmcat.c b/editor/pnmcat.c
index 20dbf34d..cc86520f 100644
--- a/editor/pnmcat.c
+++ b/editor/pnmcat.c
@@ -155,7 +155,7 @@ computeOutputParms(unsigned int     const nfiles,
                    xelval *         const newmaxvalP,
                    int *            const newformatP) {
 
-    int newcols, newrows;
+    double newcols, newrows;
     int newformat;
     xelval newmaxval;
 
@@ -187,8 +187,18 @@ computeOutputParms(unsigned int     const nfiles,
             break;
 	    }
 	}
-    *newrowsP   = newrows;
-    *newcolsP   = newcols;
+
+    /* Note that while 'double' is not in general a precise numerical type,
+       in the case of a sum of integers which is less than INT_MAX, it
+       is exact, because double's precision is greater than int's.
+    */
+    if (newcols > INT_MAX)
+       pm_error("Output width too large: %.0f.", newcols);
+    if (newrows > INT_MAX)
+       pm_error("Output height too large: %.0f.", newrows);
+	
+    *newrowsP   = (int) newrows;
+    *newcolsP   = (int) newcols;
     *newmaxvalP = newmaxval;
     *newformatP = newformat;
 }
diff --git a/editor/pnmmontage.c b/editor/pnmmontage.c
index 9eb2d7be..7fe0e3b7 100644
--- a/editor/pnmmontage.c
+++ b/editor/pnmmontage.c
@@ -10,18 +10,20 @@
  * implied warranty.
  */
 
+#include <assert.h>
 #include <limits.h>
 #include <string.h>
 
-#include "pam.h"
-#include "shhopt.h"
-#include "nstring.h"
+#include "pm_c_util.h"
 #include "mallocvar.h"
+#include "nstring.h"
+#include "shhopt.h"
+#include "pam.h"
 
 typedef struct { int f[sizeof(int) * 8 + 1]; } factorset;
 typedef struct { int x; int y; } coord;
 
-static int qfactor = 200;
+static int qfactor;
 static int quality = 5;
 
 static factorset 
@@ -63,62 +65,79 @@ gcd(int n, int m)
   return (g);
 }
 
-static __inline__ int imax(int n, int m) { return (n > m ? n : m); }
 
-static int 
-checkcollision(coord *locs, coord *szs, coord *cloc, coord *csz, int n)
-{
-  int i;
-  for (i = 0; i < n; ++i)
-  {
-    if ((locs[i].x < cloc->x + csz->x) &&
-        (locs[i].y < cloc->y + csz->y) &&
-        (locs[i].x + szs[i].x > cloc->x) &&
-        (locs[i].y + szs[i].y > cloc->y))
-      return (1);
-  }
-  return (0);
+
+static bool
+collides(const coord * const locs,
+         const coord * const szs,
+         const coord * const cloc,
+         const coord * const csz,
+         unsigned int  const n) {
+
+    unsigned int i;
+
+    for (i = 0; i < n; ++i) {
+        if ((locs[i].x < cloc->x + csz->x) &&
+            (locs[i].y < cloc->y + csz->y) &&
+            (locs[i].x + szs[i].x > cloc->x) &&
+            (locs[i].y + szs[i].y > cloc->y))
+            return true;
+    }
+    return false;
 }
 
+
+
 static void 
-recursefindpack(coord *current, coord currentsz, coord *set, 
-                coord *best, int minarea, int *maxarea, 
-                int depth, int n, int xinc, int yinc)
-{
-  coord c;
-  if (depth == n)
-  {
-    if (currentsz.x * currentsz.y < *maxarea)
-    {
-      memcpy(best, current, sizeof(coord) * n);
-      *maxarea = currentsz.x * currentsz.y;
-    }
-    return;
-  }
+recursefindpack(coord *        const current,
+                coord          const currentsz,
+                coord *        const set, 
+                coord *        const best,
+                unsigned int   const minarea,
+                unsigned int * const maxareaP, 
+                unsigned int   const depth,
+                unsigned int   const n,
+                unsigned int   const xinc,
+                unsigned int   const yinc) {
+
+    if (depth == n) {
+        if (currentsz.x * currentsz.y < *maxareaP) {
+            unsigned int i;
+            for (i = 0; i < n; ++i)
+                best[i] = current[i];
+            *maxareaP = currentsz.x * currentsz.y;
+        }
+    } else {
+        unsigned int i;
 
-  for (current[depth].x = 0; 
-       imax(current[depth].x + set[depth].x, currentsz.x) * 
-           imax(currentsz.y, set[depth].y) < *maxarea; 
-       current[depth].x += xinc)
-  {
-    for (current[depth].y = 0; 
-         imax(current[depth].x + set[depth].x, currentsz.x) * 
-             imax(currentsz.y, current[depth].y + set[depth].y) < *maxarea; 
-         current[depth].y += yinc)
-    {
-      c.x = imax(current[depth].x + set[depth].x, currentsz.x);
-      c.y = imax(current[depth].y + set[depth].y, currentsz.y);
-      if (!checkcollision(current, set, &current[depth], &set[depth], depth))
-      {
-        recursefindpack(current, c, set, best, minarea, maxarea, 
-                        depth + 1, n, xinc, yinc);
-        if (*maxarea <= minarea)
-          return;
-      }
+        for (i = 0; ; ++i) {
+            for (current[depth].x = 0, current[depth].y = i * yinc;
+                 current[depth].y <= i * yinc;) {
+
+                coord c;
+
+                c.x = MAX(current[depth].x + set[depth].x, currentsz.x);
+                c.y = MAX(current[depth].y + set[depth].y, currentsz.y);
+                if (!collides(current, set, &current[depth],
+                              &set[depth], depth)) {
+                    recursefindpack(current, c, set, best, minarea, maxareaP,
+                                    depth + 1, n, xinc, yinc);
+                    if (*maxareaP <= minarea)
+                        return;
+                }
+                if (current[depth].x == (i - 1) * xinc)
+                    current[depth].y = 0;
+                if (current[depth].x < i * xinc)
+                    current[depth].x += xinc;
+                else
+                    current[depth].y += yinc;
+            }
+        }
     }
-  }
 }
 
+
+
 static void 
 findpack(struct pam *imgs, int n, coord *coords)
 {
@@ -130,15 +149,15 @@ findpack(struct pam *imgs, int n, coord *coords)
   int miny = -1;
   coord *current;
   coord *set;
-  int z = INT_MAX;
+  unsigned int z = UINT_MAX;
   coord c = { 0, 0 };
 
   if (quality > 1)
   {
     for (minarea = i = 0; i < n; ++i)
       minarea += imgs[i].height * imgs[i].width,
-      minx = imax(minx, imgs[i].width),
-      miny = imax(miny, imgs[i].height);
+      minx = MAX(minx, imgs[i].width),
+      miny = MAX(miny, imgs[i].height);
 
     minarea = minarea * qfactor / 100;
   }
@@ -237,65 +256,241 @@ writePam(struct pam *       const outpamP,
 
 
 
+static void
+writeData(FILE *             const dataFileP,
+          unsigned int       const width,
+          unsigned int       const height,
+          unsigned int       const nfiles,
+          const char **      const names,
+          const coord *      const coords,
+          const struct pam * const imgs) {
+
+    unsigned int i;
+
+    fprintf(dataFileP, ":0:0:%u:%u\n", width, height);
+
+    for (i = 0; i < nfiles; ++i) {
+        fprintf(dataFileP, "%s:%u:%u:%u:%u\n", names[i], coords[i].x,
+                coords[i].y, imgs[i].width, imgs[i].height);
+    }
+}
+
+
+
+static void
+writeHeader(FILE * const headerFileP,
+            const char * const prefix,
+            unsigned int const width,
+            unsigned int const height,
+            unsigned int const nfiles,
+            const char ** const names,
+            const coord * const coords,
+            const struct pam * imgs) {
+
+    unsigned int i;
+
+    fprintf(headerFileP, "#define %sOVERALLX %u\n", prefix, width);
+
+    fprintf(headerFileP, "#define %sOVERALLY %u\n", prefix, height);
+
+    fprintf(headerFileP, "\n");
+
+    for (i = 0; i < nfiles; ++i) {
+        char * const buffer = strdup(names[i]);
+        coord const coord = coords[i];
+        struct pam const img = imgs[i];
+
+        unsigned int j;
+        
+        *strchr(buffer, '.') = 0;
+        for (j = 0; buffer[j]; ++j) {
+            if (ISLOWER(buffer[j]))
+                buffer[j] = TOUPPER(buffer[j]);
+        }
+        fprintf(headerFileP, "#define %s%sX %u\n", 
+                prefix, buffer, coord.x);
+
+        fprintf(headerFileP, "#define %s%sY %u\n",
+                prefix, buffer, coord.y);
+
+        fprintf(headerFileP, "#define %s%sSZX %u\n",
+                prefix, buffer, img.width);
+
+        fprintf(headerFileP, "#define %s%sSZY %u\n",
+                prefix, buffer, img.height);
+
+        fprintf(headerFileP, "\n");
+    }
+}
+
+
+
+static void
+sortImagesByArea(unsigned int  const nfiles,
+                 struct pam *  const imgs,
+                 const char ** const names) {
+/*----------------------------------------------------------------------------
+   Sort the images described by 'imgs' and 'names' in place, from largest
+   area to smallest.
+-----------------------------------------------------------------------------*/
+    /* Bubble sort */
+
+    unsigned int i;
+
+    for (i = 0; i < nfiles - 1; ++i) {
+        unsigned int j;
+        for (j = i + 1; j < nfiles; ++j) {
+            if (imgs[j].width * imgs[j].height >
+                imgs[i].width * imgs[i].height) {
+
+                struct pam p;
+                const char * c;
+                
+                p = imgs[i]; imgs[i] = imgs[j]; imgs[j] = p;
+                c = names[i]; names[i] = names[j]; names[j] = c;
+            }
+        }
+    }
+}
+
+
+
+static void
+computeOutputType(sample *           const maxvalP,
+                  int *              const formatP,
+                  char *             const tupleTypeP,
+                  unsigned int *     const depthP,
+                  unsigned int       const nfiles,
+                  const struct pam * const imgs) {
+
+    unsigned int i;
+
+    sample maxval;
+    int format;
+    const char * tupleType;
+    unsigned int depth;
+
+    assert(nfiles > 0);
+
+    /* initial guesses */
+    maxval    = imgs[0].maxval;
+    format    = imgs[0].format;
+    depth     = imgs[0].depth;
+    tupleType = imgs[0].tuple_type;
+
+    for (i = 1; i < nfiles; ++i) {
+        if (PAM_FORMAT_TYPE(imgs[i].format) > PAM_FORMAT_TYPE(format)) {
+            format    = imgs[i].format;
+            tupleType = imgs[i].tuple_type;
+        }
+        maxval = MAX(maxval, imgs[i].maxval);
+        depth  = MAX(depth,  imgs[i].depth);
+    }
+
+    *maxvalP = maxval;
+    *formatP = format;
+    *depthP  = depth;
+    memcpy(tupleTypeP, tupleType, sizeof(imgs[0].tuple_type));
+}
+
+
+
+static void
+computeOutputDimensions(int * const widthP,
+                        int * const heightP,
+                        unsigned int const nfiles,
+                        const struct pam * const imgs,
+                        const coord * const coords) {
+
+    unsigned int widthGuess, heightGuess;
+    unsigned int i;
+
+    widthGuess  = 0;  /* initial value */
+    heightGuess = 0;  /* initial value */
+    
+    for (i = 0; i < nfiles; ++i) {
+        widthGuess  = MAX(widthGuess,  imgs[i].width  + coords[i].x);
+        heightGuess = MAX(heightGuess, imgs[i].height + coords[i].y);
+    }
+
+    *widthP  = widthGuess;
+    *heightP = heightGuess;
+}
+
+
+
+struct cmdlineInfo {
+    const char * header;
+    const char * data;
+    const char * prefix;
+    unsigned int quality;
+    unsigned int q[10];
+};
+
+
+
 int 
-main(int argc, char **argv)
-{
+main(int argc, const char **argv) {
+  struct cmdlineInfo cmdline;
   struct pam *imgs;
   struct pam outimg;
-  struct pam p;
   int nfiles;
-  int i, j;
-  unsigned int q[10];
   coord *coords;
-  const char *headfname = NULL;
-  const char *datafname = NULL;
-  const char *prefix = "";
   FILE *header;
   FILE *data;
-  char **names;
-  char *c;
+  const char **names;
+  unsigned int i;
 
-  optEntry *option_def = malloc(100*sizeof(optEntry));
+  optEntry * option_def;
       /* Instructions to OptParseOptions3 on how to parse our options.
        */
   optStruct3 opt;
-
+  unsigned int dataSpec, headerSpec, prefixSpec, qualitySpec;
   unsigned int option_def_index;
 
+  pm_proginit(&argc, argv);
+
+  MALLOCARRAY_NOFAIL(option_def, 100);
+  
   option_def_index = 0;   /* incremented by OPTENTRY */
-  OPTENT3( 0,  "data",    OPT_STRING, &datafname, NULL, 0);
-  OPTENT3( 0,  "header",  OPT_STRING, &headfname, NULL, 0);
-  OPTENT3('q', "quality", OPT_UINT,   &qfactor,   NULL, 0);
-  OPTENT3('p', "prefix",  OPT_STRING, &prefix,    NULL, 0);
-  OPTENT3('0', "0",       OPT_FLAG,   NULL, &q[0],      0);
-  OPTENT3('1', "1",       OPT_FLAG,   NULL, &q[1],      0);
-  OPTENT3('2', "2",       OPT_FLAG,   NULL, &q[2],      0);
-  OPTENT3('3', "3",       OPT_FLAG,   NULL, &q[3],      0);
-  OPTENT3('4', "4",       OPT_FLAG,   NULL, &q[4],      0);
-  OPTENT3('5', "5",       OPT_FLAG,   NULL, &q[5],      0);
-  OPTENT3('6', "6",       OPT_FLAG,   NULL, &q[6],      0);
-  OPTENT3('7', "7",       OPT_FLAG,   NULL, &q[7],      0);
-  OPTENT3('8', "8",       OPT_FLAG,   NULL, &q[8],      0);
-  OPTENT3('9', "9",       OPT_FLAG,   NULL, &q[9],      0);
+  OPTENT3( 0,  "data",    OPT_STRING, &cmdline.data, &dataSpec, 0);
+  OPTENT3( 0,  "header",  OPT_STRING, &cmdline.header, &headerSpec, 0);
+  OPTENT3('q', "quality", OPT_UINT,   &cmdline.quality,   &qualitySpec, 0);
+  OPTENT3('p', "prefix",  OPT_STRING, &cmdline.prefix,    &prefixSpec, 0);
+  OPTENT3('0', "0",       OPT_FLAG,   NULL, &cmdline.q[0],      0);
+  OPTENT3('1', "1",       OPT_FLAG,   NULL, &cmdline.q[1],      0);
+  OPTENT3('2', "2",       OPT_FLAG,   NULL, &cmdline.q[2],      0);
+  OPTENT3('3', "3",       OPT_FLAG,   NULL, &cmdline.q[3],      0);
+  OPTENT3('4', "4",       OPT_FLAG,   NULL, &cmdline.q[4],      0);
+  OPTENT3('5', "5",       OPT_FLAG,   NULL, &cmdline.q[5],      0);
+  OPTENT3('6', "6",       OPT_FLAG,   NULL, &cmdline.q[6],      0);
+  OPTENT3('7', "7",       OPT_FLAG,   NULL, &cmdline.q[7],      0);
+  OPTENT3('8', "8",       OPT_FLAG,   NULL, &cmdline.q[8],      0);
+  OPTENT3('9', "9",       OPT_FLAG,   NULL, &cmdline.q[9],      0);
 
   opt.opt_table = option_def;
   opt.short_allowed = FALSE;
   opt.allowNegNum = FALSE;
 
-  pnm_init(&argc, argv);
-
   /* Check for flags. */
-  optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
+  optParseOptions3(&argc, (char**)argv, opt, sizeof(opt), 0);
 
-  if (headfname)
-    header = pm_openw(headfname);
+  if (!dataSpec)
+      cmdline.data = NULL;
+  if (!headerSpec)
+      cmdline.header = NULL;
+  if (!prefixSpec)
+      cmdline.prefix = "";
+  if (!qualitySpec)
+      cmdline.quality = 200;
 
-  if (datafname)
-    data = pm_openw(datafname);
+  header = cmdline.header ? pm_openw(cmdline.header) : NULL;
+  data = cmdline.data ? pm_openw(cmdline.data) : NULL;
+  qfactor = cmdline.quality;
 
   for (i = 0; i < 10; ++i)
   {
-    if (q[i])
+    if (cmdline.q[i])
     {
       quality = i;
       switch (quality)
@@ -345,37 +540,19 @@ main(int argc, char **argv)
     imgs[0].file = stdin;
   }
 
-  pnm_readpaminit(imgs[0].file, &imgs[0], PAM_STRUCT_SIZE(tuple_type));
-  outimg.maxval = imgs[0].maxval;
-  outimg.format = imgs[0].format;
-  memcpy(outimg.tuple_type, imgs[0].tuple_type, sizeof(imgs[0].tuple_type));
-  outimg.depth = imgs[0].depth;
-
-  for (i = 1; i < nfiles; ++i)
-  {
-    pnm_readpaminit(imgs[i].file, &imgs[i], PAM_STRUCT_SIZE(tuple_type));
-    if (PAM_FORMAT_TYPE(imgs[i].format) > PAM_FORMAT_TYPE(outimg.format))
-      outimg.format = imgs[i].format,
-      memcpy(outimg.tuple_type, imgs[i].tuple_type, 
-             sizeof(imgs[i].tuple_type));
-    outimg.maxval = imax(imgs[i].maxval, outimg.maxval);
-    outimg.depth = imax(imgs[i].depth, outimg.depth);
-  }
+  for (i = 0; i < nfiles; ++i)
+      pnm_readpaminit(imgs[i].file, &imgs[i], PAM_STRUCT_SIZE(tuple_type));
 
-  for (i = 0; i < nfiles - 1; ++i)
-    for (j = i + 1; j < nfiles; ++j)
-      if (imgs[j].width * imgs[j].height > imgs[i].width * imgs[i].height)
-        p = imgs[i], imgs[i] = imgs[j], imgs[j] = p,
-        c = names[i], names[i] = names[j], names[j] = c;
+  sortImagesByArea(nfiles, imgs, names);
 
   findpack(imgs, nfiles, coords);
 
-  outimg.height = outimg.width = 0;
-  for (i = 0; i < nfiles; ++i)
-  {
-    outimg.width = imax(outimg.width, imgs[i].width + coords[i].x);
-    outimg.height = imax(outimg.height, imgs[i].height + coords[i].y);
-  }
+  computeOutputType(&outimg.maxval, &outimg.format, outimg.tuple_type,
+                    &outimg.depth, nfiles, imgs);
+
+  computeOutputDimensions(&outimg.width, &outimg.height, nfiles, imgs, coords);
+
+  pnm_setminallocationdepth(&outimg, outimg.depth);
 
   outimg.size = sizeof(outimg);
   outimg.len = sizeof(outimg);
@@ -383,56 +560,23 @@ main(int argc, char **argv)
   outimg.bytes_per_sample = 0;
   for (i = outimg.maxval; i; i >>= 8)
     ++outimg.bytes_per_sample;
-
+ 
   writePam(&outimg, nfiles, coords, imgs);
 
-  if (datafname)
-  {
-    fprintf(data, ":0:0:%u:%u\n", outimg.width, outimg.height);
-
-    for (i = 0; i < nfiles; ++i)
-    {
-      fprintf(data, "%s:%u:%u:%u:%u\n", names[i], coords[i].x,
-          coords[i].y, imgs[i].width, imgs[i].height);
-    }
-  }
+  if (data)
+      writeData(data, outimg.width, outimg.height,
+                nfiles, names, coords, imgs);
 
-  if (headfname)
-  {
-    fprintf(header, "#define %sOVERALLX %u\n"
-                    "#define %sOVERALLY %u\n"
-                    "\n",
-                    prefix, outimg.width,
-                    prefix, outimg.height);
-
-    for (i = 0; i < nfiles; ++i)
-    {
-      *strchr(names[i], '.') = 0;
-      for (j = 0; names[i][j]; ++j)
-      {
-        if (ISLOWER(names[i][j]))
-          names[i][j] = TOUPPER(names[i][j]);
-      }
-      fprintf(header, "#define %s%sX %u\n"
-                      "#define %s%sY %u\n"
-                      "#define %s%sSZX %u\n"
-                      "#define %s%sSZY %u\n"
-                      "\n",
-                      prefix, names[i], coords[i].x,
-                      prefix, names[i], coords[i].y,
-                      prefix, names[i], imgs[i].width,
-                      prefix, names[i], imgs[i].height);
-    }
-  }
+  if (header)
+      writeHeader(header, cmdline.prefix, outimg.width, outimg.height,
+                  nfiles, names, coords, imgs);
 
   for (i = 0; i < nfiles; ++i)
     pm_close(imgs[i].file);
   pm_close(stdout);
-
-  if (headfname)
+  if (header)
     pm_close(header);
-
-  if (datafname)
+  if (data)
     pm_close(data);
 
   return 0;
diff --git a/generator/pbmmake.c b/generator/pbmmake.c
index afe1dac3..6c924ac1 100644
--- a/generator/pbmmake.c
+++ b/generator/pbmmake.c
@@ -75,16 +75,8 @@ parseCommandLine(int argc, char ** argv,
                  "non-option arguments: width and height in pixels",
                  argc-1);
     else {
-        cmdlineP->width  = atoi(argv[1]);
-        cmdlineP->height = atoi(argv[2]);
-
-        if (cmdlineP->width < 1) 
-            pm_error("Width must be positive.  You specified %d.", 
-                     cmdlineP->width);
-
-        if (cmdlineP->height < 1) 
-            pm_error("Height must be positive.  You specified %d.",
-                     cmdlineP->height);
+        cmdlineP->width  = pm_parse_width(argv[1]);
+        cmdlineP->height = pm_parse_height(argv[2]);
     }
 }
 
diff --git a/lib/Makefile b/lib/Makefile
index be960250..34547e7a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -25,7 +25,7 @@ else
   LIBSYSTEM = libsystem.o
 endif
 
-LIBOBJECTS = libpm.o fileio.o bitio.o colorname.o \
+LIBOBJECTS = libpm.o pmfileio.o fileio.o bitio.o colorname.o \
 	libpbm1.o libpbm2.o libpbm3.o libpbmfont.o \
 	libpgm1.o libpgm2.o \
 	libppm1.o libppm2.o libppmcmap.o libppmcolor.o libppmfuzzy.o \
@@ -48,7 +48,7 @@ MANUALS5 = pbm pgm ppm pnm pam
 INTERFACE_HEADERS =  pm.h pbm.h bitio.h pbmfont.h \
 	pgm.h ppm.h ppmcmap.h ppmfloyd.h colorname.h \
 	pnm.h pam.h pammap.h util/shhopt.h util/nstring.h util/mallocvar.h \
-	pm_system.h pm_gamma.h
+	pm_system.h pm_gamma.h ppmdraw.h ppmdfont.h \
 
 DATAFILES = rgb.txt
 
@@ -240,9 +240,11 @@ install.hdr: $(INTERFACE_HEADERS:%=%_installhdr)
 # prefer not to "install" them, but just to access the Netpbm source
 # directory when you compile your programs.
 
-%_installhdr: $(PKGDIR)/include
+.PHONY: $(INTERFACE_HEADERS:%=%_installhdr)
+
+$(INTERFACE_HEADERS:%=%_installhdr): $(PKGDIR)/include/netpbm
 	$(INSTALL) -c -m $(INSTALL_PERM_HDR) \
-	  $(SRCDIR)/lib/$(@:%_installhdr=%) $(PKGDIR)/include/;
+	  $(SRCDIR)/lib/$(@:%_installhdr=%) $(PKGDIR)/include/netpbm/
 
 .PHONY: install.staticlib
 install.staticlib: $(PKGDIR)/link
diff --git a/lib/bitio.h b/lib/bitio.h
index 15fe0e8a..dfc5a153 100644
--- a/lib/bitio.h
+++ b/lib/bitio.h
@@ -32,7 +32,7 @@
 #ifndef _BITIO_H_
 #define _BITIO_H_
 
-#include "pm.h"
+#include <netpbm/pm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -86,4 +86,4 @@ pm_bitwrite(BITSTREAM     b,
 #ifdef __cplusplus
 }
 #endif
-#endif /* _BITIO_H_ */
+#endif
diff --git a/lib/colorname.h b/lib/colorname.h
index d33980e4..74583144 100644
--- a/lib/colorname.h
+++ b/lib/colorname.h
@@ -3,7 +3,7 @@
 
 #include <string.h>
 #include <stdio.h>
-#include "ppm.h"
+#include <netpbm/ppm.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/lib/fileio.c b/lib/fileio.c
index d891b05a..300ae303 100644
--- a/lib/fileio.c
+++ b/lib/fileio.c
@@ -85,10 +85,18 @@ pm_getuint(FILE * const ifP) {
     do {
         unsigned int const digitVal = ch - '0';
 
-        if (i > INT_MAX/10 - digitVal)
+        if (i > INT_MAX/10)
             pm_error("ASCII decimal integer in file is "
                      "too large to be processed.  ");
-        i = i * 10 + digitVal;
+        
+        i *= 10;
+
+        if (i > INT_MAX - digitVal)
+            pm_error("ASCII decimal integer in file is "
+                     "too large to be processed.  ");
+
+        i += digitVal;
+
         ch = pm_getc(ifP);
     } while (ch >= '0' && ch <= '9');
 
diff --git a/lib/libpam.h b/lib/libpam.h
index 4b8e6693..9f8a34d0 100644
--- a/lib/libpam.h
+++ b/lib/libpam.h
@@ -4,7 +4,7 @@
 #ifndef LIBPAM_H_INCLUDED
 #define LIBPAM_H_INCLUDED
 
-#include "pam.h"
+#include "pgm.h"
 
 void
 pnm_readpaminitrestaspnm(FILE * const fileP, 
diff --git a/lib/libpbm1.c b/lib/libpbm1.c
index 8dd491a7..c559c373 100644
--- a/lib/libpbm1.c
+++ b/lib/libpbm1.c
@@ -17,13 +17,33 @@
 #define _LARGE_FILES  
 
 #include <stdio.h>
-#include "pbm.h"
-#include "libpbm.h"
+
+#include "mallocvar.h"
 #include "shhopt.h"
+#include "pbm.h"
+
+
+
+bit *
+pbm_allocrow(unsigned int const cols) {
+
+    bit * bitrow;
+
+    MALLOCARRAY(bitrow, cols);
+
+    if (bitrow == NULL)
+        pm_error("Unable to allocate space for a %u-column bit row", cols);
+
+    return bitrow;
+}
+
+
 
 void
-pbm_init(int *argcP, char *argv[]) {
-    pm_proginit(argcP, argv);
+pbm_init(int *   const argcP,
+         char ** const argv) {
+
+    pm_proginit(argcP, (const char **)argv);
 }
 
 
diff --git a/lib/libpbm2.c b/lib/libpbm2.c
index 19ca93b3..df1443a3 100644
--- a/lib/libpbm2.c
+++ b/lib/libpbm2.c
@@ -10,6 +10,8 @@
 ** implied warranty.
 */
 
+#include <limits.h>
+
 #include "pbm.h"
 #include "libpbm.h"
 #include "fileio.h"
@@ -57,6 +59,28 @@ pbm_readpbminitrest( file, colsP, rowsP )
 
 
 
+static void
+validateComputableSize(unsigned int const cols,
+                       unsigned int const rows) {
+/*----------------------------------------------------------------------------
+   Validate that the dimensions of the image are such that it can be
+   processed in typical ways on this machine without worrying about
+   overflows.  Note that in C, arithmetic is always modulus
+   arithmetic, so if your values are too big, the result is not what
+   you expect.  That failed expectation can be disastrous if you use
+   it to allocate memory.
+
+   A common operation is adding 1 or 2 to the highest row or
+   column number in the image, so we make sure that's possible.
+-----------------------------------------------------------------------------*/
+    if (cols > INT_MAX - 2)
+        pm_error("image width (%u) too large to be processed", cols);
+    if (rows > INT_MAX - 2)
+        pm_error("image height (%u) too large to be processed", rows);
+}
+
+
+
 void
 pbm_readpbminit(FILE * const ifP,
                 int *  const colsP,
@@ -88,6 +112,7 @@ pbm_readpbminit(FILE * const ifP,
     default:
         pm_error("bad magic number - not a Netpbm file");
     }
+    validateComputableSize(*colsP, *rowsP);
 }
 
 
diff --git a/lib/libpbm3.c b/lib/libpbm3.c
index 2a811748..5759e65d 100644
--- a/lib/libpbm3.c
+++ b/lib/libpbm3.c
@@ -11,7 +11,6 @@
 */
 
 #include "pbm.h"
-#include "libpbm.h"
 #include "bitreverse.h"
 
 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301) && defined (__SSE__)
diff --git a/lib/libpgm1.c b/lib/libpgm1.c
index 34cc6392..ff5b711b 100644
--- a/lib/libpgm1.c
+++ b/lib/libpgm1.c
@@ -50,13 +50,14 @@ void
 pgm_init(int *   const argcP,
          char ** const argv) {
 
-    pbm_init( argcP, argv );
+    pbm_init(argcP, argv);
 }
 
 
 
 void
-pgm_nextimage(FILE * const file, int * const eofP) {
+pgm_nextimage(FILE * const file,
+              int *  const eofP) {
     pm_nextimage(file, eofP);
 }
 
diff --git a/lib/libpgm2.c b/lib/libpgm2.c
index 9f607d73..650d2cb5 100644
--- a/lib/libpgm2.c
+++ b/lib/libpgm2.c
@@ -16,7 +16,6 @@
 #include "pm_c_util.h"
 #include "mallocvar.h"
 #include "pgm.h"
-#include "libpgm.h"
 
 
 
diff --git a/lib/libpm.c b/lib/libpm.c
index 9b1ff679..1adad8b6 100644
--- a/lib/libpm.c
+++ b/lib/libpm.c
@@ -1,72 +1,45 @@
 /**************************************************************************
                                   libpm.c
 ***************************************************************************
-  This is the most fundamental Netpbm library.  It contains routines
-  not specific to any particular Netpbm format.
+  This file contains fundamental libnetpbm services.
 
   Some of the subroutines in this library are intended and documented
   for use by Netpbm users, but most of them are just used by other
   Netpbm library subroutines.
-
-  Before May 2001, this function was served by the libpbm library
-  (in addition to being the library for handling the PBM format).
-
 **************************************************************************/
-#define _SVID_SOURCE
-    /* Make sure P_tmpdir is defined in GNU libc 2.0.7 (_XOPEN_SOURCE 500
-       does it in other libc's).  pm_config.h defines TMPDIR as P_tmpdir
-       in some environments.
-    */
-#define _XOPEN_SOURCE 500    /* Make sure ftello, fseeko are defined */
-#define _LARGEFILE_SOURCE 1  /* Make sure ftello, fseeko are defined */
-#define _LARGEFILE64_SOURCE 1 
-#define _FILE_OFFSET_BITS 64
-    /* This means ftello() is really ftello64() and returns a 64 bit file
-       position.  Unless the C library doesn't have ftello64(), in which 
-       case ftello() is still just ftello().
-
-       Likewise for all the other C library file functions.
 
-       And off_t and fpos_t are 64 bit types instead of 32.  Consequently,
-       pm_filepos_t might be 64 bits instead of 32.
-    */
-#define _LARGE_FILES  
-    /* This does for AIX what _FILE_OFFSET_BITS=64 does for GNU */
-#define _LARGE_FILE_API
-    /* This makes the the x64() functions available on AIX */
+#define _XOPEN_SOURCE 500    /* Make sure ftello, fseeko are defined */
 
 #include <unistd.h>
 #include <stdio.h>
-#include <fcntl.h>
 #include <stdarg.h>
 #include <string.h>
 #include <errno.h>
 #include <setjmp.h>
-#ifdef __DJGPP__
-  #include <io.h>
-#endif
 #include <time.h>
+#include <limits.h>
 
 #include "pm_c_util.h"
+#include "mallocvar.h"
 #include "version.h"
 #include "compile.h"
 #include "nstring.h"
 #include "shhopt.h"
-#include "mallocvar.h"
+
 #include "pm.h"
 
 /* The following are set by pm_init(), then used by subsequent calls to other
    pm_xxx() functions.
    */
-static const char * pm_progname;
-static bool pm_showmessages;  
-    /* Programs should display informational messages (because the user didn't
-       specify the --quiet option).
-    */
+const char * pm_progname;
 
 int pm_plain_output;
     /* Boolean: programs should produce output in plain format */
 
+static bool pm_showmessages;  
+    /* Programs should display informational messages (because the user didn't
+       specify the --quiet option).
+    */
 static jmp_buf * pm_jmpbufP = NULL;
     /* A description of the point to which the program should hyperjump
        if a libnetpbm function encounters an error (libnetpbm functions
@@ -216,13 +189,12 @@ pm_error(const char format[], ...) {
 }
 
 
-/* Variable-sized arrays. */
 
-char *
+void *
 pm_allocrow(unsigned int const cols,
             unsigned int const size) {
 
-    char * itrow;
+    unsigned char * itrow;
 
     if (UINT_MAX / cols < size)
         pm_error("Arithmetic overflow multiplying %u by %u to get the "
@@ -238,7 +210,7 @@ pm_allocrow(unsigned int const cols,
 
 
 void
-pm_freerow(char * const itrow) {
+pm_freerow(void * const itrow) {
     free(itrow);
 }
 
@@ -385,12 +357,12 @@ pm_freearray(char ** const rowIndex,
 /* Case-insensitive keyword matcher. */
 
 int
-pm_keymatch(char *       const strarg, 
+pm_keymatch(const char *       const strarg, 
             const char * const keywordarg, 
             int          const minchars) {
     int len;
-    const char *keyword;
-    char *str;
+    const char * keyword;
+    const char * str;
 
     str = strarg;
     keyword = keywordarg;
@@ -660,7 +632,7 @@ showNetpbmHelp(const char progname[]) {
 
 
 void
-pm_proginit(int * const argcP, char * argv[]) {
+pm_proginit(int * const argcP, const char * argv[]) {
 /*----------------------------------------------------------------------------
    Do various initialization things that all programs in the Netpbm package,
    and programs that emulate such programs, should do.
@@ -747,12 +719,13 @@ pm_setMessage(int const newState, int * const oldStateP) {
 }
 
 
+
 char *
 pm_arg0toprogname(const char arg0[]) {
 /*----------------------------------------------------------------------------
    Given a value for argv[0] (a command name or file name passed to a 
    program in the standard C calling sequence), return the name of the
-   Netpbm program to which is refers.
+   Netpbm program to which it refers.
 
    In the most ordinary case, this is simply the argument itself.
 
@@ -760,7 +733,7 @@ pm_arg0toprogname(const char arg0[]) {
    after the last slash, and if there is a .exe on it (as there is for
    DJGPP), that is removed.
 
-   The return value is in static storage within.  It is null-terminated,
+   The return value is in static storage within.  It is NUL-terminated,
    but truncated at 64 characters.
 -----------------------------------------------------------------------------*/
     static char retval[64+1];
@@ -795,902 +768,55 @@ pm_randseed(void) {
 
 
 
-/* File open/close that handles "-" as stdin/stdout and checks errors. */
-
-FILE*
-pm_openr(const char * const name) {
-    FILE* f;
-
-    if (strcmp(name, "-") == 0)
-        f = stdin;
-    else {
-#ifndef VMS
-        f = fopen(name, "rb");
-#else
-        f = fopen(name, "r", "ctx=stm");
-#endif
-        if (f == NULL) 
-            pm_error("Unable to open file '%s' for reading.  "
-                     "fopen() returns errno %d (%s)", 
-                     name, errno, strerror(errno));
-    }
-    return f;
-}
-
-
-
-FILE*
-pm_openw(const char * const name) {
-    FILE* f;
-
-    if (strcmp(name, "-") == 0)
-        f = stdout;
-    else {
-#ifndef VMS
-        f = fopen(name, "wb");
-#else
-        f = fopen(name, "w", "mbc=32", "mbf=2");  /* set buffer factors */
-#endif
-        if (f == NULL) 
-            pm_error("Unable to open file '%s' for writing.  "
-                     "fopen() returns errno %d (%s)", 
-                     name, errno, strerror(errno));
-    }
-    return f;
-}
-
-
-
-static const char *
-tmpDir(void) {
-/*----------------------------------------------------------------------------
-   Return the name of the directory in which we should create temporary
-   files.
-
-   The name is a constant in static storage.
------------------------------------------------------------------------------*/
-    const char * tmpdir;
-        /* running approximation of the result */
-
-    tmpdir = getenv("TMPDIR");   /* Unix convention */
-
-    if (!tmpdir || strlen(tmpdir) == 0)
-        tmpdir = getenv("TMP");  /* Windows convention */
-
-    if (!tmpdir || strlen(tmpdir) == 0)
-        tmpdir = getenv("TEMP"); /* Windows convention */
-
-    if (!tmpdir || strlen(tmpdir) == 0)
-        tmpdir = TMPDIR;
-
-    return tmpdir;
-}
-
-
-
-static int
-mkstempx(char * const filenameBuffer) {
+unsigned int
+pm_parse_width(const char * const arg) {
 /*----------------------------------------------------------------------------
-  This is meant to be equivalent to POSIX mkstemp().
-
-  On some old systems, mktemp() is a security hazard that allows a hacker
-  to read or write our temporary file or cause us to read or write some
-  unintended file.  On other systems, mkstemp() does not exist.
-
-  A Windows/mingw environment is one which doesn't have mkstemp()
-  (2006.06.15).
-
-  We assume that if a system doesn't have mkstemp() that its mktemp()
-  is safe, or that the total situation is such that the problems of
-  mktemp() are not a problem for the user.
+   Return the image width represented by the decimal ASCIIZ string
+   'arg'.  Fail if it doesn't validly represent a width or represents
+   a width that can't be conveniently used in computation.
 -----------------------------------------------------------------------------*/
-    int retval;
-    int fd;
-    unsigned int attempts;
-    bool gotFile;
-    bool error;
-
-    for (attempts = 0, gotFile = FALSE, error = FALSE;
-         !gotFile && !error && attempts < 100;
-         ++attempts) {
-
-        char * rc;
-        rc = mktemp(filenameBuffer);
-
-        if (rc == NULL)
-            error = TRUE;
-        else {
-            int rc;
-
-            rc = open(filenameBuffer, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
-
-            if (rc == 0) {
-                fd = rc;
-                gotFile = TRUE;
-            } else {
-                if (errno == EEXIST) {
-                    /* We'll just have to keep trying */
-                } else 
-                    error = TRUE;
-            }
-        }
-    }    
-    if (gotFile)
-        retval = fd;
-    else
-        retval = -1;
-
-    return retval;
-}
-
-
-
-static int
-mkstemp2(char * const filenameBuffer) {
-
-#if HAVE_MKSTEMP
-    if (0)
-        mkstempx(NULL);  /* defeat compiler unused function warning */
-    return mkstemp(filenameBuffer);
-#else
-    return mkstempx(filenameBuffer);
-#endif
-}
-
-
-
-static void
-makeTmpfileWithTemplate(const char *  const filenameTemplate,
-                        FILE **       const filePP,
-                        const char ** const filenameP,
-                        const char ** const errorP) {
-    
-    char * filenameBuffer;  /* malloc'ed */
-
-    filenameBuffer = strdup(filenameTemplate);
-
-    if (filenameBuffer == NULL)
-        asprintfN(errorP, "Unable to allocate storage for temporary "
-                  "file name");
-    else {
-        int rc;
-        
-        rc = mkstemp2(filenameBuffer);
-        
-        if (rc < 0)
-            asprintfN(errorP,
-                      "Unable to create temporary file according to name "
-                      "pattern '%s'.  mkstemp() failed with errno %d (%s)",
-                      filenameTemplate, errno, strerror(errno));
-        else {
-            int const fd = rc;
-            
-            FILE * fileP;
-            fileP = fdopen(fd, "w+b");
-            
-            if (fileP == NULL)
-                asprintfN(errorP, "Unable to create temporary file.  "
-                          "fdopen() failed with errno %d (%s)",
-                          errno, strerror(errno));
-            else {
-                *errorP = NULL;
-                *filePP = fileP;
-                *filenameP = filenameBuffer;
-            }
-            if (*errorP) {
-                unlink(filenameBuffer);
-                close(fd);
-            }
-        }
-        if (*errorP)
-            strfree(filenameBuffer);
-    }
-}
-
-
-
-void
-pm_make_tmpfile(FILE **       const filePP,
-                const char ** const filenameP) {
-
-    const char * filenameTemplate;
-    unsigned int fnamelen;
-    const char * tmpdir;
-    const char * dirseparator;
+    unsigned int width;
     const char * error;
 
-    fnamelen = strlen(pm_progname) + 10; /* "/" + "_XXXXXX\0" */
-
-    tmpdir = tmpDir();
+    interpret_uint(arg, &width, &error);
 
-    if (tmpdir[strlen(tmpdir) - 1] == '/')
-        dirseparator = "";
-    else
-        dirseparator = "/";
-    
-    asprintfN(&filenameTemplate, "%s%s%s%s", 
-              tmpdir, dirseparator, pm_progname, "_XXXXXX");
-
-    if (filenameTemplate == NULL)
-        asprintfN(&error,
-                  "Unable to allocate storage for temporary file name");
-    else {
-        makeTmpfileWithTemplate(filenameTemplate, filePP, filenameP, &error);
-
-        strfree(filenameTemplate);
-    }
     if (error) {
-        pm_errormsg("%s", error);
+        pm_error("'%s' is invalid as an image width.  %s", arg, error);
         strfree(error);
-        pm_longjmp();
-    }
-}
-
-
-
-FILE * 
-pm_tmpfile(void) {
-
-    FILE * fileP;
-    const char * tmpfile;
-
-    pm_make_tmpfile(&fileP, &tmpfile);
-
-    unlink(tmpfile);
-
-    strfree(tmpfile);
-
-    return fileP;
-}
-
-
-
-FILE *
-pm_openr_seekable(const char name[]) {
-/*----------------------------------------------------------------------------
-  Open the file named by name[] such that it is seekable (i.e. it can be
-  rewound and read in multiple passes with fseek()).
-
-  If the file is actually seekable, this reduces to the same as
-  pm_openr().  If not, we copy the named file to a temporary file
-  and return that file's stream descriptor.
-
-  We use a file that the operating system recognizes as temporary, so
-  it picks the filename and deletes the file when Caller closes it.
------------------------------------------------------------------------------*/
-    int stat_rc;
-    int seekable;  /* logical: file is seekable */
-    struct stat statbuf;
-    FILE * original_file;
-    FILE * seekable_file;
-
-    original_file = pm_openr((char *) name);
-
-    /* I would use fseek() to determine if the file is seekable and 
-       be a little more general than checking the type of file, but I
-       don't have reliable information on how to do that.  I have seen
-       streams be partially seekable -- you can, for example seek to
-       0 if the file is positioned at 0 but you can't actually back up
-       to 0.  I have seen documentation that says the errno for an
-       unseekable stream is EBADF and in practice seen ESPIPE.
-
-       On the other hand, regular files are always seekable and even if
-       some other file is, it doesn't hurt much to assume it isn't.
-    */
-
-    stat_rc = fstat(fileno(original_file), &statbuf);
-    if (stat_rc == 0 && S_ISREG(statbuf.st_mode))
-        seekable = TRUE;
-    else 
-        seekable = FALSE;
-
-    if (seekable) {
-        seekable_file = original_file;
     } else {
-        seekable_file = pm_tmpfile();
-        
-        /* Copy the input into the temporary seekable file */
-        while (!feof(original_file) && !ferror(original_file) 
-               && !ferror(seekable_file)) {
-            char buffer[4096];
-            int bytes_read;
-            bytes_read = fread(buffer, 1, sizeof(buffer), original_file);
-            fwrite(buffer, 1, bytes_read, seekable_file);
-        }
-        if (ferror(original_file))
-            pm_error("Error reading input file into temporary file.  "
-                     "Errno = %s (%d)", strerror(errno), errno);
-        if (ferror(seekable_file))
-            pm_error("Error writing input into temporary file.  "
-                     "Errno = %s (%d)", strerror(errno), errno);
-        pm_close(original_file);
-        {
-            int seek_rc;
-            seek_rc = fseek(seekable_file, 0, SEEK_SET);
-            if (seek_rc != 0)
-                pm_error("fseek() failed to rewind temporary file.  "
-                         "Errno = %s (%d)", strerror(errno), errno);
-        }
+        if (width > INT_MAX-10)
+            pm_error("Width %u is too large for computations.", width);
+        if (width == 0)
+            pm_error("Width argument must be a positive number.  You "
+                     "specified 0.");
     }
-    return seekable_file;
-}
-
-
-
-void
-pm_close(FILE * const f) {
-    fflush(f);
-    if (ferror(f))
-        pm_message("A file read or write error occurred at some point");
-    if (f != stdin)
-        if (fclose(f) != 0)
-            pm_error("close of file failed with errno %d (%s)",
-                     errno, strerror(errno));
-}
-
-
-
-/* The pnmtopng package uses pm_closer() and pm_closew() instead of 
-   pm_close(), apparently because the 1999 Pbmplus package has them.
-   I don't know what the difference is supposed to be.
-*/
-
-void
-pm_closer(FILE * const f) {
-    pm_close(f);
-}
-
-
-
-void
-pm_closew(FILE * const f) {
-    pm_close(f);
-}
-
-
-
-/* Endian I/O.
-
-   Before Netpbm 10.27 (March 2005), these would return failure on EOF
-   or I/O failure.  For backward compatibility, they still have the return
-   code, but it is always zero and the routines abort the program in case
-   of EOF or I/O failure.  A program that wants to handle failure differently
-   must use lower level (C library) interfaces.  But that level of detail
-   is uncharacteristic of a Netpbm program; the ease of programming that
-   comes with not checking a return code is more Netpbm.
-
-   It is also for historical reasons that these return signed values,
-   when clearly unsigned would make more sense.
-*/
-
-
-
-static void
-abortWithReadError(FILE * const ifP) {
-
-    if (feof(ifP))
-        pm_error("Unexpected end of input file");
-    else
-        pm_error("Error (not EOF) reading file.");
-}
-
-
-
-void
-pm_readchar(FILE * const ifP,
-            char * const cP) {
-    
-    int c;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-
-    *cP = c;
-}
-
-
-
-void
-pm_writechar(FILE * const ofP,
-             char   const c) {
-
-    putc(c, ofP);
-}
-
-
-
-int
-pm_readbigshort(FILE *  const ifP, 
-                short * const sP) {
-    int c;
-
-    unsigned short s;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s = (c & 0xff) << 8;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s |= c & 0xff;
-
-    *sP = s;
-
-    return 0;
-}
-
-
-
-int
-pm_writebigshort(FILE * const ofP, 
-                 short  const s) {
-
-    putc((s >> 8) & 0xff, ofP);
-    putc(s & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int
-pm_readbiglong(FILE * const ifP, 
-               long * const lP) {
-
-    int c;
-    unsigned long l;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l = c << 24;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 16;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 8;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c;
-
-    *lP = l;
-
-    return 0;
-}
-
-
-
-int
-pm_writebiglong(FILE * const ofP, 
-                long   const l) {
-
-    putc((l >> 24) & 0xff, ofP);
-    putc((l >> 16) & 0xff, ofP);
-    putc((l >>  8) & 0xff, ofP);
-    putc((l >>  0) & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int
-pm_readlittleshort(FILE *  const ifP, 
-                   short * const sP) {
-    int c;
-    unsigned short s;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s = c & 0xff;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    s |= (c & 0xff) << 8;
-
-    *sP = s;
-
-    return 0;
-}
-
-
-
-int
-pm_writelittleshort(FILE * const ofP, 
-                    short  const s) {
-
-    putc((s >> 0) & 0xff, ofP);
-    putc((s >> 8) & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int
-pm_readlittlelong(FILE * const ifP, 
-                  long * const lP) {
-    int c;
-    unsigned long l;
-
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l = c;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 8;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 16;
-    c = getc(ifP);
-    if (c == EOF)
-        abortWithReadError(ifP);
-    l |= c << 24;
-
-    *lP = l;
-
-    return 0;
-}
-
-
-
-int
-pm_writelittlelong(FILE * const ofP, 
-                   long   const l) {
-
-    putc((l >>  0) & 0xff, ofP);
-    putc((l >>  8) & 0xff, ofP);
-    putc((l >> 16) & 0xff, ofP);
-    putc((l >> 24) & 0xff, ofP);
-
-    return 0;
-}
-
-
-
-int 
-pm_readmagicnumber(FILE * const ifP) {
-
-    int ich1, ich2;
-
-    ich1 = getc(ifP);
-    ich2 = getc(ifP);
-    if (ich1 == EOF || ich2 == EOF)
-        pm_error( "Error reading magic number from Netpbm image stream.  "
-                  "Most often, this "
-                  "means your input file is empty." );
-
-    return ich1 * 256 + ich2;
-}
-
-
-
-/* Read a file of unknown size to a buffer. Return the number of bytes
-   read. Allocate more memory as we need it. The calling routine has
-   to free() the buffer.
-
-   Oliver Trepte, oliver@fysik4.kth.se, 930613 */
-
-#define PM_BUF_SIZE 16384      /* First try this size of the buffer, then
-                                  double this until we reach PM_MAX_BUF_INC */
-#define PM_MAX_BUF_INC 65536   /* Don't allocate more memory in larger blocks
-                                  than this. */
-
-char *
-pm_read_unknown_size(FILE * const file, 
-                     long * const nread) {
-    long nalloc;
-    char * buf;
-    bool eof;
-
-    *nread = 0;
-    nalloc = PM_BUF_SIZE;
-    MALLOCARRAY(buf, nalloc);
-
-    eof = FALSE;  /* initial value */
-
-    while(!eof) {
-        int val;
-
-        if (*nread >= nalloc) { /* We need a larger buffer */
-            if (nalloc > PM_MAX_BUF_INC)
-                nalloc += PM_MAX_BUF_INC;
-            else
-                nalloc += nalloc;
-            REALLOCARRAY_NOFAIL(buf, nalloc);
-        }
-
-        val = getc(file);
-        if (val == EOF)
-            eof = TRUE;
-        else 
-            buf[(*nread)++] = val;
-    }
-    return buf;
-}
-
-
-
-union cheat {
-    uint32n l;
-    short s;
-    unsigned char c[4];
-};
-
-
-
-short
-pm_bs_short(short const s) {
-    union cheat u;
-    unsigned char t;
-
-    u.s = s;
-    t = u.c[0];
-    u.c[0] = u.c[1];
-    u.c[1] = t;
-    return u.s;
-}
-
-
-
-long
-pm_bs_long(long const l) {
-    union cheat u;
-    unsigned char t;
-
-    u.l = l;
-    t = u.c[0];
-    u.c[0] = u.c[3];
-    u.c[3] = t;
-    t = u.c[1];
-    u.c[1] = u.c[2];
-    u.c[2] = t;
-    return u.l;
-}
-
-
-
-void
-pm_tell2(FILE *       const fileP, 
-         void *       const fileposP,
-         unsigned int const fileposSize) {
-/*----------------------------------------------------------------------------
-   Return the current file position as *filePosP, which is a buffer
-   'fileposSize' bytes long.  Abort the program if error, including if
-   *fileP isn't a file that has a position.
------------------------------------------------------------------------------*/
-    /* Note: FTELLO() is either ftello() or ftell(), depending on the
-       capabilities of the underlying C library.  It is defined in
-       pm_config.h.  ftello(), in turn, may be either ftell() or
-       ftello64(), as implemented by the C library.
-    */
-    pm_filepos const filepos = FTELLO(fileP);
-    if (filepos < 0)
-        pm_error("ftello() to get current file position failed.  "
-                 "Errno = %s (%d)\n", strerror(errno), errno);
-
-    if (fileposSize == sizeof(pm_filepos)) {
-        pm_filepos * const fileposP_filepos = fileposP;
-        *fileposP_filepos = filepos;
-    } else if (fileposSize == sizeof(long)) {
-        if (sizeof(pm_filepos) > sizeof(long) &&
-            filepos >= (pm_filepos) 1 << (sizeof(long)*8))
-            pm_error("File size is too large to represent in the %u bytes "
-                     "that were provided to pm_tell2()", fileposSize);
-        else {
-            long * const fileposP_long = fileposP;
-            *fileposP_long = (long)filepos;
-        }
-    } else
-        pm_error("File position size passed to pm_tell() is invalid: %u.  "
-                 "Valid sizes are %u and %u", 
-                 fileposSize, (unsigned int)sizeof(pm_filepos),
-                 (unsigned int) sizeof(long));
+    return width;
 }
 
 
 
 unsigned int
-pm_tell(FILE * const fileP) {
-    
-    long filepos;
-
-    pm_tell2(fileP, &filepos, sizeof(filepos));
-
-    return filepos;
-}
-
-
-
-void
-pm_seek2(FILE *             const fileP, 
-         const pm_filepos * const fileposP,
-         unsigned int       const fileposSize) {
+pm_parse_height(const char * const arg) {
 /*----------------------------------------------------------------------------
-   Position file *fileP to position *fileposP.  Abort if error, including
-   if *fileP isn't a seekable file.
+  Same as pm_parse_width(), but for height.
 -----------------------------------------------------------------------------*/
-    if (fileposSize == sizeof(pm_filepos)) 
-        /* Note: FSEEKO() is either fseeko() or fseek(), depending on the
-           capabilities of the underlying C library.  It is defined in
-           pm_config.h.  fseeko(), in turn, may be either fseek() or
-           fseeko64(), as implemented by the C library.
-        */
-        FSEEKO(fileP, *fileposP, SEEK_SET);
-    else if (fileposSize == sizeof(long)) {
-        long const fileposLong = *(long *)fileposP;
-        fseek(fileP, fileposLong, SEEK_SET);
-    } else
-        pm_error("File position size passed to pm_seek() is invalid: %u.  "
-                 "Valid sizes are %u and %u", 
-                 fileposSize, (unsigned int)sizeof(pm_filepos),
-                 (unsigned int) sizeof(long));
-}
-
-
-
-void
-pm_seek(FILE * const fileP, unsigned long filepos) {
-/*----------------------------------------------------------------------------
------------------------------------------------------------------------------*/
-
-    pm_filepos fileposBuff;
-
-    fileposBuff = filepos;
-
-    pm_seek2(fileP, &fileposBuff, sizeof(fileposBuff));
-}
-
+    unsigned int height;
+    const char * error;
 
+    interpret_uint(arg, &height, &error);
 
-void
-pm_nextimage(FILE * const file, int * const eofP) {
-/*----------------------------------------------------------------------------
-   Position the file 'file' to the next image in the stream, assuming it is
-   now positioned just after the current image.  I.e. read off any white
-   space at the end of the current image's raster.  Note that the raw formats
-   don't permit such white space, but this routine tolerates it anyway, 
-   because the plain formats do permit white space after the raster.
-
-   Iff there is no next image, return *eofP == TRUE.
-
-   Note that in practice, we will not normally see white space here in
-   a plain PPM or plain PGM stream because the routine to read a
-   sample from the image reads one character of white space after the
-   sample in order to know where the sample ends.  There is not
-   normally more than one character of white space (a newline) after
-   the last sample in the raster.  But plain PBM is another story.  No white
-   space is required between samples of a plain PBM image.  But the raster
-   normally ends with a newline nonetheless.  Since the sample reading code
-   will not have read that newline, it is there for us to read now.
------------------------------------------------------------------------------*/
-    bool eof;
-    bool nonWhitespaceFound;
-
-    eof = FALSE;
-    nonWhitespaceFound = FALSE;
-
-    while (!eof && !nonWhitespaceFound) {
-        int c;
-        c = getc(file);
-        if (c == EOF) {
-            if (feof(file)) 
-                eof = TRUE;
-            else
-                pm_error("File error on getc() to position to image");
-        } else {
-            if (!isspace(c)) {
-                int rc;
-
-                nonWhitespaceFound = TRUE;
-
-                /* Have to put the non-whitespace character back in
-                   the stream -- it's part of the next image.  
-                */
-                rc = ungetc(c, file);
-                if (rc == EOF) 
-                    pm_error("File error doing ungetc() "
-                             "to position to image.");
-            }
-        }
+    if (error) {
+        pm_error("'%s' is invalid as an image height.  %s", arg, error);
+        strfree(error);
+    } else {
+        if (height > INT_MAX-10)
+            pm_error("Height %u is too large for computations.", height);
+        if (height == 0)
+            pm_error("Height argument must be a positive number.  You "
+                     "specified 0.");
     }
-    *eofP = eof;
-}
-
-
-
-void
-pm_check(FILE *               const file, 
-         enum pm_check_type   const check_type, 
-         pm_filepos           const need_raster_size,
-         enum pm_check_code * const retval_p) {
-/*----------------------------------------------------------------------------
-   This is not defined for use outside of libnetpbm.
------------------------------------------------------------------------------*/
-    struct stat statbuf;
-    pm_filepos curpos;  /* Current position of file; -1 if none */
-    int rc;
-
-#ifdef LARGEFILEDEBUG
-    pm_message("pm_filepos received by pm_check() is %u bytes.",
-               sizeof(pm_filepos));
-#endif
-    /* Note: FTELLO() is either ftello() or ftell(), depending on the
-       capabilities of the underlying C library.  It is defined in
-       pm_config.h.  ftello(), in turn, may be either ftell() or
-       ftello64(), as implemented by the C library.
-    */
-    curpos = FTELLO(file);
-    if (curpos >= 0) {
-        /* This type of file has a current position */
-            
-        rc = fstat(fileno(file), &statbuf);
-        if (rc != 0) 
-            pm_error("fstat() failed to get size of file, though ftello() "
-                     "successfully identified\n"
-                     "the current position.  Errno=%s (%d)",
-                     strerror(errno), errno);
-        else if (!S_ISREG(statbuf.st_mode)) {
-            /* Not a regular file; we can't know its size */
-            if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
-        } else {
-            pm_filepos const have_raster_size = statbuf.st_size - curpos;
-            
-            if (have_raster_size < need_raster_size)
-                pm_error("File has invalid format.  The raster should "
-                         "contain %u bytes, but\n"
-                         "the file ends after only %u bytes.",
-                         (unsigned int) need_raster_size, 
-                         (unsigned int) have_raster_size);
-            else if (have_raster_size > need_raster_size) {
-                if (retval_p) *retval_p = PM_CHECK_TOO_LONG;
-            } else {
-                if (retval_p) *retval_p = PM_CHECK_OK;
-            }
-        }
-    } else
-        if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
+    return height;
 }
 
 
 
-void
-pm_drain(FILE *         const fileP,
-         unsigned int   const limit,
-         unsigned int * const bytesReadP) {
-/*----------------------------------------------------------------------------
-  Read bytes from *fileP until EOF and return as *bytesReadP how many there
-  were.
-
-  But don't read any more than 'limit'.
-
-  This is a good thing to call after reading an input file to be sure you
-  didn't leave some input behind, which could mean you didn't properly
-  interpret the file.
------------------------------------------------------------------------------*/
-    unsigned int bytesRead;
-    bool eof;
-
-    for (bytesRead = 0, eof = false; !eof && bytesRead < limit;) {
-
-        int rc;
-
-        rc = fgetc(fileP);
-
-        eof = (rc == EOF);
-        if (!eof)
-            ++bytesRead;
-    }
-    *bytesReadP = bytesRead;
-}
diff --git a/lib/libpnm2.c b/lib/libpnm2.c
index 7e4f7e2a..fdfe0dc0 100644
--- a/lib/libpnm2.c
+++ b/lib/libpnm2.c
@@ -15,13 +15,10 @@
 #include "pnm.h"
 
 #include "ppm.h"
-#include "libppm.h"
 
 #include "pgm.h"
-#include "libpgm.h"
 
 #include "pbm.h"
-#include "libpbm.h"
 
 void
 pnm_writepnminit(FILE * const fileP, 
diff --git a/lib/libpnm3.c b/lib/libpnm3.c
index 4df8041c..71df0cbf 100644
--- a/lib/libpnm3.c
+++ b/lib/libpnm3.c
@@ -13,13 +13,10 @@
 #include "pnm.h"
 
 #include "ppm.h"
-#include "libppm.h"
 
 #include "pgm.h"
-#include "libpgm.h"
 
 #include "pbm.h"
-#include "libpbm.h"
 
 xel
 pnm_backgroundxel( xel** xels, int cols, int rows, xelval maxval, int format )
diff --git a/lib/libppm2.c b/lib/libppm2.c
index 52c4ab16..78898910 100644
--- a/lib/libppm2.c
+++ b/lib/libppm2.c
@@ -17,7 +17,6 @@
 #include "pm_c_util.h"
 #include "mallocvar.h"
 #include "ppm.h"
-#include "libppm.h"
 
 void
 ppm_writeppminit(FILE*  const fileP, 
diff --git a/lib/libppmcmap.c b/lib/libppmcmap.c
index 954488d7..055bfc8b 100644
--- a/lib/libppmcmap.c
+++ b/lib/libppmcmap.c
@@ -15,7 +15,6 @@
 #include "pm_c_util.h"
 #include "nstring.h"
 #include "mallocvar.h"
-#include "libppm.h"
 #include "ppm.h"
 #include "ppmcmap.h"
 
diff --git a/lib/pam.h b/lib/pam.h
index 8f9f5d12..c28c5c2c 100644
--- a/lib/pam.h
+++ b/lib/pam.h
@@ -6,8 +6,8 @@
 #ifndef PAM_H
 #define PAM_H
 
-#include "pm.h"
-#include "pnm.h"
+#include <netpbm/pm.h>
+#include <netpbm/pnm.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/lib/pammap.h b/lib/pammap.h
index a53a9c23..c9c1deed 100644
--- a/lib/pammap.h
+++ b/lib/pammap.h
@@ -9,8 +9,8 @@
 
 #ifndef PAMMAP_H
 #define PAMMAP_H
-#include "colorname.h"
-#include "pam.h"
+#include <netpbm/colorname.h>
+#include <netpbm/pam.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/lib/pbm.h b/lib/pbm.h
index 1591c77f..1c3fedc5 100644
--- a/lib/pbm.h
+++ b/lib/pbm.h
@@ -1,7 +1,7 @@
 #ifndef PBM_H_INCLUDED
 #define PBM_H_INCLUDED
 
-#include "pm.h"
+#include <netpbm/pm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -33,13 +33,18 @@ typedef unsigned char bit;
 
 /* Declarations of routines. */
 
-void pbm_init ARGS(( int* argcP, char* argv[] ));
+void
+pbm_init(int *   const argcP,
+         char ** const argv);
+
 void
 pbm_nextimage(FILE *file, int * const eofP);
 
+bit *
+pbm_allocrow(unsigned int const cols);
+
 #define pbm_allocarray(cols, rows) \
   ((bit**) pm_allocarray(cols, rows, sizeof(bit)))
-#define pbm_allocrow(cols) ((bit*) pm_allocrow(cols, sizeof(bit)))
 #define pbm_freearray(bits, rows) pm_freearray((char**) bits, rows)
 #define pbm_freerow(bitrow) pm_freerow((char*) bitrow)
 #define pbm_packed_bytes(cols) (((cols)+7)/8)
diff --git a/lib/pbmfont.h b/lib/pbmfont.h
index 8a81c37a..d287bc6c 100644
--- a/lib/pbmfont.h
+++ b/lib/pbmfont.h
@@ -68,7 +68,7 @@ pbm_dissectfont(const bit ** const font,
 struct font* pbm_loadfont(const char * const filename);
 struct font* pbm_loadpbmfont(const char * const filename);
 struct font* pbm_loadbdffont(const char * const filename);
-void pbm_dumpfont ARGS(( struct font* fn ));
+void pbm_dumpfont(struct font * const fnP);
 
 #ifdef __cplusplus
 }
diff --git a/lib/pgm.h b/lib/pgm.h
index 86935307..2de8d531 100644
--- a/lib/pgm.h
+++ b/lib/pgm.h
@@ -4,7 +4,8 @@
 #ifndef _PGM_H_
 #define _PGM_H_
 
-#include "pbm.h"
+#include <netpbm/pm.h>
+#include <netpbm/pbm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -68,7 +69,7 @@ pgm_init(int *   const argcP,
 gray *
 pgm_allocrow(unsigned int const cols);
 
-#define pgm_freerow(grayrow) free(grayrow)
+#define pgm_freerow(grayrow) pm_freerow(grayrow)
 
 #define pgm_allocarray( cols, rows ) \
   ((gray**) pm_allocarray( cols, rows, sizeof(gray) ))
diff --git a/lib/pm.h b/lib/pm.h
index 232fc363..b38580ff 100644
--- a/lib/pm.h
+++ b/lib/pm.h
@@ -13,7 +13,7 @@
 #ifndef PM_H_INCLUDED
 #define PM_H_INCLUDED
 
-#include "pm_config.h"
+#include <netpbm/pm_config.h>
 
 #include <sys/types.h>
 #include <ctype.h>
@@ -91,12 +91,13 @@ extern int pm_plain_output;
     /* Output functions are to produce plain (as opposed to raw) format
        regardless of their 'plainformat' arguments.
     */
+extern const char * pm_progname;
 
 void 
 pm_init(const char * const progname, unsigned int const flags);
 
 void 
-pm_proginit(int* const argcP, char* argv[]);
+pm_proginit(int * const argcP, const char * argv[]);
 
 void
 pm_setMessage(int const newState, int * const oldStateP);
@@ -116,7 +117,7 @@ pm_nextimage(FILE * const file, int * const eofP);
 char** 
 pm_allocarray (int const cols, int const rows, int const size );
 
-char * 
+void * 
 pm_allocrow(unsigned int const cols,
             unsigned int const size);
 
@@ -124,12 +125,14 @@ void
 pm_freearray (char** const its, int const rows);
 
 void 
-pm_freerow(char* const itrow);
+pm_freerow(void * const row);
 
 
 /* Obsolete -- use shhopt instead */
 int 
-pm_keymatch (char* const str, const char* const keyword, int const minchars);
+pm_keymatch(const char * const str,
+            const char * const keyword,
+            int          const minchars);
 
 
 int PURE_FN_ATTR
@@ -356,6 +359,12 @@ pm_arg0toprogname(const char arg0[]);
 unsigned int
 pm_randseed(void);
 
+unsigned int
+pm_parse_width(const char * const arg);
+
+unsigned int
+pm_parse_height(const char * const arg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/pm_gamma.h b/lib/pm_gamma.h
index 92b34145..6630e05c 100644
--- a/lib/pm_gamma.h
+++ b/lib/pm_gamma.h
@@ -1,7 +1,7 @@
 #ifndef _PM_GAMMA_H_
 #define _PM_GAMMA_H_
 
-#include "pm_config.h"
+#include <netpbm/pm_config.h>
 
 #include <math.h>
 
diff --git a/lib/pmfileio.c b/lib/pmfileio.c
new file mode 100644
index 00000000..e2cd3d06
--- /dev/null
+++ b/lib/pmfileio.c
@@ -0,0 +1,948 @@
+/**************************************************************************
+                                 pmfileio.c
+***************************************************************************
+  This file contains fundamental file I/O stuff for libnetpbm.
+  These are external functions, unlike 'fileio.c', but are not
+  particular to any Netpbm format.
+**************************************************************************/
+#define _SVID_SOURCE
+    /* Make sure P_tmpdir is defined in GNU libc 2.0.7 (_XOPEN_SOURCE 500
+       does it in other libc's).  pm_config.h defines TMPDIR as P_tmpdir
+       in some environments.
+    */
+#define _XOPEN_SOURCE 500    /* Make sure ftello, fseeko are defined */
+#define _LARGEFILE_SOURCE 1  /* Make sure ftello, fseeko are defined */
+#define _LARGEFILE64_SOURCE 1 
+#define _FILE_OFFSET_BITS 64
+    /* This means ftello() is really ftello64() and returns a 64 bit file
+       position.  Unless the C library doesn't have ftello64(), in which 
+       case ftello() is still just ftello().
+
+       Likewise for all the other C library file functions.
+
+       And off_t and fpos_t are 64 bit types instead of 32.  Consequently,
+       pm_filepos_t might be 64 bits instead of 32.
+    */
+#define _LARGE_FILES  
+    /* This does for AIX what _FILE_OFFSET_BITS=64 does for GNU */
+#define _LARGE_FILE_API
+    /* This makes the the x64() functions available on AIX */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#ifdef __DJGPP__
+  #include <io.h>
+#endif
+
+#include "pm_c_util.h"
+#include "mallocvar.h"
+#include "nstring.h"
+
+#include "pm.h"
+
+
+
+/* File open/close that handles "-" as stdin/stdout and checks errors. */
+
+FILE*
+pm_openr(const char * const name) {
+    FILE* f;
+
+    if (strcmp(name, "-") == 0)
+        f = stdin;
+    else {
+#ifndef VMS
+        f = fopen(name, "rb");
+#else
+        f = fopen(name, "r", "ctx=stm");
+#endif
+        if (f == NULL) 
+            pm_error("Unable to open file '%s' for reading.  "
+                     "fopen() returns errno %d (%s)", 
+                     name, errno, strerror(errno));
+    }
+    return f;
+}
+
+
+
+FILE*
+pm_openw(const char * const name) {
+    FILE* f;
+
+    if (strcmp(name, "-") == 0)
+        f = stdout;
+    else {
+#ifndef VMS
+        f = fopen(name, "wb");
+#else
+        f = fopen(name, "w", "mbc=32", "mbf=2");  /* set buffer factors */
+#endif
+        if (f == NULL) 
+            pm_error("Unable to open file '%s' for writing.  "
+                     "fopen() returns errno %d (%s)", 
+                     name, errno, strerror(errno));
+    }
+    return f;
+}
+
+
+
+static const char *
+tmpDir(void) {
+/*----------------------------------------------------------------------------
+   Return the name of the directory in which we should create temporary
+   files.
+
+   The name is a constant in static storage.
+-----------------------------------------------------------------------------*/
+    const char * tmpdir;
+        /* running approximation of the result */
+
+    tmpdir = getenv("TMPDIR");   /* Unix convention */
+
+    if (!tmpdir || strlen(tmpdir) == 0)
+        tmpdir = getenv("TMP");  /* Windows convention */
+
+    if (!tmpdir || strlen(tmpdir) == 0)
+        tmpdir = getenv("TEMP"); /* Windows convention */
+
+    if (!tmpdir || strlen(tmpdir) == 0)
+        tmpdir = TMPDIR;
+
+    return tmpdir;
+}
+
+
+
+static int
+mkstempx(char * const filenameBuffer) {
+/*----------------------------------------------------------------------------
+  This is meant to be equivalent to POSIX mkstemp().
+
+  On some old systems, mktemp() is a security hazard that allows a hacker
+  to read or write our temporary file or cause us to read or write some
+  unintended file.  On other systems, mkstemp() does not exist.
+
+  A Windows/mingw environment is one which doesn't have mkstemp()
+  (2006.06.15).
+
+  We assume that if a system doesn't have mkstemp() that its mktemp()
+  is safe, or that the total situation is such that the problems of
+  mktemp() are not a problem for the user.
+-----------------------------------------------------------------------------*/
+    int retval;
+    int fd;
+    unsigned int attempts;
+    bool gotFile;
+    bool error;
+
+    for (attempts = 0, gotFile = FALSE, error = FALSE;
+         !gotFile && !error && attempts < 100;
+         ++attempts) {
+
+        char * rc;
+        rc = mktemp(filenameBuffer);
+
+        if (rc == NULL)
+            error = TRUE;
+        else {
+            int rc;
+
+            rc = open(filenameBuffer, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
+
+            if (rc == 0) {
+                fd = rc;
+                gotFile = TRUE;
+            } else {
+                if (errno == EEXIST) {
+                    /* We'll just have to keep trying */
+                } else 
+                    error = TRUE;
+            }
+        }
+    }    
+    if (gotFile)
+        retval = fd;
+    else
+        retval = -1;
+
+    return retval;
+}
+
+
+
+static int
+mkstemp2(char * const filenameBuffer) {
+
+#if HAVE_MKSTEMP
+    if (0)
+        mkstempx(NULL);  /* defeat compiler unused function warning */
+    return mkstemp(filenameBuffer);
+#else
+    return mkstempx(filenameBuffer);
+#endif
+}
+
+
+
+static void
+makeTmpfileWithTemplate(const char *  const filenameTemplate,
+                        FILE **       const filePP,
+                        const char ** const filenameP,
+                        const char ** const errorP) {
+    
+    char * filenameBuffer;  /* malloc'ed */
+
+    filenameBuffer = strdup(filenameTemplate);
+
+    if (filenameBuffer == NULL)
+        asprintfN(errorP, "Unable to allocate storage for temporary "
+                  "file name");
+    else {
+        int rc;
+        
+        rc = mkstemp2(filenameBuffer);
+        
+        if (rc < 0)
+            asprintfN(errorP,
+                      "Unable to create temporary file according to name "
+                      "pattern '%s'.  mkstemp() failed with errno %d (%s)",
+                      filenameTemplate, errno, strerror(errno));
+        else {
+            int const fd = rc;
+            
+            FILE * fileP;
+            fileP = fdopen(fd, "w+b");
+            
+            if (fileP == NULL)
+                asprintfN(errorP, "Unable to create temporary file.  "
+                          "fdopen() failed with errno %d (%s)",
+                          errno, strerror(errno));
+            else {
+                *errorP = NULL;
+                *filePP = fileP;
+                *filenameP = filenameBuffer;
+            }
+            if (*errorP) {
+                unlink(filenameBuffer);
+                close(fd);
+            }
+        }
+        if (*errorP)
+            strfree(filenameBuffer);
+    }
+}
+
+
+
+void
+pm_make_tmpfile(FILE **       const filePP,
+                const char ** const filenameP) {
+
+    const char * filenameTemplate;
+    unsigned int fnamelen;
+    const char * tmpdir;
+    const char * dirseparator;
+    const char * error;
+
+    fnamelen = strlen(pm_progname) + 10; /* "/" + "_XXXXXX\0" */
+
+    tmpdir = tmpDir();
+
+    if (tmpdir[strlen(tmpdir) - 1] == '/')
+        dirseparator = "";
+    else
+        dirseparator = "/";
+    
+    asprintfN(&filenameTemplate, "%s%s%s%s", 
+              tmpdir, dirseparator, pm_progname, "_XXXXXX");
+
+    if (filenameTemplate == NULL)
+        asprintfN(&error,
+                  "Unable to allocate storage for temporary file name");
+    else {
+        makeTmpfileWithTemplate(filenameTemplate, filePP, filenameP, &error);
+
+        strfree(filenameTemplate);
+    }
+    if (error) {
+        pm_errormsg("%s", error);
+        strfree(error);
+        pm_longjmp();
+    }
+}
+
+
+
+FILE * 
+pm_tmpfile(void) {
+
+    FILE * fileP;
+    const char * tmpfile;
+
+    pm_make_tmpfile(&fileP, &tmpfile);
+
+    unlink(tmpfile);
+
+    strfree(tmpfile);
+
+    return fileP;
+}
+
+
+
+FILE *
+pm_openr_seekable(const char name[]) {
+/*----------------------------------------------------------------------------
+  Open the file named by name[] such that it is seekable (i.e. it can be
+  rewound and read in multiple passes with fseek()).
+
+  If the file is actually seekable, this reduces to the same as
+  pm_openr().  If not, we copy the named file to a temporary file
+  and return that file's stream descriptor.
+
+  We use a file that the operating system recognizes as temporary, so
+  it picks the filename and deletes the file when Caller closes it.
+-----------------------------------------------------------------------------*/
+    int stat_rc;
+    int seekable;  /* logical: file is seekable */
+    struct stat statbuf;
+    FILE * original_file;
+    FILE * seekable_file;
+
+    original_file = pm_openr((char *) name);
+
+    /* I would use fseek() to determine if the file is seekable and 
+       be a little more general than checking the type of file, but I
+       don't have reliable information on how to do that.  I have seen
+       streams be partially seekable -- you can, for example seek to
+       0 if the file is positioned at 0 but you can't actually back up
+       to 0.  I have seen documentation that says the errno for an
+       unseekable stream is EBADF and in practice seen ESPIPE.
+
+       On the other hand, regular files are always seekable and even if
+       some other file is, it doesn't hurt much to assume it isn't.
+    */
+
+    stat_rc = fstat(fileno(original_file), &statbuf);
+    if (stat_rc == 0 && S_ISREG(statbuf.st_mode))
+        seekable = TRUE;
+    else 
+        seekable = FALSE;
+
+    if (seekable) {
+        seekable_file = original_file;
+    } else {
+        seekable_file = pm_tmpfile();
+        
+        /* Copy the input into the temporary seekable file */
+        while (!feof(original_file) && !ferror(original_file) 
+               && !ferror(seekable_file)) {
+            char buffer[4096];
+            int bytes_read;
+            bytes_read = fread(buffer, 1, sizeof(buffer), original_file);
+            fwrite(buffer, 1, bytes_read, seekable_file);
+        }
+        if (ferror(original_file))
+            pm_error("Error reading input file into temporary file.  "
+                     "Errno = %s (%d)", strerror(errno), errno);
+        if (ferror(seekable_file))
+            pm_error("Error writing input into temporary file.  "
+                     "Errno = %s (%d)", strerror(errno), errno);
+        pm_close(original_file);
+        {
+            int seek_rc;
+            seek_rc = fseek(seekable_file, 0, SEEK_SET);
+            if (seek_rc != 0)
+                pm_error("fseek() failed to rewind temporary file.  "
+                         "Errno = %s (%d)", strerror(errno), errno);
+        }
+    }
+    return seekable_file;
+}
+
+
+
+void
+pm_close(FILE * const f) {
+    fflush(f);
+    if (ferror(f))
+        pm_message("A file read or write error occurred at some point");
+    if (f != stdin)
+        if (fclose(f) != 0)
+            pm_error("close of file failed with errno %d (%s)",
+                     errno, strerror(errno));
+}
+
+
+
+/* The pnmtopng package uses pm_closer() and pm_closew() instead of 
+   pm_close(), apparently because the 1999 Pbmplus package has them.
+   I don't know what the difference is supposed to be.
+*/
+
+void
+pm_closer(FILE * const f) {
+    pm_close(f);
+}
+
+
+
+void
+pm_closew(FILE * const f) {
+    pm_close(f);
+}
+
+
+
+/* Endian I/O.
+
+   Before Netpbm 10.27 (March 2005), these would return failure on EOF
+   or I/O failure.  For backward compatibility, they still have the return
+   code, but it is always zero and the routines abort the program in case
+   of EOF or I/O failure.  A program that wants to handle failure differently
+   must use lower level (C library) interfaces.  But that level of detail
+   is uncharacteristic of a Netpbm program; the ease of programming that
+   comes with not checking a return code is more Netpbm.
+
+   It is also for historical reasons that these return signed values,
+   when clearly unsigned would make more sense.
+*/
+
+
+
+static void
+abortWithReadError(FILE * const ifP) {
+
+    if (feof(ifP))
+        pm_error("Unexpected end of input file");
+    else
+        pm_error("Error (not EOF) reading file.");
+}
+
+
+
+void
+pm_readchar(FILE * const ifP,
+            char * const cP) {
+    
+    int c;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+
+    *cP = c;
+}
+
+
+
+void
+pm_writechar(FILE * const ofP,
+             char   const c) {
+
+    putc(c, ofP);
+}
+
+
+
+int
+pm_readbigshort(FILE *  const ifP, 
+                short * const sP) {
+    int c;
+
+    unsigned short s;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s = (c & 0xff) << 8;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s |= c & 0xff;
+
+    *sP = s;
+
+    return 0;
+}
+
+
+
+int
+pm_writebigshort(FILE * const ofP, 
+                 short  const s) {
+
+    putc((s >> 8) & 0xff, ofP);
+    putc(s & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int
+pm_readbiglong(FILE * const ifP, 
+               long * const lP) {
+
+    int c;
+    unsigned long l;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l = c << 24;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 16;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 8;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c;
+
+    *lP = l;
+
+    return 0;
+}
+
+
+
+int
+pm_writebiglong(FILE * const ofP, 
+                long   const l) {
+
+    putc((l >> 24) & 0xff, ofP);
+    putc((l >> 16) & 0xff, ofP);
+    putc((l >>  8) & 0xff, ofP);
+    putc((l >>  0) & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int
+pm_readlittleshort(FILE *  const ifP, 
+                   short * const sP) {
+    int c;
+    unsigned short s;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s = c & 0xff;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    s |= (c & 0xff) << 8;
+
+    *sP = s;
+
+    return 0;
+}
+
+
+
+int
+pm_writelittleshort(FILE * const ofP, 
+                    short  const s) {
+
+    putc((s >> 0) & 0xff, ofP);
+    putc((s >> 8) & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int
+pm_readlittlelong(FILE * const ifP, 
+                  long * const lP) {
+    int c;
+    unsigned long l;
+
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l = c;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 8;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 16;
+    c = getc(ifP);
+    if (c == EOF)
+        abortWithReadError(ifP);
+    l |= c << 24;
+
+    *lP = l;
+
+    return 0;
+}
+
+
+
+int
+pm_writelittlelong(FILE * const ofP, 
+                   long   const l) {
+
+    putc((l >>  0) & 0xff, ofP);
+    putc((l >>  8) & 0xff, ofP);
+    putc((l >> 16) & 0xff, ofP);
+    putc((l >> 24) & 0xff, ofP);
+
+    return 0;
+}
+
+
+
+int 
+pm_readmagicnumber(FILE * const ifP) {
+
+    int ich1, ich2;
+
+    ich1 = getc(ifP);
+    ich2 = getc(ifP);
+    if (ich1 == EOF || ich2 == EOF)
+        pm_error( "Error reading magic number from Netpbm image stream.  "
+                  "Most often, this "
+                  "means your input file is empty." );
+
+    return ich1 * 256 + ich2;
+}
+
+
+
+/* Read a file of unknown size to a buffer. Return the number of bytes
+   read. Allocate more memory as we need it. The calling routine has
+   to free() the buffer.
+
+   Oliver Trepte, oliver@fysik4.kth.se, 930613
+*/
+
+#define PM_BUF_SIZE 16384      /* First try this size of the buffer, then
+                                  double this until we reach PM_MAX_BUF_INC */
+#define PM_MAX_BUF_INC 65536   /* Don't allocate more memory in larger blocks
+                                  than this. */
+
+char *
+pm_read_unknown_size(FILE * const file, 
+                     long * const nread) {
+    long nalloc;
+    char * buf;
+    bool eof;
+
+    *nread = 0;
+    nalloc = PM_BUF_SIZE;
+    MALLOCARRAY(buf, nalloc);
+
+    eof = FALSE;  /* initial value */
+
+    while(!eof) {
+        int val;
+
+        if (*nread >= nalloc) { /* We need a larger buffer */
+            if (nalloc > PM_MAX_BUF_INC)
+                nalloc += PM_MAX_BUF_INC;
+            else
+                nalloc += nalloc;
+            REALLOCARRAY_NOFAIL(buf, nalloc);
+        }
+
+        val = getc(file);
+        if (val == EOF)
+            eof = TRUE;
+        else 
+            buf[(*nread)++] = val;
+    }
+    return buf;
+}
+
+
+
+union cheat {
+    uint32n l;
+    short s;
+    unsigned char c[4];
+};
+
+
+
+short
+pm_bs_short(short const s) {
+    union cheat u;
+    unsigned char t;
+
+    u.s = s;
+    t = u.c[0];
+    u.c[0] = u.c[1];
+    u.c[1] = t;
+    return u.s;
+}
+
+
+
+long
+pm_bs_long(long const l) {
+    union cheat u;
+    unsigned char t;
+
+    u.l = l;
+    t = u.c[0];
+    u.c[0] = u.c[3];
+    u.c[3] = t;
+    t = u.c[1];
+    u.c[1] = u.c[2];
+    u.c[2] = t;
+    return u.l;
+}
+
+
+
+void
+pm_tell2(FILE *       const fileP, 
+         void *       const fileposP,
+         unsigned int const fileposSize) {
+/*----------------------------------------------------------------------------
+   Return the current file position as *filePosP, which is a buffer
+   'fileposSize' bytes long.  Abort the program if error, including if
+   *fileP isn't a file that has a position.
+-----------------------------------------------------------------------------*/
+    /* Note: FTELLO() is either ftello() or ftell(), depending on the
+       capabilities of the underlying C library.  It is defined in
+       pm_config.h.  ftello(), in turn, may be either ftell() or
+       ftello64(), as implemented by the C library.
+    */
+    pm_filepos const filepos = FTELLO(fileP);
+    if (filepos < 0)
+        pm_error("ftello() to get current file position failed.  "
+                 "Errno = %s (%d)\n", strerror(errno), errno);
+
+    if (fileposSize == sizeof(pm_filepos)) {
+        pm_filepos * const fileposP_filepos = fileposP;
+        *fileposP_filepos = filepos;
+    } else if (fileposSize == sizeof(long)) {
+        if (sizeof(pm_filepos) > sizeof(long) &&
+            filepos >= (pm_filepos) 1 << (sizeof(long)*8))
+            pm_error("File size is too large to represent in the %u bytes "
+                     "that were provided to pm_tell2()", fileposSize);
+        else {
+            long * const fileposP_long = fileposP;
+            *fileposP_long = (long)filepos;
+        }
+    } else
+        pm_error("File position size passed to pm_tell() is invalid: %u.  "
+                 "Valid sizes are %u and %u", 
+                 fileposSize, (unsigned int)sizeof(pm_filepos),
+                 (unsigned int) sizeof(long));
+}
+
+
+
+unsigned int
+pm_tell(FILE * const fileP) {
+    
+    long filepos;
+
+    pm_tell2(fileP, &filepos, sizeof(filepos));
+
+    return filepos;
+}
+
+
+
+void
+pm_seek2(FILE *             const fileP, 
+         const pm_filepos * const fileposP,
+         unsigned int       const fileposSize) {
+/*----------------------------------------------------------------------------
+   Position file *fileP to position *fileposP.  Abort if error, including
+   if *fileP isn't a seekable file.
+-----------------------------------------------------------------------------*/
+    if (fileposSize == sizeof(pm_filepos)) 
+        /* Note: FSEEKO() is either fseeko() or fseek(), depending on the
+           capabilities of the underlying C library.  It is defined in
+           pm_config.h.  fseeko(), in turn, may be either fseek() or
+           fseeko64(), as implemented by the C library.
+        */
+        FSEEKO(fileP, *fileposP, SEEK_SET);
+    else if (fileposSize == sizeof(long)) {
+        long const fileposLong = *(long *)fileposP;
+        fseek(fileP, fileposLong, SEEK_SET);
+    } else
+        pm_error("File position size passed to pm_seek() is invalid: %u.  "
+                 "Valid sizes are %u and %u", 
+                 fileposSize, (unsigned int)sizeof(pm_filepos),
+                 (unsigned int) sizeof(long));
+}
+
+
+
+void
+pm_seek(FILE * const fileP, unsigned long filepos) {
+/*----------------------------------------------------------------------------
+-----------------------------------------------------------------------------*/
+
+    pm_filepos fileposBuff;
+
+    fileposBuff = filepos;
+
+    pm_seek2(fileP, &fileposBuff, sizeof(fileposBuff));
+}
+
+
+
+void
+pm_nextimage(FILE * const file, int * const eofP) {
+/*----------------------------------------------------------------------------
+   Position the file 'file' to the next image in the stream, assuming it is
+   now positioned just after the current image.  I.e. read off any white
+   space at the end of the current image's raster.  Note that the raw formats
+   don't permit such white space, but this routine tolerates it anyway, 
+   because the plain formats do permit white space after the raster.
+
+   Iff there is no next image, return *eofP == TRUE.
+
+   Note that in practice, we will not normally see white space here in
+   a plain PPM or plain PGM stream because the routine to read a
+   sample from the image reads one character of white space after the
+   sample in order to know where the sample ends.  There is not
+   normally more than one character of white space (a newline) after
+   the last sample in the raster.  But plain PBM is another story.  No white
+   space is required between samples of a plain PBM image.  But the raster
+   normally ends with a newline nonetheless.  Since the sample reading code
+   will not have read that newline, it is there for us to read now.
+-----------------------------------------------------------------------------*/
+    bool eof;
+    bool nonWhitespaceFound;
+
+    eof = FALSE;
+    nonWhitespaceFound = FALSE;
+
+    while (!eof && !nonWhitespaceFound) {
+        int c;
+        c = getc(file);
+        if (c == EOF) {
+            if (feof(file)) 
+                eof = TRUE;
+            else
+                pm_error("File error on getc() to position to image");
+        } else {
+            if (!isspace(c)) {
+                int rc;
+
+                nonWhitespaceFound = TRUE;
+
+                /* Have to put the non-whitespace character back in
+                   the stream -- it's part of the next image.  
+                */
+                rc = ungetc(c, file);
+                if (rc == EOF) 
+                    pm_error("File error doing ungetc() "
+                             "to position to image.");
+            }
+        }
+    }
+    *eofP = eof;
+}
+
+
+
+void
+pm_check(FILE *               const file, 
+         enum pm_check_type   const check_type, 
+         pm_filepos           const need_raster_size,
+         enum pm_check_code * const retval_p) {
+/*----------------------------------------------------------------------------
+   This is not defined for use outside of libnetpbm.
+-----------------------------------------------------------------------------*/
+    struct stat statbuf;
+    pm_filepos curpos;  /* Current position of file; -1 if none */
+    int rc;
+
+#ifdef LARGEFILEDEBUG
+    pm_message("pm_filepos received by pm_check() is %u bytes.",
+               sizeof(pm_filepos));
+#endif
+    /* Note: FTELLO() is either ftello() or ftell(), depending on the
+       capabilities of the underlying C library.  It is defined in
+       pm_config.h.  ftello(), in turn, may be either ftell() or
+       ftello64(), as implemented by the C library.
+    */
+    curpos = FTELLO(file);
+    if (curpos >= 0) {
+        /* This type of file has a current position */
+            
+        rc = fstat(fileno(file), &statbuf);
+        if (rc != 0) 
+            pm_error("fstat() failed to get size of file, though ftello() "
+                     "successfully identified\n"
+                     "the current position.  Errno=%s (%d)",
+                     strerror(errno), errno);
+        else if (!S_ISREG(statbuf.st_mode)) {
+            /* Not a regular file; we can't know its size */
+            if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
+        } else {
+            pm_filepos const have_raster_size = statbuf.st_size - curpos;
+            
+            if (have_raster_size < need_raster_size)
+                pm_error("File has invalid format.  The raster should "
+                         "contain %u bytes, but\n"
+                         "the file ends after only %u bytes.",
+                         (unsigned int) need_raster_size, 
+                         (unsigned int) have_raster_size);
+            else if (have_raster_size > need_raster_size) {
+                if (retval_p) *retval_p = PM_CHECK_TOO_LONG;
+            } else {
+                if (retval_p) *retval_p = PM_CHECK_OK;
+            }
+        }
+    } else
+        if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
+}
+
+
+
+void
+pm_drain(FILE *         const fileP,
+         unsigned int   const limit,
+         unsigned int * const bytesReadP) {
+/*----------------------------------------------------------------------------
+  Read bytes from *fileP until EOF and return as *bytesReadP how many there
+  were.
+
+  But don't read any more than 'limit'.
+
+  This is a good thing to call after reading an input file to be sure you
+  didn't leave some input behind, which could mean you didn't properly
+  interpret the file.
+-----------------------------------------------------------------------------*/
+    unsigned int bytesRead;
+    bool eof;
+
+    for (bytesRead = 0, eof = false; !eof && bytesRead < limit;) {
+
+        int rc;
+
+        rc = fgetc(fileP);
+
+        eof = (rc == EOF);
+        if (!eof)
+            ++bytesRead;
+    }
+    *bytesReadP = bytesRead;
+}
diff --git a/lib/pnm.h b/lib/pnm.h
index 94d4ff85..19727c4c 100644
--- a/lib/pnm.h
+++ b/lib/pnm.h
@@ -4,7 +4,8 @@
 #ifndef _PNM_H_
 #define _PNM_H_
 
-#include "ppm.h"
+#include <netpbm/pm.h>
+#include <netpbm/ppm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -28,7 +29,9 @@ typedef pixval xelval;
 
 /* Declarations of routines. */
 
-void pnm_init ARGS(( int* argcP, char* argv[] ));
+void
+pnm_init(int *   const argcP,
+         char ** const argv);
 
 void
 pnm_nextimage(FILE *file, int * const eofP);
@@ -36,7 +39,7 @@ pnm_nextimage(FILE *file, int * const eofP);
 xel *
 pnm_allocrow(unsigned int const cols);
 
-#define pnm_freerow(xelrow) free(xelrow)
+#define pnm_freerow(xelrow) pm_freerow(xelrow)
 
 #define pnm_allocarray( cols, rows ) \
   ((xel**) pm_allocarray( cols, rows, sizeof(xel) ))
diff --git a/lib/ppm.h b/lib/ppm.h
index 7f7d6446..604b9cb1 100644
--- a/lib/ppm.h
+++ b/lib/ppm.h
@@ -3,7 +3,8 @@
 #ifndef _PPM_H_
 #define _PPM_H_
 
-#include "pgm.h"
+#include <netpbm/pm.h>
+#include <netpbm/pgm.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -50,7 +51,7 @@ typedef struct {
 #define PPM_TYPE PPM_FORMAT
 
 
-#include "ppmcmap.h"
+#include <netpbm/ppmcmap.h>
 
 /* Macro for turning a format number into a type number. */
 
@@ -85,7 +86,7 @@ ppm_allocrow(unsigned int const cols);
 
 #define ppm_freearray(pixels, rows) pm_freearray((char**) pixels, rows)
 
-#define ppm_freerow(pixelrow) free(pixelrow);
+#define ppm_freerow(pixelrow) pm_freerow(pixelrow);
 
 pixel**
 ppm_readppm(FILE *   const fileP, 
diff --git a/lib/ppmcmap.h b/lib/ppmcmap.h
index b44dcbea..a39d27c0 100644
--- a/lib/ppmcmap.h
+++ b/lib/ppmcmap.h
@@ -89,13 +89,39 @@ void
 ppm_freecolorhash(colorhash_table const cht);
 
 
-colorhash_table ppm_colorrowtocolorhash ARGS((pixel *colorrow, int ncolors));
-pixel * ppm_computecolorrow ARGS((pixel **pixels, int cols, int rows, int maxcolors, int *ncolorsP));
-pixel * ppm_mapfiletocolorrow ARGS((FILE *file, int maxcolors, int *ncolorsP, pixval *maxvalP));
-void    ppm_colorrowtomapfile ARGS((FILE *ofp, pixel *colormap, int ncolors, pixval maxval));
-void    ppm_sortcolorrow (pixel * const colorrow, const int ncolors, 
-                          int (*cmpfunc)(pixel *, pixel *) );
-int     ppm_addtocolorrow ARGS((pixel *colorrow, int *ncolorsP, int maxcolors, pixel *pixelP));
+colorhash_table
+ppm_colorrowtocolorhash(pixel * const colorrow,
+                        int     const ncolors);
+
+pixel *
+ppm_computecolorrow(pixel ** const pixels,
+                    int      const cols,
+                    int      const rows,
+                    int      const maxcolors,
+                    int *    const ncolorsP);
+
+pixel *
+ppm_mapfiletocolorrow(FILE *   const fileP,
+                      int      const maxcolors,
+                      int *    const ncolorsP,
+                      pixval * const maxvalP);
+
+void
+ppm_colorrowtomapfile(FILE *  const ofP,
+                      pixel * const colormap,
+                      int     const ncolors,
+                      pixval  const maxval);
+
+void
+ppm_sortcolorrow(pixel * const colorrow,
+                 int     const ncolors, 
+                 int (*cmpfunc)(pixel *, pixel *));
+
+int
+ppm_addtocolorrow(pixel * const colorrow,
+                  int *   const ncolorsP,
+                  int     const maxcolors,
+                  pixel * const pixelP);
 
 int
 ppm_findclosestcolor(const pixel * const colormap, 
diff --git a/lib/util/Makefile b/lib/util/Makefile
index d770555f..49396010 100644
--- a/lib/util/Makefile
+++ b/lib/util/Makefile
@@ -17,7 +17,7 @@ all: $(UTILOBJECTS)
 
 include $(SRCDIR)/Makefile.common
 
-INCLUDES = -I $(BUILDDIR) -I $(SRCDIR)/$(SUBDIR)/..
+INCLUDES = -I$(SRCDIR)/$(SUBDIR) -I. -Iimportinc
 
 $(UTILOBJECTS):%.o:%.c importinc
 	$(CC) -c $(INCLUDES) -DNDEBUG $(CPPFLAGS) $(CFLAGS) $(CFLAGS_SHLIB) \
diff --git a/lib/util/mallocvar.h b/lib/util/mallocvar.h
index e5b7b1ea..b750e177 100644
--- a/lib/util/mallocvar.h
+++ b/lib/util/mallocvar.h
@@ -83,7 +83,7 @@ reallocProduct(void **      const blockP,
     arrayName = array; \
 } while (0)
 
-#define REALLOCARRAY(arrayName, nElements) { \
+#define REALLOCARRAY(arrayName, nElements) do { \
     void * array; \
     array = arrayName; \
     reallocProduct(&array, nElements, sizeof(arrayName[0])); \
diff --git a/lib/util/nstring.c b/lib/util/nstring.c
index feb8215e..0fa78c7a 100644
--- a/lib/util/nstring.c
+++ b/lib/util/nstring.c
@@ -915,3 +915,43 @@ strishex(const char * const subject) {
 
     return retval;
 }
+
+
+
+void
+interpret_uint(const char *   const string,
+               unsigned int * const valueP,
+               const char **  const errorP) {
+
+    if (string[0] == '\0')
+        asprintfN(errorP, "Null string.");
+    else {
+        /* strtoul() does a bizarre thing where if the number is out
+           of range, it returns a clamped value but tells you about it
+           by setting errno = ERANGE.  If it is not out of range,
+           strtoul() leaves errno alone.
+        */
+        char * tail;
+        unsigned long ulongValue;
+        
+        errno = 0;  /* So we can tell if strtoul() overflowed */
+
+        ulongValue = strtoul(string, &tail, 10);
+
+        if (tail[0] != '\0')
+            asprintfN(errorP, "Non-digit stuff in string: %s", tail);
+        else if (errno == ERANGE)
+            asprintfN(errorP, "Number too large");
+        else if (ulongValue > UINT_MAX)
+            asprintfN(errorP, "Number too large");
+        else if (string[0] == '-')
+            asprintfN(errorP, "Negative number");
+            /* Sleazy code; string may have leading spaces. */
+        else {
+            *valueP = ulongValue;
+            *errorP = NULL;
+        }
+    }
+}
+
+
diff --git a/lib/util/nstring.h b/lib/util/nstring.h
index 9d61cfa5..1e5ca4c3 100644
--- a/lib/util/nstring.h
+++ b/lib/util/nstring.h
@@ -183,6 +183,11 @@ memmemN(const void * const haystackArg,
 bool
 strishex(const char * const subject);
 
+void
+interpret_uint(const char *   const string,
+               unsigned int * const valueP,
+               const char **  const errorP);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/other/pamarith.c b/other/pamarith.c
index c1e7f1ed..d79f1e60 100644
--- a/other/pamarith.c
+++ b/other/pamarith.c
@@ -1,6 +1,9 @@
 #include <assert.h>
 #include <string.h>
+#include <limits.h>
 
+#include "pm_c_util.h"
+#include "mallocvar.h"
 #include "shhopt.h"
 #include "pam.h"
 
@@ -10,25 +13,59 @@ enum function {FN_ADD, FN_SUBTRACT, FN_MULTIPLY, FN_DIVIDE, FN_DIFFERENCE,
                FN_SHIFTLEFT, FN_SHIFTRIGHT
               };
 
+
+
+static bool
+isDyadic(enum function const function) {
+
+    bool retval;
+    
+    switch (function) {
+    case FN_ADD:
+    case FN_MEAN:
+    case FN_AND:
+    case FN_OR:
+    case FN_XOR:
+        retval = FALSE;
+        break;
+    case FN_SUBTRACT:
+    case FN_DIFFERENCE:
+    case FN_MINIMUM:
+    case FN_MAXIMUM:
+    case FN_COMPARE:
+    case FN_MULTIPLY:
+    case FN_DIVIDE:
+    case FN_NAND:
+    case FN_NOR:
+    case FN_SHIFTLEFT:
+    case FN_SHIFTRIGHT:
+        retval = TRUE;
+        break;
+    }
+    return retval;
+}
+
+
+
 struct cmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    const char *input1Filespec;  
-    const char *input2Filespec;  
     enum function function;
+    unsigned int operandCt;
+    const char ** operandFileNames;
 };
 
 
 
 static void
-parseCommandLine(int argc, char ** const argv,
+parseCommandLine(int argc, const char ** const argv,
                  struct cmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    Note that the file spec array we return is stored in the storage that
    was passed to us as the argv array.
 -----------------------------------------------------------------------------*/
-    optEntry *option_def = malloc(100*sizeof(optEntry));
+    optEntry * option_def;
         /* Instructions to OptParseOptions2 on how to parse our options.
          */
     optStruct3 opt;
@@ -41,6 +78,8 @@ parseCommandLine(int argc, char ** const argv,
         andSpec, orSpec, nandSpec, norSpec, xorSpec,
         shiftleftSpec, shiftrightSpec;
 
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
     option_def_index = 0;   /* incremented by OPTENTRY */
     OPTENT3(0, "add",         OPT_FLAG,   NULL, &addSpec,        0);
     OPTENT3(0, "subtract",    OPT_FLAG,   NULL, &subtractSpec,   0);
@@ -63,7 +102,7 @@ parseCommandLine(int argc, char ** const argv,
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
     opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
 
-    optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
+    optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
     if (addSpec + subtractSpec + multiplySpec + divideSpec + differenceSpec +
@@ -72,15 +111,6 @@ parseCommandLine(int argc, char ** const argv,
         shiftleftSpec + shiftrightSpec > 1)
         pm_error("You may specify only one function");
 
-    if (argc-1 != 2)
-        pm_error("You must specify two arguments:  the files which are "
-                 "the operands of the "
-                 "dyadic function.  You specified %d", argc-1);
-    else {
-        cmdlineP->input1Filespec = argv[1];
-        cmdlineP->input2Filespec = argv[2];
-    }
-
     if (addSpec)
         cmdlineP->function = FN_ADD;
     else if (subtractSpec)
@@ -115,6 +145,21 @@ parseCommandLine(int argc, char ** const argv,
         cmdlineP->function = FN_SHIFTRIGHT;
     else
         pm_error("You must specify a function (e.g. '-add')");
+
+    if (argc-1 < 2)
+        pm_error("You must specify at least two arguments: the files which "
+                 "are the operands of the function.  You specified %u",
+                 argc-1);
+    else {
+        if (isDyadic(cmdlineP->function) && argc-1 > 2)
+            pm_error("You specified %u arguments, but a dyadic function.  "
+                     "For a dyadic function, you must specify 2 arguments:  "
+                     "the operands of the function", argc-1);
+        else {
+            cmdlineP->operandCt = argc-1;
+            cmdlineP->operandFileNames = &argv[1];
+        }
+    }
 }        
 
 
@@ -220,43 +265,124 @@ computeOutputType(struct pam *  const outpamP,
 
 
 
+static sample
+samplenSum(samplen      const operands[],
+           unsigned int const operandCt) {
+
+    unsigned int i;
+    samplen total;
+
+    for (i = 1, total = operands[0]; i < operandCt; ++i) {
+        total += operands[1];
+        if (total > 1.)
+            total = 1.;
+    }
+    return total;
+}
+
+
+
+static sample
+samplenMin(samplen      const operands[],
+           unsigned int const operandCt) {
+
+    unsigned int i;
+    samplen min;
+
+    for (i = 1, min = operands[0]; i < operandCt; ++i) {
+        if (operands[i] < min)
+            min = operands[i];
+    }
+    return min;
+}
+
+
+
+static sample
+samplenMax(samplen      const operands[],
+           unsigned int const operandCt) {
+
+    unsigned int i;
+    samplen max;
+
+    for (i = 1, max = operands[0]; i < operandCt; ++i) {
+        if (operands[i] > max)
+            max = operands[i];
+    }
+    return max;
+}
+
+
+
+static sample
+samplenMean(samplen      const operands[],
+            unsigned int const operandCt) {
+
+    unsigned int i;
+    double sum;
+
+    for (i = 0, sum = 0.; i < operandCt; ++i)
+        sum += operands[i];
+
+    return sum / operandCt;
+}
+
+
+
+static sample
+samplenProduct(samplen      const operands[],
+               unsigned int const operandCt) {
+
+    unsigned int i;
+    double product;
+
+    for (i = 1, product = operands[0]; i < operandCt; ++i)
+        product *= operands[i];
+
+    return product;
+}
+
+
+
 static samplen
 applyNormalizedFunction(enum function const function,
-                        samplen       const leftArg,
-                        samplen       const rightArg) {
+                        samplen       const operands[],
+                        unsigned int  const operandCt) {
 
     samplen result;
 
     switch (function) {
     case FN_ADD:
-        result = MIN(1., leftArg + rightArg);
+        result = samplenSum(operands, operandCt);
         break;
     case FN_SUBTRACT:
-        result = MAX(0., leftArg - rightArg);
+        result = MAX(0., operands[0] - operands[1]);
         break;
     case FN_MULTIPLY:
-        result = leftArg * rightArg;
+        result = samplenProduct(operands, operandCt);
         break;
     case FN_DIVIDE:
-        result = (rightArg > leftArg) ?
-        leftArg / rightArg : 1.;
+        result = (operands[1] > operands[0]) ?
+        operands[0] / operands[1] : 1.;
         break;
     case FN_DIFFERENCE:
-        result = leftArg > rightArg ? 
-            leftArg - rightArg : rightArg - leftArg;
+        result = operands[0] > operands[1] ? 
+            operands[0] - operands[1] : operands[1] - operands[0];
         break;
     case FN_MINIMUM:
-        result = MIN(leftArg, rightArg);
+        result = samplenMin(operands, operandCt);
         break;
     case FN_MAXIMUM:
-        result = MAX(leftArg, rightArg);
+        result = samplenMax(operands, operandCt);
         break;
     case FN_MEAN:
-        result = (leftArg + rightArg) / 2.0;
+        result = samplenMean(operands, operandCt);
         break;
     case FN_COMPARE:
         result = 
-            leftArg > rightArg ? 1. : leftArg < rightArg ? 0. : .5;
+            operands[0] > operands[1] ?
+            1. : operands[0] < operands[1] ?
+            0. : .5;
         break;
     default:
         pm_error("Internal error.  applyNormalizedFunction() called "
@@ -292,13 +418,19 @@ doNormalizedArith(struct pam *  const inpam1P,
             unsigned int outplane;
             
             for (outplane = 0; outplane < outpamP->depth; ++outplane) {
+                unsigned int const operandCt = 2;
                 unsigned int const plane1 = MIN(outplane, inpam1P->depth-1);
                 unsigned int const plane2 = MIN(outplane, inpam2P->depth-1);
 
+                samplen * operands;
+
+                MALLOCARRAY_NOFAIL(operands, operandCt);
+
+                operands[0] = tuplerown1[col][plane1];
+                operands[1] = tuplerown2[col][plane2];
+
                 tuplerownOut[col][outplane] = 
-                    applyNormalizedFunction(function, 
-                                            tuplerown1[col][plane1], 
-                                            tuplerown2[col][plane2]);
+                    applyNormalizedFunction(function, operands, operandCt); 
                 assert(tuplerownOut[col][outplane] >= 0.);
                 assert(tuplerownOut[col][outplane] <= 1.);
 
@@ -315,70 +447,238 @@ doNormalizedArith(struct pam *  const inpam1P,
 
 
 static sample
+sampleSum(sample       const operands[],
+          unsigned int const operandCt,
+          sample       const maxval) {
+
+    unsigned int i;
+    sample total;
+
+    for (i = 1, total = operands[0]; i < operandCt; ++i) {
+        total += operands[i];
+        if (total > maxval)
+            total = maxval;
+    }
+    return total;
+}
+
+
+
+static sample
+sampleMin(sample       const operands[],
+          unsigned int const operandCt) {
+
+    unsigned int i;
+    sample min;
+
+    for (i = 1, min = operands[0]; i < operandCt; ++i) {
+        if (operands[i] < min)
+            min = operands[i];
+    }
+    return min;
+}
+
+
+
+static sample
+sampleMax(sample       const operands[],
+          unsigned int const operandCt) {
+
+    unsigned int i;
+    sample max;
+
+    for (i = 1, max = operands[0]; i < operandCt; ++i) {
+        if (operands[i] > max)
+            max = operands[i];
+    }
+    return max;
+}
+
+
+
+static sample
+sampleMean(sample       const operands[],
+           unsigned int const operandCt) {
+
+    unsigned int i;
+    unsigned int sum;
+
+    for (i = 0, sum = 0; i < operandCt; ++i) {
+        sum += operands[i];
+        if (UINT_MAX - operands[i] < sum)
+            pm_error("Arithmetic overflow adding samples for mean");
+    }
+    return (sum + operandCt/2) / operandCt;
+}
+
+
+
+static sample
+sampleProduct(sample       const operands[],
+              unsigned int const operandCt,
+              sample       const maxval) {
+
+    unsigned int i;
+    double product;
+
+    for (i = 0, product = 1.0; i < operandCt; ++i) {
+        product *= ((double)operands[0]/maxval);
+    }
+    return (sample)(product * maxval + 0.5);
+}
+
+
+
+static sample
+sampleAnd(sample       const operands[],
+          unsigned int const operandCt) {
+
+    unsigned int i;
+    sample accum;
+
+    for (i = 1, accum = operands[0]; i < operandCt; ++i) {
+        accum &= operands[i];
+    }
+    return accum;
+}
+
+
+
+static sample
+sampleOr(sample       const operands[],
+         unsigned int const operandCt) {
+
+    unsigned int i;
+    sample accum;
+
+    for (i = 1, accum = operands[0]; i < operandCt; ++i) {
+        accum |= operands[i];
+    }
+    return accum;
+}
+
+
+
+static sample
+sampleNand(sample       const operands[],
+           unsigned int const operandCt,
+           sample       const maxval) {
+
+    unsigned int i;
+    sample accum;
+
+    for (i = 1, accum = operands[0]; i < operandCt; ++i) {
+        accum &= operands[i];
+    }
+    return ~accum & maxval;
+}
+
+
+
+static sample
+sampleNor(sample       const operands[],
+          unsigned int const operandCt,
+          sample       const maxval) {
+
+    unsigned int i;
+    sample accum;
+
+    for (i = 1, accum = operands[0]; i < operandCt; ++i) {
+        accum |= operands[i];
+    }
+    return ~accum & maxval;
+}
+
+
+
+static sample
+sampleXor(sample       const operands[],
+          unsigned int const operandCt) {
+
+    unsigned int i;
+    sample accum;
+
+    for (i = 1, accum = operands[0]; i < operandCt; ++i) {
+        accum ^= operands[i];
+    }
+    return accum;
+}
+
+
+
+static sample
 applyUnNormalizedFunction(enum function const function,
-                          sample        const leftArg,
-                          sample        const rightArg,
+                          sample        const operands[],
+                          unsigned int  const operandCt,
                           sample        const maxval) {
 /*----------------------------------------------------------------------------
-   Apply dyadic function 'function' to the arguments 'leftArg' and
-   'rightArg', assuming both are based on the same maxval 'maxval'.
-   Return a value which is also a fraction of 'maxval'.
+   Apply function 'function' to the arguments operands[] (there are
+   'operandCt' of them), assuming both are based on the same maxval
+   'maxval'.  Return a value which is also a fraction of 'maxval'.
 
-   Exception: for the shift operations, 'rightArg' is not based on any
+   Exception: for the shift operations, operands[1] is not based on any
    maxval.  It is an absolute bit count.
+
+   We assume 'operandCount' is sensible for 'function'.  E.g. if
+   'function' is FN_DIFFERENCE, 'operandCt' is 2.
+
+   For a function that has a concept of left and right argument,
+   operands[0] is the left and operands[1] is the right.
 -----------------------------------------------------------------------------*/
     sample result;
 
     switch (function) {
     case FN_ADD:
-        result = MIN(maxval, leftArg + rightArg);
+        result = sampleSum(operands, operandCt, maxval);
         break;
     case FN_SUBTRACT:
-        result = MAX(0, (int)leftArg - (int)rightArg);
+        result = MAX(0, (int)operands[0] - (int)operands[1]);
         break;
     case FN_DIFFERENCE:
-        result = leftArg > rightArg ? leftArg - rightArg : rightArg - leftArg;
+        result = operands[0] > operands[1] ?
+            operands[0] - operands[1] : operands[1] - operands[0];
         break;
     case FN_MINIMUM:
-        result = MIN(leftArg, rightArg);
+        result = sampleMin(operands, operandCt);
         break;
     case FN_MAXIMUM:
-        result = MAX(leftArg, rightArg);
+        result = sampleMax(operands, operandCt);
         break;
     case FN_MEAN:
-        result = (leftArg + rightArg + 1) / 2;
+        result = sampleMean(operands, operandCt);
         break;
     case FN_COMPARE:
-        result = leftArg > rightArg ? 2 : leftArg < rightArg ? 0 : 1;
+        result = operands[0] > operands[1] ?
+            2 : operands[0] < operands[1] ? 0 : 1;
         break;
     case FN_MULTIPLY:
-        result = (leftArg * rightArg + maxval/2) / maxval;
+        result = sampleProduct(operands, operandCt, maxval);
         break;
     case FN_DIVIDE:
-        result = (rightArg > leftArg) ?
-            (leftArg * maxval + rightArg/2) / rightArg : maxval;
+        result = (operands[1] > operands[0]) ?
+            (operands[0] * maxval + operands[1]/2) / operands[1] : maxval;
         break;
 
     case FN_AND:
-        result = leftArg & rightArg;
+        result = sampleAnd(operands, operandCt);
         break;
     case FN_OR:
-        result = leftArg | rightArg;
+        result = sampleOr(operands, operandCt);
         break;
     case FN_NAND:
-        result = ~(leftArg & rightArg) & maxval;
+        result = sampleNand(operands, operandCt, maxval);
         break;
     case FN_NOR:
-        result = ~(leftArg | rightArg) & maxval;
+        result = sampleNor(operands, operandCt, maxval);
         break;
     case FN_XOR:
-        result = leftArg ^ rightArg;
+        result = sampleXor(operands, operandCt);
         break;
     case FN_SHIFTLEFT:
-        result = (leftArg << rightArg) & maxval;
+        result = (operands[0] << operands[1]) & maxval;
         break;
     case FN_SHIFTRIGHT:
-        result = leftArg >> rightArg;
+        result = operands[0] >> operands[1];
         break;
     default:
         pm_error("Internal error.  applyUnNormalizedFunction() called "
@@ -425,13 +725,19 @@ doUnNormalizedArith(struct pam *  const inpam1P,
             unsigned int outplane;
             
             for (outplane = 0; outplane < outpamP->depth; ++outplane) {
+                unsigned int const operandCt = 2;
                 unsigned int const plane1 = MIN(outplane, inpam1P->depth-1);
                 unsigned int const plane2 = MIN(outplane, inpam2P->depth-1);
 
+                sample * operands;
+
+                MALLOCARRAY_NOFAIL(operands, operandCt);
+
+                operands[0] = tuplerow1[col][plane1];
+                operands[1] = tuplerow2[col][plane2];
+
                 tuplerowOut[col][outplane] = 
-                    applyUnNormalizedFunction(function, 
-                                              tuplerow1[col][plane1], 
-                                              tuplerow2[col][plane2],
+                    applyUnNormalizedFunction(function, operands, operandCt,
                                               maxval);
 
                 assert(tuplerowOut[col][outplane] >= 0);
@@ -449,7 +755,7 @@ doUnNormalizedArith(struct pam *  const inpam1P,
 
 
 int
-main(int argc, char *argv[]) {
+main(int argc, const char *argv[]) {
 
     struct cmdlineInfo cmdline;
     struct pam inpam1;
@@ -458,12 +764,17 @@ main(int argc, char *argv[]) {
     FILE * if1P;
     FILE * if2P;
     
-    pnm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
-    if1P = pm_openr(cmdline.input1Filespec);
-    if2P = pm_openr(cmdline.input2Filespec);
+    if (cmdline.operandCt != 2)
+        /* Code for > 2 operands not written yet */
+        pm_error("You specified %u operands.  We understand only 2.",
+                 cmdline.operandCt);
+
+    if1P = pm_openr(cmdline.operandFileNames[0]);
+    if2P = pm_openr(cmdline.operandFileNames[1]);
 
     pnm_readpaminit(if1P, &inpam1, PAM_STRUCT_SIZE(tuple_type));
     pnm_readpaminit(if2P, &inpam2, PAM_STRUCT_SIZE(tuple_type));