about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2021-12-27 17:33:56 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2021-12-27 17:33:56 +0000
commit6e629f983aa205c3eaa5f339f1c71bb5e7938049 (patch)
treea89f594443ac2330138f0bd0f19ee59135c2213f
parentec52f41aabc9de9aac203c2f462252e403c7374a (diff)
downloadnetpbm-mirror-6e629f983aa205c3eaa5f339f1c71bb5e7938049.tar.gz
netpbm-mirror-6e629f983aa205c3eaa5f339f1c71bb5e7938049.tar.xz
netpbm-mirror-6e629f983aa205c3eaa5f339f1c71bb5e7938049.zip
Promote Development to Advanced to make Release 10.97.00
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@4222 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--converter/other/pamtogif.c42
-rw-r--r--converter/other/pamtopdbimg.c17
-rw-r--r--converter/other/pgmtoppm.c147
-rw-r--r--converter/other/sunicontopnm.c12
-rw-r--r--converter/other/xwdtopnm.c11
-rw-r--r--converter/other/yuy2topam.c8
-rw-r--r--converter/pbm/atktopbm.c34
-rw-r--r--converter/pbm/escp2topbm.c18
-rw-r--r--converter/pbm/g3topbm.c11
-rw-r--r--converter/pbm/mgrtopbm.c30
-rw-r--r--converter/pbm/pbmtoatk.c31
-rw-r--r--converter/pbm/pbmtoescp2.c23
-rw-r--r--converter/pbm/pbmtomgr.c14
-rw-r--r--converter/pbm/ybmtopbm.c6
-rw-r--r--converter/pgm/st4topgm.c6
-rw-r--r--doc/HISTORY40
-rw-r--r--editor/pambackground.c52
-rw-r--r--editor/pamenlarge.c1
-rw-r--r--editor/pamrubber.c390
-rw-r--r--editor/pbmclean.c8
-rw-r--r--editor/pbmpscale.c30
-rw-r--r--editor/pnmcat.c5
-rw-r--r--editor/pnmpad.c22
-rw-r--r--editor/specialty/pampaintspill.c136
-rw-r--r--generator/Makefile2
-rw-r--r--generator/pbmnoise.c484
-rw-r--r--generator/pbmtext.c2
-rw-r--r--generator/ppmmake.c4
-rw-r--r--generator/ppmpat.c202
-rw-r--r--lib/libpam.c33
-rw-r--r--lib/libpamwrite.c205
-rw-r--r--lib/libpbm2.c4
-rw-r--r--lib/libpbmfont1.c63
-rw-r--r--lib/libpm.c3
-rw-r--r--lib/libpnm2.c61
-rw-r--r--lib/pam.h26
-rw-r--r--lib/pbm.h13
-rw-r--r--lib/pm.h3
-rw-r--r--lib/pmfileio.c112
-rw-r--r--lib/util/rand.c43
-rw-r--r--lib/util/rand.h5
-rwxr-xr-xtest/Execute-Tests9
-rw-r--r--test/Test-Order6
-rw-r--r--test/all-in-place.ok1
-rwxr-xr-xtest/all-in-place.test1
-rw-r--r--test/atk-roundtrip.ok8
-rwxr-xr-xtest/atk-roundtrip.test22
-rw-r--r--test/bmp-roundtrip.ok14
-rwxr-xr-xtest/bmp-roundtrip.test14
-rw-r--r--test/cis-roundtrip.ok5
-rwxr-xr-xtest/cis-roundtrip.test16
-rw-r--r--test/cmuw-roundtrip.ok3
-rwxr-xr-xtest/cmuw-roundtrip.test5
-rw-r--r--test/cut-cat-roundtrip.ok146
-rwxr-xr-xtest/cut-cat-roundtrip.test170
-rwxr-xr-xtest/cut-paste-roundtrip.test1
-rw-r--r--test/g3-roundtrip.ok9
-rwxr-xr-xtest/g3-roundtrip.test43
-rw-r--r--test/gem-roundtrip.ok2
-rwxr-xr-xtest/gem-roundtrip.test4
-rw-r--r--test/gif-roundtrip.ok24
-rwxr-xr-xtest/gif-roundtrip.test82
-rw-r--r--test/gif-transparent1.ok99
-rwxr-xr-xtest/gif-transparent1.test224
-rw-r--r--test/ilbm-roundtrip.ok12
-rwxr-xr-xtest/ilbm-roundtrip.test19
-rw-r--r--test/jbig-roundtrip.ok2
-rwxr-xr-xtest/jbig-roundtrip.test4
-rwxr-xr-xtest/lookup-roundtrip.test2
-rw-r--r--test/lps-roundtrip.ok1
-rwxr-xr-xtest/lps-roundtrip.test17
-rw-r--r--test/macp-roundtrip.ok13
-rwxr-xr-xtest/macp-roundtrip.test44
-rw-r--r--test/maze.pbmbin0 -> 481 bytes
-rw-r--r--test/mda-roundtrip.ok5
-rwxr-xr-xtest/mda-roundtrip.test12
-rw-r--r--test/mgr-roundtrip.ok8
-rwxr-xr-xtest/mgr-roundtrip.test14
-rw-r--r--test/mrf-roundtrip.ok2
-rwxr-xr-xtest/mrf-roundtrip.test4
-rw-r--r--test/pad-crop-roundtrip.ok4
-rwxr-xr-xtest/pad-crop-roundtrip.test12
-rw-r--r--test/palm-roundtrip.ok14
-rwxr-xr-xtest/palm-roundtrip.test10
-rw-r--r--test/pamarith-compare-equal.ok32
-rwxr-xr-xtest/pamarith-compare-equal.test45
-rw-r--r--test/pamarith.ok22
-rwxr-xr-xtest/pamarith.test9
-rw-r--r--test/pamchannel.ok8
-rwxr-xr-xtest/pamchannel.test25
-rwxr-xr-xtest/pamcrater.test10
-rw-r--r--test/pamcut.ok22
-rwxr-xr-xtest/pamcut.test68
-rw-r--r--test/pamdepth-roundtrip.ok2
-rwxr-xr-xtest/pamdepth-roundtrip.test2
-rwxr-xr-xtest/pamdepth.test4
-rw-r--r--test/pamdice-roundtrip.ok22
-rwxr-xr-xtest/pamdice-roundtrip.test52
-rwxr-xr-xtest/pamditherbw.test24
-rwxr-xr-xtest/pamenlarge-pamscale-point.test4
-rwxr-xr-xtest/pamenlarge-pbm.test2
-rw-r--r--test/pamenlarge.ok8
-rwxr-xr-xtest/pamenlarge.test13
-rwxr-xr-xtest/pamfile.test6
-rwxr-xr-xtest/pamfind.test8
-rwxr-xr-xtest/pamfix.test4
-rw-r--r--test/pamflip-roundtrip.ok71
-rwxr-xr-xtest/pamflip-roundtrip.test133
-rw-r--r--test/pamflip1.ok13
-rwxr-xr-xtest/pamflip1.test24
-rw-r--r--test/pamflip2.ok9
-rwxr-xr-xtest/pamflip2.test14
-rw-r--r--test/pamfunc.ok14
-rwxr-xr-xtest/pamfunc.test14
-rwxr-xr-xtest/pamgauss.test10
-rwxr-xr-xtest/pamhue.test8
-rwxr-xr-xtest/pamscale-reportonly.test12
-rwxr-xr-xtest/pamseq.test8
-rw-r--r--test/pamslice-roundtrip.ok13
-rwxr-xr-xtest/pamslice-roundtrip.test78
-rwxr-xr-xtest/pamstretch.test7
-rw-r--r--test/pamsumm.ok9
-rwxr-xr-xtest/pamsumm.test19
-rw-r--r--test/pamtopam.ok40
-rwxr-xr-xtest/pamtopam.test21
-rwxr-xr-xtest/pamtopdbimg.test6
-rw-r--r--test/pamundice.ok16
-rwxr-xr-xtest/pamundice.test58
-rw-r--r--test/pbm-ppm-roundtrip.ok39
-rwxr-xr-xtest/pbm-ppm-roundtrip.test34
-rwxr-xr-xtest/pbmclean.test2
-rwxr-xr-xtest/pbmmake.test16
-rw-r--r--test/pbmminkowski.ok8
-rwxr-xr-xtest/pbmminkowski.test7
-rw-r--r--test/pbmnoise-parameters.ok26
-rwxr-xr-xtest/pbmnoise-parameters.test143
-rw-r--r--test/pbmnoise1.ok50
-rwxr-xr-xtest/pbmnoise1.test36
-rw-r--r--test/pbmnoise2.ok19
-rwxr-xr-xtest/pbmnoise2.test85
-rwxr-xr-xtest/pbmpage.test6
-rw-r--r--test/pbmpscale.ok223
-rwxr-xr-xtest/pbmpscale.test45
-rwxr-xr-xtest/pbmtext-bdf.test2
-rw-r--r--test/pbmtog3.ok18
-rwxr-xr-xtest/pbmtog3.test37
-rwxr-xr-xtest/pbmtopgm.test8
-rwxr-xr-xtest/pbmupc.test18
-rwxr-xr-xtest/pgmhist.test8
-rwxr-xr-xtest/pgmmake.test16
-rwxr-xr-xtest/pgmminkowski.test4
-rwxr-xr-xtest/pgmnoise-parameters.test10
-rwxr-xr-xtest/pgmnoise.test22
-rw-r--r--test/pgmtoppm.ok36
-rwxr-xr-xtest/pgmtoppm.test137
-rw-r--r--test/pi3-roundtrip.ok5
-rwxr-xr-xtest/pi3-roundtrip.test15
-rwxr-xr-xtest/pj-roundtrip.test4
-rw-r--r--test/png-roundtrip.ok36
-rwxr-xr-xtest/png-roundtrip.test4
-rw-r--r--test/png-roundtrip2.ok4
-rwxr-xr-xtest/png-roundtrip2.test4
-rw-r--r--test/pnm-pam-roundtrip.ok2
-rwxr-xr-xtest/pnm-pam-roundtrip.test2
-rw-r--r--test/pnm-plain-roundtrip.ok2
-rwxr-xr-xtest/pnm-plain-roundtrip.test2
-rw-r--r--test/pnmcat.ok84
-rwxr-xr-xtest/pnmcat.test119
-rwxr-xr-xtest/pnmcolormap.test20
-rw-r--r--test/pnminvert-roundtrip.ok2
-rwxr-xr-xtest/pnminvert-roundtrip.test2
-rw-r--r--test/pnminvert.ok7
-rwxr-xr-xtest/pnminvert.test17
-rwxr-xr-xtest/pnmpsnr.test8
-rwxr-xr-xtest/pnmquant.test16
-rwxr-xr-xtest/pnmremap1.test12
-rwxr-xr-xtest/pnmtile.test8
-rwxr-xr-xtest/ppmbrighten.test3
-rw-r--r--test/ppmchange-roundtrip.ok4
-rwxr-xr-xtest/ppmchange-roundtrip.test4
-rwxr-xr-xtest/ppmforge-parameters.test14
-rwxr-xr-xtest/ppmhist.test6
-rwxr-xr-xtest/ppmmake.test20
-rw-r--r--test/ppmpat.ok20
-rwxr-xr-xtest/ppmpat.test96
-rwxr-xr-xtest/ppmtoarbtxt-roundtrip.test3
-rw-r--r--test/ppmtoppm.ok24
-rwxr-xr-xtest/ppmtoppm.test13
-rw-r--r--test/ppmwheel.ok1
-rwxr-xr-xtest/ppmwheel.test17
-rw-r--r--test/ps-alt-roundtrip.ok4
-rwxr-xr-xtest/ps-alt-roundtrip.test11
-rw-r--r--test/ps-flate-roundtrip.ok4
-rwxr-xr-xtest/ps-flate-roundtrip.test13
-rw-r--r--test/ps-roundtrip.ok9
-rwxr-xr-xtest/ps-roundtrip.test18
-rwxr-xr-xtest/random-generator.test2
-rw-r--r--test/rgb3-roundtrip.ok8
-rwxr-xr-xtest/rgb3-roundtrip.test34
-rw-r--r--test/sgi-roundtrip.ok13
-rwxr-xr-xtest/sgi-roundtrip.test29
-rw-r--r--test/sunicon-roundtrip.ok5
-rwxr-xr-xtest/sunicon-roundtrip.test25
-rw-r--r--test/targa-roundtrip.ok30
-rwxr-xr-xtest/targa-roundtrip.test26
-rw-r--r--test/tiff-flate-lzw-roundtrip.ok4
-rwxr-xr-xtest/tiff-flate-lzw-roundtrip.test12
-rw-r--r--test/tiff-roundtrip.ok20
-rwxr-xr-xtest/tiff-roundtrip.test30
-rw-r--r--test/wbmp-roundtrip.ok2
-rwxr-xr-xtest/wbmp-roundtrip.test4
-rw-r--r--test/winicon-roundtrip.ok14
-rwxr-xr-xtest/winicon-roundtrip.test9
-rwxr-xr-xtest/winicon-roundtrip2.test3
-rw-r--r--test/xbm-roundtrip.ok5
-rwxr-xr-xtest/xbm-roundtrip.test5
-rw-r--r--test/xpm-roundtrip.ok2
-rwxr-xr-xtest/xpm-roundtrip.test2
-rw-r--r--test/xwd-roundtrip.ok5
-rwxr-xr-xtest/xwd-roundtrip.test11
-rw-r--r--version.mk4
221 files changed, 4593 insertions, 1880 deletions
diff --git a/converter/other/pamtogif.c b/converter/other/pamtogif.c
index f5bdbf4b..341c9c03 100644
--- a/converter/other/pamtogif.c
+++ b/converter/other/pamtogif.c
@@ -1892,14 +1892,14 @@ computeColormapFromInput(struct pam *   const pamP,
 
 
 static void
-computeLibnetpbmColormap(struct pam *   const pamP,
-                         bool           const haveAlpha,
-                         const char *   const mapfile,
-                         tuple *        const color,
-                         tuplehash *    const tuplehashP,
-                         struct pam *   const mapPamP,
-                         unsigned int * const colorCountP,
-                         bool           const sort) {
+computeLibnetpbmColormap(struct pam *          const pamP,
+                         enum TransparencyType const transType,
+                         const char *          const mapfile,
+                         tuple *               const color,
+                         tuplehash *           const tuplehashP,
+                         struct pam *          const mapPamP,
+                         unsigned int *        const colorCountP,
+                         bool                  const sort) {
 /*----------------------------------------------------------------------------
    Compute a colormap, libnetpbm style, for the image described by
    'pamP', which is positioned to the raster.
@@ -1913,21 +1913,23 @@ computeLibnetpbmColormap(struct pam *   const pamP,
    The tuples of the color map have a meaningful depth of 1 (grayscale) or 3
    (color) and *mapPamP reflects that.
 
-   While we're at it, count the colors and validate that there aren't
-   too many.  Return the count as *colorCountP.  In determining if there are
-   too many, allow one slot for a fake transparency color if 'haveAlpha'
-   is true.  If there are too many, issue an error message and abort the
+   While we're at it, count the colors and validate that there aren't too
+   many.  Return the count as *colorCountP.  In determining if there are too
+   many, allow one slot for a fake transparency color if 'transType' is
+   'TRANS_ALPHA'.  If there are too many, issue an error message and abort the
    program.
 
    'sort' means to sort the colormap by red intensity, then by green
    intensity, then by blue intensity, as opposed to arbitrary order.
 -----------------------------------------------------------------------------*/
-    unsigned int const maxcolors = haveAlpha ? MAXCMAPSIZE - 1 : MAXCMAPSIZE;
+    unsigned int const maxcolors =
+        transType == TRANS_ALPHA ? MAXCMAPSIZE - 1 : MAXCMAPSIZE;
         /* The most colors we can tolerate in the image.  If we have
            our own made-up entry in the colormap for transparency, it
            isn't included in this count.
         */
-    unsigned int const nInputComp = haveAlpha ? pamP->depth - 1 : pamP->depth;
+    unsigned int const nInputComp =
+        pamAlphaPlane(pamP) ? pamP->depth - 1 : pamP->depth;
         /* Number of color components (not alpha) in the input image */
 
     unsigned int i;
@@ -2010,16 +2012,16 @@ main(int argc, char *argv[]) {
 
     pm_tell2(ifP, &rasterPos, sizeof(rasterPos));
 
-    computeLibnetpbmColormap(&pam, !!pamAlphaPlane(&pam), cmdline.mapfile,
+    transType = cmdline.transparent ? TRANS_COLOR :
+        pamAlphaPlane(&pam) ? TRANS_ALPHA :
+        TRANS_NONE;
+
+    computeLibnetpbmColormap(&pam, transType, cmdline.mapfile,
                              cmap.color, &cmap.tuplehash,
                              &cmap.pam, &cmap.cmapSize, cmdline.sort);
 
     assert(cmap.pam.maxval == pam.maxval);
 
-    transType = cmdline.transparent ? TRANS_COLOR :
-        pamAlphaPlane(&pam) ? TRANS_ALPHA :
-        TRANS_NONE;
-
     if (transType == TRANS_ALPHA) {
         /* Add a fake entry to the end of the colormap for transparency.
            Make its color black.
@@ -2035,7 +2037,7 @@ main(int argc, char *argv[]) {
     gifEncode(&pam, stdout, rasterPos,
               cmdline.interlace, 0, bitsPerPixel, &cmap, cmdline.comment,
               cmdline.aspect, !cmdline.nolzw, cmdline.noclear,
-              !cmdline.transparent);
+              transType==TRANS_ALPHA);
 
     destroyCmap(&cmap);
 
diff --git a/converter/other/pamtopdbimg.c b/converter/other/pamtopdbimg.c
index ce2f7659..4823686f 100644
--- a/converter/other/pamtopdbimg.c
+++ b/converter/other/pamtopdbimg.c
@@ -63,7 +63,7 @@ parseCommandLine(int argc, const char ** argv,
                  struct cmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    parse program command line described in Unix standard form by argc
-   and argv.  Return the information in the options as *cmdlineP.  
+   and argv.  Return the information in the options as *cmdlineP.
 
    If command line is internally inconsistent (invalid options, etc.),
    issue error message to stderr and abort program.
@@ -110,7 +110,7 @@ parseCommandLine(int argc, const char ** argv,
 
     if (!notefileSpec)
         cmdlineP->notefile = NULL;
-    
+
     if (compressed + uncompressed + maybeCompressed > 1)
         pm_error("You may specify only one of -compressed, -uncompressed, "
                  "-maybecompressed");
@@ -232,7 +232,7 @@ imageWrite(IMAGE *   const imgP,
 static int
 textWrite(TEXT * const textP,
           FILE * const fileP) {
-    
+
     if (textP)
         fwrite(textP->data, 1, strlen(textP->data), fileP);
 
@@ -303,7 +303,7 @@ ipdbWrite(IPDB * const pdbP,
     rc = pdbheadWrite(pdbP->p, fileP);
     if (rc != 0)
         pm_error("Failed to write PDB header.  %s", ipdb_err(rc));
-            
+
     rc = rechdrWrite(irP, fileP);
     if (rc != 0)
         pm_error("Failed to write image record header.  %s", ipdb_err(rc));
@@ -513,7 +513,7 @@ insertG16image(IPDB *          const pdbP,
         /* Pad with white on the bottom */
         for (; row < ipdb_height(pdbP); ++row)
             memset(outP, 0, rowSize);
-    } 
+    }
 }
 
 
@@ -544,7 +544,7 @@ insertGimage(IPDB *          const pdbP,
         /* Pad with white on the bottom */
         for (; row < ipdb_height(pdbP); ++row)
             memset(outP, 0, rowSize);
-    } 
+    }
 }
 
 
@@ -575,7 +575,7 @@ insertMimage(IPDB *          const pdbP,
         /* Pad with white on the bottom */
         for (; row < ipdb_height(pdbP); ++row)
             memset(outP, 0, rowSize);
-    } 
+    }
 }
 
 
@@ -730,3 +730,6 @@ main(int argc, const char **argv) {
 
     return EXIT_SUCCESS;
 }
+
+
+
diff --git a/converter/other/pgmtoppm.c b/converter/other/pgmtoppm.c
index 62a388c2..dea6c4ca 100644
--- a/converter/other/pgmtoppm.c
+++ b/converter/other/pgmtoppm.c
@@ -17,24 +17,27 @@
 
 #include "pm_c_util.h"
 #include "mallocvar.h"
+#include "nstring.h"
 #include "shhopt.h"
 #include "ppm.h"
 
-struct cmdlineInfo {
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
     const char * inputFilename;  /* '-' if stdin */
     const char * map;
-    const char * colorBlack;
-    const char * colorWhite;
+    const char * colorBlack;  /* malloc'ed */
+        /* The color to which the user says to map black */
+    const char * colorWhite;  /* malloc'ed */
+        /* The color to which the user says to map white */
 };
 
 
 
 static void
-parseCommandLine(int argc, char ** argv,
-                 struct cmdlineInfo * const cmdlineP) {
+parseCommandLine(int argc, const char ** argv,
+                 struct CmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    parse program command line described in Unix standard form by argc
    and argv.  Return the information in the options as *cmdlineP.
@@ -52,11 +55,18 @@ parseCommandLine(int argc, char ** argv,
 
     unsigned int option_def_index;
 
-    unsigned int mapSpec;
+    unsigned int blackSpec, whiteSpec, mapSpec;
+
+    const char * blackOpt;
+    const char * whiteOpt;
 
     MALLOCARRAY_NOFAIL(option_def, 100);
 
     option_def_index = 0;   /* incremented by OPTENT3 */
+    OPTENT3(0, "black",          OPT_STRING,    &blackOpt,
+            &blackSpec,          0);
+    OPTENT3(0, "white",          OPT_STRING,    &whiteOpt,
+            &whiteSpec,          0);
     OPTENT3(0, "map",            OPT_STRING,    &cmdlineP->map,
             &mapSpec,            0);
 
@@ -64,13 +74,20 @@ parseCommandLine(int argc, char ** argv,
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
     opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
 
-    pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
+    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
     if (!mapSpec)
         cmdlineP->map = NULL;
 
     if (mapSpec) {
+        if (blackSpec || whiteSpec)
+            pm_error("You may not specify -black or -white "
+                     "together with -map");
+
+        cmdlineP->colorBlack = NULL;
+        cmdlineP->colorWhite = NULL;
+
         /* No color argument; only argument is file name */
         if (argc-1 < 1)
             cmdlineP->inputFilename = "-";
@@ -81,41 +98,77 @@ parseCommandLine(int argc, char ** argv,
                          "the file name.  You specified %u", argc-1);
         }
     } else {
-        /* Arguments are color or color range and file name */
-        /* For defaults, we use "rgbi:..." instead of the simpler "black"
-           and "white" so that we don't have unnecessary dependency on a
-           color dictionary being available.
+        /* For default colors, we use "rgbi:..." instead of the simpler
+           "black" and "white" so that we don't have an unnecessary dependency
+           on a color dictionary being available.
         */
-        if (argc-1 < 1) {
-            cmdlineP->colorBlack = "rgbi:0/0/0";
-            cmdlineP->colorWhite = "rgbi:1/1/1";
+        if (blackSpec || whiteSpec) {
+            cmdlineP->colorBlack =
+                pm_strdup(blackSpec ? blackOpt : "rgbi:0/0/0");
+            cmdlineP->colorWhite =
+                pm_strdup(whiteSpec ? whiteOpt : "rgbi:1/1/1");
+
+            /* The only possibly argument is input file name */
+            if (argc-1 < 1)
+                cmdlineP->inputFilename = "-";
+            else {
+                cmdlineP->inputFilename = argv[1];
+                if (argc-1 > 1)
+                    pm_error("Whten you specify -black or -white, "
+                             "there can be at most one non-option arguments:  "
+                             "the file name.  "
+                             "You specified %u", argc-1);
+            }
         } else {
-            char * buffer = strdup(argv[1]);
-            char * hyphenPos = strchr(buffer, '-');
-            if (hyphenPos) {
-                *hyphenPos = '\0';
-                cmdlineP->colorBlack = buffer;
-                cmdlineP->colorWhite = hyphenPos+1;
+            /* Arguments are color or color range and optional file name */
+
+            if (argc-1 < 1) {
+                cmdlineP->colorBlack = pm_strdup("rgbi:0/0/0");
+                cmdlineP->colorWhite = pm_strdup("rgbi:1/1/1");
             } else {
-                cmdlineP->colorBlack = "rgbi:0/0/0";
-                cmdlineP->colorWhite = buffer;
+                char * buffer = strdup(argv[1]);
+                if (!buffer)
+                    pm_error("Out of memory allocating tiny buffer");
+                char * hyphenPos = strchr(buffer, '-');
+                if (hyphenPos) {
+                    *hyphenPos = '\0';
+                    cmdlineP->colorBlack = pm_strdup(buffer);
+                    cmdlineP->colorWhite = pm_strdup(hyphenPos+1);
+                } else {
+                    cmdlineP->colorBlack = pm_strdup("rgbi:0/0/0");
+                    cmdlineP->colorWhite = pm_strdup(buffer);
+                }
+                free(buffer);
             }
-        }
-        if (argc-1 < 2)
-            cmdlineP->inputFilename = "-";
-        else
-            cmdlineP->inputFilename = argv[2];
 
-        if (argc-1 > 2)
-            pm_error("Program takes at most 2 arguments:  "
-                     "color name/range and input file name.  "
-                     "You specified %u", argc-1);
+            if (argc-1 < 2)
+                cmdlineP->inputFilename = "-";
+            else {
+                cmdlineP->inputFilename = argv[2];
+
+                if (argc-1 > 2)
+                    pm_error("Program takes at most 2 arguments:  "
+                             "color name/range and input file name.  "
+                             "You specified %u", argc-1);
+            }
+        }
     }
 }
 
 
 
 static void
+freeCommandLine(struct CmdlineInfo const cmdline) {
+
+    if (cmdline.colorBlack)
+        pm_strfree(cmdline.colorBlack);
+    if (cmdline.colorWhite)
+        pm_strfree(cmdline.colorWhite);
+}
+
+
+
+static void
 convertWithMap(FILE * const ifP,
                unsigned int const cols,
                unsigned int const rows,
@@ -172,21 +225,19 @@ convertLinear(FILE * const ifP,
               gray *       const grayrow,
               pixel *      const pixelrow) {
 
-    pixel colorBlack, colorWhite;
-    pixval red0, grn0, blu0, red1, grn1, blu1;
-    unsigned int row;
+    pixel const colorBlack = ppm_parsecolor(colorNameBlack, maxval);
+    pixel const colorWhite = ppm_parsecolor(colorNameWhite, maxval);
 
-    ppm_writeppminit(ofP, cols, rows, maxval, 0);
+    pixval const red0 = PPM_GETR(colorBlack);
+    pixval const grn0 = PPM_GETG(colorBlack);
+    pixval const blu0 = PPM_GETB(colorBlack);
+    pixval const red1 = PPM_GETR(colorWhite);
+    pixval const grn1 = PPM_GETG(colorWhite);
+    pixval const blu1 = PPM_GETB(colorWhite);
 
-    colorBlack = ppm_parsecolor(colorNameBlack, maxval);
-    colorWhite = ppm_parsecolor(colorNameWhite, maxval);
+    unsigned int row;
 
-    red0 = PPM_GETR(colorBlack);
-    grn0 = PPM_GETG(colorBlack);
-    blu0 = PPM_GETB(colorBlack);
-    red1 = PPM_GETR(colorWhite);
-    grn1 = PPM_GETG(colorWhite);
-    blu1 = PPM_GETB(colorWhite);
+    ppm_writeppminit(ofP, cols, rows, maxval, 0);
 
     for (row = 0; row < rows; ++row) {
         unsigned int col;
@@ -208,17 +259,17 @@ convertLinear(FILE * const ifP,
 
 
 int
-main(int    argc,
-     char * argv[]) {
+main(int          argc,
+     const char * argv[]) {
 
     FILE * ifP;
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
     gray * grayrow;
     pixel * pixelrow;
     int rows, cols, format;
     gray maxval;
 
-    ppm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
@@ -240,6 +291,8 @@ main(int    argc,
     pgm_freerow(grayrow);
     pm_close(ifP);
 
+    freeCommandLine(cmdline);
+
     /* If the program failed, it previously aborted with nonzero completion
        code, via various function calls.
     */
diff --git a/converter/other/sunicontopnm.c b/converter/other/sunicontopnm.c
index db26663e..93ddc0ab 100644
--- a/converter/other/sunicontopnm.c
+++ b/converter/other/sunicontopnm.c
@@ -30,9 +30,9 @@
 
 
 static void
-ReadIconFileHeader(FILE * const file, 
-                   int *  const widthP, 
-                   int *  const heightP, 
+ReadIconFileHeader(FILE * const file,
+                   int *  const widthP,
+                   int *  const heightP,
                    int *  const depthP,
                    int *  const bitsPerItemP) {
 
@@ -51,7 +51,7 @@ ReadIconFileHeader(FILE * const file,
                 ch == ' ')
             ;
         for (i = 0;
-             ch != '=' && ch != ',' && ch != '\n' && ch != '\t' && 
+             ch != '=' && ch != ',' && ch != '\n' && ch != '\t' &&
                  ch != ' ' && (i < (sizeof(variable) - 1));
              ++i) {
             variable[i] = ch;
@@ -84,7 +84,7 @@ ReadIconFileHeader(FILE * const file,
         } else if (streq(variable, "Valid_bits_per_item")) {
             if (value != 16 && value !=32)
                 pm_error("invalid Valid_bits_per_item");
-            *bitsPerItemP = value; 
+            *bitsPerItemP = value;
             ++fieldCt;
         }
     }
@@ -165,7 +165,7 @@ main(int argc, const char ** argv) {
                 else
                     grayrow[colChar] = data;
             } else
-                pm_error("error scanning bits item %u" , colChar);
+                pm_error("error scanning bits item %u", colChar);
         }
 
         /* output row */
diff --git a/converter/other/xwdtopnm.c b/converter/other/xwdtopnm.c
index a99768b8..4eb16def 100644
--- a/converter/other/xwdtopnm.c
+++ b/converter/other/xwdtopnm.c
@@ -549,10 +549,10 @@ processX11Header(X11WDFileHeader *  const h11P,
             pm_error("couldn't read rest of X11 XWD file header");
 
     /* Check whether we can handle this dump. */
-    if (h11FixedP->pixmap_depth > 24)
-        pm_error( "can't handle X11 pixmap_depth > 24");
-    if (h11FixedP->bits_per_rgb > 24)
-        pm_error("can't handle X11 bits_per_rgb > 24");
+    if (h11FixedP->pixmap_depth > 32)
+        pm_error( "can't handle X11 pixmap_depth > 32");
+    if (h11FixedP->bits_per_rgb > 32)
+        pm_error("can't handle X11 bits_per_rgb > 32");
     if (h11FixedP->pixmap_format != ZPixmap && h11FixedP->pixmap_depth != 1)
         pm_error("can't handle X11 pixmap_format %d with depth != 1",
                  h11FixedP->pixmap_format);
@@ -593,6 +593,9 @@ processX11Header(X11WDFileHeader *  const h11P,
         *formatP = PPM_TYPE;
 
         /* See discussion above about this maxval */
+        if (h11FixedP->bits_per_rgb > 16)
+            pm_error("Invalid bits_per_rgb for TrueColor image: %u. "
+                     "Maximum possible is 16", h11FixedP->bits_per_rgb);
         *maxvalP = pm_bitstomaxval(h11FixedP->bits_per_rgb);
     } else if (*visualclassP == StaticGray && h11FixedP->bits_per_pixel == 1) {
         *formatP = PBM_TYPE;
diff --git a/converter/other/yuy2topam.c b/converter/other/yuy2topam.c
index 40ab98b3..49f284ac 100644
--- a/converter/other/yuy2topam.c
+++ b/converter/other/yuy2topam.c
@@ -31,12 +31,12 @@ struct CmdlineInfo {
 
 
 
-static void 
-parseCommandLine(int argc, const char ** argv, 
+static void
+parseCommandLine(int argc, const char ** argv,
                  struct CmdlineInfo * const cmdlineP) {
 /* --------------------------------------------------------------------------
    Parse program command line described in Unix standard form by argc
-   and argv.  Return the information in the options as *cmdlineP.  
+   and argv.  Return the information in the options as *cmdlineP
 
    If command line is internally inconsistent (invalid options, etc.),
    issue error message to stderr and abort program.
@@ -84,7 +84,7 @@ parseCommandLine(int argc, const char ** argv,
         cmdlineP->inputFileName = "-";
     else {
         cmdlineP->inputFileName = argv[1];
-        
+
         if (argc-1 > 1)
             pm_error("Too many arguments (%u).  The only non-option argument "
                      "is the input file name.", argc-1);
diff --git a/converter/pbm/atktopbm.c b/converter/pbm/atktopbm.c
index 807e4f4a..57782d91 100644
--- a/converter/pbm/atktopbm.c
+++ b/converter/pbm/atktopbm.c
@@ -44,7 +44,7 @@
 #define dataobject_OBJECTCREATIONFAILED 4
 #define dataobject_BADFORMAT 5
 
-/* ReadRow(file, row, length) 
+/* ReadRow(file, row, length)
 ** Reads from 'file' the encoding of bytes to fill in 'row'.  Row will be
 ** truncated or padded (with WHITE) to exactly 'length' bytes.
 **
@@ -52,7 +52,7 @@
 **      '|'     correct end of line
 **      '\0'    if the length was satisfied (before a terminator)
 **      EOF     if the file ended
-**      '\'  '{'    other recognized ends. 
+**      '\'  '{'    other recognized ends.
 ** The '|' is the expected end and pads the row with WHITE.
 ** The '\' and '{' are error conditions and may indicate the
 ** beginning of some other portion of the data stream.
@@ -76,11 +76,11 @@ ReadRow(FILE *          const file,
   'file' is where to get them from.
   'row' is where to put bytes.
   'length' is how many bytes in row must be filled.
-  
+
   Return the delimiter that marks the end of the row, or EOF if EOF marks
   the end of the row, or NUL in some cases.
 -----------------------------------------------------------------------------*/
-    /* Each input character is processed by the central loop.  There are 
+    /* Each input character is processed by the central loop.  There are
     ** some input codes which require two or three characters for
     ** completion; these are handled by advancing the state machine.
     ** Errors are not processed; instead the state machine is reset
@@ -98,7 +98,7 @@ ReadRow(FILE *          const file,
         RepeatAndDigit
             /* have seen repeat code and its first following digit */
     };
-    
+
     enum StateCode InputState;  /* current state */
     int c;     /* the current input character */
     long repeatcount;  /* current repeat value */
@@ -106,8 +106,8 @@ ReadRow(FILE *          const file,
     long pendinghex;    /* the first of a pair of hex characters */
     int lengthRemaining;
     unsigned char * cursor;
-    
-    /* We cannot exit when length becomes zero because we need to check 
+
+    /* We cannot exit when length becomes zero because we need to check
     ** to see if a row ending character follows.  Thus length is checked
     ** only when we get a data generating byte.  If length then is
     ** zero, we ungetc the byte.
@@ -148,7 +148,7 @@ ReadRow(FILE *          const file,
             while (lengthRemaining-- > 0)
                 *cursor++ = WHITEBYTE;
             return c;
-    
+
         CASE1(0x21):
         CASE6(0x22):
         CASE8(0x28):
@@ -227,9 +227,9 @@ ReadRow(FILE *          const file,
             break;
 
         store:
-            /* generate byte(s) into the output row 
+            /* generate byte(s) into the output row
                Use repeatcount, depending on state.  */
-            if (lengthRemaining < repeatcount) 
+            if (lengthRemaining < repeatcount)
                 /* reduce repeat count if it would exceed
                    available space */
                 repeatcount = lengthRemaining;
@@ -269,7 +269,7 @@ ReadATKRaster(FILE * const ifP) {
         pm_error ("input file not Andrew raster object");
 
     fscanf(ifP, " %d ", &version);
-    if (version < 2) 
+    if (version < 2)
         pm_error ("version too old to parse");
 
     {
@@ -277,8 +277,8 @@ ReadATKRaster(FILE * const ifP) {
         long xscale, yscale;
         long xoffset, yoffset, subwidth, subheight;
         /* ignore all these features: */
-        fscanf(ifP, " %u %ld %ld %ld %ld %ld %ld",  
-               &options, &xscale, &yscale, &xoffset, 
+        fscanf(ifP, " %u %ld %ld %ld %ld %ld %ld",
+               &options, &xscale, &yscale, &xoffset,
                &yoffset, &subwidth, &subheight);
     }
     /* scan to end of line in case this is actually something beyond V2 */
@@ -291,8 +291,12 @@ ReadATKRaster(FILE * const ifP) {
 
     fscanf(ifP, " %d %d %d ", &objectid, &width, &height);
 
-    if (width < 1 || height < 1 || width > 1000000 || height > 1000000) 
+    if (width < 1 || height < 1 || width > 1000000 || height > 1000000)
         pm_error("bad width or height");
+        /* Note: Whether these values are upper limits set by the author of
+           the original version of this program, or come from the official
+           file format specification is unknown.
+        */
 
     pbm_writepbminit(stdout, width, height, 0);
     bitrow = pbm_allocrow_packed(width);
@@ -302,7 +306,7 @@ ReadATKRaster(FILE * const ifP) {
         long const nextChar = ReadRow(ifP, bitrow, rowlen);
 
         switch (nextChar) {
-        case '|': 
+        case '|':
             pbm_writepbmrow_packed(stdout, bitrow, width, 0);
             break;
         case EOF:
diff --git a/converter/pbm/escp2topbm.c b/converter/pbm/escp2topbm.c
index 8acd13d6..de3ea931 100644
--- a/converter/pbm/escp2topbm.c
+++ b/converter/pbm/escp2topbm.c
@@ -104,7 +104,7 @@ readChar(FILE * const ifP) {
 
 
 
-static void       
+static void
 readStripeHeader(unsigned int * const widthThisStripeP,
                  unsigned int * const rowsThisStripeP,
                  unsigned int * const compressionP,
@@ -120,7 +120,7 @@ readStripeHeader(unsigned int * const widthThisStripeP,
     compression     = stripeHeader[0];
     /* verticalResolution   = stripeHeader[1]; */
     /* horizontalResolution = stripeHeader[2]; */
-    rowsThisStripe  = stripeHeader[3];  
+    rowsThisStripe  = stripeHeader[3];
     widthThisStripe = stripeHeader[5] * 256 + stripeHeader[4];
 
     if (widthThisStripe == 0 || rowsThisStripe == 0)
@@ -143,7 +143,7 @@ readStripeHeader(unsigned int * const widthThisStripeP,
 
 /* RLE decoder */
 static void
-decEpsonRLE(unsigned int    const blockSize, 
+decEpsonRLE(unsigned int    const blockSize,
             unsigned char * const outBuffer,
             FILE *          const ifP) {
 
@@ -174,7 +174,7 @@ decEpsonRLE(unsigned int    const blockSize,
             unsigned int i;
 
             for (i = 0; i < runLength; ++i)
-                outBuffer[dpos + i] = repeatChar;  
+                outBuffer[dpos + i] = repeatChar;
             dpos += runLength;
         }
     }
@@ -191,7 +191,7 @@ processStripeRaster(unsigned char ** const bitarray,
                     unsigned int     const compression,
                     FILE *           const ifP,
                     unsigned int *   const rowIdxP) {
-         
+
     unsigned int const initialRowIdx = *rowIdxP;
     unsigned int const widthInBytes = pbm_packed_bytes(width);
     unsigned int const blockSize = rowsThisStripe * widthInBytes;
@@ -237,7 +237,7 @@ expandBitarray(unsigned char *** const bitarrayP,
     if (*bitarraySizeP > heightMax)
         pm_error("Error: Image too tall");
     else
-        REALLOCARRAY_NOFAIL(*bitarrayP, *bitarraySizeP); 
+        REALLOCARRAY_NOFAIL(*bitarrayP, *bitarraySizeP);
 }
 
 
@@ -253,7 +253,7 @@ writePbmImage(unsigned char ** const bitarray,
         pm_error("No image");
 
     pbm_writepbminit(stdout, width, height, 0);
- 
+
     for (row = 0; row < height; ++row) {
         pbm_cleanrowend_packed(bitarray[row], width);
         pbm_writepbmrow_packed(stdout, bitarray[row], width, 0);
@@ -309,7 +309,7 @@ main(int          argc,
                 unsigned int compression;
                 unsigned int rowsThisStripe;
                 unsigned int widthThisStripe;
-            
+
                 readStripeHeader(&widthThisStripe, &rowsThisStripe,
                                  &compression, ifP);
 
@@ -320,7 +320,7 @@ main(int          argc,
                     /* The official Epson manual says valid values are 1, 8,
                        24 but we just print a warning message and continue if
                        other values are detected.
-                    */ 
+                    */
                     pm_message("Abnormal data block height value: %u "
                                "(ignoring)",
                                rowsThisStripe);
diff --git a/converter/pbm/g3topbm.c b/converter/pbm/g3topbm.c
index 5d98fcb2..5db507a3 100644
--- a/converter/pbm/g3topbm.c
+++ b/converter/pbm/g3topbm.c
@@ -552,12 +552,11 @@ readFaxRow(struct BitStream * const bitStreamP,
                 curcode = (curcode << 1) | bit;
                 ++curlen;
 
-		if (curlen > 11 && curcode == 0x00) {
-		    if (++fillbits > MAXFILLBITS)
-                pm_error("Encountered %u consecutive fill bits.  "
-                       "Aborting", fillbits);
-		}
-		else if (curlen - fillbits > 13) {
+                if (curlen > 11 && curcode == 0x00) {
+                    if (++fillbits > MAXFILLBITS)
+                        pm_error("Encountered %u consecutive fill bits.  "
+                                 "Aborting", fillbits);
+                } else if (curlen - fillbits > 13) {
                     formatBadCodeException(exceptionP, col, curlen, curcode);
                     done = TRUE;
                 } else if (curcode != 0) {
diff --git a/converter/pbm/mgrtopbm.c b/converter/pbm/mgrtopbm.c
index 712e3be9..e0a88883 100644
--- a/converter/pbm/mgrtopbm.c
+++ b/converter/pbm/mgrtopbm.c
@@ -58,12 +58,12 @@ interpHdrHeight(struct b_header const head,
 
 
 static void
-readMgrHeader(FILE *          const ifP, 
-              unsigned int *  const colsP, 
-              unsigned int *  const rowsP, 
-              unsigned int *  const depthP, 
+readMgrHeader(FILE *          const ifP,
+              unsigned int *  const colsP,
+              unsigned int *  const rowsP,
+              unsigned int *  const depthP,
               unsigned int *  const padrightP ) {
-    
+
     struct b_header head;
     unsigned int pad;
     size_t bytesRead;
@@ -73,10 +73,10 @@ readMgrHeader(FILE *          const ifP,
         pm_error("Unable to read 1st byte of file.  "
                  "fread() returns errno %d (%s)",
                  errno, strerror(errno));
-    if (head.magic[0] == 'y' && head.magic[1] == 'z') { 
+    if (head.magic[0] == 'y' && head.magic[1] == 'z') {
         /* new style bitmap */
         size_t bytesRead;
-        bytesRead = fread(&head.depth, 
+        bytesRead = fread(&head.depth,
                           sizeof(head) - sizeof(struct old_b_header), 1, ifP);
         if (bytesRead != 1 )
             pm_error("Unable to read header after 1st byte.  "
@@ -84,11 +84,11 @@ readMgrHeader(FILE *          const ifP,
                      errno, strerror(errno));
         *depthP = (int) head.depth - ' ';
         pad = 8;
-    } else if (head.magic[0] == 'x' && head.magic[1] == 'z') { 
+    } else if (head.magic[0] == 'x' && head.magic[1] == 'z') {
         /* old style bitmap with 32-bit padding */
         *depthP = 1;
         pad = 32;
-    } else if (head.magic[0] == 'z' && head.magic[1] == 'z') { 
+    } else if (head.magic[0] == 'z' && head.magic[1] == 'z') {
         /* old style bitmap with 16-bit padding */
         *depthP = 1;
         pad = 16;
@@ -104,8 +104,8 @@ readMgrHeader(FILE *          const ifP,
 
     interpHdrWidth (head, colsP);
     interpHdrHeight(head, rowsP);
-    
-    *padrightP = ( ( *colsP + pad - 1 ) / pad ) * pad - *colsP;
+
+    *padrightP = ((*colsP + pad - 1) / pad) * pad - *colsP;
 }
 
 
@@ -131,7 +131,7 @@ main(int    argc,
         inputFileName = argv[1];
     else
         inputFileName = "-";
-    
+
     ifP = pm_openr(inputFileName);
 
     readMgrHeader(ifP, &cols, &rows, &depth, &padright);
@@ -141,7 +141,7 @@ main(int    argc,
     pbm_writepbminit(stdout, cols, rows, 0);
 
     bitrow = pbm_allocrow_packed(cols + padright);
-    
+
     itemCount = (cols + padright ) / 8;
 
     for (row = 0; row < rows; ++row) {
@@ -166,9 +166,9 @@ main(int    argc,
    Changed bitrow from plain to raw, read function from getc() to fread(),
    write function from pbm_writepbmrow() to pbm_writepbmrow_packed().
    Retired bitwise transformation functions.
-   
+
    NOT tested for old-style format files.  Only one zz file in mgrsrc-0.69 .
-  
+
 */
 
 
diff --git a/converter/pbm/pbmtoatk.c b/converter/pbm/pbmtoatk.c
index ea5b7abe..5f2b625c 100644
--- a/converter/pbm/pbmtoatk.c
+++ b/converter/pbm/pbmtoatk.c
@@ -22,8 +22,8 @@
 
 
 static void
-write_atk_bytes(FILE *        const file, 
-                unsigned char const curbyte, 
+write_atk_bytes(FILE *        const file,
+                unsigned char const curbyte,
                 unsigned int  const startcount) {
 
     /* codes for data stream */
@@ -37,7 +37,7 @@ write_atk_bytes(FILE *        const file,
     #define BLACKBYTE 0xFF
 
     /* WriteRow table for conversion of a byte value to two character
-       hex representation 
+       hex representation
     */
 
     static unsigned char hex[16] = {
@@ -82,10 +82,10 @@ write_atk_bytes(FILE *        const file,
 
 
 static void
-process_atk_byte(int *           const pcurcount, 
-                 unsigned char * const pcurbyte, 
-                 FILE *          const file, 
-                 unsigned char   const newbyte, 
+process_atk_byte(int *           const pcurcount,
+                 unsigned char * const pcurbyte,
+                 FILE *          const file,
+                 unsigned char   const newbyte,
                  int             const eolflag) {
 
     int curcount;
@@ -93,7 +93,7 @@ process_atk_byte(int *           const pcurcount,
 
     curcount = *pcurcount;  /* initial value */
     curbyte  = *pcurbyte;  /* initial value */
-    
+
     if (curcount < 1) {
         *pcurbyte = curbyte = newbyte;
         *pcurcount = curcount = 1;
@@ -139,6 +139,17 @@ main(int argc, const char ** argv) {
     }
 
     pbm_readpbminit(ifP, &cols, &rows, &format);
+    /* Note: atktopbm imposes limits: cols <= 1000000 rows <= 1000000
+       Whether these values are defined in the official file format
+       specification is unknown.  We issue warning messages when large
+       values are encountered.
+     */
+
+    if (cols > 1000000)
+        pm_message("Proceeding with extremely large width value: %u", cols);
+    if (rows > 1000000)
+        pm_message("Proceeding with extremely large height value: %u", rows);
+
     bitrow = pbm_allocrow_packed(cols);
 
     printf("\\begindata{raster,%d}\n", 1);
@@ -152,7 +163,7 @@ main(int argc, const char ** argv) {
 
         pbm_readpbmrow_packed(ifP, bitrow, cols, format);
         pbm_cleanrowend_packed(bitrow, cols);
-        
+
         for (i = 0, curbyte = 0, curcount = 0; i < byteCt; ++i) {
             process_atk_byte(&curcount, &curbyte, stdout,
                              bitrow[i],
@@ -162,7 +173,7 @@ main(int argc, const char ** argv) {
 
     pbm_freerow_packed(bitrow);
     pm_close(ifP);
-    
+
     printf("\\enddata{raster, %d}\n", 1);
 
     return 0;
diff --git a/converter/pbm/pbmtoescp2.c b/converter/pbm/pbmtoescp2.c
index 6f284f3c..942ecec9 100644
--- a/converter/pbm/pbmtoescp2.c
+++ b/converter/pbm/pbmtoescp2.c
@@ -18,6 +18,7 @@
 *
 *  ESC/P Reference Manual (1997)
 *  ftp://download.epson-europe.com/pub/download/182/epson18162eu.zip
+*  See Part 1 "ESC ." Print Raster Graphics
 */
 
 #include <string.h>
@@ -28,7 +29,7 @@
 #include "runlength.h"
 #include "pbm.h"
 
-
+#define MAXCOLS (127 * 256 + 255)  /* Limit in official Epson manual */
 
 static char const esc = 033;
 
@@ -57,19 +58,19 @@ parseCommandLine(int argc, const char ** argv,
     opt.opt_table = option_def;
     opt.short_allowed = FALSE;
     opt.allowNegNum = FALSE;
-    OPTENT3(0, "compress",     OPT_UINT,    &cmdlineP->compress,    
+    OPTENT3(0, "compress",     OPT_UINT,    &cmdlineP->compress,
             &compressSpec,    0);
-    OPTENT3(0, "resolution",   OPT_UINT,    &cmdlineP->resolution,  
+    OPTENT3(0, "resolution",   OPT_UINT,    &cmdlineP->resolution,
             &resolutionSpec,  0);
-    OPTENT3(0, "stripeheight", OPT_UINT,    &cmdlineP->stripeHeight,  
+    OPTENT3(0, "stripeheight", OPT_UINT,    &cmdlineP->stripeHeight,
             &stripeHeightSpec, 0);
-    OPTENT3(0, "raw",          OPT_FLAG,    NULL,  
+    OPTENT3(0, "raw",          OPT_FLAG,    NULL,
             &rawSpec,    0);
-    OPTENT3(0, "formfeed",     OPT_FLAG,    NULL,  
+    OPTENT3(0, "formfeed",     OPT_FLAG,    NULL,
             &formfeedSpec,    0);
-    
+
     pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
-    
+
     if (argc-1 > 1)
         pm_error("Too many arguments: %d.  "
                  "Only argument is the filename", argc-1);
@@ -155,7 +156,7 @@ main(int argc, const char * argv[]) {
     unsigned char * bitrow[256];
     unsigned char * compressedData;
     struct CmdlineInfo cmdline;
-    
+
     pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
@@ -164,7 +165,7 @@ main(int argc, const char * argv[]) {
 
     pbm_readpbminit(ifP, &cols, &rows, &format);
 
-    if (cols / 256 > 127)  /* Limit in official Epson manual */
+    if (cols > MAXCOLS)
         pm_error("Image width is too large");
 
     outColByteCt = pbm_packed_bytes(cols);
@@ -229,7 +230,7 @@ main(int argc, const char * argv[]) {
         }
     }
 
-    free(inBuff); 
+    free(inBuff);
     free(compressedData);
     pm_close(ifP);
 
diff --git a/converter/pbm/pbmtomgr.c b/converter/pbm/pbmtomgr.c
index 0e529740..faa56d8f 100644
--- a/converter/pbm/pbmtomgr.c
+++ b/converter/pbm/pbmtomgr.c
@@ -33,7 +33,7 @@ putinit(unsigned int const rows,
     head._reserved = ' ';
     writtenCount = fwrite(&head, sizeof(head), 1, stdout);
     if (writtenCount != 1)
-        pm_error("fwrite() failed to write the MGR header.");    
+        pm_error("fwrite() failed to write the MGR header.");
 }
 
 
@@ -63,7 +63,7 @@ main(int argc,
         inputFileName = argv[1];
     else
         inputFileName = "-";
-    
+
     ifP = pm_openr(inputFileName);
 
     pbm_readpbminit(ifP, &cols, &rows, &format);
@@ -71,14 +71,14 @@ main(int argc,
         pm_error("Image width too large: %u (max: %u)", cols, maxDimension);
     if (rows > maxDimension)
         pm_error("Image height too large: %u (max: %u)", rows, maxDimension);
-    
+
     bitrow = pbm_allocrow_packed(cols);
     bytesPerRow = pbm_packed_bytes(cols);
 
     putinit(rows, cols);
-    
+
     for (row = 0; row < rows; ++row) {
-        /* The raster formats are identical. 
+        /* The raster formats are identical.
            The row end pad bits are set to 0 in mgr.
         */
         size_t bytesWritten;
@@ -89,7 +89,7 @@ main(int argc,
         bytesWritten = fwrite(bitrow, 1, bytesPerRow, stdout);
         if (bytesWritten != bytesPerRow )
             pm_error("fwrite() failed to write MGR bitmap "
-                     "to Standard Output.");    
+                     "to Standard Output.");
     }
     pm_close(ifP);
     return 0;
@@ -102,7 +102,7 @@ main(int argc,
    pbm_readpbmrow_packed(), write function from putc() to fwrite().
 
    Retired bitwise transformation functions.
-   
+
    Produces only new style bitmap (8 bit padding.)  See mgrtopbm.c .
 */
 
diff --git a/converter/pbm/ybmtopbm.c b/converter/pbm/ybmtopbm.c
index ea7e66a7..36f2dee7 100644
--- a/converter/pbm/ybmtopbm.c
+++ b/converter/pbm/ybmtopbm.c
@@ -19,7 +19,7 @@ static short const ybmMagic = ( ( '!' << 8 ) | '!' );
 
 
 static void
-getinit(FILE *  const ifP,
+getinit(FILE *         const ifP,
         unsigned int * const colsP,
         unsigned int * const rowsP,
         int *          const depthP) {
@@ -80,7 +80,7 @@ main(int argc, const char * argv[]) {
     getinit(ifP, &cols, &rows, &depth);
     if (depth != 1)
         pm_error("YBM file has depth of %u, must be 1", (unsigned int) depth);
-    
+
     pbm_writepbminit(stdout, cols, rows, 0);
 
     bitrow = pbm_allocrow_packed(cols + 8);
@@ -95,7 +95,7 @@ main(int argc, const char * argv[]) {
         for (i = 0; i < itemCt; ++i) {
             short int item;
             pm_readbigshort(ifP, &item);
-            itemrow[i] = (uint16_t) item; 
+            itemrow[i] = (uint16_t) item;
         }
 
         for (i = 0; i < pbm_packed_bytes(cols); ++i)
diff --git a/converter/pgm/st4topgm.c b/converter/pgm/st4topgm.c
index 90be6b72..791184b7 100644
--- a/converter/pgm/st4topgm.c
+++ b/converter/pgm/st4topgm.c
@@ -69,11 +69,11 @@ writeRaster(FILE *       const ifP,
         unsigned int col;
 
         for (col = 0; col < st4Width; ++col) {
-            char c;
+            unsigned char c;
 
-            pm_readchar(ifP, &c);
+            pm_readcharu(ifP, &c);
 
-            tuplerow[col][0] = (unsigned char)c;
+            tuplerow[col][0] = c;
         }
         pnm_writepamrow(pamP, tuplerow);
     }
diff --git a/doc/HISTORY b/doc/HISTORY
index 997a031c..b6e777b4 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -4,43 +4,49 @@ Netpbm.
 CHANGE HISTORY 
 --------------
 
-21.12.20 BJH  Release 10.96.06
+21.12.27 BJH  Release 10.97.00
 
-              Fix version number.
+              Add pbnnoise.
 
-21.12.19 BJH  Release 10.96.05
+              pnmpad: Use -halign with -mwidth and default to centering the
+              image instead of left-justifying when no other padding is being
+              added.  Same with -mheight, -valign, and top-justifying.
 
-              Build: Don't attempt to build 'pamexec' on systems without
-              Unix process management.  Bug introduced in Netpbm 10.94
-              (March 2021) and supposedly fixed in 10.95, but not really.
+              xwdtopnm: Add ability to process bit depth 32.
 
-21.11.28 BJH  Release 10.96.04
+              pgmtoppm: Add -black, -white.
+
+              ppmpat: Add -mesh.
+
+              pampaintspill: Add -near.  Thanks Scott Pakin.
+
+              pamtogif: Fix bug: doesn't ignore the input alpha mask when user
+              specified -transparent.  Broken in Netpbm 10.37 (December 2006).
 
               palmtopnm: Fix bug: fails with PackBits input on platform with
               default unsigned char, such as ppc64.  Always broken.  (Ability
               to convert PackBits input was new in Netpbm 10.27 (March 2005).
               
-21.11.07 BJH  Release 10.96.03
+              pamrubber: Fix bug: random behavior with -quad when you specify
+              both points for source or target and the second one is lower in
+              the image than the first.  Always broken (Pamrubber was new in
+              Netpbm 10.54 (March 2011)).
 
               sunicontopnm, escp2topbm, mgrtopbm, ybmtopbm, pamcut, pbmpscale,
               pnmcat, pnmpad: Fix arithmetic overrun with ridiculously large
               image.
 
-21.10.26 BJH  Release 10.96.02
+              pbmclean: Fix overallocation of memory (waste).
 
-              pamrubber: Fix bug: random behavior with -quad when you specify
-              both points for source or target and the second one is lower in
-              the image than the first.  Always broken (Pamrubber was new in
-              Netpbm 10.54 (March 2011)).
+              libnetbm: Add pnm_writepamrowpart, pnm_formatPamtuples.
 
               libnetpbm: When validating computable size of width and height,
               allow for adding up to 10 instead of 2, to account for rounding
               up to a multiple of 8 in processing bit maps.
 
-21.09.30 BJH  Release 10.96.01
-
-              pamtogif: Fix bug: doesn't ignore the input alpha mask when user
-              specified -transparent.  Broken in Netpbm 10.37 (December 2006).
+              Build: Don't attempt to build 'pamexec' on systems without
+              Unix process management.  Bug introduced in Netpbm 10.94
+              (March 2021) and supposedly fixed in 10.95, but not really.
 
 21.09.25 BJH  Release 10.96.00
 
diff --git a/editor/pambackground.c b/editor/pambackground.c
index b4941042..218f5b7e 100644
--- a/editor/pambackground.c
+++ b/editor/pambackground.c
@@ -4,19 +4,19 @@
 #include "shhopt.h"
 #include "pam.h"
 
-struct cmdlineInfo {
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    const char * inputFileName;  
+    const char * inputFileName;
     unsigned int verbose;
 };
 
 
 
 static void
-parseCommandLine(int argc, char ** const argv,
-                 struct cmdlineInfo * const cmdlineP) {
+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.
@@ -37,7 +37,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 */
 
-    pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
+    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
     if (argc-1 < 1)
@@ -48,7 +48,7 @@ parseCommandLine(int argc, char ** const argv,
             pm_error("There is at most one argument:  input file name.  "
                      "You specified %d", argc-1);
     }
-}        
+}
 
 
 
@@ -83,7 +83,7 @@ allocateOutputPointerRow(unsigned int const width,
 
 
 static void
-createWhiteTuple(const struct pam * const pamP, 
+createWhiteTuple(const struct pam * const pamP,
                  tuple *            const whiteTupleP) {
 /*----------------------------------------------------------------------------
    Create a "white" tuple.  By that we mean a tuple all of whose elements
@@ -134,7 +134,7 @@ selectBackground(struct pam * const pamP,
             bg = ul;
         }
     }
-    
+
     *bgColorP = pnm_allocpamtuple(pamP);
     pnm_assigntuple(pamP, *bgColorP, bg);
 }
@@ -301,8 +301,8 @@ expandBackgroundHoriz(unsigned char ** const pi,
                       unsigned int     const height,
                       bool *           const expandedP) {
 /*----------------------------------------------------------------------------
-   In every row, expand the background rightward from any known background
-   pixel through all consecutive unknown pixels.
+   In every row except top and bottom, expand the background rightward from
+   any known background pixel through all consecutive unknown pixels.
 
    Then do the same thing leftward.
 
@@ -343,8 +343,9 @@ expandBackgroundVert(unsigned char ** const pi,
                      unsigned int     const height,
                      bool *           const expandedP) {
 /*----------------------------------------------------------------------------
-   In every column, expand the background downward from any known background
-   pixel through all consecutive unknown pixels.
+   In every column except leftmost and rightmost, expand the background
+   downward from any known background pixel through all consecutive unknown
+   pixels.
 
    Then do the same thing upward.
 
@@ -400,7 +401,7 @@ findBackgroundPixels(struct pam *                   const inpamP,
 -----------------------------------------------------------------------------*/
     unsigned char ** pi;
     bool backgroundComplete;
-    unsigned int passes;
+    unsigned int passCt;
 
     pi = newPi(inpamP->width, inpamP->height);
 
@@ -409,29 +410,29 @@ findBackgroundPixels(struct pam *                   const inpamP,
     setEdges(pi, inpamP->width, inpamP->height);
 
     backgroundComplete = FALSE;
-    passes = 0;
-    
+    passCt = 0;
+
     while (!backgroundComplete) {
         bool expandedHoriz, expandedVert;
 
         expandBackgroundHoriz(pi, inpamP->width, inpamP->height,
                               &expandedHoriz);
-    
+
         expandBackgroundVert(pi, inpamP->width, inpamP->height,
                              &expandedVert);
 
         backgroundComplete = !expandedHoriz && !expandedVert;
 
-        ++passes;
+        ++passCt;
     }
 
     if (verbose)
-        pm_message("Background found in %u passes", passes);
+        pm_message("Background found in %u passes", passCt);
 
     *piP = (const unsigned char * const *)pi;
 }
 
-                     
+
 
 static void
 writeOutput(const struct pam *            const inpamP,
@@ -468,16 +469,16 @@ writeOutput(const struct pam *            const inpamP,
 
 
 int
-main(int argc, char *argv[]) {
+main(int argc, const char *argv[]) {
 
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
     struct pam inpam;
     FILE * ifP;
     pm_filepos rasterpos;
     tuple backgroundColor;
     const unsigned char * const * pi;
-    
-    pnm_init(&argc, argv);
+
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
@@ -500,6 +501,9 @@ main(int argc, char *argv[]) {
     pm_close(ifP);
 
     pnm_freepamtuple(backgroundColor);
-    
+
     return 0;
 }
+
+
+
diff --git a/editor/pamenlarge.c b/editor/pamenlarge.c
index 56a8c6f7..eb3b6ba2 100644
--- a/editor/pamenlarge.c
+++ b/editor/pamenlarge.c
@@ -690,6 +690,7 @@ enlargePbm(struct pam * const inpamP,
         unsigned int const rightPadding =
             scaleMethod == METHOD_GENERAL ? 0 : (xScaleFactor - 1) * 8;
 
+        assert (outcols < UINT_MAX - rightPadding);
         outrow = pbm_allocrow_packed(outcols + rightPadding);
 
         if (scaleMethod == METHOD_GENERAL)
diff --git a/editor/pamrubber.c b/editor/pamrubber.c
index 802d8c20..f68e36fe 100644
--- a/editor/pamrubber.c
+++ b/editor/pamrubber.c
@@ -193,38 +193,39 @@ makeline(point const p1,
 
 
 
-static bool
-intersect(line *  const l1P,
-          line *  const l2P,
-          point * const pP) {
+static void
+findIntersection(const line *  const l1P,
+                 const line *  const l2P,
+                 bool *        const theyIntersectP,
+                 point *       const intersectionP) {
 
-    bool cross;
+    bool theyIntersect;
 
     if (((l2P->p2.y - l2P->p1.y) * (l1P->p2.x - l1P->p1.x) -
          (l2P->p2.x - l2P->p1.x) * (l1P->p2.y - l1P->p1.y)) == 0) {
         /* parallel lines */
 
-        cross = false;
+        theyIntersect = false;
 
         if ((l1P->p1.x == l1P->p2.x) && (l2P->p1.x == l2P->p2.x)) {
             /* two vertical lines */
-            pP->x = (l1P->p1.x + l2P->p1.x) / 2.0;
-            pP->y = 1e10;
+            intersectionP->x = (l1P->p1.x + l2P->p1.x) / 2.0;
+            intersectionP->y = 1e10;
         } else if ((l1P->p1.y == l1P->p2.y) && (l2P->p1.y == l2P->p2.y)) {
             /* two horizontal lines */
-            pP->x = 1e10;
-            pP->y = (l1P->p1.y + l2P->p1.y) / 2.0;
+            intersectionP->x = 1e10;
+            intersectionP->y = (l1P->p1.y + l2P->p1.y) / 2.0;
         } else {
             if (fabs(l1P->p2.y - l1P->p1.y) > fabs(l1P->p2.x - l1P->p1.x)) {
                 /* steep slope */
-                pP->y = 1e10;
-                pP->x = (l1P->p2.x - l1P->p1.x) / (l1P->p2.y - l1P->p1.y)
-                    * 1e10;
+                intersectionP->y = 1e10;
+                intersectionP->x =
+                    (l1P->p2.x - l1P->p1.x) / (l1P->p2.y - l1P->p1.y) * 1e10;
             } else {
                 /* even slope */
-                pP->x = 1e10;
-                pP->y = (l1P->p2.y - l1P->p1.y) / (l1P->p2.x - l1P->p1.x)
-                    * 1e10;
+                intersectionP->x = 1e10;
+                intersectionP->y =
+                    (l1P->p2.y - l1P->p1.y) / (l1P->p2.x - l1P->p1.x) * 1e10;
             }
         }
     } else {
@@ -242,16 +243,16 @@ intersect(line *  const l1P,
                * (l1P->p2.x - l1P->p1.x) - (l2P->p2.x - l2P->p1.x)
                * (l1P->p2.y - l1P->p1.y));
 
-        pP->x = l1P->p1.x + ua * (l1P->p2.x - l1P->p1.x);
-        pP->y = l1P->p1.y + ua * (l1P->p2.y - l1P->p1.y);
+        intersectionP->x = l1P->p1.x + ua * (l1P->p2.x - l1P->p1.x);
+        intersectionP->y = l1P->p1.y + ua * (l1P->p2.y - l1P->p1.y);
 
         if ((ua >= 0.0) && (ua <= 1.0) && (ub >= 0.0) && (ub <= 1.0))
-            cross = true;
+            theyIntersect = true;
         else
-            cross = false;
+            theyIntersect = false;
     }
-
-    return cross;
+    if (theyIntersectP)
+        *theyIntersectP = theyIntersect;
 }
 
 
@@ -307,28 +308,29 @@ insidetri(triangle * const triP,
 
 
 static bool
-windtriangle(triangle * const tP,
+windtriangle(triangle * const triP,
              point      const p1,
              point      const p2,
              point      const p3) {
     point f, c;
-    line le, lv;
     bool cw;
 
     /* find cross of vertical through p3 and the edge p1-p2 */
     f.x = p3.x;
     f.y = -1.0;
-    lv = makeline(p3, f);
-    le = makeline(p1, p2);
-    intersect(&le, &lv, &c);
+    {
+        line const lv = makeline(p3, f);
+        line const le = makeline(p1, p2);
+        findIntersection(&le, &lv, NULL, &c);
+    }
 
     /* check for clockwise winding triangle */
     if (((p1.x > p2.x) && (p3.y < c.y)) ||
         ((p1.x < p2.x) && (p3.y > c.y))) {
-        *tP = maketriangle(p1, p2, p3);
+        *triP = maketriangle(p1, p2, p3);
         cw = true;
     } else { /* p1/2/3 were counter clockwise */
-        *tP = maketriangle(p1, p3, p2);
+        *triP = maketriangle(p1, p3, p2);
         cw = false;
     }
     return cw;
@@ -612,12 +614,16 @@ quadCorner(point           const p0,
 
     /* p0-p1 and p2-p3 are the diagonals */
 
+    triangle tri;
+
     if (fabs(p0.x - p1.x) + fabs(p0.y - p1.y) >=
         fabs(p2.x - p3.x) + fabs(p2.y - p3.y)) {
-        quadCornerSized(p0, p1, p2, p3, mid, quadP, triP);
+        quadCornerSized(p0, p1, p2, p3, mid, quadP, &tri);
     } else {
-        quadCornerSized(p2, p3, p0, p1, mid, quadP, triP);
+        quadCornerSized(p2, p3, p0, p1, mid, quadP, &tri);
     }
+    if (triP)
+        *triP = tri;
 }
 
 
@@ -730,7 +736,6 @@ prepTrig(int          const wd,
     point rtl2, rtr2, rbl2, rbr2;
     point c1p1, c1p2, c1p3, c1p4;
     point c2p1, c2p2, c2p3, c2p4;
-    line l1, l2;
     point p0;
     struct pm_randSt randSt;
 
@@ -897,47 +902,58 @@ prepTrig(int          const wd,
         /*       3   4    2   4      4   3    2   3      4   2    3   2      */
         /*-------------------------------------------------------------------*/
 
-        /* center two triangles */
-        l1 = makeline(c1p1, c1p4);
-        l2 = makeline(c1p2, c1p3);
-        if (intersect(&l1, &l2, &p0)) {
-            if (windtriangle(&tri1s[0], c1p1, c1p2, c1p3)) {
-                tri1s[1] = maketriangle(c1p4, c1p3, c1p2);
-                tri2s[0] = maketriangle(c2p1, c2p2, c2p3);
-                tri2s[1] = maketriangle(c2p4, c2p3, c2p2);
-            } else {
-                tri1s[1] = maketriangle(c1p4, c1p2, c1p3);
-                tri2s[0] = maketriangle(c2p1, c2p3, c2p2);
-                tri2s[1] = maketriangle(c2p4, c2p2, c2p3);
+        {
+            /* center two triangles */
+            line const l1 = makeline(c1p1, c1p4);
+            line const l2 = makeline(c1p2, c1p3);
+            bool theyIntersect;
+            findIntersection(&l1, &l2, &theyIntersect, &p0);
+            if (theyIntersect) {
+                if (windtriangle(&tri1s[0], c1p1, c1p2, c1p3)) {
+                    tri1s[1] = maketriangle(c1p4, c1p3, c1p2);
+                    tri2s[0] = maketriangle(c2p1, c2p2, c2p3);
+                    tri2s[1] = maketriangle(c2p4, c2p3, c2p2);
+                } else {
+                    tri1s[1] = maketriangle(c1p4, c1p2, c1p3);
+                    tri2s[0] = maketriangle(c2p1, c2p3, c2p2);
+                    tri2s[1] = maketriangle(c2p4, c2p2, c2p3);
+                }
             }
         }
-        l1 = makeline(c1p1, c1p3);
-        l2 = makeline(c1p2, c1p4);
-        if (intersect(&l1, &l2, &p0)) {
-            if (windtriangle(&tri1s[0], c1p1, c1p2, c1p4)) {
-                tri1s[1] = maketriangle(c1p3, c1p4, c1p2);
-                tri2s[0] = maketriangle(c2p1, c2p2, c2p4);
-                tri2s[1] = maketriangle(c2p3, c2p4, c2p2);
-            } else {
-                tri1s[1] = maketriangle(c1p3, c1p2, c1p4);
-                tri2s[0] = maketriangle(c2p1, c2p4, c2p2);
-                tri2s[1] = maketriangle(c2p3, c2p2, c2p4);
+        {
+            line const l1 = makeline(c1p1, c1p3);
+            line const l2 = makeline(c1p2, c1p4);
+            bool theyIntersect;
+            findIntersection(&l1, &l2, &theyIntersect, &p0);
+            if (theyIntersect) {
+                if (windtriangle(&tri1s[0], c1p1, c1p2, c1p4)) {
+                    tri1s[1] = maketriangle(c1p3, c1p4, c1p2);
+                    tri2s[0] = maketriangle(c2p1, c2p2, c2p4);
+                    tri2s[1] = maketriangle(c2p3, c2p4, c2p2);
+                } else {
+                    tri1s[1] = maketriangle(c1p3, c1p2, c1p4);
+                    tri2s[0] = maketriangle(c2p1, c2p4, c2p2);
+                    tri2s[1] = maketriangle(c2p3, c2p2, c2p4);
+                }
             }
         }
-        l1 = makeline(c1p1, c1p2);
-        l2 = makeline(c1p3, c1p4);
-        if (intersect(&l1, &l2, &p0)) {
-            if (windtriangle(&tri1s[0], c1p1, c1p3, c1p4)) {
-                tri1s[1] = maketriangle(c1p2, c1p4, c1p3);
-                tri2s[0] = maketriangle(c2p1, c2p3, c2p4);
-                tri2s[1] = maketriangle(c2p2, c2p4, c2p3);
-            } else {
-                tri1s[1] = maketriangle(c1p2, c1p3, c1p4);
-                tri2s[0] = maketriangle(c2p1, c2p4, c2p3);
-                tri2s[1] = maketriangle(c2p2, c2p3, c2p4);
+        {
+            line const l1 = makeline(c1p1, c1p2);
+            line const l2 = makeline(c1p3, c1p4);
+            bool theyIntersect;
+            findIntersection(&l1, &l2, &theyIntersect, &p0);
+            if (theyIntersect) {
+                if (windtriangle(&tri1s[0], c1p1, c1p3, c1p4)) {
+                    tri1s[1] = maketriangle(c1p2, c1p4, c1p3);
+                    tri2s[0] = maketriangle(c2p1, c2p3, c2p4);
+                    tri2s[1] = maketriangle(c2p2, c2p4, c2p3);
+                } else {
+                    tri1s[1] = maketriangle(c1p2, c1p3, c1p4);
+                    tri2s[0] = maketriangle(c2p1, c2p4, c2p3);
+                    tri2s[1] = maketriangle(c2p2, c2p3, c2p4);
+                }
             }
         }
-
         /* control points in correct orientation */
         c1p1 = tri1s[0].p1;
         c1p2 = tri1s[0].p2;
@@ -995,11 +1011,6 @@ prepQuad(void) {
 
 /* order quad control points */
 
-    double d01, d12, d20;
-    line l1, l2;
-    point mid;
-    triangle tri;
-
     if (nCP == 1) {
         /* create a rectangle from top-left corner of image and control
            point
@@ -1042,9 +1053,9 @@ prepQuad(void) {
                furthest apart
             */
 
-            d01 = distance(newCP[0], newCP[1]);
-            d12 = distance(newCP[1], newCP[2]);
-            d20 = distance(newCP[2], newCP[0]);
+            double const d01 = distance(newCP[0], newCP[1]);
+            double const d12 = distance(newCP[1], newCP[2]);
+            double const d20 = distance(newCP[2], newCP[0]);
 
             if ((d01 > d12) && (d01 > d20)) {
                 oldCP[3].x = oldCP[0].x + oldCP[1].x - oldCP[2].x;
@@ -1074,51 +1085,72 @@ prepQuad(void) {
 
         /* nCP = 3 or 4 */
 
-        /* find the intersection of the diagonals */
-        l1 = makeline(oldCP[0], oldCP[1]);
-        l2 = makeline(oldCP[2], oldCP[3]);
-        if (intersect(&l1, &l2, &mid)) {
-            quadCorner(oldCP[0], oldCP[1], oldCP[2], oldCP[3],
-                       mid, &quad1, &tri);
-        } else {
-            l1 = makeline(oldCP[0], oldCP[2]);
-            l2 = makeline(oldCP[1], oldCP[3]);
-            if (intersect(&l1, &l2, &mid))
-                quadCorner(oldCP[0], oldCP[2], oldCP[1], oldCP[3],
-                           mid, &quad1, &tri);
+        {
+            /* find the intersection of the diagonals */
+            line const l1 = makeline(oldCP[0], oldCP[1]);
+            line const l2 = makeline(oldCP[2], oldCP[3]);
+            bool theyIntersect;
+            point mid;
+            findIntersection(&l1, &l2, &theyIntersect, &mid);
+            if (theyIntersect)
+                quadCorner(oldCP[0], oldCP[1], oldCP[2], oldCP[3],
+                           mid, &quad1, NULL);
             else {
-                l1 = makeline(oldCP[0], oldCP[3]);
-                l2 = makeline(oldCP[1], oldCP[2]);
-                if (intersect(&l1, &l2, &mid))
-                    quadCorner(oldCP[0], oldCP[3],
-                               oldCP[1], oldCP[2], mid, &quad1, &tri);
-                else
-                    pm_error("The four old control points don't seem "
-                             "to be corners.");
+                line const l1 = makeline(oldCP[0], oldCP[2]);
+                line const l2 = makeline(oldCP[1], oldCP[3]);
+                bool theyIntersect;
+                point mid;
+                findIntersection(&l1, &l2, &theyIntersect, &mid);
+                if (theyIntersect)
+                    quadCorner(oldCP[0], oldCP[2], oldCP[1], oldCP[3],
+                               mid, &quad1, NULL);
+                else {
+                    line const l1 = makeline(oldCP[0], oldCP[3]);
+                    line const l2 = makeline(oldCP[1], oldCP[2]);
+                    bool theyIntersect;
+                    point mid;
+                    findIntersection(&l1, &l2, &theyIntersect, &mid);
+                    if (theyIntersect)
+                        quadCorner(oldCP[0], oldCP[3],
+                                   oldCP[1], oldCP[2], mid, &quad1, NULL);
+                    else
+                        pm_error("The four old control points don't seem "
+                                 "to be corners.");
+                }
             }
         }
-
-        /* repeat for the "to-be" control points */
-        l1 = makeline(newCP[0], newCP[1]);
-        l2 = makeline(newCP[2], newCP[3]);
-        if (intersect(&l1, &l2, &mid))
-            quadCorner(newCP[0], newCP[1], newCP[2], newCP[3],
-                       mid, &quad2, &tri);
-        else {
-            l1 = makeline(newCP[0], newCP[2]);
-            l2 = makeline(newCP[1], newCP[3]);
-            if (intersect(&l1, &l2, &mid))
-                quadCorner(newCP[0], newCP[2], newCP[1], newCP[3],
-                           mid, &quad2, &tri);
+        {
+            /* repeat for the "to-be" control points */
+            line const l1 = makeline(newCP[0], newCP[1]);
+            line const l2 = makeline(newCP[2], newCP[3]);
+            bool theyIntersect;
+            point mid;
+            findIntersection(&l1, &l2, &theyIntersect, &mid);
+            if (theyIntersect)
+                quadCorner(newCP[0], newCP[1], newCP[2], newCP[3],
+                           mid, &quad2, NULL);
             else {
-                l1 = makeline(newCP[0], newCP[3]);
-                l2 = makeline(newCP[1], newCP[2]);
-                if (intersect(&l1, &l2, &mid))
-                    quadCorner(newCP[0], newCP[3],
-                               newCP[1], newCP[2], mid, &quad2, &tri);
-                else
-                    pm_error("The four new control points don't seem "
-                             "to be corners.");
+                line const l1 = makeline(newCP[0], newCP[2]);
+                line const l2 = makeline(newCP[1], newCP[3]);
+                bool theyIntersect;
+                point mid;
+                findIntersection(&l1, &l2, &theyIntersect, &mid);
+                if (theyIntersect)
+                    quadCorner(newCP[0], newCP[2], newCP[1], newCP[3],
+                               mid, &quad2, NULL);
+                else {
+                    line const l1 = makeline(newCP[0], newCP[3]);
+                    line const l2 = makeline(newCP[1], newCP[2]);
+                    bool theyIntersect;
+                    point mid;
+                    findIntersection(&l1, &l2, &theyIntersect, &mid);
+                    if (theyIntersect)
+                        quadCorner(newCP[0], newCP[3],
+                                   newCP[1], newCP[2], mid, &quad2, NULL);
+                    else
+                        pm_error("The four new control points don't seem "
+                                 "to be corners.");
+                }
             }
         }
     }
@@ -1134,14 +1166,11 @@ warpTrig(point   const p2,
 
     point e1p1, e1p2, e1p3;
     point e2p1, e2p2, e2p3;
-    line lp, le;
-    line l1, l2, l3;
-    double d1, d2, d3;
-    int i;
+    unsigned int i;
 
     /* find in which triangle p2 lies */
-    for (i = 0; i < nTri; i++) {
-        if (insidetri (&tri2s[i], p2))
+    for (i = 0; i < nTri; ++i) {
+        if (insidetri(&tri2s[i], p2))
             break;
     }
 
@@ -1149,25 +1178,33 @@ warpTrig(point   const p2,
         *p1P = makepoint(0.0, 0.0);
     else {
         /* where in triangle is point */
-        d1 = fabs (p2.x - tri2s[i].p1.x) + fabs (p2.y - tri2s[i].p1.y);
-        d2 = fabs (p2.x - tri2s[i].p2.x) + fabs (p2.y - tri2s[i].p2.y);
-        d3 = fabs (p2.x - tri2s[i].p3.x) + fabs (p2.y - tri2s[i].p3.y);
-
-        /* line through p1 and p intersecting with edge p2-p3 */
-        lp = makeline(tri2s[i].p1, p2);
-        le = makeline(tri2s[i].p2, tri2s[i].p3);
-        intersect (&lp, &le, &e2p1);
-
-        /* line through p2 and p intersecting with edge p3-p1 */
-        lp = makeline(tri2s[i].p2, p2);
-        le = makeline(tri2s[i].p3, tri2s[i].p1);
-        intersect (&lp, &le, &e2p2);
+        double const d1 =
+            fabs (p2.x - tri2s[i].p1.x) + fabs (p2.y - tri2s[i].p1.y);
+        double const d2 =
+            fabs (p2.x - tri2s[i].p2.x) + fabs (p2.y - tri2s[i].p2.y);
+        double const d3 =
+            fabs (p2.x - tri2s[i].p3.x) + fabs (p2.y - tri2s[i].p3.y);
+
+        {
+            /* line through p1 and p intersecting with edge p2-p3 */
+            line const lp = makeline(tri2s[i].p1, p2);
+            line const le = makeline(tri2s[i].p2, tri2s[i].p3);
+            findIntersection(&lp, &le, NULL, &e2p1);
+        }
 
-        /* line through p3 and p intersecting with edge p1-p2 */
-        lp = makeline(tri2s[i].p3, p2);
-        le = makeline(tri2s[i].p1, tri2s[i].p2);
-        intersect (&lp, &le, &e2p3);
+        {
+            /* line through p2 and p intersecting with edge p3-p1 */
+            line const lp = makeline(tri2s[i].p2, p2);
+            line const le = makeline(tri2s[i].p3, tri2s[i].p1);
+            findIntersection(&lp, &le, NULL, &e2p2);
+        }
 
+        {
+            /* line through p3 and p intersecting with edge p1-p2 */
+            line const lp = makeline(tri2s[i].p3, p2);
+            line const le = makeline(tri2s[i].p1, tri2s[i].p2);
+            findIntersection(&lp, &le, NULL, &e2p3);
+        }
         /* map target control points to source control points */
         e1p1.x = tri1s[i].p2.x
             + (e2p1.x - tri2s[i].p2.x)/(tri2s[i].p3.x - tri2s[i].p2.x)
@@ -1188,17 +1225,19 @@ warpTrig(point   const p2,
             + (e2p3.y - tri2s[i].p1.y)/(tri2s[i].p2.y - tri2s[i].p1.y)
             * (tri1s[i].p2.y - tri1s[i].p1.y);
 
-        /* intersect grid lines in source */
-        l1 = makeline(tri1s[i].p1, e1p1);
-        l2 = makeline(tri1s[i].p2, e1p2);
-        l3 = makeline(tri1s[i].p3, e1p3);
-
-        if ((d1 < d2) && (d1 < d3))
-            intersect (&l2, &l3, p1P);
-        else if (d2 < d3)
-            intersect (&l1, &l3, p1P);
-        else
-            intersect (&l1, &l2, p1P);
+        {
+            /* intersect grid lines in source */
+            line const l1 = makeline(tri1s[i].p1, e1p1);
+            line const l2 = makeline(tri1s[i].p2, e1p2);
+            line const l3 = makeline(tri1s[i].p3, e1p3);
+
+            if ((d1 < d2) && (d1 < d3))
+                findIntersection(&l2, &l3, NULL, p1P);
+            else if (d2 < d3)
+                findIntersection(&l1, &l3, NULL, p1P);
+            else
+                findIntersection(&l1, &l2, NULL, p1P);
+        }
     }
 }
 
@@ -1215,8 +1254,6 @@ warpQuad(point   const p2,
     point c2tl, c2tr, c2bl, c2br;
     point e1t, e1b, e1l, e1r;
     point e2t, e2b, e2l, e2r;
-    line l2t, l2b, l2l, l2r;
-    line lh, lv;
 
     c1tl = quad1.tl;
     c1tr = quad1.tr;
@@ -1228,24 +1265,27 @@ warpQuad(point   const p2,
     c2bl = quad2.bl;
     c2br = quad2.br;
 
-    l2t = makeline(c2tl, c2tr);
-    l2b = makeline(c2bl, c2br);
-    l2l = makeline(c2tl, c2bl);
-    l2r = makeline(c2tr, c2br);
-
-    /* find intersections of lines through control points */
-    intersect (&l2t, &l2b, &h2);
-    intersect (&l2l, &l2r, &v2);
-
-    /* find intersections of axes through P with control point box */
-    lv = makeline(p2, v2);
-    intersect (&l2t, &lv, &e2t);
-    intersect (&l2b, &lv, &e2b);
-
-    lh = makeline(p2, h2);
-    intersect (&l2l, &lh, &e2l);
-    intersect (&l2r, &lh, &e2r);
-
+    {
+        line const l2t = makeline(c2tl, c2tr);
+        line const l2b = makeline(c2bl, c2br);
+        line const l2l = makeline(c2tl, c2bl);
+        line const l2r = makeline(c2tr, c2br);
+
+        /* find intersections of lines through control points */
+        findIntersection(&l2t, &l2b, NULL, &h2);
+        findIntersection(&l2l, &l2r, NULL, &v2);
+
+        {
+            /* find intersections of axes through P with control point box */
+            line const lv = makeline(p2, v2);
+            line const lh = makeline(p2, h2);
+
+            findIntersection(&l2t, &lv, NULL, &e2t);
+            findIntersection(&l2b, &lv, NULL, &e2b);
+            findIntersection(&l2l, &lh, NULL, &e2l);
+            findIntersection(&l2r, &lh, NULL, &e2r);
+        }
+    }
     /* map target control points to source control points */
     e1t.x = c1tl.x + (e2t.x - c2tl.x)/(c2tr.x - c2tl.x) * (c1tr.x - c1tl.x);
     if (c1tl.y == c1tr.y)
@@ -1275,10 +1315,12 @@ warpQuad(point   const p2,
             = c1tr.x + (e2r.y - c2tr.y)/(c2br.y - c2tr.y) * (c1br.x - c1tr.x);
     e1r.y = c1tr.y + (e2r.y - c2tr.y)/(c2br.y - c2tr.y) * (c1br.y - c1tr.y);
 
-    /* intersect grid lines in source */
-    lv = makeline(e1t, e1b);
-    lh = makeline(e1l, e1r);
-    intersect (&lh, &lv, p1P);
+    {
+        /* intersect grid lines in source */
+        line const lv = makeline(e1t, e1b);
+        line const lh = makeline(e1l, e1r);
+        findIntersection(&lh, &lv, NULL, p1P);
+    }
 }
 
 
diff --git a/editor/pbmclean.c b/editor/pbmclean.c
index 08f410c0..47c775e5 100644
--- a/editor/pbmclean.c
+++ b/editor/pbmclean.c
@@ -381,7 +381,7 @@ cleanSimple(FILE *             const ifP,
    Do the traditional clean where you look only at the immediate neighboring
    pixels of a subject pixel to determine whether to erase that pixel.
 -----------------------------------------------------------------------------*/
-    bit ** buffer;
+    bit ** buffer;  /* one bit per pixel */
         /* The rows of the input relevant to our current processing:
            the current row and the one above and below it.
         */
@@ -399,7 +399,7 @@ cleanSimple(FILE *             const ifP,
     setupInputBuffers(ifP, cols, format, &buffer, &edgeRow,
                       &thisRow, &nextRow);
 
-    outRow = pbm_allocrow(cols);
+    outRow = pbm_allocrow_packed(cols);
 
     pbm_writepbminit(ofP, cols, rows, 0) ;
 
@@ -756,12 +756,12 @@ cleanExtended(FILE *             const ifP,
    We erase (flip) every pixel in every trivial blob.  A trivial blob is
    one with 'trivialSize' pixels or fewer.
 -----------------------------------------------------------------------------*/
-    bit ** pixels;
+    bit ** pixels;    /* one byte per pixel */
     int cols, rows;
 
     pixels = pbm_readpbm(ifP, &cols, &rows);
 
-	cleanPixels(pixels, cols, rows, foregroundColor, trivialSize, nFlippedP);
+        cleanPixels(pixels, cols, rows, foregroundColor, trivialSize, nFlippedP);
 
     pbm_writepbm(ofP, pixels, cols, rows, 0);
 
diff --git a/editor/pbmpscale.c b/editor/pbmpscale.c
index 3b6935b2..434c7965 100644
--- a/editor/pbmpscale.c
+++ b/editor/pbmpscale.c
@@ -12,7 +12,7 @@
 #define LEFTBITS pm_byteLeftBits
 #define RIGHTBITS pm_byteRightBits
 
-/* Table for translating bit pattern into "corners" flag element */ 
+/* Table for translating bit pattern into "corners" flag element */
 
 unsigned char const
 transTable[512] = {
@@ -191,8 +191,8 @@ setFlags(const bit *     const prevrow,
          unsigned char * const flags,
          unsigned int    const cols ) {
 /*----------------------------------------------------------------------------
-   Scan one row, examining the row above and row below, and determine 
-   whether there are "corners" for each pixel.  Feed a 9 bit sample into 
+   Scan one row, examining the row above and row below, and determine
+   whether there are "corners" for each pixel.  Feed a 9 bit sample into
    pre-calculated array transTable[512] to calculate all four corner statuses
    at once.
 
@@ -220,7 +220,7 @@ setFlags(const bit *     const prevrow,
        The high byte of the patterns is a mask, which determines which bits are
        not ignored.
     */
-    uint16_t const patterns[] 
+    uint16_t const patterns[]
         = { 0x0000,   0xd555,            /* no corner */
             0x0001,   0xffc1, 0xd514,    /* normal corner */
             0x0002,   0xd554, 0xd515, 0xbea2, 0xdfc0, 0xfd81, 0xfd80, 0xdf80,
@@ -230,7 +230,7 @@ setFlags(const bit *     const prevrow,
     /*
       For example, the NE corner is examined with the following 8 bit sample:
       Current : W : NW : N : NE : E : SE : S
-      (SW is the "square behind") 
+      (SW is the "square behind")
       */
 #endif
 
@@ -257,7 +257,7 @@ setFlags(const bit *     const prevrow,
         sample = ( ( prevrow24 >> ( 8 -offset) ) & 0x01c0 )
             | ( ( thisrow24 >> (11 -offset) ) & 0x0038 )
             | ( ( nextrow24 >> (14 -offset) ) & 0x0007 );
-        
+
         flags[col] =  transTable[sample];
     }
 }
@@ -275,14 +275,14 @@ expandRow(const bit *     const thisrow,
           int             const ucutoff) {
 /*----------------------------------------------------------------------------
   Process one row, using flags array as reference.  If pixel has no corners
-  output a NxN square of the given color, otherwise output with the 
+  output a NxN square of the given color, otherwise output with the
   specified corner area(s) clipped off.
 -----------------------------------------------------------------------------*/
     unsigned int const outcols = cols * scale;
 
     unsigned int i;
     unsigned int col;
-    
+
     for (i = 0; i < scale; ++i) {
         int const zone = (i > ucutoff) - (i < cutoff);
         int const cut1 =
@@ -297,7 +297,7 @@ expandRow(const bit *     const thisrow,
         cut[1] = cut1;
         cut[2] = cut1 ? cut1 - 1 : 0;
         cut[3] = (cut1 && cutoff > 1) ? cut1 - 1 : cut1;
-      
+
         for (col = 0; col < cols; ++col) {
             unsigned int const col8 = col / 8;
             unsigned int const offset = col % 8;
@@ -309,11 +309,11 @@ expandRow(const bit *     const thisrow,
             if (flag == 0x00) {
                 /* There are no corners, no color change */
                 outcol += scale;
-            } else { 
+            } else {
                 switch (zone) {
                 case -1:
                     if (i==0 && flag == 0xff) {
-                        /* No corners, color changed */ 
+                        /* No corners, color changed */
                         cutl = cutr = 0;
                         flags[col] = 0x00;
                             /* Use above skip procedure next cycle */
@@ -330,7 +330,7 @@ expandRow(const bit *     const thisrow,
                     cutr = cut[SE(flag)];
                     break;
                 }
-                
+
                 if (cutl > 0) {
                     writeBitSpan(outrow, cutl, outcol, !pix);
                     outcol += cutl;
@@ -384,10 +384,10 @@ main(int argc, const char ** argv) {
 
     pbm_readpbminit(ifP, &cols, &rows, &format) ;
 
-    validateComputableDimensions(cols, rows, cmdline.scale); 
+    validateComputableDimensions(cols, rows, cmdline.scale);
 
     outcols = cols * cmdline.scale;
-    outrows = rows * cmdline.scale; 
+    outrows = rows * cmdline.scale;
 
     /* Initialize input buffers.
        We add a margin of 8 bits on the right of the three rows.
@@ -402,7 +402,7 @@ main(int argc, const char ** argv) {
     for (i = 0; i < pbm_packed_bytes(cols + 8); ++i)
         edgerow[i] = 0x00;
 
-    /* Add blank bytes at right edges */ 
+    /* Add blank bytes at right edges */
     for (i = 0; i < 3; ++i)
         buffer[i][pbm_packed_bytes(cols + 8) - 1] = 0x00;
 
diff --git a/editor/pnmcat.c b/editor/pnmcat.c
index 9097dc99..fea80181 100644
--- a/editor/pnmcat.c
+++ b/editor/pnmcat.c
@@ -115,7 +115,7 @@ parseCommandLine(int argc, const char ** const argv,
         cmdlineP->backcolor = BACK_AUTO;
 
     if (jtop + jbottom + jleft + jright + jcenter > 1)
-        pm_error("You may specify onlyone of -jtop, -jbottom, "
+        pm_error("You may specify only one of -jtop, -jbottom, "
                  "-jleft, and -jright");
     else {
         switch (cmdlineP->orientation) {
@@ -703,6 +703,9 @@ concatenateLeftRightGen(FILE *             const ofP,
                 */
             }
         }
+        /* Note that img2[N].inrow{] is an alias to part of outrow[], so
+           outrow[] has been set.
+        */
         pnm_writepnmrow(ofP, outrow, newcols, newmaxval, newformat, 0);
     }
     pnm_freerow(outrow);
diff --git a/editor/pnmpad.c b/editor/pnmpad.c
index 5cef21af..9fa9f9e6 100644
--- a/editor/pnmpad.c
+++ b/editor/pnmpad.c
@@ -129,16 +129,16 @@ parseCommandLine(int argc, const char ** argv,
        pm_error("You can specify -height only once");
 
     if (xalignSpec && (cmdlineP->leftSpec || cmdlineP->rightSpec))
-        pm_error("You cannot specify both -xalign and -left or -right");
+        pm_error("You cannot specify both -halign and -left or -right");
 
     if (yalignSpec && (cmdlineP->topSpec || cmdlineP->bottomSpec))
-        pm_error("You cannot specify both -yalign and -top or -bottom");
+        pm_error("You cannot specify both -valign and -top or -bottom");
 
-    if (xalignSpec && !cmdlineP->xsizeSpec)
-        pm_error("-xalign is meaningless without -width");
+    if (xalignSpec && (!cmdlineP->xsizeSpec && !mwidthSpec) )
+        pm_error("-halign is meaningless without -width or -mwidth");
 
-    if (yalignSpec && !cmdlineP->ysizeSpec)
-        pm_error("-yalign is meaningless without -height");
+    if (yalignSpec && (!cmdlineP->ysizeSpec && !mheightSpec) )
+        pm_error("-valign is meaningless without -height or -mheight");
 
     if (xalignSpec) {
         if (cmdlineP->xalign < 0)
@@ -268,7 +268,7 @@ validateHorizontalSize(struct cmdlineInfo const cmdline,
         pm_error("The right padding value you specified is too large.");
 
     if ((double) cols +
-        (double) lpad + 
+        (double) lpad +
         (double) rpad +
         (double) mwidthMaxPad > MAX_WIDTHHEIGHT)
         pm_error("Given padding parameters make output width too large "
@@ -378,9 +378,9 @@ computePadSizesOneDim(unsigned int   const unpaddedSize,
         unsigned int const totalPadBeforeMult =
             begPadBeforeMult + endPadBeforeMult;
         double const begFrac =
-            totalPadBeforeMult > 0 ? 
+            totalPadBeforeMult > 0 ?
             (double)begPadBeforeMult / totalPadBeforeMult :
-            0.0;
+            align;
         unsigned int const addlMsizeBegPad = ROUNDU(morePadNeeded * begFrac);
             /* # of pixels we have to add to the beginning to satisfy
                user's desire for the final size to be a multiple of something
@@ -501,7 +501,7 @@ reportPadSizes(int          const inCols,
 
     unsigned int const outCols = inCols + lpad + rpad;
     unsigned int const outRows = inRows + tpad + bpad;
- 
+
     printf("%u %u %u %u %u %u\n", lpad, rpad, tpad, bpad, outCols, outRows);
 
 }
@@ -547,7 +547,7 @@ padPbm(FILE *       const ifP,
     /* Write top margin */
     for (row = 0; row < tpad; ++row)
         pbm_writepbmrow_packed(stdout, bgrow, newcols, 0);
-    
+
     /* Read rows, shift and write with left and right margins added */
     for (row = 0; row < rows; ++row) {
         pbm_readpbmrow_bitoffset(ifP, newrow, cols, format, lpad);
diff --git a/editor/specialty/pampaintspill.c b/editor/specialty/pampaintspill.c
index 5cd482d5..7490fcef 100644
--- a/editor/specialty/pampaintspill.c
+++ b/editor/specialty/pampaintspill.c
@@ -6,7 +6,7 @@
  *
  * ----------------------------------------------------------------------
  *
- * Copyright (C) 2010 Scott Pakin <scott+pbm@pakin.org>
+ * Copyright (C) 2010-2021 Scott Pakin <scott+pbm@pakin.org>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -69,6 +69,8 @@ struct cmdlineInfo {
     unsigned int downsample;
     unsigned int randomseedSpec;
     unsigned int randomseed;
+    unsigned int nearSpec;
+    unsigned int near;
 };
 
 struct coords {
@@ -112,6 +114,8 @@ parseCommandLine(int argc, const char ** const argv,
             &downsampleSpec, 0);
     OPTENT3(0, "randomseed", OPT_UINT,   &cmdlineP->randomseed,
             &cmdlineP->randomseedSpec, 0);
+    OPTENT3(0, "near",       OPT_UINT,   &cmdlineP->near,
+            &cmdlineP->nearSpec, 0);
 
     opt.opt_table = option_def;
     opt.short_allowed = 0;
@@ -128,6 +132,11 @@ parseCommandLine(int argc, const char ** const argv,
     if (!downsampleSpec)
         cmdlineP->downsample = 0;
 
+    if (cmdlineP->nearSpec) {
+        if (cmdlineP->near == 0)
+            pm_error("The -near option requires a positive argument");
+    }
+
     if (argc-1 < 1)
         cmdlineP->inputFilename = "-";
     else {
@@ -365,6 +374,70 @@ reportProgress(unsigned int const rowsComplete,
 
 
 
+struct distanceList {
+    struct coords * sources;  /* malloc'ed */
+        /* The list of places in the image from which paint comes */
+    double * distSqrs;        /* malloc'ed */
+        /* The list of squared distances from the current point */
+    unsigned int size;
+        /* Number of entries in sources[] */
+};
+
+
+
+static void
+computeDistances(struct pam *           const pamP,
+                 struct coords          const target,
+                 struct paintSourceSet  const paintSources,
+                 distFunc_t *           const distFunc,
+                 bool                   const nearOnly,
+                 unsigned int           const numNear,
+                 struct distanceList *  const distancesP) {
+
+    unsigned int ps;
+
+    /* Acquire a list of all distances. */
+    distancesP->size = 0;
+    for (ps = 0; ps < paintSources.size; ++ps) {
+        struct coords const source = paintSources.list[ps];
+        double const distSqr =
+            (*distFunc)(&target, &source,
+                        pamP->width, pamP->height);
+        distancesP->sources[distancesP->size]  = source;
+        distancesP->distSqrs[distancesP->size] = distSqr;
+        ++distancesP->size;
+    }
+
+    /* If requested, truncate the list to the smallest numNear distances. */
+    if (nearOnly && numNear < distancesP->size) {
+        unsigned int i;
+
+        /* Perform a partial sort -- just enough to identify the numNear
+           smallest distances.  For performance reasons we assume that
+           numNear is much less than paintSources.size (say, less than
+           log2(paintSources.size)).
+        */
+        for (i = 0; i < numNear; ++i) {
+            unsigned int j;
+            for (j = i + 1; j < distancesP->size; ++j) {
+                if (distancesP->distSqrs[i] > distancesP->distSqrs[j]) {
+                    /* Swap elements i and j. */
+                    struct coords const src   = distancesP->sources[i];;
+                    double        const dist2 = distancesP->distSqrs[i];
+
+                    distancesP->sources[i]  = distancesP->sources[j];
+                    distancesP->distSqrs[i] = distancesP->distSqrs[j];
+                    distancesP->sources[j]  = src;
+                    distancesP->distSqrs[j] = dist2;
+                }
+            }
+        }
+        distancesP->size = numNear;
+    }
+}
+
+
+
 static void
 spillOnePixel(struct pam *          const pamP,
               struct coords         const target,
@@ -372,37 +445,38 @@ spillOnePixel(struct pam *          const pamP,
               distFunc_t *          const distFunc,
               double                const distPower,
               tuple                 const outTuple,
-              double *              const newColor) {
+              double *              const newColor,
+              bool                  const nearOnly,
+              unsigned int          const numNear,
+              struct distanceList * const distancesP) {
 
     unsigned int plane;
-    unsigned int ps;
+    unsigned int d;
     double       totalWeight;
 
     for (plane = 0; plane < pamP->depth; ++plane)
         newColor[plane] = 0.0;
+    computeDistances(pamP, target, paintSources, distFunc,
+                     nearOnly, numNear, distancesP);
     totalWeight = 0.0;
-    for (ps = 0; ps < paintSources.size; ++ps) {
-        struct coords const source = paintSources.list[ps];
-        double const distSqr =
-            (*distFunc)(&target, &source,
-                        pamP->width, pamP->height);
+    for (d = 0; d < distancesP->size; ++d) {
+        double        const distSqr = distancesP->distSqrs[d];
+        struct coords const source  = distancesP->sources[d];
 
-        if (distSqr > 0.0) {
-            /* We do special cases for some common cases with code
-               that is much faster than pow().
-            */
-            double const weight =
-                distPower == -2.0 ? 1.0 / distSqr :
-                distPower == -1.0 ? 1.0 / sqrt(distSqr):
-                pow(distSqr, distPower/2);
+        /* We do special cases for some common cases with code
+           that is much faster than pow().
+        */
+        double const weight =
+            distPower == -2.0 ? 1.0 / distSqr :
+            distPower == -1.0 ? 1.0 / sqrt(distSqr):
+            pow(distSqr, distPower/2);
 
-            unsigned int plane;
+        unsigned int plane;
 
-            for (plane = 0; plane < pamP->depth; ++plane)
-                newColor[plane] += weight * source.color[plane];
+        for (plane = 0; plane < pamP->depth; ++plane)
+            newColor[plane] += weight * source.color[plane];
 
-            totalWeight += weight;
-        }
+        totalWeight += weight;
     }
     for (plane = 0; plane < pamP->depth; ++plane)
         outTuple[plane] = (sample) (newColor[plane] / totalWeight);
@@ -418,6 +492,8 @@ produceOutputImage(struct pam *          const pamP,
                    distFunc_t *          const distFunc,
                    double                const distPower,
                    bool                  const all,
+                   bool                  const nearOnly,
+                   unsigned int          const numNear,
                    tuple ***             const outtuplesP) {
 /*--------------------------------------------------------------------
   Color each background pixel (or, if allPixels is 1, all pixels)
@@ -433,10 +509,14 @@ produceOutputImage(struct pam *          const pamP,
     rowsComplete = 0;
     #pragma omp parallel for
     for (row = 0; row < pamP->height; ++row) {
-        struct coords   target;
-        double        * newColor;
+        struct coords         target;
+        double *              newColor;   /* malloc'ed */
+        struct distanceList * distancesP; /* malloc'ed */
 
         MALLOCARRAY(newColor, pamP->depth);
+        MALLOCVAR_NOFAIL(distancesP);
+        MALLOCARRAY_NOFAIL(distancesP->sources,  paintSources.size);
+        MALLOCARRAY_NOFAIL(distancesP->distSqrs, paintSources.size);
 
         target.y = row;
         for (target.x = 0; target.x < pamP->width; ++target.x) {
@@ -445,13 +525,17 @@ produceOutputImage(struct pam *          const pamP,
 
             if (all || tupleEqualColor(pamP, targetTuple, bgColor))
                 spillOnePixel(pamP, target, paintSources, distFunc, distPower,
-                              outputTuple, newColor);
+                              outputTuple, newColor, nearOnly, numNear,
+                              distancesP);
             else
                 pnm_assigntuple(pamP,  outputTuple, targetTuple);
         }
         #pragma omp critical (rowTally)
         reportProgress(++rowsComplete, pamP->height);
 
+        free(distancesP->distSqrs);
+        free(distancesP->sources);
+        free(distancesP);
         free(newColor);
     }
     *outtuplesP = outtuples;
@@ -497,7 +581,8 @@ main(int argc, const char *argv[]) {
                        cmdline.randomseedSpec, cmdline.randomseed);
 
     produceOutputImage(&inPam, inTuples, bgColor, paintSources, distFunc,
-                       cmdline.power, cmdline.all, &outTuples);
+                       cmdline.power, cmdline.all,
+                       cmdline.nearSpec, cmdline.near, &outTuples);
 
     outPam = inPam;
     outPam.file = stdout;
@@ -510,3 +595,4 @@ main(int argc, const char *argv[]) {
 }
 
 
+
diff --git a/generator/Makefile b/generator/Makefile
index d54a6cc5..761181bd 100644
--- a/generator/Makefile
+++ b/generator/Makefile
@@ -18,7 +18,7 @@ SUBDIRS = pamtris
 
 PORTBINARIES = pamcrater pamgauss pamgradient \
 	       pamseq pamshadedrelief pamstereogram \
-	       pbmpage pbmmake pbmtext pbmupc \
+	       pbmpage pbmmake pbmnoise pbmtext pbmupc \
 	       pgmkernel pgmmake pgmnoise pgmramp \
 	       ppmcie ppmcolors ppmforge ppmmake ppmpat ppmrough ppmwheel \
 
diff --git a/generator/pbmnoise.c b/generator/pbmnoise.c
new file mode 100644
index 00000000..9a537d5c
--- /dev/null
+++ b/generator/pbmnoise.c
@@ -0,0 +1,484 @@
+/* pbmnoise.c - create a random bitmap of a specified size
+                with a specified ratio of white/black pixels
+
+   Written by Akira F Urushibata and contributed to the public domain 
+   December 2021
+*/
+
+#include <math.h>
+#include <string.h>
+#include <assert.h>
+
+#include "pm_c_util.h"
+#include "shhopt.h"
+#include "mallocvar.h"
+#include "rand.h"
+#include "nstring.h"
+
+#include "pbm.h"
+
+
+
+static void
+parseFraction(const char *   const fraction,
+              unsigned int * const numeratorP,
+              unsigned int * const precisionP) {
+
+    unsigned int numerator, denominator;
+
+    sscanf(fraction, "%u/%u", &numerator, &denominator);
+
+    if (denominator > 65536)
+        pm_error("Denominator (%u) too large.", denominator);
+    else if (denominator == 0 || (denominator & (denominator - 1)) != 0)
+        pm_error("Denominator must be a power of two.  You specified %u.",
+                 denominator);
+    else if (numerator > denominator)
+        pm_error("Invalid fraction (%s).  Denominator must be larger than "
+                 "numerator.", fraction);
+    else {
+        /* Reduce fraction to lowest terms */
+        unsigned int numerator2, denominator2;
+            /* The fraction reduced to lowest terms */
+        unsigned int precision2;
+
+        if (numerator == 0) { /* all white image */
+            numerator2   = 0;
+            denominator2 = 1;
+            precision2   = 0;
+        } else if (numerator == denominator) { /* all black */
+            numerator2   = 1;
+            denominator2 = 1;
+            precision2   = 0;
+        } else {
+            numerator2   = numerator;   /* initial value */
+            denominator2 = denominator; /* initial value */
+
+            while ((numerator2 & 0x01) != 0x01) {
+                denominator2 /= 2;
+                numerator2   /= 2;
+            }
+            precision2 = 1;
+        }
+
+      if (denominator != denominator2)
+          pm_message("Ratio %u/%u = %u/%u",
+                     numerator, denominator, numerator2, denominator2);
+
+      *precisionP = (precision2 == 0) ? 0 : pm_maxvaltobits(denominator2 - 1);
+          /* pm_maxvaltobits(N):  Max of N is 65535 */
+
+      *numeratorP = numerator2;
+    }
+}
+
+
+
+static void
+setRatio(const char *   const ratioArg,
+         unsigned int * const numeratorP,
+         unsigned int * const precisionP) {
+/*----------------------------------------------------------------------------
+    Convert string "ratioArg" to ratio: numerator / (2 ^ precision) The input
+    string must be in fraction "n/d" form and the denominator must be a power
+    of 2.
+
+    Ratio is the probability of one binary digit being "1".  The ratio of "1"
+    (=PBM black) pixels in the entire output image will be close to this
+    value.
+
+    Most invalid strings are rejected here.
+----------------------------------------------------------------------------*/
+    if (strspn(ratioArg, "0123456789/") == strlen(ratioArg) &&
+             ratioArg[0] != '/' &&
+             ratioArg[strlen(ratioArg) - 1] != '/' &&
+             strchr(ratioArg, '/') != NULL &&
+             strchr(ratioArg, '/') == strrchr(ratioArg, '/'))
+        parseFraction(ratioArg, numeratorP, precisionP);
+    else
+        pm_error("Invalid ratio: '%s'", ratioArg);
+}
+
+
+
+struct CmdlineInfo {
+    /* All the information the user supplied in the command line,
+       in a form easy for the program to use.
+    */
+    unsigned int width;
+    unsigned int height;
+    unsigned int numerator;
+    unsigned int precision;
+    unsigned int randomseed;
+    unsigned int randomseedSpec;
+    unsigned int bswap;   /* boolean */
+    unsigned int pack;    /* boolean */
+};
+
+
+
+static void
+parseCommandLine(int argc, const char ** 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;
+        /* Instructions to pm_optParseOptions3 on how to parse our options.
+         */
+    optStruct3 opt;
+
+    unsigned int option_def_index;
+    const char * ratioArg;
+    unsigned int ratioSpec;
+    const char * endianArg;
+    unsigned int endianSpec;
+
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
+    option_def_index = 0;   /* incremented by OPTENTRY */
+    OPTENT3(0, "ratio",         OPT_STRING, &ratioArg,
+            &ratioSpec,                     0);
+    OPTENT3(0, "randomseed",    OPT_UINT,   &cmdlineP->randomseed,
+            &cmdlineP->randomseedSpec,      0);
+    OPTENT3(0, "endian",        OPT_STRING, &endianArg,
+            &endianSpec,                    0);
+    OPTENT3(0, "pack",          OPT_FLAG,   NULL,
+            &cmdlineP->pack,                0);
+
+    opt.opt_table = option_def;
+    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
+    opt.allowNegNum = FALSE;  /* We may have parms that are negative numbers */
+
+    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
+    /* Uses and sets argc, argv, and some of *cmdlineP and others. */
+    free(option_def);
+
+    if (ratioSpec)
+        setRatio(ratioArg, &cmdlineP->numerator, &cmdlineP->precision);
+    else {
+        /* Set ratio to default: 1/2 */
+        cmdlineP->numerator = 1;
+        cmdlineP->precision = 1;
+    }
+
+    if (!endianSpec)
+        cmdlineP->bswap = false;
+    else {
+        if      (streq(endianArg, "native"))
+            cmdlineP->bswap = false;
+        else if (streq(endianArg, "swap"))
+            cmdlineP->bswap = true;
+        else if (streq(endianArg, "big"))
+            cmdlineP->bswap = (BYTE_ORDER == LITTLE_ENDIAN);
+        else if (streq(endianArg, "little"))
+            cmdlineP->bswap = (BYTE_ORDER != LITTLE_ENDIAN);
+        else
+            pm_error("Invalid value '%s' for -endian argument.", endianArg);
+    }
+
+    if (argc-1 != 2)
+        pm_error("Wrong number of arguments (%d).  There are two "
+                 "non-option arguments: width and height in pixels",
+                 argc-1);
+    else {
+        cmdlineP->width  = pm_parse_width (argv[1]);
+        cmdlineP->height = pm_parse_height(argv[2]);
+    }
+}
+
+
+
+static void
+writeSingleColorRaster(unsigned int const cols,
+                       unsigned int const rows,
+                       bit          const color,
+                       FILE *       const ofP) {
+/*-----------------------------------------------------------------------------
+  Generate a single-color raster of color 'color', dimensions
+  'cols' by 'rows', to output file *ofP.
+-----------------------------------------------------------------------------*/
+    unsigned int const lastColChar = (cols - 1) / 8;
+
+    unsigned char * bitrow0;
+    unsigned int i;
+
+    bitrow0 = pbm_allocrow_packed(cols + 32);
+
+    for (i = 0; i <= lastColChar; ++i)
+        bitrow0[i] = color * 0xff;
+
+    if (color != 0)
+        pbm_cleanrowend_packed(bitrow0, cols);
+
+    /* row end trimming, not necessary with white */
+
+    {
+        unsigned int row;
+        for (row = 0; row < rows; ++row)
+            pbm_writepbmrow_packed(ofP, bitrow0, cols, 0);
+    }
+    pbm_freerow(bitrow0);
+}
+
+
+
+static uint32_t
+randombits(unsigned int       const precision,
+           unsigned int       const numerator,
+           struct pm_randSt * const randStP) {
+/*----------------------------------------------------------------------------
+  Generate 32 random bits so that for each bit the probability of "1"
+  being generated is numerator / (2 ^ precision).
+
+  How this works:
+
+  Ratios such as 1/8, 7/8, 1/16, 15/16, 1/32, 31/32 are straightforward.  How
+  do you get intermediate values such as 3/8, 5/8, 3/16, 5/16, 7/16?
+
+  Imagine a set of 10 bits which are 90% 1, 10% 0 and a random number source
+  which produces 1 and 0 in even proportions.
+
+  Conduct "and" and "or" on these bits:
+
+           0011111111 (90%)       0011111111 (90%)
+      and) 0101010101 (50%)   or) 0101010101 (50%)
+      ---------------------   --------------------
+           0001010101 (45%)       0111111111 (95%)
+
+  It can be seen from this example that an "and" operation gives a new ratio
+  which is halfway between the old ratio (90% in this example) and 0%, while
+  "or" gives one at the middle of the old ratio and 100% The corresponding
+  binary operations for fixed-point fractions are: "right-shift by one and
+  insert a 0 behind the fixed point" and "right-shift by one and insert a 1
+  behind the fixed point" respecatbly.
+
+  115/128 = 89.84% (near 90%)  In binary fix-point: 0.1110011
+  0.01110011 = 115/256 = 44.92%
+  0.11110011 = 243/256 = 94.92%
+
+  So to achieve the desired ratio, start at the LSB (right end) of
+  'numerator'.  Initialize the output bits to zero.  Conduct an "and" for each
+  0 and an "or" for each 1 with a freshly drawn random number until the fixed
+  point is reached.
+
+  An "and" operation of a random number and zero always yields zero.  To avoid
+  waste, we reduce terms to eliminate the trailing zeroes in 'numerator' and
+  indicate the fixed point with 'precision'.  Each time the program is
+  executed the location of the fixed point is set anew, but it stays constant
+  until the program exits.
+----------------------------------------------------------------------------*/
+    unsigned int i;
+    uint32_t mask;
+    uint32_t retval;
+
+    for (i = 0, mask = 0x01, retval=0x00000000;
+         i < precision;
+         ++i, mask <<= 1) {
+
+        uint32_t const randval = pm_rand32(randStP);
+
+        if ((numerator & mask) != 0 )
+            retval |= randval;
+        else
+            retval &= randval;
+    }
+    return retval;
+}
+
+
+
+static uint32_t
+swapWord(uint32_t const word) {
+/*----------------------------------------------------------------------------
+  Swap four bytes.
+  This swap method works regardless of native system endianness.
+----------------------------------------------------------------------------*/
+    uint32_t const retval =
+        ((word >> 24) & 0xff) |
+        ((word >>  8) & 0xff00) |
+        ((word <<  8) & 0xff0000) |
+        ((word << 24) & 0xff000000)
+        ;
+
+    return retval;
+}
+
+
+
+static void
+swapBitrow(unsigned char * const bitrow,
+           unsigned int    const words,
+           bool            const bswap) {
+/*----------------------------------------------------------------------------
+  Modify bits in 'bitrow', swapping as indicated.
+----------------------------------------------------------------------------*/
+    uint32_t * const bitrowByWord = (uint32_t *) bitrow;
+
+    unsigned int wordCnt;
+
+    for (wordCnt=0; wordCnt < words; ++wordCnt) {
+        uint32_t const inWord = bitrowByWord[wordCnt];
+
+        bitrowByWord[wordCnt] = bswap ? swapWord(inWord) : inWord;
+    }
+}
+
+
+
+static void
+pbmnoise(FILE *             const ofP,
+         unsigned int       const cols,
+         unsigned int       const rows,
+         unsigned int       const numerator,
+         unsigned int       const precision,
+         bool               const bswap,
+         struct pm_randSt * const randStP) {
+/*----------------------------------------------------------------------------
+  Default method of constructing rows.
+
+  Generate pixels in units of 32 bits.
+
+  If cols is not a multiple of 32, discard pixels beyond row end.
+-----------------------------------------------------------------------------*/
+    unsigned int const words = (cols + 31) / 32;
+
+    unsigned char * bitrow;
+    unsigned int row;
+    unsigned int wordCnt;
+
+    bitrow = pbm_allocrow_packed(cols + 32);
+
+    for (row = 0; row < rows; ++row) {
+        uint32_t * const bitrowByWord = (uint32_t *) bitrow;
+
+        for (wordCnt = 0; wordCnt < words; ++wordCnt)
+            bitrowByWord[wordCnt] = randombits(precision, numerator, randStP);
+
+        if (bswap)
+            swapBitrow(bitrow, words, bswap);
+
+        pbm_cleanrowend_packed(bitrow, cols);
+        pbm_writepbmrow_packed(ofP, bitrow, cols, 0);
+    }
+    pbm_freerow(bitrow);
+}
+
+
+
+static void
+pbmnoise_packed(FILE *             const ofP,
+                unsigned int       const cols,
+                unsigned int       const rows,
+                unsigned int       const numerator,
+                unsigned int       const precision,
+                bool               const bswap,
+                struct pm_randSt * const randStP) {
+/*----------------------------------------------------------------------------
+  Alternate method of constructing rows.
+  Like the default pbmnoise(), generate pixels in units of 32 bits
+  but carry over unused pixel data at row end to the next row.
+-----------------------------------------------------------------------------*/
+    unsigned char * bitrow0;
+    uint32_t * bitrowByWord;
+    unsigned int offset;
+    unsigned int row;
+    uint32_t wordSave;    /* Pixels carried over to next row */
+
+    bitrow0 = pbm_allocrow_packed(cols + 63);
+    bitrowByWord = (uint32_t *) bitrow0;
+
+    for (row = 0, offset = 0; row < rows; ++row) {
+        if (offset == 0) {
+            unsigned int const words = (cols + 31 ) / 32;
+
+            unsigned int wordCnt;
+
+            for (wordCnt = 0; wordCnt< words; ++wordCnt) {
+                bitrowByWord[wordCnt] =
+                    randombits(precision, numerator, randStP);
+            }
+
+            if (bswap)
+                swapBitrow(bitrow0, words, bswap);
+
+            wordSave = bitrowByWord[words - 1];
+
+            pbm_writepbmrow_packed(ofP, bitrow0, cols, 0);
+            offset = cols % 32;
+        } else {
+            unsigned int const wordsToFetch = (cols - (32 - offset) + 31) / 32;
+            unsigned int const lastWord = wordsToFetch;
+
+            unsigned int wordCnt;
+
+            bitrowByWord[0] = wordSave;
+
+            for (wordCnt = 0; wordCnt < wordsToFetch; ++wordCnt) {
+                bitrowByWord[wordCnt + 1] =
+                    randombits(precision, numerator, randStP);
+            }
+
+            if (bswap)
+                swapBitrow((unsigned char *) & bitrowByWord[1],
+                           wordsToFetch, bswap);
+
+            wordSave = bitrowByWord [lastWord];
+
+            pbm_writepbmrow_bitoffset(ofP, bitrow0, cols, 0, offset);
+            offset = (offset + cols) % 32;
+        }
+    }
+    pbm_freerow(bitrow0);
+}
+
+
+int
+main(int argc, const char *argv[]) {
+
+    struct CmdlineInfo cmdline;
+
+    pm_proginit(&argc, argv);
+
+    parseCommandLine(argc, argv, &cmdline);
+
+    pbm_writepbminit(stdout, cmdline.width, cmdline.height, 0);
+
+    if (cmdline.precision == 0) {
+        bit color;
+
+        if (cmdline.numerator == 0)
+            color = PBM_WHITE;
+        else {
+            assert (cmdline.numerator == 1);
+            color = PBM_BLACK;
+        }
+        writeSingleColorRaster(cmdline.width, cmdline.height, color, stdout);
+    } else if (cmdline.width % 32 == 0 || !cmdline.pack) {
+        struct pm_randSt randSt;
+
+        pm_randinit(&randSt);
+        pm_srand2(&randSt, cmdline.randomseedSpec, cmdline.randomseed);
+
+        pbmnoise(stdout, cmdline.width, cmdline.height,
+                 cmdline.numerator, cmdline.precision,
+                 cmdline.bswap, &randSt);
+
+        pm_randterm(&randSt);
+    } else {
+        struct pm_randSt randSt;
+
+        pm_randinit(&randSt);
+        pm_srand2(&randSt, cmdline.randomseedSpec, cmdline.randomseed);
+
+        pbmnoise_packed(stdout, cmdline.width, cmdline.height,
+                        cmdline.numerator, cmdline.precision,
+                        cmdline.bswap, &randSt);
+
+        pm_randterm(&randSt);
+    }
+    return 0;
+}
+
+
diff --git a/generator/pbmtext.c b/generator/pbmtext.c
index 7ea64857..a4566d12 100644
--- a/generator/pbmtext.c
+++ b/generator/pbmtext.c
@@ -1282,6 +1282,8 @@ computeImageWidth(struct Text          const formattedText,
                   unsigned int *       const colsP,
                   unsigned int *       const maxleftbP) {
 
+    assert (pbm_maxfontwidth() < (INT_MAX - 10) / LINEBUFSIZE);
+
     if (intercharacterSpace < 0 && fontP->maxwidth < -intercharacterSpace)
         pm_error("negative -space value %.2f exceeds font width",
                  intercharacterSpace);
diff --git a/generator/ppmmake.c b/generator/ppmmake.c
index 7bac9601..b1de7b52 100644
--- a/generator/ppmmake.c
+++ b/generator/ppmmake.c
@@ -31,11 +31,11 @@ static void
 parseCommandLine(int argc, char ** argv,
                  struct cmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
-  Convert program invocation arguments (argc,argv) into a format the 
+  Convert program invocation arguments (argc,argv) into a format the
   program can use easily, struct cmdlineInfo.  Validate arguments along
   the way and exit program with message if invalid.
 
-  Note that some string information we return as *cmdlineP is in the storage 
+  Note that some string information we return as *cmdlineP is in the storage
   argv[] points to.
 -----------------------------------------------------------------------------*/
     optEntry * option_def;
diff --git a/generator/ppmpat.c b/generator/ppmpat.c
index 9473afeb..170bfc58 100644
--- a/generator/ppmpat.c
+++ b/generator/ppmpat.c
@@ -39,9 +39,10 @@ typedef enum {
     PAT_POLES,
     PAT_SQUIG,
     PAT_CAMO,
-    PAT_ANTICAMO,
+    PAT_ANTICAMO
 } Pattern;
 
+
 typedef struct {
 /*----------------------------------------------------------------------------
    An ordered list of colors with a cursor.
@@ -57,16 +58,79 @@ struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    Pattern      basePattern;
-    unsigned int width;
-    unsigned int height;
-    unsigned int colorSpec;
-    ColorTable   colorTable;
-    unsigned int randomseed;
-    unsigned int randomseedSpec;
+    Pattern         basePattern;
+    unsigned int    width;
+    unsigned int    height;
+    unsigned int    colorSpec;
+    ColorTable      colorTable;
+    unsigned int    randomseed;
+    unsigned int    randomseedSpec;
+    ppmd_drawproc * drawProc;
 };
 
 
+
+static pixel
+averageTwoColors(pixel const p1,
+                 pixel const p2) {
+
+    pixel p;
+
+    PPM_ASSIGN(p,
+               (PPM_GETR(p1) + PPM_GETR(p2)) / 2,
+               (PPM_GETG(p1) + PPM_GETG(p2)) / 2,
+               (PPM_GETB(p1) + PPM_GETB(p2)) / 2);
+
+    return p;
+}
+
+
+
+static ppmd_drawproc average_drawproc;
+
+static void
+average_drawproc(pixel **     const pixels,
+                 int          const cols,
+                 int          const rows,
+                 pixval       const maxval,
+                 int          const col,
+                 int          const row,
+                 const void * const clientdata) {
+/*----------------------------------------------------------------------------
+  Reset the pixel's color to the average of the original color and the color
+  indicated by * clientdata.
+-----------------------------------------------------------------------------*/
+
+    if (col >= 0 && col < cols && row >= 0 && row < rows)
+        pixels[row][col] =
+            averageTwoColors(pixels[row][col], *((const pixel*) clientdata));
+}
+
+
+
+static ppmd_drawproc checkerboard_drawproc;
+
+static void
+checkerboard_drawproc(pixel **     const pixels,
+                      int          const cols,
+                      int          const rows,
+                      pixval       const maxval,
+                      int          const col,
+                      int          const row,
+                      const void * const clientdata) {
+/*----------------------------------------------------------------------------
+  If col and row are both even or both odd, do nothing.
+  If one is even and the other is odd, set the pixel's color to that indicated
+  by * clientdata.
+-----------------------------------------------------------------------------*/
+    if (col >= 0 && col < cols && row >= 0 &&
+        row < rows && row % 2 != col % 2)
+
+        pixels[row][col] = *((const pixel*) clientdata);
+}
+
+
+
 static void
 validateColorCount(Pattern      const basePattern,
                    unsigned int const colorCount) {
@@ -182,6 +246,7 @@ parseCommandLine(int argc, const char ** argv,
     unsigned int squig;
     unsigned int camo;
     unsigned int anticamo;
+    unsigned int meshSpec;
 
     MALLOCARRAY_NOFAIL(option_def, 100);
 
@@ -199,9 +264,9 @@ parseCommandLine(int argc, const char ** argv,
     OPTENT3(0, "tartan",        OPT_FLAG,   NULL,
             &tartan,     0);
     OPTENT3(0, "argyle1",       OPT_FLAG,   NULL,
-            &argyle1,     0);
+            &argyle1,    0);
     OPTENT3(0, "argyle2",       OPT_FLAG,   NULL,
-            &argyle2,     0);
+            &argyle2,    0);
     OPTENT3(0, "poles",         OPT_FLAG,   NULL,
             &poles,      0);
     OPTENT3(0, "squig",         OPT_FLAG,   NULL,
@@ -214,6 +279,8 @@ parseCommandLine(int argc, const char ** argv,
             &cmdlineP->colorSpec,           0);
     OPTENT3(0, "randomseed",    OPT_UINT,       &cmdlineP->randomseed,
             &cmdlineP->randomseedSpec,      0);
+    OPTENT3(0, "mesh",          OPT_FLAG,   NULL,
+            &meshSpec,   0);
 
     opt.opt_table = option_def;
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
@@ -265,6 +332,14 @@ parseCommandLine(int argc, const char ** argv,
     } else
         cmdlineP->colorTable.count = 0;
 
+    if (meshSpec) {
+        if (gingham2 + gingham3 + madras + tartan > 0)
+            cmdlineP->drawProc = &checkerboard_drawproc;
+        else
+            pm_message("-mesh ignored (no effect with specified pattern)");
+    } else
+        cmdlineP->drawProc = &average_drawproc;
+
     if (argc-1 != 2)
         pm_error("You must specify 2 non-option arguments: width and height "
                  "in pixels.  You specified %u", argc-1);
@@ -363,37 +438,6 @@ randomDarkColor(struct pm_randSt * const randStP,
 
 
 
-static pixel
-averageTwoColors(pixel const p1,
-                 pixel const p2) {
-
-    pixel p;
-
-    PPM_ASSIGN(p,
-               (PPM_GETR(p1) + PPM_GETR(p2)) / 2,
-               (PPM_GETG(p1) + PPM_GETG(p2)) / 2,
-               (PPM_GETB(p1) + PPM_GETB(p2)) / 2);
-
-    return p;
-}
-
-
-
-static ppmd_drawproc average_drawproc;
-
-static void
-average_drawproc(pixel **     const pixels,
-                 int          const cols,
-                 int          const rows,
-                 pixval       const maxval,
-                 int          const col,
-                 int          const row,
-                 const void * const clientdata) {
-
-    if (col >= 0 && col < cols && row >= 0 && row < rows)
-        pixels[row][col] =
-            averageTwoColors(pixels[row][col], *((const pixel*) clientdata));
-}
 
 
 
@@ -563,7 +607,7 @@ clearBackgroundCamo(pixel **           const pixels,
     } else if (antiflag)
         color = randomAnticamoColor(randStP, maxval);
     else
-        color = randomCamoColor(randStP,maxval);
+        color = randomCamoColor(randStP, maxval);
 
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 0, cols, rows, PPMD_NULLDRAWPROC,
@@ -704,6 +748,7 @@ gingham2(pixel **           const pixels,
          unsigned int       const rows,
          ColorTable         const colorTable,
          struct pm_randSt * const randStP,
+         ppmd_drawproc    * const drawproc,
          pixval             const maxval) {
 
     bool  const colorSpec = (colorTable.count > 0);
@@ -726,11 +771,11 @@ gingham2(pixel **           const pixels,
 
     /* Woof. */
     ppmd_filledrectangle(
-        pixels, cols, rows, maxval, 0, 0, cols, rowso2, average_drawproc,
+        pixels, cols, rows, maxval, 0, 0, cols, rowso2, drawproc,
         &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rowso2, cols, rows - rowso2,
-        average_drawproc, &forecolor);
+        drawproc, &forecolor);
 }
 
 
@@ -741,6 +786,7 @@ gingham3(pixel **           const pixels,
          unsigned int       const rows,
          ColorTable         const colorTable,
          struct pm_randSt * const randStP,
+         ppmd_drawproc    * const drawproc,
          pixval             const maxval) {
 
     bool  const colorSpec = (colorTable.count > 0);
@@ -772,17 +818,17 @@ gingham3(pixel **           const pixels,
 
     /* Woof. */
     ppmd_filledrectangle(
-        pixels, cols, rows, maxval, 0, 0, cols, rowso4, average_drawproc,
+        pixels, cols, rows, maxval, 0, 0, cols, rowso4, drawproc,
         &backcolor);
     ppmd_filledrectangle(
-        pixels, cols, rows, maxval, 0, rowso4, cols, rowso4, average_drawproc,
+        pixels, cols, rows, maxval, 0, rowso4, cols, rowso4, drawproc,
         &fore1color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 2 * rowso4, cols, rowso4,
-        average_drawproc, &fore2color);
+        drawproc, &fore2color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 3 * rowso4, cols, rows - rowso4,
-        average_drawproc, &fore1color);
+        drawproc, &fore1color);
 }
 
 
@@ -793,6 +839,7 @@ madras(pixel **           const pixels,
        unsigned int       const rows,
        ColorTable         const colorTable,
        struct pm_randSt * const randStP,
+       ppmd_drawproc    * const drawproc,
        pixval             const maxval) {
 
     bool  const colorSpec = (colorTable.count > 0);
@@ -868,54 +915,54 @@ madras(pixel **           const pixels,
 
     /* Woof. */
     ppmd_filledrectangle(
-        pixels, cols, rows, maxval, 0, 0, cols, rows2, average_drawproc,
+        pixels, cols, rows, maxval, 0, 0, cols, rows2, drawproc,
         &backcolor);
     ppmd_filledrectangle(
-        pixels, cols, rows, maxval, 0, rows2, cols, rows3, average_drawproc,
+        pixels, cols, rows, maxval, 0, rows2, cols, rows3, drawproc,
         &fore2color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rows2 + rows3, cols, rows2,
-        average_drawproc, &backcolor);
+        drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 2 * rows2 + rows3, cols, rows2,
-        average_drawproc, &fore1color);
+        drawproc, &fore1color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 3 * rows2 + rows3, cols, rows2,
-        average_drawproc, &backcolor);
+        drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 4 * rows2 + rows3, cols, rows6a,
-        average_drawproc, &fore2color);
+        drawproc, &fore2color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 4 * rows2 + rows3 + rows6a, cols, rows2,
-        average_drawproc, &backcolor);
+        drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 5 * rows2 + rows3 + rows6a, cols, rows3,
-        average_drawproc, &fore1color);
+        drawproc, &fore1color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 5 * rows2 + 2 * rows3 + rows6a, cols,
-        rows2, average_drawproc, &backcolor);
+        rows2, drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 6 * rows2 + 2 * rows3 + rows6a, cols,
-        rows3, average_drawproc, &fore1color);
+        rows3, drawproc, &fore1color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 6 * rows2 + 3 * rows3 + rows6a, cols,
-        rows2, average_drawproc, &backcolor);
+        rows2, drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 7 * rows2 + 3 * rows3 + rows6a, cols,
-        rows6b, average_drawproc, &fore2color);
+        rows6b, drawproc, &fore2color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 7 * rows2 + 3 * rows3 + rows6a + rows6b,
-        cols, rows2, average_drawproc, &backcolor);
+        cols, rows2, drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 8 * rows2 + 3 * rows3 + rows6a + rows6b,
-        cols, rows2, average_drawproc, &fore1color);
+        cols, rows2, drawproc, &fore1color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, 9 * rows2 + 3 * rows3 + rows6a + rows6b,
-        cols, rows2, average_drawproc, &backcolor);
+        cols, rows2, drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0,
         10 * rows2 + 3 * rows3 + rows6a + rows6b,
-        cols, rows3, average_drawproc, &fore2color);
+        cols, rows3, drawproc, &fore2color);
 }
 
 
@@ -926,6 +973,7 @@ tartan(pixel **           const pixels,
        unsigned int       const rows,
        ColorTable         const colorTable,
        struct pm_randSt * const randStP,
+       ppmd_drawproc    * const drawproc,
        pixval             const maxval) {
 
     bool  const colorSpec = (colorTable.count > 0);
@@ -977,29 +1025,29 @@ tartan(pixel **           const pixels,
 
     /* Woof. */
     ppmd_filledrectangle(
-        pixels, cols, rows, maxval, 0, 0, cols, rows5a, average_drawproc,
+        pixels, cols, rows, maxval, 0, 0, cols, rows5a, drawproc,
         &backcolor);
     ppmd_filledrectangle(
-        pixels, cols, rows, maxval, 0, rows5a, cols, rows1, average_drawproc,
+        pixels, cols, rows, maxval, 0, rows5a, cols, rows1, drawproc,
         &fore1color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rows5a + rows1, cols, rows5b,
-        average_drawproc, &backcolor);
+        drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rows10 + rows1, cols, rows3,
-        average_drawproc, &fore2color);
+        drawproc, &fore2color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rows10 + rows1 + rows3, cols, rows1,
-        average_drawproc, &backcolor);
+        drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rows10 + 2 * rows1 + rows3, cols, rows3,
-        average_drawproc, &fore2color);
+        drawproc, &fore2color);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rows10 + 2 * rows1 + 2 * rows3, cols,
-        rows1, average_drawproc, &backcolor);
+        rows1, drawproc, &backcolor);
     ppmd_filledrectangle(
         pixels, cols, rows, maxval, 0, rows10 + 3 * rows1 + 2 * rows3, cols,
-        rows3, average_drawproc, &fore2color);
+        rows3, drawproc, &fore2color);
 }
 
 
@@ -1077,7 +1125,7 @@ argyle(pixel **     const pixels,
 
 
 
-static unsigned int const MAXPOLES = 500;
+#define MAXPOLES 500
 
 
 
@@ -1541,22 +1589,22 @@ main(int argc, const char ** argv) {
     switch (cmdline.basePattern) {
     case PAT_GINGHAM2:
         gingham2(pixels, cmdline.width, cmdline.height,
-                 cmdline.colorTable, &randSt, PPM_MAXMAXVAL);
+                 cmdline.colorTable, &randSt, cmdline.drawProc, PPM_MAXMAXVAL);
         break;
 
     case PAT_GINGHAM3:
         gingham3(pixels, cmdline.width, cmdline.height,
-                 cmdline.colorTable, &randSt, PPM_MAXMAXVAL);
+                 cmdline.colorTable, &randSt, cmdline.drawProc, PPM_MAXMAXVAL);
         break;
 
     case PAT_MADRAS:
         madras(pixels, cmdline.width, cmdline.height,
-               cmdline.colorTable, &randSt, PPM_MAXMAXVAL);
+               cmdline.colorTable, &randSt, cmdline.drawProc, PPM_MAXMAXVAL);
         break;
 
     case PAT_TARTAN:
         tartan(pixels, cmdline.width, cmdline.height,
-               cmdline.colorTable, &randSt, PPM_MAXMAXVAL);
+               cmdline.colorTable, &randSt, cmdline.drawProc, PPM_MAXMAXVAL);
         break;
 
     case PAT_ARGYLE1:
diff --git a/lib/libpam.c b/lib/libpam.c
index 72502749..b24f230e 100644
--- a/lib/libpam.c
+++ b/lib/libpam.c
@@ -90,7 +90,12 @@ validateComputableSize(struct pam * const pamP) {
    the size of a tuple row, in bytes, can be represented by an 'int'.
 
    Another common operation is adding 1 or 2 to the highest row, column,
-   or plane number in the image, so we make sure that's possible.
+   or plane number in the image, so we make sure that's possible.  And in
+   bitmap images, rounding up to multiple of 8 is common, so we provide for
+   that too.
+
+   Note that it's still the programmer's responsibility to ensure that his
+   code, using values known to have been validated here, cannot overflow.
 -----------------------------------------------------------------------------*/
     if (pamP->width == 0)
         pm_error("Width is zero.  Image must be at least one pixel wide");
@@ -111,10 +116,10 @@ validateComputableSize(struct pam * const pamP) {
 
         if (depth > INT_MAX - 2)
             pm_error("image depth (%u) too large to be processed", depth);
-        if (pamP->width > INT_MAX - 2)
+        if (pamP->width > INT_MAX - 10)
             pm_error("image width (%u) too large to be processed",
                      pamP->width);
-        if (pamP->height > INT_MAX - 2)
+        if (pamP->height > INT_MAX - 10)
             pm_error("image height (%u) too large to be processed",
                      pamP->height);
     }
@@ -416,6 +421,20 @@ pnm_setpamrow(const struct pam * const pamP,
 
 
 
+static void
+setSeekableAndRasterPos(struct pam * const pamP) {
+
+    if (pamP->size >= PAM_STRUCT_SIZE(is_seekable))
+        pamP->is_seekable = pm_is_seekable(pamP->file);
+
+    if (pamP->size >= PAM_STRUCT_SIZE(raster_pos)) {
+        if (pamP->is_seekable)
+            pm_tell2(pamP->file, &pamP->raster_pos, sizeof(pamP->raster_pos));
+    }
+}
+
+
+
 #define MAX_LABEL_LENGTH 8
 #define MAX_VALUE_LENGTH 255
 
@@ -945,6 +964,8 @@ pnm_readpaminit(FILE *       const file,
     pamP->plainformat = FALSE;
         /* See below for complex explanation of why this is FALSE. */
 
+    setSeekableAndRasterPos(pamP);
+
     interpretTupleType(pamP);
 
     validateComputableSize(pamP);
@@ -1053,8 +1074,6 @@ pnm_writepaminit(struct pam * const pamP) {
 
     interpretTupleType(pamP);
 
-    pamP->len = MIN(pamP->size, PAM_STRUCT_SIZE(opacity_plane));
-
     switch (PAM_FORMAT_TYPE(pamP->format)) {
     case PAM_TYPE:
         /* See explanation below of why we ignore 'pm_plain_output' here. */
@@ -1113,6 +1132,10 @@ pnm_writepaminit(struct pam * const pamP) {
         pm_error("Invalid format passed to pnm_writepaminit(): %d",
                  pamP->format);
     }
+
+    setSeekableAndRasterPos(pamP);
+
+    pamP->len = MIN(pamP->size, PAM_STRUCT_SIZE(raster_pos));
 }
 
 
diff --git a/lib/libpamwrite.c b/lib/libpamwrite.c
index 7edc90dc..0e1ff469 100644
--- a/lib/libpamwrite.c
+++ b/lib/libpamwrite.c
@@ -71,7 +71,7 @@ writePamPlainPbmRow(const struct pam *  const pamP,
 
 static void
 writePamPlainRow(const struct pam *  const pamP,
-                    const tuple *       const tuplerow) {
+                 const tuple *       const tuplerow) {
 
     int const samplesPerLine =
         samplesPerPlainLine(pamP->maxval, pamP->depth, 79);
@@ -101,17 +101,25 @@ writePamPlainRow(const struct pam *  const pamP,
 
 
 static void
-formatPbmRow(const struct pam * const pamP,
-             const tuple *      const tuplerow,
-             unsigned char *    const outbuf,
-             unsigned int *     const rowSizeP) {
+formatPbm(const struct pam * const pamP,
+          const tuple *      const tuplerow,
+          unsigned char *    const outbuf,
+          unsigned int       const nTuple,
+          unsigned int *     const rowSizeP) {
+/*----------------------------------------------------------------------------
+   Create the image of 'nTuple' consecutive tuples of a row in the raster of a
+   raw format PBM image.
 
+   Put the image at *outbuf; put the number of bytes of it at *rowSizeP.
+-----------------------------------------------------------------------------*/
     unsigned char accum;
     int col;
 
+    assert(nTuple <= pamP->width);
+
     accum = 0;  /* initial value */
 
-    for (col=0; col < pamP->width; ++col) {
+    for (col=0; col < nTuple; ++col) {
         accum |=
             (tuplerow[col][0] == PAM_PBM_BLACK ? PBM_BLACK : PBM_WHITE)
                 << (7-col%8);
@@ -120,12 +128,12 @@ formatPbmRow(const struct pam * const pamP,
                 accum = 0;
         }
     }
-    if (pamP->width % 8 != 0) {
-        unsigned int const lastByteIndex = pamP->width/8;
+    if (nTuple % 8 != 0) {
+        unsigned int const lastByteIndex = nTuple/8;
         outbuf[lastByteIndex] = accum;
         *rowSizeP = lastByteIndex + 1;
     } else
-        *rowSizeP = pamP->width/8;
+        *rowSizeP = nTuple/8;
 }
 
 
@@ -171,36 +179,40 @@ sampleToBytes4(unsigned char       buf[4],
 
 
 static __inline__ void
-format1BpsRow(const struct pam * const pamP,
-              const tuple *      const tuplerow,
-              unsigned char *    const outbuf,
-              unsigned int *     const rowSizeP) {
+format1Bps(const struct pam * const pamP,
+           const tuple *      const tuplerow,
+           unsigned char *    const outbuf,
+           unsigned int       const nTuple,
+           unsigned int *     const rowSizeP) {
 /*----------------------------------------------------------------------------
-   Create the image of a row in the raster of a raw format Netpbm
-   image that has one byte per sample (ergo not PBM).
+   Create the image of 'nTuple' consecutive tuples of a row in the raster of a
+   raw format Netpbm image that has one byte per sample (ergo not PBM).
 
    Put the image at *outbuf; put the number of bytes of it at *rowSizeP.
 -----------------------------------------------------------------------------*/
     int col;
     unsigned int bufferCursor;
 
+    assert(nTuple <= pamP->width);
+
     bufferCursor = 0;  /* initial value */
 
-    for (col = 0; col < pamP->width; ++col) {
+    for (col = 0; col < nTuple; ++col) {
         unsigned int plane;
         for (plane=0; plane < pamP->depth; ++plane)
             outbuf[bufferCursor++] = (unsigned char)tuplerow[col][plane];
     }
-    *rowSizeP = pamP->width * 1 * pamP->depth;
+    *rowSizeP = nTuple * 1 * pamP->depth;
 }
 
 
 
 static __inline__ void
-format2BpsRow(const struct pam * const pamP,
-              const tuple *      const tuplerow,
-              unsigned char *    const outbuf,
-              unsigned int *     const rowSizeP) {
+format2Bps(const struct pam * const pamP,
+           const tuple *      const tuplerow,
+           unsigned char *    const outbuf,
+           unsigned int       const nTuple,
+           unsigned int *     const rowSizeP) {
 /*----------------------------------------------------------------------------
   Analogous to format1BpsRow().
 -----------------------------------------------------------------------------*/
@@ -209,24 +221,27 @@ format2BpsRow(const struct pam * const pamP,
     int col;
     unsigned int bufferCursor;
 
+    assert(nTuple <= pamP->width);
+
     bufferCursor = 0;  /* initial value */
 
-    for (col=0; col < pamP->width; ++col) {
+    for (col=0; col < nTuple; ++col) {
         unsigned int plane;
         for (plane = 0; plane < pamP->depth; ++plane)
             sampleToBytes2(ob[bufferCursor++], tuplerow[col][plane]);
     }
 
-    *rowSizeP = pamP->width * 2 * pamP->depth;
+    *rowSizeP = nTuple * 2 * pamP->depth;
 }
 
 
 
 static __inline__ void
-format3BpsRow(const struct pam * const pamP,
-              const tuple *      const tuplerow,
-              unsigned char *    const outbuf,
-              unsigned int *     const rowSizeP) {
+format3Bps(const struct pam * const pamP,
+           const tuple *      const tuplerow,
+           unsigned char *    const outbuf,
+           unsigned int       const nTuple,
+           unsigned int *     const rowSizeP) {
 /*----------------------------------------------------------------------------
   Analogous to format1BpsRow().
 -----------------------------------------------------------------------------*/
@@ -235,24 +250,27 @@ format3BpsRow(const struct pam * const pamP,
     int col;
     unsigned int bufferCursor;
 
+    assert(nTuple <= pamP->width);
+
     bufferCursor = 0;  /* initial value */
 
-    for (col=0; col < pamP->width; ++col) {
+    for (col=0; col < nTuple; ++col) {
         unsigned int plane;
         for (plane = 0; plane < pamP->depth; ++plane)
             sampleToBytes3(ob[bufferCursor++], tuplerow[col][plane]);
     }
 
-    *rowSizeP = pamP->width * 3 * pamP->depth;
+    *rowSizeP = nTuple * 3 * pamP->depth;
 }
 
 
 
 static __inline__ void
-format4BpsRow(const struct pam * const pamP,
-              const tuple *      const tuplerow,
-              unsigned char *    const outbuf,
-              unsigned int *     const rowSizeP) {
+format4Bps(const struct pam * const pamP,
+           const tuple *      const tuplerow,
+           unsigned char *    const outbuf,
+           unsigned int       const nTuple,
+           unsigned int *     const rowSizeP) {
 /*----------------------------------------------------------------------------
   Analogous to format1BpsRow().
 -----------------------------------------------------------------------------*/
@@ -261,41 +279,49 @@ format4BpsRow(const struct pam * const pamP,
     int col;
     unsigned int bufferCursor;
 
+    assert(nTuple <= pamP->width);
+
     bufferCursor = 0;  /* initial value */
 
-    for (col=0; col < pamP->width; ++col) {
+    for (col=0; col < nTuple; ++col) {
         unsigned int plane;
         for (plane = 0; plane < pamP->depth; ++plane)
             sampleToBytes4(ob[bufferCursor++], tuplerow[col][plane]);
     }
 
-    *rowSizeP = pamP->width * 4 * pamP->depth;
+    *rowSizeP = nTuple * 4 * pamP->depth;
 }
 
 
 
 void
-pnm_formatpamrow(const struct pam * const pamP,
-                 const tuple *      const tuplerow,
-                 unsigned char *    const outbuf,
-                 unsigned int *     const rowSizeP) {
-/*----------------------------------------------------------------------------
-   Create the image of a row in the raster of a raw (not plain) format
-   Netpbm image, as described by *pamP and tuplerow[].  Put the image
-   at *outbuf.
+pnm_formatpamtuples(const struct pam * const pamP,
+                    const tuple *      const tuplerow,
+                    unsigned char *    const outbuf,
+                    unsigned int       const nTuple,
+                    unsigned int *     const rowSizeP) {
+/*----------------------------------------------------------------------------   Create the image of 'nTuple' consecutive tuples of a row in the raster of a
+   raw (not plain) format Netpbm image, as described by *pamP and tuplerow[].
+   Put the image at *outbuf.
 
    'outbuf' must be the address of space allocated with pnm_allocrowimage().
 
-   We return as *rowSizeP the number of bytes in the row image.
+   We return as *rowSizeP the number of bytes in the image.
 -----------------------------------------------------------------------------*/
+    if (nTuple > pamP->width) {
+        pm_error("pnm_formatpamtuples called to write more tuples (%u) "
+                 "than the width of a row (%u)",
+                 nTuple, pamP->width);
+    }
+
     if (PAM_FORMAT_TYPE(pamP->format) == PBM_TYPE)
-        formatPbmRow(pamP, tuplerow, outbuf, rowSizeP);
+        formatPbm(pamP, tuplerow, outbuf, nTuple, rowSizeP);
     else {
         switch(pamP->bytes_per_sample){
-        case 1: format1BpsRow(pamP, tuplerow, outbuf, rowSizeP); break;
-        case 2: format2BpsRow(pamP, tuplerow, outbuf, rowSizeP); break;
-        case 3: format3BpsRow(pamP, tuplerow, outbuf, rowSizeP); break;
-        case 4: format4BpsRow(pamP, tuplerow, outbuf, rowSizeP); break;
+        case 1: format1Bps(pamP, tuplerow, outbuf, nTuple, rowSizeP); break;
+        case 2: format2Bps(pamP, tuplerow, outbuf, nTuple, rowSizeP); break;
+        case 3: format3Bps(pamP, tuplerow, outbuf, nTuple, rowSizeP); break;
+        case 4: format4Bps(pamP, tuplerow, outbuf, nTuple, rowSizeP); break;
         default:
             pm_error("invalid bytes per sample passed to "
                      "pnm_formatpamrow(): %u",  pamP->bytes_per_sample);
@@ -305,6 +331,19 @@ pnm_formatpamrow(const struct pam * const pamP,
 
 
 
+void
+pnm_formatpamrow(const struct pam * const pamP,
+                 const tuple *      const tuplerow,
+                 unsigned char *    const outbuf,
+                 unsigned int *     const rowSizeP) {
+/*----------------------------------------------------------------------------
+  Same as 'pnm_formatpamtuples', except formats an entire row.
+-----------------------------------------------------------------------------*/
+    pnm_formatpamtuples(pamP, tuplerow, outbuf, pamP->width, rowSizeP);
+}
+
+
+
 static void
 writePamRawRow(const struct pam * const pamP,
                const tuple *      const tuplerow,
@@ -398,6 +437,74 @@ pnm_writepamrowmult(const struct pam * const pamP,
 
 
 void
+pnm_writepamrowpart(const struct pam * const pamP,
+                    const tuple *      const tuplerow,
+                    unsigned int       const firstRow,
+                    unsigned int       const firstCol,
+                    unsigned int       const rowCt,
+                    unsigned int       const colCt) {
+/*----------------------------------------------------------------------------
+   Write part of multiple consecutive rows to the file.
+
+   For each of 'rowCt' consecutive rows starting at 'firstRow', write the
+   'colCt' columns starting at 'firstCol'.  The tuples to write are those in
+   'tuplerow', starting at the beginning of 'tuplerow'.
+
+   Fail if the file is not seekable (or not known to be seekable) or the
+   output format is not raw (i.e. is plain) or the output format is PBM.
+-----------------------------------------------------------------------------*/
+    unsigned int const bytesPerTuple = pamP->depth * pamP->bytes_per_sample;
+
+    jmp_buf jmpbuf;
+    jmp_buf * origJmpbufP;
+    unsigned int tupleImageSize;
+    unsigned char * outbuf;  /* malloc'ed */
+
+    if (pamP->len < PAM_STRUCT_SIZE(raster_pos) || !pamP->raster_pos)
+        pm_error("pnm_writepamrowpart called on nonseekable file");
+
+    if (PAM_FORMAT_TYPE(pamP->format) == PBM_TYPE)
+        pm_error("pnm_witepamrowpart called for PBM image");
+
+    if (pm_plain_output || pamP->plainformat)
+        pm_error("pnm_writepamrowpart called for plain format image");
+
+    outbuf = pnm_allocrowimage(pamP);
+
+    pnm_formatpamtuples(pamP, tuplerow, outbuf, colCt, &tupleImageSize);
+
+    if (setjmp(jmpbuf) != 0) {
+        pnm_freerowimage(outbuf);
+        pm_setjmpbuf(origJmpbufP);
+        pm_longjmp();
+    } else {
+        unsigned int row;
+
+        pm_setjmpbufsave(&jmpbuf, &origJmpbufP);
+
+        for (row = firstRow; row < firstRow + rowCt; ++row) {
+            pm_filepos const firstTuplePos =
+                pamP->raster_pos +
+                (row * pamP->width + firstCol) * bytesPerTuple;
+            size_t bytesWritten;
+
+            pm_seek2(pamP->file, &firstTuplePos, sizeof(firstTuplePos));
+
+            bytesWritten = fwrite(outbuf, 1, tupleImageSize, pamP->file);
+
+            if (bytesWritten != tupleImageSize)
+                pm_error("fwrite() failed to write %u image tuples "
+                         "to the file.  errno=%d (%s)",
+                         colCt, errno, strerror(errno));
+        }
+        pm_setjmpbuf(origJmpbufP);
+    }
+    pnm_freerowimage(outbuf);
+}
+
+
+
+void
 pnm_writepam(struct pam * const pamP,
              tuple **     const tuplearray) {
 
diff --git a/lib/libpbm2.c b/lib/libpbm2.c
index a611bec5..2a2e2aac 100644
--- a/lib/libpbm2.c
+++ b/lib/libpbm2.c
@@ -69,8 +69,8 @@ validateComputableSize(unsigned int const cols,
    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.
+   See comments at 'validateComputableSize' in libpam.c for details on
+   the purpose of these validations.
 -----------------------------------------------------------------------------*/
     if (cols > INT_MAX - 10)
         pm_error("image width (%u) too large to be processed", cols);
diff --git a/lib/libpbmfont1.c b/lib/libpbmfont1.c
index fe014150..a76d0e6b 100644
--- a/lib/libpbmfont1.c
+++ b/lib/libpbmfont1.c
@@ -184,7 +184,7 @@ computeCharacterSize(const bit **   const font,
 
 
 
-struct font*
+struct font *
 pbm_dissectfont(const bit ** const fontsheet,
                 unsigned int const frows,
                 unsigned int const fcols) {
@@ -222,56 +222,61 @@ pbm_dissectfont(const bit ** const fontsheet,
     unsigned int charWidth, charHeight;
         /* Maximum dimensions of glyph itself, inside its cell */
 
-    int row, col, ch, r, c, i;
-    struct font * fn;
+    unsigned int row, col;
+    int ch;
+    unsigned int i;
+    struct font * fontP;
 
     computeCharacterSize(fontsheet, fcols, frows,
                          &cellWidth, &cellHeight, &charWidth, &charHeight);
 
     /* Now convert to a general font */
 
-    MALLOCVAR(fn);
-    if (fn == NULL)
+    MALLOCVAR(fontP);
+    if (fontP == NULL)
         pm_error("out of memory allocating font structure");
 
-    fn->maxwidth  = charWidth;
-    fn->maxheight = charHeight;
-    fn->x = fn->y = 0;
+    fontP->maxwidth  = charWidth;
+    fontP->maxheight = charHeight;
+    fontP->x = fontP->y = 0;
 
-    fn->oldfont = fontsheet;
-    fn->frows = frows;
-    fn->fcols = fcols;
+    fontP->oldfont = fontsheet;
+    fontP->frows = frows;
+    fontP->fcols = fcols;
 
     /* Now fill in the 0,0 coords. */
     row = cellHeight * 2;
     col = cellWidth  * 2;
 
     /* Load individual glyphs */
-    for ( ch = 0; ch < nCharsInFont; ++ch ) {
+    for (ch = 0; ch < nCharsInFont; ++ch) {
         /* Allocate memory separately for each glyph.
            pbm_loadbdffont2() does this in exactly the same manner.
          */
-        struct glyph * const glyph =
+        struct glyph * const glyphP =
              (struct glyph *) malloc (sizeof (struct glyph));
-        char * const bmap = (char*) malloc(fn->maxwidth * fn->maxheight);
+        char * const bmap = (char*) malloc(fontP->maxwidth * fontP->maxheight);
 
-        if ( bmap == NULL || glyph == NULL )
-            pm_error( "out of memory allocating glyph data" );
+        unsigned int r;
 
-        glyph->width  = fn->maxwidth;
-        glyph->height = fn->maxheight;
-        glyph->x = glyph->y = 0;
-        glyph->xadd = cellWidth;
+        if (bmap == NULL || glyphP == NULL)
+            pm_error( "out of memory allocating glyph data" );
 
-        for ( r = 0; r < glyph->height; ++r )
-            for ( c = 0; c < glyph->width; ++c )
-                bmap[r * glyph->width + c] = fontsheet[row + r][col + c];
+        glyphP->width  = fontP->maxwidth;
+        glyphP->height = fontP->maxheight;
+        glyphP->x = glyphP->y = 0;
+        glyphP->xadd = cellWidth;
 
-        glyph->bmap = bmap;
-        fn->glyph[firstCodePoint + ch] = glyph;
+        for (r = 0; r < glyphP->height; ++r) {
+            unsigned int c;
+            for (c = 0; c < glyphP->width; ++c)
+                bmap[r * glyphP->width + c] = fontsheet[row + r][col + c];
+        }
+        glyphP->bmap = bmap;
+        fontP->glyph[firstCodePoint + ch] = glyphP;
 
         col += cellWidth;
-        if ( col >= cellWidth * 14 ) {
+        if (col >= cellWidth * 14) {
             col = cellWidth * 2;
             row += cellHeight;
         }
@@ -279,12 +284,12 @@ pbm_dissectfont(const bit ** const fontsheet,
 
     /* Initialize all remaining character positions to "undefined." */
     for (i = 0; i < firstCodePoint; ++i)
-        fn->glyph[i] = NULL;
+        fontP->glyph[i] = NULL;
 
     for (i = firstCodePoint + nCharsInFont; i <= PM_FONT_MAXGLYPH; ++i)
-        fn->glyph[i] = NULL;
+        fontP->glyph[i] = NULL;
 
-    return fn;
+    return fontP;
 }
 
 
diff --git a/lib/libpm.c b/lib/libpm.c
index 6f9dea3d..78d941fa 100644
--- a/lib/libpm.c
+++ b/lib/libpm.c
@@ -844,6 +844,9 @@ pm_parse_width(const char * const arg) {
    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.
+
+   See comments at 'validateComputableSize' in libpam.c for details on
+   the purpose of these validations.
 -----------------------------------------------------------------------------*/
     unsigned int width;
     const char * error;
diff --git a/lib/libpnm2.c b/lib/libpnm2.c
index fa4bb8be..6fec91e9 100644
--- a/lib/libpnm2.c
+++ b/lib/libpnm2.c
@@ -23,11 +23,11 @@
 
 
 void
-pnm_writepnminit(FILE * const fileP, 
-                 int    const cols, 
-                 int    const rows, 
-                 xelval const maxval, 
-                 int    const format, 
+pnm_writepnminit(FILE * const fileP,
+                 int    const cols,
+                 int    const rows,
+                 xelval const maxval,
+                 int    const format,
                  int    const forceplain) {
 
     bool const plainFormat = forceplain || pm_plain_output;
@@ -47,7 +47,7 @@ pnm_writepnminit(FILE * const fileP,
 
     default:
         pm_error("invalid format argument received by pnm_writepnminit(): %d"
-                 "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", 
+                 "PNM_FORMAT_TYPE(format) must be %d, %d, or %d",
                  format, PBM_TYPE, PGM_TYPE, PPM_TYPE);
     }
 }
@@ -55,19 +55,19 @@ pnm_writepnminit(FILE * const fileP,
 
 
 static void
-writepgmrow(FILE *       const fileP, 
-            const xel *  const xelrow, 
-            unsigned int const cols, 
-            xelval       const maxval, 
-            int          const format, 
+writepgmrow(FILE *       const fileP,
+            const xel *  const xelrow,
+            unsigned int const cols,
+            xelval       const maxval,
+            int          const format,
             bool         const plainFormat) {
-    
+
     jmp_buf jmpbuf;
     jmp_buf * origJmpbufP;
     gray * grayrow;
-    
+
     grayrow = pgm_allocrow(cols);
-    
+
     if (setjmp(jmpbuf) != 0) {
         pgm_freerow(grayrow);
         pm_setjmpbuf(origJmpbufP);
@@ -76,10 +76,10 @@ writepgmrow(FILE *       const fileP,
         unsigned int col;
 
         pm_setjmpbufsave(&jmpbuf, &origJmpbufP);
-        
+
         for (col = 0; col < cols; ++col)
             grayrow[col] = PNM_GET1(xelrow[col]);
-    
+
         pgm_writepgmrow(fileP, grayrow, cols, (gray) maxval, plainFormat);
 
         pm_setjmpbuf(origJmpbufP);
@@ -100,7 +100,7 @@ writepbmrow(FILE *       const fileP,
     bit * bitrow;
 
     bitrow = pbm_allocrow(cols);
-    
+
     if (setjmp(jmpbuf) != 0) {
         pbm_freerow(bitrow);
         pm_setjmpbuf(origJmpbufP);
@@ -112,29 +112,29 @@ writepbmrow(FILE *       const fileP,
 
         for (col = 0; col < cols; ++col)
             bitrow[col] = PNM_GET1(xelrow[col]) == 0 ? PBM_BLACK : PBM_WHITE;
-    
+
         pbm_writepbmrow(fileP, bitrow, cols, plainFormat);
 
         pm_setjmpbuf(origJmpbufP);
     }
     pbm_freerow(bitrow);
-}    
+}
 
 
 
 void
-pnm_writepnmrow(FILE *      const fileP, 
-                const xel * const xelrow, 
-                int         const cols, 
-                xelval      const maxval, 
-                int         const format, 
+pnm_writepnmrow(FILE *      const fileP,
+                const xel * const xelrow,
+                int         const cols,
+                xelval      const maxval,
+                int         const format,
                 int         const forceplain) {
 
     bool const plainFormat = forceplain || pm_plain_output;
-    
+
     switch (PNM_FORMAT_TYPE(format)) {
     case PPM_TYPE:
-        ppm_writeppmrow(fileP, (pixel*) xelrow, cols, (pixval) maxval, 
+        ppm_writeppmrow(fileP, (pixel*) xelrow, cols, (pixval) maxval,
                         plainFormat);
         break;
 
@@ -145,10 +145,10 @@ pnm_writepnmrow(FILE *      const fileP,
     case PBM_TYPE:
         writepbmrow(fileP, xelrow, cols, plainFormat);
         break;
-    
+
     default:
         pm_error("invalid format argument received by pnm_writepnmrow(): %d"
-                 "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", 
+                 "PNM_FORMAT_TYPE(format) must be %d, %d, or %d",
                  format, PBM_TYPE, PGM_TYPE, PPM_TYPE);
     }
 }
@@ -167,7 +167,10 @@ pnm_writepnm(FILE * const fileP,
     unsigned int row;
 
     pnm_writepnminit(fileP, cols, rows, maxval, format, forceplain);
-    
+
     for (row = 0; row < rows; ++row)
         pnm_writepnmrow(fileP, xels[row], cols, maxval, format, forceplain);
 }
+
+
+
diff --git a/lib/pam.h b/lib/pam.h
index aebf529a..88b8c2bd 100644
--- a/lib/pam.h
+++ b/lib/pam.h
@@ -136,6 +136,18 @@ struct pam {
         /* The plane number of the opacity plane;  meaningless if
            'haveOpacity' is false or 'visual' is false.
         */
+    int is_seekable;  /* boolean */
+        /* The file 'file' is seekable -- you can set the position of next
+           reading or writing to anything and any time.
+
+           If libnetpbm cannot tell if it is seekable or not, this is false.
+        */
+    pm_filepos raster_pos;
+        /* The file position of the raster (which is also the end of the
+           header).
+
+           Meaningless if 'is_seekable' is false.
+        */
 };
 
 #define PAM_HAVE_ALLOCATION_DEPTH 1
@@ -341,6 +353,12 @@ void
 pnm_writepaminit(struct pam * const pamP);
 
 void
+pnm_formatpamtuples(const struct pam * const pamP,
+                    const tuple *      const tuplerow,
+                    unsigned char *    const outbuf,
+                    unsigned int       const nTuple,
+                    unsigned int *     const rowSizeP);
+void
 pnm_formatpamrow(const struct pam * const pamP,
                  const tuple *      const tuplerow,
                  unsigned char *    const outbuf,
@@ -355,6 +373,14 @@ pnm_writepamrowmult(const struct pam * const pamP,
                     unsigned int       const rptcnt);
 
 void
+pnm_writepamrowpart(const struct pam * const pamP,
+                    const tuple *      const tuplerow,
+                    unsigned int       const firstRow,
+                    unsigned int       const firstCol,
+                    unsigned int       const rowCt,
+                    unsigned int       const colCt);
+
+void
 pnm_writepam(struct pam * const pamP, tuple ** const tuplearray);
 
 void
diff --git a/lib/pbm.h b/lib/pbm.h
index 27fd1163..57ab3812 100644
--- a/lib/pbm.h
+++ b/lib/pbm.h
@@ -47,6 +47,19 @@ pbm_allocrow(unsigned int const cols);
   ((bit**) pm_allocarray(cols, rows, sizeof(bit)))
 #define pbm_freearray(bits, rows) pm_freearray((char**) bits, rows)
 #define pbm_freerow(bitrow) pm_freerow((char*) bitrow)
+
+/* Beware of arithmetic overflows when using pbm_packed_bytes(),
+   pbm_allocrow_packed() and pbm_allocarray_packed().
+
+   When cols is signed int, pbm_packed_bytes(cols + 8) overflows
+   with large values.   Same with pamP->width which is always signed int.
+
+   Function validateComputableSize() called by pbm_readpbminit()
+   provides a margin of 10, but the "+7" uses much of it.
+
+   To prevent overflows, cast cols or pamP->width to unsigned int
+   like this: pbm_packed_bytes((unsigned int) cols +8))
+*/
 #define pbm_packed_bytes(cols) (((cols)+7)/8)
 #define pbm_allocrow_packed(cols) \
     ((unsigned char *) pm_allocrow(pbm_packed_bytes(cols), \
diff --git a/lib/pm.h b/lib/pm.h
index 3fc92fb4..b3c3d202 100644
--- a/lib/pm.h
+++ b/lib/pm.h
@@ -386,6 +386,9 @@ pm_bs_short(short const s);
 long
 pm_bs_long(long const l);
 
+int
+pm_is_seekable(FILE * const fileP);
+
 unsigned int
 pm_tell(FILE * const fileP);
 
diff --git a/lib/pmfileio.c b/lib/pmfileio.c
index 4048e74d..1ed71f18 100644
--- a/lib/pmfileio.c
+++ b/lib/pmfileio.c
@@ -452,27 +452,16 @@ pm_tmpfile_fd(void) {
 }
 
 
-
-FILE *
-pm_openr_seekable(const char name[]) {
+static bool
+isSeekable(FILE * const fP) {
 /*----------------------------------------------------------------------------
-  Open the file named by name[] such that it is seekable (i.e. it can be
-  rewound and read in multiple passes with fseek()).
+   The file is seekable -- we can set its read/write position to anything we
+   want.
 
-  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.
+   If we can't tell if it is seekable, we return false.
 -----------------------------------------------------------------------------*/
-    int stat_rc;
-    int seekable;  /* logical: file is seekable */
+    int statRc;
     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
@@ -486,41 +475,62 @@ pm_openr_seekable(const char name[]) {
        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;
+    statRc = fstat(fileno(fP), &statbuf);
+
+    return statRc == 0 && S_ISREG(statbuf.st_mode);
+}
+
+
+
+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.
 
-    if (seekable) {
-        seekable_file = original_file;
+  We use a file that the operating system recognizes as temporary, so
+  it picks the filename and deletes the file when Caller closes it.
+-----------------------------------------------------------------------------*/
+    FILE * originalFileP;
+    FILE * seekableFileP;
+
+    originalFileP = pm_openr((char *) name);
+
+    if (isSeekable(originalFileP)) {
+        seekableFileP = originalFileP;
     } else {
-        seekable_file = pm_tmpfile();
+        seekableFileP = pm_tmpfile();
 
         /* Copy the input into the temporary seekable file */
-        while (!feof(original_file) && !ferror(original_file)
-               && !ferror(seekable_file)) {
+        while (!feof(originalFileP) && !ferror(originalFileP)
+               && !ferror(seekableFileP)) {
             char buffer[4096];
-            int bytes_read;
-            bytes_read = fread(buffer, 1, sizeof(buffer), original_file);
-            fwrite(buffer, 1, bytes_read, seekable_file);
+            size_t nBytesRead;
+
+            nBytesRead = fread(buffer, 1, sizeof(buffer), originalFileP);
+            fwrite(buffer, 1, nBytesRead, seekableFileP);
         }
-        if (ferror(original_file))
+        if (ferror(originalFileP))
             pm_error("Error reading input file into temporary file.  "
                      "Errno = %s (%d)", strerror(errno), errno);
-        if (ferror(seekable_file))
+        if (ferror(seekableFileP))
             pm_error("Error writing input into temporary file.  "
                      "Errno = %s (%d)", strerror(errno), errno);
-        pm_close(original_file);
+        pm_close(originalFileP);
         {
-            int seek_rc;
-            seek_rc = fseek(seekable_file, 0, SEEK_SET);
-            if (seek_rc != 0)
+            int seekRc;
+
+            seekRc = fseek(seekableFileP, 0, SEEK_SET);
+            if (seekRc != 0)
                 pm_error("fseek() failed to rewind temporary file.  "
                          "Errno = %s (%d)", strerror(errno), errno);
         }
     }
-    return seekable_file;
+    return seekableFileP;
 }
 
 
@@ -968,6 +978,20 @@ pm_bs_long(long const l) {
 
 
 
+int
+pm_is_seekable(FILE * const fP) {
+
+    return isSeekable(fP) ? 1 : 0;
+}
+
+
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpragmas"
+#pragma GCC diagnostic ignored "-Wduplicated-cond"
+
+
+
 void
 pm_tell2(FILE *       const fileP,
          void *       const fileposP,
@@ -1008,6 +1032,10 @@ pm_tell2(FILE *       const fileP,
 
 
 
+#pragma GCC diagnostic pop
+
+
+
 unsigned int
 pm_tell(FILE * const fileP) {
 
@@ -1020,6 +1048,12 @@ pm_tell(FILE * const fileP) {
 
 
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpragmas"
+#pragma GCC diagnostic ignored "-Wduplicated-cond"
+
+
+
 void
 pm_seek2(FILE *             const fileP,
          const pm_filepos * const fileposP,
@@ -1047,6 +1081,10 @@ pm_seek2(FILE *             const fileP,
 
 
 
+#pragma GCC diagnostic pop
+
+
+
 void
 pm_seek(FILE * const fileP, unsigned long filepos) {
 /*----------------------------------------------------------------------------
diff --git a/lib/util/rand.c b/lib/util/rand.c
index 2f60de83..6a0a2cdb 100644
--- a/lib/util/rand.c
+++ b/lib/util/rand.c
@@ -72,8 +72,8 @@ https://wnww.gnu.org/software/gsl/doc/html/rng.html
   Twister method and does not rely on any randomness facility of the operating
   system, but it is easy to compile an alternative version that uses others.
 
-  The Mersenne Twister method was new to Netpbm in Netpbm 10.94
-  (March 2021).  Before that, Netpbm used standard OS-provided facilities.
+  The Mersenne Twister method was new to Netpbm in Netpbm 10.94 (March 2021).
+  Before that, Netpbm used standard OS-provided facilities.
 
   Programs that use random numbers have existed in Netpbm since PBMPlus days.
   The system rand() function was used in instances randomness was required;
@@ -87,15 +87,15 @@ https://wnww.gnu.org/software/gsl/doc/html/rng.html
 
   This was not considered a problem in the early days.  Deterministic
   operation was not a feature users requested and it was impossible regardless
-  of the random number generation method on most programs because they did
-  not allow a user to specify a seed for the generator.
+  of the random number generation method on most programs because they did not
+  allow a user to specify a seed for the generator.
 
   This state of affairs changed as Netpbm got firmly established as a
   base-level system package.  Security became critical for many users.  A
   crucial component of quality control is automated regression tests (="make
   check").  Unpredictable behavior gets in the way of testing.  One by one
   programs were given the -randomseed (or -seed) option to ensure reproducible
-  results.  Often this was done as new tests cases were written.  However,
+  results.  Often this was done as new test cases were written.  However,
   inconsistent output caused by system-level differences in rand()
   implementation remained a major obstacle.
 
@@ -219,6 +219,39 @@ pm_gaussrand(struct pm_randSt * const randStP) {
 
 
 
+uint32_t
+pm_rand32(struct pm_randSt * const randStP) {
+/*-----------------------------------------------------------------------------
+  Generate a 32-bit random number.
+
+  This is a provision for users who select a non-default random number
+  generator which returns less than 32 bits per call.  Many system generators
+  are known to return 31 bits (max = 2147483647 or 0x7FFFFFFF).
+
+  This does not work with generators that return less than 11 bits per call.
+  The least we know of is the archaic RANDU, which generates 15 bits (max =
+  32767 or 0x7FFF).
+-----------------------------------------------------------------------------*/
+    unsigned int const randMax = randStP->max;
+
+    uint32_t retval;
+
+    if (randMax >= 0xFFFFFFFF)
+        retval = pm_rand(randStP);
+    else {
+        uint32_t scale;
+
+        retval = 0;  /* Initial value */
+
+        for (scale = 0xFFFFFFFF; scale > 0; scale /= (randMax +1))
+            retval *= (randMax + 1) + pm_rand(randStP);
+    }
+
+    return retval;;
+}
+
+
+
 void
 pm_randinit(struct pm_randSt * const randStP) {
 /*----------------------------------------------------------------------------
diff --git a/lib/util/rand.h b/lib/util/rand.h
index c441890a..44095243 100644
--- a/lib/util/rand.h
+++ b/lib/util/rand.h
@@ -3,6 +3,8 @@
 #ifndef RAND_H_INCLUDED
 #define RAND_H_INCLUDED
 
+#include <inttypes.h>
+
 #include "netpbm/pm_c_util.h"
 #include "netpbm/mallocvar.h"
 
@@ -103,5 +105,8 @@ pm_gaussrand2(struct pm_randSt * const randStP,
 extern double
 pm_gaussrand(struct pm_randSt * const randStP);
 
+extern uint32_t
+pm_rand32(struct pm_randSt * const randStP);
+
 
 #endif
diff --git a/test/Execute-Tests b/test/Execute-Tests
index 7a02fdef..da4504a4 100755
--- a/test/Execute-Tests
+++ b/test/Execute-Tests
@@ -103,6 +103,10 @@ if [ ! -f ./testgrid.pbm ]
   then cp -v ${srcdir}/testgrid.pbm ./testgrid.pbm
 fi
 
+if [ ! -f ./maze.pbm ]
+  then cp -v ${srcdir}/maze.pbm ./maze.pbm
+fi
+
 if [ ! -f ./testimg.ppm ]
   then cp -v ${srcdir}/testimg.ppm  ./testimg.ppm 
 fi
@@ -181,7 +185,7 @@ elif [ $VALGRIND_TESTS = "on" ]
   # You may want to add --track-origins=yes to the above.
 
   for i in awk basename cat cksum cmp comm cp cut date dirname \
-           egrep fgrep file grep gs head iconv mkdir mktemp perl rm \
+           egrep fgrep file grep gs head iconv ls mkdir mktemp perl rm \
            sed seq sh sort tee tr uniq wc \
            testrandom Available-Testprog
 
@@ -248,6 +252,7 @@ fi
 
 if [ ! -s ${srcdir}/${tname%.test}.ok ]
 then
+  echo "${tname%.test}.ok does not exist"
   let result=4;
 else
   ${srcdir}/Available-Testprog \
@@ -299,7 +304,7 @@ fi
 # (Do not erase them if we are working from the source directory.)
 
 if [ ! $PWD -ef ${srcdir} ]
-    then rm ./testimg.ppm ./testgrid.pbm
+    then rm ./testimg.ppm ./testgrid.pbm ./maze.pbm
 fi
 
 
diff --git a/test/Test-Order b/test/Test-Order
index 6f4334af..c21f892d 100644
--- a/test/Test-Order
+++ b/test/Test-Order
@@ -27,6 +27,9 @@ ppmforge-parameters.test
 
 # Generators with random components
 
+pbmnoise1.test
+pbmnoise2.test
+pbmnoise-parameters.test
 pgmnoise.test
 pgmnoise-parameters.test
 ppmpat-random.test
@@ -136,6 +139,8 @@ rawtoppm.test
 
 pbm-misc-converters.test
 
+gif-transparent1.test
+
 # Miscellaneous utility tests
 
 ppmdfont.test
@@ -152,6 +157,7 @@ pnminvert-roundtrip.test
 pamflip-roundtrip.test
 pamdepth-roundtrip.test
 pad-crop-roundtrip.test
+pbm-ppm-roundtrip.test
 cut-paste-roundtrip.test
 rgb3-roundtrip.test
 ppmchange-roundtrip.test
diff --git a/test/all-in-place.ok b/test/all-in-place.ok
index 88626e3d..e50efc8c 100644
--- a/test/all-in-place.ok
+++ b/test/all-in-place.ok
@@ -125,6 +125,7 @@ pbmlife: ok
 pbmmake: ok
 pbmmask: ok
 pbmminkowski: ok
+pbmnoise: ok
 pbmpage: ok
 pbmpscale: ok
 pbmreduce: ok
diff --git a/test/all-in-place.test b/test/all-in-place.test
index e21c2ad8..d154dba4 100755
--- a/test/all-in-place.test
+++ b/test/all-in-place.test
@@ -167,6 +167,7 @@ ordinary_testprogs="\
   pbmmake \
   pbmmask \
   pbmminkowski \
+  pbmnoise \
   pbmpage \
   pbmpscale \
   pbmreduce \
diff --git a/test/atk-roundtrip.ok b/test/atk-roundtrip.ok
index 845be5fb..b9437e7d 100644
--- a/test/atk-roundtrip.ok
+++ b/test/atk-roundtrip.ok
@@ -1 +1,7 @@
-2425386270 41
+Test 1. Should print 281226646 481
+281226646 481
+Test 2. Should print 1824220442 125013 twice, then 2146497872 1000013 twice
+241513515 125013
+241513515 125013
+2146497872 1000013
+2146497872 1000013
diff --git a/test/atk-roundtrip.test b/test/atk-roundtrip.test
index 6db3df6d..f87c2e78 100755
--- a/test/atk-roundtrip.test
+++ b/test/atk-roundtrip.test
@@ -1,7 +1,23 @@
 #! /bin/bash
 # This script tests: pbmtoatk atktopbm
-# Also requires:
+# Also requires: pbmmake
 
+echo "Test 1. Should print 281226646 481"
+# cksum of maze.pbm
 
-# Should print 2425386270 41, cksum of testgrid.pbm
-pbmtoatk testgrid.pbm | atktopbm | cksum
+pbmtoatk maze.pbm | atktopbm | cksum
+
+tmpdir=${tmpdir:-/tmp}
+maxwidth_pbm=${tmpdir}/maxwidth.pbm
+maxheight_pbm=${tmpdir}/maxheight.pbm
+
+echo \
+ "Test 2. Should print 1824220442 125013 twice, then 2146497872 1000013 twice"
+
+pbmmake -g 1000000 1 | tee ${maxwidth_pbm} | cksum
+pbmtoatk ${maxwidth_pbm} | atktopbm | cksum
+
+pbmmake -b 1 1000000 | tee ${maxheight_pbm} | cksum
+pbmtoatk ${maxheight_pbm} | atktopbm | cksum
+
+rm ${maxwidth_pbm} ${maxheight_pbm}
diff --git a/test/bmp-roundtrip.ok b/test/bmp-roundtrip.ok
index 4f4f8367..217a54aa 100644
--- a/test/bmp-roundtrip.ok
+++ b/test/bmp-roundtrip.ok
@@ -1,14 +1,14 @@
-PBM
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-PPM
+Test 1 PBM.  Should print 281226646 481 four times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+Test 2 PPM.  Should print 1926073387 101484 four times
 1926073387 101484
 1926073387 101484
 1926073387 101484
 1926073387 101484
-PGM
+Test 3 PGM.  Should print 1571496937 33838 nine times
 1571496937 33838
 1571496937 33838
 1571496937 33838
diff --git a/test/bmp-roundtrip.test b/test/bmp-roundtrip.test
index c9ef363a..df858a09 100755
--- a/test/bmp-roundtrip.test
+++ b/test/bmp-roundtrip.test
@@ -4,27 +4,21 @@
 
 tmpdir=${tmpdir:-/tmp}
 
-# Test 1.  Should print 2425386270 41 four times
-
-echo PBM
+echo "Test 1 PBM.  Should print 281226646 481 four times"
 
 for mode in "" "-bpp=1" "-windows" "-os2"
   do
-  ppmtobmp ${mode} testgrid.pbm | bmptopnm | cksum
+  ppmtobmp ${mode} maze.pbm | bmptopnm | cksum
   done
 
-# Test 2.  Should print 1926073387 101484 four times
-
-echo PPM
+echo "Test 2 PPM.  Should print 1926073387 101484 four times"
 
 for mode in "" "-bpp=24" "-windows" "-os2"
   do
   ppmtobmp ${mode} testimg.ppm | bmptopnm | cksum
   done
 
-# Test 3.  Should print 1571496937 33838 nine times
-
-echo PGM
+echo "Test 3 PGM.  Should print 1571496937 33838 nine times"
 
 red_pgm=${tmpdir}/red.pgm
 mapfile_pgm=${tmpdir}/mapfile.pgm
diff --git a/test/cis-roundtrip.ok b/test/cis-roundtrip.ok
index da90078b..1bb50cb7 100644
--- a/test/cis-roundtrip.ok
+++ b/test/cis-roundtrip.ok
@@ -1,2 +1,5 @@
+Test 1. Should print 2631579683 1546 twice
 2631579683 1546
-2425386270 41
+2631579683 1546
+Test 2. Should print 281226646 481, cksum of maze.pbm
+281226646 481
diff --git a/test/cis-roundtrip.test b/test/cis-roundtrip.test
index 62fcc3b9..4ad1a5e4 100755
--- a/test/cis-roundtrip.test
+++ b/test/cis-roundtrip.test
@@ -1,14 +1,20 @@
 #! /bin/bash
 # This script tests: pbmtocis cistopbm
-# Also requires: pbmmake pamcut
+# Also requires: pbmmake pamcut pamfile
 
+tmpdir=${tmpdir:-/tmp}
+test_pbm=${tmpdir}/test.pbm
 
 # Output images produced by pbmtocis are of fixed size,
 # either 128x96 or 256x192.
 # Smaller input images are padded, larger ones are cropped.
 
-# Test 1. Should print 2631579683 1546
-pbmmake -g 128 96 | pbmtocis | cistopbm | cksum
+echo "Test 1. Should print 2631579683 1546 twice"
+pbmmake -g 128 96 | tee ${test_pbm} | cksum
+pbmtocis ${test_pbm} | cistopbm | cksum
 
-# Test 2. Should print 2425386270 41
-pbmtocis testgrid.pbm | cistopbm | pamcut 0 0 14 16 | cksum
+echo "Test 2. Should print 281226646 481, cksum of maze.pbm"
+size=$(pamfile -size maze.pbm | awk '{print "-width="$1,"-height="$2}')
+pbmtocis maze.pbm | cistopbm | pamcut -left=0 -top=0 ${size} | cksum
+
+rm ${test_pbm}
\ No newline at end of file
diff --git a/test/cmuw-roundtrip.ok b/test/cmuw-roundtrip.ok
index 845be5fb..4ec1a381 100644
--- a/test/cmuw-roundtrip.ok
+++ b/test/cmuw-roundtrip.ok
@@ -1 +1,2 @@
-2425386270 41
+Should print 281226646 481, cksum of maze.pbm
+281226646 481
diff --git a/test/cmuw-roundtrip.test b/test/cmuw-roundtrip.test
index e52adb70..725f9131 100755
--- a/test/cmuw-roundtrip.test
+++ b/test/cmuw-roundtrip.test
@@ -2,6 +2,5 @@
 # This script tests: pbmtocmuwm cmuwmtopbm
 # Also requires:
 
-
-# Should print 2425386270 41, cksum of testgrid.pbm
-pbmtocmuwm testgrid.pbm | cmuwmtopbm | cksum
+echo "Should print 281226646 481, cksum of maze.pbm"
+pbmtocmuwm maze.pbm | cmuwmtopbm | cksum
diff --git a/test/cut-cat-roundtrip.ok b/test/cut-cat-roundtrip.ok
index bc9b8cb4..edf1de67 100644
--- a/test/cut-cat-roundtrip.ok
+++ b/test/cut-cat-roundtrip.ok
@@ -1,92 +1,54 @@
-Test 1.
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-Test 2.
-3891261972 202953
-3891261972 202953
-3891261972 202953
-3891261972 202953
-3891261972 202953
-3891261972 202953
-Test 3.
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-Test 4.
-26789469 202953
-26789469 202953
-26789469 202953
-26789469 202953
-26789469 202953
-Test 5.
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-Test 6.
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-1887700557 73
-Test 7.
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-Test 8.
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
-3221289196 73
+Test 1.  Should print 1926073387 101484 six times
+1926073387 101484
+1926073387 101484
+1926073387 101484
+1926073387 101484
+1926073387 101484
+1926073387 101484
+Test 2.  Should print 3891261972 202953 1926073387 101484 six times
+3891261972 202953 1926073387 101484
+3891261972 202953 1926073387 101484
+3891261972 202953 1926073387 101484
+3891261972 202953 1926073387 101484
+3891261972 202953 1926073387 101484
+3891261972 202953 1926073387 101484
+Test 3.  Should print 1926073387 101484 five times
+1926073387 101484
+1926073387 101484
+1926073387 101484
+1926073387 101484
+1926073387 101484
+Test 4.  Should print 26789469 202953 1926073387 101484 five times
+26789469 202953 1926073387 101484
+26789469 202953 1926073387 101484
+26789469 202953 1926073387 101484
+26789469 202953 1926073387 101484
+26789469 202953 1926073387 101484
+Test 5.  Should print 281226646 481 six times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+Test 6.  Should print 1748767123 895 281226646 481 six times
+1748767123 895 281226646 481
+1748767123 895 281226646 481
+1748767123 895 281226646 481
+1748767123 895 281226646 481
+1748767123 895 281226646 481
+1748767123 895 281226646 481
+Test 7.  Should print 281226646 481 seven times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+Test 8.  Should print 1346655680 954 281226646 481 five times
+1346655680 954 281226646 481
+1346655680 954 281226646 481
+1346655680 954 281226646 481
+1346655680 954 281226646 481
+1346655680 954 281226646 481
diff --git a/test/cut-cat-roundtrip.test b/test/cut-cat-roundtrip.test
index 95ee5279..970c3e17 100755
--- a/test/cut-cat-roundtrip.test
+++ b/test/cut-cat-roundtrip.test
@@ -1,128 +1,146 @@
 #! /bin/bash
 # This script tests: pamcut
-# Also requires: pnmcat pnmpad
+# Also requires: pamfile pnmcat pnmpad pnmcrop
 
 tmpdir=${tmpdir:-/tmp}
-quant_ppm=${tmpdir}/quant.ppm
 
-right_pbm=${tmpdir}/right.pbm
-left_pbm=${tmpdir}/left.pbm
-right_ppm=${tmpdir}/right.ppm
 left_ppm=${tmpdir}/left.ppm
-bottom_pbm=${tmpdir}/bottom.pbm
-top_pbm=${tmpdir}/top.pbm
-bottom_ppm=${tmpdir}/bottom.ppm
+right_ppm=${tmpdir}/right.ppm
 top_ppm=${tmpdir}/top.ppm
+bottom_ppm=${tmpdir}/bottom.ppm
+padded_ppm=${tmpdir}/padded.ppm
+
+echo "Test 1.  Should print 1926073387 101484 six times"
 
+testimg_ppm_sum=`cat testimg.ppm | cksum`
+echo ${testimg_ppm_sum}
 
-# Test 1.  Should print 1926073387 101484 six times
-echo Test 1.
+ imgsize=$(pamfile -size testimg.ppm)
+ iw=$(echo ${imgsize} | cut -d" " -f1)
+ ih=$(echo ${imgsize} | cut -d" " -f2)
 
-cat testimg.ppm | cksum
 for i in 0 1 128 224 225
   do
   pamcut -left=$((i+1)) testimg.ppm > ${right_ppm}
   pamcut -right=$i      testimg.ppm > ${left_ppm}
   pnmcat -lr ${left_ppm} ${right_ppm} | \
-    pamcut -left=0 -width=227| cksum
+    pamcut -left=0 -width=${iw} | cksum
   rm ${left_ppm} ${right_ppm}
   done
 
+echo "Test 2.  Should print 3891261972 202953 1926073387 101484 six times"
+# Padding added to right.
 
-# Test 2.  Should print 3891261972 202953 six times
-# Not roundtrip.  Padding added to right.
-echo Test 2.
+pnmpad -right=${iw} -black testimg.ppm | cksum | tr '\n' ' '
+echo ${testimg_ppm_sum}
 
-pnmpad -right=227 -black testimg.ppm | cksum
-for i in  0 1 128 224 225
+for border in 0 1 128 224 225
   do
-  pamcut -left=$((i+1)) -width=227 -pad testimg.ppm > ${right_ppm}
-  pamcut -right=$i      -width=227 -pad testimg.ppm > ${left_ppm}
+  pamcut -left=$((${border}+1)) -width=${iw} -pad testimg.ppm > ${right_ppm}
+  pamcut -right=${border} -width=${iw} -pad testimg.ppm > ${left_ppm}
   pnmcat -lr ${left_ppm} ${right_ppm} ${left_ppm} | \
-    pamcut -left=$((227-i-1))  -width=$((227*2)) | cksum
-  rm ${left_ppm} ${right_ppm}
+    pamcut -left=$((${iw}-${border}-1))  -width=$((${iw}*2)) | \
+    tee ${padded_ppm} | cksum | tr '\n' ' '
+  pnmcrop -black -right ${padded_ppm} | cksum
+  rm ${left_ppm} ${right_ppm} ${padded_ppm}
   done
 
 
-# Test 3.  Should print 1926073387 101484 five times
-echo Test 3.
+echo "Test 3.  Should print 1926073387 101484 five times"
 
-cat testimg.ppm | cksum
-for i in 0 1 70 147
+echo ${testimg_ppm_sum}
+for border in 0 1 70 147
   do
-  pamcut -top=$((i+1)) testimg.ppm > ${bottom_ppm}
-  pamcut -bottom=$i    testimg.ppm > ${top_ppm}
+  pamcut -top=$((${border}+1)) testimg.ppm > ${bottom_ppm}
+  pamcut -bottom=${border}     testimg.ppm > ${top_ppm}
   pnmcat -tb ${top_ppm} ${bottom_ppm} | \
-    pamcut -top=0 -height=149 | cksum
+    pamcut -top=0 -height=${ih} | cksum
   rm ${top_ppm} ${bottom_ppm}
   done
 
-# Test 4.  Should print 26789469 202953 five times
-# Not roundtrip.  Padding added to bottom.
-echo Test 4.
+echo "Test 4.  Should print 26789469 202953 1926073387 101484 five times"
+# Padding added to bottom.
 
-pnmpad -bottom=149 -black testimg.ppm | cksum
-for i in 0 1 70 147
+pnmpad -bottom=${ih} -black testimg.ppm | cksum | tr '\n' ' '
+echo ${testimg_ppm_sum}
+for border in 0 1 70 147
   do
-  pamcut -top=$((i+1)) -height=149 -pad testimg.ppm > ${bottom_ppm}
-  pamcut -bottom=$i    -height=149 -pad testimg.ppm > ${top_ppm}
+  pamcut -top=$((${border}+1)) -height=${ih} -pad testimg.ppm > ${bottom_ppm}
+  pamcut -bottom=${border}     -height=${ih} -pad testimg.ppm > ${top_ppm}
   pnmcat -tb ${top_ppm} ${bottom_ppm} ${top_ppm} | \
-    pamcut -top=$((149-i-1))  -height=$((149*2)) | cksum
-  rm ${top_ppm} ${bottom_ppm}
+    pamcut -top=$((${ih}-${border}-1))  -height=$((${ih}*2)) | \
+    tee ${padded_ppm} | cksum | tr '\n' ' 'cksum
+  pnmcrop -black -bottom ${padded_ppm} | cksum
+  rm ${top_ppm} ${bottom_ppm} ${padded_ppm}
   done
 
-# Test 5.  Should print 2425386270 41 fourteen times
-echo Test 5.
+left_pbm=${tmpdir}/left.pbm
+right_pbm=${tmpdir}/right.pbm
+top_pbm=${tmpdir}/top.pbm
+bottom_pbm=${tmpdir}/bottom.pbm
+padded_pbm=${tmpdir}/padded.pbm
+
+echo "Test 5.  Should print 281226646 481 six times"
+
+maze_pbm_sum=`cat maze.pbm | cksum`
+echo ${maze_pbm_sum}
+
+imgsize=$(pamfile -size maze.pbm)
+iw=$(echo ${imgsize} | cut -d" " -f1)
+ih=$(echo ${imgsize} | cut -d" " -f2)
 
-cat testgrid.pbm | cksum
-for i in `seq 0 12`
+for i in 0 1 10 30 50
   do
-  pamcut -left=$((i+1)) testgrid.pbm > ${right_pbm}
-  pamcut -right=$i      testgrid.pbm > ${left_pbm}
-  pnmcat -lr ${left_pbm} ${right_pbm} | \
-    pamcut -left=0 -width=14 | cksum
-  rm ${left_pbm} ${right_pbm}
+  pamcut -left=$((i+1)) maze.pbm > ${right_ppm}
+  pamcut -right=$i      maze.pbm > ${left_ppm}
+  pnmcat -lr ${left_ppm} ${right_ppm} | \
+    pamcut -left=0 -width=${iw} | cksum
+  rm ${left_ppm} ${right_ppm}
   done
 
 
-# Test 6.  Should print 1887700557 73 fifteen times
-# Not roundtrip.  Padding added to right.
-echo Test 6.
+echo "Test 6.  Should print 1748767123 895 281226646 481 six times"
+# Padding added to right.
+
+pnmpad -right=${iw} -black maze.pbm | cksum | tr '\n' ' '
+echo ${maze_pbm_sum}
 
-pnmpad -right=14 -black testgrid.pbm | cksum
-for i in `seq 0 13`
+for border in 0 1 10 30 50
   do
-  pamcut -left=$((i+1)) -width=14 -pad testgrid.pbm > ${right_pbm}
-  pamcut -right=$i      -width=14 -pad testgrid.pbm > ${left_pbm}
-  pnmcat -lr ${left_pbm} ${right_pbm} ${left_pbm} | \
-    pamcut -left=$((14-i-1)) -width=28 | cksum
-  rm ${left_pbm} ${right_pbm}
+  pamcut -left=$((${border}+1)) -width=${iw} -pad maze.pbm > ${right_ppm}
+  pamcut -right=${border} -width=${iw} -pad maze.pbm > ${left_ppm}
+  pnmcat -lr ${left_ppm} ${right_ppm} ${left_ppm} | \
+    pamcut -left=$((${iw}-${border}-1))  -width=$((${iw}*2)) | \
+    tee ${padded_ppm} | cksum | tr '\n' ' '
+  pnmcrop -black -right ${padded_ppm} | cksum
+  rm ${left_ppm} ${right_ppm} ${padded_ppm}
   done
 
-# Test 7.  Should print 2425386270 41 sixteen times
-echo Test 7.
 
-cat testgrid.pbm | cksum
-for i in `seq 0 14`
+echo "Test 7.  Should print 281226646 481 seven times"
+
+echo ${maze_pbm_sum}
+for border in 0 1 12 21 31 44
   do
-  pamcut -top=$((i+1)) testgrid.pbm > ${bottom_pbm}
-  pamcut -bottom=$i    testgrid.pbm > ${top_pbm}
-  pnmcat -tb ${top_pbm} ${bottom_pbm} | \
-    pamcut -top=0 -height=16 | cksum
-  rm ${top_pbm} ${bottom_pbm}
+  pamcut -top=$((${border}+1)) maze.pbm > ${bottom_ppm}
+  pamcut -bottom=${border}     maze.pbm > ${top_ppm}
+  pnmcat -tb ${top_ppm} ${bottom_ppm} | \
+    pamcut -top=0 -height=${ih} | cksum
+  rm ${top_ppm} ${bottom_ppm}
   done
 
-# Test 8.  Should print 3221289196 73 seventeen times
-# Not roundtrip.  Padding added to bottom.
-echo Test 8.
+echo "Test 8.  Should print 1346655680 954 281226646 481 five times"
+# Padding added to bottom.
 
-pnmpad -bottom=16 -black testgrid.pbm | cksum
-for i in `seq 0 15`
+pnmpad -bottom=${ih} -black maze.pbm | cksum | tr '\n' ' '
+echo ${maze_pbm_sum}
+for border in 0 1 10 50
   do
-  pamcut -top=$((i+1)) -height=16 -pad testgrid.pbm > ${bottom_pbm}
-  pamcut -bottom=$i    -height=16 -pad testgrid.pbm > ${top_pbm}
-  pnmcat -tb ${top_pbm} ${bottom_pbm} ${top_pbm} | \
-    pamcut -top=$((16-i-1)) -height=32 | cksum
-  rm ${top_pbm} ${bottom_pbm}
+  pamcut -top=$((${border}+1)) -height=${ih} -pad maze.pbm > ${bottom_ppm}
+  pamcut -bottom=${border}     -height=${ih} -pad maze.pbm > ${top_ppm}
+  pnmcat -tb ${top_ppm} ${bottom_ppm} ${top_ppm} | \
+    pamcut -top=$((${ih}-${border}-1))  -height=$((${ih}*2)) | \
+    tee ${padded_ppm} | cksum | tr '\n' ' 'cksum
+  pnmcrop -black -bottom ${padded_ppm} | cksum
+  rm ${top_ppm} ${bottom_ppm} ${padded_ppm}
   done
-
diff --git a/test/cut-paste-roundtrip.test b/test/cut-paste-roundtrip.test
index 1bb3a7c2..ac2c0fe7 100755
--- a/test/cut-paste-roundtrip.test
+++ b/test/cut-paste-roundtrip.test
@@ -2,6 +2,7 @@
 # This script tests: pamcut pnmpaste
 # Also requires: pbmmake pnmpad
 
+tmpdir=${tmpdir:-/tmp}
 base_ppm=${tmpdir}/base.ppm
 
 # Test 1. Should produce 2999529086 101484
diff --git a/test/g3-roundtrip.ok b/test/g3-roundtrip.ok
index 603c9457..7140cc8b 100644
--- a/test/g3-roundtrip.ok
+++ b/test/g3-roundtrip.ok
@@ -1,6 +1,9 @@
+Test 1.  Should print 0 0 0 : 0 or 0 0 0 0 : 0 total three times
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 0 : 0
+Test 2.  Should print 0 0 0 : 0 or 0 0 0 0 : 0 total seven times
+then 1777627284 265, 2985378006 3135, 3651878552 3135
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 0 : 0
@@ -11,3 +14,9 @@
 1777627284 265
 2985378006 3135
 3651878552 3135
+Test 3.  Should print 0 0 0 : 0 twice
+then 356688157 17399694 twice
+0 0 0 : 0
+0 0 0 : 0
+356688157 17399694
+356688157 17399694
diff --git a/test/g3-roundtrip.test b/test/g3-roundtrip.test
index d18e4466..f35c4935 100755
--- a/test/g3-roundtrip.test
+++ b/test/g3-roundtrip.test
@@ -1,23 +1,33 @@
 #! /bin/bash
 # This script tests: g3topbm pbmtog3
-# Also requires: pnmcrop pbmmake
+# Also requires: pamfile pnmcrop pbmmake pbmpage pnmpad pamenlarge
 
 tmpdir=${tmpdir:-/tmp}
 complete256_pbm=${tmpdir}/complete256.pbm
 widew_pbm=${tmpdir}/widew.pbm
 wideb_pbm=${tmpdir}/wideb.pbm
+page_pbm=${tmpdir}/page.pbm
+pagemax_pbm=${tmpdir}/pagemax.pbm
 
-pbmtog3 -nofixedwidth testgrid.pbm | \
-g3topbm -width=14 | cmp -s - testgrid.pbm
+echo "Test 1.  Should print 0 0 0 : 0 or 0 0 0 0 : 0 total three times"
+
+width=$(pamfile -size maze.pbm | cut -d" " -f1)
+
+pbmtog3 -nofixedwidth maze.pbm | \
+g3topbm -width=${width} | cmp -s - maze.pbm
 echo ${PIPESTATUS[@]} ":" $?
 
-pbmtog3 -nofixedwidth -reversebits testgrid.pbm | \
-g3topbm -width=14 -reversebits | cmp -s - testgrid.pbm
+pbmtog3 -nofixedwidth -reversebits maze.pbm | \
+g3topbm -width=${width} -reversebits | cmp -s - maze.pbm
 echo ${PIPESTATUS[@]} ":" $?
 
-pbmtog3 testgrid.pbm | \
+pbmtog3 maze.pbm | \
 g3topbm  | pnmcrop -white -right -bottom | \
- cmp -s - testgrid.pbm ; echo ${PIPESTATUS[@]} ":" $?
+ cmp -s - maze.pbm ; echo ${PIPESTATUS[@]} ":" $?
+
+
+echo "Test 2.  Should print 0 0 0 : 0 or 0 0 0 0 : 0 total seven times"
+echo "then 1777627284 265, 2985378006 3135, 3651878552 3135"
 
 # works with gawk and mawk
 # produce all possible 8-bit patterns
@@ -27,7 +37,7 @@ LC_ALL=C awk 'BEGIN { print "P4";         # header
                       for (i=0;i<256;++i) # raster
                            printf("%c",i) }' > ${complete256_pbm}
 
-pbmtog3 -nofixedwidth  ${complete256_pbm} |  g3topbm -width=8 | \
+pbmtog3 -nofixedwidth  ${complete256_pbm} | g3topbm -width=8 | \
  cmp -s - ${complete256_pbm} ; echo ${PIPESTATUS[@]} ":" $?
 
 pbmtog3 -reverse -nofixedwidth ${complete256_pbm} | \
@@ -59,3 +69,20 @@ cat ${wideb_pbm} | cksum
 cat ${widew_pbm} | cksum
 
 rm ${complete256_pbm} ${wideb_pbm} ${widew_pbm}
+
+
+echo "Test 3.  Should print 0 0 0 : 0 twice"
+echo "then 356688157 17399694 twice"
+
+pbmpage 3 -a4 | pamenlarge 2 > ${page_pbm}
+pbmtog3 -nofixedwidth  ${page_pbm} | g3topbm | \
+ cmp -s - ${page_pbm} ; echo ${PIPESTATUS[@]} ":" $?
+
+pnmpad -width=1000 -height=1400 ${page_pbm} > ${pagemax_pbm}
+pbmtog3 -nofixedwidth  ${pagemax_pbm} | g3topbm  | \
+ cmp -s - ${pagemax_pbm} ; echo ${PIPESTATUS[@]} ":" $?
+
+cat ${page_pbm} | cksum
+cat ${pagemax_pbm} | cksum
+
+rm ${page_pbm} ${pagemax_pbm} 
\ No newline at end of file
diff --git a/test/gem-roundtrip.ok b/test/gem-roundtrip.ok
index 845be5fb..5168378d 100644
--- a/test/gem-roundtrip.ok
+++ b/test/gem-roundtrip.ok
@@ -1 +1 @@
-2425386270 41
+281226646 481
diff --git a/test/gem-roundtrip.test b/test/gem-roundtrip.test
index 0fce59db..106fe2cf 100755
--- a/test/gem-roundtrip.test
+++ b/test/gem-roundtrip.test
@@ -3,5 +3,5 @@
 # Also requires:
 
 
-# Should print 2425386270 41, cksum of testgrid.pbm
-pbmtogem testgrid.pbm | gemtopnm | cksum
+# Should print 281226646 481, cksum of maze.pbm
+pbmtogem maze.pbm | gemtopnm | cksum
diff --git a/test/gif-roundtrip.ok b/test/gif-roundtrip.ok
index 607ebd5c..27d1bb47 100644
--- a/test/gif-roundtrip.ok
+++ b/test/gif-roundtrip.ok
@@ -1,18 +1,23 @@
+Test 1. Should print 1926073387 101484
 1926073387 101484
+Test 2. Should produce 1571496937 33838 six times
 1571496937 33838
 1571496937 33838
 1571496937 33838
 1571496937 33838
 1571496937 33838
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
+1571496937 33838
+Test 3. Should produce 281226646 481 six times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+Test 4. Should produce P1 4 1 0101
 P1 4 1 0101 
-4030 : 0 0 0 0 : 0 , 4030 : 0 0 0 0 : 0
-4031 : 0 0 0 0 : 0 , 4031 : 0 0 0 0 : 0
-4097 : 0 0 0 0 : 0 , 4097 : 0 0 0 0 : 0
+Test 5. Should produce: N : 0 0 0 0 : 0 , N : 0 0 0 0 : 0
+(N=238, 239, 240, 241, 255, 256, 257, 4030, 4031, 4097)
 238 : 0 0 0 0 : 0 , 238 : 0 0 0 0 : 0
 239 : 0 0 0 0 : 0 , 239 : 0 0 0 0 : 0
 240 : 0 0 0 0 : 0 , 240 : 0 0 0 0 : 0
@@ -20,3 +25,6 @@ P1 4 1 0101
 255 : 0 0 0 0 : 0 , 255 : 0 0 0 0 : 0
 256 : 0 0 0 0 : 0 , 256 : 0 0 0 0 : 0
 257 : 0 0 0 0 : 0 , 257 : 0 0 0 0 : 0
+4030 : 0 0 0 0 : 0 , 4030 : 0 0 0 0 : 0
+4031 : 0 0 0 0 : 0 , 4031 : 0 0 0 0 : 0
+4097 : 0 0 0 0 : 0 , 4097 : 0 0 0 0 : 0
diff --git a/test/gif-roundtrip.test b/test/gif-roundtrip.test
index 1e6c7760..2bc3a98d 100755
--- a/test/gif-roundtrip.test
+++ b/test/gif-roundtrip.test
@@ -7,7 +7,8 @@ tmpdir=${tmpdir:-/tmp}
 # Test 1. Break up input image into three monochrome planes,
 # maxval 255.  Transform each plane to gif and back to pgm.
 # Reassemble the planes.  Result should be identical to input.
-# Should print 1926073387 101484
+
+echo "Test 1. Should print 1926073387 101484"
 
 test_ppm=${tmpdir}/testimg.ppm
 
@@ -29,13 +30,11 @@ pamtogif ${test_blu} | giftopnm | \
 
 rm ${test_ppm} ${test_grn} ${test_blu} ${out_red} ${out_grn}
 
-
-# Test 2. Should produce 1571496937 33838
-# which is the result of cksum testimg.red
-# five times
+echo "Test 2. Should produce 1571496937 33838 six times"
 
 test_gif=${tmpdir}/testimg.gif
 
+cat ${test_red} | cksum
 pamtogif ${test_red} | giftopnm | cksum
 pamtogif -interlace ${test_red} | giftopnm | cksum
 pamtogif -noclear ${test_red} | giftopnm | cksum
@@ -46,22 +45,23 @@ echo "junk" >> ${test_gif} && \
 
 rm  ${test_gif} ${test_red}
 
+echo "Test 3. Should produce 281226646 481 six times"
+# maze.pbm is too small for -noclear to take effect 
 
-# Test 3. Should produce 2425386270 41 five times.
-# testgrid.pbm is too small for -noclear to take effect 
-
-pamtogif testgrid.pbm | giftopnm | cksum
-pamtogif -nolzw testgrid.pbm | giftopnm | cksum
-pamtogif -transparent=black testgrid.pbm | giftopnm | cksum
-pamtogif -alpha=testgrid.pbm testgrid.pbm | giftopnm | cksum
-pamtogif -transparent=white testgrid.pbm | giftopnm -alpha=- | \
+cat maze.pbm | cksum
+pamtogif maze.pbm | giftopnm | cksum
+pamtogif -nolzw maze.pbm | giftopnm | cksum
+pamtogif -transparent=black maze.pbm | giftopnm | cksum
+pamtogif -alpha=maze.pbm maze.pbm | giftopnm | cksum
+pamtogif -transparent=white maze.pbm | giftopnm -alpha=- | \
   pnminvert | cksum
 
 
 # Test 4.
 # In this gif file the code length changes after the last image data.
 # Image data: 3 bits, end code 4 bits.
-# Should produce P1 4 1 0 1 0 1
+
+echo "Test 4. Should produce P1 4 1 0101"
 
 pbmmake -g 4 1 | \
   pamtogif -verbose | giftopnm -plain | \
@@ -71,25 +71,18 @@ echo ""
 
 # Test 5.
 # Test whether saturated code tables are handled correctly.
-# Test input images which most use up the string code table or
+# Test input images which use up the string code table or
 # come close to doing so.
 
-# Should produce:
-# 4030 0 , 4030 0
-# 4031 0 , 4031 0
-# 4097 0 , 4097 0
-# 238 0 , 238 0
-# 239 0 , 239 0
-# 240 0 , 240 0
-# 241 0 , 241 0
-# 255 0 , 255 0
-# 256 0 , 256 0
-# 257 0 , 257 0
 
-test4097_pgm=${tmpdir}/testimg4097.pgm
+echo "Test 5. Should produce: N : 0 0 0 0 : 0 , N : 0 0 0 0 : 0"
+echo "(N=238, 239, 240, 241, 255, 256, 257, 4030, 4031, 4097)"
+
 test_pgm=${tmpdir}/testimg.pgm
 
-# The following awk script produces a PGM file with no repeated sequences.
+# The following awk scripts produce a PGM file with no repeated
+# sequences.  Obviously this cannot be compressed at all; the codes
+# are consumed without ever being used.  
 # Here is a smaller example with the same property:
 # P2
 # 8 8
@@ -103,7 +96,9 @@ test_pgm=${tmpdir}/testimg.pgm
 # 3 6 3 7 4 5 4 6
 # 4 7 5 6 5 7 6 7
 
-maxval=63
+test257_pgm=${tmpdir}/testimg257.pgm
+
+maxval=15
 awk -v maxval=${maxval} 'BEGIN \
 { print "P2"
          print "1 "(maxval+1) * (maxval+1) +1
@@ -115,26 +110,25 @@ awk -v maxval=${maxval} 'BEGIN \
              for (j=i+1; j<=maxval; ++j)
                  {print i; print j }
          print 0
-}' > ${test4097_pgm}
+}' >  ${test257_pgm}
 
-for size in 4030 4031 4097
+for size in 238 239 240 241 255 256 257
   do
-  pamcut -height ${size} ${test4097_pgm} > ${test_pgm} &&
+  pamcut -height=${size} ${test257_pgm} > ${test_pgm} &&
   pamtogif -verbose ${test_pgm} | giftopnm | pamdepth ${maxval} | \
     cmp - ${test_pgm}
-  # pamdepth ${maxval} is necessary because
-  # giftopnm output is maxval 255
   echo -n ${size} ":" ${PIPESTATUS[@]} ":" $? ", "
-  pamtogif -nolzw ${test_pgm} | giftopnm | pamdepth ${maxval} | \
+  pamtogif -nolzw -verbose ${test_pgm} | giftopnm | pamdepth ${maxval} | \
     cmp - ${test_pgm}
   echo ${size} ":" ${PIPESTATUS[@]} ":" $?
   rm ${test_pgm}
   done 
 
-rm ${test4097_pgm}
-test257_pgm=${tmpdir}/testimg257.pgm
+rm ${test257_pgm}
 
-maxval=15
+test4097_pgm=${tmpdir}/testimg4097.pgm
+
+maxval=63
 awk -v maxval=${maxval} 'BEGIN \
 { print "P2"
          print "1 "(maxval+1) * (maxval+1) +1
@@ -146,18 +140,20 @@ awk -v maxval=${maxval} 'BEGIN \
              for (j=i+1; j<=maxval; ++j)
                  {print i; print j }
          print 0
-}' >  ${test257_pgm}
+}' > ${test4097_pgm}
 
-for size in 238 239 240 241 255 256 257
+for size in 4030 4031 4097
   do
-  pamcut -height=${size} ${test257_pgm} > ${test_pgm} &&
+  pamcut -height ${size} ${test4097_pgm} > ${test_pgm} &&
   pamtogif -verbose ${test_pgm} | giftopnm | pamdepth ${maxval} | \
     cmp - ${test_pgm}
+  # pamdepth ${maxval} is necessary because
+  # giftopnm output is maxval 255
   echo -n ${size} ":" ${PIPESTATUS[@]} ":" $? ", "
-  pamtogif -nolzw -verbose ${test_pgm} | giftopnm | pamdepth ${maxval} | \
+  pamtogif -nolzw ${test_pgm} | giftopnm | pamdepth ${maxval} | \
     cmp - ${test_pgm}
   echo ${size} ":" ${PIPESTATUS[@]} ":" $?
   rm ${test_pgm}
   done 
 
-rm ${test257_pgm}
+rm ${test4097_pgm}
diff --git a/test/gif-transparent1.ok b/test/gif-transparent1.ok
new file mode 100644
index 00000000..a531329a
--- /dev/null
+++ b/test/gif-transparent1.ok
@@ -0,0 +1,99 @@
+Test 0.
+Should print 0 or 1 0.
+Image 1: 1 0
+Image 2: 1 0
+Image 3: 0
+Image 4: 0
+Image 5: 0
+Test 1.
+Should print 0 unless otherwise stated.
+Image 1
+a1: 1 (should print 1)
+a2: 0
+a3: 0
+b1: 1 (should print 1)
+b2: 0
+b3: 0
+c1: 0
+c2: 0
+c3: 0
+c4: 0
+d1: 0
+d2: 0
+Image 2
+a1: 1 (should print 1)
+a2: 0
+a3: 0
+b1: 1 (should print 1)
+b2: 0
+b3: 0
+c1: 0
+c2: 0
+c3: 0
+c4: 0
+d1: 0
+d2: 0
+Image 3
+a1: 1 (should print 1)
+a2: 0
+a3: 0
+b1: 1 (should print 1)
+b2: 0
+b3: 0
+c1: 0
+c2: 0
+c3: 0
+c4: 0
+d1: 0
+d2: 0
+Test 2.
+Should print 0 unless otherwise stated.
+Image 1
+cl1: 0
+cl2: 0
+op1: 1 (should print 1)
+op2: 0
+Image 2
+cl1: 0
+cl2: 0
+op1: 1 (should print 1)
+op2: 0
+Image 3
+cl1: 0
+cl2: 0
+op1: 1 (should print 1)
+op2: 0
+Test 3.
+Should print 0 in all cases.
+Image 1
+a2: 0
+a3: 0
+b2: 0
+b3: 0
+c4: 0
+Image 2
+a2: 0
+a3: 0
+b2: 0
+b3: 0
+c4: 0
+Image 3
+a2: 0
+a3: 0
+b2: 0
+b3: 0
+c4: 0
+Test 4.
+Should print 0 in all cases.
+color=rgb:0f/0f/0f
+0
+0
+0
+color=rgb:ff/00/00
+0
+0
+0
+color=rgb:00/ff/00
+0
+0
+0
diff --git a/test/gif-transparent1.test b/test/gif-transparent1.test
new file mode 100755
index 00000000..4684d1fc
--- /dev/null
+++ b/test/gif-transparent1.test
@@ -0,0 +1,224 @@
+#! /bin/bash
+# This script tests: giftopnm pamtogif ppmtogif
+# Also requires: pamarith pamcomp pamdepth pamenlarge pamstack pbmmake
+# Also requires: pnminvert ppmchange ppmpat ppmtopgm ppmtoppm 
+
+tmpdir=${tmpdir:-/tmp}
+
+madras_ppm=${tmpdir}/madras.ppm
+gingham_ppm=${tmpdir}/gingham.ppm
+argyle_ppm=${tmpdir}/argyle.ppm
+
+alpha0_pbm=${tmpdir}/alpha0.pbm
+alpha1_pbm=${tmpdir}/alpha1.pbm
+clear_pbm=${tmpdir}/clear.pbm
+opaque_pbm=${tmpdir}/opaque.pbm
+alpha0_pam=${tmpdir}/alpha0.pam
+alpha1_pam=${tmpdir}/alpha1.pam
+clear_pam=${tmpdir}/clear.pam
+opaque_pam=${tmpdir}/opaque.pam
+
+image0_alpha_pam=${tmpdir}/image0.alpha.pam
+image1_alpha_pam=${tmpdir}/image1.alpha.pam
+
+out_pnm=${tmpdir}/out.pnm
+out0_ppm=${tmpdir}/out0.ppm
+out1_ppm=${tmpdir}/out1.ppm
+out2_ppm=${tmpdir}/out2.ppm
+a0_out_pbm=${tmpdir}/a0.out.pbm
+a1_out_pbm=${tmpdir}/a1.out.pbm
+
+# Make test images & alpha masks
+
+ppmpat -argyle2 \
+  -color=rgb:00/00/00,rgb:ff/ff/ff,rgb:ff/ff/ff 40 24 > ${argyle_ppm}
+ppmpat -gingham3 \
+  -color=rgb:3f/3f/3f,rgb:7f/7f/7f,rgb:ef/ef/ef 40 24 > ${gingham_ppm}
+ppmpat -madras \
+       -color=rgb:0f/0f/0f,rgb:ff/00/00,rgb:00/ff/00 40 24 > ${madras_ppm}
+
+pbmmake -g 5 3 | pamenlarge 8 | tee ${alpha0_pbm} | \
+  pamdepth 255 > ${alpha0_pam}
+pnminvert ${alpha0_pbm} | tee ${alpha1_pbm} | \
+  pamdepth 255 > ${alpha1_pam}
+
+pbmmake -w 40 24 | tee ${clear_pbm} | pamdepth 255 > ${clear_pam}
+pbmmake -b 40 24 | tee ${opaque_pbm} | pamdepth 255 > ${opaque_pam}
+
+
+echo "Test 0."
+echo "Should print 0 or 1 0."
+i=1
+for test_pnm in ${argyle_ppm} ${gingham_ppm} ${madras_ppm} \
+			      ${alpha0_pbm} ${alpha1_pbm}
+  do
+  echo -n "Image $i: "; i=$((i+1));
+  pamtogif ${test_pnm} | giftopnm | tee ${out_pnm} | \
+    cmp -s - ${test_pnm}; result=$?
+
+  if [ $result -eq 0 ];
+    then echo $?;
+  else
+    ppmtoppm < ${out_pnm} | cmp -s - ${test_pnm}; echo $result $?
+  fi
+  # madras, alpha, alpha1 : should match
+  # argyle: giftopnm produces PBM, gingham: giftopnm produces PGM
+  rm ${out_pnm}
+  done
+
+
+echo "Test 1."  # Color + alpha channel
+echo "Should print 0 unless otherwise stated."
+
+i=1
+for test_pnm in  ${argyle_ppm} ${gingham_ppm} ${madras_ppm}
+  do
+  echo "Image $i"; i=$((i+1));
+
+  pamstack -tupletype="RGB_ALPHA" ${test_pnm} ${alpha0_pam} > \
+    ${image0_alpha_pam}
+  pamstack -tupletype="RGB_ALPHA" ${test_pnm} ${alpha1_pam} > \
+    ${image1_alpha_pam}
+
+  pamtogif ${image0_alpha_pam} | giftopnm -alphaout=${a0_out_pbm} | \
+    ppmtoppm | tee ${out0_ppm} | \
+    cmp -s - ${test_pnm}; echo "a1:" $? "(should print 1)"
+  cmp -s ${alpha0_pbm} ${a0_out_pbm}; echo "a2:" $?  # Should match
+
+  pamarith -multiply ${test_pnm} ${alpha0_pbm} | \
+    cmp -s - ${out0_ppm}; echo "a3:" $? # Should match
+
+  pamtogif ${image1_alpha_pam} | giftopnm -alphaout=${a1_out_pbm} | \
+    ppmtoppm | tee ${out1_ppm} | \
+    cmp -s - ${test_pnm}; echo "b1:" $? "(should print 1)"
+
+  cmp -s ${alpha1_pbm} ${a1_out_pbm}; echo "b2:" $?  # Should match
+
+  pamarith -multiply ${test_pnm} ${alpha1_pam} | \
+    cmp -s - ${out1_ppm}; echo "b3:" $?    # Should match
+
+  pamarith -multiply ${test_pnm} ${alpha0_pbm} | \
+    cmp -s - ${out0_ppm}; echo "c1:" $?    # Should match
+
+  pamarith -multiply ${test_pnm} ${alpha1_pbm} | \
+    cmp -s - ${out1_ppm}; echo "c2:" $?    # Should match
+
+  pamcomp -alpha=${alpha0_pbm} ${out0_ppm} ${out1_ppm} | \
+    cmp -s - ${test_pnm}; echo "c3:" $?    # Should match
+
+  pamcomp -alpha=${alpha1_pbm} ${out1_ppm} ${out0_ppm} | \
+    cmp -s - ${test_pnm}; echo "c4:" $?    # Should match
+
+  pamtogif ${image0_alpha_pam} -alphacolor=rgb:00/00/80 | \
+    giftopnm  -alphaout=${a0_out_pbm} > /dev/null
+    cmp -s ${a0_out_pbm} ${alpha0_pbm}; echo "d1:" $?    # Should match
+
+  pamtogif ${image0_alpha_pam} -alphacolor=rgb:00/00/80 | \
+    giftopnm | ppmtoppm | \
+    ppmchange rgb:00/00/80 rgb:00/00/00 -remainder=rgb:ff/ff/ff | \
+    ppmtopgm | \
+    cmp -s - ${alpha0_pam}; echo "d2:" $?    # Should match
+
+  rm ${image0_alpha_pam} ${image1_alpha_pam}
+  rm ${out0_ppm} ${out1_ppm} # ${out0_pnm} ${out1_pnm}
+  rm ${a0_out_pbm} ${a1_out_pbm}
+
+  done
+
+echo "Test 2."  # clear/opaque alpha channel
+echo "Should print 0 unless otherwise stated."
+
+i=1
+for test_pnm in  ${argyle_ppm} ${gingham_ppm} ${madras_ppm}
+  do
+  echo "Image $i"; i=$((i+1));
+
+  pamstack -tupletype="RGB_ALPHA" ${test_pnm} ${clear_pam} > \
+    ${image0_alpha_pam}
+  pamstack -tupletype="RGB_ALPHA" ${test_pnm} ${opaque_pam} > \
+    ${image1_alpha_pam}
+
+  # clear
+  pamtogif ${image0_alpha_pam} | giftopnm -alphaout=${a0_out_pbm} | \
+    ppmtoppm | tee ${out0_ppm} | \
+    cmp -s - ${test_pnm}; echo "cl1:" $? # Should match
+  cmp -s ${clear_pbm} ${a0_out_pbm}; echo "cl2:" $?  # Should match
+
+  # opaque
+  pamtogif ${image1_alpha_pam} | giftopnm -alphaout=${a1_out_pbm} | \
+    tee ${out1_ppm} | \
+    cmp -s - ${test_pnm}; echo "op1:" $? "(should print 1)"
+
+  cmp -s ${opaque_pbm} ${a1_out_pbm}; echo "op2:" $?  # Should match
+
+  rm ${image0_alpha_pam} ${image1_alpha_pam}
+  rm ${out0_ppm} ${out1_ppm}
+  rm ${a0_out_pbm} ${a1_out_pbm}
+
+  done
+
+rm ${clear_pbm} ${clear_pam} ${opaque_pbm} ${opaque_pam}
+
+
+echo "Test 3."  # ppmtogif
+echo "Should print 0 in all cases."
+
+i=1
+for test_pnm in  ${argyle_ppm} ${gingham_ppm} ${madras_ppm}
+  do
+  echo "Image $i"; i=$((i+1));
+
+  ppmtogif -alpha=${alpha0_pbm} ${test_pnm} | \
+    giftopnm -alphaout=${a0_out_pbm} | \
+    ppmtoppm > ${out0_ppm}
+    cmp -s ${alpha0_pbm} ${a0_out_pbm}; echo "a2:" $?  # Should match
+  pamarith -multiply ${test_pnm} ${alpha0_pbm} | \
+    cmp -s - ${out0_ppm}; echo "a3:" $? # Should match
+
+  ppmtogif -alpha=${alpha1_pbm} ${test_pnm} | \
+    giftopnm -alphaout=${a1_out_pbm} | \
+    ppmtoppm > ${out1_ppm}
+    cmp -s ${alpha1_pbm} ${a1_out_pbm}; echo "b2:" $?  # Should match
+  pamarith -multiply ${test_pnm} ${alpha1_pam} | \
+    cmp -s - ${out1_ppm}; echo "b3:" $?    # Should match
+
+  pamcomp -alpha=${alpha1_pbm} ${out1_ppm} ${out0_ppm} | \
+    cmp -s - ${test_pnm}; echo "c4:" $?    # Should match
+
+  rm ${out0_ppm} ${out1_ppm}
+  rm ${a0_out_pbm} ${a1_out_pbm}
+
+  done
+
+rm ${alpha0_pbm} ${alpha1_pbm}
+rm ${alpha0_pam} ${alpha1_pam}
+rm ${argyle_ppm} ${gingham_ppm}
+
+
+echo "Test 4."
+echo "Should print 0 in all cases."
+# no separate alpha channel
+# designate colors which appear in madras as transparent 
+
+for color in "0f/0f/0f" "ff/00/00" "00/ff/00"
+  do
+  echo "color=rgb:"${color}
+  pamtogif -transparent=rgb:${color} ${madras_ppm} | \
+    giftopnm -alphaout=${a0_out_pbm} > ${out0_ppm}
+    cmp -s ${madras_ppm} ${out0_ppm}; echo $?   # Should match 
+
+  pbmmake -g 40 24 | pamdepth 255 | pamstack ${madras_ppm} - | 
+  pamtogif -transparent=rgb:${color} - | \
+    giftopnm -alphaout=${a1_out_pbm} > ${out1_ppm}
+    cmp -s ${madras_ppm} ${out1_ppm}; echo $?   # Should match 
+
+  ppmchange rgb:${color} rgb:00/00/00 \
+    -remainder=rgb:ff/ff/ff ${madras_ppm} > ${out2_ppm}
+
+  ppmtoppm < ${a0_out_pbm} | \
+    cmp -s ${out2_ppm} -; echo $?   # Should match 
+
+  rm ${out0_ppm} ${out1_ppm} ${out2_ppm} ${a0_out_pbm} ${a1_out_pbm}
+  done
+
+rm ${madras_ppm} 
diff --git a/test/ilbm-roundtrip.ok b/test/ilbm-roundtrip.ok
index 54574a18..fcbdf4f6 100644
--- a/test/ilbm-roundtrip.ok
+++ b/test/ilbm-roundtrip.ok
@@ -1,10 +1,14 @@
-829921912 685
-829921912 685
-829921912 685
-829921912 685
+Test 1.  Should produce 669206373 10102 five times
+669206373 10102
+669206373 10102
+669206373 10102
+669206373 10102
+Test 2.  Should produce 1926073387 101484 three times
 1926073387 101484
 1926073387 101484
 1926073387 101484
+Test 3.  Should print 984199586 101484
 984199586 101484
+Test 4.  Should print 2059976475 661 twice
 2059976475 661
 2059976475 661
diff --git a/test/ilbm-roundtrip.test b/test/ilbm-roundtrip.test
index f62368ff..85e04b26 100755
--- a/test/ilbm-roundtrip.test
+++ b/test/ilbm-roundtrip.test
@@ -2,26 +2,27 @@
 # This script tests: ppmtoilbm ilbmtoppm
 # Also requires: pamseq pamdepth pamtopnm pnmremap
 
-#Test. 1  Should produce 829921912 685 four times
-#Output is PPM raw, 14 by 16  maxval 255
-ppmtoilbm testgrid.pbm | ilbmtoppm | cksum
-ppmtoilbm -aga testgrid.pbm | ilbmtoppm | cksum
-ppmtoilbm -ham6 testgrid.pbm | ilbmtoppm | cksum
-ppmtoilbm -ham8 testgrid.pbm | ilbmtoppm | cksum
+echo "Test 1.  Should produce 669206373 10102 five times"
+#Output is PPM raw, 57 by 59  maxval 255
 
+ppmtoilbm maze.pbm | ilbmtoppm | cksum
+ppmtoilbm -aga maze.pbm | ilbmtoppm | cksum
+ppmtoilbm -ham6 maze.pbm | ilbmtoppm | cksum
+ppmtoilbm -ham8 maze.pbm | ilbmtoppm | cksum
 
-#Test. 2  Should produce 1926073387 101484 three times
+
+echo "Test 2.  Should produce 1926073387 101484 three times"
 ppmtoilbm testimg.ppm | ilbmtoppm | cksum
 ppmtoilbm -24force testimg.ppm | ilbmtoppm | cksum
 ppmtoilbm -dcbits 8 8 8 -nocompress testimg.ppm | ilbmtoppm | cksum
 
 
-#Test. 3  Should print 984199586 101484
+echo "Test 3.  Should print 984199586 101484"
 pamseq 3 5 -tupletype=RGB | pamdepth 255 | pamtopnm | \
   pnmremap -mapfile=- testimg.ppm | ppmtoilbm | ilbmtoppm | cksum
 
 
-#Test. 4 Should print 2059976475 661 twice
+echo "Test 4.  Should print 2059976475 661 twice"
 pamseq 3 5 -tupletype=RGB | pamtopnm | \
   ppmtoilbm -compress | ilbmtoppm | cksum
 pamseq 3 5 -tupletype=RGB | pamtopnm | \
diff --git a/test/jbig-roundtrip.ok b/test/jbig-roundtrip.ok
index b98a694b..c9164120 100644
--- a/test/jbig-roundtrip.ok
+++ b/test/jbig-roundtrip.ok
@@ -1,2 +1,2 @@
-2425386270 41
+281226646 481
 1571496937 33838
diff --git a/test/jbig-roundtrip.test b/test/jbig-roundtrip.test
index 570d7336..eac742ff 100755
--- a/test/jbig-roundtrip.test
+++ b/test/jbig-roundtrip.test
@@ -2,8 +2,8 @@
 # This script tests: pnmtojbig jbigtopnm
 # Also requires: pamchannel pamtopnm
 
-# Test 1.  Should print 2425386270 41
-pnmtojbig testgrid.pbm | jbigtopnm | cksum
+# Test 1.  Should print 281226646 481
+pnmtojbig maze.pbm | jbigtopnm | cksum
 
 # Test 2.  Should print 1571496937 33838
 pamchannel -infile=testimg.ppm -tupletype="GRAYSCALE" 0 | pamtopnm | \
diff --git a/test/lookup-roundtrip.test b/test/lookup-roundtrip.test
index 63ec0777..491294a2 100755
--- a/test/lookup-roundtrip.test
+++ b/test/lookup-roundtrip.test
@@ -10,3 +10,5 @@ ppmhist testimg.ppm -map > ${mapfile}
 # Test.  Should produce 1926073387 101484
 pamunlookup -lookupfile=${mapfile} testimg.ppm |\
   pamlookup -lookupfile=${mapfile} | cksum
+
+rm ${mapfile}
\ No newline at end of file
diff --git a/test/lps-roundtrip.ok b/test/lps-roundtrip.ok
index 02b1d1a9..277907af 100644
--- a/test/lps-roundtrip.ok
+++ b/test/lps-roundtrip.ok
@@ -1 +1,2 @@
+Test.  Should print match
 match
diff --git a/test/lps-roundtrip.test b/test/lps-roundtrip.test
index f30062a8..71cd354d 100755
--- a/test/lps-roundtrip.test
+++ b/test/lps-roundtrip.test
@@ -1,22 +1,25 @@
 #! /bin/bash
 # This script tests: pbmtolps pstopnm
-# Also requires: gs pamdepth pamscale pnmcrop pnmpsnr
+# Also requires: gs pamdepth pamscale pnmcrop pnmpsnr pamfile
 
 # The ordinary round-trip does not work because of the way ghostscript
 # renders: a line is considered wider than a single pixel and all pixels
 # it touches are set to black if the output is PBM.  To work around this,
 # we tell pstopnm to output PGM at a high resolution (=large dpi value).
 
+tmpdir=${tmpdir:-/tmp}
+test_pgm=${tmpdir}/test.pgm
 
-test_pgm=${tmpdir}/testgrid.pgm
+echo "Test.  Should print match"
 
+xysize=$(pamfile -size testgrid.pbm | awk '{print "-xsize="$1,"-ysize="$2}')
 pamdepth 255 testgrid.pbm > ${test_pgm}
-
+  
 pbmtolps -dpi 72 testgrid.pbm | \
     pstopnm -dpi $((72*12)) -stdout -pgm | \
-    pnmcrop -white | pamscale -xsize=14 -ysize=16 | \
+    pnmcrop -white | pamscale ${xysize}  | \
     pnmpsnr -target=30 - ${test_pgm}
-
-# ghostscript version 8.71: pnmpsnr lumina 33.14dB
-
+  
+    # ghostscript version 8.71: pnmpsnr lumina 33.14dB
+  
 rm ${test_pgm}
diff --git a/test/macp-roundtrip.ok b/test/macp-roundtrip.ok
index 9ff9d249..28c16ee0 100644
--- a/test/macp-roundtrip.ok
+++ b/test/macp-roundtrip.ok
@@ -1,5 +1,14 @@
-2425386270 41
-2425386270 41
+Test 1. Should produce 281226646 481 twice
+281226646 481
+281226646 481
+Test 2. Should produce 2329957971 51851
 2329957971 51851
+Test 3. Should produce 2907103393 5086 three times
 2907103393 5086
 2907103393 5086
+2907103393 5086
+Test 4. Should produce 3432623660 51851 four times
+3432623660 51851
+3432623660 51851
+3432623660 51851
+3432623660 51851
diff --git a/test/macp-roundtrip.test b/test/macp-roundtrip.test
index 4774a2c2..b264e3d1 100755
--- a/test/macp-roundtrip.test
+++ b/test/macp-roundtrip.test
@@ -1,31 +1,51 @@
 #! /bin/bash
 # This script tests: pbmtomacp macptopbm
-# Also requires: pnmcrop pbmpage pbmupc pnmpad
+# Also requires: pnmcrop pbmpage pbmupc pnmpad pbmmake
 
 tmpdir=${tmpdir:-/tmp}
-temp1_pbm=${tmpdir}/temp1.ppm
-temp2_pbm=${tmpdir}/temp2.ppm
+temp1_pbm=${tmpdir}/temp1.pbm
+temp2_pbm=${tmpdir}/temp2.pbm
+tempupc_pbm=${tmpdir}/tempupc.pbm
+temp3_pbm=${tmpdir}/temp3.pbm
+temp3_macp=${tmpdir}/temp3.macp
 
-
-# Test 1. Should produce 2425386270 41 twice
-# Because Macpaint files are fixed size (576 x 720 pixels)
+# Macpaint files are fixed size (576 x 720 pixels)
 # pbmtomacp automatically adds padding when input is smaller.
 
-pbmtomacp testgrid.pbm | macptopbm | tee ${temp1_pbm} | \
+echo "Test 1. Should produce 281226646 481 twice"
+pbmtomacp maze.pbm | macptopbm | tee ${temp1_pbm} | \
     pnmcrop | cksum
 
 pbmtomacp ${temp1_pbm} | macptopbm | pnmcrop | cksum
 
+rm ${temp1_pbm}
 
-#Test 2. Should produce 2329957971 51851
+echo "Test 2. Should produce 2329957971 51851"
 pbmpage 1 | pbmtomacp | macptopbm | cksum
 
-
-#Test 3. Should produce 2907103393 5086 twice
-pbmupc 0 12345 67890 | pnmpad -black -t 44 -b 20 -l 100 -r 251 | pbmtomacp | macptopbm | \
+echo "Test 3. Should produce 2907103393 5086 three times"
+pbmupc 0 12345 67890 | tee ${tempupc_pbm} | \
+    pnmpad -black -t 44 -b 20 -l 100 -r 251 | \
+    pbmtomacp | macptopbm | \
     tee ${temp2_pbm} | \
     pnmcrop | pnmcrop | cksum
 
+pnmpad -black -t 44 -l 100 ${tempupc_pbm} | \
+    pbmtomacp|  macptopbm | pnmcrop -white | pnmcrop | cksum
+
 pbmtomacp ${temp2_pbm} | macptopbm | pnmcrop | pnmcrop | cksum
 
-rm ${temp1_pbm} ${temp2_pbm}
+rm ${temp2_pbm} ${tempupc_pbm}
+
+
+echo "Test 4. Should produce 3432623660 51851 four times"
+pbmmake -b 576 720 | tee ${temp3_pbm} | cksum
+
+pbmtomacp ${temp3_pbm} | tee ${temp3_macp} | macptopbm | cksum
+
+pbmtomacp -norle ${temp3_pbm} | macptopbm | cksum
+
+( head -c 128 /dev/zero ; cat ${temp3_macp} ) | \
+    macptopbm -extraskip 128 | cksum
+
+rm ${temp3_pbm} ${temp3_macp}
diff --git a/test/maze.pbm b/test/maze.pbm
new file mode 100644
index 00000000..9bdd80e1
--- /dev/null
+++ b/test/maze.pbm
Binary files differdiff --git a/test/mda-roundtrip.ok b/test/mda-roundtrip.ok
index ef27ffd0..ab449885 100644
--- a/test/mda-roundtrip.ok
+++ b/test/mda-roundtrip.ok
@@ -1,2 +1,5 @@
+Test 1.  Should print 1757803444 169 twice
 1757803444 169
-2425386270 41
+1757803444 169
+Test 2.  Should print 281226646 481
+281226646 481
diff --git a/test/mda-roundtrip.test b/test/mda-roundtrip.test
index 58376f3f..6b4d89b9 100755
--- a/test/mda-roundtrip.test
+++ b/test/mda-roundtrip.test
@@ -1,13 +1,15 @@
 #! /bin/bash
 # This script tests: pbmtomda mdatopbm
-# Also requires: pbmmake pnmpad pamcut
+# Also requires: pbmmake pnmpad pamcut pamfile
 
 
 # Pbmtomda requires input width and height to be multiples of 8.
 
-# Test 1.  Should print 1757803444 169
+echo "Test 1.  Should print 1757803444 169 twice"
+pbmmake -g 32 40 | cksum
 pbmmake -g 32 40 | pbmtomda | mdatopbm | cksum
 
-# Test 2.  Should print 2425386270 41
-pnmpad -right 2 testgrid.pbm | \
-  pbmtomda | mdatopbm | pamcut 0 0 14 16 | cksum
+echo "Test 2.  Should print 281226646 481"
+size=$(pamfile -size maze.pbm | awk '{print "-width="$1,"-height="$2}')
+pnmpad -mwidth=8 -halign=0 -mheight=8 -valign=0 ${size} maze.pbm | \
+    pbmtomda | mdatopbm | pamcut -left=0 -top=0 ${size} | cksum
diff --git a/test/mgr-roundtrip.ok b/test/mgr-roundtrip.ok
index 845be5fb..0e845958 100644
--- a/test/mgr-roundtrip.ok
+++ b/test/mgr-roundtrip.ok
@@ -1 +1,7 @@
-2425386270 41
+Test 1.  Should print 281226646 481
+281226646 481
+Test 2.  Should print 429369764 1034, 448060073 4105 twice each
+429369764 1034
+429369764 1034
+448060073 4105
+448060073 4105
diff --git a/test/mgr-roundtrip.test b/test/mgr-roundtrip.test
index 252ae996..d96d836d 100755
--- a/test/mgr-roundtrip.test
+++ b/test/mgr-roundtrip.test
@@ -1,7 +1,15 @@
 #! /bin/bash
 # This script tests: pbmtomgr mgrtopbm
-# Also requires:
+# Also requires: pbmmake
 
+echo "Test 1.  Should print 281226646 481"
+pbmtomgr maze.pbm | mgrtopbm | cksum
+
+echo "Test 2.  Should print 429369764 1034, 448060073 4105 twice each"
+# Maximum width and height allowed
+
+pbmmake -g 4095 2 | cksum
+pbmmake -g 4095 2 | pbmtomgr | mgrtopbm | cksum
+pbmmake -g 1 4095 | cksum
+pbmmake -g 1 4095 | pbmtomgr | mgrtopbm | cksum
 
-# Should print 2425386270 41, cksum of testgrid.pbm
-pbmtomgr testgrid.pbm | mgrtopbm | cksum
diff --git a/test/mrf-roundtrip.ok b/test/mrf-roundtrip.ok
index 845be5fb..5168378d 100644
--- a/test/mrf-roundtrip.ok
+++ b/test/mrf-roundtrip.ok
@@ -1 +1 @@
-2425386270 41
+281226646 481
diff --git a/test/mrf-roundtrip.test b/test/mrf-roundtrip.test
index c9d8ce3a..8513a908 100755
--- a/test/mrf-roundtrip.test
+++ b/test/mrf-roundtrip.test
@@ -3,5 +3,5 @@
 # Also requires:
 
 
-# Should print 2425386270 41, cksum of testgrid.pbm
-pbmtomrf testgrid.pbm | mrftopbm | cksum
+# Should print 281226646 481, cksum of maze.pbm
+pbmtomrf maze.pbm | mrftopbm | cksum
diff --git a/test/pad-crop-roundtrip.ok b/test/pad-crop-roundtrip.ok
index 0e712ce7..764cadfa 100644
--- a/test/pad-crop-roundtrip.ok
+++ b/test/pad-crop-roundtrip.ok
@@ -1,4 +1,4 @@
 1926073387 101484
 1926073387 101484
-2425386270 41
-2425386270 41
+281226646 481
+281226646 481
diff --git a/test/pad-crop-roundtrip.test b/test/pad-crop-roundtrip.test
index 75979901..8cca8a5f 100755
--- a/test/pad-crop-roundtrip.test
+++ b/test/pad-crop-roundtrip.test
@@ -2,17 +2,25 @@
 # This script tests: pnmcrop pnmmargin pnmpad
 # Also requires:
 
+# Test 1. Should produce 1926073387 101484, cksum of testimg.ppm
+
 pnmmargin -white 10 testimg.ppm | \
   pnmcrop | cksum
 
+# Test 2. Should produce 1926073387 101484
+
 pnmpad -white -left 10 -top 10 testimg.ppm | \
   pnmpad -white -right 10 -bottom 10 | \
   pnmcrop -right -bottom | pnmcrop -left -top | cksum
 
-pnmmargin -white 10 testgrid.pbm | \
+# Test 3. Should produce 281226646 481, cksum of maze.pbm
+
+pnmmargin -white 10 maze.pbm | \
   pnmcrop | cksum
 
-pnmpad -white -left 10 -top 10 testgrid.pbm | \
+# Test 4. Should produce 281226646 481
+
+pnmpad -white -left 10 -top 10 maze.pbm | \
   pnmpad -white -right 10 -bottom 10 | \
   pnmcrop -left -right | pnmcrop -top -bottom | cksum
 
diff --git a/test/palm-roundtrip.ok b/test/palm-roundtrip.ok
index b9ea3056..d69c3a96 100644
--- a/test/palm-roundtrip.ok
+++ b/test/palm-roundtrip.ok
@@ -1,10 +1,10 @@
-Test 1
-584219238 236
-584219238 236
-584219238 236
-584219238 236
-584219238 236
-Test 2
+Test 1. Should print 2067616594 3375 five times
+2067616594 3375
+2067616594 3375
+2067616594 3375
+2067616594 3375
+2067616594 3375
+Test 2. Should print 0 0 0  : 0 four times
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 : 0
diff --git a/test/palm-roundtrip.test b/test/palm-roundtrip.test
index b00454ca..20fcefa9 100755
--- a/test/palm-roundtrip.test
+++ b/test/palm-roundtrip.test
@@ -6,11 +6,9 @@ tmpdir=${tmpdir:-/tmp}
 test4bit_pgm=${tmpdir}/test4bit.pgm
 test256color_ppm=${tmpdir}/test256color.ppm
 
-# Test 1. Should print 584219238 236  5 times
+echo "Test 1. Should print 2067616594 3375 five times"
 
-echo "Test 1"
-
-pamdepth 15 testgrid.pbm | tee ${test4bit_pgm} | cksum
+pamdepth 15 maze.pbm | tee ${test4bit_pgm} | cksum
 
 for flags in "" \
              "-scanline_compression" \
@@ -21,9 +19,7 @@ for flags in "" \
 rm ${test4bit_pgm}
 
 
-# Test 2. Should print 0 4 times
-
-echo "Test 2"
+echo "Test 2. Should print 0 0 0  : 0 four times"
 
 pnmquant 256 testimg.ppm > ${test256color_ppm} || echo "pnmquant failed"
 
diff --git a/test/pamarith-compare-equal.ok b/test/pamarith-compare-equal.ok
index ef1c2981..d6b1f624 100644
--- a/test/pamarith-compare-equal.ok
+++ b/test/pamarith-compare-equal.ok
@@ -6,6 +6,17 @@ Test 1
 1:P3
 1:8 8
 1:2
+1:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 0 0 0 2 2 2 0 0 
+1:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 2 2 2 0 0 0 2 2 
+1:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 0 0 0 2 2 2 0 0 
+1:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 2 2 2 0 0 0 2 2 
+1:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 0 0 0 2 2 2 0 0 
+1:0 2 2 2 0 0 0 2 2 2 0 0 0 2 2 1 1 1 1 1 1 1 1 1 
+1:2 0 0 0 2 2 2 0 0 0 2 2 2 0 0 1 1 1 1 1 1 1 1 1 
+1:0 2 2 2 0 0 0 2 2 2 0 0 0 2 2 1 1 1 1 1 1 1 1 1 
+1:P3
+1:8 8
+1:2
 5:0 0 2 0 0 2 0 0 2 0 0 2 0 0 2 2 0 2 2 0 2 2 0 2 
 3:2 0 2 2 0 2 2 0 2 2 0 2 2 0 2 2 0 0 2 0 0 2 0 0 
 Test 2
@@ -16,14 +27,19 @@ Test 2
 1:P3
 1:8 8
 1:1
+5:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 
+3:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 
+1:P3
+1:8 8
+1:1
 8:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
 Test 3
 1:P1
-1:14 16
-16:00000000000000
+1:35 33
+33:00000000000000000000000000000000000
 1:P1
-1:14 16
-16:11111111111111
+1:35 33
+33:11111111111111111111111111111111111
 1:P1
 1:14 16
 8:10101010101010 11111111111111
@@ -32,9 +48,9 @@ Test 3
 8:01010101010101 00000000000000
 Test 4
 1:P2
-1:14 16
+1:35 33
 1:2
-16:1 1 1 1 1 1 1 1 1 1 1 1 1 1 
+33:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
 1:P2
 1:14 16
 1:2
@@ -48,11 +64,11 @@ Test 4
 1:2
 8:1 2 1 2 1 2 1 2 1 2 1 2 1 2  1 1 1 1 1 1 1 1 1 1 1 1 1 1 
 Test 5
-Should produce 1285449498 54
+Should produce 1285449498 54 four times
 1285449498 54
 1285449498 54
 1285449498 54
 1285449498 54
-Should produce 244506188 54
+Should produce 244506188 54 twice
 244506188 54
 244506188 54
diff --git a/test/pamarith-compare-equal.test b/test/pamarith-compare-equal.test
index 903ad538..0af9ee27 100755
--- a/test/pamarith-compare-equal.test
+++ b/test/pamarith-compare-equal.test
@@ -1,20 +1,25 @@
 #! /bin/bash
 # This script tests: pamarith
-# Also requires: ppmpat pnminvert pbmmake pgmmake
+# Also requires: ppmpat pnminvert pamfile pbmmake pgmmake pamcut
 
 tmpdir=${tmpdir:-/tmp}
 input1_ppm=${tmpdir}/input1.ppm
+inputm_ppm=${tmpdir}/inputm.ppm
 input2_ppm=${tmpdir}/input2.ppm
 
 echo "Test 1"
 
 ppmpat -tartan -color=rgb:00/80/ff,rgb:80/ff/00,rgb:ff/00/80 \
    8 8  > ${input1_ppm}
+ppmpat -tartan -color=rgb:00/80/ff,rgb:80/ff/00,rgb:ff/00/80 \
+   -mesh 8 8  > ${inputm_ppm}
 ppmpat -tartan -color=rgb:80/ff/00,rgb:ff/00/80,rgb:00/80/ff \
    8 8  > ${input2_ppm}
 
 pamarith -compare -plain ${input1_ppm} ${input1_ppm} | \
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
+pamarith -compare -plain ${input1_ppm} ${inputm_ppm} | \
+  uniq -c | sed -e 's/^ *//' -e 's/ /:/'
 pamarith -compare -plain ${input1_ppm} ${input2_ppm} | \
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
 
@@ -22,54 +27,66 @@ echo "Test 2"
 
 pamarith -equal -plain ${input1_ppm} ${input1_ppm} | \
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
+pamarith -equal -plain ${input1_ppm} ${inputm_ppm} | \
+  uniq -c | sed -e 's/^ *//' -e 's/ /:/'
 pamarith -equal -plain ${input1_ppm} ${input2_ppm} | \
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
 
-rm ${input1_ppm} ${input2_ppm}
+rm ${input1_ppm} ${inputm_ppm} ${input2_ppm}
 
 echo "Test 3"
 
-pamarith -equal -plain testgrid.pbm testgrid.pbm  | \
+input3_pbm=${tmpdir}/input3.pbm
+pamcut maze.pbm -left=0 -top=0 -width=35 -height=33 > ${input3_pbm} 
+
+pamarith -equal -plain ${input3_pbm} ${input3_pbm}  | \
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
-pnminvert testgrid.pbm | pamarith -equal -plain testgrid.pbm - | \
+pnminvert ${input3_pbm} | pamarith -equal -plain ${input3_pbm} - | \
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
-pbmmake -w 14 16 | pamarith -equal -plain testgrid.pbm - | \
+
+xysize=$(pamfile -size testgrid.pbm)
+
+pbmmake -w ${xysize} | pamarith -equal -plain testgrid.pbm - | \
   awk '{printf("%s%c",$0, NR<3 || NR%2==0 ? "\n" : " ")}' | 
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
-pbmmake -b 14 16 | pamarith -equal -plain testgrid.pbm - | \
+pbmmake -b ${xysize} | pamarith -equal -plain testgrid.pbm - | \
   awk '{printf("%s%c",$0, NR<3 || NR%2==0 ? "\n" : " ")}' | 
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
 
 echo "Test 4"
-  
-pamarith -compare -plain testgrid.pbm testgrid.pbm  | \
+
+
+pamarith -compare -plain ${input3_pbm} ${input3_pbm}  | \
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
+
 pnminvert testgrid.pbm | pamarith -compare -plain testgrid.pbm - | \
   awk '{printf("%s%c",$0, NR<4 || NR%2==1 ? "\n" : " ")}' | 
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
-pbmmake -w 14 16 | pamarith -compare -plain testgrid.pbm - | \
+pbmmake -w ${xysize} | pamarith -compare -plain testgrid.pbm - | \
   awk '{printf("%s%c",$0, NR<4 || NR%2==1 ? "\n" : " ")}' | 
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
-pbmmake -b 14 16 | pamarith -compare -plain testgrid.pbm - | \
+pbmmake -b ${xysize} | pamarith -compare -plain testgrid.pbm - | \
   awk '{printf("%s%c",$0, NR<4 || NR%2==1 ? "\n" : " ")}' | 
   uniq -c | sed -e 's/^ *//' -e 's/ /:/'
 
+rm ${input3_pbm}
 
 echo "Test 5"
 # -closeness
 
 input40_pgm=${tmpdir}/input40.pgm
 input50_pgm=${tmpdir}/input50.pgm
-equal_pgm=${tmpdir}/equal.pgm
 
 pgmmake -maxval=99 0.40 7 3 > ${input40_pgm}
 pgmmake -maxval=99 0.50 7 3 > ${input50_pgm}
 
-echo "Should produce 1285449498 54"
+echo "Should produce 1285449498 54 four times"
 pamarith -equal -plain ${input40_pgm} ${input40_pgm} | cksum
 pamarith -equal -plain -closeness=10.2 ${input40_pgm} ${input50_pgm} | cksum
 pamarith -equal -plain -closeness=11 ${input40_pgm} ${input50_pgm} | cksum
 pamarith -equal -plain -closeness=90 ${input40_pgm} ${input50_pgm} | cksum
-echo "Should produce 244506188 54"
+echo "Should produce 244506188 54 twice"
 pamarith -equal -plain ${input40_pgm} ${input50_pgm} | cksum
-pamarith -equal -plain -closeness=09   ${input40_pgm} ${input50_pgm} | cksum
+pamarith -equal -plain -closeness=09  ${input40_pgm} ${input50_pgm} | cksum
+
+rm ${input40_pgm} ${input50_pgm}
\ No newline at end of file
diff --git a/test/pamarith.ok b/test/pamarith.ok
index 0862df8e..c9fc49db 100644
--- a/test/pamarith.ok
+++ b/test/pamarith.ok
@@ -193,19 +193,19 @@ Test 3
 3072492814 913
 3072492814 913
 Test 4 (input = output)
-cksum is 2425386270 41 or 2921940274 59
+Prints 281226646 481 five times, then 2921940274 59 five times
 input image
-2425386270 41
+281226646 481
 -minimum
-2425386270 41
+281226646 481
 -maximum
-2425386270 41
+281226646 481
 -mean
-2425386270 41
+281226646 481
 -and
-2425386270 41
+281226646 481
 -or
-2425386270 41
+281226646 481
 input image
 2921940274 59
 -minimum
@@ -219,13 +219,13 @@ input image
 -or
 2921940274 59
 Test 5 (blank output)
-cksum is 2817549367 41 or 320101399 59
+Prints 2247801875 481 three times, then 320101399 59 three times
 -subtract
-2817549367 41
+2247801875 481
 -difference
-2817549367 41
+2247801875 481
 -xor
-2817549367 41
+2247801875 481
 -subtract
 320101399 59
 -difference
diff --git a/test/pamarith.test b/test/pamarith.test
index b7fe3212..b6b4538c 100755
--- a/test/pamarith.test
+++ b/test/pamarith.test
@@ -88,9 +88,9 @@ for fn in "-subtract" "-divide" "-compare" "-shiftleft" "-shiftright"
 rm ${input3_ppm} ${input4_ppm} ${input4_pgm}
 
 echo "Test 4 (input = output)"
-echo "cksum is 2425386270 41 or 2921940274 59"
+echo "Prints 281226646 481 five times, then 2921940274 59 five times"
 
-for image in testgrid.pbm ${input1_ppm}
+for image in maze.pbm ${input1_ppm}
   do
   echo "input image"
   cat ${image} | cksum
@@ -103,9 +103,9 @@ for image in testgrid.pbm ${input1_ppm}
 
 
 echo "Test 5 (blank output)"
-echo "cksum is 2817549367 41 or 320101399 59"
+echo "Prints 2247801875 481 three times, then 320101399 59 three times"
 
-for image in testgrid.pbm ${input1_ppm}
+for image in maze.pbm ${input1_ppm}
   do
   for fn in "-subtract" "-difference" "-xor"
     do
@@ -116,7 +116,6 @@ for image in testgrid.pbm ${input1_ppm}
 
 rm ${input1_ppm}
 
-
 echo "Invalid command-line argument combinations." 1>&2
 echo "Error messages should appear below the line." 1>&2
 echo "-----------------------------------------------------------" 1>&2
diff --git a/test/pamchannel.ok b/test/pamchannel.ok
index 41d3f890..91629202 100644
--- a/test/pamchannel.ok
+++ b/test/pamchannel.ok
@@ -1,9 +1,11 @@
-Test 1
+Test 1:red-channel  Should produce 1571496937 33838
 1571496937 33838
-Test 2
+Test 2:green-channel  Should produce 394856971 33838
 394856971 33838
-Test 3
+Test 3:blue-channel  Should produce 3164158573 33838
 3164158573 33838
+Test 4:single-channel  Should produce 281226646 481
+281226646 481
 Test Invalid
 Expected failure 1 1
 Expected failure 2 1
diff --git a/test/pamchannel.test b/test/pamchannel.test
index 6bb0cf67..bce1d4eb 100755
--- a/test/pamchannel.test
+++ b/test/pamchannel.test
@@ -12,27 +12,23 @@
 # 3164158573 33838 testimg.blu
 
 
-# Test 1. red channel
-# Should produce 1571496937 33838
-echo "Test 1"
-
+echo "Test 1:red-channel  Should produce 1571496937 33838"
 pamchannel -infile testimg.ppm 0 | \
   pamtopnm --assume | cksum
 
-# Test 2. green channel
-# Should produce  394856971 33838
-echo "Test 2"
-
+echo "Test 2:green-channel  Should produce 394856971 33838"
 pamchannel -infile testimg.ppm -tupletype="GRAYSCALE" 1 | \
   pamtopnm | cksum
 
-# Test 3. blue channel
-# Should produce 3164158573 33838
-echo "Test 3"
+echo "Test 3:blue-channel  Should produce 3164158573 33838"
 
 pamchannel -infile testimg.ppm 2 | \
   pamtopnm --assume | cksum
 
+echo "Test 4:single-channel  Should produce 281226646 481"
+pamchannel -infile maze.pbm 0 | \
+  pamtopnm --assume | cksum
+
 # Test invalid: specified channel does not exist
 
 echo 1>&2
@@ -48,14 +44,15 @@ echo "Test Invalid"
 pamchannel  -infile testgrid.pbm 1 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamchannel  -infile testimg.ppm 3 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamtopam testimg.ppm | pamchannel -infile=- 4 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
+
diff --git a/test/pamcrater.test b/test/pamcrater.test
index 027e34e3..25b12135 100755
--- a/test/pamcrater.test
+++ b/test/pamcrater.test
@@ -52,7 +52,7 @@ pamshadedrelief ${test100_pam} > ${testshaded_pam}
   pamcut -top=$((128 - 99)) -height=1  ${testshaded_pam} | cksum
 ) | uniq -c | awk '{print $1}'
 
-rm ${testshaded_pam} ${test100_pam}
+rm ${testshaded_pam} ${test100_pam} ${test150_pam}
 
 echo "Test Invalid."
 
@@ -66,19 +66,19 @@ echo "------------------------------" 1>&2
 pamcrater -width 0 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcrater -height 0 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcrater -number 0 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcrater -test -radius=10 | pamshadedrelief -gamma 0 > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamcut.ok b/test/pamcut.ok
index 3d5c5a7f..fc4dc3de 100644
--- a/test/pamcut.ok
+++ b/test/pamcut.ok
@@ -1,22 +1,22 @@
-Test 1.
+Test 1.  Should print 2958909756 124815
 2958909756 124815
-Test 2.
+Test 2.  Should print 1550940962 10933
 1550940962 10933
-Test 3.
+Test 3.  Should print 708474423 14 twice
 708474423 14
 708474423 14
-Test 4.
+Test 4.  Should print 659346598 80124 four times
 659346598 80124
 659346598 80124
 659346598 80124
 659346598 80124
-Test 5.
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-Test 6.
+Test 5. Should print 281226646 481 five times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+Test 6.  Should print 3412257956 129
 3412257956 129
 Test Invalid
 Expected failure 1 1
diff --git a/test/pamcut.test b/test/pamcut.test
index ced45f9c..e18c3cf2 100755
--- a/test/pamcut.test
+++ b/test/pamcut.test
@@ -1,28 +1,23 @@
 #! /bin/bash
 # This script tests: pamcut pbmmake
-# Also requires:
+# Also requires: pamfile
 
-# Test 1.  Should print 2958909756 124815
-echo Test 1.
+echo "Test 1.  Should print 2958909756 124815"
 
 pamcut -top 0 -left 0 -width 260 -height 160 \
   -pad testimg.ppm | cksum
 
-# Test 2.  Should print 1550940962 10933
-echo Test 2.
+echo "Test 2.  Should print 1550940962 10933"
 
 pamcut -top 200 -left 120 -width 40 -height 40 \
   -pad testimg.ppm | cksum
 
-# Test 3.  Should print 708474423 14 twice
-echo Test 3.
+echo "Test 3.  Should print 708474423 14 twice"
 
 pamcut -top 5 -left 5 -bottom 5 -right 5 testimg.ppm | cksum
 pamcut -width 1 -height 1 -bottom 5 -right 5 testimg.ppm | cksum
 
-
-# Test 4.  Should print 659346598 80124 four times
-echo Test 4.
+echo "Test 4.  Should print 659346598 80124 four times"
 
 pamcut -croptop 10 -cropleft 10 -cropbottom 10 -cropright 10 testimg.ppm | \
   cksum
@@ -30,19 +25,20 @@ pamcut -top 10 -left 10 -bottom 138 -right 216 testimg.ppm | cksum
 pamcut -top 10 -left 10 -bottom -11 -right -11 testimg.ppm | cksum
 pamcut -top 10 -left 10 -width 207 -height 129 testimg.ppm | cksum
 
+echo "Test 5. Should print 281226646 481 five times"
 
-# Test 5. Should print 2425386270 41 five times
-echo Test 5.
-
-pamcut -croptop 0 -cropleft 0 -cropbottom 0 -cropright 0 testgrid.pbm | \
+width=$(pamfile -size maze.pbm | awk '{print $1}')
+height=$(pamfile -size maze.pbm | awk '{print $2}')
+ 
+pamcut -croptop 0 -cropleft 0 -cropbottom 0 -cropright 0 maze.pbm | \
   cksum
-pamcut -top 0 -left 0 -bottom 15 -right 13 testgrid.pbm | cksum
-pamcut -top 0 -left 0 -bottom -1 -right -1 testgrid.pbm | cksum
-pamcut -top 0 -left 0 -width 14 -height 16 testgrid.pbm | cksum
-pamcut testgrid.pbm | cksum
-
-# Test 6.  Should print 3412257956 129
-echo Test 6.
+pamcut -top 0 -left 0 -bottom $((${height} -1)) -right $((${width} -1)) \
+  maze.pbm | cksum
+pamcut -top 0 -left 0 -bottom -1 -right -1 maze.pbm | cksum
+pamcut -top 0 -left 0 -width ${width} -height ${height} maze.pbm | cksum
+pamcut maze.pbm | cksum
+ 
+echo "Test 6.  Should print 3412257956 129"
 
 pbmmake -g 50 50 | pamcut 5 5 30 30 | cksum
 
@@ -61,77 +57,77 @@ echo "-----------------------------------------------------------" 1>&2
 pamcut -left=1 -right=1 -width=14 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -top=1 -bottom=1 -height=16 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -right=1 -cropright=1 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -top=1 -croptop=1 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -bottom=1 -cropbottom=1 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -left=1 -cropleft=1 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # excessive cropping
 pamcut -cropleft=7 -cropright=8 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -left=7 -right=6 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -croptop=8 -cropbottom=8 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -top=10 -bottom=9 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # pad absent
 pamcut -cropleft=1 -width=14 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 10"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut -croptop=1  -height=16 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 11"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # legacy style: insufficient number of positional parameters
 pamcut 5 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 12"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut 5 4 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 13"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamcut 5 5 30 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 14"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamdepth-roundtrip.ok b/test/pamdepth-roundtrip.ok
index 89db1a57..28db2ee1 100644
--- a/test/pamdepth-roundtrip.ok
+++ b/test/pamdepth-roundtrip.ok
@@ -5,4 +5,4 @@
 1926073387 101484
 1926073387 101484
 1926073387 101484
-2425386270 41
+281226646 481
diff --git a/test/pamdepth-roundtrip.test b/test/pamdepth-roundtrip.test
index 0c7cb8b0..d3f0d6d3 100755
--- a/test/pamdepth-roundtrip.test
+++ b/test/pamdepth-roundtrip.test
@@ -9,5 +9,5 @@ pamdepth $i testimg.ppm | \
   pamdepth 255 | cksum
 done
 
-pamdepth 255 testgrid.pbm | pamdepth 1 | \
+pamdepth 255 maze.pbm | pamdepth 1 | \
   pgmtopbm -th -val=0.5 | cksum
diff --git a/test/pamdepth.test b/test/pamdepth.test
index eaa1b165..86f6c952 100755
--- a/test/pamdepth.test
+++ b/test/pamdepth.test
@@ -32,9 +32,9 @@ echo "-----------------------------------------------------------" 1>&2
 pamdepth 0 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamdepth 65536 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamdice-roundtrip.ok b/test/pamdice-roundtrip.ok
index 4a850615..9f648fd0 100644
--- a/test/pamdice-roundtrip.ok
+++ b/test/pamdice-roundtrip.ok
@@ -1,18 +1,16 @@
-test 1
-2425386270 41
-test 2
-2425386270 41
-test 3
-2425386270 41
-test 4.
+Test 1.  Should print 281226646 481
+281226646 481
+Test 2.  Should print 281226646 481
+281226646 481
+Test 3.  Should print 281226646 481
+281226646 481
+Test 4.  Should print 281226646 481
+281226646 481
+Test 5.  Should print 1926073387 101484 five times
 1926073387 101484
-test 4.-hoverlap=0
 1926073387 101484
-test 4.-hoverlap=2
 1926073387 101484
-test 4.-voverlap=0
 1926073387 101484
-test 4.-voverlap=2
 1926073387 101484
-test 5
+Test 6.  Should print 1926073387 101484
 1926073387 101484
diff --git a/test/pamdice-roundtrip.test b/test/pamdice-roundtrip.test
index c5ccde66..741615f5 100755
--- a/test/pamdice-roundtrip.test
+++ b/test/pamdice-roundtrip.test
@@ -1,38 +1,60 @@
 #! /bin/bash
 # This script tests: pamdice pamundice
-# Also requires:
-
+# Also requires: pamfile
 
 tmpdir=${tmpdir:-/tmp}
 fname_stem=${tmpdir}/pamdice_part
 
-echo test 1
+mazesize=$(pamfile -size maze.pbm)
+mw=$(echo ${mazesize} | cut -d" " -f1)
+mh=$(echo ${mazesize} | cut -d" " -f2)
+
+echo "Test 1.  Should print 281226646 481"
 
-pamdice testgrid.pbm -outstem=${fname_stem}
+pamdice maze.pbm -outstem=${fname_stem}
 pamundice ${fname_stem}_%1d_%1a.pbm -down=1 -across=1 | cksum
 rm ${fname_stem}_0_0.pbm
 
-echo test 2
+echo "Test 2.  Should print 281226646 481"
+
+width=10
+tiles=$(( (${mw}+${width}-1) / ${width} ))
+
+
+pamdice maze.pbm -outstem=${fname_stem} -width=${width}
+pamundice ${fname_stem}_%1d_%1a.pbm -across=${tiles} | cksum
+rm ${fname_stem}_*_*.pbm
+
+echo "Test 3.  Should print 281226646 481"
 
-pamdice testgrid.pbm -outstem=${fname_stem} -width=3
-pamundice ${fname_stem}_%1d_%1a.pbm -across=5 | cksum
-rm ${fname_stem}_0_?.pbm
+width=5
+tiles=$(( (${mw}+${width}-1) / ${width} ))
 
-echo test 3
+pamdice maze.pbm -outstem=${fname_stem} -width=${width}
+pamundice ${fname_stem}_%1d_%2a.pbm -across=${tiles} | cksum
+rm ${fname_stem}_*_*.pbm
 
-pamdice testgrid.pbm -outstem=${fname_stem} -height=4
-pamundice ${fname_stem}_%1d_%1a.pbm -down=4 | cksum
-rm ${fname_stem}_?_0.pbm
 
-for flags in "" "-hoverlap=0" "-hoverlap=2" "-voverlap=0" "-voverlap=2"
+echo "Test 4.  Should print 281226646 481"
+
+height=10
+tiles=$(( (${mh}+${height}-1) / ${height} ))
+
+pamdice maze.pbm -outstem=${fname_stem} -height=${height}
+pamundice ${fname_stem}_%1d_%1a.pbm -down=${tiles} | cksum
+rm ${fname_stem}_*_*.pbm
+
+echo "Test 5.  Should print 1926073387 101484 five times"
+
+for option in "" "-hoverlap=0" "-hoverlap=2" "-voverlap=0" "-voverlap=2"
   do
-  echo test 4.${flags}
   pamdice testimg.ppm -outstem=${fname_stem} -width=50 -height=40
   pamundice ${fname_stem}_%1d_%1a.ppm -down=4 -across=5 | cksum
   rm ${fname_stem}_?_?.ppm
   done
 
-echo test 5
+echo "Test 6.  Should print 1926073387 101484"
+
 pamdice testimg.ppm -outstem=${fname_stem} -width=20 -height=10
 ls ${fname_stem}*.ppm | pamundice -listfile=- -across=12 -down=15 | cksum
 rm ${fname_stem}_??_??.ppm
\ No newline at end of file
diff --git a/test/pamditherbw.test b/test/pamditherbw.test
index 378d7e94..43d2b00f 100755
--- a/test/pamditherbw.test
+++ b/test/pamditherbw.test
@@ -49,61 +49,61 @@ echo "-----------------------------------------------------------" 1>&2
 pamditherbw -fs -atkinson       ${test_red} > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -floyd -atkinson    ${test_red} > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -dither8  -cluster3 ${test_red} > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -cluster3 -cluster4 ${test_red} > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -cluster3 -cluster8 ${test_red} > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -cluster4 -cluster8 ${test_red} > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -hilbert -threshold ${test_red} > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -clump=8            ${test_red} > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -fs -clump=8        ${test_red} > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -hilbert -clump=1   ${test_red} > ${test_out} || \
   echo -n "Expected failure 10"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -th -value=-1       ${test_red} > ${test_out} || \
   echo -n "Expected failure 11"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamditherbw -th -value=1.1      ${test_red} > ${test_out} || \
   echo -n "Expected failure 12"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 rm ${test_red}
diff --git a/test/pamenlarge-pamscale-point.test b/test/pamenlarge-pamscale-point.test
index 08b34bb7..f0f0948b 100755
--- a/test/pamenlarge-pamscale-point.test
+++ b/test/pamenlarge-pamscale-point.test
@@ -29,8 +29,8 @@ enlarge_pbm=${tmpdir}/enlarge.pbm
 for option in "6 -nomix" "15 -nomix -linear" "24 -nomix"
   do
   scale=${option%% *}
-  pamenlarge $scale testgrid.pbm > ${enlarge_pbm}
-  pamscale   $option testgrid.pbm | cmp -s - ${enlarge_pbm}  
+  pamenlarge $scale maze.pbm > ${enlarge_pbm}
+  pamscale   $option maze.pbm | cmp -s - ${enlarge_pbm}  
   echo $option ${PIPESTATUS[@]} ":" $?
   rm  ${enlarge_pbm}
   done
diff --git a/test/pamenlarge-pbm.test b/test/pamenlarge-pbm.test
index 10c00ba0..87493516 100755
--- a/test/pamenlarge-pbm.test
+++ b/test/pamenlarge-pbm.test
@@ -58,4 +58,4 @@ for width in `seq 16`
       #
     done
 
-rm ${test3_pbm}.[1-9]  ${test3_pbm}.1[1-6]
\ No newline at end of file
+rm ${test3_pbm}.[1-9]  ${test3_pbm}.1[0-6]
\ No newline at end of file
diff --git a/test/pamenlarge.ok b/test/pamenlarge.ok
index a2408871..9107e870 100644
--- a/test/pamenlarge.ok
+++ b/test/pamenlarge.ok
@@ -1,4 +1,10 @@
+Test 1.  Should print 3424505894 913236
 3424505894 913236
+Test 2.  Should print 3763267672 304422
 3763267672 304422
-3342398172 297
+Test 3.  Should print 3748791794 3905
+3748791794 3905
+Test 4.  Should print 1618994486 1194
+1618994486 1194
+Test 5.  Should print 398497872 6806
 398497872 6806
diff --git a/test/pamenlarge.test b/test/pamenlarge.test
index 3820f47e..851362e8 100755
--- a/test/pamenlarge.test
+++ b/test/pamenlarge.test
@@ -2,13 +2,14 @@
 # This script tests: pamenlarge
 # Also requires: pamchannel pamseq pamtopnm
 
-
-# Test 1.  Should print 3424505894 913236
+echo "Test 1.  Should print 3424505894 913236"
 pamenlarge 3 testimg.ppm | cksum
-# Test 2.  Should print 3763267672 304422
+echo "Test 2.  Should print 3763267672 304422"
 pamchannel -infile=testimg.ppm -tupletype="GRAYSCALE" 0 | pamtopnm | \
   pamenlarge 3 | cksum
-# Test 3.  Should print 3342398172 297
-pamenlarge 3 testgrid.pbm | cksum
-# Test 4.  Should print 398497872 6806
+echo "Test 3.  Should print 3748791794 3905"
+pamenlarge 3 maze.pbm | cksum
+echo "Test 4.  Should print 1618994486 1194"
+pamenlarge -xscale=21 -yscale=2 testgrid.pbm | cksum
+echo "Test 5.  Should print 398497872 6806"
 pamseq 3 4 | pamtopnm -assume | pamenlarge 3 -plain | cksum
diff --git a/test/pamfile.test b/test/pamfile.test
index 16d084ea..41aa3c85 100755
--- a/test/pamfile.test
+++ b/test/pamfile.test
@@ -33,14 +33,14 @@ echo "-----------------------------------------------------------" 1>&2
 pamfile -size -machine  testimg.ppm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamfile -count -machine testimg.ppm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 head -n1 testimg.ppm | pamfile > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamfind.test b/test/pamfind.test
index 7b612f78..e689754f 100755
--- a/test/pamfind.test
+++ b/test/pamfind.test
@@ -46,19 +46,19 @@ echo "Test Invalid"
 pamfind -color=black -target=1,1,1 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamfind -target=0,0 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamfind -target=0,0,0,0 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamfind testimg.ppm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamfix.test b/test/pamfix.test
index d13e0230..1293dfe2 100755
--- a/test/pamfix.test
+++ b/test/pamfix.test
@@ -1,6 +1,8 @@
 #! /bin/bash
 # This script tests: pamfix
-# Also requires:
+# Also requires: pbmmake
+
+tmpdir=${tmpdir:-/tmp}
 
 # Test 1.
 echo "Test 1"
diff --git a/test/pamflip-roundtrip.ok b/test/pamflip-roundtrip.ok
index 653ab007..48d5ab62 100644
--- a/test/pamflip-roundtrip.ok
+++ b/test/pamflip-roundtrip.ok
@@ -1,12 +1,59 @@
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-1926073387 101484
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
+Test 1.  Should produce 1988581932 2989 fifteen times
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+1988581932 2989
+Test 2.  Should produce 2729474106 27 nine times
+2729474106 27
+2729474106 27
+2729474106 27
+2729474106 27
+2729474106 27
+2729474106 27
+2729474106 27
+2729474106 27
+2729474106 27
+Test 3.  Should produce 1849343241 27 nine times
+1849343241 27
+1849343241 27
+1849343241 27
+1849343241 27
+1849343241 27
+1849343241 27
+1849343241 27
+1849343241 27
+1849343241 27
+Test 4.  Should print a single pixel PPM image maxval 65535 in 
+plain (ascii) format; then produce 2434897823 19 nine times
+P3
+1 1
+65535
+1 10000 65535 
+2434897823 19
+2434897823 19
+2434897823 19
+2434897823 19
+2434897823 19
+2434897823 19
+2434897823 19
+2434897823 19
+2434897823 19
+Test 4.  Should produce 281226646 481 seven times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
diff --git a/test/pamflip-roundtrip.test b/test/pamflip-roundtrip.test
index c5a34ad9..27a3c1f6 100755
--- a/test/pamflip-roundtrip.test
+++ b/test/pamflip-roundtrip.test
@@ -1,35 +1,108 @@
 #! /bin/bash
 # This script tests: pamflip
-# Also requires:
+# Also requires: ppmpat pamseq pamtopnm
 
+tmpdir=${tmpdir:-/tmp}
+tartan_ppm=${tmpdir}/test.ppm
+test1_pgm=${tmpdir}/test1.pgm
+test2_pgm=${tmpdir}/test2.pgm
+dot_ppm=${tmpdir}/dot.ppm
 
-pamflip -lr testimg.ppm | pamflip -lr | cksum
-pamflip -tb testimg.ppm | pamflip -tb | cksum
-pamflip -r180 testimg.ppm | \
-  pamflip -r180 | cksum
-pamflip -xy testimg.ppm | pamflip -xy | cksum
-pamflip -r90 testimg.ppm | \
-  pamflip -r90 | \
-  pamflip -r90 | \
-  pamflip -r90 | cksum
-pamflip -r270 testimg.ppm | \
-  pamflip -r270 | \
-  pamflip -r270 | \
-  pamflip -r270 | cksum
-
-pamflip -lr testgrid.pbm | \
-  pamflip -lr | cksum
-pamflip -tb testgrid.pbm | \
-  pamflip -tb | cksum
-pamflip -r180 testgrid.pbm | \
+echo "Test 1.  Should produce 1988581932 2989 fifteen times"
+
+ppmpat -tartan -mesh \
+       -color=rgb:0/0/0,rgb:ff/00/ff,rgb:80/90/20 31 32 > ${tartan_ppm}
+
+pamflip -null ${tartan_ppm} | cksum
+pamflip -lr ${tartan_ppm} | pamflip -lr | cksum
+pamflip -tb ${tartan_ppm} | pamflip -tb | cksum
+pamflip -r180 ${tartan_ppm} | \
   pamflip -r180 | cksum
-pamflip -xy testgrid.pbm | \
-  pamflip -xy | cksum
-pamflip -r90 testgrid.pbm | \
-  pamflip -r90 | \
-  pamflip -r90 | \
-  pamflip -r90 | cksum
-pamflip -r270 testgrid.pbm | \
-  pamflip -r270 | \
-  pamflip -r270 | \
-  pamflip -r270 | cksum
+pamflip -xy ${tartan_ppm} | pamflip -xy | cksum
+pamflip -r90 ${tartan_ppm} | pamflip -r90 | \
+  pamflip -r90 | pamflip -r90 | cksum
+pamflip -r270 ${tartan_ppm} | pamflip -r270 | \
+  pamflip -r270 | pamflip -r270 | cksum
+pamflip -xform=leftright ${tartan_ppm} | pamflip -leftright | cksum
+pamflip -xform=topbottom ${tartan_ppm} | pamflip -topbottom | cksum
+pamflip -xform=transpose ${tartan_ppm} | pamflip -transpose | cksum
+pamflip -xform=leftright,leftright ${tartan_ppm} | cksum
+pamflip -xform=topbottom,topbottom ${tartan_ppm} | cksum
+pamflip -xform=transpose,transpose ${tartan_ppm} | cksum
+pamflip -tb ${tartan_ppm} | pamflip -lr | \
+  pamflip -xform=leftright,topbottom | cksum
+pamflip -tb ${tartan_ppm} | pamflip -lr | pamflip -xy | \
+  pamflip -xform=leftright,topbottom,transpose | cksum
+
+rm ${tartan_ppm}
+
+pamseq 1 15 | pamtopnm -assume > ${test1_pgm}
+
+echo "Test 2.  Should produce 2729474106 27 nine times"
+
+pamflip -null ${test1_pgm} | cksum
+pamflip -lr ${test1_pgm} | pamflip -lr | cksum
+pamflip -tb ${test1_pgm} | pamflip -tb | cksum
+pamflip -r180 ${test1_pgm} | pamflip -r180 | cksum
+pamflip -xy ${test1_pgm} | pamflip -xy | cksum
+pamflip -r90 ${test1_pgm} | pamflip -r270 | cksum
+pamflip -r270 ${test1_pgm} | pamflip -r90 | cksum
+pamflip -r90 ${test1_pgm} | pamflip -r90 | \
+  pamflip -r90 | pamflip -r90 | cksum
+pamflip -r270 ${test1_pgm} | pamflip -r270 | \
+  pamflip -r270 | pamflip -r270 | cksum
+
+pamflip -r180 ${test1_pgm} > ${test2_pgm}
+rm ${test1_pgm}
+
+echo "Test 3.  Should produce 1849343241 27 nine times"
+
+pamflip -null ${test2_pgm} | cksum
+pamflip -lr ${test2_pgm} | pamflip -lr | cksum
+pamflip -tb ${test2_pgm} | pamflip -tb | cksum
+pamflip -r180 ${test2_pgm} | pamflip -r180 | cksum
+pamflip -xy ${test2_pgm} | pamflip -xy | cksum
+pamflip -r90 ${test2_pgm} | pamflip -r270 | cksum
+pamflip -r270 ${test2_pgm} | pamflip -r90 | cksum
+pamflip -r90 ${test2_pgm} | pamflip -r90 | \
+    pamflip -r90 | pamflip -r90 | cksum
+pamflip -r270 ${test2_pgm} | pamflip -r270 | \
+    pamflip -r270 | pamflip -r270 | cksum
+
+rm ${test2_pgm}
+
+cat > ${dot_ppm} <<EOF
+P3
+1 1
+65535
+1 10000 65535
+EOF
+
+echo "Test 4.  Should print a single pixel PPM image maxval 65535 in "
+echo "plain (ascii) format; then produce 2434897823 19 nine times"
+
+pamflip -null -plain ${dot_ppm}
+pamflip -null ${dot_ppm} | cksum
+pamflip -lr ${dot_ppm} | pamflip -lr | cksum
+pamflip -tb ${dot_ppm} | pamflip -tb | cksum
+pamflip -r180 ${dot_ppm} | pamflip -r180 | cksum
+pamflip -xy ${dot_ppm} | pamflip -xy | cksum
+pamflip -r90 ${dot_ppm} | pamflip -r270 | cksum
+pamflip -r270 ${dot_ppm} | pamflip -r90 | cksum
+pamflip -r90 ${dot_ppm} | pamflip -r90 | pamflip -r90 | pamflip -r90 | cksum
+pamflip -r270 ${dot_ppm} | pamflip -r270 | \
+    pamflip -r270 | pamflip -r270 | cksum
+
+rm ${dot_ppm}
+
+echo "Test 4.  Should produce 281226646 481 seven times"
+
+pamflip -null maze.pbm | cksum
+pamflip -lr maze.pbm | pamflip -lr | cksum
+pamflip -tb maze.pbm | pamflip -tb | cksum
+pamflip -r180 maze.pbm | pamflip -r180 | cksum
+pamflip -xy maze.pbm | pamflip -xy | cksum
+pamflip -r90 maze.pbm | pamflip -r90 | pamflip -r90 | pamflip -r90 | cksum
+pamflip -r270 maze.pbm | pamflip -r270 | pamflip -r270 | pamflip -r270 | cksum
+
+# TODO: PBM various lengths: samples with -lr
diff --git a/test/pamflip1.ok b/test/pamflip1.ok
index 64e0407e..1169d773 100644
--- a/test/pamflip1.ok
+++ b/test/pamflip1.ok
@@ -1,5 +1,10 @@
-2116496681 101484
+Test 1.  Should print 1350702313 12336
+1350702313 12336
+Test 2.  Should print 1035759697 40048
+1035759697 40048
+Test 3.  Should print 217037000 101484
 217037000 101484
-2052917888 101484
-3375384165 41
-604323149 41
+Test 4.  Should print 1178735266 481
+1178735266 481
+Test 5.  Should print 2339906255 481
+2339906255 481
diff --git a/test/pamflip1.test b/test/pamflip1.test
index 840cac73..b30bf6b0 100755
--- a/test/pamflip1.test
+++ b/test/pamflip1.test
@@ -1,15 +1,19 @@
 #! /bin/bash
 # This script tests: pamflip
-# Also requires:
+# Also requires: pamseq
 
+echo "Test 1.  Should print 1350702313 12336"
+pamseq 3 15 | pamflip -lr | cksum
 
-# Test 1.  Should print 2116496681 101484
-pamflip -lr testimg.ppm | cksum
-# Test 2.  Should print 217037000 101484
+echo "Test 2.  Should print 1035759697 40048"
+pamseq 4 9 | pamflip -tb | cksum
+
+echo "Test 3.  Should print 217037000 101484"
 pamflip -cw testimg.ppm | cksum
-# Test 3.  Should print 2052917888 101484
-pamflip -tb testimg.ppm | cksum
-# Test 4.  Should print 3375384165 41
-pamflip -lr testgrid.pbm | cksum
-# Test 5.  Should print 604323149 41
-pamflip -tb testgrid.pbm | cksum
+
+echo "Test 4.  Should print 1178735266 481"
+pamflip -lr maze.pbm | cksum
+
+echo "Test 5.  Should print 2339906255 481"
+pamflip -tb maze.pbm | cksum
+
diff --git a/test/pamflip2.ok b/test/pamflip2.ok
index d2ea501e..e3b4185c 100644
--- a/test/pamflip2.ok
+++ b/test/pamflip2.ok
@@ -1,3 +1,6 @@
-490797850 37
-3727293411 37
-3511745816 37
+Test 1.  Should print 748061765 465
+748061765 465
+Test 2.  Should print 3217647947 465
+3217647947 465
+Test 2.  Should print 1086015439 465
+1086015439 465
diff --git a/test/pamflip2.test b/test/pamflip2.test
index 4dddb770..8df293b5 100755
--- a/test/pamflip2.test
+++ b/test/pamflip2.test
@@ -9,9 +9,11 @@
 ## optional SSE routine.  If you make a wrong choice during configure,
 ## this test will fail.
 
-# Test 1.  Should print 490797850 37
-pamflip -cw testgrid.pbm | cksum
-# Test 1.  Should print 3727293411 37
-pamflip -ccw testgrid.pbm | cksum
-# Test 2.  Should print 3511745816 37
-pamflip -xy testgrid.pbm | cksum
+echo "Test 1.  Should print 748061765 465"
+pamflip -cw maze.pbm | cksum
+
+echo "Test 2.  Should print 3217647947 465"
+pamflip -ccw maze.pbm | cksum
+
+echo "Test 2.  Should print 1086015439 465"
+pamflip -xy maze.pbm | cksum
diff --git a/test/pamfunc.ok b/test/pamfunc.ok
index 4a531577..d358355b 100644
--- a/test/pamfunc.ok
+++ b/test/pamfunc.ok
@@ -36,13 +36,13 @@ Test 4: Should print 1926073387 101484 eight times
 1926073387 101484
 1926073387 101484
 1926073387 101484
-Test 5: Should print 2425386270 41 six times
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
+Test 5: Should print 281226646 481 six times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
 Test Invalid
 Expected failure 1 1
 Expected failure 2 1
diff --git a/test/pamfunc.test b/test/pamfunc.test
index e52584b4..fbb39164 100755
--- a/test/pamfunc.test
+++ b/test/pamfunc.test
@@ -73,16 +73,16 @@ pamfunc -shiftleft  0 testimg.ppm | cksum
 pamfunc -shiftright 0 testimg.ppm | cksum
 
 
-echo "Test 5: Should print 2425386270 41 six times"
+echo "Test 5: Should print 281226646 481 six times"
 
-cat testgrid.pbm | cksum
+cat maze.pbm | cksum
 
-pamfunc -andmask  0x1 testgrid.pbm | cksum
-pamfunc -ormask   0x0 testgrid.pbm | cksum
-pamfunc -xormask  0x1 testgrid.pbm | pamfunc -xormask 0x1 | cksum
+pamfunc -andmask  0x1 maze.pbm | cksum
+pamfunc -ormask   0x0 maze.pbm | cksum
+pamfunc -xormask  0x1 maze.pbm | pamfunc -xormask 0x1 | cksum
 
-pamfunc -shiftleft  0 testgrid.pbm | cksum
-pamfunc -shiftright 0 testgrid.pbm | cksum
+pamfunc -shiftleft  0 maze.pbm | cksum
+pamfunc -shiftright 0 maze.pbm | cksum
 
 echo "Invalid command-line argument combinations." 1>&2
 echo "Error messages should appear below the line." 1>&2
diff --git a/test/pamgauss.test b/test/pamgauss.test
index 7952c7df..87e172a8 100755
--- a/test/pamgauss.test
+++ b/test/pamgauss.test
@@ -30,24 +30,24 @@ test_out=${tmpdir}/test_out
 pamgauss 3 3               > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamgauss 3 3   -sigma=0    > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamgauss 3 3   -sigma=-1.5 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamgauss 3     -sigma=0.5  > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamgauss 3 3 3 -sigma=0.5  > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamhue.test b/test/pamhue.test
index 47688175..622fdf2f 100755
--- a/test/pamhue.test
+++ b/test/pamhue.test
@@ -16,11 +16,11 @@ pamseq -tupletype=RGB 3 1 | pamdepth 255 | pamhue -huechange=60 | \
 
 echo "Test 3"
 # pamhue has no effect on monotone images
-# Should print 2425386270 41 twice
+# Should print 281226646 481 twice
 
-pamhue -huechange=45  testgrid.pbm | cmp -s - testgrid.pbm
+pamhue -huechange=45  maze.pbm | cmp -s - maze.pbm
   echo ${PIPESTATUS[@]} ":" $?
-pamhue -huechange=180 testgrid.pbm | cmp -s - testgrid.pbm
+pamhue -huechange=180 maze.pbm | cmp -s - maze.pbm
   echo ${PIPESTATUS[@]} ":" $?
 
 echo "Test 4"
@@ -45,4 +45,4 @@ echo "-----------------------------------------------------------" 1>&2
 pamhue testimg.ppm  > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamscale-reportonly.test b/test/pamscale-reportonly.test
index 9aacab56..a5d2cdcc 100755
--- a/test/pamscale-reportonly.test
+++ b/test/pamscale-reportonly.test
@@ -33,34 +33,34 @@ pamscale -reportonly -xsize=640 -ysize=400 -xscale=2 testimg.ppm > \
   ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamscale -reportonly -xsize=640 -xscale=2 -yscale=3 testimg.ppm > \
   ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamscale -reportonly -xsize=640 -ysize=400 -pixels=200000 testimg.ppm \
   > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamscale -reportonly -xsize=640 -ysize=400 -xysize 640 400 testimg.ppm \
   > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamscale -reportonly -xsize=640 -ysize=400 -xyfit  640 400 testimg.ppm \
   > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamscale -reportonly -xsize=640 -ysize=400 -xyfill 640 400 testimg.ppm \
   > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamseq.test b/test/pamseq.test
index 9c089cf3..a98a6545 100755
--- a/test/pamseq.test
+++ b/test/pamseq.test
@@ -19,17 +19,17 @@ test_out=${tmpdir}/test_out
 pamseq 1 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamseq 0 255 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamseq 3 0   > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 c64="0123456789012345678901234567890123456789012345678901234567890123"
 c256=${c64}${c64}${c64}${c64}
@@ -39,4 +39,4 @@ c256=${c64}${c64}${c64}${c64}
 pamseq -tupletype="${c256}" 3 15 > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamslice-roundtrip.ok b/test/pamslice-roundtrip.ok
index eae64745..50e83b2a 100644
--- a/test/pamslice-roundtrip.ok
+++ b/test/pamslice-roundtrip.ok
@@ -1,4 +1,9 @@
-2425386270 41
-914327477 4864
-914327477 4864
-914327477 4864
+Test 1.  Should print 139976034 137 twice
+139976034 137
+139976034 137
+Test 2.  Should print 1624460505 574 three times
+1624460505 574
+1624460505 574
+1624460505 574
+Test 3.  Should print 1624460505 574
+1624460505 574
diff --git a/test/pamslice-roundtrip.test b/test/pamslice-roundtrip.test
index 6cb5533f..d1a8a72c 100755
--- a/test/pamslice-roundtrip.test
+++ b/test/pamslice-roundtrip.test
@@ -1,75 +1,77 @@
 #! /bin/bash
 # This script tests: pamslice pamdeinterlace
-# Also requires: pamcut pamtopnm pamflip
+# Also requires: pamfile pamcut pamtopnm pamflip
 
-
-# Test 1.
-# Slice rows, one by one, out of testgrid.pbm.
+echo "Test 1.  Should print 139976034 137 twice"
+# Slice rows, one by one, out of maze.pbm.
 # Add header and reconstruct pbm image.
 # Note that in pamslice output 0 is white and 1 is black: opposite of PBM
-# Should print 2425386270 41
+
+mwidth=$(pamfile -size maze.pbm | cut -d" " -f1)
+height=16
+
+pamcut -top=0 -left=0 -height=${height} maze.pbm | cksum
 
 (echo "P1"
- echo "14 16"
- seq 0 15 | while read i;
+ echo "${mwidth} ${height}"
+ for ((i = 0; i < ${height}; ++i))
      do
-     pamslice -row=$i testgrid.pbm
-     done | awk '{print $2}' | sed 'y/01/10/' ) \
+     pamslice -row=$i maze.pbm
+     done | cut -d" " -f2- | sed 'y/01/10/' ) \
  | pamtopnm | cksum
 
-# Test 2.
+
+echo "Test 2.  Should print 1624460505 574 three times"
 # Slice rows, one by one, out of ppm test image
 # We take a part out of testimg.ppm with pamcut for processing the
 # whole image takes much time.
 # Add header and reconstruct ppm image.
-# Should print 914327477 4864
 
-tmpdir=${tmpdir:-/tmp}
 
-test4933_ppm=${tmpdir}/test4933.ppm
+tmpdir=${tmpdir:-/tmp}
+test1711_ppm=${tmpdir}/test1711.ppm
 
-pamcut 50 50 49 33 testimg.ppm > ${test4933_ppm}
+pamcut -left=50 -top=50 -width=17 -height=11 testimg.ppm | \
+  tee ${test1711_ppm} | cksum
 
 (echo "P3"
- echo "49 33"
+ echo "17 11"
  echo "255"
- seq 0 32 | while read i;
+ for ((i = 0; i < 11; ++i))
      do
-     pamslice -row=$i ${test4933_ppm}
-     done | awk '{print $2, $3, $4}' ) \
+     pamslice -row=$i ${test1711_ppm}
+     done | cut -d" " -f2- ) \
  | pamtopnm | cksum
 
 # Same as above test 2, but take cols instead of rows.
 # Should print 914327477 4864
 
 (echo "P3"
- echo "33 49"
+ echo "11 17"
  echo "255"
- seq 0 48 | while read i;
+ for ((i = 0; i < 17; ++i))
      do
-     pamslice -col=$i ${test4933_ppm}
-     done | awk '{print $2, $3, $4}' ) \
- | pamflip -xy | cksum
+     pamslice -col=$i ${test1711_ppm}
+     done | cut -d " " -f2- ) | pamflip -xy | cksum
 
-# Test 4.
+echo "Test 3.  Should print 1624460505 574"
 # Divide input image into two with pamdeinterlace and recombine.
 
 testeven_ppm=${tmpdir}/testeven.ppm
 testodd_ppm=${tmpdir}/testodd.ppm
 
-pamdeinterlace -takeodd ${test4933_ppm} > ${testodd_ppm}
-pamdeinterlace -takeeven ${test4933_ppm} > ${testeven_ppm}
+pamdeinterlace -takeodd ${test1711_ppm} > ${testodd_ppm}
+pamdeinterlace -takeeven ${test1711_ppm} > ${testeven_ppm}
 
-(echo "P3"
- echo "49 33"
- echo "255"
- ( seq 0 15 | while read i;
-     do
-     pamslice -row=$i ${testeven_ppm}
-     pamslice -row=$i ${testodd_ppm}
-     done
-     pamslice -row=16 ${testeven_ppm};
- )  | awk '{print $2, $3, $4}' ) \
- | pamtopnm | cksum
+( echo "P3"
+  echo "17 11"
+  echo "255"
+  ( for ((i = 0; i < 5; ++i))
+        do
+        pamslice -row=$i ${testeven_ppm}
+        pamslice -row=$i ${testodd_ppm}
+        done
+        pamslice -row=5 ${testeven_ppm};
+  ) | cut -d" " -f2- ) | pamtopnm | cksum
 
-rm ${test4933_ppm} ${testodd_ppm} ${testeven_ppm}
+rm ${test1711_ppm} ${testodd_ppm} ${testeven_ppm}
diff --git a/test/pamstretch.test b/test/pamstretch.test
index 55dd4e9c..8cbc555a 100755
--- a/test/pamstretch.test
+++ b/test/pamstretch.test
@@ -13,6 +13,7 @@ pamstretch -xscale=2 -blackedge testimg.ppm | cksum
 
 # Test 3.  Should print 3427416462 301047
 echo test 3
+tmpdir=${tmpdir:-/tmp}
 dropedge1_ppm=${tmpdir}/drop1.ppm
 pamstretch -yscale=3 -dropedge testimg.ppm | tee ${dropedge1_ppm} | cksum
 
@@ -39,10 +40,10 @@ blackedge_ppm=${tmpdir}/black.ppm
 pamstretch 3 testimg.ppm | tee ${stretch_ppm} | cksum
 pamstretch 3 -blackedge testimg.ppm | tee ${blackedge_ppm} | cksum
 pamstretch -xscale=3 -dropedge ${dropedge1_ppm} | tee ${dropedge_ppm} | cksum
-width_height=`pamfile -size ${dropedge_ppm} | \
-              awk '{print "-width="$1, "-height="$2}'`
+width_height=$(pamfile -size ${dropedge_ppm} | \
+              awk '{print "-width="$1, "-height="$2}')
 pamcut -left=0 -top=0 ${width_height} ${stretch_ppm} | cksum
 pamcut -left=0 -top=0 ${width_height} ${blackedge_ppm} | cksum
 pamcut -left=0 -top=0 ${width_height} ${dropedge_ppm} | cksum
 
-rm ${stretch_ppm} ${dropedge_ppm} ${dropedge1_ppm} ${blackedge_ppm}
\ No newline at end of file
+rm ${stretch_ppm} ${dropedge_ppm} ${dropedge1_ppm} ${blackedge_ppm}
diff --git a/test/pamsumm.ok b/test/pamsumm.ok
index 63b5f6bd..b9ce0269 100644
--- a/test/pamsumm.ok
+++ b/test/pamsumm.ok
@@ -1,9 +1,14 @@
-Test 1
+Test 1.  Should print in order: 56, 0, 1, 0.250000
 56
 0
 1
 0.250000
-Test 2
+Test 2.  Should print in order: 1627, 0, 1, 0.483794
+1627
+0
+1
+0.483794
+Test 3.  Should print in order: 10772432, 15, 255, 106.164760
 10772432
 15
 255
diff --git a/test/pamsumm.test b/test/pamsumm.test
index 3d008533..32a9b8c1 100755
--- a/test/pamsumm.test
+++ b/test/pamsumm.test
@@ -2,14 +2,21 @@
 # This script tests: pamsumm
 # Also requires:
 
-echo "Test 1"
+echo "Test 1.  Should print in order: 56, 0, 1, 0.250000"
 
 for type in -sum -min -max -mean
   do
   pamsumm -brief $type testgrid.pbm
   done
 
-echo "Test 2"
+echo "Test 2.  Should print in order: 1627, 0, 1, 0.483794"
+
+for type in -sum -min -max -mean
+  do
+  pamsumm -brief $type maze.pbm
+  done
+
+echo "Test 3.  Should print in order: 10772432, 15, 255, 106.164760"
 
 for type in -sum -min -max -mean
   do
@@ -30,19 +37,19 @@ echo "-----------------------------------------------------------" 1>&2
 pamsumm -sum -min  testimg.ppm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamsumm -sum -max  testimg.ppm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamsumm -mean -max testimg.ppm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pamsumm            testimg.ppm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pamtopam.ok b/test/pamtopam.ok
index dcb597d3..d84b2b6c 100644
--- a/test/pamtopam.ok
+++ b/test/pamtopam.ok
@@ -1,3 +1,18 @@
+Test 1. Should print five PAM headers
+P7
+WIDTH 14
+HEIGHT 16
+DEPTH 1
+MAXVAL 1
+TUPLTYPE BLACKANDWHITE
+ENDHDR
+P7
+WIDTH 1
+HEIGHT 1
+DEPTH 1
+MAXVAL 255
+TUPLTYPE GRAYSCALE
+ENDHDR
 P7
 WIDTH 227
 HEIGHT 149
@@ -6,11 +21,26 @@ MAXVAL 255
 TUPLTYPE RGB
 ENDHDR
 P7
-WIDTH 14
-HEIGHT 16
-DEPTH 1
+WIDTH 64
+HEIGHT 1
+DEPTH 2
+MAXVAL 7
+TUPLTYPE GRAYSCALE_ALPHA
+ENDHDR
+P7
+WIDTH 16
+HEIGHT 1
+DEPTH 4
 MAXVAL 1
-TUPLTYPE BLACKANDWHITE
+TUPLTYPE RGB_ALPHA
 ENDHDR
-1873848880 101532
+Test 2. Should print 2260748250 293
 2260748250 293
+Test 3. Should print 1873848880 101532
+1873848880 101532
+Test 4. Should print 1475247910 3432 twice
+1475247910 3432
+1475247910 3432
+Test 5. Should print 2347274358 101825 twice
+2347274358 101825
+2347274358 101825
diff --git a/test/pamtopam.test b/test/pamtopam.test
index 394d9904..e9b4f93d 100755
--- a/test/pamtopam.test
+++ b/test/pamtopam.test
@@ -1,10 +1,25 @@
 #! /bin/bash
 # This script tests: pamtopam
-# Also requires:
+# Also requires: pgmmake pamseq
 
+echo "Test 1. Should print five PAM headers"
 
-pamtopam < testimg.ppm  | sed '/ENDHDR/q'
 pamtopam < testgrid.pbm | sed '/ENDHDR/q'
+pgmmake 0.5 1 1 | pamtopam | sed '/ENDHDR/q'
+pamtopam < testimg.ppm  | sed '/ENDHDR/q'
+pamseq -tupletype="GRAYSCALE_ALPHA" 2 7 | pamtopam | sed '/ENDHDR/q'
+pamseq -tupletype="RGB_ALPHA" 4 1 | pamtopam | sed '/ENDHDR/q'
 
-pamtopam < testimg.ppm   | cksum
+echo "Test 2. Should print 2260748250 293"
 pamtopam < testgrid.pbm  | cksum
+
+echo "Test 3. Should print 1873848880 101532"
+pamtopam < testimg.ppm   | cksum
+
+echo "Test 4. Should print 1475247910 3432 twice"
+pamtopam < maze.pbm  | cksum
+pamtopam < maze.pbm  | pamtopam | cksum
+
+echo "Test 5. Should print 2347274358 101825 twice"
+cat testgrid.pbm testimg.ppm | pamtopam | cksum
+( pamtopam  < testgrid.pbm; pamtopam < testimg.ppm ) | cksum
diff --git a/test/pamtopdbimg.test b/test/pamtopdbimg.test
index 9afee8f1..d6c105bf 100755
--- a/test/pamtopdbimg.test
+++ b/test/pamtopdbimg.test
@@ -82,13 +82,13 @@ awk 'BEGIN { ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
 head -c 65533 ${text65597} > ${text65533}
 head -c 65534 ${text65597} > ${text65534}
-pamtopdbimg -uncompressed testgrid.pbm | \
+pamtopdbimg -uncompressed maze.pbm | \
   wc -c | tr -d ' '
   echo ${PIPESTATUS[0]}
-pamtopdbimg -uncompressed -notefile=${text65533} testgrid.pbm | \
+pamtopdbimg -uncompressed -notefile=${text65533} maze.pbm | \
   wc -c | tr -d ' '
   echo ${PIPESTATUS[0]}
-pamtopdbimg -uncompressed -notefile=${text65534} testgrid.pbm | \
+pamtopdbimg -uncompressed -notefile=${text65534} maze.pbm | \
   wc -c | tr -d ' '
   echo ${PIPESTATUS[0]}
 
diff --git a/test/pamundice.ok b/test/pamundice.ok
index ef0f6e13..1234431d 100644
--- a/test/pamundice.ok
+++ b/test/pamundice.ok
@@ -1,17 +1,17 @@
-Test 1.
+Test 1.  Should print 2096818803 2818 twice
 2096818803 2818
 2096818803 2818
-Test 2.
+Test 2.  Should print 2096818803 2818 twice
 2096818803 2818
 2096818803 2818
-Test 3.
+Test 3.  Should print 2096818803 2818 twice
 2096818803 2818
 2096818803 2818
-Test 4.
-557392151 459
-557392151 459
-557392151 459
-557392151 459
+Test 4.  Should print 2434390296 4436 four times
+2434390296 4436
+2434390296 4436
+2434390296 4436
+2434390296 4436
 Test Invalid
 Expected failure 1 1
 Expected failure 2 1
diff --git a/test/pamundice.test b/test/pamundice.test
index 5c1f9c6b..1e3dd31b 100755
--- a/test/pamundice.test
+++ b/test/pamundice.test
@@ -1,12 +1,11 @@
 #! /bin/bash
 # This script tests: pamundice
-# Also requires: pgmmake pnmtile pnmcat pnmpad
+# Also requires: pamfile pgmmake pnmtile pnmcat
 
 tmpdir=${tmpdir:-/tmp}
 fname_stem=${tmpdir}/part
 
-# Test 1.
-echo "Test 1."
+echo "Test 1.  Should print 2096818803 2818 twice"
 
 for ((x=0; x<3; ++x))
   do for ((y=0; y<5; ++y))
@@ -20,9 +19,7 @@ pamundice -across=3 -down=5 ${fname_stem}"_%1d_%1a".pgm | cksum
 ls ${fname_stem}_?_?.pgm | \
     pamundice -across=3 -down=5 -listfile=- | cksum
 
-
-# Test 2.
-echo "Test 2."
+echo "Test 2.  Should print 2096818803 2818 twice"
 
 tempfile=${tmpdir}/temp
 
@@ -46,8 +43,7 @@ rm ${tempfile}_[012]
 
 rm ${fname_stem}_?_?.pgm
 
-# Test 3.
-echo "Test 3."
+echo "Test 3.  Should print 2096818803 2818 twice"
 
 for ((x=0; x<2; ++x))
   do for ((y=0; y<5; ++y))
@@ -89,21 +85,22 @@ rm ${fname_stem}_?_?.pgm
 
 
 # Test 4.
-echo "Test 4."
+echo "Test 4.  Should print 2434390296 4436 four times"
 
-# Add margins to testgrid.pbm
+msize=$(pamfile -size maze.pbm)
+mw=$(echo ${msize} | cut -d" " -f1)
+mh=$(echo ${msize} | cut -d" " -f2)
 
-testimg_pbm=${tmpdir}/testimg.pbm
-pnmpad -top=2 -left=3 -black testgrid.pbm > ${testimg_pbm}
+pnmtile $((${mw} * 2)) $((${mh} * 5)) maze.pbm | cksum
 
 for x in `seq 0 1`
   do for y in `seq 0 4`
-    do cp ${testimg_pbm} ${fname_stem}_${y}_${x}.pbm; done
+    do cp maze.pbm ${fname_stem}_${y}_${x}.pbm; done
   done
 
 for (( i=0 ; i<=9; ++i))
   do
-  echo ${testimg_pbm}
+  echo maze.pbm
   done | pamundice -down=5 -across=2 -listfile=- | cksum
 
 pamundice -down=5 -across=2 ${fname_stem}_"%1d"_"%1a".pbm | cksum
@@ -111,11 +108,8 @@ pamundice -down=5 -across=2 ${fname_stem}_"%1d"_"%1a".pbm | cksum
 echo "A warning message should appear below the line." 1>&2
 echo "-----------------------------------------------------------" 1>&2
 
-pamundice -down=5 -across=2 ${testimg_pbm} | cksum
-
-pnmtile 34 90 ${testimg_pbm} | cksum
+pamundice -down=5 -across=2 maze.pbm | cksum
 
-rm ${testimg_pbm}
 
 # Test Invalid.
 echo "Test Invalid"
@@ -131,76 +125,76 @@ echo "-----------------------------------------------------------" 1>&2
 pamundice -down=5 -across=2 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # -down=0
 pamundice -down=0 -across=2 ${fname_stem}_"%1d"_"%1a".pbm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # -across=0
 pamundice -down=5 -across=0 ${fname_stem}_"%1d"_"%1a".pbm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # -down too large
 pamundice -down=6 -across=2 ${fname_stem}_"%1d"_"%1a".pbm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # -across too large
 pamundice -down=5 -across=3 ${fname_stem}_"%1d"_"%1a".pbm > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # precision does not match
 pamundice -down=5 -across=2 ${fname_stem}_"%2d"_"%2a".pbm > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # precision set to zero
 pamundice -down=5 -across=2 ${fname_stem}_"%0d"_"%0a".pbm > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # no precision
 pamundice -down=5 -across=2 ${fname_stem}_"%d"_"%a".pbm > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # -hoverlap too large
-pamundice -down=5 -across=2 -hoverlap=18 \
+pamundice -down=5 -across=2 -hoverlap=$((${mw}+1)) \
   ${fname_stem}_"%1d"_"%1a".pbm > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # -voverlap too large
-pamundice -down=5 -across=2 -voverlap=19 \
+pamundice -down=5 -across=2 -voverlap=$((${mh}+1)) \
   ${fname_stem}_"%1d"_"%1a".pbm > ${test_out} || \
   echo -n "Expected failure 10"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # corrupt listfile : file names do not exist
 seq 10 | sed -e 's/^/::::::/' -e 's/$/::::::/' | \
   pamundice -down=5 -across=2 -listfile=- > ${test_out} || \
   echo -n "Expected failure 11"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # listfile with insufficient lines (insufficient file entries)
 ls ${fname_stem}_*_*.pbm | head -n 9 | \
   pamundice -down=5 -across=2 -listfile=- > ${test_out} || \
   echo -n "Expected failure 12"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 rm ${fname_stem}*.pbm
diff --git a/test/pbm-ppm-roundtrip.ok b/test/pbm-ppm-roundtrip.ok
new file mode 100644
index 00000000..91c502b5
--- /dev/null
+++ b/test/pbm-ppm-roundtrip.ok
@@ -0,0 +1,39 @@
+Test 1.  Should print 281226646 481 five times.
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+Test 2.  Should print three all-white, then three all-black PBM images
+P1
+7 3
+0000000
+0000000
+0000000
+
+P1
+7 3
+0000000
+0000000
+0000000
+
+P1
+7 3
+0000000
+0000000
+0000000
+
+P1
+17 2
+11111111111111111
+11111111111111111
+
+P1
+17 2
+11111111111111111
+11111111111111111
+
+P1
+17 2
+11111111111111111
+11111111111111111
diff --git a/test/pbm-ppm-roundtrip.test b/test/pbm-ppm-roundtrip.test
new file mode 100755
index 00000000..23b6c1c4
--- /dev/null
+++ b/test/pbm-ppm-roundtrip.test
@@ -0,0 +1,34 @@
+#! /bin/bash
+# This script tests: pgmtoppm ppmtopgm ppmtoppm pgmtopbm
+# Also requires: pbmmake ppmcolormask pnminvert
+
+echo "Test 1.  Should print 281226646 481 five times."
+
+cat maze.pbm | cksum
+pgmtoppm black-white maze.pbm | ppmtopgm | \
+  pgmtopbm -th -val=0.5 | cksum
+pgmtoppm white-black maze.pbm | ppmtopgm | \
+  pgmtopbm -th -val=0.5 | pnminvert | cksum
+pgmtoppm white-black maze.pbm | ppmcolormask white | cksum
+ppmtoppm < maze.pbm | ppmtopgm | pgmtopbm -th -val=0.5 | cksum
+
+echo "Test 2.  Should print three all-white, then three all-black PBM images"
+
+pbmmake -w 7 3  -plain
+echo
+
+pbmmake -w 7 3 | pgmtoppm black-white | ppmtopgm | \
+  pgmtopbm -th -val=0.5 -plain
+echo
+pbmmake -w 7 3 | ppmtoppm | ppmtopgm | \
+  pgmtopbm -th -val=0.5 -plain
+echo
+
+pbmmake -b 17 2 -plain
+echo
+
+pbmmake -b 17 2 | pgmtoppm black-white | ppmtopgm | \
+  pgmtopbm -th -val=0.5 -plain
+echo
+pbmmake -b 17 2 | ppmtoppm | ppmtopgm | \
+  pgmtopbm -th -val=0.5 -plain
diff --git a/test/pbmclean.test b/test/pbmclean.test
index 3fb24b25..0e17ba8a 100755
--- a/test/pbmclean.test
+++ b/test/pbmclean.test
@@ -48,6 +48,6 @@ echo "-----------------------------------------------------------" 1>&2
 pbmclean -black -white -min=1 -extended testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # note that without -extended the above is valid.
diff --git a/test/pbmmake.test b/test/pbmmake.test
index 9945394a..eef4e0da 100755
--- a/test/pbmmake.test
+++ b/test/pbmmake.test
@@ -34,39 +34,39 @@ test_out=${tmpdir}/test_out
 pbmmake -b -w -plain 1 1 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmmake -b -g -plain 1 1 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmmake -white -gray -plain 1 1 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmmake -white -plain   > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmmake -white -plain 1 > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmmake -white -plain 1 0 > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmmake -white -plain 0 1 > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmmake -white -plain 1 1 1 > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pbmminkowski.ok b/test/pbmminkowski.ok
index 71a909ce..9780e3c2 100644
--- a/test/pbmminkowski.ok
+++ b/test/pbmminkowski.ok
@@ -21,3 +21,11 @@ vertices:	224
     area:	56
 perimeter:	224
  eulerchi:	56
+
+   tiles:	1627
+ x-edges:	2479
+ y-edges:	2427
+vertices:	3376
+    area:	1627
+perimeter:	3304
+ eulerchi:	97
diff --git a/test/pbmminkowski.test b/test/pbmminkowski.test
index 389ff44a..3ad96dfa 100755
--- a/test/pbmminkowski.test
+++ b/test/pbmminkowski.test
@@ -1,11 +1,12 @@
 #! /bin/bash
 # This script tests: pbmminkowski
-# Also requires: pbmmake pnmmargin pnmpad
+# Also requires: pbmmake pnmpad
 
-
-pbmmake -w 1 1 | pnmmargin -b 1 | \
+pbmmake -w 1 1 | pnmpad -black -top=1 -left=1 -bottom=1 -right=1 | \
   pbmminkowski
 echo
 pbmmake -g 3 3 | pbmminkowski
 echo
 pbmminkowski testgrid.pbm
+echo
+pbmminkowski maze.pbm
\ No newline at end of file
diff --git a/test/pbmnoise-parameters.ok b/test/pbmnoise-parameters.ok
new file mode 100644
index 00000000..c2a61c37
--- /dev/null
+++ b/test/pbmnoise-parameters.ok
@@ -0,0 +1,26 @@
+Test Invalid
+Expected failure 1 1
+Expected failure 2 1
+Expected failure 3 1
+Expected failure 4 1
+Expected failure 5 1
+Expected failure 6 1
+Expected failure 7 1
+Expected failure 8 1
+Expected failure 9 1
+Expected failure 10 1
+Expected failure 11 1
+Expected failure 12 1
+Expected failure 13 1
+Expected failure 14 1
+Expected failure 15 1
+Expected failure 16 1
+Expected failure 17 1
+Expected failure 17 1
+Expected failure 18 1
+Expected failure 19 1
+Expected failure 20 1
+Expected failure 21 1
+Expected failure 22 1
+Expected failure 23 1
+Expected failure 24 1
diff --git a/test/pbmnoise-parameters.test b/test/pbmnoise-parameters.test
new file mode 100755
index 00000000..f3348ae8
--- /dev/null
+++ b/test/pbmnoise-parameters.test
@@ -0,0 +1,143 @@
+#! /bin/bash
+# This script tests: pbmnoise
+# Also requires:
+
+echo "Invalid command-line argument combinations." 1>&2
+echo "Error messages should appear below the line." 1>&2
+echo "-----------------------------------------------------------" 1>&2
+
+echo "Test Invalid"
+
+test_out=${tmpdir}/test_out
+
+# Invalid -ratio arguments
+
+pbmnoise -ratio       100 100 > ${test_out} || \
+  echo -n "Expected failure 1"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio 0     1  100 > ${test_out} || \
+  echo -n "Expected failure 2"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=1.1   100 100 > ${test_out} || \
+  echo -n "Expected failure 3"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+  
+pbmnoise -ratio=-1    100 100 > ${test_out} || \
+  echo -n "Expected failure 4"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=half  100 100 > ${test_out} || \
+  echo -n "Expected failure 5"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=0/1/1 100 100 > ${test_out} || \
+  echo -n "Expected failure 6"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=-1/2  100 100 > ${test_out} || \
+  echo -n "Expected failure 7"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=1/0   100 100 > ${test_out} || \
+  echo -n "Expected failure 8"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=/2    100 100 > ${test_out} || \
+  echo -n "Expected failure 9"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=4/2   100 100 > ${test_out} || \
+  echo -n "Expected failure 10"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=6/    100 100 > ${test_out} || \
+  echo -n "Expected failure 11"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=1.0/2.0 100 100 > ${test_out} || \
+  echo -n "Expected failure 12"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+# denominator must be power of 2  
+pbmnoise -ratio=3/9   100 100 > ${test_out} || \
+  echo -n "Expected failure 13"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=a/2   100 100 > ${test_out} || \
+  echo -n "Expected failure 14"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+
+pbmnoise -ratio=2/a  100 100 > ${test_out} || \
+        echo -n "Expected failure 15"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=1/-2  100 100 > ${test_out} || \
+        echo -n "Expected failure 16"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+# Denominator must be 65536 or less
+pbmnoise -ratio=1/65537 100 100 > ${test_out} || \
+  echo -n "Expected failure 17"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -ratio=1/131072 100 100 > ${test_out} || \
+  echo -n "Expected failure 17"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -endian=large 100 100 > ${test_out} || \
+  echo -n "Expected failure 18"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -randomseed 100 100 > ${test_out} || \
+  echo -n "Expected failure 19"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -randomseed=-1 100 100 > ${test_out} || \
+  echo -n "Expected failure 20"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise -randomseed=0.1 100 100 > ${test_out} || \
+  echo -n "Expected failure 21"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise > ${test_out} || \
+  echo -n "Expected failure 22"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise 100 > ${test_out} || \
+  echo -n "Expected failure 23"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pbmnoise 100 200 300 > ${test_out} || \
+  echo -n "Expected failure 24"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+  
diff --git a/test/pbmnoise1.ok b/test/pbmnoise1.ok
new file mode 100644
index 00000000..84f1f04e
--- /dev/null
+++ b/test/pbmnoise1.ok
@@ -0,0 +1,50 @@
+Test 1.  Should print 242292858 962 five times
+242292858 962
+242292858 962
+242292858 962
+242292858 962
+242292858 962
+Test 2.
+P1 32 1 00000000000000000000000000000000 
+P1 32 1 00010000000001000000000000000000 
+P1 32 1 00010000000001000010000000000000 
+P1 32 1 00010000000001000000000000000000 
+P1 32 1 00010000000001000010010000000000 
+P1 32 1 01010001000101000000000000000000 
+P1 32 1 00010000000001000010100000000000 
+P1 32 1 01010001000111000000000000100010 
+P1 32 1 10011001000001100010010000000000 
+P1 32 1 01010001000111000000010100100010 
+P1 32 1 01011001000101000010100000000000 
+P1 32 1 01010001000111000001010100100110 
+P1 32 1 00010000000001000111110000000100 
+P1 32 1 01010001010111001001010100100110 
+P1 32 1 01011011000111000010101000101010 
+P1 32 1 01010001011111011001010100110110 
+P1 32 1 10011001001001110010011000101110 
+P1 32 1 01010001011111011011010101110110 
+P1 32 1 01011011000111000010111110101010 
+P1 32 1 01010001011111011011110101110110 
+P1 32 1 11011001010101101111110000000100 
+P1 32 1 01011001011111011011110101110110 
+P1 32 1 01011011000111000111111110101110 
+P1 32 1 01011011011111011011111101111110 
+P1 32 1 10111111101011110111111000111111 
+P1 32 1 01011011011111011011111111111110 
+P1 32 1 11011011010111101111111110101110 
+P1 32 1 01011011011111011111111111111110 
+P1 32 1 11111111111111111111111000111111 
+P1 32 1 11011011011111111111111111111110 
+P1 32 1 11111111111111111111111110111111 
+P1 32 1 11111111111111111111111111111111 
+P1 32 1 11111111111111111111111111111111 
+Test 3.  Should print 126142586 1218 four times
+126142586 1218
+126142586 1218
+126142586 1218
+126142586 1218
+Test 4.
+P1 64 1 1000010001101101101000100101001110101011010010011111001011100110 
+P1 64 1 1000010001101101101000100101001110101011010010011111001011100110 
+P1 64 1 0101001110100010011011011000010011100110111100100100100110101011 
+P1 64 1 0101001110100010011011011000010011100110111100100100100110101011 
diff --git a/test/pbmnoise1.test b/test/pbmnoise1.test
new file mode 100755
index 00000000..1b8fc241
--- /dev/null
+++ b/test/pbmnoise1.test
@@ -0,0 +1,36 @@
+#! /bin/bash
+# This script tests: pbmnoise
+#
+
+# Assumes that the random number generator is Mersenne Twister
+
+echo "Test 1.  Should print 242292858 962 five times"
+
+for arg in "" "-ratio=1/2" "-ratio=256/512" "-ratio=32768/65536" "-pack" 
+    do
+    pbmnoise -endian=big -randomseed=1 1088 7 ${arg} | cksum
+done
+
+echo "Test 2."
+
+for ((i=0 ; i<=32; i++))
+  do
+  pbmnoise -endian=little -randomseed=11 -ratio=$i/32 -plain 32 1 | \
+  tr '\n' ' ';  echo
+done
+
+echo "Test 3.  Should print 126142586 1218 four times"
+
+pbmnoise -endian=big -pack -randomseed=12 -plain 19 64 | sed 2d | tr -d '\n' | cksum
+pbmnoise -endian=big -pack -randomseed=12 -plain 152 8 | sed 2d | tr -d '\n' | cksum
+pbmnoise -endian=big -pack -randomseed=12 -plain 64 19 | sed 2d | tr -d '\n' | cksum
+pbmnoise -endian=big       -randomseed=12 -plain 64 19 | sed 2d | tr -d '\n' | cksum
+
+echo "Test 4."
+
+   pbmnoise -endian=big    -randomseed=23 64 1 -plain | tr '\n' ' '; echo
+(  pbmnoise -endian=native -randomseed=23 64 1 -plain | tr '\n' ' '; echo 
+   pbmnoise -endian=swap   -randomseed=23 64 1 -plain | tr '\n' ' '; echo
+)  | sort -nr
+   pbmnoise -endian=little -randomseed=23 64 1 -plain | tr '\n' ' '; echo
+
diff --git a/test/pbmnoise2.ok b/test/pbmnoise2.ok
new file mode 100644
index 00000000..5c3959e0
--- /dev/null
+++ b/test/pbmnoise2.ok
@@ -0,0 +1,19 @@
+Test 1.  Should print OK 1, then OK 2 three times
+OK 1
+OK 2
+OK 2
+OK 2
+Test 2.  Should print OK 1, then OK 2 three times
+OK 1
+OK 2
+OK 2
+OK 2
+Test 3-black.  Should print 255    384    100%    100% four times
+  255    384    100%    100%
+  255    384    100%    100%
+  255    384    100%    100%
+  255    384    100%    100%
+Test 3-white.  Should print 0    320    100%    100% three times
+    0    320    100%    100%
+    0    320    100%    100%
+    0    320    100%    100%
diff --git a/test/pbmnoise2.test b/test/pbmnoise2.test
new file mode 100755
index 00000000..7476d03c
--- /dev/null
+++ b/test/pbmnoise2.test
@@ -0,0 +1,85 @@
+#! /bin/bash
+# This script tests: pbmnoise
+# Also requires: pgmhist
+
+# Expected value and variance of binominal distribution:
+# Pb: probability of getting a black pixel (=ratio)
+# Pw: probability of getting a white pixel (= 1 - Pb = 1 - ratio)
+#
+# E(width, height, Pb, Pw) = width * height * Pb
+# V(width, height, Pb, Pw) = width * height * Pb * Pw
+#
+# When width * height is large and Pb is not too near edges
+# approximate with normal distribution
+
+echo "Test 1.  Should print OK 1, then OK 2 three times"
+
+args1="-endian=big -randomseed=1"
+
+( pbmnoise            ${args1} 320 200 | pgmhist
+  pbmnoise -ratio=1/2 ${args1} 160 400 | pgmhist
+  pbmnoise -ratio=2/4 ${args1} 64000 1 | pgmhist
+  pbmnoise -pack      ${args1} 10 6400 | pgmhist ) | \
+  awk 'BEGIN { pixels=64000; Pb=Pw=0.5;
+               average=pixels*Pb;
+               sd=sqrt(pixels*Pb*Pw);  # standard deviation
+               sl=1.96                 # significance level 
+               limit1 = average - sd * sl;
+               limit2 = average + sd * sl
+               count=-1  # initial value
+              }
+       $1!=0  { next }
+              { v=1  }    # initial value
+       limit1 < $2 && $2 < limit2 { v=0 }
+       v==0   {print "OK", (count==prev)+1}
+       v==1   {print "NG" $0}
+              {prev=count}'
+
+
+
+echo "Test 2.  Should print OK 1, then OK 2 three times"
+
+args2="-endian=big -randomseed=2"
+
+( pbmnoise -ratio=11/32        ${args1} 32  119 | pgmhist
+  pbmnoise -ratio=11/32        ${args1} 224  17 | pgmhist
+  pbmnoise -ratio=22/64        ${args1} 3808  1 | pgmhist
+  pbmnoise -ratio=44/128 -pack ${args1} 1904 2 | pgmhist ) | \
+  awk 'BEGIN {pixels=32*119; Pb=11/32; Pw=1-Pb;
+               average=pixels*Pb;
+               sd=sqrt(pixels*Pb*Pw);  # standard deviation
+               sl=1.96                 # significance level 
+               limit1 = average - sd * sl;
+               limit2 = average + sd * sl
+               count=-1  # initial value
+              }
+       $1!=0  { next }
+              { v=1  }    # initial value
+       limit1 < $2 && $2 < limit2 { v=0 }
+       v==0   {print "OK", (count==prev)+1}
+       v==1   {print "NG" $0}
+              {prev=count}'
+
+
+
+
+echo "Test 3-black.  Should print 255    384    100%    100% four times"
+
+pbmnoise -ratio=0/1     -randomseed=1       128 3 | pgmhist | \
+  awk '$1==0 || $1==255'
+pbmnoise -ratio=0/1024  -randomseed=1       64  6 | pgmhist | \
+  awk '$1==0 || $1==255'
+pbmnoise -ratio=0/8192  -randomseed=1       32 12 | pgmhist | \
+  awk '$1==0 || $1==255'
+pbmnoise -ratio=0/65536 -randomseed=1 -pack 1 384 | pgmhist | \
+  awk '$1==0 || $1==255'
+
+echo "Test 3-white.  Should print 0    320    100%    100% three times"
+
+pbmnoise -ratio=1/1         -randomseed=1       64  5 | pgmhist | \
+  awk '$1==0 || $1==255'
+pbmnoise -ratio=8/8         -randomseed=1       32 10 | pgmhist | \
+  awk '$1==0 || $1==255'
+pbmnoise -ratio=65536/65536 -randomseed=1 -pack 2 160 | pgmhist | \
+  awk '$1==0 || $1==255'
+
diff --git a/test/pbmpage.test b/test/pbmpage.test
index cfacefa7..2ee27122 100755
--- a/test/pbmpage.test
+++ b/test/pbmpage.test
@@ -22,14 +22,14 @@ test_out=${tmpdir}/test_out
 pbmpage -a3 1 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmpage 0 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmpage 4 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pbmpscale.ok b/test/pbmpscale.ok
index c26ffef3..9ea594d8 100644
--- a/test/pbmpscale.ok
+++ b/test/pbmpscale.ok
@@ -1,53 +1,178 @@
+Test 1.  Should print "abc" rendered in PBM plain format
 P1
-21 45
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-111111111111111111110
-111111111111111111111
-111111111111111111111
-001111100000000001111
-000111000000000000111
-000111000000000000111
-000111000000000000000
-000111000000000000000
-000111000000000000000
-000111000000000111000
-000111000000000111000
-000111100000001111000
-000111111111111111000
-000111111111111111000
-000111111111111111000
-000111100000001111000
-000111000000000111000
-000111000000000111000
-000111000000000000000
-000111000000000000000
-000111000000000000000
-000111000000000000000
-000111000000000000000
-001111100000000000000
-111111111111000000000
-111111111111000000000
-111111111111000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-000000000000000000000
-2150868536 457
-3761734242 1065
-3462719777 1803
+17 15
+00000000000000000
+00000000000000000
+00000000000000000
+00000011000000000
+00000001000000000
+00000001000000000
+00110001110000110
+01001001001001001
+00111001001001000
+01001001001001000
+01001001001001001
+00110101110000110
+00000000000000000
+00000000000000000
+00000000000000000
+Test 2.  "ABC" in PBM plain format
+P1
+48 30
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000001100000000111111111110000000000111100011
+000000001100000000111111111111100000011111110011
+000000001100000000011110000111100000011110011111
+000000011110000000001100000011110000111100001111
+000000010010000000001100000000110000111000000111
+000000110011000000001100000000110001110000000011
+000000110011000000001100000011110001100000000000
+000001100001100000001110000111100011000000000000
+000001000000100000001111111111000011000000000000
+000011000000110000001111111111000011000000000000
+000011111111110000001110000001100011000000000000
+000011111111110000001100000000110001100000000000
+000011100001110000001100000000110001110000000011
+000111000000111000001100000001110000111000000111
+001100000000001100001100000011110000111100001111
+011100000000001110011110000111100000011110011110
+111111000000111111111111111111100000011111111110
+111111000000111111111111111110000000000111111000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000
+Test 3.  "D.!" in PBM plain format
+P1
+42 45
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+111111111111111110000000000000000000000111
+111111111111111111000000000000000000000111
+111111111111111111100000000000000000000111
+001111100000001111110000000000000000000111
+000111000000000111111000000000000000000111
+000111000000000111111000000000000000000111
+000111000000000001111000000000000000000111
+000111000000000000111000000000000000000111
+000111000000000000111100000000000000000111
+000111000000000000001110000000000000000111
+000111000000000000000111000000000000000111
+000111000000000000000111000000000000000111
+000111000000000000000111000000000000000111
+000111000000000000000111000000000000000111
+000111000000000000000111000000000000000111
+000111000000000000000111000000000000000111
+000111000000000000000111000000000000000111
+000111000000000000001110000000000000000111
+000111000000000000111100000000000000000111
+000111000000000000111000000000000000000111
+000111000000000001111000000000000000000111
+000111000000000111111000000000000000000000
+000111000000000111111000000000000000000000
+001111100000001111110000000000000000000000
+111111111111111111100000000000111000000111
+111111111111111111000000000000111000000111
+111111111111111110000000000000111000000111
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000
+Test 3.  "E,?" in PBM plain format
+P1
+60 60
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+011111111111111111111111110000000000000000000000011111100000
+111111111111111111111111111000000000000000000000111111110000
+111111111111111111111111111100000000000000000001111111111000
+011111111111111111111111111100000000000000000011111111111100
+001111111100000000000011111100000000000000000111110000111110
+000111111000000000000001111100000000000000001111100000011111
+000011110000000000000000111100000000000000001111000000001111
+000011110000000000000000011000000000000000000110000000001111
+000011110000000000000000000000000000000000000000000000001111
+000011110000000000000000000000000000000000000000000000001111
+000011110000000000000000000000000000000000000000000000011111
+000011110000000000000000000000000000000000000000000000111110
+000011110000000000000110000000000000000000000000000001111100
+000011110000000000001111000000000000000000000000000011111000
+000011111000000000011111000000000000000000000000000111110000
+000011111100000000111111000000000000000000000000001111100000
+000011111111111111111111000000000000000000000000011111000000
+000011111111111111111111000000000000000000000000111110000000
+000011111111111111111111000000000000000000000000111100000000
+000011111111111111111111000000000000000000000000111100000000
+000011111100000000111111000000000000000000000000111100000000
+000011111000000000011111000000000000000000000000111100000000
+000011110000000000001111000000000000000000000000111100000000
+000011110000000000000110000000000000000000000000111100000000
+000011110000000000000000000000000000000000000000111100000000
+000011110000000000000000000000000000000000000000111100000000
+000011110000000000000000000000000000000000000000111100000000
+000011110000000000000000000000000000000000000000011000000000
+000011110000000000000000011000000000000000000000000000000000
+000011110000000000000000111100000000000000000000000000000000
+000111111000000000000001111100000000000000000000000000000000
+001111111100000000000011111100000000000000000000000000000000
+011111111111111111111111111100000000011000000000011000000000
+111111111111111111111111111100000000111100000000111100000000
+111111111111111111111111111000000000111100000000111100000000
+011111111111111111111111110000000000111100000000011000000000
+000000000000000000000000000000000000111100000000000000000000
+000000000000000000000000000000000000111100000000000000000000
+000000000000000000000000000000000001111100000000000000000000
+000000000000000000000000000000000011111000000000000000000000
+000000000000000000000000000000000111110000000000000000000000
+000000000000000000000000000000001111100000000000000000000000
+000000000000000000000000000000001111000000000000000000000000
+000000000000000000000000000000000110000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+000000000000000000000000000000000000000000000000000000000000
+Test 5.  Should print 576003211 10631
+576003211 10631
+Test 6.  Should print 3501321825 369
+3501321825 369
+Test 7.  Should print 1588181271 535
+1588181271 535
+Test 8.  Should print 2843090781 610
+2843090781 610
+Test 9.  Should print 146160766 820
+146160766 820
+Test 10. Should print 3327221668 2111
+3327221668 2111
+Test Invalid
 Expected failure 1 1
 Expected failure 2 1
 Expected failure 3 1
diff --git a/test/pbmpscale.test b/test/pbmpscale.test
index 21ad6cd1..a92511b0 100755
--- a/test/pbmpscale.test
+++ b/test/pbmpscale.test
@@ -1,18 +1,35 @@
 #! /bin/bash
 # This script tests: pbmpscale
-# Also requires: pamenlarge pbmtext
-
-
-pbmtext -nomargin "F" | pbmpscale 3 -plain
-
-for i in 2 3 4
-do
-pamenlarge 2 testgrid.pbm | \
-  pbmpscale $i | cksum
-done
-
+# Also requires: pbmtext
+
+echo "Test 1.  Should print \"abc\" rendered in PBM plain format"
+pbmtext -nomargin "abc" | pbmpscale 1 -plain
+echo "Test 2.  \"ABC\" in PBM plain format"
+pbmtext -nomargin "ABC" | pbmpscale 2 -plain
+echo "Test 3.  \"D.!\" in PBM plain format"
+pbmtext -nomargin "D.!" | pbmpscale 3 -plain
+echo "Test 3.  \"E,?\" in PBM plain format"
+pbmtext -nomargin "E,?" | pbmpscale 4 -plain
+
+echo "Test 5.  Should print 576003211 10631"
+pbmpscale 5 maze.pbm | cksum
+
+echo "Test 6.  Should print 3501321825 369"
+pbmtext -nomargin "6" | pbmpscale 6 | cksum
+echo "Test 7.  Should print 1588181271 535"
+pbmtext -nomargin "7" | pbmpscale 7 | cksum
+echo "Test 8.  Should print 2843090781 610"
+pbmtext -nomargin "8" | pbmpscale 8 | cksum
+echo "Test 9.  Should print 146160766 820"
+pbmtext -nomargin "9" | pbmpscale 9 | cksum
+echo "Test 10. Should print 3327221668 2111"
+pbmtext -nomargin "10" | pbmpscale 10 | cksum
+
+tmpdir=${tmpdir:-/tmp}
 test_out=${tmpdir}/test_out
 
+echo "Test Invalid"
+
 echo 1>&2
 echo "Invalid command-line argument combinations." 1>&2
 echo "Error messages should appear below the line." 1>&2
@@ -21,14 +38,14 @@ echo "-----------------------------------------------------------" 1>&2
 pbmpscale testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmpscale 0 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmpscale 2 3 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pbmtext-bdf.test b/test/pbmtext-bdf.test
index b12c10bf..d3d4b37f 100755
--- a/test/pbmtext-bdf.test
+++ b/test/pbmtext-bdf.test
@@ -173,3 +173,5 @@ do
   echo $?
   rm ${font_corrupt_bdf}
 done
+
+rm ${font_bdf}
\ No newline at end of file
diff --git a/test/pbmtog3.ok b/test/pbmtog3.ok
index cef9e3f9..6660206b 100644
--- a/test/pbmtog3.ok
+++ b/test/pbmtog3.ok
@@ -1,10 +1,20 @@
-3697098186 144
-1248301383 122
-686713716 144
-215463240 122
+Test 1.  Should print 2156383714 1106
+2156383714 1106
+Test 2.  Should print 162371073 1049
+162371073 1049
+Test 3.  Should print 750267045 1106
+750267045 1106
+Test 4.  Should print 1140269483 1049
+1140269483 1049
+Test 5.  Should print 28792587 47
 28792587 47
+Test 6.  Should print 277456854 32
 277456854 32
+Test 7.  Should print 28792587 47
 28792587 47
+Test 8.  Should print 3736247115 62
 3736247115 62
+Test 9.  Should print 2820255307 2191856
 2820255307 2191856
+Test 10.  Should print 4159089282 2226575
 4159089282 2226575
diff --git a/test/pbmtog3.test b/test/pbmtog3.test
index c5dc3357..c23e8af0 100755
--- a/test/pbmtog3.test
+++ b/test/pbmtog3.test
@@ -2,24 +2,33 @@
 # This script tests: pbmtog3
 # Also requires: pbmmake
 
-# Test 1.  Should print 3697098186 144
-pbmtog3 testgrid.pbm | cksum
-# Test 2.  Should print 1248301383 122
-pbmtog3 -nofixedwidth testgrid.pbm | cksum
-# Test 3.  Should print 686713716 144
-pbmtog3 -reverse testgrid.pbm | cksum
-# Test 4.  Should print 215463240 122
-pbmtog3 -nofixedwidth -reverse testgrid.pbm | cksum
-# Test 5.  Should print 28792587 47
+echo "Test 1.  Should print 2156383714 1106"
+pbmtog3 maze.pbm | cksum
+
+echo "Test 2.  Should print 162371073 1049"
+pbmtog3 -nofixedwidth maze.pbm | cksum
+
+echo "Test 3.  Should print 750267045 1106"
+pbmtog3 -reverse maze.pbm | cksum
+
+echo "Test 4.  Should print 1140269483 1049"
+pbmtog3 -nofixedwidth -reverse maze.pbm | cksum
+
+echo "Test 5.  Should print 28792587 47"
 pbmmake -w 10 10 | pbmtog3 | cksum
-# Test 6.  Should print 277456854 32
+
+echo "Test 6.  Should print 277456854 32"
 pbmmake -w 10 10 | \
   pbmtog3 -nofixedwidth | cksum
-# Test 7.  Should print 28792587 47
+
+echo "Test 7.  Should print 28792587 47"
 pbmmake -w 10000 10 | pbmtog3 | cksum
-# Test 8.  Should print 3736247115 62
+
+echo "Test 8.  Should print 3736247115 62"
 pbmmake -b 10 10 | pbmtog3 | cksum
-# Test 9.  Should print 2820255307 2191856
+
+echo "Test 9.  Should print 2820255307 2191856"
 pbmmake -g 1700 2286 | pbmtog3 | cksum
-# Test 10.  Should print 4159089282 2226575
+
+echo "Test 10.  Should print 4159089282 2226575"
 pbmmake -g 1800 2286 | pbmtog3 | cksum
diff --git a/test/pbmtopgm.test b/test/pbmtopgm.test
index dc872640..53b25aab 100755
--- a/test/pbmtopgm.test
+++ b/test/pbmtopgm.test
@@ -20,19 +20,19 @@ echo "-----------------------------------------------------------" 1>&2
 pbmtopgm 5 0 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmtopgm 0 9 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmtopgm 15 5 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmtopgm 5 17 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pbmupc.test b/test/pbmupc.test
index a29a06f3..f4a4d1e8 100755
--- a/test/pbmupc.test
+++ b/test/pbmupc.test
@@ -22,44 +22,44 @@ echo "-----------------------------------------------------------" 1>&2
 pbmupc -s3 0 72890 00011     > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1   72890 00011     > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1 0 72890           > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1 10 72890 00011    > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1 0 172890 00011    > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1 0   2890 00011    > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1 0 72890 100011    > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1 0 72890   0011    > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pbmupc -s1 0 72890 100011 1  > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pgmhist.test b/test/pgmhist.test
index 0179a4f6..b028b07a 100755
--- a/test/pgmhist.test
+++ b/test/pgmhist.test
@@ -37,19 +37,19 @@ echo "-----------------------------------------------------------" 1>&2
 pgmhist -median   -quartile testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmhist -median   -decile   testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmhist -quartile -decile   testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmhist testimg.ppm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pgmmake.test b/test/pgmmake.test
index bd9882a5..0ec5efd6 100755
--- a/test/pgmmake.test
+++ b/test/pgmmake.test
@@ -21,39 +21,39 @@ test_out=${tmpdir}/test_out
 pgmmake 100  5 5 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmmake 1.01 5 5 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmmake .5   5   > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmmake .5       > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmmake -maxval=5        5 5 > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmmake -maxval=0     .5 5 5 > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmmake -maxval=-1    .5 5 5 > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmmake -maxval=65536 .5 5 5 > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pgmminkowski.test b/test/pgmminkowski.test
index 52e02e19..9d8c257d 100755
--- a/test/pgmminkowski.test
+++ b/test/pgmminkowski.test
@@ -1,8 +1,8 @@
 #! /bin/bash
 # This script tests: pgmminkowski
-# Also requires: pbmmake pgmmake pnmmargin pnmpad
+# Also requires: pbmmake pgmmake pnmpad
 
-pgmmake .5 1 1 | pnmmargin -b 1 | \
+pgmmake .5 1 1 | pnmpad -black -top=1 -left=1 -bottom=1 -right=1 | \
   pgmminkowski
 echo
 pbmmake -g 3 3 | pgmminkowski
diff --git a/test/pgmnoise-parameters.test b/test/pgmnoise-parameters.test
index d2a96530..bf3deb3f 100755
--- a/test/pgmnoise-parameters.test
+++ b/test/pgmnoise-parameters.test
@@ -13,24 +13,24 @@ echo "-----------------------------------------------------------" 1>&2
 pgmnoise -maxval=255  -randomseed=1 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 100 -randomseed=1 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 100 0 -randomseed=1 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 0 100 -randomseed=1 > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 100 100 100 -randomseed=1 > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pgmnoise.test b/test/pgmnoise.test
index ab98a5e2..4a6e21dd 100755
--- a/test/pgmnoise.test
+++ b/test/pgmnoise.test
@@ -1,6 +1,6 @@
 #! /bin/bash
 # This script tests: pgmnoise
-# Also requires: pgmhist pamvalidate
+# Also requires: pgmhist pamvalidate pamfile
 
 echo "Test 1."
 echo "Should print: 2132901423 10015" # (Mersenne Twister)
@@ -70,49 +70,49 @@ echo "-----------------------------------------------------------" 1>&2
 pgmnoise 0 0  > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 0 1  > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 1 0  > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise      > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 1    > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise 100 -1 > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise -randomseed=-1 100 100  > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise -maxval=-1 100 100  > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise -maxval=0 100 100  > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pgmnoise -maxval=$((256 * 256 * 256 * 256)) 10 10 > ${test_out} || \
   echo -n "Expected failure 10"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pgmtoppm.ok b/test/pgmtoppm.ok
index 55e5af95..2484c487 100644
--- a/test/pgmtoppm.ok
+++ b/test/pgmtoppm.ok
@@ -1,3 +1,39 @@
+Test 1.  Should print 1767073524 779 twice, then 1676700883 779
+1767073524 779
 1767073524 779
 1676700883 779
+Test 2.  Should print 718580145 779 twice
+718580145 779
+718580145 779
+Test 3.  Should print 718580145 779 twice
+718580145 779
 718580145 779
+Test 4.  Should print 829921912 685 seven times
+829921912 685
+829921912 685
+829921912 685
+829921912 685
+829921912 685
+829921912 685
+829921912 685
+Test 5.  Should print 934505096 685 four times
+934505096 685
+934505096 685
+934505096 685
+934505096 685
+Test 6. Should print 3083028153 685 three times
+3083028153 685
+3083028153 685
+3083028153 685
+Test 7 Invalid
+Expected failure 1 1
+Expected failure 2 1
+Expected failure 3 1
+Expected failure 4 1
+Expected failure 5 1
+Expected failure 6 1
+Expected failure 7 1
+Expected failure 8 1
+Expected failure 9 1
+Expected failure 10 1
+Expected failure 11 1
diff --git a/test/pgmtoppm.test b/test/pgmtoppm.test
index 70df0d97..622a0688 100755
--- a/test/pgmtoppm.test
+++ b/test/pgmtoppm.test
@@ -1,22 +1,139 @@
 #! /bin/bash
 # This script tests: pgmtoppm
-# Also requires: pamseq pamtopnm pgmramp
-
+# Also requires: pamtopnm pnminvert pamseq pgmramp
 
 tmpdir=${tmpdir:-/tmp}
 
+echo "Test 1.  Should print 1767073524 779 twice, then 1676700883 779"
+
 test_pgm=${tmpdir}/test.pgm
-palette=${tmpdir}/palette
+palette_pam=${tmpdir}/palette.pam
+palette_ppm=${tmpdir}/palette.ppm
 
-pgmramp -maxval=5 -lr 256 1 >${test_pgm}
-pamseq 3 5 -tupletype=RGB | pamtopnm \
-  >${palette}
+pgmramp -maxval=5 -lr 256 1 > ${test_pgm}
 
-# Test 1.
 pgmtoppm green ${test_pgm} | cksum
-
+pgmtoppm black-green ${test_pgm} | cksum
 pgmtoppm yellow-blue ${test_pgm} | cksum
 
-pgmtoppm -map=${palette} ${test_pgm} | cksum
 
-rm ${test_pgm} ${palette}
+echo "Test 2.  Should print 718580145 779 twice"
+pamseq 3 5 -tupletype=RGB | tee ${palette_pam} | pamtopnm > ${palette_ppm}
+pgmtoppm -map=${palette_pam} ${test_pgm} | cksum
+pgmtoppm -map=${palette_ppm} ${test_pgm} | cksum
+
+rm ${palette_pam}
+
+echo "Test 3.  Should print 718580145 779 twice"
+
+pgmtoppm white-black ${test_pgm} | pnminvert | cksum
+pnminvert ${test_pgm} | pgmtoppm white-black | cksum
+
+rm ${test_pgm}
+
+echo "Test 4.  Should print 829921912 685 seven times"
+
+pgmtoppm white testgrid.pbm | cksum
+pgmtoppm black-white testgrid.pbm | cksum
+pgmtoppm rgb:ff/ff/ff testgrid.pbm | cksum
+pgmtoppm -white=rgb-255:255/255/255 testgrid.pbm | cksum
+pgmtoppm rgb:00/00/00-rgb:ff/ff/ff testgrid.pbm | cksum
+pgmtoppm rgbi:0/0/0-rgbi:1/1/1 testgrid.pbm | cksum
+pgmtoppm -black=rgb-255:0/0/0 -white=rgb-255:255/255/255 testgrid.pbm | cksum
+
+echo "Test 5.  Should print 934505096 685 four times"
+
+pgmtoppm white-black testgrid.pbm | cksum
+pgmtoppm white testgrid.pbm | pnminvert| cksum
+pnminvert testgrid.pbm | pgmtoppm black-white | cksum
+pnminvert testgrid.pbm | pgmtoppm white | cksum
+
+echo "Test 6. Should print 3083028153 685 three times"
+
+pgmtoppm black testgrid.pbm | cksum
+pgmtoppm black-black testgrid.pbm | cksum
+pgmtoppm rgbi:0/0/0-rgbi:0/0/0 testgrid.pbm | cksum
+
+echo "Test 7 Invalid"
+
+tmpdir=${tmpdir:-/tmp}
+test_out=${tmpdir}/test_out
+
+echo 1>&2
+echo "Invalid command-line argument combinations." 1>&2
+echo "Error messages should appear below the line." 1>&2
+echo "-----------------------------------------------------------" 1>&2
+
+pgmtoppm white testimg.ppm > \
+  ${test_out} || \
+  echo -n "Expected failure 1"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm -map=/dev/null testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 2"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm black white testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 3"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm hwite testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 4"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm off-color testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 5"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm rgb-255:7/7/7 testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 6"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm -black=black -white=white white testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 7"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm -black=rgb:0/0/0 -map=${palette_ppm} testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 8"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm -white=rgb:ff/ff/ff -map=${palette_ppm} testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 9"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm rgb:ff/ff/ff -map=${palette_ppm} testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 10"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+pgmtoppm rgb:00/00/00-rgb:ff/ff/ff -map=${palette_ppm} testgrid.pbm > \
+  ${test_out} || \
+  echo -n "Expected failure 11"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
+
+#pgmtoppm rgb-255:7/7/7 testgrid.pbm > \
+#  ${test_out} || \
+#  echo -n "Expected failure 12"
+#  test -s ${test_out}; echo " "$?
+#  rm -f ${test_out}
+
+rm -f ${palette_ppm}
diff --git a/test/pi3-roundtrip.ok b/test/pi3-roundtrip.ok
index 3fde31b1..c4351b83 100644
--- a/test/pi3-roundtrip.ok
+++ b/test/pi3-roundtrip.ok
@@ -1,2 +1,5 @@
+Test 1. Should print: 3139449799 32011 twice
 3139449799 32011
-2425386270 41
+3139449799 32011
+Test 2. Should print: 281226646 481, cksum of maze.pbm
+281226646 481
diff --git a/test/pi3-roundtrip.test b/test/pi3-roundtrip.test
index 5700337d..6c75acc7 100755
--- a/test/pi3-roundtrip.test
+++ b/test/pi3-roundtrip.test
@@ -1,16 +1,15 @@
 #! /bin/bash
 # This script tests: pbmtopi3 pi3topbm
-# Also requires: pbmmake pamcut
-
+# Also requires: pbmmake pamcut pamfile
 
 # The pi3 image format specifies a fixed image size of 640x400.
 # Pbmtopi3 rejects images that do not conform.
 
-# Test
-# Should print: 3139449799 32011
+echo "Test 1. Should print: 3139449799 32011 twice"
+pbmmake -g 640 400 | cksum
 pbmmake -g 640 400 | pbmtopi3 | pi3topbm | cksum
 
-# Test 2.
-# Should print: 2425386270 41
-pamcut -pad 0 0 640 400 testgrid.pbm | \
-  pbmtopi3 | pi3topbm | pamcut 0 0 14 16 | cksum
+echo "Test 2. Should print: 281226646 481, cksum of maze.pbm"
+size=$(pamfile -size maze.pbm | awk '{print "-width="$1,"-height="$2}')
+pamcut -pad -left=0 -top=0 -width=640 -height=400 maze.pbm | \
+  pbmtopi3 | pi3topbm | pamcut -left=0 -top=0 ${size} | cksum
diff --git a/test/pj-roundtrip.test b/test/pj-roundtrip.test
index 2b0d61e5..3ed30927 100755
--- a/test/pj-roundtrip.test
+++ b/test/pj-roundtrip.test
@@ -19,8 +19,8 @@ echo "Test 2. Should print:  0 0 : 0"
 
 testout_ppm=${tmpdir}/testout.ppm
 
-ppmtopj testgrid.pbm | pjtoppm > ${testout_ppm}
-  ppmtoppm < testgrid.pbm | cmp -s - ${testout_ppm} > /dev/null
+ppmtopj maze.pbm | pjtoppm > ${testout_ppm}
+  ppmtoppm < maze.pbm | cmp -s - ${testout_ppm} > /dev/null
   echo ${PIPESTATUS[@]} ":" $?
 
 rm ${testout_ppm}
diff --git a/test/png-roundtrip.ok b/test/png-roundtrip.ok
index 28b8c057..ec0a1937 100644
--- a/test/png-roundtrip.ok
+++ b/test/png-roundtrip.ok
@@ -16,21 +16,21 @@
 1926073387 101484
 1926073387 101484
 1926073387 101484
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
diff --git a/test/png-roundtrip.test b/test/png-roundtrip.test
index 7e7c4a9b..fcb5927a 100755
--- a/test/png-roundtrip.test
+++ b/test/png-roundtrip.test
@@ -28,7 +28,7 @@ for flags in "" -interlace \
 pnmtopng testimg.ppm $flags | pngtopam | cksum
 done
 
-# Test 2.  Should print 2425386270 41 18 times
+# Test 2.  Should print 281226646 481 18 times
 for flags in "" -interlace \
   -gamma=.45 \
   -hist \
@@ -47,5 +47,5 @@ for flags in "" -interlace \
   -comp_strategy=filtered \
   -force
   do
-  pnmtopng testgrid.pbm $flags | pngtopam | cksum
+  pnmtopng maze.pbm $flags | pngtopam | cksum
   done
diff --git a/test/png-roundtrip2.ok b/test/png-roundtrip2.ok
index 0e712ce7..764cadfa 100644
--- a/test/png-roundtrip2.ok
+++ b/test/png-roundtrip2.ok
@@ -1,4 +1,4 @@
 1926073387 101484
 1926073387 101484
-2425386270 41
-2425386270 41
+281226646 481
+281226646 481
diff --git a/test/png-roundtrip2.test b/test/png-roundtrip2.test
index af2ad029..5705a1ae 100755
--- a/test/png-roundtrip2.test
+++ b/test/png-roundtrip2.test
@@ -15,8 +15,8 @@ for flags in "" -gamma=.45
   pamtopng testimg.ppm $flags | pngtopam | cksum
   done
 
-# Test 2.  Should print 2425386270 41 twice
+# Test 2.  Should print 281226646 481 twice
 for flags in "" -gamma=.45
   do
-  pamtopng testgrid.pbm $flags | pngtopam | cksum
+  pamtopng maze.pbm $flags | pngtopam | cksum
   done
diff --git a/test/pnm-pam-roundtrip.ok b/test/pnm-pam-roundtrip.ok
index 67f7a1fe..ace97410 100644
--- a/test/pnm-pam-roundtrip.ok
+++ b/test/pnm-pam-roundtrip.ok
@@ -1,2 +1,2 @@
 1926073387 101484
-2425386270 41
+281226646 481
diff --git a/test/pnm-pam-roundtrip.test b/test/pnm-pam-roundtrip.test
index b8e60c88..13925f74 100755
--- a/test/pnm-pam-roundtrip.test
+++ b/test/pnm-pam-roundtrip.test
@@ -4,4 +4,4 @@
 
 
 pamtopam < testimg.ppm | pamtopnm | cksum
-pamtopam < testgrid.pbm | pamtopnm | cksum
+pamtopam < maze.pbm | pamtopnm | cksum
diff --git a/test/pnm-plain-roundtrip.ok b/test/pnm-plain-roundtrip.ok
index 67f7a1fe..ace97410 100644
--- a/test/pnm-plain-roundtrip.ok
+++ b/test/pnm-plain-roundtrip.ok
@@ -1,2 +1,2 @@
 1926073387 101484
-2425386270 41
+281226646 481
diff --git a/test/pnm-plain-roundtrip.test b/test/pnm-plain-roundtrip.test
index 3779d761..bf685e85 100755
--- a/test/pnm-plain-roundtrip.test
+++ b/test/pnm-plain-roundtrip.test
@@ -5,5 +5,5 @@
 
 pamtopnm -plain testimg.ppm | \
   pamtopnm | cksum
-pamtopnm -plain testgrid.pbm | \
+pamtopnm -plain maze.pbm | \
   pamtopnm | cksum
diff --git a/test/pnmcat.ok b/test/pnmcat.ok
index b92a1420..57769353 100644
--- a/test/pnmcat.ok
+++ b/test/pnmcat.ok
@@ -1,8 +1,82 @@
-Test 1.
-1704087873 73
-4150323653 73
-1522490272 202953
-2862441566 202953
+Test 1.  Should print a simple PBM image four times
+P1
+2 2
+01
+10
+P1
+2 2
+01
+10
+P1
+2 2
+01
+10
+P1
+2 2
+01
+10
+Test 2.  Should print 2197356643 895 twice
+2197356643 895
+2197356643 895
+Test 3.  Should print 3313353797 954
+3313353797 954
+Test 4.  Should print 1731660895 1308 three times
+1731660895 1308
+1731660895 1308
+1731660895 1308
+Test 5.  Should print 2985957591 1426 three times
+2985957591 1426
+2985957591 1426
+2985957591 1426
+Test 6.  Should print a simple PGM image three times
+P2
+4 4
+3
+0 0 1 1
+0 1 1 2
+1 1 2 2
+1 2 2 3
+P2
+4 4
+3
+0 0 1 1
+0 1 1 2
+1 1 2 2
+1 2 2 3
+P2
+4 4
+3
+0 0 1 1
+0 1 1 2
+1 1 2 2
+1 2 2 3
+Test 7.  Should print 2097565808 394 twice
+2097565808 394
+2097565808 394
+Test 8.  Should print 3086569577 394
+3086569577 394
+Test 9.  Should print 4288335051 586 three times
+4288335051 586
+4288335051 586
+4288335051 586
+Test 10.  Should print 642720605 586 three times
+642720605 586
+642720605 586
+642720605 586
+Test 11.  Should print 3622741282 1019 twice
+3622741282 1019
+3622741282 1019
+Test 12.  Should print 1401081637 1019
+1401081637 1019
+Test 13.  Should print 2756501917 1019
+2756501917 1019
+Test 14. Should print 587933655 107742 twice
+587933655 107742
+587933655 107742
+Test 15.  Should print 3948141157 107742
+3948141157 107742
+Test 16.  Should print 3910239002 107742
+3910239002 107742
 Test Invalid
 Expected failure 1 1
 Expected failure 2 1
diff --git a/test/pnmcat.test b/test/pnmcat.test
index 519e01c7..c098d967 100755
--- a/test/pnmcat.test
+++ b/test/pnmcat.test
@@ -1,18 +1,91 @@
 #! /bin/bash
 # This script tests: pnmcat
-# Also requires:
+# Also requires: pbmmake pgmramp ppmtoppm
 
-echo "Test 1."
-
-pnmcat -lr testgrid.pbm testgrid.pbm | cksum
-pnmcat -tb testgrid.pbm testgrid.pbm | cksum
-
-pnmcat -lr testimg.ppm testimg.ppm | cksum
-pnmcat -tb testimg.ppm testimg.ppm | cksum
+tmpdir=${tmpdir:-/tmp}
+check2x2_pbm=${tmpdir}/check2x2.pbm
+maze2lr_pbm=${tmpdir}/maze2lr.pbm
+maze2tb_pbm=${tmpdir}/maze2tb.pbm
+diag_pgm=${tmpdir}/diag.pgm
+diag_ppm=${tmpdir}/diag.ppm
+diag2lr_ppm=${tmpdir}/diag2lr.ppm
+diag2tb_ppm=${tmpdir}/diag2tb.ppm
+
+echo "Test 1.  Should print a simple PBM image four times"
+pbmmake -g 2 2 | tee ${check2x2_pbm} | pnmcat -lr -plain
+pnmcat -tb -plain ${check2x2_pbm}
+pnmcat -lr -black -plain ${check2x2_pbm}
+cat ${check2x2_pbm} | pnmcat -tb -white -plain
+
+rm ${check2x2_pbm}
+
+echo "Test 2.  Should print 2197356643 895 twice"
+pnmcat -lr -white maze.pbm maze.pbm | tee ${maze2lr_pbm} | cksum
+cat maze.pbm | pnmcat -lr -black maze.pbm - | cksum
+
+echo "Test 3.  Should print 3313353797 954"
+pnmcat -tb maze.pbm maze.pbm | tee ${maze2tb_pbm} | cksum
+
+echo "Test 4.  Should print 1731660895 1308 three times"
+pnmcat -lr maze.pbm maze.pbm maze.pbm | cksum
+pnmcat -lr ${maze2lr_pbm} maze.pbm | cksum
+cat ${maze2lr_pbm} | pnmcat -lr - maze.pbm | cksum
+
+echo "Test 5.  Should print 2985957591 1426 three times"
+pnmcat -tb maze.pbm maze.pbm maze.pbm | cksum
+pnmcat -tb maze.pbm ${maze2tb_pbm} | cksum
+cat maze.pbm | pnmcat -tb - ${maze2tb_pbm} | cksum
+
+rm ${maze2lr_pbm} ${maze2tb_pbm}
+
+echo "Test 6.  Should print a simple PGM image three times"
+pgmramp -diag 4 4 -maxval=3 | tee ${diag_pgm} | pnmcat -lr -plain
+pnmcat -tb -plain ${diag_pgm}
+cat ${diag_pgm} | pnmcat -tb -plain
+
+rm ${diag_pgm}
+
+pgmramp -diag 8 8 -maxval 7 | ppmtoppm > ${diag_ppm} 
+
+echo "Test 7.  Should print 2097565808 394 twice"
+pnmcat -lr -black ${diag_ppm} ${diag_ppm} | tee ${diag2lr_ppm} | cksum
+cat ${diag_ppm} | pnmcat -lr -white ${diag_ppm} - | cksum
+
+echo "Test 8.  Should print 3086569577 394"
+pnmcat -tb ${diag_ppm} ${diag_ppm} | tee ${diag2tb_ppm} | cksum
+
+echo "Test 9.  Should print 4288335051 586 three times"
+pnmcat -lr ${diag_ppm} ${diag_ppm} ${diag_ppm} | cksum
+pnmcat -lr ${diag_ppm}  ${diag2lr_ppm} | cksum 
+cat ${diag2lr_ppm} | pnmcat -lr - ${diag_ppm} | cksum
+
+echo "Test 10.  Should print 642720605 586 three times"
+pnmcat -tb ${diag_ppm} ${diag_ppm} ${diag_ppm} | cksum
+pnmcat -tb ${diag2tb_ppm} ${diag_ppm} | cksum
+cat ${diag_ppm} | pnmcat -tb ${diag2tb_ppm} - | cksum
+
+rm ${diag2lr_ppm} ${diag2tb_ppm}
+
+echo "Test 11.  Should print 3622741282 1019 twice"
+pnmcat -tb -white testgrid.pbm ${diag_ppm} | cksum
+pnmcat -tb -jcenter -white testgrid.pbm ${diag_ppm} | cksum
+echo "Test 12.  Should print 1401081637 1019"
+pnmcat -tb -jleft -white testgrid.pbm ${diag_ppm}  | cksum
+echo "Test 13.  Should print 2756501917 1019"
+pnmcat -tb -jright -white testgrid.pbm ${diag_ppm} | cksum
+
+echo "Test 14. Should print 587933655 107742 twice"
+pnmcat -lr -black testgrid.pbm testimg.ppm | cksum
+pnmcat -lr -jcenter -black testgrid.pbm testimg.ppm | cksum
+echo "Test 15.  Should print 3948141157 107742"
+pnmcat -lr -jtop -black testgrid.pbm testimg.ppm | cksum
+echo "Test 16.  Should print 3910239002 107742"
+pnmcat -lr -jbottom -black testgrid.pbm testimg.ppm | cksum
+
+rm ${diag_ppm}
 
 echo "Test Invalid"
 
-tmpdir=${tmpdir:-/tmp}
 test_out=${tmpdir}/test_out
 
 echo 1>&2
@@ -24,74 +97,74 @@ echo "-----------------------------------------------------------" 1>&2
 pnmcat testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # both directions specified
 pnmcat -topbottom -leftright testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # both pad colors specified
 pnmcat -topbottom -white -black testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # justification parameters overspecified
 pnmcat -lr -jtop -jbottom testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -lr -jtop -jcenter testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -lr -jcenter -jbottom testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -tb -jleft -jright testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -tb -jleft -jcenter testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -tb -jcenter -jright testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # justification parameter in the wrong direction
 pnmcat -lr -jleft    testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 10"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -lr -jright   testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 11"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -tb -jtop     testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 12"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcat -tb -jbottom  testgrid.pbm testimg.ppm > ${test_out} || \
   echo -n "Expected failure 13"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # more than one input image from standard input
 cat testgrid.pbm | pnmcat -lr - - testimg.ppm > ${test_out} || \
   echo -n "Expected failure 14"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pnmcolormap.test b/test/pnmcolormap.test
index 9ac0ea9e..90d11f6d 100755
--- a/test/pnmcolormap.test
+++ b/test/pnmcolormap.test
@@ -2,9 +2,9 @@
 # This script tests: pnmcolormap
 # Also requires:
 
-pnmcolormap -plain -sort 2 testgrid.pbm
-pnmcolormap -plain -sort -square 2 testgrid.pbm
-pnmcolormap -plain -sort all testgrid.pbm
+pnmcolormap -plain -sort 2 maze.pbm
+pnmcolormap -plain -sort -square 2 maze.pbm
+pnmcolormap -plain -sort all maze.pbm
 
 echo 1>&2
 echo "Invalid command-line arguments." 1>&2
@@ -17,35 +17,35 @@ test_out=${tmpdir}/test_out
 pnmcolormap 0 testimg.ppm   > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcolormap -1 testimg.ppm  > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcolormap 0.1 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcolormap -center -meancolor 16 testimg.ppm    > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcolormap -center -meanpixel 16 testimg.ppm    > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcolormap -meancolor -meanpixel 16 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmcolormap -spreadbrightness -spreadluminosity 16 \
   testimg.ppm > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pnminvert-roundtrip.ok b/test/pnminvert-roundtrip.ok
index 67f7a1fe..ace97410 100644
--- a/test/pnminvert-roundtrip.ok
+++ b/test/pnminvert-roundtrip.ok
@@ -1,2 +1,2 @@
 1926073387 101484
-2425386270 41
+281226646 481
diff --git a/test/pnminvert-roundtrip.test b/test/pnminvert-roundtrip.test
index 52d15039..ddc5dc5d 100755
--- a/test/pnminvert-roundtrip.test
+++ b/test/pnminvert-roundtrip.test
@@ -4,4 +4,4 @@
 
 
 pnminvert testimg.ppm | pnminvert | cksum
-pnminvert testgrid.pbm | pnminvert | cksum
+pnminvert maze.pbm | pnminvert | cksum
diff --git a/test/pnminvert.ok b/test/pnminvert.ok
index 6cf5f011..d35b30b9 100644
--- a/test/pnminvert.ok
+++ b/test/pnminvert.ok
@@ -1,7 +1,12 @@
-1240379484 41
+Test 1.  Should print 1739442872 481
+1739442872 481
+Test 2.  Should print 1416115901 101484
 1416115901 101484
+Test 3.  Should print 1174803406 33838
 1174803406 33838
+Test 4.  Should print 2595564405 14 three times
 2595564405 14
 2595564405 14
 2595564405 14
+Test 7.  Should print 2896726098 15
 2896726098 15
diff --git a/test/pnminvert.test b/test/pnminvert.test
index b80716ef..68a4e066 100755
--- a/test/pnminvert.test
+++ b/test/pnminvert.test
@@ -2,25 +2,20 @@
 # This script tests: pnminvert
 # Also requires: pbmmake pamchannel pamtopnm
 
+echo "Test 1.  Should print 1739442872 481"
+pnminvert maze.pbm | cksum
 
-# Test 1.  Should print 1240379484 41
-pnminvert testgrid.pbm | cksum
-
-# Test 2.  Should print 1416115901 101484
+echo "Test 2.  Should print 1416115901 101484"
 pnminvert testimg.ppm | cksum
 
-# Test 3.  Should print 1174803406 33838
+echo "Test 3.  Should print 1174803406 33838"
 pamchannel -infile=testimg.ppm -tupletype="GRAYSCALE" 0 | pamtopnm | \
   pnminvert | cksum
 
-# Test 4.  Should print 2595564405 14
+echo "Test 4.  Should print 2595564405 14 three times"
 pbmmake -w 7 7 | pnminvert | cksum
-
-# Test 5.  Should print 2595564405 14
 pbmmake -b 7 7 | cksum
-
-# Test 6.  Should print 2595564405 14
 pbmmake -b 7 7 | pnminvert | pnminvert | cksum
 
-# Test 7.  Should print 2896726098 15
+echo "Test 7.  Should print 2896726098 15"
 pbmmake -g 8 8 | pnminvert | cksum
diff --git a/test/pnmpsnr.test b/test/pnmpsnr.test
index bdd67883..2de7fc95 100755
--- a/test/pnmpsnr.test
+++ b/test/pnmpsnr.test
@@ -38,21 +38,21 @@ echo "-----------------------------------------------------------" 1>&2
 pnmpsnr ${b_pbm} ${w_pbm} ${b_pbm}     > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmpsnr ${b_pbm}                       > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmpsnr ${b_pbm} ${w_pbm} -target1=100 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmpsnr                   -machine     > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 rm ${b_pbm} ${w_pbm}
diff --git a/test/pnmquant.test b/test/pnmquant.test
index 07830a3a..24cee077 100755
--- a/test/pnmquant.test
+++ b/test/pnmquant.test
@@ -44,39 +44,39 @@ test_out=${tmpdir}/test_out
 pnmquant 0 testimg.ppm   > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmquant testimg.ppm  > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmquant 10.5 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmquant -center -meancolor 16 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmquant -center -meanpixel 16 testimg.ppm    > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmquant -meancolor -meanpixel 16 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmquant -spreadbrightness -spreadluminosity 16 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmquant -randomseed 1 -norandom 10 testimg.ppm > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pnmremap1.test b/test/pnmremap1.test
index c2b9a6a6..b1cdd6d9 100755
--- a/test/pnmremap1.test
+++ b/test/pnmremap1.test
@@ -35,30 +35,30 @@ test_out=${tmpdir}/test_out
 pnmremap -mapfile=/dev/null testimg.ppm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmremap -mapfile=/dev/zero testimg.ppm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmremap testimg.ppm                    > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmremap -fs -nofs testimg.ppm          > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmremap -mapfile=testgrid.pbm -missingcolor=rgb:00/ff/00 testimg.ppm \
  > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmremap -mapfile=testgrid.pbm -firstisdefault testimg.ppm > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/pnmtile.test b/test/pnmtile.test
index 80378432..b9597467 100755
--- a/test/pnmtile.test
+++ b/test/pnmtile.test
@@ -31,19 +31,19 @@ echo "-----------------------------------------------------------" 1>&2
 pnmtile 100 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmtile 100 0 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmtile 0 100 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 pnmtile 100 100 100 testgrid.pbm > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/ppmbrighten.test b/test/ppmbrighten.test
index 40fe08f6..29ec78fb 100755
--- a/test/ppmbrighten.test
+++ b/test/ppmbrighten.test
@@ -26,6 +26,7 @@ echo "-----------------------------------------------------------" 1>&2
 
 echo "Test Error: Should print 1 four times"
 
+tmpdir=${tmpdir:-/tmp}
 output_ppm=${tmpdir}/output.ppm
 
 pambrighten -v -120 testimg.ppm > ${output_ppm}
@@ -36,3 +37,5 @@ pambrighten -s -120 testimg.ppm > ${output_ppm}
 echo $?
 test -s ${output_ppm}
 echo $?
+
+rm ${output_ppm}
diff --git a/test/ppmchange-roundtrip.ok b/test/ppmchange-roundtrip.ok
index a676a1f2..4fb7d428 100644
--- a/test/ppmchange-roundtrip.ok
+++ b/test/ppmchange-roundtrip.ok
@@ -1,2 +1,2 @@
-2425386270 41
-2425386270 41
+281226646 481
+281226646 481
diff --git a/test/ppmchange-roundtrip.test b/test/ppmchange-roundtrip.test
index 78f5b548..c1a35078 100755
--- a/test/ppmchange-roundtrip.test
+++ b/test/ppmchange-roundtrip.test
@@ -3,10 +3,10 @@
 # Also requires: pgmtopbm pnminvert ppmtopgm
 
 
-ppmchange black white white black testgrid.pbm | \
+ppmchange black white white black maze.pbm | \
 pnminvert | ppmtopgm | \
 pgmtopbm -th -val=0.5 | cksum
 
-ppmchange  black white white black testgrid.pbm | \
+ppmchange  black white white black maze.pbm | \
 ppmchange  black white white black | \
 ppmtopgm | pgmtopbm -th -val=0.5 | cksum
diff --git a/test/ppmforge-parameters.test b/test/ppmforge-parameters.test
index 342b8242..efc846a8 100755
--- a/test/ppmforge-parameters.test
+++ b/test/ppmforge-parameters.test
@@ -32,34 +32,34 @@ echo "-----------------------------------------------------------" 1>&2
 ppmforge -night  -dimension=0  > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmforge  -dimension=10  > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmforge  -dimension=-1  > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmforge -clouds -mesh=1.99    > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmforge -clouds -power=0      > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmforge         -ice=-1       > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmforge         -glaciers=-1  > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/ppmhist.test b/test/ppmhist.test
index a89c95bb..b5812503 100755
--- a/test/ppmhist.test
+++ b/test/ppmhist.test
@@ -38,14 +38,14 @@ echo "-----------------------------------------------------------" 1>&2
 ppmhist -hexcolor -float testimg.ppm > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmhist -hexcolor -map   testimg.ppm > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmhist -float    -map   testimg.ppm > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/ppmmake.test b/test/ppmmake.test
index 61f417ca..26b592f7 100755
--- a/test/ppmmake.test
+++ b/test/ppmmake.test
@@ -21,49 +21,49 @@ echo "-----------------------------------------------------------" 1>&2
 ppmmake rgb:gg/00/00  2 2  > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake rgb:ff/ff/00  2    > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake rgbi:1.1/0/0  2 2  > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake rgbi:1.0/.5   2 2  > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake rainbow       2 2  > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake               2 2  > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake blue -maxval=0 2 2  > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake blue -maxval=-1 2 2  > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmmake blue -maxval=65536 2 2  > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 RGBDEF=/dev/null ppmmake red 2 2 > ${test_out} || \
   echo -n "Expected failure 10"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/ppmpat.ok b/test/ppmpat.ok
index 4a9cebd9..f2b355a9 100644
--- a/test/ppmpat.ok
+++ b/test/ppmpat.ok
@@ -1,14 +1,22 @@
-Test 1
+Test 1. Should print: 4008533639 781
 4008533639 781
-Test 2
+Test 2. Should print: 738714211 781
+738714211 781
+Test 3. Should print: 3805937800 9613
 3805937800 9613
-Test 3
+Test 4. Should print: 1586690955 9613
+1586690955 9613
+Test 5. Should print: 2698433077 1549
 2698433077 1549
-Test 4
+Test 6. Should print: 1526189097 1549
+1526189097 1549
+Test 7. Should print: 3705929501 781
 3705929501 781
-Test 5
+Test 8. Should print: 1996587666 781
+1996587666 781
+Test 9. Should print: 3057513592 661
 3057513592 661
-Test 6
+Test 10. Should print: 1861389287 661
 1861389287 661
 Test Invalid
 Expected failure 1 1
diff --git a/test/ppmpat.test b/test/ppmpat.test
index 88fb9fdd..6e916944 100755
--- a/test/ppmpat.test
+++ b/test/ppmpat.test
@@ -2,31 +2,43 @@
 # This script tests: ppmpat
 # Also requires:
 
-# Test 1. Should print: 4008533639 781
-echo "Test 1"
-ppmpat -g2 --color=rgb:32/0d/b7,rgb:31/58/a3 16 16 | cksum
+echo "Test 1. Should print: 4008533639 781"
+ppmpat -gingham2 \
+       -color=rgb:32/0d/b7,rgb:31/58/a3 16 16 | cksum
 
-# Test 2. Should print: 3805937800 9613
-echo "Test 2"
-ppmpat -g3 --color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 64 50 | cksum
+echo "Test 2. Should print: 738714211 781"
+ppmpat -g2 -mesh \
+       --color=rgb:32/0d/b7,rgb:31/58/a3 16 16 | cksum
 
-# Test 3. Should print: 2698433077 1549
-echo "Test 3"
-ppmpat -madras --color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 32 16 | cksum
+echo "Test 3. Should print: 3805937800 9613"
+ppmpat -gingham3 \
+       -color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 64 50 | cksum
 
-# Test 4. Should print: 3705929501 781
-echo "Test 4"
+echo "Test 4. Should print: 1586690955 9613"
+ppmpat -g3 -mesh \
+       -color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 64 50 | cksum
+
+echo "Test 5. Should print: 2698433077 1549"
+ppmpat -madras \
+       --color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 32 16 | cksum
+
+echo "Test 6. Should print: 1526189097 1549"
+ppmpat -madras -mesh \
+       --color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 32 16 | cksum
+
+echo "Test 7. Should print: 3705929501 781"
 ppmpat -tartan --color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 16 16 | cksum
 
-# Test 5. Should print: 3057513592 661
-echo "Test 5"
+echo "Test 8. Should print: 1996587666 781"
+ppmpat -tartan -mesh \
+       --color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 16 16 | cksum
+
+echo "Test 9. Should print: 3057513592 661"
 ppmpat -argyle1 --color=rgb:ff/ff/ff,rgb:ff/0/0 12 18 | cksum
 
-# Test 6. Should print: 1861389287 661
-echo "Test 6"
+echo "Test 10. Should print: 1861389287 661"
 ppmpat -argyle2 --color=rgb:00/00/00,rgb:ff/80/80,rgb:e0/e0/e0 12 18 | cksum
 
-# Test 7.
 echo "Test Invalid"
 
 tmpdir=${tmpdir:-/tmp}
@@ -40,47 +52,47 @@ echo "-----------------------------------------------------------" 1>&2
 ppmpat -g2 -g3 10 10 > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -madras -tartan 10 10 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -poles -squig 10 10 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -camo -anticamo 10 10 > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -argyle1 -argyle2 10 10 > ${test_out} || \
   echo -n "Expected failure 5"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat 10 10 > ${test_out} || \
   echo -n "Expected failure 6"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -g2 10 > ${test_out} || \
   echo -n "Expected failure 7"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -g2 10 10 10 > ${test_out} || \
   echo -n "Expected failure 8"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -g2 10 > ${test_out} || \
   echo -n "Expected failure 9"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 clist1="-color=rgb:00/00/00"
 clist2="-color=rgb:00/00/00,rgb:00/00/ff"
@@ -91,87 +103,87 @@ clist4="-color=rgb:00/00/00,rgb:00/00/ff,rgb:00/ff/ff,rgb:ff/ff/ff"
 ppmpat -gingham2 ${clist1} 10 10 > ${test_out} || \
   echo -n "Expected failure 10"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -argyle1  ${clist1} 10 10 > ${test_out} || \
   echo -n "Expected failure 11"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -gingham2 ${clist3} 10 10 > ${test_out} || \
   echo -n "Expected failure 12"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -argyle1  ${clist3} 10 10 > ${test_out} || \
   echo -n "Expected failure 13"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # These require exactly 3 colors
 ppmpat -gingham3 ${clist2} 10 10 > ${test_out} || \
   echo -n "Expected failure 14"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -argyle2  ${clist2} 10 10 > ${test_out} || \
   echo -n "Expected failure 15"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -madras   ${clist2} 10 10 > ${test_out} || \
   echo -n "Expected failure 16"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -tartan   ${clist2} 10 10 > ${test_out} || \
   echo -n "Expected failure 17"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -gingham3 ${clist4} 10 10 > ${test_out} || \
   echo -n "Expected failure 18"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -argyle2  ${clist4} 10 10 > ${test_out} || \
   echo -n "Expected failure 19"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -madras   ${clist4} 10 10 > ${test_out} || \
   echo -n "Expected failure 20"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -tartan   ${clist4} 10 10 > ${test_out} || \
   echo -n "Expected failure 21"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # These require at least 3 colors
 ppmpat -squig    ${clist2} 10 10 > ${test_out} || \
   echo -n "Expected failure 22"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -camo     ${clist2} 10 10 > ${test_out} || \
   echo -n "Expected failure 23"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -anticamo ${clist2} 10 10 > ${test_out} || \
   echo -n "Expected failure 24"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 # The squig pattern has an aspect ratio restriction
 ppmpat -squig ${clist3} 10 250  > ${test_out} || \
   echo -n "Expected failure 25"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmpat -squig ${clist3} 500 20  > ${test_out} || \
   echo -n "Expected failure 26"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
diff --git a/test/ppmtoarbtxt-roundtrip.test b/test/ppmtoarbtxt-roundtrip.test
index dff78250..7c64114e 100755
--- a/test/ppmtoarbtxt-roundtrip.test
+++ b/test/ppmtoarbtxt-roundtrip.test
@@ -30,5 +30,4 @@ ppmtoarbtxt ${bodyskl} -hd ${headskl} testimg.ppm | tee ${inv_ppm} |
 
 cat ${inv_ppm} | ppmtoarbtxt ${bodyskl} -hd ${headskl} | pamtopnm | cksum
 
-
-rm ${bodyskl} ${headskl}
+rm ${inv_ppm} ${bodyskl} ${headskl}
diff --git a/test/ppmtoppm.ok b/test/ppmtoppm.ok
index 764a3464..2b4ad9f8 100644
--- a/test/ppmtoppm.ok
+++ b/test/ppmtoppm.ok
@@ -1 +1,23 @@
-829921912 685
+Test 1.  Should print a simple 4 x 3 pattern
+P3
+4 3
+255
+255 255 255  0 0 0  255 255 255  0 0 0
+0 0 0  255 255 255  0 0 0  255 255 255
+255 255 255  0 0 0  255 255 255  0 0 0
+Test 2.  Should print a simple 6 x 8 pattern
+P3
+6 8
+5
+0 0 0  0 0 0  0 0 0  1 1 1  1 1 1  2 2 2
+0 0 0  0 0 0  1 1 1  1 1 1  2 2 2  2 2 2
+0 0 0  1 1 1  1 1 1  2 2 2  2 2 2  2 2 2
+1 1 1  1 1 1  2 2 2  2 2 2  2 2 2  3 3 3
+1 1 1  2 2 2  2 2 2  2 2 2  3 3 3  3 3 3
+2 2 2  2 2 2  2 2 2  3 3 3  3 3 3  4 4 4
+2 2 2  2 2 2  3 3 3  3 3 3  4 4 4  4 4 4
+2 2 2  3 3 3  3 3 3  4 4 4  4 4 4  5 5 5
+Test 3.  Should print 669206373 10102
+669206373 10102
+Test 4.  Should print 1926073387 101484
+1926073387 101484
diff --git a/test/ppmtoppm.test b/test/ppmtoppm.test
index 797c733d..411c17f6 100755
--- a/test/ppmtoppm.test
+++ b/test/ppmtoppm.test
@@ -1,6 +1,15 @@
 #! /bin/bash
 # This script tests: ppmtoppm
-# Also requires:
+# Also requires: pbmmake pgmramp
 
+echo "Test 1.  Should print a simple 4 x 3 pattern"
+pbmmake -g 4 3 | ppmtoppm -plain
 
-ppmtoppm < testgrid.pbm | cksum
+echo "Test 2.  Should print a simple 6 x 8 pattern"
+pgmramp -diagonal -maxval=5 6 8 | ppmtoppm -plain
+
+echo "Test 3.  Should print 669206373 10102"
+ppmtoppm < maze.pbm | cksum
+
+echo "Test 4.  Should print 1926073387 101484"
+ppmtoppm < testimg.ppm | cksum
\ No newline at end of file
diff --git a/test/ppmwheel.ok b/test/ppmwheel.ok
index 0b0a7746..790a19cb 100644
--- a/test/ppmwheel.ok
+++ b/test/ppmwheel.ok
@@ -6,3 +6,4 @@ Expected failure 1 1
 Expected failure 2 1
 Expected failure 3 1
 Expected failure 4 1
+Expected failure 5 1
diff --git a/test/ppmwheel.test b/test/ppmwheel.test
index f2fa55d9..150a75e5 100755
--- a/test/ppmwheel.test
+++ b/test/ppmwheel.test
@@ -124,19 +124,24 @@ echo "-----------------------------------------------------------" 1>&2
 ppmwheel 10 -huevalue -huesaturation > ${test_out} || \
   echo -n "Expected failure 1"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
 ppmwheel 0 > ${test_out} || \
   echo -n "Expected failure 2"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
-ppmwheel > ${test_out} || \
+ppmwheel 3 > ${test_out} || \
   echo -n "Expected failure 3"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
 
-ppmwheel 10 10 > ${test_out} || \
+ppmwheel > ${test_out} || \
   echo -n "Expected failure 4"
   test -s ${test_out}; echo " "$?
-  rm -f {test_out}
+  rm -f ${test_out}
+
+ppmwheel 10 10 > ${test_out} || \
+  echo -n "Expected failure 5"
+  test -s ${test_out}; echo " "$?
+  rm -f ${test_out}
diff --git a/test/ps-alt-roundtrip.ok b/test/ps-alt-roundtrip.ok
index 2deba38d..a23005f9 100644
--- a/test/ps-alt-roundtrip.ok
+++ b/test/ps-alt-roundtrip.ok
@@ -1,2 +1,4 @@
+Test 1.  Should print: 281226646 481
 2425386270 41
-2916080186 235
+Test 2.  Should print: 2154092425 3374
+2154092425 3374
diff --git a/test/ps-alt-roundtrip.test b/test/ps-alt-roundtrip.test
index 8fc08f2b..07081d3a 100755
--- a/test/ps-alt-roundtrip.test
+++ b/test/ps-alt-roundtrip.test
@@ -24,7 +24,9 @@
 # pstopnm does not use libnetpbm functions for output.
 # Output is filtered through at least one Netpbm program.
 
-# Test 1. Should print: 2425386270 41
+echo "Test 1.  Should print: 281226646 481"
+
+tmpdir=${tmpdir:-/tmp}
 testgrid1_ps=${tmpdir}/testgrid1.ps
 
 pbmtopsg3 -dpi=72 testgrid.pbm \
@@ -36,13 +38,12 @@ pstopnm -xborder=0 -yborder=0 -llx=0 -lly=-16 -urx=14 \
 rm ${testgrid1_ps}
 
 
-
-# Test 2. Should print: 2916080186 235
+echo "Test 2.  Should print: 2154092425 3374"
 # Output is pgm maxval=1 with black and white inverted.
-#
+
 testgrid_epsi=${tmpdir}/testgrid.epsi
 
-pbmtoepsi testgrid.pbm > ${testgrid_epsi} && \
+pbmtoepsi maze.pbm > ${testgrid_epsi} && \
 xysizebps=`awk  '/BeginPreview/ {print $2,$3,$4}' \
     ${testgrid_epsi}` && \
 awk '/^%%BeginPreview:/ { p=1; next } /^%%EndImage/ { p=0; next } \
diff --git a/test/ps-flate-roundtrip.ok b/test/ps-flate-roundtrip.ok
index 57fb124f..0fdf23f2 100644
--- a/test/ps-flate-roundtrip.ok
+++ b/test/ps-flate-roundtrip.ok
@@ -1,3 +1,5 @@
+Test 1.  Should print: 1926073387 101484 twice
 1926073387 101484
 1926073387 101484
-1386192571 507420
+Test 2. Should print 1831481505 608904
+1831481505 608904
diff --git a/test/ps-flate-roundtrip.test b/test/ps-flate-roundtrip.test
index 08d1500c..888f2856 100755
--- a/test/ps-flate-roundtrip.test
+++ b/test/ps-flate-roundtrip.test
@@ -17,7 +17,7 @@ tmpdir=${tmpdir:-/tmp}
 # pstopnm does not use libnetpbm functions for output.
 # Output is filtered through pamtopnm.
 
-# Test 1.  Should print: 1926073387 101484 twice
+echo "Test 1.  Should print: 1926073387 101484 twice"
 test1_ps=${tmpdir}/testimg1.ps
 
 for flag in "-ps -flate" "-ps -rle -ascii -flate"
@@ -32,16 +32,17 @@ for flag in "-ps -flate" "-ps -rle -ascii -flate"
 
 rm ${test1_ps}
 
-# Test 2. Should print: 1386192571 507420
+echo "Test 2. Should print 1831481505 608904"
 # See comments in ps-roundtrip.test
 
 test3_ps=${tmpdir}/testimg3.ps
 flag="-ps -bitspersample=12 -flate -rle -vmreclaim"
-cat testimg.ppm testimg.ppm testimg.ppm testgrid.pbm testgrid.pbm | \
-pnmtops -nocenter -equalpixels -dpi 72 -noturn -setpage ${flag} \
-  > ${test3_ps} &&
+cat testimg.ppm testimg.ppm testimg.ppm maze.pbm \
+  testgrid.pbm testgrid.pbm | \
+  pnmtops -nocenter -equalpixels -dpi 72 -noturn -setpage ${flag} \
+    > ${test3_ps} &&
 xysize3=`awk  '/BoundingBox/ {print "-xsize="$4,"-ysize="$5 ; exit}' \
-  ${test3_ps}` &&
+    ${test3_ps}` &&
 pstopnm -portrait -xborder=0 -yborder=0 $xysize3 -stdout  ${test3_ps} | \
   pamtopnm | cksum
 
diff --git a/test/ps-roundtrip.ok b/test/ps-roundtrip.ok
index 5ef66cc4..1a69224d 100644
--- a/test/ps-roundtrip.ok
+++ b/test/ps-roundtrip.ok
@@ -1,7 +1,9 @@
+Test 1.  Should print 1926073387 101484 four times
 1926073387 101484
 1926073387 101484
 1926073387 101484
 1926073387 101484
+Test 2.  Should print 2918318199 62 seven times
 2918318199 62
 2918318199 62
 2918318199 62
@@ -9,6 +11,7 @@
 2918318199 62
 2918318199 62
 2918318199 62
-1386192571 507420
-1386192571 507420
-1386192571 507420
+Test 3. Should print 1831481505 608904 three times
+1831481505 608904
+1831481505 608904
+1831481505 608904
diff --git a/test/ps-roundtrip.test b/test/ps-roundtrip.test
index 207646cc..efce4fd6 100755
--- a/test/ps-roundtrip.test
+++ b/test/ps-roundtrip.test
@@ -11,7 +11,7 @@ tmpdir=${tmpdir:-/tmp}
 # pstopnm does not use libnetpbm functions for output.
 # Output is filtered through pamtopnm.
 
-# Test 1.  Should print: 1926073387 101484 four times
+echo "Test 1.  Should print 1926073387 101484 four times"
 test1_ps=${tmpdir}/testimg1.ps
 
 for flag in "" "-ps" "-rle" "-ps -ascii"
@@ -25,9 +25,9 @@ for flag in "" "-ps" "-rle" "-ps -ascii"
   done
 
 rm ${test1_ps}
-# Test 2.  Should print: 2918318199 62 seven times
+
+echo "Test 2.  Should print 2918318199 62 seven times"
 # Test image designed to detect problems with run-length compression
-#
 
 g_pbm=${tmpdir}/g.pbm
 t_pbm=${tmpdir}/t.pbm
@@ -52,8 +52,8 @@ for flag in "" "-rle" "-ps -rle -ascii" \
 
 rm ${grid_ps} ${g_pbm} ${t_pbm}
 
-#Test 3. Should print: 1386192571 507420 three times
-#
+echo "Test 3. Should print 1831481505 608904 three times"
+
 # Special care is needed when conducting round-trips with multiple-image
 # files as input.
 # (1) pnmtops: -setpage is mandatory
@@ -61,16 +61,16 @@ rm ${grid_ps} ${g_pbm} ${t_pbm}
 #          Subsequent BoundingBox values are ignored.
 # (3) pstopnm: input must be an ordinary file.  Input from stdin
 #     (by pipe or input redirection: "< file" ) does not work.
-#
 
 test3_ps=${tmpdir}/testimg3.ps
 
 for flag in "" "-ps" \
             "-ps -bitspersample=12 -rle -vmreclaim"
   do
-cat testimg.ppm testimg.ppm testimg.ppm testgrid.pbm testgrid.pbm | \
-pnmtops -nocenter -equalpixels -dpi 72 -noturn -setpage ${flag} \
-  > ${test3_ps} &&
+cat testimg.ppm testimg.ppm testimg.ppm maze.pbm \
+  testgrid.pbm testgrid.pbm | \
+  pnmtops -nocenter -equalpixels -dpi 72 -noturn -setpage ${flag} \
+    > ${test3_ps} &&
 xysize3=`awk  '/BoundingBox/ {print "-xsize="$4,"-ysize="$5 ; exit}' \
   ${test3_ps}` &&
 pstopnm -portrait -xborder=0 -yborder=0 $xysize3 -stdout  ${test3_ps} | \
diff --git a/test/random-generator.test b/test/random-generator.test
index ef204e4b..23dc1903 100755
--- a/test/random-generator.test
+++ b/test/random-generator.test
@@ -55,7 +55,7 @@ perlPgmProcessorProgram='
   }
 '
 
-pgmnoise -randomseed=5489 -plain -maxval=65535 10 200 | tee /tmp/z |
+pgmnoise -randomseed=5489 -plain -maxval=65535 10 200 | \
   perl -walne "$perlPgmProcessorProgram"
 
 # The above outputs (10 * 200 / 2) = 1000 samples.
diff --git a/test/rgb3-roundtrip.ok b/test/rgb3-roundtrip.ok
index 367e5429..cbdb369c 100644
--- a/test/rgb3-roundtrip.ok
+++ b/test/rgb3-roundtrip.ok
@@ -1,6 +1,12 @@
+Test 1.  Should print 1926073387 101484
 1926073387 101484
+Test 2.  Should print 3744829044 101514
 3744829044 101514
-2425386270 41
+Test 3.  Should print 281226646 481 twice
+281226646 481
+281226646 481
+Test 4.  Should print 0 twice
 0
 0
+Test 5.  Should print 0 0 : 0
 0 0 : 0
diff --git a/test/rgb3-roundtrip.test b/test/rgb3-roundtrip.test
index 42fd822f..5edb1f19 100755
--- a/test/rgb3-roundtrip.test
+++ b/test/rgb3-roundtrip.test
@@ -10,6 +10,7 @@
 tmpdir=${tmpdir:-/tmp}
 
 # Test 1.  PPM (color) input
+echo "Test 1.  Should print 1926073387 101484"
 testimg_ppm=${tmpdir}/testimg.ppm
 testimg_red=${tmpdir}/testimg.red
 testimg_grn=${tmpdir}/testimg.grn
@@ -19,28 +20,35 @@ cp testimg.ppm ${tmpdir} &&
 ppmtorgb3 ${testimg_ppm} &&
 rgb3toppm ${testimg_red} ${testimg_grn} ${testimg_blu} | cksum
 
+# Simple cat of three planes
+echo "Test 2.  Should print 3744829044 101514"
 cat ${testimg_red} ${testimg_grn} ${testimg_blu} | cksum
 
 rm ${testimg_ppm} ${testimg_red} ${testimg_grn} ${testimg_blu}
 
-# Test 2.  PBM (monochrome) input
-testgrid_pbm=${tmpdir}/testgrid.pbm
-testgrid_red=${tmpdir}/testgrid.red
-testgrid_grn=${tmpdir}/testgrid.grn
-testgrid_blu=${tmpdir}/testgrid.blu
+# Test 3.  PBM (monochrome) input
+echo "Test 3.  Should print 281226646 481 twice"
+maze_pbm=${tmpdir}/maze.pbm
+maze_red=${tmpdir}/maze.red
+maze_grn=${tmpdir}/maze.grn
+maze_blu=${tmpdir}/maze.blu
 
-cp testgrid.pbm ${tmpdir} &&
-ppmtorgb3 ${testgrid_pbm} &&
-rgb3toppm ${testgrid_red} ${testgrid_grn} ${testgrid_blu} | \
+cp maze.pbm ${tmpdir} &&
+ppmtorgb3 ${maze_pbm} &&
+rgb3toppm ${maze_red} ${maze_grn} ${maze_blu} | \
   ppmtopgm | pgmtopbm -th -val=0.5 | cksum
+pgmtopbm -th -val=0.5 ${maze_red} | cksum
 
-# Test 3.
+# Test 4.
 # With PGM or PBM input, the three monochrome planes should be
 # identical.  Test for this.
 
-cmp -s ${testgrid_red} ${testgrid_grn} ; echo $?
-cmp -s ${testgrid_grn} ${testgrid_blu} ; echo $?
-pgmtopgm < testgrid.pbm | cmp -s - ${testgrid_red}
+echo "Test 4.  Should print 0 twice"
+cmp -s ${maze_red} ${maze_grn} ; echo $?
+cmp -s ${maze_grn} ${maze_blu} ; echo $?
+
+echo "Test 5.  Should print 0 0 : 0"
+pgmtopgm < maze.pbm | cmp -s - ${maze_red}
   echo ${PIPESTATUS[@]} ":" $?
 
-rm ${testgrid_pbm} ${testgrid_red} ${testgrid_grn} ${testgrid_blu}
+rm ${maze_pbm} ${maze_red} ${maze_grn} ${maze_blu}
diff --git a/test/sgi-roundtrip.ok b/test/sgi-roundtrip.ok
index b1bd5ba6..3c3f022a 100644
--- a/test/sgi-roundtrip.ok
+++ b/test/sgi-roundtrip.ok
@@ -1,8 +1,13 @@
+Test 1.  Should produce 1926073387 101484 twice
 1926073387 101484
 1926073387 101484
+Test 2.  Should produce 1926073387 101484 twice
 1926073387 101484
 1926073387 101484
-538848130 235
-538848130 235
-2394972481 463
-2394972481 463
+Test 3.  Should produce 281226646 481 twice
+281226646 481
+281226646 481
+Test 4. Should produce 4155890407 6741 three times
+4155890407 6741
+4155890407 6741
+4155890407 6741
diff --git a/test/sgi-roundtrip.test b/test/sgi-roundtrip.test
index 5052bb81..073755d8 100755
--- a/test/sgi-roundtrip.test
+++ b/test/sgi-roundtrip.test
@@ -1,14 +1,14 @@
 #! /bin/bash
 # This script tests: pnmtosgi sgitopnm
-# Also requires: rgb3toppm pamdepth pamtopnm
-
+# Also requires: rgb3toppm pamdepth pamtopnm pgmtopbm
 
+tmpdir=${tmpdir:-/tmp}
 a_sgi=${tmpdir}/a.sgi
 a_red=${tmpdir}/a.red
 a_grn=${tmpdir}/a.grn
 a_blu=${tmpdir}/a.blu
 
-# Test 1.  Should produce 1926073387 101484 twice
+echo "Test 1.  Should produce 1926073387 101484 twice"
 pnmtosgi -rle testimg.ppm | tee ${a_sgi} | sgitopnm  | cksum
 sgitopnm -channel=0  ${a_sgi} > ${a_red}
 sgitopnm -channel=1  ${a_sgi} > ${a_grn}
@@ -21,7 +21,7 @@ b_red=${tmpdir}/b.red
 b_grn=${tmpdir}/b.grn
 b_blu=${tmpdir}/b.blu
 
-# Test 2.  Should produce 1926073387 101484 twice
+echo "Test 2.  Should produce 1926073387 101484 twice"
 pnmtosgi -verbatim testimg.ppm | tee ${b_sgi} | sgitopnm  | cksum
 sgitopnm -channel=0  ${b_sgi} > ${b_red}
 sgitopnm -channel=1  ${b_sgi} > ${b_grn}
@@ -29,15 +29,16 @@ sgitopnm -channel=2  ${b_sgi} > ${b_blu}
 rgb3toppm ${b_red} ${b_grn} ${b_blu} | cksum
 rm ${b_sgi} ${b_red} ${b_grn} ${b_blu}
 
-# Test 3.  Should produce 2425386270 41 twice
-pnmtosgi testgrid.pbm | sgitopnm | cksum             # Defaults to -rle
-pnmtosgi -verbatim testgrid.pbm | sgitopnm | cksum
-
+echo "Test 3.  Should produce 281226646 481 twice"
+pnmtosgi maze.pbm | sgitopnm | pgmtopbm -threshold -value=0.5 | cksum 
+# Defaults to -rle
+pnmtosgi -verbatim maze.pbm | sgitopnm | \
+  pgmtopbm -threshold -value=0.5 | cksum 
 
-testgrid_pgm=${tmpdir}/testgrid.pgm
+maze_pgm=${tmpdir}/maze.pgm
 
-# Test 4. Should produce 2394972481 463 twice
-pamdepth 65535 testgrid.pbm | pamtopnm | tee ${testgrid_pgm} | \
-  pnmtosgi -rle | sgitopnm | cksum
-pnmtosgi -verbatim ${testgrid_pgm} | sgitopnm | cksum
-rm ${testgrid_pgm}
+echo "Test 4. Should produce 4155890407 6741 three times"
+pamdepth 65535 maze.pbm | pamtopnm | tee ${maze_pgm} | cksum
+pnmtosgi -rle ${maze_pgm} | sgitopnm | cksum
+pnmtosgi -verbatim ${maze_pgm} | sgitopnm | cksum
+rm ${maze_pgm}
diff --git a/test/sunicon-roundtrip.ok b/test/sunicon-roundtrip.ok
index 845be5fb..8bfdc3a0 100644
--- a/test/sunicon-roundtrip.ok
+++ b/test/sunicon-roundtrip.ok
@@ -1 +1,4 @@
-2425386270 41
+Test.  Should print 0 three times
+0
+0
+0
diff --git a/test/sunicon-roundtrip.test b/test/sunicon-roundtrip.test
index a52fda68..c5b85909 100755
--- a/test/sunicon-roundtrip.test
+++ b/test/sunicon-roundtrip.test
@@ -1,8 +1,27 @@
 #! /bin/bash
 # This script tests: pbmtosunicon sunicontopnm
-# Also requires: pamcut
+# Also requires: pamcut pnmpad
 
 # Width of Sun icons are multiples of 8.
 
-# Test.  Should print: 2425386270 41
-pbmtosunicon testgrid.pbm | sunicontopnm | pamcut 1 0 14 16 | cksum
+#imgsize=$(pamfile -size maze.pbm | awk '{print "-width="$1, "-height="$2}')
+
+tmpdir=${tmpdir:-/tmp}
+
+maze16_pbm=${tmpdir}/maze16.pbm
+maze32_pbm=${tmpdir}/maze32.pbm
+maze64_pbm=${tmpdir}/maze64.pbm
+
+pamcut -top=0 -left=0 -width=16 -height=16 maze.pbm > ${maze16_pbm} 
+pamcut -top=0 -left=0 -width=32 -height=32 maze.pbm > ${maze32_pbm} 
+pnmpad -width=64 -height=64 maze.pbm > ${maze64_pbm} 
+
+echo "Test.  Should print 0 three times"
+pbmtosunicon ${maze16_pbm} | sunicontopnm | cmp -s - ${maze16_pbm}
+echo $?
+pbmtosunicon ${maze32_pbm} | sunicontopnm | cmp -s - ${maze32_pbm}
+echo $?
+pbmtosunicon ${maze64_pbm} | sunicontopnm | cmp -s - ${maze64_pbm}
+echo $?
+
+rm ${maze16_pbm} ${maze32_pbm} ${maze64_pbm}
\ No newline at end of file
diff --git a/test/targa-roundtrip.ok b/test/targa-roundtrip.ok
index 514f970f..beb6700e 100644
--- a/test/targa-roundtrip.ok
+++ b/test/targa-roundtrip.ok
@@ -1,15 +1,15 @@
-Test 1
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-2425386270 41
-Test 2
+Test 1:  Should print 281226646 481 ten times
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+281226646 481
+Test 2:  Should produce 1571496937 33838 ten times
 1571496937 33838
 1571496937 33838
 1571496937 33838
@@ -20,19 +20,19 @@ Test 2
 1571496937 33838
 1571496937 33838
 1571496937 33838
-Test 3
+Test 3.  Should print 1926073387 101484 four times
 1926073387 101484
 1926073387 101484
 1926073387 101484
 1926073387 101484
-Test 4
+Test 4.  Should print 0 0 0 : 0 six times
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 : 0
-Test 5
+Test 4.  Should print 0 0 0 : 0 twice, then 0 0 0 0 : 0 six times
 0 0 0 : 0
 0 0 0 : 0
 0 0 0 0 : 0
diff --git a/test/targa-roundtrip.test b/test/targa-roundtrip.test
index 79b01b61..2dd04e91 100755
--- a/test/targa-roundtrip.test
+++ b/test/targa-roundtrip.test
@@ -4,25 +4,21 @@
 
 tmpdir=${tmpdir:-/tmp}
 
-#Test 1: Should print 2425386270 41, cksum of testgrid.pbm, ten times
-
-echo "Test 1"
+echo "Test 1:  Should print 281226646 481 ten times"
 
 for mode in "-mono" "-mono -norle" \
             "-cmap" "-cmap -norle" "-cmap16" "-cmap16 -norle" \
             "-rgb"  "-rgb  -norle" "" "-norle"
   do
-  pamtotga ${mode} testgrid.pbm | \
+  pamtotga ${mode} maze.pbm | \
     tgatoppm | ppmtopgm | \
     pgmtopbm -threshold -val 0.5 | cksum
   done
 
-#Test 2:  Should produce 1571496937 33838, cksum of testimg.red, ten times
-
-echo "Test 2"
+echo "Test 2:  Should produce 1571496937 33838 ten times"
 
 test_pam=${tmpdir}/testimg.pgm
-pamchannel -infile=testimg.ppm -tupletype="GRAYSCALE" 0 > ${test_pam}
+pamchannel -infile=testimg.ppm -tupletype="GRAYSCALE" 0 > ${test_pam} 
 
 for mode in "-mono" "-mono -norle" \
             "-cmap" "-cmap -norle" "-cmap16" "-cmap16 -norle" \
@@ -33,19 +29,14 @@ for mode in "-mono" "-mono -norle" \
 
 rm ${test_pam}
 
-
-#Test 3: Should print 1926073387 101484, cksum of testimg.ppm, four times
-
-echo "Test 3"
+echo "Test 3.  Should print 1926073387 101484 four times"
 
 for mode in "-rgb" "-rgb -norle" "" "-norle"
   do
   pamtotga ${mode} testimg.ppm | tgatoppm | cksum
   done
 
-#Test 4: Should print 0 six times
-
-echo "Test 4"
+echo "Test 4.  Should print 0 0 0 : 0 six times"
 
 test256_ppm=${tmpdir}/test256.ppm
 pnmquant 256 testimg.ppm > ${test256_ppm} || echo "pnmquant failed"
@@ -57,10 +48,7 @@ for mode in "-rgb" "-rgb -norle" "-cmap" "-cmap -norle" "" "-norle"
   echo ${PIPESTATUS[@]} ":" $?
 done
 
-
-#Test 5: Should print 0 eight times
-
-echo "Test 5"
+echo "Test 4.  Should print 0 0 0 : 0 twice, then 0 0 0 0 : 0 six times"
 
 test256_31_ppm=${tmpdir}/test256-31.ppm
 pamdepth 31 ${test256_ppm} > ${test256_31_ppm} || echo "pamdepth failed"
diff --git a/test/tiff-flate-lzw-roundtrip.ok b/test/tiff-flate-lzw-roundtrip.ok
index 4d5597cc..b3b0e1e7 100644
--- a/test/tiff-flate-lzw-roundtrip.ok
+++ b/test/tiff-flate-lzw-roundtrip.ok
@@ -1,7 +1,9 @@
+Test 1.  Should print 1079668603 10237 six times
 1079668603 10237
 1079668603 10237
 1079668603 10237
 1079668603 10237
 1079668603 10237
 1079668603 10237
-2425386270 41
+Test 2.  Should print 281226646 481
+281226646 481
diff --git a/test/tiff-flate-lzw-roundtrip.test b/test/tiff-flate-lzw-roundtrip.test
index 59d4c483..f62c6767 100755
--- a/test/tiff-flate-lzw-roundtrip.test
+++ b/test/tiff-flate-lzw-roundtrip.test
@@ -8,11 +8,11 @@
 ## features.
 
 tmpdir=${tmpdir:-/tmp}
-
 tartan_ppm=${tmpdir}/testimg40.ppm
 
 # Make a test image with reduced colors which compresses better
-# cksum is 1079668603 10237
+
+echo "Test 1.  Should print 1079668603 10237 six times"
 
 ppmpat -tartan --color=rgb:32/0d/b7,rgb:31/58/a3,rgb:e9/5e/d4 71 48 | \
   tee ${tartan_ppm} | cksum
@@ -28,7 +28,9 @@ pamtotiff -adobeflate ${tartan_ppm} | tifftopnm | cksum
 pamtotiff -lzw ${tartan_ppm} | tifftopnm | cksum
 pamtotiff -lzw -predictor=1 ${tartan_ppm} | tifftopnm | cksum
 
-# PBM image: test flate compression
-pamtotiff -flate testgrid.pbm | tifftopnm | cksum
-
 rm ${tartan_ppm}
+
+echo "Test 2.  Should print 281226646 481"
+
+# PBM image: test flate compression
+pamtotiff -flate maze.pbm | tifftopnm | cksum
diff --git a/test/tiff-roundtrip.ok b/test/tiff-roundtrip.ok
index 14258de2..dec662ed 100644
--- a/test/tiff-roundtrip.ok
+++ b/test/tiff-roundtrip.ok
@@ -1,36 +1,36 @@
-PPM-rose
+Test 1:PPM-rose  Should print 1926073387 101484 five times
 1926073387 101484
 1926073387 101484
 1926073387 101484
 1926073387 101484
 1926073387 101484
-PPM-6
+Test 2:PPM-6  Should print 1646869394 29498 four times
 1646869394 29498
 1646869394 29498
 1646869394 29498
 1646869394 29498
-PPM-2
+Test 3:PPM-2  Should print 3743909631 14836 four times
 3743909631 14836
 3743909631 14836
 3743909631 14836
 3743909631 14836
-PPM-gray
+Test 4:PPM-gray  Should print 603974118 4954 six times
 603974118 4954
 603974118 4954
 603974118 4954
 603974118 4954
 603974118 4954
 603974118 4954
-PPM-bilevel-black-white
+Test 5:PPM-bilevel-black-white  Should print 1001018459 4954 four times
 1001018459 4954
 1001018459 4954
 1001018459 4954
 1001018459 4954
-PBM
-2425386270 41
-2425386270 41
-2425386270 41
-Extract from multi-image TIFF
+Test 6:PBM  Should print 281226646 481 three times
+281226646 481
+281226646 481
+281226646 481
+Test 7:Extract from multi-image TIFF
 1926073387 101484
 1646869394 29498
 3743909631 14836
diff --git a/test/tiff-roundtrip.test b/test/tiff-roundtrip.test
index 55aba278..b4169319 100755
--- a/test/tiff-roundtrip.test
+++ b/test/tiff-roundtrip.test
@@ -13,9 +13,8 @@ argylegray_ppm=${tmpdir}/argyle2.ppm
 multiimage_tiff=${tmpdir}/multiimage.ppm
 
 # PPM image
-# Should print 1926073387 101484 five times
 
-echo PPM-rose
+echo "Test 1:PPM-rose  Should print 1926073387 101484 five times"
 pamtotiff testimg.ppm | tifftopnm -headerdump | cksum
 pamtotiff -truecolor testimg.ppm -output ${multiimage_tiff} && \
   tifftopnm ${multiimage_tiff} -byrow | cksum
@@ -25,9 +24,8 @@ pamtotiff -truecolor -lsb2msb  testimg.ppm | \
   tifftopnm -respectfillorder | cksum
 
 # PPM image with six colors
-# Should print 1646869394 29498 four times
 
-echo PPM-6
+echo "Test 2:PPM-6  Should print 1646869394 29498 four times"
 ppmpat -tartan -color=rgb:0/8/8,rgb:8/f/8,rgb:f/f/0 108 91 | \
   tee ${tartan_ppm} | cksum
 pamtotiff -truecolor ${tartan_ppm} | tifftopnm | cksum
@@ -37,12 +35,12 @@ pamtotiff ${tartan_ppm} -append -output ${multiimage_tiff} && \
 
 
 # PPM image with two colors
-# Should print 3743909631 14836 four times
 
-echo PPM-2
+echo "Test 3:PPM-2  Should print 3743909631 14836 four times"
 ppmpat -argyle2 -color=rgb:0/0/0,rgb:f/8/f,rgb:0/0/0 61 81 | \
   tee ${argylecolor_ppm} | cksum
-pamtotiff -indexbits=2 ${argylecolor_ppm} -append -output ${multiimage_tiff} && \
+pamtotiff -indexbits=2 ${argylecolor_ppm} -append \
+  -output ${multiimage_tiff} && \
   tifftopnm ${multiimage_tiff} | pampick 2 | cksum
 pamtotiff -indexbits=1 ${argylecolor_ppm} | tifftopnm | cksum
 pamtotiff -indexbits=1 -g4 ${argylecolor_ppm} | tifftopnm | cksum
@@ -51,9 +49,8 @@ pamtotiff -indexbits=1 -g4 ${argylecolor_ppm} | tifftopnm | cksum
 # pamtotiff detects the absence of colors and produces a grayscale
 # tiff image unless -color is specified
 # Pipeline output is PGM unless -color is specified
-# Should print 603974118 4954 six times
 
-echo PPM-gray
+echo "Test 4:PPM-gray  Should print 603974118 4954 six times"
 ppmpat -gingham3 -color=rgb:0/0/0,rgb:ff/ff/ff,rgb:0/0/0 61 81 | \
   tee ${gingham_ppm} | ppmtopgm | cksum
 pamtotiff -color ${gingham_ppm} | tifftopnm | cksum
@@ -67,9 +64,8 @@ pamtotiff -mb ${gingham_ppm} -append -output ${multiimage_tiff} && \
 # pamtotiff detects the absence of colors and produces a grayscale
 # tiff image unless -color is specified
 # Pipeline output is PGM unless -color is specified
-# Should print 1001018459 4954 four times
 
-echo PPM-bilevel-black-white
+echo "Test 5:PPM-bilevel-black-white  Should print 1001018459 4954 four times"
 ppmpat -argyle2 -color=rgb:0/0/0,rgb:ff/ff/ff,rgb:0/0/0 61 81 | \
   tee ${argylegray_ppm} | ppmtopgm | cksum
 pamtotiff -color -fill ${argylegray_ppm} | tifftopnm | cksum
@@ -78,17 +74,17 @@ pamtotiff -mb ${argylegray_ppm} -append -output ${multiimage_tiff} && \
   tifftopnm ${multiimage_tiff} | pampick 4 | cksum
 
 # PBM image
-# Should print 2425386270 41 three times
 
-echo PBM
-pamtotiff testgrid.pbm | tifftopnm | cksum
-pamtotiff -g3 -fill testgrid.pbm | tifftopnm | cksum
-pamtotiff -g4 -mb testgrid.pbm -append -output ${multiimage_tiff} && \
+
+echo "Test 6:PBM  Should print 281226646 481 three times"
+pamtotiff maze.pbm | tifftopnm | cksum
+pamtotiff -g3 -fill maze.pbm | tifftopnm | cksum
+pamtotiff -g4 -mb maze.pbm -append -output ${multiimage_tiff} && \
   tifftopnm ${multiimage_tiff} | pampick 5 | cksum
 
 # Extract individual images from multi-image tiff file
 
-echo "Extract from multi-image TIFF"
+echo "Test 7:Extract from multi-image TIFF"
 tifftopnm ${multiimage_tiff} | pampick 0 | cksum
 tifftopnm ${multiimage_tiff} | pampick 1 | cksum
 tifftopnm ${multiimage_tiff} | pampick 2 | cksum
diff --git a/test/wbmp-roundtrip.ok b/test/wbmp-roundtrip.ok
index 845be5fb..5168378d 100644
--- a/test/wbmp-roundtrip.ok
+++ b/test/wbmp-roundtrip.ok
@@ -1 +1 @@
-2425386270 41
+281226646 481
diff --git a/test/wbmp-roundtrip.test b/test/wbmp-roundtrip.test
index b651c3d0..6197032c 100755
--- a/test/wbmp-roundtrip.test
+++ b/test/wbmp-roundtrip.test
@@ -3,5 +3,5 @@
 # Also requires:
 
 
-# Should print 2425386270 41, cksum of testgrid.pbm
-pbmtowbmp testgrid.pbm | wbmptopbm | cksum
+# Should print 281226646 481, cksum of maze.pbm
+pbmtowbmp maze.pbm | wbmptopbm | cksum
diff --git a/test/winicon-roundtrip.ok b/test/winicon-roundtrip.ok
index 4ba2606e..73e5030c 100644
--- a/test/winicon-roundtrip.ok
+++ b/test/winicon-roundtrip.ok
@@ -3,10 +3,10 @@ Test 1. Should print 4241318573 6973 four times
 4241318573 6973
 4241318573 6973
 4241318573 6973
-Test 1. Should print 819454437 1093 six times
-819454437 1093
-819454437 1093
-819454437 1093
-819454437 1093
-819454437 1093
-819454437 1093
+Test 1. Should print 1009647580 1093 six times
+1009647580 1093
+1009647580 1093
+1009647580 1093
+1009647580 1093
+1009647580 1093
+1009647580 1093
diff --git a/test/winicon-roundtrip.test b/test/winicon-roundtrip.test
index 00c78400..b4814fee 100755
--- a/test/winicon-roundtrip.test
+++ b/test/winicon-roundtrip.test
@@ -1,6 +1,6 @@
 #! /bin/bash
 # This script tests: pamtowinicon ppmtowinicon winicontopam
-# Also requires: pamchannel pamcut pamdepth pamtopam pngtopam pnmtile pnmtopng
+# Also requires: pamchannel pamcut pamdepth pamtopam
 
 tmpdir=${tmpdir:-/tmp}
 test_pam=${tmpdir}/testimg.pam
@@ -18,9 +18,9 @@ pamtowinicon -pngthreshold=1   ${test_pam} | winicontopam | \
 
 rm ${test_pam}
 
-echo "Test 1. Should print 819454437 1093 six times"
+echo "Test 1. Should print 1009647580 1093 six times"
 
-pnmtile 32 32 testgrid.pbm | pamtopam | \
+pamcut -top=0 -left=0 -width=32 -height=32 maze.pbm | pamtopam | \
   tee ${test_pam} | cksum
 pamtowinicon ${test_pam} | winicontopam | \
   pamdepth 1 | pamchannel -tupletype="BLACKANDWHITE" 0 | cksum
@@ -33,5 +33,6 @@ pamtowinicon -pngthreshold=1   ${test_pam} | winicontopam |
 
 rm ${test_pam}
 
-pnmtile 32 32 testgrid.pbm | ppmtowinicon | winicontopam | \
+pamcut -top=0 -left=0 -width=32 -height=32 maze.pbm | \
+  ppmtowinicon | winicontopam | \
   pamdepth 1 | pamchannel -tupletype="BLACKANDWHITE" 0 | cksum
diff --git a/test/winicon-roundtrip2.test b/test/winicon-roundtrip2.test
index 9d55ebee..d1f29cc9 100755
--- a/test/winicon-roundtrip2.test
+++ b/test/winicon-roundtrip2.test
@@ -1,7 +1,6 @@
 #! /bin/bash
 # This script tests: pamtowinicon winicontopam
-# Also requires: pamchannel pamdepth pamstack pamtopam pbmmake pngtopam
-# Also requires: pnmtopng ppmpat
+# Also requires: pamchannel pamdepth pamstack pamtopam pbmmake ppmpat
 
 tmpdir=${tmpdir:-/tmp}
 test_pam=${tmpdir}/testimg.pam
diff --git a/test/xbm-roundtrip.ok b/test/xbm-roundtrip.ok
index a676a1f2..82c6b5d8 100644
--- a/test/xbm-roundtrip.ok
+++ b/test/xbm-roundtrip.ok
@@ -1,2 +1,3 @@
-2425386270 41
-2425386270 41
+Test.  Should print 281226646 481 twice
+281226646 481
+281226646 481
diff --git a/test/xbm-roundtrip.test b/test/xbm-roundtrip.test
index e84d6fc3..3f06e9a5 100755
--- a/test/xbm-roundtrip.test
+++ b/test/xbm-roundtrip.test
@@ -2,6 +2,7 @@
 # This script tests: pbmtoxbm xbmtopbm
 # Also requires:
 
+echo "Test.  Should print 281226646 481 twice"
 
-pbmtoxbm testgrid.pbm | xbmtopbm | cksum
-pbmtoxbm -x10 testgrid.pbm | xbmtopbm | cksum
+pbmtoxbm maze.pbm | xbmtopbm | cksum
+pbmtoxbm -x10 maze.pbm | xbmtopbm | cksum
diff --git a/test/xpm-roundtrip.ok b/test/xpm-roundtrip.ok
index 845be5fb..5168378d 100644
--- a/test/xpm-roundtrip.ok
+++ b/test/xpm-roundtrip.ok
@@ -1 +1 @@
-2425386270 41
+281226646 481
diff --git a/test/xpm-roundtrip.test b/test/xpm-roundtrip.test
index fd0253b1..61a5192d 100755
--- a/test/xpm-roundtrip.test
+++ b/test/xpm-roundtrip.test
@@ -5,5 +5,5 @@
 
 #ppmtoxpm -hexonly testimg.ppm | \
 # xpmtoppm  | cksum
-ppmtoxpm testgrid.pbm | xpmtoppm | \
+ppmtoxpm maze.pbm | xpmtoppm | \
   ppmtopgm | pgmtopbm -th -value=0.5 | cksum
diff --git a/test/xwd-roundtrip.ok b/test/xwd-roundtrip.ok
index 25d3d871..864d737a 100644
--- a/test/xwd-roundtrip.ok
+++ b/test/xwd-roundtrip.ok
@@ -1,3 +1,6 @@
+Test 1.  Should produce 1571496937 33838, cksum of testimg.red
 1571496937 33838
+Test 2.  Should produce 1926073387 101484
 1926073387 101484
-2425386270 41
+Test 3.  Should produce 281226646 481
+281226646 481
diff --git a/test/xwd-roundtrip.test b/test/xwd-roundtrip.test
index cd0d38ad..4dffbbb0 100755
--- a/test/xwd-roundtrip.test
+++ b/test/xwd-roundtrip.test
@@ -2,15 +2,14 @@
 # This script tests: pnmtoxwd xwdtopnm
 # Also requires: pamchannel pamtopnm pamdepth
 
-
-# Test 1.  Should produce 1571496937 33838, cksum of testimg.red
+echo "Test 1.  Should produce 1571496937 33838, cksum of testimg.red"
 pamchannel -infile=testimg.ppm -tupletype="GRAYSCALE" 0 | pamtopnm | \
   pnmtoxwd | xwdtopnm | pamdepth 255 | cksum
 
-# Test 2.  Should produce 1926073387 101484
-pnmtoxwd --quiet  testimg.ppm | \
+echo "Test 2.  Should produce 1926073387 101484"
+pnmtoxwd --quiet testimg.ppm | \
   xwdtopnm --quiet | pamdepth 255 | cksum
 
-# Test 3.  Should produce 2425386270 41
-pnmtoxwd --quiet  testgrid.pbm | \
+echo "Test 3.  Should produce 281226646 481"
+pnmtoxwd --quiet maze.pbm | \
   xwdtopnm | cksum
diff --git a/version.mk b/version.mk
index ead9f55d..ba898559 100644
--- a/version.mk
+++ b/version.mk
@@ -1,3 +1,3 @@
 NETPBM_MAJOR_RELEASE = 10
-NETPBM_MINOR_RELEASE = 96
-NETPBM_POINT_RELEASE = 6
+NETPBM_MINOR_RELEASE = 97
+NETPBM_POINT_RELEASE = 0