about summary refs log tree commit diff
path: root/converter
diff options
context:
space:
mode:
Diffstat (limited to 'converter')
-rw-r--r--converter/bmp.h133
-rw-r--r--converter/other/Makefile38
-rwxr-xr-xconverter/other/anytopnm14
-rw-r--r--converter/other/bmptopnm.c827
-rw-r--r--converter/other/cameratopam/camera.c48
-rw-r--r--converter/other/cameratopam/cameratopam.c1
-rw-r--r--converter/other/cameratopam/foveon.c2
-rw-r--r--converter/other/cameratopam/identify.c1
-rw-r--r--converter/other/cameratopam/ljpeg.c1
-rw-r--r--converter/other/fiasco/binerror.c5
-rw-r--r--converter/other/fiasco/binerror.h4
-rw-r--r--converter/other/fiasco/buttons.c4
-rw-r--r--converter/other/fiasco/buttons.h4
-rw-r--r--converter/other/fiasco/codec/approx.c1025
-rw-r--r--converter/other/fiasco/codec/approx.h4
-rw-r--r--converter/other/fiasco/codec/bintree.c4
-rw-r--r--converter/other/fiasco/codec/bintree.h4
-rw-r--r--converter/other/fiasco/codec/coder.c5
-rw-r--r--converter/other/fiasco/codec/coder.h4
-rw-r--r--converter/other/fiasco/codec/coeff.c4
-rw-r--r--converter/other/fiasco/codec/coeff.h4
-rw-r--r--converter/other/fiasco/codec/control.c4
-rw-r--r--converter/other/fiasco/codec/control.h4
-rw-r--r--converter/other/fiasco/codec/cwfa.h4
-rw-r--r--converter/other/fiasco/codec/decoder.c316
-rw-r--r--converter/other/fiasco/codec/decoder.h4
-rw-r--r--converter/other/fiasco/codec/dfiasco.c4
-rw-r--r--converter/other/fiasco/codec/dfiasco.h4
-rw-r--r--converter/other/fiasco/codec/domain-pool.c4
-rw-r--r--converter/other/fiasco/codec/domain-pool.h4
-rw-r--r--converter/other/fiasco/codec/ip.c4
-rw-r--r--converter/other/fiasco/codec/ip.h4
-rw-r--r--converter/other/fiasco/codec/motion.c4
-rw-r--r--converter/other/fiasco/codec/motion.h4
-rw-r--r--converter/other/fiasco/codec/mwfa.c4
-rw-r--r--converter/other/fiasco/codec/mwfa.h4
-rw-r--r--converter/other/fiasco/codec/options.c5
-rw-r--r--converter/other/fiasco/codec/options.h4
-rw-r--r--converter/other/fiasco/codec/prediction.c4
-rw-r--r--converter/other/fiasco/codec/prediction.h4
-rw-r--r--converter/other/fiasco/codec/subdivide.c4
-rw-r--r--converter/other/fiasco/codec/subdivide.h4
-rw-r--r--converter/other/fiasco/codec/tiling.c4
-rw-r--r--converter/other/fiasco/codec/tiling.h4
-rw-r--r--converter/other/fiasco/codec/wfa.h4
-rw-r--r--converter/other/fiasco/codec/wfalib.c5
-rw-r--r--converter/other/fiasco/codec/wfalib.h4
-rw-r--r--converter/other/fiasco/display.c4
-rw-r--r--converter/other/fiasco/display.h4
-rw-r--r--converter/other/fiasco/doc/README.LIB2
-rw-r--r--converter/other/fiasco/doc/fiasco_c_options_new.32
-rw-r--r--converter/other/fiasco/doc/fiasco_coder.32
-rw-r--r--converter/other/fiasco/doc/fiasco_d_options_new.32
-rw-r--r--converter/other/fiasco/doc/fiasco_decoder_new.32
-rw-r--r--converter/other/fiasco/doc/fiasco_get_error_message.32
-rw-r--r--converter/other/fiasco/doc/fiasco_image_new.32
-rw-r--r--converter/other/fiasco/doc/fiasco_options_new.32
-rw-r--r--converter/other/fiasco/doc/fiasco_renderer_new.32
-rw-r--r--converter/other/fiasco/doc/fiasco_set_verbosity.32
-rw-r--r--converter/other/fiasco/fiasco.h4
-rw-r--r--converter/other/fiasco/fiascotopnm.c7
-rw-r--r--converter/other/fiasco/input/basis.c4
-rw-r--r--converter/other/fiasco/input/basis.h4
-rw-r--r--converter/other/fiasco/input/matrices.c4
-rw-r--r--converter/other/fiasco/input/matrices.h4
-rw-r--r--converter/other/fiasco/input/mc.c4
-rw-r--r--converter/other/fiasco/input/mc.h4
-rw-r--r--converter/other/fiasco/input/nd.c4
-rw-r--r--converter/other/fiasco/input/nd.h4
-rw-r--r--converter/other/fiasco/input/read.c5
-rw-r--r--converter/other/fiasco/input/read.h4
-rw-r--r--converter/other/fiasco/input/tree.c4
-rw-r--r--converter/other/fiasco/input/tree.h4
-rw-r--r--converter/other/fiasco/input/weights.c4
-rw-r--r--converter/other/fiasco/input/weights.h4
-rw-r--r--converter/other/fiasco/lib/arith.c4
-rw-r--r--converter/other/fiasco/lib/arith.h4
-rw-r--r--converter/other/fiasco/lib/bit-io.c5
-rw-r--r--converter/other/fiasco/lib/bit-io.h4
-rw-r--r--converter/other/fiasco/lib/dither.c16
-rw-r--r--converter/other/fiasco/lib/dither.h4
-rw-r--r--converter/other/fiasco/lib/error.c403
-rw-r--r--converter/other/fiasco/lib/error.h4
-rw-r--r--converter/other/fiasco/lib/image.c4
-rw-r--r--converter/other/fiasco/lib/image.h4
-rw-r--r--converter/other/fiasco/lib/list.c4
-rw-r--r--converter/other/fiasco/lib/list.h4
-rw-r--r--converter/other/fiasco/lib/macros.h4
-rw-r--r--converter/other/fiasco/lib/misc.c4
-rw-r--r--converter/other/fiasco/lib/misc.h4
-rw-r--r--converter/other/fiasco/lib/rpf.c296
-rw-r--r--converter/other/fiasco/lib/rpf.h4
-rw-r--r--converter/other/fiasco/lib/types.h4
-rw-r--r--converter/other/fiasco/output/matrices.c4
-rw-r--r--converter/other/fiasco/output/matrices.h4
-rw-r--r--converter/other/fiasco/output/mc.c4
-rw-r--r--converter/other/fiasco/output/mc.h4
-rw-r--r--converter/other/fiasco/output/nd.c4
-rw-r--r--converter/other/fiasco/output/nd.h4
-rw-r--r--converter/other/fiasco/output/tree.c4
-rw-r--r--converter/other/fiasco/output/tree.h4
-rw-r--r--converter/other/fiasco/output/weights.c4
-rw-r--r--converter/other/fiasco/output/weights.h4
-rw-r--r--converter/other/fiasco/output/write.c4
-rw-r--r--converter/other/fiasco/output/write.h4
-rw-r--r--converter/other/fiasco/params.c18
-rw-r--r--converter/other/fiasco/params.h4
-rw-r--r--converter/other/fiasco/pnmtofiasco.c4
-rw-r--r--converter/other/fiasco/system.fiascorc4
-rw-r--r--converter/other/gemtopnm.c200
-rw-r--r--converter/other/giftopnm.c575
-rw-r--r--converter/other/ipdb.c5
-rw-r--r--converter/other/jbig/jbigtopnm.c394
-rw-r--r--converter/other/jbig/libjbig/jbig.c198
-rw-r--r--converter/other/jpeg2000/jpeg2ktopam.c108
-rw-r--r--converter/other/jpeg2000/libjasper/jp2/jp2_dec.c1032
-rw-r--r--converter/other/jpeg2000/libjasper/jpc/jpc_dec.c15
-rw-r--r--converter/other/jpeg2000/libjasper/jpc/jpc_enc.c494
-rw-r--r--converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c1557
-rw-r--r--converter/other/jpeg2000/libjasper/jpc/jpc_util.c26
-rw-r--r--converter/other/jpeg2000/pamtojpeg2k.c89
-rw-r--r--converter/other/jpegtopnm.c1
-rw-r--r--converter/other/pamtogif.c627
-rw-r--r--converter/other/pamtopdbimg.c152
-rw-r--r--converter/other/pamtopng.c396
-rw-r--r--converter/other/pamtopnm.c2
-rw-r--r--converter/other/pamtosvg/pxl-outline.c2
-rw-r--r--converter/other/pamtosvg/vector.c2
-rw-r--r--converter/other/pamtotga.c350
-rw-r--r--converter/other/pamtotiff.c210
-rw-r--r--converter/other/pamtouil.c1
-rw-r--r--converter/other/pamtowinicon.c18
-rw-r--r--converter/other/pclxl.h14
-rw-r--r--converter/other/pdbimgtopam.c33
-rw-r--r--converter/other/pgmtoppm.c1
-rw-r--r--converter/other/pngtopam.c98
-rw-r--r--converter/other/pngx.c72
-rw-r--r--converter/other/pngx.h17
-rw-r--r--converter/other/pnmtoddif.c3
-rw-r--r--converter/other/pnmtojpeg.c1
-rw-r--r--converter/other/pnmtopalm/palm.h30
-rw-r--r--converter/other/pnmtopalm/palmcolormap.c134
-rw-r--r--converter/other/pnmtopalm/palmcolormap.h25
-rw-r--r--converter/other/pnmtopalm/palmtopnm.c402
-rw-r--r--converter/other/pnmtopalm/pnmtopalm.c567
-rw-r--r--converter/other/pnmtopclxl.c126
-rwxr-xr-xconverter/other/pnmtoplainpnm2
-rw-r--r--converter/other/pnmtopng.c390
-rw-r--r--converter/other/pnmtops.c7
-rw-r--r--converter/other/pnmtosir.c153
-rw-r--r--converter/other/pstopnm.c285
-rw-r--r--converter/other/rletopnm.c13
-rw-r--r--converter/other/svgtopam.c254
-rw-r--r--converter/other/tiff.c1
-rw-r--r--converter/other/tifftopnm.c558
-rw-r--r--converter/other/winicontopam.c6
-rw-r--r--converter/other/xwdtopnm.c1
-rw-r--r--converter/pbm/Makefile19
-rw-r--r--converter/pbm/cmuwmtopbm.c7
-rw-r--r--converter/pbm/g3.h136
-rw-r--r--converter/pbm/g3prefab.c286
-rw-r--r--converter/pbm/g3prefab.h14
-rw-r--r--converter/pbm/g3topbm.c172
-rw-r--r--converter/pbm/g3ttable.c116
-rw-r--r--converter/pbm/g3ttable.h13
-rw-r--r--converter/pbm/pbmtoascii.c49
-rw-r--r--converter/pbm/pbmtoepson.c1
-rw-r--r--converter/pbm/pbmtog3.c729
-rw-r--r--converter/pbm/pbmtolj.c41
-rw-r--r--converter/pbm/pbmtoln03.c211
-rw-r--r--converter/pbm/pbmtomacp.c6
-rw-r--r--converter/pbm/pbmtomgr.c9
-rw-r--r--converter/pbm/pbmtonokia.c9
-rw-r--r--converter/pbm/pbmtopk.c1
-rw-r--r--converter/pbm/pbmtoppa/pbm.c273
-rw-r--r--converter/pbm/pbmtoppa/pbmtoppa.c1
-rw-r--r--converter/pbm/pbmtox10bm37
-rw-r--r--converter/pbm/pbmtoxbm.c1
-rw-r--r--converter/pgm/sbigtopgm.c1
-rw-r--r--converter/ppm/hpcdtoppm/Makefile4
-rwxr-xr-xconverter/ppm/hpcdtoppm/hpcdtoppm22
-rw-r--r--converter/ppm/ilbmtoppm.c809
-rw-r--r--converter/ppm/picttoppm.c369
-rw-r--r--converter/ppm/pjtoppm.c215
-rw-r--r--converter/ppm/ppmtoarbtxt.c52
-rw-r--r--converter/ppm/ppmtobmp.c447
-rw-r--r--converter/ppm/ppmtogif.c1
-rw-r--r--converter/ppm/ppmtoicr.c32
-rw-r--r--converter/ppm/ppmtomitsu.c16
-rw-r--r--converter/ppm/ppmtompeg/HISTORY2
-rw-r--r--converter/ppm/ppmtompeg/frame.c18
-rw-r--r--converter/ppm/ppmtompeg/frametype.c3
-rw-r--r--converter/ppm/ppmtompeg/gethostname.c1
-rw-r--r--converter/ppm/ppmtompeg/jrevdct.c44
-rw-r--r--converter/ppm/ppmtompeg/mfwddct.c31
-rw-r--r--converter/ppm/ppmtompeg/mpeg.c1
-rw-r--r--converter/ppm/ppmtompeg/parallel.c2
-rw-r--r--converter/ppm/ppmtompeg/param.c2
-rw-r--r--converter/ppm/ppmtompeg/ppmtompeg.c16
-rw-r--r--converter/ppm/ppmtompeg/rate.c46
-rw-r--r--converter/ppm/ppmtompeg/readframe.c1
-rw-r--r--converter/ppm/ppmtompeg/specifics.c242
-rw-r--r--converter/ppm/ppmtoxpm.c1
-rw-r--r--converter/ppm/sldtoppm.c220
-rw-r--r--converter/ppm/tgatoppm.c292
-rw-r--r--converter/ppm/winicontoppm.c1
-rw-r--r--converter/ppm/ximtoppm.c1
-rw-r--r--converter/ppm/xpmtoppm.c113
-rw-r--r--converter/ppm/xvminitoppm.c64
-rw-r--r--converter/ppm/yuvtoppm.c1
-rw-r--r--converter/tga.h13
211 files changed, 10224 insertions, 8141 deletions
diff --git a/converter/bmp.h b/converter/bmp.h
index 524bbf7e..78c469d1 100644
--- a/converter/bmp.h
+++ b/converter/bmp.h
@@ -83,7 +83,37 @@
 
 #include "pm.h"  /* For pm_error() */
 
-enum bmpClass {C_WIN=1, C_OS2=2};
+enum bmpClass {
+    BMP_C_OS2_1x,
+    BMP_C_OS2_2x,
+    BMP_C_WIN_V1,
+    BMP_C_WIN_V2,
+    BMP_C_WIN_V3,
+    BMP_C_WIN_V4,
+    BMP_C_WIN_V5
+};
+
+
+
+static __inline__ const char *
+BMPClassName(enum bmpClass const class) {
+
+    const char * name;
+
+    switch (class) {
+    case BMP_C_OS2_1x: name = "OS/2 (v1)";    break;
+    case BMP_C_OS2_2x: name = "OS/2 (v2)";    break;
+    case BMP_C_WIN_V1: name = "Windows (v1)"; break;
+    case BMP_C_WIN_V2: name = "Windows (v2)"; break;
+    case BMP_C_WIN_V3: name = "Windows (v3)"; break;
+    case BMP_C_WIN_V4: name = "Windows (v4)"; break;
+    case BMP_C_WIN_V5: name = "Windows (v5)"; break;
+    }
+
+  return name;
+}
+
+
 
 static char const er_internal[] = "%s: internal error!";
 
@@ -114,29 +144,100 @@ BMPCompTypeName(BMPCompType const compression) {
 
 
 static __inline__ unsigned int
-BMPlenfileheader(enum bmpClass const class) {
+BMPlenfileheader(void) {
+
+    return 14;
+}
+
+
+
+enum BMPinfoHeaderLen {
+/*----------------------------------------------------------------------------
+   BMPs come in various kinds, distinguished by the length of their
+   info header, which is the first field in that header.
+
+   These are those lengths.
+-----------------------------------------------------------------------------*/
+    BMP_HDRLEN_OS2_1x =  12,
+        /* BITMAPCOREHEADER; since Windows 2.0, OS/2 1.x */
+    BMP_HDRLEN_OS2_2x =  64,
+        /* not documented by Microsoft; since OS/2 2.x */
+    BMP_HDRLEN_WIN_V1 =  40,
+        /* BITMAPINFOHEADER; since Windows NT 3, Windows 3.x */
+    BMP_HDRLEN_WIN_V2 =  52,
+        /* not documented by Microsoft */
+    BMP_HDRLEN_WIN_V3 =  56,
+        /* not documented by Microsoft */
+    BMP_HDRLEN_WIN_V4 = 108,
+        /* BITMAPV4HEADER; since Windows NT 4, Windows 95 */
+    BMP_HDRLEN_WIN_V5 = 124
+        /* BITMAPV5HEADER; since Windows 2000, Windows 98 */
+};
+
+
+
+static __inline__ unsigned int
+BMPleninfoheader(enum bmpClass const class) {
 
     unsigned int retval;
 
     switch (class) {
-    case C_WIN: retval = 14; break;
-    case C_OS2: retval = 14; break;
+    case BMP_C_WIN_V1: retval = BMP_HDRLEN_WIN_V1; break;
+    case BMP_C_WIN_V2: retval = BMP_HDRLEN_WIN_V2; break;
+    case BMP_C_WIN_V3: retval = BMP_HDRLEN_WIN_V3; break;
+    case BMP_C_WIN_V4: retval = BMP_HDRLEN_WIN_V4; break;
+    case BMP_C_WIN_V5: retval = BMP_HDRLEN_WIN_V5; break;
+    case BMP_C_OS2_1x: retval = BMP_HDRLEN_OS2_1x; break;
+    case BMP_C_OS2_2x: retval = BMP_HDRLEN_OS2_2x; break;
     }
     return retval;
 }
 
 
 
+static __inline__ void
+BMPdetermineclass(unsigned int    const infoHdrLen,
+                  enum bmpClass * const classP,
+                  const char **   const errorP) {
+/*----------------------------------------------------------------------------
+   Determine the class of BMP, based on the fact that the info header is
+   'infoHdrLen' bytes long.
+-----------------------------------------------------------------------------*/
+    switch (infoHdrLen) {
+    case BMP_HDRLEN_OS2_1x: *errorP = NULL; *classP = BMP_C_OS2_1x; break;
+    case BMP_HDRLEN_OS2_2x: *errorP = NULL; *classP = BMP_C_OS2_2x; break;
+    case BMP_HDRLEN_WIN_V1: *errorP = NULL; *classP = BMP_C_WIN_V1; break;
+    case BMP_HDRLEN_WIN_V2: *errorP = NULL; *classP = BMP_C_WIN_V2; break;
+    case BMP_HDRLEN_WIN_V3: *errorP = NULL; *classP = BMP_C_WIN_V3; break;
+    case BMP_HDRLEN_WIN_V4: *errorP = NULL; *classP = BMP_C_WIN_V4; break;
+    case BMP_HDRLEN_WIN_V5: *errorP = NULL; *classP = BMP_C_WIN_V5; break;
+
+    default:
+        pm_asprintf(errorP, "Not one of the 7 lengths we recognize");
+    }
+}
+
+
+
 static __inline__ unsigned int
-BMPleninfoheader(enum bmpClass const class) {
+BMPlenrgb(enum bmpClass const class) {
 
-    unsigned int retval;
+    unsigned int lenrgb;
 
     switch (class) {
-    case C_WIN: retval = 40; break;
-    case C_OS2: retval = 12; break;
+    case BMP_C_OS2_1x:
+    case BMP_C_OS2_2x:
+        lenrgb = 3;
+        break;
+    case BMP_C_WIN_V1:
+    case BMP_C_WIN_V2:
+    case BMP_C_WIN_V3:
+    case BMP_C_WIN_V4:
+    case BMP_C_WIN_V5:
+        lenrgb = 4;
+        break;
     }
-    return retval;
+    return lenrgb;
 }
 
 
@@ -152,7 +253,6 @@ BMPlencolormap(enum bmpClass const class,
 
    'cmapsize' == 0 means there is no palette.
 -----------------------------------------------------------------------------*/
-    unsigned int lenrgb;
     unsigned int lencolormap;
 
     if (bitcount < 1)
@@ -160,15 +260,10 @@ BMPlencolormap(enum bmpClass const class,
     else if (bitcount > 8) 
         lencolormap = 0;
     else {
-        switch (class) {
-        case C_WIN: lenrgb = 4; break;
-        case C_OS2: lenrgb = 3; break;
-        }
-
-        if (!cmapsize) 
-            lencolormap = (1 << bitcount) * lenrgb;
+        if (cmapsize) 
+            lencolormap = cmapsize * BMPlenrgb(class);
         else 
-            lencolormap = cmapsize * lenrgb;
+            lencolormap = (1 << bitcount) * BMPlenrgb(class);
     }
     return lencolormap;
 }
@@ -231,7 +326,7 @@ BMPoffbits(enum bmpClass const class,
 /*----------------------------------------------------------------------------
   return the offset to the BMP image bits.
 -----------------------------------------------------------------------------*/
-    return BMPlenfileheader(class)
+    return BMPlenfileheader()
         + BMPleninfoheader(class)
         + BMPlencolormap(class, bitcount, cmapsize);
 }
diff --git a/converter/other/Makefile b/converter/other/Makefile
index 6a3d14ed..2be88781 100644
--- a/converter/other/Makefile
+++ b/converter/other/Makefile
@@ -7,12 +7,12 @@ VPATH=.:$(SRCDIR)/$(SUBDIR)
 
 include $(BUILDDIR)/config.mk
 
-TEST_PKGCONFIG_LIBXML2 = if pkg-config libxml-2.0; then echo exists; fi
+TEST_PKGCONFIG_LIBXML2 = if $(PKG_CONFIG) libxml-2.0; then echo exists; fi
 
 ifneq ($(shell $(TEST_PKGCONFIG_LIBXML2)),)
   # pkg-config libxml2 works on this system
-  XML2_LIBS = $(shell pkg-config libxml-2.0 --libs)
-  XML2_CFLAGS = $(shell pkg-config libxml-2.0 --cflags)
+  XML2_LIBS = $(shell $(PKG_CONFIG) libxml-2.0 --libs)
+  XML2_CFLAGS = $(shell $(PKG_CONFIG) libxml-2.0 --cflags)
 else
   ifeq ($(shell xml2-config --version),)
     XML2_LIBS=NONE
@@ -34,12 +34,12 @@ ifneq ($(TIFFLIB),NONE)
   endif
 endif
 
-TEST_PKGCONFIG_LIBPNG = if pkg-config libpng$(PNGVER); then echo exists; fi
+TEST_PKGCONFIG_LIBPNG = if $(PKG_CONFIG) libpng$(PNGVER); then echo exists; fi
 
 ifneq ($(shell $(TEST_PKGCONFIG_LIBPNG)),)
   # pkg-config libpng works on this system
   HAVE_PNGLIB = Y
-  EXTERN_INCLUDES += $(shell pkg-config libpng$(PNGVER) --cflags)
+  EXTERN_INCLUDES += $(shell $(PKG_CONFIG) libpng$(PNGVER) --cflags)
 else
   ifneq ($(shell libpng$(PNGVER)-config --version),)
     # No pkg-config, but we have libpng-config on this system
@@ -166,6 +166,8 @@ endif
 OBJECTS = $(BINARIES:%=%.o) $(EXTRA_OBJECTS)
 MERGE_OBJECTS = $(MERGEBINARIES:%=%.o2) $(EXTRA_OBJECTS)
 
+HAVE_MERGE_COMPAT=YES
+
 pnmtops.o pnmtops.o2: CFLAGS_TARGET=$(PNMTOPS_NOFLATE_OPT)
 
 SCRIPTS = anytopnm pnmtoplainpnm
@@ -192,7 +194,7 @@ tifftopnm pamtotiff pnmtotiffcmyk: \
 
 ifneq ($(shell $(TEST_PKGCONFIG_LIBPNG)),)
   # pkg-config libpng works on this system
-  PNGLIB_LIBOPTS = $(shell pkg-config libpng$(PNGVER) --libs)
+  PNGLIB_LIBOPTS = $(shell $(PKG_CONFIG) libpng$(PNGVER) --libs)
 else
   ifneq ($(shell libpng$(PNGVER)-config --version),)
     # No pkg-config, but we have libpng-config on this system
@@ -248,7 +250,7 @@ bmptopnm.o bmptopnm.o2: bmp.h
 
 pamtotga.o pamtotga.o2: tga.h
 
-install.bin: install.bin.local
+install.bin install.merge: install.bin.local
 .PHONY: install.bin.local
 install.bin.local: $(PKGDIR)/bin
 # Remember that $(SYMLINK) might just be a copy command.
@@ -296,3 +298,25 @@ ifeq ($(HAVE_PNGLIB),Y)
 	cd $(PKGDIR)/bin ; \
 	$(SYMLINK) pamtopng$(EXE) pamrgbatopng$(EXE)
 endif
+
+mergecomptrylist:
+	cat /dev/null >$@
+	echo "TRY(\"bmptoppm\",     main_bmptopnm);"     >>$@
+	echo "TRY(\"gemtopbm\",     main_gemtopnm);"     >>$@
+ifneq ($(JPEGLIB),NONE)
+	echo "TRY(\"ppmtojpeg\",    main_pnmtojpeg);"    >>$@
+endif
+	echo "TRY(\"ppmtouil\",     main_pamtouil);"     >>$@
+	echo "TRY(\"ppmtotga\",     main_pamtotga);"     >>$@
+	echo "TRY(\"pnmtopnm\",     main_pamtopnm);"     >>$@
+	echo "TRY(\"pnmtofits\",    main_pamtofits);"    >>$@
+ifneq ($(TIFF_PREREQ_MISSING),Y)
+	echo "TRY(\"pnmtotiff\",    main_pamtotiff);"    >>$@
+endif
+ifeq ($(HAVE_PNGLIB),Y)
+	echo "TRY(\"pngtopnm\",     main_pngtopam);"     >>$@
+endif
+	echo "TRY(\"icontopbm\",    main_sunicontopnm);" >>$@
+ifeq ($(HAVE_PNGLIB),Y)
+	echo "TRY(\"pamrgbatopng\", main_pamtopng);"     >>$@
+endif
diff --git a/converter/other/anytopnm b/converter/other/anytopnm
index 397faae5..e5de85cc 100755
--- a/converter/other/anytopnm
+++ b/converter/other/anytopnm
@@ -527,14 +527,18 @@ if [ $# -gt 1 ] ; then
          "input file name." 1>&2
     exit 1
 elif [ $# -eq 1 ] ; then
-    inputFile="$1"
+    if [ $1 = "--version" -o $1 = "-version" ] ; then
+        pamtopnm --version ; exit $?
+    else 
+        inputFile="$1"
+    fi
 else
-    inputFile="-"
+        inputFile="-"
+
 fi
 
-tempdir="${TMPDIR-/tmp}/anytopnm.$$"
-mkdir -m 0700 $tempdir || \
-  { echo "Could not create temporary file. Exiting."; exit 1;}
+tempdir=$(mktemp -d "${TMPDIR:-/tmp}/anytopnm.XXXXXXXX") ||
+    ( echo "Could not create temporary file. Exiting." 1>&2; exit 1; ) 
 trap 'rm -rf $tempdir' 0
 
 # Take out all spaces
diff --git a/converter/other/bmptopnm.c b/converter/other/bmptopnm.c
index bf4d10f8..3f66634b 100644
--- a/converter/other/bmptopnm.c
+++ b/converter/other/bmptopnm.c
@@ -1,7 +1,7 @@
 /*****************************************************************************
                                     bmptopnm.c
 ******************************************************************************
- 
+
  Bmptopnm - Converts from a Microsoft Windows or OS/2 .BMP file to a
  PBM, PGM, or PPM file.
 
@@ -9,7 +9,7 @@
  The name was changed in March 2002.
 
  Copyright (C) 1992 by David W. Sanderson.
- 
+
  Permission to use, copy, modify, and distribute this software and its
  documentation for any purpose and without fee is hereby granted,
  provided that the above copyright notice appear in all copies and
@@ -62,14 +62,14 @@ struct bitPosition {
 };
 
 struct pixelformat {
-    /* The format of a pixel representation from the raster.  i.e. which 
-       bits apply to red, green, blue, and transparency 
+    /* The format of a pixel representation from the raster.  i.e. which
+       bits apply to red, green, blue, and transparency
     */
     struct bitPosition red;
     struct bitPosition blu;
     struct bitPosition grn;
     struct bitPosition trn;
-    
+
     bool conventionalBgr;
         /* This means that the above bit positions are just the conventional
            BGR format -- one byte Blue, one byte Green, one byte Red,
@@ -80,6 +80,19 @@ struct pixelformat {
         */
 };
 
+typedef struct {
+    /* These are all encodings of floating point */
+    unsigned long x;
+    unsigned long y;
+    unsigned long z;
+} cieXyz;
+
+typedef struct {
+    cieXyz red;
+    cieXyz grn;
+    cieXyz blu;
+} cieXyzTriple;
+
 struct bmpInfoHeader {
     enum rowOrder rowOrder;
     unsigned int cols;
@@ -92,17 +105,18 @@ struct bmpInfoHeader {
            described by the "mask" values in the header, rather than
            fixed formats.
         */
-    unsigned int cmapsize;
+    unsigned int cmapSize;
         /* Size in bytes of the colormap (palette) in the BMP file.
 
            Zero means there is no colormap.
         */
     unsigned int imageSize;
-        /* Size in bytes of the image data.  We only reference this 
-           when the image is compressed. */    
+        /* Size in bytes of the image data.  We only reference this
+           when the image is compressed. */
     unsigned short cPlanes;
     BMPCompType compression;
     struct pixelformat pixelformat;
+    cieXyzTriple endPoints;
 };
 
 
@@ -137,7 +151,7 @@ parseCommandLine(int argc, const char ** argv,
 
     option_def_index = 0;   /* incremented by OPTENT3 */
     OPTENT3(0,   "verbose",     OPT_FLAG,   NULL, &cmdlineP->verbose,   0);
- 
+
     opt.opt_table = option_def;
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
     opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
@@ -145,7 +159,7 @@ parseCommandLine(int argc, const char ** argv,
     pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
-    if (argc-1 == 0) 
+    if (argc-1 == 0)
         cmdlineP->inputFileName = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
@@ -208,27 +222,74 @@ GetLong(FILE * const fp) {
 
 
 
-typedef struct {
-    long dummy[12];
-} cieXyzTriple;
+static cieXyz
+GetCieXyz(FILE * const ifP) {
+
+    cieXyz retval;
+
+    retval.x = GetLong(ifP);
+    retval.y = GetLong(ifP);
+    retval.z = GetLong(ifP);
+
+    return retval;
+}
+
+
 
 static cieXyzTriple
-GetCieXyzTriple(FILE * const fp) {
+GetCieXyzTriple(FILE *         const ifP) {
 
-    cieXyzTriple v;
-    unsigned int i;
+    cieXyzTriple retval;
 
-    for (i = 0; i < 12; ++i) 
-        if (pm_readlittlelong(fp, &v.dummy[i]) == -1)
-            pm_error(er_read, ifname);
+    retval.red = GetCieXyz(ifP);
+    retval.grn = GetCieXyz(ifP);
+    retval.blu = GetCieXyz(ifP);
 
-    return v;
+    return retval;
 }
 
 
 
 static struct pixelformat
-defaultPixelformat(unsigned int const bitCount);
+defaultPixelformat(unsigned int const bitCount) {
+
+    struct pixelformat retval;
+
+    switch (bitCount) {
+    case 16:
+        /* This layout is sometimes called "RGB555".  A document from
+           Microsoft says this is the default (when the "compression"
+           field of the header says COMP_BITFIELDS).
+        */
+        retval.conventionalBgr = FALSE;
+        retval.red.shift = 10;
+        retval.grn.shift = 5;
+        retval.blu.shift = 0;
+        retval.trn.shift = 0;
+        retval.red.mask = 0x1f;  /* 5 bits */
+        retval.grn.mask = 0x1f;  /* 5 bits */
+        retval.blu.mask = 0x1f;  /* 5 bits */
+        retval.trn.mask = 0;
+        break;
+    case 24:
+    case 32:
+        retval.conventionalBgr = TRUE;
+        retval.red.shift = 16;
+        retval.grn.shift = 8;
+        retval.blu.shift = 0;
+        retval.trn.shift = 0;
+        retval.red.mask = 0xff;  /* 8 bits */
+        retval.grn.mask = 0xff;  /* 8 bits */
+        retval.blu.mask = 0xff;  /* 8 bits */
+        retval.trn.mask = 0;
+        break;
+    default:
+        /* colormapped - masks are undefined */
+        break;
+    }
+
+    return retval;
+}
 
 
 
@@ -238,7 +299,7 @@ readOffBytes(FILE * const fp, unsigned int const nbytes) {
    Read 'nbytes' from file 'fp'.  Abort program if read error.
 -----------------------------------------------------------------------------*/
     int i;
-    
+
     for(i = 0; i < nbytes; ++i) {
         int rc;
         rc = getc(fp);
@@ -250,15 +311,11 @@ readOffBytes(FILE * const fp, unsigned int const nbytes) {
 
 
 static void
-bmpReadfileheader(FILE *         const ifP, 
-                  unsigned int * const bytesReadP, 
+bmpReadfileheader(FILE *         const ifP,
+                  unsigned int * const bytesReadP,
                   unsigned int * const offBitsP) {
 
-    unsigned short    xHotSpot;
-    unsigned short    yHotSpot;
     unsigned long     offBits;
-    unsigned long int fileSize;
-
 
     if (GetByte(ifP) != 'B')
         pm_error("'%s' is not a BMP file.  (It doesn't start with 'BM')",
@@ -268,33 +325,47 @@ bmpReadfileheader(FILE *         const ifP,
                  ifname);
 
 
-    fileSize = GetLong(ifP);  /* This is not always reliable. */
-    xHotSpot = GetShort(ifP);
-    yHotSpot = GetShort(ifP);
+    /* fileSize = */ GetLong(ifP);  /* This is not always reliable. */
+    /* xHotSpot = */ GetShort(ifP);
+    /* yHotSpot = */ GetShort(ifP);
     offBits  = GetLong(ifP);
 
     *offBitsP = offBits;
 
+    assert(BMPlenfileheader() == 14);
+
     *bytesReadP = 14;
 }
 
 
 
 static void
-readOs2InfoHeader(FILE *                 const ifP,
-                  struct bmpInfoHeader * const headerP) {
+readOs2InfoHeaderRest(FILE *                 const ifP,
+                      struct bmpInfoHeader * const headerP) {
+/*----------------------------------------------------------------------------
+   Read the rest of the info header, after its size field, of an OS2 BMP from
+   *ifP.
+
+   Add the information from it to *headerP, in particular these members:
 
+     cols
+     rows
+     rowOrder
+     cPlanes
+     cBitCount
+     cmapSize
+     pixelformat
+     compression
+-----------------------------------------------------------------------------*/
     unsigned short colsField, rowsField;
     unsigned short planesField, bitCountField;
 
-    headerP->class = C_OS2;
-
     pm_readlittleshortu(ifP, &colsField);
     if (colsField == 0)
         pm_error("Invalid BMP file: says width is zero");
     else
         headerP->cols = colsField;
-    
+
     pm_readlittleshortu(ifP, &rowsField);
     if (rowsField == 0)
         pm_error("Invalid BMP file: says height is zero");
@@ -313,22 +384,19 @@ readOs2InfoHeader(FILE *                 const ifP,
        same as for Windows.
     */
     if (headerP->cBitCount <= 8)
-        headerP->cmapsize = 1 << headerP->cBitCount;
+        headerP->cmapSize = 1 << headerP->cBitCount;
     else if (headerP->cBitCount == 24)
-        headerP->cmapsize = 0;
+        headerP->cmapSize = 0;
     /* There is a 16 bit truecolor format, but we don't know how the
        bits are divided among red, green, and blue, so we can't handle it.
     */
     else
         pm_error("Unrecognized bits per pixel in OS/2 BMP file header: %d",
                  headerP->cBitCount);
-                 
+
     headerP->pixelformat = defaultPixelformat(headerP->cBitCount);
 
     headerP->compression = BMPCOMP_RGB;
-    
-    pm_message("OS/2 BMP, %dx%dx%d",
-               headerP->cols, headerP->rows, headerP->cBitCount);
 }
 
 
@@ -337,24 +405,24 @@ static void
 validateCompression(unsigned long const compression,
                     enum rowOrder const rowOrder,
                     unsigned int  const cBitCount) {
-    
+
     if (compression != BMPCOMP_RGB && compression != BMPCOMP_BITFIELDS &&
-        compression != BMPCOMP_RLE4 && compression != BMPCOMP_RLE8) 
+        compression != BMPCOMP_RLE4 && compression != BMPCOMP_RLE8)
         pm_error("Input has unknown encoding.  "
                  "Compression type code = %ld.  The only ones we know "
                  "are RGB (%u), BITFIELDS (%u), "
                  "RLE4 (%u), and RLE8 (%u)",
                  compression, BMPCOMP_RGB, BMPCOMP_BITFIELDS,
                  BMPCOMP_RLE4, BMPCOMP_RLE8);
-                     
+
     if ((compression == BMPCOMP_RLE4 || compression == BMPCOMP_RLE8) &&
-        rowOrder == TOPDOWN )                        
+        rowOrder == TOPDOWN )
         pm_error("Invalid BMP header.  Claims image is top-down and also "
                  "compressed, which is an impossible combination.");
 
     if ((compression == BMPCOMP_RLE4 && cBitCount !=4 ) ||
-        (compression == BMPCOMP_RLE8 && cBitCount !=8 )) 
-        pm_error("Invalid BMP header.  " 
+        (compression == BMPCOMP_RLE8 && cBitCount !=8 ))
+        pm_error("Invalid BMP header.  "
                  "Compression type (%s) disagrees with "
                  "number of bits per pixel (%u).",
                  compression == BMPCOMP_RLE4 ? "RLE4" : "RLE8",
@@ -373,13 +441,10 @@ readWindowsBasic40ByteInfoHeader(FILE *                 const ifP,
 
    Return the information from the info header as *headerP.
 -----------------------------------------------------------------------------*/
-    int colorsimportant;   /* ColorsImportant value from header */
     int colorsused;        /* ColorsUsed value from header */
     unsigned short planesField, bitCountField;
     int32_t colsField;
 
-    headerP->class = C_WIN;
-
     pm_readlittlelong2(ifP, &colsField);
 
     if (colsField == 0)
@@ -388,6 +453,7 @@ readWindowsBasic40ByteInfoHeader(FILE *                 const ifP,
         pm_error("Invalid BMP file: says width is negative (%d)", colsField);
     else
         headerP->cols = (unsigned int)colsField;
+
     {
         long const cy = GetLong(ifP);
 
@@ -413,7 +479,7 @@ readWindowsBasic40ByteInfoHeader(FILE *                 const ifP,
 
         headerP->bitFields = (compression == BMPCOMP_BITFIELDS);
 
-        headerP->compression = compression;             
+        headerP->compression = compression;
     }
     /* And read the rest of the junk in the 40 byte header */
     headerP->imageSize = GetLong(ifP);   /* ImageSize */
@@ -421,24 +487,30 @@ readWindowsBasic40ByteInfoHeader(FILE *                 const ifP,
     GetLong(ifP);   /* YpixelsPerMeter */
     colorsused = GetLong(ifP);   /* ColorsUsed */
     /* See comments in bmp.h for info about the definition of the following
-       word and its relationship to the color map size (headerP->cmapsize).
+       word and its relationship to the color map size (headerP->cmapSize).
     */
-    colorsimportant = GetLong(ifP);  /* ColorsImportant */
+    /* colorsimportant = */ GetLong(ifP);  /* ColorsImportant */
 
     if (headerP->cBitCount <= 8) {
         if (colorsused != 0) {
             if (colorsused > 1 << headerP->cBitCount)
                 pm_error("Invalid BMP header.  Says %u bits per pixel, "
-                         "but %d colors used", 
+                         "but %d colors used",
                          headerP->cBitCount, colorsused);
+            else if (colorsused == 1 && headerP->cBitCount == 1) {
+                pm_message("Abnormal BMP header.  Says 1 bit per pixel. "
+                           "Should have 2 colors, but says only 1 color used. "
+                    );
+                headerP->cmapSize = colorsused;
+        }
             else
-                headerP->cmapsize = colorsused;
-        } else 
-            headerP->cmapsize = 1 << headerP->cBitCount;
-    } else if (headerP->cBitCount == 24 || 
-               headerP->cBitCount == 16 || 
+                headerP->cmapSize = colorsused;
+        } else
+            headerP->cmapSize = 1 << headerP->cBitCount;
+    } else if (headerP->cBitCount == 24 ||
+               headerP->cBitCount == 16 ||
                headerP->cBitCount == 32)
-        headerP->cmapsize = 0;
+        headerP->cmapSize = 0;
     else
         pm_error("Unrecognized bits per pixel in Windows BMP file header: %d",
                  headerP->cBitCount);
@@ -451,18 +523,18 @@ lsbZeroCount(unsigned int const mask)
 /*----------------------------------------------------------------------------
    Return the number of consecutive zeroes in the mask 'mask', starting with
    the least significant bit and going up.  E.g. for 0x20, it would be 5.
-   
+
    Use GCC built-in when available.
 -----------------------------------------------------------------------------*/
 
-#if HAVE_GCC_BITCOUNT 
+#if HAVE_GCC_BITCOUNT
 {
       return ( mask==0 ? sizeof(mask)*8 : __builtin_ctz(mask) );
 }
 #else
 {
       unsigned int i=0;
-    
+
       while (((mask >> i) & 0x1) == 0 && i < sizeof(mask)*8)
         ++i;
 
@@ -512,52 +584,17 @@ computeConventionalBgr(struct pixelformat * const fP,
 
 
 
-static struct pixelformat
-defaultPixelformat(unsigned int const bitCount) {
-
-    struct pixelformat retval;
-
-    switch (bitCount) {
-    case 16:
-        /* This layout is sometimes called "RGB555".  A document from
-           Microsoft says this is the default (when the "compression"
-           field of the header says COMP_BITFIELDS).
-        */
-        retval.conventionalBgr = FALSE;
-        retval.red.shift = 10;
-        retval.grn.shift = 5;
-        retval.blu.shift = 0;
-        retval.trn.shift = 0;
-        retval.red.mask = 0x1f;  /* 5 bits */
-        retval.grn.mask = 0x1f;  /* 5 bits */
-        retval.blu.mask = 0x1f;  /* 5 bits */
-        retval.trn.mask = 0;
-        break;
-    case 24:
-    case 32:
-        retval.conventionalBgr = TRUE;
-        retval.red.shift = 16;
-        retval.grn.shift = 8;
-        retval.blu.shift = 0;
-        retval.trn.shift = 0;
-        retval.red.mask = 0xff;  /* 8 bits */
-        retval.grn.mask = 0xff;  /* 8 bits */
-        retval.blu.mask = 0xff;  /* 8 bits */
-        retval.trn.mask = 0;
-        break;
-    default:
-        /* colormapped - masks are undefined */
-        break;
-    }
-
-    return retval;
-}
-
+static void
+readV4InfoHeaderExtension(FILE *                 const ifP,
+                          struct bmpInfoHeader * const headerP,
+                          unsigned int *         const bytesReadP) {
 
+    unsigned long redMsk, grnMsk, bluMsk, trnMsk;
 
-static void
-readV4InfoHeaderExtension(FILE *                 const ifP, 
-                          struct bmpInfoHeader * const headerP) {
+    redMsk = GetLong(ifP);
+    grnMsk = GetLong(ifP);
+    bluMsk = GetLong(ifP);
+    trnMsk = GetLong(ifP);
 
     if (headerP->bitFields) {
         /* A document from Microsoft says on Windows 95 there is no
@@ -565,21 +602,40 @@ readV4InfoHeaderExtension(FILE *                 const ifP,
            (5,5,5) or (5,6,5) for 16 bit and (8,8,8) for 32 bit.
            It calls these RGB555, RGB565, RGB888.
         */
-        headerP->pixelformat.red = bitPositionFromMask(GetLong(ifP));
-        headerP->pixelformat.grn = bitPositionFromMask(GetLong(ifP));
-        headerP->pixelformat.blu = bitPositionFromMask(GetLong(ifP));
-        headerP->pixelformat.trn = bitPositionFromMask(GetLong(ifP));
+        headerP->pixelformat.red = bitPositionFromMask(redMsk);
+        headerP->pixelformat.grn = bitPositionFromMask(grnMsk);
+        headerP->pixelformat.blu = bitPositionFromMask(bluMsk);
+        headerP->pixelformat.trn = bitPositionFromMask(trnMsk);
 
         computeConventionalBgr(&headerP->pixelformat, headerP->cBitCount);
     } else
         headerP->pixelformat = defaultPixelformat(headerP->cBitCount);
 
     GetLong(ifP);  /* Color space */
-    GetCieXyzTriple(ifP);  /* Endpoints */
+
+    headerP->endPoints = GetCieXyzTriple(ifP);  /* 36 bytes */
+
     GetLong(ifP);  /* GammaRed */
     GetLong(ifP);  /* GammaGreen */
     GetLong(ifP);  /* GammaBlue */
-} 
+
+    *bytesReadP = 68;
+}
+
+
+
+static void
+readV5InfoHeaderExtension(FILE *                 const ifP,
+                          struct bmpInfoHeader * const headerP,
+                          unsigned int *         const bytesReadP) {
+
+    GetLong(ifP);  /* Intent */
+    GetLong(ifP);  /* ProfileData */
+    GetLong(ifP);  /* ProfileSize */
+    GetLong(ifP);  /* Reserved */
+
+    *bytesReadP = 16;
+}
 
 
 
@@ -593,117 +649,157 @@ defaultV4InfoHeaderExtension(struct bmpInfoHeader * const headerP) {
 
 
 static void
-readWindowsInfoHeader(FILE *                 const ifP, 
-                      unsigned int           const cInfoHeaderSize,
-                      struct bmpInfoHeader * const headerP) {
-
+readWindowsInfoHeaderRest(FILE *                 const ifP,
+                          unsigned int           const cInfoHeaderSize,
+                          struct bmpInfoHeader * const headerP) {
+/*----------------------------------------------------------------------------
+   Read the rest of the info header, after the length field, of a Windows BMP
+   from *ifP.
+
+   'cInfoHeaderSize' is the size of the info header, not counting its size
+   field.  Note that besides telling us how much data to read, this also
+   implies which of the three major formats the data is in.
+
+   Add the information from it to *headerP, in particular these members:
+
+     cols
+     rows
+     rowOrder
+     cPlanes
+     cBitCount
+     bitFields
+     compression
+     imageSize
+     cmapSize
+     pixelformat
+     endPoints
+-----------------------------------------------------------------------------*/
     /* There are 3 major formats of Windows
        BMP, identified by the 3 info header lengths.  The original
        one is 40 bytes.  The "V4 header" is 108 bytes and was
        new with Windows 95 and NT 4.0.  The "V5 header" is 124 bytes
        and was new with Windows 98 and Windows 2000.
     */
+    unsigned int bytesRead;
+
     readWindowsBasic40ByteInfoHeader(ifP, headerP);
 
-    if (cInfoHeaderSize >= 108) 
-        readV4InfoHeaderExtension(ifP, headerP);
-    else 
+    bytesRead = 40;
+
+    if (cInfoHeaderSize >= BMP_HDRLEN_WIN_V4) {
+        unsigned int v4BytesRead;
+        readV4InfoHeaderExtension(ifP, headerP, &v4BytesRead);
+        bytesRead += v4BytesRead;
+
+        assert(bytesRead == BMP_HDRLEN_WIN_V4);
+    } else
         defaultV4InfoHeaderExtension(headerP);
 
-    if (cInfoHeaderSize >= 124) {
-        /* Read off the V5 info header extension. */
-        GetLong(ifP);  /* Intent */
-        GetLong(ifP);  /* ProfileData */
-        GetLong(ifP);  /* ProfileSize */
-        GetLong(ifP);  /* Reserved */
+    if (cInfoHeaderSize >= BMP_HDRLEN_WIN_V5) {
+        unsigned int v5BytesRead;
+        readV5InfoHeaderExtension(ifP, headerP, &v5BytesRead);
+        bytesRead += v5BytesRead;
+        assert(bytesRead == BMP_HDRLEN_WIN_V5);
     }
 
-    pm_message("Windows BMP, %dx%dx%d",
-               headerP->cols, headerP->rows, headerP->cBitCount);
+    for (; bytesRead < cInfoHeaderSize;) {
+        GetByte(ifP);
+        ++bytesRead;
+    }
+
+    assert(bytesRead == cInfoHeaderSize);
 }
 
 
 
 static void
-bmpReadinfoheader(FILE *                 const ifP, 
+bmpReadinfoheader(FILE *                 const ifP,
                   unsigned int *         const bytesReadP,
-                  struct bmpInfoHeader * const headerP) {
+                  struct bmpInfoHeader * const headerP,
+                  const char **          const errorP) {
 
     unsigned int const cInfoHeaderSize = GetLong(ifP);
 
-    switch (cInfoHeaderSize) {
-    case 12:
-        readOs2InfoHeader(ifP, headerP);
-        break;
-    case 40: 
-        readWindowsInfoHeader(ifP, cInfoHeaderSize, headerP);
-        break;
-    case 108:
-        pm_error("This is a Version 4 Windows BMP; "
-                 "this program knows only Version 1");
-        break;
-    case 124:
-        pm_error("This is a Version 5 Windows BMP; "
-                 "this program knows only Version 1");
-        break;
-    default:
-        pm_error("%s: unknown Info Header size: %u bytes", 
-                 ifname, cInfoHeaderSize);
-        break;
+    const char * error;
+
+    BMPdetermineclass(cInfoHeaderSize, &headerP->class, &error);
+
+    if (error) {
+        pm_asprintf(errorP, "Cannot determine the class of BMP from the "
+                    "info header size %u.  %s", cInfoHeaderSize, error);
+        pm_strfree(error);
+    } else {
+        switch (headerP->class) {
+        case BMP_C_WIN_V1:
+        case BMP_C_WIN_V2:
+        case BMP_C_WIN_V3:
+        case BMP_C_WIN_V4:
+        case BMP_C_WIN_V5:
+            readWindowsInfoHeaderRest(ifP, cInfoHeaderSize, headerP);
+            break;
+        case BMP_C_OS2_1x:
+        case BMP_C_OS2_2x:
+            readOs2InfoHeaderRest(ifP, headerP);
+            break;
+        }
+        *errorP = NULL;
+        *bytesReadP = cInfoHeaderSize;
     }
-    *bytesReadP = cInfoHeaderSize;
+    /* Part of our anti-arithmetic overflow strategy is to make sure height
+       and width always fit in 16 bits, so they can be multiplied together.
+       This shouldn't be a problem, since they come from 16 bit fields in
+       the BMP info header.
+    */
+    assert(headerP->cols < (1<<16));
+    assert(headerP->rows < (1<<16));
 }
 
 
 
 static void
-bmpReadColormap(FILE *         const ifP, 
-                int            const class, 
-                xel **         const colormapP, 
-                unsigned int   const cmapsize,
+bmpReadColormap(FILE *         const ifP,
+                enum bmpClass  const class,
+                xel **         const colormapP,
+                unsigned int   const cmapSize,
                 unsigned int * const bytesReadP) {
 /*----------------------------------------------------------------------------
    Read the color map from the present position in the input BMP file
    *ifP.
 
-   The map has 'cmapsize' entries in it.  cmapsize == 0 means there is
+   The map has 'cmapSize' entries in it.  cmapSize == 0 means there is
    no color map.
 
    We return a color map as *colormapP.  If there is no color map in the
    BMP, this is just an arbitrary color map.
- 
+
    'class' is the class of BMP image - Windows or OS/2.
 -----------------------------------------------------------------------------*/
+    xel * const colormap = pnm_allocrow(MAX(1, cmapSize));
 
     unsigned int i;
-
-    xel * colormap;
     unsigned int bytesRead;
 
-    colormap = pnm_allocrow(MAX(1,cmapsize));
-    
-    bytesRead = 0;  /* initial value */
-
-    for (i = 0; i < cmapsize; ++i) {
+    for (i = 0, bytesRead = 0; i < cmapSize; ++i) {
         /* There is a document that says the bytes are ordered R,G,B,Z,
            but in practice it appears to be the following instead:
         */
-        unsigned int r, g, b;
-        
-        b = GetByte(ifP);
-        g = GetByte(ifP);
-        r = GetByte(ifP);
+        unsigned int const b = GetByte(ifP);
+        unsigned int const g = GetByte(ifP);
+        unsigned int const r = GetByte(ifP);
+
+        unsigned int j;
 
         PNM_ASSIGN(colormap[i], r, g, b);
 
         bytesRead += 3;
 
-        if (class == C_WIN) {
+        for (j = 3; j < BMPlenrgb(class); ++j) {
             GetByte(ifP);
             bytesRead += 1;
         }
     }
-    *colormapP = colormap;
+
+    *colormapP  = colormap;
     *bytesReadP = bytesRead;
 }
 
@@ -718,13 +814,13 @@ extractBitFields(unsigned int       const rasterval,
                  pixval *           const bP,
                  pixval *           const aP) {
 
-    unsigned int const rbits = 
+    unsigned int const rbits =
         (rasterval >> pixelformat.red.shift) & pixelformat.red.mask;
-    unsigned int const gbits = 
+    unsigned int const gbits =
         (rasterval >> pixelformat.grn.shift) & pixelformat.grn.mask;
-    unsigned int const bbits = 
+    unsigned int const bbits =
         (rasterval >> pixelformat.blu.shift) & pixelformat.blu.mask;
-    unsigned int const abits = 
+    unsigned int const abits =
         (rasterval >> pixelformat.trn.shift) & pixelformat.trn.mask;
 
     *rP = pixelformat.red.mask > 0 ?
@@ -735,7 +831,7 @@ extractBitFields(unsigned int       const rasterval,
         (unsigned int) bbits * maxval / pixelformat.blu.mask : 0;
     *aP = pixelformat.trn.mask > 0 ?
         (unsigned int) abits * maxval / pixelformat.trn.mask : 0;
-}        
+}
 
 
 
@@ -758,7 +854,7 @@ convertRow16(unsigned char      const bmprow[],
         extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a);
 
         PNM_ASSIGN(xelrow[col], r, g, b);
-        
+
         cursor += 2;
     }
 }
@@ -770,7 +866,7 @@ convertRow24(unsigned char      const bmprow[],
              xel                      xelrow[],
              int                const cols,
              struct pixelformat const pixelformat) {
-    
+
     /* It's truecolor */
     /* There is a document that gives a much different format for
        24 bit BMPs.  But this seems to be the de facto standard, and is,
@@ -780,7 +876,7 @@ convertRow24(unsigned char      const bmprow[],
 
     unsigned int col;
     unsigned int cursor;
-    
+
     cursor = 0;
     for (col = 0; col < cols; ++col) {
         pixval r, g, b, a;
@@ -791,17 +887,17 @@ convertRow24(unsigned char      const bmprow[],
             b = bmprow[cursor+0];
             a = 0;
         } else {
-            unsigned int const rasterval = 
+            unsigned int const rasterval =
                 (bmprow[cursor+0] << 16) +
                 (bmprow[cursor+1] << 8) +
                 (bmprow[cursor+2] << 0);
-            
+
             extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a);
         }
         PNM_ASSIGN(xelrow[col], r, g, b);
         cursor += 3;
     }
-} 
+}
 
 
 
@@ -810,7 +906,7 @@ convertRow32(unsigned char      const bmprow[],
              xel                      xelrow[],
              int                const cols,
              struct pixelformat const pixelformat) {
-    
+
     /* It's truecolor */
 
     unsigned int col;
@@ -826,40 +922,40 @@ convertRow32(unsigned char      const bmprow[],
             b = bmprow[cursor+0];
             a = 0;
         } else {
-            unsigned int const rasterval = 
+            unsigned int const rasterval =
                 (bmprow[cursor+0] << 24) +
                 (bmprow[cursor+1] << 16) +
                 (bmprow[cursor+2] << 8) +
                 (bmprow[cursor+3] << 0);
-            
+
             extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a);
         }
 
-        PNM_ASSIGN(xelrow[col], 
+        PNM_ASSIGN(xelrow[col],
                    bmprow[cursor+2], bmprow[cursor+1], bmprow[cursor+0]);
         cursor += 4;
     }
-} 
+}
 
 
 static void
 validateIndex(unsigned int const index,
-	      unsigned int const cmapsize ) {
+              unsigned int const cmapSize ) {
 
-    if (index >= cmapsize)
+    if (index >= cmapSize)
         pm_error("Error: invalid index to color palette.");
 }
 
 
 
 static void
-convertRow(unsigned char      const bmprow[], 
+convertRow(unsigned char      const bmprow[],
            xel                      xelrow[],
-           int                const cols, 
-           unsigned int       const cBitCount, 
+           int                const cols,
+           unsigned int       const cBitCount,
            struct pixelformat const pixelformat,
            xel                const colormap[],
-           unsigned int       const cmapsize
+           unsigned int       const cmapSize
            ) {
 /*----------------------------------------------------------------------------
    Convert a row in raw BMP raster format bmprow[] to a row of xels xelrow[].
@@ -871,20 +967,20 @@ convertRow(unsigned char      const bmprow[],
    If the image is colormapped, colormap[] is the colormap
    (colormap[i] is the color with color index i).
 -----------------------------------------------------------------------------*/
-    if (cBitCount == 24) 
+    if (cBitCount == 24)
         convertRow24(bmprow, xelrow, cols, pixelformat);
-    else if (cBitCount == 16) 
+    else if (cBitCount == 16)
         convertRow16(bmprow, xelrow, cols, pixelformat);
-    else if (cBitCount == 32) 
+    else if (cBitCount == 32)
         convertRow32(bmprow, xelrow, cols, pixelformat);
-    else if (cBitCount == 8) {            
+    else if (cBitCount == 8) {
         /* It's a whole byte colormap index */
-        unsigned int col; 
+        unsigned int col;
         for (col = 0; col < cols; ++col) {
             unsigned int const index = bmprow[col];
-            validateIndex(index, cmapsize);
+            validateIndex(index, cmapSize);
             xelrow[col] = colormap[index];
-	}
+    }
     } else if (cBitCount == 1 || cBitCount == 2 || cBitCount == 4) {
         /* It's a bit field color index */
         unsigned char const mask = ( 1 << cBitCount ) - 1;
@@ -894,9 +990,9 @@ convertRow(unsigned char      const bmprow[],
         for (col = 0; col < cols; ++col) {
             unsigned int const cursor = (col*cBitCount)/8;
             unsigned int const shift = 8 - ((col*cBitCount) % 8) - cBitCount;
-            unsigned int const index = 
+            unsigned int const index =
                 (bmprow[cursor] & (mask << shift)) >> shift;
-            validateIndex(index, cmapsize);
+            validateIndex(index, cmapSize);
             xelrow[col] = colormap[index];
         }
     } else {
@@ -911,8 +1007,8 @@ static unsigned char **
 allocBmpRaster(unsigned int const rows,
                unsigned int const bytesPerRow) {
 
-    unsigned int const storageSize = 
-        rows * sizeof(unsigned char *) + rows * bytesPerRow;        
+    unsigned int const storageSize =
+        rows * sizeof(unsigned char *) + rows * bytesPerRow;
     unsigned char ** bmpRaster;
     unsigned int row;
     unsigned char * startOfRows;
@@ -932,7 +1028,7 @@ allocBmpRaster(unsigned int const rows,
 
     startOfRows = (unsigned char *)(bmpRaster + rows);
 
-    for (row = 0; row < rows; ++row) 
+    for (row = 0; row < rows; ++row)
         bmpRaster[row] = startOfRows + row * bytesPerRow;
 
     return bmpRaster;
@@ -950,47 +1046,51 @@ readrow(FILE *           const ifP,
     size_t bytesRead;
 
     assert(bytesPerRow > 0);
-    
+
     bytesRead = fread(bmpRaster[row], 1, bytesPerRow, ifP);
 
     if (bytesRead < bytesPerRow) {
         if (feof(ifP))
             pm_error("End of file reading row %u of BMP raster.", row);
-        else 
+        else
             pm_error("Error reading BMP raster.  Errno=%d (%s)",
                      errno, strerror(errno));
     }
     *bytesReadP += bytesRead;
 }
- 
+
 
 
 static void
-nibbleAlign(unsigned char * const ptr,
-            unsigned int    const nibbles){
+nybbleAlign(unsigned char * const bytes,
+            unsigned int    const nybbleCt){
 /*----------------------------------------------------------------------------
-  Shift data pointed by ptr one half byte toward the MSB (to the left).
- 
+  Shift the 'nybbleCt' nybbles of bytes[], after the first byte, one nybble
+  toward the left, with the first of those nybble shifting into the right half
+  of the first byte.  Leave the left half of the first byte alone.
+
   Example:
- 
-  (Numbers in hex, 8 nibbles)
-            5F 13 7E 89 A1
+
+  (Numbers in hex, 8 nybbles)
+            5? 13 7E 89 A1
    becomes  51 37 E8 9A 10
 -----------------------------------------------------------------------------*/
-    unsigned int const fullByteCount = (nibbles-1) / 2;
+    unsigned int const fullByteCt = (nybbleCt + 1) / 2;
     unsigned int i;
-              
-    ptr[0] = ptr[0] & ptr[1] >> 4;
-                                    
-    for (i = 0; i < fullByteCount; ++i)
-        ptr[i+1] = ptr[i+1] << 4 & ptr[i+2] >> 4;
-    
-    if (nibbles % 2 == 1)   /* if there is a final odd nibble */
-        ptr[fullByteCount+1] <<= 4; /* shift it a half byte */
+
+    bytes[0] >>= 4;
+
+    for (i = 0; i < fullByteCt; ++i)
+        bytes[i] = bytes[i] << 4 | bytes[i+1] >> 4;
+
+    if (nybbleCt % 2 == 0) {
+        /* There is a final right nybble.  Shift it. */
+        bytes[fullByteCt] <<= 4;
+    }
 }
 
 
-           
+
 enum rleStatus { ABS_MODE, ENC_MODE, END_OF_ROW, END_OF_BMP, DELTA };
 
 static enum rleStatus
@@ -1000,9 +1100,9 @@ readRLEcode(FILE *          const ifP,
 
     unsigned short s;
     enum rleStatus retval;
-    
+
     s = GetBigShort(ifP);
-    
+
     if      (s == 0) retval = END_OF_ROW;
     else if (s == 1) retval = END_OF_BMP;
     else if (s == 2) retval = DELTA;
@@ -1034,16 +1134,18 @@ readrowRLE(FILE *           const ifP,
     bool const rle4 = (compression == BMPCOMP_RLE4);
     int  const pixelsPerRowMargin = rle4 ? cols % 2 : 0;
 
-    char const err_decode[] = 
+    char const err_decode[] =
         "Error while decoding compressed BMP image.  "
-        "%s.  Row: %u  Pixel: %u" ; 
-     
+        "%s.  Row: %u  Pixel: %u" ;
+
     unsigned int totalBytesRead;
     unsigned int pixelsRead;
 
-    /* There are RLE4 images with rows coded up the byte boundary, resulting
-       in each row one pixel larger than the column length stated in the
-       BMP info header (header.cols) when the column length is odd.
+    /* There are RLE4 images with rows coded up to the byte boundary,
+       resulting in each row one pixel larger than the column length
+       stated in the BMP info header (header.cols) when the column length
+       is odd.
+
        pixelsPerRowMargin is a "wart" to provide for this case.
     */
 
@@ -1052,7 +1154,7 @@ readrowRLE(FILE *           const ifP,
 
     while (true) {
         unsigned int n;
-            /* decompressed bytes already read; current write point */ 
+            /* decompressed bytes already read; current write point */
         unsigned int cnt;
         unsigned char code;
 
@@ -1061,23 +1163,23 @@ readrowRLE(FILE *           const ifP,
         switch (readRLEcode(ifP, &cnt, &code)) {
         case ENC_MODE: {
             unsigned int const byteCnt = rle4 ? (cnt + 1) /2 : cnt;
-            unsigned int i; 
+            unsigned int i;
 
             if (pixelsRead + cnt > cols + pixelsPerRowMargin)
                 pm_error(err_decode,  "Too many pixels in encoded mode",
-                         row, pixelsRead ); 
-                 
+                         row, pixelsRead );
+
             for (i = 0; i < byteCnt; ++i)
                 bmpRaster[row][n+i] = code;
-                 
+
             if (rle4 && pixelsRead % 2 == 1)
                 /* previous read ended odd */
-                nibbleAlign(&bmpRaster[row][n-1], cnt); 
-            
+                nybbleAlign(&bmpRaster[row][n-1], cnt);
+
             pixelsRead += cnt;
             totalBytesRead += 2;
         } break;
-        
+
         case ABS_MODE: {
             unsigned int cmpBytesRead; /* compressed bytes read */
             /* align read-end to 16 bit boundary */
@@ -1086,43 +1188,43 @@ readrowRLE(FILE *           const ifP,
 
             if (pixelsRead + cnt > cols + pixelsPerRowMargin)
                 pm_error(err_decode,  "Too many pixels in absolute mode",
-                         row, pixelsRead); 
+                         row, pixelsRead);
 
-            cmpBytesRead = fread(&bmpRaster[row][n], 
+            cmpBytesRead = fread(&bmpRaster[row][n],
                                  sizeof(char), bytesToRead, ifP);
 
             if (cmpBytesRead < bytesToRead) {
                 if (feof(ifP))
                     pm_error("End of file reading row %u "
                              "of compressed BMP raster.", row);
-                else 
+                else
                     pm_error("Error reading BMP raster.  Errno=%d (%s)",
                              errno, strerror(errno));
             }
             if (rle4 && pixelsRead % 2 == 1) /* previous read ended odd */
-                nibbleAlign(&bmpRaster[row][n-1], cnt); 
-    
+                nybbleAlign(&bmpRaster[row][n-1], cnt);
+
             pixelsRead += cnt;
             totalBytesRead += cmpBytesRead + 2;
         } break;
-            
+
         case END_OF_ROW: {
             if (cols == pixelsRead ||
                 cols + pixelsPerRowMargin == pixelsRead) {
                 if (!lastrow) {
                     *bytesReadP += totalBytesRead + 2;
                     return;
-                } else if (readRLEcode(ifP, NULL, NULL) == END_OF_BMP) { 
+                } else if (readRLEcode(ifP, NULL, NULL) == END_OF_BMP) {
                     *bytesReadP += totalBytesRead +4;
                     return;
                 } else
                     /* lastrow and END_OF_BITMAP not detected */
                     pm_error(err_decode,  "End of bitmap not marked",
-                             row, pixelsRead ); 
+                             row, pixelsRead );
             } else
                 pm_error(err_decode,  "Premature end of row",
                          row, pixelsRead);
-        } break;  
+        } break;
 
         case END_OF_BMP: {
             if (lastrow && (cols == pixelsRead ||
@@ -1132,15 +1234,27 @@ readrowRLE(FILE *           const ifP,
             } else
                 pm_error(err_decode,  "Premature end of bitmap",
                          row, pixelsRead );
+            /* Windows programs do not reject premature end of bitmap.
+               Rather, they set the remaining pixels of the raster to
+               an arbitrary value.  In practice, images with incomplete
+               bitmaps are rare.
+            */
         } break;
 
         case DELTA: {
+            /* Delta means "move the point (col,row) by the amount given
+               in the next two bytes."  Like premature end of bitmap, the
+               official specs do not specify what value the skipped pixels
+               should be set to.  Judging from Windows utilities, there is
+               no consensus within Microsoft either.
+            */
             pm_error(err_decode,
                      "Delta code in compressed BMP image.  "
-                     "This program does not process deltas.",
+                     "This program does not process deltas",
                      row, pixelsRead);
+
         } break;
-         
+
         default:
             pm_error("Internal error processing RLE code in row %u", row);
         }
@@ -1150,13 +1264,13 @@ readrowRLE(FILE *           const ifP,
 
 
 static void
-bmpReadraster(FILE *            const ifP, 
-              unsigned int      const cols, 
-              unsigned int      const rows, 
+bmpReadraster(FILE *            const ifP,
+              unsigned int      const cols,
+              unsigned int      const rows,
               enum rowOrder     const rowOrder,
-              unsigned int      const cBitCount, 
+              unsigned int      const cBitCount,
               BMPCompType       const compression,
-              unsigned char *** const bmpRasterP, 
+              unsigned char *** const bmpRasterP,
               unsigned int *    const bytesReadP) {
 /*----------------------------------------------------------------------------
    Read the raster from the BMP file on *ifP (which is positioned to the
@@ -1179,37 +1293,40 @@ bmpReadraster(FILE *            const ifP,
         */
     unsigned char ** bmpRaster;
 
+    assert(cols < (1<<16));
+    assert(bytesPerRow < (1<<16));
+
     bmpRaster = allocBmpRaster(rows, bytesPerRow);
 
     *bytesReadP = 0;
 
-    /* row order BOTTOMUP is by far the most common case - the bottom 
+    /* row order BOTTOMUP is by far the most common case - the bottom
        line is first in the file, the top line last.
-       
+
        We have never actually seen TOPDOWN, except in a Microsoft spec
     */
-    
+
     switch(compression){
     case BMPCOMP_RGB:
     case BMPCOMP_BITFIELDS: {
         unsigned int i;
         for (i = 0; i < rows; ++i)
-            readrow(ifP, rowOrder == TOPDOWN ? i : rows - i - 1, 
+            readrow(ifP, rowOrder == TOPDOWN ? i : rows - i - 1,
                     bytesPerRow, bmpRaster, bytesReadP);
     } break;
-    case BMPCOMP_RLE4: 
+    case BMPCOMP_RLE4:
     case BMPCOMP_RLE8: {
         unsigned int i;
         /* Read all rows except last */
         assert(rows >= 1);
         for (i = 0; i < rows - 1; ++i){
-            readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1, 
+            readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1,
                        cols, FALSE, compression, bmpRaster, bytesReadP);
         }
         /* Read last row */
-        readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1, 
+        readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1,
                    cols, TRUE,  compression, bmpRaster, bytesReadP);
-    } break;             
+    } break;
     case BMPCOMP_JPEG:
         pm_error("BMP file uses JPEG compression.  We don't know how to "
                  "interpret that.");
@@ -1226,36 +1343,51 @@ bmpReadraster(FILE *            const ifP,
 
 static void
 reportHeader(struct bmpInfoHeader const header,
-             unsigned int         const offBits) {
-             
-    pm_message("BMP image header says:");
-    pm_message("  Class of BMP: %s", 
-               header.class == C_WIN ? "Windows" : 
-               header.class == C_OS2 ? "OS/2" :
-               "???");
-    pm_message("  Width: %d pixels", header.cols);
-    pm_message("  Height: %d pixels", header.rows);
-    pm_message("  Depth: %d planes", header.cPlanes);
-    pm_message("  Row order: %s", 
-               header.rowOrder == BOTTOMUP ? "bottom up" : "top down");
-    pm_message("  Byte offset of raster within file: %u", offBits);
-    pm_message("  Bits per pixel in raster: %u", header.cBitCount);
-    pm_message("  Compression: %s", BMPCompTypeName(header.compression));
-    pm_message("  Colors in color map: %u", header.cmapsize);
-}        
+             unsigned int         const offBits,
+             bool                 const verbose) {
+
+    if (verbose) {
+        pm_message("BMP image header says:");
+        pm_message("  Class of BMP: %s", BMPClassName(header.class));
+        pm_message("  Width: %d pixels", header.cols);
+        pm_message("  Height: %d pixels", header.rows);
+        pm_message("  Depth: %d planes", header.cPlanes);
+        pm_message("  Row order: %s",
+                   header.rowOrder == BOTTOMUP ? "bottom up" : "top down");
+        pm_message("  Byte offset of raster within file: %u", offBits);
+        pm_message("  Bits per pixel in raster: %u", header.cBitCount);
+        pm_message("  Compression: %s", BMPCompTypeName(header.compression));
+        pm_message("  Colors in color map: %u", header.cmapSize);
+    } else {
+        pm_message("%s BMP, %ux%ux%u",
+                   BMPClassName(header.class),
+                   header.cols,
+                   header.rows,
+                   header.cBitCount);
+    }
+}
+
+
+
+static void
+validateCPlanes(unsigned short const cPlanes) {
+
+    if (cPlanes != 1)
+        pm_error("Error: invalid planes value in BMP header.  Must be 1");
+}
 
 
 
 static void
 analyzeColors(xel          const colormap[],
-              unsigned int const cmapsize,
+              unsigned int const cmapSize,
               xelval       const maxval,
               bool *       const grayPresentP,
               bool *       const colorPresentP) {
-    
-    if (cmapsize == 0) {
+
+    if (cmapSize == 0) {
         /* No colormap, and we're not about to search the entire raster,
-           so we just assume it's full color 
+           so we just assume it's full color
         */
         *colorPresentP = TRUE;
         *grayPresentP = TRUE;
@@ -1264,7 +1396,7 @@ analyzeColors(xel          const colormap[],
 
         *colorPresentP = FALSE;  /* initial assumption */
         *grayPresentP = FALSE;   /* initial assumption */
-        for (i = 0; i < cmapsize; ++i) {
+        for (i = 0; i < cmapSize; ++i) {
             if (PPM_ISGRAY(colormap[i])) {
                 if (PPM_GETR(colormap[i]) != 0 &&
                     PPM_GETR(colormap[i]) != maxval)
@@ -1281,8 +1413,8 @@ static void
 warnIfOffBitsWrong(struct bmpInfoHeader const bmpHeader,
                    unsigned int         const offBits) {
 
-    if (offBits != BMPoffbits(bmpHeader.class, bmpHeader.cBitCount, 
-                              bmpHeader.cmapsize)) {
+    if (offBits != BMPoffbits(bmpHeader.class, bmpHeader.cBitCount,
+                              bmpHeader.cmapSize)) {
 
         pm_message("warning: the BMP header says the raster starts "
                    "at offset %u bytes into the file (offbits), "
@@ -1290,8 +1422,8 @@ warnIfOffBitsWrong(struct bmpInfoHeader const bmpHeader,
                    "the raster.  This inconsistency probably means the "
                    "input file is not a legal BMP file and is unusable.",
                    offBits,
-                   BMPoffbits(bmpHeader.class, bmpHeader.cBitCount, 
-                              bmpHeader.cmapsize));
+                   BMPoffbits(bmpHeader.class, bmpHeader.cBitCount,
+                              bmpHeader.cmapSize));
     }
 }
 
@@ -1305,8 +1437,8 @@ readColorMap(FILE *               const ifP,
 
     unsigned int bytesRead;
 
-    bmpReadColormap(ifP, bmpHeader.class, 
-                    colorMapP, bmpHeader.cmapsize, &bytesRead);
+    bmpReadColormap(ifP, bmpHeader.class,
+                    colorMapP, bmpHeader.cmapSize, &bytesRead);
 
     *posP += bytesRead;
 }
@@ -1316,7 +1448,7 @@ readColorMap(FILE *               const ifP,
 static void
 readRaster(FILE *               const ifP,
            struct bmpInfoHeader const bmpHeader,
-           unsigned char ***    const bmpRasterP, 
+           unsigned char ***    const bmpRasterP,
            unsigned int *       const posP) {
 
     unsigned int bytesRead;
@@ -1350,16 +1482,16 @@ isValidBmpBpp(unsigned int const cBitCount) {
 
 
 static void
-readBmp(FILE *               const ifP, 
-        unsigned char ***    const bmpRasterP, 
-        int *                const colsP, 
-        int *                const rowsP,
-        bool *               const grayPresentP, 
+readBmp(FILE *               const ifP,
+        unsigned char ***    const bmpRasterP,
+        unsigned int *       const colsP,
+        unsigned int *       const rowsP,
+        bool *               const grayPresentP,
         bool *               const colorPresentP,
-        unsigned int *       const cBitCountP, 
+        unsigned int *       const cBitCountP,
         struct pixelformat * const pixelformatP,
         xel **               const colormapP,
-        unsigned int *       const cmapsizeP,
+        unsigned int *       const cmapSizeP,
         bool                 const verbose) {
 
     xel * colormap;  /* malloc'ed */
@@ -1367,33 +1499,39 @@ readBmp(FILE *               const ifP,
         /* Current byte position in the BMP file */
 
     /* The following are all information from the BMP headers */
-    
+
     unsigned int offBits;
         /* Byte offset into file of raster */
     struct bmpInfoHeader bmpHeader;
 
     pos = 0;  /* Starting at the beginning ... */
-    { 
+    {
         unsigned int bytesRead;
         bmpReadfileheader(ifP, &bytesRead, &offBits);
         pos += bytesRead;
     }
     {
         unsigned int bytesRead;
-        bmpReadinfoheader(ifP, &bytesRead, &bmpHeader);
+        const char * error;
+        bmpReadinfoheader(ifP, &bytesRead, &bmpHeader, &error);
+        if (error)
+            pm_error("Failed to read the BMP info header.  Image may "
+                     "not be a valid BMP.  %s", error);
+
         if (verbose)
             pm_message("Read %u bytes of header", bytesRead);
         pos += bytesRead;
     }
 
-    if (verbose) 
-        reportHeader(bmpHeader, offBits);
+    reportHeader(bmpHeader, offBits, verbose);
+
+    validateCPlanes(bmpHeader.cPlanes);
 
     warnIfOffBitsWrong(bmpHeader, offBits);
 
     readColorMap(ifP, bmpHeader, &colormap, &pos);
 
-    analyzeColors(colormap, bmpHeader.cmapsize, bmpMaxval, 
+    analyzeColors(colormap, bmpHeader.cmapSize, bmpMaxval,
                   grayPresentP, colorPresentP);
 
     readOffBytes(ifP, offBits - pos);
@@ -1404,7 +1542,7 @@ readBmp(FILE *               const ifP,
 
     if (fgetc(ifP) != EOF)
         pm_message("warning: some image data remains unread.");
-    
+
     if (!isValidBmpBpp(bmpHeader.cBitCount))
         pm_error("Invalid BMP image: 'cBitCount' field of header "
                  "(number of bits for each pixel in raster) is %u",
@@ -1416,23 +1554,23 @@ readBmp(FILE *               const ifP,
     *rowsP        = bmpHeader.rows;
     *pixelformatP = bmpHeader.pixelformat;
     *colormapP    = colormap;
-    *cmapsizeP    = bmpHeader.cmapsize;
+    *cmapSizeP    = bmpHeader.cmapSize;
 }
 
 
 
 static void
 writeRasterGen(unsigned char **   const bmpRaster,
-               int                const cols, 
-               int                const rows, 
+               unsigned int       const cols,
+               unsigned int       const rows,
                int                const format,
-               unsigned int       const cBitCount, 
+               unsigned int       const cBitCount,
                struct pixelformat const pixelformat,
                xel                const colormap[],
-               unsigned int       const cmapsize) {
+               unsigned int       const cmapSize) {
 /*----------------------------------------------------------------------------
   Write the PNM raster to Standard Output, corresponding to the raw BMP
-  raster bmpRaster.  Write the raster assuming the PNM image has 
+  raster bmpRaster.  Write the raster assuming the PNM image has
   dimensions 'cols' by 'rows' and format 'format', with maxval 255.
 
   The BMP image has 'cBitCount' bits per pixel, arranged in format
@@ -1440,7 +1578,7 @@ writeRasterGen(unsigned char **   const bmpRaster,
 
   If the image is colormapped, colormap[] is the colormap
   (colormap[i] is the color with color index i).
-  
+
   writeRasterPbm() is faster for a PBM image.
 -----------------------------------------------------------------------------*/
     xel * xelrow;
@@ -1450,7 +1588,7 @@ writeRasterGen(unsigned char **   const bmpRaster,
 
     for (row = 0; row < rows; ++row) {
         convertRow(bmpRaster[row], xelrow, cols, cBitCount, pixelformat,
-                   colormap, cmapsize);
+                   colormap, cmapSize);
         pnm_writepnmrow(stdout, xelrow, cols, bmpMaxval, format, FALSE);
     }
     pnm_freerow(xelrow);
@@ -1460,12 +1598,12 @@ writeRasterGen(unsigned char **   const bmpRaster,
 
 static void
 writeRasterPbm(unsigned char ** const bmpRaster,
-               int              const cols, 
-               int              const rows, 
+               unsigned int     const cols,
+               unsigned int     const rows,
                xel              const colormap[]) {
 /*----------------------------------------------------------------------------
   Write the PBM raster to Standard Output corresponding to the raw BMP
-  raster bmpRaster.  Write the raster assuming the PBM image has 
+  raster bmpRaster.  Write the raster assuming the PBM image has
   dimensions 'cols' by 'rows'.
 
   The BMP image has 'cBitCount' bits per pixel, arranged in format
@@ -1475,28 +1613,28 @@ writeRasterPbm(unsigned char ** const bmpRaster,
   (colormap[i] is the color with color index i).  We cannot handle the
   abnormal case in which colormap[0] and colormap[1] have the same
   value (i.e. both white or both black.)
-  
+
   We destroy *bmpRaster as a side effect.
 -----------------------------------------------------------------------------*/
-    unsigned int const colChars = pbm_packed_bytes(cols);
-    
-    int row;
+    unsigned int const colCharCt = pbm_packed_bytes(cols);
+
+    unsigned int row;
     enum colorFormat {BlackWhite, WhiteBlack};
     enum colorFormat colorformat;
-                  
+
     if (PPM_GETR(colormap[0]) > 0)
         colorformat = WhiteBlack;
-    else                  
+    else
         colorformat = BlackWhite;
-        
-    for (row=0; row < rows; ++row){
-        unsigned char * const bitrow = bmpRaster[row]; 
+
+    for (row = 0; row < rows; ++row){
+        unsigned char * const bitrow = bmpRaster[row];
 
         if (colorformat == BlackWhite) {
             unsigned int i;
-            for (i = 0; i < colChars; ++i) 
-                bitrow[i] = ~bitrow[i]; /* flip all pixels */ 
-        }   
+            for (i = 0; i < colCharCt; ++i)
+                bitrow[i] = ~bitrow[i]; /* flip all pixels */
+        }
 
         pbm_cleanrowend_packed(bitrow, cols);
         pbm_writepbmrow_packed(stdout, bitrow, cols, FALSE);
@@ -1517,8 +1655,8 @@ main(int argc, const char ** argv) {
            black and white and whether it has colors other than black, white,
            and gray.
         */
-    int cols, rows;
-    unsigned char **bmpRaster;
+    unsigned int cols, rows;
+    unsigned char ** bmpRaster;
         /* The raster part of the BMP image, as a row x column array, with
            each element being a raw byte from the BMP raster.  Note that
            bmpRaster[0] is really Row 0 -- the top row of the image, even
@@ -1532,13 +1670,12 @@ main(int argc, const char ** argv) {
         /* Malloc'ed colormap (palette) from the BMP.  Contents of map
            undefined if not a colormapped BMP.
          */
-    unsigned int cmapsize;
-        /* Number of colormap entries.  Described in the BMP header.
-           Note that a file may be 8 bits per pixel but have less than
-           256 colors.  In the 1 bit per pixel case, there should be
-           2 entries according to the official specification, but we
-           allow files with just 1.
-	 */
+    unsigned int cmapSize;
+        /* Number of colormap entries.  From BMP header.  Note that a file may
+           be 8 bits per pixel but have fewer than 256 colors.  In the 1 bit
+           per pixel case, there should be 2 entries according to the official
+           specification, but we allow files with just 1.
+        */
 
     pm_proginit(&argc, argv);
 
@@ -1547,11 +1684,11 @@ main(int argc, const char ** argv) {
     ifP = pm_openr(cmdline.inputFileName);
     if (streq(cmdline.inputFileName, "-"))
         ifname = "Standard Input";
-    else 
+    else
         ifname = cmdline.inputFileName;
 
-    readBmp(ifP, &bmpRaster, &cols, &rows, &grayPresent, &colorPresent, 
-            &cBitCount, &pixelformat, &colormap, &cmapsize,
+    readBmp(ifP, &bmpRaster, &cols, &rows, &grayPresent, &colorPresent,
+            &cBitCount, &pixelformat, &colormap, &cmapSize,
             cmdline.verbose);
     pm_close(ifP);
 
@@ -1565,14 +1702,14 @@ main(int argc, const char ** argv) {
         outputType = PBM_TYPE;
         pm_message("WRITING PBM IMAGE");
     }
-    
+
     if (outputType == PBM_TYPE  && cBitCount == 1){
         pbm_writepbminit(stdout, cols, rows, FALSE);
         writeRasterPbm(bmpRaster, cols, rows, colormap);
     } else {
         pnm_writepnminit(stdout, cols, rows, bmpMaxval, outputType, FALSE);
         writeRasterGen(bmpRaster, cols, rows, outputType, cBitCount,
-                       pixelformat, colormap, cmapsize); 
+                       pixelformat, colormap, cmapSize);
     }
     free(colormap);
     free(bmpRaster);
diff --git a/converter/other/cameratopam/camera.c b/converter/other/cameratopam/camera.c
index a1adba95..439c9413 100644
--- a/converter/other/cameratopam/camera.c
+++ b/converter/other/cameratopam/camera.c
@@ -1,7 +1,6 @@
-#define _BSD_SOURCE
-    /* Make sure strcasecmp is in string.h */
-#define _XOPEN_SOURCE
-    /* Make sure putenv is in stdlib.h */
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
+#define _XOPEN_SOURCE 500
+    /* Make sure putenv is in stdlib.h, strcaseeq is in nstring.h */
 
 #include <stdlib.h>
 #include <string.h>
@@ -16,6 +15,7 @@
 #include "pm.h"
 #include "mallocvar.h"
 #include "pm_c_util.h"
+#include "nstring.h"
 
 #include "global_variables.h"
 #include "cameratopam.h"
@@ -46,8 +46,8 @@
 #define FORC3 for (c=0; c < 3; c++)
 #define FORC4 for (c=0; c < colors; c++)
 
-static void 
-merror (const void *ptr, const char *where) 
+static void
+merror (const void *ptr, const char *where)
 {
     if (ptr == NULL)
         pm_error ("Out of memory in %s", where);
@@ -56,7 +56,7 @@ merror (const void *ptr, const char *where)
 
 
 
-static void  
+static void
 adobeCopyPixel(Image             const image,
                unsigned int      const row,
                unsigned int      const col,
@@ -306,7 +306,7 @@ minolta_z2()
 
 void
 nikon_e2100_load_raw(Image const image) {
-        
+
   unsigned char   data[3432], *dp;
   unsigned short pixel[2288], *pix;
   int row, col;
@@ -383,7 +383,7 @@ fuji_s3_load_raw(Image const image) {
   }
 }
 
-static void 
+static void
 fuji_common_load_raw(Image        const image,
                      unsigned int const ncol,
                      unsigned int const icol,
@@ -1022,6 +1022,7 @@ static void  sony_decrypt (unsigned *data, int len, int start, int key)
 {
   static uint32_t pad[128];
   unsigned int p;
+  unsigned int i;
 
   if (start) {
     for (p=0; p < 4; p++)
@@ -1042,12 +1043,13 @@ static void  sony_decrypt (unsigned *data, int len, int start, int key)
         u.bytes[1] = pad[p] >> 16;
         u.bytes[2] = pad[p] >>  8;
         u.bytes[3] = pad[p] >>  0;
-        
+
         pad[p] = u.word;
     }
   }
-  while (len--)
-    *data++ ^= pad[p++ & 0x7f] = pad[(p+1) & 0x7f] ^ pad[(p+65) & 0x7f];
+  for (i = 0, p = 0; i < len; ++i, ++p) {
+    *data++ ^= pad[p & 0x7f] = pad[(p+1) & 0x7f] ^ pad[(p+65) & 0x7f];
+  }
 }
 
 void
@@ -1151,9 +1153,9 @@ static void  ciff_block_1030()
 /*
    Parse a CIFF file, better known as Canon CRW format.
  */
-void 
-parse_ciff(FILE * const ifp, 
-           int    const offset, 
+void
+parse_ciff(FILE * const ifp,
+           int    const offset,
            int    const length)
 {
   int tboff, nrecs, i, type, len, roff, aoff, save, wbi=-1;
@@ -1306,7 +1308,7 @@ parse_rollei(FILE * const ifp)
 
 
 void
-parse_mos(FILE * const ifp, 
+parse_mos(FILE * const ifp,
           int    const offset)
 {
     char data[40];
@@ -1355,7 +1357,7 @@ static double getrat()
 
 
 
-static void 
+static void
 parse_makernote(FILE * const ifp)
 {
   unsigned base=0, offset=0, entries, tag, type, len, save;
@@ -1501,15 +1503,15 @@ get_timestamp(FILE * const ifp)
     timestamp = ts;
 }
 
-static void 
+static void
 parse_exif(FILE * const ifp, int base)
 {
-  int entries, tag, type, len, val, save;
+  int entries, tag, len, val, save;
 
   entries = get2(ifp);
   while (entries--) {
     tag  = get2(ifp);
-    type = get2(ifp);
+    /* type = */ get2(ifp);
     len  = get4(ifp);
     val  = get4(ifp);
     save = ftell(ifp);
@@ -1526,7 +1528,7 @@ parse_exif(FILE * const ifp, int base)
   }
 }
 
-static int 
+static int
 parse_tiff_ifd(FILE * const ifp, int base, int level)
 {
   unsigned entries, tag, type, len, plen=16, save;
@@ -1687,7 +1689,7 @@ guess_cfa_pc:
       case 50723:           /* CameraCalibration1 */
       case 50724:           /* CameraCalibration2 */
     for (i=0; i < colors; i++)
-      FORC4 cc[i][c] = getrat();    
+      FORC4 cc[i][c] = getrat();
       case 50727:           /* AnalogBalance */
     FORC4 ab[c] = getrat();
     break;
@@ -1767,7 +1769,7 @@ parse_external_jpeg(const char * const ifname)
     strcpy (jname, ifname);
     jfile = jname + (file - ifname);
     jext  = jname + (ext  - ifname);
-    if (strcasecmp (ext, ".jpg")) {
+    if (!strcaseeq (ext, ".jpg")) {
         strcpy (jext, isupper(ext[1]) ? ".JPG":".jpg");
         memcpy (jfile, file+4, 4);
         memcpy (jfile+4, file, 4);
diff --git a/converter/other/cameratopam/cameratopam.c b/converter/other/cameratopam/cameratopam.c
index ec33dd31..2c832714 100644
--- a/converter/other/cameratopam/cameratopam.c
+++ b/converter/other/cameratopam/cameratopam.c
@@ -7,6 +7,7 @@
  */
 
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure string.h contains strdup() */
 #define _XOPEN_SOURCE 500
    /* Make sure unistd.h contains swab(), string.h constains strdup() */
diff --git a/converter/other/cameratopam/foveon.c b/converter/other/cameratopam/foveon.c
index a3e5449a..992f3883 100644
--- a/converter/other/cameratopam/foveon.c
+++ b/converter/other/cameratopam/foveon.c
@@ -1,6 +1,6 @@
 /* This code is licensed to the public by its copyright owners under GPL. */
 
-#define _XOPEN_SOURCE   /* get M_PI */
+#define _XOPEN_SOURCE 500  /* get M_PI in math.h */
 
 #include <stdio.h>
 #include <assert.h>
diff --git a/converter/other/cameratopam/identify.c b/converter/other/cameratopam/identify.c
index 02208be6..7e77adb2 100644
--- a/converter/other/cameratopam/identify.c
+++ b/converter/other/cameratopam/identify.c
@@ -1,3 +1,4 @@
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE   /* Make sure strcasecmp() is in string.h */
 #include <string.h>
 
diff --git a/converter/other/cameratopam/ljpeg.c b/converter/other/cameratopam/ljpeg.c
index 07791e25..331d258c 100644
--- a/converter/other/cameratopam/ljpeg.c
+++ b/converter/other/cameratopam/ljpeg.c
@@ -1,3 +1,4 @@
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE    /* Make sure string.h containst strcasecmp() */
 #include <stdlib.h>
 #include <string.h>
diff --git a/converter/other/fiasco/binerror.c b/converter/other/fiasco/binerror.c
index 77243c64..9820d853 100644
--- a/converter/other/fiasco/binerror.c
+++ b/converter/other/fiasco/binerror.c
@@ -7,8 +7,8 @@
  *  Credits:	Modelled after variable argument routines from Jef
  *		Poskanzer's pbmplus package. 
  *
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -18,6 +18,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _ERROR_C
diff --git a/converter/other/fiasco/binerror.h b/converter/other/fiasco/binerror.h
index e7ff43c9..4f313aca 100644
--- a/converter/other/fiasco/binerror.h
+++ b/converter/other/fiasco/binerror.h
@@ -4,8 +4,8 @@
  *  Written by:		Stefan Frank
  *			Ullrich Hafner
  *
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/buttons.c b/converter/other/fiasco/buttons.c
index 82ed18cd..fc54d84a 100644
--- a/converter/other/fiasco/buttons.c
+++ b/converter/other/fiasco/buttons.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/buttons.h b/converter/other/fiasco/buttons.h
index a09f3423..079715f5 100644
--- a/converter/other/fiasco/buttons.h
+++ b/converter/other/fiasco/buttons.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/approx.c b/converter/other/fiasco/codec/approx.c
index 5072fae3..d8fefcaa 100644
--- a/converter/other/fiasco/codec/approx.c
+++ b/converter/other/fiasco/codec/approx.c
@@ -1,10 +1,10 @@
 /*
- *  approx.c:		Approximation of range images with matching pursuit
+ *  approx.c:       Approximation of range images with matching pursuit
  *
- *  Written by:		Ullrich Hafner
- *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  Written by:     Ullrich Hafner
+ *      
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -34,7 +34,7 @@
 
 /*****************************************************************************
 
-			     local variables
+                 local variables
   
 *****************************************************************************/
 
@@ -52,227 +52,205 @@ typedef struct mp
 
 /*****************************************************************************
 
-			     prototypes
+                 prototypes
   
 *****************************************************************************/
 
 static void
 orthogonalize (unsigned index, unsigned n, unsigned level, real_t min_norm,
-	       const word_t *domain_blocks, const coding_t *c);
+           const word_t *domain_blocks, const coding_t *c);
 static void 
 matching_pursuit (mp_t *mp, bool_t full_search, real_t price,
-		  unsigned max_edges, int y_state, const range_t *range,
-		  const domain_pool_t *domain_pool, const coeff_t *coeff,
-		  const wfa_t *wfa, const coding_t *c);
+          unsigned max_edges, int y_state, const range_t *range,
+          const domain_pool_t *domain_pool, const coeff_t *coeff,
+          const wfa_t *wfa, const coding_t *c);
 
 /*****************************************************************************
 
-				public code
+                public code
   
 *****************************************************************************/
 
 real_t 
 approximate_range (real_t max_costs, real_t price, int max_edges,
-		   int y_state, range_t *range, domain_pool_t *domain_pool,
-		   coeff_t *coeff, const wfa_t *wfa, const coding_t *c)
-/*
- *  Approximate image block 'range' by matching pursuit. This functions
- *  calls the matching pursuit algorithm several times (with different
- *  parameters) in order to find the best approximation. Refer to function
- *  'matching_pursuit()' for more details about parameters.
- *
- *  Return value:
- *	approximation costs
- */
-{
-   mp_t	  mp;
-   bool_t success = NO;
-
-   /*
-    *  First approximation attempt: default matching pursuit algorithm.
-    */
-   mp.exclude [0] = NO_EDGE;
-   matching_pursuit (&mp, c->options.full_search, price, max_edges,
-		     y_state, range, domain_pool, coeff, wfa, c);
-
-   /*
-    *  Next approximation attempt: remove domain block mp->indices [0]
-    *  from domain pool (vector with smallest costs) and run the
-    *  matching pursuit again.
-    */
-   if (c->options.second_domain_block)
-   {
-      mp_t tmp_mp = mp;
+                   int y_state, range_t *range, domain_pool_t *domain_pool,
+                   coeff_t *coeff, const wfa_t *wfa, const coding_t *c) {
+/*---------------------------------------------------------------------------- 
+  Approximate image block 'range' by matching pursuit. This functions
+  calls the matching pursuit algorithm several times (with different
+  parameters) in order to find the best approximation. Refer to function
+  'matching_pursuit()' for more details about parameters.
+
+  Return value: approximation costs
+-----------------------------------------------------------------------------*/
+    mp_t mp;
+
+    /*
+     *  First approximation attempt: default matching pursuit algorithm.
+     */
+    mp.exclude [0] = NO_EDGE;
+    matching_pursuit(&mp, c->options.full_search, price, max_edges,
+                     y_state, range, domain_pool, coeff, wfa, c);
+
+    /*
+     *  Next approximation attempt: remove domain block mp->indices [0]
+     *  from domain pool (vector with smallest costs) and run the
+     *  matching pursuit again.
+     */
+    if (c->options.second_domain_block) {
+        mp_t tmp_mp;
       
-      tmp_mp.exclude [0] = tmp_mp.indices [0];
-      tmp_mp.exclude [1] = NO_EDGE;
-	    
-      matching_pursuit (&tmp_mp, c->options.full_search, price, max_edges,
-			y_state, range, domain_pool, coeff, wfa, c);
-      if (tmp_mp.costs < mp.costs)	/* success */
-      {
-	 success = YES;
-	 mp      = tmp_mp;
-      }
-   }
-
-   /*
-    *  Next approximation attempt: check whether some coefficients have
-    *  been quantized to zero. Vectors causing the underflow are
-    *  removed from the domain pool and then the matching pursuit
-    *  algorithm is run again (until underflow doesn't occur anymore).
-    */
-   if (c->options.check_for_underflow)
-   {
-      int  iteration = -1;
-      mp_t tmp_mp    = mp;
+        tmp_mp = mp;  /* initial value */
+
+        tmp_mp.exclude[0] = tmp_mp.indices [0];
+        tmp_mp.exclude[1] = NO_EDGE;
+        
+        matching_pursuit(&tmp_mp, c->options.full_search, price, max_edges,
+                         y_state, range, domain_pool, coeff, wfa, c);
+        if (tmp_mp.costs < mp.costs)  /* success */ 
+            mp = tmp_mp;
+    }
+
+    /*
+     *  Next approximation attempt: check whether some coefficients have
+     *  been quantized to zero. Vectors causing the underflow are
+     *  removed from the domain pool and then the matching pursuit
+     *  algorithm is run again (until underflow doesn't occur anymore).
+     */
+    if (c->options.check_for_underflow) {
+        mp_t tmp_mp;
+        int  iteration;
+
+        tmp_mp = mp;  /* initial value */
+        iteration = -1;  /* initial value */
       
-      do
-      {
-	 int i;
+        do {
+            int i;
+            
+            ++iteration;
+            tmp_mp.exclude[iteration] = NO_EDGE;
+     
+            for (i = 0; isdomain(tmp_mp.indices[i]); ++i) {
+                if (tmp_mp.weight [i] == 0) {
+                    tmp_mp.exclude[iteration] = tmp_mp.indices [i];
+                    break;
+                }
+            }      
+            if (isdomain (tmp_mp.exclude [iteration])) {
+                /* try again */
+                tmp_mp.exclude [iteration + 1] = NO_EDGE;
+        
+                matching_pursuit(&tmp_mp, c->options.full_search, price,
+                                 max_edges, y_state, range, domain_pool,
+                                 coeff, wfa, c);
+                if (tmp_mp.costs < mp.costs)     /* success */
+                    mp = tmp_mp;
+            }
+        } while (isdomain (tmp_mp.exclude [iteration])
+                 && iteration < MAXEDGES - 1);
+    }
+
+    /*
+     *  Next approximation attempt: check whether some coefficients have
+     *  been quantized to +/- max-value. Vectors causing the overflow are
+     *  removed from the domain pool and then the matching pursuit
+     *  algorithm is run again (until overflow doesn't occur anymore).
+     */
+    if (c->options.check_for_overflow) {
+        mp_t tmp_mp;
+        int  iteration;
+
+        tmp_mp = mp;  /* initial value */
+        iteration = -1;  /* initial value */
+
+        do {
+            int i;
  
-	 iteration++;
-	 tmp_mp.exclude [iteration] = NO_EDGE;
-	 
-	 for (i = 0; isdomain (tmp_mp.indices [i]); i++)
-	    if (tmp_mp.weight [i] == 0)
-	    {
-	       tmp_mp.exclude [iteration] = tmp_mp.indices [i];
-	       break;
-	    }
+            ++iteration;
+            tmp_mp.exclude[iteration] = NO_EDGE;
+     
+            for (i = 0; isdomain (tmp_mp.indices [i]); ++i) {
+                rpf_t * const rpf =
+                    tmp_mp.indices [i] ? coeff->rpf : coeff->dc_rpf;
+        
+                if (tmp_mp.weight [i] == btor (rtob (200, rpf), rpf)
+                    || tmp_mp.weight [i] == btor (rtob (-200, rpf), rpf)) {
+                    tmp_mp.exclude [iteration] = tmp_mp.indices [i];
+                    break;
+                }
+            }
       
-	 if (isdomain (tmp_mp.exclude [iteration])) /* try again */
-	 {
-	    tmp_mp.exclude [iteration + 1] = NO_EDGE;
-	    
-	    matching_pursuit (&tmp_mp, c->options.full_search, price,
-			      max_edges, y_state, range, domain_pool,
-			      coeff, wfa, c);
-	    if (tmp_mp.costs < mp.costs)	/* success */
-	    {
-	       success = YES;
-	       mp      = tmp_mp;
-	    }
-	 }
-      } while (isdomain (tmp_mp.exclude [iteration])
-	       && iteration < MAXEDGES - 1);
-   }
-
-   /*
-    *  Next approximation attempt: check whether some coefficients have
-    *  been quantized to +/- max-value. Vectors causing the overflow are
-    *  removed from the domain pool and then the matching pursuit
-    *  algorithm is run again (until overflow doesn't occur anymore).
-    */
-   if (c->options.check_for_overflow)
-   {
-      int  iteration = -1;
-      mp_t tmp_mp    = mp;
-      
-      do
-      {
-	 int i;
- 
-	 iteration++;
-	 tmp_mp.exclude [iteration] = NO_EDGE;
-	 
-	 for (i = 0; isdomain (tmp_mp.indices [i]); i++)
-	 {
-	    rpf_t *rpf = tmp_mp.indices [i] ? coeff->rpf : coeff->dc_rpf;
-	    
-	    if (tmp_mp.weight [i] == btor (rtob (200, rpf), rpf)
-		|| tmp_mp.weight [i] == btor (rtob (-200, rpf), rpf))
-	    {
-	       tmp_mp.exclude [iteration] = tmp_mp.indices [i];
-	       break;
-	    }
-	 }
+            if (isdomain(tmp_mp.exclude[iteration])) {
+                /* try again */
+                tmp_mp.exclude[iteration + 1] = NO_EDGE;
+        
+                matching_pursuit(&tmp_mp, c->options.full_search, price,
+                                 max_edges, y_state, range, domain_pool,
+                                 coeff, wfa, c);
+                if (tmp_mp.costs < mp.costs)   /* success */
+                    mp = tmp_mp;
+            }
+        } while (isdomain (tmp_mp.exclude [iteration])
+                 && iteration < MAXEDGES - 1);
+    }
+
+    /*
+     *  Finally, check whether the best approximation has costs
+     *  smaller than 'max_costs'.
+     */
+    if (mp.costs < max_costs) {
+        int    edge;
+        int    new_index, old_index;
+
+        new_index = 0;
+        for (old_index = 0; isdomain (mp.indices[old_index]); ++old_index) {
+            if (mp.weight [old_index] != 0) {
+                mp.indices [new_index] = mp.indices [old_index];
+                mp.into    [new_index] = mp.into    [old_index];
+                mp.weight  [new_index] = mp.weight  [old_index];
+                ++new_index;
+            }
+        }
+        mp.indices [new_index] = NO_EDGE;
+        mp.into    [new_index] = NO_EDGE;
+
+        /*
+         *  Update of probability models
+         */
+        {
+            word_t * const domain_blocks =
+                domain_pool->generate(range->level, y_state,
+                                      wfa,
+                                      domain_pool->model);
+            domain_pool->update(domain_blocks, mp.indices,
+                                range->level, y_state, wfa,
+                                domain_pool->model);
+            coeff->update (mp.weight, mp.into, range->level, coeff);
+     
+            Free(domain_blocks);
+        }
       
-	 if (isdomain (tmp_mp.exclude [iteration])) /* try again */
-	 {
-	    tmp_mp.exclude [iteration + 1] = NO_EDGE;
-	    
-	    matching_pursuit (&tmp_mp, c->options.full_search, price,
-			      max_edges, y_state, range, domain_pool,
-			      coeff, wfa, c);
-	    if (tmp_mp.costs < mp.costs)	/* success */
-	    {
-	       success = YES;
-	       mp      = tmp_mp;
-	    }
-	 }
-      } while (isdomain (tmp_mp.exclude [iteration])
-	       && iteration < MAXEDGES - 1);
-   }
-
-   /*
-    *  Finally, check whether the best approximation has costs
-    *  smaller than 'max_costs'.
-    */
-   if (mp.costs < max_costs) 
-   {
-      int    edge;
-      bool_t overflow  = NO;
-      bool_t underflow = NO;
-      int    new_index, old_index;
-
-      new_index = 0;
-      for (old_index = 0; isdomain (mp.indices [old_index]); old_index++)
-	 if (mp.weight [old_index] != 0)
-	 {
-	    rpf_t *rpf = mp.indices [old_index] ? coeff->rpf : coeff->dc_rpf;
-	    
-	    if (mp.weight [old_index] == btor (rtob (200, rpf), rpf)
-		|| mp.weight [old_index] == btor (rtob (-200, rpf), rpf))
-	       overflow = YES;
-	    
-	    mp.indices [new_index] = mp.indices [old_index];
-	    mp.into [new_index]    = mp.into [old_index];
-	    mp.weight [new_index]  = mp.weight [old_index];
-	    new_index++;
-	 }
-	 else
-	    underflow = YES;
-      
-      mp.indices [new_index] = NO_EDGE;
-      mp.into  [new_index]   = NO_EDGE;
-
-      /*
-       *  Update of probability models
-       */
-      {
-	 word_t *domain_blocks = domain_pool->generate (range->level, y_state,
-							wfa,
-							domain_pool->model);
-	 domain_pool->update (domain_blocks, mp.indices,
-			      range->level, y_state, wfa, domain_pool->model);
-	 coeff->update (mp.weight, mp.into, range->level, coeff);
-	 
-	 Free (domain_blocks);
-      }
-      
-      for (edge = 0; isedge (mp.indices [edge]); edge++)
-      {
-	 range->into [edge]   = mp.into [edge];
-	 range->weight [edge] = mp.weight [edge];
-      }
-      range->into [edge]  = NO_EDGE;
-      range->matrix_bits  = mp.matrix_bits;
-      range->weights_bits = mp.weights_bits;
-      range->err          = mp.err;
-   }
-   else
-   {
-      range->into [0] = NO_EDGE;
-      mp.costs	      = MAXCOSTS;
-   }
+        for (edge = 0; isedge (mp.indices [edge]); ++edge) {
+            range->into   [edge] = mp.into   [edge];
+            range->weight [edge] = mp.weight [edge];
+        }
+        range->into [edge]  = NO_EDGE;
+        range->matrix_bits  = mp.matrix_bits;
+        range->weights_bits = mp.weights_bits;
+        range->err          = mp.err;
+    } else {
+        range->into [0] = NO_EDGE;
+        mp.costs        = MAXCOSTS;
+    }
    
-   return mp.costs;
+    return mp.costs;
 }
 
+
+
 /*****************************************************************************
 
-			     local variables
+                 local variables
   
 *****************************************************************************/
 
@@ -310,15 +288,15 @@ static bool_t used [MAXSTATES];
 
 /*****************************************************************************
 
-				private code
+                private code
   
 *****************************************************************************/
 
 static void 
 matching_pursuit (mp_t *mp, bool_t full_search, real_t price,
-		  unsigned max_edges, int y_state, const range_t *range,
-		  const domain_pool_t *domain_pool, const coeff_t *coeff,
-		  const wfa_t *wfa, const coding_t *c)
+                  unsigned max_edges, int y_state, const range_t *range,
+                  const domain_pool_t *domain_pool, const coeff_t *coeff,
+                  const wfa_t *wfa, const coding_t *c)
 /*
  *  Find an approximation of the current 'range' with a linear
  *  combination of vectors of the 'domain_pool'. The linear
@@ -339,311 +317,318 @@ matching_pursuit (mp_t *mp, bool_t full_search, real_t price,
  *  No return value.
  *
  *  Side effects:
- *	vectors, factors, rate, distortion and costs are stored in 'mp'
+ *  vectors, factors, rate, distortion and costs are stored in 'mp'
  */
 {
-   unsigned	 n;			/* current vector of the OB */
-   int		 index;			/* best fitting domain image */
-   unsigned	 domain;		/* counter */
-   real_t	 norm;			/* norm of range image */
-   real_t	 additional_bits;	/* bits for mc, nd, and tree */
-   word_t	*domain_blocks;		/* current set of domain images */
-   const real_t  min_norm = 2e-3;	/* lower bound of norm */
-   unsigned 	 best_n   = 0;
-   unsigned	 size 	  = size_of_level (range->level);
+    unsigned  n;         /* current vector of the OB */
+    int       index;         /* best fitting domain image */
+    unsigned  domain;        /* counter */
+    real_t    norm;          /* norm of range image */
+    real_t    additional_bits;   /* bits for mc, nd, and tree */
+    word_t   *domain_blocks;     /* current set of domain images */
+    const real_t  min_norm = 2e-3;   /* lower bound of norm */
+    unsigned      best_n   = 0;
+    unsigned  size     = size_of_level (range->level);
  
-   /*
-    *  Initialize domain pool and inner product arrays
-    */
-   domain_blocks = domain_pool->generate (range->level, y_state, wfa,
-					  domain_pool->model);
-   for (domain = 0; domain_blocks [domain] >= 0; domain++)
-   {
-      used [domain] = NO;
-      rem_denominator [domain]		/* norm of domain */
-	 = get_ip_state_state (domain_blocks [domain], domain_blocks [domain],
-			       range->level, c);
-      if (rem_denominator [domain] / size < min_norm)
-	 used [domain] = YES;		/* don't use domains with small norm */
-      else
-	 rem_numerator [domain]		/* inner product <s_domain, b> */
-	    = get_ip_image_state (range->image, range->address,
-				  range->level, domain_blocks [domain], c);
-      if (!used [domain] && fabs (rem_numerator [domain]) < min_norm)
-	 used [domain] = YES;
-   }
-
-   /*
-    *  Exclude all domain blocks given in array 'mp->exclude'
-    */
-   for (n = 0; isdomain (mp->exclude [n]); n++)
-      used [mp->exclude [n]] = YES;
-
-   /*
-    *  Compute the approximation costs if 'range' is approximated with
-    *  no linear combination, i.e. the error is equal to the square
-    *  of the image norm and the size of the automaton is determined by
-    *  storing only zero elements in the current matrix row
-    */
-   for (norm = 0, n = 0; n < size; n++)
-      norm += square (c->pixels [range->address * size + n]);
-
-   additional_bits = range->tree_bits + range->mv_tree_bits
-		     + range->mv_coord_bits + range->nd_tree_bits
-		     + range->nd_weights_bits;
-
-   mp->err          = norm;
-   mp->weights_bits = 0;
-   mp->matrix_bits  = domain_pool->bits (domain_blocks, NULL, range->level,
-					 y_state, wfa, domain_pool->model);
-   mp->costs        = (mp->matrix_bits + mp->weights_bits
-		       + additional_bits) * price + mp->err;
-
-   n = 0;
-   do 
-   {
-      /*
-       *  Current approximation is: b = d_0 o_0 + ... + d_(n-1) o_(n-1)
-       *  with corresponding costs 'range->err + range->bits * p'.
-       *  For all remaining state images s_i (used[s_i] == NO) set
-       *  o_n :	= s_i - \sum(k = 0, ... , n-1) {(<s_i, o_k> / ||o_k||^2) o_k}
-       *  and try to beat current costs.
-       *  Choose that vector for the next orthogonalization step,
-       *  which has minimal costs: s_index.
-       *  (No progress is indicated by index == -1)
-       */
+    /*
+     *  Initialize domain pool and inner product arrays
+     */
+    domain_blocks = domain_pool->generate (range->level, y_state, wfa,
+                                           domain_pool->model);
+    for (domain = 0; domain_blocks [domain] >= 0; domain++)
+    {
+        used [domain] = NO;
+        rem_denominator [domain] =     /* norm of domain */
+            get_ip_state_state (domain_blocks [domain], domain_blocks [domain],
+                                range->level, c);
+        if (rem_denominator [domain] / size < min_norm)
+            used [domain] = YES;       /* don't use domains with small norm */
+        else
+            rem_numerator [domain]     /* inner product <s_domain, b> */
+                = get_ip_image_state (range->image, range->address,
+                                      range->level, domain_blocks [domain], c);
+        if (!used [domain] && fabs (rem_numerator [domain]) < min_norm)
+            used [domain] = YES;
+    }
+
+    /*
+     *  Exclude all domain blocks given in array 'mp->exclude'
+     */
+    for (n = 0; isdomain (mp->exclude [n]); n++)
+        used [mp->exclude [n]] = YES;
+
+    /*
+     *  Compute the approximation costs if 'range' is approximated with
+     *  no linear combination, i.e. the error is equal to the square
+     *  of the image norm and the size of the automaton is determined by
+     *  storing only zero elements in the current matrix row
+     */
+    for (norm = 0, n = 0; n < size; n++)
+        norm += square (c->pixels [range->address * size + n]);
+
+    additional_bits = range->tree_bits + range->mv_tree_bits
+        + range->mv_coord_bits + range->nd_tree_bits
+        + range->nd_weights_bits;
+
+    mp->err          = norm;
+    mp->weights_bits = 0;
+    mp->matrix_bits  = domain_pool->bits (domain_blocks, NULL, range->level,
+                                          y_state, wfa, domain_pool->model);
+    mp->costs        = (mp->matrix_bits + mp->weights_bits
+                        + additional_bits) * price + mp->err;
+
+    n = 0;
+    do 
+    {
+        /*
+         *  Current approximation is: b = d_0 o_0 + ... + d_(n-1) o_(n-1)
+         *  with corresponding costs 'range->err + range->bits * p'.
+         *  For all remaining state images s_i (used[s_i] == NO) set
+         *  o_n : = s_i - \sum(k = 0, ... , n-1) {(<s_i, o_k> / ||o_k||^2) o_k}
+         *  and try to beat current costs.
+         *  Choose that vector for the next orthogonalization step,
+         *  which has minimal costs: s_index.
+         *  (No progress is indicated by index == -1)
+         */
       
-      real_t min_matrix_bits  = 0;
-      real_t min_weights_bits = 0;
-      real_t min_error 	      = 0;
-      real_t min_weight [MAXEDGES];
-      real_t min_costs = full_search ? MAXCOSTS : mp->costs;
+        real_t min_matrix_bits  = 0;
+        real_t min_weights_bits = 0;
+        real_t min_error        = 0;
+        real_t min_weight [MAXEDGES];
+        real_t min_costs = full_search ? MAXCOSTS : mp->costs;
       
-      for (index = -1, domain = 0; domain_blocks [domain] >= 0; domain++) 
-	 if (!used [domain]) 
-	 {
-	    real_t    matrix_bits, weights_bits;
-	    /*
-	     *  To speed up the search through the domain images,
-	     *  the costs of using domain image 'domain' as next vector
-	     *  can be approximated in a first step:
-	     *  improvement of image quality
-	     *	  <= square (rem_numerator[domain]) / rem_denominator[domain]
-	     */
-	    {
-		  word_t   vectors [MAXEDGES + 1];
-		  word_t   states [MAXEDGES + 1];
-		  real_t   weights [MAXEDGES + 1];
-		  unsigned i, k;
-		  
-		  for (i = 0, k = 0; k < n; k++)
-		     if (mp->weight [k] != 0)
-		     {
-			vectors [i] = mp->indices [k];
-			states [i]  = domain_blocks [vectors [i]];
-			weights [i] = mp->weight [k];
-			i++;
-		     }
-		  vectors [i] 	  = domain;
-		  states [i]  	  = domain_blocks [domain];
-		  weights [i] 	  = 0.5;
-		  vectors [i + 1] = -1;
-		  states [i + 1]  = -1;
-
-		  weights_bits = coeff->bits (weights, states, range->level,
-					      coeff);
-		  matrix_bits = domain_pool->bits (domain_blocks, vectors,
-						   range->level, y_state,
-						   wfa, domain_pool->model);
-	    }
-	    if (((matrix_bits + weights_bits + additional_bits) * price +
-		 mp->err -
-		 square (rem_numerator [domain]) / rem_denominator [domain])
-		< min_costs)
-	    {
-	       /*
-		*  1.) Compute the weights (linear factors) c_i of the
-		*  linear combination
-		*  b = c_0 v_0 + ... + c_(n-1) v_(n-1) + c_n v_'domain'
-		*  Use backward substitution to obtain c_i from the linear
-		*  factors of the lin. comb. b = d_0 o_0 + ... + d_n o_n
-		*  of the corresponding orthogonal vectors {o_0, ..., o_n}.
-		*  Vector o_n of the orthogonal basis is obtained by using
-		*  vector 'v_domain' in step n of the Gram Schmidt
-		*  orthogonalization (see above for definition of o_n).
-		*  Recursive formula for the coefficients c_i:
-		*  c_n := <b, o_n> / ||o_n||^2
-		*  for i = n - 1, ... , 0:
-		*  c_i := <b, o_i> / ||o_i||^2 +
-		*          \sum (k = i + 1, ... , n){ c_k <v_k, o_i>
-		*					/ ||o_i||^2 }
-		*  2.) Because linear factors are stored with reduced precision
-		*  factor c_i is rounded with the given precision in step i
-		*  of the recursive formula. 
-		*/
-
-	       unsigned k;		/* counter */
-	       int    	l;		/* counter */
-	       real_t 	m_bits;		/* number of matrix bits to store */
-	       real_t 	w_bits;		/* number of weights bits to store */
-	       real_t 	r [MAXEDGES];	/* rounded linear factors */
-	       real_t 	f [MAXEDGES];	/* linear factors */
-	       int    	v [MAXEDGES];	/* mapping of domains to vectors */
-	       real_t 	costs;		/* current approximation costs */
-	       real_t 	m_err;		/* current approximation error */
-
-	       f [n] = rem_numerator [domain] / rem_denominator [domain];
-	       v [n] = domain;		/* corresponding mapping */
-	       for (k = 0; k < n; k++)
-	       {
-		  f [k] = ip_image_ortho_vector [k] / norm_ortho_vector [k];
-		  v [k] = mp->indices [k];
-	       }
-	    
-	       for (l = n; l >= 0; l--) 
-	       {
-		  rpf_t *rpf = domain_blocks [v [l]]
-			       ? coeff->rpf : coeff->dc_rpf;
-
-		  r [l] = f [l] = btor (rtob (f [l], rpf), rpf);
-		     
-		  for (k = 0; k < (unsigned) l; k++)
-		     f [k] -= f [l] * ip_domain_ortho_vector [v [l]][k]
-			      / norm_ortho_vector [k] ;
-	       } 
-
-	       /*
-		*  Compute the number of output bits of the linear combination
-		*  and store the weights with reduced precision. The
-		*  resulting linear combination is
-		*  b = r_0 v_0 + ... + r_(n-1) v_(n-1) + r_n v_'domain'
-		*/
-	       {
-		  word_t vectors [MAXEDGES + 1];
-		  word_t states [MAXEDGES + 1];
-		  real_t weights [MAXEDGES + 1];
-		  int	 i;
-		  
-		  for (i = 0, k = 0; k <= n; k++)
-		     if (f [k] != 0)
-		     {
-			vectors [i] = v [k];
-			states [i]  = domain_blocks [v [k]];
-			weights [i] = f [k];
-			i++;
-		     }
-		  vectors [i] = -1;
-		  states [i]  = -1;
-
-		  w_bits = coeff->bits (weights, states, range->level, coeff);
-		  m_bits = domain_pool->bits (domain_blocks, vectors,
-					      range->level, y_state,
-					      wfa, domain_pool->model);
-	       }
-	       
-	       /*
-		*  To compute the approximation error, the corresponding
-		*  linear factors of the linear combination 
-		*  b = r_0 o_0 + ... + r_(n-1) o_(n-1) + r_n o_'domain'
-		*  with orthogonal vectors must be computed with following
-		*  formula:
-		*  r_i := r_i +
-		*          \sum (k = i + 1, ... , n) { r_k <v_k, o_i>
-		*					/ ||o_i||^2 }
-		*/
-	       for (l = 0; (unsigned) l <= n; l++)
-	       {
-		  /*
-		   *  compute <v_n, o_n>
-		   */
-		  real_t a;
-
-		  a = get_ip_state_state (domain_blocks [v [l]],
-					  domain_blocks [domain],
-					  range->level, c);
-		  for (k = 0; k < n; k++) 
-		     a -= ip_domain_ortho_vector [v [l]][k]
-			  / norm_ortho_vector [k]
-			  * ip_domain_ortho_vector [domain][k];
-		  ip_domain_ortho_vector [v [l]][n] = a;
-	       }
-	       norm_ortho_vector [n]     = rem_denominator [domain];
-	       ip_image_ortho_vector [n] = rem_numerator [domain];
- 	    
-	       for (k = 0; k <= n; k++)
-		  for (l = k + 1; (unsigned) l <= n; l++)
-		     r [k] += ip_domain_ortho_vector [v [l]][k] * r [l]
-			      / norm_ortho_vector [k];
-	       /*
-		*  Compute approximation error:
-		*  error := ||b||^2 +
-		*  \sum (k = 0, ... , n){r_k^2 ||o_k||^2 - 2 r_k <b, o_k>}
-		*/
-	       m_err = norm;
-	       for (k = 0; k <= n; k++)
-		  m_err += square (r [k]) * norm_ortho_vector [k]
-			 - 2 * r [k] * ip_image_ortho_vector [k];
-	       if (m_err < 0)		/* TODO: return MAXCOSTS */
-		  warning ("Negative image norm: %f"
-			   " (current domain: %d, level = %d)",
-			   (double) m_err, domain, range->level);
-
-	       costs = (m_bits + w_bits + additional_bits) * price + m_err;
-	       if (costs < min_costs)	/* found a better approximation */
-	       {
-		  index            = domain;
-		  min_costs        = costs;
-		  min_matrix_bits  = m_bits;
-		  min_weights_bits = w_bits;
-		  min_error        = m_err;
-		  for (k = 0; k <= n; k++)
-		     min_weight [k] = f [k];
-	       }
-	    }
-	 }
+        for (index = -1, domain = 0; domain_blocks [domain] >= 0; domain++) 
+            if (!used [domain]) 
+            {
+                real_t    matrix_bits, weights_bits;
+                /*
+                 *  To speed up the search through the domain images,
+                 *  the costs of using domain image 'domain' as next vector
+                 *  can be approximated in a first step:
+                 *  improvement of image quality
+                 *  <= square (rem_numerator[domain]) / rem_denominator[domain]
+                 */
+                {
+                    word_t   vectors [MAXEDGES + 1];
+                    word_t   states [MAXEDGES + 1];
+                    real_t   weights [MAXEDGES + 1];
+                    unsigned i, k;
+          
+                    for (i = 0, k = 0; k < n; k++)
+                        if (mp->weight [k] != 0)
+                        {
+                            vectors [i] = mp->indices [k];
+                            states [i]  = domain_blocks [vectors [i]];
+                            weights [i] = mp->weight [k];
+                            i++;
+                        }
+                    vectors [i]     = domain;
+                    states [i]      = domain_blocks [domain];
+                    weights [i]     = 0.5;
+                    vectors [i + 1] = -1;
+                    states [i + 1]  = -1;
+
+                    weights_bits = coeff->bits (weights, states, range->level,
+                                                coeff);
+                    matrix_bits = domain_pool->bits (domain_blocks, vectors,
+                                                     range->level, y_state,
+                                                     wfa, domain_pool->model);
+                }
+                if (((matrix_bits + weights_bits + additional_bits) * price +
+                     mp->err -
+                     square (rem_numerator[domain]) / rem_denominator[domain])
+                    < min_costs)
+                {
+                    /*
+                     *  1.) Compute the weights (linear factors) c_i of the
+                     *  linear combination
+                     *  b = c_0 v_0 + ... + c_(n-1) v_(n-1) + c_n v_'domain'
+                     *  Use backward substitution to obtain c_i from the linear
+                     *  factors of the lin. comb. b = d_0 o_0 + ... + d_n o_n
+                     *  of the corresponding orthogonal vectors {o_0, ..., o_n}.
+                     *  Vector o_n of the orthogonal basis is obtained by using
+                     *  vector 'v_domain' in step n of the Gram Schmidt
+                     *  orthogonalization (see above for definition of o_n).
+                     *  Recursive formula for the coefficients c_i:
+                     *  c_n := <b, o_n> / ||o_n||^2
+                     *  for i = n - 1, ... , 0:
+                     *  c_i := <b, o_i> / ||o_i||^2 +
+                     *          \sum (k = i + 1, ... , n){ c_k <v_k, o_i>
+                     *                   / ||o_i||^2 }
+                     *  2.) Because linear factors are stored with reduced
+                     *  precision factor c_i is rounded with the given
+                     *  precision in step i of the recursive formula.
+                     */
+
+                    unsigned k;      /* counter */
+                    int      l;      /* counter */
+                    real_t   m_bits;     /* number of matrix bits to store */
+                    real_t   w_bits;     /* number of weights bits to store */
+                    real_t   r [MAXEDGES];   /* rounded linear factors */
+                    real_t   f [MAXEDGES];   /* linear factors */
+                    int      v [MAXEDGES];   /* mapping of domains to vectors*/
+                    real_t   costs;      /* current approximation costs */
+                    real_t   m_err;      /* current approximation error */
+
+                    f [n] = rem_numerator [domain] / rem_denominator [domain];
+                    v [n] = domain;      /* corresponding mapping */
+                    for (k = 0; k < n; k++)
+                    {
+                        f[k] = ip_image_ortho_vector[k] / norm_ortho_vector[k];
+                        v [k] = mp->indices [k];
+                    }
+        
+                    for (l = n; l >= 0; --l) {
+                        rpf_t * const rpf = domain_blocks[v[l]]
+                            ? coeff->rpf : coeff->dc_rpf;
+
+                        unsigned int k;
+
+                        r[l] = f[l] = btor(rtob(f[l], rpf), rpf);
+
+                        {
+                            real_t const fl = f[l];
+             
+                            for (k = 0; k < l; ++k) {
+                                f[k] -= fl * ip_domain_ortho_vector[v[l]][k]
+                                    / norm_ortho_vector[k];
+                            }
+                        }
+                    } 
+
+                    /*
+                     *  Compute the number of output bits of the linear
+                     *  combination and store the weights with reduced
+                     *  precision. The resulting linear combination is
+                     *  b = r_0 v_0 + ... + r_(n-1) v_(n-1) + r_n v_'domain'
+                     */
+                    {
+                        word_t vectors [MAXEDGES + 1];
+                        word_t states [MAXEDGES + 1];
+                        real_t weights [MAXEDGES + 1];
+                        int    i;
+          
+                        for (i = 0, k = 0; k <= n; k++)
+                            if (f [k] != 0)
+                            {
+                                vectors [i] = v [k];
+                                states [i]  = domain_blocks [v [k]];
+                                weights [i] = f [k];
+                                i++;
+                            }
+                        vectors [i] = -1;
+                        states [i]  = -1;
+
+                        w_bits =
+                            coeff->bits(weights, states, range->level, coeff);
+                        m_bits = domain_pool->bits (domain_blocks, vectors,
+                                                    range->level, y_state,
+                                                    wfa, domain_pool->model);
+                    }
+           
+                    /*
+                     *  To compute the approximation error, the corresponding
+                     *  linear factors of the linear combination 
+                     *  b = r_0 o_0 + ... + r_(n-1) o_(n-1) + r_n o_'domain'
+                     *  with orthogonal vectors must be computed with following
+                     *  formula:
+                     *  r_i := r_i +
+                     *          \sum (k = i + 1, ... , n) { r_k <v_k, o_i>
+                     *                   / ||o_i||^2 }
+                     */
+                    for (l = 0; (unsigned) l <= n; l++)
+                    {
+                        /*
+                         *  compute <v_n, o_n>
+                         */
+                        real_t a;
+
+                        a = get_ip_state_state (domain_blocks [v [l]],
+                                                domain_blocks [domain],
+                                                range->level, c);
+                        for (k = 0; k < n; k++) 
+                            a -= ip_domain_ortho_vector [v [l]][k]
+                                / norm_ortho_vector [k]
+                                * ip_domain_ortho_vector [domain][k];
+                        ip_domain_ortho_vector [v [l]][n] = a;
+                    }
+                    norm_ortho_vector [n]     = rem_denominator [domain];
+                    ip_image_ortho_vector [n] = rem_numerator [domain];
+        
+                    for (k = 0; k <= n; k++)
+                        for (l = k + 1; (unsigned) l <= n; l++)
+                            r [k] += ip_domain_ortho_vector [v [l]][k] * r [l]
+                                / norm_ortho_vector [k];
+                    /*
+                     *  Compute approximation error:
+                     *  error := ||b||^2 +
+                     *  \sum (k = 0, ... , n){r_k^2 ||o_k||^2 - 2 r_k <b, o_k>}
+                     */
+                    m_err = norm;
+                    for (k = 0; k <= n; k++)
+                        m_err += square (r [k]) * norm_ortho_vector [k]
+                            - 2 * r [k] * ip_image_ortho_vector [k];
+                    if (m_err < 0)       /* TODO: return MAXCOSTS */
+                        warning ("Negative image norm: %f"
+                                 " (current domain: %d, level = %d)",
+                                 (double) m_err, domain, range->level);
+
+                    costs = (m_bits + w_bits + additional_bits) * price + m_err;
+                    if (costs < min_costs)   /* found a better approximation */
+                    {
+                        index            = domain;
+                        min_costs        = costs;
+                        min_matrix_bits  = m_bits;
+                        min_weights_bits = w_bits;
+                        min_error        = m_err;
+                        for (k = 0; k <= n; k++)
+                            min_weight [k] = f [k];
+                    }
+                }
+            }
       
-      if (index >= 0)			/* found a better approximation */
-      {
-	 if (min_costs < mp->costs)
-	 {
-	    unsigned k;
-	    
-	    mp->costs        = min_costs;
-	    mp->err          = min_error;
-	    mp->matrix_bits  = min_matrix_bits;
-	    mp->weights_bits = min_weights_bits;
-	    
-	    for (k = 0; k <= n; k++)
-	       mp->weight [k] = min_weight [k];
-
-	    best_n = n + 1;
-	 }
-	 
-	 mp->indices [n] = index;
-	 mp->into [n]    = domain_blocks [index];
-
-	 used [index] = YES;
-
-	 /* 
-	  *  Gram-Schmidt orthogonalization step n 
-	  */
-	 orthogonalize (index, n, range->level, min_norm, domain_blocks, c);
-	 n++;
-      }	
-   } 
-   while (n < max_edges && index >= 0);
-
-   mp->indices [best_n] = NO_EDGE;
+        if (index >= 0)           /* found a better approximation */
+        {
+            if (min_costs < mp->costs)
+            {
+                unsigned k;
+        
+                mp->costs        = min_costs;
+                mp->err          = min_error;
+                mp->matrix_bits  = min_matrix_bits;
+                mp->weights_bits = min_weights_bits;
+        
+                for (k = 0; k <= n; k++)
+                    mp->weight [k] = min_weight [k];
+
+                best_n = n + 1;
+            }
+     
+            mp->indices [n] = index;
+            mp->into [n]    = domain_blocks [index];
+
+            used [index] = YES;
+
+            /* 
+             *  Gram-Schmidt orthogonalization step n 
+             */
+            orthogonalize (index, n, range->level, min_norm, domain_blocks, c);
+            n++;
+        } 
+    } 
+    while (n < max_edges && index >= 0);
+
+    mp->indices [best_n] = NO_EDGE;
    
-   mp->costs = (mp->matrix_bits + mp->weights_bits + additional_bits) * price
-	       + mp->err;
+    mp->costs = (mp->matrix_bits + mp->weights_bits + additional_bits) * price
+        + mp->err;
 
-   Free (domain_blocks);
+    Free (domain_blocks);
 }
 
 static void
 orthogonalize (unsigned index, unsigned n, unsigned level, real_t min_norm,
-	       const word_t *domain_blocks, const coding_t *c)
+           const word_t *domain_blocks, const coding_t *c)
 /*
  *  Step 'n' of the Gram-Schmidt orthogonalization procedure:
  *  vector 'index' is orthogonalized with respect to the set
@@ -655,8 +640,8 @@ orthogonalize (unsigned index, unsigned n, unsigned level, real_t min_norm,
  *  No return value.
  *
  *  Side effects:
- *	The remainder values (numerator and denominator) of
- *	all 'domain_blocks' are updated. 
+ *  The remainder values (numerator and denominator) of
+ *  all 'domain_blocks' are updated. 
  */
 {
    unsigned domain;
@@ -676,25 +661,25 @@ orthogonalize (unsigned index, unsigned n, unsigned level, real_t min_norm,
    for (domain = 0; domain_blocks [domain] >= 0; domain++) 
       if (!used [domain]) 
       {
-	 unsigned k;
-	 real_t   tmp = get_ip_state_state (domain_blocks [index],
-					    domain_blocks [domain], level, c);
-	 
-	 for (k = 0; k < n; k++) 
-	    tmp -= ip_domain_ortho_vector [domain][k] / norm_ortho_vector [k]
-		   * ip_domain_ortho_vector [index][k];
-	 ip_domain_ortho_vector [domain][n] = tmp;
-	 rem_denominator [domain] -= square (tmp) / norm_ortho_vector [n];
-	 rem_numerator [domain]   -= ip_image_ortho_vector [n]
-				     / norm_ortho_vector [n]
-				     * ip_domain_ortho_vector [domain][n] ;
-
-	 /*
-	  *  Exclude vectors with small denominator
-	  */
-	 if (!used [domain]) 
-	    if (rem_denominator [domain] / size_of_level (level) < min_norm) 
-	       used [domain] = YES;
+     unsigned k;
+     real_t   tmp = get_ip_state_state (domain_blocks [index],
+                        domain_blocks [domain], level, c);
+     
+     for (k = 0; k < n; k++) 
+        tmp -= ip_domain_ortho_vector [domain][k] / norm_ortho_vector [k]
+           * ip_domain_ortho_vector [index][k];
+     ip_domain_ortho_vector [domain][n] = tmp;
+     rem_denominator [domain] -= square (tmp) / norm_ortho_vector [n];
+     rem_numerator [domain]   -= ip_image_ortho_vector [n]
+                     / norm_ortho_vector [n]
+                     * ip_domain_ortho_vector [domain][n] ;
+
+     /*
+      *  Exclude vectors with small denominator
+      */
+     if (!used [domain]) 
+        if (rem_denominator [domain] / size_of_level (level) < min_norm) 
+           used [domain] = YES;
       }
 }
 
diff --git a/converter/other/fiasco/codec/approx.h b/converter/other/fiasco/codec/approx.h
index c54b78c9..42f1458a 100644
--- a/converter/other/fiasco/codec/approx.h
+++ b/converter/other/fiasco/codec/approx.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/bintree.c b/converter/other/fiasco/codec/bintree.c
index ddd74e15..b050ab63 100644
--- a/converter/other/fiasco/codec/bintree.c
+++ b/converter/other/fiasco/codec/bintree.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/bintree.h b/converter/other/fiasco/codec/bintree.h
index cdb80c94..3dc88ac1 100644
--- a/converter/other/fiasco/codec/bintree.h
+++ b/converter/other/fiasco/codec/bintree.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/coder.c b/converter/other/fiasco/codec/coder.c
index 94e367dd..1e21994d 100644
--- a/converter/other/fiasco/codec/coder.c
+++ b/converter/other/fiasco/codec/coder.c
@@ -3,8 +3,8 @@
  *
  *  Written by:     Ullrich Hafner
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -14,6 +14,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/other/fiasco/codec/coder.h b/converter/other/fiasco/codec/coder.h
index c6f4bb7c..2fe81d81 100644
--- a/converter/other/fiasco/codec/coder.h
+++ b/converter/other/fiasco/codec/coder.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/coeff.c b/converter/other/fiasco/codec/coeff.c
index 0cd64f17..6d33bad5 100644
--- a/converter/other/fiasco/codec/coeff.c
+++ b/converter/other/fiasco/codec/coeff.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/coeff.h b/converter/other/fiasco/codec/coeff.h
index 118cd0fc..6193e2ee 100644
--- a/converter/other/fiasco/codec/coeff.h
+++ b/converter/other/fiasco/codec/coeff.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/control.c b/converter/other/fiasco/codec/control.c
index 94c23c83..2ea06052 100644
--- a/converter/other/fiasco/codec/control.c
+++ b/converter/other/fiasco/codec/control.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/control.h b/converter/other/fiasco/codec/control.h
index f601d8b8..6c0834a1 100644
--- a/converter/other/fiasco/codec/control.h
+++ b/converter/other/fiasco/codec/control.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/cwfa.h b/converter/other/fiasco/codec/cwfa.h
index 9c4e7fee..dd86fbfc 100644
--- a/converter/other/fiasco/codec/cwfa.h
+++ b/converter/other/fiasco/codec/cwfa.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/decoder.c b/converter/other/fiasco/codec/decoder.c
index 26284596..229996b6 100644
--- a/converter/other/fiasco/codec/decoder.c
+++ b/converter/other/fiasco/codec/decoder.c
@@ -3,11 +3,11 @@
  *
  *  Written by:     Ullrich Hafner
  *          Michael Unger
- *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
- 
+
 /*
  *  $Date: 2000/10/22 10:44:48 $
  *  $Author: hafner $
@@ -37,7 +37,7 @@
 /*****************************************************************************
 
                 prototypes
-  
+
 *****************************************************************************/
 
 static void
@@ -63,7 +63,7 @@ duplicate_state_image (const word_t *domain, unsigned offset, unsigned level);
 /*****************************************************************************
 
                 public code
-  
+
 *****************************************************************************/
 
 video_t *
@@ -78,7 +78,7 @@ alloc_video (bool_t store_wfa)
  */
 {
    video_t *video = Calloc (1, sizeof (video_t));
-   
+
    video->future_display = -1;
    video->display        = 0;
 
@@ -138,13 +138,13 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
  *  Get next frame of the WFA 'video' from stream 'input'.
  *  'orig_wfa' is the constant part of the WFA used by all frames.
  *  Depending on values of 'enlarge_factor' and 'smoothing' enlarge and
- *  smooth image, respectively. 
+ *  smooth image, respectively.
  *  If 'store_wfa' is TRUE, then store WFA structure of reference frames
  *  (used by analysis tool xwfa).
  *  If 'reference_frame' is not NULL, then load image 'reference_frame'
  *  from disk.
  *  'format' gives the color format to be used (either 4:2:0 or 4:4:4).
- *  If 'timer' is not NULL, then accumulate running time statistics. 
+ *  If 'timer' is not NULL, then accumulate running time statistics.
  *
  *  Return value:
  *  pointer to decoded frame
@@ -157,7 +157,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
    image_t *sframe            = NULL; /* current smoothed frame */
    bool_t   current_frame_is_future_frame = NO;
 
-   if (video->future_display == video->display)  
+   if (video->future_display == video->display)
    {
       /*
        *  Future frame is already computed since it has been used
@@ -189,7 +189,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
      clock_t       ptimer;
      unsigned int  stop_timer [3];
      wfa_t        *tmp_wfa = NULL;
-     
+
      if (!store_wfa)
         video->wfa = orig_wfa;
      else
@@ -198,7 +198,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
         copy_wfa (tmp_wfa, video->wfa);
         copy_wfa (video->wfa, orig_wfa);
      }
-   
+
      /*
       *  First step: read WFA from disk
       */
@@ -210,7 +210,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
         timer->input [video->wfa->frame_type] += stop_timer [0];
         timer->frames [video->wfa->frame_type]++;
      }
-      
+
      /*
       *  Read reference frame from disk if required
       *  (i.e., 1st frame is of type B or P)
@@ -225,7 +225,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
         video->frame  = read_image_file (reference_frame);
         video->sframe = NULL;
      }
-   
+
      /*
       *  Depending on current frame type update past and future frames
       */
@@ -313,7 +313,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
      }
      if (tmp_wfa)
         free_wfa (tmp_wfa);
-     
+
      current_frame_is_future_frame = NO;
      /*
       *  Second step: decode image
@@ -323,7 +323,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
         unsigned orig_width, orig_height;
 
         stop_timer [0] = stop_timer [1] = stop_timer [2] = 0;
-     
+
         enlarge_image (enlarge_factor, format,
                (video->wfa->wfainfo->color
                 && format == FORMAT_4_2_0)
@@ -332,10 +332,10 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
         if (enlarge_factor > 0)
         {
            orig_width  = video->wfa->wfainfo->width  << enlarge_factor;
-           orig_height = video->wfa->wfainfo->height << enlarge_factor; 
+           orig_height = video->wfa->wfainfo->height << enlarge_factor;
         }
         else
-        { 
+        {
            orig_width  = video->wfa->wfainfo->width  >> - enlarge_factor;
            orig_height = video->wfa->wfainfo->height >> - enlarge_factor;
            if (orig_width & 1)
@@ -343,7 +343,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
            if (orig_height & 1)
           orig_height++;
         }
-     
+
         frame = decode_image (orig_width, orig_height, format,
                   timer != NULL ? stop_timer : NULL,
                   video->wfa);
@@ -381,7 +381,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
      }
      else
         sframe = NULL;
-     
+
      stop_timer [0] = prg_timer (&ptimer, STOP);
      if (timer)
         timer->smooth [video->wfa->frame_type] += stop_timer [0];
@@ -399,7 +399,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
         video->future_display     = frame_number;
         current_frame_is_future_frame = YES;
      }
-      
+
      if (!store_wfa)
         remove_states (video->wfa->basis_states, video->wfa);
       } while (!video->frame);
@@ -407,7 +407,7 @@ get_next_frame (bool_t store_wfa, int enlarge_factor,
       if (!store_wfa)
      video->wfa = NULL;
    }
-   
+
    return video->sframe ? video->sframe : video->frame;
 }
 
@@ -418,8 +418,8 @@ decode_image (unsigned orig_width, unsigned orig_height, format_e format,
  *  Compute image which is represented by the given 'wfa'.
  *  'orig_width'x'orig_height' gives the resolution of the image at
  *  coding time. Use 4:2:0 subsampling or 4:4:4 'format' for color images.
- *  If 'dec_timer' is given, accumulate running time statistics. 
- *  
+ *  If 'dec_timer' is given, accumulate running time statistics.
+ *
  *  Return value:
  *  pointer to decoded image
  *
@@ -458,7 +458,7 @@ decode_image (unsigned orig_width, unsigned orig_height, format_e format,
    for (max_level = 0, state = wfa->basis_states; state < wfa->states; state++)
       if (isedge (wfa->into [state][0][0]) || isedge (wfa->into [state][1][0]))
      max_level = MAX(max_level, wfa->level_of_state [state]);
-   
+
 
    /*
     *  Allocate frame buffer for decoded image
@@ -468,7 +468,7 @@ decode_image (unsigned orig_width, unsigned orig_height, format_e format,
    width  = MAX(width, orig_width);
    height = MAX(height, orig_height);
    frame = alloc_image (width, height, wfa->wfainfo->color, format);
-   
+
    /*
     *  Allocate buffers for intermediate state images
     */
@@ -478,7 +478,7 @@ decode_image (unsigned orig_width, unsigned orig_height, format_e format,
       wfa->level_of_state [wfa->tree[wfa->root_state][0]] = 128;
       wfa->level_of_state [wfa->tree[wfa->root_state][1]] = 128;
    }
-   alloc_state_images (&images, &offsets, frame, root_state, 0, max_level, 
+   alloc_state_images (&images, &offsets, frame, root_state, 0, max_level,
                format, wfa);
 
    if (dec_timer)
@@ -498,20 +498,20 @@ decode_image (unsigned orig_width, unsigned orig_height, format_e format,
    prg_timer (&ptimer, START);
    free_state_images (max_level, frame->color, images, offsets, root_state, 0,
               format, wfa);
-   
+
    /*
     *  Crop decoded image if the image size differs.
     */
    if (orig_width != width || orig_height != height)
    {
-      frame->height = orig_height;  
-      frame->width  = orig_width;   
-      if (orig_width != width)      
+      frame->height = orig_height;
+      frame->width  = orig_width;
+      if (orig_width != width)
       {
      color_e   band;        /* current color band */
      word_t   *src, *dst;       /* source and destination pointers */
      unsigned  y;           /* current row */
-     
+
      for (band  = first_band (frame->color);
           band <= last_band (frame->color); band++)
      {
@@ -546,7 +546,7 @@ decode_state (unsigned state, unsigned level, wfa_t *wfa)
  *  pointer to decoded state image
  *
  *  Side effects:
- *  'wfa' states > 'state' are removed.  
+ *  'wfa' states > 'state' are removed.
  */
 {
    word_t  *domains [2];
@@ -571,7 +571,7 @@ decode_state (unsigned state, unsigned level, wfa_t *wfa)
    {
       word_t   *src, *dst;
       unsigned  y;
-        
+
       src = domains [0];
       dst = img->pixels [GRAY];
       for (y = img->height; y; y--)
@@ -603,75 +603,73 @@ decode_range (unsigned range_state, unsigned range_label, unsigned range_level,
  *  'wfa->level_of_state []' is changed
  */
 {
-   unsigned   root_state [3];       /* dummy (for alloc_state_images) */
-   image_t   *state_image;      /* regenerated state image */
-   word_t   **images;           /* pointer to array of pointers
-                       to state images */
-   u_word_t  *offsets;          /* pointer to array of state image
-                       offsets */
-   word_t    *range;
-
-   enlarge_image (range_level - (wfa->level_of_state [range_state] - 1),
-          FORMAT_4_4_4, -1, wfa);
-   root_state [0] = range_state;
-   state_image    = alloc_image (width_of_level (range_level + 1),
-                 height_of_level (range_level + 1),
-                 NO, FORMAT_4_4_4);
-   alloc_state_images (&images, &offsets, state_image, NULL, range_state,
-               range_level + 1, NO, wfa);
-   compute_state_images (range_level + 1, images, offsets, wfa);
-
-   range = Calloc (size_of_level (range_level), sizeof (word_t));
-
-   if ((range_level & 1) == 0)      /* square image */
-   {
-      memcpy (range,
-          images [range_state + (range_level + 1) * wfa->states]
-          + range_label * size_of_level (range_level),
-          size_of_level (range_level) * sizeof (word_t));
-   }
-   else                 /* rectangle */
-   {
-      word_t   *src, *dst;
-      unsigned  y;
-      
-      src = images [range_state + (range_level + 1) * wfa->states]
-        + range_label * width_of_level (range_level);
-      dst = range;
-      for (y = height_of_level (range_level); y; y--)
-      {
-     memcpy (dst, src, width_of_level (range_level) * sizeof (word_t));
-     dst += width_of_level (range_level);
-     src += width_of_level (range_level + 1);
-      }
-   }
-
-   if (domain != NULL)          /* copy domain images */
-   {
-      int      s;           /* domain state */
-      unsigned edge;            /* counter */
-        
-      if (ischild (s = wfa->tree [range_state][range_label]))
-     *domain++ = duplicate_state_image (images [s + (range_level)
-                           * wfa->states],
-                        offsets [s + (range_level)
-                            * wfa->states],
-                        range_level);
-      for (edge = 0; isedge (s = wfa->into[range_state][range_label][edge]);
-       edge++)
-     *domain++ = duplicate_state_image (images [s + (range_level)
-                           * wfa->states],
-                        offsets [s + (range_level)
-                            * wfa->states],
-                        range_level);
-      *domain = NULL;
-   }
-   
-   free_state_images (range_level + 1, NO, images, offsets, NULL, range_state,
-              NO, wfa);
-   free_image (state_image);
-   
-   return range;
+    image_t   *state_image;      /* regenerated state image */
+    word_t   **images;           /* pointer to array of pointers
+                                    to state images */
+    u_word_t  *offsets;          /* pointer to array of state image
+                                    offsets */
+    word_t    *range;
+
+    enlarge_image (range_level - (wfa->level_of_state [range_state] - 1),
+                   FORMAT_4_4_4, -1, wfa);
+    state_image = alloc_image (width_of_level (range_level + 1),
+                               height_of_level (range_level + 1),
+                               NO, FORMAT_4_4_4);
+    alloc_state_images (&images, &offsets, state_image, NULL, range_state,
+                        range_level + 1, FORMAT_4_4_4, wfa);
+    compute_state_images (range_level + 1, images, offsets, wfa);
+
+    range = Calloc (size_of_level (range_level), sizeof (word_t));
+
+    if ((range_level & 1) == 0)      /* square image */
+    {
+        memcpy (range,
+                images [range_state + (range_level + 1) * wfa->states]
+                + range_label * size_of_level (range_level),
+                size_of_level (range_level) * sizeof (word_t));
+    }
+    else                 /* rectangle */
+    {
+        word_t   *src, *dst;
+        unsigned  y;
+
+        src = images [range_state + (range_level + 1) * wfa->states]
+            + range_label * width_of_level (range_level);
+        dst = range;
+        for (y = height_of_level (range_level); y; y--)
+        {
+            memcpy (dst, src, width_of_level (range_level) * sizeof (word_t));
+            dst += width_of_level (range_level);
+            src += width_of_level (range_level + 1);
+        }
+    }
+
+    if (domain != NULL)          /* copy domain images */
+    {
+        int      s;           /* domain state */
+        unsigned edge;            /* counter */
+
+        if (ischild (s = wfa->tree [range_state][range_label]))
+            *domain++ = duplicate_state_image (images [s + (range_level)
+                                                       * wfa->states],
+                                               offsets [s + (range_level)
+                                                        * wfa->states],
+                                               range_level);
+        for (edge = 0; isedge (s = wfa->into[range_state][range_label][edge]);
+             edge++)
+            *domain++ = duplicate_state_image (images [s + (range_level)
+                                                       * wfa->states],
+                                               offsets [s + (range_level)
+                                                        * wfa->states],
+                                               range_level);
+        *domain = NULL;
+    }
+
+    free_state_images (range_level + 1, NO, images, offsets, NULL, range_state,
+                       FORMAT_4_4_4, wfa);
+    free_image (state_image);
+
+    return range;
 }
 
 void
@@ -687,7 +685,7 @@ smooth_image (unsigned sf, const wfa_t *wfa, image_t *image)
  */
 {
    int      is, inegs;          /* integer factors of s and 1 - s*/
-   unsigned state;          
+   unsigned state;
    unsigned img_width  = image->width;
    unsigned img_height = image->height;
    real_t   s          = 1.0 - sf / 200.0;
@@ -697,7 +695,7 @@ smooth_image (unsigned sf, const wfa_t *wfa, image_t *image)
 
    is    = s * 512 + .5;        /* integer representation of s */
    inegs = (1 - s) * 512 + .5;      /* integer representation of 1 - s */
-   
+
    for (state = wfa->basis_states;
     state < (wfa->wfainfo->color
          ? wfa->tree [wfa->root_state][0]
@@ -708,10 +706,10 @@ smooth_image (unsigned sf, const wfa_t *wfa, image_t *image)
       unsigned  level  = wfa->level_of_state[state]; /* level of state image */
       unsigned  width  = width_of_level (level); /* size of state image */
       unsigned  height = height_of_level (level); /* size of state image */
-      
+
       if (wfa->y [state][1] >= img_height || wfa->x [state][1] >= img_width)
      continue;          /* outside visible area */
-     
+
       if (level % 2)            /* horizontal smoothing */
       {
      unsigned  i;           /* line counter */
@@ -721,12 +719,12 @@ smooth_image (unsigned sf, const wfa_t *wfa, image_t *image)
      img1 = bptr + (wfa->y [state][1] - 1) * img_width
         + wfa->x [state][1];
      img2 = bptr + wfa->y [state][1] * img_width + wfa->x [state][1];
-     
+
      for (i = MIN(width, img_width - wfa->x [state][1]); i;
           i--, img1++, img2++)
      {
         int tmp = *img1;
-        
+
 #ifdef HAVE_SIGNED_SHIFT
         *img1 = (((is * tmp) >> 10) << 1)
             + (((inegs * (int) *img2) >> 10) << 1);
@@ -748,12 +746,12 @@ smooth_image (unsigned sf, const wfa_t *wfa, image_t *image)
 
      img1 = bptr + wfa->y [state][1] * img_width + wfa->x [state][1] - 1;
      img2 = bptr + wfa->y [state][1] * img_width + wfa->x [state][1];
-     
+
      for (i = MIN(height, img_height - wfa->y [state][1]); i;
           i--, img1 += img_width, img2 += img_width)
      {
         int tmp = *img1;
-        
+
 #ifdef HAVE_SIGNED_SHIFT
         *img1 = (((is * tmp) >> 10) << 1)
             + (((inegs * (int) *img2) >> 10) << 1);
@@ -773,7 +771,7 @@ smooth_image (unsigned sf, const wfa_t *wfa, image_t *image)
 /*****************************************************************************
 
                 private code
-  
+
 *****************************************************************************/
 
 static void
@@ -791,7 +789,7 @@ enlarge_image (int enlarge_factor, format_e format, unsigned y_root,
  *  are modified.
  */
 {
-   
+
    if (enlarge_factor != 0 || format == FORMAT_4_2_0)
    {
       unsigned state;
@@ -803,11 +801,11 @@ enlarge_image (int enlarge_factor, format_e format, unsigned y_root,
       }
       else
      state = wfa->basis_states;
-      
+
       for (; state < wfa->states; state++)
       {
      unsigned label, n;
-     
+
      wfa->level_of_state [state]
         = MAX(wfa->level_of_state [state] + enlarge_factor * 2, 0);
 
@@ -857,12 +855,12 @@ compute_actual_size (unsigned luminance_root,
 {
    unsigned x = 0, y = 0;       /* maximum coordinates */
    unsigned state;          /* counter */
-   
+
    for (state = wfa->basis_states; state < wfa->states; state++)
       if (isedge (wfa->into [state][0][0]) || isedge (wfa->into [state][1][0]))
       {
           unsigned mult = state > luminance_root ? 2 : 1;
-          
+
           x = MAX((wfa->x [state][0]
                    + width_of_level (wfa->level_of_state [state])) * mult, x);
           y = MAX((wfa->y [state][0]
@@ -888,7 +886,7 @@ alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
  *  'max_level' fives the max. level of a linear combination.
  *  Memory is allocated for every required state image.
  *  Use 4:2:0 subsampling or 4:4:4 'format' for color images.
- *  If 'range_state' > 0 then rather compute image of 'range_state' than 
+ *  If 'range_state' > 0 then rather compute image of 'range_state' than
  *  image of 'wfa->root_state'.
  *
  *  Return values:
@@ -902,7 +900,7 @@ alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
    word_t   **simg;         /* ptr to list of state image ptr's */
    u_word_t  *offs;         /* ptr to list of offsets */
    unsigned   level;            /* counter */
-   
+
    simg = Calloc (wfa->states * (max_level + 1), sizeof (word_t *));
    offs = Calloc (wfa->states * (max_level + 1), sizeof (u_word_t));
 
@@ -940,7 +938,7 @@ alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
         }
       }
    }
-   
+
    /*
     *  Generate list of state images which must be computed at each level
     */
@@ -948,9 +946,9 @@ alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
    {
       int      child, domain;
       unsigned state, label, edge;
-      
+
       /*
-       *  Range approximation with child. 
+       *  Range approximation with child.
        */
       for (state = 1; state < (range_state > 0 ?
                    range_state + 1 : wfa->states); state++)
@@ -992,7 +990,7 @@ alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
           }
            }
       /*
-       *  Range approximation with linear combination 
+       *  Range approximation with linear combination
        */
       for (state = 1; state < (range_state > 0 ?
                    range_state + 1 : wfa->states); state++)
@@ -1010,7 +1008,7 @@ alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
             = width_of_level (level - 1);
           }
            }
-      
+
    }
 
    *images  = simg;
@@ -1041,7 +1039,7 @@ free_state_images (unsigned max_level, bool_t color, word_t **state_image,
    else
    {
       unsigned state;
-      
+
       /*
        *  Initialize state image array with states at 'max_level'
        */
@@ -1055,19 +1053,19 @@ free_state_images (unsigned max_level, bool_t color, word_t **state_image,
         level = max_level - 2;
      else
         level = max_level;
-      
+
      for (; state < wfa->states; state++)
         if (wfa->level_of_state [state] == level)
            state_image [state + level * wfa->states] = &marker;
       }
    }
-   
+
    for (level = max_level; level > 0; level--)
    {
       int      domain, child;
       unsigned state, label, edge;
       /*
-       *  Range approximation with child. 
+       *  Range approximation with child.
        */
       for (state = 1; state < (range_state > 0 ?
                    range_state + 1 : wfa->states); state++)
@@ -1082,7 +1080,7 @@ free_state_images (unsigned max_level, bool_t color, word_t **state_image,
           state_image [child + (level - 1) * wfa->states] = &marker;
            }
       /*
-       *  Range approximation with linear combination 
+       *  Range approximation with linear combination
        */
       for (state = 1; state < (range_state > 0 ?
                    range_state + 1 : wfa->states);
@@ -1091,7 +1089,7 @@ free_state_images (unsigned max_level, bool_t color, word_t **state_image,
         for (label = 0; label < MAXLABELS; label++)
            for (edge = 0; isedge (domain = wfa->into[state][label][edge]);
             edge++)
-          if (domain > 0    
+          if (domain > 0
               && (state_image [domain + (level - 1) * wfa->states]
               != NULL)
               && (state_image [domain + (level - 1) * wfa->states]
@@ -1114,7 +1112,7 @@ compute_state_images (unsigned max_level, word_t **simg,
  *  which are marked in the array 'simg' (offsets of state images
  *  are given by 'offset').
  *
- *  Warning: Several optimizations are used in this function making 
+ *  Warning: Several optimizations are used in this function making
  *  it difficult to understand.
  *
  *  No return value.
@@ -1125,7 +1123,7 @@ compute_state_images (unsigned max_level, word_t **simg,
  */
 {
    unsigned level, state;
-     
+
    /*
     *  Copy one-pixel images in case state_image pointer != &final distr.
     */
@@ -1140,17 +1138,17 @@ compute_state_images (unsigned max_level, word_t **simg,
     *  'weight' gives the weight in integer notation
     *  'src', 'dst', and 'idst' are pointers to the source and
     *  destination pixels (short or integer format), respectively.
-    *  Short format : one operation per register (16 bit mode). 
-    *  Integer format : two operations per register (32 bit mode). 
+    *  Short format : one operation per register (16 bit mode).
+    *  Integer format : two operations per register (32 bit mode).
     *  'src_offset', 'dst_offset', and 'dst_offset' give the number of
     *  pixels which have to be omitted when jumping to the next image row.
     */
-   for (level = 1; level <= max_level; level++) 
+   for (level = 1; level <= max_level; level++)
    {
       unsigned label;
       unsigned width  = width_of_level (level - 1);
       unsigned height = height_of_level (level - 1);
-      
+
       for (state = 1; state < wfa->states; state++)
      if (simg [state + level * wfa->states] != NULL)
         for (label = 0; label < MAXLABELS; label++)
@@ -1178,7 +1176,7 @@ compute_state_images (unsigned max_level, word_t **simg,
           }
 
           /*
-           *  Generate the state images by adding the corresponding 
+           *  Generate the state images by adding the corresponding
            *  weighted state images:
            *  subimage [label] =
            *       weight_1 * image_1 + ... + weight_n * image_n
@@ -1228,12 +1226,12 @@ compute_state_images (unsigned max_level, word_t **simg,
             src_offset = offset [domain + ((level - 1)
                                * wfa->states)] - width;
             weight     = wfa->int_weight [state][label][edge];
-            
+
             if (width == 1) /* can't add two-pixels in a row */
             {
                word_t   *dst;
                unsigned  dst_offset;
-               
+
                dst        = range;
                dst_offset = offset [state + level * wfa->states]
                     - width;
@@ -1242,7 +1240,7 @@ compute_state_images (unsigned max_level, word_t **simg,
 #else                   /* not HAVE_SIGNED_SHIFT */
                *dst++ = ((weight * (int) *src++) / 1024) * 2;
 #endif /* not HAVE_SIGNED_SHIFT */
-               if (height == 2) 
+               if (height == 2)
                {
                   src += src_offset;
                   dst += dst_offset;
@@ -1258,14 +1256,14 @@ compute_state_images (unsigned max_level, word_t **simg,
                unsigned  y;
                int      *idst;
                unsigned  idst_offset;
-               
+
                idst        = (int *) range;
                idst_offset = (offset [state + level * wfa->states]
                       - width) / 2;
                for (y = height; y; y--)
                {
                   int *comp_dst = idst + (width >> 1);
-                  
+
                   for (; idst != comp_dst; )
                   {
                  int tmp; /* temp. value of adjacent pixels */
@@ -1314,11 +1312,11 @@ compute_state_images (unsigned max_level, word_t **simg,
             {
                word_t   *dst;
                unsigned  dst_offset;
-               
+
                dst        = range;
                dst_offset = offset [state + level * wfa->states]
                     - width;
-               
+
                *dst++ = weight;
                if (height == 2)
                {
@@ -1331,7 +1329,7 @@ compute_state_images (unsigned max_level, word_t **simg,
                unsigned  x, y;
                int      *idst;
                unsigned  idst_offset;
-               
+
                weight      = (weight * 65536) | (weight & 0xffff);
                idst        = (int *) range;
                idst_offset = offset [state + level * wfa->states]
@@ -1353,7 +1351,7 @@ compute_state_images (unsigned max_level, word_t **simg,
           }
           else
              edge = 0;
-          
+
           /*
            *  Add remaining weighted domain images to current range
            */
@@ -1370,12 +1368,12 @@ compute_state_images (unsigned max_level, word_t **simg,
             src_offset = offset [domain + ((level - 1)
                                * wfa->states)] - width;
             weight     = wfa->int_weight [state][label][edge];
-            
+
             if (width == 1) /* can't add two-pixels in a row */
             {
                word_t   *dst;
                unsigned  dst_offset;
-               
+
                dst        = range;
                dst_offset = offset [state + level * wfa->states]
                     - width;
@@ -1385,7 +1383,7 @@ compute_state_images (unsigned max_level, word_t **simg,
 #else /* not HAVE_SIGNED_SHIFT */
                *dst++ += ((weight * (int) *src++) / 1024) * 2;
 #endif /* not HAVE_SIGNED_SHIFT */
-               if (height == 2) 
+               if (height == 2)
                {
                   src += src_offset;
                   dst += dst_offset;
@@ -1401,15 +1399,15 @@ compute_state_images (unsigned max_level, word_t **simg,
                int      *idst;
                unsigned  idst_offset;
                unsigned  y;
-               
+
                idst        = (int *) range;
                idst_offset = (offset [state + level * wfa->states]
                       - width) / 2;
-               
+
                for (y = height; y; y--)
                {
                   int *comp_dst = idst + (width >> 1);
-                  
+
                   for (; idst != comp_dst;)
                   {
                  int tmp; /* temp. value of adjacent pixels */
@@ -1459,11 +1457,11 @@ compute_state_images (unsigned max_level, word_t **simg,
             {
                word_t   *dst;
                unsigned  dst_offset;
-               
+
                dst        = range;
                dst_offset = offset [state + level * wfa->states]
                     - width;
-               
+
                *dst++ += weight;
                if (height == 2)
                {
@@ -1476,16 +1474,16 @@ compute_state_images (unsigned max_level, word_t **simg,
                int      *idst;
                unsigned  idst_offset;
                unsigned  y;
-               
+
                weight      = (weight * 65536) | (weight & 0xffff);
                idst        = (int *) range;
                idst_offset = (offset [state + level * wfa->states]
                       - width) /2;
-               
+
                for (y = height; y; y--)
                {
                   int *comp_dst = idst + (width >> 1);
-                  
+
                   for (; idst != comp_dst; )
                   {
                  *idst = (*idst + weight) & 0xfffefffe;
@@ -1496,14 +1494,14 @@ compute_state_images (unsigned max_level, word_t **simg,
             }
              }
           }
-           } 
+           }
    }
 }
 
 static word_t *
 duplicate_state_image (const word_t *domain, unsigned offset, unsigned level)
 /*
- *  Allocate new memory block 'pixels' and copy pixel values of 'domain' 
+ *  Allocate new memory block 'pixels' and copy pixel values of 'domain'
  *  (size and pixel offset are given by 'level' and 'offset')
  *  to the lock 'pixels'.
  *
diff --git a/converter/other/fiasco/codec/decoder.h b/converter/other/fiasco/codec/decoder.h
index 8cd211e0..7823ab91 100644
--- a/converter/other/fiasco/codec/decoder.h
+++ b/converter/other/fiasco/codec/decoder.h
@@ -3,8 +3,8 @@
  *		
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/dfiasco.c b/converter/other/fiasco/codec/dfiasco.c
index 2fdec573..d45c9e39 100644
--- a/converter/other/fiasco/codec/dfiasco.c
+++ b/converter/other/fiasco/codec/dfiasco.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
  
 /*
diff --git a/converter/other/fiasco/codec/dfiasco.h b/converter/other/fiasco/codec/dfiasco.h
index bcc3c7f9..eed12b6b 100644
--- a/converter/other/fiasco/codec/dfiasco.h
+++ b/converter/other/fiasco/codec/dfiasco.h
@@ -3,8 +3,8 @@
  *		
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/domain-pool.c b/converter/other/fiasco/codec/domain-pool.c
index 7cc3900e..1c14a30c 100644
--- a/converter/other/fiasco/codec/domain-pool.c
+++ b/converter/other/fiasco/codec/domain-pool.c
@@ -3,8 +3,8 @@
  *
  *  Written by:     Ullrich Hafner
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/domain-pool.h b/converter/other/fiasco/codec/domain-pool.h
index d1488779..a2c46141 100644
--- a/converter/other/fiasco/codec/domain-pool.h
+++ b/converter/other/fiasco/codec/domain-pool.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/ip.c b/converter/other/fiasco/codec/ip.c
index ade0d916..56b0ae9a 100644
--- a/converter/other/fiasco/codec/ip.c
+++ b/converter/other/fiasco/codec/ip.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/ip.h b/converter/other/fiasco/codec/ip.h
index e5e4dd65..0c693826 100644
--- a/converter/other/fiasco/codec/ip.h
+++ b/converter/other/fiasco/codec/ip.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/motion.c b/converter/other/fiasco/codec/motion.c
index 876a2998..0d1fa099 100644
--- a/converter/other/fiasco/codec/motion.c
+++ b/converter/other/fiasco/codec/motion.c
@@ -4,8 +4,8 @@
  *  Written by:		Ullrich Hafner
  *			Michael Unger
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/motion.h b/converter/other/fiasco/codec/motion.h
index 2ea382f7..82d1f1ec 100644
--- a/converter/other/fiasco/codec/motion.h
+++ b/converter/other/fiasco/codec/motion.h
@@ -4,8 +4,8 @@
  *  Written by:		Ullrich Hafner
  *			Michael Unger
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/mwfa.c b/converter/other/fiasco/codec/mwfa.c
index 43a7dae2..0be08a4f 100644
--- a/converter/other/fiasco/codec/mwfa.c
+++ b/converter/other/fiasco/codec/mwfa.c
@@ -4,8 +4,8 @@
  *  Written by:		Michael Unger
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/mwfa.h b/converter/other/fiasco/codec/mwfa.h
index 52f41866..2c1e7c9f 100644
--- a/converter/other/fiasco/codec/mwfa.h
+++ b/converter/other/fiasco/codec/mwfa.h
@@ -4,8 +4,8 @@
  *  Written by:		Michael Unger
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/options.c b/converter/other/fiasco/codec/options.c
index c8e4d2e2..b9c64efd 100644
--- a/converter/other/fiasco/codec/options.c
+++ b/converter/other/fiasco/codec/options.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -14,6 +14,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/other/fiasco/codec/options.h b/converter/other/fiasco/codec/options.h
index 3af6be01..783c41ef 100644
--- a/converter/other/fiasco/codec/options.h
+++ b/converter/other/fiasco/codec/options.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/prediction.c b/converter/other/fiasco/codec/prediction.c
index e056d10f..e78e5acc 100644
--- a/converter/other/fiasco/codec/prediction.c
+++ b/converter/other/fiasco/codec/prediction.c
@@ -4,8 +4,8 @@
  *  Written by:		Ullrich Hafner
  *			Michael Unger
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/prediction.h b/converter/other/fiasco/codec/prediction.h
index 1068501a..0646dfd8 100644
--- a/converter/other/fiasco/codec/prediction.h
+++ b/converter/other/fiasco/codec/prediction.h
@@ -4,8 +4,8 @@
  *  Written by:		Ullrich Hafner
  *			Michael Unger
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/subdivide.c b/converter/other/fiasco/codec/subdivide.c
index 2ace18e4..348f838a 100644
--- a/converter/other/fiasco/codec/subdivide.c
+++ b/converter/other/fiasco/codec/subdivide.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/subdivide.h b/converter/other/fiasco/codec/subdivide.h
index b6840e58..bd9cd10e 100644
--- a/converter/other/fiasco/codec/subdivide.h
+++ b/converter/other/fiasco/codec/subdivide.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/tiling.c b/converter/other/fiasco/codec/tiling.c
index 21e4428a..9af43da0 100644
--- a/converter/other/fiasco/codec/tiling.c
+++ b/converter/other/fiasco/codec/tiling.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/tiling.h b/converter/other/fiasco/codec/tiling.h
index 2eb04fe0..6668ad8b 100644
--- a/converter/other/fiasco/codec/tiling.h
+++ b/converter/other/fiasco/codec/tiling.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/wfa.h b/converter/other/fiasco/codec/wfa.h
index 9253affd..59020fc1 100644
--- a/converter/other/fiasco/codec/wfa.h
+++ b/converter/other/fiasco/codec/wfa.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/codec/wfalib.c b/converter/other/fiasco/codec/wfalib.c
index 61d64d2f..90420d6f 100644
--- a/converter/other/fiasco/codec/wfalib.c
+++ b/converter/other/fiasco/codec/wfalib.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -14,6 +14,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/other/fiasco/codec/wfalib.h b/converter/other/fiasco/codec/wfalib.h
index 4622fcd2..5c1e0907 100644
--- a/converter/other/fiasco/codec/wfalib.h
+++ b/converter/other/fiasco/codec/wfalib.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/display.c b/converter/other/fiasco/display.c
index cf160329..d8d32fa6 100644
--- a/converter/other/fiasco/display.c
+++ b/converter/other/fiasco/display.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  *		
  *  Based on mpeg2decode, (C) 1994, MPEG Software Simulation Group
  *  and      mpeg2play,   (C) 1994 Stefan Eckart
diff --git a/converter/other/fiasco/display.h b/converter/other/fiasco/display.h
index 5f30b117..8049456a 100644
--- a/converter/other/fiasco/display.h
+++ b/converter/other/fiasco/display.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/doc/README.LIB b/converter/other/fiasco/doc/README.LIB
index 4bf8c382..ce8a6b2b 100644
--- a/converter/other/fiasco/doc/README.LIB
+++ b/converter/other/fiasco/doc/README.LIB
@@ -1,5 +1,5 @@
 ---------------------------------------------------------------------------
-	 FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
+	 FIASCO (Fractal Image And Sequence COdec)
 		       Copyright (C) 1994-2000
      Ullrich Hafner <hafner@bigfoot.de>, http://ulli.linuxave.net
 			     Version 1.0
diff --git a/converter/other/fiasco/doc/fiasco_c_options_new.3 b/converter/other/fiasco/doc/fiasco_c_options_new.3
index 52efb86c..d873b642 100644
--- a/converter/other/fiasco/doc/fiasco_c_options_new.3
+++ b/converter/other/fiasco/doc/fiasco_c_options_new.3
@@ -429,4 +429,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_coder.3 b/converter/other/fiasco/doc/fiasco_coder.3
index 3d1c6b87..9f2e0f8e 100644
--- a/converter/other/fiasco/doc/fiasco_coder.3
+++ b/converter/other/fiasco/doc/fiasco_coder.3
@@ -103,4 +103,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_d_options_new.3 b/converter/other/fiasco/doc/fiasco_d_options_new.3
index 4294330a..d7f2181a 100644
--- a/converter/other/fiasco/doc/fiasco_d_options_new.3
+++ b/converter/other/fiasco/doc/fiasco_d_options_new.3
@@ -119,4 +119,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_decoder_new.3 b/converter/other/fiasco/doc/fiasco_decoder_new.3
index 05e981a9..5d1d0d08 100644
--- a/converter/other/fiasco/doc/fiasco_decoder_new.3
+++ b/converter/other/fiasco/doc/fiasco_decoder_new.3
@@ -191,4 +191,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_get_error_message.3 b/converter/other/fiasco/doc/fiasco_get_error_message.3
index 09d593fb..ce53e6d9 100644
--- a/converter/other/fiasco/doc/fiasco_get_error_message.3
+++ b/converter/other/fiasco/doc/fiasco_get_error_message.3
@@ -38,4 +38,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_image_new.3 b/converter/other/fiasco/doc/fiasco_image_new.3
index 10625b63..ac3297bf 100644
--- a/converter/other/fiasco/doc/fiasco_image_new.3
+++ b/converter/other/fiasco/doc/fiasco_image_new.3
@@ -92,4 +92,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_options_new.3 b/converter/other/fiasco/doc/fiasco_options_new.3
index 26e070ca..f26586a7 100644
--- a/converter/other/fiasco/doc/fiasco_options_new.3
+++ b/converter/other/fiasco/doc/fiasco_options_new.3
@@ -438,4 +438,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_renderer_new.3 b/converter/other/fiasco/doc/fiasco_renderer_new.3
index b24d8462..8b5e47cd 100644
--- a/converter/other/fiasco/doc/fiasco_renderer_new.3
+++ b/converter/other/fiasco/doc/fiasco_renderer_new.3
@@ -122,4 +122,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/doc/fiasco_set_verbosity.3 b/converter/other/fiasco/doc/fiasco_set_verbosity.3
index 746854b1..3e88399a 100644
--- a/converter/other/fiasco/doc/fiasco_set_verbosity.3
+++ b/converter/other/fiasco/doc/fiasco_set_verbosity.3
@@ -43,4 +43,4 @@ Finite Automata\fP, Ph.D. thesis, Mensch & Buch Verlag, ISBN
 3-89820-002-7, October 1999.
 
 .SH AUTHOR
-Ullrich Hafner <hafner@bigfoot.de>
+Ullrich Hafner
diff --git a/converter/other/fiasco/fiasco.h b/converter/other/fiasco/fiasco.h
index 59367bb8..88f999fc 100644
--- a/converter/other/fiasco/fiasco.h
+++ b/converter/other/fiasco/fiasco.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  *
  *  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
diff --git a/converter/other/fiasco/fiascotopnm.c b/converter/other/fiasco/fiascotopnm.c
index dfba2256..e95e27fc 100644
--- a/converter/other/fiasco/fiascotopnm.c
+++ b/converter/other/fiasco/fiascotopnm.c
@@ -4,8 +4,8 @@
  *  Written by:     Ullrich Hafner
  *          Michael Unger
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
  
 /*
@@ -15,6 +15,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
@@ -361,6 +362,8 @@ video_decoder (const char *wfa_name, const char *image_name, bool_t panel,
                 while (prg_timer (&fps_timer, STOP) < frame_time) /* wait */
                     ;
             }
+#else
+            if (frame_time) {/* defeat compiler warning */}
 #endif /* not X_DISPLAY_MISSING */   
         }
         free (filename);
diff --git a/converter/other/fiasco/input/basis.c b/converter/other/fiasco/input/basis.c
index 4a748f61..aa371ea1 100644
--- a/converter/other/fiasco/input/basis.c
+++ b/converter/other/fiasco/input/basis.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/basis.h b/converter/other/fiasco/input/basis.h
index fa26bca2..e2242198 100644
--- a/converter/other/fiasco/input/basis.h
+++ b/converter/other/fiasco/input/basis.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/matrices.c b/converter/other/fiasco/input/matrices.c
index 47cde1aa..388855ea 100644
--- a/converter/other/fiasco/input/matrices.c
+++ b/converter/other/fiasco/input/matrices.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *  
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/matrices.h b/converter/other/fiasco/input/matrices.h
index ba8fd9bf..6d1cef15 100644
--- a/converter/other/fiasco/input/matrices.h
+++ b/converter/other/fiasco/input/matrices.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/mc.c b/converter/other/fiasco/input/mc.c
index 070d839e..2d4a073d 100644
--- a/converter/other/fiasco/input/mc.c
+++ b/converter/other/fiasco/input/mc.c
@@ -4,8 +4,8 @@
  *  written by: Michael Unger
  *		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/mc.h b/converter/other/fiasco/input/mc.h
index 1e14d287..9d9d714d 100644
--- a/converter/other/fiasco/input/mc.h
+++ b/converter/other/fiasco/input/mc.h
@@ -4,8 +4,8 @@
  *  written by: Michael Unger
  *		Ullrich Hafner
  
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/nd.c b/converter/other/fiasco/input/nd.c
index 1a68bfbf..52d48e61 100644
--- a/converter/other/fiasco/input/nd.c
+++ b/converter/other/fiasco/input/nd.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/nd.h b/converter/other/fiasco/input/nd.h
index 2c2fff4b..0fe06cf1 100644
--- a/converter/other/fiasco/input/nd.h
+++ b/converter/other/fiasco/input/nd.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/read.c b/converter/other/fiasco/input/read.c
index e6e2d7e8..9f4ac993 100644
--- a/converter/other/fiasco/input/read.c
+++ b/converter/other/fiasco/input/read.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *  
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -14,6 +14,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/other/fiasco/input/read.h b/converter/other/fiasco/input/read.h
index d0d0ee13..1f59f103 100644
--- a/converter/other/fiasco/input/read.h
+++ b/converter/other/fiasco/input/read.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *  
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/tree.c b/converter/other/fiasco/input/tree.c
index e3e7117e..0ac2b6ae 100644
--- a/converter/other/fiasco/input/tree.c
+++ b/converter/other/fiasco/input/tree.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/tree.h b/converter/other/fiasco/input/tree.h
index e4b5f2d8..f743ee98 100644
--- a/converter/other/fiasco/input/tree.h
+++ b/converter/other/fiasco/input/tree.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/weights.c b/converter/other/fiasco/input/weights.c
index 15c35731..8a1e8899 100644
--- a/converter/other/fiasco/input/weights.c
+++ b/converter/other/fiasco/input/weights.c
@@ -3,8 +3,8 @@
  *
  *  Written by:         Ullrich Hafner
  *              
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/input/weights.h b/converter/other/fiasco/input/weights.h
index 1e2285a9..36cea5c2 100644
--- a/converter/other/fiasco/input/weights.h
+++ b/converter/other/fiasco/input/weights.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/arith.c b/converter/other/fiasco/lib/arith.c
index dc35d1d1..825d4757 100644
--- a/converter/other/fiasco/lib/arith.c
+++ b/converter/other/fiasco/lib/arith.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *  
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/arith.h b/converter/other/fiasco/lib/arith.h
index 744eb9d7..04297eb5 100644
--- a/converter/other/fiasco/lib/arith.h
+++ b/converter/other/fiasco/lib/arith.h
@@ -1,8 +1,8 @@
 /*
  *  arith.h
  *
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/bit-io.c b/converter/other/fiasco/lib/bit-io.c
index 1bfef598..f69343e6 100644
--- a/converter/other/fiasco/lib/bit-io.c
+++ b/converter/other/fiasco/lib/bit-io.c
@@ -3,8 +3,8 @@
  *
  *  Written by:     Ullrich Hafner
  *  
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
  
 /*
@@ -14,6 +14,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/other/fiasco/lib/bit-io.h b/converter/other/fiasco/lib/bit-io.h
index d37cc47c..08029824 100644
--- a/converter/other/fiasco/lib/bit-io.h
+++ b/converter/other/fiasco/lib/bit-io.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/dither.c b/converter/other/fiasco/lib/dither.c
index accd9dd6..669bb16c 100644
--- a/converter/other/fiasco/lib/dither.c
+++ b/converter/other/fiasco/lib/dither.c
@@ -3,8 +3,8 @@
  *
  *  Adapted by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -713,15 +713,11 @@ display_24_bit_bgr (const struct fiasco_renderer *this, unsigned char *ximage,
       word_t 	   *cbptr, *crptr;	/* pointer to chroma bands */
       word_t 	   *yptr;		/* pointers to lumincance band */
       int 	   *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;
-      unsigned int *r_table, *g_table, *b_table;
 
       Cr_g_tab = private->Cr_g_tab;
       Cr_r_tab = private->Cr_r_tab;
       Cb_b_tab = private->Cb_b_tab;
       Cb_g_tab = private->Cb_g_tab;
-      r_table  = private->r_table;
-      g_table  = private->g_table;
-      b_table  = private->b_table;
       yptr     = image->pixels [Y];
       cbptr    = image->pixels [Cb];
       crptr    = image->pixels [Cr];
@@ -1044,9 +1040,7 @@ display_24_bit_bgr (const struct fiasco_renderer *this, unsigned char *ximage,
    {
       unsigned int *dst;		/* pointer to dithered pixels */
       word_t	   *src;		/* current pixel of frame */
-      unsigned int *y_table;
 
-      y_table = private->y_table;
       dst     = (unsigned int *) out;
       src     = image->pixels [GRAY];
 
@@ -1164,15 +1158,11 @@ display_24_bit_rgb (const struct fiasco_renderer *this, unsigned char *ximage,
       word_t 	   *cbptr, *crptr;	/* pointer to chroma bands */
       word_t 	   *yptr;		/* pointers to lumincance band */
       int 	   *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;
-      unsigned int *r_table, *g_table, *b_table;
 
       Cr_g_tab = private->Cr_g_tab;
       Cr_r_tab = private->Cr_r_tab;
       Cb_b_tab = private->Cb_b_tab;
       Cb_g_tab = private->Cb_g_tab;
-      r_table  = private->r_table;
-      g_table  = private->g_table;
-      b_table  = private->b_table;
       yptr     = image->pixels [Y];
       cbptr    = image->pixels [Cb];
       crptr    = image->pixels [Cr];
@@ -1495,9 +1485,7 @@ display_24_bit_rgb (const struct fiasco_renderer *this, unsigned char *ximage,
    {
       unsigned int *dst;		/* pointer to dithered pixels */
       word_t	   *src;		/* current pixel of frame */
-      unsigned int *y_table;
 
-      y_table = private->y_table;
       dst     = (unsigned int *) out;
       src     = image->pixels [GRAY];
 
diff --git a/converter/other/fiasco/lib/dither.h b/converter/other/fiasco/lib/dither.h
index 71f9d3c3..767bca28 100644
--- a/converter/other/fiasco/lib/dither.h
+++ b/converter/other/fiasco/lib/dither.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/error.c b/converter/other/fiasco/lib/error.c
index ee3afe1f..394f896f 100644
--- a/converter/other/fiasco/lib/error.c
+++ b/converter/other/fiasco/lib/error.c
@@ -3,29 +3,19 @@
  *
  *  Written by:		Stefan Frank
  *			Ullrich Hafner
- *  
+ *
  *  Credits:	Modelled after variable argument routines from Jef
- *		Poskanzer's pbmplus package. 
+ *		Poskanzer's pbmplus package.
  *
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
-
-    "int dummy = " change to int dummy; dummy =" for Netpbm to avoid 
-    unused variable warning.
-
- */
-
-/*
- *  $Date: 2000/06/14 20:49:37 $
- *  $Author: hafner $
- *  $Revision: 5.1 $
- *  $State: Exp $
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 #define _ERROR_C
 
 #include "config.h"
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <errno.h>
 
@@ -47,7 +37,7 @@
 /*****************************************************************************
 
 			     local variables
-  
+
 *****************************************************************************/
 
 static fiasco_verbosity_e  verboselevel  = FIASCO_SOME_VERBOSITY;
@@ -60,234 +50,237 @@ jmp_buf env;
 /*****************************************************************************
 
 			       public code
-  
+
 *****************************************************************************/
 
 void
-set_error (const char *format, ...)
-/*
- *  Set error text to given string.
- */
-{
-   va_list     args;
-   unsigned    len = 0;
-   const char *str = format;
-   
-   VA_START (args, format);
-
-   len = strlen (format);
-   while ((str = strchr (str, '%')))
-   {
-      str++;
-      if (*str == 's')
-      {
-	 char *vstring = va_arg (args, char *);
-	 len += strlen (vstring);
-      }
-      else if (*str == 'd')
-      {
-	 int dummy;
-     dummy = va_arg (args, int);
-	 len += 10;
-      }
-      else if (*str == 'c')
-      {
-	 int dummy;
-     dummy = va_arg (args, int);
-	 len += 1;
-      }
-      else
-	 return;
-      str++;
-   }
-   va_end(args);
-
-   VA_START (args, format);
-
-   if (error_message)
-      Free (error_message);
-   error_message = Calloc (len, sizeof (char));
-   
-   vsprintf (error_message, format, args);
-
-   va_end (args);
+set_error(const char * const format, ...) {
+/*----------------------------------------------------------------------------
+  Set error text to given string.
+-----------------------------------------------------------------------------*/
+    va_list      args;
+    unsigned     len;
+    bool         error;
+    const char * str;
+
+    VA_START (args, format);
+
+    /* Compute how long the error text will be: 'len' */
+
+    for (len = strlen(format), str = &format[0], error = false;
+         *str && !error; ) {
+
+        str = strchr(str, '%');
+
+        if (*str) {
+            ++str; /* Move past % */
+            if (*str == 's') {
+                char * const vstring = va_arg (args, char *);
+                len += strlen(vstring);
+            } else if (*str == 'd') {
+                (void)va_arg(args, int);
+                len += 10;
+            } else if (*str == 'c') {
+                (void)va_arg(args, int);
+                len += 1;
+            } else
+                error = true;
+            if (!error)
+                ++str;
+        }
+    }
+    va_end(args);
+
+    if (!error) {
+        VA_START(args, format);
+
+        if (error_message)
+            Free(error_message);
+        error_message = Calloc(len, sizeof (char));
+
+        vsprintf(error_message, format, args);
+
+        va_end(args);
+    }
 }
 
+
+
 void
-error (const char *format, ...)
-/*
- *  Set error text to given string.
- */
-{
-   va_list     args;
-   unsigned    len = 0;
-   const char *str = format;
-   
-   VA_START (args, format);
-
-   len = strlen (format);
-   while ((str = strchr (str, '%')))
-   {
-      str++;
-      if (*str == 's')
-      {
-	 char *vstring = va_arg (args, char *);
-	 len += strlen (vstring);
-      }
-      else if (*str == 'd')
-      {
-	 int dummy;
-     dummy = va_arg (args, int);
-	 len += 10;
-      }
-      else if (*str == 'c')
-      {
-	 int dummy;
-     dummy = va_arg (args, int);
-	 len += 1;
-      }
-      else
-      {
+error(const char * const format, ...) {
+/*----------------------------------------------------------------------------
+  Set error text to given string.
+-----------------------------------------------------------------------------*/
+    va_list      args;
+    unsigned     len;
+    const char * str;
+
+    len = 0; /* initial value */
+    str = &format[0];  /* initial value */
+
+    VA_START (args, format);
+
+    len = strlen (format);
+    while ((str = strchr (str, '%'))) {
+        ++str;
+        if (*str == 's') {
+            char * const vstring = va_arg (args, char *);
+            len += strlen(vstring);
+        } else if (*str == 'd') {
+            (void)va_arg(args, int);
+            len += 10;
+        } else if (*str == 'c') {
+            (void)va_arg(args, int);
+            len += 1;
+        } else {
 #if HAVE_SETJMP_H
-	 longjmp (env, 1);
-#else /* not HAVE_SETJMP_H */
-	 exit (1);
-#endif /* HAVE_SETJMP_H */
-      };
-      
-      str++;
-   }
-   va_end(args);
-
-   VA_START (args, format);
-
-   if (error_message)
-      Free (error_message);
-   error_message = Calloc (len, sizeof (char));
-   
-   vsprintf (error_message, format, args);
-
-   va_end (args);
-   
+            longjmp(env, 1);
+#else
+            exit(1);
+#endif
+        };
+
+        ++str;
+    }
+    va_end(args);
+
+    VA_START(args, format);
+
+    if (error_message)
+        Free(error_message);
+    error_message = Calloc(len, sizeof (char));
+
+    vsprintf(error_message, format, args);
+
+    va_end(args);
+
 #if HAVE_SETJMP_H
-   longjmp (env, 1);
-#else /* not HAVE_SETJMP_H */
-   exit (1);
-#endif /* HAVE_SETJMP_H */
+    longjmp(env, 1);
+#else
+    exit(1);
+#endif
 }
 
+
+
 const char *
-fiasco_get_error_message (void)
-/*
- *  Return value:
- *	Last error message of FIASCO library.
- */
-{
-   return error_message ? error_message : "";
+fiasco_get_error_message(void) {
+/*----------------------------------------------------------------------------
+  Last error message of FIASCO library.
+-----------------------------------------------------------------------------*/
+    return error_message ? error_message : "";
 }
 
+
+
 const char *
-get_system_error (void)
-{
-   return strerror (errno);
+get_system_error(void) {
+    return strerror(errno);
 }
 
+
+
 void
-file_error (const char *filename)
-/*
- *  Print file error message and exit.
- *
- *  No return value.
- */
-{
-   error ("File `%s': I/O Error - %s.", filename, get_system_error ());
+file_error(const char * const filename) {
+/*----------------------------------------------------------------------------
+   Print file error message and exit.
+-----------------------------------------------------------------------------*/
+    error("File `%s': I/O Error - %s.", filename, get_system_error ());
 }
 
-void 
-warning (const char *format, ...)
-/*
- *  Issue a warning and continue execution.
- *
- *  No return value.
- */
-{
-   va_list	args;
-
-   VA_START (args, format);
 
-   if (verboselevel == FIASCO_NO_VERBOSITY)
-      return;
-	
-   fprintf (stderr, "Warning: ");
-   vfprintf (stderr, format, args);
-   fputc ('\n', stderr);
 
-   va_end (args);
+void
+warning(const char * const format, ...) {
+/*----------------------------------------------------------------------------
+  Issue a warning.
+-----------------------------------------------------------------------------*/
+    va_list	args;
+
+    VA_START (args, format);
+
+    if (verboselevel == FIASCO_NO_VERBOSITY) {
+        /* User doesn't want warnings */
+    } else {
+        fprintf (stderr, "Warning: ");
+        vfprintf (stderr, format, args);
+        fputc ('\n', stderr);
+    }
+    va_end (args);
 }
 
-void 
-message (const char *format, ...)
-/*
- *  Print a message to stderr.
- */
-{
-   va_list args;
-
-   VA_START (args, format);
 
-   if (verboselevel == FIASCO_NO_VERBOSITY)
-      return;
 
-   vfprintf (stderr, format, args);
-   fputc ('\n', stderr);
-   va_end (args);
+void
+message(const char * const format, ...) {
+/*----------------------------------------------------------------------------
+   Print a message to Standard Error
+-----------------------------------------------------------------------------*/
+    va_list args;
+
+    VA_START (args, format);
+
+    if (verboselevel == FIASCO_NO_VERBOSITY) {
+        /* User doesn't want messages */
+    } else {
+        vfprintf (stderr, format, args);
+        fputc ('\n', stderr);
+    }
+    va_end (args);
 }
 
-void 
-debug_message (const char *format, ...)
-/*
- *  Print a message to stderr.
- */
-{
-   va_list args;
 
-   VA_START (args, format);
-
-   if (verboselevel < FIASCO_ULTIMATE_VERBOSITY)
-      return;
-
-   fprintf (stderr, "*** ");
-   vfprintf (stderr, format, args);
-   fputc ('\n', stderr);
-   va_end (args);
-}
 
 void
-info (const char *format, ...)
-/*
- *  Print a message to stderr. Do not append a newline.
- */
-{
-   va_list args;
+debug_message(const char * const format, ...) {
+/*----------------------------------------------------------------------------
+   Print a message to Standard Error if debug messages are enabled.
+-----------------------------------------------------------------------------*/
+    va_list args;
+
+    VA_START (args, format);
+
+    if (verboselevel >= FIASCO_ULTIMATE_VERBOSITY) {
+        fprintf (stderr, "*** ");
+        vfprintf (stderr, format, args);
+        fputc ('\n', stderr);
+    }
+    va_end (args);
+}
 
-   VA_START (args, format);
 
-   if (verboselevel == FIASCO_NO_VERBOSITY)
-      return;
 
-   vfprintf (stderr, format, args);
-   fflush (stderr);
-   va_end (args);
+void
+info(const char * const format, ...) {
+/*----------------------------------------------------------------------------
+   Print a message to stderr. Do not append a newline.
+-----------------------------------------------------------------------------*/
+    va_list args;
+
+    VA_START (args, format);
+
+    if (verboselevel == FIASCO_NO_VERBOSITY) {
+        /* User doesn't want informational messages */
+    } else {
+        vfprintf (stderr, format, args);
+        fflush (stderr);
+    }
+    va_end (args);
 }
 
+
+
 void
-fiasco_set_verbosity (fiasco_verbosity_e level)
-{
+fiasco_set_verbosity(fiasco_verbosity_e const level) {
    verboselevel = level;
 }
 
+
+
 fiasco_verbosity_e
-fiasco_get_verbosity (void)
-{
+fiasco_get_verbosity(void) {
    return verboselevel;
 }
+
+
+
diff --git a/converter/other/fiasco/lib/error.h b/converter/other/fiasco/lib/error.h
index 288b25f4..4159ccd2 100644
--- a/converter/other/fiasco/lib/error.h
+++ b/converter/other/fiasco/lib/error.h
@@ -4,8 +4,8 @@
  *  Written by:     Stefan Frank
  *          Ullrich Hafner
  *
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 #ifndef ERROR_H_INCLUDED
diff --git a/converter/other/fiasco/lib/image.c b/converter/other/fiasco/lib/image.c
index fa3b2db5..a700fe88 100644
--- a/converter/other/fiasco/lib/image.c
+++ b/converter/other/fiasco/lib/image.c
@@ -3,8 +3,8 @@
  *
  *  Written by:     Ullrich Hafner
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/image.h b/converter/other/fiasco/lib/image.h
index 00978526..a87a3c05 100644
--- a/converter/other/fiasco/lib/image.h
+++ b/converter/other/fiasco/lib/image.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/list.c b/converter/other/fiasco/lib/list.c
index bb4efae1..b67ff4b8 100644
--- a/converter/other/fiasco/lib/list.c
+++ b/converter/other/fiasco/lib/list.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/list.h b/converter/other/fiasco/lib/list.h
index db7c08b2..68577dfd 100644
--- a/converter/other/fiasco/lib/list.h
+++ b/converter/other/fiasco/lib/list.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/macros.h b/converter/other/fiasco/lib/macros.h
index 9968110a..0bc80e7c 100644
--- a/converter/other/fiasco/lib/macros.h
+++ b/converter/other/fiasco/lib/macros.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/misc.c b/converter/other/fiasco/lib/misc.c
index 782ed1e9..c5629c5c 100644
--- a/converter/other/fiasco/lib/misc.c
+++ b/converter/other/fiasco/lib/misc.c
@@ -6,8 +6,8 @@
  *  Written by:		Stefan Frank
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/misc.h b/converter/other/fiasco/lib/misc.h
index 28fd8b5a..6f643b7a 100644
--- a/converter/other/fiasco/lib/misc.h
+++ b/converter/other/fiasco/lib/misc.h
@@ -4,8 +4,8 @@
  *  Written by:		Stefan Frank
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/rpf.c b/converter/other/fiasco/lib/rpf.c
index 84c1f9b6..e6ff6e09 100644
--- a/converter/other/fiasco/lib/rpf.c
+++ b/converter/other/fiasco/lib/rpf.c
@@ -1,10 +1,10 @@
 /*
- *  rpf.c:		Conversion of float to reduced precision format values
+ *  rpf.c:      Conversion of float to reduced precision format values
  *
- *  Written by:		Stefan Frank
- *			Richard Krampfl
- *			Ullrich Hafner
- *		
+ *  Written by:     Stefan Frank
+ *          Richard Krampfl
+ *          Ullrich Hafner
+ *      
  *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
  *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
  */
@@ -18,6 +18,7 @@
 
 #include "pm_config.h"
 #include "config.h"
+#include "mallocvar.h"
 
 #include "types.h"
 #include "macros.h"
@@ -26,39 +27,37 @@
 #include "misc.h"
 #include "rpf.h"
 
-/* 
- * CAUTION: The IEEE float format must be used by your compiler,
- *          or all following code is void!
- */
+int const RPF_ZERO = -1;
 
-#if BYTE_ORDER == BIG_ENDIAN
-/*
- *  Big-Endian Architecture (e.g. SUN, Motorola)
- *  Memory representation of integer 0x00112233 is 00,11,22,33
- */
+/*****************************************************************************
 
-enum real_bytes {BYTE_0, BYTE_1, BYTE_2, BYTE_3};
+                   private code
+  
+*****************************************************************************/
 
-#else
-/*
- *  Little-Endian Architecture (e.g. Intel, VAX, Alpha)
- *  Memory representation of integer 0x00112233 is 33,22,11,00
- */
 
-enum real_bytes {BYTE_3, BYTE_2, BYTE_1, BYTE_0};
+typedef struct {
+    double fraction;
+    int    exponent;
+}  FracExp;
 
-#endif
 
-const int RPF_ZERO = -1;
 
-/*****************************************************************************
+static FracExp
+fracExpFromDouble(double const x) {
+
+    FracExp retval;
+
+    retval.fraction = frexp(x, &retval.exponent);
+
+    return retval;
+}
+
 
-			       private code
-  
-*****************************************************************************/
 
 int
-rtob (real_t f, const rpf_t *rpf)
+rtob (real_t        const f,
+      const rpf_t * const rpfP)
 /*
  *  Convert real number 'f' into fixed point format.
  *  The real number in [-'range'; +'range'] is scaled to [-1 ; +1].
@@ -66,111 +65,98 @@ rtob (real_t f, const rpf_t *rpf)
  *  packed into one integer.  
  *
  *  Return value:
- *	real value in reduced precision format
+ *  real value in reduced precision format
  */
 {  
-   unsigned int	mantissa;
-   int		exponent, sign;
-   union
-   {
-      float f;
-      unsigned char c[4];
-   } v;					/* conversion dummy */
-
-   f  /= rpf->range;			/* scale f to [-1,+1] */	
-   v.f = f;
-
-   /*
-    *  Extract mantissa (23 Bits), exponent (8 Bits) and sign (1 Bit)
-    */
-
-   mantissa = ((((v.c[BYTE_1] & 127) << 8 ) | v.c[BYTE_2]) << 8) | v.c[BYTE_3];
-   exponent = (((v.c[BYTE_0] & 127) << 1) | (v.c[BYTE_1] & 128 ? 1 : 0)) - 126;
-   sign     = v.c[BYTE_0] & 128 ? 1 : 0;
-		
-   /*
-    *  Generate reduced precision mantissa.
-    */
-   mantissa >>= 1;				/* shift 1 into from left */
-   mantissa  |= (1 << 22);
-   if (exponent > 0) 
-      mantissa <<= exponent;
-   else
-      mantissa >>= -exponent;  
+    /*
+     *  Extract mantissa (23 Bits), exponent (8 Bits) and sign (1 Bit)
+     */
+
+    double const normalized = f / rpfP->range;
+        /* 'f' scaled to [-1,+1] */    
+    FracExp const fracExp = fracExpFromDouble(normalized);
+    unsigned int const signedMantissa =
+        (unsigned int) (fracExp.fraction * (1<<23));
+
+    unsigned int mantissa;
+    unsigned int sign;  /* 0 for positive; 1 for negative */
+    
+    if (signedMantissa < 0) {
+        mantissa = -signedMantissa;
+        sign = 1;
+    } else {
+        mantissa = +signedMantissa;
+        sign = 0;
+    }
+
+    /*
+     *  Generate reduced precision mantissa.
+     */
+    if (fracExp.exponent > 0) 
+        mantissa <<= fracExp.exponent;
+    else
+        mantissa >>= -fracExp.exponent;  
+    
+    mantissa >>= (23 - rpfP->mantissa_bits - 1);
+
+    mantissa +=  1;          /* Round last bit. */
+    mantissa >>= 1;
    
-   mantissa >>= (23 - rpf->mantissa_bits - 1);
-
-   mantissa +=  1;			/* Round last bit. */
-   mantissa >>= 1;
-   
-   if (mantissa == 0)			/* close to zero */
-      return RPF_ZERO;
-   else if (mantissa >= (1U << rpf->mantissa_bits)) /* overflow */
-      return sign;
-   else
-      return ((mantissa & ((1U << rpf->mantissa_bits) - 1)) << 1) | sign;
+    if (mantissa == 0)           /* close to zero */
+        return RPF_ZERO;
+    else if (mantissa >= (1U << rpfP->mantissa_bits)) /* overflow */
+        return sign;
+    else
+        return ((mantissa & ((1U << rpfP->mantissa_bits) - 1)) << 1) | sign;
 }
 
+
+
 float
-btor (int binary, const rpf_t *rpf)
+btor (int           const binary,
+      const rpf_t * const rpfP)
 /*
  *  Convert value 'binary' in reduced precision format to a real value.
- *  For more information refer to function lin_rtob() above.
+ *  For more information refer to function rtob() above.
  *
  *  Return value:
- *	converted value
+ *  converted value
  */
 {
-   unsigned int	mantissa;
-   int		sign, exponent;
-   union
-   {
-      float f;
-      unsigned char c[4];
-   } value;
-
-   if (binary == RPF_ZERO)
-      return 0;
-
-   if (binary < 0 || binary >= 1 << (rpf->mantissa_bits + 1))
-      error ("Reduced precision format: value %d out of range.", binary);
-
-   /*
-    *  Restore IEEE float format:
-    *  mantissa (23 Bits), exponent (8 Bits) and sign (1 Bit)
-    */
+    unsigned int mantissa;
+    float sign;
+    float f;
+ 
+    if (binary == RPF_ZERO)
+        return 0;
+
+    if (binary < 0 || binary >= 1 << (rpfP->mantissa_bits + 1))
+        error ("Reduced precision format: value %d out of range.", binary);
+
+    /*
+     *  Restore IEEE float format:
+     *  mantissa (23 Bits), exponent (8 Bits) and sign (1 Bit)
+     */
    
-   sign       = binary & 1;
-   mantissa   = (binary & ((1 << (rpf->mantissa_bits + 1)) - 1)) >> 1; 
-   mantissa <<= (23 - rpf->mantissa_bits);
-   exponent   = 0;
-
-   if (mantissa == 0)
-   {
-      value.f = (sign ? -1.0 : 1.0);
-   }
-   else
-   {
-      while (!(mantissa & (1 << 22)))	/* normalize mantissa */
-      {
-	 exponent--;
-	 mantissa <<= 1;
-      }
-      mantissa <<= 1;
-
-      value.c[BYTE_0] = (sign << 7) | ((exponent + 126) >> 1);
-      value.c[BYTE_1] = (((exponent + 126) & 1) << 7)
-			| ((mantissa  >> 16) & 127);
-      value.c[BYTE_2] = (mantissa >> 8) & 255;
-      value.c[BYTE_3] = mantissa & 255;
-   }
+    sign       = (binary & 0x1) == 0 ? 1.0 : -1.0;
+    mantissa   = (binary & ((0x1 << (rpfP->mantissa_bits + 1)) - 1)) >> 1; 
+    mantissa <<= (23 - rpfP->mantissa_bits);
+
+    if (mantissa == 0) 
+        f = sign;
+    else
+        f =  sign * (float) mantissa / 8388608;
    
-   return value.f * rpf->range;		/* expand [ -1 ; +1 ] to
-					   [ -range ; +range ] */
+    return f * rpfP->range;       /* expand [ -1 ; +1 ] to
+                                     [ -range ; +range ] */
 }
 
+
+
+
 rpf_t *
-alloc_rpf (unsigned mantissa, fiasco_rpf_range_e range)
+alloc_rpf (unsigned           const mantissa,
+           fiasco_rpf_range_e const range)
 /*
  *  Reduced precision format constructor.
  *  Allocate memory for the rpf_t structure.
@@ -180,45 +166,47 @@ alloc_rpf (unsigned mantissa, fiasco_rpf_range_e range)
  *  returned. 
  *
  *  Return value
- *	pointer to the new rpf structure
+ *  pointer to the new rpf structure
  */
 {
-   rpf_t *rpf = Calloc (1, sizeof (rpf_t));
+    rpf_t * rpfP;
+
+    MALLOCVAR(rpfP);
    
-   if (mantissa < 2)
-   {
-      warning (_("Size of RPF mantissa has to be in the interval [2,8]. "
-		 "Using minimum value 2.\n"));
-      mantissa = 2;
-   }
-   else if (mantissa > 8)
-   {
-      warning (_("Size of RPF mantissa has to be in the interval [2,8]. "
-		 "Using maximum value 8.\n"));
-      mantissa = 2;
-   }
-
-   rpf->mantissa_bits = mantissa;
-   rpf->range_e       = range;
-   switch (range)
-   {
-      case FIASCO_RPF_RANGE_0_75:
-	 rpf->range = 0.75;
-	 break;
-      case FIASCO_RPF_RANGE_1_50:
-	 rpf->range = 1.50;
-	 break;
-      case FIASCO_RPF_RANGE_2_00:
-	 rpf->range = 2.00;
-	 break;
-      case FIASCO_RPF_RANGE_1_00:
-	 rpf->range = 1.00;
-	 break;
-      default:
-	 warning (_("Invalid RPF range specified. Using default value 1.0."));
-	 rpf->range   = 1.00;
-	 rpf->range_e = FIASCO_RPF_RANGE_1_00;
-	 break;
-   }
-   return rpf;
+    if (mantissa < 2) {
+        warning (_("Size of RPF mantissa has to be in the interval [2,8]. "
+                   "Using minimum value 2.\n"));
+        rpfP->mantissa_bits = 2;
+    } else if (mantissa > 8) {
+        warning (_("Size of RPF mantissa has to be in the interval [2,8]. "
+                   "Using maximum value 8.\n"));
+        rpfP->mantissa_bits = 2;
+    } else
+        rpfP->mantissa_bits = mantissa;
+
+    switch (range) {
+    case FIASCO_RPF_RANGE_0_75:
+        rpfP->range   = 0.75;
+        rpfP->range_e = range;
+        break;
+    case FIASCO_RPF_RANGE_1_50:
+        rpfP->range   = 1.50;
+        rpfP->range_e = range;
+        break;
+    case FIASCO_RPF_RANGE_2_00:
+        rpfP->range   = 2.00;
+        rpfP->range_e = range;
+        break;
+    case FIASCO_RPF_RANGE_1_00:
+        rpfP->range   = 1.00;
+        rpfP->range_e = range;
+        break;
+    default:
+        warning (_("Invalid RPF range specified. Using default value 1.0."));
+        rpfP->range   = 1.00;
+        rpfP->range_e = FIASCO_RPF_RANGE_1_00;
+        break;
+    }
+    return rpfP;
 }
+
diff --git a/converter/other/fiasco/lib/rpf.h b/converter/other/fiasco/lib/rpf.h
index ba3ff6be..e543f855 100644
--- a/converter/other/fiasco/lib/rpf.h
+++ b/converter/other/fiasco/lib/rpf.h
@@ -5,8 +5,8 @@
  *			Richard Krampfl
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/lib/types.h b/converter/other/fiasco/lib/types.h
index 16d8028c..02e40a90 100644
--- a/converter/other/fiasco/lib/types.h
+++ b/converter/other/fiasco/lib/types.h
@@ -3,8 +3,8 @@
  *
  *  Written by:     Ullrich Hafner
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/matrices.c b/converter/other/fiasco/output/matrices.c
index 01189669..7dc0e6d9 100644
--- a/converter/other/fiasco/output/matrices.c
+++ b/converter/other/fiasco/output/matrices.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /* NETPBM: When you call delta_encoding() with last_domain < 4, it
diff --git a/converter/other/fiasco/output/matrices.h b/converter/other/fiasco/output/matrices.h
index f880fef8..44671496 100644
--- a/converter/other/fiasco/output/matrices.h
+++ b/converter/other/fiasco/output/matrices.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/mc.c b/converter/other/fiasco/output/mc.c
index afff586b..d048bef5 100644
--- a/converter/other/fiasco/output/mc.c
+++ b/converter/other/fiasco/output/mc.c
@@ -4,8 +4,8 @@
  *  Written by:		Michael Unger
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/mc.h b/converter/other/fiasco/output/mc.h
index b7843fd8..cacddfe7 100644
--- a/converter/other/fiasco/output/mc.h
+++ b/converter/other/fiasco/output/mc.h
@@ -4,8 +4,8 @@
  *  Written by:		Michael Unger
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/nd.c b/converter/other/fiasco/output/nd.c
index a09ff762..65a85467 100644
--- a/converter/other/fiasco/output/nd.c
+++ b/converter/other/fiasco/output/nd.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/nd.h b/converter/other/fiasco/output/nd.h
index 600b3d73..01a37838 100644
--- a/converter/other/fiasco/output/nd.h
+++ b/converter/other/fiasco/output/nd.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/tree.c b/converter/other/fiasco/output/tree.c
index 0056d7dd..2eae2df9 100644
--- a/converter/other/fiasco/output/tree.c
+++ b/converter/other/fiasco/output/tree.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/tree.h b/converter/other/fiasco/output/tree.h
index 6f8a3800..50fe2279 100644
--- a/converter/other/fiasco/output/tree.h
+++ b/converter/other/fiasco/output/tree.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/weights.c b/converter/other/fiasco/output/weights.c
index 5aa17674..0203fef0 100644
--- a/converter/other/fiasco/output/weights.c
+++ b/converter/other/fiasco/output/weights.c
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/weights.h b/converter/other/fiasco/output/weights.h
index 271203ad..f22bd9f8 100644
--- a/converter/other/fiasco/output/weights.h
+++ b/converter/other/fiasco/output/weights.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/write.c b/converter/other/fiasco/output/write.c
index e6185ad3..d6faee26 100644
--- a/converter/other/fiasco/output/write.c
+++ b/converter/other/fiasco/output/write.c
@@ -3,8 +3,8 @@
  *
  *  Written by:     Ullrich Hafner
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/output/write.h b/converter/other/fiasco/output/write.h
index a3ede1f4..6bdc2f1b 100644
--- a/converter/other/fiasco/output/write.h
+++ b/converter/other/fiasco/output/write.h
@@ -3,8 +3,8 @@
  *
  *  Written by:		Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/params.c b/converter/other/fiasco/params.c
index afacbada..7d0b84ab 100644
--- a/converter/other/fiasco/params.c
+++ b/converter/other/fiasco/params.c
@@ -4,8 +4,8 @@
  *  Written by:		Stefan Frank
  *			Ullrich Hafner
  *		
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
@@ -15,6 +15,7 @@
  *  $State: Exp $
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1
     /* Make sure strdup() is in string.h and strcaseeq() is in nstring.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
@@ -325,8 +326,17 @@ parseargs (param_t *usr_params,
 		      NO, sys_path, usr_file_name);
 	    else if (streq (params [param_index].name, "version"))
 	    {
-	       fprintf (stderr, "%s " VERSION "\n", argv [0]);
-	       exit (2);
+           fprintf (stderr, "%s " VERSION "\n", argv [0]);
+           {
+              /* Kludge for standard Netpbm version announcement */
+              char * modified_argv[2];
+              int argc;
+              modified_argv[0] = argv[0];
+              modified_argv[1] = (char *) "--version";
+              argc = 2;
+              pm_proginit(&argc, (const char **) modified_argv);
+           }
+           exit (2);
 	    }
 	    else if (streq (params [param_index].name, "verbose"))
 	       fiasco_set_verbosity (
diff --git a/converter/other/fiasco/params.h b/converter/other/fiasco/params.h
index 810a9ff0..a1164cf7 100644
--- a/converter/other/fiasco/params.h
+++ b/converter/other/fiasco/params.h
@@ -4,8 +4,8 @@
  *  Written by:     Stefan Frank
  *          Ullrich Hafner
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
 
 /*
diff --git a/converter/other/fiasco/pnmtofiasco.c b/converter/other/fiasco/pnmtofiasco.c
index eebd09a9..e2656c71 100644
--- a/converter/other/fiasco/pnmtofiasco.c
+++ b/converter/other/fiasco/pnmtofiasco.c
@@ -3,8 +3,8 @@
  *
  *  Written by:     Ullrich Hafner
  *      
- *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
- *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+ *  This file is part of FIASCO (Fractal Image And Sequence COdec)
+ *  Copyright (C) 1994-2000 Ullrich Hafner
  */
  
 /*
diff --git a/converter/other/fiasco/system.fiascorc b/converter/other/fiasco/system.fiascorc
index 86ff2da2..77d60603 100644
--- a/converter/other/fiasco/system.fiascorc
+++ b/converter/other/fiasco/system.fiascorc
@@ -3,8 +3,8 @@
 # 
 #  Written by:		Ullrich Hafner
 #		
-#  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
-#  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
+#  This file is part of FIASCO (Fractal Image And Sequence COdec)
+#  Copyright (C) 1994-2000 Ullrich Hafner
 
 #
 #  $Date: 2000/06/25 16:38:01 $
diff --git a/converter/other/gemtopnm.c b/converter/other/gemtopnm.c
index aac74793..d862213f 100644
--- a/converter/other/gemtopnm.c
+++ b/converter/other/gemtopnm.c
@@ -36,7 +36,7 @@
  * read 4-plane color IMG files.  Therefore changed from PBM to PPM.
  * Bryan changed it further to use the PNM facilities so it outputs
  * both PBM and PPM in the Netpbm tradition.  Name changed from
- * gemtopbm to gemtopnm.  
+ * gemtopbm to gemtopnm.
  */
 
 #include <assert.h>
@@ -50,106 +50,106 @@
 char pattern[8];
 
 static void getinit ARGS ((FILE *file, int *colsP, int *rowsP, int *padrightP,
-			   int *patlenP, int *planesP));
+               int *patlenP, int *planesP));
 
 int
 main(argc, argv)
-	int             argc;
-	char           *argv[];
+    int             argc;
+    char           *argv[];
 {
-	int     debug = 0;
-	FILE    *f;
+    int     debug = 0;
+    FILE    *f;
     int     row;
-	int     rows, cols, padright, patlen, planes;
+    int     rows, cols, padright, patlen, planes;
       /* attributes of input image */
     int type;  /* The format type (PBM/PPM) of the output image */
-	bit	*bitrow[4];
+    bit *bitrow[4];
       /* One row of input, one or four planes.  (If one, only [0] is defined)*/
     xel * xelrow;  /* One row of output */
-	const char * const usage = "[-debug] [gem IMG file]";
-	int argn;
+    const char * const usage = "[-debug] [gem IMG file]";
+    int argn;
 
 /* Process multiple planes by maintaining a separate row of bits for each
- * plane. In a single-plane image, all we have to do is write out the 
+ * plane. In a single-plane image, all we have to do is write out the
  * first plane; in a multiple-plane image, we combine them just before writing
  * out the row.
  */
-	pnm_init( &argc, argv );
-    
+    pnm_init( &argc, argv );
+
     argn = 1;
 
-	while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0')
-	  {
-	    if (pm_keymatch(argv[1], "-debug", 2))
-	      debug = 1;
-	    else
-	      pm_usage (usage);
-	    ++argn;
-	  }
+    while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0')
+      {
+        if (pm_keymatch(argv[1], "-debug", 2))
+          debug = 1;
+        else
+          pm_usage (usage);
+        ++argn;
+      }
 
-	if (argc == argn)
-		f = stdin;
-	else {
-		f = pm_openr (argv[argn]);
-		++argn;
-	}
+    if (argc == argn)
+        f = stdin;
+    else {
+        f = pm_openr (argv[argn]);
+        ++argn;
+    }
 
-	if (argn != argc)
-	  pm_usage (usage);
+    if (argn != argc)
+      pm_usage (usage);
 
-	getinit (f, &cols, &rows, &padright, &patlen, &planes);
+    getinit (f, &cols, &rows, &padright, &patlen, &planes);
 
-    if (planes == 1) 
+    if (planes == 1)
         type = PBM_TYPE;
-    else 
+    else
         type = PPM_TYPE;
 
-	pnm_writepnminit( stdout, cols, rows, MAXVAL, type, 0 );
+    pnm_writepnminit( stdout, cols, rows, MAXVAL, type, 0 );
 
-    { 
+    {
         /* allocate input row data structure */
         int plane;
-        for (plane = 0; plane < planes; plane++) 
+        for (plane = 0; plane < planes; plane++)
             bitrow[plane] = malloc (cols + padright);
     }
     xelrow = pnm_allocrow(cols+padright);   /* Output row */
 
-	for (row = 0; row < rows; ) {
+    for (row = 0; row < rows; ) {
       int linerep;
       int plane;
 
-	  linerep = 1;
-	  for (plane = 0; plane < planes; plane++) {
+      linerep = 1;
+      for (plane = 0; plane < planes; plane++) {
         int col;
-		col = 0;
-		while (col < cols) {
+        col = 0;
+        while (col < cols) {
             int c;
-			switch (c = getc(f)) {
-			case 0x80:	/* Bit String */
+            switch (c = getc(f)) {
+            case 0x80:  /* Bit String */
             {
                 int j;
-				c = getc(f);	/* Byte count */
-				if (debug)
+                c = getc(f);    /* Byte count */
+                if (debug)
                   pm_message("bit string of %d bytes", c);
-				
-				if (col + c * 8 > cols + padright)
-				  pm_error ("bad byte count");
-				for (j = 0; j < c; ++j) {
+
+                if (col + c * 8 > cols + padright)
+                  pm_error ("bad byte count");
+                for (j = 0; j < c; ++j) {
                     int cc, k;
-					cc = getc(f);
-					for (k = 0x80; k; k >>= 1) {
-						bitrow[plane][col] = (k & cc) ? 0 : 1;
-						++col;
-					}
-				}
+                    cc = getc(f);
+                    for (k = 0x80; k; k >>= 1) {
+                        bitrow[plane][col] = (k & cc) ? 0 : 1;
+                        ++col;
+                    }
+                }
             }
             break;
-			case 0:		/* Pattern run */
+            case 0:     /* Pattern run */
             {
                 int j, l;
-				c = getc(f);	/* Repeat count */
-				if (debug)
-					pm_message("pattern run of %d repetitions",	c);
+                c = getc(f);    /* Repeat count */
+                if (debug)
+                    pm_message("pattern run of %d repetitions", c);
                 /* line repeat */
                 if (c == 0) {
                     c = getc(f);
@@ -158,25 +158,25 @@ main(argc, argv)
                     linerep = getc(f);
                     break;
                 }
-				fread (pattern, 1, patlen, f);
-				if (col + c * patlen * 8 > cols + padright)
-				  pm_error ("bad pattern repeat count");
-				for (j = 0; j < c; ++j)
-					for (l = 0; l < patlen; ++l) {
+                fread (pattern, 1, patlen, f);
+                if (col + c * patlen * 8 > cols + padright)
+                  pm_error ("bad pattern repeat count");
+                for (j = 0; j < c; ++j)
+                    for (l = 0; l < patlen; ++l) {
                         int k;
-						for (k = 0x80; k; k >>= 1) {
-							bitrow[plane][col] = (k & pattern[l]) ? 0 : 1;
-							++col;
-						}
+                        for (k = 0x80; k; k >>= 1) {
+                            bitrow[plane][col] = (k & pattern[l]) ? 0 : 1;
+                            ++col;
+                        }
                     }
             }
             break;
 
-			default:	/* Solid run */
+            default:    /* Solid run */
             {
                 int l, j;
-				if (debug)
-					pm_message("solid run of %d bytes %s", c & 0x7f,
+                if (debug)
+                    pm_message("solid run of %d bytes %s", c & 0x7f,
                                c & 0x80 ? "on" : "off" );
                 /* each byte had eight bits DSB */
                 l = (c & 0x80) ? 0: 1;
@@ -185,23 +185,23 @@ main(argc, argv)
                     pm_error ("bad solid run repeat count");
                 for (j = 0; j < c; ++j) {
                     bitrow[plane][col] = l;
-					++col;
+                    ++col;
                 }
             }
-				break;
+                break;
 
-			case EOF:	/* End of file */
-				pm_error( "end of file reached" );
+            case EOF:   /* End of file */
+                pm_error( "end of file reached" );
 
-			}
-		}
+            }
+        }
                 if ( debug )
                         pm_message( "EOL plane %d row %d", plane, row );
                 if (col != cols + padright)
                         pm_error( "EOL beyond edge" );
-	  }
+      }
 
-	  if (planes == 4) {
+      if (planes == 4) {
           /* Construct a pixel from the 4 planes of bits for this row */
           int col;
           for (col = 0; col < cols; col++) {
@@ -212,21 +212,21 @@ main(argc, argv)
             const int b_bit = !bitrow[2][col];
             i = bitrow[3][col];
 
-			/* Deal with weird GEM palette - white/black/gray are
-               encoded oddly 
+            /* Deal with weird GEM palette - white/black/gray are
+               encoded oddly
             */
-			if (r_bit == g_bit && g_bit == b_bit) {
+            if (r_bit == g_bit && g_bit == b_bit) {
                 /* It's black, white, or gray */
-				if (r_bit && i) r = LIGHT;
-				else if (r_bit) r = BLACK;
-				else if (i) r = MAXVAL;
-				else r = DARK;
-				g = b = r;	
-			} else {
+                if (r_bit && i) r = LIGHT;
+                else if (r_bit) r = BLACK;
+                else if (i) r = MAXVAL;
+                else r = DARK;
+                g = b = r;
+            } else {
                 /* It's one of the twelve colored colors */
                 if (!i) {
                     /* Low intensity */
-                    r = r_bit * LIGHT; 
+                    r = r_bit * LIGHT;
                     g = g_bit * LIGHT;
                     b = b_bit * LIGHT;
                 } else {
@@ -237,21 +237,21 @@ main(argc, argv)
                 }
             }
             PPM_ASSIGN(xelrow[col], r, g, b);
-		}
-	  } else {
+        }
+      } else {
           int col;
-          for (col = 0; col < cols; col++) 
+          for (col = 0; col < cols; col++)
               PNM_ASSIGN1(xelrow[col], bitrow[0][col]);
       }
-	  while (linerep--) {
-		pnm_writepnmrow( stdout, xelrow, cols, MAXVAL, type, 0 );
-		++row;
-	  }
-	}
+      while (linerep--) {
+        pnm_writepnmrow( stdout, xelrow, cols, MAXVAL, type, 0 );
+        ++row;
+      }
+    }
     pnm_freerow(xelrow);
-	pm_close( f );
-	pm_close( stdout );
-	exit(0);
+    pm_close( f );
+    pm_close( stdout );
+    exit(0);
 }
 
 
@@ -303,3 +303,5 @@ getinit (file, colsP, rowsP, padrightP, patlenP, planesP)
     }
 }
 
+
+
diff --git a/converter/other/giftopnm.c b/converter/other/giftopnm.c
index 76cf4bff..e2d5923d 100644
--- a/converter/other/giftopnm.c
+++ b/converter/other/giftopnm.c
@@ -9,7 +9,7 @@
 /* +-------------------------------------------------------------------+ */
 
 /* There is a copy of the GIF89 specification, as defined by its inventor,
-   Compuserve, in 1990 at: 
+   Compuserve, in 1990 at:
    http://www.w3.org/Graphics/GIF/spec-gif89a.txt
 
    This covers the high level format, but does not cover how the "data"
@@ -19,10 +19,12 @@
    describe the Lempel-Ziv base.
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE   /* for strcaseeq */
 #include <string.h>
 #include <assert.h>
+#include <stdbool.h>
 
 #include "pm_config.h"
 #include "pm_c_util.h"
@@ -41,13 +43,13 @@
 #define MAX_LZW_BITS  12
 
 #ifndef   FASTPBMRENDER
-  #define FASTPBMRENDER TRUE
+  #define FASTPBMRENDER true
 #endif
 
 static const bool useFastPbmRender = FASTPBMRENDER;
 
 #ifndef   REPORTLZWCODES
-  #define REPORTLZWCODES FALSE
+  #define REPORTLZWCODES false
 #endif
 
 static const bool wantLzwCodes = REPORTLZWCODES;
@@ -60,7 +62,11 @@ readFile(FILE *          const ifP,
          unsigned char * const buffer,
          size_t          const len,
          const char **   const errorP) {
+/*----------------------------------------------------------------------------
+   Read the next 'len' bytes from *ifP into 'buffer'.
 
+   Fail if there aren't that many bytes to read.
+-----------------------------------------------------------------------------*/
     size_t bytesRead;
 
     bytesRead = fread(buffer, 1, len, ifP);
@@ -91,7 +97,7 @@ struct CmdlineInfo {
     bool allImages;  /* He wants all the images */
     unsigned int imageNum;
         /* image number he wants from input, starting at 0.  Undefined
-           if allImages is TRUE
+           if allImages is true
         */
     const char * alphaFileName;
     unsigned int quitearly;
@@ -101,7 +107,7 @@ struct CmdlineInfo {
 
 
 static void
-parseCommandLine(int argc, char ** argv,
+parseCommandLine(int argc, const char ** argv,
                  struct CmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    Note that the file spec array we return is stored in the storage that
@@ -110,7 +116,7 @@ parseCommandLine(int argc, char ** argv,
     optEntry * option_def;
         /* Instructions to pm_optParseOptions3 on how to parse our options.
          */
-    
+
     optStruct3 opt;
 
     unsigned int alphaSpec, imageSpec;
@@ -119,9 +125,9 @@ parseCommandLine(int argc, char ** argv,
     unsigned int option_def_index;
 
     MALLOCARRAY_NOFAIL(option_def, 100);
-    
+
     option_def_index = 0;   /* incremented by OPTENTRY */
-    OPTENT3(0, "verbose",     OPT_FLAG, NULL, 
+    OPTENT3(0, "verbose",     OPT_FLAG, NULL,
             &cmdlineP->verbose,         0);
     OPTENT3(0, "comments",    OPT_FLAG, NULL,
             &cmdlineP->comments,        0);
@@ -131,24 +137,24 @@ parseCommandLine(int argc, char ** argv,
             &cmdlineP->repair,          0);
     OPTENT3(0, "image",       OPT_STRING, &image,
             &imageSpec,                 0);
-    OPTENT3(0, "alphaout",    OPT_STRING, &cmdlineP->alphaFileName, 
+    OPTENT3(0, "alphaout",    OPT_STRING, &cmdlineP->alphaFileName,
             &alphaSpec,                 0);
 
     opt.opt_table = option_def;
-    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
-    opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
+    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. */
 
     free(option_def);
 
     if (!imageSpec) {
         cmdlineP->imageNum = 0;
-        cmdlineP->allImages = FALSE;
+        cmdlineP->allImages = false;
     } else {
-        if (strcaseeq(image, "all")) { 
-            cmdlineP->allImages = TRUE;
+        if (strcaseeq(image, "all")) {
+            cmdlineP->allImages = true;
         } else {
             char * tailptr;
 
@@ -164,12 +170,12 @@ parseCommandLine(int argc, char ** argv,
                 pm_error("Invalid value for 'image' option.  You specified "
                          "zero.  The first image is 1.");
 
-            cmdlineP->allImages = FALSE;
+            cmdlineP->allImages = false;
             cmdlineP->imageNum = (unsigned int) imageNum - 1;
         }
     }
-    
-    if (argc-1 == 0) 
+
+    if (argc-1 == 0)
         cmdlineP->inputFilespec = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
@@ -177,7 +183,7 @@ parseCommandLine(int argc, char ** argv,
     else
         cmdlineP->inputFilespec = argv[1];
 
-    if (!alphaSpec) 
+    if (!alphaSpec)
         cmdlineP->alphaFileName = NULL;
 }
 
@@ -249,13 +255,13 @@ struct GifScreen {
         /* Aspect ratio of each pixel, times 64, minus 15.  (i.e. 1 => 1:4).
            But Zero means 1:1.
         */
-    bool     hasGray;  
+    bool     hasGray;
         /* Boolean: global colormap has at least one gray color
-           (not counting black and white) 
+           (not counting black and white)
         */
     bool     hasColor;
         /* Boolean: global colormap has at least one non-gray,
-           non-black, non-white color 
+           non-black, non-white color
         */
 };
 
@@ -263,7 +269,7 @@ struct Gif89 {
     bool         haveTransColor;
         /* The GIF specifies a transparent background color */
     unsigned int transparentIndex;
-        /* The color index of the color which is the transparent 
+        /* The color index of the color which is the transparent
            background color.
 
            Meaningful only when 'haveTransColor' is true
@@ -282,7 +288,7 @@ initGif89(struct Gif89 * const gif89P) {
     gif89P->haveDelayTime  = false;
     gif89P->haveInputFlag  = false;
     gif89P->haveDisposal   = false;
-}       
+}
 
 
 static bool verbose;
@@ -311,8 +317,8 @@ readColorMap(FILE *        const ifP,
 
     assert(cmapSize <= MAXCOLORMAPSIZE);
 
-    *hasGrayP = FALSE;  /* initial assumption */
-    *hasColorP = FALSE;  /* initial assumption */
+    *hasGrayP = false;  /* initial assumption */
+    *hasColorP = false;  /* initial assumption */
 
     for (i = 0; i < cmapSize; ++i) {
         const char * error;
@@ -326,23 +332,23 @@ readColorMap(FILE *        const ifP,
 
         if (rgb[0] == rgb[1] && rgb[1] == rgb[2]) {
             if (rgb[0] != 0 && rgb[0] != GIFMAXVAL)
-                *hasGrayP = TRUE;
+                *hasGrayP = true;
         } else
-            *hasColorP = TRUE;
+            *hasColorP = true;
     }
     cmapP->size = cmapSize;
 }
 
 
 
-static bool zeroDataBlock = FALSE;
+static bool zeroDataBlock = false;
     /* the most recently read DataBlock was an EOD marker, i.e. had
        zero length
     */
 
 static void
-getDataBlock(FILE *          const ifP, 
-             unsigned char * const buf, 
+getDataBlock(FILE *          const ifP,
+             unsigned char * const buf,
              bool *          const eofP,
              unsigned int *  const lengthP,
              const char **   const errorP) {
@@ -354,10 +360,10 @@ getDataBlock(FILE *          const ifP,
    of the datablock at 'buf', and its length as *lengthP.
 
    Except that if we hit EOF or have an I/O error reading the first
-   byte (size field) of the DataBlock, we return *eofP == TRUE and
+   byte (size field) of the DataBlock, we return *eofP == true and
    *lengthP == 0.
 
-   We return *eofP == FALSE if we don't hit EOF or have an I/O error.
+   We return *eofP == false if we don't hit EOF or have an I/O error.
 
    If we hit EOF or have an I/O error reading the data portion of the
    DataBlock, we exit the program with pm_error().
@@ -366,7 +372,7 @@ getDataBlock(FILE *          const ifP,
 
     unsigned char count;
     const char * error;
-    
+
     readFile(ifP, &count, sizeof(count), &error);
 
     if (error) {
@@ -374,22 +380,22 @@ getDataBlock(FILE *          const ifP,
                    error);
         pm_strfree(error);
         *errorP = NULL;
-        *eofP = TRUE;
+        *eofP = true;
         *lengthP = 0;
     } else {
         if (verbose)
             pm_message("%d byte block at Position %ld", count, pos);
-        *eofP = FALSE;
+        *eofP = false;
         *lengthP = count;
 
         if (count == 0) {
             *errorP = NULL;
-            zeroDataBlock = TRUE;
+            zeroDataBlock = true;
         } else {
             const char * error;
 
-            zeroDataBlock = FALSE;
-            readFile(ifP, buf, count, &error); 
+            zeroDataBlock = false;
+            readFile(ifP, buf, count, &error);
 
             if (error) {
                 pm_asprintf(errorP,
@@ -416,7 +422,7 @@ readThroughEod(FILE * const ifP) {
     unsigned char buf[256];
     bool eod;
 
-    eod = FALSE;  /* initial value */
+    eod = false;  /* initial value */
     while (!eod) {
         bool eof;
         unsigned int count;
@@ -429,7 +435,7 @@ readThroughEod(FILE * const ifP) {
                        "anyway as if an EOD marker were at the end "
                        "of the file.");
         if (error || eof || count == 0)
-            eod = TRUE;
+            eod = true;
     }
 }
 
@@ -440,7 +446,7 @@ doPlainTextExtension(FILE * const ifP) {
 #if 0
     /* incomplete code fragment, attempt to handle Plain Text Extension */
     GetDataBlock(ifP, (unsigned char*) buf, &eof, &length);
-    
+
     lpos   = LM_to_uint(buf[0], buf[1]);
     tpos   = LM_to_uint(buf[2], buf[3]);
     width  = LM_to_uint(buf[4], buf[5]);
@@ -461,25 +467,25 @@ doCommentExtension(FILE * const ifP) {
 /*----------------------------------------------------------------------------
    Read the rest of a comment extension from the input file 'ifP' and handle
    it.
-   
+
    We ought to deal with the possibility that the comment is not text.  I.e.
    it could have nonprintable characters or embedded nulls.  I don't know if
    the GIF spec requires regular text or not.
 -----------------------------------------------------------------------------*/
     char buf[256];
-    unsigned int blocklen;  
+    unsigned int blocklen;
     bool done;
 
-    done = FALSE;
+    done = false;
     while (!done) {
         bool eof;
         const char * error;
-        getDataBlock(ifP, (unsigned char*) buf, &eof, &blocklen, &error); 
+        getDataBlock(ifP, (unsigned char*) buf, &eof, &blocklen, &error);
         if (error)
             pm_error("Error reading a data block in a comment extension.  %s",
                      error);
         if (blocklen == 0 || eof)
-            done = TRUE;
+            done = true;
         else {
             buf[blocklen] = '\0';
             if (showComment) {
@@ -500,7 +506,7 @@ LM_to_uint(unsigned char const a,
 
 
 
-static void 
+static void
 doGraphicControlExtension(FILE *         const ifP,
                           struct Gif89 * const gif89P) {
 
@@ -515,7 +521,7 @@ doGraphicControlExtension(FILE *         const ifP,
     if (eof)
         pm_error("EOF/error encountered reading "
                  "1st DataBlock of Graphic Control Extension.");
-    else if (length < 4) 
+    else if (length < 4)
         pm_error("graphic control extension 1st DataBlock too short.  "
                  "It must be at least 4 bytes; it is %d bytes.",
                  length);
@@ -542,7 +548,7 @@ doExtension(FILE *         const ifP,
             struct Gif89 * const gif89P) {
 
     const char * str;
-    
+
     switch (label) {
     case 0x01:              /* Plain Text Extension */
         str = "Plain Text";
@@ -576,7 +582,7 @@ doExtension(FILE *         const ifP,
 
 struct GetCodeState {
     unsigned char buf[280];
-        /* This is the buffer through which we read the data from the 
+        /* This is the buffer through which we read the data from the
            stream.  We must buffer it because we have to read whole data
            blocks at a time, but our client wants one code at a time.
            The buffer typically contains the contents of one data block
@@ -599,7 +605,7 @@ struct GetCodeState {
 
 
 static void
-getAnotherBlock(FILE *                const ifP, 
+getAnotherBlock(FILE *                const ifP,
                 struct GetCodeState * const gsP,
                 const char **         const errorP) {
 
@@ -617,11 +623,11 @@ getAnotherBlock(FILE *                const ifP,
 
     gsP->curbit -= (gsP->bufCount-2)*8;
     gsP->bufCount = 2;
-        
+
     /* Add the next block to the buffer */
     getDataBlock(ifP, &gsP->buf[gsP->bufCount], &eof, &count, errorP);
     if (*errorP)
-        gsP->streamExhausted = TRUE;
+        gsP->streamExhausted = true;
     else {
         if (eof) {
             pm_message("EOF encountered in image "
@@ -634,7 +640,7 @@ getAnotherBlock(FILE *                const ifP,
             assumedCount = count;
 
         gsP->streamExhausted = (assumedCount == 0);
-        
+
         gsP->bufCount += assumedCount;
     }
 }
@@ -645,13 +651,13 @@ static struct GetCodeState getCodeState;
 
 static void
 getCode_init(struct GetCodeState * const getCodeStateP) {
-    
+
     /* Fake a previous data block */
     getCodeStateP->buf[0] = 0;
     getCodeStateP->buf[1] = 0;
     getCodeStateP->bufCount = 2;
     getCodeStateP->curbit = getCodeStateP->bufCount * 8;
-    getCodeStateP->streamExhausted = FALSE;
+    getCodeStateP->streamExhausted = false;
 }
 
 
@@ -669,7 +675,7 @@ bitsOfLeBuffer(const unsigned char * const buf,
    first byte of buf[].
 
    We return the string as an integer such that its pure binary encoding with
-   the bits numbered Intel-style is the string.  E.g. the string 0,1,1 
+   the bits numbered Intel-style is the string.  E.g. the string 0,1,1
    yields six.
 -----------------------------------------------------------------------------*/
     uint32_t codeBlock;
@@ -688,8 +694,8 @@ bitsOfLeBuffer(const unsigned char * const buf,
             (buf[start/8+0] <<  0) |
             (buf[start/8+1] <<  8) |
             (buf[start/8+2] << 16);
-            
-    return (unsigned int) 
+
+    return (unsigned int)
         (codeBlock >> (start % 8)) & ((1 << len) - 1);
 }
 
@@ -697,7 +703,7 @@ bitsOfLeBuffer(const unsigned char * const buf,
 
 static void
 getCode_get(struct GetCodeState * const gsP,
-            FILE *                const ifP, 
+            FILE *                const ifP,
             int                   const codeSize,
             bool *                const eofP,
             unsigned int *        const codeP,
@@ -707,21 +713,21 @@ getCode_get(struct GetCodeState * const gsP,
 
   'codeSize' is the number of bits in the code we are to get.
 
-  Return *eofP == TRUE iff we hit the end of the stream.  That means a legal
+  Return *eofP == true iff we hit the end of the stream.  That means a legal
   end of stream, marked by an EOD marker, not just end of file.  An end of
   file in the middle of the GIF stream is an error.
 
   If there are bits left in the stream, but not 'codeSize' of them, we
-  call that a success with *eofP == TRUE.
+  call that a success with *eofP == true.
 
-  Return the code read (assuming *eofP == FALSE and *errorP == NULL)
+  Return the code read (assuming *eofP == false and *errorP == NULL)
   as *codeP.
 -----------------------------------------------------------------------------*/
 
     *errorP = NULL;
 
     while (gsP->curbit + codeSize > gsP->bufCount * 8 &&
-           !gsP->streamExhausted && !*errorP) 
+           !gsP->streamExhausted && !*errorP)
         /* Not enough left in buffer to satisfy request.  Get the next
            data block into the buffer.
 
@@ -736,7 +742,7 @@ getCode_get(struct GetCodeState * const gsP,
             /* The buffer still doesn't have enough bits in it; that means
                there were no data blocks left to read.
             */
-            *eofP = TRUE;
+            *eofP = true;
 
             {
                 int const bitsUnused = gsP->bufCount * 8 - gsP->curbit;
@@ -753,7 +759,7 @@ getCode_get(struct GetCodeState * const gsP,
                 pm_message("LZW code=0x%03x [%d]", *codeP, codeSize);
 
             gsP->curbit += codeSize;
-            *eofP = FALSE;
+            *eofP = false;
         }
     }
 }
@@ -769,7 +775,7 @@ struct Stack {
 
 
 
-static void 
+static void
 initStack(struct Stack * const stackP, unsigned int const size) {
 
     MALLOCARRAY(stackP->stack, size);
@@ -804,7 +810,7 @@ popStack(struct Stack * const stackP) {
 
     if (stackP->sp <= stackP->stack)
         pm_error("stack underflow");
-    
+
     return *(--stackP->sp);
 }
 
@@ -816,14 +822,14 @@ termStack(struct Stack * const stackP) {
     stackP->stack = NULL;
 }
 
-    
+
 
 /*----------------------------------------------------------------------------
    Some notes on LZW.
 
    LZW is an extension of Limpel-Ziv.  The two extensions are:
 
-     1) in Limpel-Ziv, codes are all the same number of bits.  In
+     1) In Limpel-Ziv, codes are all the same number of bits.  In
         LZW, they start out small and increase as the stream progresses.
 
      2) LZW has a clear code that resets the string table and code
@@ -833,33 +839,55 @@ termStack(struct Stack * const stackP) {
 
    The true data elements are dataWidth bits wide, so the maximum
    value of a true data element is 2**dataWidth-1.  We call that
-   max_dataVal.  The first byte in the stream tells you what dataWidth
+   maxDataVal.  The first byte in the stream tells you what dataWidth
    is.
 
-   LZW codes 0 - max_dataVal are direct codes.  Each one represents
+   LZW codes 0 - maxDataVal are direct codes.  Each one represents
    the true data element whose value is that of the LZW code itself.
    No decompression is required.
 
-   max_dataVal + 1 and up are compression codes.  They encode
+   maxDataVal + 1 and up are compression codes.  They encode
    true data elements:
 
-   max_dataVal + 1 is the clear code.
-         
-   max_dataVal + 2 is the end code.
+   maxDataVal + 1 is the clear code.
+
+   maxDataVal + 2 is the end code.
 
-   max_dataVal + 3 and up are string codes.  Each string code 
+   maxDataVal + 3 and up are string codes.  Each string code
    represents a string of true data elements.  The translation from a
    string code to the string of true data elements varies as the stream
    progresses.  In the beginning and after every clear code, the
    translation table is empty, so no string codes are valid.  As the
-   stream progresses, the table gets filled and more string codes 
+   stream progresses, the table gets filled and more string codes
    become valid.
 
+   At the beginning of the stream, string codes are represented by
+   dataWidth + 1 bits.  When enough codes have been defined to use up that
+   space, they start being represented by dataWidth + 2 bits, and so on.
+
+   What we call 'dataWidth', others call the "minimum code size," which is a
+   misnomer, because the minimum code size in a stream must be at least one
+   more than 'dataWidth', to accomodate the clear and end codes.
 -----------------------------------------------------------------------------*/
 
 static int const maxLzwCodeCt = (1<<MAX_LZW_BITS);
 
-struct Decompressor {
+typedef struct {
+/*----------------------------------------------------------------------------
+   An entry in the decompressor LZW code table.
+-----------------------------------------------------------------------------*/
+    unsigned int next;
+        /* The next code in the expansion after the one this entry specifies;
+           this is either another index into the LZW code table or a direct
+           code, which means it's the last data element in the expansion.
+        */
+    unsigned int dataElement;
+        /* The data element (color map index or gray level) to add to the
+           expansion for this entry.
+        */
+} CodeTableEntry;
+
+typedef struct {
     struct Stack stack;
     bool fresh;
         /* The stream is right after a clear code or at the very beginning */
@@ -869,7 +897,7 @@ struct Decompressor {
            the stream.
         */
     unsigned int maxCodeCt;
-        /* The maximum number of LZW codes that can be represented with the 
+        /* The maximum number of LZW codes that can be represented with the
            current code size.  (1 << codeSize)
         */
     unsigned int nextTableSlot;
@@ -882,6 +910,9 @@ struct Decompressor {
     /* The following are constant for the life of the decompressor */
     FILE * ifP;
     unsigned int initCodeSize;
+        /* The code size, in bits, at the start of the stream and immediately
+           after a Clear code.
+        */
     unsigned int cmapSize;
     unsigned int maxDataVal;
     unsigned int clearCode;
@@ -889,22 +920,28 @@ struct Decompressor {
     bool haveTransColor;
     unsigned int transparentIndex;
         /* meaningful only when 'haveTransColor' is true */
-    bool tolerateBadInput; 
+    bool tolerateBadInput;
         /* We are to tolerate bad input data as best we can, rather than
            just declaring an error and bailing out.
         */
-    unsigned int table[(1 << MAX_LZW_BITS)][2];   /* LZW code table */  
-};
+    CodeTableEntry table[(1 << MAX_LZW_BITS)];   /* LZW code table */
+        /* This contains the strings of expansions of LZW string codes, in
+           linked list form.  table[N] gives the first data element for the
+           string with string code N and the LZW code for the rest of the
+           string.  The latter is often a string code itself, which can also
+           be looked up in this table.
+        */
+} Decompressor;
 
 
 
 static void
-resetDecompressor(struct Decompressor * const decompP) {
+resetDecompressor(Decompressor * const decompP) {
 
-    decompP->codeSize      = decompP->initCodeSize+1;
+    decompP->codeSize      = decompP->initCodeSize;
     decompP->maxCodeCt     = 1 << decompP->codeSize;
     decompP->nextTableSlot = decompP->maxDataVal + 3;
-    decompP->fresh         = TRUE;
+    decompP->fresh         = true;
 }
 
 
@@ -918,7 +955,7 @@ validateTransparentIndex(unsigned int const transparentIndex,
     if (transparentIndex >= cmapSize) {
         if (tolerateBadInput) {
             if (transparentIndex > maxDataVal)
-                pm_error("Invalid transparent index value: %d",
+                pm_error("Invalid transparent index value: %u",
                          transparentIndex);
         } else {
             pm_error("Invalid transparent index value %d in image with "
@@ -935,23 +972,21 @@ validateTransparentIndex(unsigned int const transparentIndex,
 
 
 static void
-lzwInit(struct Decompressor * const decompP, 
-        FILE *                const ifP,
-        int                   const initCodeSize,
-        unsigned int          const cmapSize,
-        bool                  const haveTransColor,
-        unsigned int          const transparentIndex,
-        bool                  const tolerateBadInput) {
-
-    unsigned int const maxDataVal = (1 << initCodeSize) - 1;
-    
+lzwInit(Decompressor * const decompP,
+        FILE *         const ifP,
+        int            const dataWidth,
+        unsigned int   const cmapSize,
+        bool           const haveTransColor,
+        unsigned int   const transparentIndex,
+        bool           const tolerateBadInput) {
+
+    unsigned int const maxDataVal = (1 << dataWidth) - 1;
+
     if (verbose)
-        pm_message("Image says the initial compression code size is "
-                   "%d bits", 
-                   initCodeSize);
-    
+        pm_message("Image says the data width is %u bits", dataWidth);
+
     decompP->ifP              = ifP;
-    decompP->initCodeSize     = initCodeSize;
+    decompP->initCodeSize     = dataWidth + 1;
     decompP->cmapSize         = cmapSize;
     decompP->tolerateBadInput = tolerateBadInput;
     decompP->maxDataVal       = maxDataVal;
@@ -961,9 +996,9 @@ lzwInit(struct Decompressor * const decompP,
     if (verbose)
         pm_message("Initial code size is %u bits; clear code = 0x%03x, "
                    "end code = 0x%03x",
-                   decompP->initCodeSize, 
+                   decompP->initCodeSize,
                    decompP->clearCode, decompP->endCode);
-    
+
     /* The entries in the translation table for true data codes are
        constant throughout the image.  For PBM output we make an
        adjustment later.  Once set entries never change.
@@ -971,8 +1006,8 @@ lzwInit(struct Decompressor * const decompP,
     {
         unsigned int i;
         for (i = 0; i <= maxDataVal; ++i) {
-            decompP->table[i][0] = 0;
-            decompP->table[i][1] = i < cmapSize ? i : 0;
+            decompP->table[i].next = 0;
+            decompP->table[i].dataElement = i < cmapSize ? i : 0;
         }
     }
     decompP->haveTransColor   = haveTransColor;
@@ -985,19 +1020,19 @@ lzwInit(struct Decompressor * const decompP,
     resetDecompressor(decompP);
 
     getCode_init(&getCodeState);
-    
-    decompP->fresh = TRUE;
-    
+
+    decompP->fresh = true;
+
     initStack(&decompP->stack, maxLzwCodeCt);
 
-    assert(decompP->initCodeSize < sizeof(decompP->maxDataVal) * 8);
+    assert(decompP->initCodeSize < sizeof(decompP->maxDataVal) * 8 + 1);
 }
 
 
 
 static void
-lzwAdjustForPBM(struct Decompressor * const decompP,
-                GifColorMap           const cmap) {
+lzwAdjustForPBM(Decompressor * const decompP,
+                GifColorMap    const cmap) {
 /*----------------------------------------------------------------------------
   In the PBM case we use the table index value directly instead of looking up
   the color map for each pixel.
@@ -1008,13 +1043,14 @@ lzwAdjustForPBM(struct Decompressor * const decompP,
 ----------------------------------------------------------------------------*/
     unsigned int i;
     for (i = 0; i < cmap.size; ++i)
-        decompP->table[i][1] = cmap.map[i][0] == 0 ? PBM_BLACK : PBM_WHITE;
+        decompP->table[i].dataElement =
+            cmap.map[i][0] == 0 ? PBM_BLACK : PBM_WHITE;
 }
 
 
 
 static void
-lzwTerm(struct Decompressor * const decompP) {
+lzwTerm(Decompressor * const decompP) {
 
     termStack(&decompP->stack);
 }
@@ -1022,8 +1058,8 @@ lzwTerm(struct Decompressor * const decompP) {
 
 
 static void
-pushWholeStringOnStack(struct Decompressor * const decompP,
-                       unsigned int          const code0) {
+pushWholeStringOnStack(Decompressor * const decompP,
+                       unsigned int   const code0) {
 /*----------------------------------------------------------------------------
   Get the whole string that compression code 'code0' represents and push
   it onto the code stack so the leftmost code is on top.  Set
@@ -1034,21 +1070,47 @@ pushWholeStringOnStack(struct Decompressor * const decompP,
 
     for (stringCount = 0, code = code0;
          code > decompP->maxDataVal;
-         ++stringCount, code = decompP->table[code][0]
+         ++stringCount, code = decompP->table[code].next
         ) {
 
-        pushStack(&decompP->stack, decompP->table[code][1]);
+        pushStack(&decompP->stack, decompP->table[code].dataElement);
     }
-    decompP->firstcode = decompP->table[code][1];
+    decompP->firstcode = decompP->table[code].dataElement;
     pushStack(&decompP->stack, decompP->firstcode);
 }
 
 
 
 static void
-expandCodeOntoStack(struct Decompressor * const decompP,
-                    unsigned int          const incode,
-                    const char **         const errorP) {
+addLzwStringCode(Decompressor * const decompP) {
+
+    if (decompP->nextTableSlot < maxLzwCodeCt) {
+        decompP->table[decompP->nextTableSlot].next =
+            decompP->prevcode;
+        decompP->table[decompP->nextTableSlot].dataElement =
+            decompP->firstcode;
+        ++decompP->nextTableSlot;
+        if (decompP->nextTableSlot >= decompP->maxCodeCt) {
+            /* We've used up all the codes of the current code size.
+               Future codes in the stream will have codes one bit longer.
+               But there's an exception if we're already at the LZW
+               maximum, in which case the codes will simply continue
+               the same size and no new ones will be defined.
+            */
+            if (decompP->codeSize < MAX_LZW_BITS) {
+                ++decompP->codeSize;
+                decompP->maxCodeCt = 1 << decompP->codeSize;
+            }
+        }
+    }
+}
+
+
+
+static void
+expandCodeOntoStack(Decompressor * const decompP,
+                    unsigned int   const incode,
+                    const char **  const errorP) {
 /*----------------------------------------------------------------------------
    'incode' is an LZW string code.  It represents a string of true data
    elements, as defined by the string translation table in *decompP.
@@ -1063,58 +1125,56 @@ expandCodeOntoStack(struct Decompressor * const decompP,
    as *errorP).
 -----------------------------------------------------------------------------*/
     unsigned int code;
+    const char * gifError;
 
-    *errorP = NULL; /* Initial value */
+    gifError = NULL; /* Initial value */
 
     if (incode <= decompP->maxDataVal) {
         if (incode < decompP->cmapSize)
             code = incode;      /* Direct index */
         else if (decompP->tolerateBadInput &&
                  decompP->haveTransColor &&
-                 decompP->table[incode][1] == decompP->transparentIndex)
+                 decompP->table[incode].dataElement ==
+                     decompP->transparentIndex)
             /* transparent code outside cmap   exceptional case */
             code = incode;
         else
-            pm_asprintf(errorP, "Error in GIF image: invalid color code %u. "
+            pm_asprintf(&gifError, "Invalid color code %u. "
                         "Valid color values are 0 - %u",
                         incode, decompP->cmapSize - 1);
-    }
-    else if (incode < decompP->nextTableSlot)  
+    } else if (incode < decompP->nextTableSlot)
         /* LZW string, defined */
         code = incode;
-    else if  (incode == decompP->nextTableSlot && !decompP->fresh) {
+    else if (incode == decompP->nextTableSlot) {
         /* It's a code that isn't in our translation table yet.
-           This does not happen with the decoder in a fresh state.
         */
-        if (wantLzwCodes && verbose)
-            pm_message ("LZW code valid, but not in decoder table");
+        if (decompP->fresh)
+            pm_asprintf(&gifError, "LZW string code encountered with "
+                        "decompressor in fresh state");
+        else {
+            if (wantLzwCodes && verbose)
+                pm_message ("LZW code valid, but not in decoder table");
 
-        pushStack(&decompP->stack, decompP->firstcode);
-        code = decompP->prevcode;
+            pushStack(&decompP->stack, decompP->firstcode);
+            code = decompP->prevcode;
+        }
     } else
-        pm_asprintf(errorP, "Error in GIF image: invalid LZW code");
-
-    if (!*errorP) {
+        pm_asprintf(&gifError, "LZW string code %u "
+                    "is neither a previously defined one nor the "
+                    "next in sequence to define (%u)",
+                    incode, decompP->nextTableSlot);
+
+    if (gifError) {
+        pm_asprintf(errorP, "INVALID GIF IMAGE: %s", gifError);
+        pm_strfree(gifError);
+    } else {
         pushWholeStringOnStack(decompP, code);
 
-        if (decompP->nextTableSlot < maxLzwCodeCt) {
-            decompP->table[decompP->nextTableSlot][0] = decompP->prevcode;
-            decompP->table[decompP->nextTableSlot][1] = decompP->firstcode;
-            ++decompP->nextTableSlot;
-            if (decompP->nextTableSlot >= decompP->maxCodeCt) {
-                /* We've used up all the codes of the current code size.
-                   Future codes in the stream will have codes one bit longer.
-                   But there's an exception if we're already at the LZW
-                   maximum, in which case the codes will simply continue
-                   the same size.
-                */
-                if (decompP->codeSize < MAX_LZW_BITS) {
-                    ++decompP->codeSize;
-                    decompP->maxCodeCt = 1 << decompP->codeSize;
-                }
-            }
-        }
+        addLzwStringCode(decompP);
+
         decompP->prevcode = incode;
+
+        *errorP = NULL;
     }
 }
 
@@ -1122,7 +1182,7 @@ expandCodeOntoStack(struct Decompressor * const decompP,
 
 static void
 lzwReadByteFresh(struct GetCodeState * const getCodeStateP,
-                 struct Decompressor * const decompP,
+                 Decompressor *        const decompP,
                  bool *                const endOfImageP,
                  unsigned char *       const dataReadP,
                  const char **         const errorP) {
@@ -1142,7 +1202,7 @@ lzwReadByteFresh(struct GetCodeState * const getCodeStateP,
 
     assert(decompP->fresh);  /* Entry requirement */
 
-    decompP->fresh = FALSE;
+    decompP->fresh = false;
 
     do {
         getCode_get(getCodeStateP, decompP->ifP, decompP->codeSize,
@@ -1151,12 +1211,12 @@ lzwReadByteFresh(struct GetCodeState * const getCodeStateP,
 
     if (!*errorP) {
         if (eof)
-            *endOfImageP = TRUE;
+            *endOfImageP = true;
         else if (code == decompP->endCode) {
             if (!zeroDataBlock)
                 readThroughEod(decompP->ifP);
-            *endOfImageP = TRUE;
-        } else if (code >= decompP->cmapSize) { 
+            *endOfImageP = true;
+        } else if (code >= decompP->cmapSize) {
             pm_asprintf(errorP, "Error in GIF image: invalid color code %u. "
                         "Valid color values are: 0 - %u",
                         code, decompP->cmapSize-1);
@@ -1165,12 +1225,12 @@ lzwReadByteFresh(struct GetCodeState * const getCodeStateP,
             */
             decompP->prevcode = decompP->firstcode = 0;
 
-            *endOfImageP = FALSE;
+            *endOfImageP = false;
         } else {    /* valid code */
             decompP->prevcode  = code;
-            decompP->firstcode = decompP->table[code][1];
+            decompP->firstcode = decompP->table[code].dataElement;
             *dataReadP = decompP->firstcode;
-            *endOfImageP = FALSE;
+            *endOfImageP = false;
         }
     }
 }
@@ -1179,10 +1239,10 @@ lzwReadByteFresh(struct GetCodeState * const getCodeStateP,
 
 
 static void
-lzwReadByte(struct Decompressor * const decompP,
-            unsigned char *       const dataReadP,
-            bool *                const endOfImageP,
-            const char **         const errorP) {
+lzwReadByte(Decompressor *  const decompP,
+            unsigned char * const dataReadP,
+            bool *          const endOfImageP,
+            const char **   const errorP) {
 /*----------------------------------------------------------------------------
   Return the next data element of the decompressed image.  In the context
   of a GIF, a data element is the color table index of one pixel.
@@ -1201,7 +1261,7 @@ lzwReadByte(struct Decompressor * const decompP,
 -----------------------------------------------------------------------------*/
     if (!stackIsEmpty(&decompP->stack)) {
         *errorP = NULL;
-        *endOfImageP = FALSE;
+        *endOfImageP = false;
         *dataReadP = popStack(&decompP->stack);
     } else if (decompP->fresh) {
         lzwReadByteFresh(&getCodeState, decompP, endOfImageP, dataReadP,
@@ -1224,10 +1284,10 @@ lzwReadByte(struct Decompressor * const decompP,
                     if (code == decompP->endCode) {
                         if (!zeroDataBlock)
                             readThroughEod(decompP->ifP);
-                        *endOfImageP = TRUE;
+                        *endOfImageP = true;
                         *errorP = NULL;
                     } else {
-                        *endOfImageP = FALSE;
+                        *endOfImageP = false;
                         expandCodeOntoStack(decompP, code, errorP);
                         if (!*errorP)
                             *dataReadP = popStack(&decompP->stack);
@@ -1255,7 +1315,7 @@ bumpRowInterlace(unsigned int   const rows,
        MULT4PLUS2: Rows 2, 6, 10, 14, etc.
        MULT2PLUS1: Rows 1, 3, 5, 7, 9, etc.
     */
-    
+
     switch (*passP) {
     case MULT8PLUS0:
         *rowP += 8;
@@ -1300,7 +1360,7 @@ bumpRowInterlace(unsigned int   const rows,
 static void
 renderRow(unsigned char *    const cmapIndexRow,
           unsigned int       const cols,
-          GifColorMap        const cmap, 
+          GifColorMap        const cmap,
           bool               const haveTransColor,
           unsigned int       const transparentIndex,
           FILE *             const imageOutfile,
@@ -1311,11 +1371,14 @@ renderRow(unsigned char *    const cmapIndexRow,
 /*----------------------------------------------------------------------------
   Convert one row of cmap indexes to PPM/PGM/PBM output.
 
+  The row is *xelrow, which is 'cols' columns wide and has pixels of format
+  'format'.
+
   Render the alpha row to *alphaFileP iff 'alphabits' is non-NULL.  If
   'haveTransColor' is false, render all white (i.e. the row is
   opaque).  'alphabits' is otherwise just a one-row buffer for us to use
   in rendering the alpha row.
-  
+
   imageOutfile is NULL if user wants only the alpha file.
 ----------------------------------------------------------------------------*/
     if (alphabits) {
@@ -1332,7 +1395,7 @@ renderRow(unsigned char *    const cmapIndexRow,
     if (imageOutfile) {
         if (useFastPbmRender && format == PBM_FORMAT && !haveTransColor) {
 
-            bit * const bitrow = cmapIndexRow; 
+            bit * const bitrow = cmapIndexRow;
 
             pbm_writepbmrow(imageOutfile, bitrow, cols, false);
         } else {
@@ -1386,7 +1449,7 @@ pnmFormat(bool const hasGray,
 -----------------------------------------------------------------------------*/
     int format;
     const char * formatName;
-           
+
     if (hasColor) {
         format = PPM_FORMAT;
         formatName = "PPM";
@@ -1397,21 +1460,21 @@ pnmFormat(bool const hasGray,
         format = PBM_FORMAT;
         formatName = "PBM";
     }
-    if (verbose) 
+    if (verbose)
         pm_message("writing a %s file", formatName);
- 
+
     return format;
 }
 
 
 
 static void
-makePnmRow(struct Decompressor * const decompP,
-           unsigned int          const cols,
-           unsigned int          const rows,
-           bool                  const fillWithZero,
-           unsigned char *       const cmapIndexRow,
-           const char **         const errorP) {
+makePnmRow(Decompressor * const decompP,
+           unsigned int   const cols,
+           unsigned int   const rows,
+           bool           const fillWithZero,
+           unsigned char *const cmapIndexRow,
+           const char **  const errorP) {
 
     bool fillingWithZero;
     unsigned int col;
@@ -1426,7 +1489,7 @@ makePnmRow(struct Decompressor * const decompP,
 
         if (fillingWithZero)
             colorIndex = 0;
-        else { 
+        else {
             const char *  readError;
             unsigned char readColorIndex;
             bool          endOfImage;
@@ -1455,15 +1518,15 @@ makePnmRow(struct Decompressor * const decompP,
 
 
 static void
-convertRaster(struct Decompressor * const decompP,
-              unsigned int          const cols,
-              unsigned int          const rows,
-              GifColorMap           const cmap, 
-              bool                  const interlace,
-              FILE *                const imageOutFileP,
-              FILE *                const alphaFileP,
-              bool                  const hasGray,
-              bool                  const hasColor) {
+convertRaster(Decompressor * const decompP,
+              unsigned int   const cols,
+              unsigned int   const rows,
+              GifColorMap    const cmap,
+              bool           const interlace,
+              FILE *         const imageOutFileP,
+              FILE *         const alphaFileP,
+              bool           const hasGray,
+              bool           const hasColor) {
 /*----------------------------------------------------------------------------
    Read the raster from the GIF decompressor *decompP, and write it as a
    complete PNM stream (starting with the header) on *imageOutFileP and
@@ -1492,11 +1555,11 @@ convertRaster(struct Decompressor * const decompP,
     MALLOCARRAY2(cmapIndexArray, interlace ? rows : 1 , cols);
 
     if (imageOutFileP)
-        pnm_writepnminit(imageOutFileP, cols, rows, GIFMAXVAL, format, FALSE);
+        pnm_writepnminit(imageOutFileP, cols, rows, GIFMAXVAL, format, false);
     if (alphaFileP)
-        pbm_writepbminit(alphaFileP, cols, rows, FALSE);
+        pbm_writepbminit(alphaFileP, cols, rows, false);
 
-    xelrow = pnm_allocrow(cols);  
+    xelrow = pnm_allocrow(cols);
     if (!xelrow)
         pm_error("couldn't alloc space for image" );
 
@@ -1540,10 +1603,10 @@ convertRaster(struct Decompressor * const decompP,
                       decompP->haveTransColor, decompP->transparentIndex,
                       imageOutFileP, format, xelrow, alphaFileP, alphabits);
     }
-    /* All rows decompressed (and rendered and output if non-interlaced) */  
+    /* All rows decompressed (and rendered and output if non-interlaced) */
     if (interlace) {
         unsigned int row;
-        for (row = 0; row < rows; ++row) 
+        for (row = 0; row < rows; ++row)
             renderRow(cmapIndexArray[row], cols, cmap,
                       decompP->haveTransColor, decompP->transparentIndex,
                       imageOutFileP, format, xelrow, alphaFileP, alphabits);
@@ -1558,13 +1621,13 @@ convertRaster(struct Decompressor * const decompP,
 
 
 static void
-skipExtraneousData(struct Decompressor * const decompP) {
+skipExtraneousData(Decompressor * const decompP) {
 
     unsigned char byteRead;
     bool endOfImage;
     const char * error;
 
-    endOfImage = FALSE;  /* initial value */
+    endOfImage = false;  /* initial value */
 
     lzwReadByte(decompP, &byteRead, &endOfImage, &error);
 
@@ -1591,13 +1654,13 @@ skipExtraneousData(struct Decompressor * const decompP) {
 
 static void
 issueTransparencyMessage(bool         const haveTransColor,
-                         unsigned int const transparentIndex, 
+                         unsigned int const transparentIndex,
                          GifColorMap  const cmap) {
 /*----------------------------------------------------------------------------
    If user wants verbose output, tell him whether there is a transparent
    background color ('haveTransColor') and if so what it is
    ('transparentIndex').
-   
+
    Some GIFs put transparentIndex outside the color map.  Allow this only
    with "-repair", checked in lzwInit().  Here we issue a warning and report
    the substitute color.
@@ -1632,10 +1695,10 @@ issueTransparencyMessage(bool         const haveTransColor,
 
 
 static void
-readImageData(FILE *       const ifP, 
+readImageData(FILE *       const ifP,
               unsigned int const cols,
               unsigned int const rows,
-              GifColorMap  const cmap, 
+              GifColorMap  const cmap,
               bool         const interlace,
               bool         const haveTransColor,
               unsigned int const transparentIndex,
@@ -1645,22 +1708,25 @@ readImageData(FILE *       const ifP,
               bool         const hasColor,
               bool         const tolerateBadInput) {
 
-    unsigned char lzwMinCodeSize;      
-    struct Decompressor decomp;
+    unsigned char lzwDataWidth;
+    Decompressor decomp;
     const char * error;
 
-    readFile(ifP, &lzwMinCodeSize, sizeof(lzwMinCodeSize), &error);
+    readFile(ifP, &lzwDataWidth, sizeof(lzwDataWidth), &error);
     if (error)
         pm_error("Can't read GIF stream "
                  "right after an image separator; no "
                  "image data follows.  %s", error);
 
-    if (lzwMinCodeSize > MAX_LZW_BITS)
-        pm_error("Invalid minimum code size value in image data: %u.  "
-                 "Maximum allowable code size in GIF is %u", 
-                 lzwMinCodeSize, MAX_LZW_BITS);
+    if (lzwDataWidth+1 > MAX_LZW_BITS)
+        pm_error("Invalid data width (bits for a true data item) "
+                 "in image data: %u.  "
+                 "Maximum allowable code size in GIF is %u, "
+                 "and a code has to be wide enough to accomodate both "
+                 "all possible data values and two control codes",
+                 lzwDataWidth, MAX_LZW_BITS);
 
-    lzwInit(&decomp, ifP, lzwMinCodeSize, cmap.size,
+    lzwInit(&decomp, ifP, lzwDataWidth, cmap.size,
             haveTransColor, transparentIndex, tolerateBadInput);
 
     issueTransparencyMessage(haveTransColor, transparentIndex, cmap);
@@ -1721,24 +1787,24 @@ readGifHeader(FILE *             const gifFileP,
     readFile(gifFileP, buf, 6, &error);
     if (error)
         pm_error("Error reading magic number.  %s", error);
-    
+
     if (!strneq((char *)buf, "GIF", 3))
         pm_error("File does not contain a GIF stream.  It does not start "
                  "with 'GIF'.");
-    
+
     strncpy(version, (char *)buf + 3, 3);
     version[3] = '\0';
-    
+
     if (verbose)
         pm_message("GIF format version is '%s'", version);
-    
+
     if ((!streq(version, "87a")) && (!streq(version, "89a")))
         pm_error("Bad version number, not '87a' or '89a'" );
-    
+
     readFile(gifFileP, buf, 7, &error);
     if (error)
         pm_error("Failed to read screen descriptor.  %s", error);
-    
+
     gifScreenP->width           = LM_to_uint(buf[0],buf[1]);
     gifScreenP->height          = LM_to_uint(buf[2],buf[3]);
     cmapSize                    = 1 << ((buf[4] & 0x07) + 1);
@@ -1749,25 +1815,25 @@ readGifHeader(FILE *             const gifFileP,
     if (verbose) {
         pm_message("GIF Width = %u GIF Height = %u "
                    "Pixel aspect ratio = %u (%f:1)",
-                   gifScreenP->width, gifScreenP->height, 
-                   gifScreenP->aspectRatio, 
-                   gifScreenP->aspectRatio == 0 ? 
+                   gifScreenP->width, gifScreenP->height,
+                   gifScreenP->aspectRatio,
+                   gifScreenP->aspectRatio == 0 ?
                    1 : (gifScreenP->aspectRatio + 15) / 64.0);
         pm_message("Global color count = %u   Color Resolution = %u",
                    cmapSize, gifScreenP->colorResolution);
-    }           
+    }
     if (buf[4] & GLOBALCOLORMAP) {
         gifScreenP->hasGlobalColorMap = true;
         readColorMap(gifFileP, cmapSize, &gifScreenP->colorMap,
                      &gifScreenP->hasGray, &gifScreenP->hasColor);
         if (verbose) {
-            pm_message("Global color map %s grays, %s colors", 
+            pm_message("Global color map %s grays, %s colors",
                        gifScreenP->hasGray ? "contains" : "doesn't contain",
                        gifScreenP->hasColor ? "contains" : "doesn't contain");
         }
     } else
         gifScreenP->hasGlobalColorMap = false;
-    
+
     if (gifScreenP->aspectRatio != 0 && gifScreenP->aspectRatio != 49)
         warnUserNotSquare(gifScreenP->aspectRatio);
 
@@ -1777,7 +1843,7 @@ readGifHeader(FILE *             const gifFileP,
 
 
 static void
-readExtensions(FILE*          const ifP, 
+readExtensions(FILE*          const ifP,
                struct Gif89 * const gif89P,
                bool *         const eodP,
                const char **  const errorP) {
@@ -1786,8 +1852,8 @@ readExtensions(FILE*          const ifP,
    positioned.  Read up through the image separator that begins the
    next image or GIF stream terminator.
 
-   If we encounter EOD (end of GIF stream) before we find an image 
-   separator, we return *eodP == TRUE.  Else *eodP == FALSE.
+   If we encounter EOD (end of GIF stream) before we find an image
+   separator, we return *eodP == true.  Else *eodP == false.
 
    If we hit end of file before an EOD marker, we fail.
 -----------------------------------------------------------------------------*/
@@ -1796,8 +1862,8 @@ readExtensions(FILE*          const ifP,
 
     *errorP = NULL;  /* initial value */
 
-    eod = FALSE;
-    imageStart = FALSE;
+    eod = false;
+    imageStart = false;
 
     /* Read the image descriptor */
     while (!imageStart && !eod && !*errorP) {
@@ -1813,7 +1879,7 @@ readExtensions(FILE*          const ifP,
             pm_strfree(error);
         } else {
             if (c == ';') {         /* GIF terminator */
-                eod = TRUE;
+                eod = true;
             } else if (c == '!') {         /* Extension */
                 unsigned char functionCode;
                 const char * error;
@@ -1828,9 +1894,9 @@ readExtensions(FILE*          const ifP,
                 } else {
                     doExtension(ifP, functionCode, gif89P);
                 }
-            } else if (c == ',') 
-                imageStart = TRUE;
-            else 
+            } else if (c == ',')
+                imageStart = true;
+            else
                 pm_message("Encountered invalid character 0x%02x while "
                            "seeking extension block, ignoring", (int)c);
         }
@@ -1846,7 +1912,7 @@ struct GifImageHeader {
 -----------------------------------------------------------------------------*/
     bool hasLocalColormap;
         /* The image has its own color map.  Its size is 'localColorMapSize' */
-        /* (If an image does not have its own color map, the image uses the 
+        /* (If an image does not have its own color map, the image uses the
            global color map for the GIF stream)
         */
     unsigned int localColorMapSize;
@@ -1875,7 +1941,7 @@ reportImageHeader(struct GifImageHeader const imageHeader) {
     if (imageHeader.lpos > 0 || imageHeader.tpos > 0)
         pm_message("  Image left position: %u top position: %u",
                    imageHeader.lpos, imageHeader.tpos);
-    
+
     if (imageHeader.hasLocalColormap)
         pm_message("  Uses local colormap of %u colors",
                    imageHeader.localColorMapSize);
@@ -1921,7 +1987,7 @@ validateWithinGlobalScreen(struct GifImageHeader const imageHeader,
                            struct GifScreen      const gifScreen) {
 
     unsigned long int const rpos = imageHeader.lpos + imageHeader.cols;
-    unsigned long int const bpos = imageHeader.tpos + imageHeader.rows; 
+    unsigned long int const bpos = imageHeader.tpos + imageHeader.rows;
 
     if (rpos > gifScreen.width)
         pm_error("Image right end (%lu) is outside global screen: %u x %u",
@@ -1950,17 +2016,17 @@ skipImageData(FILE * const ifP) {
 
 
 static void
-convertImage(FILE *           const ifP, 
-             bool             const skipIt, 
-             FILE *           const imageoutFileP, 
-             FILE *           const alphafileP, 
+convertImage(FILE *           const ifP,
+             bool             const skipIt,
+             FILE *           const imageoutFileP,
+             FILE *           const alphafileP,
              struct GifScreen const gifScreen,
              struct Gif89     const gif89,
              bool             const tolerateBadInput) {
 /*----------------------------------------------------------------------------
    Read a single GIF image from the current position of file 'ifP'.
 
-   If 'skipIt' is TRUE, don't do anything else.  Otherwise, write the
+   If 'skipIt' is true, don't do anything else.  Otherwise, write the
    image to the current position of files *imageoutFileP and *alphafileP.
    If *alphafileP is NULL, though, don't write any alpha information.
 -----------------------------------------------------------------------------*/
@@ -1974,7 +2040,7 @@ convertImage(FILE *           const ifP,
     validateWithinGlobalScreen(imageHeader, gifScreen);
 
     if (imageHeader.hasLocalColormap) {
-        readColorMap(ifP, imageHeader.localColorMapSize, &localColorMap, 
+        readColorMap(ifP, imageHeader.localColorMapSize, &localColorMap,
                      &hasGray, &hasColor);
         currentColorMapP = &localColorMap;
     } else if (gifScreen.hasGlobalColorMap) {
@@ -2015,18 +2081,18 @@ disposeOfReadExtensionsError(const char * const error,
             pm_error("Error accessing Image %u of stream.  %s",
                      imageSeq, error);
         pm_strfree(error);
-        *eodP = TRUE;
+        *eodP = true;
     }
 }
 
 
 
 static void
-convertImages(FILE *       const ifP, 
+convertImages(FILE *       const ifP,
               bool         const allImages,
-              unsigned int const requestedImageSeq, 
+              unsigned int const requestedImageSeq,
               bool         const drainStream,
-              FILE *       const imageOutFileP, 
+              FILE *       const imageOutFileP,
               FILE *       const alphaFileP,
               bool         const tolerateBadInput) {
 /*----------------------------------------------------------------------------
@@ -2034,9 +2100,9 @@ convertImages(FILE *       const ifP,
    it as PNM images to file 'imageOutFileP'.  If the images have transparency
    and 'alphafile' is non-NULL, write PGM alpha masks to file 'alphaFileP'.
 
-   'allImages' means Caller wants all the images in the stream.  
+   'allImages' means Caller wants all the images in the stream.
 
-   'requestedImageSeq' is meaningful only when 'allImages' is FALSE.  It 
+   'requestedImageSeq' is meaningful only when 'allImages' is false.  It
    is the sequence number of the one image Caller wants from the stream,
    with the first image being 0.
 
@@ -2062,7 +2128,7 @@ convertImages(FILE *       const ifP,
 
     readGifHeader(ifP, &gifScreen);
 
-    for (imageSeq = 0, eod = FALSE;
+    for (imageSeq = 0, eod = false;
          !eod && (allImages || imageSeq <= requestedImageSeq || drainStream);
          ++imageSeq) {
 
@@ -2083,7 +2149,7 @@ convertImages(FILE *       const ifP,
             if (verbose)
                 pm_message("Reading Image Sequence %u", imageSeq);
 
-            convertImage(ifP, !allImages && (imageSeq != requestedImageSeq), 
+            convertImage(ifP, !allImages && (imageSeq != requestedImageSeq),
                          imageOutFileP, alphaFileP, gifScreen, gif89,
                          tolerateBadInput);
         }
@@ -2093,19 +2159,19 @@ convertImages(FILE *       const ifP,
 
 
 int
-main(int argc, char **argv) {
+main(int argc, const char **argv) {
 
     struct CmdlineInfo cmdline;
     FILE * ifP;
     FILE * alphaFileP;
     FILE * imageOutFileP;
 
-    pnm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
     verbose = cmdline.verbose;
     showComment = cmdline.comments;
-   
+
     ifP = pm_openr(cmdline.inputFilespec);
 
     if (cmdline.alphaFileName == NULL)
@@ -2118,12 +2184,12 @@ main(int argc, char **argv) {
     else
         imageOutFileP = stdout;
 
-    convertImages(ifP, cmdline.allImages, cmdline.imageNum, 
+    convertImages(ifP, cmdline.allImages, cmdline.imageNum,
                   !cmdline.quitearly, imageOutFileP, alphaFileP,
                   cmdline.repair);
 
     pm_close(ifP);
-    if (imageOutFileP != NULL) 
+    if (imageOutFileP != NULL)
         pm_close(imageOutFileP);
     if (alphaFileP != NULL)
         pm_close(alphaFileP);
@@ -2132,4 +2198,3 @@ main(int argc, char **argv) {
 }
 
 
-
diff --git a/converter/other/ipdb.c b/converter/other/ipdb.c
index 7ee37872..5e4dc82e 100644
--- a/converter/other/ipdb.c
+++ b/converter/other/ipdb.c
@@ -19,6 +19,7 @@
  *   Authors:  Eric A. Howe (mu@trends.net)
  *             Bryan Henderson, 2010
  */
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE   /* Ensure strdup() is in <string.h> */
 #include <assert.h>
@@ -282,8 +283,8 @@ ipdb_image_alloc(const char * const name,
             if (w != 0 && h != 0) {
                 MALLOCARRAY(imgP->data, w * h);
 
-                if (imgP->data) {
-                    memset(imgP->data, 0, sizeof(*(imgP->data)) * w * h);
+                if (imgP->data != NULL) {
+                  memset(imgP->data, 0, sizeof(*(imgP->data)) * w * h);
                 } else
                     failed = true;
             }
diff --git a/converter/other/jbig/jbigtopnm.c b/converter/other/jbig/jbigtopnm.c
index 733ba227..0d55ccc1 100644
--- a/converter/other/jbig/jbigtopnm.c
+++ b/converter/other/jbig/jbigtopnm.c
@@ -1,22 +1,93 @@
 /*
     jbigtopnm - JBIG to PNM converter
-  
+
     This program was derived from jbgtopbm.c in Markus Kuhn's
     JBIG-KIT package by Bryan Henderson on 2000.05.11
 
     The main difference is that this version uses the Netpbm libraries.
-  
+
  */
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <limits.h>
+
 #include <jbig.h>
+
 #include "pnm.h"
+#include "shhopt.h"
+#include "mallocvar.h"
 
 #define BUFSIZE 8192
 
 
+
+typedef struct {
+    /* All the information the user supplied in the command line,
+       in a form easy for the program to use.
+    */
+    const char * inputFileName;
+    const char * outputFileName;
+    unsigned long xmax;
+    unsigned long ymax;
+    unsigned int binary;
+    unsigned int diagnose;
+    unsigned int planeSpec;
+    unsigned int plane;
+} CmdlineInfo;
+
+
+
+static void
+parseCommandLine(int                 argc,
+                 const char ** const argv,
+                 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;
+
+    optStruct3 opt;
+
+    unsigned int xmaxSpec, ymaxSpec;
+
+    unsigned int option_def_index;
+
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
+    option_def_index = 0;   /* incremented by OPTENT3 */
+    OPTENT3(0, "binary",   OPT_FLAG, NULL,             &cmdlineP->binary,   0);
+    OPTENT3(0, "diagnose", OPT_FLAG, NULL,             &cmdlineP->diagnose, 0);
+    OPTENT3(0, "plane",    OPT_UINT, &cmdlineP->plane, &cmdlineP->planeSpec,0);
+    OPTENT3(0, "xmax",     OPT_UINT, &cmdlineP->xmax,  &xmaxSpec,           0);
+    OPTENT3(0, "ymax",     OPT_UINT, &cmdlineP->ymax,  &ymaxSpec,           0);
+
+    opt.opt_table = option_def;
+    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, (char**)argv, opt, sizeof(opt), 0);
+    /* Uses and sets argc, argv, and some of *cmdlineP and others. */
+
+    if (!xmaxSpec)
+        cmdlineP->xmax = UINT_MAX;
+    if (!ymaxSpec)
+        cmdlineP->ymax = UINT_MAX;
+
+    cmdlineP->inputFileName  = (argc-1 >= 1) ? argv[1] : "-";
+    cmdlineP->outputFileName = (argc-1 >= 2) ? argv[2] : "-";
+
+    if (argc-1 > 2)
+        pm_error("Too  many arguments: %u.  The only possible "
+                 "non-option arguments are input file name and "
+                 "output file name", argc-1);
+}
+
+
+
 static void
 collect_image (unsigned char *data, size_t len, void *image) {
     static int cursor = 0;
@@ -29,7 +100,7 @@ collect_image (unsigned char *data, size_t len, void *image) {
 
 
 
-static void 
+static void
 write_pnm (FILE *fout, const unsigned char * const image, const int bpp,
            const int rows, const int cols, const int maxval,
            const int format) {
@@ -46,22 +117,22 @@ write_pnm (FILE *fout, const unsigned char * const image, const int bpp,
         for (col = 0; col < cols; col++) {
             int j;
             for (j = 0; j < bpp; j++)
-                PNM_ASSIGN1(pnm_row[col], 
+                PNM_ASSIGN1(pnm_row[col],
                             image[(((row*cols)+col) * bpp) + j]);
         }
         pnm_writepnmrow(fout, pnm_row, cols, maxval, format, 0);
     }
-    
+
     pnm_freerow(pnm_row);
 }
 
 
 
 static void
-write_raw_pbm(FILE * const fout, 
+write_raw_pbm(FILE * const fout,
               const unsigned char * const binary_image,
               int                   const cols,
-              int                   const rows) { 
+              int                   const rows) {
 
     unsigned int const bytes_per_row = pbm_packed_bytes(cols);
 
@@ -70,7 +141,7 @@ write_raw_pbm(FILE * const fout,
     pbm_writepbminit(fout, cols, rows, 0);
 
     for (row = 0; row < rows; ++row)
-        pbm_writepbmrow_packed(fout, &binary_image[row*bytes_per_row], cols, 
+        pbm_writepbmrow_packed(fout, &binary_image[row*bytes_per_row], cols,
                                0);
 }
 
@@ -79,7 +150,7 @@ write_raw_pbm(FILE * const fout,
 /*
  *
  */
-static void 
+static void
 diagnose_bie(FILE *f)
 {
   unsigned char bih[20];
@@ -89,198 +160,153 @@ diagnose_bie(FILE *f)
   len = fread(bih, 1, 20, f);
   if (len < 20) {
     printf("Input file is %d < 20 bytes long and does therefore not "
-	   "contain an intact BIE header!\n", len);
+       "contain an intact BIE header!\n", len);
     return;
   }
 
   printf("Decomposition of BIH:\n\n  DL = %d\n  D  = %d\n  P  = %d\n"
-	 "  -  = %d\n  XD = %lu\n  YD = %lu\n  L0 = %lu\n  MX = %d\n"
-	 "  MY = %d\n",
-	 bih[0], bih[1], bih[2], bih[3],
-	 xd = ((unsigned long) bih[ 4] << 24) | ((unsigned long)bih[ 5] << 16)|
-	 ((unsigned long) bih[ 6] <<  8) | ((unsigned long) bih[ 7]),
-	 yd = ((unsigned long) bih[ 8] << 24) | ((unsigned long)bih[ 9] << 16)|
-	 ((unsigned long) bih[10] <<  8) | ((unsigned long) bih[11]),
-	 l0 = ((unsigned long) bih[12] << 24) | ((unsigned long)bih[13] << 16)|
-	 ((unsigned long) bih[14] <<  8) | ((unsigned long) bih[15]),
-	 bih[16], bih[17]);
+     "  -  = %d\n  XD = %lu\n  YD = %lu\n  L0 = %lu\n  MX = %d\n"
+     "  MY = %d\n",
+     bih[0], bih[1], bih[2], bih[3],
+     xd = ((unsigned long) bih[ 4] << 24) | ((unsigned long)bih[ 5] << 16)|
+     ((unsigned long) bih[ 6] <<  8) | ((unsigned long) bih[ 7]),
+     yd = ((unsigned long) bih[ 8] << 24) | ((unsigned long)bih[ 9] << 16)|
+     ((unsigned long) bih[10] <<  8) | ((unsigned long) bih[11]),
+     l0 = ((unsigned long) bih[12] << 24) | ((unsigned long)bih[13] << 16)|
+     ((unsigned long) bih[14] <<  8) | ((unsigned long) bih[15]),
+     bih[16], bih[17]);
   printf("  order   = %d %s%s%s%s%s\n", bih[18],
-	 bih[18] & JBG_HITOLO ? " HITOLO" : "",
-	 bih[18] & JBG_SEQ ? " SEQ" : "",
-	 bih[18] & JBG_ILEAVE ? " ILEAVE" : "",
-	 bih[18] & JBG_SMID ? " SMID" : "",
-	 bih[18] & 0xf0 ? " other" : "");
+     bih[18] & JBG_HITOLO ? " HITOLO" : "",
+     bih[18] & JBG_SEQ ? " SEQ" : "",
+     bih[18] & JBG_ILEAVE ? " ILEAVE" : "",
+     bih[18] & JBG_SMID ? " SMID" : "",
+     bih[18] & 0xf0 ? " other" : "");
   printf("  options = %d %s%s%s%s%s%s%s%s\n", bih[19],
-	 bih[19] & JBG_LRLTWO ? " LRLTWO" : "",
-	 bih[19] & JBG_VLENGTH ? " VLENGTH" : "",
-	 bih[19] & JBG_TPDON ? " TPDON" : "",
-	 bih[19] & JBG_TPBON ? " TPBON" : "",
-	 bih[19] & JBG_DPON ? " DPON" : "",
-	 bih[19] & JBG_DPPRIV ? " DPPRIV" : "",
-	 bih[19] & JBG_DPLAST ? " DPLAST" : "",
-	 bih[19] & 0x80 ? " other" : "");
+     bih[19] & JBG_LRLTWO ? " LRLTWO" : "",
+     bih[19] & JBG_VLENGTH ? " VLENGTH" : "",
+     bih[19] & JBG_TPDON ? " TPDON" : "",
+     bih[19] & JBG_TPBON ? " TPBON" : "",
+     bih[19] & JBG_DPON ? " DPON" : "",
+     bih[19] & JBG_DPPRIV ? " DPPRIV" : "",
+     bih[19] & JBG_DPLAST ? " DPLAST" : "",
+     bih[19] & 0x80 ? " other" : "");
   printf("\n  %lu stripes, %d layers, %d planes\n\n",
-	 ((yd >> bih[1]) +  ((((1UL << bih[1]) - 1) & xd) != 0) + l0 - 1) / l0,
-	 bih[1] - bih[0], bih[2]);
+     ((yd >> bih[1]) +  ((((1UL << bih[1]) - 1) & xd) != 0) + l0 - 1) / l0,
+     bih[1] - bih[0], bih[2]);
 
   return;
 }
 
 
-int main (int argc, char **argv)
+int main (int argc, const char **argv)
 {
-    FILE *fin = stdin, *fout = stdout;
-    const char *fnin = "<stdin>", *fnout = "<stdout>";
-    int i, j, result;
-    int all_args = 0, files = 0;
-    struct jbg_dec_state s;
-    char *buffer;
-    unsigned char *p;
-    size_t len, cnt;
-    unsigned long xmax = 4294967295UL, ymax = 4294967295UL;
-    int plane = -1, use_graycode = 1, diagnose = 0;
-
-    pnm_init(&argc, argv);
-
-    buffer = malloc(BUFSIZE);
-    if (!buffer)
-        pm_error("Sorry, not enough memory available!");
-
-    /* parse command line arguments */
-    for (i = 1; i < argc; i++) {
-        if (!all_args && argv[i][0] == '-') {
-            if (argv[i][1] == '\0' && files == 0)
-                ++files;
-            else {
-                for (j = 1; j > 0 && argv[i][j]; j++) {
-                    switch(tolower(argv[i][j])) {
-                    case '-' :
-                        all_args = 1;
-                        break;
-                    case 'b':
-                        use_graycode = 0;
-                        break;
-                    case 'd':
-                        diagnose = 1;
-                        break;
-                    case 'x':
-                        if (++i >= argc)
-                            pm_error("-x needs a value");
-                        xmax = atol(argv[i]);
-                        j = -1;
-                        break;
-                    case 'y':
-                        if (++i >= argc)
-                            pm_error("-y needs a value");
-                        ymax = atol(argv[i]);
-                        j = -1;
-                        break;
-                    case 'p':
-                        if (++i >= argc)
-                            pm_error("-p needs a value");
-                        plane = atoi(argv[i]);
-                        j = -1;
-                        break;
-                    default:
-                        pm_error("Unrecognized option: %c", argv[i][j]);
-                    }
-                }
-            }
-        } else {
-            switch (files++) {
-            case 0:
-                if (argv[i][0] != '-' || argv[i][1] != '\0') {
-                    fnin = argv[i];
-                    fin = fopen(fnin, "rb");
-                    if (!fin)
-                        pm_error("Can't open input file '%s'", fnin);
-                }
-                if (diagnose) {
-                    diagnose_bie(fin);
-                    exit(0);
-                }
-                break;
-            case 1:
-                fnout = argv[i];
-                fout = fopen(fnout, "wb");
-                if (!fout)
-                    pm_error("Can't open output file '%s'", fnout);
+    CmdlineInfo cmdline;
+    FILE * ifP;
+    FILE * ofP;
+
+    pm_proginit(&argc, argv);
+
+    parseCommandLine(argc, argv, &cmdline);
+
+    ifP = pm_openr(cmdline.inputFileName);
+    ofP = pm_openw(cmdline.outputFileName);
+
+    if (cmdline.diagnose)
+        diagnose_bie(ifP);
+    else {
+        struct jbg_dec_state s;
+        unsigned char * buffer;
+        int result;
+
+        MALLOCARRAY(buffer, BUFSIZE);
+        if (!buffer)
+            pm_error("Failed to get %u bytes of memory for buffer", BUFSIZE);
+
+        /* send input file to decoder */
+        jbg_dec_init(&s);
+        jbg_dec_maxsize(&s, cmdline.xmax, cmdline.ymax);
+        result = JBG_EAGAIN;
+        do {
+            size_t len;
+            size_t cnt;
+            unsigned char * p;
+
+            len = fread(buffer, 1, BUFSIZE, ifP);
+            if (len == 0)
                 break;
-            default:
-                pm_error("Too many non-option arguments");
+            cnt = 0;
+            p = &buffer[0];
+            while (len > 0 && (result == JBG_EAGAIN || result == JBG_EOK)) {
+                result = jbg_dec_in(&s, p, len, &cnt);
+                p += cnt;
+                len -= cnt;
+            }
+        } while (result == JBG_EAGAIN || result == JBG_EOK);
+        if (ferror(ifP))
+            pm_error("Error reading input file");
+        if (result != JBG_EOK && result != JBG_EOK_INTR)
+            pm_error("Invalid contents of input file.  %s",
+                     jbg_strerror(result));
+        if (cmdline.planeSpec && jbg_dec_getplanes(&s) <= cmdline.plane)
+            pm_error("Image has only %u planes", jbg_dec_getplanes(&s));
+
+        {
+            /* Write it out */
+
+            int rows, cols;
+            int maxval;
+            int bpp;
+            bool justOnePlane;
+            unsigned int plane_to_write;
+
+            cols = jbg_dec_getwidth(&s);
+            rows = jbg_dec_getheight(&s);
+            maxval = pm_bitstomaxval(jbg_dec_getplanes(&s));
+            bpp = (jbg_dec_getplanes(&s)+7)/8;
+
+            if (jbg_dec_getplanes(&s) == 1) {
+                justOnePlane = true;
+                plane_to_write = 0;
+            } else {
+                if (cmdline.planeSpec) {
+                    justOnePlane = true;
+                    plane_to_write = cmdline.plane;
+                } else
+                    justOnePlane = false;
             }
-        }
-    }
 
-    /* send input file to decoder */
-    jbg_dec_init(&s);
-    jbg_dec_maxsize(&s, xmax, ymax);
-    result = JBG_EAGAIN;
-    do {
-        len = fread(buffer, 1, BUFSIZE, fin);
-        if (!len) break;
-        cnt = 0;
-        p = (unsigned char *) buffer;
-        while (len > 0 && (result == JBG_EAGAIN || result == JBG_EOK)) {
-            result = jbg_dec_in(&s, p, len, &cnt);
-            p += cnt;
-            len -= cnt;
-        }
-    } while (result == JBG_EAGAIN || result == JBG_EOK);
-    if (ferror(fin)) 
-        pm_error("Problem while reading input file '%s", fnin);
-    if (result != JBG_EOK && result != JBG_EOK_INTR) 
-        pm_error("Problem with input file '%s': %s\n", 
-                 fnin, jbg_strerror(result));
-    if (plane >= 0 && jbg_dec_getplanes(&s) <= plane) 
-        pm_error("Image has only %d planes!\n", jbg_dec_getplanes(&s));
-
-    {
-        /* Write it out */
-
-        int rows, cols;
-        int maxval;
-        int bpp;
-        int plane_to_write;
-
-        cols = jbg_dec_getwidth(&s);
-        rows = jbg_dec_getheight(&s);
-        maxval = pm_bitstomaxval(jbg_dec_getplanes(&s));
-        bpp = (jbg_dec_getplanes(&s)+7)/8;
-
-        if (jbg_dec_getplanes(&s) == 1) 
-            plane_to_write = 0;
-        else 
-            plane_to_write = plane;
-
-        if (plane_to_write >= 0) {
-            /* Write just one plane */
-            unsigned char * binary_image;
-
-            pm_message("WRITING PBM FILE");
-
-            binary_image=jbg_dec_getimage(&s, plane_to_write);
-            write_raw_pbm(fout, binary_image, cols, rows);
-        } else {
-            unsigned char *image;
-            pm_message("WRITING PGM FILE");
-
-            /* Write out all the planes */
-            /* What jbig.doc doesn't tell you is that jbg_dec_merge_planes
-               delivers the image in chunks, in consecutive calls to 
-               the data-out callback function.  And a row can span two
-               chunks.
-            */
-            image = malloc(cols*rows*bpp);
-            jbg_dec_merge_planes(&s, use_graycode, collect_image, image);
-            write_pnm(fout, image, bpp, rows, cols, maxval, PGM_TYPE);
-            free(image);
+            if (justOnePlane) {
+                unsigned char * binary_image;
+
+                pm_message("WRITING PBM FILE");
+
+                binary_image=jbg_dec_getimage(&s, plane_to_write);
+                write_raw_pbm(ofP, binary_image, cols, rows);
+            } else {
+                unsigned char *image;
+                pm_message("WRITING PGM FILE");
+
+                /* Write out all the planes */
+                /* What jbig.doc doesn't tell you is that jbg_dec_merge_planes
+                   delivers the image in chunks, in consecutive calls to
+                   the data-out callback function.  And a row can span two
+                   chunks.
+                */
+                image = malloc(cols*rows*bpp);
+                jbg_dec_merge_planes(&s, !cmdline.binary, collect_image,
+                                     image);
+                write_pnm(ofP, image, bpp, rows, cols, maxval, PGM_TYPE);
+                free(image);
+            }
+            jbg_dec_free(&s);
         }
-    }
-  
-    pm_close(fout);
-
-    jbg_dec_free(&s);
 
+        pm_close(ofP);
+        pm_close(ifP);
+        free(buffer);
+    }
     return 0;
 }
+
+
+
diff --git a/converter/other/jbig/libjbig/jbig.c b/converter/other/jbig/libjbig/jbig.c
index d7141a75..e8141070 100644
--- a/converter/other/jbig/libjbig/jbig.c
+++ b/converter/other/jbig/libjbig/jbig.c
@@ -22,15 +22,13 @@
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- * 
+ *
  *  If you want to use this program under different license conditions,
  *  then contact the author for an arrangement.
  */
 
 #ifdef DEBUG
 #include <stdio.h>
-#else
-#define NDEBUG
 #endif
 
 #include <stdlib.h>
@@ -68,7 +66,7 @@
 
 /* object code version id */
 
-const char jbg_version[] = 
+const char jbg_version[] =
   "JBIG-KIT " JBG_VERSION " -- (c) 1995-2014 Markus Kuhn -- "
   "Licence: " JBG_LICENCE "\n";
 
@@ -117,7 +115,7 @@ static const char *errmsg[] = {
  * then simply add the necessary semaphores or mutex primitives below.
  * In contrast to C's malloc() and realloc(), but like C's calloc(),
  * these functions take two parameters nmemb and size that are multiplied
- * before being passed on to the corresponding C function. 
+ * before being passed on to the corresponding C function.
  * This we can catch all overflows during a size_t multiplication a
  * a single place.
  */
@@ -137,7 +135,7 @@ static void *checked_malloc(size_t nmemb, size_t size)
   /* assert that nmemb * size <= SIZE_MAX */
   if (size > SIZE_MAX / nmemb)
     abort();
-  
+
   p = malloc(nmemb * size);
 
   if (!p)
@@ -163,7 +161,7 @@ static void *checked_realloc(void *ptr, size_t nmemb, size_t size)
   /* assert that nmemb * size <= SIZE_MAX */
   if (size > SIZE_MAX / nmemb)
     abort();
-  
+
   p = realloc(ptr, nmemb * size);
 
   if (!p)
@@ -212,7 +210,7 @@ static void checked_free(void *ptr)
 static struct jbg_buf *jbg_buf_init(struct jbg_buf **free_list)
 {
   struct jbg_buf *new_block;
-  
+
   /* Test whether a block from the free list is available */
   if (*free_list) {
     new_block = *free_list;
@@ -238,13 +236,13 @@ static struct jbg_buf *jbg_buf_init(struct jbg_buf **free_list)
 static void jbg_buf_free(struct jbg_buf **free_list)
 {
   struct jbg_buf *tmp;
-  
+
   while (*free_list) {
     tmp = (*free_list)->next;
     checked_free(*free_list);
     *free_list = tmp;
   }
-  
+
   return;
 }
 
@@ -308,7 +306,7 @@ static void jbg_buf_remove_zeros(struct jbg_buf *head)
    */
   if (head->last->len && head->last->d[head->last->len - 1] == MARKER_ESC)
     jbg_buf_write(MARKER_STUFF, head);
- 
+
   return;
 }
 
@@ -324,7 +322,7 @@ static void jbg_buf_prefix(struct jbg_buf *new_prefix, struct jbg_buf **start)
   new_prefix->last->next->previous = new_prefix->last;
   new_prefix->last = new_prefix->last->next->last;
   *start = new_prefix;
-  
+
   return;
 }
 
@@ -341,7 +339,7 @@ static void jbg_buf_output(struct jbg_buf **head,
 			void *file)
 {
   struct jbg_buf *tmp;
-  
+
   while (*head) {
     data_out((*head)->d, (*head)->len, file);
     tmp = (*head)->next;
@@ -349,7 +347,7 @@ static void jbg_buf_output(struct jbg_buf **head,
     *(*head)->free_list = *head;
     *head = tmp;
   }
-  
+
   return;
 }
 
@@ -364,7 +362,7 @@ static void jbg_buf_output(struct jbg_buf **head,
 unsigned long jbg_ceil_half(unsigned long x, int n)
 {
   unsigned long mask;
-  
+
   assert(n >= 0 && n < 32);
   mask = (1UL << n) - 1;     /* the lowest n bits are 1 here */
   return (x >> n) + ((mask & x) != 0);
@@ -796,7 +794,7 @@ void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y,
   s->comment = NULL;
   s->dppriv = jbg_dptable;
   s->res_tab = jbg_resred;
-  
+
   s->highres = (int *) checked_malloc(planes, sizeof(int));
   s->lhp[0] = p;
   s->lhp[1] = (unsigned char **)
@@ -806,9 +804,9 @@ void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y,
     s->lhp[1][i] = (unsigned char *)
       checked_malloc(jbg_ceil_half(y, 1), jbg_ceil_half(x, 1+3));
   }
-  
+
   s->free_list = NULL;
-  s->s = (struct jbg_arenc_state *) 
+  s->s = (struct jbg_arenc_state *)
     checked_malloc(s->planes, sizeof(struct jbg_arenc_state));
   s->tx = (int *) checked_malloc(s->planes, sizeof(int));
   lx = jbg_ceil_half(x, 1);
@@ -830,9 +828,9 @@ void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y,
  * l0 (height of one stripe in the lowest resolution layer) is
  * selected, which obeys the recommended limitations for l0 in annex A
  * and C of the JBIG standard. The selected number of resolution layers
- * is returned. 
+ * is returned.
  */
-int jbg_enc_lrlmax(struct jbg_enc_state *s, unsigned long x, 
+int jbg_enc_lrlmax(struct jbg_enc_state *s, unsigned long x,
 		   unsigned long y)
 {
   for (s->d = 0; s->d < 6; s->d++)
@@ -846,8 +844,8 @@ int jbg_enc_lrlmax(struct jbg_enc_state *s, unsigned long x,
 
 
 /*
- * As an alternative to jbg_enc_lrlmax(), the following function allows
- * to specify the number of layers directly. The stripe height and layer
+ * As an alternative to jbg_enc_lrlmax(), the following function allows the
+ * user to specify the number of layers directly. The stripe height and layer
  * range is also adjusted automatically here.
  */
 void jbg_enc_layers(struct jbg_enc_state *s, int d)
@@ -879,9 +877,9 @@ int jbg_enc_lrange(struct jbg_enc_state *s, int dl, int dh)
 
 
 /*
- * The following function allows to specify the bits describing the
- * options of the format as well as the maximum AT movement window and
- * the number of layer 0 lines per stripes.
+ * The following function allows the user to specify the bits describing the
+ * options of the format as well as the maximum AT movement window and the
+ * number of layer 0 lines per stripes.
  */
 void jbg_enc_options(struct jbg_enc_state *s, int order, int options,
 		     unsigned long l0, int mx, int my)
@@ -957,7 +955,7 @@ static void encode_sde(struct jbg_enc_state *s,
   hp = s->lhp[s->highres[plane]][plane] + stripe * hl * hbpl;
   lp2 = s->lhp[1 - s->highres[plane]][plane] + stripe * ll * lbpl;
   lp1 = lp2 + lbpl;
-  
+
   /* check whether we can refer to any state of a previous stripe */
   reset = (stripe == 0) || (s->options & JBG_SDRST);
 
@@ -1035,7 +1033,7 @@ static void encode_sde(struct jbg_enc_state *s,
 	at_determined = 1;
       }
       assert(s->tx[plane] >= 0); /* i.e., tx can safely be cast to unsigned */
-      
+
       /* typical prediction */
       if (s->options & JBG_TPBON) {
 	ltp = 1;
@@ -1066,11 +1064,11 @@ static void encode_sde(struct jbg_enc_state *s,
        *          76543210765432107654321076543210     line_h2
        *  76543210765432107654321X76543210             line_h1
        */
-      
+
       line_h1 = line_h2 = line_h3 = 0;
       if (i > 0 || !reset) line_h2 = (long)*(hp - hbpl) << 8;
       if (i > 1 || !reset) line_h3 = (long)*(hp - hbpl - hbpl) << 8;
-      
+
       /* encode line */
       for (j = 0; j < hx; hp++) {
 	line_h1 |= *hp;
@@ -1177,7 +1175,7 @@ static void encode_sde(struct jbg_enc_state *s,
     /*
      *  Encode differential layer
      */
-    
+
     for (i = 0; i < hl && y < hy; i++, y++) {
 
       /* check whether it is worth to perform an ATMOVE */
@@ -1213,7 +1211,7 @@ static void encode_sde(struct jbg_enc_state *s,
 	}
 	at_determined = 1;
       }
-      
+
       if ((i >> 1) >= ll - 1 || (y >> 1) >= ly - 1)
 	lp1 = lp2;
 
@@ -1299,7 +1297,7 @@ static void encode_sde(struct jbg_enc_state *s,
        *            76543210 7654321Y 76543210 76543210     line_l2
        *            76543210 76543210 76543210 76543210     line_l1
        */
-      
+
 
       line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0;
       if (i > 0 || !reset) line_h2 = (long)*(hp - hbpl) << 8;
@@ -1309,7 +1307,7 @@ static void encode_sde(struct jbg_enc_state *s,
       }
       line_l2 = (long)*lp2 << 8;
       line_l1 = (long)*lp1 << 8;
-      
+
       /* encode line */
       for (j = 0; j < hx; lp1++, lp2++) {
 	if ((j >> 1) < lbpl * 8 - 8) {
@@ -1403,8 +1401,8 @@ static void encode_sde(struct jbg_enc_state *s,
 #endif
 			continue;
 		      }
-		    }	
-		  }	
+		    }
+		  }
 		}
 
 		/* determine context */
@@ -1437,7 +1435,7 @@ static void encode_sde(struct jbg_enc_state *s,
 #ifdef DEBUG
 		encoded_pixels++;
 #endif
-		
+
 		/* statistics for adaptive template changes */
 		if (!at_determined && j >= s->mx) {
 		  c[0] += !(((line_h2 >> 6) ^ line_h1) & 0x100);
@@ -1445,7 +1443,7 @@ static void encode_sde(struct jbg_enc_state *s,
 		    c[t] += !(((line_h1 >> t) ^ line_h1) & 0x100);
 		  ++c_all;
 		}
-		
+
 	      } while (++j & 1 && j < hx);
 	  } while (j & 7 && j < hx);
 	  hp++;
@@ -1457,10 +1455,10 @@ static void encode_sde(struct jbg_enc_state *s,
 	lp1 -= lbpl;
 	lp2 -= lbpl;
       }
-      
+
     } /* for (i = ...) */
   }
-  
+
   arith_encode_flush(se);
   jbg_buf_remove_zeros(s->sde[stripe][layer][plane]);
   jbg_buf_write(MARKER_ESC, s->sde[stripe][layer][plane]);
@@ -1541,7 +1539,7 @@ static void resolution_reduction(struct jbg_enc_state *s, int plane,
   hp1 = hp2 + hbpl;
   hp3 = hp2 - hbpl;
   lp = s->lhp[1 - s->highres[plane]][plane];
-  
+
 #ifdef DEBUG
   fprintf(stderr, "resolution_reduction: plane = %d, higher_layer = %d\n",
 	  plane, higher_layer);
@@ -1608,7 +1606,7 @@ static void resolution_reduction(struct jbg_enc_state *s, int plane,
   {
     FILE *f;
     char fn[50];
-    
+
     sprintf(fn, "dbg_d=%02d.pbm", higher_layer - 1);
     f = fopen(fn, "wb");
     fprintf(f, "P4\n%lu %lu\n", lx, ly);
@@ -1621,7 +1619,7 @@ static void resolution_reduction(struct jbg_enc_state *s, int plane,
 }
 
 
-/* 
+/*
  * This function is called inside the three loops of jbg_enc_out() in
  * order to write the next SDE. It has first to generate the required
  * SDE and all SDEs which have to be encoded before this SDE can be
@@ -1642,7 +1640,7 @@ static void output_sde(struct jbg_enc_state *s,
   int lfcl;     /* lowest fully coded layer */
   long i;
   unsigned long u;
-  
+
   assert(s->sde[stripe][layer][plane] != SDE_DONE);
 
   if (s->sde[stripe][layer][plane] != SDE_TODO) {
@@ -1679,7 +1677,7 @@ static void output_sde(struct jbg_enc_state *s,
     if (lfcl > 1)
       resolution_reduction(s, plane, lfcl - 1);
   }
-  
+
   encode_sde(s, stripe, layer, plane);
 
 #ifdef DEBUG
@@ -1687,14 +1685,14 @@ static void output_sde(struct jbg_enc_state *s,
 #endif
   jbg_buf_output(&s->sde[stripe][layer][plane], s->data_out, s->file);
   s->sde[stripe][layer][plane] = SDE_DONE;
-  
+
   if (stripe == s->stripes - 1 && layer > 0 &&
       s->sde[0][layer-1][plane] == SDE_TODO) {
     s->highres[plane] ^= 1;
     if (layer > 1)
       resolution_reduction(s, plane, layer - 1);
   }
-  
+
   return;
 }
 
@@ -1729,7 +1727,7 @@ void jbg_int2dppriv(unsigned char *dptable, const char *internal)
   int trans1[ 9] = { 1, 0, 3, 2, 8, 7, 6, 5, 4 };
   int trans2[11] = { 1, 0, 3, 2, 10, 9, 8, 7, 6, 5, 4 };
   int trans3[12] = { 1, 0, 3, 2, 11, 10, 9, 8, 7, 6, 5, 4 };
-  
+
   for (i = 0; i < 1728; dptable[i++] = 0) ;
 
 #define FILL_TABLE1(offset, len, trans) \
@@ -1762,7 +1760,7 @@ void jbg_dppriv2int(char *internal, const unsigned char *dptable)
   int trans1[ 9] = { 1, 0, 3, 2, 8, 7, 6, 5, 4 };
   int trans2[11] = { 1, 0, 3, 2, 10, 9, 8, 7, 6, 5, 4 };
   int trans3[12] = { 1, 0, 3, 2, 11, 10, 9, 8, 7, 6, 5, 4 };
-  
+
 #define FILL_TABLE2(offset, len, trans) \
   for (i = 0; i < len; i++) { \
     k = 0; \
@@ -1790,7 +1788,7 @@ void jbg_enc_out(struct jbg_enc_state *s)
   unsigned long bpl;
   unsigned char buf[20];
   unsigned long xd, yd, y;
-  long ii[3], is[3], ie[3];    /* generic variables for the 3 nested loops */ 
+  long ii[3], is[3], ie[3];    /* generic variables for the 3 nested loops */
   unsigned long stripe;
   int layer, plane;
   int order;
@@ -1918,7 +1916,7 @@ void jbg_enc_out(struct jbg_enc_state *s)
   for (ii[0] = is[0]; ii[0] <= ie[0]; ii[0]++)
     for (ii[1] = is[1]; ii[1] <= ie[1]; ii[1]++)
       for (ii[2] = is[2]; ii[2] <= ie[2]; ii[2]++) {
-	
+
 	stripe = ii[iindex[order][STRIPE]];
 	if (s->order & JBG_HITOLO)
 	  layer = s->dh - (ii[iindex[order][LAYER]] - s->dl);
@@ -1949,7 +1947,7 @@ void jbg_enc_out(struct jbg_enc_state *s)
 	 */
 	if (s->yd1 > s->yd &&
 	    (stripe == s->stripes - 1 ||
-	     (stripe == s->stripes - 2 && 
+	     (stripe == s->stripes - 2 &&
 	      (s->dl != s->dh || s->planes > 1)))) {
 	  s->yd1 = s->yd;
 	  yd = jbg_ceil_half(s->yd, s->d - s->dh);
@@ -2017,10 +2015,10 @@ void jbg_enc_free(struct jbg_enc_state *s)
       checked_free(s->lhp[1][plane]);
     checked_free(s->lhp[1]);
   }
-  
+
   /* clear buffer for index of highres image in lhp */
   checked_free(s->highres);
-  
+
   return;
 }
 
@@ -2039,7 +2037,7 @@ const char *jbg_strerror(int errnum)
 
 
 /*
- * The constructor for a decoder 
+ * The constructor for a decoder
  */
 void jbg_dec_init(struct jbg_dec_state *s)
 {
@@ -2077,7 +2075,7 @@ void jbg_dec_maxsize(struct jbg_dec_state *s, unsigned long xmax,
 /*
  * Decode the new len PSDC bytes to which data points and add them to
  * the current stripe. Return the number of bytes which have actually
- * been read (this will be less than len if a marker segment was 
+ * been read (this will be less than len if a marker segment was
  * part of the data or if the final byte was 0xff, in which case
  * this code cannot determine whether we have a marker segment).
  */
@@ -2106,7 +2104,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
   se = s->s[plane] + layer - s->dl;
   se->pscd_ptr = data;
   se->pscd_end = data + len;
-  
+
   /* number of lines per stripe in highres image */
   hl = s->l0 << layer;
   /* number of lines per stripe in lowres image */
@@ -2194,7 +2192,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 	/* this line is 'not typical' and has to be coded completely */
       }
       s->pseudo = 0;
-      
+
       /*
        * Layout of the variables line_h1, line_h2, line_h3, which contain
        * as bits the neighbour pixels of the currently decoded pixel X:
@@ -2203,7 +2201,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
        *                     76543210 76543210 76543210 76543210     line_h2
        *   76543210 76543210 76543210 76543210 X                     line_h1
        */
-      
+
       if (x == 0) {
 	line_h1 = line_h2 = line_h3 = 0;
 	if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl]))
@@ -2211,7 +2209,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 	if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
 	  line_h3 = (long)*(hp - hbpl - hbpl) << 8;
       }
-      
+
       /*
        * Another tiny JBIG standard bug:
        *
@@ -2315,7 +2313,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 				      (line_h1 & 0x003)));
 	    if (pix < 0)
 	      goto leave;
-	    
+
 	    line_h1 = (line_h1 << 1) | pix;
 	    line_h2 <<= 1;
 	    line_h3 <<= 1;
@@ -2327,7 +2325,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
       x = 0;
       s->pseudo = 1;
     } /* for (i = ...) */
-    
+
   } else {
 
     /*
@@ -2378,7 +2376,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
        *                     76543210 76543210 Y6543210 76543210     line_l2
        *                     76543210 76543210 76543210 76543210     line_l1
        */
-      
+
 
       if (x == 0) {
 	line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0;
@@ -2392,14 +2390,14 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 	line_l2 = (long)*lp2 << 8;
 	line_l1 = (long)*lp1 << 8;
       }
-      
+
       /* decode line */
       while (x < hx) {
 	if ((x & 15) == 0)
 	  if ((x >> 1) < lbpl * 8 - 8) {
 	    line_l1 |= *(lp1 + 1);
 	    line_l2 |= *(lp2 + 1);
-	    if (s->i > 1 || 
+	    if (s->i > 1 ||
 		(y > 1 && !s->reset[plane][layer - s->dl]))
 	      line_l3 |= *(lp2 - lbpl + 1);
 	  }
@@ -2430,13 +2428,13 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 		line_h1 = (line_h1 << 1) | (cx & 1);
 	      } while ((++x & 1) && x < hx);
 	      line_h2 <<= 2;  line_h3 <<= 2;
-	    } else 
+	    } else
 	      do {
-		
+
 		/* deterministic prediction */
 		if (s->options & JBG_DPON)
 		  if ((y & 1) == 0)
-		    if ((x & 1) == 0) 
+		    if ((x & 1) == 0)
 		      /* phase 0 */
 		      pix = s->dppriv[((line_l3 >> 15) & 0x003) |
 				      ((line_l2 >> 13) & 0x00c) |
@@ -2492,7 +2490,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 		line_h1 = (line_h1 << 1) | pix;
 		line_h2 <<= 1;
 		line_h3 <<= 1;
-		
+
 	      } while ((++x & 1) && x < hx);
 	    line_l1 <<= 1; line_l2 <<= 1;  line_l3 <<= 1;
 	  } while ((x & 7) && x < hx);
@@ -2502,7 +2500,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 	++lp2;
       } /* while */
       x = 0;
-      
+
       *(hp - 1) <<= hbpl * 8 - hx;
       if ((s->i & 1) == 0) {
 	/* low resolution pixels are used twice */
@@ -2510,9 +2508,9 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
 	lp2 -= lbpl;
       } else
 	s->pseudo = 1;
-      
+
     } /* for (i = ...) */
-    
+
   }
 
  leave:
@@ -2581,7 +2579,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
   if (s->bie_len < 20) {
     while (s->bie_len < 20 && *cnt < len)
       s->buffer[s->bie_len++] = data[(*cnt)++];
-    if (s->bie_len < 20) 
+    if (s->bie_len < 20)
       return JBG_EAGAIN;
     /* test whether this looks like a valid JBIG header at all */
     if (s->buffer[1] < s->buffer[0])
@@ -2642,7 +2640,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
 
     /* calculate number of stripes that will be required */
     s->stripes = jbg_stripes(s->l0, s->yd, s->d);
-    
+
     /* some initialization */
     s->ii[iindex[s->order & 7][STRIPE]] = 0;
     s->ii[iindex[s->order & 7][LAYER]] = s->dl;
@@ -2705,7 +2703,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
   }
 
   /* read in DPTABLE */
-  if (s->bie_len < 20 + 1728 && 
+  if (s->bie_len < 20 + 1728 &&
       (s->options & (JBG_DPON | JBG_DPPRIV | JBG_DPLAST)) ==
       (JBG_DPON | JBG_DPPRIV)) {
     assert(s->bie_len >= 20);
@@ -2713,7 +2711,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
       s->dppriv = (char *) checked_malloc(1728, sizeof(char));
     while (s->bie_len < 20 + 1728 && *cnt < len)
       s->dppriv[s->bie_len++ - 20] = data[(*cnt)++];
-    if (s->bie_len < 20 + 1728) 
+    if (s->bie_len < 20 + 1728)
       return JBG_EAGAIN;
     dppriv = (unsigned char *) s->dppriv;
     s->dppriv = (char *) checked_malloc(6912, sizeof(char));
@@ -2724,7 +2722,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
   /*
    * BID processing loop
    */
-  
+
   while (*cnt < len) {
 
     /* process floating marker segments */
@@ -2801,27 +2799,27 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
 	break;
       case MARKER_ABORT:
 	return JBG_EABORT;
-	
+
       case MARKER_SDNORM:
       case MARKER_SDRST:
 	/* decode final pixels based on trailing zero bytes */
 	decode_pscd(s, s->buffer, 2);
 
-	arith_decode_init(s->s[s->ii[iindex[s->order & 7][PLANE]]] + 
+	arith_decode_init(s->s[s->ii[iindex[s->order & 7][PLANE]]] +
 			  s->ii[iindex[s->order & 7][LAYER]] - s->dl,
 			  s->ii[iindex[s->order & 7][STRIPE]] != s->stripes - 1
 			  && s->buffer[1] != MARKER_SDRST);
-	
+
 	s->reset[s->ii[iindex[s->order & 7][PLANE]]]
 	  [s->ii[iindex[s->order & 7][LAYER]] - s->dl] =
 	    (s->buffer[1] == MARKER_SDRST);
-	
+
 	/* prepare for next SDE */
 	s->x = 0;
 	s->i = 0;
 	s->pseudo = 1;
 	s->at_moves = 0;
-	
+
 	/* increment layer/stripe/plane loop variables */
 	/* start and end value for each loop: */
 	is[iindex[s->order & 7][STRIPE]] = 0;
@@ -2842,7 +2840,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
 	} while (--i >= 0 && j);
 
 	s->buf_len = 0;
-	
+
 	/* check whether this have been all SDEs */
 	if (j) {
 #ifdef DEBUG
@@ -2887,7 +2885,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
 #endif
 	return JBG_EINVAL | 14;
       }
-      
+
     }
   }  /* of BID processing loop 'while (*cnt < len) ...' */
 
@@ -2930,7 +2928,7 @@ unsigned long jbg_dec_getheight(const struct jbg_dec_state *s)
     else
       return jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1));
   }
-  
+
   return s->yd;
 }
 
@@ -2950,7 +2948,7 @@ unsigned char *jbg_dec_getimage(const struct jbg_dec_state *s, int plane)
     else
       return s->lhp[(s->ii[0] - 1) & 1][plane];
   }
-  
+
   return s->lhp[s->d & 1][plane];
 }
 
@@ -2968,11 +2966,11 @@ unsigned long jbg_dec_getsize(const struct jbg_dec_state *s)
     if (s->ii[0] < 1)
       return 0;
     else
-      return 
+      return
 	jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1) + 3) * /* overflow risk? */
 	jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1));
   }
-  
+
   return jbg_ceil_half(s->xd, 3) * s->yd;
 }
 
@@ -2990,17 +2988,17 @@ unsigned long jbg_dec_getsize_merged(const struct jbg_dec_state *s)
     if (s->ii[0] < 1)
       return 0;
     else
-      return 
+      return
 	jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1)) * /* overflow risk? */
 	jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1)) *
 	((s->planes + 7) / 8);
   }
-  
+
   return s->xd * s->yd * ((s->planes + 7) / 8);
 }
 
 
-/* 
+/*
  * The destructor function which releases any resources obtained by the
  * other decoder functions.
  */
@@ -3021,7 +3019,7 @@ void jbg_dec_free(struct jbg_dec_state *s)
     checked_free(s->lhp[0][i]);
     checked_free(s->lhp[1][i]);
   }
-  
+
   checked_free(s->s);
   checked_free(s->tx);
   checked_free(s->ty);
@@ -3063,10 +3061,10 @@ void jbg_split_planes(unsigned long x, unsigned long y, int has_planes,
   if (encode_planes > has_planes)
     encode_planes = has_planes;
   use_graycode = use_graycode != 0 && encode_planes > 1;
-  
+
   for (p = 0; p < encode_planes; p++)
     memset(dest[p], 0, bpl * y);
-  
+
   for (line = 0; line < y; line++) {                 /* lines loop */
     for (i = 0; i * 8 < x; i++) {                    /* dest bytes loop */
       for (k = 0; k < 8 && i * 8 + k < x; k++) {     /* pixel loop */
@@ -3098,11 +3096,11 @@ void jbg_split_planes(unsigned long x, unsigned long y, int has_planes,
     for (p = 0; p < encode_planes; p++)              /* right padding loop */
       dest[p][bpl * (line + 1) - 1] <<= 8 - k;
   }
-  
+
   return;
 }
 
-/* 
+/*
  * Merge the separate bit planes decoded by the JBIG decoder into an
  * integer pixel field. This is essentially the counterpart to
  * jbg_split_planes().
@@ -3123,7 +3121,7 @@ void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode,
 
   /* sanity check */
   use_graycode = use_graycode != 0;
-  
+
   x = jbg_dec_getwidth(s);
   y = jbg_dec_getheight(s);
   if (x == 0 || y == 0)
@@ -3137,7 +3135,7 @@ void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode,
       src = s->lhp[(s->ii[0] - 1) & 1];
   else
     src = s->lhp[s->d & 1];
-  
+
   for (line = 0; line < y; line++) {                    /* lines loop */
     for (i = 0; i * 8 < x; i++) {                       /* src bytes loop */
       for (k = 0; k < 8 && i * 8 + k < x; k++) {        /* pixel loop */
@@ -3157,10 +3155,10 @@ void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode,
       }
     }
   }
-  
+
   if (bp - buf > 0)
     data_out(buf, bp - buf, file);
-  
+
   return;
 }
 
@@ -3175,7 +3173,7 @@ void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode,
  *  - not enough bytes left for complete marker segment
  *  - no marker segment terminates the PSCD
  *  - unknown marker code encountered
- *  
+ *
  */
 static unsigned char *jbg_next_pscdms(unsigned char *p, size_t len)
 {
diff --git a/converter/other/jpeg2000/jpeg2ktopam.c b/converter/other/jpeg2000/jpeg2ktopam.c
index 405de9c9..b507f56e 100644
--- a/converter/other/jpeg2000/jpeg2ktopam.c
+++ b/converter/other/jpeg2000/jpeg2ktopam.c
@@ -8,6 +8,7 @@
 
 *****************************************************************************/
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
     /* In 2014.09, this was _XOPEN_SOURCE 600, with a comment saying it was
@@ -61,7 +62,7 @@ parseCommandLine(int argc, char ** argv,
     MALLOCARRAY_NOFAIL(option_def, 100);
 
     option_def_index = 0;   /* incremented by OPTENTRY */
-    OPTENT3(0, "verbose",      OPT_FLAG,   NULL, 
+    OPTENT3(0, "verbose",      OPT_FLAG,   NULL,
             &cmdlineP->verbose,   0);
     OPTENT3(0, "debuglevel",   OPT_UINT,   &cmdlineP->debuglevel,
             &debuglevelSpec,      0);
@@ -79,7 +80,7 @@ parseCommandLine(int argc, char ** argv,
         cmdlineP->inputFilename = strdup("-");  /* he wants stdin */
     else if (argc - 1 == 1)
         cmdlineP->inputFilename = strdup(argv[1]);
-    else 
+    else
         pm_error("Too many arguments.  The only argument accepted\n"
                  "is the input file specification");
 
@@ -106,11 +107,11 @@ validateJ2k(jas_stream_t * const instreamP) {
     }
 }
 
-        
+
 
 
 static void
-readJ2k(const char *   const inputFilename, 
+readJ2k(const char *   const inputFilename,
         jas_image_t ** const jasperPP) {
 
     jas_image_t * jasperP;
@@ -126,7 +127,7 @@ readJ2k(const char *   const inputFilename,
         instreamP = jas_stream_fopen(inputFilename, "rb");
         if (instreamP == NULL )
             pm_error("cannot open input image file '%s'", inputFilename);
-    } 
+    }
 
     validateJ2k(instreamP);
 
@@ -138,7 +139,7 @@ readJ2k(const char *   const inputFilename,
         pm_error("Unable to interpret JPEG-2000 input.  "
                  "The Jasper library jas_image_decode() subroutine failed.");
 
-	jas_stream_close(instreamP);
+    jas_stream_close(instreamP);
 
     *jasperPP = jasperP;
 }
@@ -149,7 +150,7 @@ static void
 getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) {
 
     {
-        int const rc = 
+        int const rc =
             jas_image_getcmptbytype(jasperP,
                                     JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R));
         if (rc < 0)
@@ -158,12 +159,12 @@ getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) {
         else
             jasperCmpnt[PAM_RED_PLANE] = rc;
 
-        if (jas_image_cmptsgnd(jasperP, rc)) 
+        if (jas_image_cmptsgnd(jasperP, rc))
             pm_error("Input image says it is RGB, but has signed values "
                      "for what should be the red intensities.");
     }
     {
-        int const rc = 
+        int const rc =
             jas_image_getcmptbytype(jasperP,
                                     JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G));
         if (rc < 0)
@@ -172,12 +173,12 @@ getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) {
         else
             jasperCmpnt[PAM_GRN_PLANE] = rc;
 
-        if (jas_image_cmptsgnd(jasperP, rc)) 
+        if (jas_image_cmptsgnd(jasperP, rc))
             pm_error("Input image says it is RGB, but has signed values "
                      "for what should be the green intensities.");
     }
     {
-        int const rc = 
+        int const rc =
             jas_image_getcmptbytype(jasperP,
                                     JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B));
         if (rc < 0)
@@ -186,18 +187,18 @@ getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) {
         else
             jasperCmpnt[PAM_BLU_PLANE] = rc;
 
-        if (jas_image_cmptsgnd(jasperP, rc)) 
+        if (jas_image_cmptsgnd(jasperP, rc))
             pm_error("Input image says it is RGB, but has signed values "
                      "for what should be the blue intensities.");
     }
-}            
+}
 
 
 
 static void
 getGrayComponent(int * jasperCmptP, jas_image_t * const jasperP) {
 
-    int const rc = 
+    int const rc =
         jas_image_getcmptbytype(jasperP,
                                 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y));
     if (rc < 0)
@@ -205,7 +206,7 @@ getGrayComponent(int * jasperCmptP, jas_image_t * const jasperP) {
                  "no gray intensity component");
     else
         *jasperCmptP = rc;
-    if (jas_image_cmptsgnd(jasperP, 0)) 
+    if (jas_image_cmptsgnd(jasperP, 0))
         pm_error("Input image says it is grayscale, but has signed values "
                  "for what should be the gray levels.");
 }
@@ -220,13 +221,13 @@ validateComponentsAlike(jas_image_t * const jasperP) {
    all the channels are the same, and abort the program if not.
 -----------------------------------------------------------------------------*/
     int cmptNo;
-    
+
     for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo) {
-        if (jas_image_cmptwidth(jasperP, cmptNo) != 
+        if (jas_image_cmptwidth(jasperP, cmptNo) !=
             jas_image_cmptwidth(jasperP, 0))
             pm_message("Input image does not have components all the same "
                        "width.");
-        if (jas_image_cmptheight(jasperP, cmptNo) != 
+        if (jas_image_cmptheight(jasperP, cmptNo) !=
             jas_image_cmptheight(jasperP, 0))
             pm_message("Input image does not have components all the same "
                        "height.");
@@ -240,7 +241,7 @@ maxJasperComponentPrecision(jas_image_t * const jasperP) {
 
     int cmptNo;
     unsigned int max;
-    
+
     max = 1;
 
     for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo)
@@ -261,8 +262,8 @@ computeOutputParm(jas_image_t * const jasperP,
           with the Jasper library that corresponds to Plane P of the PAM.
        */
 
-	switch (jas_clrspc_fam(jas_image_clrspc(jasperP))) {
-	case JAS_CLRSPC_FAM_GRAY:
+    switch (jas_clrspc_fam(jas_image_clrspc(jasperP))) {
+    case JAS_CLRSPC_FAM_GRAY:
         outpamP->depth = 1;
         MALLOCARRAY_NOFAIL(jasperCmptNo, 1);
         getGrayComponent(&jasperCmptNo[0], jasperP);
@@ -274,7 +275,7 @@ computeOutputParm(jas_image_t * const jasperP,
             strcpy(outpamP->tuple_type, PAM_PGM_TUPLETYPE);
         }
         break;
-	case JAS_CLRSPC_FAM_RGB:
+    case JAS_CLRSPC_FAM_RGB:
         outpamP->depth = 3;
         MALLOCARRAY_NOFAIL(jasperCmptNo, 3);
         getRgbComponents(jasperCmptNo, jasperP);
@@ -292,15 +293,15 @@ computeOutputParm(jas_image_t * const jasperP,
                 jasperCmptNo[plane] = plane;
         }
         strcpy(outpamP->tuple_type, "");
-        if (jas_image_cmptsgnd(jasperP, 0)) 
+        if (jas_image_cmptsgnd(jasperP, 0))
             pm_message("Warning: Input image has signed sample values.  "
                        "They will be represented in the PAM output in "
                        "two's complement.");
     }
     outpamP->plainformat = FALSE;
 
-	outpamP->width = jas_image_cmptwidth(jasperP, 0);
-	outpamP->height = jas_image_cmptheight(jasperP, 0);
+    outpamP->width = jas_image_cmptwidth(jasperP, 0);
+    outpamP->height = jas_image_cmptheight(jasperP, 0);
 
     validateComponentsAlike(jasperP);
 
@@ -308,7 +309,7 @@ computeOutputParm(jas_image_t * const jasperP,
         unsigned int const maxPrecision = maxJasperComponentPrecision(jasperP);
 
         outpamP->maxval = pm_bitstomaxval(maxPrecision);
-        
+
         outpamP->bytes_per_sample = (maxPrecision + 7)/8;
     }
     *jasperCmptNoP = jasperCmptNo;
@@ -319,7 +320,7 @@ computeOutputParm(jas_image_t * const jasperP,
 static void
 createMatrices(struct pam * const outpamP, jas_matrix_t *** matrixP) {
 
-    jas_matrix_t ** matrix; 
+    jas_matrix_t ** matrix;
     unsigned int plane;
 
     MALLOCARRAY_NOFAIL(matrix, outpamP->depth);
@@ -330,14 +331,14 @@ createMatrices(struct pam * const outpamP, jas_matrix_t *** matrixP) {
         if (matrix[plane] == NULL)
             pm_error("Unable to create matrix for plane %u.  "
                      "jas_matrix_create() failed.", plane);
-    }   
+    }
     *matrixP = matrix;
 }
 
 
 
 static void
-destroyMatrices(struct pam *    const outpamP, 
+destroyMatrices(struct pam *    const outpamP,
                 jas_matrix_t ** const matrix ) {
 
     unsigned int plane;
@@ -345,7 +346,7 @@ destroyMatrices(struct pam *    const outpamP,
     for (plane = 0; plane < outpamP->depth; ++plane)
         jas_matrix_destroy(matrix[plane]);
     free(matrix);
-}    
+}
 
 
 
@@ -355,7 +356,7 @@ computeComponentMaxval(struct pam *  const outpamP,
                        int           const jasperCmpt[],
                        sample **     const jasperMaxvalP,
                        bool *        const singleMaxvalP) {
-    
+
     sample * jasperMaxval;
     unsigned int plane;
 
@@ -363,7 +364,7 @@ computeComponentMaxval(struct pam *  const outpamP,
 
     *singleMaxvalP = TRUE;  /* initial assumption */
     for (plane = 0; plane < outpamP->depth; ++plane) {
-        jasperMaxval[plane] = 
+        jasperMaxval[plane] =
             pm_bitstomaxval(jas_image_cmptprec(jasperP, jasperCmpt[plane]));
         if (jasperMaxval[plane] != jasperMaxval[0])
             *singleMaxvalP = FALSE;
@@ -371,7 +372,7 @@ computeComponentMaxval(struct pam *  const outpamP,
     *jasperMaxvalP = jasperMaxval;
 }
 
-                       
+
 
 static void
 copyRowSingleMaxval(jas_seqent_t ** const jasperRow,
@@ -386,10 +387,10 @@ copyRowSingleMaxval(jas_seqent_t ** const jasperRow,
    This is significantly faster than copyRowAnyMaxval().
 -----------------------------------------------------------------------------*/
     unsigned int col;
-    
+
     for (col = 0; col < outpamP->width; ++col) {
         unsigned int plane;
-        for (plane = 0; plane < outpamP->depth; ++plane) 
+        for (plane = 0; plane < outpamP->depth; ++plane)
             tuplerow[col][plane] = jasperRow[plane][col];
     }
 }
@@ -410,12 +411,12 @@ copyRowAnyMaxval(jas_seqent_t ** const jasperRow,
    This is significantly slower than copyRowSingleMaxval().
 -----------------------------------------------------------------------------*/
     unsigned int col;
-            
+
     for (col = 0; col < outpamP->width; ++col) {
         unsigned int plane;
-        for (plane = 0; plane < outpamP->depth; ++plane) 
-            tuplerow[col][plane] = 
-                jasperRow[plane][col] * 
+        for (plane = 0; plane < outpamP->depth; ++plane)
+            tuplerow[col][plane] =
+                jasperRow[plane][col] *
                 outpamP->maxval / jasperMaxval[plane];
     }
 }
@@ -460,11 +461,11 @@ convertToPamPnm(struct pam *  const outpamP,
                                     matrix[plane]);
             if (rc != 0)
                 pm_error("jas_image_readcmpt() of row %u plane %u "
-                         "failed.", 
+                         "failed.",
                          row, plane);
             jasperRow[plane] = jas_matrix_getref(matrix[plane], 0, 0);
         }
-        if (singleMaxval) 
+        if (singleMaxval)
             copyRowSingleMaxval(jasperRow, tuplerow, outpamP);
         else
             copyRowAnyMaxval(jasperRow, tuplerow, outpamP, jasperMaxval);
@@ -488,25 +489,25 @@ main(int argc, char **argv)
     struct pam outpam;
     int * jasperCmpt;  /* malloc'ed */
        /* jaspercmpt[P] is the component number for use with the
-          Jasper library that corresponds to Plane P of the PAM.  
+          Jasper library that corresponds to Plane P of the PAM.
        */
     jas_image_t * jasperP;
 
     pnm_init(&argc, argv);
-    
+
     parseCommandLine(argc, argv, &cmdline);
-    
-    { 
+
+    {
         int rc;
-        
+
         rc = jas_init();
         if ( rc != 0 )
             pm_error("Failed to initialize Jasper library.  "
                      "jas_init() returns rc %d", rc );
     }
-    
+
     jas_setdbglevel(cmdline.debuglevel);
-    
+
     readJ2k(cmdline.inputFilename, &jasperP);
 
     outpam.file = stdout;
@@ -516,13 +517,16 @@ main(int argc, char **argv)
     computeOutputParm(jasperP, &outpam, &jasperCmpt);
 
     pnm_writepaminit(&outpam);
-    
+
     convertToPamPnm(&outpam, jasperP, jasperCmpt);
-    
+
     free(jasperCmpt);
-	jas_image_destroy(jasperP);
+    jas_image_destroy(jasperP);
 
     pm_close(stdout);
-    
+
     return 0;
 }
+
+
+
diff --git a/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c b/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c
index 91ce6c51..a9463a57 100644
--- a/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c
+++ b/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c
@@ -110,16 +110,6 @@
  * __END_OF_JASPER_LICENSE__
  */
 
-/*
- * JP2 Library
- *
- * $Id$
- */
-
-/******************************************************************************\
-* Includes.
-\******************************************************************************/
-
 #include "jasper/jas_image.h"
 #include "jasper/jas_stream.h"
 #include "jasper/jas_math.h"
@@ -130,520 +120,548 @@
 #include "jp2_cod.h"
 #include "jp2_dec.h"
 
-#define	JP2_VALIDATELEN	(JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK))
-
-static jp2_dec_t *jp2_dec_create(void);
-static void jp2_dec_destroy(jp2_dec_t *dec);
-static int jp2_getcs(jp2_colr_t *colr);
-static int jp2_getct(int colorspace, int type, int assoc);
-static int fromiccpcs(int cs);
-
-/******************************************************************************\
-* Functions.
-\******************************************************************************/
-
-jas_image_t *jp2_decode(jas_stream_t *in, char *optstr)
-{
-	jp2_box_t *box;
-	int found;
-	jas_image_t *image;
-	jp2_dec_t *dec;
-	bool samedtype;
-	int dtype;
-	int i;
-	jp2_cmap_t *cmapd;
-	jp2_pclr_t *pclrd;
-	jp2_cdef_t *cdefd;
-	int channo;
-	int newcmptno;
-	int_fast32_t *lutents;
-	jp2_cmapent_t *cmapent;
-	unsigned char *iccp;
-	int cs;
-
-	dec = 0;
-	box = 0;
-	image = 0;
-
-	if (!(dec = jp2_dec_create())) {
-		goto error;
-	}
-
-	/* Get the first box.  This should be a JP box. */
-	if (!(box = jp2_box_get(in))) {
-		jas_eprintf("error: cannot get box\n");
-		goto error;
-	}
-	if (box->type != JP2_BOX_JP) {
-		jas_eprintf("error: expecting signature box\n");
-		goto error;
-	}
-	if (box->data.jp.magic != JP2_JP_MAGIC) {
-		jas_eprintf("incorrect magic number\n");
-		goto error;
-	}
-	jp2_box_destroy(box);
-	box = 0;
-
-	/* Get the second box.  This should be a FTYP box. */
-	if (!(box = jp2_box_get(in))) {
-		goto error;
-	}
-	if (box->type != JP2_BOX_FTYP) {
-		jas_eprintf("expecting file type box\n");
-		goto error;
-	}
-	jp2_box_destroy(box);
-	box = 0;
-
-	/* Get more boxes... */
-	found = 0;
-	while ((box = jp2_box_get(in))) {
-		if (jas_getdbglevel() >= 1) {
-			fprintf(stderr, "box type %s\n", box->info->name);
-		}
-		switch (box->type) {
-		case JP2_BOX_JP2C:
-			found = 1;
-			break;
-		case JP2_BOX_IHDR:
-			if (!dec->ihdr) {
-				dec->ihdr = box;
-				box = 0;
-			}
-			break;
-		case JP2_BOX_BPCC:
-			if (!dec->bpcc) {
-				dec->bpcc = box;
-				box = 0;
-			}
-			break;
-		case JP2_BOX_CDEF:
-			if (!dec->cdef) {
-				dec->cdef = box;
-				box = 0;
-			}
-			break;
-		case JP2_BOX_PCLR:
-			if (!dec->pclr) {
-				dec->pclr = box;
-				box = 0;
-			}
-			break;
-		case JP2_BOX_CMAP:
-			if (!dec->cmap) {
-				dec->cmap = box;
-				box = 0;
-			}
-			break;
-		case JP2_BOX_COLR:
-			if (!dec->colr) {
-				dec->colr = box;
-				box = 0;
-			}
-			break;
-		}
-		if (box) {
-			jp2_box_destroy(box);
-			box = 0;
-		}
-		if (found) {
-			break;
-		}
-	}
-
-	if (!found) {
-		jas_eprintf("error: no code stream found\n");
-		goto error;
-	}
-
-	if (!(dec->image = jpc_decode(in, optstr))) {
-		jas_eprintf("error: cannot decode code stream\n");
-		goto error;
-	}
-
-	/* An IHDR box must be present. */
-	if (!dec->ihdr) {
-		jas_eprintf("error: missing IHDR box\n");
-		goto error;
-	}
-
-	/* Does the number of components indicated in the IHDR box match
-	  the value specified in the code stream? */
-	if (dec->ihdr->data.ihdr.numcmpts != jas_image_numcmpts(dec->image)) {
-		jas_eprintf("warning: number of components mismatch\n");
-	}
-
-	/* At least one component must be present. */
-	if (!jas_image_numcmpts(dec->image)) {
-		jas_eprintf("error: no components\n");
-		goto error;
-	}
-
-	/* Determine if all components have the same data type. */
-	samedtype = true;
-	dtype = jas_image_cmptdtype(dec->image, 0);
-	for (i = 1; i < jas_image_numcmpts(dec->image); ++i) {
-		if (jas_image_cmptdtype(dec->image, i) != dtype) {
-			samedtype = false;
-			break;
-		}
-	}
-
-	/* Is the component data type indicated in the IHDR box consistent
-	  with the data in the code stream? */
-	if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) ||
-	  (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) {
-		jas_eprintf("warning: component data type mismatch\n");
-	}
-
-	/* Can we handle the compression type? */
-	if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) {
-		jas_eprintf("error: not capable of this compression type\n");
-		goto error;
-	}
-
-	if (dec->bpcc) {
-		/* Is the number of components indicated in the BPCC box
-		  consistent with the code stream data? */
-		if (dec->bpcc->data.bpcc.numcmpts != jas_image_numcmpts(
-		  dec->image)) {
-			jas_eprintf("warning: number of components mismatch\n");
-		}
-		/* Is the component data type information indicated in the BPCC
-		  box consistent with the code stream data? */
-		if (!samedtype) {
-			for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
-				if (jas_image_cmptdtype(dec->image, i) != JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) {
-					jas_eprintf("warning: component data type mismatch\n");
-				}
-			}
-		} else {
-			jas_eprintf("warning: superfluous BPCC box\n");
-		}
-	}
-
-	/* A COLR box must be present. */
-	if (!dec->colr) {
-		jas_eprintf("error: no COLR box\n");
-		goto error;
-	}
-
-	switch (dec->colr->data.colr.method) {
-	case JP2_COLR_ENUM:
-		jas_image_setcolorspace(dec->image, jp2_getcs(&dec->colr->data.colr));
-		break;
-	case JP2_COLR_ICC:
-		if (dec->colr->data.colr.iccplen < 128) {
-			abort();
-		}
-		iccp = dec->colr->data.colr.iccp;
-		cs = (iccp[16] << 24) | (iccp[17] << 16) | (iccp[18] << 8) |
-		  iccp[19];
-        if (jas_getdbglevel() > 1)
-            jas_eprintf("ICC Profile CS %08x\n", cs);
-		jas_image_setcolorspace(dec->image, fromiccpcs(cs));
-		break;
-	}
-
-	/* If a CMAP box is present, a PCLR box must also be present. */
-	if (dec->cmap && !dec->pclr) {
-		jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n");
-		jp2_box_destroy(dec->cmap);
-		dec->cmap = 0;
-	}
-
-	/* If a CMAP box is not present, a PCLR box must not be present. */
-	if (!dec->cmap && dec->pclr) {
-		jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n");
-		jp2_box_destroy(dec->pclr);
-		dec->pclr = 0;
-	}
-
-	/* Determine the number of channels (which is essentially the number
-	  of components after any palette mappings have been applied). */
-	dec->numchans = dec->cmap ? dec->cmap->data.cmap.numchans : jas_image_numcmpts(dec->image);
-
-	/* Perform a basic sanity check on the CMAP box if present. */
-	if (dec->cmap) {
-		for (i = 0; i < dec->numchans; ++i) {
-			/* Is the component number reasonable? */
-			if (dec->cmap->data.cmap.ents[i].cmptno >= jas_image_numcmpts(dec->image)) {
-				jas_eprintf("error: invalid component number in CMAP box\n");
-				goto error;
-			}
-			/* Is the LUT index reasonable? */
-			if (dec->cmap->data.cmap.ents[i].pcol >= dec->pclr->data.pclr.numchans) {
-				jas_eprintf("error: invalid CMAP LUT index\n");
-				goto error;
-			}
-		}
-	}
-
-	/* Allocate space for the channel-number to component-number LUT. */
-	if (!(dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t)))) {
-		jas_eprintf("error: no memory\n");
-		goto error;
-	}
-
-	if (!dec->cmap) {
-		for (i = 0; i < dec->numchans; ++i) {
-			dec->chantocmptlut[i] = i;
-		}
-	} else {
-		cmapd = &dec->cmap->data.cmap;
-		pclrd = &dec->pclr->data.pclr;
-		cdefd = &dec->cdef->data.cdef;
-		for (channo = 0; channo < cmapd->numchans; ++channo) {
-			cmapent = &cmapd->ents[channo];
-			if (cmapent->map == JP2_CMAP_DIRECT) {
-				dec->chantocmptlut[channo] = channo;
-			} else if (cmapent->map == JP2_CMAP_PALETTE) {
-				lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t));
-				for (i = 0; i < pclrd->numlutents; ++i) {
-					lutents[i] = pclrd->lutdata[cmapent->pcol + i * pclrd->numchans];
-				}
-				newcmptno = jas_image_numcmpts(dec->image);
-				jas_image_depalettize(dec->image, cmapent->cmptno, pclrd->numlutents, lutents, JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno);
-				dec->chantocmptlut[channo] = newcmptno;
-				jas_free(lutents);
-#if 0
-				if (dec->cdef) {
-					cdefent = jp2_cdef_lookup(cdefd, channo);
-					if (!cdefent) {
-						abort();
-					}
-				jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_colorspace(dec->image), cdefent->type, cdefent->assoc));
-				} else {
-				jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_colorspace(dec->image), 0, channo + 1));
-				}
-#endif
-			}
-		}
-	}
-
-	/* Mark all components as being of unknown type. */
-
-	for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
-		jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN);
-	}
-
-	/* Determine the type of each component. */
-	if (dec->cdef) {
-		for (i = 0; i < dec->numchans; ++i) {
-			jas_image_setcmpttype(dec->image,
-			  dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo],
-			  jp2_getct(jas_image_colorspace(dec->image),
-			  dec->cdef->data.cdef.ents[i].type, dec->cdef->data.cdef.ents[i].assoc));
-		}
-	} else {
-		for (i = 0; i < dec->numchans; ++i) {
-			jas_image_setcmpttype(dec->image, dec->chantocmptlut[i],
-			  jp2_getct(jas_image_colorspace(dec->image), 0, i + 1));
-		}
-	}
-
-	/* Delete any components that are not of interest. */
-	for (i = jas_image_numcmpts(dec->image) - 1; i >= 0; --i) {
-		if (jas_image_cmpttype(dec->image, i) == JAS_IMAGE_CT_UNKNOWN) {
-			jas_image_delcmpt(dec->image, i);
-		}
-	}
-
-	/* Ensure that some components survived. */
-	if (!jas_image_numcmpts(dec->image)) {
-		jas_eprintf("error: no components\n");
-		goto error;
-	}
-
-	/* Prevent the image from being destroyed later. */
-	image = dec->image;
-	dec->image = 0;
-
-	jp2_dec_destroy(dec);
-
-	return image;
+#define JP2_VALIDATELEN (JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK))
 
-error:
-	if (box) {
-		jp2_box_destroy(box);
-	}
-	if (dec) {
-		jp2_dec_destroy(dec);
-	}
-	return 0;
+
+
+static jp2_dec_t *
+jp2_dec_create(void) {
+
+    jp2_dec_t *dec;
+
+    dec = jas_malloc(sizeof(jp2_dec_t));
+    if (dec) {
+        dec->ihdr = 0;
+        dec->bpcc = 0;
+        dec->cdef = 0;
+        dec->pclr = 0;
+        dec->image = 0;
+        dec->chantocmptlut = 0;
+        dec->cmap = 0;
+        dec->colr = 0;
+    }
+    return dec;
 }
 
-int jp2_validate(jas_stream_t *in)
-{
-	char buf[JP2_VALIDATELEN];
-	int i;
-	int n;
-
-	assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN);
-
-	/* Read the validation data (i.e., the data used for detecting
-	  the format). */
-	if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) {
-		return -1;
-	}
-
-	/* Put the validation data back onto the stream, so that the
-	  stream position will not be changed. */
-	for (i = n - 1; i >= 0; --i) {
-		if (jas_stream_ungetc(in, buf[i]) == EOF) {
-			return -1;
-		}
-	}
-
-	/* Did we read enough data? */
-	if (n < JP2_VALIDATELEN) {
-		return -1;
-	}
-
-	/* Is the box type correct? */
-	if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) !=
-	  JP2_BOX_JP)
-	{
-		return -1;
-	}
-
-	return 0;
+
+
+static void
+jp2_dec_destroy(jp2_dec_t *dec) {
+
+    if (dec->ihdr)
+        jp2_box_destroy(dec->ihdr);
+
+    if (dec->bpcc)
+        jp2_box_destroy(dec->bpcc);
+
+    if (dec->cdef)
+        jp2_box_destroy(dec->cdef);
+
+    if (dec->pclr)
+        jp2_box_destroy(dec->pclr);
+
+    if (dec->image)
+        jas_image_destroy(dec->image);
+
+    if (dec->cmap)
+        jp2_box_destroy(dec->cmap);
+
+    if (dec->colr)
+        jp2_box_destroy(dec->colr);
+
+    if (dec->chantocmptlut)
+        jas_free(dec->chantocmptlut);
+
+    jas_free(dec);
 }
 
-static jp2_dec_t *jp2_dec_create(void)
-{
-	jp2_dec_t *dec;
-
-	if (!(dec = jas_malloc(sizeof(jp2_dec_t)))) {
-		return 0;
-	}
-	dec->ihdr = 0;
-	dec->bpcc = 0;
-	dec->cdef = 0;
-	dec->pclr = 0;
-	dec->image = 0;
-	dec->chantocmptlut = 0;
-	dec->cmap = 0;
-	dec->colr = 0;
-	return dec;
+
+
+static int
+jp2_getct(int colorspace, int type, int assoc) {
+
+    if (type == 1 && assoc == 0)
+        return JAS_IMAGE_CT_OPACITY;
+
+    if (type == 0 && assoc >= 1 && assoc <= 65534) {
+        switch (colorspace) {
+        case JAS_IMAGE_CS_RGB:
+            switch (assoc) {
+            case JP2_CDEF_RGB_R:
+                return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R);
+                break;
+            case JP2_CDEF_RGB_G:
+                return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G);
+                break;
+            case JP2_CDEF_RGB_B:
+                return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B);
+                break;
+            }
+            break;
+        case JAS_IMAGE_CS_YCBCR:
+            switch (assoc) {
+            case JP2_CDEF_YCBCR_Y:
+                return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_Y);
+                break;
+            case JP2_CDEF_YCBCR_CB:
+                return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CB);
+                break;
+            case JP2_CDEF_YCBCR_CR:
+                return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CR);
+                break;
+            }
+            break;
+        case JAS_IMAGE_CS_GRAY:
+            switch (assoc) {
+            case JP2_CDEF_GRAY_Y:
+                return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y);
+                break;
+            }
+            break;
+#if 0
+        case JAS_IMAGE_CS_ICC:
+#endif
+        default:
+            return JAS_IMAGE_CT_COLOR(assoc - 1);
+            break;
+        }
+    }
+    return JAS_IMAGE_CT_UNKNOWN;
 }
 
-static void jp2_dec_destroy(jp2_dec_t *dec)
-{
-	if (dec->ihdr) {
-		jp2_box_destroy(dec->ihdr);
-	}
-	if (dec->bpcc) {
-		jp2_box_destroy(dec->bpcc);
-	}
-	if (dec->cdef) {
-		jp2_box_destroy(dec->cdef);
-	}
-	if (dec->pclr) {
-		jp2_box_destroy(dec->pclr);
-	}
-	if (dec->image) {
-		jas_image_destroy(dec->image);
-	}
-	if (dec->cmap) {
-		jp2_box_destroy(dec->cmap);
-	}
-	if (dec->colr) {
-		jp2_box_destroy(dec->colr);
-	}
-	if (dec->chantocmptlut) {
-		jas_free(dec->chantocmptlut);
-	}
-	jas_free(dec);
+
+
+static int
+jp2_getcs(jp2_colr_t *colr) {
+
+    if (colr->method == JP2_COLR_ENUM) {
+        switch (colr->csid) {
+        case JP2_COLR_SRGB:
+            return JAS_IMAGE_CS_RGB;
+            break;
+        case JP2_COLR_SYCC:
+            return JAS_IMAGE_CS_YCBCR;
+            break;
+        case JP2_COLR_SGRAY:
+            return JAS_IMAGE_CS_GRAY;
+            break;
+        }
+    }
+    return JAS_IMAGE_CS_UNKNOWN;
 }
 
 
 
+static int
+fromiccpcs(int cs) {
+    
+    switch (cs) {
+    case ICC_CS_RGB:
+        return JAS_IMAGE_CS_RGB;
+        break;
+    case ICC_CS_YCBCR:
+        return JAS_IMAGE_CS_YCBCR;
+        break;
+    case ICC_CS_GRAY:
+        return JAS_IMAGE_CS_GRAY;
+        break;
+    }
+    return JAS_IMAGE_CS_UNKNOWN;
+}
 
 
 
-static int jp2_getct(int colorspace, int type, int assoc)
-{
-	if (type == 1 && assoc == 0) {
-		return JAS_IMAGE_CT_OPACITY;
-	}
-	if (type == 0 && assoc >= 1 && assoc <= 65534) {
-		switch (colorspace) {
-		case JAS_IMAGE_CS_RGB:
-			switch (assoc) {
-			case JP2_CDEF_RGB_R:
-				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R);
-				break;
-			case JP2_CDEF_RGB_G:
-				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G);
-				break;
-			case JP2_CDEF_RGB_B:
-				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B);
-				break;
-			}
-			break;
-		case JAS_IMAGE_CS_YCBCR:
-			switch (assoc) {
-			case JP2_CDEF_YCBCR_Y:
-				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_Y);
-				break;
-			case JP2_CDEF_YCBCR_CB:
-				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CB);
-				break;
-			case JP2_CDEF_YCBCR_CR:
-				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CR);
-				break;
-			}
-			break;
-		case JAS_IMAGE_CS_GRAY:
-			switch (assoc) {
-			case JP2_CDEF_GRAY_Y:
-				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y);
-				break;
-			}
-			break;
+jas_image_t *
+jp2_decode(jas_stream_t *in, char *optstr) {
+
+    jp2_box_t *box;
+    int found;
+    jas_image_t *image;
+    jp2_dec_t *dec;
+    bool samedtype;
+    int dtype;
+    int i;
+    jp2_cmap_t *cmapd;
+    jp2_pclr_t *pclrd;
+    int channo;
+    int newcmptno;
+    int_fast32_t *lutents;
+    jp2_cmapent_t *cmapent;
+    unsigned char *iccp;
+    int cs;
+
+    dec = 0;
+    box = 0;
+    image = 0;
+
+    if (!(dec = jp2_dec_create())) {
+        goto error;
+    }
+
+    /* Get the first box.  This should be a JP box. */
+    if (!(box = jp2_box_get(in))) {
+        jas_eprintf("error: cannot get box\n");
+        goto error;
+    }
+    if (box->type != JP2_BOX_JP) {
+        jas_eprintf("error: expecting signature box\n");
+        goto error;
+    }
+    if (box->data.jp.magic != JP2_JP_MAGIC) {
+        jas_eprintf("incorrect magic number\n");
+        goto error;
+    }
+    jp2_box_destroy(box);
+    box = 0;
+
+    /* Get the second box.  This should be a FTYP box. */
+    if (!(box = jp2_box_get(in))) {
+        goto error;
+    }
+    if (box->type != JP2_BOX_FTYP) {
+        jas_eprintf("expecting file type box\n");
+        goto error;
+    }
+    jp2_box_destroy(box);
+    box = 0;
+
+    /* Get more boxes... */
+    found = 0;
+    while ((box = jp2_box_get(in))) {
+        if (jas_getdbglevel() >= 1) {
+            fprintf(stderr, "box type %s\n", box->info->name);
+        }
+        switch (box->type) {
+        case JP2_BOX_JP2C:
+            found = 1;
+            break;
+        case JP2_BOX_IHDR:
+            if (!dec->ihdr) {
+                dec->ihdr = box;
+                box = 0;
+            }
+            break;
+        case JP2_BOX_BPCC:
+            if (!dec->bpcc) {
+                dec->bpcc = box;
+                box = 0;
+            }
+            break;
+        case JP2_BOX_CDEF:
+            if (!dec->cdef) {
+                dec->cdef = box;
+                box = 0;
+            }
+            break;
+        case JP2_BOX_PCLR:
+            if (!dec->pclr) {
+                dec->pclr = box;
+                box = 0;
+            }
+            break;
+        case JP2_BOX_CMAP:
+            if (!dec->cmap) {
+                dec->cmap = box;
+                box = 0;
+            }
+            break;
+        case JP2_BOX_COLR:
+            if (!dec->colr) {
+                dec->colr = box;
+                box = 0;
+            }
+            break;
+        }
+        if (box) {
+            jp2_box_destroy(box);
+            box = 0;
+        }
+        if (found) {
+            break;
+        }
+    }
+
+    if (!found) {
+        jas_eprintf("error: no code stream found\n");
+        goto error;
+    }
+
+    if (!(dec->image = jpc_decode(in, optstr))) {
+        jas_eprintf("error: cannot decode code stream\n");
+        goto error;
+    }
+
+    /* An IHDR box must be present. */
+    if (!dec->ihdr) {
+        jas_eprintf("error: missing IHDR box\n");
+        goto error;
+    }
+
+    /* Does the number of components indicated in the IHDR box match
+      the value specified in the code stream? */
+    if (dec->ihdr->data.ihdr.numcmpts != jas_image_numcmpts(dec->image)) {
+        jas_eprintf("warning: number of components mismatch\n");
+    }
+
+    /* At least one component must be present. */
+    if (!jas_image_numcmpts(dec->image)) {
+        jas_eprintf("error: no components\n");
+        goto error;
+    }
+
+    /* Determine if all components have the same data type. */
+    samedtype = true;
+    dtype = jas_image_cmptdtype(dec->image, 0);
+    for (i = 1; i < jas_image_numcmpts(dec->image); ++i) {
+        if (jas_image_cmptdtype(dec->image, i) != dtype) {
+            samedtype = false;
+            break;
+        }
+    }
+
+    /* Is the component data type indicated in the IHDR box consistent
+      with the data in the code stream? */
+    if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) ||
+      (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) {
+        jas_eprintf("warning: component data type mismatch\n");
+    }
+
+    /* Can we handle the compression type? */
+    if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) {
+        jas_eprintf("error: not capable of this compression type\n");
+        goto error;
+    }
+
+    if (dec->bpcc) {
+        /* Is the number of components indicated in the BPCC box
+          consistent with the code stream data? */
+        if (dec->bpcc->data.bpcc.numcmpts != jas_image_numcmpts(
+          dec->image)) {
+            jas_eprintf("warning: number of components mismatch\n");
+        }
+        /* Is the component data type information indicated in the BPCC
+          box consistent with the code stream data? */
+        if (!samedtype) {
+            for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
+                if (jas_image_cmptdtype(dec->image, i) !=
+                    JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) {
+                    jas_eprintf("warning: component data type mismatch\n");
+                }
+            }
+        } else {
+            jas_eprintf("warning: superfluous BPCC box\n");
+        }
+    }
+
+    /* A COLR box must be present. */
+    if (!dec->colr) {
+        jas_eprintf("error: no COLR box\n");
+        goto error;
+    }
+
+    switch (dec->colr->data.colr.method) {
+    case JP2_COLR_ENUM:
+        jas_image_setcolorspace(dec->image, jp2_getcs(&dec->colr->data.colr));
+        break;
+    case JP2_COLR_ICC:
+        if (dec->colr->data.colr.iccplen < 128) {
+            abort();
+        }
+        iccp = dec->colr->data.colr.iccp;
+        cs = (iccp[16] << 24) | (iccp[17] << 16) | (iccp[18] << 8) |
+          iccp[19];
+        if (jas_getdbglevel() > 1)
+            jas_eprintf("ICC Profile CS %08x\n", cs);
+        jas_image_setcolorspace(dec->image, fromiccpcs(cs));
+        break;
+    }
+
+    /* If a CMAP box is present, a PCLR box must also be present. */
+    if (dec->cmap && !dec->pclr) {
+        jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n");
+        jp2_box_destroy(dec->cmap);
+        dec->cmap = 0;
+    }
+
+    /* If a CMAP box is not present, a PCLR box must not be present. */
+    if (!dec->cmap && dec->pclr) {
+        jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n");
+        jp2_box_destroy(dec->pclr);
+        dec->pclr = 0;
+    }
+
+    /* Determine the number of channels (which is essentially the number
+      of components after any palette mappings have been applied). */
+    dec->numchans = dec->cmap ?
+        dec->cmap->data.cmap.numchans : 
+        jas_image_numcmpts(dec->image);
+
+    /* Perform a basic sanity check on the CMAP box if present. */
+    if (dec->cmap) {
+        for (i = 0; i < dec->numchans; ++i) {
+            /* Is the component number reasonable? */
+            if (dec->cmap->data.cmap.ents[i].cmptno >=
+                jas_image_numcmpts(dec->image)) {
+                jas_eprintf("error: invalid component number in CMAP box\n");
+                goto error;
+            }
+            /* Is the LUT index reasonable? */
+            if (dec->cmap->data.cmap.ents[i].pcol >=
+                dec->pclr->data.pclr.numchans) {
+                jas_eprintf("error: invalid CMAP LUT index\n");
+                goto error;
+            }
+        }
+    }
+
+    /* Allocate space for the channel-number to component-number LUT. */
+    dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t));
+    if (!dec->chantocmptlut) {
+        jas_eprintf("error: no memory\n");
+        goto error;
+    }
+
+    if (!dec->cmap) {
+        for (i = 0; i < dec->numchans; ++i) {
+            dec->chantocmptlut[i] = i;
+        }
+    } else {
+        cmapd = &dec->cmap->data.cmap;
+        pclrd = &dec->pclr->data.pclr;
+        for (channo = 0; channo < cmapd->numchans; ++channo) {
+            cmapent = &cmapd->ents[channo];
+            if (cmapent->map == JP2_CMAP_DIRECT) {
+                dec->chantocmptlut[channo] = channo;
+            } else if (cmapent->map == JP2_CMAP_PALETTE) {
+                lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t));
+                for (i = 0; i < pclrd->numlutents; ++i) {
+                    lutents[i] =
+                        pclrd->lutdata[cmapent->pcol + i * pclrd->numchans];
+                }
+                newcmptno = jas_image_numcmpts(dec->image);
+                jas_image_depalettize(
+                    dec->image, cmapent->cmptno, pclrd->numlutents, lutents,
+                    JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno);
+                dec->chantocmptlut[channo] = newcmptno;
+                jas_free(lutents);
 #if 0
-		case JAS_IMAGE_CS_ICC:
+                if (dec->cdef) {
+                    jp2_cdef_t * const cdefd = &dec->cdef->data.cdef;
+                    cdefent = jp2_cdef_lookup(cdefd, channo);
+                    if (!cdefent) {
+                        abort();
+                    }
+                jas_image_setcmpttype(
+                    dec->image, newcmptno,
+                    jp2_getct(jas_image_colorspace(dec->image),
+                              cdefent->type, cdefent->assoc));
+                } else {
+                    jas_image_setcmpttype(
+                        dec->image, newcmptno,
+                        jp2_getct(jas_image_colorspace(dec->image),
+                                  0,
+                                  channo + 1));
+                }
 #endif
-		default:
-			return JAS_IMAGE_CT_COLOR(assoc - 1);
-			break;
-		}
-	}
-	return JAS_IMAGE_CT_UNKNOWN;
-}
+            }
+        }
+    }
+
+    /* Mark all components as being of unknown type. */
+
+    for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
+        jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN);
+    }
+
+    /* Determine the type of each component. */
+    if (dec->cdef) {
+        unsigned int i;
+        for (i = 0; i < dec->numchans; ++i) {
+            jas_image_setcmpttype(
+                dec->image,
+                dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo],
+                jp2_getct(jas_image_colorspace(dec->image),
+                          dec->cdef->data.cdef.ents[i].type,
+                          dec->cdef->data.cdef.ents[i].assoc));
+        }
+    } else {
+        unsigned int i;
+        for (i = 0; i < dec->numchans; ++i) {
+            jas_image_setcmpttype(dec->image, dec->chantocmptlut[i],
+              jp2_getct(jas_image_colorspace(dec->image), 0, i + 1));
+        }
+    }
+
+    /* Delete any components that are not of interest. */
+    for (i = jas_image_numcmpts(dec->image) - 1; i >= 0; --i) {
+        if (jas_image_cmpttype(dec->image, i) == JAS_IMAGE_CT_UNKNOWN) {
+            jas_image_delcmpt(dec->image, i);
+        }
+    }
+
+    /* Ensure that some components survived. */
+    if (!jas_image_numcmpts(dec->image)) {
+        jas_eprintf("error: no components\n");
+        goto error;
+    }
+
+    /* Prevent the image from being destroyed later. */
+    image = dec->image;
+    dec->image = 0;
+
+    jp2_dec_destroy(dec);
+
+    return image;
 
-static int jp2_getcs(jp2_colr_t *colr)
-{
-	if (colr->method == JP2_COLR_ENUM) {
-		switch (colr->csid) {
-		case JP2_COLR_SRGB:
-			return JAS_IMAGE_CS_RGB;
-			break;
-		case JP2_COLR_SYCC:
-			return JAS_IMAGE_CS_YCBCR;
-			break;
-		case JP2_COLR_SGRAY:
-			return JAS_IMAGE_CS_GRAY;
-			break;
-		}
-	}
-	return JAS_IMAGE_CS_UNKNOWN;
+error:
+    if (box) {
+        jp2_box_destroy(box);
+    }
+    if (dec) {
+        jp2_dec_destroy(dec);
+    }
+    return 0;
 }
 
-static int fromiccpcs(int cs)
-{
-	switch (cs) {
-	case ICC_CS_RGB:
-		return JAS_IMAGE_CS_RGB;
-		break;
-	case ICC_CS_YCBCR:
-		return JAS_IMAGE_CS_YCBCR;
-		break;
-	case ICC_CS_GRAY:
-		return JAS_IMAGE_CS_GRAY;
-		break;
-	}
-	return JAS_IMAGE_CS_UNKNOWN;
+
+
+int
+jp2_validate(jas_stream_t *in) {
+
+    char buf[JP2_VALIDATELEN];
+    int i;
+    int n;
+
+    assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN);
+
+    /* Read the validation data (i.e., the data used for detecting
+      the format). */
+    if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) {
+        return -1;
+    }
+
+    /* Put the validation data back onto the stream, so that the
+      stream position will not be changed. */
+    for (i = n - 1; i >= 0; --i) {
+        if (jas_stream_ungetc(in, buf[i]) == EOF) {
+            return -1;
+        }
+    }
+
+    /* Did we read enough data? */
+    if (n < JP2_VALIDATELEN) {
+        return -1;
+    }
+
+    /* Is the box type correct? */
+    if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) !=
+      JP2_BOX_JP)
+    {
+        return -1;
+    }
+
+    return 0;
 }
+
+
+
diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c b/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c
index 72bd0126..cada97f8 100644
--- a/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c
+++ b/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c
@@ -458,6 +458,12 @@ static int jpc_dec_decode(jpc_dec_t *dec)
 
 static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms)
 {
+    /* Ignore the information in the CRG marker segment for now.
+       This information serves no useful purpose for decoding anyhow.
+       Some other parts of the code need to be changed if these lines
+       are enabled.
+    */
+#ifdef USING_CRG
     uint_fast16_t cmptno;
     jpc_dec_cmpt_t *cmpt;
     jpc_crg_t *crg;
@@ -465,14 +471,10 @@ static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms)
     crg = &ms->parms.crg;
     for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno,
       ++cmpt) {
-        /* Ignore the information in the CRG marker segment for now.
-          This information serves no useful purpose for decoding anyhow.
-          Some other parts of the code need to be changed if these lines
-          are uncommented.
         cmpt->hsubstep = crg->comps[cmptno].hoff;
         cmpt->vsubstep = crg->comps[cmptno].voff;
-        */
     }
+#endif
     return 0;
 }
 
@@ -701,7 +703,6 @@ static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile)
     uint_fast32_t tlcbgxstart;
     uint_fast32_t tlcbgystart;
     uint_fast32_t brcbgxend;
-    uint_fast32_t brcbgyend;
     uint_fast32_t cbgxstart;
     uint_fast32_t cbgystart;
     uint_fast32_t cbgxend;
@@ -792,14 +793,12 @@ rlvl->bands = 0;
                 tlcbgxstart = tlprcxstart;
                 tlcbgystart = tlprcystart;
                 brcbgxend = brprcxend;
-                brcbgyend = brprcyend;
                 rlvl->cbgwidthexpn = rlvl->prcwidthexpn;
                 rlvl->cbgheightexpn = rlvl->prcheightexpn;
             } else {
                 tlcbgxstart = JPC_CEILDIVPOW2(tlprcxstart, 1);
                 tlcbgystart = JPC_CEILDIVPOW2(tlprcystart, 1);
                 brcbgxend = JPC_CEILDIVPOW2(brprcxend, 1);
-                brcbgyend = JPC_CEILDIVPOW2(brprcyend, 1);
                 rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
                 rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
             }
diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c
index d17e9aa3..9db41ca2 100644
--- a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c
+++ b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c
@@ -73,9 +73,9 @@ int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size);
 static void pass_destroy(jpc_enc_pass_t *pass);
 void jpc_enc_dump(jpc_enc_t *enc);
 
-/******************************************************************************\
+/*****************************************************************************\
 * Local prototypes.
-\******************************************************************************/
+\*****************************************************************************/
 
 void quantize(jas_matrix_t *data, jpc_fix_t stepsize);
 static int jpc_enc_encodemainhdr(jpc_enc_t *enc);
@@ -90,8 +90,11 @@ int setins(int numvalues, jpc_flt_t *values, jpc_flt_t value);
 static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image);
 void jpc_enc_cp_destroy(jpc_enc_cp_t *cp);
 
-static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn)
-{
+
+
+static uint_fast32_t
+jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn) {
+
     int p;
     uint_fast32_t mant;
     uint_fast32_t expn;
@@ -111,6 +114,8 @@ static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn)
     return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant);
 }
 
+
+
 typedef enum {
     OPT_DEBUG,
     OPT_IMGAREAOFFX,
@@ -199,6 +204,7 @@ jas_taginfo_t modetab[] = {
 };
 
 
+
 static void
 tracev(const char * const fmt,
        va_list            args) {
@@ -224,12 +230,9 @@ trace(const char * const fmt, ...) {
 
 
 
-/******************************************************************************\
-* The main encoder entry point.
-\******************************************************************************/
+int
+jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) {
 
-int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
-{
     jpc_enc_t *enc;
     jpc_enc_cp_t *cp;
 
@@ -287,12 +290,15 @@ error:
     return -1;
 }
 
-/******************************************************************************\
+
+
+/*****************************************************************************\
 * Option parsing code.
-\******************************************************************************/
+\*****************************************************************************/
+
+static jpc_enc_cp_t *
+cp_create(char *optstr, jas_image_t *image) {
 
-static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
-{
     jpc_enc_cp_t *cp;
     jas_tvparser_t *tvp;
     int ret;
@@ -343,10 +349,12 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
     hsteplcm = 1;
     vsteplcm = 1;
     for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
-        if (jas_image_cmptbrx(image, cmptno) + jas_image_cmpthstep(image, cmptno) <=
+        if (jas_image_cmptbrx(image, cmptno) +
+            jas_image_cmpthstep(image, cmptno) <=
           jas_image_brx(image) || jas_image_cmptbry(image, cmptno) +
           jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) {
-            fprintf(stderr, "We don't know how to interpret this image type\n");
+            fprintf(stderr,
+                    "We don't know how to interpret this image type\n");
             goto error;
         }
         /* Note: We ought to be calculating the LCMs here.  Fix some day. */
@@ -495,7 +503,8 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
             if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates,
               &ilyrrates)) {
                 fprintf(stderr,
-                  "warning: invalid intermediate layer rates specifier ignored (%s)\n",
+                        "warning: invalid intermediate layer rates specifier "
+                        "ignored (%s)\n",
                   jas_tvparser_getval(tvp));
             }
             break;
@@ -552,20 +561,26 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
     if (cp->numcmpts == 3) {
         mctvalid = true;
         for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
-            if (jas_image_cmptprec(image, cmptno) != jas_image_cmptprec(image, 0) ||
-              jas_image_cmptsgnd(image, cmptno) != jas_image_cmptsgnd(image, 0) ||
-              jas_image_cmptwidth(image, cmptno) != jas_image_cmptwidth(image, 0) ||
-              jas_image_cmptheight(image, cmptno) != jas_image_cmptheight(image, 0)) {
+            if (jas_image_cmptprec(image, cmptno) !=
+                jas_image_cmptprec(image, 0) ||
+              jas_image_cmptsgnd(image, cmptno) !=
+                jas_image_cmptsgnd(image, 0) ||
+              jas_image_cmptwidth(image, cmptno) !=
+                jas_image_cmptwidth(image, 0) ||
+              jas_image_cmptheight(image, cmptno) !=
+                jas_image_cmptheight(image, 0)) {
                 mctvalid = false;
             }
         }
     } else {
         mctvalid = false;
     }
-    if (mctvalid && enablemct && jas_image_colorspace(image) != JAS_IMAGE_CS_RGB) {
+    if (mctvalid && enablemct && jas_image_colorspace(image) !=
+        JAS_IMAGE_CS_RGB) {
         fprintf(stderr, "warning: color model apparently not RGB\n");
     }
-    if (mctvalid && enablemct && jas_image_colorspace(image) == JAS_IMAGE_CS_RGB) {
+    if (mctvalid && enablemct && jas_image_colorspace(image) ==
+        JAS_IMAGE_CS_RGB) {
         tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT);
     } else {
         tcp->mctid = JPC_MCT_NONE;
@@ -682,16 +697,20 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
         /* The intermediate layers rates must increase monotonically. */
         for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) {
             if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) {
-                fprintf(stderr, "intermediate layer rates must increase monotonically\n");
+                fprintf(stderr,
+                        "intermediate layer rates must increase "
+                        "monotonically\n");
                 goto error;
             }
         }
         /* The intermediate layer rates must be less than the overall rate. */
         if (cp->totalsize != UINT_FAST32_MAX) {
             for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) {
-                if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) > ((double) cp->totalsize)
-                  / cp->rawsize) {
-                    fprintf(stderr, "warning: intermediate layer rates must be less than overall rate\n");
+                if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) >
+                    ((double) cp->totalsize) / cp->rawsize) {
+                    fprintf(stderr,
+                            "warning: intermediate layer rates must be "
+                            "less than overall rate\n");
                     goto error;
                 }
             }
@@ -718,8 +737,11 @@ error:
     return 0;
 }
 
-void jpc_enc_cp_destroy(jpc_enc_cp_t *cp)
-{
+
+
+void
+jpc_enc_cp_destroy(jpc_enc_cp_t *cp) {
+
     if (cp->ccps) {
         if (cp->tcp.ilyrrates) {
             jas_free(cp->tcp.ilyrrates);
@@ -729,8 +751,11 @@ void jpc_enc_cp_destroy(jpc_enc_cp_t *cp)
     jas_free(cp);
 }
 
-int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size)
-{
+
+
+int
+ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size) {
+
     char *cp;
     jpc_flt_t f;
 
@@ -750,12 +775,13 @@ int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size)
     return 0;
 }
 
-/******************************************************************************\
+/*****************************************************************************\
 * Encoder constructor and destructor.
-\******************************************************************************/
+\*****************************************************************************/
+
+jpc_enc_t *
+jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) {
 
-jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image)
-{
     jpc_enc_t *enc;
 
     enc = 0;
@@ -788,11 +814,15 @@ error:
     return 0;
 }
 
-void jpc_enc_destroy(jpc_enc_t *enc)
-{
+
+
+void
+jpc_enc_destroy(jpc_enc_t *enc) {
+
     /* The image object (i.e., enc->image) and output stream object
-    (i.e., enc->out) are created outside of the encoder.
-    Therefore, they must not be destroyed here. */
+       (i.e., enc->out) are created outside of the encoder.
+       Therefore, they must not be destroyed here.
+    */
 
     if (enc->curtile) {
         jpc_enc_tile_destroy(enc->curtile);
@@ -810,24 +840,22 @@ void jpc_enc_destroy(jpc_enc_t *enc)
     jas_free(enc);
 }
 
-/******************************************************************************\
-* Code.
-\******************************************************************************/
 
-static int jpc_enc_encodemainhdr(jpc_enc_t *enc)
-{
+
+static int
+jpc_enc_encodemainhdr(jpc_enc_t *enc) {
+
     jpc_siz_t *siz;
     jpc_cod_t *cod;
     jpc_qcd_t *qcd;
     int i;
-long startoff;
-long mainhdrlen;
+    long startoff;
+    long mainhdrlen;
     jpc_enc_cp_t *cp;
     jpc_qcc_t *qcc;
     jpc_enc_tccp_t *tccp;
     uint_fast16_t cmptno;
     jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
-    jpc_fix_t mctsynweight;
     jpc_enc_tcp_t *tcp;
     jpc_tsfb_t *tsfb;
     jpc_tsfb_band_t *bandinfo;
@@ -841,7 +869,7 @@ long mainhdrlen;
 
     cp = enc->cp;
 
-startoff = jas_stream_getrwcount(enc->out);
+    startoff = jas_stream_getrwcount(enc->out);
 
     /* Write SOC marker segment. */
     if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) {
@@ -922,7 +950,6 @@ startoff = jas_stream_getrwcount(enc->out);
         jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls,
           bandinfos);
         jpc_tsfb_destroy(tsfb);
-        mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno);
         numbands = 3 * tccp->maxrlvls - 2;
         for (bandno = 0, bandinfo = bandinfos; bandno < numbands;
           ++bandno, ++bandinfo) {
@@ -953,14 +980,17 @@ startoff = jas_stream_getrwcount(enc->out);
     cod->prg = cp->tcp.prg;
     cod->numlyrs = cp->tcp.numlyrs;
     cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn);
-    cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn);
+    cod->compparms.cblkheightval =
+        JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn);
     cod->compparms.cblksty = cp->tccp.cblksty;
     cod->compparms.qmfbid = cp->tccp.qmfbid;
     cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE);
     if (tccp->csty & JPC_COX_PRT) {
         for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
-            cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno];
-            cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno];
+            cod->compparms.rlvls[rlvlno].parwidthval =
+                tccp->prcwidthexpns[rlvlno];
+            cod->compparms.rlvls[rlvlno].parheightval =
+                tccp->prcheightexpns[rlvlno];
         }
     }
     if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
@@ -1023,17 +1053,23 @@ startoff = jas_stream_getrwcount(enc->out);
     return 0;
 }
 
-int jpc_enc_encodetiledata(jpc_enc_t *enc)
-{
-assert(enc->tmpstream);
+
+
+int
+jpc_enc_encodetiledata(jpc_enc_t *enc) {
+
+    assert(enc->tmpstream);
     if (jpc_enc_encpkts(enc, enc->tmpstream)) {
         return -1;
     }
     return 0;
 }
 
-void quantize(jas_matrix_t *data, jpc_fix_t stepsize)
-{
+
+
+void
+quantize(jas_matrix_t *data, jpc_fix_t stepsize) {
+
     int i;
     int j;
     jpc_fix_t t;
@@ -1059,8 +1095,11 @@ void quantize(jas_matrix_t *data, jpc_fix_t stepsize)
     }
 }
 
-static void calcrdslopes(jpc_enc_cblk_t *cblk)
-{
+
+
+static void
+calcrdslopes(jpc_enc_cblk_t *cblk) {
+
     jpc_enc_pass_t *endpasses;
     jpc_enc_pass_t *pass0;
     jpc_enc_pass_t *pass1;
@@ -1113,10 +1152,12 @@ static void calcrdslopes(jpc_enc_cblk_t *cblk)
 
 #if 0
     for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) {
-if (pass0->rdslope > 0.0) {
-        fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes,
-          fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope);
-}
+        if (pass0->rdslope > 0.0) {
+            fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n",
+                    pass0 - cblk->passes,
+                    fixtodbl(pass0->nmsedec), pass0->wmsedec,
+                    pass0->end, pass0->rdslope);
+        }
     }
 #endif
 }
@@ -1196,8 +1237,8 @@ computeLayerSizes(jpc_enc_t *      const encP,
 
 
 
-static void dump_layeringinfo(jpc_enc_t *enc)
-{
+static void
+dump_layeringinfo(jpc_enc_t *enc) {
 
     jpc_enc_tcmpt_t *tcmpt;
     uint_fast16_t tcmptno;
@@ -1626,12 +1667,15 @@ performTier2Coding(jpc_enc_t *     const encP,
     JAS_DBGLOG(10, ("done doing rateallocation\n"));
 }
 
+
+
 /*****************************************************************************\
 * Tile constructors and destructors.
 \*****************************************************************************/
 
-jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno)
-{
+jpc_enc_tile_t *
+jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno) {
+
     jpc_enc_tile_t *tile;
     uint_fast32_t htileno;
     uint_fast32_t vtileno;
@@ -1733,8 +1777,11 @@ error:
     return 0;
 }
 
-void jpc_enc_tile_destroy(jpc_enc_tile_t *tile)
-{
+
+
+void
+jpc_enc_tile_destroy(jpc_enc_tile_t *tile) {
+
     jpc_enc_tcmpt_t *tcmpt;
     uint_fast16_t cmptno;
 
@@ -1754,9 +1801,14 @@ void jpc_enc_tile_destroy(jpc_enc_tile_t *tile)
     jas_free(tile);
 }
 
-static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
-  jas_image_t *image, jpc_enc_tile_t *tile)
-{
+
+
+static jpc_enc_tcmpt_t *
+tcmpt_create(jpc_enc_tcmpt_t * const tcmpt,
+             jpc_enc_cp_t *    const cp,
+             jas_image_t *     const image,
+             jpc_enc_tile_t *  const tile) {
+
     uint_fast16_t cmptno;
     uint_fast16_t rlvlno;
     jpc_enc_rlvl_t *rlvl;
@@ -1787,15 +1839,15 @@ static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
     bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy);
 
     /* Create a sequence to hold the tile-component sample data. */
-    if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) {
+    tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry);
+    if (!tcmpt->data)
         goto error;
-    }
 
     /* Get the image data associated with this tile-component. */
     cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx);
     cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy);
     if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly,
-      brx - tlx, bry - tly, tcmpt->data)) {
+                           brx - tlx, bry - tly, tcmpt->data)) {
         goto error;
     }
 
@@ -1803,9 +1855,11 @@ static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
     tcmpt->qmfbid = cp->tccp.qmfbid;
     tcmpt->numrlvls = cp->tccp.maxrlvls;
     tcmpt->numbands = 3 * tcmpt->numrlvls - 2;
-    if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) {
+
+    tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1);
+
+    if (!tcmpt->tsfb)
         goto error;
-    }
 
     for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) {
         tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno];
@@ -1818,17 +1872,19 @@ static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
 
     tcmpt->numstepsizes = tcmpt->numbands;
     assert(tcmpt->numstepsizes <= JPC_MAXBANDS);
-    memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes *
-      sizeof(uint_fast16_t)));
+    memset(tcmpt->stepsizes, 0,
+           sizeof(tcmpt->numstepsizes * sizeof(uint_fast16_t)));
 
     /* Retrieve information about the various bands. */
     jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data),
-      jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data),
-      jas_seq2d_yend(tcmpt->data), bandinfos);
+                      jas_seq2d_ystart(tcmpt->data),
+                      jas_seq2d_xend(tcmpt->data),
+                      jas_seq2d_yend(tcmpt->data), bandinfos);
 
-    if (!(tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)))) {
+    tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t));
+    if (!tcmpt->rlvls)
         goto error;
-    }
+
     for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
       ++rlvlno, ++rlvl) {
         rlvl->bands = 0;
@@ -1850,8 +1906,11 @@ error:
 
 }
 
-static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt)
-{
+
+
+static void
+tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) {
+
     jpc_enc_rlvl_t *rlvl;
     uint_fast16_t rlvlno;
 
@@ -1871,9 +1930,12 @@ static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt)
     }
 }
 
-static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
-  jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos)
-{
+
+
+static jpc_enc_rlvl_t *
+rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
+            jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos) {
+
     uint_fast16_t rlvlno;
     uint_fast32_t tlprctlx;
     uint_fast32_t tlprctly;
@@ -1891,14 +1953,18 @@ static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
 
     /* Compute the coordinates of the top-left and bottom-right
       corners of the tile-component at this resolution. */
-    rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls -
-      1 - rlvlno);
-    rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls -
-      1 - rlvlno);
-    rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls -
-      1 - rlvlno);
-    rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls -
-      1 - rlvlno);
+    rlvl->tlx =
+        JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls -
+                        1 - rlvlno);
+    rlvl->tly =
+        JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls -
+                        1 - rlvlno);
+    rlvl->brx =
+        JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls -
+                        1 - rlvlno);
+    rlvl->bry =
+        JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls -
+                        1 - rlvlno);
 
     if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) {
         rlvl->numhprcs = 0;
@@ -1917,8 +1983,10 @@ static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
         rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
         rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
     }
-    rlvl->cblkwidthexpn = JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn);
-    rlvl->cblkheightexpn = JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn);
+    rlvl->cblkwidthexpn =
+        JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn);
+    rlvl->cblkheightexpn =
+        JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn);
 
     /* Compute the number of precincts. */
     tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
@@ -1926,20 +1994,22 @@ static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
     brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn);
     brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn);
     rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn);
-    rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn);
+    rlvl->numvprcs =
+        JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn);
     rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs;
 
-    if (!(rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)))) {
+    rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t));
+    if (!rlvl->bands)
         goto error;
-    }
+
     for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
-      ++bandno, ++band) {
+         ++bandno, ++band) {
         band->prcs = 0;
         band->data = 0;
         band->rlvl = rlvl;
     }
     for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
-      ++bandno, ++band) {
+         ++bandno, ++band) {
         if (!band_create(band, cp, rlvl, bandinfos)) {
             goto error;
         }
@@ -1952,8 +2022,11 @@ error:
     return 0;
 }
 
-static void rlvl_destroy(jpc_enc_rlvl_t *rlvl)
-{
+
+
+static void
+rlvl_destroy(jpc_enc_rlvl_t *rlvl) {
+
     jpc_enc_band_t *band;
     uint_fast16_t bandno;
 
@@ -1966,9 +2039,12 @@ static void rlvl_destroy(jpc_enc_rlvl_t *rlvl)
     }
 }
 
-static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
-  jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos)
-{
+
+
+static jpc_enc_band_t *
+band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
+            jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos) {
+
     uint_fast16_t bandno;
     uint_fast16_t gblbandno;
     uint_fast16_t rlvlno;
@@ -1989,42 +2065,46 @@ static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
 
     bandinfo = &bandinfos[gblbandno];
 
-if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) {
-    if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) {
-        goto error;
+    if (bandinfo->xstart != bandinfo->xend &&
+        bandinfo->ystart != bandinfo->yend) {
+        band->data = jas_seq2d_create(0, 0, 0, 0);
+        if (!band->data)
+            goto error;
+        jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart,
+                          bandinfo->locystart, bandinfo->locxend,
+                          bandinfo->locyend);
+        jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart);
     }
-    jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart,
-      bandinfo->locystart, bandinfo->locxend, bandinfo->locyend);
-    jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart);
-}
     band->orient = bandinfo->orient;
     band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno,
-      band->orient);
+                                     band->orient);
     band->numbps = 0;
     band->absstepsize = 0;
     band->stepsize = 0;
     band->synweight = bandinfo->synenergywt;
 
-if (band->data) {
-    if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)))) {
-        goto error;
-    }
-    for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
-      ++prc) {
-        prc->cblks = 0;
-        prc->incltree = 0;
-        prc->nlibtree = 0;
-        prc->savincltree = 0;
-        prc->savnlibtree = 0;
-        prc->band = band;
-    }
-    for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
-      ++prc) {
-        if (!prc_create(prc, cp, band)) {
+    if (band->data) {
+        band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t));
+        if (!band->prcs)
             goto error;
+        for (prcno = 0, prc = band->prcs;
+             prcno < rlvl->numprcs;
+             ++prcno, ++prc) {
+            prc->cblks = 0;
+            prc->incltree = 0;
+            prc->nlibtree = 0;
+            prc->savincltree = 0;
+            prc->savnlibtree = 0;
+            prc->band = band;
+        }
+        for (prcno = 0, prc = band->prcs;
+             prcno < rlvl->numprcs;
+             ++prcno, ++prc) {
+            if (!prc_create(prc, cp, band)) {
+                goto error;
+            }
         }
     }
-}
 
     return band;
 
@@ -2033,8 +2113,11 @@ error:
     return 0;
 }
 
-static void band_destroy(jpc_enc_band_t *band)
-{
+
+
+static void
+band_destroy(jpc_enc_band_t *band) {
+
     jpc_enc_prc_t *prc;
     jpc_enc_rlvl_t *rlvl;
     uint_fast32_t prcno;
@@ -2052,8 +2135,11 @@ static void band_destroy(jpc_enc_band_t *band)
     }
 }
 
-static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band)
-{
+
+
+static jpc_enc_prc_t *
+prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band) {
+
     uint_fast32_t prcno;
     uint_fast32_t prcxind;
     uint_fast32_t prcyind;
@@ -2081,32 +2167,34 @@ static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_b
 
     rlvl = band->rlvl;
     tcmpt = rlvl->tcmpt;
-rlvlno = rlvl - tcmpt->rlvls;
+    rlvlno = rlvl - tcmpt->rlvls;
     prcno = prc - band->prcs;
     prcxind = prcno % rlvl->numhprcs;
     prcyind = prcno / rlvl->numhprcs;
     prc->band = band;
 
-tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
-tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
-if (!rlvlno) {
-    tlcbgtlx = tlprctlx;
-    tlcbgtly = tlprctly;
-} else {
-    tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1);
-    tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1);
-}
+    tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
+    tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
 
-    /* Compute the coordinates of the top-left and bottom-right
-      corners of the precinct. */
+    if (!rlvlno) {
+        tlcbgtlx = tlprctlx;
+        tlcbgtly = tlprctly;
+    } else {
+        tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1);
+        tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1);
+    }
+
+    /* Compute the coordinates of the top-left and bottom-right corners of the
+       precinct.
+    */
     cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn);
     cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn);
     prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx);
     prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly);
     prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx +
-      (1 << rlvl->cbgwidthexpn));
+                       (1 << rlvl->cbgwidthexpn));
     prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly +
-      (1 << rlvl->cbgheightexpn));
+                       (1 << rlvl->cbgheightexpn));
 
     if (prc->tlx < prc->brx && prc->tly < prc->bry) {
         /* The precinct contains at least one code block. */
@@ -2116,33 +2204,35 @@ if (!rlvlno) {
         brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn);
         brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn);
         prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx,
-          rlvl->cblkwidthexpn);
+                                          rlvl->cblkwidthexpn);
         prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly,
-          rlvl->cblkheightexpn);
+                                          rlvl->cblkheightexpn);
         prc->numcblks = prc->numhcblks * prc->numvcblks;
 
         if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks,
-          prc->numvcblks))) {
+                                                 prc->numvcblks))) {
             goto error;
         }
         if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks,
-          prc->numvcblks))) {
+                                                 prc->numvcblks))) {
             goto error;
         }
         if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks,
-          prc->numvcblks))) {
+                                                    prc->numvcblks))) {
             goto error;
         }
         if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks,
-          prc->numvcblks))) {
+                                                    prc->numvcblks))) {
             goto error;
         }
 
-        if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) {
+        prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t));
+        
+        if (!prc->cblks)
             goto error;
-        }
-        for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
-          ++cblkno, ++cblk) {
+        for (cblkno = 0, cblk = prc->cblks;
+             cblkno < prc->numcblks;
+             ++cblkno, ++cblk) {
             cblk->passes = 0;
             cblk->stream = 0;
             cblk->mqenc = 0;
@@ -2150,8 +2240,9 @@ if (!rlvlno) {
             cblk->flags = 0;
             cblk->prc = prc;
         }
-        for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
-          ++cblkno, ++cblk) {
+        for (cblkno = 0, cblk = prc->cblks;
+             cblkno < prc->numcblks;
+             ++cblkno, ++cblk) {
             if (!cblk_create(cblk, cp, prc)) {
                 goto error;
             }
@@ -2177,8 +2268,11 @@ error:
     return 0;
 }
 
-static void prc_destroy(jpc_enc_prc_t *prc)
-{
+
+
+static void
+prc_destroy(jpc_enc_prc_t *prc) {
+
     jpc_enc_cblk_t *cblk;
     uint_fast32_t cblkno;
 
@@ -2203,8 +2297,11 @@ static void prc_destroy(jpc_enc_prc_t *prc)
     }
 }
 
-static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc)
-{
+
+
+static jpc_enc_cblk_t *
+cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc) {
+
     jpc_enc_band_t *band;
     uint_fast32_t cblktlx;
     uint_fast32_t cblktly;
@@ -2241,18 +2338,22 @@ static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_e
     band = prc->band;
     tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
     tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
-    cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx);
-    cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly);
-    cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn),
-      prc->brx);
-    cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn),
-      prc->bry);
+    cblktlx =
+        JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx);
+    cblktly =
+        JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly);
+    cblkbrx =
+        JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn), prc->brx);
+    cblkbry =
+        JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn),
+                prc->bry);
 
     assert(cblktlx < cblkbrx && cblktly < cblkbry);
-    if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) {
+    cblk->data = jas_seq2d_create(0, 0, 0, 0);
+    if (!cblk->data)
         goto error;
-    }
-    jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry);
+    jas_seq2d_bindsub(cblk->data, band->data,
+                      cblktlx, cblktly, cblkbrx, cblkbry);
 
     return cblk;
 
@@ -2261,8 +2362,11 @@ error:
     return 0;
 }
 
-static void cblk_destroy(jpc_enc_cblk_t *cblk)
-{
+
+
+static void
+cblk_destroy(jpc_enc_cblk_t *cblk) {
+
     uint_fast16_t passno;
     jpc_enc_pass_t *pass;
     if (cblk->passes) {
@@ -2286,13 +2390,19 @@ static void cblk_destroy(jpc_enc_cblk_t *cblk)
     }
 }
 
-static void pass_destroy(jpc_enc_pass_t *pass)
-{
+
+
+static void
+pass_destroy(jpc_enc_pass_t *pass) {
+
     /* XXX - need to free resources here */
 }
 
-void jpc_enc_dump(jpc_enc_t *enc)
-{
+
+
+void
+jpc_enc_dump(jpc_enc_t *enc) {
+
     jpc_enc_tile_t *tile;
     jpc_enc_tcmpt_t *tcmpt;
     jpc_enc_rlvl_t *rlvl;
@@ -2358,11 +2468,10 @@ void jpc_enc_dump(jpc_enc_t *enc)
 
 
 
-static int jpc_enc_encodemainbody(jpc_enc_t *enc)
-{
+static int
+jpc_enc_encodemainbody(jpc_enc_t *enc) {
+
     int tileno;
-    int tilex;
-    int tiley;
     int i;
     jpc_sot_t *sot;
     jpc_enc_tcmpt_t *comp;
@@ -2376,7 +2485,6 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc)
     int adjust;
     int j;
     int absbandno;
-    long numbytes;
     long tilehdrlen;
     long tilelen;
     jpc_enc_tile_t *tile;
@@ -2398,13 +2506,7 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc)
 
     cp = enc->cp;
 
-    /* Avoid compile warnings. */
-    numbytes = 0;
-
     for (tileno = 0; tileno < cp->numtiles; ++tileno) {
-        tilex = tileno % cp->numhtiles;
-        tiley = tileno / cp->numhtiles;
-
         enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno);
         if (!enc->curtile)
             abort();
@@ -2459,7 +2561,6 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc)
 
         }
 
-
         endcomps = &tile->tcmpts[tile->numtcmpts];
         for (cmptno = 0, comp = tile->tcmpts;
              comp != endcomps;
@@ -2544,15 +2645,16 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc)
             return -1;
         }
 
-        if (!(enc->tmpstream = jas_stream_memopen(0, 0))) {
+        enc->tmpstream = jas_stream_memopen(0, 0);
+        if (!enc->tmpstream) {
             fprintf(stderr, "cannot open tmp file\n");
             return -1;
         }
 
         /* Write the tile header. */
-        if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) {
+        enc->mrk = jpc_ms_create(JPC_MS_SOT);
+        if (!enc->mrk)
             return -1;
-        }
         sot = &enc->mrk->parms.sot;
         sot->len = 0;
         sot->tileno = tileno;
diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c b/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c
index 8aa024a9..e1af0f61 100644
--- a/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c
+++ b/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c
@@ -1,4 +1,4 @@
-/*
+ /*
  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  *   British Columbia.
  * Copyright (c) 2001-2002 Michael David Adams.
@@ -156,853 +156,872 @@ static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int,
 /* Encode all of the code blocks associated with the current tile. */
 int jpc_enc_enccblks(jpc_enc_t *enc)
 {
-	jpc_enc_tcmpt_t *tcmpt;
-	jpc_enc_tcmpt_t *endcomps;
-	jpc_enc_rlvl_t *lvl;
-	jpc_enc_rlvl_t *endlvls;
-	jpc_enc_band_t *band;
-	jpc_enc_band_t *endbands;
-	jpc_enc_cblk_t *cblk;
-	jpc_enc_cblk_t *endcblks;
-	int i;
-	int j;
-	int mx;
-	int bmx;
-	int v;
-	jpc_enc_tile_t *tile;
-	uint_fast32_t prcno;
-	jpc_enc_prc_t *prc;
-
-	tile = enc->curtile;
-
-	endcomps = &tile->tcmpts[tile->numtcmpts];
-	for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) {
-		endlvls = &tcmpt->rlvls[tcmpt->numrlvls];
-		for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) {
-			if (!lvl->bands) {
-				continue;
-			}
-			endbands = &lvl->bands[lvl->numbands];
-			for (band = lvl->bands; band != endbands; ++band) {
-				if (!band->data) {
-					continue;
-				}
-				for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
-					if (!prc->cblks) {
-						continue;
-					}
-					bmx = 0;
-					endcblks = &prc->cblks[prc->numcblks];
-					for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
-						mx = 0;
-						for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) {
-							for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) {
-								v = abs(jas_matrix_get(cblk->data, i, j));
-								if (v > mx) {
-									mx = v;
-								}
-							}
-						}
-						if (mx > bmx) {
-							bmx = mx;
-						}
-						cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0);
-					}
-
-					for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
-						cblk->numimsbs = band->numbps - cblk->numbps;
-						assert(cblk->numimsbs >= 0);
-					}
-
-					for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
-						if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) {
-							return -1;
-						}
-					}
-				}
-			}
-		}
-	}
-	return 0;
+    jpc_enc_tcmpt_t *tcmpt;
+    jpc_enc_tcmpt_t *endcomps;
+    jpc_enc_rlvl_t *lvl;
+    jpc_enc_rlvl_t *endlvls;
+    jpc_enc_band_t *band;
+    jpc_enc_band_t *endbands;
+    jpc_enc_cblk_t *cblk;
+    jpc_enc_cblk_t *endcblks;
+    int i;
+    int j;
+    int mx;
+    int bmx;
+    int v;
+    jpc_enc_tile_t *tile;
+    uint_fast32_t prcno;
+    jpc_enc_prc_t *prc;
+
+    tile = enc->curtile;
+
+    endcomps = &tile->tcmpts[tile->numtcmpts];
+    for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) {
+        endlvls = &tcmpt->rlvls[tcmpt->numrlvls];
+        for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) {
+            if (!lvl->bands) {
+                continue;
+            }
+            endbands = &lvl->bands[lvl->numbands];
+            for (band = lvl->bands; band != endbands; ++band) {
+                if (!band->data) {
+                    continue;
+                }
+                for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
+                    if (!prc->cblks) {
+                        continue;
+                    }
+                    bmx = 0;
+                    endcblks = &prc->cblks[prc->numcblks];
+                    for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
+                        mx = 0;
+                        for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) {
+                            for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) {
+                                v = abs(jas_matrix_get(cblk->data, i, j));
+                                if (v > mx) {
+                                    mx = v;
+                                }
+                            }
+                        }
+                        if (mx > bmx) {
+                            bmx = mx;
+                        }
+                        cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0);
+                    }
+
+                    for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
+                        cblk->numimsbs = band->numbps - cblk->numbps;
+                        assert(cblk->numimsbs >= 0);
+                    }
+
+                    for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
+                        if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) {
+                            return -1;
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return 0;
 }
 
 static int getthebyte(jas_stream_t *in, long off)
 {
-	int c;
-	long oldpos;
-	oldpos = jas_stream_tell(in);
-	assert(oldpos >= 0);
-	jas_stream_seek(in, off, SEEK_SET);
-	c = jas_stream_peekc(in);
-	jas_stream_seek(in, oldpos, SEEK_SET);
-	return c;
+    int c;
+    long oldpos;
+    oldpos = jas_stream_tell(in);
+    assert(oldpos >= 0);
+    jas_stream_seek(in, off, SEEK_SET);
+    c = jas_stream_peekc(in);
+    jas_stream_seek(in, oldpos, SEEK_SET);
+    return c;
 }
 
-/* Encode a single code block. */
-int jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt, jpc_enc_band_t *band, jpc_enc_cblk_t *cblk)
-{
-	jpc_enc_pass_t *pass;
-	jpc_enc_pass_t *endpasses;
-	int bitpos;
-	int n;
-	int adjust;
-	int ret;
-	int passtype;
-	int t;
-	jpc_bitstream_t *bout;
-	jpc_enc_pass_t *termpass;
-	jpc_enc_rlvl_t *rlvl;
-	int vcausal;
-	int segsym;
-	int termmode;
-	int c;
-
-	bout = 0;
-	rlvl = band->rlvl;
-
-	cblk->stream = jas_stream_memopen(0, 0);
-	assert(cblk->stream);
-	cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream);
-	assert(cblk->mqenc);
-	jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs);
-
-	cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0;
-	if (cblk->numpasses > 0) {
-		cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t));
-		assert(cblk->passes);
-	} else {
-		cblk->passes = 0;
-	}
-	endpasses = &cblk->passes[cblk->numpasses];
-	for (pass = cblk->passes; pass != endpasses; ++pass) {
-		pass->start = 0;
-		pass->end = 0;
-		pass->term = JPC_ISTERMINATED(pass - cblk->passes, 0, cblk->numpasses, (tcmpt->cblksty & JPC_COX_TERMALL) != 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0);
-		pass->type = JPC_SEGTYPE(pass - cblk->passes, 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0);
-		pass->lyrno = -1;
-if (pass == endpasses - 1) {
-assert(pass->term == 1);
-	pass->term = 1;
-}
-	}
-
-	cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2,
-	  jas_matrix_numcols(cblk->data) + 2);
-	assert(cblk->flags);
-
 
-	bitpos = cblk->numbps - 1;
-	pass = cblk->passes;
-	n = cblk->numpasses;
-	while (--n >= 0) {
 
-		if (pass->type == JPC_SEG_MQ) {
-			/* NOP */
-		} else {
-			assert(pass->type == JPC_SEG_RAW);
-			if (!bout) {
-				bout = jpc_bitstream_sopen(cblk->stream, "w");
-				assert(bout);
-			}
-		}
+int
+jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt,
+                jpc_enc_band_t *band, jpc_enc_cblk_t *cblk) {
+/*----------------------------------------------------------------------------
+  Encode a single code block.
+-----------------------------------------------------------------------------*/
+    jpc_enc_pass_t *pass;
+    jpc_enc_pass_t *endpasses;
+    int bitpos;
+    int n;
+    int adjust;
+    int passtype;
+    int t;
+    jpc_bitstream_t *bout;
+    jpc_enc_pass_t *termpass;
+    int vcausal;
+    int segsym;
+    int termmode;
+    int c;
+
+    bout = 0;
+
+    cblk->stream = jas_stream_memopen(0, 0);
+    assert(cblk->stream);
+    cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream);
+    assert(cblk->mqenc);
+    jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs);
+
+    cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0;
+    if (cblk->numpasses > 0) {
+        cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t));
+        assert(cblk->passes);
+    } else {
+        cblk->passes = 0;
+    }
+    endpasses = &cblk->passes[cblk->numpasses];
+    for (pass = cblk->passes; pass != endpasses; ++pass) {
+        pass->start = 0;
+        pass->end = 0;
+        pass->term =
+            JPC_ISTERMINATED(pass - cblk->passes, 0,
+                             cblk->numpasses,
+                             (tcmpt->cblksty & JPC_COX_TERMALL) != 0,
+                             (tcmpt->cblksty & JPC_COX_LAZY) != 0);
+        pass->type = JPC_SEGTYPE(pass - cblk->passes, 0,
+                                 (tcmpt->cblksty & JPC_COX_LAZY) != 0);
+        pass->lyrno = -1;
+        if (pass == endpasses - 1) {
+            assert(pass->term == 1);
+            pass->term = 1;
+        }
+    }
+
+    cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2,
+      jas_matrix_numcols(cblk->data) + 2);
+    assert(cblk->flags);
+
+
+    bitpos = cblk->numbps - 1;
+    pass = cblk->passes;
+    n = cblk->numpasses;
+    while (--n >= 0) {
+
+        if (pass->type == JPC_SEG_MQ) {
+            /* NOP */
+        } else {
+            assert(pass->type == JPC_SEG_RAW);
+            if (!bout) {
+                bout = jpc_bitstream_sopen(cblk->stream, "w");
+                assert(bout);
+            }
+        }
 
 #if 1
-		passtype = (pass - cblk->passes + 2) % 3;
+        passtype = (pass - cblk->passes + 2) % 3;
 #else
-		passtype = JPC_PASSTYPE(pass - cblk->passes + 2);
+        passtype = JPC_PASSTYPE(pass - cblk->passes + 2);
 #endif
-		pass->start = jas_stream_tell(cblk->stream);
+        pass->start = jas_stream_tell(cblk->stream);
 #if 0
 assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream));
 #endif
-		assert(bitpos >= 0);
-		vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0;
-		segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0;
-		if (pass->term) {
-			termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ?
-			  JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1;
-		} else {
-			termmode = 0;
-		}
-		switch (passtype) {
-		case JPC_SIGPASS:
-			ret = (pass->type == JPC_SEG_MQ) ? jpc_encsigpass(cblk->mqenc,
-			  bitpos, band->orient, vcausal, cblk->flags,
-			  cblk->data, termmode, &pass->nmsedec) :
-			  jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags,
-			  cblk->data, termmode, &pass->nmsedec);
-			break;
-		case JPC_REFPASS:
-			ret = (pass->type == JPC_SEG_MQ) ? jpc_encrefpass(cblk->mqenc,
-			  bitpos, vcausal, cblk->flags, cblk->data, termmode,
-			  &pass->nmsedec) : jpc_encrawrefpass(bout, bitpos,
-			  vcausal, cblk->flags, cblk->data, termmode,
-			  &pass->nmsedec);
-			break;
-		case JPC_CLNPASS:
-			assert(pass->type == JPC_SEG_MQ);
-			ret = jpc_encclnpass(cblk->mqenc, bitpos, band->orient,
-			  vcausal, segsym, cblk->flags, cblk->data, termmode,
-			  &pass->nmsedec);
-			break;
-		default:
-			assert(0);
-			break;
-		}
-
-		if (pass->type == JPC_SEG_MQ) {
-			if (pass->term) {
-				jpc_mqenc_init(cblk->mqenc);
-			}
-			jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate);
-			pass->end = jas_stream_tell(cblk->stream);
-			if (tcmpt->cblksty & JPC_COX_RESET) {
-				jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs);
-			}
-		} else {
-			if (pass->term) {
-				if (jpc_bitstream_pending(bout)) {
-					jpc_bitstream_outalign(bout, 0x2a);
-				}
-				jpc_bitstream_close(bout);
-				bout = 0;
-				pass->end = jas_stream_tell(cblk->stream);
-			} else {
-				pass->end = jas_stream_tell(cblk->stream) +
-				  jpc_bitstream_pending(bout);
-/* NOTE - This will not work.  need to adjust by # of pending output bytes */
-			}
-		}
+        assert(bitpos >= 0);
+        vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0;
+        segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0;
+        if (pass->term) {
+            termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ?
+              JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1;
+        } else {
+            termmode = 0;
+        }
+        switch (passtype) {
+        case JPC_SIGPASS:
+            if (pass->type == JPC_SEG_MQ)
+            jpc_encsigpass(cblk->mqenc,
+                           bitpos, band->orient, vcausal, cblk->flags,
+                           cblk->data, termmode, &pass->nmsedec);
+            else
+                jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags,
+                                  cblk->data, termmode, &pass->nmsedec);
+            break;
+        case JPC_REFPASS:
+            if (pass->type == JPC_SEG_MQ)
+            jpc_encrefpass(
+                cblk->mqenc,
+                bitpos, vcausal, cblk->flags, cblk->data, termmode,
+                &pass->nmsedec);
+            else
+                jpc_encrawrefpass(bout, bitpos,
+                                  vcausal, cblk->flags, cblk->data, termmode,
+                                  &pass->nmsedec);
+            break;
+        case JPC_CLNPASS:
+            assert(pass->type == JPC_SEG_MQ);
+            jpc_encclnpass(
+                cblk->mqenc, bitpos, band->orient,
+                vcausal, segsym, cblk->flags, cblk->data, termmode,
+                &pass->nmsedec);
+            break;
+        default:
+            assert(false);
+            break;
+        }
+
+        if (pass->type == JPC_SEG_MQ) {
+            if (pass->term) {
+                jpc_mqenc_init(cblk->mqenc);
+            }
+            jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate);
+            pass->end = jas_stream_tell(cblk->stream);
+            if (tcmpt->cblksty & JPC_COX_RESET) {
+                jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs);
+            }
+        } else {
+            if (pass->term) {
+                if (jpc_bitstream_pending(bout)) {
+                    jpc_bitstream_outalign(bout, 0x2a);
+                }
+                jpc_bitstream_close(bout);
+                bout = 0;
+                pass->end = jas_stream_tell(cblk->stream);
+            } else {
+                pass->end = jas_stream_tell(cblk->stream) +
+                  jpc_bitstream_pending(bout);
+                /* NOTE - This will not work.  need to adjust by # of pending
+                   output bytes
+                */
+            }
+        }
 #if 0
-/* XXX - This assertion fails sometimes when various coding modes are used.
-This seems to be harmless, but why does it happen at all? */
-assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream));
+    /* XXX - This assertion fails sometimes when various coding modes are used.
+       This seems to be harmless, but why does it happen at all? */
+        assert(jas_stream_tell(cblk->stream) == 
+               jas_stream_getrwcount(cblk->stream));
 #endif
 
-		pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) *
-		  jpc_fixtodbl(band->rlvl->tcmpt->synweight) *
-		  jpc_fixtodbl(band->synweight) *
-		  jpc_fixtodbl(band->synweight) *
-		  jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) *
-		  ((double) (1 << bitpos)) * ((double)(1 << bitpos)) *
-		  jpc_fixtodbl(pass->nmsedec);
-		pass->cumwmsedec = pass->wmsedec;
-		if (pass != cblk->passes) {
-			pass->cumwmsedec += pass[-1].cumwmsedec;
-		}
-		if (passtype == JPC_CLNPASS) {
-			--bitpos;
-		}
-		++pass;
-	}
+        pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) *
+          jpc_fixtodbl(band->rlvl->tcmpt->synweight) *
+          jpc_fixtodbl(band->synweight) *
+          jpc_fixtodbl(band->synweight) *
+          jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) *
+          ((double) (1 << bitpos)) * ((double)(1 << bitpos)) *
+          jpc_fixtodbl(pass->nmsedec);
+        pass->cumwmsedec = pass->wmsedec;
+        if (pass != cblk->passes) {
+            pass->cumwmsedec += pass[-1].cumwmsedec;
+        }
+        if (passtype == JPC_CLNPASS) {
+            --bitpos;
+        }
+        ++pass;
+    }
 
 #if 0
-dump_passes(cblk->passes, cblk->numpasses, cblk);
+    dump_passes(cblk->passes, cblk->numpasses, cblk);
 #endif
 
-	n = 0;
-	endpasses = &cblk->passes[cblk->numpasses];
-	for (pass = cblk->passes; pass != endpasses; ++pass) {
-		if (pass->start < n) {
-			pass->start = n;
-		}
-		if (pass->end < n) {
-			pass->end = n;
-		}
-		if (!pass->term) {
-			termpass = pass;
-			while (termpass - pass < cblk->numpasses &&
-			  !termpass->term) {
-				++termpass;
-			}
-			if (pass->type == JPC_SEG_MQ) {
-				t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0;
-				if (pass->mqencstate.ctreg >= 5) {
-					adjust = 4 + t;
-				} else {
-					adjust = 5 + t;
-				}
-				pass->end += adjust;
-			}
-			if (pass->end > termpass->end) {
-				pass->end = termpass->end;
-			}
-			if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) {
-				abort();
-			}
-			if (c == 0xff) {
-				++pass->end;
-			}
-			n = JAS_MAX(n, pass->end);
-		} else {
-			n = JAS_MAX(n, pass->end);
-		}
-	}
+    n = 0;
+    endpasses = &cblk->passes[cblk->numpasses];
+    for (pass = cblk->passes; pass != endpasses; ++pass) {
+        if (pass->start < n) {
+            pass->start = n;
+        }
+        if (pass->end < n) {
+            pass->end = n;
+        }
+        if (!pass->term) {
+            termpass = pass;
+            while (termpass - pass < cblk->numpasses &&
+              !termpass->term) {
+                ++termpass;
+            }
+            if (pass->type == JPC_SEG_MQ) {
+                t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0;
+                if (pass->mqencstate.ctreg >= 5) {
+                    adjust = 4 + t;
+                } else {
+                    adjust = 5 + t;
+                }
+                pass->end += adjust;
+            }
+            if (pass->end > termpass->end) {
+                pass->end = termpass->end;
+            }
+            if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) {
+                abort();
+            }
+            if (c == 0xff) {
+                ++pass->end;
+            }
+            n = JAS_MAX(n, pass->end);
+        } else {
+            n = JAS_MAX(n, pass->end);
+        }
+    }
 
 #if 0
-dump_passes(cblk->passes, cblk->numpasses, cblk);
+    dump_passes(cblk->passes, cblk->numpasses, cblk);
 #endif
 
-	if (bout) {
-		jpc_bitstream_close(bout);
-	}
+    if (bout) {
+        jpc_bitstream_close(bout);
+    }
 
-	return 0;
+    return 0;
 }
 
-/******************************************************************************\
+
+
+/*****************************************************************************\
 * Code for significance pass.
-\******************************************************************************/
+\*****************************************************************************/
 
-#define	sigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, orient, mqenc, vcausalflag) \
+#define sigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, orient, mqenc, vcausalflag) \
 { \
-	int f; \
-	int v; \
-	f = *(fp); \
-	if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
-		v = (abs(*(dp)) & (one)) ? 1 : 0; \
-		jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \
-		jpc_mqenc_putbit(mqenc, v); \
-		if (v) { \
-			*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
-			v = ((*(dp) < 0) ? 1 : 0); \
-			jpc_mqenc_setcurctx(mqenc, JPC_GETSCCTXNO(f)); \
-			jpc_mqenc_putbit(mqenc, v ^ JPC_GETSPB(f)); \
-			JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \
-			*(fp) |= JPC_SIG; \
-		} \
-		*(fp) |= JPC_VISIT; \
-	} \
+    int f; \
+    int v; \
+    f = *(fp); \
+    if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
+        v = (abs(*(dp)) & (one)) ? 1 : 0; \
+        jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \
+        jpc_mqenc_putbit(mqenc, v); \
+        if (v) { \
+            *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
+            v = ((*(dp) < 0) ? 1 : 0); \
+            jpc_mqenc_setcurctx(mqenc, JPC_GETSCCTXNO(f)); \
+            jpc_mqenc_putbit(mqenc, v ^ JPC_GETSPB(f)); \
+            JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \
+            *(fp) |= JPC_SIG; \
+        } \
+        *(fp) |= JPC_VISIT; \
+    } \
 }
 
 static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag,
   jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec)
 {
-	int i;
-	int j;
-	int one;
-	int vscanlen;
-	int width;
-	int height;
-	int frowstep;
-	int drowstep;
-	int fstripestep;
-	int dstripestep;
-	jpc_fix_t *fstripestart;
-	jpc_fix_t *dstripestart;
-	jpc_fix_t *fp;
-	jpc_fix_t *dp;
-	jpc_fix_t *fvscanstart;
-	jpc_fix_t *dvscanstart;
-	int k;
-
-	*nmsedec = 0;
-	width = jas_matrix_numcols(data);
-	height = jas_matrix_numrows(data);
-	frowstep = jas_matrix_rowstep(flags);
-	drowstep = jas_matrix_rowstep(data);
-	fstripestep = frowstep << 2;
-	dstripestep = drowstep << 2;
-
-	one = 1 << (bitpos + JPC_NUMEXTRABITS);
-
-	fstripestart = jas_matrix_getref(flags, 1, 1);
-	dstripestart = jas_matrix_getref(data, 0, 0);
-	for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
-	  dstripestart += dstripestep) {
-		fvscanstart = fstripestart;
-		dvscanstart = dstripestart;
-		vscanlen = JAS_MIN(i, 4);
-		for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
-			fp = fvscanstart;
-			dp = dvscanstart;
-			k = vscanlen;
-
-			sigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, orient, mqenc, vcausalflag);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			sigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, orient, mqenc, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			sigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, orient, mqenc, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			sigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, orient, mqenc, 0);
-
-		}
-	}
-
-	if (term) {
-		jpc_mqenc_flush(mqenc, term - 1);
-	}
-
-	return jpc_mqenc_error(mqenc) ? (-1) : 0;
+    int i;
+    int j;
+    int one;
+    int vscanlen;
+    int width;
+    int height;
+    int frowstep;
+    int drowstep;
+    int fstripestep;
+    int dstripestep;
+    jpc_fix_t *fstripestart;
+    jpc_fix_t *dstripestart;
+    jpc_fix_t *fp;
+    jpc_fix_t *dp;
+    jpc_fix_t *fvscanstart;
+    jpc_fix_t *dvscanstart;
+    int k;
+
+    *nmsedec = 0;
+    width = jas_matrix_numcols(data);
+    height = jas_matrix_numrows(data);
+    frowstep = jas_matrix_rowstep(flags);
+    drowstep = jas_matrix_rowstep(data);
+    fstripestep = frowstep << 2;
+    dstripestep = drowstep << 2;
+
+    one = 1 << (bitpos + JPC_NUMEXTRABITS);
+
+    fstripestart = jas_matrix_getref(flags, 1, 1);
+    dstripestart = jas_matrix_getref(data, 0, 0);
+    for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
+      dstripestart += dstripestep) {
+        fvscanstart = fstripestart;
+        dvscanstart = dstripestart;
+        vscanlen = JAS_MIN(i, 4);
+        for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
+            fp = fvscanstart;
+            dp = dvscanstart;
+            k = vscanlen;
+
+            sigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, orient, mqenc, vcausalflag);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            sigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, orient, mqenc, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            sigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, orient, mqenc, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            sigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, orient, mqenc, 0);
+
+        }
+    }
+
+    if (term) {
+        jpc_mqenc_flush(mqenc, term - 1);
+    }
+
+    return jpc_mqenc_error(mqenc) ? (-1) : 0;
 }
 
-#define	rawsigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, out, vcausalflag) \
+#define rawsigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, out, vcausalflag) \
 { \
-	jpc_fix_t f = *(fp); \
-	jpc_fix_t v; \
-	if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
-		v = (abs(*(dp)) & (one)) ? 1 : 0; \
-		if ((jpc_bitstream_putbit((out), v)) == EOF) { \
-			return -1; \
-		} \
-		if (v) { \
-			*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
-			v = ((*(dp) < 0) ? 1 : 0); \
-			if (jpc_bitstream_putbit(out, v) == EOF) { \
-				return -1; \
-			} \
-			JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \
-			*(fp) |= JPC_SIG; \
-		} \
-		*(fp) |= JPC_VISIT; \
-	} \
+    jpc_fix_t f = *(fp); \
+    jpc_fix_t v; \
+    if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
+        v = (abs(*(dp)) & (one)) ? 1 : 0; \
+        if ((jpc_bitstream_putbit((out), v)) == EOF) { \
+            return -1; \
+        } \
+        if (v) { \
+            *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
+            v = ((*(dp) < 0) ? 1 : 0); \
+            if (jpc_bitstream_putbit(out, v) == EOF) { \
+                return -1; \
+            } \
+            JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \
+            *(fp) |= JPC_SIG; \
+        } \
+        *(fp) |= JPC_VISIT; \
+    } \
 }
 
 static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags,
   jas_matrix_t *data, int term, long *nmsedec)
 {
-	int i;
-	int j;
-	int k;
-	int one;
-	int vscanlen;
-	int width;
-	int height;
-	int frowstep;
-	int drowstep;
-	int fstripestep;
-	int dstripestep;
-	jpc_fix_t *fstripestart;
-	jpc_fix_t *dstripestart;
-	jpc_fix_t *fp;
-	jpc_fix_t *dp;
-	jpc_fix_t *fvscanstart;
-	jpc_fix_t *dvscanstart;
-
-	*nmsedec = 0;
-	width = jas_matrix_numcols(data);
-	height = jas_matrix_numrows(data);
-	frowstep = jas_matrix_rowstep(flags);
-	drowstep = jas_matrix_rowstep(data);
-	fstripestep = frowstep << 2;
-	dstripestep = drowstep << 2;
-
-	one = 1 << (bitpos + JPC_NUMEXTRABITS);
-
-	fstripestart = jas_matrix_getref(flags, 1, 1);
-	dstripestart = jas_matrix_getref(data, 0, 0);
-	for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
-	  dstripestart += dstripestep) {
-		fvscanstart = fstripestart;
-		dvscanstart = dstripestart;
-		vscanlen = JAS_MIN(i, 4);
-		for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
-			fp = fvscanstart;
-			dp = dvscanstart;
-			k = vscanlen;
-
-			rawsigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, out, vcausalflag);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-
-			rawsigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, out, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-
-			rawsigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, out, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-
-			rawsigpass_step(fp, frowstep, dp, bitpos, one,
-			  nmsedec, out, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-
-		}
-	}
-
-	if (term) {
-		jpc_bitstream_outalign(out, 0x2a);
-	}
-
-	return 0;
+    int i;
+    int j;
+    int k;
+    int one;
+    int vscanlen;
+    int width;
+    int height;
+    int frowstep;
+    int drowstep;
+    int fstripestep;
+    int dstripestep;
+    jpc_fix_t *fstripestart;
+    jpc_fix_t *dstripestart;
+    jpc_fix_t *fp;
+    jpc_fix_t *dp;
+    jpc_fix_t *fvscanstart;
+    jpc_fix_t *dvscanstart;
+
+    *nmsedec = 0;
+    width = jas_matrix_numcols(data);
+    height = jas_matrix_numrows(data);
+    frowstep = jas_matrix_rowstep(flags);
+    drowstep = jas_matrix_rowstep(data);
+    fstripestep = frowstep << 2;
+    dstripestep = drowstep << 2;
+
+    one = 1 << (bitpos + JPC_NUMEXTRABITS);
+
+    fstripestart = jas_matrix_getref(flags, 1, 1);
+    dstripestart = jas_matrix_getref(data, 0, 0);
+    for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
+      dstripestart += dstripestep) {
+        fvscanstart = fstripestart;
+        dvscanstart = dstripestart;
+        vscanlen = JAS_MIN(i, 4);
+        for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
+            fp = fvscanstart;
+            dp = dvscanstart;
+            k = vscanlen;
+
+            rawsigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, out, vcausalflag);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+
+            rawsigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, out, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+
+            rawsigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, out, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+
+            rawsigpass_step(fp, frowstep, dp, bitpos, one,
+              nmsedec, out, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+
+        }
+    }
+
+    if (term) {
+        jpc_bitstream_outalign(out, 0x2a);
+    }
+
+    return 0;
 }
 
 /******************************************************************************\
 * Code for refinement pass.
 \******************************************************************************/
 
-#define	refpass_step(fp, dp, bitpos, one, nmsedec, mqenc, vcausalflag) \
+#define refpass_step(fp, dp, bitpos, one, nmsedec, mqenc, vcausalflag) \
 { \
-	int v; \
-	if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
-		(d) = *(dp); \
-		*(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \
-		jpc_mqenc_setcurctx((mqenc), JPC_GETMAGCTXNO(*(fp))); \
-		v = (abs(d) & (one)) ? 1 : 0; \
-		jpc_mqenc_putbit((mqenc), v); \
-		*(fp) |= JPC_REFINE; \
-	} \
+    int v; \
+    if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
+        (d) = *(dp); \
+        *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \
+        jpc_mqenc_setcurctx((mqenc), JPC_GETMAGCTXNO(*(fp))); \
+        v = (abs(d) & (one)) ? 1 : 0; \
+        jpc_mqenc_putbit((mqenc), v); \
+        *(fp) |= JPC_REFINE; \
+    } \
 }
 
 static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data,
   int term, long *nmsedec)
 {
-	int i;
-	int j;
-	int one;
-	int vscanlen;
-	int d;
-	int width;
-	int height;
-	int frowstep;
-	int drowstep;
-	int fstripestep;
-	int dstripestep;
-	jpc_fix_t *fstripestart;
-	jpc_fix_t *dstripestart;
-	jpc_fix_t *fvscanstart;
-	jpc_fix_t *dvscanstart;
-	jpc_fix_t *dp;
-	jpc_fix_t *fp;
+    int i;
+    int j;
+    int one;
+    int vscanlen;
+    int d;
+    int width;
+    int height;
+    int frowstep;
+    int drowstep;
+    int fstripestep;
+    int dstripestep;
+    jpc_fix_t *fstripestart;
+    jpc_fix_t *dstripestart;
+    jpc_fix_t *fvscanstart;
+    jpc_fix_t *dvscanstart;
+    jpc_fix_t *dp;
+    jpc_fix_t *fp;
 int k;
 
-	*nmsedec = 0;
-	width = jas_matrix_numcols(data);
-	height = jas_matrix_numrows(data);
-	frowstep = jas_matrix_rowstep(flags);
-	drowstep = jas_matrix_rowstep(data);
-	fstripestep = frowstep << 2;
-	dstripestep = drowstep << 2;
-
-	one = 1 << (bitpos + JPC_NUMEXTRABITS);
-
-	fstripestart = jas_matrix_getref(flags, 1, 1);
-	dstripestart = jas_matrix_getref(data, 0, 0);
-	for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
-	  dstripestart += dstripestep) {
-		fvscanstart = fstripestart;
-		dvscanstart = dstripestart;
-		vscanlen = JAS_MIN(i, 4);
-		for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
-			fp = fvscanstart;
-			dp = dvscanstart;
-			k = vscanlen;
-
-			refpass_step(fp, dp, bitpos, one, nmsedec,
-			  mqenc, vcausalflag);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			refpass_step(fp, dp, bitpos, one, nmsedec,
-			  mqenc, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			refpass_step(fp, dp, bitpos, one, nmsedec,
-			  mqenc, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			refpass_step(fp, dp, bitpos, one, nmsedec,
-			  mqenc, 0);
-
-		}
-	}
-
-	if (term) {
-		jpc_mqenc_flush(mqenc, term - 1);
-	}
-
-	return jpc_mqenc_error(mqenc) ? (-1) : 0;
+    *nmsedec = 0;
+    width = jas_matrix_numcols(data);
+    height = jas_matrix_numrows(data);
+    frowstep = jas_matrix_rowstep(flags);
+    drowstep = jas_matrix_rowstep(data);
+    fstripestep = frowstep << 2;
+    dstripestep = drowstep << 2;
+
+    one = 1 << (bitpos + JPC_NUMEXTRABITS);
+
+    fstripestart = jas_matrix_getref(flags, 1, 1);
+    dstripestart = jas_matrix_getref(data, 0, 0);
+    for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
+      dstripestart += dstripestep) {
+        fvscanstart = fstripestart;
+        dvscanstart = dstripestart;
+        vscanlen = JAS_MIN(i, 4);
+        for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
+            fp = fvscanstart;
+            dp = dvscanstart;
+            k = vscanlen;
+
+            refpass_step(fp, dp, bitpos, one, nmsedec,
+              mqenc, vcausalflag);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            refpass_step(fp, dp, bitpos, one, nmsedec,
+              mqenc, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            refpass_step(fp, dp, bitpos, one, nmsedec,
+              mqenc, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            refpass_step(fp, dp, bitpos, one, nmsedec,
+              mqenc, 0);
+
+        }
+    }
+
+    if (term) {
+        jpc_mqenc_flush(mqenc, term - 1);
+    }
+
+    return jpc_mqenc_error(mqenc) ? (-1) : 0;
 }
 
-#define	rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag) \
+#define rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag) \
 { \
-	jpc_fix_t d; \
-	jpc_fix_t v; \
-	if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
-		d = *(dp); \
-		*(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \
-		v = (abs(d) & (one)) ? 1 : 0; \
-		if (jpc_bitstream_putbit((out), v) == EOF) { \
-			return -1; \
-		} \
-		*(fp) |= JPC_REFINE; \
-	} \
+    jpc_fix_t d; \
+    jpc_fix_t v; \
+    if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
+        d = *(dp); \
+        *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \
+        v = (abs(d) & (one)) ? 1 : 0; \
+        if (jpc_bitstream_putbit((out), v) == EOF) { \
+            return -1; \
+        } \
+        *(fp) |= JPC_REFINE; \
+    } \
 }
 
 static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags,
   jas_matrix_t *data, int term, long *nmsedec)
 {
-	int i;
-	int j;
-	int k;
-	int one;
-	int vscanlen;
-	int width;
-	int height;
-	int frowstep;
-	int drowstep;
-	int fstripestep;
-	int dstripestep;
-	jpc_fix_t *fstripestart;
-	jpc_fix_t *dstripestart;
-	jpc_fix_t *fvscanstart;
-	jpc_fix_t *dvscanstart;
-	jpc_fix_t *dp;
-	jpc_fix_t *fp;
-
-	*nmsedec = 0;
-	width = jas_matrix_numcols(data);
-	height = jas_matrix_numrows(data);
-	frowstep = jas_matrix_rowstep(flags);
-	drowstep = jas_matrix_rowstep(data);
-	fstripestep = frowstep << 2;
-	dstripestep = drowstep << 2;
-
-	one = 1 << (bitpos + JPC_NUMEXTRABITS);
-
-	fstripestart = jas_matrix_getref(flags, 1, 1);
-	dstripestart = jas_matrix_getref(data, 0, 0);
-	for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
-	  dstripestart += dstripestep) {
-		fvscanstart = fstripestart;
-		dvscanstart = dstripestart;
-		vscanlen = JAS_MIN(i, 4);
-		for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
-			fp = fvscanstart;
-			dp = dvscanstart;
-			k = vscanlen;
-
-			rawrefpass_step(fp, dp, bitpos, one, nmsedec,
-			  out, vcausalflag);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			rawrefpass_step(fp, dp, bitpos, one, nmsedec,
-			  out, vcausalflag);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			rawrefpass_step(fp, dp, bitpos, one, nmsedec,
-			  out, vcausalflag);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			rawrefpass_step(fp, dp, bitpos, one, nmsedec,
-			  out, vcausalflag);
-
-		}
-	}
-
-	if (term) {
-		jpc_bitstream_outalign(out, 0x2a);
-	}
-
-	return 0;
+    int i;
+    int j;
+    int k;
+    int one;
+    int vscanlen;
+    int width;
+    int height;
+    int frowstep;
+    int drowstep;
+    int fstripestep;
+    int dstripestep;
+    jpc_fix_t *fstripestart;
+    jpc_fix_t *dstripestart;
+    jpc_fix_t *fvscanstart;
+    jpc_fix_t *dvscanstart;
+    jpc_fix_t *dp;
+    jpc_fix_t *fp;
+
+    *nmsedec = 0;
+    width = jas_matrix_numcols(data);
+    height = jas_matrix_numrows(data);
+    frowstep = jas_matrix_rowstep(flags);
+    drowstep = jas_matrix_rowstep(data);
+    fstripestep = frowstep << 2;
+    dstripestep = drowstep << 2;
+
+    one = 1 << (bitpos + JPC_NUMEXTRABITS);
+
+    fstripestart = jas_matrix_getref(flags, 1, 1);
+    dstripestart = jas_matrix_getref(data, 0, 0);
+    for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
+      dstripestart += dstripestep) {
+        fvscanstart = fstripestart;
+        dvscanstart = dstripestart;
+        vscanlen = JAS_MIN(i, 4);
+        for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
+            fp = fvscanstart;
+            dp = dvscanstart;
+            k = vscanlen;
+
+            rawrefpass_step(fp, dp, bitpos, one, nmsedec,
+              out, vcausalflag);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            rawrefpass_step(fp, dp, bitpos, one, nmsedec,
+              out, vcausalflag);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            rawrefpass_step(fp, dp, bitpos, one, nmsedec,
+              out, vcausalflag);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            rawrefpass_step(fp, dp, bitpos, one, nmsedec,
+              out, vcausalflag);
+
+        }
+    }
+
+    if (term) {
+        jpc_bitstream_outalign(out, 0x2a);
+    }
+
+    return 0;
 }
 
 /******************************************************************************\
 * Code for cleanup pass.
 \******************************************************************************/
 
-#define	clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, label1, label2, vcausalflag) \
+#define clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, label1, label2, vcausalflag) \
 { \
-	int f; \
-	int v; \
+    int f; \
+    int v; \
 label1 \
-	f = *(fp); \
-	if (!(f & (JPC_SIG | JPC_VISIT))) { \
-		jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \
-		v = (abs(*(dp)) & (one)) ? 1 : 0; \
-		jpc_mqenc_putbit((mqenc), v); \
-		if (v) { \
+    f = *(fp); \
+    if (!(f & (JPC_SIG | JPC_VISIT))) { \
+        jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \
+        v = (abs(*(dp)) & (one)) ? 1 : 0; \
+        jpc_mqenc_putbit((mqenc), v); \
+        if (v) { \
 label2 \
-			f = *(fp); \
-			/* Coefficient is significant. */ \
-			*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
-			jpc_mqenc_setcurctx((mqenc), JPC_GETSCCTXNO(f)); \
-			v = ((*(dp) < 0) ? 1 : 0); \
-			jpc_mqenc_putbit((mqenc), v ^ JPC_GETSPB(f)); \
-			JPC_UPDATEFLAGS4((fp), (frowstep), v, vcausalflag); \
-			*(fp) |= JPC_SIG; \
-		} \
-	} \
-	*(fp) &= ~JPC_VISIT; \
+            f = *(fp); \
+            /* Coefficient is significant. */ \
+            *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
+            jpc_mqenc_setcurctx((mqenc), JPC_GETSCCTXNO(f)); \
+            v = ((*(dp) < 0) ? 1 : 0); \
+            jpc_mqenc_putbit((mqenc), v ^ JPC_GETSPB(f)); \
+            JPC_UPDATEFLAGS4((fp), (frowstep), v, vcausalflag); \
+            *(fp) |= JPC_SIG; \
+        } \
+    } \
+    *(fp) &= ~JPC_VISIT; \
 }
 
 static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags,
   jas_matrix_t *data, int term, long *nmsedec)
 {
-	int i;
-	int j;
-	int k;
-	int vscanlen;
-	int v;
-	int runlen;
-	jpc_fix_t *fp;
-	int width;
-	int height;
-	jpc_fix_t *dp;
-	int one;
-	int frowstep;
-	int drowstep;
-	int fstripestep;
-	int dstripestep;
-	jpc_fix_t *fstripestart;
-	jpc_fix_t *dstripestart;
-	jpc_fix_t *fvscanstart;
-	jpc_fix_t *dvscanstart;
-
-	*nmsedec = 0;
-	width = jas_matrix_numcols(data);
-	height = jas_matrix_numrows(data);
-	frowstep = jas_matrix_rowstep(flags);
-	drowstep = jas_matrix_rowstep(data);
-	fstripestep = frowstep << 2;
-	dstripestep = drowstep << 2;
-
-	one = 1 << (bitpos + JPC_NUMEXTRABITS);
-
-	fstripestart = jas_matrix_getref(flags, 1, 1);
-	dstripestart = jas_matrix_getref(data, 0, 0);
-	for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
-	  dstripestart += dstripestep) {
-		fvscanstart = fstripestart;
-		dvscanstart = dstripestart;
-		vscanlen = JAS_MIN(i, 4);
-		for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
-
-			fp = fvscanstart;
-			if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT |
-			  JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG |
-			  JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) &
-			  (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep,
-			  !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) {
-				dp = dvscanstart;
-				for (k = 0; k < vscanlen; ++k) {
-					v = (abs(*dp) & one) ? 1 : 0;
-					if (v) {
-						break;
-					}
-					dp += drowstep;
-				}
-				runlen = k;
-				if (runlen >= 4) {
-					jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO);
-					jpc_mqenc_putbit(mqenc, 0);
-					continue;
-				}
-				jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO);
-				jpc_mqenc_putbit(mqenc, 1);
-				jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO);
-				jpc_mqenc_putbit(mqenc, runlen >> 1);
-				jpc_mqenc_putbit(mqenc, runlen & 1);
-				fp = fvscanstart + frowstep * runlen;
-				dp = dvscanstart + drowstep * runlen;
-				k = vscanlen - runlen;
-				switch (runlen) {
-				case 0:
-					goto clnpass_partial0;
-					break;
-				case 1:
-					goto clnpass_partial1;
-					break;
-				case 2:
-					goto clnpass_partial2;
-					break;
-				case 3:
-					goto clnpass_partial3;
-					break;
-				}
-			} else {
-				runlen = 0;
-				fp = fvscanstart;
-				dp = dvscanstart;
-				k = vscanlen;
-				goto clnpass_full0;
-			}
-			clnpass_step(fp, frowstep, dp, bitpos, one,
-			  orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			clnpass_step(fp, frowstep, dp, bitpos, one,
-				orient, nmsedec, mqenc, ;, clnpass_partial1:, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			clnpass_step(fp, frowstep, dp, bitpos, one,
-				orient, nmsedec, mqenc, ;, clnpass_partial2:, 0);
-			if (--k <= 0) {
-				continue;
-			}
-			fp += frowstep;
-			dp += drowstep;
-			clnpass_step(fp, frowstep, dp, bitpos, one,
-				orient, nmsedec, mqenc, ;, clnpass_partial3:, 0);
-		}
-	}
-
-	if (segsymflag) {
-		jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO);
-		jpc_mqenc_putbit(mqenc, 1);
-		jpc_mqenc_putbit(mqenc, 0);
-		jpc_mqenc_putbit(mqenc, 1);
-		jpc_mqenc_putbit(mqenc, 0);
-	}
-
-	if (term) {
-		jpc_mqenc_flush(mqenc, term - 1);
-	}
-
-	return jpc_mqenc_error(mqenc) ? (-1) : 0;
+    int i;
+    int j;
+    int k;
+    int vscanlen;
+    int v;
+    int runlen;
+    jpc_fix_t *fp;
+    int width;
+    int height;
+    jpc_fix_t *dp;
+    int one;
+    int frowstep;
+    int drowstep;
+    int fstripestep;
+    int dstripestep;
+    jpc_fix_t *fstripestart;
+    jpc_fix_t *dstripestart;
+    jpc_fix_t *fvscanstart;
+    jpc_fix_t *dvscanstart;
+
+    *nmsedec = 0;
+    width = jas_matrix_numcols(data);
+    height = jas_matrix_numrows(data);
+    frowstep = jas_matrix_rowstep(flags);
+    drowstep = jas_matrix_rowstep(data);
+    fstripestep = frowstep << 2;
+    dstripestep = drowstep << 2;
+
+    one = 1 << (bitpos + JPC_NUMEXTRABITS);
+
+    fstripestart = jas_matrix_getref(flags, 1, 1);
+    dstripestart = jas_matrix_getref(data, 0, 0);
+    for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
+      dstripestart += dstripestep) {
+        fvscanstart = fstripestart;
+        dvscanstart = dstripestart;
+        vscanlen = JAS_MIN(i, 4);
+        for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
+
+            fp = fvscanstart;
+            if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT |
+              JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG |
+              JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) &
+              (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep,
+              !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) {
+                dp = dvscanstart;
+                for (k = 0; k < vscanlen; ++k) {
+                    v = (abs(*dp) & one) ? 1 : 0;
+                    if (v) {
+                        break;
+                    }
+                    dp += drowstep;
+                }
+                runlen = k;
+                if (runlen >= 4) {
+                    jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO);
+                    jpc_mqenc_putbit(mqenc, 0);
+                    continue;
+                }
+                jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO);
+                jpc_mqenc_putbit(mqenc, 1);
+                jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO);
+                jpc_mqenc_putbit(mqenc, runlen >> 1);
+                jpc_mqenc_putbit(mqenc, runlen & 1);
+                fp = fvscanstart + frowstep * runlen;
+                dp = dvscanstart + drowstep * runlen;
+                k = vscanlen - runlen;
+                switch (runlen) {
+                case 0:
+                    goto clnpass_partial0;
+                    break;
+                case 1:
+                    goto clnpass_partial1;
+                    break;
+                case 2:
+                    goto clnpass_partial2;
+                    break;
+                case 3:
+                    goto clnpass_partial3;
+                    break;
+                }
+            } else {
+                runlen = 0;
+                fp = fvscanstart;
+                dp = dvscanstart;
+                k = vscanlen;
+                goto clnpass_full0;
+            }
+            clnpass_step(fp, frowstep, dp, bitpos, one,
+              orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            clnpass_step(fp, frowstep, dp, bitpos, one,
+                orient, nmsedec, mqenc, ;, clnpass_partial1:, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            clnpass_step(fp, frowstep, dp, bitpos, one,
+                orient, nmsedec, mqenc, ;, clnpass_partial2:, 0);
+            if (--k <= 0) {
+                continue;
+            }
+            fp += frowstep;
+            dp += drowstep;
+            clnpass_step(fp, frowstep, dp, bitpos, one,
+                orient, nmsedec, mqenc, ;, clnpass_partial3:, 0);
+        }
+    }
+
+    if (segsymflag) {
+        jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO);
+        jpc_mqenc_putbit(mqenc, 1);
+        jpc_mqenc_putbit(mqenc, 0);
+        jpc_mqenc_putbit(mqenc, 1);
+        jpc_mqenc_putbit(mqenc, 0);
+    }
+
+    if (term) {
+        jpc_mqenc_flush(mqenc, term - 1);
+    }
+
+    return jpc_mqenc_error(mqenc) ? (-1) : 0;
 }
diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_util.c b/converter/other/jpeg2000/libjasper/jpc/jpc_util.c
index ecc4b914..fede2bef 100644
--- a/converter/other/jpeg2000/libjasper/jpc/jpc_util.c
+++ b/converter/other/jpeg2000/libjasper/jpc/jpc_util.c
@@ -6,14 +6,14 @@
  */
 
 /* __START_OF_JASPER_LICENSE__
- * 
+ *
  * JasPer Software License
- * 
+ *
  * IMAGE POWER JPEG-2000 PUBLIC LICENSE
  * ************************************
- * 
+ *
  * GRANT:
- * 
+ *
  * Permission is hereby granted, free of charge, to any person (the "User")
  * obtaining a copy of this software and associated documentation, to deal
  * in the JasPer Software without restriction, including without limitation
@@ -21,22 +21,22 @@
  * and/or sell copies of the JasPer Software (in source and binary forms),
  * and to permit persons to whom the JasPer Software is furnished to do so,
  * provided further that the License Conditions below are met.
- * 
+ *
  * License Conditions
  * ******************
- * 
+ *
  * A.  Redistributions of source code must retain the above copyright notice,
  * and this list of conditions, and the following disclaimer.
- * 
+ *
  * B.  Redistributions in binary form must reproduce the above copyright
  * notice, and this list of conditions, and the following disclaimer in
  * the documentation and/or other materials provided with the distribution.
- * 
+ *
  * C.  Neither the name of Image Power, Inc. nor any other contributor
  * (including, but not limited to, the University of British Columbia and
  * Michael David Adams) may be used to endorse or promote products derived
  * from this software without specific prior written permission.
- * 
+ *
  * D.  User agrees that it shall not commence any action against Image Power,
  * Inc., the University of British Columbia, Michael David Adams, or any
  * other contributors (collectively "Licensors") for infringement of any
@@ -56,17 +56,17 @@
  * trade dress, or service mark rights); and (v) divisions, continuations,
  * renewals, reissues and extensions of the foregoing (as and to the extent
  * applicable) now existing, hereafter filed, issued or acquired.
- * 
+ *
  * E.  If User commences an infringement action against any Licensor(s) then
  * such Licensor(s) shall have the right to terminate User's license and
  * all sublicenses that have been granted hereunder by User to other parties.
- * 
+ *
  * F.  This software is for use only in hardware or software products that
  * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
  * or right to this Software is granted for products that do not comply
  * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
  * from the ISO.
- * 
+ *
  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
  * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
@@ -106,7 +106,7 @@
  * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
  * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
  * NOTICE SPECIFIED IN THIS SECTION.
- * 
+ *
  * __END_OF_JASPER_LICENSE__
  */
 
diff --git a/converter/other/jpeg2000/pamtojpeg2k.c b/converter/other/jpeg2000/pamtojpeg2k.c
index 142e452f..4d73316a 100644
--- a/converter/other/jpeg2000/pamtojpeg2k.c
+++ b/converter/other/jpeg2000/pamtojpeg2k.c
@@ -8,6 +8,7 @@
 
 *****************************************************************************/
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1    /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
     /* In 2014.09, this was _XOPEN_SOURCE 600, with a comment saying it was
@@ -97,7 +98,7 @@ parseCommandLine(int argc, char ** argv,
     char * modeOpt;
 
     unsigned int option_def_index;
-    
+
     MALLOCARRAY_NOFAIL(option_def, 100);
 
     option_def_index = 0;   /* incremented by OPTENTRY */
@@ -133,25 +134,25 @@ parseCommandLine(int argc, char ** argv,
             &numrlvlsSpec,       0);
     OPTENT3(0, "numgbits",     OPT_UINT,   &cmdlineP->numgbits,
             &numgbitsSpec,       0);
-    OPTENT3(0, "nomct",        OPT_FLAG,   NULL, 
+    OPTENT3(0, "nomct",        OPT_FLAG,   NULL,
             &cmdlineP->nomct,    0);
-    OPTENT3(0, "sop",          OPT_FLAG,   NULL, 
+    OPTENT3(0, "sop",          OPT_FLAG,   NULL,
             &cmdlineP->sop,      0);
-    OPTENT3(0, "eph",          OPT_FLAG,   NULL, 
+    OPTENT3(0, "eph",          OPT_FLAG,   NULL,
             &cmdlineP->eph,      0);
-    OPTENT3(0, "lazy",         OPT_FLAG,   NULL, 
+    OPTENT3(0, "lazy",         OPT_FLAG,   NULL,
             &cmdlineP->lazy,     0);
-    OPTENT3(0, "termall",      OPT_FLAG,   NULL, 
+    OPTENT3(0, "termall",      OPT_FLAG,   NULL,
             &cmdlineP->termall,  0);
-    OPTENT3(0, "segsym",       OPT_FLAG,   NULL, 
+    OPTENT3(0, "segsym",       OPT_FLAG,   NULL,
             &cmdlineP->segsym,    0);
-    OPTENT3(0, "vcausal",      OPT_FLAG,   NULL, 
+    OPTENT3(0, "vcausal",      OPT_FLAG,   NULL,
             &cmdlineP->vcausal,   0);
-    OPTENT3(0, "pterm",        OPT_FLAG,   NULL, 
+    OPTENT3(0, "pterm",        OPT_FLAG,   NULL,
             &cmdlineP->pterm,     0);
-    OPTENT3(0, "resetprob",    OPT_FLAG,   NULL, 
+    OPTENT3(0, "resetprob",    OPT_FLAG,   NULL,
             &cmdlineP->resetprob, 0);
-    OPTENT3(0, "verbose",      OPT_FLAG,   NULL, 
+    OPTENT3(0, "verbose",      OPT_FLAG,   NULL,
             &cmdlineP->verbose,   0);
     OPTENT3(0, "debuglevel",   OPT_UINT,   &cmdlineP->debuglevel,
             &debuglevelSpec,      0);
@@ -222,7 +223,7 @@ parseCommandLine(int argc, char ** argv,
         cmdlineP->inputFilename = strdup("-");  /* he wants stdin */
     else if (argc - 1 == 1)
         cmdlineP->inputFilename = strdup(argv[1]);
-    else 
+    else
         pm_error("Too many arguments.  The only argument accepted\n"
                  "is the input file specification");
 
@@ -231,7 +232,7 @@ parseCommandLine(int argc, char ** argv,
 
 
 static void
-createJasperRaster(struct pam *  const inpamP, 
+createJasperRaster(struct pam *  const inpamP,
                    jas_image_t * const jasperP) {
 /*----------------------------------------------------------------------------
    Create the raster in the *jasperP object, reading the raster from the
@@ -253,7 +254,7 @@ createJasperRaster(struct pam *  const inpamP,
         if (matrix[plane] == NULL)
             pm_error("Unable to create matrix for plane %u.  "
                      "jas_matrix_create() failed.", plane);
-    }   
+    }
     tuplerow = pnm_allocpamrow(inpamP);
 
     jasperMaxval = pm_bitstomaxval(pm_maxvaltobits(inpamP->maxval));
@@ -270,7 +271,7 @@ createJasperRaster(struct pam *  const inpamP,
                 unsigned int jasperSample;
 
                 if (oddMaxval)
-                    jasperSample = tuplerow[col][plane] * 
+                    jasperSample = tuplerow[col][plane] *
                         jasperMaxval / inpamP->maxval;
                 else
                     jasperSample = tuplerow[col][plane];
@@ -278,16 +279,16 @@ createJasperRaster(struct pam *  const inpamP,
                 jas_matrix_set(matrix[plane], 0, col, jasperSample);
             }
         }
-        { 
+        {
             unsigned int plane;
 
             for (plane = 0; plane < inpamP->depth; ++plane) {
                 int rc;
-                rc = jas_image_writecmpt(jasperP, plane, 0, row, 
+                rc = jas_image_writecmpt(jasperP, plane, 0, row,
                                          inpamP->width, 1,
                                          matrix[plane]);
                 if (rc != 0)
-                    pm_error("jas_image_writecmpt() of plane %u failed.", 
+                    pm_error("jas_image_writecmpt() of plane %u failed.",
                              plane);
             }
         }
@@ -296,14 +297,14 @@ createJasperRaster(struct pam *  const inpamP,
     pnm_freepamrow(tuplerow);
     for (plane = 0; plane < inpamP->depth; ++plane)
         jas_matrix_destroy(matrix[plane]);
-    
+
     free(matrix);
 }
 
 
 
 static void
-createJasperImage(struct pam *   const inpamP, 
+createJasperImage(struct pam *   const inpamP,
                   jas_image_t ** const jasperPP) {
 
 	jas_image_cmptparm_t * cmptparms;
@@ -321,7 +322,7 @@ createJasperImage(struct pam *   const inpamP,
         cmptparms[plane].prec = pm_maxvaltobits(inpamP->maxval);
         cmptparms[plane].sgnd = 0;
     }
-    *jasperPP = 
+    *jasperPP =
         jas_image_create(inpamP->depth, cmptparms, JAS_CLRSPC_UNKNOWN);
     if (*jasperPP == NULL)
         pm_error("Unable to create jasper image structure.  "
@@ -340,7 +341,7 @@ convertToJasperImage(struct pam *   const inpamP,
 
     createJasperImage(inpamP, &jasperP);
 
-    if (strncmp(inpamP->tuple_type, "RGB", 3) == 0) {
+    if (strneq(inpamP->tuple_type, "RGB", 3)) {
         if (inpamP->depth < 3)
             pm_error("Input tuple type is RGB*, but depth is only %d.  "
                      "It should be at least 3.", inpamP->depth);
@@ -354,8 +355,8 @@ convertToJasperImage(struct pam *   const inpamP,
                                   JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B));
         }
     } else {
-        if (strncmp(inpamP->tuple_type, "GRAYSCALE", 9) == 0 ||
-            strncmp(inpamP->tuple_type, "BLACKANDWHITE", 13) == 0) {
+        if (strneq(inpamP->tuple_type, "GRAYSCALE", 9) ||
+            strneq(inpamP->tuple_type, "BLACKANDWHITE", 13)) {
             jas_image_setclrspc(jasperP, JAS_CLRSPC_GENGRAY);
             jas_image_setcmpttype(jasperP, 0,
                                   JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y));
@@ -370,7 +371,7 @@ convertToJasperImage(struct pam *   const inpamP,
 
 
 static void
-writeJpc(jas_image_t *      const jasperP, 
+writeJpc(jas_image_t *      const jasperP,
          struct cmdlineInfo const cmdline,
          FILE *             const ofP) {
 
@@ -382,8 +383,8 @@ writeJpc(jas_image_t *      const jasperP,
 
     /* Note: ilyrrates is a hack because we're too lazy to properly parse
        command line options to get the information and then compose
-       a proper input to Jasper.  So the user can screw things up by 
-       specifying garbage for the -ilyrrates option 
+       a proper input to Jasper.  So the user can screw things up by
+       specifying garbage for the -ilyrrates option
     */
     if (strlen(cmdline.ilyrrates) > 0)
         pm_asprintf(&ilyrratesOpt, "ilyrrates=%s", cmdline.ilyrrates);
@@ -409,7 +410,7 @@ writeJpc(jas_image_t *      const jasperP,
         */
         rateOpt[0] = '\0';
     }
-    pm_asprintf(&options, 
+    pm_asprintf(&options,
                 "imgareatlx=%u "
                 "imgareatly=%u "
                 "tilegrdtlx=%u "
@@ -427,7 +428,7 @@ writeJpc(jas_image_t *      const jasperP,
                 "numrlvls=%u "
                 "numgbits=%u "
                 "%s %s %s %s %s %s %s %s %s",
-                
+
                 cmdline.imgareatlx,
                 cmdline.imgareatly,
                 cmdline.tilegrdtlx,
@@ -470,8 +471,8 @@ writeJpc(jas_image_t *      const jasperP,
             pm_message("Using Jasper to encode to 'jpc' format with options "
                        "'%s'", options);
 
-        rc = jas_image_encode(jasperP, outStreamP, 
-                              jas_image_strtofmt((char*)"jpc"), 
+        rc = jas_image_encode(jasperP, outStreamP,
+                              jas_image_strtofmt((char*)"jpc"),
                               (char *)options);
         if (rc != 0)
             pm_error("jas_image_encode() failed to encode the JPEG 2000 "
@@ -483,11 +484,11 @@ writeJpc(jas_image_t *      const jasperP,
         int rc;
 
         rc = jas_stream_close(outStreamP);
-            
+
         if (rc != 0)
             pm_error("Failed to close output stream, "
                      "jas_stream_close() rc = %d", rc);
-    }                     
+    }
 
 	jas_image_clearfmts();
 
@@ -505,33 +506,33 @@ main(int argc, char **argv)
     jas_image_t * jasperP;
 
     pnm_init(&argc, argv);
-    
+
     parseCommandLine(argc, argv, &cmdline);
-    
-    { 
+
+    {
         int rc;
-        
+
         rc = jas_init();
         if ( rc != 0 )
             pm_error("Failed to initialize Jasper library.  "
                      "jas_init() returns rc %d", rc );
     }
-    
+
     jas_setdbglevel(cmdline.debuglevel);
-    
+
     ifP = pm_openr(cmdline.inputFilename);
-    
+
     pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
-    
+
     convertToJasperImage(&inpam, &jasperP);
-    
+
     writeJpc(jasperP, cmdline, stdout);
-    
+
 	jas_image_destroy(jasperP);
 
     pm_close(ifP);
 
     pm_close(stdout);
-    
+
     return 0;
 }
diff --git a/converter/other/jpegtopnm.c b/converter/other/jpegtopnm.c
index ab3b18e5..98552c00 100644
--- a/converter/other/jpegtopnm.c
+++ b/converter/other/jpegtopnm.c
@@ -48,6 +48,7 @@
     
 *****************************************************************************/
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/other/pamtogif.c b/converter/other/pamtogif.c
index aabf7fc2..8d432da1 100644
--- a/converter/other/pamtogif.c
+++ b/converter/other/pamtogif.c
@@ -8,6 +8,7 @@
 
 #include <assert.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include "pm_c_util.h"
 #include "mallocvar.h"
@@ -23,7 +24,7 @@ static unsigned int const gifMaxval = 255;
 static bool verbose;
 
 
-typedef int stringCode;
+typedef unsigned int StringCode;
     /* A code to be place in the GIF raster.  It represents
        a string of one or more pixels.  You interpret this in the context
        of a current code size.  The lower half of the values representable
@@ -34,15 +35,11 @@ typedef int stringCode;
        strings.  The mapping between value and the sequence of pixels
        changes throughout the image.
 
-       A variable of this type sometimes has the value -1 instead of
-       a string code because of cheesy programming.
-
-       Ergo, this data structure must be signed and at least BITS bits
-       wide plus sign bit.
+       Ergo, this data structure must be at least BITS bits wide.
     */
 
 
-struct cmap {
+struct Cmap {
     /* This is the information for the GIF colormap (aka palette). */
 
     struct pam pam;
@@ -67,7 +64,7 @@ struct cmap {
         /* A hash table to translate color to GIF colormap index. */
 };
 
-struct cmdlineInfo {
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
@@ -79,6 +76,7 @@ struct cmdlineInfo {
     const char *transparent;    /* -transparent option value.  NULL if none. */
     const char *comment;        /* -comment option value; NULL if none */
     unsigned int nolzw;         /* -nolzw option */
+    unsigned int noclear;       /* -noclear option */
     float aspect;               /* -aspect option value (the ratio).  */
     unsigned int verbose;
 };
@@ -100,7 +98,7 @@ pamAlphaPlane(struct pam * const pamP) {
         alphaPlane = 2;
     else
         alphaPlane = 0;
-    
+
     if (alphaPlane >= pamP->depth)
         pm_error("Tuple type is '%s', but depth (%u) is less than %u",
                  pamP->tuple_type, pamP->depth, alphaPlane + 1);
@@ -112,7 +110,7 @@ pamAlphaPlane(struct pam * const pamP) {
 
 static void
 parseCommandLine(int argc, char ** argv,
-                 struct cmdlineInfo * const cmdlineP) {
+                 struct CmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    Parse the program arguments (given by argc and argv) into a form
    the program can deal with more easily -- a cmdline_info structure.
@@ -131,30 +129,32 @@ parseCommandLine(int argc, char ** argv,
     MALLOCARRAY_NOFAIL(option_def, 100);
 
     option_def_index = 0;   /* incremented by OPTENT3 */
-    OPTENT3(0,   "interlace",   OPT_FLAG,   
+    OPTENT3(0,   "interlace",   OPT_FLAG,
             NULL,                       &cmdlineP->interlace, 0);
-    OPTENT3(0,   "sort",        OPT_FLAG,   
+    OPTENT3(0,   "sort",        OPT_FLAG,
             NULL,                       &cmdlineP->sort, 0);
-    OPTENT3(0,   "nolzw",       OPT_FLAG,   
+    OPTENT3(0,   "nolzw",       OPT_FLAG,
             NULL,                       &cmdlineP->nolzw, 0);
-    OPTENT3(0,   "mapfile",     OPT_STRING, 
+    OPTENT3(0,   "noclear",     OPT_FLAG,
+            NULL,                       &cmdlineP->noclear, 0);
+    OPTENT3(0,   "mapfile",     OPT_STRING,
             &cmdlineP->mapfile,        NULL, 0);
-    OPTENT3(0,   "transparent", OPT_STRING, 
+    OPTENT3(0,   "transparent", OPT_STRING,
             &cmdlineP->transparent,    NULL, 0);
-    OPTENT3(0,   "comment",     OPT_STRING, 
+    OPTENT3(0,   "comment",     OPT_STRING,
             &cmdlineP->comment,        NULL, 0);
-    OPTENT3(0,   "alphacolor",  OPT_STRING, 
+    OPTENT3(0,   "alphacolor",  OPT_STRING,
             &cmdlineP->alphacolor,     NULL, 0);
-    OPTENT3(0,   "aspect",      OPT_FLOAT, 
+    OPTENT3(0,   "aspect",      OPT_FLOAT,
             &cmdlineP->aspect,         &aspectSpec, 0);
-    OPTENT3(0,   "verbose",     OPT_FLAG, 
+    OPTENT3(0,   "verbose",     OPT_FLAG,
             NULL,                      &cmdlineP->verbose, 0);
-    
+
     /* Set the defaults */
     cmdlineP->mapfile = NULL;
     cmdlineP->transparent = NULL;  /* no transparency */
     cmdlineP->comment = NULL;      /* no comment */
-    cmdlineP->alphacolor = "rgb:0/0/0";      
+    cmdlineP->alphacolor = "rgb:0/0/0";
         /* We could say "black" here, but then we depend on the color names
            database existing.
         */
@@ -166,15 +166,15 @@ parseCommandLine(int argc, char ** argv,
     pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
-    if (argc-1 == 0) 
+    if (argc-1 == 0)
         cmdlineP->input_filespec = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
                  "specified %d", argc-1);
     else
         cmdlineP->input_filespec = argv[1];
-        
-    if (aspectSpec) { 
+
+    if (aspectSpec) {
         if (cmdlineP->aspect < 0.25  || cmdlineP->aspect > 4.21875)
             pm_error("Invalid -aspect value: %f.  "
                      "GIF allows only the range 0.25-4.0 .",
@@ -205,10 +205,10 @@ Putword(int const w, FILE * const fp) {
 static int
 closestColor(tuple         const color,
              struct pam *  const pamP,
-             struct cmap * const cmapP) {
+             struct Cmap * const cmapP) {
 /*----------------------------------------------------------------------------
    Return the colormap index of the color in the colormap *cmapP
-   that is closest to the color 'color', whose format is specified by 
+   that is closest to the color 'color', whose format is specified by
    *pamP.
 
    Also add 'color' to the colormap hash, with the colormap index we
@@ -217,7 +217,7 @@ closestColor(tuple         const color,
 -----------------------------------------------------------------------------*/
     unsigned int const nComp = pamP->depth >= 3 ? 3 : 1;
         /* Number of color components (not alpha) in 'color' */
-    
+
     unsigned int i;
     unsigned int imin, dmin;
     int fits;
@@ -234,8 +234,8 @@ closestColor(tuple         const color,
 
         if (distance < dmin) {
             dmin = distance;
-            imin = i; 
-        } 
+            imin = i;
+        }
     }
     pnm_addtotuplehash(pamP, cmapP->tuplehash, color, imin, &fits);
 
@@ -268,16 +268,16 @@ typedef struct {
         /* A bitbucket for rows we read in order to advance the file
            position.
         */
-} rowReader;
+} RowReader;
 
 
 
-static rowReader *
+static RowReader *
 rowReader_create(struct pam * const pamP,
                  pm_filepos   const rasterPos,
                  bool         const interlace) {
 
-    rowReader * rdrP;
+    RowReader * rdrP;
 
     MALLOCVAR_NOFAIL(rdrP);
 
@@ -298,7 +298,7 @@ rowReader_create(struct pam * const pamP,
 
 
 static void
-rowReader_destroy(rowReader * const rdrP) {
+rowReader_destroy(RowReader * const rdrP) {
 
     pnm_freepamrow(rdrP->discardBuffer);
     free(rdrP);
@@ -307,7 +307,7 @@ rowReader_destroy(rowReader * const rdrP) {
 
 
 static void
-rowReaderSkipRows(rowReader *  const rdrP,
+rowReaderSkipRows(RowReader *  const rdrP,
                   unsigned int const rowCount,
                   bool *       const eofP) {
 /*----------------------------------------------------------------------------
@@ -338,7 +338,7 @@ rowReaderSkipRows(rowReader *  const rdrP,
 
 
 static void
-rowReaderGotoNextInterlaceRow(rowReader * const rdrP) {
+rowReaderGotoNextInterlaceRow(RowReader * const rdrP) {
 /*----------------------------------------------------------------------------
   Position reader to the next row in the interlace pattern, assuming it
   is now positioned immediately after the current row.
@@ -351,7 +351,7 @@ rowReaderGotoNextInterlaceRow(rowReader * const rdrP) {
        MULT4PLUS2: Rows 2, 6, 10, 14, etc.
        MULT2PLUS1: Rows 1, 3, 5, 7, 9, etc.
     */
-    
+
     switch (rdrP->pass) {
     case MULT8PLUS0:
         rowReaderSkipRows(rdrP, 7, &endOfPass);
@@ -399,7 +399,7 @@ rowReaderGotoNextInterlaceRow(rowReader * const rdrP) {
 
 
 static void
-rowReaderGotoNextStraightRow(rowReader * const rdrP) {
+rowReaderGotoNextStraightRow(RowReader * const rdrP) {
 /*----------------------------------------------------------------------------
   Position reader to the next row in a straight, non-interlace
   pattern, assuming the file is now positioned immediately after the
@@ -415,7 +415,7 @@ rowReaderGotoNextStraightRow(rowReader * const rdrP) {
 
 
 static void
-rowReader_read(rowReader * const rdrP,
+rowReader_read(RowReader * const rdrP,
                tuple *     const tuplerow) {
 
     if (rdrP->eof)
@@ -424,7 +424,7 @@ rowReader_read(rowReader * const rdrP,
 
     pnm_readpamrow(&rdrP->pam, tuplerow);
     ++rdrP->nextRow;
-    
+
     if (rdrP->interlace)
         rowReaderGotoNextInterlaceRow(rdrP);
     else
@@ -437,8 +437,8 @@ static unsigned int
 gifPixel(struct pam *   const pamP,
          tuple          const tuple,
          unsigned int   const alphaPlane,
-         sample         const alphaThreshold, 
-         struct cmap *  const cmapP) {
+         sample         const alphaThreshold,
+         struct Cmap *  const cmapP) {
 /*----------------------------------------------------------------------------
    Return as *colorIndexP the colormap index of the tuple 'tuple',
    whose format is described by *pamP, using colormap *cmapP.
@@ -456,7 +456,7 @@ gifPixel(struct pam *   const pamP,
 
         pnm_lookuptuple(pamP, cmapP->tuplehash, tuple,
                         &found, &colorIndex);
-        
+
         if (!found)
             colorIndex = closestColor(tuple, pamP, cmapP);
     }
@@ -493,13 +493,13 @@ writeCommentExtension(FILE * const ofP,
     unsigned int const maxSegmentSize = 255;
 
     const char * segment;
-    
+
     fputc('!',  ofP);   /* Identifies an extension */
     fputc(0xfe, ofP);   /* Identifies a comment */
 
     /* Write it out in segments no longer than 255 characters */
-    for (segment = &comment[0]; 
-         segment < comment + strlen(comment); 
+    for (segment = &comment[0];
+         segment < comment + strlen(comment);
          segment += maxSegmentSize) {
 
         unsigned int const lengthThisSegment =
@@ -546,20 +546,30 @@ writeCommentExtension(FILE * const ofP,
  */
 
 
-static stringCode const maxCodeLimitLzw = (stringCode)1 << BITS;
-       /* One beyond the largest string code that can exist in GIF */ 
+static StringCode const maxCodeLimitLzw = (StringCode)1 << BITS;
+       /* One beyond the largest string code that can exist in GIF */
        /* Used only in assertions  */
 
 
-struct hashTableEntry {
-    stringCode fcode;   /* -1 means unused slot */
-    unsigned int ent;
-};    
+struct HashTableEntry {
+    /* This is an entry in the string table, which is a hash table.  It says
+       that the string code 'combinedString' represents the string which is
+       the single pixel 'additionalPixel' appended to 'baseString', where
+       'baseString' may represent a multi-pixel string.
+    */
+    bool present;
+        /* There is an entry here.  Following members are meaningless if
+           not.
+        */
+    StringCode baseString;
+    StringCode additionalPixel;
+    StringCode combinedString;
+};
 
 
 
 /***************************************************************************
-*                          BYTE OUTPUTTER                 
+*                          BYTE OUTPUTTER
 ***************************************************************************/
 
 typedef struct {
@@ -568,14 +578,14 @@ typedef struct {
         /* Number of bytes so far in the current data block */
     unsigned char buffer[256];
         /* The current data block, under construction */
-} byteBuffer;
+} ByteBuffer;
 
 
 
-static byteBuffer *
+static ByteBuffer *
 byteBuffer_create(FILE * const fileP) {
 
-    byteBuffer * byteBufferP;
+    ByteBuffer * byteBufferP;
 
     MALLOCVAR_NOFAIL(byteBufferP);
 
@@ -588,7 +598,7 @@ byteBuffer_create(FILE * const fileP) {
 
 
 static void
-byteBuffer_destroy(byteBuffer * const byteBufferP) {
+byteBuffer_destroy(ByteBuffer * const byteBufferP) {
 
     free(byteBufferP);
 }
@@ -596,9 +606,9 @@ byteBuffer_destroy(byteBuffer * const byteBufferP) {
 
 
 static void
-byteBuffer_flush(byteBuffer * const byteBufferP) {
+byteBuffer_flush(ByteBuffer * const byteBufferP) {
 /*----------------------------------------------------------------------------
-   Write the current data block to the output file, then reset the current 
+   Write the current data block to the output file, then reset the current
    data block to empty.
 -----------------------------------------------------------------------------*/
     if (byteBufferP->count > 0 ) {
@@ -613,10 +623,10 @@ byteBuffer_flush(byteBuffer * const byteBufferP) {
 
 
 static void
-byteBuffer_flushFile(byteBuffer * const byteBufferP) {
-    
+byteBuffer_flushFile(ByteBuffer * const byteBufferP) {
+
     fflush(byteBufferP->fileP);
-    
+
     if (ferror(byteBufferP->fileP))
         pm_error("error writing output file");
 }
@@ -624,7 +634,7 @@ byteBuffer_flushFile(byteBuffer * const byteBufferP) {
 
 
 static void
-byteBuffer_out(byteBuffer *  const byteBufferP,
+byteBuffer_out(ByteBuffer *  const byteBufferP,
                unsigned char const c) {
 /*----------------------------------------------------------------------------
   Add a byte to the end of the current data block, and if it is now 255
@@ -638,39 +648,40 @@ byteBuffer_out(byteBuffer *  const byteBufferP,
 
 
 /***************************************************************************
-*                          GIF CODE OUTPUTTER                 
+*                          GIF CODE OUTPUTTER
 ***************************************************************************/
 
 typedef struct {
-    byteBuffer * byteBufferP;
+    ByteBuffer * byteBufferP;
     unsigned int initBits;
     unsigned int nBits;
         /* Number of bits to put in output for each code */
-    stringCode maxCode;                  /* maximum code, given n_bits */
-    stringCode maxCodeLimit;
+    StringCode maxCode;                  /* maximum code, given n_bits */
+    StringCode maxCodeLimit;
         /* LZW: One beyond the largest string code that can exist in GIF.
            Uncompressed: a ceiling to prevent code size from ratcheting up.
            In either case, output code never reaches this value.
-        */  
+        */
     unsigned long curAccum;
-    int curBits;
-    unsigned int codeCount;
-        /* Number of codes that have been output to this buffer (doesn't
-           matter if they have gone out the other side yet or not) since
-           the last flush (or ever, if no last flush).  The main use of this
-           is debugging -- when something fails, you can see in a debugger
-           where in the image it was, then set a trap for there.
+    unsigned int curBits;
+    unsigned int stringCount;
+        /* Number of strings that have been output to this buffer (by writing
+           a string code) since the last flush (or ever, if no last flush).
+           Note that this counts only strings that go into the buffer; it
+           doesn't matter if they have gone out the other side yet.  The main
+           use of this is debugging -- when something fails, you can see in a
+           debugger where in the image it was, then set a trap for there.
         */
-} codeBuffer;
+} CodeBuffer;
 
 
 
-static codeBuffer *
+static CodeBuffer *
 codeBuffer_create(FILE *       const ofP,
                   unsigned int const initBits,
                   bool         const lzw) {
 
-    codeBuffer * codeBufferP;
+    CodeBuffer * codeBufferP;
 
     MALLOCVAR_NOFAIL(codeBufferP);
 
@@ -678,11 +689,11 @@ codeBuffer_create(FILE *       const ofP,
     codeBufferP->nBits       = codeBufferP->initBits;
     codeBufferP->maxCode     = (1 << codeBufferP->nBits) - 1;
     codeBufferP->maxCodeLimit = lzw ?
-        (stringCode)1 << BITS : (stringCode) (1 << codeBufferP->nBits) - 1; 
+        (StringCode)1 << BITS : (StringCode) (1 << codeBufferP->nBits) - 1;
     codeBufferP->byteBufferP = byteBuffer_create(ofP);
     codeBufferP->curAccum    = 0;
     codeBufferP->curBits     = 0;
-    codeBufferP->codeCount   = 0;
+    codeBufferP->stringCount = 0;
 
     return codeBufferP;
 }
@@ -690,7 +701,7 @@ codeBuffer_create(FILE *       const ofP,
 
 
 static void
-codeBuffer_destroy(codeBuffer * const codeBufferP) {
+codeBuffer_destroy(CodeBuffer * const codeBufferP) {
 
     byteBuffer_destroy(codeBufferP->byteBufferP);
 
@@ -700,7 +711,7 @@ codeBuffer_destroy(codeBuffer * const codeBufferP) {
 
 
 static void
-codeBuffer_resetCodeSize(codeBuffer * const codeBufferP) {
+codeBuffer_resetCodeSize(CodeBuffer * const codeBufferP) {
 
     codeBufferP->nBits = codeBufferP->initBits;
 
@@ -712,7 +723,7 @@ codeBuffer_resetCodeSize(codeBuffer * const codeBufferP) {
 
 
 static void
-codeBuffer_increaseCodeSize(codeBuffer * const codeBufferP) {
+codeBuffer_increaseCodeSize(CodeBuffer * const codeBufferP) {
 
     ++codeBufferP->nBits;
 
@@ -721,18 +732,16 @@ codeBuffer_increaseCodeSize(codeBuffer * const codeBufferP) {
     codeBufferP->maxCode = (1 << codeBufferP->nBits) - 1;
 }
 
+
+
 static void
-codeBuffer_output(codeBuffer * const codeBufferP,
-                  stringCode   const code) {
+codeBuffer_output(CodeBuffer * const codeBufferP,
+                  StringCode   const code) {
 /*----------------------------------------------------------------------------
    Output one GIF code to the file, through the code buffer.
 
    The code is represented as N bits in the file -- the lower
    N bits of 'code'.  N is a the current code size of *codeBufferP.
-   
-   Id 'code' is the maximum possible code for the current code size
-   for *codeBufferP, increase that code size (unless it's already 
-   maxed out).
 -----------------------------------------------------------------------------*/
     assert (code <= codeBufferP->maxCode);
 
@@ -752,13 +761,13 @@ codeBuffer_output(codeBuffer * const codeBufferP,
         codeBufferP->curBits -= 8;
     }
 
-    ++codeBufferP->codeCount;
+    ++codeBufferP->stringCount;
 }
 
 
 
 static void
-codeBuffer_flush(codeBuffer * const codeBufferP) {
+codeBuffer_flush(CodeBuffer * const codeBufferP) {
 
     /* Output the possible partial byte in the buffer */
 
@@ -768,19 +777,19 @@ codeBuffer_flush(codeBuffer * const codeBufferP) {
         codeBufferP->curBits = 0;
     }
     byteBuffer_flush(codeBufferP->byteBufferP);
-    
+
     byteBuffer_flushFile(codeBufferP->byteBufferP);
 
     if (verbose)
         pm_message("%u strings of pixels written to file",
-                   codeBufferP->codeCount);
-    codeBufferP->codeCount = 0;
+                   codeBufferP->stringCount);
+    codeBufferP->stringCount = 0;
 }
 
 
 
 typedef struct {
-    codeBuffer * codeBufferP;
+    CodeBuffer * codeBufferP;
         /* The place to which we write our string codes.
 
            Constant.
@@ -791,12 +800,16 @@ typedef struct {
            proper data, but always using one code per pixel, and therefore
            not effecting any compression and not using the LZW patent.
         */
+    bool noclear;
+        /* Never put a clear code in the output.  Ergo don't recompute the
+           string table from current input.  When the string table fills up,
+           continue using that table for the rest of the image.
+        */
     unsigned int hsize;
         /* The number of slots in the hash table.  This variable to
            enhance overall performance by reducing memory use when
-           encoding smaller gifs. 
+           encoding smaller gifs.
          */
-        
     unsigned int hshift;
         /* This is how many bits we shift left a string code in forming the
            primary hash of the concatenation of that string with another.
@@ -810,37 +823,32 @@ typedef struct {
        represents a string of pixels that is defined by the preceding
        stream.
     */
-    stringCode clearCode;
+    StringCode clearCode;
         /* The code in an LZW stream that means to clear the string
            dictionary and start fresh.
 
            Constant.
         */
-    stringCode eofCode;
+    StringCode eofCode;
         /* The code in an LZW stream that means there's no more coming
 
            Constant.
         */
-    stringCode initCodeLimit;
+    StringCode initCodeLimit;
         /* The value of 'codeLimit' at the start of a block.
 
            Constant.
         */
-
-    stringCode codeLimit;
+    StringCode codeLimit;
         /* One beyond the maximum code possible with the current code
            size.
         */
-
-    struct hashTableEntry * hashTable;
-    stringCode nextUnusedCode;
-        /* Numerically next code available to assign to a a multi-pixel
-           string.  Note that codes for multi-pixel strings are in the
-           upper half of the range of codes, always greater than
-           'clearCode'.
+    struct HashTableEntry * hashTable;
+    StringCode nextCodeToDefine;
+        /* The next string code the GIF protocol will define.  It will do this
+           the next time we emit a string code.
         */
-
-    stringCode stringSoFar;
+    StringCode stringSoFar;
         /* The code for the string we have built so far.  This code indicates
            one or more pixels that we have encoded but not yet output
            because we're hoping to match an even longer string.
@@ -853,16 +861,20 @@ typedef struct {
         /* We are in the middle of building a string; 'stringSoFar' describes
            the pixels in it so far.  The only time this is false is at the
            very beginning of the stream.
- 
-           Ignored in the non-lzw case. 
+
+           Ignored in the non-lzw case.
+        */
+    bool reportedNoclear;
+        /* We have reported to Standard Error that the string table filled up
+           and we elected not to clear it.
         */
-} lzwCompressor;
+} LzwCompressor;
 
 
 
 
 static unsigned int
-nSignificantBits( unsigned int const arg ){
+nSignificantBits(unsigned int const arg){
 
 #if HAVE_GCC_BITCOUNT
 
@@ -880,37 +892,39 @@ nSignificantBits( unsigned int const arg ){
 
 
 
-static lzwCompressor *
+static LzwCompressor *
 lzw_create(FILE *       const ofP,
            unsigned int const initBits,
            bool         const lzw,
+           bool         const noclear,
            unsigned int const pixelCount) {
 
     unsigned int const hsizeTable[] = {257, 521, 1031, 2053, 4099, 5003};
     /* If the image has 4096 or fewer pixels we use prime numbers slightly
        above powers of two between 8 and 12.  In this case the hash table
        never fills up; clear code is never emitted.
-    
+
        Above that we use a table with 4096 slots plus 20% extra.
        When this is not enough the clear code is emitted.
        Because of the extra 20% the table itself never fills up.
-       
+
        lzw.hsize and lzw.hshift stay constant through the image.
 
        Variable hsize is a performance enhancement based on the fact that
        the encoder never needs more codes than the number of pixels in
        the image.  Typically, the ratio of pixels to codes is around
        10:1 to 20:1.
-   
+
        Logic works with fixed values lzw.hsize=5003 and t=13.
     */
 
-    lzwCompressor * lzwP;
-       
+    LzwCompressor * lzwP;
+
     MALLOCVAR_NOFAIL(lzwP);
 
     /* Constants */
-    lzwP->lzw = lzw;
+    lzwP->lzw     = lzw;
+    lzwP->noclear = noclear;
 
     lzwP->clearCode     = 1 << (initBits - 1);
     lzwP->eofCode       = lzwP->clearCode + 1;
@@ -920,18 +934,18 @@ lzw_create(FILE *       const ofP,
         unsigned int const t =
             MIN(13, MAX(8, nSignificantBits(pixelCount +lzwP->eofCode - 2)));
             /* Index into hsizeTable */
-    
+
         lzwP->hsize = hsizeTable[t-8];
 
         lzwP->hshift = (t == 13 ? 12 : t) - nSignificantBits(MAXCMAPSIZE-1);
 
         MALLOCARRAY(lzwP->hashTable, lzwP->hsize);
-        
+
         if (lzwP->hashTable == NULL)
             pm_error("Couldn't get memory for %u-entry hash table.",
                      lzwP->hsize);
     } else {
-        /* No LZW compression.  We don't need a stringcode hash table */  
+        /* No LZW compression.  We don't need a stringcode hash table */
         lzwP->hashTable = NULL;
         lzwP->hsize     = 0;
     }
@@ -940,13 +954,15 @@ lzw_create(FILE *       const ofP,
 
     lzwP->codeBufferP = codeBuffer_create(ofP, initBits, lzw);
 
+    lzwP->reportedNoclear = false;
+
     return lzwP;
 }
 
 
 
 static void
-lzw_destroy(lzwCompressor * const lzwP) {
+lzw_destroy(LzwCompressor * const lzwP) {
 
     codeBuffer_destroy(lzwP->codeBufferP);
 
@@ -958,24 +974,38 @@ lzw_destroy(lzwCompressor * const lzwP) {
 
 
 static void
-lzwHashClear(lzwCompressor * const lzwP) {
+lzwHashClear(LzwCompressor * const lzwP) {
 
     /* Empty the code table */
 
     unsigned int i;
 
     for (i = 0; i < lzwP->hsize; ++i)
-        lzwP->hashTable[i].fcode = -1;
+        lzwP->hashTable[i].present = false;
+
+    lzwP->nextCodeToDefine = lzwP->clearCode + 2;
+}
+
+
 
-    lzwP->nextUnusedCode = lzwP->clearCode + 2;
+static void
+lzw_reportNoclear(LzwCompressor * const lzwP) {
+
+    if (verbose && !lzwP->reportedNoclear) {
+        pm_message("String table filled up.  Not starting a new one "
+                   "because of noclear mode");
+        lzwP->reportedNoclear = true;
+    }
 }
 
 
 
 static void
-lzw_clearBlock(lzwCompressor * const lzwP) {
+lzw_clearBlock(LzwCompressor * const lzwP) {
 /*----------------------------------------------------------------------------
-  
+  Insert a string table clear in the stream.  Clear our table and set it up to
+  start building again, and emit the code to tell the decoder we're doing it
+  so he can do the same.
 -----------------------------------------------------------------------------*/
     lzwHashClear(lzwP);
 
@@ -989,8 +1019,8 @@ lzw_clearBlock(lzwCompressor * const lzwP) {
 
 
 static void
-lzwAdjustCodeSize(lzwCompressor * const lzwP,
-                  stringCode      const newCode) {
+lzwAdjustCodeSize(LzwCompressor * const lzwP,
+                  StringCode      const newCode) {
 /*----------------------------------------------------------------------------
    Assuming we just defined code 'newCode', increase the code size as
    required so that this code fits.
@@ -1011,64 +1041,67 @@ lzwAdjustCodeSize(lzwCompressor * const lzwP,
 
 
 static void
-lzwOutputCurrentString(lzwCompressor * const lzwP) {
+lzwOutputCurrentString(LzwCompressor * const lzwP) {
 /*----------------------------------------------------------------------------
    Put a code for the currently built-up string in the output stream.
 
-   Doing this causes a new string code to be defined (code is
-   lzwP->nextUnusedCode), so Caller must add that to the hash.  If
-   that code's size is beyond the overall limit, we reset the hash
-   (which means future codes will start back at the minimum size) and
-   put a clear code in the stream to tell the decompressor to do the
-   same.  So Caller must add it to the hash _before_ calling us.
-
-   Note that in the non-compressing case, the overall limit is small
-   enough to prevent us from ever defining string codes; we'll always
-   reset the hash.
-
-   There's an odd case that always screws up any attempt to make this
-   code cleaner: At the end of the LZW stream, you have to output the
-   code for the final string even though you don't have a following
-   pixel that would make a longer string.  So there's nothing to add
-   to the hash table and no point in allocating a new string code.
-   But the decompressor doesn't know that we're done, so he allocates
-   the next string code and may therefore increase his code length.
-   If we don't do the same, we will write our one last code -- the EOF
-   code -- in a code length smaller than what the decompressor is
-   expecting, and he will have a premature end of stream.
-
-   So this subroutine does run for that final code flush and does some
-   of the motions of defining a new string code, but this subroutine
-   can't update the hash because in that particular case, there's
-   nothing to add.
+   Doing this causes the protocol to define a new string code, defined as the
+   string we put plus the first pixel of the next string we put.  (It almost
+   seems to violate causality, especially since the next string we put can
+   legally be the very string code that gets defined here, but it actually
+   works).
+
+   The code that gets defined is lzwP->nextCodeToDefine.  Caller is
+   responsible for figuring out the value for the code (i.e. what we just said
+   above) so it can use the code in the future.
+
+   BUT: if the string table has reached its maximum size, issue a clear code
+   instead to cause the protocol to forget all the defined string coes and
+   start its table over (so Caller must start its own table over too).
+   EXCEPT: if we're running in no-clear mode; then we skip the clear code (so
+   the protocol maintains all the string code definitions and caller will have
+   to do so as well).
+
+   Note that in the non-compressing case, the overall limit is small enough to
+   prevent us from ever defining string codes; we'll always issue the clear
+   code.
+
+   Note that there is a case where Caller can just ignore the fact that we
+   cause the protocol to define a new string code: where the string we're
+   outputting is the last one in the stream.  In that case, the new string
+   code we define is irrelevant; it will never be used.
 -----------------------------------------------------------------------------*/
     codeBuffer_output(lzwP->codeBufferP, lzwP->stringSoFar);
-    if (lzwP->nextUnusedCode < lzwP->codeBufferP->maxCodeLimit) {
-        /* Allocate the code for the extended string, which Caller
-           should have already put in the hash so he can use it in the
-           future.  Decompressor knows when it sees the code output
-           above to define a string on its end too, using the same
-           string code we do.
-        */
-        stringCode const newCode = lzwP->nextUnusedCode++;
 
-        /* This code may be too big to fit in the current code size, in
-           which case we have to increase the code size (and decompressor
-           will do the same).
+    if (lzwP->nextCodeToDefine < lzwP->codeBufferP->maxCodeLimit) {
+        /* Record that the protocol defined a new string code, to wit
+           the numerically next one, when we did the output to the stream
+           above, and adjust the code size if this code is too wide to
+           fit in the current size.
         */
+        StringCode const newCode = lzwP->nextCodeToDefine++;
+
         lzwAdjustCodeSize(lzwP, newCode);
     } else {
-        /* Forget all the strings so far; start building again; tell
-           decompressor to do the same.
-        */
-        lzw_clearBlock(lzwP);
+        if (lzwP->noclear)
+            lzw_reportNoclear(lzwP);
+        else {
+            /* Forget all the strings so far; start building again; tell
+               decompressor to do the same.
+            */
+            lzw_clearBlock(lzwP);
+
+            if (verbose)
+                pm_message("String table filled up.  "
+                           "Clearing and starting over");
+        }
     }
 }
 
 
 
 static void
-lzw_flush(lzwCompressor * const lzwP) {
+lzw_flush(LzwCompressor * const lzwP) {
 
     if (lzwP->lzw)
         lzwOutputCurrentString(lzwP);
@@ -1082,8 +1115,8 @@ lzw_flush(lzwCompressor * const lzwP) {
 
 
 static unsigned int
-primaryHash(stringCode   const baseString,
-            stringCode   const additionalPixel,
+primaryHash(StringCode   const baseString,
+            StringCode   const additionalPixel,
             unsigned int const hshift) {
 
     unsigned int hash;
@@ -1092,71 +1125,76 @@ primaryHash(stringCode   const baseString,
     assert(additionalPixel < MAXCMAPSIZE);
 
     hash = (additionalPixel << hshift) ^ baseString;
-    
+
     return hash;
 }
 
-    
+
 
 static void
-lookupInHash(lzwCompressor *  const lzwP,
+lookupInHash(LzwCompressor *  const lzwP,
              unsigned int     const gifPixel,
-             stringCode       const fcode,
              bool *           const foundP,
-             unsigned short * const codeP,
+             StringCode *     const codeP,
              unsigned int *   const hashP) {
 
-    int disp;
+    unsigned int disp;
         /* secondary hash stride (after G. Knott) */
-    int i;
+    unsigned int hash;
         /* Index into hash table */
 
-    i = primaryHash(lzwP->stringSoFar, gifPixel, lzwP->hshift);
-    disp = (i == 0) ? 1 : lzwP->hsize - i;
-
-    while (lzwP->hashTable[i].fcode != fcode &&
-           lzwP->hashTable[i].fcode >= 0) {
-        i -= disp;
-        if (i < 0)
-            i += lzwP->hsize;
+    hash = primaryHash(lzwP->stringSoFar, gifPixel, lzwP->hshift);
+    assert(hash < lzwP->hsize);
+    disp = (hash == 0) ? 1 : lzwP->hsize - hash;
+
+    while (lzwP->hashTable[hash].present &&
+           (lzwP->hashTable[hash].baseString != lzwP->stringSoFar ||
+            lzwP->hashTable[hash].additionalPixel != gifPixel)) {
+        if (hash < disp)
+            hash += lzwP->hsize;
+        assert(hash >= disp);
+        hash -= disp;
+        assert(hash < lzwP->hsize);
     }
 
-    if (lzwP->hashTable[i].fcode == fcode) {
+    if (lzwP->hashTable[hash].present) {
         /* Found fcode in hash table */
-        *foundP = TRUE;
-        *codeP = lzwP->hashTable[i].ent;
+        *foundP = true;
+        *codeP  = lzwP->hashTable[hash].combinedString;
     } else {
         /* Found where it _should_ be (but it's not) with primary hash */
-        *foundP = FALSE;
-        *hashP = i;
+        *foundP = false;
+        *hashP  = hash;
     }
 }
 
 
 
 static void
-lzw_encodePixel(lzwCompressor * const lzwP,
+lzw_encodePixel(LzwCompressor * const lzwP,
                 unsigned int    const gifPixel) {
 
-    bool found;
-    unsigned short code;
-    unsigned int hash;
-        /* Index into hash table where the value should go */
-    
     assert(gifPixel < 256);
 
     if (!lzwP->buildingString) {
         /* Start a new string with just this pixel */
         lzwP->stringSoFar = gifPixel;
-        lzwP->buildingString = TRUE;
+        lzwP->buildingString = true;
     } else {
-        stringCode const fcode =
-            ((stringCode) gifPixel << BITS) + lzwP->stringSoFar;
-            /* The encoding of the string we've already recognized plus the
-               instant pixel, to be looked up in the hash of known strings.
+        bool found;
+            /* There's a code for the current string in the string table */
+        StringCode code;
+            /* Existing code for the current string in the string table, if
+               any
+            */
+        unsigned int hash;
+            /* Index into hash table where the entry for the new string code
+               should go; meaningless if we don't need a new string code
+               (because there's already one in the hash table for the
+               current string)
             */
-    
-        lookupInHash(lzwP, gifPixel, fcode, &found, &code, &hash);
+
+        lookupInHash(lzwP, gifPixel, &found, &code, &hash);
 
         if (found)
             /* With this new pixel, it is still a known string; 'code' is
@@ -1164,14 +1202,22 @@ lzw_encodePixel(lzwCompressor * const lzwP,
             */
             lzwP->stringSoFar = code;
         else {
-            /* It's no longer a known string.  Output the code for the
-               known prefix of the string, thus defining a new string
-               code for possible later use.  Warning:
-               lzwOutputCurrentString() does more than you think. 
+            /* We've found the longest prefix of the rest of the image for
+               which we have a string code defined.  Output the code for that
+               prefix, thus defining a new string code in the protocol for
+               possible later use.  The new code is defined as this string
+               plus the first pixel of the next string.
+
+               But if there aren't any unused string codes left, outputting
+               our string doesn't define any new string code.
             */
 
-            lzwP->hashTable[hash].ent   = lzwP->nextUnusedCode;
-            lzwP->hashTable[hash].fcode = fcode;
+            if (lzwP->nextCodeToDefine < lzwP->codeBufferP->maxCodeLimit) {
+                lzwP->hashTable[hash].present         = true;
+                lzwP->hashTable[hash].baseString      = lzwP->stringSoFar;
+                lzwP->hashTable[hash].additionalPixel = gifPixel;
+                lzwP->hashTable[hash].combinedString  = lzwP->nextCodeToDefine;
+            }
 
             lzwOutputCurrentString(lzwP);
 
@@ -1184,8 +1230,6 @@ lzw_encodePixel(lzwCompressor * const lzwP,
 
 
 /*
- * compress stdin to stdout
- *
  * Algorithm:  use open addressing double hashing (no chaining) on the
  * prefix code / next character combination.  We do a variant of Knuth's
  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
@@ -1200,23 +1244,25 @@ lzw_encodePixel(lzwCompressor * const lzwP,
  */
 
 static void
-writePixelUncompressed(lzwCompressor * const lzwP,
+writePixelUncompressed(LzwCompressor * const lzwP,
                        unsigned int    const gifPixel) {
-                      
+
     lzwP->stringSoFar = gifPixel;
     lzwOutputCurrentString(lzwP);
 
-}    
+}
+
 
 static void
 writeRaster(struct pam *  const pamP,
-            rowReader *   const rowReaderP,
+            RowReader *   const rowReaderP,
             unsigned int  const alphaPlane,
             unsigned int  const alphaThreshold,
-            struct cmap * const cmapP, 
+            struct Cmap * const cmapP,
             unsigned int  const initBits,
             FILE *        const ofP,
-            bool          const lzw) {
+            bool          const lzw,
+            bool          const noclear) {
 /*----------------------------------------------------------------------------
    Write the raster to file 'ofP'.
 
@@ -1228,8 +1274,12 @@ writeRaster(struct pam *  const pamP,
 
    Write the raster using LZW compression, or uncompressed depending
    on 'lzw'.
+
+   If 'noclear', don't use any GIF clear codes in the output; i.e. don't
+   recompute the string table from current input.  Once the string table gets
+   to maximum size, just keep using that table for the rest of the image.
 -----------------------------------------------------------------------------*/
-    lzwCompressor * lzwP;
+    LzwCompressor * lzwP;
     tuple * tuplerow;
     unsigned int nRowsDone;
         /* Number of rows we have read so far from the the input (the
@@ -1237,15 +1287,15 @@ writeRaster(struct pam *  const pamP,
            in case of interlace, this is not the same thing as the row
            number of the current row.
         */
-    
-    lzwP = lzw_create(ofP, initBits, lzw, pamP->height * pamP->width);
+
+    lzwP = lzw_create(ofP, initBits, lzw, noclear, pamP->height * pamP->width);
 
     tuplerow = pnm_allocpamrow(pamP);
 
     lzw_clearBlock(lzwP);
 
     nRowsDone = 0;
-    
+
     while (nRowsDone < pamP->height) {
         unsigned int col;
 
@@ -1255,14 +1305,14 @@ writeRaster(struct pam *  const pamP,
             unsigned int const colorIndex =
                 gifPixel(pamP, tuplerow[col], alphaPlane, alphaThreshold,
                          cmapP);
-            
+
                 /* The value for the pixel in the GIF image.  I.e. the colormap
                    index.
                 */
             if (lzw)
                 lzw_encodePixel(lzwP, colorIndex);
             else
-                writePixelUncompressed(lzwP, colorIndex);    
+                writePixelUncompressed(lzwP, colorIndex);
         }
         ++nRowsDone;
     }
@@ -1272,7 +1322,7 @@ writeRaster(struct pam *  const pamP,
     lzw_flush(lzwP);
 
     pnm_freepamrow(tuplerow);
-    
+
     lzw_destroy(lzwP);
 }
 
@@ -1280,7 +1330,7 @@ writeRaster(struct pam *  const pamP,
 
 static void
 writeGlobalColorMap(FILE *              const ofP,
-                    const struct cmap * const cmapP,
+                    const struct Cmap * const cmapP,
                     unsigned int        const bitsPerPixel) {
 /*----------------------------------------------------------------------------
   Write out the Global Color Map
@@ -1326,16 +1376,16 @@ writeGlobalColorMap(FILE *              const ofP,
     }
     pnm_freepamtuple(tupleRgb255);
 }
-        
+
 
 
 static void
 writeGifHeader(FILE *              const ofP,
                unsigned int        const width,
-               unsigned int        const height, 
-               unsigned int        const background, 
+               unsigned int        const height,
+               unsigned int        const background,
                unsigned int        const bitsPerPixel,
-               const struct cmap * const cmapP,
+               const struct Cmap * const cmapP,
                char                const comment[],
                float               const aspect) {
 
@@ -1371,12 +1421,12 @@ writeGifHeader(FILE *              const ofP,
 
     {
         int const aspectValue = aspect == 1.0 ? 0 : ROUND(aspect * 64) - 15;
-        assert(0 <= aspectValue && aspectValue <= 255); 
+        assert(0 <= aspectValue && aspectValue <= 255);
         fputc(aspectValue, ofP);
     }
     writeGlobalColorMap(ofP, cmapP, bitsPerPixel);
 
-    if (cmapP->haveTransparent) 
+    if (cmapP->haveTransparent)
         writeTransparentColorIndexExtension(ofP, cmapP->transparent);
 
     if (comment)
@@ -1430,15 +1480,16 @@ reportImageInfo(bool         const interlace,
 
 static void
 gifEncode(struct pam *  const pamP,
-          FILE *        const ofP, 
+          FILE *        const ofP,
           pm_filepos    const rasterPos,
           bool          const gInterlace,
-          int           const background, 
+          int           const background,
           unsigned int  const bitsPerPixel,
-          struct cmap * const cmapP,
+          struct Cmap * const cmapP,
           char          const comment[],
           float         const aspect,
-          bool          const lzw) {
+          bool          const lzw,
+          bool          const noclear) {
 
     unsigned int const leftOffset = 0;
     unsigned int const topOffset  = 0;
@@ -1453,14 +1504,14 @@ gifEncode(struct pam *  const pamP,
 
     unsigned int const alphaPlane = pamAlphaPlane(pamP);
 
-    rowReader * rowReaderP;
+    RowReader * rowReaderP;
 
     reportImageInfo(gInterlace, background, bitsPerPixel);
 
     if (pamP->width > 65535)
         pm_error("Image width %u too large for GIF format.  (Max 65535)",
                  pamP->width);
-     
+
     if (pamP->height > 65535)
         pm_error("Image height %u too large for GIF format.  (Max 65535)",
                  pamP->height);
@@ -1479,7 +1530,7 @@ gifEncode(struct pam *  const pamP,
     /* Write the actual raster */
 
     writeRaster(pamP, rowReaderP, alphaPlane, alphaThreshold,
-                cmapP, initCodeSize + 1, ofP, lzw);
+                cmapP, initCodeSize + 1, ofP, lzw, noclear);
 
     rowReader_destroy(rowReaderP);
 
@@ -1493,7 +1544,7 @@ gifEncode(struct pam *  const pamP,
 
 
 static void
-reportTransparent(struct cmap * const cmapP) {
+reportTransparent(struct Cmap * const cmapP) {
 
     if (verbose) {
         if (cmapP->haveTransparent) {
@@ -1511,10 +1562,10 @@ reportTransparent(struct cmap * const cmapP) {
 
 
 static void
-computeTransparent(char          const colorarg[], 
+computeTransparent(char          const colorarg[],
                    bool          const usingFakeTrans,
                    unsigned int  const fakeTransparent,
-                   struct cmap * const cmapP) {
+                   struct Cmap * const cmapP) {
 /*----------------------------------------------------------------------------
    Figure out the color index (index into the colormap) of the color
    that is to be transparent in the GIF.
@@ -1546,7 +1597,7 @@ computeTransparent(char          const colorarg[],
         tuple transcolor;
         int found;
         int colorindex;
-        
+
         if (colorarg[0] == '=') {
             colorspec = &colorarg[1];
             exact = TRUE;
@@ -1558,7 +1609,7 @@ computeTransparent(char          const colorarg[],
         transcolor = pnm_parsecolor(colorspec, cmapP->pam.maxval);
         pnm_lookuptuple(&cmapP->pam, cmapP->tuplehash, transcolor, &found,
                         &colorindex);
-        
+
         if (found) {
             cmapP->haveTransparent = TRUE;
             cmapP->transparent = colorindex;
@@ -1602,7 +1653,7 @@ sortCompareColor(const void * const entry1P,
     struct tupleint * const * const tupleint1PP = entry1P;
     struct tupleint * const * const tupleint2PP = entry2P;
 
-    return (sortOrderColor((*tupleint1PP)->tuple) 
+    return (sortOrderColor((*tupleint1PP)->tuple)
             - sortOrderColor((*tupleint2PP)->tuple));
 }
 
@@ -1640,15 +1691,15 @@ sortTupletable(struct pam * const mapPamP,
     if (mapPamP->depth < 3)
         qsort(tuplefreq, colors, sizeof(tuplefreq[0]), sortCompareGray);
     else
-        qsort(tuplefreq, colors, sizeof(tuplefreq[0]), sortCompareColor); 
+        qsort(tuplefreq, colors, sizeof(tuplefreq[0]), sortCompareColor);
 
 }
 
 
 
 static void
-addToColormap(struct cmap *  const cmapP, 
-              const char *   const colorspec, 
+addToColormap(struct Cmap *  const cmapP,
+              const char *   const colorspec,
               unsigned int * const newIndexP) {
 /*----------------------------------------------------------------------------
   Add a new entry to the colormap.  Make the color that specified by
@@ -1685,7 +1736,7 @@ addToColormap(struct cmap *  const cmapP,
 static void
 colormapFromFile(char               const filespec[],
                  unsigned int       const maxcolors,
-                 tupletable *       const tupletableP, 
+                 tupletable *       const tupletableP,
                  struct pam *       const mapPamP,
                  unsigned int *     const colorCountP) {
 /*----------------------------------------------------------------------------
@@ -1703,13 +1754,13 @@ colormapFromFile(char               const filespec[],
     pm_close(mapfileP);
 
     pm_message("computing other colormap ...");
-    
-    *tupletableP = 
+
+    *tupletableP =
         pnm_computetuplefreqtable(mapPamP, colors, maxcolors, &colorCount);
 
     *colorCountP = colorCount;
 
-    pnm_freepamarray(colors, mapPamP); 
+    pnm_freepamarray(colors, mapPamP);
 }
 
 
@@ -1717,7 +1768,7 @@ colormapFromFile(char               const filespec[],
 static void
 readAndValidateColormapFromFile(char           const filename[],
                                 unsigned int   const maxcolors,
-                                tupletable *   const tuplefreqP, 
+                                tupletable *   const tuplefreqP,
                                 struct pam *   const mapPamP,
                                 unsigned int * const colorCountP,
                                 unsigned int   const nInputComp,
@@ -1753,7 +1804,7 @@ computeColormapBw(struct pam *   const pamP,
    $ pbmmake -w 600 400 | pamtogif -sort > canvas.gif
 -----------------------------------------------------------------------------*/
     tupletable const colormap = pnm_alloctupletable(pamP, 2);
-    
+
     *mapPamP = *pamP;
     mapPamP->depth = 1;
 
@@ -1761,12 +1812,12 @@ computeColormapBw(struct pam *   const pamP,
     colormap[0]->tuple[0] = PAM_BLACK;
     colormap[1]->value = 1;
     colormap[1]->tuple[0] = PAM_BW_WHITE;
-    
+
     *tuplefreqP  = colormap;
     *colorCountP = 2;
 }
-  
-    
+
+
 
 static void
 computeColormapFromInput(struct pam *   const pamP,
@@ -1775,7 +1826,7 @@ computeColormapFromInput(struct pam *   const pamP,
                          struct pam *   const mapPamP,
                          unsigned int * const colorCountP,
                          tupletable *   const tuplefreqP) {
-    
+
     tupletable tuplefreq;
 
     pm_message("computing colormap...");
@@ -1842,9 +1893,9 @@ computeLibnetpbmColormap(struct pam *   const pamP,
              pamP->height * pamP->width > 1)
         computeColormapBw(pamP, mapPamP, &colorCount, &tuplefreq);
     else
-        computeColormapFromInput(pamP, maxcolors, nInputComp, 
+        computeColormapFromInput(pamP, maxcolors, nInputComp,
                                  mapPamP, &colorCount, &tuplefreq);
-    
+
     if (tuplefreq == NULL)
         pm_error("too many colors - try doing a 'pnmquant %u'", maxcolors);
 
@@ -1870,10 +1921,10 @@ computeLibnetpbmColormap(struct pam *   const pamP,
 
 
 static void
-destroyCmap(struct cmap * const cmapP) {
+destroyCmap(struct Cmap * const cmapP) {
 
     unsigned int colorIndex;
-    
+
     for (colorIndex = 0; colorIndex < cmapP->cmapSize; ++colorIndex)
         pnm_freepamtuple(cmapP->color[colorIndex]);
 
@@ -1884,41 +1935,41 @@ destroyCmap(struct cmap * const cmapP) {
 
 int
 main(int argc, char *argv[]) {
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
     FILE * ifP;
     struct pam pam;
     unsigned int bitsPerPixel;
     pm_filepos rasterPos;
 
-    struct cmap cmap;
+    struct Cmap cmap;
         /* The colormap, with all its accessories */
     unsigned int fakeTransparent;
         /* colormap index of the fake transparency color we're using to
            implement the alpha mask.  Undefined if we're not doing an alpha
            mask.
         */
-    
+
     pnm_init(&argc, argv);
-    
+
     parseCommandLine(argc, argv, &cmdline);
-    
+
     verbose = cmdline.verbose;
-    
+
     ifP = pm_openr_seekable(cmdline.input_filespec);
-    
+
     pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
-    
+
     pm_tell2(ifP, &rasterPos, sizeof(rasterPos));
-    
-    computeLibnetpbmColormap(&pam, !!pamAlphaPlane(&pam), cmdline.mapfile, 
+
+    computeLibnetpbmColormap(&pam, !!pamAlphaPlane(&pam), cmdline.mapfile,
                              cmap.color, &cmap.tuplehash,
                              &cmap.pam, &cmap.cmapSize, cmdline.sort);
-    
+
     assert(cmap.pam.maxval == pam.maxval);
 
     if (pamAlphaPlane(&pam)) {
-        /* Add a fake entry to the end of the colormap for transparency.  
-           Make its color black. 
+        /* Add a fake entry to the end of the colormap for transparency.
+           Make its color black.
         */
         addToColormap(&cmap, cmdline.alphacolor, &fakeTransparent);
     }
@@ -1931,13 +1982,13 @@ main(int argc, char *argv[]) {
     /* All set, let's do it. */
     gifEncode(&pam, stdout, rasterPos,
               cmdline.interlace, 0, bitsPerPixel, &cmap, cmdline.comment,
-              cmdline.aspect, !cmdline.nolzw);
-    
+              cmdline.aspect, !cmdline.nolzw, cmdline.noclear);
+
     destroyCmap(&cmap);
 
     pm_close(ifP);
     pm_close(stdout);
-    
+
     return 0;
 }
 
@@ -1955,21 +2006,21 @@ main(int argc, char *argv[]) {
   JPEG Group's djpeg on 2001.09.29.  In 2006.12 the output subroutines
   were rewritten; now no uncompressed output subroutines are derived from
   the Independent JPEG Group's source code.
-  
+
   2007.01  Changed sort routine to qsort.  (afu)
   2007.03  Implemented variable hash table size, PBM color table
            shortcut and "-aspect" command line option.   (afu)
 
- 
+
   Copyright (C) 1989 by Jef Poskanzer.
- 
+
   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.  This software is provided "as is" without express or
   implied warranty.
- 
+
   The Graphics Interchange Format(c) is the Copyright property of
   CompuServe Incorporated.  GIF(sm) is a Service Mark property of
   CompuServe Incorporated.
diff --git a/converter/other/pamtopdbimg.c b/converter/other/pamtopdbimg.c
index 6454292e..ce2f7659 100644
--- a/converter/other/pamtopdbimg.c
+++ b/converter/other/pamtopdbimg.c
@@ -38,6 +38,7 @@
 #include "mallocvar.h"
 #include "nstring.h"
 #include "shhopt.h"
+#include "runlength.h"
 #include "pam.h"
 
 #include "ipdb.h"
@@ -128,6 +129,8 @@ parseCommandLine(int argc, const char ** argv,
         cmdlineP->inputFileName = argv[1];
     else
         pm_error("Program takes at most one argument:  input file name");
+
+    free(option_def);
 }
 
 
@@ -238,91 +241,6 @@ textWrite(TEXT * const textP,
 
 
 
-typedef struct {
-    unsigned int match;
-    uint8_t      buf[128];
-    int          mode;
-    size_t       len;
-    size_t       used;
-    uint8_t *    p;
-} RLE;
-#define MODE_MATCH  0
-#define MODE_LIT    1
-#define MODE_NONE   2
-
-#define reset(r) {                              \
-        (r)->match = 0xffff;                    \
-        (r)->mode  = MODE_NONE;                 \
-        (r)->len   = 0;                         \
-    }
-
-
-
-static void
-putMatch(RLE *  const rleP,
-          size_t const n) {
-
-    *rleP->p++ = 0x80 + n - 1;
-    *rleP->p++ = rleP->match;
-    rleP->used += 2;
-    reset(rleP);
-}
-
-
-
-static void
-putLit(RLE *  const rleP,
-       size_t const n) {
-
-    *rleP->p++ = n - 1;
-    rleP->p = (uint8_t *)memcpy(rleP->p, rleP->buf, n) + n;
-    rleP->used += n + 1;
-    reset(rleP);
-}
-
-
-
-static size_t
-compress(const uint8_t * const inData,
-         size_t          const n_in,
-         uint8_t *       const out) {
-
-    static void (*put[])(RLE *, size_t) = {putMatch, putLit};
-    RLE rle;
-    size_t  i;
-    const uint8_t * p;
-
-    MEMSZERO(&rle);
-    rle.p = out;
-    reset(&rle);
-
-    for (i = 0, p = &inData[0]; i < n_in; ++i, ++p) {
-        if (*p == rle.match) {
-            if (rle.mode == MODE_LIT && rle.len > 1) {
-                putLit(&rle, rle.len - 1);
-                ++rle.len;
-                rle.match = *p;
-            }
-            rle.mode = MODE_MATCH;
-            ++rle.len;
-        } else {
-            if (rle.mode == MODE_MATCH)
-                putMatch(&rle, rle.len);
-            rle.mode         = MODE_LIT;
-            rle.match        = *p;
-            rle.buf[rle.len++] = *p;
-        }
-        if (rle.len == 128)
-            put[rle.mode](&rle, rle.len);
-    }
-    if (rle.len != 0)
-        put[rle.mode](&rle, rle.len);
-
-    return rle.used;
-}
-
-
-
 static void
 compressIfRequired(IPDB *     const pdbP,
                    int        const comp,
@@ -334,38 +252,29 @@ compressIfRequired(IPDB *     const pdbP,
         *compressedSizeP = ipdb_img_size(pdbP->i);
     } else {
         int const uncompressedSz = ipdb_img_size(pdbP->i);
-        
-        /* Allocate for the worst case. */
-        size_t const allocSz = (3 * uncompressedSz + 2)/2;
 
-        uint8_t * data;
-            
-        data = pdbP->i->data;
+        unsigned char * outbuf;
+        size_t          compressedSz;
 
-        MALLOCARRAY(data, allocSz);
-            
-        if (data == NULL)
-            pm_error("Could not get %lu bytes of memory to decompress",
-                     (unsigned long)allocSz);
-        else {
-            size_t compressedSz;
-            compressedSz = compress(pdbP->i->data, uncompressedSz, data);
-            if (comp == IPDB_COMPMAYBE && compressedSz >= uncompressedSz) {
-                /* Return the uncompressed data */
-                free(data);
-                *compressedDataP = pdbP->i->data;
-                *compressedSizeP = uncompressedSz;
-            } else {
-                pdbP->i->compressed = TRUE;
-                if (pdbP->i->type == IMG_GRAY16)
-                    pdbP->i->version = 9;
-                else
-                    pdbP->i->version = 1;
-                if (pdbP->t != NULL)
-                    pdbP->t->r->offset -= uncompressedSz - compressedSz;
-                *compressedDataP = data;
-                *compressedSizeP = compressedSz;
-            }
+        pm_rlenc_allocoutbuf(&outbuf, uncompressedSz, PM_RLE_PALMPDB);
+
+        pm_rlenc_compressbyte(pdbP->i->data, outbuf, PM_RLE_PALMPDB,
+                              uncompressedSz, &compressedSz);
+        if (comp == IPDB_COMPMAYBE && compressedSz >= uncompressedSz) {
+            /* Return the uncompressed data */
+            free(outbuf);
+            *compressedDataP = pdbP->i->data;
+            *compressedSizeP = uncompressedSz;
+        } else {
+            pdbP->i->compressed = TRUE;
+            if (pdbP->i->type == IMG_GRAY16)
+                pdbP->i->version = 9;
+            else
+                pdbP->i->version = 1;
+            if (pdbP->t != NULL)
+                pdbP->t->r->offset -= uncompressedSz - compressedSz;
+            *compressedDataP = outbuf;
+            *compressedSizeP = compressedSz;
         }
     }
 }
@@ -742,6 +651,14 @@ readtxt(IPDB *       const pdbP,
         pm_error("stat of '%s' failed, errno = %d (%s)",
                  noteFileName, errno, strerror(errno));
 
+    /* The maximum size of a memory block that a Palm can allocate is 64K.
+       Abort with error if specified note file is any larger.
+    */
+
+    if (st.st_size + 1 >= 65535)
+        pm_error("Note file is too large: %lu bytes",
+                  (unsigned long) st.st_size);
+
     fP = pm_openr(noteFileName);
 
     MALLOCARRAY(fileContent, st.st_size + 1);
@@ -758,6 +675,8 @@ readtxt(IPDB *       const pdbP,
 
     pm_close(fP);
 
+    fileContent[st.st_size] = 0x00;  /* add terminating NUL char */
+
     /* Chop of trailing newlines */
     for (n = strlen(fileContent) - 1; n >= 0 && fileContent[n] == '\n'; --n)
         fileContent[n] = '\0';
@@ -787,6 +706,9 @@ main(int argc, const char **argv) {
     case MAYBE:        comp = IPDB_COMPMAYBE;  break;
     }
 
+    if (strlen(cmdline.title) > 31)
+        pm_error("Title too long.  Max length is 31 characters.");
+
     pdbP = ipdb_alloc(cmdline.title);
 
     if (pdbP == NULL)
diff --git a/converter/other/pamtopng.c b/converter/other/pamtopng.c
index fdeb6582..a323844f 100644
--- a/converter/other/pamtopng.c
+++ b/converter/other/pamtopng.c
@@ -1,21 +1,8 @@
-/*
-** read a PNM/PAM image and produce a Portable Network Graphics (PNG) file
-**
-** derived from pnmtorast.c by Jef Poskanzer and pamrgbatopng.c by Bryan
-** Henderson <bryanh@giraffe-data.com> and probably some other sources
-**
-** Copyright (C) 1995-1998 by Alexander Lehmann <alex@hal.rhein-main.de>
-**                        and Willem van Schaik <willem@schaik.com>
-** Copyright (C) 1999,2001 by Greg Roelofs <newt@pobox.com>
-** Copyright (C) 2015 by Willem van Schaik <willem@schaik.com>
-**
-** Permission to use, copy, modify, and distribute this software and its
-** documentation for any purpose and without fee is hereby granted, provided
-** that the above copyright notice appear in all copies and that both that
-** copyright notice and this permission notice appear in supporting
-** documentation.  This software is provided "as is" without express or
-** implied warranty.
-*/
+/*=============================================================================
+                                  pamtopng
+===============================================================================
+  Read a Netpbm image and produce a PNG (Portable Network Graphics) image.
+=============================================================================*/
 
 /*
   This Netpbm program pamtopng was derived in 2015 from the Netpbm program
@@ -29,18 +16,19 @@
     had become rather complex.  This program is roughly 1/3 the size of
     pnmtopng.c that it replaces.
 
-  - In 1995 bandwith was limited and therefore filesize had to be kept
+  - In 1995, bandwith was limited and therefore filesize had to be kept
     small. The original program tried to optimize for that by applying
-    many "clever tricks". Today that isn't an issue anymore, so gone 
+    many "clever tricks". Today that isn't an issue anymore, so gone
     are filters, palettes, etc. Also, image conversions were removed,
     because those should be done with other NetPBM tools.
 
   - Add ability to create iTXt (international language) chunks.
 */
 
-
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <time.h>
 #include <png.h>
 /* setjmp.h needs to be included after png.h */
 #include <setjmp.h>
@@ -61,6 +49,7 @@ static bool verbose;
 struct CmdlineInfo {
     const char * inputFileName;
     unsigned int verbose;
+    unsigned int interlace;
     unsigned int transparencySpec;
     const char * transparency;
     unsigned int chromaSpec;
@@ -88,7 +77,7 @@ parseChromaOpt(const char *         const chromaOpt,
                struct pngx_chroma * const chromaP) {
 
     int count;
-    
+
     count = sscanf(chromaOpt, "%f %f %f %f %f %f %f %f",
                    &chromaP->wx, &chromaP->wy,
                    &chromaP->rx, &chromaP->ry,
@@ -107,7 +96,7 @@ parseChromaOpt(const char *         const chromaOpt,
 static void
 parseSrgbintentOpt(const char *      const srgbintentOpt,
                    pngx_srgbIntent * const srgbintentP) {
-    
+
     if (streq(srgbintentOpt, "perceptual"))
         *srgbintentP = PNGX_PERCEPTUAL;
     else if (streq(srgbintentOpt, "relativecolorimetric"))
@@ -145,7 +134,7 @@ parseTimeOpt(const char * const timeOpt,
     if (count != 6)
         pm_error("Invalid value for -time '%s'.   It should have "
                  "the form [yy]yy-mm-dd hh:mm:ss.", timeOpt);
-    
+
     if (year < 0)
         pm_error("Year is negative in -time value '%s'", timeOpt);
     if (year > 9999)
@@ -192,7 +181,7 @@ static void
 parseCommandLine (int                  argc,
                   const char **        argv,
                   struct CmdlineInfo * const cmdlineP) {
-    
+
     optEntry * option_def;
     optStruct3 opt;
     unsigned int option_def_index = 0;  /* incremented by OPTENT3 */
@@ -205,6 +194,8 @@ parseCommandLine (int                  argc,
 
     OPTENT3(0,  "verbose",      OPT_FLAG,       NULL,
             &cmdlineP->verbose,        0);
+    OPTENT3(0,  "interlace",    OPT_FLAG,       NULL,
+            &cmdlineP->interlace,      0);
     OPTENT3(0,  "transparency", OPT_STRING,     &cmdlineP->transparency,
             &cmdlineP->transparencySpec, 0);
     OPTENT3(0,  "chroma",       OPT_STRING,     &chroma,
@@ -239,7 +230,7 @@ parseCommandLine (int                  argc,
 
     if (cmdlineP->timeSpec)
         parseTimeOpt(time, &cmdlineP->time);
-    
+
     /* get the input-file or stdin pipe */
     if (argc-1 < 1)
         cmdlineP->inputFileName = "-";
@@ -256,7 +247,7 @@ parseCommandLine (int                  argc,
 static png_byte
 colorTypeFromInputType(const struct pam * const pamP) {
 /*----------------------------------------------------------------------------
-  Analyse the Netpbm image for color-type and bit-depth
+  Analyze the Netpbm image for color-type and bit-depth
 -----------------------------------------------------------------------------*/
     png_byte retval;
 
@@ -286,7 +277,7 @@ colorTypeFromInputType(const struct pam * const pamP) {
         if (pamP->depth == 2)
             retval = PNG_COLOR_TYPE_GRAY_ALPHA;
         else
-            pm_error("Input tupel type is GRAYSCALE_ALPHA, "
+            pm_error("Input tuple type is GRAYSCALE_ALPHA, "
                      "but number of planes is %u instread of 2",
                      pamP->depth);
     } else if (strneq(pamP->tuple_type, "GRAYSCALE", 9)) {
@@ -385,6 +376,8 @@ sigBitsFmImgType(unsigned int const pnmBitDepth,
             retval.gray  = pnmBitDepth;
             retval.alpha = pnmBitDepth;
             break;
+        default:
+            assert(false);
         }
     } else {
         /* PNG can (so presumably will) use original bit depth */
@@ -404,7 +397,7 @@ doTrnsChunk(const struct pam * const pamP,
             struct pngx *      const pngxP,
             const char *       const trans) {
 
-    if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA || 
+    if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA ||
         pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA)
         pm_error("Both alpha channel and transparency chunk not allowed.");
     else {
@@ -434,13 +427,13 @@ doTrnsChunk(const struct pam * const pamP,
 static void
 doChrmChunk(struct pngx *      const pngxP,
             struct pngx_chroma const chroma) {
-    
+
     pngx_setChrm(pngxP, chroma);
 
     if (verbose) {
         pm_message("writing cHRM chunk { wx, wy, rx, ry, gx, gy, bx, by } = "
                    "{ %4.2f, %4.2f, %4.2f, %4.2f, "
-                   "%4.2f, %4.2f, %4.2f, %4.2f }", 
+                   "%4.2f, %4.2f, %4.2f, %4.2f }",
                    chroma.wx, chroma.wy,
                    chroma.rx, chroma.ry,
                    chroma.gx, chroma.gy,
@@ -450,7 +443,7 @@ doChrmChunk(struct pngx *      const pngxP,
 
 
 
-static void 
+static void
 doGamaChunk(struct pngx *  const pngxP,
             float          const gamma) {
 
@@ -500,9 +493,9 @@ doTextChunkSet(struct pngx * const pngxP,
     FILE * tfP;
 
     tfP = pm_openr(textFileName);
-    
+
     pngtxt_addChunk(pngxP, tfP, ztxt, itxt, verbose);
-    
+
     pm_close(tfP);
 }
 
@@ -518,9 +511,9 @@ doZtxtChunkSet(struct pngx * const pngxP,
     FILE * tfP;
 
     tfP = pm_openr(textFileName);
-    
+
     pngtxt_addChunk(pngxP, tfP, ztxt, itxt, verbose);
-    
+
     pm_close(tfP);
 }
 
@@ -556,11 +549,11 @@ doBkgdChunk (const struct pam * const pamP,
     pngx_setBkgdRgb(pngxP, pngColor);
 
     if (verbose) {
-        if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY || 
+        if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY ||
             pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA) {
             pm_message("writing bKGD chunk with gray level = %u",
                        pngColor.gray);
-        } else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB || 
+        } else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB ||
                    pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA) {
             pm_message("writing bKGD chunk with color {red, green, blue} = "
                        "{%u, %u, %u}",
@@ -591,24 +584,10 @@ doTimeChunk(struct pngx * const pngxP,
 
 
 static void
-setShift(struct pngx * const pngxP,
-         png_color_8   const sigBits) {
-
-    if (sigBits.red + sigBits.green + sigBits.blue +
-        sigBits.gray + sigBits.alpha > 0) {
-
-        /* Move the 1, 2, 4 bits to most significant bits */
-        pngx_setShift(pngxP, sigBits);
-    }
-}
-
-
-
-static void
-convertRaster(const struct pam * const pamP,
-              const tuple *      const tuplerow,
-              png_byte *         const pngRow,
-              unsigned int       const bitDepth) {
+convertRow(const struct pam * const pamP,
+           const tuple *      const tuplerow,
+           png_byte *         const pngRow,
+           unsigned int       const bitDepth) {
 
     unsigned int col;
 
@@ -637,9 +616,12 @@ convertRaster(const struct pam * const pamP,
 
 
 static void
-writeRaster(const struct pam * const pamP,
-            struct pngx *      const pngxP,
-            int                const bitDepth) {
+writeRasterRowByRow(const struct pam * const pamP,
+                    struct pngx *      const pngxP,
+                    int                const bitDepth) {
+
+    unsigned int const rowSz =
+        pamP->width * pamP->depth * (MAX(1, bitDepth/8));
 
     tuple * tupleRow;
     png_byte * pngRow;
@@ -649,8 +631,7 @@ writeRaster(const struct pam * const pamP,
 
     tupleRow = pnm_allocpamrow(pamP);
 
-    MALLOCARRAY(pngRow, pamP->width * 8);
-        /* sufficient to store a 16-bit RGB+A row */
+    MALLOCARRAY(pngRow, rowSz);
 
     if (pngRow == NULL)
         pm_error("Unable to allocate space for PNG pixel row for "
@@ -659,9 +640,9 @@ writeRaster(const struct pam * const pamP,
         for (row = 0; row < pamP->height; ++row) {
             pnm_readpamrow(pamP, tupleRow);
 
-            convertRaster(pamP, tupleRow, pngRow, bitDepth);
+            convertRow(pamP, tupleRow, pngRow, bitDepth);
 
-            png_write_row(pngxP->png_ptr, pngRow);
+            pngx_writeRow(pngxP, pngRow);
         }
         free(pngRow);
     }
@@ -670,40 +651,160 @@ writeRaster(const struct pam * const pamP,
 
 
 
+static png_bytep
+mallocPngImage(unsigned int const rowSize,
+               unsigned int const height) {
+
+    png_bytep pngImage;
+
+    if (UINT_MAX / rowSize < height)
+        pm_error("Image is uncomputably large at %u rows of %u bytes",
+                 height, rowSize);
+
+    MALLOCARRAY(pngImage, height * rowSize);
+
+    if (!pngImage)
+        pm_error("could not allocate %u bytes for a PNG image buffer",
+                 height * rowSize);
+
+    return pngImage;
+}
+
+
+
+static unsigned int
+pngLineSize(struct pngx * const pngxP) {
+
+    unsigned int const bytesPerSample = pngx_bitDepth(pngxP) == 16 ? 2 : 1;
+
+    unsigned int samplesPerPixel;
+
+    switch (pngx_colorType(pngxP)) {
+        case PNG_COLOR_TYPE_GRAY:
+            samplesPerPixel = 1;
+            break;
+        case PNG_COLOR_TYPE_GRAY_ALPHA:
+            samplesPerPixel = 2;
+            break;
+        case PNG_COLOR_TYPE_RGB:
+            samplesPerPixel = 3;
+            break;
+        case PNG_COLOR_TYPE_RGB_ALPHA:
+            samplesPerPixel = 4;
+            break;
+        default:
+            assert(false);
+    }
+
+    if (UINT_MAX / bytesPerSample / samplesPerPixel < pngx_imageWidth(pngxP)) {
+        pm_error("pngcopy: width %u of PNG is uncomputably large\n",
+                  pngx_imageWidth(pngxP));
+    }
+
+    return pngx_imageWidth(pngxP) * bytesPerSample * samplesPerPixel;
+}
+
+
+
 static void
-writePng(const struct pam * const pamP,
-         FILE *             const ofP,
-         struct CmdlineInfo const cmdline) {
+writeRasterWholeImg(struct pam *  const pamP,
+                    struct pngx * const pngxP,
+                    unsigned int  const bitDepth) {
 
-    unsigned int const pnmBitDepth = pm_maxvaltobits(pamP->maxval);
-    int const pngColorType = colorTypeFromInputType(pamP);
+    unsigned int const pngRowSize = pngLineSize(pngxP);
 
-    struct pngx * pngxP;
-    unsigned int pngBitDepth;
-    png_color_8 sBit;
+    tuple * tupleRow;
+    png_bytep pngImage;
+        /* A one-dimensional malloc'ed array of all pixels in image */
+    png_bytep * pngRowP;
+        /* A malloc'ed array of row pointers into pngImage[] */
+    unsigned int row;
 
-    pngx_create(&pngxP, PNGX_WRITE, NULL);
+    tupleRow = pnm_allocpamrow(pamP);
+
+    pngImage = mallocPngImage(pngRowSize, pamP->height);
+
+    MALLOCARRAY(pngRowP, pamP->height);
+
+    if (!pngRowP)
+        pm_error("Failed to allocate an array for %u PNG row pointers",
+                 pamP->height);
 
+    for (row = 0; row < pamP->height; ++row) {
+        png_bytep const thisPngRowP = &pngImage[row * pngRowSize];
 
+        pnm_readpamrow(pamP, tupleRow);
+
+        convertRow(pamP, tupleRow, thisPngRowP, bitDepth);
+
+        pngRowP[row] = thisPngRowP;
+    }
+
+    pngx_writeImage(pngxP, pngRowP);
+
+    free(pngRowP);
+    free(pngImage);
+    pnm_freepamrow(tupleRow);
+}
+
+
+
+static void
+reportInputFormat(const struct pam * const pamP) {
+
+    const char * formatDesc;
+
+    if (pamP->format == PBM_FORMAT || pamP->format == RPBM_FORMAT)
+        formatDesc = "PBM";
+    else if (pamP->format == PGM_FORMAT || pamP->format == RPGM_FORMAT)
+        formatDesc = "PGM";
+    else if (pamP->format == PPM_FORMAT || pamP->format == RPPM_FORMAT)
+        formatDesc = "PPM";
+    else if (pamP->format == PAM_FORMAT)
+        formatDesc = "PAM";
+    else
+        formatDesc = NULL;
+
+    if (formatDesc)
+        pm_message("Input format = %s", formatDesc);
+    else
+        pm_message("Unrecognized input format, format code = 0x%x",
+                   pamP->format);
+
+    pm_message("Input tuple type = '%s'", pamP->tuple_type);
+    pm_message("Input depth = %u", pamP->depth);
+    pm_message("Input maxval = %u", (unsigned int) pamP->maxval);
+}
+
+
+
+static unsigned int
+pngBitDepth(unsigned int const pnmBitDepth,
+            int          const pngColorType) {
+
+    unsigned int retval;
 
     if ((pngColorType == PNG_COLOR_TYPE_RGB ||
          pngColorType == PNG_COLOR_TYPE_RGB_ALPHA) &&
         pnmBitDepth < 8) {
 
-        pngBitDepth = 8;
+        retval = 8;
     } else
-        pngBitDepth = pnmBitDepth;
+        retval = pnmBitDepth;
 
-    png_init_io(pngxP->png_ptr, ofP);
+    return retval;
+}
 
-    pngx_setIhdr(pngxP, pamP->width, pamP->height,
-                 pngBitDepth, pngColorType,
-                 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
-                 PNG_FILTER_TYPE_BASE);
 
-    sBit = sigBitsFmImgType(pnmBitDepth, pngColorType);
 
-    /* Where requested, add ancillary chunks */
+static void
+addAncillaryChunks(struct pam *       const pamP,
+                   struct pngx *      const pngxP,
+                   struct CmdlineInfo const cmdline,
+                   png_color_8        const sigBits) {
+/*----------------------------------------------------------------------------
+  Where requested, add ancillary chunks.
+-----------------------------------------------------------------------------*/
     if (cmdline.transparencySpec)
         doTrnsChunk(pamP, pngxP,cmdline.transparency);
 
@@ -715,7 +816,7 @@ writePng(const struct pam * const pamP,
 
     /* no iccp */
 
-    doSbitChunk(pamP, pngxP, sBit);
+    doSbitChunk(pamP, pngxP, sigBits);
 
     if (cmdline.srgbintentSpec)
         doSrgbChunk(pngxP, cmdline.srgbintent);
@@ -741,54 +842,96 @@ writePng(const struct pam * const pamP,
     if (cmdline.timeSpec)
         doTimeChunk(pngxP, cmdline.time);
 
-    setShift(pngxP, sBit);
-
-    /* Write the ancillary chunks to PNG file */
+    /* Write the ancillary chunks to PNG image */
     pngx_writeInfo(pngxP);
+}
+
+
+
+static void
+setShift(struct pngx * const pngxP,
+         png_color_8   const sigBits) {
+
+    if (sigBits.red + sigBits.green + sigBits.blue +
+        sigBits.gray + sigBits.alpha > 0) {
 
-    if (pngColorType != PNG_COLOR_TYPE_GRAY && pnmBitDepth < 8) {
         /* Move the 1, 2, 4 bits to most significant bits */
-        pngx_setShift(pngxP, sBit);
-    }
-    if ((pngColorType == PNG_COLOR_TYPE_GRAY) && (pnmBitDepth < 8)) {
-        /* Pack multiple pixels in a byte */
-        pngx_setPacking(pngxP);
+        pngx_setShift(pngxP, sigBits);
     }
+}
 
-    writeRaster(pamP, pngxP, pnmBitDepth);
 
-    pngx_writeEnd(pngxP);
-    pngx_destroy(pngxP);
-}
 
+static void
+doIhdrChunk(struct pngx * const pngxP,
+            unsigned int  const width,
+            unsigned int  const height,
+            unsigned int  const pnmBitDepth,
+            int           const pngColorType,
+            bool          const interlace) {
+
+    int const interlaceMethod =
+        interlace ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE;
+
+    pngx_setIhdr(pngxP, width, height,
+                 pngBitDepth(pnmBitDepth, pngColorType), pngColorType,
+                 interlaceMethod, PNG_COMPRESSION_TYPE_BASE,
+                 PNG_FILTER_TYPE_BASE);
+}
 
 
 
 static void
-reportInputFormat(const struct pam * const pamP) {
+pamtopng(FILE *             const ifP,
+         FILE *             const ofP,
+         struct CmdlineInfo const cmdline) {
 
-    const char * formatDesc;
+    unsigned int  pnmBitDepth;
+    int           pngColorType;
+    struct pngx * pngxP;
+    png_color_8   sigBits;
+    struct pam    pam;
 
-    if (pamP->format == PBM_FORMAT || pamP->format == RPBM_FORMAT)
-        formatDesc = "PBM";
-    else if (pamP->format == PGM_FORMAT || pamP->format == RPGM_FORMAT)
-        formatDesc = "PGM";
-    else if (pamP->format == PPM_FORMAT || pamP->format == RPPM_FORMAT)
-        formatDesc = "PPM";
-    else if (pamP->format == PAM_FORMAT)
-        formatDesc = "PAM";
-    else
-        formatDesc = NULL;
+    pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
 
-    if (formatDesc)
-        pm_message("Input format = %s", formatDesc);
-    else
-        pm_message("Unrecognized input format, format code = 0x%x",
-                   pamP->format);
+    if (verbose)
+        reportInputFormat(&pam);
 
-    pm_message("Input tuple type = '%s'", pamP->tuple_type);
-    pm_message("Input depth = %u", pamP->depth);
-    pm_message("Input maxval = %u", (unsigned int) pamP->maxval);
+    pnmBitDepth = pm_maxvaltobits(pam.maxval);
+
+    pngColorType = colorTypeFromInputType(&pam);
+
+    pngx_create(&pngxP, PNGX_WRITE, NULL);
+
+    png_init_io(pngxP->png_ptr, ofP);
+
+    doIhdrChunk(pngxP, pam.width, pam.height,
+                pnmBitDepth, pngColorType, cmdline.interlace > 0);
+
+    sigBits = sigBitsFmImgType(pnmBitDepth, pngColorType);
+
+    addAncillaryChunks(&pam, pngxP, cmdline, sigBits);
+
+    setShift(pngxP, sigBits);
+
+    if ((pngColorType == PNG_COLOR_TYPE_GRAY) && (pnmBitDepth < 8)) {
+        /* Pack multiple pixels in a byte */
+        pngx_setPacking(pngxP);
+    }
+
+    if (cmdline.interlace) {
+        /* Libpng will expect us to provide pixels in interlaced sequence
+           if we write row-by-row, and that is much to difficult, so we
+           do whole-image-at-once and let Libpng do the work.
+        */
+        writeRasterWholeImg(&pam, pngxP, pnmBitDepth);
+    } else {
+        /* We save memory by going row-by-row */
+        writeRasterRowByRow(&pam, pngxP, pnmBitDepth);
+    }
+
+    pngx_writeEnd(pngxP);
+    pngx_destroy(pngxP);
 }
 
 
@@ -799,7 +942,6 @@ main(int           argc,
 
     FILE * ifP;
     struct CmdlineInfo cmdline;
-    struct pam pam;
 
     pm_proginit(&argc, argv);
 
@@ -809,12 +951,7 @@ main(int           argc,
 
     ifP = pm_openr(cmdline.inputFileName);
 
-    pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
-
-    if (verbose)
-        reportInputFormat(&pam);
-
-    writePng(&pam, stdout, cmdline);
+    pamtopng(ifP, stdout, cmdline);
 
     pm_close(ifP);
 
@@ -823,3 +960,18 @@ main(int           argc,
 
 
 
+/* Derived from pnmtorast.c by Jef Poskanzer and pamrgbatopng.c by Bryan
+** Henderson <bryanh@giraffe-data.com> and probably some other sources
+**
+** Copyright (C) 1995-1998 by Alexander Lehmann <alex@hal.rhein-main.de>
+**                        and Willem van Schaik <willem@schaik.com>
+** Copyright (C) 1999,2001 by Greg Roelofs <newt@pobox.com>
+** Copyright (C) 2015 by Willem van Schaik <willem@schaik.com>
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted, provided
+** that the above copyright notice appear in all copies and that both that
+** copyright notice and this permission notice appear in supporting
+** documentation.  This software is provided "as is" without express or
+** implied warranty.
+*/
diff --git a/converter/other/pamtopnm.c b/converter/other/pamtopnm.c
index f043d721..f0ad451a 100644
--- a/converter/other/pamtopnm.c
+++ b/converter/other/pamtopnm.c
@@ -96,7 +96,7 @@ validateTupleType(struct pam const inpam,
             /* It's a PPMable image */
         } else 
             pm_error("PAM image does not have a depth and tuple_type "
-                     "consistent with a PNM image."
+                     "consistent with a PNM image.  "
                      "According to its "
                      "header, depth is %d and tuple_type is '%s'.  "
                      "Use the -assume option to convert anyway.",
diff --git a/converter/other/pamtosvg/pxl-outline.c b/converter/other/pamtosvg/pxl-outline.c
index 456f41e1..19451c04 100644
--- a/converter/other/pamtosvg/pxl-outline.c
+++ b/converter/other/pamtosvg/pxl-outline.c
@@ -206,10 +206,8 @@ next_unmarked_pixel(unsigned int *   const row,
     direction_type const orig_dir = *dir;
 
     direction_type test_dir;
-    pixel color;
 
     test_dir = *dir;  /* initial value */
-    color = getBitmapColor(bitmap, *row, *col);
 
     do {
         if (is_valid_dir(orig_row, orig_col, test_dir, bitmap, *marked)) {
diff --git a/converter/other/pamtosvg/vector.c b/converter/other/pamtosvg/vector.c
index 0a5ef3a9..771e5f27 100644
--- a/converter/other/pamtosvg/vector.c
+++ b/converter/other/pamtosvg/vector.c
@@ -1,6 +1,6 @@
 /* vector.c: vector/point operations. */
 
-#define _XOPEN_SOURCE   /* Make sure M_PI is in <math.h> */
+#define _XOPEN_SOURCE 500  /* get M_PI in math.h */
 #include <math.h>
 #include <errno.h>
 #include <assert.h>
diff --git a/converter/other/pamtotga.c b/converter/other/pamtotga.c
index aca93015..1daa1e25 100644
--- a/converter/other/pamtotga.c
+++ b/converter/other/pamtotga.c
@@ -10,9 +10,11 @@
 ** implied warranty.
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE  /* Make sure string.h contains strdup() */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
+#include <assert.h>
 #include <string.h>
 
 #include "pm_c_util.h"
@@ -26,22 +28,24 @@
 /* Max number of colors allowed for colormapped output. */
 #define MAXCOLORS 256
 
-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 *inputFilespec;  /* Filespec of input file */
-    char *outName;
+    const char *          inputFileName;
+    const char *          outName;
     enum TGAbaseImageType imgType;
-    bool defaultFormat;
-    unsigned int norle;
+    enum TGAmapType       mapType;
+    bool                  defaultFormat;
+    unsigned int          norle;
+    unsigned int          verbose;
 };
 
 
 
 static void
-parseCommandLine(int argc, char ** argv,
-                 struct cmdlineInfo * const cmdlineP) {
+parseCommandLine(int argc, const char ** argv,
+                 struct CmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    Parse the program arguments (given by argc and argv) into a form
    the program can deal with more easily -- a cmdline_info structure.
@@ -56,38 +60,47 @@ parseCommandLine(int argc, char ** argv,
     unsigned int option_def_index;
 
     unsigned int outNameSpec;
-    unsigned int cmap, mono, rgb;
+    unsigned int cmap, cmap16, mono, rgb;
 
     option_def_index = 0;   /* incremented by OPTENT3 */
-    OPTENT3(0,   "name",       OPT_STRING, 
+    OPTENT3(0,   "name",       OPT_STRING,
             &cmdlineP->outName, &outNameSpec, 0);
-    OPTENT3(0,   "cmap",       OPT_FLAG, 
+    OPTENT3(0,   "cmap",       OPT_FLAG,
             NULL, &cmap, 0);
-    OPTENT3(0,   "mono",       OPT_FLAG, 
+    OPTENT3(0,   "cmap16",     OPT_FLAG,
+            NULL, &cmap16, 0);
+    OPTENT3(0,   "mono",       OPT_FLAG,
             NULL, &mono, 0);
-    OPTENT3(0,   "rgb",        OPT_FLAG, 
+    OPTENT3(0,   "rgb",        OPT_FLAG,
             NULL, &rgb, 0);
-    OPTENT3(0,   "norle",      OPT_FLAG, 
+    OPTENT3(0,   "norle",      OPT_FLAG,
             NULL, &cmdlineP->norle, 0);
-    
+    OPTENT3(0,   "verbose",    OPT_FLAG,
+            NULL, &cmdlineP->verbose, 0);
+
     opt.opt_table = option_def;
     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 *cmdline_p and others. */
 
-    if (cmap + mono + rgb > 1)
-        pm_error("You may specify only one of -cmap, -mono, and -rgb.");
+    if (cmap + cmap16 + mono + rgb > 1)
+        pm_error("You may specify only one of -cmap, -cmap16, "
+                 "-mono, and -rgb.");
 
-    if (cmap + mono + rgb == 0)
+    if (cmap + cmap16 + mono + rgb == 0)
         cmdlineP->defaultFormat = TRUE;
     else {
         cmdlineP->defaultFormat = FALSE;
-    
-        if (cmap)
+
+        if (cmap) {
             cmdlineP->imgType = TGA_MAP_TYPE;
-        else if (mono)
+            cmdlineP->mapType = TGA_MAPTYPE_LONG;
+        } else if (cmap16) {
+            cmdlineP->imgType = TGA_MAP_TYPE;
+            cmdlineP->mapType = TGA_MAPTYPE_SHORT;
+        } else if (mono)
             cmdlineP->imgType = TGA_MONO_TYPE;
         else if (rgb)
             cmdlineP->imgType = TGA_RGB_TYPE;
@@ -95,56 +108,22 @@ parseCommandLine(int argc, char ** argv,
 
     if (!outNameSpec)
         cmdlineP->outName = NULL;
-    
-    if (argc-1 == 0) 
-        cmdlineP->inputFilespec = "-";
+
+    if (argc-1 == 0)
+        cmdlineP->inputFileName = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
                  "specified %d", argc-1);
     else
-        cmdlineP->inputFilespec = argv[1];
-
-}
-
-
-static void
-writeTgaHeader(struct ImageHeader const tgaHeader) {
-
-    unsigned char flags;
-
-    putchar(tgaHeader.IdLength);
-    putchar(tgaHeader.CoMapType);
-    putchar(tgaHeader.ImgType);
-    putchar(tgaHeader.Index_lo);
-    putchar(tgaHeader.Index_hi);
-    putchar(tgaHeader.Length_lo);
-    putchar(tgaHeader.Length_hi);
-    putchar(tgaHeader.CoSize);
-    putchar(tgaHeader.X_org_lo);
-    putchar(tgaHeader.X_org_hi);
-    putchar(tgaHeader.Y_org_lo);
-    putchar(tgaHeader.Y_org_hi);
-    putchar(tgaHeader.Width_lo);
-    putchar(tgaHeader.Width_hi);
-    putchar(tgaHeader.Height_lo);
-    putchar(tgaHeader.Height_hi);
-    putchar(tgaHeader.PixelSize);
-    flags = (tgaHeader.AttBits & 0xf) | 
-        ((tgaHeader.Rsrvd & 0x1) << 4) |
-        ((tgaHeader.OrgBit & 0x1) << 5) | 
-        ((tgaHeader.OrgBit & 0x3) << 6);
-    putchar(flags);
+        cmdlineP->inputFileName = argv[1];
 
-    if (tgaHeader.IdLength > 0)
-        fwrite(tgaHeader.Id, 1, (int) tgaHeader.IdLength, stdout);
 }
-    
 
 
 static void
 putPixel(struct pam *          const pamP,
-         tuple                 const tuple, 
-         enum TGAbaseImageType const imgType, 
+         tuple                 const tuple,
+         enum TGAbaseImageType const imgType,
          bool                  const withAlpha,
          tuplehash             const cht) {
 /*----------------------------------------------------------------------------
@@ -166,22 +145,22 @@ putPixel(struct pam *          const pamP,
         if (imgType == TGA_RGB_TYPE && pamP->depth < 3) {
             /* Make RGB pixel out of a single input plane */
             unsigned int plane;
-            
-            for (plane = 0; plane < 3; ++plane) 
-                putchar(pnm_scalesample(tuple[0], 
+
+            for (plane = 0; plane < 3; ++plane)
+                putchar(pnm_scalesample(tuple[0],
                                         pamP->maxval, TGA_MAXVAL));
         } else if (imgType == TGA_MONO_TYPE)
-            putchar(pnm_scalesample(tuple[0], 
+            putchar(pnm_scalesample(tuple[0],
                                     pamP->maxval, TGA_MAXVAL));
         else {
-            putchar(pnm_scalesample(tuple[PAM_BLU_PLANE], 
+            putchar(pnm_scalesample(tuple[PAM_BLU_PLANE],
                                     pamP->maxval, TGA_MAXVAL));
-            putchar(pnm_scalesample(tuple[PAM_GRN_PLANE], 
+            putchar(pnm_scalesample(tuple[PAM_GRN_PLANE],
                                     pamP->maxval, TGA_MAXVAL));
-            putchar(pnm_scalesample(tuple[PAM_RED_PLANE], 
+            putchar(pnm_scalesample(tuple[PAM_RED_PLANE],
                                     pamP->maxval, TGA_MAXVAL));
             if (withAlpha)
-                putchar(pnm_scalesample(tuple[PAM_TRN_PLANE], 
+                putchar(pnm_scalesample(tuple[PAM_TRN_PLANE],
                                         pamP->maxval, TGA_MAXVAL));
         }
     }
@@ -190,39 +169,47 @@ putPixel(struct pam *          const pamP,
 
 
 static void
-putMapEntry(struct pam * const pamP, 
-            tuple        const value, 
+putMapEntry(struct pam * const pamP,
+            tuple        const value,
             int          const size) {
 
-    if (size == 15 || size == 16) {
-        /* 5 bits each of red, green, and blue.  Watch for byte order */
-
+    if (size == 8)
+        putchar(pnm_scalesample(value[0],
+                                pamP->maxval, TGA_MAXVAL));
+    else if (size == 15 || size == 16) {
         tuple const tuple31 = pnm_allocpamtuple(pamP);
 
+        assert(pamP->depth >= 3);
+
         pnm_scaletuple(pamP, tuple31, value, 31);
         {
-            int const mapentry = 
+            unsigned int const trn =
+                size == 16 && tuple31[PAM_TRN_PLANE] > 0 ? 1 : 0;
+
+            unsigned int const mapentry =
                 tuple31[PAM_BLU_PLANE] << 0 |
                 tuple31[PAM_GRN_PLANE] << 5 |
-                tuple31[PAM_RED_PLANE] << 10;
-            
+                tuple31[PAM_RED_PLANE] << 10 |
+                trn                    << 15;
+
+            /* Note little-endian byte swapping */
             putchar(mapentry % 256);
             putchar(mapentry / 256);
         }
         pnm_freepamtuple(tuple31);
-    } else if (size == 8)
-        putchar(pnm_scalesample(value[0], 
-                                pamP->maxval, TGA_MAXVAL));
-    else {
-        /* Must be 24 or 32 */
-        putchar(pnm_scalesample(value[PAM_BLU_PLANE], 
+    } else {
+        assert(size == 24 || size == 32);
+
+        assert(pamP->depth >= 3);
+
+        putchar(pnm_scalesample(value[PAM_BLU_PLANE],
                                 pamP->maxval, TGA_MAXVAL));
-        putchar(pnm_scalesample(value[PAM_GRN_PLANE], 
+        putchar(pnm_scalesample(value[PAM_GRN_PLANE],
                                 pamP->maxval, TGA_MAXVAL));
-        putchar(pnm_scalesample(value[PAM_RED_PLANE], 
+        putchar(pnm_scalesample(value[PAM_RED_PLANE],
                                     pamP->maxval, TGA_MAXVAL));
         if (size == 32)
-            putchar(pnm_scalesample(value[PAM_TRN_PLANE], 
+            putchar(pnm_scalesample(value[PAM_TRN_PLANE],
                                     pamP->maxval, TGA_MAXVAL));
     }
 }
@@ -230,8 +217,8 @@ putMapEntry(struct pam * const pamP,
 
 
 static void
-computeRunlengths(struct pam * const pamP, 
-                   tuple *      const tuplerow, 
+computeRunlengths(struct pam * const pamP,
+                   tuple *      const tuplerow,
                    int *        const runlength) {
 
     int col, start;
@@ -239,7 +226,7 @@ computeRunlengths(struct pam * const pamP,
     /* Initialize all run lengths to 0.  (This is just an error check.) */
     for (col = 0; col < pamP->width; ++col)
         runlength[col] = 0;
-    
+
     /* Find runs of identical pixels. */
     for ( col = 0; col < pamP->width; ) {
         start = col;
@@ -250,7 +237,7 @@ computeRunlengths(struct pam * const pamP,
                   pnm_tupleequal(pamP, tuplerow[col], tuplerow[start]));
         runlength[start] = col - start;
     }
-    
+
     /* Now look for runs of length-1 runs, and turn them into negative runs. */
     for (col = 0; col < pamP->width; ) {
         if (runlength[col] == 1) {
@@ -270,23 +257,23 @@ computeRunlengths(struct pam * const pamP,
 
 
 static void
-computeOutName(struct cmdlineInfo const cmdline, 
+computeOutName(struct CmdlineInfo const cmdline,
                const char **      const outNameP) {
-    
+
     char * workarea;
 
     if (cmdline.outName)
         workarea = strdup(cmdline.outName);
-    else if (streq(cmdline.inputFilespec, "-"))
+    else if (streq(cmdline.inputFileName, "-"))
         workarea = NULL;
     else {
         char * cp;
-        workarea = strdup(cmdline.inputFilespec);
+        workarea = strdup(cmdline.inputFileName);
         cp = strchr(workarea, '.');
         if (cp != NULL)
         	*cp = '\0';	/* remove extension */
     }
-    
+
     if (workarea == NULL)
         *outNameP = NULL;
     else {
@@ -305,16 +292,16 @@ validateTupleType(struct pam * const pamP) {
     if (streq(pamP->tuple_type, "RGB_ALPHA")) {
         if (pamP->depth < 4)
             pm_error("Invalid depth for tuple type RGB_ALPHA.  "
-                     "Should have at least 4 planes, but has %d.", 
+                     "Should have at least 4 planes, but has %d.",
                      pamP->depth);
     } else if (streq(pamP->tuple_type, "RGB")) {
         if (pamP->depth < 3)
             pm_error("Invalid depth for tuple type RGB.  "
-                     "Should have at least 3 planes, but has %d.", 
+                     "Should have at least 3 planes, but has %d.",
                      pamP->depth);
     } else if (streq(pamP->tuple_type, "GRAYSCALE")) {
     } else if (streq(pamP->tuple_type, "BLACKANDWHITE")) {
-    } else 
+    } else
         pm_error("Invalid type of input.  PAM tuple type is '%s'.  "
                  "This programs understands only RGB_ALPHA, RGB, GRAYSCALE, "
                  "and BLACKANDWHITE.", pamP->tuple_type);
@@ -324,69 +311,76 @@ validateTupleType(struct pam * const pamP) {
 
 static void
 computeImageType_cht(struct pam *            const pamP,
-                     struct cmdlineInfo      const cmdline, 
+                     struct CmdlineInfo      const cmdline,
                      tuple **                const tuples,
                      enum TGAbaseImageType * const baseImgTypeP,
+                     enum TGAmapType *       const mapTypeP,
                      bool *                  const withAlphaP,
                      tupletable *            const chvP,
-                     tuplehash *             const chtP, 
+                     tuplehash *             const chtP,
                      int *                   const ncolorsP) {
 
-    unsigned int ncolors;
+    unsigned int          ncolors;
     enum TGAbaseImageType baseImgType;
-    bool withAlpha;
+    enum TGAmapType       mapType;
+    bool                  withAlpha;
 
     validateTupleType(pamP);
 
-    withAlpha = (streq(pamP->tuple_type, "RGB_ALPHA"));
-
     if (cmdline.defaultFormat) {
         /* default the image type */
-        if (withAlpha) {
+        if (streq(pamP->tuple_type, "RGB_ALPHA")) {
             baseImgType = TGA_RGB_TYPE;
             *chvP = NULL;
-        } else if (pamP->depth > 1) {
+            withAlpha = true;
+        } else if (streq(pamP->tuple_type, "RGB")) {
             pm_message("computing colormap...");
-            *chvP = 
+            *chvP =
                 pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors);
             if (*chvP == NULL) {
                 pm_message("Too many colors for colormapped TGA.  Doing RGB.");
                 baseImgType = TGA_RGB_TYPE;
-            } else 
+            } else {
                 baseImgType = TGA_MAP_TYPE;
+                mapType = TGA_MAPTYPE_LONG;
+            }
+            withAlpha = false;
         } else {
             baseImgType = TGA_MONO_TYPE;
             *chvP = NULL;
+            withAlpha = false;
         }
     } else {
+        withAlpha = (streq(pamP->tuple_type, "RGB_ALPHA"));
+
         baseImgType = cmdline.imgType;
 
         if (baseImgType == TGA_MAP_TYPE) {
+            mapType = cmdline.mapType;
+
             if (withAlpha)
                 pm_error("Can't do a colormap because image has transparency "
                          "information");
             pm_message("computing colormap...");
-            *chvP = 
+            *chvP =
                 pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors);
-            if (*chvP == NULL) 
+            if (*chvP == NULL)
                 pm_error("Too many colors for colormapped TGA.  "
-                         "Use 'pnmquant %d' to reduce the number of colors.", 
+                         "Use 'pnmquant %d' to reduce the number of colors.",
                          MAXCOLORS);
         } else
             *chvP = NULL;
-        if (baseImgType == TGA_MONO_TYPE && pamP->depth > 1)
-            pm_error("For Mono TGA output, input must be "
-                     "GRAYSCALE or BLACKANDWHITE PAM or PBM or PGM");
     }
-    
+
     if (baseImgType == TGA_MAP_TYPE) {
-        pm_message("%d colors found.", ncolors);
+        pm_message("%u colors found.", ncolors);
         /* Make a hash table for fast color lookup. */
         *chtP = pnm_computetupletablehash(pamP, *chvP, ncolors);
     } else
         *chtP = NULL;
 
     *baseImgTypeP = baseImgType;
+    *mapTypeP     = mapType;
     *withAlphaP   = withAlpha;
     *ncolorsP     = ncolors;
 }
@@ -396,8 +390,9 @@ computeImageType_cht(struct pam *            const pamP,
 static void
 computeTgaHeader(struct pam *          const pamP,
                  enum TGAbaseImageType const baseImgType,
+                 enum TGAmapType       const mapType,
                  bool                  const withAlpha,
-                 bool                  const rle, 
+                 bool                  const rle,
                  int                   const ncolors,
                  unsigned char         const orgBit,
                  const char *          const id,
@@ -416,7 +411,7 @@ computeTgaHeader(struct pam *          const pamP,
         case TGA_RGB_TYPE:  tgaHeaderP->ImgType = TGA_RGB;           break;
         }
     }
-    
+
     if (id) {
         tgaHeaderP->IdLength = strlen(id);
         tgaHeaderP->Id = strdup(id);
@@ -428,7 +423,18 @@ computeTgaHeader(struct pam *          const pamP,
         tgaHeaderP->CoMapType = 1;
         tgaHeaderP->Length_lo = ncolors % 256;
         tgaHeaderP->Length_hi = ncolors / 256;
-        tgaHeaderP->CoSize = 8 * pamP->depth;
+        if (pamP->depth < 3)
+            tgaHeaderP->CoSize = 8;
+        else {
+            switch (mapType) {
+            case TGA_MAPTYPE_SHORT:
+                tgaHeaderP->CoSize = withAlpha ? 16 : 15;
+                break;
+            case TGA_MAPTYPE_LONG:
+                tgaHeaderP->CoSize = withAlpha ? 32 : 24;
+                break;
+            }
+        }
     } else {
         tgaHeaderP->CoMapType = 0;
         tgaHeaderP->Length_lo = 0;
@@ -440,7 +446,7 @@ computeTgaHeader(struct pam *          const pamP,
         tgaHeaderP->PixelSize = 8;
         break;
     case TGA_RGB_TYPE:
-        tgaHeaderP->PixelSize = 8 * MAX((withAlpha ? 4: 3), pamP->depth);
+        tgaHeaderP->PixelSize = 8 * (withAlpha ? 4: 3);
         break;
     case TGA_MONO_TYPE:
         tgaHeaderP->PixelSize = 8;
@@ -460,6 +466,70 @@ computeTgaHeader(struct pam *          const pamP,
 
 
 static void
+reportTgaHeader(struct ImageHeader const tgaHeader) {
+
+    switch (tgaHeader.ImgType) {
+    case TGA_RLEMono:
+        pm_message("Generating monochrome, run-length encoded");
+        break;
+    case TGA_RLEMap:
+        pm_message("Generating colormapped, run-length encoded");
+        pm_message("%u bits per colormap entry", tgaHeader.CoSize);
+        break;
+    case TGA_RLERGB:
+        pm_message("Generating RGB truecolor, run-length encoded");
+        break;
+    case TGA_Mono:
+        pm_message("Generating monochrome, uncompressed");
+        break;
+    case TGA_Map:
+        pm_message("Generating colormapped, uncompressed");
+        pm_message("%u bits per colormap entry", tgaHeader.CoSize);
+        break;
+    case TGA_RGB:
+        pm_message("Generating RGB truecolor, uncompressed");
+        break;
+    }
+    pm_message("%u bits per pixel", tgaHeader.PixelSize);
+}
+
+
+
+static void
+writeTgaHeader(struct ImageHeader const tgaHeader) {
+
+    unsigned char flags;
+
+    putchar(tgaHeader.IdLength);
+    putchar(tgaHeader.CoMapType);
+    putchar(tgaHeader.ImgType);
+    putchar(tgaHeader.Index_lo);
+    putchar(tgaHeader.Index_hi);
+    putchar(tgaHeader.Length_lo);
+    putchar(tgaHeader.Length_hi);
+    putchar(tgaHeader.CoSize);
+    putchar(tgaHeader.X_org_lo);
+    putchar(tgaHeader.X_org_hi);
+    putchar(tgaHeader.Y_org_lo);
+    putchar(tgaHeader.Y_org_hi);
+    putchar(tgaHeader.Width_lo);
+    putchar(tgaHeader.Width_hi);
+    putchar(tgaHeader.Height_lo);
+    putchar(tgaHeader.Height_hi);
+    putchar(tgaHeader.PixelSize);
+    flags = (tgaHeader.AttBits & 0xf) |
+        ((tgaHeader.Rsrvd & 0x1) << 4) |
+        ((tgaHeader.OrgBit & 0x1) << 5) |
+        ((tgaHeader.OrgBit & 0x3) << 6);
+    putchar(flags);
+
+    if (tgaHeader.IdLength > 0)
+        fwrite(tgaHeader.Id, 1, (int) tgaHeader.IdLength, stdout);
+}
+
+
+
+static void
 releaseTgaHeader(struct ImageHeader const tgaHeader) {
 
     if (tgaHeader.IdLength > 0)
@@ -468,9 +538,9 @@ releaseTgaHeader(struct ImageHeader const tgaHeader) {
 
 
 
-static void 
+static void
 writeTgaRaster(struct pam *          const pamP,
-               tuple **              const tuples, 
+               tuple **              const tuples,
                tuplehash             const cht,
                enum TGAbaseImageType const imgType,
                bool                  const withAlpha,
@@ -499,7 +569,7 @@ writeTgaRaster(struct pam *          const pamP,
                     int i;
                     putchar(-runlength[col] - 1);
                     for (i = 0; i < -runlength[col]; ++i)
-                        putPixel(pamP, tuples[realrow][col+i], 
+                        putPixel(pamP, tuples[realrow][col+i],
                                  imgType, withAlpha, cht);
                     col += -runlength[col];
                 } else
@@ -518,47 +588,53 @@ writeTgaRaster(struct pam *          const pamP,
 
 
 int
-main(int argc, char *argv[]) {
+main(int argc, const char **argv) {
 
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
     FILE * ifP;
     tuple ** tuples;
     struct pam pam;
-    int ncolors;
+    int colorCt;
     tupletable chv;
     tuplehash cht;
     struct ImageHeader tgaHeader;
     enum TGAbaseImageType baseImgType;
+    enum TGAmapType mapType;
     bool withAlpha;
-    const char *outName;
+    const char * outName;
 
-    pnm_init( &argc, argv );
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
-    ifP = pm_openr(cmdline.inputFilespec);
+    ifP = pm_openr(cmdline.inputFileName);
 
     computeOutName(cmdline, &outName);
 
     tuples = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
     pm_close(ifP);
 
-    computeImageType_cht(&pam, cmdline, tuples, 
-                         &baseImgType, &withAlpha, &chv, &cht, &ncolors);
+    computeImageType_cht(&pam, cmdline, tuples,
+                         &baseImgType, &mapType,
+                         &withAlpha, &chv, &cht, &colorCt);
 
     /* Do the Targa header */
-    computeTgaHeader(&pam, baseImgType, withAlpha, !cmdline.norle,
-                     ncolors, 0, outName, &tgaHeader);
+    computeTgaHeader(&pam, baseImgType, mapType, withAlpha, !cmdline.norle,
+                     colorCt, 0, outName, &tgaHeader);
+
+    if (cmdline.verbose)
+        reportTgaHeader(tgaHeader);
+
     writeTgaHeader(tgaHeader);
-    
+
     if (baseImgType == TGA_MAP_TYPE) {
         /* Write out the Targa colormap. */
-        int i;
-        for (i = 0; i < ncolors; ++i)
+        unsigned int i;
+        for (i = 0; i < colorCt; ++i)
             putMapEntry(&pam, chv[i]->tuple, tgaHeader.CoSize);
     }
 
-    writeTgaRaster(&pam, tuples, cht, baseImgType, withAlpha, 
+    writeTgaRaster(&pam, tuples, cht, baseImgType, withAlpha,
                    !cmdline.norle, 0);
 
     if (cht)
diff --git a/converter/other/pamtotiff.c b/converter/other/pamtotiff.c
index 7b645b23..115b8b4a 100644
--- a/converter/other/pamtotiff.c
+++ b/converter/other/pamtotiff.c
@@ -22,7 +22,6 @@
 */
 
 #define _XOPEN_SOURCE    /* Make sure stdio.h contains fileno() */
-#define _BSD_SOURCE      /* Make sure string.h contains strcasecmp() */
 
 #include <unistd.h>
 #include <stdio.h>
@@ -135,6 +134,44 @@ validateTagList(struct optNameValue const taglist[]) {
 
 
 static void
+parseIndexbits(bool          const indexbitsSpec,
+               char **       const indexbits,
+               CmdlineInfo * const cmdlineP) {
+
+    if (indexbitsSpec) {
+        unsigned int i;
+
+        /* Set initial values */
+        cmdlineP->indexsizeAllowed.b1 = false;
+        cmdlineP->indexsizeAllowed.b2 = false;
+        cmdlineP->indexsizeAllowed.b4 = false;
+        cmdlineP->indexsizeAllowed.b8 = false;
+
+        for (i = 0; indexbits[i]; ++i) {
+            const char * const thisItem = indexbits[i];
+            if (streq(thisItem, "1"))
+                cmdlineP->indexsizeAllowed.b1 = true;
+            else if (streq(thisItem, "2"))
+                cmdlineP->indexsizeAllowed.b2 = true;
+            else if (streq(thisItem, "4"))
+                cmdlineP->indexsizeAllowed.b4 = true;
+            else if (streq(thisItem, "8"))
+                cmdlineP->indexsizeAllowed.b8 = true;
+            else
+                pm_error("Invalid item in -indexbits list: '%s'.  "
+                         "We recognize only 1, 2, 4, and 8", thisItem);
+        }
+    } else {
+        cmdlineP->indexsizeAllowed.b1 = false;
+        cmdlineP->indexsizeAllowed.b2 = false;
+        cmdlineP->indexsizeAllowed.b4 = false;
+        cmdlineP->indexsizeAllowed.b8 = true;
+    }
+}
+
+
+
+static void
 parseCommandLine(int                 argc,
                  const char ** const argv,
                  CmdlineInfo * const cmdlineP) {
@@ -148,7 +185,7 @@ parseCommandLine(int                 argc,
 
     unsigned int none, packbits, lzw, g3, g4, msb2lsb, lsb2msb, opt_2d, fill;
     unsigned int flate, adobeflate;
-    char * indexbits;
+    char ** indexbits;
     char * resolutionunit;
 
     unsigned int appendSpec, outputSpec, predictorSpec, rowsperstripSpec,
@@ -191,13 +228,13 @@ parseCommandLine(int                 argc,
             &yresolutionSpec,  0);
     OPTENT3(0, "resolutionunit", OPT_STRING, &resolutionunit,
             &resolutionunitSpec,    0);
-    OPTENT3(0, "indexbits",    OPT_STRING,   &indexbits,
+    OPTENT3(0, "indexbits",    OPT_STRINGLIST, &indexbits,
             &indexbitsSpec,    0);
     OPTENT3(0, "tag",          OPT_NAMELIST, &cmdlineP->taglist, &tagSpec, 0);
 
     opt.opt_table = option_def;
-    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
-    opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
+    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, (char**)argv, opt, sizeof(opt), 0);
     /* Uses and sets argc, argv, and some of *cmdlineP and others. */
@@ -301,35 +338,16 @@ parseCommandLine(int                 argc,
     } else
         cmdlineP->resolutionunit = RESUNIT_INCH;
 
-    if (indexbitsSpec) {
-        if (strstr(indexbits, "1"))
-            cmdlineP->indexsizeAllowed.b1 = TRUE;
-        else
-            cmdlineP->indexsizeAllowed.b1 = FALSE;
-        if (strstr(indexbits, "2"))
-            cmdlineP->indexsizeAllowed.b2 = TRUE;
-        else
-            cmdlineP->indexsizeAllowed.b2 = FALSE;
-        if (strstr(indexbits, "4"))
-            cmdlineP->indexsizeAllowed.b4 = TRUE;
-        else
-            cmdlineP->indexsizeAllowed.b4 = FALSE;
-        if (strstr(indexbits, "8"))
-            cmdlineP->indexsizeAllowed.b8 = TRUE;
-        else
-            cmdlineP->indexsizeAllowed.b8 = FALSE;
-    } else {
-        cmdlineP->indexsizeAllowed.b1 = FALSE;
-        cmdlineP->indexsizeAllowed.b2 = FALSE;
-        cmdlineP->indexsizeAllowed.b4 = FALSE;
-        cmdlineP->indexsizeAllowed.b8 = TRUE;
-    }
+    parseIndexbits(indexbitsSpec, indexbits, cmdlineP);
+
+    if (indexbitsSpec)
+        free(indexbits);
 
     if (tagSpec)
         validateTagList(cmdlineP->taglist);
     else {
         MALLOCARRAY_NOFAIL(cmdlineP->taglist, 1);
-        cmdlineP->taglist[0].name = NULL;
+        cmdlineP->taglist[0].name  = NULL;
         cmdlineP->taglist[0].value = NULL;
     }
 
@@ -345,6 +363,14 @@ parseCommandLine(int                 argc,
 
 
 static void
+freeCmdline(CmdlineInfo const cmdline) {
+
+    pm_optDestroyNameValueList(cmdline.taglist);
+}
+
+
+
+static void
 fillRowOfSubBytePixels(struct pam *    const pamP,
                        const tuple *   const tuplerow,
                        unsigned char * const buf,
@@ -517,7 +543,7 @@ writeScanLines(struct pam *   const pamP,
 
        The samples form pixel values according to the pixel format indicated
        by the TIFF photometric.  E.g. if it is MINISWHITE, then a pixel is
-       one sample and a value of 0 for that sample means white.
+       grayscale, composed of one sample where a value of 0 means white.
     */
     MALLOCARRAY(buf, bytesperrow);
 
@@ -577,14 +603,14 @@ analyzeColorsInRgbInput(struct pam *   const pamP,
                         CmdlineInfo    const cmdline,
                         int            const maxcolors,
                         tupletable *   const chvP,
-                        unsigned int * const colorsP,
+                        unsigned int * const colorCtP,
                         bool *         const grayscaleP) {
 /*----------------------------------------------------------------------------
    Same as analyzeColors(), except assuming input image has R/G/B tuples.
 -----------------------------------------------------------------------------*/
     if (cmdline.color && cmdline.truecolor) {
         *chvP = NULL;
-        *grayscaleP = FALSE;
+        *grayscaleP = false;
     } else {
         tupletable chv;
         bool grayscale;
@@ -592,17 +618,17 @@ analyzeColorsInRgbInput(struct pam *   const pamP,
         pm_message("computing colormap...");
         chv = pnm_computetuplefreqtable2(pamP, NULL, maxcolors,
                                          pamP->maxval,
-                                         colorsP);
+                                         colorCtP);
         if (chv == NULL) {
-            grayscale = FALSE;
+            grayscale = false;
         } else {
             unsigned int i;
             pm_message("%u color%s found",
-                       *colorsP, *colorsP == 1 ? "" : "s");
-            grayscale = TRUE;  /* initial assumption */
-            for (i = 0; i < *colorsP && grayscale; ++i) {
+                       *colorCtP, *colorCtP == 1 ? "" : "s");
+            grayscale = true;  /* initial assumption */
+            for (i = 0; i < *colorCtP && grayscale; ++i) {
                 if (!pnm_rgbtupleisgray(chv[i]->tuple))
-                    grayscale = FALSE;
+                    grayscale = false;
             }
         }
         *grayscaleP = grayscale;
@@ -634,21 +660,22 @@ analyzeColors(struct pam *   const pamP,
               CmdlineInfo    const cmdline,
               int            const maxcolors,
               tupletable *   const chvP,
-              unsigned int * const colorsP,
+              unsigned int * const colorCtP,
               bool *         const grayscaleP) {
 /*----------------------------------------------------------------------------
    Analyze the colors in the input image described by 'pamP', whose file
    is positioned to the raster.
 
-   If the colors, combined with command line options 'cmdline', indicate
-   a colormapped TIFF should be generated, return as *chvP the address
-   of a color map (in newly malloc'ed space).  If a colormapped TIFF is
-   not indicated, return *chvP == NULL.
-
    Return *grayscaleP == true iff the image should be stored as a grayscale
    image (which means the image is monochromatic and the user doesn't
    insist on color format).
 
+   If *grayscaleP is false and the colors, combined with command line options
+   'cmdline', indicate a colormapped TIFF should be generated, return as *chvP
+   the address of a color map (in newly malloc'ed space) and the number of
+   colors in it as *colorCtP.  If a colormapped color TIFF is not indicated,
+   return *chvP == NULL and nothing as *colorCtP.
+
    Leave the file position undefined.
 -----------------------------------------------------------------------------*/
     if (pamP->depth >= 3)
@@ -656,10 +683,28 @@ analyzeColors(struct pam *   const pamP,
            (tuple type RGB or RGB_ALPHA)
         */
         analyzeColorsInRgbInput(pamP, cmdline, maxcolors,
-                                chvP, colorsP, grayscaleP);
+                                chvP, colorCtP, grayscaleP);
     else {
         *chvP = NULL;
-        *grayscaleP = TRUE;
+        *grayscaleP = true;
+    }
+}
+
+
+
+static void
+reportTiffType(bool const grayscale,
+               bool const colormapped,
+               unsigned int const colorCt,
+               bool const verbose) {
+
+    if (verbose) {
+        pm_message("Generating %s TIFF", grayscale ? "grayscale" : "color");
+
+        if (colormapped)
+            pm_message("TIFF will have palette of %u colors", colorCt);
+        else
+            pm_message("TIFF will be truecolor (24 bit RGB)");
     }
 }
 
@@ -668,7 +713,7 @@ analyzeColors(struct pam *   const pamP,
 static void
 computeRasterParm(struct pam *     const pamP,
                   tupletable       const chv,
-                  int              const colors,
+                  int              const colorCt,
                   bool             const grayscale,
                   int              const compression,
                   bool             const minisblack,
@@ -682,7 +727,7 @@ computeRasterParm(struct pam *     const pamP,
    Compute the parameters of the raster portion of the TIFF image.
 
    'minisblack' and 'miniswhite' mean the user requests the corresponding
-   photometric.  Both FALSE means user has no explicit requirement.
+   photometric.  Both false means user has no explicit requirement.
 -----------------------------------------------------------------------------*/
     unsigned short defaultPhotometric;
     /* The photometric we use if the user specified no preference */
@@ -709,14 +754,14 @@ computeRasterParm(struct pam *     const pamP,
         if (chv) {
             *samplesperpixelP = 1;  /* Pixel is just the one index value */
             *bitspersampleP =
-                colors <=   2 && indexsizeAllowed.b1 ? 1 :
-                colors <=   4 && indexsizeAllowed.b2 ? 2 :
-                colors <=  16 && indexsizeAllowed.b4 ? 4 :
-                colors <= 256 && indexsizeAllowed.b8 ? 8 :
+                colorCt <=   2 && indexsizeAllowed.b1 ? 1 :
+                colorCt <=   4 && indexsizeAllowed.b2 ? 2 :
+                colorCt <=  16 && indexsizeAllowed.b4 ? 4 :
+                colorCt <= 256 && indexsizeAllowed.b8 ? 8 :
                 0;
             if (*bitspersampleP == 0)
                 pm_error("Your -indexbits option is insufficient for the "
-                         "%d colors in this image.", colors);
+                         "%d colors in this image.", colorCt);
 
             defaultPhotometric = PHOTOMETRIC_PALETTE;
         } else {
@@ -736,11 +781,15 @@ computeRasterParm(struct pam *     const pamP,
         }
     }
 
-    if (miniswhite)
+    if (miniswhite) {
+        if (!grayscale)
+            pm_error("Image is color, so -miniswhite is invalid");
         *photometricP = PHOTOMETRIC_MINISWHITE;
-    else if (minisblack)
+    } else if (minisblack) {
+        if (!grayscale)
+            pm_error("Image is color, so -minisblack is invalid");
         *photometricP = PHOTOMETRIC_MINISBLACK;
-    else
+    } else
         *photometricP = defaultPhotometric;
 
     {
@@ -764,9 +813,9 @@ computeRasterParm(struct pam *     const pamP,
 /*----------------------------------------------------------------------------
   WRITE MODES
   -----------
-  
+
   The Tiff library does all output.  There are several issues:
-  
+
     1) The manner of output is opaque to the library client.  I.e.  we cannot
        see or control it.
 
@@ -777,22 +826,22 @@ computeRasterParm(struct pam *     const pamP,
 
     4) The Tiff library produces unhelpful error messages when the above
        conditions are not met.
-  
+
   We provide two modes for output:
-  
+
   1. Tmpfile mode (default)
-  
+
      We have the Tiff library direct output to an unnamed temporary file we
      create which is seekable and readable.  When output is complete, we copy
      the file's contents to Standard Output.
-  
+
   2. Direct mode (specified with -output)
-  
+
      We have the Tiff library write output to the specified file.  As the Tiff
      library requires taht it be be seekable and readable, we fail the program
      rather than ask the Tiff library to use the file if it does not meet
      these requirements.
-  
+
      Direct mode is further divided into append and create.  They are the same
      except that in append mode, we insist that the file already exist,
      whereas with create mode, we create it if necessary.  In either case, if
@@ -972,9 +1021,9 @@ copyBufferToStdout(int const tmpfileFd) {
 
 
 static void
-destroyTiffGenerator(WriteMethod const writeMethod,
-                     TIFF *      const tifP,
-                     int         const ofd) {
+closeTiffGenerator(WriteMethod const writeMethod,
+                   TIFF *      const tifP,
+                   int         const ofd) {
 
     TIFFFlushData(tifP);
 
@@ -994,7 +1043,7 @@ static void
 createTiffColorMap(struct pam *       const pamP,
                    unsigned int       const bitspersample,
                    tupletable         const chv,
-                   unsigned int       const colors,
+                   unsigned int       const colorCt,
                    unsigned short *** const tiffColorMapP) {
 
     unsigned int const colorMapSize = 1 << bitspersample;
@@ -1009,7 +1058,7 @@ createTiffColorMap(struct pam *       const pamP,
     for (i = 0; i < colorMapSize; ++i) {
         unsigned int plane;
         for (plane = 0; plane < pamP->depth; ++plane) {
-            if (i < colors)
+            if (i < colorCt)
                 tiffColorMap[plane][i] =
                     chv[i]->tuple[plane] * 65535L / pamP->maxval;
             else
@@ -1136,7 +1185,7 @@ convertImage(FILE *       const ifP,
     tuplehash cht;
     unsigned short ** tiffColorMap;  /* malloc'ed */
     struct pam pam;
-    unsigned int colors;
+    unsigned int colorCt;
     bool grayscale;
     unsigned short photometric;
     unsigned short samplesperpixel;
@@ -1152,14 +1201,16 @@ convertImage(FILE *       const ifP,
 
     pm_tell2(ifP, &rasterPos, sizeof(rasterPos));
 
-    analyzeColors(&pam, cmdline, MAXCOLORS, &chv, &colors, &grayscale);
+    analyzeColors(&pam, cmdline, MAXCOLORS, &chv, &colorCt, &grayscale);
+
+    reportTiffType(grayscale, chv != NULL, colorCt, cmdline.verbose);
 
     /* Go back to beginning of raster */
     pm_seek2(ifP, &rasterPos, sizeof(rasterPos));
 
     /* Figure out TIFF parameters. */
 
-    computeRasterParm(&pam, chv, colors, grayscale,
+    computeRasterParm(&pam, chv, colorCt, grayscale,
                       cmdline.compression,
                       cmdline.minisblack, cmdline.miniswhite,
                       cmdline.indexsizeAllowed,
@@ -1172,10 +1223,10 @@ convertImage(FILE *       const ifP,
         cht = NULL;
         tiffColorMap = NULL;
     } else {
-        createTiffColorMap(&pam, bitspersample, chv, colors, &tiffColorMap);
+        createTiffColorMap(&pam, bitspersample, chv, colorCt, &tiffColorMap);
 
         /* Convert color vector to color hash table, for fast lookup. */
-        cht = pnm_computetupletablehash(&pam, chv, colors);
+        cht = pnm_computetupletablehash(&pam, chv, colorCt);
         pnm_freetupletable(&pam, chv);
     }
 
@@ -1193,9 +1244,6 @@ convertImage(FILE *       const ifP,
 
 
 
-
-
-
 int
 main(int argc, const char *argv[]) {
     CmdlineInfo cmdline;
@@ -1205,7 +1253,7 @@ main(int argc, const char *argv[]) {
     int ofd;
     int eof;
     unsigned int imageSeq;
-    
+
     pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
@@ -1229,7 +1277,7 @@ main(int argc, const char *argv[]) {
         break;
     }
 
-    eof = FALSE;  /* initial assumption */
+    eof = false;  /* initial assumption */
     imageSeq = 0;
 
     while (!eof) {
@@ -1254,9 +1302,11 @@ main(int argc, const char *argv[]) {
         }
     }
 
-    destroyTiffGenerator(cmdline.writeMethod, tifP, ofd);
+    closeTiffGenerator(cmdline.writeMethod, tifP, ofd);
     pm_close(ifP);
 
+    freeCmdline(cmdline);
+
     return 0;
 }
 
diff --git a/converter/other/pamtouil.c b/converter/other/pamtouil.c
index ee7f5ae6..01858cbc 100644
--- a/converter/other/pamtouil.c
+++ b/converter/other/pamtouil.c
@@ -13,6 +13,7 @@
 ** implied warranty.
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE  /* Make sure string.h contains strdup() */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #include <ctype.h>
diff --git a/converter/other/pamtowinicon.c b/converter/other/pamtowinicon.c
index 7e2c9e86..3d77d731 100644
--- a/converter/other/pamtowinicon.c
+++ b/converter/other/pamtowinicon.c
@@ -643,7 +643,7 @@ writePng(const struct pam * const pamP,
     
     if (haveAlpha || haveAnd) {
         const char * alphaFileName;
-        const char * command;
+        const char * alphaOpt;
 
         if (haveAlpha)
             makeAlphaFile(pamP, tuples, alphaPlane, &alphaFileName);
@@ -653,19 +653,19 @@ writePng(const struct pam * const pamP,
         strcpy (pam.tuple_type,
                 pam.depth == 3 ? PAM_PPM_TUPLETYPE: PAM_PGM_TUPLETYPE);
         
-        pm_asprintf(&command, "pnmtopng -alpha=\"%s\"", alphaFileName);
+        pm_asprintf(&alphaOpt, "-alpha=%s", alphaFileName);
 
-        pm_system(pm_feed_from_pamtuples, &pamTuples,
-                  acceptToFile, &acceptParm,
-                  command);
+        pm_system_lp("pnmtopng", pm_feed_from_pamtuples, &pamTuples,
+                     acceptToFile, &acceptParm,
+                     "pnmtopng", alphaOpt, NULL);
     
-        pm_strfree(command);
+        pm_strfree(alphaOpt);
     
         unlink(alphaFileName);
     } else {
-        pm_system(pm_feed_from_pamtuples, &pamTuples,
-                  acceptToFile, &acceptParm,
-                  "pnmtopng");
+        pm_system_lp("pnmtopng", pm_feed_from_pamtuples, &pamTuples,
+                     acceptToFile, &acceptParm,
+                     "pnmtopng", NULL);
     }
 
     *sizeP = pngSize;
diff --git a/converter/other/pclxl.h b/converter/other/pclxl.h
index b1d1c043..5b3eae4b 100644
--- a/converter/other/pclxl.h
+++ b/converter/other/pclxl.h
@@ -48,7 +48,7 @@ enum DataOrg {
 };
 
 enum DataSource {
-    eDefault = 0
+    eDefaultDataSource = 0
 };
 
 enum DataType {
@@ -58,9 +58,9 @@ enum DataType {
     eSint16 = 3
 };
 
-enum DitherMatrix {  
+enum DitherMatrix {
     eDeviceBest = 0
-}; 
+};
 
 
 enum DuplexPageMode {
@@ -109,7 +109,7 @@ enum Measure {
 };
 
 enum MediaSource {
-    eDefaultSource = 0,
+    eDefaultMediaSource = 0,
     eAutoSelect = 1,
     eManualFeed = 2,
     eMultiPurposeTray = 3,
@@ -246,7 +246,7 @@ enum Attribute {
     aYSpacingData        = 176,
     aCharBoldValue       = 177
 };
-                                          
+
 enum Operator {
     oBeginSession        = 0x41,
     oEndSession          = 0x42,
@@ -317,7 +317,7 @@ enum Operator {
     oPiePath             = 0x9f,
     oRectangle           = 0xa0,
     oRectanglePath       = 0xa1,
-    oRoundRectangle      = 0xa2,  
+    oRoundRectangle      = 0xa2,
     oRoundRectanglePath  = 0xa3,
     oText                = 0xa8,
     oTextPath            = 0xa9,
@@ -384,7 +384,7 @@ struct sPaperFormat {
 enum {
     eSTART,
     eRLE,
-    eLIT 
+    eLIT
 } RLEstates;
 
 #endif
diff --git a/converter/other/pdbimgtopam.c b/converter/other/pdbimgtopam.c
index 3cb4129a..67044109 100644
--- a/converter/other/pdbimgtopam.c
+++ b/converter/other/pdbimgtopam.c
@@ -97,6 +97,8 @@ parseCommandLine(int argc, const char ** argv,
         cmdlineP->inputFileName = argv[1];
     else
         pm_error("Program takes at most one argument:  input file name");
+
+    free(option_def);
 }
 
 
@@ -211,7 +213,8 @@ readCompressed(IMAGE *    const imgP,
 
     if (end_offset == UNKNOWN_OFFSET) {
         /*
-         * Read until EOF. Some of them have an extra zero byte
+         * Read sufficient amount for worst-case compressed image,
+         * or until EOF.  Some of them have an extra zero byte
          * dangling off the end.  I originally thought this was
          * an empty note record (even though there was no record
          * header for it); however, the release notes for Image
@@ -221,12 +224,20 @@ readCompressed(IMAGE *    const imgP,
          * this extra byte and ignore it by paying attention to
          * the image dimensions.
          */
-        MALLOCARRAY(buffer, ipdb_img_size(imgP));
+       size_t const maxCompressedSizeWithBloat = ipdb_img_size(imgP) * 2;
+         /*
+          * Provide a buffer large enough for the worst case.
+          * See note in lib/util/runlength.c .
+          * We do not use pm_rlenc_allocoutbuf() because there is no
+          * guarantee that the encoder that produced the image was
+          * efficient.
+          */
+       MALLOCARRAY(buffer, maxCompressedSizeWithBloat);
 
         if (buffer == NULL)
             retval = ENOMEM;
         else {
-            dataSize = fread(buffer, 1, ipdb_img_size(imgP), fP);
+            dataSize = fread(buffer, 1,  maxCompressedSizeWithBloat, fP);
             if (dataSize <= 0)
                 retval = EIO;
             else
@@ -307,6 +318,8 @@ imageReadData(FILE *   const fileP,
               IMAGE *  const imgP,
               uint32_t const end_offset) {
 
+    size_t const imageSize = ipdb_img_size(imgP);  
+
     int retval;
     size_t dataSize;
     uint8_t * buffer;
@@ -318,14 +331,24 @@ imageReadData(FILE *   const fileP,
          * Compressed data can cross row boundaries so we decompress
          * the data here to avoid messiness in the row access functions.
          */
-        if (dataSize != ipdb_img_size(imgP)) {
-            decompress(buffer, dataSize, ipdb_img_size(imgP), &imgP->data);
+        if (dataSize < imageSize || imgP->version == 1) {
+            if (imgP->version == 0)
+                pm_message("Image header says raster data is uncompressed.  "
+                           "Encountered only %u instead of the "
+                           "required %u bytes.  Assuming compressed mode.",
+                           (unsigned)dataSize, (unsigned)imageSize);
+            decompress(buffer, dataSize, imageSize, &imgP->data);
             if (imgP->data == NULL)
                 retval = ENOMEM;
             else
                 imgP->compressed = true;
             free(buffer);
         } else {
+            if (dataSize > imageSize)
+                pm_message("Image header says raster data is uncompressed. "
+                           "Encountered %u instead of the required %u bytes. "
+                           "Assuming uncompressed mode.",
+                           (unsigned)dataSize, (unsigned)imageSize);
             imgP->compressed = false;
             imgP->data       = buffer;
             /* Storage at 'buffer' now belongs to *imgP */
diff --git a/converter/other/pgmtoppm.c b/converter/other/pgmtoppm.c
index f8a69424..c3a26594 100644
--- a/converter/other/pgmtoppm.c
+++ b/converter/other/pgmtoppm.c
@@ -10,6 +10,7 @@
 ** implied warranty.
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE  /* Make sure strdup() is in <string.h> */
 #include <string.h>
diff --git a/converter/other/pngtopam.c b/converter/other/pngtopam.c
index 8743174e..1023c6c6 100644
--- a/converter/other/pngtopam.c
+++ b/converter/other/pngtopam.c
@@ -64,12 +64,12 @@ static bool verbose;
 
 
 static void
-parseCommandLine(int                  argc, 
+parseCommandLine(int                  argc,
                  const char **        argv,
                  struct CmdlineInfo * 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.
@@ -90,13 +90,13 @@ parseCommandLine(int                  argc,
     MALLOCARRAY(option_def, 100);
 
     option_def_index = 0;   /* incremented by OPTENT3 */
-    OPTENT3(0, "verbose",     OPT_FLAG,   NULL,                  
+    OPTENT3(0, "verbose",     OPT_FLAG,   NULL,
             &cmdlineP->verbose,       0);
-    OPTENT3(0, "alpha",       OPT_FLAG,   NULL,                  
+    OPTENT3(0, "alpha",       OPT_FLAG,   NULL,
             &alphaSpec,               0);
-    OPTENT3(0, "alphapam",    OPT_FLAG,   NULL,                  
+    OPTENT3(0, "alphapam",    OPT_FLAG,   NULL,
             &alphapamSpec,            0);
-    OPTENT3(0, "mix",         OPT_FLAG,   NULL,                  
+    OPTENT3(0, "mix",         OPT_FLAG,   NULL,
             &mixSpec,                 0);
     OPTENT3(0, "background",  OPT_STRING, &cmdlineP->background,
             &backgroundSpec,          0);
@@ -104,9 +104,9 @@ parseCommandLine(int                  argc,
             &cmdlineP->gammaSpec,     0);
     OPTENT3(0, "text",        OPT_STRING, &cmdlineP->text,
             &textSpec,                0);
-    OPTENT3(0, "time",        OPT_FLAG,   NULL,                  
+    OPTENT3(0, "time",        OPT_FLAG,   NULL,
             &cmdlineP->time,          0);
-    OPTENT3(0, "byrow",       OPT_FLAG,   NULL,                  
+    OPTENT3(0, "byrow",       OPT_FLAG,   NULL,
             &cmdlineP->byrow,         0);
 
     opt.opt_table = option_def;
@@ -248,7 +248,7 @@ computePngLineSize(struct pngx * const pngxP) {
     if (UINT_MAX / bytesPerSample / samplesPerPixel < pngx_imageWidth(pngxP))
         pm_error("Width %u of PNG is uncomputably large",
                  pngx_imageWidth(pngxP));
-       
+
     return pngx_imageWidth(pngxP) * bytesPerSample * samplesPerPixel;
 }
 
@@ -370,7 +370,7 @@ reader_createRowByRow(struct pngx * const pngxP,
 
     readerP->pngRaster = NULL;
 
-    MALLOCARRAY(readerP->rowBuf, computePngLineSize(pngxP)); 
+    MALLOCARRAY(readerP->rowBuf, computePngLineSize(pngxP));
 
     if (!readerP->rowBuf)
         pm_error("Could not allocate %u bytes for a PNG row buffer",
@@ -393,7 +393,7 @@ reader_destroy(Reader * const readerP) {
 
     if (readerP->pngRaster)
         freePngRaster(readerP->pngRaster, readerP->pngxP);
-   
+
     if (readerP->rowBuf)
         free(readerP->rowBuf);
 
@@ -434,14 +434,14 @@ getPngVal(const png_byte ** const pp,
           int               const bitDepth) {
 
     png_uint_16 c;
-    
+
     if (bitDepth == 16)
         c = *(*pp)++ << 8;
     else
         c = 0;
 
     c |= *(*pp)++;
-    
+
     return c;
 }
 
@@ -498,7 +498,7 @@ setTuple(const struct pam *  const pamP,
             tuple[PAM_GRN_PLANE] = foreground.g;
             tuple[PAM_BLU_PLANE] = foreground.b;
             tuple[PAM_TRN_PLANE] = alpha;
-        }    
+        }
     } else {
         assert(alphaHandling == ALPHA_MIX);
 
@@ -540,7 +540,7 @@ saveText(struct pngx * const pngxP,
 
         while (text.line[i].key[j] != '\0' &&
                text.line[i].key[j] != ' ')
-            ++j;    
+            ++j;
 
         if (text.line[i].key[j] != ' ') {
             fprintf(tfP, "%s", text.line[i].key);
@@ -552,7 +552,7 @@ saveText(struct pngx * const pngxP,
                 putc(' ', tfP);
         }
         putc(' ', tfP); /* at least one space between key and text */
-    
+
         for (j = 0; j < text.line[i].text_length; ++j) {
             putc(text.line[i].text[j], tfP);
             if (text.line[i].text[j] == '\n') {
@@ -607,11 +607,11 @@ dumpTypeAndFilter(struct pngx * const pngxP) {
     case PNG_COLOR_TYPE_GRAY:
         typeString = "gray";
         break;
-        
+
     case PNG_COLOR_TYPE_GRAY_ALPHA:
         typeString = "gray+alpha";
         break;
-        
+
     case PNG_COLOR_TYPE_PALETTE:
         typeString = "palette";
         break;
@@ -630,13 +630,13 @@ dumpTypeAndFilter(struct pngx * const pngxP) {
         pm_asprintf(&filterString, "base filter");
         break;
     default:
-        pm_asprintf(&filterString, "unknown filter type %d", 
+        pm_asprintf(&filterString, "unknown filter type %d",
                     pngx_filterType(pngxP));
     }
 
     pm_message("%s, %s, %s",
                typeString,
-               pngx_interlaceType(pngxP) ? 
+               pngx_interlaceType(pngxP) ?
                "Adam7 interlaced" : "not interlaced",
                filterString);
 
@@ -679,7 +679,7 @@ dumpPngInfo(struct pngx * const pngxP) {
                    pngx_gama(pngxP));
     else
         pm_message("gAMA chunk (image gamma): not present");
-    
+
     if (pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT))
         pm_message("sBIT chunk: present");
     else
@@ -739,7 +739,7 @@ transColor(struct pngx * const pngxP) {
     struct pngx_trns const trans = pngx_trns(pngxP);
 
     assert(pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS));
-    
+
     return trans.transColor;
 }
 
@@ -775,7 +775,7 @@ isTransparentColor(pngcolor        const color,
            We could fix this by not letting libpng gamma-correct the
            pixels, and just do it ourselves.
         */
-    
+
         switch (pngx_colorType(pngxP)) {
         case PNG_COLOR_TYPE_GRAY:
             retval = color.r == gammaCorrect(transColor16.gray, gamma,
@@ -788,7 +788,7 @@ isTransparentColor(pngcolor        const color,
                                                      pngxP->maxval));
         }
         }
-    } else 
+    } else
         retval = FALSE;
 
     return retval;
@@ -810,7 +810,7 @@ setupGammaCorrection(struct pngx *     const pngxP,
    use *gammaCorrectionP to do it).
 
    'screenGammaIsKnown' means we know what the screen gamma is, and it is
-   'screenGamma'.  If we don't know what the screen gamma is, gamma 
+   'screenGamma'.  If we don't know what the screen gamma is, gamma
    correction is not possible, so we set up for no gamma correction.
 
    The gamma correction we ordain is a combination of the image gamma,
@@ -845,7 +845,7 @@ setupGammaCorrection(struct pngx *     const pngxP,
             gammaCorrectionP->needCorrection = true;
             gammaCorrectionP->gamma = imageGamma * screenGamma;
             /* In case of gamma-corrections, sBIT's as in the
-               PNG-file are not valid anymore 
+               PNG-file are not valid anymore
             */
             pngx_removeChunk(pngxP, PNG_INFO_sBIT);
             if (verbose)
@@ -870,7 +870,7 @@ paletteHasPartialTransparency(struct pngx * const pngxP) {
 
             bool foundGray;
             unsigned int i;
-            
+
             for (i = 0, foundGray = FALSE;
                  i < trans.numTrans && !foundGray;
                  ++i) {
@@ -945,7 +945,7 @@ getComponentSbit(struct pngx *       const pngxP,
         /* We care about both the foreground and the alpha */
         bool fgNotUniform;
         png_byte fgSbit;
-        
+
         getComponentSbitFg(pngxP, &fgSbit, &fgNotUniform);
 
         if (fgNotUniform)
@@ -961,7 +961,7 @@ getComponentSbit(struct pngx *       const pngxP,
     }
 }
 
-                 
+
 
 static void
 shiftPalette(struct pngx * const pngxP,
@@ -977,9 +977,9 @@ shiftPalette(struct pngx * const pngxP,
                  shift);
     else {
         struct pngx_plte const palette = pngx_plte(pngxP);
-        
+
         unsigned int i;
-        
+
         for (i = 0; i < palette.size; ++i) {
             palette.palette[i].red   >>= (8 - shift);
             palette.palette[i].green >>= (8 - shift);
@@ -1004,7 +1004,7 @@ computeMaxvalFromSbit(struct pngx *       const pngxP,
        mix both, the multiplication may result in values that require
        the normal bit depth, so we will use the sBIT info only for
        transparency, if we know that only solid and fully transparent
-       is used 
+       is used
     */
 
     bool notUniform;
@@ -1043,7 +1043,7 @@ computeMaxvalFromSbit(struct pngx *       const pngxP,
                            "writing file with %u bits", componentSigBit);
                 *maxvalP = (1l << componentSigBit) - 1;
                 *succeededP = true;
-                
+
                 if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE)
                     shiftPalette(pngxP, componentSigBit);
                 else
@@ -1070,7 +1070,7 @@ setupSignificantBits(struct pngx *       const pngxP,
   Also set up *pngxP for the corresponding significant bits.
 -----------------------------------------------------------------------------*/
     bool gotItFromSbit;
-    
+
     if (pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT))
         computeMaxvalFromSbit(pngxP, alphaHandling,
                               &pngxP->maxval, &gotItFromSbit, errorLevelP);
@@ -1119,7 +1119,7 @@ imageHasColor(struct pngx * const pngxP) {
 
         bool foundColor;
         unsigned int i;
-            
+
         for (i = 0, foundColor = FALSE;
              i < palette.size && !foundColor;
              ++i) {
@@ -1148,7 +1148,7 @@ determineOutputType(struct pngx *       const pngxP,
         /* The output is a old style pseudo-PNM transparency image */
         *depthP = 1;
         *formatP = maxval > 1 ? PGM_FORMAT : PBM_FORMAT;
-    } else {            
+    } else {
         /* The output is a normal Netpbm image */
         bool const outputIsColor =
             imageHasColor(pngxP) || !isGrayscale(bgColor);
@@ -1191,7 +1191,7 @@ getBackgroundColor(struct pngx *   const pngxP,
     if (requestedColor) {
         /* Background was specified from the command-line; we always
            use that.  I chose to do no gamma-correction in this case;
-           which is a bit arbitrary.  
+           which is a bit arbitrary.
         */
         pixel const backcolor = ppm_parsecolor(requestedColor, maxval);
 
@@ -1207,12 +1207,12 @@ getBackgroundColor(struct pngx *   const pngxP,
         switch (pngx_colorType(pngxP)) {
         case PNG_COLOR_TYPE_GRAY:
         case PNG_COLOR_TYPE_GRAY_ALPHA:
-            bgColorP->r = bgColorP->g = bgColorP->b = 
+            bgColorP->r = bgColorP->g = bgColorP->b =
                 gammaCorrect(background.gray, gamma, pngxP->maxval);
             break;
         case PNG_COLOR_TYPE_PALETTE: {
             struct pngx_plte const palette = pngx_plte(pngxP);
-            png_color const rawBgcolor = 
+            png_color const rawBgcolor =
                 palette.palette[background.index];
             *bgColorP = gammaCorrectColor(pngcolorFromByte(rawBgcolor),
                                           gamma, pngxP->maxval);
@@ -1221,13 +1221,13 @@ getBackgroundColor(struct pngx *   const pngxP,
         case PNG_COLOR_TYPE_RGB:
         case PNG_COLOR_TYPE_RGB_ALPHA: {
             png_color_16 const rawBgcolor = background;
-            
+
             *bgColorP = gammaCorrectColor(pngcolorFrom16(rawBgcolor),
                                           gamma, pngxP->maxval);
         }
         break;
         }
-    } else 
+    } else
         /* when no background given, we use white [from version 2.37] */
         bgColorP->r = bgColorP->g = bgColorP->b = maxval;
 }
@@ -1345,7 +1345,7 @@ makeTupleRow(const struct pam *  const pamP,
                      pngxP, paletteAlpha(pngxP, index, pngxP->maxval));
         }
         break;
-                
+
         case PNG_COLOR_TYPE_RGB: {
             pngcolor fgColor;
 
@@ -1403,7 +1403,7 @@ reportOutputFormat(const struct pam * const pamP) {
         assert(false); /* Every possible value handled above */
     }
 }
-    
+
 
 
 static void
@@ -1449,9 +1449,9 @@ writeNetpbm(struct pam *        const pamP,
 
 
 
-static void 
-convertpng(FILE *             const ifP, 
-           FILE *             const tfP, 
+static void
+convertpng(FILE *             const ifP,
+           FILE *             const tfP,
            struct CmdlineInfo const cmdline,
            int *              const errorLevelP) {
 
@@ -1487,7 +1487,7 @@ convertpng(FILE *             const ifP,
 
     getBackgroundColor(pngxP, cmdline.background, gamma, pngxP->maxval,
                        &bgColor);
-  
+
     pam.size        = sizeof(pam);
     pam.len         = PAM_STRUCT_SIZE(tuple_type);
     pam.file        = stdout;
@@ -1499,7 +1499,7 @@ convertpng(FILE *             const ifP,
     determineOutputType(pngxP, cmdline.alpha, bgColor, pngxP->maxval,
                         &pam.format, &pam.depth, pam.tuple_type);
 
-    rasterReaderP = cmdline.byrow ? 
+    rasterReaderP = cmdline.byrow ?
         reader_createRowByRow(pngxP, ifP) : reader_createAllAtOnce(pngxP, ifP);
 
     writeNetpbm(&pam, pngxP, rasterReaderP, bgColor,
@@ -1516,7 +1516,7 @@ convertpng(FILE *             const ifP,
 
 
 
-int 
+int
 main(int argc, const char *argv[]) {
 
     struct CmdlineInfo cmdline;
diff --git a/converter/other/pngx.c b/converter/other/pngx.c
index 185b4a27..4bb09421 100644
--- a/converter/other/pngx.c
+++ b/converter/other/pngx.c
@@ -1,3 +1,4 @@
+#include <stdbool.h>
 #include <assert.h>
 #include <png.h>
 #include "pm_c_util.h"
@@ -59,6 +60,7 @@ pngx_create(struct pngx ** const pngxPP,
     if (!pngxP)
         pm_error("Failed to allocate memory for PNG object");
     else {
+        pngxP->infoPrepared = false;
         pngxP->numPassesRequired = 1;
 
         switch(rw) {
@@ -347,7 +349,7 @@ void
 pngx_setChrm(struct pngx *      const pngxP,
              struct pngx_chroma const chroma) {
 
-    png_set_cHRM(pngxP->png_ptr, pngxP->info_ptr, 
+    png_set_cHRM(pngxP->png_ptr, pngxP->info_ptr,
                  chroma.wx, chroma.wy,
                  chroma.rx, chroma.ry,
                  chroma.gx, chroma.gy,
@@ -466,6 +468,37 @@ pngx_setIhdr(struct pngx * const pngxP,
 
 void
 pngx_setInterlaceHandling(struct pngx * const pngxP) {
+    /* The documentation is vague and contradictory on what this does, but
+       what it appears from reasoning and experimentation to do is the
+       following.
+
+       It applies to reading and writing by rows (png_write_row, png_read_row)
+       as opposed to whole image (png_write_image, png_read_image).  It has
+       no effect on whole image read and write.
+
+       This is not what makes an image interlaced or tells the decompressor
+       that it is interlaced.  All it does is control how you you read and
+       write the raster when the image is interlaced.  It has no effect if the
+       image is not interlaced.  (You make an image interlaced by setting the
+       IHDR; the decompressor finds out from the IHDR that it is interlaced).
+
+       In the write case, it controls whether you construct the subimages
+       yourself and feed them to libpng in sequence or you feed libpng the
+       entire image multiple times and libpng picks out the pixels appropriate
+       for each subimage in each pass.
+
+       In the read case, it controls whether you get the raw subimages and you
+       assemble them into the full image or you read the whole image multiple
+       times into the same buffer, with the pixels that belong to each
+       subimage being filled in on each pass.
+
+       Note that the only kind of interlacing that exists today is ADAM7 and
+       consequently, the number of passes is always 1 (for no interlacing) or
+       7 (for interlacing).
+    */
+    if (!pngxP->infoPrepared)
+        pm_error("pngx_setInterlaceHandling must not be called before "
+                 "pngx_writeInfo or pngx_readInfo");
 
     pngxP->numPassesRequired = png_set_interlace_handling(pngxP->png_ptr);
 }
@@ -475,6 +508,10 @@ pngx_setInterlaceHandling(struct pngx * const pngxP) {
 void
 pngx_setPacking(struct pngx * const pngxP) {
 
+    if (!pngxP->infoPrepared)
+        pm_error("pngx_setPacking must not be called before "
+                 "pngx_writeInfo or pngx_readInfo");
+
     png_set_packing(pngxP->png_ptr);
 }
 
@@ -484,7 +521,7 @@ void
 pngx_setPhys(struct pngx *    const pngxP,
              struct pngx_phys const phys) {
 
-    png_set_pHYs(pngxP->png_ptr, pngxP->info_ptr, 
+    png_set_pHYs(pngxP->png_ptr, pngxP->info_ptr,
                  phys.x, phys.y, phys.unit);
 }
 
@@ -516,7 +553,19 @@ pngx_setSbit(struct pngx * const pngxP,
 void
 pngx_setShift(struct pngx * const pngxP,
               png_color_8   const sigBitArg) {
-
+/*----------------------------------------------------------------------------
+   Tell the number of significant bits in the row buffers that will be given
+   to the compressor.  Those bits are the least significant of the 8 bits of
+   space in the row buffer for each sample.  For example, if red sample values
+   are in the range 0-7, only the lower 3 bits of the 8-bit byte for each
+   red sample will be used, so one would call this with sigBitArg.red == 3.
+
+   The name alludes to the fact that to normalize the sample to 8 bits, one
+   shifts it left, and this function tells how much shift has to happen.  In
+   the example above, each red sample has to be shifted left 5 bits (so that
+   the upper 3 bits are significant and the lower 5 bits are always zero) to
+   create an 8 bit sample out of the 3 bit samples.
+-----------------------------------------------------------------------------*/
     png_color_8 sigBit;
 
     sigBit = sigBitArg;
@@ -651,6 +700,8 @@ void
 pngx_readInfo(struct pngx * const pngxP) {
 
     png_read_info(pngxP->png_ptr, pngxP->info_ptr);
+
+    pngxP->infoPrepared = true;
 }
 
 
@@ -659,6 +710,8 @@ void
 pngx_writeInfo(struct pngx * const pngxP) {
 
     png_write_info(pngxP->png_ptr, pngxP->info_ptr);
+
+    pngxP->infoPrepared = true;
 }
 
 
@@ -688,7 +741,7 @@ pngx_readStart(struct pngx * const pngxP,
                FILE *        const ifP) {
 
     size_t sigByteCt;
-            
+
     verifyFileIsPng(ifP, &sigByteCt);
 
     /* Declare that we already read the signature bytes */
@@ -733,9 +786,18 @@ pngx_writeRow(struct pngx *    const pngxP,
 
 
 void
+pngx_writeImage(struct pngx * const pngxP,
+                png_byte **   const raster) {
+
+    png_write_image(pngxP->png_ptr, (png_byte **)raster);
+}
+
+
+
+void
 pngx_readEnd(struct pngx * const pngxP) {
 
-    /* Note that some of info_ptr is not defined until png_read_end() 
+    /* Note that some of info_ptr is not defined until png_read_end()
        completes.  That's because it comes from chunks that are at the
        end of the stream.  In particular, text and time chunks may
        be at the end.  Furthermore, they may be in both places, in
diff --git a/converter/other/pngx.h b/converter/other/pngx.h
index 81e0dc55..f4701bb2 100644
--- a/converter/other/pngx.h
+++ b/converter/other/pngx.h
@@ -1,7 +1,12 @@
 #ifndef PNGX_H_INCLUDED
 #define PNGX_H_INCLUDED
 
+#include <stdbool.h>
 #include <png.h>
+    /* This includes the Zlib interface header file zlib.h because libpng uses
+       libz and some of the Zlib interface, e.g. the Z_DEFLATED constant,
+       is part of the libpng interface.
+    */
 #include "pm_c_util.h"
 
 /* pngx is designed to be an extension of the PNG library to make using
@@ -49,10 +54,16 @@ struct pngx {
     pngx_rw      rw;
     png_uint_16  maxval;
     unsigned int numPassesRequired;
-        /* The number of times we have write the complete image to the
+        /* The number of times we have to write the complete image to the
            compressor.  This is more than one when the compressor is set
            up to do an interlaced format.
         */
+    bool         infoPrepared;
+        /* png_write_info or png_read_info has been called, so libpng is in a
+           state in which things such as png_set_interlace_handling will work.
+           These functions use information in *png_ptr that is set by
+           png_XXX_info.
+        */
 };
 
 void
@@ -266,6 +277,10 @@ pngx_writeRow(struct pngx *    const pngxP,
               const png_byte * const line);
 
 void
+pngx_writeImage(struct pngx * const pngxP,
+                png_byte **   const raster);
+
+void
 pngx_readEnd(struct pngx * const pngxP);
 
 void
diff --git a/converter/other/pnmtoddif.c b/converter/other/pnmtoddif.c
index ae8c8524..ac02e425 100644
--- a/converter/other/pnmtoddif.c
+++ b/converter/other/pnmtoddif.c
@@ -538,15 +538,12 @@ convertRaster(FILE *       const ifP,
               unsigned int const bytesPerLine) {
 
     unsigned char * data;
-    unsigned char * p;
 
     MALLOCARRAY(data, bytesPerLine);
 
     if (data == NULL)
         pm_error("Couldn't allocate %u-byte line buffer", bytesPerLine);
 
-    p = data;  /* initial value */
-
     switch (PNM_FORMAT_TYPE(format)) {
     case PBM_TYPE:
         convertPbmRaster(ifP, format, cols, rows, ofP, bytesPerLine, data);
diff --git a/converter/other/pnmtojpeg.c b/converter/other/pnmtojpeg.c
index 7e7272a0..44826245 100644
--- a/converter/other/pnmtojpeg.c
+++ b/converter/other/pnmtojpeg.c
@@ -16,6 +16,7 @@
 
 *****************************************************************************/
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/other/pnmtopalm/palm.h b/converter/other/pnmtopalm/palm.h
index 718a66cf..0edf9a28 100644
--- a/converter/other/pnmtopalm/palm.h
+++ b/converter/other/pnmtopalm/palm.h
@@ -28,9 +28,18 @@
 #define PALM_FORMAT_565LE       0x02    /* Palm says internal use only */
 #define PALM_FORMAT_INDEXEDLE   0x03    /* Palm says internal use only */
 
-typedef unsigned long Color_s;
+typedef unsigned long ColormapEntry;
+    /* A entry in a Colormap.  It is an encoding of 4 bytes as the integer
+       that those 4 bytes would represent in pure binary:
 
-typedef Color_s * Color;
+          MSB 0: the color index
+              1: red intensity
+              2: green intensity
+          LSB 3: blue intensity
+
+       The intensities are on a scale with a certain maxval (that must be
+       specified to interpret a ColormapEntry).
+    */
 
 typedef struct {
     unsigned int nentries;
@@ -39,24 +48,11 @@ typedef struct {
         /* number of colors actually in 'color_entries' -- entries are
            filled from 0 consecutively, one color per entry.
         */
-    Color_s * color_entries;  /* Array of colors */
-} Colormap_s;
-
-typedef Colormap_s * Colormap;
+    ColormapEntry * color_entries;  /* Array of colors */
+} Colormap;
 
 qsort_comparison_fn palmcolor_compare_indices;
 
 qsort_comparison_fn palmcolor_compare_colors;
 
-Colormap
-palmcolor_build_custom_8bit_colormap(unsigned int const rows,
-                                     unsigned int const cols,
-                                     pixel **     const pixels);
-
-Colormap
-palmcolor_build_default_8bit_colormap(void);
-
-Colormap
-palmcolor_read_colormap (FILE * const ifP);
-
 #endif
diff --git a/converter/other/pnmtopalm/palmcolormap.c b/converter/other/pnmtopalm/palmcolormap.c
index 0f47558c..1341ca2b 100644
--- a/converter/other/pnmtopalm/palmcolormap.c
+++ b/converter/other/pnmtopalm/palmcolormap.c
@@ -4,18 +4,46 @@
 #include "pm_c_util.h"
 #include "mallocvar.h"
 #include "pnm.h"
-
 #include "palm.h"
 
+#include "palmcolormap.h"
+
+
+
+static pixval
+scaleSample(pixval const arg,
+            pixval const oldMaxval,
+            pixval const newMaxval) {
+
+    return (arg * newMaxval + oldMaxval/2) / oldMaxval;
+}
+
+
+
+ColormapEntry
+palmcolor_mapEntryColorFmPixel(pixel  const color,
+                               pixval const maxval,
+                               pixval const newMaxval) {
+
+
+    return
+        0
+        | (scaleSample(PPM_GETR(color), maxval, newMaxval) << 16) 
+        | (scaleSample(PPM_GETG(color), maxval, newMaxval) <<  8)
+        | (scaleSample(PPM_GETB(color), maxval, newMaxval) <<  0);
+}
+
+
+
 int
 palmcolor_compare_indices(const void * const p1,
                           const void * const p2) {
 /*----------------------------------------------------------------------------
    This is a 'qsort' collation function.
 -----------------------------------------------------------------------------*/
-    if ((*((Color) p1) & 0xFF000000) < (*((Color) p2) & 0xFF000000))
+    if ((*((ColormapEntry *) p1) & 0xFF000000) < (*((ColormapEntry *) p2) & 0xFF000000))
         return -1;
-    else if ((*((Color) p1) & 0xFF000000) > (*((Color) p2) & 0xFF000000))
+    else if ((*((ColormapEntry *) p1) & 0xFF000000) > (*((ColormapEntry *) p2) & 0xFF000000))
         return 1;
     else
         return 0;
@@ -165,86 +193,88 @@ static int PalmPalette8bpp[256][3] =
 
 
 
-Colormap
+Colormap *
 palmcolor_build_default_8bit_colormap(void) {
 
     unsigned int i;
 
-    Colormap cm;
+    Colormap * cmP;
 
-    MALLOCVAR_NOFAIL(cm);
-    cm->nentries = 232;
-    MALLOCARRAY_NOFAIL(cm->color_entries, cm->nentries);
+    MALLOCVAR_NOFAIL(cmP);
+    cmP->nentries = 232;
+    MALLOCARRAY_NOFAIL(cmP->color_entries, cmP->nentries);
 
     /* Fill in the colors */
     for (i = 0; i < 231; ++i) {
-        cm->color_entries[i] = ((i << 24) |
+        cmP->color_entries[i] = ((i << 24) |
                                 (PalmPalette8bpp[i][0] << 16) |
                                 (PalmPalette8bpp[i][1] << 8) |
                                 (PalmPalette8bpp[i][2]));
     }
-    cm->color_entries[231] = 0xFF000000;
-    cm->ncolors = 232;
+    cmP->color_entries[231] = 0xFF000000;
+    cmP->ncolors = 232;
 
     /* now sort the table */
-    qsort (cm->color_entries, cm->ncolors, sizeof(Color_s), 
-           palmcolor_compare_colors);
-    return cm;
+    qsort(cmP->color_entries, cmP->ncolors, sizeof(ColormapEntry), 
+          palmcolor_compare_colors);
+    return cmP;
 }
 
 
 
-Colormap
-palmcolor_build_custom_8bit_colormap(unsigned int const rows,
+Colormap *
+palmcolor_build_custom_8bit_colormap(pixel **     const pixels,
+                                     unsigned int const rows,
                                      unsigned int const cols,
-                                     pixel **     const pixels) {
+                                     pixval       const maxval) {
+
     unsigned int row;
-    Colormap colormap;
+    Colormap * colormapP;
 
-    MALLOCVAR_NOFAIL(colormap);
-    colormap->nentries = 256;
-    MALLOCARRAY_NOFAIL(colormap->color_entries, colormap->nentries);
-    colormap->ncolors = 0;  /* initial value */
+    MALLOCVAR_NOFAIL(colormapP);
+    colormapP->nentries = 256;
+    MALLOCARRAY_NOFAIL(colormapP->color_entries, colormapP->nentries);
+    colormapP->ncolors = 0;  /* initial value */
     
     for (row = 0; row < rows; ++row) {
         unsigned int col;
         for (col = 0; col < cols; ++col) {
-            Color found;
-            Color_s temp;
-
-            temp = ((PPM_GETR(pixels[row][col]) << 16) |
-                    (PPM_GETG(pixels[row][col]) << 8) |
-                    PPM_GETB(pixels[row][col]));
-            found = (bsearch (&temp,
-                              colormap->color_entries, colormap->ncolors,
-                              sizeof(Color_s), palmcolor_compare_colors));
-            if (!found) {
-                if (colormap->ncolors >= colormap->nentries)
+            ColormapEntry * foundEntryP;
+            ColormapEntry const searchTarget =
+                palmcolor_mapEntryColorFmPixel(pixels[row][col], maxval, 255);
+
+            foundEntryP =
+                bsearch(&searchTarget,
+                        colormapP->color_entries, colormapP->ncolors,
+                        sizeof(ColormapEntry), palmcolor_compare_colors);
+            if (!foundEntryP) {
+                if (colormapP->ncolors >= colormapP->nentries)
                     pm_error("Too many colors for custom colormap "
-                             "(max 256).  "
+                             "(max %u).  "
                              "Try using pnmquant to reduce the number "
-                             "of colors.");
+                             "of colors.", colormapP->nentries);
                 else {
                     /* add the new color, and re-sort */
-                    temp |= ((colormap->ncolors) << 24);
-                    colormap->color_entries[colormap->ncolors] = temp;
-                    colormap->ncolors += 1;
-                    qsort(colormap->color_entries, colormap->ncolors, 
-                          sizeof(Color_s), palmcolor_compare_colors);
+                    unsigned int const colorIndex = colormapP->ncolors++;
+                    ColormapEntry const newEntry =
+                        searchTarget | (colorIndex << 24);
+                    colormapP->color_entries[colorIndex] = newEntry;
+                    qsort(colormapP->color_entries, colormapP->ncolors, 
+                          sizeof(ColormapEntry), palmcolor_compare_colors);
                 }
             }
         }
     }
-    return colormap;
+    return colormapP;
 }
 
 
 
-Colormap
+Colormap *
 palmcolor_read_colormap (FILE * const ifP) {
 
     unsigned short ncolors;
-    Colormap retval;
+    Colormap * retval;
     int rc;
     
     rc = pm_readbigshort(ifP, (short *) &ncolors);
@@ -252,13 +282,13 @@ palmcolor_read_colormap (FILE * const ifP) {
         retval = NULL;
     else {
         long colorentry;
-        Colormap colormap;
+        Colormap * colormapP;
         unsigned int i;
         bool error;
 
-        MALLOCVAR_NOFAIL(colormap);
-        colormap->nentries = ncolors;
-        MALLOCARRAY_NOFAIL(colormap->color_entries, colormap->nentries);
+        MALLOCVAR_NOFAIL(colormapP);
+        colormapP->nentries = ncolors;
+        MALLOCARRAY_NOFAIL(colormapP->color_entries, colormapP->nentries);
         
         for (i = 0, error = FALSE;  i < ncolors && !error;  ++i) {
             int rc;
@@ -266,15 +296,15 @@ palmcolor_read_colormap (FILE * const ifP) {
             if (rc != 0)
                 error = TRUE;
             else
-                colormap->color_entries[i] = (colorentry & 0xFFFFFFFF);
+                colormapP->color_entries[i] = (colorentry & 0xFFFFFFFF);
         }
         if (error) {
-            free (colormap->color_entries);
-            free (colormap);
+            free (colormapP->color_entries);
+            free (colormapP);
             retval = NULL;
         } else {
-            colormap->ncolors = ncolors;
-            retval = colormap;
+            colormapP->ncolors = ncolors;
+            retval = colormapP;
         }
     }
     return retval;
diff --git a/converter/other/pnmtopalm/palmcolormap.h b/converter/other/pnmtopalm/palmcolormap.h
new file mode 100644
index 00000000..cbdb2031
--- /dev/null
+++ b/converter/other/pnmtopalm/palmcolormap.h
@@ -0,0 +1,25 @@
+#ifndef PALMCOLORMAP_H_INCLUDED
+#define PALMCOLORMAP_H_INCLUDED
+
+#include <stdio.h>
+#include "ppm.h"
+#include "palm.h"
+
+ColormapEntry
+palmcolor_mapEntryColorFmPixel(pixel    const color,
+                               pixval const maxval,
+                               pixval const newMaxval);
+
+Colormap *
+palmcolor_build_custom_8bit_colormap(pixel **     const pixels,
+                                     unsigned int const rows,
+                                     unsigned int const cols,
+                                     pixval       const maxval);
+
+Colormap *
+palmcolor_build_default_8bit_colormap(void);
+
+Colormap *
+palmcolor_read_colormap (FILE * const ifP);
+
+#endif
diff --git a/converter/other/pnmtopalm/palmtopnm.c b/converter/other/pnmtopalm/palmtopnm.c
index 0f76207d..b3c0321a 100644
--- a/converter/other/pnmtopalm/palmtopnm.c
+++ b/converter/other/pnmtopalm/palmtopnm.c
@@ -21,17 +21,18 @@
 #include "mallocvar.h"
 
 #include "palm.h"
+#include "palmcolormap.h"
 
 
 
-enum palmCompressionType {
-    COMPRESSION_NONE, 
-    COMPRESSION_RLE, 
+enum PalmCompressionType {
+    COMPRESSION_NONE,
+    COMPRESSION_RLE,
     COMPRESSION_SCANLINE,
     COMPRESSION_PACKBITS
 };
 
-struct palmHeader {
+struct PalmHeader {
     unsigned short cols;
     unsigned short rows;
     unsigned short bytesPerRow;
@@ -46,7 +47,7 @@ struct palmHeader {
     unsigned int   pixelSize;
     unsigned char  version;
     unsigned int   transparentIndex;
-    enum palmCompressionType compressionType;
+    enum PalmCompressionType compressionType;
     /* version 3 encoding specific */
     unsigned char  size;
     unsigned char  pixelFormat;
@@ -56,7 +57,7 @@ struct palmHeader {
 
 
 
-struct directPixelFormat {
+struct DirectPixelFormat {
     unsigned int redbits;
     unsigned int greenbits;
     unsigned int bluebits;
@@ -64,15 +65,15 @@ struct directPixelFormat {
 
 
 
-struct directColorInfo {
-    struct directPixelFormat pixelFormat;
-    Color_s                  transparentColor;
+struct DirectColorInfo {
+    struct DirectPixelFormat pixelFormat;
+    ColormapEntry            transparentColor;
 };
 
 
 
 
-struct cmdlineInfo {
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
@@ -85,13 +86,13 @@ struct cmdlineInfo {
 
 
 static void
-parseCommandLine(int argc, char ** argv,
-                 struct cmdlineInfo *cmdlineP) {
+parseCommandLine(int argc, const char ** argv,
+                 struct CmdlineInfo *cmdlineP) {
 /*----------------------------------------------------------------------------
    Note that the file spec array we return is stored in the storage that
    was passed to us as the argv array.
 -----------------------------------------------------------------------------*/
-    optEntry *option_def = malloc( 100*sizeof( optEntry ) );
+    optEntry * option_def;
         /* Instructions to pm_optParseOptions3 on how to parse our options.
          */
     optStruct3 opt;
@@ -100,6 +101,8 @@ parseCommandLine(int argc, char ** argv,
 
     unsigned int option_def_index;
 
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
     option_def_index = 0;   /* incremented by OPTENTRY */
     OPTENT3(0, "verbose",     OPT_FLAG, NULL,
             &cmdlineP->verbose,  0);
@@ -107,23 +110,23 @@ parseCommandLine(int argc, char ** argv,
             &cmdlineP->showhist, 0);
     OPTENT3(0, "transparent",    OPT_FLAG, NULL,
             &cmdlineP->transparent, 0);
-    OPTENT3(0, "rendition",  OPT_UINT, &cmdlineP->rendition, 
+    OPTENT3(0, "rendition",  OPT_UINT, &cmdlineP->rendition,
             &renditionSpec, 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, 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 (renditionSpec) {
         if (cmdlineP->rendition < 1)
             pm_error("The -rendition value must be at least 1");
-    } else 
+    } else
         cmdlineP->rendition = 1;
-    
+
     if (cmdlineP->transparent && cmdlineP->showhist)
         pm_error("You can't specify -showhist with -transparent");
 
@@ -135,12 +138,13 @@ parseCommandLine(int argc, char ** argv,
             pm_error("Too many arguments (%d).  The only non-option "
                      "argument is the file name", argc-1);
     }
+    free(option_def);
 }
 
 
 
 static xelval *
-createGraymap(unsigned int const ncolors, 
+createGraymap(unsigned int const ncolors,
               xelval       const maxval) {
     int i;
     xelval *map;
@@ -154,8 +158,8 @@ createGraymap(unsigned int const ncolors,
 
 
 
-static void 
-skipbytes(FILE *       const ifP, 
+static void
+skipbytes(FILE *       const ifP,
           unsigned int const nbytes) {
 
     unsigned char buf[256];
@@ -176,15 +180,15 @@ skipbytes(FILE *       const ifP,
                pm_error("Error reading Palm file.  Short read.");
             n = 0;
         }
-    }    
+    }
 }
 
 
 
 static void
 interpretCompression(unsigned char              const compressionValue,
-                     enum palmCompressionType * const compressionTypeP) {
-    
+                     enum PalmCompressionType * const compressionTypeP) {
+
     switch (compressionValue) {
     case PALM_COMPRESSION_RLE:
         *compressionTypeP = COMPRESSION_RLE;
@@ -220,17 +224,18 @@ readRestOfHeaderVersion3(FILE *           const ifP,
                          short *          const nextDepthOffsetP) {
 
     unsigned char unused;
-    
+
     pm_readcharu(ifP, sizeP);
     /* should be 0x18, but I can't see why we should really care */
     if (*sizeP != 0x18)
-        pm_message("Strange value for Palm bitmap header size: %hu", *sizeP);
+        pm_message("Strange value for Palm bitmap header size: %u",
+                   (unsigned)*sizeP);
 
     pm_readcharu(ifP, pixelFormatP);
     if (*pixelFormatP != PALM_FORMAT_INDEXED &&
         *pixelFormatP != PALM_FORMAT_565)
         pm_error("Unrecognized pixelformat type: %u", *pixelFormatP);
-    
+
     pm_readcharu(ifP, &unused);
 
 
@@ -244,15 +249,15 @@ readRestOfHeaderVersion3(FILE *           const ifP,
         *densityP != PALM_DENSITY_TRIPLE &&
         *densityP != PALM_DENSITY_QUADRUPLE)
         pm_error("Invalid value for -density: %d.", *densityP);
- 
+
     pm_readbiglong(ifP, transparentValueP);
     if (pixelSize < 16)
         *transparentIndexP = *transparentValueP;
     else
         *transparentIndexP = 0;
- 
+
     pm_readbiglong(ifP, nextBitmapOffsetP);
-    
+
     /* version < 3 specific */
     *nextDepthOffsetP = 0;
 }
@@ -269,18 +274,18 @@ readRestOfHeaderOld(FILE *           const ifP,
                     long *           const transparentValueP,
                     long *           const nextBitmapOffsetP,
                     short *          const nextDepthOffsetP) {
-    
+
     short pad;
     unsigned char transparentIndex;
-    
+
     pm_readbigshort(ifP, nextDepthOffsetP);
     pm_readcharu(ifP, &transparentIndex);
     *transparentIndexP = transparentIndex;
- 
+
     pm_readcharu(ifP,compressionTypeP);
-    
+
     pm_readbigshort(ifP, &pad); /* reserved by Palm as of 8/9/00 */
-    
+
     /* version 3 specific */
     *sizeP = 0;
     *pixelFormatP = 0;
@@ -292,7 +297,7 @@ readRestOfHeaderOld(FILE *           const ifP,
 
 
 static void
-interpretHeader(struct palmHeader * const palmHeaderP,
+interpretHeader(struct PalmHeader * const palmHeaderP,
                 short               const cols,
                 short               const rows,
                 short               const bytesPerRow,
@@ -306,7 +311,7 @@ interpretHeader(struct palmHeader * const palmHeaderP,
                 long                const transparentValue,
                 unsigned int        const transparentIndex,
                 unsigned char       const compressionType) {
-    
+
     palmHeaderP->cols = cols;
     palmHeaderP->rows = rows;
     palmHeaderP->bytesPerRow = bytesPerRow;
@@ -329,11 +334,11 @@ interpretHeader(struct palmHeader * const palmHeaderP,
         */
         pm_error("PALM_DIRECT_COLOR_FLAG is set but pixelFormat is not"
                  "PALM_FORMAT_565.");
-        
-    palmHeaderP->directColor = ((flags & PALM_DIRECT_COLOR_FLAG) || 
+
+    palmHeaderP->directColor = ((flags & PALM_DIRECT_COLOR_FLAG) ||
                                 palmHeaderP->pixelFormat == PALM_FORMAT_565);
-    
-    if (flags & PALM_IS_COMPRESSED_FLAG) 
+
+    if (flags & PALM_IS_COMPRESSED_FLAG)
         interpretCompression(compressionType,
                              &palmHeaderP->compressionType);
     else
@@ -345,7 +350,7 @@ interpretHeader(struct palmHeader * const palmHeaderP,
 static void
 readHeader(FILE *              const ifP,
            unsigned int        const requestedRendition,
-           struct palmHeader * const palmHeaderP) {
+           struct PalmHeader * const palmHeaderP) {
 /*----------------------------------------------------------------------------
    Read the Palm Bitmap header from the file 'ifP'.  Read past all
    renditions up to 'requestedRendition' and read the header of that
@@ -358,7 +363,7 @@ readHeader(FILE *              const ifP,
     currentRendition = 1;
     while (!gotHeader) {
         short cols, rows, bytesPerRow, flags, nextDepthOffset, density;
-        unsigned char pixelSizeCode, version, compressionType, 
+        unsigned char pixelSizeCode, version, compressionType,
             size, pixelFormat;
         long transparentValue, nextBitmapOffset;
         unsigned int pixelSize, transparentIndex;
@@ -367,7 +372,7 @@ readHeader(FILE *              const ifP,
         pm_readbigshort(ifP, &rows);
         pm_readbigshort(ifP, &bytesPerRow);
         pm_readbigshort(ifP, &flags);
- 
+
         pm_readcharu(ifP, &pixelSizeCode);
         pixelSize = pixelSizeCode == 0 ? 1 : pixelSizeCode;
         if (pixelSizeCode != 0x00 &&
@@ -384,12 +389,12 @@ readHeader(FILE *              const ifP,
                      "bits per pixel.", bytesPerRow, cols, pixelSize);
 
         pm_readcharu(ifP, &version);
-        if (version > 3) 
+        if (version > 3)
             pm_error("Unknown encoding version type: %d", version);
         else if (version == 3)
             readRestOfHeaderVersion3(ifP, pixelSize,
                                      &size, &pixelFormat, &compressionType,
-                                     &density, &transparentIndex, 
+                                     &density, &transparentIndex,
                                      &transparentValue, &nextBitmapOffset,
                                      &nextDepthOffset);
         else
@@ -400,29 +405,29 @@ readHeader(FILE *              const ifP,
                                 &nextDepthOffset);
 
         if (currentRendition < requestedRendition) {
-             if (version < 3 && nextDepthOffset == 0 && pixelSizeCode != 0xFF) 
+             if (version < 3 && nextDepthOffset == 0 && pixelSizeCode != 0xFF)
                  pm_error("Not enough renditions in the input Palm Bitmap "
                           "to extract the %dth", requestedRendition);
-             if (version == 3 && nextBitmapOffset == 0) 
+             if (version == 3 && nextBitmapOffset == 0)
                  pm_error("Not enough renditions in the input Palm Bitmap "
                           "to extract the %dth", requestedRendition);
              /* nextDepthOffset is calculated in 4 byte words
-                from the beginning of this bitmap (so it equals its size) 
+                from the beginning of this bitmap (so it equals its size)
              */
              if (version < 3 && pixelSizeCode != 0xFF )
                  skipbytes(ifP, (nextDepthOffset*4)-16);
              else if (version == 3)
                  /* FIXME rewrite skipbytes to accept longs? */
-                 skipbytes(ifP, (short) nextBitmapOffset-24); 
+                 skipbytes(ifP, (short) nextBitmapOffset-24);
              if (pixelSizeCode != 0xFF)
                  ++currentRendition;
         } else if (pixelSizeCode != 0xFF) {
             gotHeader = TRUE;
-            
+
             interpretHeader(palmHeaderP,
                             cols, rows, bytesPerRow, flags, pixelSizeCode,
                             pixelSize, version, size, pixelFormat, density,
-                            transparentValue, transparentIndex, 
+                            transparentValue, transparentIndex,
                             compressionType);
         }
     }
@@ -441,8 +446,8 @@ yesno(bool const arg) {
 
 
 static void
-reportPalmHeader(struct palmHeader      const palmHeader,
-                 struct directColorInfo const directColorInfo) {
+reportPalmHeader(struct PalmHeader      const palmHeader,
+                 struct DirectColorInfo const directColorInfo) {
 
     const char *ctype;
 
@@ -462,9 +467,9 @@ reportPalmHeader(struct palmHeader      const palmHeader,
     }
     pm_message("Dimensions: %hu columns x %hu rows",
                palmHeader.cols, palmHeader.rows);
-    pm_message("Row layout: %hu bytes per row, %hu bits per pixel",
+    pm_message("Row layout: %hu bytes per row, %u bits per pixel",
                palmHeader.bytesPerRow, palmHeader.pixelSize);
-    pm_message("Pixel Size code: %hu", palmHeader.pixelSizeCode);
+    pm_message("Pixel Size code: %u", (unsigned)palmHeader.pixelSizeCode);
     pm_message("Flags: 0x%04hx", palmHeader.flags);
     pm_message("  Direct Color: %s", yesno(palmHeader.directColor));
     pm_message("  Colormap:     %s", yesno(palmHeader.hasColormap));
@@ -473,10 +478,10 @@ reportPalmHeader(struct palmHeader      const palmHeader,
     if (palmHeader.hasTransparency) {
         if (palmHeader.directColor) {
             /* Copied from doTransparent(...) */
-            Color_s const color = directColorInfo.transparentColor;
-            pm_message("Transparent value: #%02x%02x%02x", 
-                       (unsigned int)((color >> 16) & 0xFF), 
-                       (unsigned int)((color >>  8) & 0xFF), 
+            ColormapEntry const color = directColorInfo.transparentColor;
+            pm_message("Transparent value: #%02x%02x%02x",
+                       (unsigned int)((color >> 16) & 0xFF),
+                       (unsigned int)((color >>  8) & 0xFF),
                        (unsigned int)((color >>  0) & 0xFF));
         } else
             pm_message("Transparent index: %u", palmHeader.transparentIndex);
@@ -489,7 +494,7 @@ reportPalmHeader(struct palmHeader      const palmHeader,
 
 
 static void
-determineOutputFormat(struct palmHeader const palmHeader,
+determineOutputFormat(struct PalmHeader const palmHeader,
                       int *             const formatP,
                       xelval *          const maxvalP) {
 
@@ -515,14 +520,14 @@ determineOutputFormat(struct palmHeader const palmHeader,
 
 static void
 readRgbFormat(FILE *                     const ifP,
-              struct directPixelFormat * const pixelFormatP) {
+              struct DirectPixelFormat * const pixelFormatP) {
 
     unsigned char r, g, b;
 
     pm_readcharu(ifP, &r);
     pm_readcharu(ifP, &g);
     pm_readcharu(ifP, &b);
-    
+
     if (r != 5 || g != 6 || b != 5)
         pm_error("This image has a direct color pixel format of "
                  "%u red, %u green, %u blue bits.  This program "
@@ -537,8 +542,8 @@ readRgbFormat(FILE *                     const ifP,
 
 
 static void
-readDirectTransparentColor(FILE *    const ifP,
-                           Color_s * const colorP) {
+readDirectTransparentColor(FILE *          const ifP,
+                           ColormapEntry * const colorP) {
 
     unsigned char r, g, b;
 
@@ -553,8 +558,8 @@ readDirectTransparentColor(FILE *    const ifP,
 
 static void
 readDirectInfoType(FILE *                   const ifP,
-                   struct palmHeader        const palmHeader,
-                   struct directColorInfo * const directInfoTypeP) {
+                   struct PalmHeader        const palmHeader,
+                   struct DirectColorInfo * const directInfoTypeP) {
 /*----------------------------------------------------------------------------
    Read the Palm Bitmap Direct Info Type section, if any.
 
@@ -568,12 +573,12 @@ readDirectInfoType(FILE *                   const ifP,
     if ((palmHeader.directColor) && palmHeader.pixelSize != 16)
         pm_error("The image is of the direct color type, but has %u "
                  "bits per pixel.  The only kind of direct color images "
-                 "this program understands are 16 bit ones.", 
+                 "this program understands are 16 bit ones.",
                  palmHeader.pixelSize);
 
     if (palmHeader.version == 3) {
         /* All direct color info is in the header, because it'sversion
-           3 encoding.  No Direct Info Type section.  
+           3 encoding.  No Direct Info Type section.
         */
     } else {
         if (palmHeader.directColor) {
@@ -584,7 +589,7 @@ readDirectInfoType(FILE *                   const ifP,
             pm_readcharu(ifP, &padding);
             pm_readcharu(ifP, &padding);
 
-            readDirectTransparentColor(ifP, 
+            readDirectTransparentColor(ifP,
                                        &directInfoTypeP->transparentColor);
         } else {
             /* Not a direct color image; no Direct Info Type section. */
@@ -596,8 +601,8 @@ readDirectInfoType(FILE *                   const ifP,
 
 static void
 readColormap(FILE *            const ifP,
-             struct palmHeader const palmHeader,
-             Colormap *        const colormapP) {
+             struct PalmHeader const palmHeader,
+             Colormap **       const colormapPP) {
 /*----------------------------------------------------------------------------
    Read the colormap, if any from the Palm Bitmap.
 
@@ -605,18 +610,18 @@ readColormap(FILE *            const ifP,
    return an undefined value as *colormapP.
 -----------------------------------------------------------------------------*/
     if (palmHeader.hasColormap)
-        *colormapP = palmcolor_read_colormap(ifP);
+        *colormapPP = palmcolor_read_colormap(ifP);
 }
 
 
 
 static void
-getColorInfo(struct palmHeader        const palmHeader,
-             struct directColorInfo   const directInfoType,
-             Colormap                 const colormapFromImage,
-             Colormap *               const colormapP,
+getColorInfo(struct PalmHeader        const palmHeader,
+             struct DirectColorInfo   const directInfoType,
+             Colormap *               const colormapFromImageP,
+             Colormap **              const colormapPP,
              unsigned int *           const ncolorsP,
-             struct directColorInfo * const directColorInfoP) {
+             struct DirectColorInfo * const directColorInfoP) {
 /*----------------------------------------------------------------------------
    Gather color encoding information from the various sources.
 
@@ -626,14 +631,14 @@ getColorInfo(struct palmHeader        const palmHeader,
    If it's a version 3 direct color, the pixel format must be "565".
 -----------------------------------------------------------------------------*/
     if (palmHeader.version == 3 && palmHeader.directColor) {
-        *colormapP = NULL;
+        *colormapPP = NULL;
 
         assert(palmHeader.pixelFormat == PALM_FORMAT_565);
 
         directColorInfoP->pixelFormat.redbits   = 5;
         directColorInfoP->pixelFormat.greenbits = 6;
         directColorInfoP->pixelFormat.bluebits  = 5;
-        directColorInfoP->transparentColor = 
+        directColorInfoP->transparentColor =
             /* See convertRowToPnmDirect for this trick
 
                This will break once maxval isn't always set 255 for
@@ -646,18 +651,18 @@ getColorInfo(struct palmHeader        const palmHeader,
             ((((palmHeader.transparentValue >>  0) & 0x1F) * 255 / 0x1F)
              <<  0);
     } else if (palmHeader.directColor) {
-        *colormapP = NULL;
+        *colormapPP = NULL;
         *directColorInfoP = directInfoType;
     } else if (palmHeader.hasColormap)
-        *colormapP = colormapFromImage;
+        *colormapPP = colormapFromImageP;
     else if (palmHeader.pixelSize >= 8) {
-        Colormap colormap;
-        colormap = palmcolor_build_default_8bit_colormap();
-        qsort(colormap->color_entries, colormap->ncolors, 
-              sizeof(Color_s), palmcolor_compare_indices);
-        *colormapP = colormap;
+        Colormap * const colormapP =
+            palmcolor_build_default_8bit_colormap();
+        qsort(colormapP->color_entries, colormapP->ncolors,
+              sizeof(ColormapEntry), palmcolor_compare_indices);
+        *colormapPP = colormapP;
     } else
-        *colormapP = NULL;
+        *colormapPP = NULL;
 
     *ncolorsP = 1 << palmHeader.pixelSize;
 }
@@ -670,8 +675,8 @@ doTransparent(FILE *                 const ofP,
               bool                   const directColor,
               unsigned char          const transparentIndex,
               unsigned char          const pixelSize,
-              Colormap               const colormap,
-              struct directColorInfo const directColorInfo) {
+              Colormap *             const colormapP,
+              struct DirectColorInfo const directColorInfo) {
 /*----------------------------------------------------------------------------
    Generate a PNM comment on *ofP telling what color in the raster is
    supposed to be transparent.
@@ -681,31 +686,32 @@ doTransparent(FILE *                 const ofP,
    RGB_ALPHA and GRAYSCALE_ALPHA tuple types).
 -----------------------------------------------------------------------------*/
     if (hasTransparency) {
-        if (colormap) {
-            Color_s const color = transparentIndex << 24;
-            Color const actualColor = (bsearch(&color,
-                                               colormap->color_entries, 
-                                               colormap->ncolors,
-                                               sizeof(color), 
-                                               palmcolor_compare_indices));
-            if (!actualColor)
+        if (colormapP) {
+            ColormapEntry const searchTarget = transparentIndex << 24;
+            ColormapEntry * const foundEntryP =
+                bsearch(&searchTarget,
+                        colormapP->color_entries,
+                        colormapP->ncolors,
+                        sizeof(searchTarget),
+                        palmcolor_compare_indices);
+            if (!foundEntryP)
                 pm_error("Invalid input; transparent index %u "
                          "is not among the %u colors in the image's colormap",
-                         transparentIndex, colormap->ncolors);
+                         transparentIndex, colormapP->ncolors);
 
-            fprintf(ofP, "#%02x%02x%02x\n", 
-                   (unsigned int) ((*actualColor >> 16) & 0xFF),
-                   (unsigned int) ((*actualColor >>  8) & 0xFF), 
-                   (unsigned int) ((*actualColor >>  0) & 0xFF));
+            fprintf(ofP, "#%02x%02x%02x\n",
+                   (unsigned int) ((*foundEntryP >> 16) & 0xFF),
+                   (unsigned int) ((*foundEntryP >>  8) & 0xFF),
+                   (unsigned int) ((*foundEntryP >>  0) & 0xFF));
         } else if (directColor) {
-            Color_s const color = directColorInfo.transparentColor;
-            fprintf(ofP, "#%02x%02x%02x\n", 
-                   (unsigned int)((color >> 16) & 0xFF), 
-                   (unsigned int)((color >>  8) & 0xFF), 
+            ColormapEntry const color = directColorInfo.transparentColor;
+            fprintf(ofP, "#%02x%02x%02x\n",
+                   (unsigned int)((color >> 16) & 0xFF),
+                   (unsigned int)((color >>  8) & 0xFF),
                    (unsigned int)((color >>  0) & 0xFF));
         } else {
             unsigned int const maxval = pm_bitstomaxval(pixelSize);
-            unsigned int const grayval = 
+            unsigned int const grayval =
                 ((maxval - transparentIndex) * 256) / maxval;
             fprintf(ofP, "#%02x%02x%02x\n", grayval, grayval, grayval);
         }
@@ -723,10 +729,10 @@ createHistogram(unsigned int    const ncolors,
     MALLOCARRAY(seen, ncolors);
     if (!seen)
         pm_error("Can't allocate array for keeping track of "
-                 "how many pixels of each of %u colors are in the image.", 
+                 "how many pixels of each of %u colors are in the image.",
                  ncolors);
 
-    {    
+    {
         /* Initialize the counter for each color to zero */
         unsigned int i;
         for (i = 0; i < ncolors; ++i)
@@ -761,7 +767,7 @@ readScanlineRow(FILE *          const ifP,
 
         pm_readcharu(ifP, &diffmask);
         byteCount = MIN(bytesPerRow - j, 8);
-        
+
         for (k = 0; k < byteCount; ++k) {
             /* the first row cannot be compressed */
             if (firstRow || ((diffmask & (1 << (7 - k))) != 0)) {
@@ -800,7 +806,7 @@ readRleRow(FILE *          const ifP,
         memset(palmrow + j, inval, incount);
         j += incount;
     }
-} 
+}
 
 
 
@@ -810,7 +816,7 @@ readPackBitsRow16(FILE *          const ifP,
                   unsigned int    const bytesPerRow) {
 
     /*  From the Palm OS Programmer's API Reference:
-  
+
         Although the [...] spec is byte-oriented, the 16-bit algorithm is
         identical [to the 8-bit algorithm]: just substitute "word" for "byte".
     */
@@ -819,7 +825,7 @@ readPackBitsRow16(FILE *          const ifP,
     for (j = 0;  j < bytesPerRow; ) {
         char incount;
         pm_readchar(ifP, &incount);
-        if (incount < 0) { 
+        if (incount < 0) {
             /* How do we handle incount == -128 ? */
             unsigned int const runlength = (-incount + 1) * 2;
             unsigned int k;
@@ -841,13 +847,13 @@ readPackBitsRow16(FILE *          const ifP,
             }
             j += nonrunlength;
         }
-        if (j > bytesPerRow) 
+        if (j > bytesPerRow)
             pm_error("Bytes in PackBits compressed row exceed bytes per row.  "
                      "Bytes per row is %u.  "
                      "The bytes in this row were pushed up to %u bytes "
                      "(and then we gave up).", bytesPerRow, j);
     }
-} 
+}
 
 
 
@@ -861,7 +867,7 @@ readPackBitsRow(FILE *          const ifP,
     for (j = 0;  j < bytesPerRow; ) {
         char incount;
         pm_readchar(ifP, &incount);
-        if (incount < 0) { 
+        if (incount < 0) {
             /* How do we handle incount == -128 ? */
             unsigned int const runlength = -incount + 1;
             unsigned char inval;
@@ -879,13 +885,13 @@ readPackBitsRow(FILE *          const ifP,
             }
             j += nonrunlength;
         }
-        if (j > bytesPerRow) 
+        if (j > bytesPerRow)
             pm_error("Bytes in PackBits compressed row exceed bytes per row.  "
                      "Bytes per row is %u.  "
                      "The bytes in this row were pushed up to %u bytes "
                      "(and then we gave up).", bytesPerRow, j);
     }
-} 
+}
 
 
 
@@ -895,7 +901,7 @@ readUncompressedRow(FILE *          const ifP,
                     unsigned int    const bytesPerRow) {
 
     int bytesRead;
-    
+
     bytesRead = fread(palmrow, 1, bytesPerRow, ifP);
     if (bytesRead != bytesPerRow)
         pm_error("Error reading Palm file.  Short read.");
@@ -907,7 +913,7 @@ static void
 readDecompressedRow(FILE *                   const ifP,
                     unsigned char *          const palmrow,
                     unsigned char *          const lastrow,
-                    enum palmCompressionType const compressionType,
+                    enum PalmCompressionType const compressionType,
                     unsigned int             const bytesPerRow,
                     unsigned int             const pixelSize,
                     bool                     const firstRow) {
@@ -968,27 +974,27 @@ convertRowToPnmDirect(const unsigned char * const palmrow,
        blue=0x1F.  How do we promote those colors?  Simple
        shift would give us R=248,G=252,B=248; which is
        slightly green.  Hardly seems right.
-       
+
        So I've perverted the math a bit.  Each color value is
        multiplied by 255, then divided by either 31 (red or
        blue) or 63 (green).  That's the right way to do it
-       anyway.  
+       anyway.
     */
 
     const unsigned char *inbyte;
     unsigned int j;
-    
+
     for (inbyte = palmrow, j = 0;  j < cols;  ++j) {
         unsigned int inval;
         inval = *inbyte++ << 8;
         inval |= *inbyte++;
-        
+
         if (seen)
             ++seen[inval];
-        
-        PPM_ASSIGN(xelrow[j], 
-                   (((inval >> 11) & 0x1F) * maxval) / 0x1F, 
-                   (((inval >>  5) & 0x3F) * maxval) / 0x3F, 
+
+        PPM_ASSIGN(xelrow[j],
+                   (((inval >> 11) & 0x1F) * maxval) / 0x1F,
+                   (((inval >>  5) & 0x3F) * maxval) / 0x3F,
                    (((inval >>  0) & 0x1F) * maxval) / 0x1F
             );
     }
@@ -1000,46 +1006,50 @@ static void
 convertRowToPnmNotDirect(const unsigned char * const palmrow,
                          xel *                 const xelrow,
                          unsigned int          const cols,
-                         Colormap              const colormap,
+                         Colormap *            const colormapP,
                          xelval *              const graymap,
                          unsigned int *        const seen,
                          unsigned int          const pixelSize) {
 
     unsigned int const mask = (1 << pixelSize) - 1;
 
-    const unsigned char *inbyte;
+    const unsigned char * inbyteP;
     unsigned int inbit;
     unsigned int j;
-    
+
+    assert(pixelSize <= 8);
+
     inbit = 8 - pixelSize;
-    inbyte = palmrow;
+    inbyteP = &palmrow[0];
     for (j = 0; j < cols; ++j) {
-        short const color = ((*inbyte) & (mask << inbit)) >> inbit;
+        short const color = (*inbyteP & (mask << inbit)) >> inbit;
         if (seen)
             ++seen[color];
-        
-        if (colormap) {
-            Color_s const color2      = color << 24;
-            Color   const actualColor = (bsearch (&color2,
-                                                  colormap->color_entries, 
-                                                  colormap->ncolors,
-                                                  sizeof(color2), 
-                                                  palmcolor_compare_indices));
-            if (!actualColor)
+
+        if (colormapP) {
+            ColormapEntry const searchTarget = color << 24;
+            ColormapEntry * const foundEntryP =
+                bsearch(&searchTarget,
+                        colormapP->color_entries,
+                        colormapP->ncolors,
+                        sizeof(searchTarget),
+                        palmcolor_compare_indices);
+
+            if (!foundEntryP)
                 pm_error("Invalid input.  A color index in column %u "
                          "is %u, which is not among the %u colors "
                          "in the colormap",
-                         j, color, colormap->ncolors);
+                         j, color, colormapP->ncolors);
 
-            PPM_ASSIGN(xelrow[j], 
-                       (*actualColor >> 16) & 0xFF, 
-                       (*actualColor >>  8) & 0xFF, 
-                       (*actualColor >>  0) & 0xFF);
+            PPM_ASSIGN(xelrow[j],
+                       (*foundEntryP >> 16) & 0xFF,
+                       (*foundEntryP >>  8) & 0xFF,
+                       (*foundEntryP >>  0) & 0xFF);
         } else
             PNM_ASSIGN1(xelrow[j], graymap[color]);
-        
+
         if (!inbit) {
-            ++inbyte;
+            ++inbyteP;
             inbit = 8 - pixelSize;
         } else
             inbit -= pixelSize;
@@ -1050,9 +1060,9 @@ convertRowToPnmNotDirect(const unsigned char * const palmrow,
 
 static void
 writePnm(FILE *            const ofP,
-         struct palmHeader const palmHeader,
+         struct PalmHeader const palmHeader,
          FILE *            const ifP,
-         Colormap          const colormap,
+         Colormap *        const colormapP,
          xelval *          const graymap,
          unsigned int      const nColors,
          int               const format,
@@ -1067,22 +1077,22 @@ writePnm(FILE *            const ofP,
     xel *           xelrow;
     unsigned int *  seen;
     unsigned int    row;
-    
+
     pnm_writepnminit(ofP, cols, rows, maxval, format, 0);
     xelrow = pnm_allocrow(cols);
 
     /* Read the picture data, one row at a time */
     MALLOCARRAY_NOFAIL(palmrow, palmHeader.bytesPerRow);
-    MALLOCARRAY_NOFAIL(lastrow, palmHeader.bytesPerRow); 
-    
+    MALLOCARRAY_NOFAIL(lastrow, palmHeader.bytesPerRow);
+
     if (seenP) {
         createHistogram(nColors, &seen);
         *seenP = seen;
     } else
         seen = NULL;
 
-    /* We should actually use compressedDataSizeNN for checking the sanity 
-       of the data we're reading ... 
+    /* We should actually use compressedDataSizeNN for checking the sanity
+       of the data we're reading ...
     */
     if (palmHeader.compressionType != COMPRESSION_NONE) {
         if (palmHeader.version < 3) {
@@ -1095,8 +1105,8 @@ writePnm(FILE *            const ofP,
     }
 
     for (row = 0; row < rows; ++row) {
-        readDecompressedRow(ifP, palmrow, lastrow, 
-                            palmHeader.compressionType, 
+        readDecompressedRow(ifP, palmrow, lastrow,
+                            palmHeader.compressionType,
                             palmHeader.bytesPerRow,
                             palmHeader.pixelSize,
                             row == 0);
@@ -1105,7 +1115,7 @@ writePnm(FILE *            const ofP,
             assert(palmHeader.pixelSize == 16);
             convertRowToPnmDirect(palmrow, xelrow, cols, maxval, seen);
         } else
-            convertRowToPnmNotDirect(palmrow, xelrow, cols, colormap, graymap,
+            convertRowToPnmNotDirect(palmrow, xelrow, cols, colormapP, graymap,
                                      seen, palmHeader.pixelSize);
 
         pnm_writepnmrow(ofP, xelrow, cols, maxval, format, 0);
@@ -1119,28 +1129,29 @@ writePnm(FILE *            const ofP,
 
 static void
 showHistogram(unsigned int * const seen,
-              Colormap       const colormap,
+              Colormap *     const colormapP,
               const xelval * const graymap,
               unsigned int   const ncolors) {
 
     unsigned int colorIndex;
-    
+
     for (colorIndex = 0;  colorIndex < ncolors; ++colorIndex) {
-        if (!colormap)
-            pm_message("%.3d -> %.3d:  %d", 
+        if (!colormapP)
+            pm_message("%.3d -> %.3d:  %d",
                        colorIndex, graymap[colorIndex], seen[colorIndex]);
         else {
-            Color_s const color = colorIndex << 24;
-            Color const actualColor = (bsearch(&color,
-                                               colormap->color_entries, 
-                                               colormap->ncolors,
-                                               sizeof(color), 
-                                               palmcolor_compare_indices));
-            if (actualColor)
+            ColormapEntry const searchTarget = colorIndex << 24;
+            ColormapEntry * const foundEntryP =
+                bsearch(&searchTarget,
+                        colormapP->color_entries,
+                        colormapP->ncolors,
+                        sizeof(searchTarget),
+                        palmcolor_compare_indices);
+            if (foundEntryP)
                 pm_message("%.3d -> %ld,%ld,%ld:  %d", colorIndex,
-                           (*actualColor >> 16) & 0xFF,
-                           (*actualColor >> 8) & 0xFF,
-                           (*actualColor & 0xFF), seen[colorIndex]);
+                           (*foundEntryP >> 16) & 0xFF,
+                           (*foundEntryP >> 8) & 0xFF,
+                           (*foundEntryP & 0xFF), seen[colorIndex]);
         }
     }
 }
@@ -1148,22 +1159,21 @@ showHistogram(unsigned int * const seen,
 
 
 int
-main(int argc, char **argv) {
+main(int argc, const char **argv) {
 
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
 
-    FILE* ifP;
-    struct palmHeader palmHeader;
-    struct directColorInfo directInfoType;
-    Colormap colormapFromImage;
-    Colormap colormap;
-    struct directColorInfo directColorInfo;
+    FILE * ifP;
+    struct PalmHeader palmHeader;
+    struct DirectColorInfo directInfoType;
+    Colormap * colormapFromImageP;
+    Colormap * colormapP;
+    struct DirectColorInfo directColorInfo;
     int format;
     xelval maxval;
     unsigned int nColors;
 
-    /* Parse default params */
-    pnm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
@@ -1172,22 +1182,22 @@ main(int argc, char **argv) {
     readHeader(ifP, cmdline.rendition, &palmHeader);
 
     readDirectInfoType(ifP, palmHeader, &directInfoType);
-   
-    readColormap(ifP, palmHeader, &colormapFromImage);
-    
+
+    readColormap(ifP, palmHeader, &colormapFromImageP);
+
     determineOutputFormat(palmHeader, &format, &maxval);
-    
-    getColorInfo(palmHeader, directInfoType, colormapFromImage,
-                 &colormap, &nColors, &directColorInfo);
+
+    getColorInfo(palmHeader, directInfoType, colormapFromImageP,
+                 &colormapP, &nColors, &directColorInfo);
 
     if (cmdline.verbose)
         reportPalmHeader(palmHeader, directColorInfo);
 
     if (cmdline.transparent)
-        doTransparent(stdout, 
+        doTransparent(stdout,
                       palmHeader.hasTransparency, palmHeader.directColor,
-                      palmHeader.transparentIndex, 
-                      palmHeader.pixelSize, colormap, directColorInfo);
+                      palmHeader.transparentIndex,
+                      palmHeader.pixelSize, colormapP, directColorInfo);
     else {
         unsigned int * seen;
         xelval * graymap;
@@ -1195,12 +1205,12 @@ main(int argc, char **argv) {
         graymap = createGraymap(nColors, maxval);
 
         writePnm(stdout,
-                 palmHeader, ifP, colormap, graymap, nColors, format, maxval, 
+                 palmHeader, ifP, colormapP, graymap, nColors, format, maxval,
                  cmdline.showhist ? &seen : NULL);
-        
+
         if (cmdline.showhist)
-            showHistogram(seen, colormap, graymap, nColors);
-        
+            showHistogram(seen, colormapP, graymap, nColors);
+
         free(graymap);
     }
     pm_close(ifP);
diff --git a/converter/other/pnmtopalm/pnmtopalm.c b/converter/other/pnmtopalm/pnmtopalm.c
index a7d1fd46..25c8af2e 100644
--- a/converter/other/pnmtopalm/pnmtopalm.c
+++ b/converter/other/pnmtopalm/pnmtopalm.c
@@ -29,33 +29,37 @@
 
 #include "pm_c_util.h"
 #include "pnm.h"
-#include "palm.h"
 #include "shhopt.h"
 #include "mallocvar.h"
 #include "runlength.h"
 
-enum compressionType {COMP_NONE, COMP_SCANLINE, COMP_RLE, COMP_PACKBITS};
+#include "palm.h"
+#include "palmcolormap.h"
+
+enum CompressionType {COMP_NONE, COMP_SCANLINE, COMP_RLE, COMP_PACKBITS};
 
-struct cmdline_info {
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
     const char * inputFilespec;  /* Filespecs of input files */
-    const char * transparent;    /* -transparent value.  Null if unspec */
-    unsigned int depth;         /* -depth value.  0 if unspec */
-    unsigned int maxdepth;      /* -maxdepth value.  0 if unspec */
-    enum compressionType compression;
+    const char * transparent;
+    unsigned int depthSpec;
+    unsigned int depth;
+    unsigned int maxdepthSpec;
+    unsigned int maxdepth;
+    enum CompressionType compression;
     unsigned int verbose;
     unsigned int colormap;
-    unsigned int offset;        /* -offset specified */
-    unsigned int density;       /* screen density */
+    unsigned int offset;
+    unsigned int density;
     unsigned int withdummy;
 };
 
 
 
 static void
-parseCommandLine(int argc, char ** argv, struct cmdline_info *cmdlineP) {
+parseCommandLine(int argc, const char ** argv, struct CmdlineInfo *cmdlineP) {
 /*----------------------------------------------------------------------------
    Note that the file spec array we return is stored in the storage that
    was passed to us as the argv array.
@@ -64,7 +68,7 @@ parseCommandLine(int argc, char ** argv, struct cmdline_info *cmdlineP) {
     optEntry *option_def;
     unsigned int option_def_index;
 
-    unsigned int transSpec, depthSpec, maxdepthSpec, densitySpec;
+    unsigned int transSpec, densitySpec;
     unsigned int scanline_compression, rle_compression, packbits_compression;
 
     MALLOCARRAY_NOFAIL(option_def, 100);
@@ -73,9 +77,9 @@ parseCommandLine(int argc, char ** argv, struct cmdline_info *cmdlineP) {
     OPTENT3(0, "transparent",      OPT_STRING, 
             &cmdlineP->transparent, &transSpec, 0);
     OPTENT3(0, "depth",            OPT_UINT, 
-            &cmdlineP->depth,       &depthSpec, 0);
+            &cmdlineP->depth,       &cmdlineP->depthSpec, 0);
     OPTENT3(0, "maxdepth",         OPT_UINT, 
-            &cmdlineP->maxdepth,    &maxdepthSpec, 0);
+            &cmdlineP->maxdepth,    &cmdlineP->maxdepthSpec, 0);
     OPTENT3(0, "scanline_compression", OPT_FLAG, 
             NULL,                   &scanline_compression, 0);
     OPTENT3(0, "rle_compression",  OPT_FLAG, 
@@ -97,28 +101,26 @@ parseCommandLine(int argc, char ** argv, struct cmdline_info *cmdlineP) {
     opt.short_allowed = FALSE; /* We have some 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 *cmdline_p and others. */
 
-    if (depthSpec) {
+    if (cmdlineP->depthSpec) {
         if (cmdlineP->depth != 1 && cmdlineP->depth != 2 
             && cmdlineP->depth != 4 && cmdlineP->depth != 8
             && cmdlineP->depth != 16)
             pm_error("invalid value for -depth: %u.  Valid values are "
                      "1, 2, 4, 8, and 16", cmdlineP->depth);
-    } else
-        cmdlineP->depth = 0;
+    }
 
-    if (maxdepthSpec) {
+    if (cmdlineP->maxdepthSpec) {
         if (cmdlineP->maxdepth != 1 && cmdlineP->maxdepth != 2 
             && cmdlineP->maxdepth != 4 && cmdlineP->maxdepth != 8
             && cmdlineP->maxdepth != 16)
             pm_error("invalid value for -maxdepth: %u.  Valid values are "
                      "1, 2, 4, 8, and 16", cmdlineP->maxdepth);
-    } else
-        cmdlineP->maxdepth = 0;
+    }
 
-    if (depthSpec && maxdepthSpec && 
+    if (cmdlineP->depthSpec && cmdlineP->maxdepthSpec && 
         cmdlineP->depth > cmdlineP->maxdepth)
         pm_error("-depth value (%u) is greater than -maxdepth (%u) value.",
                  cmdlineP->depth, cmdlineP->maxdepth);
@@ -132,8 +134,8 @@ parseCommandLine(int argc, char ** argv, struct cmdline_info *cmdlineP) {
             cmdlineP->density != PALM_DENSITY_DOUBLE &&
             cmdlineP->density != PALM_DENSITY_TRIPLE &&
             cmdlineP->density != PALM_DENSITY_QUADRUPLE)
-            pm_error("Invalid value for -density: %d.  Valid values are "
-                     "%d, %d, %d, %d and %d.", cmdlineP->density, 
+            pm_error("Invalid value for -density: %u.  Valid values are "
+                     "%u, %u, %u, %u and %u.", cmdlineP->density, 
                      PALM_DENSITY_LOW, PALM_DENSITY_ONEANDAHALF,
                      PALM_DENSITY_DOUBLE, PALM_DENSITY_TRIPLE,
                      PALM_DENSITY_QUADRUPLE);
@@ -164,7 +166,7 @@ parseCommandLine(int argc, char ** argv, struct cmdline_info *cmdlineP) {
         
     if (argc-1 > 1)
         pm_error("This program takes at most 1 argument: the file name.  "
-                 "You specified %d", argc-1);
+                 "You specified %u", argc-1);
     else if (argc-1 > 0) 
         cmdlineP->inputFilespec = argv[1];
     else
@@ -173,108 +175,241 @@ parseCommandLine(int argc, char ** argv, struct cmdline_info *cmdlineP) {
 
 
 
+static xelval
+scaleSample(pixval const arg,
+            pixval const oldMaxval,
+            pixval const newMaxval) {
+
+    return (arg * newMaxval + oldMaxval/2) / oldMaxval;
+}
+
+
+
+static void
+determinePalmFormatPgm(xelval               const maxval, 
+                       bool                 const bppSpecified,
+                       unsigned int         const bpp,
+                       bool                 const maxBppSpecified,
+                       unsigned int         const maxBpp, 
+                       bool                 const wantCustomColormap,
+                       enum CompressionType const compression,
+                       bool                 const verbose,
+                       unsigned int *       const bppP) {
+        
+    /* We can usually handle this one, but may not have enough pixels.  So
+       check.
+    */
+
+    if (wantCustomColormap)
+        pm_error("You specified -colormap with a black and white input"
+                 "image.  -colormap is valid only with color.");
+    if (bppSpecified)
+        *bppP = bpp;
+    else if (maxBppSpecified && (maxval >= (1 << maxBpp)))
+        *bppP = maxBpp;
+    else if (compression != COMP_NONE && maxval > 255)
+        *bppP = 8;
+    else if (maxval > 16)
+        *bppP = 4;
+    else {
+        /* scale to minimum number of bpp needed */
+        unsigned int bpp;
+        for (bpp = 1;  (1 << bpp) < maxval;  bpp *= 2)
+            ;
+        *bppP = bpp;
+    }
+    if (verbose)
+        pm_message("output is grayscale %u bits-per-pixel", *bppP);
+}
+
+
+
+static void
+validateImageAgainstStandardColormap(const Colormap * const colormapP,
+                                     xel **           const xels,
+                                     unsigned int     const cols,
+                                     unsigned int     const rows,
+                                     xelval           const maxval) {
+/*----------------------------------------------------------------------------
+   Abort program if the image xels[][] (which is 'cols' x 'rows') contains a
+   color not in the colormap *colormapP, giving an error message assuming the
+   user chose the standard Palm colormap.
+-----------------------------------------------------------------------------*/
+    unsigned int row;
+
+    for (row = 0; row < rows; ++row) {
+        unsigned int col;
+
+        for (col = 0; col < cols; ++col) {
+            ColormapEntry const searchTarget = 
+                palmcolor_mapEntryColorFmPixel(xels[row][col], maxval, 255);
+
+            ColormapEntry * const foundEntryP = 
+                (bsearch(&searchTarget,
+                         colormapP->color_entries, colormapP->ncolors,
+                         sizeof(ColormapEntry), palmcolor_compare_colors));
+            if (!foundEntryP)
+                pm_error(
+                    "A color in the input image is not in the standard Palm "
+                    "8-bit color palette.  Either adjust the colors in the "
+                    "input with 'pnmremap' and the 'palmcolor8.map' file "
+                    "(see manual) or specify -colormap or -depth=16");
+        }
+    }
+}
+
+
+
+static void
+determinePalmFormatPpm(unsigned int         const cols, 
+                       unsigned int         const rows, 
+                       xelval               const maxval, 
+                       xel **               const xels,
+                       bool                 const bppSpecified,
+                       unsigned int         const bpp,
+                       bool                 const maxBppSpecified,
+                       unsigned int         const maxBpp, 
+                       bool                 const wantCustomColormap,
+                       enum CompressionType const compression,
+                       bool                 const verbose,
+                       unsigned int *       const bppP, 
+                       bool *               const directColorP, 
+                       Colormap **          const colormapPP) {
+            
+    /* We don't attempt to identify PPM files that are actually
+       monochrome.  So there are two options here: either 8-bit with a
+       colormap, either the standard one or a custom one, or 16-bit direct
+       color.  In the colormap case, if 'wantCustomColormap' is true (not
+       recommended by Palm) we will put in our own colormap that has the
+       colors of the input image; otherwise we will select the default
+       Palm colormap and will fail if the input image has any colors that
+       are not in that map (user should use Pnmremap and the
+       palmcolor8.map file that comes with Netpbm to avoid this).  We try
+       for colormapped first, since it works on more PalmOS devices.
+    */
+    if ((bppSpecified && bpp == 16) || 
+        (!bppSpecified && maxBppSpecified && maxBpp == 16)) {
+        /* we do the 16-bit direct color */
+        *directColorP = TRUE;
+        *colormapPP = NULL;
+        *bppP = 16;
+    } else if (!wantCustomColormap) {
+        /* colormapped with the standard colormap */
+        Colormap * colormapP;
+            
+        if ((bppSpecified && bpp != 8) || (maxBppSpecified && maxBpp < 8))
+            pm_error("Must use depth of 8 for color Palm Bitmap without "
+                     "custom color table.");
+        colormapP = palmcolor_build_default_8bit_colormap();
+        validateImageAgainstStandardColormap(colormapP,
+                                             xels, cols, rows, maxval);
+
+        *colormapPP = colormapP;
+        *bppP = 8;
+        *directColorP = FALSE;
+        if (verbose)
+            pm_message("Output is color with default colormap at 8 bpp");
+    } else {
+        /* colormapped with a custom colormap */
+        *colormapPP = 
+            palmcolor_build_custom_8bit_colormap(xels, rows, cols, maxval);
+        for (*bppP = 1; (1 << *bppP) < (*colormapPP)->ncolors; *bppP *= 2);
+        if (bppSpecified) {
+            if (bpp >= *bppP)
+                *bppP = bpp;
+            else
+                pm_error("Too many colors for specified depth.  "
+                         "Specified depth is %u bits; would need %u to "
+                         "represent the %u colors in the image.  "
+                         "Use pnmquant to reduce.",
+                         maxBpp, *bppP, (*colormapPP)->ncolors);
+        } else if (maxBppSpecified && maxBpp < *bppP) {
+            pm_error("Too many colors for specified max depth.  "
+                     "Specified maximum is %u bits; would need %u to "
+                     "represent the %u colors in the image.  "
+                     "Use pnmquant to reduce.",
+                     maxBpp, *bppP, (*colormapPP)->ncolors);
+        } else if (compression != COMP_NONE && *bppP > 8) {
+            pm_error("Too many colors for a compressed image.  "
+                     "Maximum is 256; the image has %u",
+                     (*colormapPP)->ncolors);
+        }
+        *directColorP = FALSE;
+        if (verbose)
+            pm_message("Output is color with custom colormap "
+                       "with %u colors at %u bpp", 
+                       (*colormapPP)->ncolors, *bppP);
+    }
+}
+
+
+
 static void
-determinePalmFormat(unsigned int   const cols, 
-                    unsigned int   const rows, 
-                    xelval         const maxval, 
-                    int            const format, 
-                    xel **         const xels,
-                    unsigned int   const specified_bpp,
-                    unsigned int   const max_bpp, 
-                    bool           const custom_colormap,
-                    bool           const verbose,
-                    unsigned int * const bppP, 
-                    bool *         const directColorP, 
-                    Colormap *     const colormapP) {
+determinePalmFormat(unsigned int         const cols, 
+                    unsigned int         const rows, 
+                    xelval               const maxval, 
+                    int                  const format, 
+                    xel **               const xels,
+                    bool                 const bppSpecified,
+                    unsigned int         const bpp,
+                    bool                 const maxBppSpecified,
+                    unsigned int         const maxBpp, 
+                    bool                 const wantCustomColormap,
+                    enum CompressionType const compression,
+                    bool                 const verbose,
+                    unsigned int *       const bppP, 
+                    bool *               const directColorP, 
+                    Colormap **          const colormapPP) {
+/*----------------------------------------------------------------------------
+   Determine what kind of Palm output file to make.
 
+   Also compute the colormap, if there is to be one.  This could be either one
+   we make up, that needs to go into the image, or a standard one.
+-----------------------------------------------------------------------------*/
+    if (compression != COMP_NONE) {
+        if (bppSpecified && bpp > 8)
+            pm_error("You requested %u bits per pixel and compression.  "
+                     "This program does not know how to generate a "
+                     "compressed image with more than 8 bits per pixel",
+                     bpp);
+        if (maxBppSpecified && maxBpp > 8)
+            pm_error("You requested %u max bits per pixel and compression.  "
+                     "This program does not know how to generate a "
+                     "compressed image with more than 8 bits per pixel",
+                     maxBpp);
+    }
     if (PNM_FORMAT_TYPE(format) == PBM_TYPE) {
-        if (custom_colormap)
+        if (wantCustomColormap)
             pm_error("You specified -colormap with a black and white input "
                      "image.  -colormap is valid only with color.");
-        if (specified_bpp)
-            *bppP = specified_bpp;
+        if (bppSpecified)
+            *bppP = bpp;
         else
             *bppP = 1;    /* no point in wasting bits */
         *directColorP = FALSE;
-        *colormapP = NULL;
+        *colormapPP = NULL;
         if (verbose)
             pm_message("output is black and white");
     } else if (PNM_FORMAT_TYPE(format) == PGM_TYPE) {
-        /* we can usually handle this one, but may not have enough
-           pixels.  So check... */
-        if (custom_colormap)
-            pm_error("You specified -colormap with a black and white input"
-                     "image.  -colormap is valid only with color.");
-        if (specified_bpp)
-            *bppP = specified_bpp;
-        else if (max_bpp && (maxval >= (1 << max_bpp)))
-            *bppP = max_bpp;
-        else if (maxval > 16)
-            *bppP = 4;
-        else {
-            /* scale to minimum number of bpp needed */
-            for (*bppP = 1;  (1 << *bppP) < maxval;  *bppP *= 2)
-                ;
-        }
-        if (*bppP > 4)
-            *bppP = 4;
-        if (verbose)
-            pm_message("output is grayscale %d bits-per-pixel", *bppP);
+        determinePalmFormatPgm(maxval,
+                               bppSpecified, bpp, maxBppSpecified, maxBpp,
+                               wantCustomColormap, compression,
+                               verbose,
+                               bppP);
+
         *directColorP = FALSE;
-        *colormapP = NULL;
+        *colormapPP = NULL;
     } else if (PNM_FORMAT_TYPE(format) == PPM_TYPE) {
-
-        /* We assume that we only get a PPM if the image cannot be
-           represented as PBM or PGM.  There are two options here: either
-           8-bit with a colormap, either the standard one or a custom one,
-           or 16-bit direct color.  In the 8-bit case, if "custom_colormap"
-           is specified (not recommended by Palm) we will put in our own
-           colormap; otherwise we will assume that the colors have been
-           mapped to the default Palm colormap by appropriate use of
-           pnmquant.  We try for 8-bit color first, since it works on
-           more PalmOS devices. 
-        */
-        if ((specified_bpp == 16) || 
-            (specified_bpp == 0 && max_bpp == 16)) {
-            /* we do the 16-bit direct color */
-            *directColorP = TRUE;
-            *colormapP = NULL;
-            *bppP = 16;
-        } else if (!custom_colormap) {
-            /* standard indexed 8-bit color */
-            *colormapP = palmcolor_build_default_8bit_colormap();
-            *bppP = 8;
-            if (((specified_bpp != 0) && (specified_bpp != 8)) ||
-                ((max_bpp != 0) && (max_bpp < 8)))
-                pm_error("Must use depth of 8 for color Palm Bitmap without "
-                         "custom color table.");
-            *directColorP = FALSE;
-            if (verbose)
-                pm_message("Output is color with default colormap at 8 bpp");
-        } else {
-            /* indexed 8-bit color with a custom colormap */
-            *colormapP = 
-                palmcolor_build_custom_8bit_colormap(rows, cols, xels);
-            for (*bppP = 1; (1 << *bppP) < (*colormapP)->ncolors; *bppP *= 2);
-            if (specified_bpp != 0) {
-                if (specified_bpp >= *bppP)
-                    *bppP = specified_bpp;
-                else
-                    pm_error("Too many colors for specified depth.  "
-                             "Use pnmquant to reduce.");
-            } else if ((max_bpp != 0) && (max_bpp < *bppP)) {
-                pm_error("Too many colors for specified max depth.  "
-                         "Use pnmquant to reduce.");
-            }
-            *directColorP = FALSE;
-            if (verbose)
-                pm_message("Output is color with custom colormap "
-                           "with %d colors at %d bpp", 
-                           (*colormapP)->ncolors, *bppP);
-        }
+        determinePalmFormatPpm(cols, rows, maxval, xels, bppSpecified, bpp,
+                               maxBppSpecified, maxBpp,
+                               wantCustomColormap, compression, verbose,
+                               bppP, directColorP, colormapPP);
     } else {
         pm_error("unknown format 0x%x on input file", (unsigned) format);
     }
+
+    if (compression != COMP_NONE)
+        assert(*bppP <= 8);
 }
 
 
@@ -300,25 +435,23 @@ findTransparentColor(const char *   const colorSpec,
                      pixval         const newMaxval,
                      bool           const directColor, 
                      pixval         const maxval, 
-                     Colormap       const colormap,
+                     Colormap *     const colormapP,
                      xel *          const transcolorP, 
                      unsigned int * const transindexP) {
 
     *transcolorP = ppm_parsecolor(colorSpec, maxval);
     if (!directColor) {
-        Color_s const temp_color = 
-            ((((PPM_GETR(*transcolorP)*newMaxval) / maxval) << 16) 
-             | (((PPM_GETG(*transcolorP)*newMaxval) / maxval) << 8)
-             | ((PPM_GETB(*transcolorP)*newMaxval) / maxval));
-        Color const found = 
-            (bsearch(&temp_color,
-                     colormap->color_entries, colormap->ncolors,
-                     sizeof(Color_s), palmcolor_compare_colors));
-        if (!found) {
+        ColormapEntry const searchTarget = 
+            palmcolor_mapEntryColorFmPixel(*transcolorP, maxval, newMaxval);
+        ColormapEntry * const foundEntryP = 
+            (bsearch(&searchTarget,
+                     colormapP->color_entries, colormapP->ncolors,
+                     sizeof(ColormapEntry), palmcolor_compare_colors));
+        if (!foundEntryP) {
             pm_error("Specified transparent color %s not found "
                      "in colormap.", colorSpec);
         } else
-            *transindexP = (*found >> 24) & 0xFF;
+            *transindexP = (*foundEntryP >> 24) & 0xFF;
     }
 }
 
@@ -326,9 +459,9 @@ findTransparentColor(const char *   const colorSpec,
 
 static unsigned int
 bitmapVersion(unsigned int         const bpp,
-              bool                 const colormap,
+              bool                 const colormapped,
               bool                 const transparent,
-              enum compressionType const compression,
+              enum CompressionType const compression,
               unsigned int         const density) {
 /*----------------------------------------------------------------------------
    Return the version number of the oldest version that can represent
@@ -343,7 +476,7 @@ bitmapVersion(unsigned int         const bpp,
         version = 3;
     else if (transparent || compression != COMP_NONE)
         version = 2;
-    else if (bpp > 1 || colormap)
+    else if (bpp > 1 || colormapped)
         version = 1;
     else
         version = 0;
@@ -357,8 +490,8 @@ static void
 writeCommonHeader(unsigned int         const cols,
                   unsigned int         const rows,
                   unsigned int         const rowbytes,
-                  enum compressionType const compression,
-                  bool                 const colormap,
+                  enum CompressionType const compression,
+                  bool                 const colormapped,
                   bool                 const transparent,
                   bool                 const directColor,
                   unsigned int         const bpp,
@@ -382,7 +515,7 @@ writeCommonHeader(unsigned int         const cols,
     flags = 0;  /* initial value */
     if (compression != COMP_NONE)
         flags |= PALM_IS_COMPRESSED_FLAG;
-    if (colormap)
+    if (colormapped)
         flags |= PALM_HAS_COLORMAP_FLAG;
     if (transparent)
         flags |= PALM_HAS_TRANSPARENCY_FLAG;
@@ -398,7 +531,7 @@ writeCommonHeader(unsigned int         const cols,
 
 
 static unsigned char 
-compressionFieldValue(enum compressionType const compression) {
+compressionFieldValue(enum CompressionType const compression) {
 
     unsigned char retval;
 
@@ -424,7 +557,7 @@ compressionFieldValue(enum compressionType const compression) {
 static void
 writeRemainingHeaderLow(unsigned int         const nextDepthOffset,
                         unsigned int         const transindex,
-                        enum compressionType const compression,
+                        enum CompressionType const compression,
                         unsigned int         const bpp) {
 /*----------------------------------------------------------------------------
    Write last 6 bytes of a low density Palm Bitmap header. 
@@ -449,7 +582,7 @@ writeRemainingHeaderLow(unsigned int         const nextDepthOffset,
 
 static void
 writeRemainingHeaderHigh(unsigned int         const bpp,
-                         enum compressionType const compression,
+                         enum CompressionType const compression,
                          unsigned int         const density,
                          xelval               const maxval,
                          bool                 const transparent,
@@ -480,9 +613,9 @@ writeRemainingHeaderHigh(unsigned int         const bpp,
         if (bpp == 16) {
             /* Blind guess here */
             fputc(0, stdout);
-            fputc((PPM_GETR(transcolor) * 255) / maxval, stdout);
-            fputc((PPM_GETG(transcolor) * 255) / maxval, stdout);
-            fputc((PPM_GETB(transcolor) * 255) / maxval, stdout);
+            fputc(scaleSample(PPM_GETR(transcolor), maxval, 255), stdout);
+            fputc(scaleSample(PPM_GETG(transcolor), maxval, 255), stdout);
+            fputc(scaleSample(PPM_GETB(transcolor), maxval, 255), stdout);
         } else {
             assert(transindex <= UCHAR_MAX);
             fputc(0, stdout);
@@ -523,7 +656,7 @@ writeDummy() {
 
 static void
 writeColormap(bool         const explicitColormap,
-              Colormap     const colormap,
+              Colormap *   const colormapP,
               bool         const directColor,
               unsigned int const bpp,
               bool         const transparent,
@@ -534,14 +667,14 @@ writeColormap(bool         const explicitColormap,
     /* if there's a colormap, write it out */
     if (explicitColormap) {
         unsigned int row;
-        if (!colormap)
+        if (!colormapP)
             pm_error("Internal error: user specified -colormap, but we did "
                      "not generate a colormap.");
-        qsort (colormap->color_entries, colormap->ncolors,
-               sizeof(Color_s), palmcolor_compare_indices);
-        pm_writebigshort( stdout, colormap->ncolors );
-        for (row = 0;  row < colormap->ncolors; ++row)
-            pm_writebiglong (stdout, colormap->color_entries[row]);
+        qsort(colormapP->color_entries, colormapP->ncolors,
+              sizeof(ColormapEntry), palmcolor_compare_indices);
+        pm_writebigshort( stdout, colormapP->ncolors );
+        for (row = 0;  row < colormapP->ncolors; ++row)
+            pm_writebiglong (stdout, colormapP->color_entries[row]);
     }
 
     if (directColor && (version < 3)) {
@@ -552,13 +685,13 @@ writeColormap(bool         const explicitColormap,
             fputc(5, stdout);   /* # of bits of blue */
             fputc(0, stdout);   /* reserved by Palm */
         } else
-            pm_error("Don't know how to create %d bit DirectColor bitmaps.", 
+            pm_error("Don't know how to create %u bit DirectColor bitmaps.", 
                      bpp);
         if (transparent) {
             fputc(0, stdout);
-            fputc((PPM_GETR(transcolor) * 255) / maxval, stdout);
-            fputc((PPM_GETG(transcolor) * 255) / maxval, stdout);
-            fputc((PPM_GETB(transcolor) * 255) / maxval, stdout);
+            fputc(scaleSample(PPM_GETR(transcolor) , maxval, 255), stdout);
+            fputc(scaleSample(PPM_GETG(transcolor) , maxval, 255), stdout);
+            fputc(scaleSample(PPM_GETB(transcolor) , maxval, 255), stdout);
         } else
             pm_writebiglong(stdout, 0);     /* no transparent color */
     }
@@ -571,17 +704,29 @@ computeRawRowDirectColor(const xel *     const xelrow,
                          unsigned int    const cols,
                          xelval          const maxval,
                          unsigned char * const rowdata) {
+/*----------------------------------------------------------------------------
+  Compute a row of Palm data in raw (uncompressed) form for an image that
+  uses direct color (really, true color: each pixel contains RGB intensities
+  as distinct R, G, and B numbers).
 
+  In this format, each pixel is 16 bits: 5 red, 6 green, 5 blue.
+
+  'xelrow' is the image contents of row.  It is 'cols' columns wide and
+  samples are based on maxval 'maxval'.
+
+  Put the output data at 'rowdata'. 
+-----------------------------------------------------------------------------*/
     unsigned int col;
-    unsigned char *outptr;
+    unsigned char * outCursor;
     
-    for (col = 0, outptr = rowdata; col < cols; ++col) {
+    for (col = 0, outCursor = &rowdata[0]; col < cols; ++col) {
         unsigned int const color = 
-            ((((PPM_GETR(xelrow[col])*31)/maxval) << 11) |
-             (((PPM_GETG(xelrow[col])*63)/maxval) << 5) |
-             ((PPM_GETB(xelrow[col])*31)/maxval));
-        *outptr++ = (color >> 8) & 0xFF;
-        *outptr++ = color & 0xFF;
+            (scaleSample(PPM_GETR(xelrow[col]), maxval, 31) << 11) |
+            (scaleSample(PPM_GETG(xelrow[col]), maxval, 63) <<  5) |
+            (scaleSample(PPM_GETB(xelrow[col]), maxval, 31) <<  0);
+
+        *outCursor++ = (color >> 8) & 0xFF;
+        *outCursor++ = color & 0xFF;
     }
 }
 
@@ -592,23 +737,40 @@ computeRawRowNonDirect(const xel *     const xelrow,
                        unsigned int    const cols,
                        xelval          const maxval,
                        unsigned int    const bpp,
-                       Colormap        const colormap,
+                       Colormap *      const colormapP,
                        unsigned int    const newMaxval,
                        unsigned char * const rowdata) {
+/*----------------------------------------------------------------------------
+  Compute a row of Palm data in raw (uncompressed) form for an image that
+  does not have a raster whose elements are explicit R, G, and B
+  intensities.
 
+  If 'colormapP' is non-null, the pixel is an index into that colormap.
+  'newMaxval' is meaningless.
+
+  If 'colormapP' is null, the pixel is a grayscale intensity, on a scale with
+  maximum value 'newMaxval'.  (N.B. this is really direct color, but for some
+  reason it's historically lumped in with the paletted formats).
+
+  'xelrow' is the image contents of row.  It is 'cols' columns wide and
+  samples are based on maxval 'maxval'.
+
+  Put the output data at 'rowdata', using 'bpp' bits per pixel.
+-----------------------------------------------------------------------------*/
     unsigned int col;
-    unsigned char *outptr;
+    unsigned char * outCursor;
+        /* Points to next slot in 'rowdata' we will fill */
     unsigned char outbyte;
         /* Accumulated bits to be output */
     unsigned char outbit;
         /* The lowest bit number we want to access for this pixel */
 
-    outbyte = 0x00;
-    outptr = rowdata;
+    outbyte = 0x00;  /* initial value */
+    outCursor = &rowdata[0];  /* Start at the beginning of the row */
 
     for (outbit = 8 - bpp, col = 0; col < cols; ++col) {
         unsigned int color;
-        if (!colormap) {
+        if (!colormapP) {
             /* we assume grayscale, and use simple scaling */
             color = (PNM_GET1(xelrow[col]) * newMaxval)/maxval;
             if (color > newMaxval)
@@ -616,24 +778,23 @@ computeRawRowNonDirect(const xel *     const xelrow,
                          "color of %u.", color);
             color = newMaxval - color; /* note grayscale maps are inverted */
         } else {
-            Color_s const temp_color =
-                ((((PPM_GETR(xelrow[col])*newMaxval)/maxval)<<16) 
-                 | (((PPM_GETG(xelrow[col])*newMaxval)/maxval)<<8)
-                 | (((PPM_GETB(xelrow[col])*newMaxval)/maxval)));
-            Color const found = (bsearch (&temp_color,
-                                          colormap->color_entries, 
-                                          colormap->ncolors,
-                                          sizeof(Color_s), 
-                                          palmcolor_compare_colors));
-            if (!found) {
-                pm_error("Color %d:%d:%d not found in colormap.  "
-                         "Try using pnmquant to reduce the "
-                         "number of colors.",
+            ColormapEntry const searchTarget =
+                palmcolor_mapEntryColorFmPixel(xelrow[col], maxval, 255);
+            ColormapEntry * const foundEntryP =
+                bsearch(&searchTarget,
+                        colormapP->color_entries, 
+                        colormapP->ncolors,
+                        sizeof(ColormapEntry), 
+                        palmcolor_compare_colors);
+            if (!foundEntryP) {
+                pm_error("INERNAL ERROR: "
+                         "Color (%u,%u,%u) not found in colormap, "
+                         "though it was supposedly there before",
                          PPM_GETR(xelrow[col]), 
                          PPM_GETG(xelrow[col]), 
                          PPM_GETB(xelrow[col]));
             }
-            color = (*found >> 24) & 0xFF;
+            color = (*foundEntryP >> 24) & 0xFF;
         }
 
         if (color > newMaxval)
@@ -642,7 +803,7 @@ computeRawRowNonDirect(const xel *     const xelrow,
         outbyte |= (color << outbit);
         if (outbit == 0) {
             /* Bit buffer is full.  Flush to to rowdata. */
-            *outptr++ = outbyte;
+            *outCursor++ = outbyte;
             outbyte = 0x00;
             outbit = 8 - bpp;
         } else
@@ -650,7 +811,7 @@ computeRawRowNonDirect(const xel *     const xelrow,
     }
     if ((cols % (8 / bpp)) != 0) {
         /* Flush bits remaining in the bit buffer to rowdata */
-        *outptr++ = outbyte;
+        *outCursor++ = outbyte;
     }
 }
 
@@ -830,7 +991,7 @@ rleCompressAndBufferRow(const unsigned char * const rowdata,
         unsigned int repeatcount;
         for (repeatcount = 1;  
              repeatcount < (rowbytes - pos) && repeatcount  < 255;  
-             ++repeatcount)
+             ++repeatcount) 
             if (rowdata[pos + repeatcount] != rowdata[pos])
                 break;
 
@@ -870,7 +1031,7 @@ packbitsCompressAndBufferRow(const unsigned char * const rowdata,
 static void
 bufferRowFromRawRowdata(const unsigned char *  const rowdata,
                         unsigned int           const rowbytes,
-                        enum compressionType   const compression,
+                        enum CompressionType   const compression,
                         const unsigned char *  const lastrow,
                         struct seqBuffer *     const rasterBufferP) {
 /*----------------------------------------------------------------------------
@@ -909,16 +1070,17 @@ bufferRow(const xel *          const xelrow,
           unsigned int         const rowbytes,
           unsigned int         const bpp,
           unsigned int         const newMaxval,
-          enum compressionType const compression,
+          enum CompressionType const compression,
           bool                 const directColor,
-          Colormap             const colormap,
+          Colormap *           const colormapP,
           unsigned char *      const rowdata,
           unsigned char *      const lastrow,
           struct seqBuffer *   const rasterBufferP) {
 /*----------------------------------------------------------------------------
    Add a row of the Palm Bitmap raster to buffer 'rasterBufferP'.
    
-   'xelrow' is the image contents of row.  It is 'cols' columns wide.
+   'xelrow' is the image contents of row.  It is 'cols' columns wide and
+   samples are based on maxval 'maxval'.
 
    If 'compression' indicates scanline compression, 'lastrow' is the
    row immediately preceding this one in the image (and this function
@@ -930,7 +1092,7 @@ bufferRow(const xel *          const xelrow,
     if (directColor)
         computeRawRowDirectColor(xelrow, cols, maxval, rowdata);
     else 
-        computeRawRowNonDirect(xelrow, cols, maxval, bpp, colormap, newMaxval,
+        computeRawRowNonDirect(xelrow, cols, maxval, bpp, colormapP, newMaxval,
                                rowdata);
 
     bufferRowFromRawRowdata(rowdata, rowbytes, compression,
@@ -947,9 +1109,9 @@ bufferRaster(xel **               const xels,
              unsigned int         const rowbytes,
              unsigned int         const bpp,
              unsigned int         const newMaxval,
-             enum compressionType const compression,
+             enum CompressionType const compression,
              bool                 const directColor,
-             Colormap             const colormap,
+             Colormap *           const colormapP,
              struct seqBuffer **  const rasterBufferPP) {
     
     unsigned char * rowdata;
@@ -971,7 +1133,7 @@ bufferRaster(xel **               const xels,
     for (row = 0; row < rows; ++row) {
         bufferRow(xels[row], cols, maxval, rowbytes, bpp, newMaxval,
                   compression,
-                  directColor, colormap, rowdata, row > 0 ? lastrow : NULL,
+                  directColor, colormapP, rowdata, row > 0 ? lastrow : NULL,
                   *rasterBufferPP);
 
         if (compression == COMP_SCANLINE)
@@ -988,8 +1150,8 @@ static void
 computeOffsetStuff(bool                 const offsetWanted,
                    unsigned int         const version,
                    bool                 const directColor,
-                   enum compressionType const compression,
-                   bool                 const colormap,
+                   enum CompressionType const compression,
+                   bool                 const colormapped,
                    unsigned int         const colormapColorCount,
                    unsigned int         const sizePlusRasterSize,
                    unsigned int *       const nextDepthOffsetP,
@@ -1003,7 +1165,7 @@ computeOffsetStuff(bool                 const offsetWanted,
         */
         unsigned int const headerSize = ((version < 3) ? 16 : 24);
         unsigned int const colormapSize =
-            (colormap ? (2 + colormapColorCount * 4) : 0);
+            (colormapped ? (2 + colormapColorCount * 4) : 0);
         if (version < 3) {
             unsigned int const directSize = 
                 (directColor && version < 3) ? 8 : 0; 
@@ -1058,12 +1220,12 @@ writeBitmap(xel **               const xels,
             unsigned int         const rowbytes,
             unsigned int         const bpp,
             unsigned int         const newMaxval,
-            enum compressionType const compression,
+            enum CompressionType const compression,
             bool                 const transparent,
             bool                 const directColor,
             bool                 const offsetWanted,
-            bool                 const hasColormap,
-            Colormap             const colormap,
+            bool                 const colormapped,
+            Colormap *           const colormapP,
             unsigned int         const transindex,
             xel                  const transcolor,
             unsigned int         const version,
@@ -1086,11 +1248,11 @@ writeBitmap(xel **               const xels,
         */
     struct seqBuffer * rasterBufferP;
 
-    writeCommonHeader(cols, rows, rowbytes, compression, hasColormap, 
+    writeCommonHeader(cols, rows, rowbytes, compression, colormapped, 
                       transparent, directColor, bpp, version);
     
     bufferRaster(xels, cols, rows, maxval, rowbytes, bpp, newMaxval,
-                 compression, directColor, colormap, &rasterBufferP);
+                 compression, directColor, colormapP, &rasterBufferP);
 
     /* rasterSize itself takes 2 or 4 bytes */
     if (version < 3)
@@ -1099,7 +1261,7 @@ writeBitmap(xel **               const xels,
         sizePlusRasterSize = 4 + bufferLength(rasterBufferP);
     
     computeOffsetStuff(offsetWanted, version, directColor, compression,
-                       hasColormap, hasColormap ? colormap->ncolors : 0, 
+                       colormapped, colormapped ? colormapP->ncolors : 0, 
                        sizePlusRasterSize,
                        &nextDepthOffset, &nextBitmapOffset,
                        &padBytesRequired);
@@ -1111,7 +1273,7 @@ writeBitmap(xel **               const xels,
                                  maxval, transparent, transcolor,
                                  transindex, nextBitmapOffset);
 
-    writeColormap(hasColormap, colormap, directColor, bpp, 
+    writeColormap(colormapped, colormapP, directColor, bpp, 
                   transparent, transcolor, maxval, version);
 
     if (compression != COMP_NONE)
@@ -1134,8 +1296,8 @@ writeBitmap(xel **               const xels,
 
 
 int 
-main( int argc, char **argv ) {
-    struct cmdline_info cmdline;
+main( int argc, const char **argv ) {
+    struct CmdlineInfo cmdline;
     unsigned int version;
     FILE* ifP;
     xel** xels;
@@ -1148,10 +1310,9 @@ main( int argc, char **argv ) {
     unsigned int bpp;
     bool directColor;
     unsigned int newMaxval;
-    Colormap colormap;
+    Colormap * colormapP;
     
-    /* Parse default params */
-    pnm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
@@ -1161,18 +1322,20 @@ main( int argc, char **argv ) {
     pm_close(ifP);
 
     if (cmdline.verbose)
-        pm_message("Input is %dx%d %s, maxval %d", 
+        pm_message("Input is %ux%u %s, maxval %u", 
                    cols, rows, formatName(format), maxval);
     
-    determinePalmFormat(cols, rows, maxval, format, xels, cmdline.depth,
-                        cmdline.maxdepth, cmdline.colormap, cmdline.verbose,
-                        &bpp, &directColor, &colormap);
+    determinePalmFormat(cols, rows, maxval, format, xels,
+                        cmdline.depthSpec, cmdline.depth,
+                        cmdline.maxdepthSpec, cmdline.maxdepth,
+                        cmdline.colormap, cmdline.compression, cmdline.verbose,
+                        &bpp, &directColor, &colormapP);
 
     newMaxval = (1 << bpp) - 1;
 
     if (cmdline.transparent) 
         findTransparentColor(cmdline.transparent, newMaxval, directColor,
-                             maxval, colormap, &transcolor, &transindex);
+                             maxval, colormapP, &transcolor, &transindex);
     else 
         transindex = 0;
 
@@ -1185,7 +1348,7 @@ main( int argc, char **argv ) {
     writeBitmap(xels, cols, rows, maxval,
                 rowbytes, bpp, newMaxval, cmdline.compression,
                 !!cmdline.transparent, directColor, cmdline.offset, 
-                cmdline.colormap, colormap, transindex, transcolor,
+                cmdline.colormap, colormapP, transindex, transcolor,
                 version, cmdline.density, cmdline.withdummy);
     
     return 0;
diff --git a/converter/other/pnmtopclxl.c b/converter/other/pnmtopclxl.c
index 8cabb614..f7db62ee 100644
--- a/converter/other/pnmtopclxl.c
+++ b/converter/other/pnmtopclxl.c
@@ -3,7 +3,7 @@
  *
  *  (C) 2002 Jochen Karrer, Linuxdata GbR
  *
- *      convert a pnm to PCL-XL image 
+ *      convert a pnm to PCL-XL image
  *
  * -------------------------------------------------------------
  */
@@ -44,7 +44,7 @@
 
 
 typedef struct InputSource {
-    const char *         name; 
+    const char *         name;
     struct InputSource * next;
 } InputSource;
 
@@ -82,7 +82,7 @@ parseCommandLine(int argc, 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.
@@ -104,25 +104,25 @@ parseCommandLine(int argc, char ** argv,
     option_def_index = 0;   /* incremented by OPTENT3 */
     OPTENT3(0, "dpi",       OPT_UINT,    &cmdlineP->dpi,
             &dpiSpec,         0);
-    OPTENT3(0, "xoffs",     OPT_FLOAT,   &cmdlineP->xoffs, 
+    OPTENT3(0, "xoffs",     OPT_FLOAT,   &cmdlineP->xoffs,
             &xoffsSpec,        0);
-    OPTENT3(0, "yoffs",     OPT_FLOAT,   &cmdlineP->yoffs, 
+    OPTENT3(0, "yoffs",     OPT_FLOAT,   &cmdlineP->yoffs,
             &yoffsSpec,        0);
-    OPTENT3(0, "format",    OPT_STRING,  &formatOpt, 
+    OPTENT3(0, "format",    OPT_STRING,  &formatOpt,
             &formatSpec,        0);
-    OPTENT3(0, "duplex",    OPT_STRING,  &duplexOpt, 
+    OPTENT3(0, "duplex",    OPT_STRING,  &duplexOpt,
             &cmdlineP->duplexSpec,        0);
     OPTENT3(0, "copies",    OPT_UINT,    &cmdlineP->copies,
             &cmdlineP->copiesSpec,        0);
-    OPTENT3(0, "colorok",   OPT_FLAG,    NULL,                  
+    OPTENT3(0, "colorok",   OPT_FLAG,    NULL,
             &cmdlineP->colorok, 0);
-    OPTENT3(0, "center",    OPT_FLAG,    NULL,                  
+    OPTENT3(0, "center",    OPT_FLAG,    NULL,
             &cmdlineP->center, 0 );
     OPTENT3(0, "feeder",    OPT_UINT,    &cmdlineP->feeder,
             &cmdlineP->feederSpec,        0);
     OPTENT3(0, "outtray",   OPT_UINT,    &cmdlineP->outtray,
             &cmdlineP->outtraySpec,       0);
-    OPTENT3(0, "verbose",   OPT_FLAG,    NULL,                  
+    OPTENT3(0, "verbose",   OPT_FLAG,    NULL,
             &cmdlineP->verbose, 0);
     OPTENT3(0, "jobsetup",  OPT_STRING,  &cmdlineP->jobsetup,
             &jobsetupSpec,      0);
@@ -216,7 +216,7 @@ parseCommandLine(int argc, char ** argv,
 
 static void
 freeSource(InputSource * const firstSourceP) {
-    
+
     InputSource * sourceP;
 
     sourceP = firstSourceP;
@@ -284,7 +284,7 @@ pnmToPcllinePackbits(pclGenerator * const pclGeneratorP,
     unsigned int col, padCt;
     unsigned int const padsize =
         pclGeneratorP->paddedLinelen - pclGeneratorP->linelen;
-        
+
     tuplerow = pnm_allocpamrow(pamP);
 
     pnm_readpamrow(pamP, tuplerow);
@@ -297,7 +297,7 @@ pnmToPcllinePackbits(pclGenerator * const pclGeneratorP,
         if (bitmask == 0) {
             pclGeneratorP->data[pclGeneratorP->cursor++] = accum;
             bitmask = 0x80; accum = 0x0;
-        } 
+        }
     }
     if (bitmask != 0x80)
         pclGeneratorP->data[pclGeneratorP->cursor++] = accum;
@@ -351,7 +351,7 @@ createPclGeneratorPackbits(struct pam *    const pamP,
 
     pclGeneratorP->data =
         malloc(pclDatabuffSize(pclGeneratorP->paddedLinelen));
-    
+
     if (pclGeneratorP->data == NULL)
         pm_error("Unable to allocate row buffer.");
 
@@ -378,7 +378,7 @@ pnmToPcllineWholebytes(pclGenerator * const pclGeneratorP,
     for (col = 0; col < pamP->width; ++col) {
         unsigned int plane;
         for (plane = 0; plane < pamP->depth; ++plane) {
-            pclGeneratorP->data[pclGeneratorP->cursor++] = 
+            pclGeneratorP->data[pclGeneratorP->cursor++] =
                 pnm_scalesample(tuplerow[col][plane], pamP->maxval, 255);
         }
     }
@@ -399,7 +399,7 @@ createPclGeneratorWholebytes(struct pam *    const pamP,
     pclGenerator * pclGenP;
 
     MALLOCVAR_NOFAIL(pclGenP);
-    
+
     if (pamP->depth <  3)
         pclGenP->colorSpace = eGray;
     else
@@ -418,7 +418,7 @@ createPclGeneratorWholebytes(struct pam *    const pamP,
         if (pclGenP->data == NULL)
             pm_error("Unable to allocate row buffer.");
     }
-    
+
     pclGenP->getnextrow = pnmToPcllineWholebytes;
 
     *pclGenPP = pclGenP;
@@ -436,7 +436,7 @@ destroyPclGenerator(pclGenerator * const pclGenP) {
 
 
 
-static void 
+static void
 createPclGenerator(struct pam *        const pamP,
                    pclGenerator **     const pclGeneratorPP,
                    bool                const colorok) {
@@ -448,16 +448,16 @@ createPclGenerator(struct pam *        const pamP,
                    "through Ppmtopgm.  To suppress this warning, use the "
                    "-colorok option.");
 
-    if (pamP->depth == 1 && pamP->maxval == 1) 
+    if (pamP->depth == 1 && pamP->maxval == 1)
         createPclGeneratorPackbits(pamP, pclGeneratorPP);
-    else 
+    else
         createPclGeneratorWholebytes(pamP, pclGeneratorPP);
 }
 
 
 
 
-struct tPrinter { 
+struct tPrinter {
     const char *name;
     float topmargin;
     float bottommargin;
@@ -478,7 +478,7 @@ out_ubyte(int           const fd,
 
 
 
-static int 
+static int
 XL_Operator(int           const fd,
             enum Operator const data)  {
 
@@ -514,7 +514,7 @@ static int
 out_sint16(int          const fd,
            signed short const sdata ) {
 
-    unsigned short const data= (unsigned short)sdata;    
+    unsigned short const data= (unsigned short)sdata;
 
     unsigned char c[2];
 
@@ -559,7 +559,7 @@ xl_ubyte_array(int                   const fd,
 
     unsigned int i;
     unsigned char head[4];
-    
+
     head[0] = 0xc8;
     head[1] = 0xc1;
     head[2] = len & 0xff;
@@ -671,7 +671,7 @@ convertAndWriteRleBlock(int                  const outFd,
     pm_rlenc_compressbyte(pclGeneratorP->data, outbuf, PM_RLE_PACKBITS,
                           pclGeneratorP->paddedLinelen * lineCt, &rlelen);
 
-    xl_dataLength(outFd, rlelen); 
+    xl_dataLength(outFd, rlelen);
     XY_Write(outFd, outbuf, rlelen);
 }
 
@@ -680,10 +680,10 @@ convertAndWriteRleBlock(int                  const outFd,
 /*
  * ------------------------------------------------------------
  * XL_WriteImage
- *  Write a PCL-XL image to the datastream 
+ *  Write a PCL-XL image to the datastream
  * ------------------------------------------------------------
  */
-static void 
+static void
 convertAndWriteImage(int            const outFd,
                      pclGenerator * const pclGenP,
                      struct pam *   const pamP) {
@@ -695,10 +695,10 @@ convertAndWriteImage(int            const outFd,
 
     xl_ubyte(outFd, eDirectPixel); xl_attr_ubyte(outFd, aColorMapping);
     xl_ubyte(outFd, pclGenP->colorDepth); xl_attr_ubyte(outFd, aColorDepth);
-    xl_uint16(outFd, pclGenP->width); xl_attr_ubyte(outFd, aSourceWidth);  
-    xl_uint16(outFd, pclGenP->height); xl_attr_ubyte(outFd, aSourceHeight);    
-    xl_uint16_xy(outFd, pclGenP->width*1, pclGenP->height*1); 
-    xl_attr_ubyte(outFd, aDestinationSize);   
+    xl_uint16(outFd, pclGenP->width); xl_attr_ubyte(outFd, aSourceWidth);
+    xl_uint16(outFd, pclGenP->height); xl_attr_ubyte(outFd, aSourceHeight);
+    xl_uint16_xy(outFd, pclGenP->width*1, pclGenP->height*1);
+    xl_attr_ubyte(outFd, aDestinationSize);
     XL_Operator(outFd, oBeginImage);
 
     pm_rlenc_allocoutbuf(&outbuf, inSize, PM_RLE_PACKBITS);
@@ -707,13 +707,13 @@ convertAndWriteImage(int            const outFd,
         unsigned int const blockHeight =
             MIN(20, pclGenP->height-blockStartLine);
 
-        xl_uint16(outFd, blockStartLine); xl_attr_ubyte(outFd, aStartLine); 
+        xl_uint16(outFd, blockStartLine); xl_attr_ubyte(outFd, aStartLine);
         xl_uint16(outFd, blockHeight); xl_attr_ubyte(outFd, aBlockHeight);
         xl_ubyte(outFd, eRLECompression); xl_attr_ubyte(outFd, aCompressMode);
         /* In modern PCL-XL, we could use a PadBytesMultiple attribute
            here to avoid having to pad the data to a multiple of 4
            bytes.  But PCL-XL 1.1 didn't have PadBytesMultiple.
-           xl_ubyte(outFd, 1); xl_attr_ubyte(outFd, aPadBytesMultiple); 
+           xl_ubyte(outFd, 1); xl_attr_ubyte(outFd, aPadBytesMultiple);
         */
         XL_Operator(outFd, oReadImage);
         convertAndWriteRleBlock(outFd, pclGenP, pamP,
@@ -735,16 +735,16 @@ printEmbeddedImage(int                 const outFd,
     struct pam pam;
     pclGenerator * pclGeneratorP;
 
-    openDataSource(outFd, eBinaryLowByteFirst, eDefaultSource);
+    openDataSource(outFd, eBinaryLowByteFirst, eDefaultDataSource);
 
     ifP = pm_openr(sourceP->name);
 
     pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
-                
+
     createPclGenerator(&pam, &pclGeneratorP, colorok);
 
     convertAndWriteImage(outFd, pclGeneratorP, &pam);
-    
+
     destroyPclGenerator(pclGeneratorP);
 
     pm_close(ifP);
@@ -772,9 +772,9 @@ copyFile(const char * const sourceFileName,
         if (ferror(sourceFileP))
             pm_error("Read from file failed.  errno=%d (%s)",
                      errno, strerror(errno));
-        
+
         totalBytesWritten = 0;
-        
+
         while (totalBytesWritten < bytesRead) {
             ssize_t rc;
 
@@ -804,16 +804,16 @@ jobHead(int          const outFd,
    as opposed to e.g. Postscript.
 -----------------------------------------------------------------------------*/
     /* Reset */
-    XY_Puts(outFd,"\033%-12345X");  
+    XY_Puts(outFd,"\033%-12345X");
 
     if (userJobSetupFileName)
         copyFile(userJobSetupFileName, outFd);
 
     if (renderGray)
-        XY_Puts(outFd, "@PJL SET RENDERMODE=GRAYSCALE\n");  
+        XY_Puts(outFd, "@PJL SET RENDERMODE=GRAYSCALE\n");
 
-    XY_Puts(outFd, "@PJL ENTER LANGUAGE=PCLXL\n");  
-    XY_Puts(outFd, ") HP-PCL XL;1;1;Generated by Netpbm Pnmtopclxl\n");  
+    XY_Puts(outFd, "@PJL ENTER LANGUAGE=PCLXL\n");
+    XY_Puts(outFd, ") HP-PCL XL;1;1;Generated by Netpbm Pnmtopclxl\n");
 }
 
 
@@ -825,7 +825,7 @@ jobEnd(int const outFd) {
 
    Reset printer to quiescent mode.  Exit the printer language.
 -----------------------------------------------------------------------------*/
-    XY_Puts(outFd,"\033%-12345X");  
+    XY_Puts(outFd,"\033%-12345X");
 }
 
 
@@ -852,7 +852,7 @@ beginPage(int                 const outFd,
     }
 
     if (doMediaDestination) {
-        xl_ubyte(outFd, mediaDestination);  
+        xl_ubyte(outFd, mediaDestination);
         xl_attr_ubyte(outFd, aMediaDestination);
     }
 
@@ -898,11 +898,11 @@ setColorSpace(int                   const outFd,
    paletteDepth is not e8Bit.  Is each palette entry still a byte and only
    some of the byte gets used?  Or are there multiple entries per byte?
 -----------------------------------------------------------------------------*/
-    xl_ubyte(outFd, colorSpace); xl_attr_ubyte(outFd, aColorSpace);   
+    xl_ubyte(outFd, colorSpace); xl_attr_ubyte(outFd, aColorSpace);
     if (palette) {
-        xl_ubyte(outFd, paletteDepth); 
-        xl_attr_ubyte(outFd, aPaletteDepth);   
-        xl_ubyte_array(outFd, palette, paletteSize); 
+        xl_ubyte(outFd, paletteDepth);
+        xl_attr_ubyte(outFd, aPaletteDepth);
+        xl_ubyte_array(outFd, palette, paletteSize);
         xl_attr_ubyte(outFd, aPaletteData);
     }
     XL_Operator(outFd, oSetColorSpace);
@@ -925,8 +925,8 @@ positionCursor(int            const outFd,
     float xpos, ypos;
 
     if (center) {
-        float const width  = 1.0 * imageWidth/dpi;  
-        float const height = 1.0 * imageHeight/dpi;    
+        float const width  = 1.0 * imageWidth/dpi;
+        float const height = 1.0 * imageHeight/dpi;
         xpos = (PAPERWIDTH(format) - width)/2;
         ypos = (PAPERHEIGHT(format) - height)/2;
     } else {
@@ -985,11 +985,11 @@ convertAndPrintPage(int                  const outFd,
        an IllegalArraySize error from the printer on the SetColorSpace
        command.
 
-       So we don't use a palette at all now.  
+       So we don't use a palette at all now.
     */
     setColorSpace(outFd, pclGeneratorP->colorSpace, NULL, 0, 0);
 
-    positionCursor(outFd, center, xoffs, yoffs, 
+    positionCursor(outFd, center, xoffs, yoffs,
                    pclGeneratorP->width, pclGeneratorP->height, dpi, format);
 
     convertAndWriteImage(outFd, pclGeneratorP, pamP);
@@ -1007,7 +1007,7 @@ beginSession(int              const outFd,
              bool             const noReporting,
              enum ErrorReport const errorReport) {
 
-    xl_uint16_xy(outFd, xdpi, ydpi); xl_attr_ubyte(outFd, aUnitsPerMeasure); 
+    xl_uint16_xy(outFd, xdpi, ydpi); xl_attr_ubyte(outFd, aUnitsPerMeasure);
     xl_ubyte(outFd, measure);  xl_attr_ubyte(outFd, aMeasure);
     /* xl_ubyte(outFd,eNoReporting); xl_attr_ubyte(outFd,aErrorReport); */
     xl_ubyte(outFd,errorReport); xl_attr_ubyte(outFd,aErrorReport);
@@ -1015,8 +1015,8 @@ beginSession(int              const outFd,
 }
 
 
-             
-static void 
+
+static void
 endSession(int outFd) {
     XL_Operator(outFd,oEndSession);
 }
@@ -1046,9 +1046,9 @@ printPages(int                 const outFd,
     InputSource * sourceP;
     unsigned int sourceNum;
 
-    sourceP = firstSourceP;    
+    sourceP = firstSourceP;
 
-    openDataSource(outFd, eBinaryLowByteFirst, eDefaultSource);
+    openDataSource(outFd, eBinaryLowByteFirst, eDefaultDataSource);
 
     sourceNum = 0;   /* initial value */
 
@@ -1074,9 +1074,9 @@ printPages(int                 const outFd,
                 pm_message("Processing File %u, Page %u", sourceNum, pageNum);
 
                 pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
-                
+
                 createPclGenerator(&pam, &pclGeneratorP, colorok);
-                
+
                 convertAndPrintPage(
                     outFd, pclGeneratorP, &pam,
                     format, dpi, center, xoffs, yoffs, doDuplex, duplex,
@@ -1087,7 +1087,7 @@ printPages(int                 const outFd,
             }
         }
         pm_close(ifP);
-        sourceP = sourceP->next; 
+        sourceP = sourceP->next;
     }
     closeDataSource(outFd);
 }
@@ -1100,9 +1100,9 @@ main(int argc, char *argv[]) {
     int const outFd = STDOUT_FILENO;
 
     struct cmdlineInfo cmdline;
-    
+
     /* In case you're wondering why we do direct file descriptor I/O
-       instead of stream (FILE *), it's because Jochen originally 
+       instead of stream (FILE *), it's because Jochen originally
        wrote this code for an embedded system with diet-libc.  Without
        the stream library, the statically linked binary was only about
        5K big.
@@ -1116,7 +1116,7 @@ main(int argc, char *argv[]) {
     else {
         jobHead(outFd, cmdline.rendergray, cmdline.jobsetup);
 
-        beginSession(outFd, cmdline.dpi, cmdline.dpi, eInch, 
+        beginSession(outFd, cmdline.dpi, cmdline.dpi, eInch,
                      FALSE, eBackChAndErrPage);
 
         printPages(outFd, cmdline.sourceP,
diff --git a/converter/other/pnmtoplainpnm b/converter/other/pnmtoplainpnm
index 87c58597..4f5378b2 100755
--- a/converter/other/pnmtoplainpnm
+++ b/converter/other/pnmtoplainpnm
@@ -1,3 +1,3 @@
 #! /bin/sh
 
-pnmtopnm -plain $@
+pamtopnm -plain $@
diff --git a/converter/other/pnmtopng.c b/converter/other/pnmtopng.c
index 3899a9d2..60b8276b 100644
--- a/converter/other/pnmtopng.c
+++ b/converter/other/pnmtopng.c
@@ -26,7 +26,7 @@
    it's easy to see that an ordinary fax could deplete your virtual
    memory and even if it didn't, it might deplete your real memory and
    iterating through the array would cause thrashing.  This program
-   iterates through the image multiple times.  
+   iterates through the image multiple times.
 
    So instead, we read the image into memory one row at a time, into a
    single row buffer.  We use Netpbm's pm_openr_seekable() facility to
@@ -38,19 +38,19 @@
    in the system's I/O cache (remember that the file is a lot smaller than
    the xel array you'd get by doing a pnm_readpnm() of it).
 
-   However, it does introduce some delay because of all the system calls 
+   However, it does introduce some delay because of all the system calls
    required to read the file.  A future enhancement might read the entire
-   file into an xel array in some cases, and read one row at a time in 
+   file into an xel array in some cases, and read one row at a time in
    others, depending on the needs of the particular use.
 
    We do still read the entire alpha mask (if there is one) into a
-   'gray' array, rather than access it one row at a time.  
+   'gray' array, rather than access it one row at a time.
 
    Before May 2001, we did in fact read the whole image into an xel array,
    and we got complaints.  Before April 2000, it wasn't as big a problem
    because xels were only 24 bits.  Now they're 96.
 */
-   
+
 #ifndef PNMTOPNG_WARNING_LEVEL
 #  define PNMTOPNG_WARNING_LEVEL 0   /* use 0 for backward compatibility, */
 #endif                               /*  2 for warnings (1 == error) */
@@ -62,7 +62,7 @@
 /* Because of a design error in png.h, you must not #include <setjmp.h> before
    <png.h>.  If you do, png.h won't compile.
 */
-#include <setjmp.h> 
+#include <setjmp.h>
 #include <zlib.h>
 
 #include "pm_c_util.h"
@@ -170,7 +170,7 @@ parseSizeOpt(const char *       const sizeOpt,
              struct pngx_phys * const sizeP) {
 
     int count;
-    
+
     count = sscanf(sizeOpt, "%d %d %d", &sizeP->x, &sizeP->y, &sizeP->unit);
 
     if (count != 3)
@@ -185,7 +185,7 @@ parseRgbOpt(const char *         const rgbOpt,
             struct pngx_chroma * const rgbP) {
 
     int count;
-    
+
     count = sscanf(rgbOpt, "%f %f %f %f %f %f %f %f",
                    &rgbP->wx, &rgbP->wy,
                    &rgbP->rx, &rgbP->ry,
@@ -204,7 +204,7 @@ parseRgbOpt(const char *         const rgbOpt,
 static void
 parseSrgbintentOpt(const char *      const srgbintentOpt,
                    pngx_srgbIntent * const srgbintentP) {
-    
+
     if (streq(srgbintentOpt, "perceptual"))
         *srgbintentP = PNGX_PERCEPTUAL;
     else if (streq(srgbintentOpt, "relativecolorimetric"))
@@ -242,7 +242,7 @@ parseModtimeOpt(const char * const modtimeOpt,
     if (count != 6)
         pm_error("Invalid value for -modtime '%s'.   It should have "
                  "the form [yy]yy-mm-dd hh:mm:ss.", modtimeOpt);
-    
+
     if (year < 0)
         pm_error("Year is negative in -modtime value '%s'", modtimeOpt);
     if (year > 9999)
@@ -290,7 +290,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.
@@ -422,11 +422,11 @@ parseCommandLine(int argc, const char ** argv,
         cmdlineP->ztxt = NULL;
     if (!paletteSpec)
         cmdlineP->palette = NULL;
-    
+
     if (filterSpec && (nofilter + sub + up + avg + paeth > 0))
         pm_error("You may mot specify -filter with "
                  "-nofilter, -sub, -up, -avg, or -paeth");
-    
+
     if (filterSpec) {
         if (filter < 0 || filter > 4)
             pm_error("-filter is obsolete.  Use -nofilter, -sub, -up, -avg, "
@@ -460,13 +460,12 @@ parseCommandLine(int argc, const char ** argv,
                 cmdlineP->filterSet |= PNG_FILTER_PAETH;
         }
     }
-
     if (cmdlineP->sizeSpec)
         parseSizeOpt(size, &cmdlineP->size);
 
     if (cmdlineP->rgbSpec)
         parseRgbOpt(rgb, &cmdlineP->rgb);
-    
+
     if (cmdlineP->srgbintentSpec)
         parseSrgbintentOpt(srgbintent, &cmdlineP->srgbintent);
 
@@ -538,14 +537,14 @@ reportInputType(int    const format,
 
 
 static png_color_16
-xelToPngColor_16(xel    const input, 
-                 xelval const maxval, 
+xelToPngColor_16(xel    const input,
+                 xelval const maxval,
                  xelval const pngMaxval) {
 
     png_color_16 retval;
 
     xel scaled;
-    
+
     PPM_DEPTH(scaled, input, maxval, pngMaxval);
 
     retval.red   = PPM_GETR(scaled);
@@ -559,12 +558,12 @@ xelToPngColor_16(xel    const input,
 
 
 static void
-closestColorInPalette(pixel          const targetColor, 
+closestColorInPalette(pixel          const targetColor,
                       pixel                palette_pnm[],
                       unsigned int   const paletteSize,
                       unsigned int * const bestIndexP,
                       unsigned int * const bestMatchP) {
-    
+
     unsigned int paletteIndex;
     unsigned int bestIndex;
     unsigned int bestMatch;
@@ -573,7 +572,7 @@ closestColorInPalette(pixel          const targetColor,
 
     bestMatch = UINT_MAX;
     for (paletteIndex = 0; paletteIndex < paletteSize; ++paletteIndex) {
-        unsigned int const dist = 
+        unsigned int const dist =
             PPM_DISTANCE(palette_pnm[paletteIndex], targetColor);
 
         if (dist < bestMatch) {
@@ -678,13 +677,13 @@ static colorhist_vector getChv_chv;
 
 
 static void
-getChv(FILE *             const ifP, 
+getChv(FILE *             const ifP,
        pm_filepos         const rasterPos,
-       int                const cols, 
-       int                const rows, 
+       int                const cols,
+       int                const rows,
        xelval             const maxval,
-       int                const format, 
-       int                const maxColors, 
+       int                const format,
+       int                const maxColors,
        colorhist_vector * const chvP,
        unsigned int *     const colorsP) {
 /*----------------------------------------------------------------------------
@@ -693,7 +692,7 @@ getChv(FILE *             const ifP,
    raster starts at position 'rasterPos' of the file.  The image's properties
    are 'cols', 'rows', 'maxval', and 'format'.
 
-   Return the number of colors as *colorsP.  Return the details of the 
+   Return the number of colors as *colorsP.  Return the details of the
    colors in newly malloc'ed storage, and its address as *chvP.  If
    there are more than 'maxColors' colors, though, just return NULL as
    *chvP and leave *colorsP undefined.
@@ -708,13 +707,13 @@ getChv(FILE *             const ifP,
 
     if (!getChv_computed) {
         int colorCount;
-        if (verbose) 
+        if (verbose)
             pm_message ("Finding colors in input image...");
 
         pm_seek2(ifP, &rasterPos, sizeof(rasterPos));
-        getChv_chv = ppm_computecolorhist2(ifP, cols, rows, maxval, format, 
+        getChv_chv = ppm_computecolorhist2(ifP, cols, rows, maxval, format,
                                            maxColors, &colorCount);
-        
+
         getChv_colors = colorCount;
 
         if (verbose) {
@@ -745,7 +744,7 @@ static void freeChv(void) {
 static bool
 pgmBitsAreRepeated(unsigned int const repeatedSize,
                    FILE *       const ifP,
-                   pm_filepos   const rasterPos, 
+                   pm_filepos   const rasterPos,
                    int          const cols,
                    int          const rows,
                    xelval       const maxval,
@@ -772,7 +771,7 @@ pgmBitsAreRepeated(unsigned int const repeatedSize,
     xel * xelrow;
 
     xelrow = pnm_allocrow(cols);
-    
+
     pm_seek2(ifP, &rasterPos, sizeof(rasterPos));
 
     mayscale = TRUE;  /* initial assumption */
@@ -797,8 +796,8 @@ pgmBitsAreRepeated(unsigned int const repeatedSize,
 
 
 static void
-meaningful_bits_pgm(FILE *         const ifP, 
-                    pm_filepos     const rasterPos, 
+meaningful_bits_pgm(FILE *         const ifP,
+                    pm_filepos     const rasterPos,
                     int            const cols,
                     int            const rows,
                     xelval         const maxval,
@@ -849,8 +848,8 @@ meaningful_bits_pgm(FILE *         const ifP,
 
 
 static void
-meaningful_bits_ppm(FILE *         const ifp, 
-                    pm_filepos     const rasterPos, 
+meaningful_bits_ppm(FILE *         const ifp,
+                    pm_filepos     const rasterPos,
                     int            const cols,
                     int            const rows,
                     xelval         const maxval,
@@ -903,12 +902,12 @@ meaningful_bits_ppm(FILE *         const ifp,
 
 
 static void
-tryTransparentColor(FILE *     const ifp, 
-                    pm_filepos const rasterPos, 
-                    int        const cols, 
-                    int        const rows, 
+tryTransparentColor(FILE *     const ifp,
+                    pm_filepos const rasterPos,
+                    int        const cols,
+                    int        const rows,
                     xelval     const maxval,
-                    int        const format, 
+                    int        const format,
                     gray **    const alphaMask,
                     gray       const alphaMaxval,
                     pixel      const transcolor,
@@ -926,7 +925,7 @@ tryTransparentColor(FILE *     const ifp,
     pm_seek2(ifp, &rasterPos, sizeof(rasterPos));
 
     singleColorIsTrans = TRUE;  /* initial assumption */
-        
+
     for (row = 0; row < rows && singleColorIsTrans; ++row) {
         int col;
         pnm_readpnmrow(ifp, xelrow, cols, maxval, format);
@@ -961,7 +960,7 @@ tryTransparentColor(FILE *     const ifp,
                 }
             }
         }
-    }  
+    }
     pnm_freerow(xelrow);
 }
 
@@ -969,15 +968,15 @@ tryTransparentColor(FILE *     const ifp,
 
 static void
 analyzeAlpha(FILE *       const ifP,
-             pm_filepos   const rasterPos, 
-             unsigned int const cols, 
-             unsigned int const rows, 
+             pm_filepos   const rasterPos,
+             unsigned int const cols,
+             unsigned int const rows,
              xelval       const maxval,
-             int          const format, 
+             int          const format,
              gray **      const alphaMask,
              gray         const alphaMaxval,
              bool *       const allOpaqueP,
-             bool *       const singleColorIsTransP, 
+             bool *       const singleColorIsTransP,
              pixel *      const alphaTranscolorP) {
 /*----------------------------------------------------------------------------
   Get information about the alpha mask, in combination with the masked
@@ -1003,7 +1002,7 @@ analyzeAlpha(FILE *       const ifP,
         /* We found a pixel in the image where the alpha mask says it is
            not fully opaque.
         */
-    
+
     xelrow = pnm_allocrow(cols);
 
     {
@@ -1083,7 +1082,7 @@ determineTransparency(struct cmdlineInfo const cmdline,
    or identify a transparent color.
 
    We return as *transparentP:
-   
+
      -1 PNG is not to have single-color transparency
       1 PNG is to have single-color transparency as directed by user
       2 PNG is to have single-color transparency that effects an alpha
@@ -1107,7 +1106,7 @@ determineTransparency(struct cmdlineInfo const cmdline,
         if (alphaCols != cols || alphaRows != rows) {
             pm_error("dimensions for image and alpha mask do not agree");
         }
-        analyzeAlpha(ifP, rasterPos, cols, rows, maxval, format, 
+        analyzeAlpha(ifP, rasterPos, cols, rows, maxval, format,
                      alphaMask, alphaMaxval, &allOpaque,
                      &alphaCanBeTransparencyIndex, &alphaTranscolor);
 
@@ -1137,7 +1136,7 @@ determineTransparency(struct cmdlineInfo const cmdline,
         *alphaMaxvalP = 255;
 
         if (cmdline.transparent) {
-            const char * transstring2;  
+            const char * transstring2;
             /* The -transparent value, but with possible leading '=' removed */
             if (cmdline.transparent[0] == '=') {
                 *transExactP = TRUE;
@@ -1145,12 +1144,12 @@ determineTransparency(struct cmdlineInfo const cmdline,
             } else {
                 *transExactP = FALSE;
                 transstring2 = cmdline.transparent;
-            }  
+            }
             /* We do this funny PPM_DEPTH thing instead of just passing 'maxval'
                to ppm_parsecolor() because ppm_parsecolor() does a cheap maxval
                scaling, and this is more precise.
             */
-            PPM_DEPTH(*transColorP, 
+            PPM_DEPTH(*transColorP,
                       ppm_parsecolor(transstring2, PNM_OVERALLMAXVAL),
                       PNM_OVERALLMAXVAL, maxval);
 
@@ -1167,9 +1166,9 @@ determineBackground(struct cmdlineInfo const cmdline,
                     xelval             const maxval,
                     xel *              const backColorP) {
 
-  if (cmdline.background) 
+  if (cmdline.background)
       PPM_DEPTH(*backColorP,
-                ppm_parsecolor(cmdline.background, PNM_OVERALLMAXVAL), 
+                ppm_parsecolor(cmdline.background, PNM_OVERALLMAXVAL),
                 PNM_OVERALLMAXVAL, maxval);;
 }
 
@@ -1221,8 +1220,8 @@ hasColor(FILE *       const ifP,
 
 
 static void
-findRedundantBits(FILE *         const ifp, 
-                  int            const rasterPos, 
+findRedundantBits(FILE *         const ifp,
+                  int            const rasterPos,
                   int            const cols,
                   int            const rows,
                   xelval         const maxval,
@@ -1239,13 +1238,13 @@ findRedundantBits(FILE *         const ifp,
    of bits, starting from the least significant end, that contain
    original information.
 -----------------------------------------------------------------------------*/
-  if (!alpha && PNM_FORMAT_TYPE(format) == PGM_TYPE && !force) 
+  if (!alpha && PNM_FORMAT_TYPE(format) == PGM_TYPE && !force)
       meaningful_bits_pgm(ifp, rasterPos, cols, rows, maxval, format,
                           meaningfulBitsP);
   else if (PNM_FORMAT_TYPE(format) == PPM_TYPE && !force)
       meaningful_bits_ppm(ifp, rasterPos, cols, rows, maxval, format,
                           meaningfulBitsP);
-  else 
+  else
       *meaningfulBitsP = pm_maxvaltobits(maxval);
 
   if (verbose && *meaningfulBitsP != pm_maxvaltobits(maxval))
@@ -1257,24 +1256,24 @@ findRedundantBits(FILE *         const ifp,
 
 static void
 readOrderedPalette(FILE *         const pfp,
-                   xel                  ordered_palette[], 
+                   xel                  ordered_palette[],
                    unsigned int * const ordered_palette_size_p) {
 
     xel ** xels;
     int cols, rows;
     xelval maxval;
     int format;
-    
+
     if (verbose)
         pm_message("reading ordered palette (colormap)...");
 
     xels = pnm_readpnm(pfp, &cols, &rows, &maxval, &format);
-    
-    if (PNM_FORMAT_TYPE(format) != PPM_TYPE) 
+
+    if (PNM_FORMAT_TYPE(format) != PPM_TYPE)
         pm_error("ordered palette must be a PPM file, not type %d", format);
 
     *ordered_palette_size_p = rows * cols;
-    if (*ordered_palette_size_p > MAXCOLORS) 
+    if (*ordered_palette_size_p > MAXCOLORS)
         pm_error("ordered-palette image contains %d pixels.  Maximum is %d",
                  *ordered_palette_size_p, MAXCOLORS);
     if (verbose)
@@ -1286,12 +1285,12 @@ readOrderedPalette(FILE *         const pfp,
         j = 0;  /* initial value */
         for (row = 0; row < rows; ++row) {
             int col;
-            for (col = 0; col < cols; ++col) 
+            for (col = 0; col < cols; ++col)
                 ordered_palette[j++] = xels[row][col];
         }
     }
     pnm_freearray(xels, rows);
-}        
+}
 
 
 
@@ -1315,31 +1314,31 @@ compute_nonalpha_palette(colorhist_vector const chv,
    wants the colors in a particular order in the palette.
 -----------------------------------------------------------------------------*/
     unsigned int colorIndex;
-    
+
     xel ordered_palette[MAXCOLORS];
     unsigned int ordered_palette_size;
 
     if (pfp) {
         readOrderedPalette(pfp, ordered_palette, &ordered_palette_size);
 
-        if (colors != ordered_palette_size) 
+        if (colors != ordered_palette_size)
             pm_error("sizes of ordered palette (%d) "
                      "and existing palette (%d) differ",
                      ordered_palette_size, colors);
-        
+
         /* Make sure the ordered palette contains all the colors in
-           the image 
+           the image
         */
         for (colorIndex = 0; colorIndex < colors; colorIndex++) {
             int j;
             bool found;
-            
+
             found = FALSE;
             for (j = 0; j < ordered_palette_size && !found; ++j) {
-                if (PNM_EQUAL(ordered_palette[j], chv[colorIndex].color)) 
+                if (PNM_EQUAL(ordered_palette[j], chv[colorIndex].color))
                     found = TRUE;
             }
-            if (!found) 
+            if (!found)
                 pm_error("failed to find color (%d, %d, %d), which is in the "
                          "input image, in the ordered palette",
                          PPM_GETR(chv[colorIndex].color),
@@ -1352,7 +1351,7 @@ compute_nonalpha_palette(colorhist_vector const chv,
         for (colorIndex = 0; colorIndex < colors; ++colorIndex)
             palette_pnm[colorIndex] = ordered_palette[colorIndex];
     } else {
-        for (colorIndex = 0; colorIndex < colors; ++colorIndex) 
+        for (colorIndex = 0; colorIndex < colors; ++colorIndex)
             palette_pnm[colorIndex] = chv[colorIndex].color;
     }
     *paletteSizeP = colors;
@@ -1442,7 +1441,7 @@ computeUnsortedAlphaPalette(FILE *           const ifP,
    described by 'cols', 'rows', 'maxval', and 'format'.
 
    Using the alpha mask 'alpha_mask' and color map 'chv' (of size 'colors')
-   for the image, construct a palette of (color index, alpha) ordered pairs 
+   for the image, construct a palette of (color index, alpha) ordered pairs
    for the image, as follows.
 
    The alpha/color palette is the set of all ordered pairs of
@@ -1476,7 +1475,7 @@ computeUnsortedAlphaPalette(FILE *           const ifP,
         alphasOfColor[colorIndex] = NULL;
         alphasOfColorCnt[colorIndex] = 0;
     }
- 
+
     pm_seek2(ifP, &rasterPos, sizeof(rasterPos));
 
     xelrow = pnm_allocrow(cols);
@@ -1562,22 +1561,22 @@ sortAlphaPalette(gray *         const alphasOfColor[],
         unsigned int bot_idx;
         unsigned int top_idx;
         unsigned int colorIndex;
-    
+
         /* We start one index at the bottom of the palette index range
            and another at the top.  We run through the unsorted palette,
            and when we see an opaque entry, we map it to the current top
-           cursor and bump it down.  When we see a non-opaque entry, we map 
+           cursor and bump it down.  When we see a non-opaque entry, we map
            it to the current bottom cursor and bump it up.  Because the input
            and output palettes are the same size, the two cursors should meet
            right when we process the last entry of the unsorted palette.
-        */    
+        */
         bot_idx = 0;
         top_idx = alphasFirstIndex[colors-1] + alphasOfColorCnt[colors-1] - 1;
-    
+
         for (colorIndex = 0;  colorIndex < colors;  ++colorIndex) {
             unsigned int j;
             for (j = 0; j < alphasOfColorCnt[colorIndex]; ++j) {
-                unsigned int const paletteIndex = 
+                unsigned int const paletteIndex =
                     alphasFirstIndex[colorIndex] + j;
                 if (alphasOfColor[colorIndex][j] == alphaMaxval)
                     mapping[paletteIndex] = top_idx--;
@@ -1598,7 +1597,7 @@ sortAlphaPalette(gray *         const alphasOfColor[],
 
 
 static void
-compute_alpha_palette(FILE *         const ifP, 
+compute_alpha_palette(FILE *         const ifP,
                       int            const cols,
                       int            const rows,
                       xelval         const maxval,
@@ -1626,10 +1625,10 @@ compute_alpha_palette(FILE *         const ifP,
    The palette is sorted so that the opaque entries are last, and we return
    *transSizeP as the number of non-opaque entries.
 
-   palette[] and trans[] are allocated by the caller to at least 
+   palette[] and trans[] are allocated by the caller to at least
    MAXPALETTEENTRIES elements.
 
-   If there are more than MAXPALETTEENTRIES color/alpha pairs in the image, 
+   If there are more than MAXPALETTEENTRIES color/alpha pairs in the image,
    don't return any palette information -- just return *tooBigP == TRUE.
 -----------------------------------------------------------------------------*/
     colorhist_vector chv;
@@ -1638,8 +1637,8 @@ compute_alpha_palette(FILE *         const ifP,
     gray * alphas_of_color[MAXPALETTEENTRIES];
     unsigned int alphas_first_index[MAXPALETTEENTRIES];
     unsigned int alphas_of_color_cnt[MAXPALETTEENTRIES];
- 
-    getChv(ifP, rasterPos, cols, rows, maxval, format, MAXCOLORS, 
+
+    getChv(ifP, rasterPos, cols, rows, maxval, format, MAXCOLORS,
            &chv, &colors);
 
     assert(colors <= ARRAY_SIZE(alphas_of_color));
@@ -1658,7 +1657,7 @@ compute_alpha_palette(FILE *         const ifP,
                index into the sorted PNG palette of the alpha/color
                pair whose index is x in the unsorted PNG palette.
                This mapping sorts the palette so that opaque entries
-               are last.  
+               are last.
             */
 
         *paletteSizeP = colors == 0 ?
@@ -1677,22 +1676,22 @@ compute_alpha_palette(FILE *         const ifP,
             for (colorIndex = 0; colorIndex < colors; ++colorIndex) {
                 unsigned int j;
                 for (j = 0; j < alphas_of_color_cnt[colorIndex]; ++j) {
-                    unsigned int const paletteIndex = 
+                    unsigned int const paletteIndex =
                         alphas_first_index[colorIndex] + j;
                     palette_pnm[mapping[paletteIndex]] = chv[colorIndex].color;
-                    trans_pnm[mapping[paletteIndex]] = 
+                    trans_pnm[mapping[paletteIndex]] =
                     alphas_of_color[colorIndex][j];
                 }
             }
         }
         freeAlphasOfColor(alphas_of_color, colors);
     }
-} 
+}
 
 
 
 static void
-makeOneColorTransparentInPalette(xel            const transColor, 
+makeOneColorTransparentInPalette(xel            const transColor,
                                  bool           const exact,
                                  pixel                palette_pnm[],
                                  unsigned int   const paletteSize,
@@ -1700,12 +1699,12 @@ makeOneColorTransparentInPalette(xel            const transColor,
                                  unsigned int * const transSizeP) {
 /*----------------------------------------------------------------------------
    Find the color 'transColor' in the color/alpha palette defined by
-   palette_pnm[], paletteSize, trans_pnm[] and *transSizeP.  
+   palette_pnm[], paletteSize, trans_pnm[] and *transSizeP.
 
    Make that entry fully transparent.
 
    Rearrange the palette so that that entry is first.  (The PNG compressor
-   can do a better job when the opaque entries are all last in the 
+   can do a better job when the opaque entries are all last in the
    color/alpha palette).
 
    If the specified color is not there and exact == TRUE, return
@@ -1720,26 +1719,26 @@ makeOneColorTransparentInPalette(xel            const transColor,
     unsigned int distance;
 
     assert(paletteSize > 0);
-    
+
     if (*transSizeP != 0)
         pm_error("Internal error: trying to make a color in the palette "
                  "transparent where there already is one.");
 
-    closestColorInPalette(transColor, palette_pnm, paletteSize, 
+    closestColorInPalette(transColor, palette_pnm, paletteSize,
                           &transparentIndex, &distance);
 
     if (distance != 0 && exact) {
         pm_message("specified transparent color not present in palette; "
                    "ignoring -transparent");
         errorlevel = PNMTOPNG_WARNING_LEVEL;
-    } else {        
+    } else {
         /* Swap this with the first entry in the palette */
         pixel tmp;
-    
+
         tmp = palette_pnm[transparentIndex];
         palette_pnm[transparentIndex] = palette_pnm[0];
         palette_pnm[0] = tmp;
-        
+
         /* Make it transparent */
         trans_pnm[0] = PGM_TRANSPARENT;
         *transSizeP = 1;
@@ -1755,8 +1754,8 @@ makeOneColorTransparentInPalette(xel            const transColor,
 
 
 static void
-findOrAddBackgroundInPalette(pixel          const backColor, 
-                             pixel                palette_pnm[], 
+findOrAddBackgroundInPalette(pixel          const backColor,
+                             pixel                palette_pnm[],
                              unsigned int * const paletteSizeP,
                              unsigned int * const backgroundIndexP) {
 /*----------------------------------------------------------------------------
@@ -1765,11 +1764,11 @@ findOrAddBackgroundInPalette(pixel          const backColor,
   add it, choose a background color that's already in the palette,
   as close to 'backColor' as possible.
 
-  If we add an entry to the palette, make it opaque.  But in searching the 
+  If we add an entry to the palette, make it opaque.  But in searching the
   existing palette, ignore transparency.
 
   Note that PNG specs say that transparency of the background is meaningless;
-  i.e. a viewer must ignore the transparency of the palette entry when 
+  i.e. a viewer must ignore the transparency of the palette entry when
   using the background color.
 
   Return the palette index of the background color as *backgroundIndexP.
@@ -1778,9 +1777,9 @@ findOrAddBackgroundInPalette(pixel          const backColor,
     unsigned int paletteIndex;
 
     backgroundIndex = -1;
-    for (paletteIndex = 0; 
-         paletteIndex < *paletteSizeP; 
-         ++paletteIndex) 
+    for (paletteIndex = 0;
+         paletteIndex < *paletteSizeP;
+         ++paletteIndex)
         if (PPM_EQUAL(palette_pnm[paletteIndex], backColor))
             backgroundIndex = paletteIndex;
 
@@ -1823,8 +1822,8 @@ findOrAddBackgroundInPalette(pixel          const backColor,
 
 
 
-static void 
-buildColorLookup(pixel                   palette_pnm[], 
+static void
+buildColorLookup(pixel                   palette_pnm[],
                  unsigned int      const paletteSize,
                  colorhash_table * const chtP) {
 /*----------------------------------------------------------------------------
@@ -1849,14 +1848,14 @@ buildColorLookup(pixel                   palette_pnm[],
 
 
 
-static void 
-buildColorAlphaLookup(pixel              palette_pnm[], 
+static void
+buildColorAlphaLookup(pixel              palettePnm[],
                       unsigned int const paletteSize,
-                      gray               trans_pnm[], 
+                      gray               transPnm[],
                       unsigned int const transSize,
                       gray         const alphaMaxval,
                       coloralphahash_table * const cahtP) {
-    
+
     coloralphahash_table const caht = alloccoloralphahash();
 
     unsigned int paletteIndex;
@@ -1865,12 +1864,12 @@ buildColorAlphaLookup(pixel              palette_pnm[],
         gray paletteTrans;
 
         if (paletteIndex < transSize)
-            paletteTrans = alphaMaxval;
+            paletteTrans = transPnm[paletteIndex];
         else
-            paletteTrans = trans_pnm[paletteIndex];
+            paletteTrans = alphaMaxval;
 
-        addtocoloralphahash(caht, &palette_pnm[paletteIndex],
-                            &trans_pnm[paletteIndex], paletteIndex);
+        addtocoloralphahash(caht, &palettePnm[paletteIndex],
+                            &paletteTrans, paletteIndex);
     }
     *cahtP = caht;
 }
@@ -1905,18 +1904,18 @@ tryAlphaPalette(FILE *         const ifP,
                  "a PNG with transparency when you specify "
                  "the palette with -palette.");
 
-    compute_alpha_palette(ifP, cols, rows, maxval, format, 
+    compute_alpha_palette(ifP, cols, rows, maxval, format,
                           rasterPos,  alpha_mask, alphaMaxval,
-                          palette_pnm, trans_pnm, 
+                          palette_pnm, trans_pnm,
                           paletteSizeP, transSizeP, &tooBig);
     if (tooBig) {
         pm_asprintf(impossibleReasonP,
                     "too many color/transparency pairs "
-                    "(more than the PNG maximum of %u", 
+                    "(more than the PNG maximum of %u",
                     MAXPALETTEENTRIES);
     } else
         *impossibleReasonP = NULL;
-} 
+}
 
 
 
@@ -1930,15 +1929,15 @@ computePixelWidth(bool           const colorPng,
     unsigned int bitsPerSample, bitsPerPixel;
 
     if (colorPng || alpha) {
-        /* PNG allows only depths of 8 and 16 for a truecolor image 
+        /* PNG allows only depths of 8 and 16 for a truecolor image
            and for a grayscale image with an alpha channel.
           */
         if (pnmMeaningfulBitCt > 8)
             bitsPerSample = 16;
-        else 
+        else
             bitsPerSample = 8;
     } else {
-        /* A grayscale, non-colormapped, no-alpha PNG may have any 
+        /* A grayscale, non-colormapped, no-alpha PNG may have any
              bit depth from 1 to 16
           */
         if (pnmMeaningfulBitCt > 8)
@@ -1977,7 +1976,7 @@ paletteIndexBits(unsigned int const nColors) {
   Return the number of bits that a palette index in the PNG will
   occupy given that the palette has 'nColors' colors in it.  It is 1,
   2, 4, or 8 bits.
-  
+
   If 'nColors' is not a valid PNG palette size, return 0.
 -----------------------------------------------------------------------------*/
     unsigned int retval;
@@ -2056,7 +2055,7 @@ computeColorMap(FILE *         const ifP,
         pm_asprintf(noColormapReasonP, "You requested no color map");
     else if (maxval > PALETTEMAXVAL)
         pm_asprintf(noColormapReasonP, "The maxval of the input image (%u) "
-                    "exceeds the PNG palette maxval (%u)", 
+                    "exceeds the PNG palette maxval (%u)",
                     maxval, PALETTEMAXVAL);
     else {
         unsigned int bitsPerPixel;
@@ -2076,20 +2075,20 @@ computeColorMap(FILE *         const ifP,
             */
             colorhist_vector chv;
             unsigned int colors;
-            
-            getChv(ifP, rasterPos, cols, rows, maxval, format, MAXCOLORS, 
+
+            getChv(ifP, rasterPos, cols, rows, maxval, format, MAXCOLORS,
                    &chv, &colors);
 
             if (chv == NULL) {
-                pm_asprintf(noColormapReasonP, 
+                pm_asprintf(noColormapReasonP,
                             "More than %u colors found -- too many for a "
                             "colormapped PNG", MAXCOLORS);
             } else {
                 /* There are few enough colors that a palette is possible */
                 if (bitsPerPixel <= paletteIndexBits(colors) && !pfP)
-                    pm_asprintf(noColormapReasonP, 
+                    pm_asprintf(noColormapReasonP,
                                 "palette index for %u colors would be "
-                                "no smaller than the indexed value (%u bits)", 
+                                "no smaller than the indexed value (%u bits)",
                                 colors, bitsPerPixel);
                 else {
                     unsigned int paletteSize;
@@ -2098,7 +2097,7 @@ computeColorMap(FILE *         const ifP,
                         tryAlphaPalette(ifP, cols, rows, maxval, format,
                                         rasterPos, alpha_mask, alphaMaxval,
                                         pfP,
-                                        palette_pnm, &paletteSize, 
+                                        palette_pnm, &paletteSize,
                                         trans_pnm, &transSize,
                                         noColormapReasonP);
 
@@ -2106,13 +2105,13 @@ computeColorMap(FILE *         const ifP,
                         *noColormapReasonP = NULL;
 
                         compute_nonalpha_palette(chv, colors, maxval, pfP,
-                                                 palette_pnm, &paletteSize, 
+                                                 palette_pnm, &paletteSize,
                                                  trans_pnm, &transSize);
-    
+
                         if (transparent)
                             makeOneColorTransparentInPalette(
-                                transcolor, transexact, 
-                                palette_pnm, paletteSize, trans_pnm, 
+                                transcolor, transexact,
+                                palette_pnm, paletteSize, trans_pnm,
                                 &transSize);
                     }
                     if (!*noColormapReasonP) {
@@ -2145,7 +2144,7 @@ static void computeColorMapLookupTable(
 /*----------------------------------------------------------------------------
    Compute applicable lookup tables for the palette index.  If there's no
    alpha mask, this is just a standard Netpbm colorhash_table.  If there's
-   an alpha mask, it is the slower Pnmtopng-specific 
+   an alpha mask, it is the slower Pnmtopng-specific
    coloralphahash_table.
 
    If a lookup table is not applicable to the image, return NULL as
@@ -2153,10 +2152,10 @@ static void computeColorMapLookupTable(
 -----------------------------------------------------------------------------*/
     if (colorMapped) {
         if (alpha) {
-            buildColorAlphaLookup(palette_pnm, palette_size, 
+            buildColorAlphaLookup(palette_pnm, palette_size,
                                   trans_pnm, trans_size, alpha_maxval, cahtP);
             *chtP = NULL;
-        } else { 
+        } else {
             buildColorLookup(palette_pnm, palette_size, chtP);
             *cahtP = NULL;
         }
@@ -2203,15 +2202,15 @@ computeRasterWidth(bool           const colorMapped,
                           bitsPerSampleP, bitsPerPixelP);
 
         if (verbose)
-            pm_message("Writing %u bits per component per pixel", 
+            pm_message("Writing %u bits per component per pixel",
                        *bitsPerSampleP);
     }
 }
 
 
 static void
-createPngPalette(pixel              palette_pnm[], 
-                 unsigned int const paletteSize, 
+createPngPalette(pixel              palette_pnm[],
+                 unsigned int const paletteSize,
                  pixval       const maxval,
                  gray               trans_pnm[],
                  unsigned int const transSize,
@@ -2269,7 +2268,7 @@ setZlibCompression(struct pngx *          const pngxP,
         pngx_setCompressionSize(pngxP, zlibCompression.buffer_size);
     }
 }
-                  
+
 
 
 static void
@@ -2284,7 +2283,7 @@ makePngLine(png_byte *           const line,
             struct pngx *        const pngxP,
             xelval               const png_maxval,
             unsigned int         const depth) {
-            
+
     unsigned int col;
     png_byte *pp;
 
@@ -2318,7 +2317,7 @@ makePngLine(png_byte *           const line,
             *pp++ = PPM_GETB(p_png) & 0xff;
         } else
             pm_error("INTERNAL ERROR: undefined color_type");
-                
+
         if (pngx_colorType(pngxP) & PNG_COLOR_MASK_ALPHA) {
             int const png_alphaval = (int)
                 alpha_mask[col] * (float) png_maxval / maxval + 0.5;
@@ -2372,7 +2371,7 @@ writeRaster(struct pngx *        const pngxP,
             pnm_readpnmrow(ifP, xelrow, cols, maxval, format);
             pnm_promoteformatrow(xelrow, cols, maxval, format, maxval,
                                  PPM_TYPE);
-            
+
             makePngLine(line, xelrow, cols, maxval,
                         alpha, alpha ? alpha_mask[row] : NULL,
                         cht, caht, pngxP, png_maxval, depth);
@@ -2401,15 +2400,15 @@ doHistChunk(struct pngx * const pngxP,
         colorhist_vector chv;
         unsigned int colorCt;
         colorhash_table cht;
-        
-        getChv(ifP, rasterPos, cols, rows, maxval, format, MAXCOLORS, 
+
+        getChv(ifP, rasterPos, cols, rows, maxval, format, MAXCOLORS,
                &chv, &colorCt);
 
         cht = ppm_colorhisttocolorhash(chv, colorCt);
-                
-        { 
+
+        {
             png_uint_16 * histogram;  /* malloc'ed */
-        
+
             MALLOCARRAY(histogram, MAXCOLORS);
 
             if (!histogram)
@@ -2424,7 +2423,7 @@ doHistChunk(struct pngx * const pngxP,
                     else
                         histogram[i] = chv[chvIndex].value;
                 }
-            
+
                 pngx_setHist(pngxP, histogram);
 
                 if (verbose)
@@ -2471,7 +2470,7 @@ doIhdrChunk(struct pngx * const pngxP,
 static void
 doGamaChunk(struct cmdlineInfo const cmdline,
             struct pngx *      const pngxP) {
-            
+
     if (cmdline.gammaSpec)
         pngx_setGama(pngxP, cmdline.gamma);
 }
@@ -2541,7 +2540,7 @@ reportTrans(struct pngx * const pngxP) {
         struct pngx_trns const transInfo = pngx_trns(pngxP);
 
         pm_message("%u transparency values", transInfo.numTrans);
-        
+
         pm_message("Transparent color {gray, red, green, blue} = "
                    "{%d, %d, %d, %d}",
                    transInfo.transColor.gray,
@@ -2594,21 +2593,21 @@ doBkgdChunk(struct pngx * const pngxP,
             xelval        const maxval,
             xelval        const pngMaxval,
             bool          const verbose) {
-    
+
     if (bkgdRequested) {
         if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE)
             pngx_setBkgdPalette(pngxP, backgroundIndex);
         else {
-            png_color_16 const pngBackground = 
+            png_color_16 const pngBackground =
                 xelToPngColor_16(backColor, maxval, pngMaxval);
             pngx_setBkgdRgb(pngxP, pngBackground);
             if (verbose)
                 pm_message("Writing bKGD chunk with background color "
                            " {gray, red, green, blue} = {%d, %d, %d, %d}",
-                           pngBackground.gray, 
-                           pngBackground.red, 
-                           pngBackground.green, 
-                           pngBackground.blue ); 
+                           pngBackground.gray,
+                           pngBackground.red,
+                           pngBackground.green,
+                           pngBackground.blue );
         }
     }
 }
@@ -2625,20 +2624,19 @@ doSbitChunk(struct pngx * const pngxP,
     if (pngx_colorType(pngxP) != PNG_COLOR_TYPE_PALETTE &&
         (pngMaxval > maxval || (alpha && pngMaxval > alphaMaxval))) {
 
-        /* We're writing in a bit depth that doesn't match the maxval
-           of the input image and the alpha mask.  So we write an sBIT
-           chunk to tell what the original image's maxval was.  The
-           sBit chunk doesn't let us specify any maxval -- only powers
-           of two minus one.  So we pick the power of two minus one
-           which is greater than or equal to the actual input maxval.
-           
-           PNG also doesn't let an sBIT chunk indicate a maxval
-           _greater_ than the the PNG maxval.  The designers probably
-           did not conceive of the case where that would happen.  The
-           case is this: We detected redundancy in the bits so were
-           able to store fewer bits than the user provided.  But since
-           PNG doesn't allow it, we don't attempt to create such an
-           sBIT chunk.
+        /* We're writing in a bit depth that doesn't match the maxval of the
+           input image and the alpha mask.  So we write an sBIT chunk to tell
+           what the original image's maxval was.  The sBit chunk doesn't let
+           us specify any maxval -- only powers of two minus one.  So we pick
+           the power of two minus one which is greater than or equal to the
+           actual input maxval.
+
+           PNG also doesn't let an sBIT chunk indicate a maxval _greater_ than
+           the PNG maxval.  The designers probably did not conceive of the
+           case where that would happen.  The case is this: We detected
+           redundancy in the bits so were able to store fewer bits than the
+           user provided.  But since PNG doesn't allow it, we don't attempt to
+           create such an sBIT chunk.
         */
 
         {
@@ -2652,7 +2650,7 @@ doSbitChunk(struct pngx * const pngxP,
                 sbit.blue  = sbitval;
             } else
                 sbit.gray  = sbitval;
-            
+
             if (verbose)
                 pm_message("Writing sBIT chunk with bits = %d", sbitval);
 
@@ -2683,7 +2681,7 @@ addSrgbChunk(struct pngx *   const pngxP,
 
 
 
-static void 
+static void
 convertpnm(struct cmdlineInfo const cmdline,
            FILE *             const ifP,
            FILE *             const ofP,
@@ -2702,14 +2700,14 @@ convertpnm(struct cmdlineInfo const cmdline,
     xelval maxval;
     /* The maxval of the input image */
     xelval pngMaxval;
-        /* The maxval of the samples in the PNG output 
+        /* The maxval of the samples in the PNG output
            (must be 1, 3, 7, 15, 255, or 65535)
         */
     pixel transcolor;
         /* The color that is to be transparent, with maxval equal to that
            of the input image.
         */
-    bool transExact;  
+    bool transExact;
         /* boolean: the user wants only the exact color he specified to be
            transparent; not just something close to it.
         */
@@ -2728,14 +2726,14 @@ convertpnm(struct cmdlineInfo const cmdline,
     pixel palettePnm[MAXCOLORS];
     png_color palette[MAXCOLORS];
         /* The color part of the color/alpha palette passed to the PNG
-           compressor 
+           compressor
         */
     unsigned int paletteSize;
 
     gray transPnm[MAXCOLORS];
     png_byte  trans[MAXCOLORS];
         /* The alpha part of the color/alpha palette passed to the PNG
-           compressor 
+           compressor
         */
     unsigned int transSize;
 
@@ -2751,7 +2749,7 @@ convertpnm(struct cmdlineInfo const cmdline,
            we should.  malloc'ed null-terminated string.
         */
     unsigned int depth;
-        /* The number of bits per sample in the (uncompressed) png 
+        /* The number of bits per sample in the (uncompressed) png
            raster -- if the raster contains palette indices, this is the
            number of bits in the index.
         */
@@ -2759,7 +2757,7 @@ convertpnm(struct cmdlineInfo const cmdline,
         /* The total number of bits per pixel in the (uncompressed) png
            raster, including all channels.
         */
-    pm_filepos rasterPos;  
+    pm_filepos rasterPos;
         /* file position in input image file of start of image (i.e. after
            the header)
         */
@@ -2800,7 +2798,7 @@ convertpnm(struct cmdlineInfo const cmdline,
         colorPng = (PNM_FORMAT_TYPE(format) == PPM_TYPE);
     else {
         if (PNM_FORMAT_TYPE(format) == PPM_TYPE) {
-            colorPng = hasColor(ifP, cols, rows, maxval, format, rasterPos); 
+            colorPng = hasColor(ifP, cols, rows, maxval, format, rasterPos);
         } else
             colorPng = false;
     }
@@ -2815,10 +2813,10 @@ convertpnm(struct cmdlineInfo const cmdline,
 
     findRedundantBits(ifP, rasterPos, cols, rows, maxval, format, alpha,
                       cmdline.force, &pnmMeaningfulBitCt);
-  
+
     computeColorMap(ifP, rasterPos, cols, rows, maxval, colorPng, format,
                     cmdline.force, pfP,
-                    alpha, transparent >= 0, transcolor, transExact, 
+                    alpha, transparent >= 0, transcolor, transExact,
                     !!cmdline.background, backColor,
                     alpha_mask, alphaMaxval, pnmMeaningfulBitCt,
                     palettePnm, &paletteSize, transPnm, &transSize,
@@ -2835,7 +2833,7 @@ convertpnm(struct cmdlineInfo const cmdline,
         colorMapped = FALSE;
     } else
         colorMapped = TRUE;
-  
+
     computeColorMapLookupTable(colorMapped, palettePnm, paletteSize,
                                transPnm, transSize, alpha, alphaMaxval,
                                &cht, &caht);
@@ -2846,7 +2844,7 @@ convertpnm(struct cmdlineInfo const cmdline,
     if (verbose)
         pm_message ("writing a%s %d-bit %s%s file%s",
                     fulldepth == 8 ? "n" : "", fulldepth,
-                    colorMapped ? "palette": 
+                    colorMapped ? "palette":
                     colorPng ? "RGB" : "gray",
                     alpha ? (colorMapped ? "+transparency" : "+alpha") : "",
                     cmdline.interlace ? " (interlaced)" : "");
@@ -2873,7 +2871,7 @@ convertpnm(struct cmdlineInfo const cmdline,
         /* creating PNG palette (Not counting the transparency palette) */
 
         createPngPalette(palettePnm, paletteSize, maxval,
-                         transPnm, transSize, alphaMaxval, 
+                         transPnm, transSize, alphaMaxval,
                          palette, trans);
         pngx_setPlte(pngxP, palette, paletteSize);
 
@@ -2973,7 +2971,7 @@ displayVersion() {
 
 
 
-int 
+int
 main(int argc, const char * argv[]) {
 
     struct cmdlineInfo cmdline;
@@ -2983,29 +2981,29 @@ main(int argc, const char * argv[]) {
     FILE * tfP;
 
     int errorlevel;
-    
+
     pm_proginit(&argc, argv);
-    
+
     parseCommandLine(argc, argv, &cmdline);
-    
+
     if (cmdline.libversion) {
         displayVersion();
         return 0;
     }
     verbose = cmdline.verbose;
-    
+
     ifP = pm_openr_seekable(cmdline.inputFileName);
-    
+
     if (cmdline.alpha)
         afP = pm_openr(cmdline.alpha);
     else
         afP = NULL;
-    
+
     if (cmdline.palette)
         pfP = pm_openr(cmdline.palette);
     else
         pfP = NULL;
-    
+
     if (cmdline.text)
         tfP = pm_openr(cmdline.text);
     else if (cmdline.ztxt)
@@ -3014,7 +3012,7 @@ main(int argc, const char * argv[]) {
         tfP = NULL;
 
     convertpnm(cmdline, ifP, stdout, afP, pfP, tfP, &errorlevel);
-    
+
     if (afP)
         pm_close(afP);
     if (pfP)
diff --git a/converter/other/pnmtops.c b/converter/other/pnmtops.c
index c1dadc3e..de0dfd8d 100644
--- a/converter/other/pnmtops.c
+++ b/converter/other/pnmtops.c
@@ -33,6 +33,7 @@
    goes in separate from the rest of the raster.
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE  /* Make sure string.h contains strdup() */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #include <stdlib.h>
@@ -86,7 +87,7 @@ setSignals() {
 
 
 
-struct cmdlineInfo {
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
@@ -208,7 +209,7 @@ validateCompDimension(unsigned int const value,
 
 static void
 parseCommandLine(int argc, const char ** argv,
-                 struct cmdlineInfo * const cmdlineP) {
+                 struct CmdlineInfo * const cmdlineP) {
 
     unsigned int imagewidthSpec, imageheightSpec;
     float imagewidth, imageheight;
@@ -2034,7 +2035,7 @@ main(int argc, const char * argv[]) {
 
     FILE * ifP;
     const char * name;  /* malloc'ed */
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
 
     pm_proginit(&argc, argv);
 
diff --git a/converter/other/pnmtosir.c b/converter/other/pnmtosir.c
index c8dec5b6..20bb6178 100644
--- a/converter/other/pnmtosir.c
+++ b/converter/other/pnmtosir.c
@@ -1,5 +1,5 @@
 /* pnmtosir.c - read a portable anymap and produce a Solitaire Image Recorder
-**		file (MGI TYPE 11 or MGI TYPE 17)
+**      file (MGI TYPE 11 or MGI TYPE 17)
 **
 ** Copyright (C) 1991 by Marvin Landis
 **
@@ -11,67 +11,63 @@
 ** implied warranty.
 */
 
+#include <stdbool.h>
 #include "pnm.h"
 
 #define MAXCOLORS 256
 
-int main(int argc, char * argv[]) {
-    FILE* ifp;
-    xel** xels;
-    register xel* xP;
-    const char* dumpname;
-    int rows, cols, format, row, col;
-    int m, n;
-    int grayscale;
+
+
+int
+main(int argc, const char * argv[]) {
+    
+    FILE * ifP;
+    xel ** xels;
+    int rows, cols, format;
+    unsigned int n;
+    bool isGrayscale;
     xelval maxval;
-    const char* const usage = "[pnmfile]";
-    unsigned char ub;
     unsigned short Header[16];
     unsigned short LutHeader[16];
     unsigned short Lut[2048];
 
-    pnm_init( &argc, argv );
+    pm_proginit(&argc, argv);
 
-    if ( argc > 2 )
-        pm_usage( usage );
+    if (argc-1 > 1)
+        pm_error("There is only one possible argument: the input file.  "
+                 "You specified %d", argc-1);
 
-    if ( argc == 2 )
-	{
-        dumpname = argv[1];
-        ifp = pm_openr( argv[1] );
-	}
-    else
-	{
-        dumpname = "Standard Input";
-        ifp = stdin;
-	}
+    if (argc-1 > 0) {
+        const char * const inputFileName = argv[1];
+        ifP = pm_openr(inputFileName);
+    }  else {
+        ifP = stdin;
+    }
     
-    xels = pnm_readpnm( ifp, &cols, &rows, &maxval, &format );
-    pm_close( ifp );
+    xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format);
     
     /* Figure out the colormap. */
-    switch ( PNM_FORMAT_TYPE(format) )
-	{
-	case PPM_TYPE:
-        grayscale = 0;
-        pm_message( "Writing a 24-bit SIR format (MGI TYPE 11)" );
+    switch (PNM_FORMAT_TYPE(format) ) {
+    case PPM_TYPE:
+        isGrayscale = false;
+        pm_message("Writing a 24-bit SIR format (MGI TYPE 11)");
         break;
 
     case PGM_TYPE:
-        grayscale = 1;
-        pm_message( "Writing a grayscale SIR format (MGI TYPE 17)" );
+        isGrayscale = true;
+        pm_message("Writing a grayscale SIR format (MGI TYPE 17)");
         break;
 
-	default:
-        grayscale = 1;
-        pm_message( "Writing a monochrome SIR format (MGI TYPE 17)" );
+    default:
+        isGrayscale = true;
+        pm_message("Writing a monochrome SIR format (MGI TYPE 17)");
         break;
-	}
+    }
 
     /* Set up the header. */
     Header[0] = 0x3a4f;
     Header[1] = 0;
-    if (grayscale)
+    if (isGrayscale)
         Header[2] = 17;
     else
         Header[2] = 11;
@@ -93,54 +89,65 @@ int main(int argc, char * argv[]) {
     LutHeader[2] = 5;
     LutHeader[3] = 256;
     LutHeader[4] = 256;
-    for (n = 0; n < 5; n++)
+    for (n = 0; n < 5; ++n)
         pm_writelittleshort(stdout,LutHeader[n]);
-    for (n = 5; n < 256; n++)
+    for (n = 5; n < 256; ++n)
         pm_writelittleshort(stdout,0);
  
-    for(n = 0; n < 3; n ++)
-        for (m = 0; m < 256; m++)
+    for (n = 0; n < 3; ++n) {
+        unsigned int m;
+        for (m = 0; m < 256; ++m)
             Lut[m * 4 + n] = m << 8;
-    for (n = 0; n < 1024; n++)
+    }
+    for (n = 0; n < 1024; ++n)
         pm_writelittleshort(stdout,Lut[n]);
  
     /* Finally, write out the data. */
-    switch ( PNM_FORMAT_TYPE(format) )
-    {
-	case PPM_TYPE:
-	    for ( row = 0; row < rows; ++row )
-            for ( col = 0, xP = xels[row]; col < cols; ++col, ++xP )
-            {
-                ub = (char) ( PPM_GETR( *xP ) * ( 255 / maxval ) ); 
-                fputc( ub, stdout );
+    switch (PNM_FORMAT_TYPE(format)) {
+    case PPM_TYPE: {
+        unsigned int row;
+        for (row = 0; row < rows; ++row) {
+            unsigned int col;
+            for (col = 0; col < cols; ++col) {
+                unsigned char const ub =
+                    (char) (PPM_GETR(xels[row][col]) * (255 / maxval)); 
+                fputc(ub, stdout);
             }
-        for ( row = 0; row < rows; ++row )
-            for ( col = 0, xP = xels[row]; col < cols; ++col, ++xP )
-            {  
-                ub = (char) ( PPM_GETG( *xP ) * ( 255 / maxval ) );
-                fputc( ub, stdout );
+        }
+        for (row = 0; row < rows; ++row) {
+            unsigned int col;
+            for (col = 0; col < cols; ++col) {  
+                unsigned const char ub =
+                    (char) (PPM_GETG(xels[row][col]) * (255 / maxval));
+                fputc(ub, stdout);
             }
-        for ( row = 0; row < rows; ++row )
-            for ( col = 0, xP = xels[row]; col < cols; ++col, ++xP )
-            {  
-                ub = (char) ( PPM_GETB( *xP ) * ( 255 / maxval ) );
-                fputc( ub, stdout );
+        }
+        for (row = 0; row < rows; ++row) {
+            unsigned int col;
+            for (col = 0; col < cols; ++col) {  
+                unsigned const char ub =
+                    (char) (PPM_GETB(xels[row][col]) * (255 / maxval));
+                fputc(ub, stdout);
             }
-	    break;
+        }
+    } break;
 
-    default:
-        for ( row = 0; row < rows; ++row )
-            for ( col = 0, xP = xels[row]; col < cols; ++col, ++xP )
-            {
-                register unsigned long val;
-
-                val = PNM_GET1( *xP );
-                ub = (char) ( val * ( 255 / maxval ) );
-                fputc( ub, stdout );
+    default: {
+        unsigned int row;
+        for (row = 0; row < rows; ++row) {
+            unsigned int col;
+            for (col = 0; col < cols; ++col) {
+                unsigned long const val = PNM_GET1(xels[row][col]);
+                unsigned const char ub = (char) (val * (255 / maxval));
+                fputc(ub, stdout);
             }
-        break;
+        }
+    } break;
     }
+    
+    pm_close(ifP);
 
-    exit( 0 );
+    return 0;
 }
 
+
diff --git a/converter/other/pstopnm.c b/converter/other/pstopnm.c
index 19b1630a..f5342655 100644
--- a/converter/other/pstopnm.c
+++ b/converter/other/pstopnm.c
@@ -14,8 +14,9 @@
 
 -----------------------------------------------------------------------------*/
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
-#define _XOPEN_SOURCE 500  
+#define _XOPEN_SOURCE 500
     /* Make sure fdopen() is in stdio.h and strdup() is in string.h */
 
 #include <assert.h>
@@ -24,7 +25,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <signal.h>
-#include <sys/wait.h>  
+#include <sys/wait.h>
 #include <sys/stat.h>
 
 #include "pm_c_util.h"
@@ -37,17 +38,32 @@ static bool verbose;
 
 enum Orientation {PORTRAIT, LANDSCAPE, UNSPECIFIED};
 struct Box {
-    /* Description of a rectangle within an image; all coordinates 
-       measured in points (1/72") with lower left corner of page being the 
-       origin.
+    /* Description of a rectangle within an image; all coordinates
+       measured in points (1/72") with lower left corner of page being the
+       origin.  Negative values are OK.
     */
+    bool isDefined;
+
+    /* Nothing below is meaningful unless 'isDefined' is true */
     int llx;  /* lower left X coord */
-        /* -1 for llx means whole box is undefined. */
     int lly;  /* lower left Y coord */
     int urx;  /* upper right X coord */
     int ury;  /* upper right Y coord */
 };
 
+
+
+static void
+assertValidBox(struct Box const box) {
+
+    if (box.isDefined) {
+        assert(box.urx >= box.llx);
+        assert(box.ury >= box.lly);
+    }
+}
+
+
+
 struct Dimensions {
 /*----------------------------------------------------------------------------
   Horizontal and vertical dimensions of something, both in pixels and
@@ -106,7 +122,7 @@ parseCommandLine(int argc, char ** argv,
     unsigned int textalphabitsSpec;
 
     MALLOCARRAY_NOFAIL(option_def, 100);
-    
+
     option_def_index = 0;   /* incremented by OPTENTRY */
     OPTENT3(0, "forceplain", OPT_FLAG,  NULL, &cmdlineP->forceplain,     0);
     OPTENT3(0, "llx",        OPT_FLOAT, &llx, &llxSpec,                  0);
@@ -150,7 +166,7 @@ parseCommandLine(int argc, char ** argv,
     if (ymaxSpec) {
         if (cmdlineP->ymax == 0)
             pm_error("zero is not a valid value for -ymax");
-    } else 
+    } else
         cmdlineP->ymax = 792;
 
     if (xsizeSpec) {
@@ -162,7 +178,7 @@ parseCommandLine(int argc, char ** argv,
     if (ysizeSpec) {
         if (cmdlineP->ysize == 0)
             pm_error("zero is not a valid value for -ysize");
-    } else 
+    } else
         cmdlineP->ysize = 0;
 
     if (portraitOpt && !landscapeOpt)
@@ -184,9 +200,11 @@ parseCommandLine(int argc, char ** argv,
         cmdlineP->formatType = PPM_TYPE;
 
     /* If any one of the 4 bounding box coordinates is given on the
-       command line, we default any of the 4 that aren't.  
+       command line, we default any of the 4 that aren't.
     */
     if (llxSpec || llySpec || urxSpec || urySpec) {
+        cmdlineP->extractBox.isDefined = true;
+
         if (!llxSpec) cmdlineP->extractBox.llx = 72;
         else cmdlineP->extractBox.llx = llx * 72;
         if (!llySpec) cmdlineP->extractBox.lly = 72;
@@ -196,7 +214,7 @@ parseCommandLine(int argc, char ** argv,
         if (!urySpec) cmdlineP->extractBox.ury = 720;
         else cmdlineP->extractBox.ury = ury * 72;
     } else {
-        cmdlineP->extractBox.llx = -1;
+        cmdlineP->extractBox.isDefined = false;
     }
 
     if (dpiSpec) {
@@ -224,7 +242,7 @@ parseCommandLine(int argc, char ** argv,
         cmdlineP->inputFileName = "-";  /* stdin */
     else if (argc-1 == 1)
         cmdlineP->inputFileName = argv[1];
-    else 
+    else
         pm_error("Too many arguments (%d).  "
                  "Only need one: the Postscript file name", argc-1);
 
@@ -248,7 +266,7 @@ addPsToFileName(char          const origFileName[],
     int statRc;
 
     statRc = lstat(origFileName, &statbuf);
-    
+
     if (statRc == 0)
         *newFileNameP = strdup(origFileName);
     else {
@@ -269,34 +287,62 @@ addPsToFileName(char          const origFileName[],
 
 
 
+static unsigned int
+resolution(unsigned int const dotCt,
+           unsigned int const pointCt) {
+/*----------------------------------------------------------------------------
+   The resolution in dots per inch when 'dotCt' dots print 'pointCt' points
+   long.
+
+   When this would round to zero, we return 1 dot per inch instead so it
+   doesn't play havoc with arithmetic - it's never going to happen unless
+   something is broken anyway.
+-----------------------------------------------------------------------------*/
+    return MAX(1, (unsigned int)((float)dotCt * 72 / pointCt + 0.5));
+}
+
+
+
 static void
 computeSizeResFromSizeSpec(unsigned int        const requestedXsize,
                            unsigned int        const requestedYsize,
                            unsigned int        const imageWidth,
                            unsigned int        const imageHeight,
                            struct Dimensions * const imageDimP) {
+/*----------------------------------------------------------------------------
+   Compute output image size and assumed Postscript input resolution, assuming
+   user requested a specific size for at least one of the dimensions and the
+   input is 'imageWidth' x 'imageHeight' points.
+
+   'requestedXsize' is what the user requested for output image width in
+   pixels, or zero if he made no request.  'requestedYsize' is analogous
+   for the height.
+-----------------------------------------------------------------------------*/
+    assert(requestedXsize || requestedYsize);
+
+    assert(imageWidth > 0);
 
     if (requestedXsize) {
         imageDimP->xsize = requestedXsize;
-        imageDimP->xres = (unsigned int)
-            (requestedXsize * 72 / imageWidth + 0.5);
+        imageDimP->xres = resolution(requestedXsize, imageWidth);
         if (!requestedYsize) {
             imageDimP->yres = imageDimP->xres;
             imageDimP->ysize = (unsigned int)
                 (imageHeight * (float)imageDimP->yres/72 + 0.5);
-            }
         }
+    }
+
+    assert(imageHeight > 0);
 
     if (requestedYsize) {
         imageDimP->ysize = requestedYsize;
-        imageDimP->yres = (unsigned int)
-            (requestedYsize * 72 / imageHeight + 0.5);
+        imageDimP->yres = resolution(requestedYsize, imageHeight);
         if (!requestedXsize) {
             imageDimP->xres = imageDimP->yres;
             imageDimP->xsize = (unsigned int)
                 (imageWidth * (float)imageDimP->xres/72 + 0.5);
         }
-    } 
+    }
 }
 
 
@@ -312,8 +358,9 @@ computeSizeResBlind(unsigned int        const xmax,
     if (imageWidth == 0 || imageHeight == 0) {
         imageDimP->xres = imageDimP->yres = 72;
     } else {
-        imageDimP->xres = imageDimP->yres = MIN(xmax * 72 / imageWidth,
-                                                ymax * 72 / imageHeight);
+        imageDimP->xres = imageDimP->yres =
+            MIN(resolution(xmax, imageWidth),
+                resolution(ymax, imageHeight));
     }
 
     if (nocrop) {
@@ -330,17 +377,17 @@ computeSizeResBlind(unsigned int        const xmax,
 
 
 static void
-computeSizeRes(struct CmdlineInfo  const cmdline, 
+computeSizeRes(struct CmdlineInfo  const cmdline,
                struct Box          const borderedBox,
                struct Dimensions * const imageDimP) {
 /*----------------------------------------------------------------------------
   Figure out how big the output image should be and what output device
   resolution Ghostscript should assume (return as *imageDimP).
 
-  A resolution number is the number of pixels per inch that the a
+  A resolution number is the number of pixels per inch that the
   printer prints.  Since we're emulating a printed page with a PNM
   image, and a PNM image has no spatial dimension (you can't say how
-  many inches wide a PNM image is), it's kind of confusing.  
+  many inches wide a PNM image is), it's kind of confusing.
 
   If the user doesn't select a resolution, we choose the resolution
   that causes the image to be a certain number of pixels, knowing how
@@ -349,8 +396,8 @@ computeSizeRes(struct CmdlineInfo  const cmdline,
   inches wide.  We want the PNM image to be 1000 pixels wide.  So we
   tell Ghostscript that our horizontal output device resolution is 500
   pixels per inch.
-  
-  X and Y in all returned values is with respect to the image, not the
+
+  X and Y in all returned values are with respect to the image, not the
   page.  Note that the image might be placed sideways on the page, so that
   page X and Y would be reversed from image X and Y.
 -----------------------------------------------------------------------------*/
@@ -360,6 +407,8 @@ computeSizeRes(struct CmdlineInfo  const cmdline,
     unsigned int const sx = borderedBox.urx - borderedBox.llx;
     unsigned int const sy = borderedBox.ury - borderedBox.lly;
 
+    assertValidBox(borderedBox); assert(borderedBox.isDefined);
+
     if (cmdline.dpi) {
         /* User gave resolution; we figure out output image size */
         imageDimP->xres = imageDimP->yres = cmdline.dpi;
@@ -369,6 +418,7 @@ computeSizeRes(struct CmdlineInfo  const cmdline,
         if (sx == 0 || sy == 0)
             pm_error("Input image is zero size; we cannot satisfy your "
                      "produce your requested output dimensions");
+
         computeSizeResFromSizeSpec(cmdline.xsize, cmdline.ysize, sx, sy,
                                    imageDimP);
     } else
@@ -397,7 +447,7 @@ languageDeclaration(char const inputFileName[]) {
     enum PostscriptLanguage language;
 
     if (streq(inputFileName, "-"))
-        /* Can't read stdin, because we need it to remain positioned for the 
+        /* Can't read stdin, because we need it to remain positioned for the
            Ghostscript interpreter to read it.
         */
         language = COMMON_POSTSCRIPT;
@@ -430,12 +480,57 @@ languageDeclaration(char const inputFileName[]) {
 
 
 static struct Box
+boundingBoxFmPostscriptFile(FILE * const ifP) {
+
+    struct Box retval;
+    bool eof;
+
+    for (retval.isDefined = false, eof = false; !retval.isDefined && !eof; ) {
+        char line[200];
+        char * fgetsRc;
+
+        fgetsRc = fgets(line, sizeof(line), ifP);
+
+        if (fgetsRc == NULL)
+            eof = true;
+        else {
+            int rc;
+            int llx, lly, urx, ury;
+
+            rc = sscanf(line, "%%%%BoundingBox: %d %d %d %d",
+                        &llx, &lly, &urx, &ury);
+            if (rc == 4) {
+                /* We found a BoundingBox statement */
+
+                if (llx > urx)
+                    pm_error("%%%%BoundingBox statement in input file has "
+                             "lower left corner to the right of the "
+                             "upper right corner");
+                if (lly > ury)
+                    pm_error("%%%%BoundingBox statement in input file has "
+                             "lower left corner above the "
+                             "upper right corner");
+
+                retval.llx = llx; retval.lly = lly;
+                retval.urx = urx; retval.ury = ury;
+                retval.isDefined = true;
+            }
+        }
+    }
+    fclose(ifP);
+
+    return retval;
+}
+
+
+
+static struct Box
 computeBoxToExtract(struct Box const cmdlineExtractBox,
                     char       const inputFileName[]) {
 
     struct Box retval;
 
-    if (cmdlineExtractBox.llx != -1)
+    if (cmdlineExtractBox.isDefined)
         /* User told us what box to extract, so that's what we'll do */
         retval = cmdlineExtractBox;
     else {
@@ -446,54 +541,37 @@ computeBoxToExtract(struct Box const cmdlineExtractBox,
 
         if (streq(inputFileName, "-"))
             /* Can't read stdin, because we need it to remain
-               positioned for the Ghostscript interpreter to read it.  
+               positioned for the Ghostscript interpreter to read it.
             */
-            psBb.llx = -1;
+            psBb.isDefined = false;
         else {
             FILE * ifP;
-            bool foundBb;
-            bool eof;
 
             ifP = pm_openr(inputFileName);
-            
-            for (foundBb = FALSE, eof = FALSE; !foundBb && !eof; ) {
-                char line[200];
-                char * fgetsRc;
-
-                fgetsRc = fgets(line, sizeof(line), ifP);
-
-                if (fgetsRc == NULL)
-                    eof = TRUE;
-                else {
-                    int rc;
-                    rc = sscanf(line, "%%%%BoundingBox: %d %d %d %d",
-                                &psBb.llx, &psBb.lly, 
-                                &psBb.urx, &psBb.ury);
-                    if (rc == 4) 
-                        foundBb = TRUE;
-                }
-            }
-            fclose(ifP);
 
-            if (!foundBb) {
-                psBb.llx = -1;
+            psBb = boundingBoxFmPostscriptFile(ifP);
+
+            if (!psBb.isDefined)
                 pm_message("Warning: no %%%%BoundingBox statement "
                            "in the input or command line.  "
                            "Will use defaults");
-            }
         }
-        if (psBb.llx != -1) {
+        if (psBb.isDefined) {
             if (verbose)
                 pm_message("Using %%%%BoundingBox statement from input.");
             retval = psBb;
-        } else { 
+        } else {
             /* Use the center of an 8.5" x 11" page with 1" border all around*/
+            retval.isDefined = true;
             retval.llx = 72;
             retval.lly = 72;
             retval.urx = 540;
             retval.ury = 720;
         }
     }
+
+    assert(retval.isDefined);
+
     if (verbose)
         pm_message("Extracting the box ((%d,%d),(%d,%d))",
                    retval.llx, retval.lly, retval.urx, retval.ury);
@@ -503,7 +581,7 @@ computeBoxToExtract(struct Box const cmdlineExtractBox,
 
 
 static enum Orientation
-computeOrientation(struct CmdlineInfo const cmdline, 
+computeOrientation(struct CmdlineInfo const cmdline,
                    struct Box         const extractBox) {
 /*----------------------------------------------------------------------------
    The proper orientation of the image on the page, given the user's
@@ -531,7 +609,7 @@ computeOrientation(struct CmdlineInfo const cmdline,
         /* Dimensions of image to print, in points */
         unsigned int const imageWidPt = extractBox.urx - extractBox.llx;
         unsigned int const imageHgtPt = extractBox.ury - extractBox.lly;
-        
+
         /* Dimensions of image to print, in pixels (possibly of assumed
            resolution)
         */
@@ -579,31 +657,30 @@ computeOrientation(struct CmdlineInfo const cmdline,
 
 
 static struct Box
-addBorders(struct Box const inputBox, 
+addBorders(struct Box const inputBox,
            float      const xborderScale,
            float      const yborderScale) {
 /*----------------------------------------------------------------------------
    Return a box which is 'inputBox' plus some borders.
 
    Add left and right borders that are the fraction 'xborderScale' of the
-   width of the input box; likewise for top and bottom borders with 
+   width of the input box; likewise for top and bottom borders with
    'yborderScale'.
 -----------------------------------------------------------------------------*/
-    unsigned int const leftRightBorderSize = 
+    unsigned int const leftRightBorderSize =
         ROUNDU((inputBox.urx - inputBox.llx) * xborderScale);
-    unsigned int const topBottomBorderSize = 
+    unsigned int const topBottomBorderSize =
         ROUNDU((inputBox.ury - inputBox.lly) * yborderScale);
 
     struct Box retval;
 
-
-    assert(inputBox.urx >= inputBox.llx);
-    assert(inputBox.ury >= inputBox.lly);
+    assertValidBox(inputBox); assert(inputBox.isDefined);
 
     retval.llx = inputBox.llx - (int)leftRightBorderSize;
     retval.lly = inputBox.lly - (int)topBottomBorderSize;
     retval.urx = inputBox.urx + (int)leftRightBorderSize;
     retval.ury = inputBox.ury + (int)topBottomBorderSize;
+    retval.isDefined = true;
 
     if (verbose)
         pm_message("With borders, extracted box is ((%d,%d),(%d,%d))",
@@ -627,6 +704,8 @@ writePstrans(struct Box        const box,
 
     const char * pstrans;
 
+    assert(xres > 0); assert(yres > 0);
+
     switch (orientation) {
     case PORTRAIT: {
         int llx, lly;
@@ -647,7 +726,7 @@ writePstrans(struct Box        const box,
     if (pstrans == pm_strsol)
         pm_error("Unable to allocate memory for pstrans");
 
-    if (verbose) 
+    if (verbose)
         pm_message("Postscript prefix command: '%s'", pstrans);
 
     fprintf(pipeToGsP, "%s\n", pstrans);
@@ -678,10 +757,10 @@ computeOutfileArg(struct CmdlineInfo const cmdline) {
     else {
         char * basename;
         const char * suffix;
-        
+
         basename  = strdup(cmdline.inputFileName);
-        if (strlen(basename) > 3 && 
-            streq(basename+strlen(basename)-3, ".ps")) 
+        if (strlen(basename) > 3 &&
+            streq(basename+strlen(basename)-3, ".ps"))
             /* The input file name ends in ".ps".  Chop it off. */
             basename[strlen(basename)-3] = '\0';
 
@@ -729,7 +808,7 @@ computeGsDevice(int  const formatType,
 
 static void
 findGhostscriptProg(const char ** const retvalP) {
-    
+
     *retvalP = NULL;  /* initial assumption */
     if (getenv("GHOSTSCRIPT"))
         *retvalP = strdup(getenv("GHOSTSCRIPT"));
@@ -739,7 +818,7 @@ findGhostscriptProg(const char ** const retvalP) {
             const char * candidate;
 
             pathwork = strdup(getenv("PATH"));
-            
+
             candidate = strtok(pathwork, ":");
 
             *retvalP = NULL;
@@ -773,7 +852,7 @@ findGhostscriptProg(const char ** const retvalP) {
 static void
 execGhostscript(int               const inputPipeFd,
                 char              const ghostscriptDevice[],
-                char              const outfileArg[], 
+                char              const outfileArg[],
                 struct Dimensions const pageDim,
                 unsigned int      const textalphabits) {
 /*----------------------------------------------------------------------------
@@ -791,12 +870,11 @@ execGhostscript(int               const inputPipeFd,
     const char * gopt;
     const char * ropt;
     const char * textalphabitsopt;
-    int rc;
 
     findGhostscriptProg(&ghostscriptProg);
 
     /* Put the input pipe on Standard Input */
-    rc = dup2(inputPipeFd, STDIN_FILENO);
+    dup2(inputPipeFd, STDIN_FILENO);
     close(inputPipeFd);
 
     pm_asprintf(&arg0, "gs");
@@ -817,13 +895,13 @@ execGhostscript(int               const inputPipeFd,
                    "'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'",
                    ghostscriptProg, arg0,
                    deviceopt, outfileopt, gopt, ropt, textalphabitsopt,
-                   "-q", "-dNOPAUSE", 
+                   "-q", "-dNOPAUSE",
                    "-dSAFER", "-");
     }
 
     execl(ghostscriptProg, arg0, deviceopt, outfileopt, gopt, ropt, 
 	  textalphabitsopt, "-q", "-dNOPAUSE", "-dSAFER", "-", NULL);
-    
+
     pm_error("execl() of Ghostscript ('%s') failed, errno=%d (%s)",
              ghostscriptProg, errno, strerror(errno));
 }
@@ -852,26 +930,26 @@ feedPsToGhostScript(const char *            const inputFileName,
     bool eof;  /* End of file on input */
 
     pipeToGsP = fdopen(pipeToGhostscriptFd, "w");
-    if (pipeToGsP == NULL) 
+    if (pipeToGsP == NULL)
         pm_error("Unable to open stream on pipe to Ghostscript process.");
-    
+
     ifP = pm_openr(inputFileName);
     /*
       In encapsulated Postscript, we the encapsulator are supposed to
       handle showing the page (which we do by passing a showpage
-      statement to Ghostscript).  Any showpage statement in the 
+      statement to Ghostscript).  Any showpage statement in the
       input must be defined to have no effect.
-          
+
       See "Enscapsulated PostScript Format File Specification",
       v. 3.0, 1 May 1992, in particular Example 2, p. 21.  I found
-      it at 
+      it at
       http://partners.adobe.com/asn/developer/pdfs/tn/5002.EPSF_Spec.pdf
       The example given is a much fancier solution than we need
-      here, I think, so I boiled it down a bit.  JM 
+      here, I think, so I boiled it down a bit.  JM
     */
     if (language == ENCAPSULATED_POSTSCRIPT)
         fprintf(pipeToGsP, "\n/b4_Inc_state save def /showpage { } def\n");
- 
+
     writePstrans(borderedBox, imageDim, orientation, pipeToGsP);
 
     /* If our child dies, it closes the pipe and when we next write to it,
@@ -884,11 +962,11 @@ feedPsToGhostScript(const char *            const inputFileName,
     while (!eof) {
         char buffer[4096];
         size_t readCt;
-            
+
         readCt = fread(buffer, 1, sizeof(buffer), ifP);
-        if (readCt == 0) 
+        if (readCt == 0)
             eof = TRUE;
-        else 
+        else
             fwrite(buffer, 1, readCt, pipeToGsP);
     }
     pm_close(ifP);
@@ -897,7 +975,7 @@ feedPsToGhostScript(const char *            const inputFileName,
         fprintf(pipeToGsP, "\nb4_Inc_state restore showpage\n");
 
     fclose(pipeToGsP);
-}        
+}
 
 
 
@@ -940,7 +1018,7 @@ executeGhostscript(char                    const inputFileName[],
                    struct Dimensions       const imageDim,
                    enum Orientation        const orientation,
                    char                    const ghostscriptDevice[],
-                   char                    const outfileArg[], 
+                   char                    const outfileArg[],
                    unsigned int            const textalphabits,
                    enum PostscriptLanguage const language) {
 
@@ -949,12 +1027,12 @@ executeGhostscript(char                    const inputFileName[],
 
     if (strlen(outfileArg) > 80)
         pm_error("output file spec too long.");
-    
+
     rc = pm_pipe(pipefd);
     if (rc < 0)
         pm_error("Unable to create pipe to talk to Ghostscript process.  "
                  "errno = %d (%s)", errno, strerror(errno));
-    
+
     rc = fork();
     if (rc < 0)
         pm_error("Unable to fork a Ghostscript process.  errno=%d (%s)",
@@ -986,13 +1064,13 @@ executeGhostscript(char                    const inputFileName[],
 
         if (gsTermStatus != 0) {
             if (WIFEXITED(gsTermStatus))
-                pm_error("Ghostscript failed.  Exit code=%d\n", 
+                pm_error("Ghostscript failed.  Exit code=%d\n",
                          WEXITSTATUS(gsTermStatus));
             else if (WIFSIGNALED(gsTermStatus))
                 pm_error("Ghostscript process died because of a signal %d.",
                          WTERMSIG(gsTermStatus));
-            else 
-                pm_error("Ghostscript process died with exit code %d", 
+            else
+                pm_error("Ghostscript process died with exit code %d",
                          gsTermStatus);
         }
     }
@@ -1010,7 +1088,7 @@ main(int argc, char ** argv) {
         /* Size and resolution of the input image */
     struct Box extractBox;
         /* coordinates of the box within the input we are to extract; i.e.
-           that will become the output. 
+           that will become the output.
            */
     struct Box borderedBox;
         /* Same as above, but expanded to include borders */
@@ -1031,32 +1109,31 @@ main(int argc, char ** argv) {
     extractBox = computeBoxToExtract(cmdline.extractBox, inputFileName);
 
     language = languageDeclaration(inputFileName);
-    
+
     orientation = computeOrientation(cmdline, extractBox);
 
     borderedBox = addBorders(extractBox, cmdline.xborder, cmdline.yborder);
 
+    assertValidBox(borderedBox); assert(borderedBox.isDefined);
+
     computeSizeRes(cmdline, borderedBox, &imageDim);
 
-    if (imageDim.xres == 0)
-        imageDim.xres = 1;
-    if (imageDim.yres == 0)
-        imageDim.yres = 1;
-    
+    assert(imageDim.xres > 0); assert(imageDim.yres > 0);
+
     outfileArg = computeOutfileArg(cmdline);
 
-    ghostscriptDevice = 
+    ghostscriptDevice =
         computeGsDevice(cmdline.formatType, cmdline.forceplain);
-    
+
     pm_message("Writing %s format", ghostscriptDevice);
-    
+
     executeGhostscript(inputFileName, borderedBox, imageDim, orientation,
                        ghostscriptDevice, outfileArg, cmdline.textalphabits,
                        language);
 
     pm_strfree(ghostscriptDevice);
     pm_strfree(outfileArg);
-    
+
     return 0;
 }
 
diff --git a/converter/other/rletopnm.c b/converter/other/rletopnm.c
index 99959141..018456c8 100644
--- a/converter/other/rletopnm.c
+++ b/converter/other/rletopnm.c
@@ -35,6 +35,7 @@
  *
  */
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
@@ -264,7 +265,8 @@ writePpmRaster(FILE * const imageoutFileP,
     pixel *pixelrow;
     gray *alpharow;
    
-    int scan, x, y;
+    int scan;
+    int x;
     /*
      *  Allocate some stuff.
      */
@@ -280,9 +282,9 @@ writePpmRaster(FILE * const imageoutFileP,
     /*
      * Loop through those scan lines.
      */
-    for (scan = 0; scan < height; scan++)
-        y = rle_getrow(&hdr, scanlines[height - scan - 1]);
-    for (scan = 0; scan < height; scan++) {
+    for (scan = 0; scan < height; ++scan)
+        rle_getrow(&hdr, scanlines[height - scan - 1]);
+    for (scan = 0; scan < height; ++scan) {
         scanline = scanlines[scan];
         switch (visual) {
         case GRAYSCALE:    /* 8 bits without colormap */
@@ -366,7 +368,6 @@ writePgmRaster(FILE * const imageoutFileP,
     gray * pixelrow;
     gray * alpharow;
     int scan;
-    int y;
     /*
      *  Allocate some stuff.
      */
@@ -383,7 +384,7 @@ writePgmRaster(FILE * const imageoutFileP,
      * Loop through those scan lines.
      */
     for (scan = 0; scan < height; ++scan)
-        y = rle_getrow(&hdr, scanlines[height - scan - 1]);
+        rle_getrow(&hdr, scanlines[height - scan - 1]);
 
     for (scan = 0; scan < height; ++scan) {
         int x;
diff --git a/converter/other/svgtopam.c b/converter/other/svgtopam.c
index 58e7928f..ca6f4dc7 100644
--- a/converter/other/svgtopam.c
+++ b/converter/other/svgtopam.c
@@ -26,6 +26,7 @@
    
 ============================================================================*/
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE  /* Make sure strdup() is in <string.h> */
 #define _POSIX_SOURCE   /* Make sure fileno() is in <stdio.h> */
@@ -146,36 +147,36 @@ typedef struct {
     unsigned int height;
     pixel ** pixels;
     pixval maxval;
-} canvas;
+} Canvas;
 
 typedef struct {
     pixel fillColor;
-} style;
+} Style;
 
 
 
 typedef struct {
     const char * pathText;
         /* This is e.g. "M0 0 L1 1 L9 8 Z" */
-    style        style;
+    Style        style;
         /* This is the style as given by a 'style' attribute of <path> */
     unsigned int pathTextLength;
         /* This is the length in characters of 'pathText'.  It's redundant
            with 'pathText' and exists for convenience.
         */
-} path;
+} Path;
 
 static void
 createPath(const char * const pathText,
-           style        const style,
-           path **      const pathPP) {
+           Style        const style,
+           Path **      const pathPP) {
 /*----------------------------------------------------------------------------
    Create a path as described by a <path> element whose "style" attribute
    indicates style 'style' and whose "d" attribute indicates path data
    'pathText'.
 -----------------------------------------------------------------------------*/
     bool error;
-    path * pathP;
+    Path * pathP;
     
     MALLOCVAR(pathP);
     if (pathP == NULL)
@@ -203,7 +204,7 @@ createPath(const char * const pathText,
 
 
 static void
-destroyPath(path * const pathP) {
+destroyPath(Path * const pathP) {
     
     assert(pathP->pathTextLength == strlen(pathP->pathText));
 
@@ -217,13 +218,13 @@ destroyPath(path * const pathP) {
 typedef struct {
     unsigned int x;
     unsigned int y;
-} point;
+} Point;
 
-static point
+static Point
 makePoint(unsigned int const x,
           unsigned int const y) {
 
-    point p;
+    Point p;
     
     p.x = x;
     p.y = y;
@@ -232,7 +233,7 @@ makePoint(unsigned int const x,
 }
 
 static ppmd_point
-makePpmdPoint(point const arg) {
+makePpmdPoint(Point const arg) {
 
     ppmd_point p;
 
@@ -247,16 +248,16 @@ typedef enum {
     PATH_LINETO,
     PATH_CLOSEPATH,
     PATH_CUBIC
-} pathCommandVerb;
+} PathCommandVerb;
 
 typedef struct {
-    point dest;
-} pathMovetoArgs;
+    Point dest;
+} PathMovetoArgs;
 
 typedef struct {
     /* Draw a line segment from current point to 'dest' */
-    point dest;
-} pathLinetoArgs;
+    Point dest;
+} PathLinetoArgs;
 
 typedef struct {
     /* Draw a cubic spline from current point to 'dest' with control points
@@ -271,19 +272,19 @@ typedef struct {
        A cubic curve is a plot of a polynomial equation of degree 3
        (or less, for our purposes).
     */
-    point dest;
-    point ctl1;
-    point ctl2;
-} pathCubicArgs;
+    Point dest;
+    Point ctl1;
+    Point ctl2;
+} PathCubicArgs;
 
 typedef struct {
-    pathCommandVerb verb;
+    PathCommandVerb verb;
     union {
-        pathMovetoArgs moveto;
-        pathLinetoArgs lineto;
-        pathCubicArgs  cubic;
+        PathMovetoArgs moveto;
+        PathLinetoArgs lineto;
+        PathCubicArgs  cubic;
     } args;
-} pathCommand;
+} PathCommand;
 
 
 
@@ -291,15 +292,15 @@ typedef struct {
 /*----------------------------------------------------------------------------
    This is an object for reading through a path from beginning to end.
 -----------------------------------------------------------------------------*/
-    path *       pathP;
+    Path *       pathP;
     unsigned int cursor;
-} pathReader;
+} PathReader;
 
 static void
-createPathReader(path *        const pathP,
-                 pathReader ** const pathReaderPP) {
+pathReader_create(Path *        const pathP,
+                  PathReader ** const pathReaderPP) {
 
-    pathReader * pathReaderP;
+    PathReader * pathReaderP;
 
     MALLOCVAR_NOFAIL(pathReaderP);
 
@@ -310,18 +311,31 @@ createPathReader(path *        const pathP,
 }
 
 static void
-destroyPathReader(pathReader * const pathReaderP) {
+pathReader_destroy(PathReader * const pathReaderP) {
     free(pathReaderP);
 }
 
 
 
+static const char *
+pathReader_context(PathReader * const pathReaderP) {
+
+    const char * retval;
+
+    pm_asprintf(&retval, "Character position %u (starting at 0) in '%s'",
+                pathReaderP->cursor, pathReaderP->pathP->pathText);
+
+    return retval;
+}
+
+
+
 static void
-skipWhiteSpace(pathReader * const pathReaderP) {
+pathReader_skipWhiteSpace(PathReader * const pathReaderP) {
 /*----------------------------------------------------------------------------
    Move the cursor over any white space where it now points.
 -----------------------------------------------------------------------------*/
-    const path * const pathP = pathReaderP->pathP;
+    const Path * const pathP = pathReaderP->pathP;
 
     while (isspace(pathP->pathText[pathReaderP->cursor]) &&
            pathReaderP->cursor < pathP->pathTextLength)
@@ -331,10 +345,10 @@ skipWhiteSpace(pathReader * const pathReaderP) {
 
 
 static void
-getNumber(pathReader *   const pathReaderP,
-          unsigned int * const numberP) {
+pathReader_getNumber(PathReader *   const pathReaderP,
+                     unsigned int * const numberP) {
 
-    const path * const pathP          = pathReaderP->pathP;
+    const Path * const pathP          = pathReaderP->pathP;
     const char * const pathText       = pathP->pathText;
     size_t       const pathTextLength = pathP->pathTextLength;
 
@@ -342,7 +356,10 @@ getNumber(pathReader *   const pathReaderP,
 
     if (pathReaderP->cursor >= pathTextLength)
         pm_error("Path description ends where a number was expected.");
-    else {
+    else if (!isdigit(pathText[pathReaderP->cursor])) {
+        pm_error("Character '%c' instead of a digit where number expected",
+                 pathText[pathReaderP->cursor]);
+    } else {
         unsigned int number;
 
         number = 0;  /* initial value */
@@ -352,6 +369,10 @@ getNumber(pathReader *   const pathReaderP,
             number = 10 * number + (pathText[pathReaderP->cursor] - '0');
             ++pathReaderP->cursor;
         }
+        if (pathText[pathReaderP->cursor] == '.')
+            pm_error("Number contains decimal point.  This program does not "
+                     "know how to deal with fractional positions");
+
         *numberP = number;
     }
 }
@@ -359,15 +380,15 @@ getNumber(pathReader *   const pathReaderP,
 
 
 static void
-getNextCommand(pathReader *  const pathReaderP,
-               pathCommand * const pathCommandP,
-               bool *        const endOfPathP) {
+pathReader_getNextCommand(PathReader *  const pathReaderP,
+                          PathCommand * const pathCommandP,
+                          bool *        const endOfPathP) {
 
-    const path * const pathP          = pathReaderP->pathP;
+    const Path * const pathP          = pathReaderP->pathP;
     const char * const pathText       = pathP->pathText;
     size_t       const pathTextLength = pathP->pathTextLength;
 
-    skipWhiteSpace(pathReaderP);
+    pathReader_skipWhiteSpace(pathReaderP);
 
     if (pathReaderP->cursor >= pathTextLength)
         *endOfPathP = true;
@@ -375,67 +396,79 @@ getNextCommand(pathReader *  const pathReaderP,
         switch (pathText[pathReaderP->cursor++]) {
         case 'M':
             pathCommandP->verb = PATH_MOVETO;
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.moveto.dest.x);
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.moveto.dest.y);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP,
+                                 &pathCommandP->args.moveto.dest.x);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP,
+                                 &pathCommandP->args.moveto.dest.y);
             break;
         case 'L':
             pathCommandP->verb = PATH_LINETO;
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.lineto.dest.x);
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.lineto.dest.y);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP,
+                                 &pathCommandP->args.lineto.dest.x);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP,
+                                 &pathCommandP->args.lineto.dest.y);
             break;
         case 'C':
             pathCommandP->verb = PATH_CUBIC;
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.cubic.ctl1.x);
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.cubic.ctl1.y);
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.cubic.ctl2.x);
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.cubic.ctl2.y);
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.cubic.dest.x);
-            skipWhiteSpace(pathReaderP);
-            getNumber(pathReaderP, &pathCommandP->args.cubic.dest.y);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl1.x);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl1.y);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl2.x);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl2.y);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.dest.x);
+            pathReader_skipWhiteSpace(pathReaderP);
+            pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.dest.y);
             break;
         case 'z':
             pathCommandP->verb = PATH_CLOSEPATH;
             break;
-        default:
-            pm_error("Unrecognized command in <path>: '%c'",
-                     pathText[pathReaderP->cursor++]);
+        default: {
+            const char * const context = pathReader_context(pathReaderP);
+            
+            pm_errormsg("Unrecognized command in <path>: '%c'.  %s",
+                        pathText[pathReaderP->cursor++], context);
+
+            pm_strfree(context);
+
+            pm_longjmp();
+        }
         }
     }
 }
 
 
+
 static void
-outlineObject(path *           const pathP,
+outlineObject(Path *           const pathP,
               struct fillobj * const fillObjP) {
 /*----------------------------------------------------------------------------
   Create a fill object, which contains and outline of the object and
   can be used with ppmd_fill() to fill the figure.  The outline is as
   described by *pathP.
 -----------------------------------------------------------------------------*/
-    pathReader * pathReaderP;
+    PathReader * pathReaderP;
     bool endOfPath;
-    point currentPos;
-    point subpathStart;
+    Point currentPos;
+    Point subpathStart;
         /* Point at which the current subpath starts */
 
     endOfPath = false;
     subpathStart = makePoint(0,0);
     currentPos = subpathStart;
 
-    createPathReader(pathP, &pathReaderP);
+    pathReader_create(pathP, &pathReaderP);
 
     while (!endOfPath) {
-        pathCommand pathCommand;
-        getNextCommand(pathReaderP, &pathCommand, &endOfPath);
+        PathCommand pathCommand;
+        pathReader_getNextCommand(pathReaderP, &pathCommand, &endOfPath);
         if (!endOfPath) {
             switch (pathCommand.verb) {
             case PATH_MOVETO:
@@ -447,7 +480,7 @@ outlineObject(path *           const pathP,
                 currentPos = subpathStart;
                 break;
             case PATH_LINETO: {
-                point const dest = pathCommand.args.lineto.dest;
+                Point const dest = pathCommand.args.lineto.dest;
                 if (traceDraw)
                     pm_message("Lining to (%u, %u)", dest.x, dest.y);
                 ppmd_line(NULL, 0, 0, 0,
@@ -465,12 +498,14 @@ outlineObject(path *           const pathP,
                 currentPos = subpathStart;
                 break;
             case PATH_CUBIC: {
-                point const dest = pathCommand.args.cubic.dest;
-                point const ctl1 = pathCommand.args.cubic.ctl1;
-                point const ctl2 = pathCommand.args.cubic.ctl2;
+                Point const dest = pathCommand.args.cubic.dest;
+                Point const ctl1 = pathCommand.args.cubic.ctl1;
+                Point const ctl2 = pathCommand.args.cubic.ctl2;
                 if (traceDraw)
                     pm_message("Doing cubic spline to (%u, %u)",
                                dest.x, dest.y);
+                pm_error("SVG image contains a cubic spline path.  "
+                         "This program cannot process cubic splines.");
                 /* We need to write ppmd_spline4() */
                 ppmd_spline4p(NULL, 0, 0, 0,
                               makePpmdPoint(currentPos),
@@ -483,14 +518,14 @@ outlineObject(path *           const pathP,
             }
         }
     }
-    destroyPathReader(pathReaderP);
+    pathReader_destroy(pathReaderP);
 }
 
 
 
 static void
-drawPath(canvas * const canvasP,
-         path *   const pathP) {
+drawPath(Canvas * const canvasP,
+         Path *   const pathP) {
 /*----------------------------------------------------------------------------
    Draw the path 'pathP' on the canvas 'canvasP'.
 -----------------------------------------------------------------------------*/
@@ -517,10 +552,10 @@ drawPath(canvas * const canvasP,
 
 
 
-static style
+static Style
 interpretStyle(const char * const styleAttr) {
 
-    style style;
+    Style style;
 
     char * buffer;
 
@@ -581,7 +616,7 @@ interpretStyle(const char * const styleAttr) {
 
 static void
 getPathAttributes(xmlTextReaderPtr const xmlReaderP,
-                  style *          const styleP,
+                  Style *          const styleP,
                   const char **    const pathP) {
 
     const char * const style = getAttribute(xmlReaderP, "style");
@@ -627,11 +662,11 @@ processSubPathNode(xmlTextReaderPtr const xmlReaderP,
 
 static void
 processPathElement(xmlTextReaderPtr const xmlReaderP,
-                   canvas *         const canvasP) {
+                   Canvas *         const canvasP) {
 
-    style style;
+    Style style;
     const char * pathData;
-    path * pathP;
+    Path * pathP;
     bool endOfPath;
 
     assert(xmlTextReaderNodeType(xmlReaderP) == XML_READER_TYPE_ELEMENT);
@@ -669,29 +704,6 @@ processPathElement(xmlTextReaderPtr const xmlReaderP,
 
 
 static void
-stringToUint(const char *   const string,
-             unsigned int * const uintP,
-             const char **  const errorP) {
-
-    /* TODO: move this to nstring.c */
-
-    if (strlen(string) == 0)
-        pm_asprintf(errorP, "Value is a null string");
-    else {
-        char * tailptr;
-
-        *uintP = strtoul(string, &tailptr, 10);
-
-        if (*tailptr != '\0')
-            pm_asprintf(errorP, "Non-numeric crap in string: '%s'", tailptr);
-        else
-            *errorP = NULL;
-    }
-}
-
-
-
-static void
 getSvgAttributes(xmlTextReaderPtr const xmlReaderP,
                  unsigned int *   const colsP,
                  unsigned int *   const rowsP) {
@@ -701,14 +713,16 @@ getSvgAttributes(xmlTextReaderPtr const xmlReaderP,
 
     const char * error;
 
-    stringToUint(width, colsP, &error);
+    pm_string_to_uint(width, colsP, &error);
     if (error) {
-        pm_error("'width' attribute of <svg> has invalid value.  %s", error);
+        pm_error("'width' attribute of <svg> has invalid value '%s'.  %s",
+                 width, error);
         pm_strfree(error);
     }
-    stringToUint(height, rowsP, &error);
+    pm_string_to_uint(height, rowsP, &error);
     if (error) {
-        pm_error("'height' attribute of <svg> has invalid value.  %s", error);
+        pm_error("'height' attribute of <svg> has invalid value '%s'.  %s",
+                 height, error);
         pm_strfree(error);
     }
 }
@@ -717,7 +731,7 @@ getSvgAttributes(xmlTextReaderPtr const xmlReaderP,
 
 static void
 processSubSvgElement(xmlTextReaderPtr const xmlReaderP,
-                     canvas *         const canvasP) {
+                     Canvas *         const canvasP) {
 
     const char * const nodeName = currentNodeName(xmlReaderP);
 
@@ -734,7 +748,7 @@ processSubSvgElement(xmlTextReaderPtr const xmlReaderP,
 
 static void
 processSubSvgNode(xmlTextReaderPtr const xmlReaderP,
-                  canvas *         const canvasP,
+                  Canvas *         const canvasP,
                   bool *           const endOfSvgP) {
 
     xmlReaderTypes const nodeType = xmlTextReaderNodeType(xmlReaderP);
@@ -764,9 +778,9 @@ static void
 createCanvas(unsigned int const width,
              unsigned int const height,
              pixval       const maxval,
-             canvas **    const canvasPP) {
+             Canvas **    const canvasPP) {
 
-    canvas * canvasP;
+    Canvas * canvasP;
 
     MALLOCVAR_NOFAIL(canvasP);
 
@@ -781,7 +795,7 @@ createCanvas(unsigned int const width,
 
 
 static void
-destroyCanvas(canvas * const canvasP) {
+destroyCanvas(Canvas * const canvasP) {
 
     ppm_freearray(canvasP->pixels, canvasP->height);
 
@@ -792,7 +806,7 @@ destroyCanvas(canvas * const canvasP) {
 
 static void
 writePam(FILE *   const ofP,
-         canvas * const canvasP) {
+         Canvas * const canvasP) {
 
     unsigned int row;
     struct pam pam;
@@ -838,7 +852,7 @@ processSvgElement(xmlTextReaderPtr const xmlReaderP,
 
     unsigned int width, height;
     bool endOfSvg;
-    canvas * canvasP;
+    Canvas * canvasP;
 
     assert(xmlTextReaderNodeType(xmlReaderP) == XML_READER_TYPE_ELEMENT);
     assert(streq(currentNodeName(xmlReaderP), "svg"));
diff --git a/converter/other/tiff.c b/converter/other/tiff.c
index d0cbbd74..39e3b0ce 100644
--- a/converter/other/tiff.c
+++ b/converter/other/tiff.c
@@ -6,6 +6,7 @@
 
 ============================================================================*/
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE    /* Make sure strcaseeq() is in nstring.h */
 
 #include <string.h>
diff --git a/converter/other/tifftopnm.c b/converter/other/tifftopnm.c
index 4a8cf902..c1e7af85 100644
--- a/converter/other/tifftopnm.c
+++ b/converter/other/tifftopnm.c
@@ -46,12 +46,14 @@
    give the user the -byrow option to order (2) only.
 */
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
 #include <assert.h>
 #include <string.h>
 #include <stdio.h>
+#include <sys/wait.h>  /* WIFSIGNALED, etc. */
 
 #include "pm_c_util.h"
 #include "shhopt.h"
@@ -182,14 +184,14 @@ getBps(TIFF *           const tif,
 
     unsigned short tiffBps;
     unsigned short bps;
-    int rc;
+    int fldPresent;
 
-    rc = TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiffBps);
-    bps = (rc == 0) ? 1 : tiffBps;
+    fldPresent = TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiffBps);
+    bps = fldPresent ? tiffBps : 1;
 
     if (bps < 1 || (bps > 8 && bps != 16 && bps != 32))
         pm_error("This program can process Tiff images with only "
-                 "1-8 or 16 bits per sample.  The input Tiff image "
+                 "1-8 or 16 or 32 bits per sample.  The input Tiff image "
                  "has %hu bits per sample.", bps);
     else
         *bpsP = bps;
@@ -217,26 +219,30 @@ struct tiffDirInfo {
 
 
 static void
-tiffToImageDim(unsigned int   const tiffCols,
-               unsigned int   const tiffRows,
+tiffToImageDim(unsigned int   const tiffWidth,
+               unsigned int   const tiffHeight,
                unsigned short const orientation,
                unsigned int * const imageColsP,
                unsigned int * const imageRowsP) {
-
+/*----------------------------------------------------------------------------
+   Determine the image dimensions (as *imageColsP and *imageRowsP) from the
+   width, height, and orientation of the TIFF raster ('tiffWidth',
+   'tiffHeight', and 'orientation', respectively.
+-----------------------------------------------------------------------------*/
     switch (orientation) {
     case ORIENTATION_TOPLEFT:
     case ORIENTATION_TOPRIGHT:
     case ORIENTATION_BOTRIGHT:
     case ORIENTATION_BOTLEFT:
-        *imageColsP = tiffCols;
-        *imageRowsP = tiffRows;
+        *imageColsP = tiffWidth;
+        *imageRowsP = tiffHeight;
         break;
     case ORIENTATION_LEFTTOP:
     case ORIENTATION_RIGHTTOP:
     case ORIENTATION_RIGHTBOT:
     case ORIENTATION_LEFTBOT:
-        *imageColsP = tiffRows;
-        *imageRowsP = tiffCols;
+        *imageColsP = tiffHeight;
+        *imageRowsP = tiffWidth;
         break;
     default:
         pm_error("Invalid value for orientation tag in TIFF directory: %u",
@@ -255,28 +261,96 @@ getTiffDimensions(TIFF *         const tiffP,
    dimensions of the internal raster matrix -- the dimensions of the
    actual visual image.
 -----------------------------------------------------------------------------*/
-    int ok;
+    int fldPresent;
 
     unsigned int width, length;
     unsigned short tiffOrientation;
     unsigned short orientation;
-    int present;
 
-    ok = TIFFGetField(tiffP, TIFFTAG_IMAGEWIDTH, &width);
-    if (!ok)
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_IMAGEWIDTH, &width);
+    if (!fldPresent)
         pm_error("Input Tiff file is invalid.  It has no IMAGEWIDTH tag.");
-    ok = TIFFGetField(tiffP, TIFFTAG_IMAGELENGTH, &length);
-    if (!ok)
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_IMAGELENGTH, &length);
+    if (!fldPresent)
         pm_error("Input Tiff file is invalid.  It has no IMAGELENGTH tag.");
 
-    present = TIFFGetField(tiffP, TIFFTAG_ORIENTATION, &tiffOrientation);
-    orientation = present ? tiffOrientation : ORIENTATION_TOPLEFT;
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_ORIENTATION, &tiffOrientation);
+    orientation = fldPresent ? tiffOrientation : ORIENTATION_TOPLEFT;
 
     tiffToImageDim(width, length, orientation, colsP, rowsP);
 }
 
 
 
+static unsigned short
+planarConfigFmTiff(TIFF * const tiffP) {
+
+    int fldPresent;
+    unsigned short retval;
+
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_PLANARCONFIG, &retval);
+
+    if (!fldPresent)
+        pm_error("PLANARCONFIG tag is not in Tiff file, though it "
+                 "has more than one sample per pixel.  "
+                 "TIFFGetField() of it failed.  This means the input "
+                 "is not valid Tiff.");
+
+    return retval;
+}
+
+
+
+static void
+validatePlanarConfig(unsigned short const planarconfig,
+                     unsigned short const photomet) {
+
+    switch (planarconfig) {
+    case PLANARCONFIG_CONTIG:
+        break;
+    case PLANARCONFIG_SEPARATE:
+        if (photomet != PHOTOMETRIC_RGB && 
+            photomet != PHOTOMETRIC_SEPARATED)
+            pm_error("This program can handle separate planes only "
+                     "with RGB (PHOTOMETRIC tag = %u) or SEPARATED "
+                     "(PHOTOMETRIC tag = %u) data.  The input Tiff file " 
+                     "has PHOTOMETRIC tag = %hu.",
+                     PHOTOMETRIC_RGB, PHOTOMETRIC_SEPARATED,
+                     photomet);
+        break;
+    default:
+        pm_error("Unrecognized PLANARCONFIG tag value in Tiff input: %u",
+                 planarconfig);
+    }
+}
+
+
+
+static unsigned short
+orientationFmTiff(TIFF * const tiffP) {
+
+    unsigned short tiffOrientation;
+    int fldPresent;
+
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_ORIENTATION, &tiffOrientation);
+
+    return fldPresent ? tiffOrientation : ORIENTATION_TOPLEFT;
+}
+
+
+
+static void
+dumpHeader(const struct tiffDirInfo * const headerP) {
+
+    pm_message("%ux%ux%u raster matrix, oriented %u",
+               headerP->width, headerP->height,
+               headerP->bps * headerP->spp, headerP->orientation);
+    pm_message("%hu bits/sample, %hu samples/pixel",
+               headerP->bps, headerP->spp);
+}
+
+
+
 static void 
 readDirectory(TIFF *               const tiffP,
               bool                 const headerdump,
@@ -285,7 +359,7 @@ readDirectory(TIFF *               const tiffP,
    Read various values of TIFF tags from the TIFF directory, and
    default them if not in there and make guesses where values are
    invalid.  Exit program with error message if required tags aren't
-   there or values are inconsistent or beyond our capabilities.  if
+   there or values are inconsistent or beyond our capabilities.  If
    'headerdump' is true, issue informational messages about what we
    find.
 
@@ -293,7 +367,7 @@ readDirectory(TIFF *               const tiffP,
    input file contains invalid values).  We generally return those
    invalid values to our caller.
 -----------------------------------------------------------------------------*/
-    int rc;
+    int fldPresent;
     unsigned short tiffSpp;
 
     if (headerdump)
@@ -301,65 +375,35 @@ readDirectory(TIFF *               const tiffP,
 
     getBps(tiffP, &headerP->bps);
 
-    rc = TIFFGetFieldDefaulted(tiffP, TIFFTAG_FILLORDER, &headerP->fillorder);
-    rc = TIFFGetField(tiffP, TIFFTAG_SAMPLESPERPIXEL, &tiffSpp);
-    headerP->spp = (rc == 0) ? 1 : tiffSpp;
+    fldPresent =
+        TIFFGetFieldDefaulted(tiffP, TIFFTAG_FILLORDER, &headerP->fillorder);
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_SAMPLESPERPIXEL, &tiffSpp);
+    headerP->spp = fldPresent ? tiffSpp: 1;
 
-    rc = TIFFGetField(tiffP, TIFFTAG_PHOTOMETRIC, &headerP->photomet);
-    if (rc == 0)
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_PHOTOMETRIC, &headerP->photomet);
+    if (!fldPresent)
         pm_error("PHOTOMETRIC tag is not in Tiff file.  "
                  "TIFFGetField() of it failed.\n"
                  "This means the input is not valid Tiff.");
 
-    if (headerP->spp > 1) {
-        rc = TIFFGetField(tiffP, TIFFTAG_PLANARCONFIG, &headerP->planarconfig);
-        if (rc == 0)
-            pm_error("PLANARCONFIG tag is not in Tiff file, though it "
-                     "has more than one sample per pixel.  "
-                     "TIFFGetField() of it failed.  This means the input "
-                     "is not valid Tiff.");
-    } else
+    if (headerP->spp > 1)
+        headerP->planarconfig = planarConfigFmTiff(tiffP);
+    else
         headerP->planarconfig = PLANARCONFIG_CONTIG;
 
-    switch (headerP->planarconfig) {
-    case PLANARCONFIG_CONTIG:
-        break;
-    case PLANARCONFIG_SEPARATE:
-        if (headerP->photomet != PHOTOMETRIC_RGB && 
-            headerP->photomet != PHOTOMETRIC_SEPARATED)
-            pm_error("This program can handle separate planes only "
-                     "with RGB (PHOTOMETRIC tag = %u) or SEPARATED "
-                     "(PHOTOMETRIC tag = %u) data.  The input Tiff file " 
-                     "has PHOTOMETRIC tag = %hu.",
-                     PHOTOMETRIC_RGB, PHOTOMETRIC_SEPARATED,
-                     headerP->photomet);
-        break;
-    default:
-        pm_error("Unrecognized PLANARCONFIG tag value in Tiff input: %u.\n",
-                 headerP->planarconfig);
-    }
+    validatePlanarConfig(headerP->planarconfig, headerP->photomet);
 
-    rc = TIFFGetField(tiffP, TIFFTAG_IMAGEWIDTH, &headerP->width);
-    if (rc == 0)
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_IMAGEWIDTH, &headerP->width);
+    if (!fldPresent)
         pm_error("Input Tiff file is invalid.  It has no IMAGEWIDTH tag.");
-    rc = TIFFGetField(tiffP, TIFFTAG_IMAGELENGTH, &headerP->height);
-    if (rc == 0)
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_IMAGELENGTH, &headerP->height);
+    if (!fldPresent)
         pm_error("Input Tiff file is invalid.  It has no IMAGELENGTH tag.");
 
-    {
-        unsigned short tiffOrientation;
-        int present;
-        present = TIFFGetField(tiffP, TIFFTAG_ORIENTATION, &tiffOrientation);
-        headerP->orientation =
-            present ? tiffOrientation : ORIENTATION_TOPLEFT;
-    }
-    if (headerdump) {
-        pm_message("%ux%ux%u raster matrix, oriented %u",
-                   headerP->width, headerP->height,
-                   headerP->bps * headerP->spp, headerP->orientation);
-        pm_message("%hu bits/sample, %hu samples/pixel",
-                   headerP->bps, headerP->spp);
-    }
+    headerP->orientation = orientationFmTiff(tiffP);
+
+    if (headerdump)
+        dumpHeader(headerP);
 }
 
 
@@ -533,7 +577,7 @@ computeFillorder(unsigned short   const fillorderTag,
 
 
 static void
-analyzeImageType(TIFF *             const tif, 
+analyzeImageType(TIFF *             const tiffP, 
                  unsigned short     const bps, 
                  unsigned short     const spp, 
                  unsigned short     const photomet,
@@ -542,129 +586,147 @@ analyzeImageType(TIFF *             const tif,
                  xel *              const colormap,
                  bool               const headerdump,
                  struct CmdlineInfo const cmdline) {
+/*----------------------------------------------------------------------------
+   Determine from the TIFF header in *tif certain properties of the image
+   as well as the proper format of PNM image for the conversion.
 
-    bool grayscale; 
-
-        /* How come we don't deal with the photometric for the monochrome 
-           case (make sure it's one we know)?  -Bryan 00.03.04
-        */
-        switch (photomet) {
-        case PHOTOMETRIC_MINISBLACK:
-        case PHOTOMETRIC_MINISWHITE:
-            if (spp != 1)
-                pm_error("This grayscale image has %d samples per pixel.  "
-                         "We understand only 1.", spp);
-            grayscale = TRUE;
-            *maxvalP = pm_bitstomaxval(MIN(bps,16));
-            if (headerdump)
-                pm_message("grayscale image, (min=%s) output maxval %u ", 
-                           photomet == PHOTOMETRIC_MINISBLACK ? 
-                           "black" : "white",
-                           *maxvalP
-                           );
-            break;
+   *formatP and *maxvalP are the basic PNM parameters.
+-----------------------------------------------------------------------------*/
+    switch (photomet) {
+    case PHOTOMETRIC_MINISBLACK:
+    case PHOTOMETRIC_MINISWHITE:
+        if (spp != 1)
+            pm_error("This grayscale image has %d samples per pixel.  "
+                     "We understand only 1.", spp);
+
+        *formatP = bps == 1 ? PBM_TYPE : PGM_TYPE;
+
+        *maxvalP = pm_bitstomaxval(MIN(bps, 16));
+
+        if (headerdump)
+            pm_message("grayscale image, (min=%s) output maxval %u ", 
+                       photomet == PHOTOMETRIC_MINISBLACK ? 
+                       "black" : "white",
+                       *maxvalP
+                );
+        break;
             
-        case PHOTOMETRIC_PALETTE: {
-            int i;
-            int numcolors;
-            unsigned short* redcolormap;
-            unsigned short* greencolormap;
-            unsigned short* bluecolormap;
-
-            if (headerdump)
-                pm_message("colormapped");
-
-            if (spp != 1)
-                pm_error("This paletted image has %d samples per pixel.  "
-                         "We understand only 1.", spp);
-
-            if (!TIFFGetField(tif, TIFFTAG_COLORMAP, 
-                              &redcolormap, &greencolormap, &bluecolormap))
-                pm_error("error getting colormaps");
-
-            numcolors = 1 << bps;
-            if (numcolors > MAXCOLORS)
-                pm_error("too many colors");
-            *maxvalP = PNM_MAXMAXVAL;
-            grayscale = FALSE;
-            for (i = 0; i < numcolors; ++i) {
-                xelval r, g, b;
-                r = (long) redcolormap[i] * PNM_MAXMAXVAL / 65535L;
-                g = (long) greencolormap[i] * PNM_MAXMAXVAL / 65535L;
-                b = (long) bluecolormap[i] * PNM_MAXMAXVAL / 65535L;
-                PPM_ASSIGN(colormap[i], r, g, b);
-            }
+    case PHOTOMETRIC_PALETTE: {
+        int fldPresent;
+        int i;
+        int numcolors;
+        unsigned short* redcolormap;
+        unsigned short* greencolormap;
+        unsigned short* bluecolormap;
+
+        if (headerdump)
+            pm_message("colormapped");
+
+        if (spp != 1)
+            pm_error("This paletted image has %d samples per pixel.  "
+                     "We understand only 1.", spp);
+
+        fldPresent = TIFFGetField(
+            tiffP, TIFFTAG_COLORMAP, 
+            &redcolormap, &greencolormap, &bluecolormap);
+
+        if (!fldPresent)
+            pm_error("error getting colormaps");
+
+        numcolors = 1 << bps;
+        if (numcolors > MAXCOLORS)
+            pm_error("too many colors");
+
+        *formatP = PPM_TYPE;
+
+        *maxvalP = PNM_MAXMAXVAL;
+
+        for (i = 0; i < numcolors; ++i) {
+            xelval r, g, b;
+            r = (long) redcolormap[i] * PNM_MAXMAXVAL / 65535L;
+            g = (long) greencolormap[i] * PNM_MAXMAXVAL / 65535L;
+            b = (long) bluecolormap[i] * PNM_MAXMAXVAL / 65535L;
+            PPM_ASSIGN(colormap[i], r, g, b);
         }
+    }
         break;
 
-        case PHOTOMETRIC_SEPARATED: {
-            unsigned short inkset;
-
-            if (headerdump)
-                pm_message("color separation");
-            if (TIFFGetField(tif, TIFFTAG_INKNAMES, &inkset) == 1
-                && inkset != INKSET_CMYK)
-            if (inkset != INKSET_CMYK) 
-                pm_error("This color separation file uses an inkset (%d) "
-                         "we can't handle.  We handle only CMYK.", inkset);
-            if (spp != 4) 
-                pm_error("This CMYK color separation file is %d samples per "
-                         "pixel.  "
-                         "We need 4 samples, though: C, M, Y, and K.  ",
-                         spp);
-            grayscale = FALSE;
-            *maxvalP = (1 << bps) - 1;
-        }
+    case PHOTOMETRIC_SEPARATED: {
+        unsigned short inkset;
+        int fldPresent;
+
+        if (headerdump)
+            pm_message("color separation");
+
+        fldPresent = TIFFGetField(tiffP, TIFFTAG_INKNAMES, &inkset);
+        if (fldPresent && inkset != INKSET_CMYK)
+            pm_error("This color separation file uses an inkset (%d) "
+                     "we can't handle.  We handle only CMYK.", inkset);
+        if (spp != 4) 
+            pm_error("This CMYK color separation file is %d samples per "
+                     "pixel.  "
+                     "We need 4 samples, though: C, M, Y, and K.  ",
+                     spp);
+
+        *formatP = PPM_TYPE;
+
+        *maxvalP = (1 << bps) - 1;
+    }
         break;
             
-        case PHOTOMETRIC_RGB:
-            if (headerdump)
-                pm_message("RGB truecolor");
-            grayscale = FALSE;
+    case PHOTOMETRIC_RGB:
+        if (headerdump)
+            pm_message("RGB truecolor");
 
-            if (spp != 3 && spp != 4)
-                pm_error("This RGB image has %d samples per pixel.  "
-                         "We understand only 3 or 4.", spp);
+        if (spp != 3 && spp != 4)
+            pm_error("This RGB image has %d samples per pixel.  "
+                     "We understand only 3 or 4.", spp);
 
-            *maxvalP = (1 << bps) - 1;
-            break;
+        *formatP = PPM_TYPE;
 
-        case PHOTOMETRIC_MASK:
-            pm_error("don't know how to handle PHOTOMETRIC_MASK");
+        *maxvalP = (1 << bps) - 1;
+        break;
 
-        case PHOTOMETRIC_DEPTH:
-            pm_error("don't know how to handle PHOTOMETRIC_DEPTH");
+    case PHOTOMETRIC_MASK:
+        pm_error("don't know how to handle PHOTOMETRIC_MASK");
 
-        case PHOTOMETRIC_YCBCR:
-            pm_error("don't know how to handle PHOTOMETRIC_YCBCR");
+    case PHOTOMETRIC_DEPTH:
+        pm_error("don't know how to handle PHOTOMETRIC_DEPTH");
 
-        case PHOTOMETRIC_CIELAB:
-            pm_error("don't know how to handle PHOTOMETRIC_CIELAB");
+    case PHOTOMETRIC_YCBCR:
+        pm_error("don't know how to handle PHOTOMETRIC_YCBCR");
 
-        case PHOTOMETRIC_LOGL:
-            pm_error("don't know how to handle PHOTOMETRIC_LOGL");
+    case PHOTOMETRIC_CIELAB:
+        pm_error("don't know how to handle PHOTOMETRIC_CIELAB");
 
-        case PHOTOMETRIC_LOGLUV:
-            pm_error("don't know how to handle PHOTOMETRIC_LOGLUV");
+    case PHOTOMETRIC_LOGL:
+        pm_error("don't know how to handle PHOTOMETRIC_LOGL");
+
+    case PHOTOMETRIC_LOGLUV:
+        pm_error("don't know how to handle PHOTOMETRIC_LOGLUV");
             
-        default:
-            pm_error("unknown photometric: %d", photomet);
-        }
+    default:
+        pm_error("unknown photometric: %d", photomet);
+    }
     if (*maxvalP > PNM_OVERALLMAXVAL)
-        pm_error("bits/sample (%d) in the input image is too large.",
-                 bps);
-    if (grayscale) {
-        if (*maxvalP == 1) {
-            *formatP = PBM_TYPE;
-            pm_message("writing PBM file");
-        } else {
-            *formatP = PGM_TYPE;
-            pm_message("writing PGM file");
-        }
-    } else {
-        *formatP = PPM_TYPE;
-        pm_message("writing PPM file");
+        pm_error("bits/sample (%u) in the input image is too large.", bps);
+}
+
+
+
+static void
+reportOutputFormat(int const format) {
+
+    const char * formatDesc;
+
+    switch (format) {
+    case PBM_TYPE: formatDesc = "PBM"; break;
+    case PGM_TYPE: formatDesc = "PGM"; break;
+    case PPM_TYPE: formatDesc = "PPM"; break;
+    default: assert(false);
     }
+
+    pm_message("writing %s file", formatDesc);
 }
 
 
@@ -775,14 +837,22 @@ spawnWithInputPipe(const char *  const shellCmd,
                 else
                     *errorP = NULL;
             } else {
-                int rc;
+                int terminationStatus;
                 close(fd[PIPE_WRITE]);
                 close(STDIN_FILENO);
                 dup2(fd[PIPE_READ], STDIN_FILENO);
 
-                rc = system(shellCmd);
+                terminationStatus = system(shellCmd);
 
-                exit(rc);
+                if (WIFSIGNALED(terminationStatus))
+                    pm_error("Shell process was killed "
+                             "by a Class %u signal.",
+                             WTERMSIG(terminationStatus));
+                else if (!WIFEXITED(terminationStatus))
+                    pm_error("Shell process died, but its termination status "
+                             "0x%x doesn't make sense", terminationStatus);
+                else
+                    exit(WEXITSTATUS(terminationStatus));
             }
         }
     }
@@ -1364,9 +1434,9 @@ warnBrokenTiffLibrary(TIFF * const tiffP) {
 */
 
     unsigned short tiffOrientation;
-    int present;
-    present = TIFFGetField(tiffP, TIFFTAG_ORIENTATION, &tiffOrientation);
-    if (present) {
+    int fldPresent;
+    fldPresent = TIFFGetField(tiffP, TIFFTAG_ORIENTATION, &tiffOrientation);
+    if (fldPresent) {
         switch (tiffOrientation) {
         case ORIENTATION_LEFTTOP:
         case ORIENTATION_RIGHTTOP:
@@ -1438,16 +1508,47 @@ enum convertDisp {CONV_DONE,
                   CONV_FAILED, 
                   CONV_NOTATTEMPTED};
 
+
+static void
+convertRasterIntoProvidedMemory(pnmOut *           const pnmOutP,
+                                unsigned int       const cols,
+                                unsigned int       const rows,
+                                xelval             const maxval,
+                                TIFF *             const tif,
+                                bool               const verbose,
+                                uint32 *           const raster,
+                                enum convertDisp * const statusP) {
+
+    int const stopOnErrorFalse = false;
+
+    TIFFRGBAImage img;
+    char emsg[1024];
+    int ok;
+                
+    ok = TIFFRGBAImageBegin(&img, tif, stopOnErrorFalse, emsg);
+    if (!ok) {
+        pm_message("%s", emsg);
+        *statusP = CONV_FAILED;
+    } else {
+        int ok;
+        ok = TIFFRGBAImageGet(&img, raster, cols, rows);
+        TIFFRGBAImageEnd(&img) ;
+        if (!ok) {
+            pm_message("%s", emsg);
+            *statusP = CONV_FAILED;
+        } else {
+            *statusP = CONV_DONE;
+            convertTiffRaster(raster, cols, rows, maxval, pnmOutP);
+        }
+    } 
+}
+
+
+
 static void
 convertRasterInMemory(pnmOut *           const pnmOutP,
                       xelval             const maxval,
                       TIFF *             const tif,
-                      unsigned short     const photomet, 
-                      unsigned short     const planarconfig,
-                      unsigned short     const bps,
-                      unsigned short     const spp,
-                      unsigned short     const fillorder,
-                      xel                const colormap[],
                       bool               const verbose,
                       enum convertDisp * const statusP) {
 /*----------------------------------------------------------------------------
@@ -1467,64 +1568,49 @@ convertRasterInMemory(pnmOut *           const pnmOutP,
    programs, we simply abort the program if we are unable to allocate
    memory for other things.
 -----------------------------------------------------------------------------*/
-    unsigned int cols, rows;  /* Dimensions of output image */
+    char emsg[1024];
+    int ok;
 
     if (verbose)
         pm_message("Converting in memory ...");
 
     warnBrokenTiffLibrary(tif);
 
-    getTiffDimensions(tif, &cols, &rows);
-
-    if (rows == 0 || cols == 0) 
-        *statusP = CONV_DONE;
-    else {
-        char emsg[1024];
-        int ok;
-        ok = TIFFRGBAImageOK(tif, emsg);
-        if (!ok) {
-            pm_message("%s", emsg);
-            *statusP = CONV_UNABLE;
-        } else {
-            uint32 * raster;
+    ok = TIFFRGBAImageOK(tif, emsg);
+    if (!ok) {
+        pm_message("%s", emsg);
+        *statusP = CONV_UNABLE;
+    } else {
+        unsigned int cols, rows;  /* Dimensions of output image */
+        getTiffDimensions(tif, &cols, &rows);
 
-            /* Note that TIFFRGBAImageGet() converts any bits per sample
-               to 8.  Maxval of the raster it returns is always 255.
-            */
+        if (rows == 0 || cols == 0) 
+            *statusP = CONV_DONE;
+        else {
             if (cols > UINT_MAX/rows) {
                 pm_message("%u rows of %u columns is too large to compute",
                            rows, cols);
                 *statusP = CONV_OOM;
-                return;
-            }
-
-            MALLOCARRAY(raster, cols * rows);
-            if (raster == NULL) {
-                pm_message("Unable to allocate space for a raster of %u "
-                           "pixels.", cols * rows);
-                *statusP = CONV_OOM;
             } else {
-                int const stopOnErrorFalse = FALSE;
-                TIFFRGBAImage img;
-                int ok;
-                
-                ok = TIFFRGBAImageBegin(&img, tif, stopOnErrorFalse, emsg);
-                if (!ok) {
-                    pm_message("%s", emsg);
-                    *statusP = CONV_FAILED;
+                unsigned int const pixelCt = rows * cols;
+
+                uint32 * raster;
+
+                /* Note that TIFFRGBAImageGet() converts any bits per sample
+                   to 8.  Maxval of the raster it returns is always 255.
+                */
+                MALLOCARRAY(raster, pixelCt);
+                if (raster == NULL) {
+                    pm_message("Unable to allocate space for a raster of %u "
+                               "pixels.", pixelCt);
+                    *statusP = CONV_OOM;
                 } else {
-                    int ok;
-                    ok = TIFFRGBAImageGet(&img, raster, cols, rows);
-                    TIFFRGBAImageEnd(&img) ;
-                    if (!ok) {
-                        pm_message("%s", emsg);
-                        *statusP = CONV_FAILED;
-                    } else {
-                        *statusP = CONV_DONE;
-                        convertTiffRaster(raster, cols, rows, maxval, pnmOutP);
-                    }
-                } 
-                free(raster);
+                    convertRasterIntoProvidedMemory(
+                        pnmOutP, cols, rows, maxval, tif, verbose,
+                        raster, statusP);
+                    
+                    free(raster);
+                }
             }
         }
     }
@@ -1549,11 +1635,7 @@ convertRaster(pnmOut *           const pnmOutP,
     if (byrow || !flipOk)
         status = CONV_NOTATTEMPTED;
     else {
-        convertRasterInMemory(
-            pnmOutP, maxval,
-            tifP, tiffDir.photomet, tiffDir.planarconfig, 
-            tiffDir.bps, tiffDir.spp, fillorder,
-            colormap, verbose, &status);
+        convertRasterInMemory(pnmOutP, maxval, tifP, verbose, &status);
     }
     if (status == CONV_DONE) {
         if (tiffDir.bps > 8)
@@ -1602,6 +1684,8 @@ convertImage(TIFF *             const tifP,
     analyzeImageType(tifP, tiffDir.bps, tiffDir.spp, tiffDir.photomet, 
                      &maxval, &format, colormap, cmdline.headerdump, cmdline);
 
+    reportOutputFormat(format);
+
     pnmOut_init(imageoutFileP, alphaFileP, tiffDir.width, tiffDir.height,
                 tiffDir.orientation, maxval, format, maxval,
                 cmdline.byrow, cmdline.orientraw,
diff --git a/converter/other/winicontopam.c b/converter/other/winicontopam.c
index 664b4ef9..69ce7f05 100644
--- a/converter/other/winicontopam.c
+++ b/converter/other/winicontopam.c
@@ -1156,9 +1156,9 @@ convertPng(const unsigned char * const image,
     imageBuffer.buffer = (unsigned char *)image;
 
     fflush (stdout);
-    pm_system(pm_feed_from_memory, &imageBuffer,
-              NULL /* stdout accepter */, NULL,
-              "pngtopam -alphapam");
+    pm_system_lp("pngtopam", pm_feed_from_memory, &imageBuffer,
+                 NULL /* stdout accepter */, NULL,
+                 "pngtopam", "-alphapam", NULL);
 }
 
 
diff --git a/converter/other/xwdtopnm.c b/converter/other/xwdtopnm.c
index d49a2b09..df3c7375 100644
--- a/converter/other/xwdtopnm.c
+++ b/converter/other/xwdtopnm.c
@@ -23,6 +23,7 @@
 */
 
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/pbm/Makefile b/converter/pbm/Makefile
index 602cb156..352b73de 100644
--- a/converter/pbm/Makefile
+++ b/converter/pbm/Makefile
@@ -31,11 +31,14 @@ endif
 MATHBINARIES =	pbmtopk
 BINARIES =	$(PORTBINARIES) $(MATHBINARIES)
 SCRIPTS =       pbmtox10bm
+EXTRA_OBJECTS = g3ttable.o g3prefab.o
 
-OBJECTS = $(BINARIES:%=%.o)
+OBJECTS = $(BINARIES:%=%.o) $(EXTRA_OBJECTS)
 
 MERGEBINARIES = $(BINARIES)
-MERGE_OBJECTS = $(MERGEBINARIES:%=%.o2)
+MERGE_OBJECTS = $(MERGEBINARIES:%=%.o2) $(EXTRA_OBJECTS)
+
+HAVE_MERGE_COMPAT=YES
 
 SUBDIRS=pbmtoppa
 
@@ -73,7 +76,7 @@ thinkjettopbm.c:%.c:%.c1 $(SRCDIR)/lib/util/lexheader
 	  grep -v "^[[:space:]]*int yywrap(void);" \
 	  >$@
 
-install.bin: install.bin.local
+install.bin install.merge: install.bin.local
 .PHONY: install.bin.local
 install.bin.local: $(PKGDIR)/bin
 # Remember that $(SYMLINK) might just be a copy command.
@@ -81,7 +84,17 @@ install.bin.local: $(PKGDIR)/bin
 	cd $(PKGDIR)/bin ; \
 	$(SYMLINK) pbmtosunicon$(EXE) pbmtoicon$(EXE)
 
+mergecomptrylist:
+	cat /dev/null >$@
+	echo "TRY(\"pbmtoicon\",   main_pbmtosunicon);"     >>$@
+
 thisdirclean: localclean
 .PHONY: localclean
 localclean:
 	-rm -f thinkjettopbm.c
+
+pbmtog3: g3ttable.o g3prefab.o
+pbmtog3: ADDL_OBJECTS = g3ttable.o g3prefab.o
+
+g3topbm: g3ttable.o
+g3topbm: ADDL_OBJECTS = g3ttable.o
diff --git a/converter/pbm/cmuwmtopbm.c b/converter/pbm/cmuwmtopbm.c
index ccf8cfc9..be8a7fc1 100644
--- a/converter/pbm/cmuwmtopbm.c
+++ b/converter/pbm/cmuwmtopbm.c
@@ -68,8 +68,8 @@ readCmuwmHeader(FILE *         const ifP,
 
 
 int
-main(int     argc,
-     char * argv[]) {
+main(int           argc,
+     const char ** argv) {
 
     FILE * ifP;
     unsigned char * bitrow;
@@ -78,7 +78,7 @@ main(int     argc,
 
     const char * inputFileName;
 
-    pbm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     if (argc-1 > 1)
         pm_error("Too many arguments (%u).  "
@@ -117,6 +117,7 @@ main(int     argc,
         pbm_writepbmrow_packed(stdout, bitrow, cols, 0);
     }
 
+    pbm_freerow_packed(bitrow);
     pm_close(ifP);
     pm_close(stdout);
 
diff --git a/converter/pbm/g3.h b/converter/pbm/g3.h
index e982f2da..3e216a78 100644
--- a/converter/pbm/g3.h
+++ b/converter/pbm/g3.h
@@ -21,126 +21,24 @@
    the subject of this header file.  It also specifies layers
    underneath the bit stream.
 
-   There is also the newer G4.  
+   There is also the newer G4.
 */
-
-typedef struct g3TableEntry {
-    short int code;
-    short int length;
-} g3TableEntry;
-
-static struct g3TableEntry ttable[] = {
-/*    TERMWHITE           TERMBLACK   */
-    { 0x35, 8 },    { 0x37, 10 },       /* white 0 , black 0 */
-    { 0x07, 6 },    { 0x02,  3 },
-    { 0x07, 4 },    { 0x03,  2 },
-    { 0x08, 4 },    { 0x02,  2 },
-    { 0x0b, 4 },    { 0x03,  3 },
-    { 0x0c, 4 },    { 0x03,  4 },
-    { 0x0e, 4 },    { 0x02,  4 },
-    { 0x0f, 4 },    { 0x03,  5 },
-    { 0x13, 5 },    { 0x05,  6 },
-    { 0x14, 5 },    { 0x04,  6 },
-    { 0x07, 5 },    { 0x04,  7 },
-    { 0x08, 5 },    { 0x05,  7 },
-    { 0x08, 6 },    { 0x07,  7 },
-    { 0x03, 6 },    { 0x04,  8 },
-    { 0x34, 6 },    { 0x07,  8 },
-    { 0x35, 6 },    { 0x18,  9 },
-    { 0x2a, 6 },    { 0x17, 10 },
-    { 0x2b, 6 },    { 0x18, 10 },
-    { 0x27, 7 },    { 0x08, 10 },
-    { 0x0c, 7 },    { 0x67, 11 },
-    { 0x08, 7 },    { 0x68, 11 },
-    { 0x17, 7 },    { 0x6c, 11 },
-    { 0x03, 7 },    { 0x37, 11 },
-    { 0x04, 7 },    { 0x28, 11 },
-    { 0x28, 7 },    { 0x17, 11 },
-    { 0x2b, 7 },    { 0x18, 11 },
-    { 0x13, 7 },    { 0xca, 12 },
-    { 0x24, 7 },    { 0xcb, 12 },
-    { 0x18, 7 },    { 0xcc, 12 },
-    { 0x02, 8 },    { 0xcd, 12 },
-    { 0x03, 8 },    { 0x68, 12 },
-    { 0x1a, 8 },    { 0x69, 12 },
-    { 0x1b, 8 },    { 0x6a, 12 },
-    { 0x12, 8 },    { 0x6b, 12 },
-    { 0x13, 8 },    { 0xd2, 12 },
-    { 0x14, 8 },    { 0xd3, 12 },
-    { 0x15, 8 },    { 0xd4, 12 },
-    { 0x16, 8 },    { 0xd5, 12 },
-    { 0x17, 8 },    { 0xd6, 12 },
-    { 0x28, 8 },    { 0xd7, 12 },
-    { 0x29, 8 },    { 0x6c, 12 },
-    { 0x2a, 8 },    { 0x6d, 12 },
-    { 0x2b, 8 },    { 0xda, 12 },
-    { 0x2c, 8 },    { 0xdb, 12 },
-    { 0x2d, 8 },    { 0x54, 12 },
-    { 0x04, 8 },    { 0x55, 12 },
-    { 0x05, 8 },    { 0x56, 12 },
-    { 0x0a, 8 },    { 0x57, 12 },
-    { 0x0b, 8 },    { 0x64, 12 },
-    { 0x52, 8 },    { 0x65, 12 },
-    { 0x53, 8 },    { 0x52, 12 },
-    { 0x54, 8 },    { 0x53, 12 },
-    { 0x55, 8 },    { 0x24, 12 },
-    { 0x24, 8 },    { 0x37, 12 },
-    { 0x25, 8 },    { 0x38, 12 },
-    { 0x58, 8 },    { 0x27, 12 },
-    { 0x59, 8 },    { 0x28, 12 },
-    { 0x5a, 8 },    { 0x58, 12 },
-    { 0x5b, 8 },    { 0x59, 12 },
-    { 0x4a, 8 },    { 0x2b, 12 },
-    { 0x4b, 8 },    { 0x2c, 12 },
-    { 0x32, 8 },    { 0x5a, 12 },
-    { 0x33, 8 },    { 0x66, 12 },
-    { 0x34, 8 },    { 0x67, 12 },       /* white 63 , black 63 */
-
-/* mtable */    
-/*    MKUPWHITE           MKUPBLACK   */
-    { 0x00, 0 },    { 0x00,  0 },   /* dummy to simplify pointer math */
-    { 0x1b, 5 },    { 0x0f, 10 },   /* white 64 , black 64 */
-    { 0x12, 5 },    { 0xc8, 12 },
-    { 0x17, 6 },    { 0xc9, 12 },
-    { 0x37, 7 },    { 0x5b, 12 },
-    { 0x36, 8 },    { 0x33, 12 },
-    { 0x37, 8 },    { 0x34, 12 },
-    { 0x64, 8 },    { 0x35, 12 },
-    { 0x65, 8 },    { 0x6c, 13 },
-    { 0x68, 8 },    { 0x6d, 13 },
-    { 0x67, 8 },    { 0x4a, 13 },
-    { 0xcc, 9 },    { 0x4b, 13 },
-    { 0xcd, 9 },    { 0x4c, 13 },
-    { 0xd2, 9 },    { 0x4d, 13 },
-    { 0xd3, 9 },    { 0x72, 13 },
-    { 0xd4, 9 },    { 0x73, 13 },
-    { 0xd5, 9 },    { 0x74, 13 },
-    { 0xd6, 9 },    { 0x75, 13 },
-    { 0xd7, 9 },    { 0x76, 13 },
-    { 0xd8, 9 },    { 0x77, 13 },
-    { 0xd9, 9 },    { 0x52, 13 },
-    { 0xda, 9 },    { 0x53, 13 },
-    { 0xdb, 9 },    { 0x54, 13 },
-    { 0x98, 9 },    { 0x55, 13 },
-    { 0x99, 9 },    { 0x5a, 13 },
-    { 0x9a, 9 },    { 0x5b, 13 },
-    { 0x18, 6 },    { 0x64, 13 },
-    { 0x9b, 9 },    { 0x65, 13 },
-    { 0x08, 11 },   { 0x08, 11 },        /* extable len = 1792 */
-    { 0x0c, 11 },   { 0x0c, 11 },
-    { 0x0d, 11 },   { 0x0d, 11 },
-    { 0x12, 12 },   { 0x12, 12 },
-    { 0x13, 12 },   { 0x13, 12 },
-    { 0x14, 12 },   { 0x14, 12 },
-    { 0x15, 12 },   { 0x15, 12 },
-    { 0x16, 12 },   { 0x16, 12 },
-    { 0x17, 12 },   { 0x17, 12 },
-    { 0x1c, 12 },   { 0x1c, 12 },
-    { 0x1d, 12 },   { 0x1d, 12 },
-    { 0x1e, 12 },   { 0x1e, 12 },
-    { 0x1f, 12 },   { 0x1f, 12 },
+#include "pm_config.h"  /* uint32_t */
+
+struct BitString {
+    /* A string of bits, up to as many fit in 32 bits. */
+    uint32_t     intBuffer;
+        /* The bits are in the 'bitCount' least significant bit positions
+           of this number.  The rest of the bits of this number are always
+           zero.
+        */
+    unsigned int bitCount;
+        /* The length of the bit string */
+
+    /* Example:  The bit string 010100 would be represented by
+       bitCount = 6, intBuffer = 20
+       (N.B. 20 = 00000000 00000000 00000000 00010100 in binary)
+    */
 };
 
-#define mtable ((ttable)+64*2)
-
 #endif
diff --git a/converter/pbm/g3prefab.c b/converter/pbm/g3prefab.c
new file mode 100644
index 00000000..4c45b6a2
--- /dev/null
+++ b/converter/pbm/g3prefab.c
@@ -0,0 +1,286 @@
+/* Prefabricated G3 Huffman code
+
+   Most g3 fax encoders scan the input data bitwise, count run-lengths
+   and emit the corresponding Huffman codes.  This is a different approach.
+
+   8 bits of raw input data is read and Huffman codes are emitted directly,
+   reading from a look-up table.
+
+   For example if the input is WWBBWWWB then the Huffman codes for
+   White(2), Black(2), White(3), Black(1) are combined and emitted.
+
+   In reality things are a somewhat more complicated for the runs on
+   both ends of the input byte are likely to be parts of longer runs
+   encompassing multiple bytes.
+
+   This method has one serious drawback: It does not work well
+   with adaptive Huffman coding schemes.  This is, of course, never
+   a problem with CCITT G3 fax images for which the Huffman code table
+   is fixed by standard.  For adaptive Huffman coding schemes, one
+   should consider preparing a code table like the one below which
+   encodes mixed black-white sequences up to a certain length.
+
+*/
+#include "g3.h"
+
+#include "g3prefab.h"
+
+ struct PrefabCode const g3prefab_code[256] =
+{
+{ 0, 8, {   0x0000,   0}},   /* 000 */
+{ 7, 1, {   0x0000,   0}},   /* 001 */
+{ 6, 1, {   0x0002,   3}},   /* 002 */
+{ 6, 2, {   0x0000,   0}},   /* 003 */
+{ 5, 2, {   0x0002,   3}},   /* 004 */
+{ 5, 1, {   0x0087,   9}},   /* 005 */
+{ 5, 1, {   0x0003,   2}},   /* 006 */
+{ 5, 3, {   0x0000,   0}},   /* 007 */
+{ 4, 3, {   0x0002,   3}},   /* 008 */
+{ 4, 1, {   0x0027,   7}},   /* 009 */
+{ 4, 1, {   0x043a,  12}},   /* 010 */
+{ 4, 2, {   0x0087,   9}},   /* 011 */
+{ 4, 2, {   0x0003,   2}},   /* 012 */
+{ 4, 1, {   0x00c7,   8}},   /* 013 */
+{ 4, 1, {   0x0002,   2}},   /* 014 */
+{ 4, 4, {   0x0000,   0}},   /* 015 */
+{ 3, 4, {   0x0002,   3}},   /* 016 */
+{ 3, 1, {   0x0028,   7}},   /* 017 */
+{ 3, 1, {   0x013a,  10}},   /* 018 */
+{ 3, 2, {   0x0027,   7}},   /* 019 */
+{ 3, 2, {   0x043a,  12}},   /* 020 */
+{ 3, 1, {  0x10e87,  18}},   /* 021 */
+{ 3, 1, {   0x021f,  11}},   /* 022 */
+{ 3, 3, {   0x0087,   9}},   /* 023 */
+{ 3, 3, {   0x0003,   2}},   /* 024 */
+{ 3, 1, {   0x0037,   6}},   /* 025 */
+{ 3, 1, {   0x063a,  11}},   /* 026 */
+{ 3, 2, {   0x00c7,   8}},   /* 027 */
+{ 3, 2, {   0x0002,   2}},   /* 028 */
+{ 3, 1, {   0x0087,   8}},   /* 029 */
+{ 3, 1, {   0x0003,   3}},   /* 030 */
+{ 3, 5, {   0x0000,   0}},   /* 031 */
+{ 2, 5, {   0x0002,   3}},   /* 032 */
+{ 2, 1, {   0x002b,   7}},   /* 033 */
+{ 2, 1, {   0x0142,  10}},   /* 034 */
+{ 2, 2, {   0x0028,   7}},   /* 035 */
+{ 2, 2, {   0x013a,  10}},   /* 036 */
+{ 2, 1, {   0x4e87,  16}},   /* 037 */
+{ 2, 1, {   0x009f,   9}},   /* 038 */
+{ 2, 3, {   0x0027,   7}},   /* 039 */
+{ 2, 3, {   0x043a,  12}},   /* 040 */
+{ 2, 1, {   0x43a7,  16}},   /* 041 */
+{ 2, 1, {  0x8743a,  21}},   /* 042 */
+{ 2, 2, {  0x10e87,  18}},   /* 043 */
+{ 2, 2, {   0x021f,  11}},   /* 044 */
+{ 2, 1, {   0x87c7,  17}},   /* 045 */
+{ 2, 1, {   0x021e,  11}},   /* 046 */
+{ 2, 4, {   0x0087,   9}},   /* 047 */
+{ 2, 4, {   0x0003,   2}},   /* 048 */
+{ 2, 1, {   0x0038,   6}},   /* 049 */
+{ 2, 1, {   0x01ba,   9}},   /* 050 */
+{ 2, 2, {   0x0037,   6}},   /* 051 */
+{ 2, 2, {   0x063a,  11}},   /* 052 */
+{ 2, 1, {  0x18e87,  17}},   /* 053 */
+{ 2, 1, {   0x031f,  10}},   /* 054 */
+{ 2, 3, {   0x00c7,   8}},   /* 055 */
+{ 2, 3, {   0x0002,   2}},   /* 056 */
+{ 2, 1, {   0x0027,   6}},   /* 057 */
+{ 2, 1, {   0x043a,  11}},   /* 058 */
+{ 2, 2, {   0x0087,   8}},   /* 059 */
+{ 2, 2, {   0x0003,   3}},   /* 060 */
+{ 2, 1, {   0x00c7,   9}},   /* 061 */
+{ 2, 1, {   0x0003,   4}},   /* 062 */
+{ 2, 6, {   0x0000,   0}},   /* 063 */
+{ 1, 6, {   0x0002,   3}},   /* 064 */
+{ 1, 1, {   0x002c,   7}},   /* 065 */
+{ 1, 1, {   0x015a,  10}},   /* 066 */
+{ 1, 2, {   0x002b,   7}},   /* 067 */
+{ 1, 2, {   0x0142,  10}},   /* 068 */
+{ 1, 1, {   0x5087,  16}},   /* 069 */
+{ 1, 1, {   0x00a3,   9}},   /* 070 */
+{ 1, 3, {   0x0028,   7}},   /* 071 */
+{ 1, 3, {   0x013a,  10}},   /* 072 */
+{ 1, 1, {   0x13a7,  14}},   /* 073 */
+{ 1, 1, {  0x2743a,  19}},   /* 074 */
+{ 1, 2, {   0x4e87,  16}},   /* 075 */
+{ 1, 2, {   0x009f,   9}},   /* 076 */
+{ 1, 1, {   0x27c7,  15}},   /* 077 */
+{ 1, 1, {   0x009e,   9}},   /* 078 */
+{ 1, 4, {   0x0027,   7}},   /* 079 */
+{ 1, 4, {   0x043a,  12}},   /* 080 */
+{ 1, 1, {   0x43a8,  16}},   /* 081 */
+{ 1, 1, {  0x21d3a,  19}},   /* 082 */
+{ 1, 2, {   0x43a7,  16}},   /* 083 */
+{ 1, 2, {  0x8743a,  21}},   /* 084 */
+{ 1, 1, {0x21d0e87,  27}},   /* 085 */
+{ 1, 1, {  0x43a1f,  20}},   /* 086 */
+{ 1, 3, {  0x10e87,  18}},   /* 087 */
+{ 1, 3, {   0x021f,  11}},   /* 088 */
+{ 1, 1, {   0x21f7,  15}},   /* 089 */
+{ 1, 1, {  0x43e3a,  20}},   /* 090 */
+{ 1, 2, {   0x87c7,  17}},   /* 091 */
+{ 1, 2, {   0x021e,  11}},   /* 092 */
+{ 1, 1, {   0x8787,  17}},   /* 093 */
+{ 1, 1, {   0x043b,  12}},   /* 094 */
+{ 1, 5, {   0x0087,   9}},   /* 095 */
+{ 1, 5, {   0x0003,   2}},   /* 096 */
+{ 1, 1, {   0x003b,   6}},   /* 097 */
+{ 1, 1, {   0x01c2,   9}},   /* 098 */
+{ 1, 2, {   0x0038,   6}},   /* 099 */
+{ 1, 2, {   0x01ba,   9}},   /* 100 */
+{ 1, 1, {   0x6e87,  15}},   /* 101 */
+{ 1, 1, {   0x00df,   8}},   /* 102 */
+{ 1, 3, {   0x0037,   6}},   /* 103 */
+{ 1, 3, {   0x063a,  11}},   /* 104 */
+{ 1, 1, {   0x63a7,  15}},   /* 105 */
+{ 1, 1, {  0xc743a,  20}},   /* 106 */
+{ 1, 2, {  0x18e87,  17}},   /* 107 */
+{ 1, 2, {   0x031f,  10}},   /* 108 */
+{ 1, 1, {   0xc7c7,  16}},   /* 109 */
+{ 1, 1, {   0x031e,  10}},   /* 110 */
+{ 1, 4, {   0x00c7,   8}},   /* 111 */
+{ 1, 4, {   0x0002,   2}},   /* 112 */
+{ 1, 1, {   0x0028,   6}},   /* 113 */
+{ 1, 1, {   0x013a,   9}},   /* 114 */
+{ 1, 2, {   0x0027,   6}},   /* 115 */
+{ 1, 2, {   0x043a,  11}},   /* 116 */
+{ 1, 1, {  0x10e87,  17}},   /* 117 */
+{ 1, 1, {   0x021f,  10}},   /* 118 */
+{ 1, 3, {   0x0087,   8}},   /* 119 */
+{ 1, 3, {   0x0003,   3}},   /* 120 */
+{ 1, 1, {   0x0037,   7}},   /* 121 */
+{ 1, 1, {   0x063a,  12}},   /* 122 */
+{ 1, 2, {   0x00c7,   9}},   /* 123 */
+{ 1, 2, {   0x0003,   4}},   /* 124 */
+{ 1, 1, {   0x00c7,  10}},   /* 125 */
+{ 1, 1, {   0x0002,   4}},   /* 126 */
+{ 1, 7, {   0x0000,   0}},   /* 127 */
+{ 1, 7, {   0x0000,   0}},   /* 128 */
+{ 1, 1, {   0x000e,   4}},   /* 129 */
+{ 1, 1, {   0x0062,   7}},   /* 130 */
+{ 1, 2, {   0x000c,   4}},   /* 131 */
+{ 1, 2, {   0x005a,   7}},   /* 132 */
+{ 1, 1, {   0x1687,  13}},   /* 133 */
+{ 1, 1, {   0x002f,   6}},   /* 134 */
+{ 1, 3, {   0x000b,   4}},   /* 135 */
+{ 1, 3, {   0x0042,   7}},   /* 136 */
+{ 1, 1, {   0x0427,  11}},   /* 137 */
+{ 1, 1, {   0x843a,  16}},   /* 138 */
+{ 1, 2, {   0x1087,  13}},   /* 139 */
+{ 1, 2, {   0x0023,   6}},   /* 140 */
+{ 1, 1, {   0x08c7,  12}},   /* 141 */
+{ 1, 1, {   0x0022,   6}},   /* 142 */
+{ 1, 4, {   0x0008,   4}},   /* 143 */
+{ 1, 4, {   0x003a,   7}},   /* 144 */
+{ 1, 1, {   0x03a8,  11}},   /* 145 */
+{ 1, 1, {   0x1d3a,  14}},   /* 146 */
+{ 1, 2, {   0x03a7,  11}},   /* 147 */
+{ 1, 2, {   0x743a,  16}},   /* 148 */
+{ 1, 1, { 0x1d0e87,  22}},   /* 149 */
+{ 1, 1, {   0x3a1f,  15}},   /* 150 */
+{ 1, 3, {   0x0e87,  13}},   /* 151 */
+{ 1, 3, {   0x001f,   6}},   /* 152 */
+{ 1, 1, {   0x01f7,  10}},   /* 153 */
+{ 1, 1, {   0x3e3a,  15}},   /* 154 */
+{ 1, 2, {   0x07c7,  12}},   /* 155 */
+{ 1, 2, {   0x001e,   6}},   /* 156 */
+{ 1, 1, {   0x0787,  12}},   /* 157 */
+{ 1, 1, {   0x003b,   7}},   /* 158 */
+{ 1, 5, {   0x0007,   4}},   /* 159 */
+{ 1, 5, {   0x003a,   9}},   /* 160 */
+{ 1, 1, {   0x03ab,  13}},   /* 161 */
+{ 1, 1, {   0x1d42,  16}},   /* 162 */
+{ 1, 2, {   0x03a8,  13}},   /* 163 */
+{ 1, 2, {   0x1d3a,  16}},   /* 164 */
+{ 1, 1, {  0x74e87,  22}},   /* 165 */
+{ 1, 1, {   0x0e9f,  15}},   /* 166 */
+{ 1, 3, {   0x03a7,  13}},   /* 167 */
+{ 1, 3, {   0x743a,  18}},   /* 168 */
+{ 1, 1, {  0x743a7,  22}},   /* 169 */
+{ 1, 1, { 0xe8743a,  27}},   /* 170 */
+{ 1, 2, { 0x1d0e87,  24}},   /* 171 */
+{ 1, 2, {   0x3a1f,  17}},   /* 172 */
+{ 1, 1, {  0xe87c7,  23}},   /* 173 */
+{ 1, 1, {   0x3a1e,  17}},   /* 174 */
+{ 1, 4, {   0x0e87,  15}},   /* 175 */
+{ 1, 4, {   0x001f,   8}},   /* 176 */
+{ 1, 1, {   0x01f8,  12}},   /* 177 */
+{ 1, 1, {   0x0fba,  15}},   /* 178 */
+{ 1, 2, {   0x01f7,  12}},   /* 179 */
+{ 1, 2, {   0x3e3a,  17}},   /* 180 */
+{ 1, 1, {  0xf8e87,  23}},   /* 181 */
+{ 1, 1, {   0x1f1f,  16}},   /* 182 */
+{ 1, 3, {   0x07c7,  14}},   /* 183 */
+{ 1, 3, {   0x001e,   8}},   /* 184 */
+{ 1, 1, {   0x01e7,  12}},   /* 185 */
+{ 1, 1, {   0x3c3a,  17}},   /* 186 */
+{ 1, 2, {   0x0787,  14}},   /* 187 */
+{ 1, 2, {   0x003b,   9}},   /* 188 */
+{ 1, 1, {   0x0ec7,  15}},   /* 189 */
+{ 1, 1, {   0x0073,  10}},   /* 190 */
+{ 1, 6, {   0x0007,   6}},   /* 191 */
+{ 2, 6, {   0x0000,   0}},   /* 192 */
+{ 2, 1, {   0x000c,   4}},   /* 193 */
+{ 2, 1, {   0x005a,   7}},   /* 194 */
+{ 2, 2, {   0x000b,   4}},   /* 195 */
+{ 2, 2, {   0x0042,   7}},   /* 196 */
+{ 2, 1, {   0x1087,  13}},   /* 197 */
+{ 2, 1, {   0x0023,   6}},   /* 198 */
+{ 2, 3, {   0x0008,   4}},   /* 199 */
+{ 2, 3, {   0x003a,   7}},   /* 200 */
+{ 2, 1, {   0x03a7,  11}},   /* 201 */
+{ 2, 1, {   0x743a,  16}},   /* 202 */
+{ 2, 2, {   0x0e87,  13}},   /* 203 */
+{ 2, 2, {   0x001f,   6}},   /* 204 */
+{ 2, 1, {   0x07c7,  12}},   /* 205 */
+{ 2, 1, {   0x001e,   6}},   /* 206 */
+{ 2, 4, {   0x0007,   4}},   /* 207 */
+{ 2, 4, {   0x003a,   9}},   /* 208 */
+{ 2, 1, {   0x03a8,  13}},   /* 209 */
+{ 2, 1, {   0x1d3a,  16}},   /* 210 */
+{ 2, 2, {   0x03a7,  13}},   /* 211 */
+{ 2, 2, {   0x743a,  18}},   /* 212 */
+{ 2, 1, { 0x1d0e87,  24}},   /* 213 */
+{ 2, 1, {   0x3a1f,  17}},   /* 214 */
+{ 2, 3, {   0x0e87,  15}},   /* 215 */
+{ 2, 3, {   0x001f,   8}},   /* 216 */
+{ 2, 1, {   0x01f7,  12}},   /* 217 */
+{ 2, 1, {   0x3e3a,  17}},   /* 218 */
+{ 2, 2, {   0x07c7,  14}},   /* 219 */
+{ 2, 2, {   0x001e,   8}},   /* 220 */
+{ 2, 1, {   0x0787,  14}},   /* 221 */
+{ 2, 1, {   0x003b,   9}},   /* 222 */
+{ 2, 5, {   0x0007,   6}},   /* 223 */
+{ 3, 5, {   0x0000,   0}},   /* 224 */
+{ 3, 1, {   0x000b,   4}},   /* 225 */
+{ 3, 1, {   0x0042,   7}},   /* 226 */
+{ 3, 2, {   0x0008,   4}},   /* 227 */
+{ 3, 2, {   0x003a,   7}},   /* 228 */
+{ 3, 1, {   0x0e87,  13}},   /* 229 */
+{ 3, 1, {   0x001f,   6}},   /* 230 */
+{ 3, 3, {   0x0007,   4}},   /* 231 */
+{ 3, 3, {   0x003a,   9}},   /* 232 */
+{ 3, 1, {   0x03a7,  13}},   /* 233 */
+{ 3, 1, {   0x743a,  18}},   /* 234 */
+{ 3, 2, {   0x0e87,  15}},   /* 235 */
+{ 3, 2, {   0x001f,   8}},   /* 236 */
+{ 3, 1, {   0x07c7,  14}},   /* 237 */
+{ 3, 1, {   0x001e,   8}},   /* 238 */
+{ 3, 4, {   0x0007,   6}},   /* 239 */
+{ 4, 4, {   0x0000,   0}},   /* 240 */
+{ 4, 1, {   0x0008,   4}},   /* 241 */
+{ 4, 1, {   0x003a,   7}},   /* 242 */
+{ 4, 2, {   0x0007,   4}},   /* 243 */
+{ 4, 2, {   0x003a,   9}},   /* 244 */
+{ 4, 1, {   0x0e87,  15}},   /* 245 */
+{ 4, 1, {   0x001f,   8}},   /* 246 */
+{ 4, 3, {   0x0007,   6}},   /* 247 */
+{ 5, 3, {   0x0000,   0}},   /* 248 */
+{ 5, 1, {   0x0007,   4}},   /* 249 */
+{ 5, 1, {   0x003a,   9}},   /* 250 */
+{ 5, 2, {   0x0007,   6}},   /* 251 */
+{ 6, 2, {   0x0000,   0}},   /* 252 */
+{ 6, 1, {   0x0007,   6}},   /* 253 */
+{ 7, 1, {   0x0000,   0}},   /* 254 */
+{ 0, 8, {   0x0000,   0}},   /* 255 */
+  };
diff --git a/converter/pbm/g3prefab.h b/converter/pbm/g3prefab.h
new file mode 100644
index 00000000..4fa4751a
--- /dev/null
+++ b/converter/pbm/g3prefab.h
@@ -0,0 +1,14 @@
+#ifndef G3_PREFAB_H_INCLUDED
+#define G3_PREFAB_H_INCLUDED
+
+struct PrefabCode {
+    unsigned int leadBits;
+    unsigned int trailBits;
+    struct BitString activeBits;
+};
+
+
+extern struct PrefabCode const g3prefab_code[256];
+
+#endif
+
diff --git a/converter/pbm/g3topbm.c b/converter/pbm/g3topbm.c
index fcac1981..5d98fcb2 100644
--- a/converter/pbm/g3topbm.c
+++ b/converter/pbm/g3topbm.c
@@ -4,7 +4,7 @@
 
   This program reads a Group 3 FAX file and produces a PBM image.
 
-  Bryan Henderson wrote this on August 5, 2004 and contributed it to 
+  Bryan Henderson wrote this on August 5, 2004 and contributed it to
   the public domain.
 
   This program is designed to be a drop-in replacement for the program
@@ -18,6 +18,7 @@
   contributing their work to the public domain.
 ===========================================================================*/
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE   /* Make nstring.h define strcaseeq() */
 
 #include "pm_c_util.h"
@@ -26,6 +27,7 @@
 #include "nstring.h"
 #include "mallocvar.h"
 #include "g3.h"
+#include "g3ttable.h"
 #include "bitreverse.h"
 #include "bitarith.h"
 
@@ -43,11 +45,37 @@
 
 #define HASHSIZE 1021
 
-static g3TableEntry * whash[HASHSIZE];
-static g3TableEntry * bhash[HASHSIZE];
+#define MAXFILLBITS (5 * 9600)
 
+/*
+Fill bits are for flow control.  This was important when DRAM was
+expensive and fax machines came with small buffers.
 
-struct cmdlineInfo {
+If data arrives too quickly it may overflow the buffer of the
+receiving device.  On sending devices transmission time of compressed
+data representing a single row can be shorter than the time required
+to scan and encode.  The CCITT standard allows sending devices to
+insert fill bits to put communication on hold in these cases.
+
+By the CCITT standard, the maximum transmission time for one row is:
+
+100 - 400 pixels/inch 13 seconds
+(standard mode: 200 pixels/inch)
+600 pixels/inch 19 seconds
+1200 pixels/inch 37 seconds
+
+If one row is not received within the above limits, the receiving
+machine must disconnect the line.
+
+The receiver may be less patient.  It may opt to disconnect if one row
+is not received within 5 seconds.
+*/
+
+static G3TableEntry * whash[HASHSIZE];
+static G3TableEntry * bhash[HASHSIZE];
+
+
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
@@ -62,8 +90,8 @@ struct cmdlineInfo {
 
 
 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.
@@ -84,20 +112,20 @@ parseCommandLine(int argc, char ** const argv,
             0);
     OPTENT3(0, "kludge",           OPT_FLAG,  NULL, &cmdlineP->kludge,
             0);
-    OPTENT3(0, "stretch",          OPT_FLAG,  NULL, &cmdlineP->stretch, 
+    OPTENT3(0, "stretch",          OPT_FLAG,  NULL, &cmdlineP->stretch,
             0);
-    OPTENT3(0, "stop_error",       OPT_FLAG,  NULL, &cmdlineP->stop_error, 
+    OPTENT3(0, "stop_error",       OPT_FLAG,  NULL, &cmdlineP->stop_error,
             0);
     OPTENT3(0, "width",            OPT_UINT,  &cmdlineP->expectedLineSize,
             &widthSpec,                0);
     OPTENT3(0, "paper_size",       OPT_STRING, &paperSize,
             &paper_sizeSpec,           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, 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 (widthSpec && paper_sizeSpec)
@@ -124,7 +152,7 @@ parseCommandLine(int argc, char ** const argv,
     } else
         cmdlineP->expectedLineSize = 0;
 
-    if (argc-1 == 0) 
+    if (argc-1 == 0)
         cmdlineP->inputFilespec = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
@@ -135,7 +163,7 @@ parseCommandLine(int argc, char ** const argv,
 
 
 
-struct bitStream {
+struct BitStream {
 
     FILE * fileP;
     bool reversebits;
@@ -155,7 +183,7 @@ struct bitStream {
 
 
 static void
-readBit(struct bitStream * const bitStreamP,
+readBit(struct BitStream * const bitStreamP,
         unsigned int *     const bitP,
         const char **      const errorP) {
 /*----------------------------------------------------------------------------
@@ -188,7 +216,7 @@ readBit(struct bitStream * const bitStreamP,
 
 
 static void
-readBitAndDetectEol(struct bitStream * const bitStreamP,
+readBitAndDetectEol(struct BitStream * const bitStreamP,
                     unsigned int *     const bitP,
                     bool *             const eolP,
                     const char **      const errorP) {
@@ -216,10 +244,10 @@ readBitAndDetectEol(struct bitStream * const bitStreamP,
 
 
 static void
-initBitStream(struct bitStream * const bitStreamP,
+initBitStream(struct BitStream * const bitStreamP,
               FILE *             const fileP,
               bool               const reversebits) {
-    
+
     bitStreamP->fileP        = fileP;
     bitStreamP->reversebits  = reversebits;
     bitStreamP->shbit        = 0x00;
@@ -229,17 +257,17 @@ initBitStream(struct bitStream * const bitStreamP,
 
 
 static void
-skipToNextLine(struct bitStream * const bitStreamP) {
+skipToNextLine(struct BitStream * const bitStreamP) {
 
     bool eol;
     const char * error;
 
     eol = FALSE;
     error = NULL;
-    
+
     while (!eol && !error) {
         unsigned int bit;
-        
+
         readBitAndDetectEol(bitStreamP, &bit, &eol, &error);
     }
 }
@@ -247,16 +275,16 @@ skipToNextLine(struct bitStream * const bitStreamP) {
 
 
 static void
-addtohash(g3TableEntry *     hash[], 
-          g3TableEntry       table[], 
-          unsigned int const n, 
-          int          const a, 
+addtohash(G3TableEntry *     hash[],
+          G3TableEntry       table[],
+          unsigned int const n,
+          int          const a,
           int          const b) {
-    
+
     unsigned int i;
 
     for (i = 0; i < n; ++i) {
-        g3TableEntry * const teP = &table[i*2];
+        G3TableEntry * const teP = &table[i*2];
         unsigned int const pos =
             ((teP->length + a) * (teP->code + b)) % HASHSIZE;
         if (hash[pos])
@@ -267,15 +295,15 @@ addtohash(g3TableEntry *     hash[],
 
 
 
-static g3TableEntry *
-hashfind(g3TableEntry *       hash[], 
-         int            const length, 
-         int            const code, 
-         int            const a, 
+static G3TableEntry *
+hashfind(G3TableEntry *       hash[],
+         int            const length,
+         int            const code,
+         int            const a,
          int            const b) {
 
     unsigned int pos;
-    g3TableEntry * te;
+    G3TableEntry * te;
 
     pos = ((length + a) * (code + b)) % HASHSIZE;
     te = hash[pos];
@@ -285,19 +313,19 @@ hashfind(g3TableEntry *       hash[],
 
 
 static void
-buildHashes(g3TableEntry * (*whashP)[HASHSIZE],
-            g3TableEntry * (*bhashP)[HASHSIZE]) {
+buildHashes(G3TableEntry * (*whashP)[HASHSIZE],
+            G3TableEntry * (*bhashP)[HASHSIZE]) {
 
     unsigned int i;
 
     for (i = 0; i < HASHSIZE; ++i)
         (*whashP)[i] = (*bhashP)[i] = NULL;
 
-    addtohash(*whashP, &ttable[0], 64, WHASHA, WHASHB);
-    addtohash(*whashP, &mtable[2], 40, WHASHA, WHASHB);
+    addtohash(*whashP, &g3ttable_table[0], 64, WHASHA, WHASHB);
+    addtohash(*whashP, &g3ttable_mtable[2], 40, WHASHA, WHASHB);
 
-    addtohash(*bhashP, &ttable[1], 64, BHASHA, BHASHB);
-    addtohash(*bhashP, &mtable[3], 40, BHASHA, BHASHB);
+    addtohash(*bhashP, &g3ttable_table[1], 64, BHASHA, BHASHB);
+    addtohash(*bhashP, &g3ttable_mtable[3], 40, BHASHA, BHASHB);
 
 }
 
@@ -314,7 +342,7 @@ makeRowWhite(unsigned char * const packedBitrow,
 
 
 
-static g3TableEntry *
+static G3TableEntry *
 g3code(unsigned int const curcode,
        unsigned int const curlen,
        bit          const color) {
@@ -325,7 +353,7 @@ g3code(unsigned int const curcode,
    Note that it is the _position_ in the table that determines the meaning
    of the code.  The contents of the table entry do not.
 -----------------------------------------------------------------------------*/
-    g3TableEntry * retval;
+    G3TableEntry * retval;
 
     switch (color) {
     case PBM_WHITE:
@@ -382,7 +410,7 @@ writeBlackBitSpan(unsigned char * const packedBitrow,
 enum g3tableId {TERMWHITE, TERMBLACK, MKUPWHITE, MKUPBLACK};
 
 static void
-processG3Code(const g3TableEntry * const teP,
+processG3Code(const G3TableEntry * const teP,
               unsigned char *      const packedBitrow,
               unsigned int *       const colP,
               bit *                const colorP,
@@ -393,15 +421,15 @@ processG3Code(const g3TableEntry * const teP,
    matters.
 -----------------------------------------------------------------------------*/
     enum g3tableId const teId =
-        (teP > mtable ? 2 : 0) + (teP - ttable) % 2;
+        (teP > g3ttable_mtable ? 2 : 0) + (teP - g3ttable_table) % 2;
 
     unsigned int teCount;
-    
+
     switch(teId) {
-    case TERMWHITE: teCount = (teP - ttable    ) / 2;      break;
-    case TERMBLACK: teCount = (teP - ttable - 1) / 2;      break;
-    case MKUPWHITE: teCount = (teP - mtable    ) / 2 * 64; break;
-    case MKUPBLACK: teCount = (teP - mtable - 1) / 2 * 64; break;
+    case TERMWHITE: teCount = (teP - g3ttable_table    ) / 2;      break;
+    case TERMBLACK: teCount = (teP - g3ttable_table - 1) / 2;      break;
+    case MKUPWHITE: teCount = (teP - g3ttable_mtable    ) / 2 * 64; break;
+    case MKUPBLACK: teCount = (teP - g3ttable_mtable - 1) / 2 * 64; break;
     }
 
     switch (teId) {
@@ -409,7 +437,7 @@ processG3Code(const g3TableEntry * const teP,
     case TERMBLACK: {
         unsigned int totalRunLength;
         unsigned int col;
-        
+
         col = *colP;
         totalRunLength = MIN(*countP + teCount, MAXCOLS - col);
 
@@ -451,13 +479,13 @@ formatBadCodeException(const char ** const exceptionP,
 
 
 static void
-readFaxRow(struct bitStream * const bitStreamP,
+readFaxRow(struct BitStream * const bitStreamP,
            unsigned char *    const packedBitrow,
            unsigned int *     const lineLengthP,
            const char **      const exceptionP,
            const char **      const errorP) {
 /*----------------------------------------------------------------------------
-  Read one line of G3 fax from the bit stream *bitStreamP into 
+  Read one line of G3 fax from the bit stream *bitStreamP into
   packedBitrow[].  Return the length of the line, in pixels, as *lineLengthP.
 
   If there's a problem with the line, return as much of it as we can,
@@ -468,15 +496,17 @@ readFaxRow(struct bitStream * const bitStreamP,
   We guarantee that we make progress through the input stream.
 
   Iff there is an error, return a text description of it in newly
-  malloc'ed storage at *errorP and all other specified behavior 
+  malloc'ed storage at *errorP and all other specified behavior
   (including return values) is unspecified.
 -----------------------------------------------------------------------------*/
     unsigned int col;
-    unsigned int curlen;  
-        /* Number of bits we've read so far for the code we're currently 
+    unsigned int curlen;
+        /* Number of bits we've read so far for the code we're currently
            reading
         */
-    unsigned int curcode; 
+    unsigned int fillbits;
+        /* Number of consecutive 0 bits.  Can precede EOL codes */
+    unsigned int curcode;
         /* What we've assembled so far of the code we're currently reading */
     unsigned int count;
         /* Number of consecutive pixels of the same color */
@@ -489,6 +519,7 @@ readFaxRow(struct bitStream * const bitStreamP,
     col = 0;
     curlen = 0;
     curcode = 0;
+    fillbits = 0;
     currentColor = PBM_WHITE;
     count = 0;
     *exceptionP = NULL;
@@ -520,21 +551,26 @@ readFaxRow(struct bitStream * const bitStreamP,
             else {
                 curcode = (curcode << 1) | bit;
                 ++curlen;
-            
-                if (curlen > 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) {
-                    const g3TableEntry * const teP =
+                    const G3TableEntry * const teP =
                         g3code(curcode, curlen, currentColor);
-                        /* Address of structure that describes the 
+                        /* Address of structure that describes the
                            current G3 code.  Null means 'curcode' isn't
                            a G3 code yet (probably just the beginning of one)
                         */
                     if (teP) {
                         processG3Code(teP, packedBitrow,
                                       &col, &currentColor, &count);
-                        
+
                         curcode = 0;
                         curlen = 0;
                     }
@@ -562,7 +598,7 @@ freeBits(unsigned char ** const packedBits,
             /* This is just a pointer to the previous row; don't want to
                free it twice.
             */
-        } else 
+        } else
             pbm_freerow_packed(packedBits[row]);
     }
     free(packedBits);
@@ -675,11 +711,11 @@ analyzeLineSize(lineSizeAnalyzer * const analyzerP,
    line.  Starting in Netpbm 10.24 (August 2004), we assume there is
    no valid reason to have an empty line and recognize EOF as any
    empty line.  Alternatively, we could read off and ignore two empty
-   lines without a 3rd.  
+   lines without a 3rd.
 */
 
 static void
-readFax(struct bitStream * const bitStreamP,
+readFax(struct BitStream * const bitStreamP,
         bool               const stretch,
         unsigned int       const expectedLineSize,
         bool               const tolerateErrors,
@@ -692,7 +728,7 @@ readFax(struct bitStream * const bitStreamP,
     const char * error;
     bool eof;
     unsigned int row;
-    
+
     MALLOCARRAY_NOFAIL(packedBits, MAXROWS);
 
     initializeLineSizeAnalyzer(&lineSizeAnalyzer,
@@ -723,7 +759,7 @@ readFax(struct bitStream * const bitStreamP,
                     eof = TRUE;
                 } else {
                     analyzeLineSize(&lineSizeAnalyzer, lineSize);
-                    
+
                     if (stretch) {
                         ++row;
                         if (row >= MAXROWS)
@@ -746,15 +782,15 @@ readFax(struct bitStream * const bitStreamP,
 
 
 int
-main(int argc, char * argv[]) {
+main(int argc, const char * argv[]) {
 
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
     FILE * ifP;
-    struct bitStream bitStream;
+    struct BitStream bitStream;
     unsigned int rows, cols;
     unsigned char ** packedBits;
 
-    pbm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
@@ -773,7 +809,7 @@ main(int argc, char * argv[]) {
     buildHashes(&whash, &bhash);
 
     readFax(&bitStream, cmdline.stretch, cmdline.expectedLineSize,
-            !cmdline.stop_error, 
+            !cmdline.stop_error,
             &packedBits, &cols, &rows);
 
     pm_close(ifP);
diff --git a/converter/pbm/g3ttable.c b/converter/pbm/g3ttable.c
new file mode 100644
index 00000000..6f9eb94f
--- /dev/null
+++ b/converter/pbm/g3ttable.c
@@ -0,0 +1,116 @@
+#include "g3ttable.h"
+
+struct G3TableEntry g3ttable_table[] = {
+/*    TERMWHITE           TERMBLACK   */
+    { 0x35, 8 },    { 0x37, 10 },       /* white 0 , black 0 */
+    { 0x07, 6 },    { 0x02,  3 },
+    { 0x07, 4 },    { 0x03,  2 },
+    { 0x08, 4 },    { 0x02,  2 },
+    { 0x0b, 4 },    { 0x03,  3 },
+    { 0x0c, 4 },    { 0x03,  4 },
+    { 0x0e, 4 },    { 0x02,  4 },
+    { 0x0f, 4 },    { 0x03,  5 },
+    { 0x13, 5 },    { 0x05,  6 },
+    { 0x14, 5 },    { 0x04,  6 },
+    { 0x07, 5 },    { 0x04,  7 },
+    { 0x08, 5 },    { 0x05,  7 },
+    { 0x08, 6 },    { 0x07,  7 },
+    { 0x03, 6 },    { 0x04,  8 },
+    { 0x34, 6 },    { 0x07,  8 },
+    { 0x35, 6 },    { 0x18,  9 },
+    { 0x2a, 6 },    { 0x17, 10 },
+    { 0x2b, 6 },    { 0x18, 10 },
+    { 0x27, 7 },    { 0x08, 10 },
+    { 0x0c, 7 },    { 0x67, 11 },
+    { 0x08, 7 },    { 0x68, 11 },
+    { 0x17, 7 },    { 0x6c, 11 },
+    { 0x03, 7 },    { 0x37, 11 },
+    { 0x04, 7 },    { 0x28, 11 },
+    { 0x28, 7 },    { 0x17, 11 },
+    { 0x2b, 7 },    { 0x18, 11 },
+    { 0x13, 7 },    { 0xca, 12 },
+    { 0x24, 7 },    { 0xcb, 12 },
+    { 0x18, 7 },    { 0xcc, 12 },
+    { 0x02, 8 },    { 0xcd, 12 },
+    { 0x03, 8 },    { 0x68, 12 },
+    { 0x1a, 8 },    { 0x69, 12 },
+    { 0x1b, 8 },    { 0x6a, 12 },
+    { 0x12, 8 },    { 0x6b, 12 },
+    { 0x13, 8 },    { 0xd2, 12 },
+    { 0x14, 8 },    { 0xd3, 12 },
+    { 0x15, 8 },    { 0xd4, 12 },
+    { 0x16, 8 },    { 0xd5, 12 },
+    { 0x17, 8 },    { 0xd6, 12 },
+    { 0x28, 8 },    { 0xd7, 12 },
+    { 0x29, 8 },    { 0x6c, 12 },
+    { 0x2a, 8 },    { 0x6d, 12 },
+    { 0x2b, 8 },    { 0xda, 12 },
+    { 0x2c, 8 },    { 0xdb, 12 },
+    { 0x2d, 8 },    { 0x54, 12 },
+    { 0x04, 8 },    { 0x55, 12 },
+    { 0x05, 8 },    { 0x56, 12 },
+    { 0x0a, 8 },    { 0x57, 12 },
+    { 0x0b, 8 },    { 0x64, 12 },
+    { 0x52, 8 },    { 0x65, 12 },
+    { 0x53, 8 },    { 0x52, 12 },
+    { 0x54, 8 },    { 0x53, 12 },
+    { 0x55, 8 },    { 0x24, 12 },
+    { 0x24, 8 },    { 0x37, 12 },
+    { 0x25, 8 },    { 0x38, 12 },
+    { 0x58, 8 },    { 0x27, 12 },
+    { 0x59, 8 },    { 0x28, 12 },
+    { 0x5a, 8 },    { 0x58, 12 },
+    { 0x5b, 8 },    { 0x59, 12 },
+    { 0x4a, 8 },    { 0x2b, 12 },
+    { 0x4b, 8 },    { 0x2c, 12 },
+    { 0x32, 8 },    { 0x5a, 12 },
+    { 0x33, 8 },    { 0x66, 12 },
+    { 0x34, 8 },    { 0x67, 12 },       /* white 63 , black 63 */
+
+/* mtable */
+/*    MKUPWHITE           MKUPBLACK   */
+    { 0x00, 0 },    { 0x00,  0 },   /* dummy to simplify pointer math */
+    { 0x1b, 5 },    { 0x0f, 10 },   /* white 64 , black 64 */
+    { 0x12, 5 },    { 0xc8, 12 },
+    { 0x17, 6 },    { 0xc9, 12 },
+    { 0x37, 7 },    { 0x5b, 12 },
+    { 0x36, 8 },    { 0x33, 12 },
+    { 0x37, 8 },    { 0x34, 12 },
+    { 0x64, 8 },    { 0x35, 12 },
+    { 0x65, 8 },    { 0x6c, 13 },
+    { 0x68, 8 },    { 0x6d, 13 },
+    { 0x67, 8 },    { 0x4a, 13 },
+    { 0xcc, 9 },    { 0x4b, 13 },
+    { 0xcd, 9 },    { 0x4c, 13 },
+    { 0xd2, 9 },    { 0x4d, 13 },
+    { 0xd3, 9 },    { 0x72, 13 },
+    { 0xd4, 9 },    { 0x73, 13 },
+    { 0xd5, 9 },    { 0x74, 13 },
+    { 0xd6, 9 },    { 0x75, 13 },
+    { 0xd7, 9 },    { 0x76, 13 },
+    { 0xd8, 9 },    { 0x77, 13 },
+    { 0xd9, 9 },    { 0x52, 13 },
+    { 0xda, 9 },    { 0x53, 13 },
+    { 0xdb, 9 },    { 0x54, 13 },
+    { 0x98, 9 },    { 0x55, 13 },
+    { 0x99, 9 },    { 0x5a, 13 },
+    { 0x9a, 9 },    { 0x5b, 13 },
+    { 0x18, 6 },    { 0x64, 13 },
+    { 0x9b, 9 },    { 0x65, 13 },
+    { 0x08, 11 },   { 0x08, 11 },        /* extable len = 1792 */
+    { 0x0c, 11 },   { 0x0c, 11 },
+    { 0x0d, 11 },   { 0x0d, 11 },
+    { 0x12, 12 },   { 0x12, 12 },
+    { 0x13, 12 },   { 0x13, 12 },
+    { 0x14, 12 },   { 0x14, 12 },
+    { 0x15, 12 },   { 0x15, 12 },
+    { 0x16, 12 },   { 0x16, 12 },
+    { 0x17, 12 },   { 0x17, 12 },
+    { 0x1c, 12 },   { 0x1c, 12 },
+    { 0x1d, 12 },   { 0x1d, 12 },
+    { 0x1e, 12 },   { 0x1e, 12 },
+    { 0x1f, 12 },   { 0x1f, 12 },
+};
+
+
+
diff --git a/converter/pbm/g3ttable.h b/converter/pbm/g3ttable.h
new file mode 100644
index 00000000..2ae2bda4
--- /dev/null
+++ b/converter/pbm/g3ttable.h
@@ -0,0 +1,13 @@
+#ifndef G3TTABLE_H_INCLUDED
+#define G3TTABLE_H_INCLUDED
+
+typedef struct G3TableEntry {
+    unsigned short int code;
+    unsigned short int length;
+} G3TableEntry;
+
+extern struct G3TableEntry g3ttable_table[];
+
+#define g3ttable_mtable ((g3ttable_table)+64*2)
+
+#endif
diff --git a/converter/pbm/pbmtoascii.c b/converter/pbm/pbmtoascii.c
index 976b188f..0305a47b 100644
--- a/converter/pbm/pbmtoascii.c
+++ b/converter/pbm/pbmtoascii.c
@@ -83,15 +83,15 @@ BSQ,D04, D04,D05, D04,'L', D05,'[', '<','Z', '/','Z', 'c','k',D06,'R',/*D0-DF*/
 
 
 static void
-makeRowOfSigs(FILE *         const ifP,
-              unsigned int   const cols,
-              unsigned int   const rows,
-              int            const format,
-              unsigned int   const cellWidth,
-              unsigned int   const cellHeight,
-              unsigned int   const row,
-              unsigned int * const sig,
-              unsigned int   const ccols) {
+makeRowOfSignatures(FILE *         const ifP,
+                    unsigned int   const cols,
+                    unsigned int   const rows,
+                    int            const format,
+                    unsigned int   const cellWidth,
+                    unsigned int   const cellHeight,
+                    unsigned int   const row,
+                    unsigned int * const sig,
+                    unsigned int   const ccols) {
 /*----------------------------------------------------------------------------
    Compute the signatures for every cell in a row.
 
@@ -183,23 +183,30 @@ pbmtoascii(FILE *       const ifP,
            unsigned int const cellHeight,
            const char * const carr) {
 
-    int cols, rows, format;
+    int format;
+    int cols, rows;
+        /* Dimensions of the input in pixels */
     unsigned int ccols;
+        /* Width of the output in characters */
     char * line;         /* malloc'ed array */
     unsigned int row;
-    unsigned int * sig;  /* malloc'ed array */
-        /* This describes in a single integer the pixels of a cell,
-           as described above.
+    unsigned int * signatureRow;  /* malloc'ed array */
+        /* This is the cell signatures of a row of cells.
+           signatureRow[0] is the signature for the first (leftmost) cell
+           in the row, signatureRow[1] is the signature for the next one,
+           etc.  A signature is an encoding of the pixels of a cell as
+           an integer, as described above.
         */
-    assert(cellWidth * cellHeight <= sizeof(sig[0])*8);
+    assert(cellWidth * cellHeight <= sizeof(signatureRow[0])*8);
 
     pbm_readpbminit(ifP, &cols, &rows, &format);
 
     ccols = (cols + cellWidth - 1) / cellWidth;
 
-    MALLOCARRAY(sig, ccols);
-    if (sig == NULL)
+    MALLOCARRAY(signatureRow, ccols);
+    if (signatureRow == NULL)
         pm_error("No memory for %u columns", ccols);
+
     MALLOCARRAY_NOFAIL(line, ccols+1);
     if (line == NULL)
         pm_error("No memory for %u columns", ccols);
@@ -207,16 +214,16 @@ pbmtoascii(FILE *       const ifP,
     for (row = 0; row < rows; row += cellHeight) {
         unsigned int endCol;
 
-        makeRowOfSigs(ifP, cols, rows, format, cellWidth, cellHeight,
-                      row, sig, ccols);
+        makeRowOfSignatures(ifP, cols, rows, format, cellWidth, cellHeight,
+                            row, signatureRow, ccols);
 
-        findRightMargin(sig, ccols, carr, &endCol);
+        findRightMargin(signatureRow, ccols, carr, &endCol);
 
-        assembleCellRow(sig, endCol, carr, line);
+        assembleCellRow(signatureRow, endCol, carr, line);
 
         puts(line);
     }
-    free(sig);
+    free(signatureRow);
     free(line);
 }
 
diff --git a/converter/pbm/pbmtoepson.c b/converter/pbm/pbmtoepson.c
index bb36791d..122a438f 100644
--- a/converter/pbm/pbmtoepson.c
+++ b/converter/pbm/pbmtoepson.c
@@ -11,6 +11,7 @@
 ** implied warranty.
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE    /* Make sure strcaseeq() is in nstring.h */
 #include <stdio.h>
 
diff --git a/converter/pbm/pbmtog3.c b/converter/pbm/pbmtog3.c
index f0fd1252..f034b466 100644
--- a/converter/pbm/pbmtog3.c
+++ b/converter/pbm/pbmtog3.c
@@ -1,84 +1,54 @@
 /* pbmtog3.c - read a PBM image and produce a Group 3 FAX file
-**
-** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
-**
-** Permission to use, copy, modify, and distribute this software and its
-** documentation for any purpose and without fee is hereby granted, provided
-** that the above copyright notice appear in all copies and that both that
-** copyright notice and this permission notice appear in supporting
-** documentation.  This software is provided "as is" without express or
-** implied warranty.
-*/
 
-/*
-   For specifications for Group 3 (G3) fax MH coding see ITU-T T.4
-   This program generates only MH.  It is coded with future expansion for
-   MR and MMR in mind.
+   For specifications for Group 3 (G3) fax MH coding see ITU-T T.4:
+   Standardization of Group 3 facsimile terminals for document transmission
+   https://www.itu.int/rec/T-REC-T.4/en
+
+   This program generates only MH.
 */
 
+
 #include <assert.h>
 
 #include "pm_c_util.h"
 #include "shhopt.h"
 #include "mallocvar.h"
 #include "bitreverse.h"
-#include "wordaccess.h"
-#include "wordintclz.h"
+#include "intcode.h"
 #include "g3.h"
+#include "g3ttable.h"
+#include "g3prefab.h"
 #include "pbm.h"
 
-#define TC_MC 64
-
-static bool const pbmtorl = 
-#ifdef PBMTORL
-    TRUE;
-#else
-    FALSE;
-#endif
-
+enum G3eol {EOL, ALIGN8, ALIGN16, NO_EOL, NO_RTC, NO_EOLRTC};
 
-struct bitString {
-    /* A string of bits, up to as many fit in a word. */
-    unsigned int bitCount;
-        /* The length of the bit string */
-    wordint intBuffer;
-        /* The bits are in the 'bitCount' least significant bit positions 
-           of this number.  The rest of the bits of this number are always 
-           zero.
-        */
+struct OutStream;
 
-    /* Example:  The bit string 010100, on a machine with a 32 bit word,
-       would be represented by bitCount = 6, intBuffer = 20
-       (N.B. 20 = 00000000 00000000 00000000 00010100 in binary)
-    */
+struct OutStream {
+    FILE * fp;
+    struct BitString buffer;
+    bool reverseBits;    /* Reverse bit order */
+    enum G3eol eolAlign; /* Omit EOL and/or RTC; align EOL to 8/16 bits */
+    void * data;         /* Reserved for future expansion */
 };
 
 
 
-struct outStream {
-    struct bitString buffer;
-
-    bool reverseBits;
-};
-
-/* This is a global variable for speed. */
-static struct outStream out;
-
-
 struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
     const char * inputFileName;
     unsigned int reversebits;
-    unsigned int nofixedwidth;
+    enum G3eol   align;
+    unsigned int desiredWidth;
     unsigned int verbose;
 };
 
 
 
 static void
-parseCommandLine(int argc, char ** const argv,
+parseCommandLine(int argc, const char ** const argv,
                  struct CmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    Note that the file spec array we return is stored in the storage that
@@ -87,6 +57,8 @@ parseCommandLine(int argc, char ** const argv,
     optEntry * option_def;
         /* Instructions to OptParseOptions2 on how to parse our options.  */
     optStruct3 opt;
+    unsigned int nofixedwidth;
+    unsigned int align8, align16;
 
     unsigned int option_def_index;
 
@@ -95,9 +67,13 @@ parseCommandLine(int argc, char ** const argv,
     option_def_index = 0;   /* incremented by OPTENTRY */
     OPTENT3(0,   "reversebits",      OPT_FLAG,  NULL, &cmdlineP->reversebits,
             0);
-    OPTENT3(0,   "nofixedwidth",     OPT_FLAG,  NULL, &cmdlineP->nofixedwidth,
+    OPTENT3(0,   "nofixedwidth",     OPT_FLAG,  NULL, &nofixedwidth,
             0);
-    OPTENT3(0,   "verbose",          OPT_FLAG,  NULL, &cmdlineP->verbose, 
+    OPTENT3(0,   "align8",           OPT_FLAG,  NULL, &align8,
+            0);
+    OPTENT3(0,   "align16",          OPT_FLAG,  NULL, &align16,
+            0);
+    OPTENT3(0,   "verbose",          OPT_FLAG,  NULL, &cmdlineP->verbose,
             0);
 
     /* TODO
@@ -105,15 +81,30 @@ parseCommandLine(int argc, char ** const argv,
     */
 
     opt.opt_table = option_def;
-    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
-    opt.allowNegNum = TRUE;  /* We may have parms that are negative numbers */
+    opt.short_allowed = false;  /* We have no short (old-fashioned) options */
+    opt.allowNegNum = true;  /* We may have 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. */
 
     free(option_def);
 
-    if (argc-1 == 0) 
+    if (align8) {
+        if (align16)
+            pm_error("You can't specify both -align8 and -align16");
+        else
+            cmdlineP->align = ALIGN8;
+    } else if (align16)
+        cmdlineP->align = ALIGN16;
+    else
+        cmdlineP->align = EOL;
+
+    if (nofixedwidth)
+        cmdlineP->desiredWidth = 0;
+    else
+        cmdlineP->desiredWidth = 1728;
+
+    if (argc-1 == 0)
         cmdlineP->inputFileName = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
@@ -125,31 +116,79 @@ parseCommandLine(int argc, char ** const argv,
 
 
 static void
-reversebuffer(unsigned char * const p, 
+reversebuffer(unsigned char * const p,
               unsigned int    const n) {
 
     unsigned int i;
+
     for (i = 0; i < n; ++i)
         p[i] = bitreverse[p[i]];
 }
 
 
 
-static struct bitString
-makeBs(wordint      const bits, 
-       unsigned int const bitCount) {
+static void
+flushBuffer(struct OutStream * const outP) {
+/*----------------------------------------------------------------------------
+  Flush the contents of the bit buffer
+-----------------------------------------------------------------------------*/
+    struct BitString const buffer = outP->buffer;
 
-    struct bitString retval;
-    retval.intBuffer = bits;
-    retval.bitCount  = bitCount;
+    assert (buffer.bitCount <= 32);
 
-    return retval;
+    if (buffer.bitCount > 0) {
+        unsigned int const fullBuffer = sizeof(buffer.intBuffer) * 8;
+        unsigned int const bytesToWrite = (buffer.bitCount+7)/8;
+        bigend32 outbytes;
+        size_t rc;
+
+        outbytes = pm_bigendFromUint32(
+                   buffer.intBuffer << (fullBuffer - buffer.bitCount));
+        if (outP->reverseBits)
+    	reversebuffer((unsigned char *)&outbytes, bytesToWrite);
+        rc = fwrite((unsigned char *)&outbytes, 1, bytesToWrite, outP->fp);
+        if (rc != bytesToWrite)
+            pm_error("Output error");
+    }
 }
 
 
+#if 1==0
+static void
+putbitsDump(struct OutStream * const outP,
+            struct BitString   const newBits) {
+/*----------------------------------------------------------------------------
+  Print the content of the bit put request, in human readable text form
+  For debugging.  Also good for studying how the coding scheme works.
+
+  By default the compiler ignores this function.
+  To turn on, remove the "#if" - "#endif" lines enclosing the function and
+  edit the output function name in putbits().
+-----------------------------------------------------------------------------*/
+    unsigned int const bitCount = newBits.bitCount;
+
+    unsigned int i;
+    char charBuff[128];
+
+    assert (bitCount >= 0 && bitCount < 32);
+    assert (sizeof(newBits.intBuffer) + 2 < 128);
+
+    for (i = 0; i < bitCount; ++i) {
+        unsigned int const n = bitCount - i - 1;
+        charBuff[i] = ((newBits.intBuffer >> n) & 0x01) + '0';
+    }
+
+    charBuff[bitCount]   = '\n';
+    charBuff[bitCount+1] = '\0';
+    fwrite(charBuff, 1, bitCount+1, outP->fp);
+}
+#endif
+
+
 
-static __inline__ void
-putbits(struct bitString const newBits) {
+static void
+putbitsBinary(struct OutStream * const outP,
+              struct BitString   const newBits) {
 /*----------------------------------------------------------------------------
    Push the bits 'newBits' onto the right end of output buffer
    out.buffer (moving the bits already in the buffer left).
@@ -158,334 +197,462 @@ putbits(struct bitString const newBits) {
 
    'newBits' must be shorter than a whole word.
 
-   N.B. the definition of struct bitString requires upper bits to be zero.
+   N.B. the definition of struct BitString requires upper bits to be zero.
 -----------------------------------------------------------------------------*/
-    unsigned int const spaceLeft = 
-        sizeof(out.buffer.intBuffer)*8 - out.buffer.bitCount;
+    unsigned int const fullBuffer = sizeof(outP->buffer.intBuffer) * 8;
+    unsigned int const spaceLeft = fullBuffer - outP->buffer.bitCount;
         /* Number of bits of unused space (at the high end) in buffer */
 
-    assert(newBits.bitCount < sizeof(out.buffer.intBuffer) * 8);
+    assert(newBits.bitCount < fullBuffer);
     assert(newBits.intBuffer >> newBits.bitCount == 0);
 
     if (spaceLeft > newBits.bitCount) {
         /* New bits fit with bits to spare */
-        out.buffer.intBuffer = 
-            out.buffer.intBuffer << newBits.bitCount | newBits.intBuffer;
-        out.buffer.bitCount += newBits.bitCount;
-    } else { 
+        outP->buffer.intBuffer =
+            outP->buffer.intBuffer << newBits.bitCount | newBits.intBuffer;
+        outP->buffer.bitCount += newBits.bitCount;
+    } else {
         /* New bits fill buffer.  We'll have to flush the buffer to stdout
            and put the rest of the bits in the new buffer.
         */
         unsigned int const nextBufBitCount = newBits.bitCount - spaceLeft;
+        unsigned int const bitMask = ((1<<nextBufBitCount) - 1);
 
-        wordintBytes outbytes;
-        size_t rc;
+        outP->buffer.intBuffer = ( (outP->buffer.intBuffer << spaceLeft)
+                                 | (newBits.intBuffer >> nextBufBitCount));
+        outP->buffer.bitCount  = fullBuffer;
+        flushBuffer(outP);
 
-        wordintToBytes(&outbytes, 
-                       (out.buffer.intBuffer << spaceLeft) 
-                       | (newBits.intBuffer >> nextBufBitCount));
-        if (out.reverseBits)
-            reversebuffer(outbytes, sizeof(outbytes));
+        outP->buffer.intBuffer = newBits.intBuffer & bitMask;
+        outP->buffer.bitCount = nextBufBitCount;
+    }
+}
 
-        rc = fwrite(outbytes, 1, sizeof(outbytes), stdout);
-        if (rc != sizeof(outbytes))
-            pm_error("Output error.  Unable to fwrite() to stdout");
 
-        out.buffer.intBuffer = newBits.intBuffer & ((1<<nextBufBitCount) - 1); 
-        out.buffer.bitCount = nextBufBitCount;
-    }
+
+static void
+initOutStream(struct OutStream * const outP,
+              bool               const reverseBits,
+              enum G3eol         const eolAlign) {
+
+    outP->buffer.intBuffer = 0;
+    outP->buffer.bitCount  = 0;
+    outP->reverseBits      = reverseBits;
+    outP->fp               = stdout;
+    outP->eolAlign         = eolAlign;
+}
+
+
+
+static struct BitString
+tableEntryToBitString(G3TableEntry const tableEntry) {
+
+    struct BitString retval;
+
+    retval.intBuffer = tableEntry.code;
+    retval.bitCount  = tableEntry.length;
+
+    return retval;
 }
 
 
 
-static void 
-initOutStream(bool const reverseBits) {
-    out.buffer.intBuffer = 0;
-    out.buffer.bitCount  = 0;
-    out.reverseBits = reverseBits;
+static void
+putbits(struct OutStream * const outP,
+        struct BitString   const newBits) {
+
+    putbitsBinary(outP, newBits);
+    /* Change to putbitsDump() for human readable output */
 }
 
 
 
-static __inline__ void
-putcode(unsigned int const clr, 
-        unsigned int const ix) {
+static void
+putcodeShort(struct OutStream * const outP,
+             bit                const color,
+             unsigned int       const runLength) {
 
-    /* Note that this requires ttable to be aligned white entry, black
-       entry, white, black, etc.  
+    /* Note that this requires g3ttable_table to be aligned white entry, black
+       entry, white, black, etc.
     */
-    putbits(makeBs(ttable[ix * 2 + clr].code, ttable[ix * 2 + clr].length));
+    unsigned int index = runLength * 2 + color;
+    putbits(outP, tableEntryToBitString(g3ttable_table[index]));
 }
 
 
 
-static __inline__ void
-putcode2(int const clr,
-         int const ix) {
+static void
+putcodeLong(struct OutStream * const outP,
+            bit                const color,
+            unsigned int       const runLength) {
 /*----------------------------------------------------------------------------
    Output Make-up code and Terminating code at once.
 
-   For run lengths above TC_MC threshold (usually 64).
+   For run lengths which require both: length 64 and above
 
    The codes are combined here to avoid calculations in putbits()
-   wordint is usually wide enough, with 32 or 64 bits.
-   Provisions are made for 16 bit wordint (for debugging).
 
    Terminating code is max 12 bits, Make-up code is max 13 bits.
-   (See ttable, mtable entries in pbmtog3.h)
+   (See g3ttable_table, g3ttable_mtable entries in g3ttable.h)
 
    Also reduces object code size when putcode is compiled inline.
 -----------------------------------------------------------------------------*/
-    unsigned int const loIndex = ix % 64 * 2 + clr;
-    unsigned int const hiIndex = ix / 64 * 2 + clr;
-
-    if (sizeof(wordint) * 8 > 24) {
-        unsigned int const l1 = ttable[loIndex].length;
-
-        putbits(
-            makeBs(mtable[hiIndex].code << l1 | ttable[loIndex].code,
-                   mtable[hiIndex].length + l1)
-            );
-    } else { /* typically 16 bit wordint used for debugging */
-        putbits(makeBs(mtable[hiIndex].code, mtable[hiIndex].length));
-        putbits(makeBs(ttable[loIndex].code, ttable[loIndex].length));
-    }
+    unsigned int const loIndex = runLength % 64 * 2 + color;
+    unsigned int const hiIndex = runLength / 64 * 2 + color;
+    unsigned int const loLength = g3ttable_table[loIndex].length;
+    unsigned int const hiLength = g3ttable_mtable[hiIndex].length;
+
+    struct BitString combinedCode;
+
+    combinedCode.intBuffer = g3ttable_mtable[hiIndex].code << loLength |
+                             g3ttable_table[loIndex].code;
+    combinedCode.bitCount  = hiLength + loLength;
+
+    putbits(outP, combinedCode);
 }
 
 
 
-static __inline__ void
-putspan_normal(bit          const color, 
-               unsigned int const len) {
+static  void
+putcodeExtra(struct OutStream * const outP,
+             int                const color,
+             int                const runLength) {
+/*----------------------------------------------------------------------------
+   Lengths over 2560.  This is rare.
+   According to the standard, the mark-up code for 2560 can be issued as
+   many times as necessary without terminal codes.
+   --------------------------------------------------------------------------*/
+    G3TableEntry const markUp2560 = g3ttable_mtable[2560/64*2];
+                              /* Same code for black and white */
 
-    if (len < TC_MC)
-        putcode(color, len);
-    else if (len < 2624)
-        putcode2(color, len);
-    else {  /* len >= 2624 : rare */
-        unsigned int remainingLen;
+    unsigned int remainingLen;
 
-        for (remainingLen = len;
-             remainingLen >= 2624;
-             remainingLen -= 2623) {
+    for (remainingLen = runLength; remainingLen > 2560; remainingLen -= 2560)
+      putbits(outP, tableEntryToBitString(markUp2560));
+    /* after the above: 0 < remainingLen <= 2560 */
 
-            putcode2(color, 2560+63);
-            putcode(!color, 0);
-        }
-        if (remainingLen < TC_MC)
-            putcode(color, remainingLen);
-        else  /* TC_MC <= len < 2624 */
-            putcode2(color, remainingLen);
-    }
+    if (remainingLen >= 64)
+        putcodeLong(outP, color, remainingLen);
+    else
+        putcodeShort(outP, color, remainingLen);
 }
 
 
 
-static __inline__ void
-putspan(bit          const color, 
-        unsigned int const len) {
-/*----------------------------------------------------------------------------
-   Put a span of 'len' pixels of color 'color' in the output.
------------------------------------------------------------------------------*/
-    if (pbmtorl) {
-        if (len > 0) 
-            printf("%c %d\n", color == PBM_WHITE ? 'W' : 'B', len);
-    } else 
-        putspan_normal(color, len);
+static void
+putspan(struct OutStream * const outP,
+        bit                const color,
+        unsigned int       const runLength) {
+
+    if (runLength < 64)
+        putcodeShort(outP, color, runLength);
+    else if (runLength < 2560)
+        putcodeLong (outP, color, runLength);
+    else  /* runLength > 2560 : rare */
+        putcodeExtra(outP, color, runLength);
 }
 
 
 
 static void
-puteol(void) {
+puteol(struct OutStream * const outP) {
+
+    switch (outP->eolAlign) {
+    case EOL: {
+        struct BitString const eol = { 1, 12 };
+
+        putbits(outP, eol);
+    } break;
+    case ALIGN8:  case ALIGN16: {
+        unsigned int const bitCount = outP->buffer.bitCount;
+        unsigned int const fillbits =
+            (outP->eolAlign == ALIGN8) ? (44 - bitCount) % 8
+            : (52 - bitCount) % 16;
+
+        struct BitString eol;
+
+        eol.bitCount = 12 + fillbits;     eol.intBuffer = 1;
+        putbits(outP, eol);
+    } break;
+    case NO_EOL: case NO_EOLRTC:
+        break;
+    case NO_RTC:
+        pm_error("INTERNAL ERROR: no-RTC EOL treatment not implemented");
+        break;
+    }
+}
 
-    if (pbmtorl)
-        puts("EOL");
-    else {
-        struct bitString const eol = {12, 1};
 
-        putbits(eol);
+
+static void
+putrtc(struct OutStream * const outP) {
+
+    switch (outP->eolAlign) {
+    case NO_RTC: case NO_EOLRTC:
+        break;
+    default:
+        puteol(outP);    puteol(outP);    puteol(outP);
+        puteol(outP);    puteol(outP);    puteol(outP);
     }
 }
 
 
 
-/*
-  PBM raw bitrow to inflection point array
+static void
+readOffSideMargins(unsigned char * const bitrow,
+                   unsigned int    const colChars,
+                   unsigned int  * const firstNonWhiteCharP,
+                   unsigned int  * const lastNonWhiteCharP,
+                   bool          * const blankRowP) {
+/*----------------------------------------------------------------------------
+  Determine the white margins on the left and right side of a row.
+  This is an enhancement: convertRowToG3() works without this.
+-----------------------------------------------------------------------------*/
+    unsigned int charCnt;
+    unsigned int firstChar;
+    unsigned int lastChar;
+    bool         blankRow;
 
-  Write inflection (=color change) points into array milepost[].  
-  It is easy to calculate run length from this.
+    assert(colChars > 0);
 
-  In milepost, a white-to-black (black-to-white) inflection point
-  always has an even (odd) index.  A line starting with black is
-  indicated by bitrow[0] == 0.
+    for (charCnt = 0; charCnt < colChars && bitrow[charCnt] == 0; ++charCnt);
 
-  WWWWWWWBBWWWWWWW ... = 7,2,7, ...
-  BBBBBWBBBBBWBBBB ... = 0,5,1,5,1,4, ...
+    if (charCnt >= colChars) {
+        /* Reached end of bitrow with no black pixels encountered */
+        firstChar = lastChar = 0;
+        blankRow  = true;
+    } else {
+        /* There is at least one black pixel in the row */
+        firstChar = charCnt;
+        blankRow = false;
 
-  Return the number of milepost elements written.
-  Note that max number of entries into milepost = cols+1 .
+        charCnt = colChars - 1;
 
-  The inflection points are calculated like this:
+        while (bitrow[charCnt--] == 0x00)
+            ;
+        lastChar = charCnt + 1;
+    }
 
-   r1: 00000000000111111110011111000000
-   r2: c0000000000011111111001111100000 0->carry
-  xor: ?0000000000100000001010000100000
+    *firstNonWhiteCharP = firstChar;
+    *lastNonWhiteCharP  = lastChar;
+    *blankRowP          = blankRow;
+}
 
-  The 1 bits in the xor above are the inflection points.
-*/
 
-static __inline__ void
-convertRowToRunLengths(unsigned char * const bitrow, 
-                       int             const cols, 
-                       unsigned int *  const milepost,
-                       unsigned int *  const lengthP) {
-
-    unsigned int   const bitsPerWord  = sizeof(wordint) * 8;
-    wordint      * const bitrowByWord = (wordint *) bitrow;
-    int            const wordCount    = (cols + bitsPerWord - 1)/bitsPerWord; 
-        /* Number of full and partial words in the row */
-
-
-    if (cols % bitsPerWord != 0) {
-        /* Clean final word in row.  For loop simplicity */
-        wordint r1;
-        r1 = bytesToWordint((unsigned char *)&bitrowByWord[wordCount - 1]);
-        r1 >>= bitsPerWord - cols % bitsPerWord;
-        r1 <<= bitsPerWord - cols % bitsPerWord;
-        wordintToBytes((wordintBytes *)&bitrowByWord[wordCount - 1], r1);
+
+static void
+setBlockBitsInFinalChar(unsigned char * const finalByteP,
+                        unsigned int    const cols) {
+/*----------------------------------------------------------------------------
+   If the char in the row is fractional, set it up so that the don't care
+   bits are the opposite color of the last valid pixel.
+----------------------------------------------------------------------------*/
+    unsigned char const finalByte  = *finalByteP;
+    unsigned int const silentBitCnt = 8 - cols % 8;
+    bit const rowEndColor = (finalByte >> silentBitCnt) & 0x01;
+
+    if (rowEndColor == PBM_WHITE) {
+        unsigned char const blackMask = (0x01 << silentBitCnt) - 1;
+
+        *finalByteP = finalByte | blackMask;
     }
-    {
-
-        wordint carry;
-        wordint r1, r2;
-        unsigned int n;
-        int i,c,k;
-
-        for (i = carry = n = 0; i < wordCount; ++i) {
-            r1 = r2 = bytesToWordint((unsigned char *)&bitrowByWord[i]);
-            r2 = r1 ^ (carry << (bitsPerWord-1) | r2 >> 1);
-            carry = r1 & 0x1;  k = 0;
-            while (r2 != 0) {
-                /* wordintClz(r2) reports most significant "1" bit of r2
-                   counting from MSB = position 0.
-                */
-                c = wordintClz(r2);
-                milepost[n++] = i * bitsPerWord + k + c;
-                r2 <<= c++; r2 <<= 1;  k += c; 
-            } 
+    /* No adjustment required if the row ends with a black pixel.
+       pbm_cleanrowend_packed() takes care of this.
+    */
+}
+
+
+
+static void
+trimFinalChar(struct OutStream * const outP,
+              bit                const color,
+              int                const carryLength,
+              int                const existingCols,
+              int                const desiredWidth) {
+/*---------------------------------------------------------------------------
+   If the carry value from the last char in the row represents a valid
+   sequence, output it.
+
+   (1) If input row width is not a whole multiple of 8 and -nofixwidth
+       was specified, the final carry value represents inactive bits
+       at the row end.  Emit no code.  See setBlockBitsInFinalChar().
+
+   (2) If there is white margin on the right side, the final carry value
+       is valid.  We add to it the margin width.  Right-side margin may
+       be added in main() to a narrow input image, detected in the
+       input row by readOffSideMargins() or both.  The same treatment
+       applies regardless of the nature of the right-side margin.
+----------------------------------------------------------------------------*/
+    if (existingCols == desiredWidth) {
+        if (existingCols % 8 == 0)
+            putspan(outP, color, carryLength);  /* Code up to byte boundary */
+        /* Emit nothing if existingCols is not a whole multiple of 8 */
+    } else if (existingCols < desiredWidth) {
+        if (color == 0) {       /* Last bit sequence in final char: white */
+            unsigned int const totalLength =
+                carryLength + (desiredWidth - existingCols);
+            putspan(outP, 0, totalLength);
+        } else {                 /* Black */
+            unsigned int const padLength = desiredWidth - existingCols;
+            putspan(outP, 1, carryLength);
+            putspan(outP, 0, padLength);
         }
-        if (n == 0 || milepost[n - 1] != cols) 
-            milepost[n++] = cols;
-        *lengthP = n;
     }
 }
 
 
 
 static void
-padToDesiredWidth(unsigned int * const milepost,
-                  unsigned int * const nRunP,
-                  int            const existingCols,
-                  int            const desiredCols) {
-
-    if (existingCols < desiredCols) {
-        /* adjustment for narrow input in fixed width mode
-           nRun % 2 == 1 (0) means last (=rightmost) pixel is white (black)
-           if white, extend the last span to outwidth
-           if black, fill with a white span len (outwidth - readcols)
-        */
-        if (*nRunP % 2 == 0)
-            ++*nRunP;
-        milepost[*nRunP - 1] = desiredCols;
+convertRowToG3(struct OutStream * const outP,
+               unsigned char    * const bitrow,
+               unsigned int       const existingCols,
+               unsigned int       const desiredWidth) {
+/*----------------------------------------------------------------------------
+   Table based Huffman coding
+
+   Normally Huffman code encoders count sequences of ones and zeros
+   and convert them to binary codes as they terminate.  This program
+   recognizes chains of pixels and converts them directly, reading
+   prefabricated code chains from an indexed table.
+
+   For example the 8-bit sequence 01100110 translates to
+   Huffman code: 000111 11 0111 11 000111.
+
+   In reality things are more complicated.  The leftmost 0 (MSB) may be
+   part of a longer sequence starting in the adjacent byte or perhaps
+   spanning several bytes.  Likewise for the rightmost 0.
+
+   So we first remove the sequence on the left side and compare its
+   color with the leftmost pixel of the adjacent byte and emit either
+   one code for a single sequence if they agree or two if they disagree.
+   Next the composite code for the central part (in the above example
+   110011 -> 11 0111 11) is emitted.  Finally we save the length and
+   color of the sequence on the right end as carry-over for the next
+   byte cycle.  Some 8-bit input sequences (00000000, 01111111,
+   00111111, etc.) have no central part: these are special cases.
+---------------------------------------------------------------------------*/
+    unsigned int const colChars = pbm_packed_bytes(existingCols);
+
+    unsigned int charCnt;
+    unsigned int firstActiveChar;
+    unsigned int lastActiveChar;
+    bool         blankRow;
+    bit          borderColor;
+
+    borderColor = PBM_WHITE; /* initial value */
+
+    if (existingCols == desiredWidth && (desiredWidth % 8) > 0)
+        setBlockBitsInFinalChar(&bitrow[colChars-1], desiredWidth);
+
+    readOffSideMargins(bitrow, colChars,
+                       &firstActiveChar, &lastActiveChar, &blankRow);
+
+    if (blankRow)
+        putspan(outP, PBM_WHITE, desiredWidth);
+    else {
+        unsigned int carryLength;
+
+        for (charCnt = firstActiveChar, carryLength = firstActiveChar * 8;
+             charCnt <=lastActiveChar;
+             ++charCnt) {
+
+            unsigned char const byte = bitrow[charCnt];
+            bit const rColor = !borderColor;
+
+            if (byte == borderColor * 0xFF) {
+                carryLength += 8;
+            } else if (byte == (unsigned char) ~(borderColor * 0xFF)) {
+                putspan(outP, borderColor, carryLength);
+                carryLength = 8;
+                borderColor = rColor;
+            } else {
+                struct PrefabCode const code = g3prefab_code[byte];
+                unsigned int const activeLength =
+                    8 - code.leadBits - code.trailBits;
+
+                if (borderColor == (byte >> 7)) {
+                    putspan(outP, borderColor, carryLength + code.leadBits);
+                } else {
+                    putspan(outP, borderColor, carryLength);
+                    putcodeShort(outP, rColor, code.leadBits);
+                }
+                if (activeLength > 0)
+                    putbits(outP, code.activeBits);
+
+                borderColor = byte & 0x01;
+                carryLength = code.trailBits;
+            }
+        }
+        trimFinalChar(outP, borderColor, carryLength,
+                      (lastActiveChar + 1) * 8, desiredWidth);
     }
+    puteol(outP);
 }
 
 
 
 int
-main(int    argc,
-     char * argv[]) {
+main(int          argc,
+     const char * argv[]) {
 
     struct CmdlineInfo cmdline;
     FILE * ifP;
     unsigned char * bitrow;
        /* This is the bits of the current row, as read from the input and
-           modified various ways at various points in the program.  It has
-           a word of zero padding on the high (right) end for the convenience
-           of code that accesses this buffer in word-size bites.
+          modified various ways at various points in the program.  It has
+          a word of zero padding on the high (right) end for the convenience
+          of code that accesses this buffer in word-size bites.
         */
 
     int rows;
     int cols;
-    int readcols;
-    int outwidth;
     int format;
-    int row;
-    unsigned int * milepost;
+    unsigned int existingCols;
+    unsigned int desiredWidth;
+    unsigned int row;
+    struct OutStream out;
 
-    pbm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
     ifP = pm_openr(cmdline.inputFileName);
 
     pbm_readpbminit(ifP, &cols, &rows, &format);
-    if (cmdline.nofixedwidth)
-        readcols = outwidth = cols;
+
+    if (cmdline.desiredWidth == 0)
+        desiredWidth = existingCols = cols;
     else {
-        readcols = MIN(cols, 1728);
-        outwidth = 1728;
+        if (cmdline.desiredWidth < cols)
+            existingCols = desiredWidth = cmdline.desiredWidth;
+        else {
+            existingCols = pbm_packed_bytes(cols) * 8;
+            desiredWidth = cmdline.desiredWidth;
+        }
     }
 
-    MALLOCARRAY_NOFAIL(bitrow, pbm_packed_bytes(cols) + sizeof(wordint));
+    MALLOCARRAY(bitrow, pbm_packed_bytes(cols) + sizeof(uint32_t));
 
-    MALLOCARRAY_NOFAIL(milepost, readcols + 2);
+    if (!bitrow)
+        pm_error("Failed to allocate a row buffer for %u columns", cols);
 
-    initOutStream(cmdline.reversebits);
-    puteol();
+    initOutStream(&out, cmdline.reversebits, cmdline.align);
 
-    for (row = 0; row < rows; ++row) {
-        unsigned int nRun;  /* Number of runs in milepost[] */
-        unsigned int p;
-        unsigned int i;
+    puteol(&out);
 
+    for (row = 0; row < rows; ++row) {
         pbm_readpbmrow_packed(ifP, bitrow, cols, format);
-
-        convertRowToRunLengths(bitrow, readcols, milepost, &nRun);
-
-        padToDesiredWidth(milepost, &nRun, readcols, outwidth);
-
-        for (i = p = 0; i < nRun; p = milepost[i++])
-            putspan(i%2 == 0 ? PBM_WHITE : PBM_BLACK, milepost[i] - p);
-        /* TODO 2-dimensional coding MR, MMR */
-        puteol();
+        pbm_cleanrowend_packed(bitrow, cols);
+        convertRowToG3(&out, bitrow, existingCols, desiredWidth);
     }
 
-    free(milepost);
     pbm_freerow_packed(bitrow);
-
-    {
-        unsigned int i;  
-        for( i = 0; i < 6; ++i)
-            puteol();
-    }
-    if (out.buffer.bitCount > 0) {
-        /* flush final partial buffer */
-        unsigned int const bytesToWrite = (out.buffer.bitCount+7)/8;
-
-        unsigned char outbytes[sizeof(wordint)];
-        size_t rc;
-        wordintToBytes(&outbytes, 
-                       out.buffer.intBuffer << (sizeof(out.buffer.intBuffer)*8 
-                                                - out.buffer.bitCount));
-        if (out.reverseBits)
-            reversebuffer(outbytes, bytesToWrite);
-        rc = fwrite(outbytes, 1, bytesToWrite, stdout);
-        if (rc != bytesToWrite)
-            pm_error("Output error");
-    }
+    putrtc(&out);
+    flushBuffer(&out);
     pm_close(ifP);
 
     return 0;
 }
+
+
+
diff --git a/converter/pbm/pbmtolj.c b/converter/pbm/pbmtolj.c
index 0cceb4fe..3cd76703 100644
--- a/converter/pbm/pbmtolj.c
+++ b/converter/pbm/pbmtolj.c
@@ -1,14 +1,14 @@
 /* pbmtolj.c - read a portable bitmap and produce a LaserJet bitmap file
-**  
+**
 **  based on pbmtops.c
 **
 **  Michael Haberler HP Vienna mah@hpuviea.uucp
 **                 mcvax!tuvie!mah
-**  misfeatures: 
+**  misfeatures:
 **      no positioning
 **
 **      Bug fix Dec 12, 1988 :
-**              lines in putbit() reshuffled 
+**              lines in putbit() reshuffled
 **              now runs OK on HP-UX 6.0 with X10R4 and HP Laserjet II
 **      Bo Thide', Swedish Institute of Space Physics, Uppsala <bt@irfu.se>
 **
@@ -75,7 +75,7 @@ parseCommandLine(int argc, char ** argv,
     MALLOCARRAY(option_def, 100);
 
     option_def_index = 0;   /* incremented by OPTENTRY */
-    OPTENT3(0,   "resolution",  OPT_UINT, &cmdlineP->dpi, 
+    OPTENT3(0,   "resolution",  OPT_UINT, &cmdlineP->dpi,
             &dpiSpec, 0);
     OPTENT3(0,   "copies",      OPT_UINT, &cmdlineP->copies,
             &copiesSpec, 0);
@@ -97,7 +97,7 @@ parseCommandLine(int argc, char ** argv,
     pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
-    if (argc-1 == 0) 
+    if (argc-1 == 0)
         cmdlineP->inputFilename = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
@@ -246,7 +246,7 @@ packbits(void) {
           We drop out here after having found a [possibly empty]
           literal, followed by a [possibly degenerate] run of repeated
           bytes.  Degenerate runs can occur at the end of the scan line...
-          there may be a "repeat" of 1 byte (which can't actually be 
+          there may be a "repeat" of 1 byte (which can't actually be
           represented as a repeat) so we simply fold it into the previous
           literal.
         */
@@ -320,11 +320,11 @@ deltarow(void) {
             skip = 1;
         }
         if (mustBurst) {
-            burstCode = burstEnd - burstStart; 
+            burstCode = burstEnd - burstStart;
                 /* 0-7, means 1-8 bytes follow */
             code = (burstCode << 5) | skipped;
             deltaBuffer[deltaBufferIndex++] = (char) code;
-            memcpy(deltaBuffer+deltaBufferIndex, rowBuffer+burstStart, 
+            memcpy(deltaBuffer+deltaBufferIndex, rowBuffer+burstStart,
                    burstCode + 1);
             deltaBufferIndex += burstCode + 1;
             burstStart = -1;
@@ -369,7 +369,7 @@ convertRow(const bit *    const bitrow,
            bool *         const rowIsBlankP) {
 
     unsigned int rightmostBlackCol;
-        
+
     findRightmostBlackCol(bitrow, cols, rowIsBlankP, &rightmostBlackCol);
 
     if (!*rowIsBlankP) {
@@ -377,7 +377,7 @@ convertRow(const bit *    const bitrow,
             /* Number of columns excluding white right margin */
         unsigned int const rucols = ((nzcol + 7) / 8) * 8;
             /* 'nzcol' rounded up to nearest multiple of 8 */
-        
+
         unsigned int col;
 
         memset(rowBuffer, 0, rowBufferSize);
@@ -404,7 +404,7 @@ convertRow(const bit *    const bitrow,
 
         if (delta) {
             /* May need to temporarily bump the row buffer index up to
-               whatever the previous line's was - if this line is shorter 
+               whatever the previous line's was - if this line is shorter
                than the previous would otherwise leave dangling cruft.
             */
             unsigned int const savedRowBufferIndex = rowBufferIndex;
@@ -421,7 +421,7 @@ convertRow(const bit *    const bitrow,
 }
 
 
-    
+
 static void
 printBlankRows(unsigned int const count) {
 
@@ -430,12 +430,12 @@ printBlankRows(unsigned int const count) {
         /* The code used to be this, but Charles Howes reports that
            this escape sequence does not exist on his HP Laserjet IIP
            plus, so we use the following less elegant code instead.
-           
-           printf("\033*b%dY", (*blankRowsP)); 
+
+           printf("\033*b%dY", (*blankRowsP));
         */
-        for (x = 0; x < count; ++x) 
+        for (x = 0; x < count; ++x)
             printf("\033*b0W");
-        
+
         memset(prevRowBuffer, 0, rowBufferSize);
     }
 }
@@ -494,7 +494,7 @@ printRow(void) {
 
 
 static void
-doPage(FILE *             const ifP, 
+doPage(FILE *             const ifP,
        struct cmdlineInfo const cmdline) {
 
     bit * bitrow;
@@ -525,10 +525,10 @@ doPage(FILE *             const ifP,
         else {
             printBlankRows(blankRows);
             blankRows = 0;
-            
+
             printRow();
         }
-    }    
+    }
     printBlankRows(blankRows);
     blankRows = 0;
 
@@ -563,3 +563,6 @@ main(int argc, char * argv[]) {
 
     return 0;
 }
+
+
+
diff --git a/converter/pbm/pbmtoln03.c b/converter/pbm/pbmtoln03.c
index f7cf53c7..235429cd 100644
--- a/converter/pbm/pbmtoln03.c
+++ b/converter/pbm/pbmtoln03.c
@@ -40,116 +40,130 @@ work.
  */
 
 #include <stdio.h>
-#include "pbm.h"
-
-FILE *input ;
-
-#ifndef print
-#define print(s)        fputs (s, stdout)
-#define fprint(f, s)    fputs (s, f)
-#endif
 
+#include "mallocvar.h"
+#include "pbm.h"
 
-static void 
-output_sixel_record (unsigned char * record, int width) {
 
-   int i, j, k ;
-   unsigned char last_char ;
-   int start_repeat = 0 ;
-   int repeated ;
-   char repeated_str[16] ;
-   char *p ;
 
-   /* Do RLE */
-   last_char = record[0] ;
-   j = 0 ;
+static void
+outputSixelRecord(unsigned char * const record,
+                  unsigned int    const width) {
 
-   /* This will make the following loop complete */
-   record[width] = '\0' ;
+    unsigned int i, j;
+    unsigned char lastChar;
+    int startRepeat;
+    char repeatedStr[16];
 
-   for (i = 1 ; i <= width ; i++) {
+    /* Do RLE */
+    lastChar = record[0];
+    j = 0;
 
-      repeated = i - start_repeat ;
+    /* This will make the following loop complete */
+    record[width] = '\0' ;
 
-      if (record[i] != last_char || repeated >= 32766) {
+    for (i = 1, startRepeat = 0; i <= width; ++i) {
+        unsigned int const repeated = i - startRepeat;
 
-         /* Repeat has ended */
+        if (record[i] != lastChar || repeated >= 32766) {
 
-         if (repeated > 3) {
+            /* Repeat has ended */
 
-            /* Do an encoding */
-            record[j++] = '!' ;
-            sprintf (repeated_str, "%d", i - start_repeat) ;
-            for (p = repeated_str ; *p ; p++)
-               record[j++] = *p ;
-               record[j++] = last_char ; }
+            if (repeated > 3) {
+                /* Do an encoding */
+                char * p;
+                record[j++] = '!' ;
+                sprintf(repeatedStr, "%u", i - startRepeat);
+                for (p = repeatedStr; *p; ++p)
+                    record[j++] = *p;
+                record[j++] = lastChar;
+            } else {
+                unsigned int k;
 
-         else {
-            for (k = 0 ; k < repeated ; k++)
-               record[j++] = last_char ; }
+                for (k = 0; k < repeated; ++k)
+                    record[j++] = lastChar;
+            }
 
-         start_repeat = i ;
-         last_char = record[i] ; }
-      }
+            startRepeat = i ;
+            lastChar = record[i];
+        }
+    }
 
-   fwrite ((char *) record, j, 1, stdout) ;
-   putchar ('-') ;      /* DECGNL (graphics new-line) */
-   putchar ('\n') ;
-   }
+    fwrite((char *) record, j, 1, stdout) ;
+    putchar('-') ;      /* DECGNL (graphics new-line) */
+    putchar('\n') ;
+}
 
 
-static void 
-convert (int width, int height, int format) {
-
-   int i ;
-   unsigned char *sixel ;               /* A row of sixels */
-   int sixel_row ;
-
-   bit *row = pbm_allocrow (width) ;
-
-   sixel = (unsigned char *) malloc (width + 2) ;
-
-   sixel_row = 0 ;
-   while (height--) {
-      pbm_readpbmrow (input, row, width, format) ;
-      switch (sixel_row) {
-         case 0 :
-           for (i = 0 ; i < width ; i++)
-              sixel[i] = row[i] ;
-           break ;
-         case 1 :
-           for (i = 0 ; i < width ; i++)
-              sixel[i] += row[i] << 1 ;
-           break ;
-         case 2 :
-           for (i = 0 ; i < width ; i++)
-              sixel[i] += row[i] << 2 ;
-           break ;
-         case 3 :
-           for (i = 0 ; i < width ; i++)
-              sixel[i] += row[i] << 3 ;
-           break ;
-         case 4 :
-           for (i = 0 ; i < width ; i++)
-              sixel[i] += row[i] << 4 ;
-           break ;
-         case 5 :
-           for (i = 0 ; i < width ; i++)
-              sixel[i] += (row[i] << 5) + 077 ;
-           output_sixel_record (sixel, width) ;
-           break ; }
-      if (sixel_row == 5)
-         sixel_row = 0 ;
-      else
-         sixel_row++ ;
-      }
 
-   if (sixel_row > 0) {
-      /* Incomplete sixel record needs to be output */
-      for (i = 0 ; i < width ; i++)
-         sixel[i] += 077 ;
-      output_sixel_record (sixel, width) ; }
-   }
+static void
+convert(FILE *       const ifP,
+        unsigned int const width,
+        unsigned int const height,
+        unsigned int const format) {
+
+    unsigned char * sixel;  /* A row of sixels */
+    unsigned int sixelRow;
+    bit * bitrow;
+    unsigned int remainingHeight;
+
+    bitrow = pbm_allocrow(width);
+
+    MALLOCARRAY(sixel, width + 2);
+    if (!sixel)
+        pm_error("Unable to allocation %u bytes for a row buffer", width + 2);
+
+    for (remainingHeight = height, sixelRow = 0;
+         remainingHeight > 0;
+         --remainingHeight) {
+
+        unsigned int i;
+
+        pbm_readpbmrow(ifP, bitrow, width, format);
+
+        switch (sixelRow) {
+        case 0 :
+            for (i = 0; i < width; ++i)
+                sixel[i] = bitrow[i] ;
+            break ;
+        case 1 :
+            for (i = 0; i < width; ++i)
+                sixel[i] += bitrow[i] << 1;
+            break ;
+        case 2 :
+            for (i = 0; i < width; ++i)
+                sixel[i] += bitrow[i] << 2;
+            break ;
+        case 3 :
+            for (i = 0; i < width; ++i)
+                sixel[i] += bitrow[i] << 3;
+            break ;
+        case 4 :
+            for (i = 0; i < width; ++i)
+                sixel[i] += bitrow[i] << 4;
+            break ;
+        case 5 :
+            for (i = 0; i < width; ++i)
+                sixel[i] += (bitrow[i] << 5) + 077;
+            outputSixelRecord(sixel, width);
+            break ; }
+        if (sixelRow == 5)
+            sixelRow = 0;
+        else
+            ++sixelRow;
+    }
+
+    if (sixelRow > 0) {
+        /* Incomplete sixel record needs to be output */
+        unsigned int i;
+        for (i = 0; i < width; ++i)
+            sixel[i] += 077;
+        outputSixelRecord(sixel, width);
+    }
+
+    pbm_freerow(bitrow);
+    free(sixel);
+}
 
 
 
@@ -169,6 +183,7 @@ main (int argc, char **argv) {
    const char *opt_bottom_margin = "3400";
    const char *opt_form_length = opt_bottom_margin;
 
+   FILE * ifP;
    int width, height, format ;
 
    pbm_init (&argc_copy, argv_copy) ;
@@ -210,16 +225,16 @@ main (int argc, char **argv) {
    }
 
    if( argn < argc ) {
-      input = pm_openr( argv[argn] );
+      ifP = pm_openr( argv[argn] );
       argn++;
    }
    else
-      input = stdin;
+      ifP = stdin;
 
    if( argn != argc )
       pm_usage(usage);
 
-   pbm_readpbminit (input, &width, &height, &format) ;
+   pbm_readpbminit (ifP, &width, &height, &format) ;
 
 /*
  * In explanation of the sequence below:
@@ -241,10 +256,10 @@ main (int argc, char **argv) {
       opt_form_length);
 
    /* Convert data */
-   convert (width, height, format) ;
+   convert (ifP, width, height, format) ;
 
    /* Terminate sixel data */
-   print ("\033\\\n") ;
+   puts ("\033\\") ;
 
    /* If the program failed, it previously aborted with nonzero completion
       code, via various function calls.
diff --git a/converter/pbm/pbmtomacp.c b/converter/pbm/pbmtomacp.c
index df5cbb0c..e02f5559 100644
--- a/converter/pbm/pbmtomacp.c
+++ b/converter/pbm/pbmtomacp.c
@@ -177,7 +177,7 @@ calculateCropPad(struct CmdlineInfo         const cmdline,
             pm_message("Specified -bottom value %u is beyond edge of "
                        "input image", cmdline.bottom);
 
-            bottom = MIN3(cmdline.bottom, rows - 1, top + MACP_ROWS - 1);
+        bottom = MIN3(cmdline.bottom, rows - 1, top + MACP_ROWS - 1);
     } else
         bottom = MIN(rows - 1, top + MACP_ROWS - 1);
 
@@ -234,7 +234,7 @@ writeMacpRowUnpacked(const bit  * const imageBits,
     char const marginByte = 0x00;  /* White bits for margin */
     unsigned int const rightMarginCharCt =
         MACP_COLCHARS - leftMarginCharCt - imageColCharCt;
-    
+
     unsigned int i;
 
     fputc(MACP_COLCHARS - 1, ofP);
@@ -361,7 +361,7 @@ encodeRowsWithShift(bit *                    const bitrow,
 
     for (row = 0; row < cropPad.imageHeight; ++row) {
         pbm_readpbmrow_bitoffset(ifP, bitrow, inCols, format, offset);
-        
+
         /* Trim off fractional margin portion in first byte of image data */
         if (leftTrim > 0) {
             bitrow[startChar] <<= leftTrim;
diff --git a/converter/pbm/pbmtomgr.c b/converter/pbm/pbmtomgr.c
index e8e30148..0e529740 100644
--- a/converter/pbm/pbmtomgr.c
+++ b/converter/pbm/pbmtomgr.c
@@ -40,7 +40,7 @@ putinit(unsigned int const rows,
 
 int
 main(int argc,
-     char * argv[]) {
+     const char * argv[]) {
 
     FILE * ifP;
     unsigned char * bitrow;
@@ -50,15 +50,11 @@ main(int argc,
     unsigned int row;
     unsigned int bytesPerRow;
         /* Number of packed bytes (8 columns per byte) in a row. */
-    unsigned int padright;
-        /* Number of columns added to the right of each row to get up to
-           a multiple of 8, i.e. an integral number of packed bytes.
-        */
     const char * inputFileName;
     unsigned int const maxDimension = 4095;
         /* Dimensions are 2 characters of the header -- 12 bits */
 
-    pbm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     if (argc-1 > 1)
         pm_error("Too many arguments (%u).  "
@@ -78,7 +74,6 @@ main(int argc,
     
     bitrow = pbm_allocrow_packed(cols);
     bytesPerRow = pbm_packed_bytes(cols);
-    padright = bytesPerRow * 8 - cols;
 
     putinit(rows, cols);
     
diff --git a/converter/pbm/pbmtonokia.c b/converter/pbm/pbmtonokia.c
index bf3b9e41..73b62b96 100644
--- a/converter/pbm/pbmtonokia.c
+++ b/converter/pbm/pbmtonokia.c
@@ -4,6 +4,7 @@
    Copyright information is at end of file.
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE    /* Make sure strcaseeq() is in nstring.h */
 #include <string.h>
@@ -321,10 +322,8 @@ convertToNol(bit **       const image,
     /* image */
     for (row = 0; row < rows; ++row) {
         unsigned int col;
-        unsigned int p;
-        unsigned int c;
 
-        for (p = 0, c = 0, col = 0; col < cols; ++col) {
+        for (col = 0; col < cols; ++col) {
             char const output = image[row][col] == PBM_BLACK ? '1' : '0';
 
             putc(output, ofP);
@@ -374,10 +373,8 @@ convertToNgg(bit **       const image,
 
     for (row = 0; row < rows; ++row) {
         unsigned int col;
-        unsigned int p;
-        unsigned int c;
 
-        for (p = 0, c = 0, col = 0; col < cols; ++col) {
+        for (col = 0; col < cols; ++col) {
             char const output = image[row][col] == PBM_BLACK ? '1' : '0';
 
             putc(output, ofP);
diff --git a/converter/pbm/pbmtopk.c b/converter/pbm/pbmtopk.c
index 3948ae0d..2f05d449 100644
--- a/converter/pbm/pbmtopk.c
+++ b/converter/pbm/pbmtopk.c
@@ -9,6 +9,7 @@
   https://www.tug.org/TUGboat/tb06-1/tb11gf.pdf
 */
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/pbm/pbmtoppa/pbm.c b/converter/pbm/pbmtoppa/pbm.c
index 2f8a42b1..ae36e0d2 100644
--- a/converter/pbm/pbmtoppa/pbm.c
+++ b/converter/pbm/pbmtoppa/pbm.c
@@ -11,126 +11,185 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 
+#include "pm.h"
+#include "nstring.h"
 #include "ppapbm.h"
 
-int make_pbm_stat(pbm_stat* pbm,FILE* fptr)
-{
-  char line[1024];
-
-  pbm->fptr=fptr;
-  pbm->version=none;
-  pbm->current_line=0;
-  pbm->unread = 0;
+int
+make_pbm_stat(pbm_stat * const pbmStatP,
+              FILE *     const ifP) {
+
+    char line[1024];
+    char * rc;
+    int retval;
+
+    pbmStatP->fptr         = ifP;
+    pbmStatP->version      = none;
+    pbmStatP->current_line = 0;
+    pbmStatP->unread       = 0;
+
+    rc = fgets(line, 1024, ifP);
+    if (rc == NULL)
+        retval = 0;
+    else {
+        line[strlen(line)-1] = 0;
+
+        if (streq(line,"P1"))
+            pbmStatP->version=P1;
+        if (streq(line,"P4"))
+            pbmStatP->version=P4;
+
+        if (pbmStatP->version == none) {
+            pm_message("unknown PBM magic '%s'", line);
+            retval = 0;
+        } else {
+            do {
+                char * rc;
+                rc = fgets(line, 1024, ifP);
+                if (rc == NULL)
+                    return 0;
+            } while (line[0] == '#');
+            {
+                int rc;
+                rc = sscanf(line, "%d %d",
+                            &pbmStatP->width, &pbmStatP->height);
+                if (rc != 2)
+                    retval = 0;
+                else {
+                    if (pbmStatP->width < 0) {
+                        pm_message("Image has negative width");
+                        retval = 0;
+                    } else if (pbmStatP->width > INT_MAX/2) {
+                        pm_message("Uncomputeably large width: %d",
+                                   pbmStatP->width);
+                        retval = 0;
+                    } else if (pbmStatP->height < 0) {
+                        pm_message("Image has negative height");
+                        retval = 0;
+                    } else if (pbmStatP->height > INT_MAX/2) {
+                        pm_message("Uncomputeably large height: %d",
+                                   pbmStatP->height);
+                        retval = 0;
+                    } else
+                        retval = 1;
+                }
+            }
+        }
+    }
+    return retval;
+}
 
-  if (fgets (line, 1024, fptr) == NULL)
-    return 0;
-  line[strlen(line)-1] = 0;
 
-  if(!strcmp(line,"P1")) pbm->version=P1;
-  if(!strcmp(line,"P4")) pbm->version=P4;
-  if(pbm->version == none)
-  {
-    fprintf(stderr,"pbm_readheader(): unknown PBM magic '%s'\n",line);
-    return 0;
-  }
 
-  do
-    if (fgets (line, 1024, fptr) == NULL)
-      return 0;
-  while (line[0] == '#');
+static int
+getbytes(FILE *          const ifP,
+         unsigned int    const width,
+         unsigned char * const data) {
+
+    unsigned char mask;
+    unsigned char acc;
+    unsigned char * place;
+    unsigned int num;
+    int retval;
+
+    if (width == 0)
+        retval = 0;
+    else {
+        for (mask = 0x80, acc = 0, num = 0, place = data; num < width; ) {
+            switch (getc(ifP)) {
+            case EOF:
+                return 0;
+            case '1':
+                acc |= mask;
+                /* fall through */
+            case '0':
+                mask >>= 1;
+                ++num;
+                if (mask == 0x00) { /* if (num % 8 == 0) */
+                    *place++ = acc;
+                    acc = 0;
+                    mask = 0x80;
+                }
+            }
+        }
+        if (width % 8 != 0)
+            *place = acc;
+
+        retval = 1;
+    }
+    return retval;
+}
 
-  if (2 != sscanf (line, "%d %d", &pbm->width, &pbm->height))
-    return 0;
 
-  return 1;
-}
 
-static int getbytes(FILE *fptr,int width,unsigned char* data)
-{
-  unsigned char mask,acc,*place;
-  int num;
-
-  if(!width) return 0;
-  for(mask=0x80, acc=0, num=0, place=data; num<width; )
-  {
-    switch(getc(fptr))
-    {
-    case EOF:      
-      return 0;
-    case '1':
-      acc|=mask;
-      /* fall through */
-    case '0':
-      mask>>=1;
-      num++;
-      if(!mask) /* if(num%8 == 0) */
-      {
-	*place++ = acc;
-	acc=0;
-	mask=0x80;
-      }
+int
+pbm_readline(pbm_stat *      const pbmStatP,
+             unsigned char * const data) {
+/*----------------------------------------------------------------------------
+  Read a single line into data which must be at least (pbmStatP->width+7)/8
+  bytes of storage.
+-----------------------------------------------------------------------------*/
+    int retval;
+
+    if (pbmStatP->current_line >= pbmStatP->height)
+        retval = 0;
+    else {
+        if (pbmStatP->unread) {
+            memcpy(data, pbmStatP->revdata, (pbmStatP->width+7)/8);
+            ++pbmStatP->current_line;
+            pbmStatP->unread = 0;
+            free(pbmStatP->revdata);
+            pbmStatP->revdata = NULL;
+            retval = 1;
+        } else {
+            switch (pbmStatP->version) {
+            case P1:
+                if (getbytes(pbmStatP->fptr, pbmStatP->width, data)) {
+                    pbmStatP->current_line++;
+                    retval = 1;
+                } else
+                    retval = 0;
+                break;
+            case P4: {
+                int tmp, tmp2;
+                tmp = (pbmStatP->width+7)/8;
+                tmp2 = fread(data,1,tmp,pbmStatP->fptr);
+                if (tmp2 == tmp) {
+                    ++pbmStatP->current_line;
+                    retval = 1;
+                } else {
+                    pm_message("error reading line data (%d)", tmp2);
+                    retval = 0;
+                }
+            } break;
+
+            default:
+                pm_message("unknown PBM version");
+                retval = 0;
+            }
+        }
     }
-  }
-  if(width%8)
-    *place=acc;
-  return 1;
+    return retval;
 }
 
-/* Reads a single line into data which must be at least (pbm->width+7)/8
-   bytes of storage */
-int pbm_readline(pbm_stat* pbm,unsigned char* data)
-{
-  int tmp,tmp2;
-
-  if(pbm->current_line >= pbm->height) return 0;
-
-  if (pbm->unread)
-    {
-      memcpy (data, pbm->revdata, (pbm->width+7)/8);
-      pbm->current_line++;
-      pbm->unread = 0;
-      free (pbm->revdata);
-      pbm->revdata = NULL;
-      return 1;
-    }
 
-  switch(pbm->version)
-  {
-  case P1:
-    if(getbytes(pbm->fptr,pbm->width,data))
-    {
-      pbm->current_line++;
-      return 1;
-    }
-    return 0;
-
-  case P4:
-    tmp=(pbm->width+7)/8;
-    tmp2=fread(data,1,tmp,pbm->fptr);
-    if(tmp2 == tmp)
-    {
-      pbm->current_line++;
-      return 1;
-    }
-    fprintf(stderr,"pbm_readline(): error reading line data (%d)\n",tmp2);
-    return 0;
 
-  default:
-    fprintf(stderr,"pbm_readline(): unknown PBM version\n");
-    return 0;
-  }
-}
+void
+pbm_unreadline(pbm_stat * const pbmStatP,
+               void *     const data) {
+/*----------------------------------------------------------------------------
+  Push a line back into the buffer; we read too much!
+-----------------------------------------------------------------------------*/
+    /* can store only one line in the unread buffer */
 
-/* push a line back into the buffer; we read too much! */
-void pbm_unreadline (pbm_stat *pbm, void *data)
-{
-  /* can only store one line in the unread buffer */
-  if (pbm->unread)
-    return;
-
-  pbm->unread = 1;
-  pbm->revdata = malloc ((pbm->width+7)/8);
-  memcpy (pbm->revdata, data, (pbm->width+7)/8);
-  pbm->current_line--;
+    if (!pbmStatP->unread) {
+        pbmStatP->unread = 1;
+        pbmStatP->revdata = malloc ((pbmStatP->width+7)/8);
+        memcpy(pbmStatP->revdata, data, (pbmStatP->width+7)/8);
+        --pbmStatP->current_line;
+    }
 }
+
+
diff --git a/converter/pbm/pbmtoppa/pbmtoppa.c b/converter/pbm/pbmtoppa/pbmtoppa.c
index f43c08a8..ff4a599e 100644
--- a/converter/pbm/pbmtoppa/pbmtoppa.c
+++ b/converter/pbm/pbmtoppa/pbmtoppa.c
@@ -4,6 +4,7 @@
  * 2-24-98
  */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE
     /* This makes sure strcasecmp() is in string.h */
 #include <stdio.h>
diff --git a/converter/pbm/pbmtox10bm b/converter/pbm/pbmtox10bm
index 9a1a7286..deb3aeab 100644
--- a/converter/pbm/pbmtox10bm
+++ b/converter/pbm/pbmtox10bm
@@ -1,3 +1,27 @@
+#!/bin/sh
+
+##############################################################################
+# This is essentially a Perl program.  We exec the Perl interpreter specifying
+# this same file as the Perl program and use the -x option to cause the Perl
+# interpreter to skip down to the Perl code.  The reason we do this instead of
+# just making /usr/bin/perl the script interpreter (instead of /bin/sh) is
+# that the user may have multiple Perl interpreters and the one he wants to
+# use is properly located in the PATH.  The user's choice of Perl interpreter
+# may be crucial, such as when the user also has a PERL5LIB environment
+# variable and it selects modules that work with only a certain main
+# interpreter program.
+#
+# An alternative some people use is to have /usr/bin/env as the script
+# interpreter.  We don't do that because we think the existence and
+# compatibility of /bin/sh is more reliable.
+#
+# Note that we aren't concerned about efficiency because the user who needs
+# high efficiency can use directly the programs that this program invokes.
+#
+##############################################################################
+
+exec perl -w -x -S -- "$0" "$@"
+
 #! /usr/bin/perl
 
 #============================================================================
@@ -14,6 +38,19 @@ use strict;
 use File::Basename;
 use Cwd 'abs_path';
 
+sub doVersionHack($) {
+    my ($argvR) = @_;
+
+    my $arg1 = $argvR->[0];
+
+    if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) {
+        my $termStatus = system('pbmtoxbm', '--version');
+        exit($termStatus == 0 ? 0 : 1);
+    }
+}
+
+doVersionHack(\@ARGV);
+
 my $infile;
 
 foreach (@ARGV) {
diff --git a/converter/pbm/pbmtoxbm.c b/converter/pbm/pbmtoxbm.c
index 14c6b85e..ecb72b30 100644
--- a/converter/pbm/pbmtoxbm.c
+++ b/converter/pbm/pbmtoxbm.c
@@ -20,6 +20,7 @@
 
 */
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/pgm/sbigtopgm.c b/converter/pgm/sbigtopgm.c
index 3c223c47..7833cb62 100644
--- a/converter/pgm/sbigtopgm.c
+++ b/converter/pgm/sbigtopgm.c
@@ -54,7 +54,6 @@ parseCommandLine(int argc, const char ** argv,
 
     MALLOCARRAY_NOFAIL(option_def, 100);
     
-    option_def_index = 0;   /* incremented by OPTENT3 */
     OPTENTINIT;
 
     opt.opt_table     = option_def;
diff --git a/converter/ppm/hpcdtoppm/Makefile b/converter/ppm/hpcdtoppm/Makefile
index ddf79ee5..59ba3630 100644
--- a/converter/ppm/hpcdtoppm/Makefile
+++ b/converter/ppm/hpcdtoppm/Makefile
@@ -14,12 +14,12 @@ MERGE_OBJECTS =
 
 include $(SRCDIR)/common.mk
 
-install: install.bin.local
+install.bin install.merge: install.bin.local
 .PHONY: install.bin.local
 install.bin.local: $(PKGDIR)/bin
 # In June 2002, pcdovtoppm replaced pcdindex
 	cd $(PKGDIR)/bin ; \
-	$(SYMLINK) pcdindex$(EXE) pcdovtoppm$(EXE)
+	$(SYMLINK) pcdovtoppm$(EXE) pcdindex$(EXE) 
 
 
 FORCE:
diff --git a/converter/ppm/hpcdtoppm/hpcdtoppm b/converter/ppm/hpcdtoppm/hpcdtoppm
index cb6d084b..2af4a384 100755
--- a/converter/ppm/hpcdtoppm/hpcdtoppm
+++ b/converter/ppm/hpcdtoppm/hpcdtoppm
@@ -1,24 +1,24 @@
 #!/bin/sh
 
 cat <<EOF 1>&2
-You are running a program named 'hpcdtoppm' that stands in for the
-real program by that name.
+You are running a program named 'hpcdtoppm' that stands in for the real
+program by that name.
 
-The real 'hpcdtoppm' is a program that converts an image from Photo CD
-format to PPM format.  The program you are running now just issues the
-message you are reading.
+The real 'hpcdtoppm' is a program that converts an image from Photo CD format
+to PPM format.  The program you are running now just issues the message you
+are reading.
 
 Please get a copy of Hpcdtoppm from 
 
-  ftp://ibiblio.org/pub/linux/graphics/convert
+  http://terasaur.org/item/show/hpcdtoppm-netpbm-convert-photo-cd/4967
 
 and replace this stand-in program with the real one.
 
-The point of this is that this stand-in 'hpcdtoppm' is distributed
-with Netpbm on Sourceforge.  Hpcdtoppm does not meet the requirements
-to be distributed on Sourceforge because a copyright holder does not
-permit people to sell copies.  In the past the real Hpcdtoppm was
-distributed with Netpbm on Sourceforge by mistake.
+The point of this is that this stand-in 'hpcdtoppm' is distributed with Netpbm
+on Sourceforge.  Hpcdtoppm does not meet the requirements to be distributed on
+Sourceforge because a copyright holder does not permit people to sell copies.
+At one time, the real Hpcdtoppm was distributed with Netpbm on Sourceforge by
+mistake.
 
 EOF
 
diff --git a/converter/ppm/ilbmtoppm.c b/converter/ppm/ilbmtoppm.c
index 280ca939..92d4d6f2 100644
--- a/converter/ppm/ilbmtoppm.c
+++ b/converter/ppm/ilbmtoppm.c
@@ -99,10 +99,9 @@ static bool debug = FALSE;
 
 
 static char *
-ID2string(id)
-    IFF_ID id;
-{
-    static char str[] = "abcd";
+ID2string(IFF_ID const id) {
+
+    static char str[4];
 
     str[0] = (char)(id >> 24 & 0xff);
     str[1] = (char)(id >> 16 & 0xff);
@@ -141,16 +140,15 @@ alloc_cmap(void) {
 
 
 static rawtype *
-alloc_rawrow(cols)
-    int cols;
-{
-    rawtype *r;
-    int i;
+alloc_rawrow(unsigned int const cols) {
+
+    rawtype * r;
+    unsigned int col;
 
     MALLOCARRAY_NOFAIL(r, cols);
 
-    for( i = 0; i < cols; i++ )
-        r[i] = 0;
+    for (col = 0; col < cols; ++col)
+        r[col] = 0;
 
     return r;
 }
@@ -161,17 +159,17 @@ alloc_rawrow(cols)
  ****************************************************************************/
 
 static void
-readerr(f, iffid)
-    FILE *f;
-    IFF_ID iffid;
-{
-    if( ferror(f) )
+readerr(FILE * const fP,
+        IFF_ID const iffId) {
+
+    if (ferror(fP))
         pm_error("read error");
     else
-        pm_error("premature EOF in %s chunk", ID2string(iffid));
+        pm_error("premature EOF in %s chunk", ID2string(iffId));
 }
 
 
+
 static void
 read_bytes(FILE *          const ifP,
            int             const bytes,
@@ -191,21 +189,20 @@ read_bytes(FILE *          const ifP,
 
 
 static unsigned char
-get_byte(ifP, iffid, counter)
-    FILE* ifP;
-    IFF_ID iffid;
-    long *counter;
-{
+get_byte(FILE *          const ifP,
+         IFF_ID          const iffId,
+         unsigned long * const counterP) {
+
     int i;
 
-    if( counter ) {
-        if( *counter == 0 )
-            pm_error("insufficient data in %s chunk", ID2string(iffid));
-        --(*counter);
+    if (counterP) {
+        if (*counterP == 0 )
+            pm_error("insufficient data in %s chunk", ID2string(iffId));
+        --(*counterP);
     }
     i = getc(ifP);
-    if( i == EOF )
-        readerr(ifP, iffid);
+    if (i == EOF)
+        readerr(ifP, iffId);
 
     return (unsigned char) i;
 }
@@ -216,7 +213,7 @@ get_big_long(FILE *          const ifP,
              unsigned long * const counterP) {
 
     long l;
-    
+
     if (counterP) {
         if (*counterP < 4)
             pm_error("insufficient data in %s chunk", ID2string(iffid));
@@ -273,114 +270,117 @@ chunk_end(FILE *        const ifP,
 
 static void
 skip_chunk(FILE *        const ifP,
-           IFF_ID        const iffid,
-           unsigned long const chunksize) {
-    unsigned long remainingChunksize;
+           IFF_ID        const iffId,
+           unsigned long const chunkSize) {
 
-    remainingChunksize = chunksize;  /* initial value */
+    unsigned long remainingChunkSize;
 
-    while (remainingChunksize > 0)
-        get_byte(ifP, iffid, &remainingChunksize);
+    for (remainingChunkSize = chunkSize; remainingChunkSize > 0; )
+        get_byte(ifP, iffId, &remainingChunkSize);
 }
 
 
 
 static void
 display_chunk(FILE *        const ifP,
-              IFF_ID        const iffid,
-              unsigned long const chunksize) {
+              IFF_ID        const iffId,
+              unsigned long const chunkSize) {
 
     int byte;
-    unsigned long remainingChunksize;
+    unsigned long remainingChunkSize;
 
-    pm_message("contents of %s chunk:", ID2string(iffid));
+    pm_message("contents of %s chunk:", ID2string(iffId));
 
-    remainingChunksize = chunksize;  /* initial value */
-    byte = '\0';
+    for (remainingChunkSize = chunkSize, byte = '\0';
+         remainingChunkSize > 0; ) {
 
-    while (remainingChunksize > 0) {
-        byte = get_byte(ifP, iffid, &remainingChunksize);
+        byte = get_byte(ifP, iffId, &remainingChunkSize);
         if (fputc(byte, stderr) == EOF)
             pm_error("write error");
     }
-    if (byte != '\n')
-        if (fputc('\n', stderr) == EOF)
+    if (byte != '\n') {
+        int rc;
+        rc = fputc('\n', stderr);
+        if (rc == EOF)
             pm_error("write error");
+    }
 }
 
 
+
 static void
-read_cmap(FILE *     const ifP,
-          IFF_ID     const iffid,
-          long       const chunksize,
-          ColorMap * const cmap) {
+read_cmap(FILE *        const ifP,
+          IFF_ID        const iffId,
+          unsigned long const chunkSize,
+          ColorMap *    const cmapP) {
 
-    long colors;
+    unsigned long const colorCt = chunkSize / 3;
 
-    colors = chunksize / 3;
-    if( colors == 0 ) {
-        pm_error("warning - empty %s colormap", ID2string(iffid));
-        skip_chunk(ifP, iffid, chunksize);
+    if (colorCt == 0) {
+        pm_error("warning - empty %s colormap", ID2string(iffId));
+        skip_chunk(ifP, iffId, chunkSize);
     } else {
         unsigned int i;
-        if( cmap->color )               /* prefer CMAP-chunk over CMYK-chunk */
-            ppm_freerow(cmap->color);
-        cmap->color = ppm_allocrow(colors);
-        cmap->ncolors = colors;
-        
-        for( i = 0; i < colors; ++i ) {
-            int r, g, b;
-            r = get_byte(ifP, iffid, &chunksize);
-            g = get_byte(ifP, iffid, &chunksize);
-            b = get_byte(ifP, iffid, &chunksize);
-            PPM_ASSIGN(cmap->color[i], r, g, b);
+        unsigned long remainingChunkSize;
+
+        if (cmapP->color)               /* prefer CMAP-chunk over CMYK-chunk */
+            ppm_freerow(cmapP->color);
+        cmapP->color   = ppm_allocrow(colorCt);
+        cmapP->ncolors = colorCt;
+
+        for (i = 0, remainingChunkSize = chunkSize; i < colorCt; ++i ) {
+            int const r = get_byte(ifP, iffId, &remainingChunkSize);
+            int const g = get_byte(ifP, iffId, &remainingChunkSize);
+            int const b = get_byte(ifP, iffId, &remainingChunkSize);
+            PPM_ASSIGN(cmapP->color[i], r, g, b);
         }
-        chunk_end(ifP, iffid, chunksize);
+        chunk_end(ifP, iffId, remainingChunkSize);
     }
 }
 
 
 
 static void
-read_cmyk(FILE *     const ifP,
-          IFF_ID     const iffid,
-          long       const chunksize,
-          ColorMap * const cmap) {
+read_cmyk(FILE *        const ifP,
+          IFF_ID        const iffId,
+          unsigned long const chunkSize,
+          ColorMap *    const cmapP) {
 
-    if( HAS_COLORMAP(cmap) ) {      /* prefer RGB color map */
-        skip_chunk(ifP, iffid, chunksize);
+    if (HAS_COLORMAP(cmapP)) {      /* prefer RGB color map */
+        skip_chunk(ifP, iffId, chunkSize);
     } else {
-        long const colors = chunksize/4;
-        if( colors == 0 ) {
-            pm_error("warning - empty %s colormap", ID2string(iffid));
-            skip_chunk(ifP, iffid, chunksize);
+        unsigned long const colorCt = chunkSize / 4;
+        if (colorCt == 0 ) {
+            pm_error("warning - empty %s colormap", ID2string(iffId));
+            skip_chunk(ifP, iffId, chunkSize);
         } else {
             unsigned int i;
-            cmap->color = ppm_allocrow(colors);
-            cmap->ncolors = colors;
-            
-            for( i = 0; i < colors; ++i ) {
-                int c, m, y, k;
-                c = get_byte(ifP, iffid, &chunksize);
-                m = get_byte(ifP, iffid, &chunksize);
-                y = get_byte(ifP, iffid, &chunksize);
-                k = get_byte(ifP, iffid, &chunksize);
+            unsigned long remainingChunkSize;
+
+            cmapP->color   = ppm_allocrow(colorCt);
+            cmapP->ncolors = colorCt;
+
+            for (i = 0, remainingChunkSize = chunkSize; i < colorCt; ++i) {
+                int const c = get_byte(ifP, iffId, &remainingChunkSize);
+                int const m = get_byte(ifP, iffId, &remainingChunkSize);
+                int const y = get_byte(ifP, iffId, &remainingChunkSize);
+                int const k = get_byte(ifP, iffId, &remainingChunkSize);
 
                 {
-                    pixval const red = 
+                    pixval const red =
                         MAXCOLVAL - MIN(MAXCOLVAL,
-                                        c*(MAXCOLVAL-k)/MAXCOLVAL+k); 
-                    pixval const green = 
+                                        c * (MAXCOLVAL-k) / MAXCOLVAL+k);
+                    pixval const green =
                         MAXCOLVAL - MIN(MAXCOLVAL,
-                                        m*(MAXCOLVAL-k)/MAXCOLVAL+k);
-                    pixval const blue = 
+                                        m * (MAXCOLVAL-k) / MAXCOLVAL+k);
+                    pixval const blue =
                         MAXCOLVAL - MIN(MAXCOLVAL,
-                                        y*(MAXCOLVAL-k)/MAXCOLVAL+k);
+                                        y * (MAXCOLVAL-k) / MAXCOLVAL+k);
 
-                    PPM_ASSIGN(cmap->color[i], red, green, blue);
+                    PPM_ASSIGN(cmapP->color[i], red, green, blue);
                 }
             }
-            chunk_end(ifP, iffid, chunksize);
+            chunk_end(ifP, iffId, remainingChunkSize);
         }
     }
 }
@@ -389,45 +389,45 @@ read_cmyk(FILE *     const ifP,
 
 static void
 read_clut(FILE *        const ifP,
-          IFF_ID        const iffid,
-          unsigned long const chunksize,
-          ColorMap *    const cmap) {
-
-    if (chunksize != CLUTSize) {
-        pm_message("invalid size for %s chunk - skipping it", 
-                   ID2string(iffid));
-        skip_chunk(ifP, iffid, chunksize);
+          IFF_ID        const iffId,
+          unsigned long const chunkSize,
+          ColorMap *    const cmapP) {
+
+    if (chunkSize != CLUTSize) {
+        pm_message("invalid size for %s chunk - skipping it",
+                   ID2string(iffId));
+        skip_chunk(ifP, iffId, chunkSize);
     } else {
         long type;
         unsigned char * lut;
-        unsigned long remainingChunksize;
+        unsigned long remainingChunkSize;
         unsigned int i;
 
-        remainingChunksize = chunksize;  /* initial value */
+        remainingChunkSize = chunkSize;  /* initial value */
 
-        type = get_big_long(ifP, iffid, &remainingChunksize);
-        get_big_long(ifP, iffid, &remainingChunksize); /* skip reserved fld */
+        type = get_big_long(ifP, iffId, &remainingChunkSize);
+        get_big_long(ifP, iffId, &remainingChunkSize); /* skip reserved fld */
 
         MALLOCARRAY_NOFAIL(lut, 256);
         for( i = 0; i < 256; ++i )
-            lut[i] = get_byte(ifP, iffid, &remainingChunksize);
+            lut[i] = get_byte(ifP, iffId, &remainingChunkSize);
 
         switch( type ) {
         case CLUT_MONO:
-            cmap->monolut  = lut;
+            cmapP->monolut  = lut;
             break;
         case CLUT_RED:
-            cmap->redlut   = lut;
+            cmapP->redlut   = lut;
             break;
         case CLUT_GREEN:
-            cmap->greenlut = lut;
+            cmapP->greenlut = lut;
             break;
         case CLUT_BLUE:
-            cmap->bluelut  = lut;
+            cmapP->bluelut  = lut;
             break;
         default:
             pm_message("warning - %s type %ld not recognized",
-                       ID2string(iffid), type);
+                       ID2string(iffId), type);
             free(lut);
         }
     }
@@ -446,8 +446,8 @@ warnNonsquarePixels(uint8_t const xAspect,
             pm_message("%s; to fix do a 'pamscale -%cscale %g'",
                        baseMsg,
                        xAspect > yAspect ? 'x' : 'y',
-                       xAspect > yAspect ? 
-                       (float)xAspect/yAspect : 
+                       xAspect > yAspect ?
+                       (float)xAspect/yAspect :
                        (float)yAspect/xAspect);
         else
             pm_message("%s", baseMsg);
@@ -464,7 +464,7 @@ read_bmhd(FILE *        const ifP,
     BitMapHeader * bmhdP;
 
     if (chunksize != BitMapHeaderSize) {
-        pm_message("invalid size for %s chunk - skipping it", 
+        pm_message("invalid size for %s chunk - skipping it",
                    ID2string(iffid));
         skip_chunk(ifP, iffid, chunksize);
         bmhdP = NULL;
@@ -474,7 +474,7 @@ read_bmhd(FILE *        const ifP,
         MALLOCVAR_NOFAIL(bmhdP);
 
         remainingChunksize = chunksize;  /* initial value */
-        
+
         bmhdP->w = get_big_short(ifP, iffid, &remainingChunksize);
         bmhdP->h = get_big_short(ifP, iffid, &remainingChunksize);
         bmhdP->x = get_big_short(ifP, iffid, &remainingChunksize);
@@ -492,7 +492,7 @@ read_bmhd(FILE *        const ifP,
 
         if (verbose) {
             if (typeid == ID_ILBM)
-                pm_message("dimensions: %dx%d, %d planes", 
+                pm_message("dimensions: %dx%d, %d planes",
                            bmhdP->w, bmhdP->h, bmhdP->nPlanes);
             else
                 pm_message("dimensions: %dx%d", bmhdP->w, bmhdP->h);
@@ -509,11 +509,11 @@ read_bmhd(FILE *        const ifP,
                 case mskHasTransparentColor:
                     if (!maskfile)
                         pm_message("use '-maskfile <filename>' "
-                                   "to generate a PBM mask file from %s", 
+                                   "to generate a PBM mask file from %s",
                                    mskNAME[bmhdP->masking]);
                     break;
                 case mskLasso:
-                    pm_message("warning - masking type '%s' not recognized", 
+                    pm_message("warning - masking type '%s' not recognized",
                                mskNAME[bmhdP->masking]);
                     break;
                 default:
@@ -529,7 +529,7 @@ read_bmhd(FILE *        const ifP,
 
         /* fix aspect ratio */
         if (bmhdP->xAspect == 0 || bmhdP->yAspect == 0) {
-            pm_message("warning - illegal aspect ratio %d:%d, using 1:1", 
+            pm_message("warning - illegal aspect ratio %d:%d, using 1:1",
                        bmhdP->xAspect, bmhdP->yAspect);
             bmhdP->xAspect = bmhdP->yAspect = 1;
         }
@@ -647,7 +647,7 @@ decode_mask(FILE *          const ifP,
     case mskNone:
         break;
     case mskHasMask:        /* mask plane */
-        read_ilbm_plane(ifP, remainingChunksizeP, RowBytes(cols), 
+        read_ilbm_plane(ifP, remainingChunksizeP, RowBytes(cols),
                         bmhdP->compression);
         if (maskfile) {
             ilp = ilbmrow;
@@ -669,7 +669,7 @@ decode_mask(FILE *          const ifP,
     case mskHasTransparentColor:
         if (!chunkyrow)
             pm_error("decode_mask(): chunkyrow == NULL - can't happen");
-        
+
         if (maskfile) {
             for (col = 0; col < cols; ++col) {
                 if (chunkyrow[col] == bmhdP->transparentColor)
@@ -685,7 +685,7 @@ decode_mask(FILE *          const ifP,
         pm_error("This program does not know how to process Lasso masking");
         break;
     default:
-        pm_error("decode_mask(): unknown masking type %d - can't happen", 
+        pm_error("decode_mask(): unknown masking type %d - can't happen",
                  bmhdP->masking);
     }
 }
@@ -697,79 +697,82 @@ decode_mask(FILE *          const ifP,
 
 
 static void
-multi_adjust(cmap, row, palchange)
-    ColorMap *cmap;
-    int row;
-    PaletteChange *palchange;
-{
-    int i, reg;
-
-    for( i = 0; palchange[i].reg != MP_REG_END; i++ ) {
-        reg = palchange[i].reg;
-        if( reg >= cmap->ncolors ) {
+multi_adjust(ColorMap *            const cmapP,
+             unsigned int          const row,
+             const PaletteChange * const palchange) {
+
+    unsigned int i;
+
+    for (i = 0; palchange[i].reg != MP_REG_END; ++i) {
+        int const reg = palchange[i].reg;
+        if (reg >= cmapP->ncolors) {
             pm_message("warning - palette change register out of range");
-            pm_message("    row %d  change structure %d  reg=%d (max %d)", 
-                       row, i, reg, cmap->ncolors-1);
+            pm_message("    row %u  change structure %d  reg=%d (max %d)",
+                       row, i, reg, cmapP->ncolors-1);
             pm_message("    ignoring it...  "
                        "colors might get messed up from here");
-        }
-        else
-        if( reg != MP_REG_IGNORE ) {
-            PPM_ASSIGN(cmap->color[reg], 
-                       palchange[i].r, palchange[i].g, palchange[i].b);
+        } else {
+            if (reg != MP_REG_IGNORE) {
+                PPM_ASSIGN(cmapP->color[reg],
+                           palchange[i].r, palchange[i].g, palchange[i].b);
+            }
         }
     }
 }
 
+
+
 static void
-multi_init(cmap, viewportmodes)
-    ColorMap *cmap;
-    long viewportmodes;
-{
-    if( cmap->mp_init )
-        multi_adjust(cmap, -1, cmap->mp_init);
-    if( !(viewportmodes & vmLACE) )
-        cmap->mp_flags &= ~(MP_FLAGS_SKIPLACED);
+multi_init(ColorMap * const cmapP,
+           long       const viewportmodes) {
+
+    if (cmapP->mp_init)
+        multi_adjust(cmapP, -1, cmapP->mp_init);
+    if (!(viewportmodes & vmLACE) )
+        cmapP->mp_flags &= ~(MP_FLAGS_SKIPLACED);
 }
 
+
+
 static void
-multi_update(cmap, row)
-    ColorMap *cmap;
-    int row;
-{
-    if( cmap->mp_flags & MP_FLAGS_SKIPLACED ) {
-        if( ODD(row) )
+multi_update(ColorMap *   const cmapP,
+             unsigned int const row) {
+
+    if (cmapP->mp_flags & MP_FLAGS_SKIPLACED) {
+        if (ODD(row))
             return;
-        if( row/2 < cmap->mp_rows && cmap->mp_change[row/2] )
-            multi_adjust(cmap, row, cmap->mp_change[row/2]);
-    }
-    else {
-        if( row < cmap->mp_rows && cmap->mp_change[row] )
-            multi_adjust(cmap, row, cmap->mp_change[row]);
+        if (row/2 < cmapP->mp_rows && cmapP->mp_change[row/2])
+            multi_adjust(cmapP, row, cmapP->mp_change[row/2]);
+    } else {
+        if (row < cmapP->mp_rows && cmapP->mp_change[row])
+            multi_adjust(cmapP, row, cmapP->mp_change[row]);
     }
 }
 
+
+
 static void
-multi_free(cmap)
-    ColorMap *cmap;
-{
-    int i;
+multi_free(ColorMap * const cmapP) {
 
-    if( cmap->mp_init ) {
-        free(cmap->mp_init);
-        cmap->mp_init = NULL;
+    if (cmapP->mp_init) {
+        free(cmapP->mp_init);
+        cmapP->mp_init = NULL;
     }
-    if( cmap->mp_change ) {
-        for( i = 0; i < cmap->mp_rows; i++ ) {
-            if( cmap->mp_change[i] )
-                free(cmap->mp_change[i]);
+
+    if (cmapP->mp_change) {
+        unsigned int i;
+
+        for (i = 0; i < cmapP->mp_rows; ++i) {
+            if (cmapP->mp_change[i])
+                free(cmapP->mp_change[i]);
         }
-        free(cmap->mp_change);
-        cmap->mp_change = NULL;
+        free(cmapP->mp_change);
+        cmapP->mp_change = NULL;
     }
-    cmap->mp_rows = 0;
-    cmap->mp_type = 0;
-    cmap->mp_flags = 0;
+
+    cmapP->mp_rows  = 0;
+    cmapP->mp_type  = 0;
+    cmapP->mp_flags = 0;
 }
 
 
@@ -788,7 +791,7 @@ analyzeCmapSamples(const ColorMap * const cmapP,
     pixval       maxSample;
     bool         shifted;
     unsigned int i;
-        
+
     for (i = 0, maxSample = 0, shifted = true; i < cmapP->ncolors; ++i) {
         pixval const r = PPM_GETR(cmapP->color[i]);
         pixval const g = PPM_GETG(cmapP->color[i]);
@@ -857,7 +860,7 @@ transpColor(const BitMapHeader * const bmhdP,
     pixel * transpColorP;
 
     if (bmhdP) {
-        if (bmhdP->masking == mskHasTransparentColor || 
+        if (bmhdP->masking == mskHasTransparentColor ||
             bmhdP->masking == mskLasso) {
             MALLOCVAR_NOFAIL(transpColorP);
 
@@ -909,80 +912,83 @@ prepareCmap(const BitMapHeader * const bmhdP,
 
 
 static pixval
-lookup_red(cmap, oldval)
-    ColorMap *cmap;
-    int oldval;
-{
-    if( cmap && cmap->redlut && oldval < 256 )
-        return cmap->redlut[oldval];
+lookup_red(ColorMap *   const cmapP,
+           unsigned int const oldval) {
+
+    if (cmapP && cmapP->redlut && oldval < 256)
+        return cmapP->redlut[oldval];
     else
         return oldval;
 }
 
+
+
 static pixval
-lookup_green(cmap, oldval)
-    ColorMap *cmap;
-    int oldval;
-{
-    if( cmap && cmap->greenlut && oldval < 256 )
-        return cmap->greenlut[oldval];
+lookup_green(ColorMap *   const cmapP,
+             unsigned int const oldval) {
+
+    if (cmapP && cmapP->greenlut && oldval < 256)
+        return cmapP->greenlut[oldval];
     else
         return oldval;
 }
 
+
+
 static pixval
-lookup_blue(cmap, oldval)
-    ColorMap *cmap;
-    int oldval;
-{
-    if( cmap && cmap->bluelut && oldval < 256 )
-        return cmap->bluelut[oldval];
+lookup_blue(ColorMap *   const cmapP,
+            unsigned int const oldval) {
+
+    if (cmapP && cmapP->bluelut && oldval < 256)
+        return cmapP->bluelut[oldval];
     else
         return oldval;
 }
 
+
+
 static pixval
-lookup_mono(cmap, oldval)
-    ColorMap *cmap;
-    int oldval;
-{
-    if( cmap && cmap->monolut && oldval < 256 )
-        return cmap->monolut[oldval];
+lookup_mono(ColorMap *   const cmapP,
+            unsigned int const oldval) {
+
+    if (cmapP && cmapP->monolut && oldval < 256)
+        return cmapP->monolut[oldval];
     else
         return oldval;
 }
 
+
+
 static ColorMap *
-ehbcmap(cmap)
-    ColorMap *cmap;
-{
-    pixel *tempcolor = NULL;
-    int i, col;
-
-    col = cmap->ncolors;
-
-    tempcolor = ppm_allocrow(col * 2);
-    for( i = 0; i < col; i++ ) {
-        tempcolor[i] = cmap->color[i];
-        PPM_ASSIGN(tempcolor[col + i],  PPM_GETR(cmap->color[i]) / 2,
-                                        PPM_GETG(cmap->color[i]) / 2,
-                                        PPM_GETB(cmap->color[i]) / 2 );
+ehbcmap(ColorMap * const cmapP) {
+
+    pixel * tempcolor;
+    unsigned int i;
+
+    tempcolor = ppm_allocrow(cmapP->ncolors * 2);
+
+    for (i = 0; i < cmapP->ncolors; ++i) {
+        tempcolor[i] = cmapP->color[i];
+        PPM_ASSIGN(tempcolor[cmapP->ncolors + i],
+                   PPM_GETR(cmapP->color[i]) / 2,
+                   PPM_GETG(cmapP->color[i]) / 2,
+                   PPM_GETB(cmapP->color[i]) / 2);
     }
-    ppm_freerow(cmap->color);
-    cmap->color = tempcolor;
-    cmap->ncolors *= 2;
+    ppm_freerow(cmapP->color);
+    cmapP->color = tempcolor;
+    cmapP->ncolors *= 2;
 
-    return cmap;
+    return cmapP;
 }
 
 
 
 static pixval
-lut_maxval(ColorMap * const cmap, 
+lut_maxval(ColorMap * const cmap,
            pixval     const maxval) {
 
     pixval retval;
-    
+
     if (maxval >= 255)
         retval = maxval;
     else {
@@ -993,16 +999,16 @@ lut_maxval(ColorMap * const cmap,
             unsigned char maxlut;
             maxlut = maxval;
             for( i = 0; i < maxval; i++ ) {
-                if( cmap->redlut   && cmap->redlut[i]   > maxlut ) 
+                if( cmap->redlut   && cmap->redlut[i]   > maxlut )
                     maxlut = cmap->redlut[i];
-                if( cmap->greenlut && cmap->greenlut[i] > maxlut ) 
+                if( cmap->greenlut && cmap->greenlut[i] > maxlut )
                     maxlut = cmap->greenlut[i];
-                if( cmap->bluelut  && cmap->bluelut[i]  > maxlut ) 
+                if( cmap->bluelut  && cmap->bluelut[i]  > maxlut )
                     maxlut = cmap->bluelut[i];
             }
             pm_message("warning - "
                        "%d-bit index into 8-bit color lookup table, "
-                       "table maxval=%d", 
+                       "table maxval=%d",
                        pm_maxvaltobits(maxval), maxlut);
             if( maxlut != maxval )
                 retval = 255;
@@ -1017,49 +1023,49 @@ lut_maxval(ColorMap * const cmap,
 
 
 static void
-get_color(cmap, idx, red, green, blue)
-    ColorMap *cmap;
-    int idx;
-    pixval *red, *green, *blue;
-{
-    if( HAS_COLORMAP(cmap) ) {
-        pixval r, g, b;
-
-        if( idx >= cmap->ncolors )
-            pm_error("color index out of range: %d (max %d)", 
-                     idx, cmap->ncolors);
-        r = PPM_GETR(cmap->color[idx]);
-        g = PPM_GETG(cmap->color[idx]);
-        b = PPM_GETB(cmap->color[idx]);
-
-        *red    = lookup_red(cmap, r);
-        *green  = lookup_green(cmap, g);
-        *blue   = lookup_blue(cmap, b);
-    }
-    else {
-        *red = *green = *blue = lookup_mono(cmap, idx);
-    }
+get_color(ColorMap *   const cmapP,
+          unsigned int const idx,
+          pixval *     const redP,
+          pixval *     const greenP,
+          pixval *     const blueP) {
+
+    if (HAS_COLORMAP(cmapP)) {
+        if (idx >= cmapP->ncolors)
+            pm_error("color index out of range: %u (max %u)",
+                     idx, cmapP->ncolors);
+        else {
+            pixval const r = PPM_GETR(cmapP->color[idx]);
+            pixval const g = PPM_GETG(cmapP->color[idx]);
+            pixval const b = PPM_GETB(cmapP->color[idx]);
+
+            *redP    = lookup_red   (cmapP, r);
+            *greenP  = lookup_green (cmapP, g);
+            *blueP   = lookup_blue  (cmapP, b);
+        }
+    } else
+        *redP = *greenP = *blueP = lookup_mono(cmapP, idx);
 }
 
 
+
 /****************************************************************************
  Conversion functions
  ****************************************************************************/
 
 static void
-std_to_ppm(FILE *         const ifP, 
-           long           const chunksize, 
-           BitMapHeader * const bmhdP, 
-           ColorMap *     const cmap, 
+std_to_ppm(FILE *         const ifP,
+           long           const chunksize,
+           BitMapHeader * const bmhdP,
+           ColorMap *     const cmap,
            long           const viewportmodes);
 
 
 
 static void
-ham_to_ppm(FILE *         const ifP, 
-           long           const chunksize, 
-           BitMapHeader * const bmhdP, 
-           ColorMap *     const cmap, 
+ham_to_ppm(FILE *         const ifP,
+           long           const chunksize,
+           BitMapHeader * const bmhdP,
+           ColorMap *     const cmap,
            long           const viewportmodes) {
 
     int cols, rows, hambits, hammask, hamshift, hammask2, col, row;
@@ -1077,7 +1083,7 @@ ham_to_ppm(FILE *         const ifP,
     if( hambits > 8 || hambits < 1 ) {
         int const assumed_viewportmodes = viewportmodes & ~(vmHAM);
 
-        pm_message("%d-plane HAM?? - interpreting image as a normal ILBM", 
+        pm_message("%d-plane HAM?? - interpreting image as a normal ILBM",
                    bmhdP->nPlanes);
         std_to_ppm(ifP, chunksize, bmhdP, cmap, assumed_viewportmodes);
         return;
@@ -1085,8 +1091,8 @@ ham_to_ppm(FILE *         const ifP,
         unsigned long remainingChunksize;
         pixel * transpColorP;
 
-        pm_message("input is a %sHAM%d file", 
-                   HAS_MULTIPALETTE(cmap) ? "multipalette " : "", 
+        pm_message("input is a %sHAM%d file",
+                   HAS_MULTIPALETTE(cmap) ? "multipalette " : "",
                    bmhdP->nPlanes);
 
         if( HAS_COLORLUT(cmap) || HAS_MONOLUT(cmap) ) {
@@ -1095,7 +1101,7 @@ ham_to_ppm(FILE *         const ifP,
             if( cmap->greenlut )    free(cmap->greenlut);
             if( cmap->bluelut )     free(cmap->bluelut);
             if( cmap->monolut )     free(cmap->monolut);
-            cmap->redlut = cmap->greenlut = cmap->bluelut = 
+            cmap->redlut = cmap->greenlut = cmap->bluelut =
                 cmap->monolut = NULL;
         }
         if( !HAS_COLORMAP(cmap) ) {
@@ -1165,10 +1171,10 @@ ham_to_ppm(FILE *         const ifP,
 
 
 static void
-std_to_ppm(FILE *         const ifP, 
-           long           const chunksize, 
-           BitMapHeader * const bmhdP, 
-           ColorMap *     const cmap, 
+std_to_ppm(FILE *         const ifP,
+           long           const chunksize,
+           BitMapHeader * const bmhdP,
+           ColorMap *     const cmap,
            long           const viewportmodes) {
 
     if (viewportmodes & vmHAM) {
@@ -1213,7 +1219,7 @@ std_to_ppm(FILE *         const ifP,
         ppm_writeppminit( stdout, cols, rows, maxval, 0 );
 
         remainingChunksize = chunksize;  /* initial value */
-    
+
         for (row = 0; row < rows; ++row) {
 
             if( HAS_MULTIPALETTE(cmap) )
@@ -1240,9 +1246,9 @@ std_to_ppm(FILE *         const ifP,
 
 
 static void
-deep_to_ppm(FILE *         const ifP, 
-            long           const chunksize, 
-            BitMapHeader * const bmhdP, 
+deep_to_ppm(FILE *         const ifP,
+            long           const chunksize,
+            BitMapHeader * const bmhdP,
             ColorMap *     const cmap) {
 
     unsigned int const cols = bmhdP->w;
@@ -1259,9 +1265,9 @@ deep_to_ppm(FILE *         const ifP,
     if( planespercolor > MAXPLANES )
         pm_error("too many planes (max %d)", MAXPLANES * 3);
 
-    if( bmhdP->masking == mskHasTransparentColor || 
+    if( bmhdP->masking == mskHasTransparentColor ||
         bmhdP->masking == mskLasso ) {
-        pm_message("masking type '%s' in a deep ILBM?? - ignoring it", 
+        pm_message("masking type '%s' in a deep ILBM?? - ignoring it",
                    mskNAME[bmhdP->masking]);
         bmhdP->masking = mskNone;
     }
@@ -1271,7 +1277,7 @@ deep_to_ppm(FILE *         const ifP,
         pm_error("nPlanes is too large");
 
     transpColorP = transpColor(bmhdP, cmap, transpName, maxval);
-        
+
     Rrow = alloc_rawrow(cols);
     Grow = alloc_rawrow(cols);
     Brow = alloc_rawrow(cols);
@@ -1313,7 +1319,7 @@ dcol_to_ppm(FILE *         const ifP,
     unsigned int const redplanes   = dcol->r;
     unsigned int const greenplanes = dcol->g;
     unsigned int const blueplanes  = dcol->b;
-    
+
     int col, row;
     rawtype *Rrow, *Grow, *Brow;
     pixval maxval, redmaxval, greenmaxval, bluemaxval;
@@ -1324,16 +1330,16 @@ dcol_to_ppm(FILE *         const ifP,
     pm_message("input is a %d:%d:%d direct color ILBM",
                 redplanes, greenplanes, blueplanes);
 
-    if( redplanes > MAXPLANES || 
-        blueplanes > MAXPLANES || 
+    if( redplanes > MAXPLANES ||
+        blueplanes > MAXPLANES ||
         greenplanes > MAXPLANES )
         pm_error("too many planes (max %d per color component)", MAXPLANES);
 
     if( bmhdP->nPlanes != (redplanes + greenplanes + blueplanes) )
-        pm_error("%s/%s plane number mismatch", 
+        pm_error("%s/%s plane number mismatch",
                  ID2string(ID_BMHD), ID2string(ID_DCOL));
 
-    if( bmhdP->masking == mskHasTransparentColor || 
+    if( bmhdP->masking == mskHasTransparentColor ||
         bmhdP->masking == mskLasso ) {
         pm_message("masking type '%s' in a direct color ILBM?? - ignoring it",
                    mskNAME[bmhdP->masking]);
@@ -1356,7 +1362,7 @@ dcol_to_ppm(FILE *         const ifP,
 
     if( redmaxval != maxval || greenmaxval != maxval || bluemaxval != maxval )
         pm_message("scaling colors to %d bits", pm_maxvaltobits(maxval));
-    
+
     MALLOCARRAY_NOFAIL(redtable,   redmaxval   +1);
     MALLOCARRAY_NOFAIL(greentable, greenmaxval +1);
     MALLOCARRAY_NOFAIL(bluetable,  bluemaxval  +1);
@@ -1403,7 +1409,7 @@ dcol_to_ppm(FILE *         const ifP,
 
 static void
 cmapToPpm(FILE *     const ofP,
-            ColorMap * const cmapP) {
+          ColorMap * const cmapP) {
 
     ppm_colorrowtomapfile(ofP, cmapP->color, cmapP->ncolors, MAXCOLVAL);
 }
@@ -1425,11 +1431,11 @@ ipbm_to_ppm(FILE *         const ifP,
     unsigned long remainingChunksize;
     pixel * transpColorP;
 
-    pm_message("input is a %sPBM ", 
+    pm_message("input is a %sPBM ",
                HAS_MULTIPALETTE(cmap) ? "multipalette " : "");
 
     if( bmhdP->nPlanes != 8 )
-        pm_error("invalid number of planes for IFF-PBM: %d (must be 8)", 
+        pm_error("invalid number of planes for IFF-PBM: %d (must be 8)",
                  bmhdP->nPlanes);
 
     if( bmhdP->masking == mskHasMask )
@@ -1456,7 +1462,7 @@ ipbm_to_ppm(FILE *         const ifP,
     for( row = 0; row < rows; row++ ) {
         if( HAS_MULTIPALETTE(cmap) )
             multi_update(cmap, row);
-        
+
         read_ilbm_plane(ifP, &remainingChunksize, cols, bmhdP->compression);
 
         for( col = 0; col < cols; col++ ) {
@@ -1494,24 +1500,24 @@ rgbn_to_ppm(FILE *         const ifP,
     pm_message("input is a %d-bit RGB image", (typeid == ID_RGB8 ? 8 : 4));
 
     if (bmhdP->compression != 4)
-        pm_error("invalid compression mode for %s: %d (must be 4)", 
+        pm_error("invalid compression mode for %s: %d (must be 4)",
                  ID2string(typeid), bmhdP->compression);
-    
+
     switch (typeid) {
     case ID_RGBN:
         if (bmhdP->nPlanes != 13)
-            pm_error("invalid number of planes for %s: %d (must be 13)", 
+            pm_error("invalid number of planes for %s: %d (must be 13)",
                      ID2string(typeid), bmhdP->nPlanes);
         maxval = lut_maxval(cmap, 15);
         break;
     case ID_RGB8:
         if (bmhdP->nPlanes != 25)
-            pm_error("invalid number of planes for %s: %d (must be 25)", 
+            pm_error("invalid number of planes for %s: %d (must be 25)",
                      ID2string(typeid), bmhdP->nPlanes);
         maxval = 255;
         break;
     default:
-        pm_error("rgbn_to_ppm(): invalid IFF ID %s - can't happen", 
+        pm_error("rgbn_to_ppm(): invalid IFF ID %s - can't happen",
                  ID2string(typeid));
     }
 
@@ -1533,7 +1539,7 @@ rgbn_to_ppm(FILE *         const ifP,
             tries = 0;
             while (count == 0) {
                 if (typeid == ID_RGB8) {
-                    r = lookup_red(cmap,   get_byte(ifP, ID_BODY, 
+                    r = lookup_red(cmap,   get_byte(ifP, ID_BODY,
                                                     &remainingChunksize));
                     g = lookup_green(cmap, get_byte(ifP, ID_BODY,
                                                     &remainingChunksize));
@@ -1675,40 +1681,46 @@ read_4bit_mp(FILE *     const ifP,
 
 
 static void
-PCHG_DecompHuff(src, dest, tree, origsize)
-    unsigned char *src, *dest;
-    short *tree;
-    unsigned long origsize;
-{
-    unsigned long i = 0, bits = 0;
+PCHG_DecompHuff(const unsigned char * const src,
+                unsigned char *       const dst,
+                short *               const tree,
+                unsigned long         const origsize) {
+
+    const unsigned char * srcCursor;
+    unsigned char * dstCursor;
+    unsigned long i;
+    unsigned long bits;
     unsigned char thisbyte;
-    short *p;
+    short * p;
 
-    p = tree;
-    while( i < origsize ) {
-        if( bits == 0 ) {
-            thisbyte = *src++;
+    srcCursor = &src[0];  /* initial value */
+    dstCursor = &dst[0];  /* initial value */
+    i = 0;     /* initial value */
+    bits = 0;  /* initial value */
+    p = tree;  /* initial value */
+
+    while (i < origsize) {
+        if (bits == 0)  {
+            thisbyte = *srcCursor++;
             bits = 8;
         }
-        if( thisbyte & (1 << 7) ) {
-            if( *p >= 0 ) {
-                *dest++ = (unsigned char)*p;
-                i++;
+        if (thisbyte & (1 << 7)) {
+            if (*p >= 0) {
+                *dstCursor++ = (unsigned char)*p;
+                ++i;
                 p = tree;
-            }
-            else
+            } else
                 p += (*p / 2);
-        }
-        else {
-            p--;
-            if( *p > 0 && (*p & 0x100) ) {
-                *dest++ = (unsigned char )*p;
-                i++;
+        } else {
+            --p;
+            if (*p > 0 && (*p & 0x100)) {
+                *dstCursor++ = (unsigned char )*p;
+                ++i;
                 p = tree;
             }
         }
         thisbyte <<= 1;
-        bits--;
+        --bits;
     }
 }
 
@@ -1743,9 +1755,9 @@ PCHG_Decompress(PCHGHeader *     const PCHG,
             hufftree[i] = pm_uintFromBigend16(bigendComptree[i]);
 
         /* decompress the change structure data */
-        PCHG_DecompHuff(compdata, data, &hufftree[huffsize-1], 
+        PCHG_DecompHuff(compdata, data, &hufftree[huffsize-1],
                         CompHdr->OriginalDataSize);
-        
+
         free(hufftree);
     } break;
         default:
@@ -1755,99 +1767,103 @@ PCHG_Decompress(PCHGHeader *     const PCHG,
 
 
 static void
-PCHG_ConvertSmall(PCHG, cmap, mask, datasize)
-    PCHGHeader *PCHG;
-    ColorMap *cmap;
-    unsigned char *mask;
-    unsigned long datasize;
-{
+PCHG_ConvertSmall(PCHGHeader *    const pchgP,
+                  ColorMap *      const cmapP,
+                  unsigned char * const mask,
+                  unsigned long   const dataSize) {
+
     unsigned char *data;
+    unsigned long remDataSize;
     unsigned char thismask;
     int bits, row, i, changes, masklen, reg;
     unsigned char ChangeCount16, ChangeCount32;
     unsigned short SmallChange;
-    unsigned long totalchanges = 0;
-    int changedlines = PCHG->ChangedLines;
+    unsigned long totalchanges;
+    int changedlines;
+    unsigned char * maskCursor;
 
-    masklen = 4 * MaskLongWords(PCHG->LineCount);
-    data = mask + masklen; datasize -= masklen;
+    totalchanges = 0;  /* initial value */
+    changedlines = pchgP->ChangedLines;  /* initial value */
+    masklen = 4 * MaskLongWords(pchgP->LineCount);
+    maskCursor = mask;
+    data = maskCursor + masklen; remDataSize = dataSize - masklen;
 
     bits = 0;
-    for( row = PCHG->StartLine; changedlines && row < 0; row++ ) {
-        if( bits == 0 ) {
-            if( masklen == 0 ) goto fail2;
-            thismask = *mask++;
+    for (row = pchgP->StartLine; changedlines && row < 0; ++row) {
+        if (bits == 0) {
+            if (masklen == 0) goto fail2;
+            thismask = *maskCursor++;
             --masklen;
             bits = 8;
         }
-        if( thismask & (1<<7) ) {
-            if( datasize < 2 ) goto fail;
+        if (thismask & (1<<7)) {
+            if (remDataSize < 2) goto fail;
             ChangeCount16 = *data++;
             ChangeCount32 = *data++;
-            datasize -= 2;
+            remDataSize -= 2;
 
             changes = ChangeCount16 + ChangeCount32;
-            for( i = 0; i < changes; i++ ) {
-                if( totalchanges >= PCHG->TotalChanges ) goto fail;
-                if( datasize < 2 ) goto fail;
-                SmallChange = BIG_WORD(data); data += 2; datasize -= 2;
-                reg = ((SmallChange & 0xf000) >> 12) + 
+            for (i = 0; i < changes; ++i) {
+                if (totalchanges >= pchgP->TotalChanges) goto fail;
+                if (remDataSize < 2) goto fail;
+                SmallChange = BIG_WORD(data); data += 2; remDataSize -= 2;
+                reg = ((SmallChange & 0xf000) >> 12) +
                     (i >= ChangeCount16 ? 16 : 0);
-                cmap->mp_init[reg - PCHG->MinReg].reg = reg;
-                cmap->mp_init[reg - PCHG->MinReg].r = 
+                cmapP->mp_init[reg - pchgP->MinReg].reg = reg;
+                cmapP->mp_init[reg - pchgP->MinReg].r =
                     ((SmallChange & 0x0f00) >> 8) * FACTOR_4BIT;
-                cmap->mp_init[reg - PCHG->MinReg].g = 
+                cmapP->mp_init[reg - pchgP->MinReg].g =
                     ((SmallChange & 0x00f0) >> 4) * FACTOR_4BIT;
-                cmap->mp_init[reg - PCHG->MinReg].b = 
+                cmapP->mp_init[reg - pchgP->MinReg].b =
                     ((SmallChange & 0x000f) >> 0) * FACTOR_4BIT;
                 ++totalchanges;
             }
             --changedlines;
         }
         thismask <<= 1;
-        bits--;
+        --bits;
     }
 
-    for( row = PCHG->StartLine; changedlines && row < cmap->mp_rows; row++ ) {
-        if( bits == 0 ) {
-            if( masklen == 0 ) goto fail2;
-            thismask = *mask++;
+    for (row = pchgP->StartLine; changedlines && row < cmapP->mp_rows; row++) {
+        if (bits == 0) {
+            if (masklen == 0) goto fail2;
+            thismask = *maskCursor++;
             --masklen;
             bits = 8;
         }
-        if( thismask & (1<<7) ) {
-            if( datasize < 2 ) goto fail;
+        if(thismask & (1<<7)) {
+            if (remDataSize < 2) goto fail;
             ChangeCount16 = *data++;
             ChangeCount32 = *data++;
-            datasize -= 2;
+            remDataSize -= 2;
 
             changes = ChangeCount16 + ChangeCount32;
-            MALLOCARRAY_NOFAIL(cmap->mp_change[row], changes + 1);
-            for( i = 0; i < changes; i++ ) {
-                if( totalchanges >= PCHG->TotalChanges ) goto fail;
-                if( datasize < 2 ) goto fail;
-                SmallChange = BIG_WORD(data); data += 2; datasize -= 2;
-                reg = ((SmallChange & 0xf000) >> 12) + 
+            MALLOCARRAY_NOFAIL(cmapP->mp_change[row], changes + 1);
+            for (i = 0; i < changes; ++i) {
+                if (totalchanges >= pchgP->TotalChanges) goto fail;
+                if (remDataSize < 2) goto fail;
+                SmallChange = BIG_WORD(data); data += 2; remDataSize -= 2;
+                reg = ((SmallChange & 0xf000) >> 12) +
                     (i >= ChangeCount16 ? 16 : 0);
-                cmap->mp_change[row][i].reg = reg;
-                cmap->mp_change[row][i].r = 
+                cmapP->mp_change[row][i].reg = reg;
+                cmapP->mp_change[row][i].r =
                     ((SmallChange & 0x0f00) >> 8) * FACTOR_4BIT;
-                cmap->mp_change[row][i].g = 
+                cmapP->mp_change[row][i].g =
                     ((SmallChange & 0x00f0) >> 4) * FACTOR_4BIT;
-                cmap->mp_change[row][i].b = 
+                cmapP->mp_change[row][i].b =
                     ((SmallChange & 0x000f) >> 0) * FACTOR_4BIT;
                 ++totalchanges;
             }
-            cmap->mp_change[row][changes].reg = MP_REG_END;
+            cmapP->mp_change[row][changes].reg = MP_REG_END;
             --changedlines;
         }
         thismask <<= 1;
-        bits--;
+        --bits;
     }
-    if( totalchanges != PCHG->TotalChanges )
+    if (totalchanges != pchgP->TotalChanges)
         pm_message("warning - got %ld change structures, "
-                   "chunk header reports %ld", 
-                   totalchanges, PCHG->TotalChanges);
+                   "chunk header reports %ld",
+                   totalchanges, pchgP->TotalChanges);
     return;
 fail:
     pm_error("insufficient data in SmallLineChanges structures");
@@ -1872,16 +1888,16 @@ PCHG_ConvertBig(PCHGHeader *    const PCHG,
     int reg;
     unsigned long totalchanges;
     int changedlines;
-    unsigned long dataRemaining;
+    unsigned long remDataSize;
     unsigned char * mask;
 
     mask = maskStart;  /* initial value */
-    dataRemaining = datasize;  /* initial value */
+    remDataSize = datasize;  /* initial value */
     changedlines = PCHG->ChangedLines;  /* initial value */
     totalchanges = 0;  /* initial value */
 
     masklen = 4 * MaskLongWords(PCHG->LineCount);
-    data = mask + masklen; dataRemaining -= masklen;
+    data = mask + masklen; remDataSize -= masklen;
 
     for (row = PCHG->StartLine, bits = 0; changedlines && row < 0; ++row) {
         if (bits == 0) {
@@ -1894,16 +1910,16 @@ PCHG_ConvertBig(PCHGHeader *    const PCHG,
         if (thismask & (1<<7)) {
             unsigned int i;
 
-            if (dataRemaining < 2)
+            if (remDataSize < 2)
                 pm_error("insufficient data in BigLineChanges structures");
 
-            changes = BIG_WORD(data); data += 2; dataRemaining -= 2;
+            changes = BIG_WORD(data); data += 2; remDataSize -= 2;
 
             for (i = 0; i < changes; ++i) {
                 if (totalchanges >= PCHG->TotalChanges)
                     pm_error("insufficient data in BigLineChanges structures");
 
-                if (dataRemaining < 6)
+                if (remDataSize < 6)
                     pm_error("insufficient data in BigLineChanges structures");
 
                 reg = BIG_WORD(data); data += 2;
@@ -1912,7 +1928,7 @@ PCHG_ConvertBig(PCHGHeader *    const PCHG,
                 cmap->mp_init[reg - PCHG->MinReg].r = *data++;
                 cmap->mp_init[reg - PCHG->MinReg].b = *data++;  /* yes, RBG */
                 cmap->mp_init[reg - PCHG->MinReg].g = *data++;
-                dataRemaining -= 6;
+                remDataSize -= 6;
                 ++totalchanges;
             }
             --changedlines;
@@ -1933,17 +1949,17 @@ PCHG_ConvertBig(PCHGHeader *    const PCHG,
         if (thismask & (1<<7)) {
             unsigned int i;
 
-            if (dataRemaining < 2)
+            if (remDataSize < 2)
                 pm_error("insufficient data in BigLineChanges structures");
 
-            changes = BIG_WORD(data); data += 2; dataRemaining -= 2;
+            changes = BIG_WORD(data); data += 2; remDataSize -= 2;
 
             MALLOCARRAY_NOFAIL(cmap->mp_change[row], changes + 1);
             for (i = 0; i < changes; ++i) {
                 if (totalchanges >= PCHG->TotalChanges)
                     pm_error("insufficient data in BigLineChanges structures");
 
-                if (dataRemaining < 6)
+                if (remDataSize < 6)
                     pm_error("insufficient data in BigLineChanges structures");
 
                 reg = BIG_WORD(data); data += 2;
@@ -1952,7 +1968,7 @@ PCHG_ConvertBig(PCHGHeader *    const PCHG,
                 cmap->mp_change[row][i].r = *data++;
                 cmap->mp_change[row][i].b = *data++;    /* yes, RBG */
                 cmap->mp_change[row][i].g = *data++;
-                dataRemaining -= 6;
+                remDataSize -= 6;
                 ++totalchanges;
             }
             cmap->mp_change[row][changes].reg = MP_REG_END;
@@ -1963,7 +1979,7 @@ PCHG_ConvertBig(PCHGHeader *    const PCHG,
     }
     if (totalchanges != PCHG->TotalChanges)
         pm_message("warning - got %ld change structures, "
-                   "chunk header reports %ld", 
+                   "chunk header reports %ld",
                    totalchanges, PCHG->TotalChanges);
 }
 
@@ -2027,7 +2043,7 @@ read_pchg(FILE *     const ifP,
 
             datasize = CompHdr.OriginalDataSize;
             MALLOCARRAY_NOFAIL(data, datasize);
-            PCHG_Decompress(&PCHG, &CompHdr, compdata, 
+            PCHG_Decompress(&PCHG, &CompHdr, compdata,
                             compsize, comptree, data);
 
             free(comptree);
@@ -2073,7 +2089,7 @@ read_pchg(FILE *     const ifP,
             }
             else
                 pm_error("unknown palette changes structure "
-                         "format in %s chunk", 
+                         "format in %s chunk",
                          ID2string(iffid));
         }
         free(data);
@@ -2101,7 +2117,7 @@ ignored_iffid(IFF_ID       const iffid,
 
 
 
-static void 
+static void
 process_body( FILE *          const ifP,
               long            const chunksize,
               BitMapHeader *  const bmhdP,
@@ -2111,9 +2127,9 @@ process_body( FILE *          const ifP,
               int             const isdeepopt,
               DirectColor *   const dcol,
               int *           const viewportmodesP) {
-    
+
     if (bmhdP == NULL)
-        pm_error("%s chunk without %s chunk", 
+        pm_error("%s chunk without %s chunk",
                  ID2string(ID_BODY), ID2string(ID_BMHD));
 
     prepareCmap(bmhdP, cmap);
@@ -2132,12 +2148,12 @@ process_body( FILE *          const ifP,
 
         if( isdeepopt > 0 && (bmhdP->nPlanes % 3 != 0) ) {
             pm_message("cannot interpret %d-plane image as 'deep' "
-                       "(# of planes must be divisible by 3)", 
+                       "(# of planes must be divisible by 3)",
                        bmhdP->nPlanes);
             isdeep = 0;
         } else
             isdeep = isdeepopt;
-        
+
         if (isdeep > 0)
             deep_to_ppm(ifP, chunksize, bmhdP, cmap);
         else if (dcol)
@@ -2151,7 +2167,7 @@ process_body( FILE *          const ifP,
                 /* will be interpreted as grayscale */
                 std_to_ppm(ifP, chunksize, bmhdP, cmap, *viewportmodesP);
             else
-                pm_error("don't know how to interpret %d-plane image", 
+                pm_error("don't know how to interpret %d-plane image",
                          bmhdP->nPlanes);
         } else
             std_to_ppm(ifP, chunksize, bmhdP, cmap, *viewportmodesP);
@@ -2163,7 +2179,7 @@ process_body( FILE *          const ifP,
 
 
 
-static void 
+static void
 processChunk(FILE *          const ifP,
              long            const formsize,
              IFF_ID          const ignorelist[],
@@ -2200,7 +2216,7 @@ processChunk(FILE *          const ifP,
     } else if (iffid == ID_END) {
         /* END chunks are not officially valid in IFF, but
            suggested as a future expansion for stream-writing,
-           see Amiga RKM Devices, 3rd Ed, page 376 
+           see Amiga RKM Devices, 3rd Ed, page 376
         */
         if (chunksize != 0 ) {
             pm_message("warning - non-0 %s chunk", ID2string(iffid));
@@ -2212,7 +2228,7 @@ processChunk(FILE *          const ifP,
                        ID2string(iffid), formsize, 0xffffffff);
         *endchunkP = 1;
     } else if (*bodyChunkProcessedP) {
-        pm_message("%s chunk found after %s chunk - skipping", 
+        pm_message("%s chunk found after %s chunk - skipping",
                    ID2string(iffid), ID2string(ID_BODY));
         skip_chunk(ifP, iffid, chunksize);
     } else
@@ -2251,11 +2267,11 @@ processChunk(FILE *          const ifP,
             (*dcolP)->b = get_byte(ifP, iffid, NULL);
             get_byte(ifP, iffid, NULL);       /* skip pad byte */
             break;
-        case ID_BODY: 
+        case ID_BODY:
             if (cmaponly || (*bmhdP && (*bmhdP)->nPlanes == 0))
                 skip_chunk(ifP, ID_BODY,  chunksize);
             else {
-                process_body(ifP, chunksize, *bmhdP, cmap, 
+                process_body(ifP, chunksize, *bmhdP, cmap,
                              maskfile, fakeviewport, isdeepopt, *dcolP,
                              viewportmodesP);
 
@@ -2280,11 +2296,11 @@ processChunk(FILE *          const ifP,
             x = get_big_short(ifP, ID_DPI, NULL);
             y = get_big_short(ifP, ID_DPI, NULL);
             if (verbose)
-                pm_message("%s chunk:  dpi_x = %d    dpi_y = %d", 
+                pm_message("%s chunk:  dpi_x = %d    dpi_y = %d",
                            ID2string(ID_DPI), x, y);
         } break;
         default:
-            pm_message("unknown chunk type %s - skipping", 
+            pm_message("unknown chunk type %s - skipping",
                        ID2string(iffid));
             skip_chunk(ifP, iffid, chunksize);
             break;
@@ -2295,7 +2311,7 @@ processChunk(FILE *          const ifP,
     if (ODD(chunksize)) {
         get_byte(ifP, iffid, NULL);
         bytesread += 1;
-    } 
+    }
     *bytesReadP = bytesread;
 }
 
@@ -2338,7 +2354,7 @@ maybeWriteColorMap(FILE *               const ofP,
             prepareCmap(bmhdP, cmapP);
             cmapToPpm(ofP, cmapP);
         } else
-            pm_error("ILBM has neither %s or %s chunk", 
+            pm_error("ILBM has neither %s or %s chunk",
                      ID2string(ID_BODY), ID2string(ID_CMAP));
     }
 }
@@ -2413,13 +2429,13 @@ main(int argc, char *argv[]) {
                          "as argument");
             if( ignorecount >= MAX_IGNORE )
                 pm_error("max %d chunk IDs to ignore", MAX_IGNORE);
-            ignorelist[ignorecount++] = 
-                MAKE_ID(argv[argn][0], argv[argn][1], argv[argn][2], 
+            ignorelist[ignorecount++] =
+                MAKE_ID(argv[argn][0], argv[argn][1], argv[argn][2],
                         argv[argn][3]);
         } else
             pm_usage(usage);
         ++argn;
-    }    
+    }
 
     if( argn < argc ) {
         ifP = pm_openr( argv[argn] );
@@ -2439,7 +2455,7 @@ main(int argc, char *argv[]) {
         pm_error("input is not a FORM type IFF file");
     formsize = get_big_long(ifP, ID_FORM, NULL);
     typeid = get_big_long(ifP, ID_FORM, NULL);
-    if (typeid != ID_ILBM && typeid != ID_RGBN && typeid != ID_RGB8 && 
+    if (typeid != ID_ILBM && typeid != ID_RGBN && typeid != ID_RGB8 &&
         typeid != ID_PBM)
         pm_error("input is not an ILBM, RGBN, RGB8 or PBM "
                  "type FORM IFF file");
@@ -2474,7 +2490,7 @@ main(int argc, char *argv[]) {
 
     {
         unsigned int skipped;
-        
+
         for (skipped = 0; fgetc(ifP) != EOF; ++skipped)
             ++bytesread;
 
@@ -2492,3 +2508,6 @@ main(int argc, char *argv[]) {
 
     return 0;
 }
+
+
+
diff --git a/converter/ppm/picttoppm.c b/converter/ppm/picttoppm.c
index b14675c7..b8fb8642 100644
--- a/converter/ppm/picttoppm.c
+++ b/converter/ppm/picttoppm.c
@@ -15,7 +15,7 @@
  * University of British Columbia
  *
  *
- * 2003-02:    Handling for DirectBitsRgn opcode (0x9b) added by 
+ * 2003-02:    Handling for DirectBitsRgn opcode (0x9b) added by
  *             kabe@sra-tohoku.co.jp.
  *
  * 2004-03-27: Several bugs fixed by Steve Summit, scs@eskimo.com.
@@ -153,7 +153,7 @@ struct raster {
 
    Each pixel is either a palette index or an RGB triple, depending on
    the format of the associated PICT.
-   
+
    Each pixel is one byte if the associated PICT has 8 or fewer bits
    per pixel.  If the associated PICT has 16 or 32 bits per pixel, an
    element herein is 2 or 4 bytes, respectively.
@@ -231,7 +231,7 @@ allocateRaster(struct raster * const rasterP,
     default:
         pm_error("INTERNAL ERROR: impossible bitsPerPixel value in "
                  "unpackbits(): %u", bitsPerPixel);
-    }    
+    }
     if (UINT_MAX / rasterP->rowSize < rasterP->rowCount)
         pm_error("Arithmetic overflow computing size of %u x %u pixel "
                  "array.", rasterP->rowSize, rasterP->rowCount);
@@ -360,7 +360,7 @@ readSignedByte(void) {
 
 
 
-static void 
+static void
 readShortPoint(struct Point * const p) {
     p->x = readSignedByte();
     p->y = readSignedByte();
@@ -457,15 +457,15 @@ const_name(const struct const_name * const table,
     for (i = 0; table[i].name; ++i)
         if (table[i].value == ct)
             return table[i].name;
-    
+
     sprintf(numbuf, "? (%u)", ct);
     return numbuf;
 }
 
 
 
-static void 
-picComment(Word const type, 
+static void
+picComment(Word const type,
            int const length) {
 
     unsigned int remainingLength;
@@ -736,7 +736,7 @@ parseFontLine(const char **      const token,
 
 
 
-static int 
+static int
 load_fontdir(const char * const dirfile) {
 /*----------------------------------------------------------------------------
    Load the font directory from file named 'dirfile'.  Add its contents
@@ -744,7 +744,7 @@ load_fontdir(const char * const dirfile) {
 -----------------------------------------------------------------------------*/
     FILE * ifP;
     unsigned int nFont;
-    char line[1024]; 
+    char line[1024];
 
     ifP = pm_openr(dirfile);
 
@@ -832,7 +832,7 @@ rectheight(const struct Rect * const r) {
 
 
 static bool
-rectsamesize(struct Rect const r1, 
+rectsamesize(struct Rect const r1,
              struct Rect const r2) {
     return r1.right - r1.left == r2.right - r2.left &&
            r1.bottom - r1.top == r2.bottom - r2.top ;
@@ -841,8 +841,8 @@ rectsamesize(struct Rect const r1,
 
 
 static void
-rectinter(struct Rect   const r1, 
-          struct Rect   const r2, 
+rectinter(struct Rect   const r1,
+          struct Rect   const r2,
           struct Rect * const intersectionP) {
 
     intersectionP->left   = MAX(r1.left,   r2.left);
@@ -854,8 +854,8 @@ rectinter(struct Rect   const r1,
 
 
 static void
-rectscale(struct Rect * const r, 
-          double        const xscale, 
+rectscale(struct Rect * const r,
+          double        const xscale,
           double        const yscale) {
     r->left *= xscale;
     r->right *= xscale;
@@ -886,7 +886,7 @@ addBlitList(blitList *        const blitListP,
             int               const mode) {
 
     struct blit_info * biP;
-    
+
     MALLOCVAR(biP);
     if (biP == NULL)
         pm_error("out of memory for blit list");
@@ -940,8 +940,8 @@ rgbIsBlack(const struct RGBColor * const colorP) {
 }
 
 
-static void 
-srcCopy(struct RGBColor * const src, 
+static void
+srcCopy(struct RGBColor * const src,
         struct RGBColor * const dst) {
 
     if (rgbIsBlack(src))
@@ -952,8 +952,8 @@ srcCopy(struct RGBColor * const src,
 
 
 
-static void 
-srcOr(struct RGBColor * const src, 
+static void
+srcOr(struct RGBColor * const src,
       struct RGBColor * const dst) {
     if (rgbIsBlack(src))
         *dst = foreground;
@@ -961,8 +961,8 @@ srcOr(struct RGBColor * const src,
 
 
 
-static void 
-srcXor(struct RGBColor * const src, 
+static void
+srcXor(struct RGBColor * const src,
        struct RGBColor * const dst) {
     dst->red ^= ~src->red;
     dst->grn ^= ~src->grn;
@@ -971,8 +971,8 @@ srcXor(struct RGBColor * const src,
 
 
 
-static void 
-srcBic(struct RGBColor * const src, 
+static void
+srcBic(struct RGBColor * const src,
        struct RGBColor * const dst) {
     if (rgbIsBlack(src))
         *dst = background;
@@ -980,8 +980,8 @@ srcBic(struct RGBColor * const src,
 
 
 
-static void 
-notSrcCopy(struct RGBColor * const src, 
+static void
+notSrcCopy(struct RGBColor * const src,
            struct RGBColor * const dst) {
     if (rgbIsWhite(src))
         *dst = foreground;
@@ -991,8 +991,8 @@ notSrcCopy(struct RGBColor * const src,
 
 
 
-static void 
-notSrcOr(struct RGBColor * const src, 
+static void
+notSrcOr(struct RGBColor * const src,
          struct RGBColor * const dst) {
     if (rgbIsWhite(src))
         *dst = foreground;
@@ -1000,8 +1000,8 @@ notSrcOr(struct RGBColor * const src,
 
 
 
-static void 
-notSrcBic(struct RGBColor * const src, 
+static void
+notSrcBic(struct RGBColor * const src,
           struct RGBColor * const dst) {
     if (rgbIsWhite(src))
         *dst = background;
@@ -1009,8 +1009,8 @@ notSrcBic(struct RGBColor * const src,
 
 
 
-static void 
-notSrcXor(struct RGBColor * const src, 
+static void
+notSrcXor(struct RGBColor * const src,
           struct RGBColor * const dst) {
     dst->red ^= src->red;
     dst->grn ^= src->grn;
@@ -1019,8 +1019,8 @@ notSrcXor(struct RGBColor * const src,
 
 
 
-static void 
-addOver(struct RGBColor * const src, 
+static void
+addOver(struct RGBColor * const src,
         struct RGBColor * const dst) {
     dst->red += src->red;
     dst->grn += src->grn;
@@ -1029,8 +1029,8 @@ addOver(struct RGBColor * const src,
 
 
 
-static void 
-addPin(struct RGBColor * const src, 
+static void
+addPin(struct RGBColor * const src,
        struct RGBColor * const dst) {
     if ((long)dst->red + (long)src->red > (long)op_color.red)
         dst->red = op_color.red;
@@ -1050,8 +1050,8 @@ addPin(struct RGBColor * const src,
 
 
 
-static void 
-subOver(struct RGBColor * const src, 
+static void
+subOver(struct RGBColor * const src,
         struct RGBColor * const dst) {
     dst->red -= src->red;
     dst->grn -= src->grn;
@@ -1063,8 +1063,8 @@ subOver(struct RGBColor * const src,
 /* or maybe its src - dst; my copy of Inside Mac is unclear */
 
 
-static void 
-subPin(struct RGBColor * const src, 
+static void
+subPin(struct RGBColor * const src,
        struct RGBColor * const dst) {
     if ((long)dst->red - (long)src->red < (long)op_color.red)
         dst->red = op_color.red;
@@ -1084,8 +1084,8 @@ subPin(struct RGBColor * const src,
 
 
 
-static void 
-adMax(struct RGBColor * const src, 
+static void
+adMax(struct RGBColor * const src,
       struct RGBColor * const dst) {
     if (src->red > dst->red) dst->red = src->red;
     if (src->grn > dst->grn) dst->grn = src->grn;
@@ -1094,8 +1094,8 @@ adMax(struct RGBColor * const src,
 
 
 
-static void 
-adMin(struct RGBColor * const src, 
+static void
+adMin(struct RGBColor * const src,
       struct RGBColor * const dst) {
     if (src->red < dst->red) dst->red = src->red;
     if (src->grn < dst->grn) dst->grn = src->grn;
@@ -1104,8 +1104,8 @@ adMin(struct RGBColor * const src,
 
 
 
-static void 
-blend(struct RGBColor * const src, 
+static void
+blend(struct RGBColor * const src,
       struct RGBColor * const dst) {
 #define blend_component(cmp)    \
     ((long)src->cmp * (long)op_color.cmp) / 65536 +    \
@@ -1118,8 +1118,8 @@ blend(struct RGBColor * const src,
 
 
 
-static void 
-transparent(struct RGBColor * const src, 
+static void
+transparent(struct RGBColor * const src,
             struct RGBColor * const dst) {
     if (src->red != background.red ||
         src->grn != background.grn ||
@@ -1130,7 +1130,7 @@ transparent(struct RGBColor * const src,
 
 
 
-static transfer_func 
+static transfer_func
 transfer(int const mode) {
     switch (mode) {
     case  0: return srcCopy;
@@ -1162,7 +1162,7 @@ transfer(int const mode) {
 static pixval
 redepth(pixval const c,
         pixval const oldMaxval) {
-    
+
     return ROUNDDIV(c * PPM_MAXMAXVAL, oldMaxval);
 }
 
@@ -1185,7 +1185,7 @@ decode16(unsigned char * const sixteen) {
     retval.red = (sixteen[0] & 0x7c) >> 2;
     retval.grn = (sixteen[0] & 0x03) << 3 | (sixteen[1] & 0xe0) >> 5;
     retval.blu = (sixteen[1] & 0x1f) >> 0;
-                
+
     return retval;
 }
 
@@ -1198,9 +1198,9 @@ doDiffSize(struct Rect       const clipsrc,
            int               const xsize,
            int               const ysize,
            transfer_func     const trf,
-           struct RGBColor * const color_map, 
+           struct RGBColor * const color_map,
            unsigned char *   const src,
-           int               const srcwid, 
+           int               const srcwid,
            struct rgbPlanes  const dst,
            unsigned int      const dstwid) {
 
@@ -1275,7 +1275,7 @@ doDiffSize(struct Rect       const clipsrc,
     case 32: {
         unsigned int const planeSize = srcwid / 4;
         unsigned int rowNumber;
-        
+
         for (rowNumber = 0; rowNumber < ysize; ++rowNumber) {
             unsigned char * const row = &src[rowNumber * srcwid];
             unsigned char * const redPlane = &row[planeSize * 0];
@@ -1300,16 +1300,16 @@ doDiffSize(struct Rect       const clipsrc,
     ppm_readppminit(scaled = pm_openr(tempFilename), &cols, &rows,
                     &maxval, &format);
     row = ppm_allocrow(cols);
-    /* couldn't hurt to assert cols, rows and maxval... */  
+    /* couldn't hurt to assert cols, rows and maxval... */
 
     if (trf == NULL) {
         while (rows-- > 0) {
             unsigned int i;
             ppm_readppmrow(scaled, row, cols, maxval, format);
             for (i = 0, rowp = row; i < cols; ++i, ++rowp) {
-                *reddst++ = PPM_GETR(*rowp) * 65536L / (maxval + 1); 
-                *grndst++ = PPM_GETG(*rowp) * 65536L / (maxval + 1); 
-                *bludst++ = PPM_GETB(*rowp) * 65536L / (maxval + 1); 
+                *reddst++ = PPM_GETR(*rowp) * 65536L / (maxval + 1);
+                *grndst++ = PPM_GETG(*rowp) * 65536L / (maxval + 1);
+                *bludst++ = PPM_GETB(*rowp) * 65536L / (maxval + 1);
             }
             reddst += dstadd;
             grndst += dstadd;
@@ -1325,9 +1325,9 @@ doDiffSize(struct Rect       const clipsrc,
                 dst_c.red = *reddst;
                 dst_c.grn = *grndst;
                 dst_c.blu = *bludst;
-                src_c.red = PPM_GETR(*rowp) * 65536L / (maxval + 1); 
-                src_c.grn = PPM_GETG(*rowp) * 65536L / (maxval + 1); 
-                src_c.blu = PPM_GETB(*rowp) * 65536L / (maxval + 1); 
+                src_c.red = PPM_GETR(*rowp) * 65536L / (maxval + 1);
+                src_c.grn = PPM_GETG(*rowp) * 65536L / (maxval + 1);
+                src_c.blu = PPM_GETB(*rowp) * 65536L / (maxval + 1);
                 (*trf)(&src_c, &dst_c);
                 *reddst++ = dst_c.red;
                 *grndst++ = dst_c.grn;
@@ -1480,7 +1480,7 @@ blitIdempotent(unsigned int          const pixSize,
                unsigned int          const ysize,
                unsigned char *       const src,
                unsigned int          const srcwid,
-               struct RGBColor *     const colorMap, 
+               struct RGBColor *     const colorMap,
                struct rgbPlanes      const dst,
                unsigned int          const dstwid) {
 /*----------------------------------------------------------------------------
@@ -1494,7 +1494,7 @@ blitIdempotent(unsigned int          const pixSize,
     switch (pixSize) {
     case 8: {
         unsigned int rowNumber;
-        
+
         for (rowNumber = 0; rowNumber < ysize; ++rowNumber) {
             unsigned char * const srcrow = &src[rowNumber * srcwid];
             unsigned int const dstRowCurs = rowNumber * dstwid;
@@ -1554,15 +1554,15 @@ blitIdempotent(unsigned int          const pixSize,
 
 
 static void
-doBlit(struct Rect       const srcRect, 
-       struct Rect       const dstRect, 
-       struct Rect       const srcBounds, 
+doBlit(struct Rect       const srcRect,
+       struct Rect       const dstRect,
+       struct Rect       const srcBounds,
        struct raster     const srcplane,
-       struct Rect       const dstBounds, 
+       struct Rect       const dstBounds,
        struct rgbPlanes  const canvasPlanes,
-       int               const pixSize, 
-       int               const dstwid, 
-       struct RGBColor * const color_map, 
+       int               const pixSize,
+       int               const dstwid,
+       struct RGBColor * const color_map,
        int               const mode) {
 /*----------------------------------------------------------------------------
    Transfer some pixels from 'srcplane' to 'canvasPlanes', applying the
@@ -1583,7 +1583,6 @@ doBlit(struct Rect       const srcRect,
     int dstoff;
     int xsize;
     int ysize;
-    int srcadd;
     transfer_func trf;
 
     if (verbose) {
@@ -1604,7 +1603,6 @@ doBlit(struct Rect       const srcRect,
         src = srcplane.bytes + srcRowNumber * srcplane.rowSize + srcRowOffset;
         xsize = rectwidth(&srcRect);
         ysize = rectheight(&srcRect);
-        srcadd = srcplane.rowSize - xsize * pkpixsize;
     }
 
     dstoff = (dstRect.top - dstBounds.top) * dstwid +
@@ -1635,16 +1633,16 @@ doBlit(struct Rect       const srcRect,
 
 
 static int
-blit(struct Rect       const srcRect, 
-     struct Rect       const srcBounds, 
+blit(struct Rect       const srcRect,
+     struct Rect       const srcBounds,
      struct raster     const srcplane,
      struct canvas *   const canvasP,
      blitList *        const blitListP,
-     int               const pixSize, 
-     struct Rect       const dstRect, 
-     struct Rect       const dstBounds, 
-     int               const dstwid, 
-     struct RGBColor * const color_map, 
+     int               const pixSize,
+     struct Rect       const dstRect,
+     struct Rect       const dstBounds,
+     int               const dstwid,
+     struct RGBColor * const color_map,
      int               const mode) {
 /*----------------------------------------------------------------------------
    'srcplane' contains the rectangle 'srcBounds' of the image.
@@ -1657,7 +1655,7 @@ blit(struct Rect       const srcRect,
 
     /* I can't tell what the result value of this function is supposed to mean,
        but I found several return statements that did not set it to anything,
-       and several calls that examine it.  I'm guessing that "1" is the 
+       and several calls that examine it.  I'm guessing that "1" is the
        appropriate thing to return in those cases, so I made it so.
        -Bryan 00.03.02
     */
@@ -1701,7 +1699,7 @@ blit(struct Rect       const srcRect,
  * even only a bit, but that would require even more extra work).
  */
 
-static void 
+static void
 allocPlanes(unsigned int       const width,
             unsigned int       const height,
             struct rgbPlanes * const planesP) {
@@ -1858,7 +1856,7 @@ doBlitList(struct canvas * const canvasP,
 
     if (xscale != 1.0 || yscale != 1.0) {
         struct blit_info * biP;
-        
+
         for (biP = blitListP->firstP; biP; biP = biP->next)
             rectscale(&biP->dstRect, xscale, yscale);
 
@@ -2078,7 +2076,7 @@ readColorTable(void) {
 
 static void
 readBytes(FILE *          const ifP,
-          unsigned int    const n, 
+          unsigned int    const n,
           unsigned char * const buf) {
 
     align += n;
@@ -2111,7 +2109,7 @@ expand4Bits(unsigned char * const packed,
     unsigned char * dst;
 
     dst = &expanded[0];
-    
+
     for (i = 0; i < packedLen; ++i) {
         *dst++ = (packed[i] >> 4) & 0x0f;
         *dst++ = (packed[i] >> 0) & 0x0f;
@@ -2124,12 +2122,12 @@ static void
 expand2Bits(unsigned char * const packed,
             unsigned char * const expanded,
             unsigned int    const packedLen) {
-        
+
     unsigned int i;
     unsigned char * dst;
 
     dst = &expanded[0];
-    
+
     for (i = 0; i < packedLen; ++i) {
         *dst++ = (packed[i] >> 6) & 0x03;
         *dst++ = (packed[i] >> 4) & 0x03;
@@ -2149,7 +2147,7 @@ expand1Bit(unsigned char * const packed,
     unsigned char * dst;
 
     dst = &expanded[0];
-    
+
     for (i = 0; i < packedLen; ++i) {
         *dst++ = (packed[i] >> 7) & 0x01;
         *dst++ = (packed[i] >> 6) & 0x01;
@@ -2165,7 +2163,7 @@ expand1Bit(unsigned char * const packed,
 
 
 static void
-unpackBuf(unsigned char *  const packed, 
+unpackBuf(unsigned char *  const packed,
           unsigned int     const packedLen,
           int              const bitsPerPixel,
           unsigned char ** const expandedP,
@@ -2186,14 +2184,9 @@ unpackBuf(unsigned char *  const packed,
    'packedLen' must not be greater than 256.
 -----------------------------------------------------------------------------*/
     static unsigned char expanded[256 * 8];
-    unsigned char * src;
-    unsigned char * dst;
 
     assert(packedLen <= 256);
 
-    src = &packed[0];
-    dst = &expanded[0];
-
     switch (bitsPerPixel) {
     case 8:
     case 16:
@@ -2247,7 +2240,7 @@ unpackUncompressedBits(FILE *          const ifP,
     if (linebuf == NULL)
         pm_error("can't allocate memory for line buffer");
 
-    for (rowOfRect = 0; rowOfRect < raster.rowCount; ++rowOfRect) { 
+    for (rowOfRect = 0; rowOfRect < raster.rowCount; ++rowOfRect) {
         unsigned char * bytePixels;
         unsigned int expandedByteCount;
         unsigned char * rasterRow;
@@ -2331,12 +2324,12 @@ expandRun(unsigned char * const block,
                  "Invalid PICT file.");
     else {
         unsigned int const runLength = (block[0] ^ 0xff) + 2;
-        
+
         unsigned int i;
         unsigned char * bytePixels;  /* Points to static storage */
         unsigned int expandedByteCount;
         unsigned int outputCursor;
-        
+
         assert(block[0] & 0x80);  /* It's a run */
 
         if (verbose > 1)
@@ -2356,13 +2349,13 @@ expandRun(unsigned char * const block,
 
            That is what I saw in a test image.
         */
-        
+
         if (expandedByteCount * runLength > expandedSize)
             pm_error("Invalid PICT image.  It contains a row with more pixels "
                      "than the width of the rectangle containing it, "
                      "even padded up to a "
                      "multiple of 16 pixels.  Use -verbose to see details.");
-        
+
         outputCursor = 0;
         for (i = 0; i < runLength; ++i) {
             unsigned int j;
@@ -2414,14 +2407,14 @@ copyPixelGroup(unsigned char * const block,
         unsigned char * bytePixels;  /* Points to static storage */
         unsigned int bytePixelLen;
         unsigned int rasterBytesGenerated;
-        
+
         assert(blockLimit >= 1);  /* block[0] exists */
         assert((block[0] & 0x80) == 0);  /* It's not a run */
-        
+
         if (verbose > 1)
             pm_message("Block: %u explicit packed %u-byte units",
                        groupLen, pkpixsize);
-        
+
         unpackBuf(&block[1], groupLen * pkpixsize, bitsPerPixel,
                   &bytePixels, &bytePixelLen);
 
@@ -2439,7 +2432,7 @@ copyPixelGroup(unsigned char * const block,
 
         for (i = 0; i < rasterBytesGenerated; ++i)
             dest[i] = bytePixels[i];
-        
+
         *blockLengthP = blockLength;
         *rasterBytesGeneratedP = rasterBytesGenerated;
     }
@@ -2506,9 +2499,9 @@ interpretCompressedLine(unsigned char * const linebuf,
 
     for (lineCursor = 0, rasterCursor = 0; lineCursor < linelen; ) {
         unsigned int blockLength, rasterBytesGenerated;
-        
+
         assert(lineCursor <= linelen);
-            
+
         if (verbose > 2)
             pm_message("At Byte %u of line, Column %u of row",
                        lineCursor, rasterCursor);
@@ -2518,7 +2511,7 @@ interpretCompressedLine(unsigned char * const linebuf,
             bitsPerPixel,
             &rowRaster[rasterCursor], rowSize - rasterCursor,
             &blockLength, &rasterBytesGenerated);
-        
+
         lineCursor += blockLength;
         rasterCursor += rasterBytesGenerated;
         assert(rasterCursor <= rowSize);
@@ -2546,7 +2539,7 @@ interpretCompressedLine(unsigned char * const linebuf,
   Of course, neither 200 nor 250 make any logical sense.  In the worst case,
   you can represent 254 pixels of 8 bpp or less in a 255 byte line.
   In the worst case, you can represent 127 16bpp pixels in a 255 byte line.
-  So with 200 being the cutoff, it's actually impossible to represent some 
+  So with 200 being the cutoff, it's actually impossible to represent some
   16 bpp images with 200 pixels per row.
 
   We have not been able to find an official spec for PICT.
@@ -2616,13 +2609,13 @@ unpackCompressedBits(FILE *          const ifP,
 static void
 unpackbits(FILE *           const ifP,
            struct Rect *    const boundsP,
-           Word             const rowBytesArg, 
+           Word             const rowBytesArg,
            int              const bitsPerPixel,
            struct raster *  const rasterP) {
 
     unsigned int const rectHeight = boundsP->bottom - boundsP->top;
     unsigned int const rectWidth  = boundsP->right  - boundsP->left;
-    
+
     struct raster raster;
     unsigned int rowBytes;
 
@@ -2631,7 +2624,7 @@ unpackbits(FILE *           const ifP,
     if (verbose)
         pm_message("rowBytes = %u, bitsPerPixel = %d",
                    rowBytesArg, bitsPerPixel);
-        
+
     allocateRaster(&raster, rectWidth, rectHeight, bitsPerPixel);
 
     rowBytes = rowBytesArg ? rowBytesArg : raster.rowSize;
@@ -2769,7 +2762,7 @@ read8x8Pattern(struct Pattern * const pat) {
 
 static drawFn BkPat;
 
-static void 
+static void
 BkPat(struct canvas * const canvasP,
       blitList *      const blitListP,
       int             const version) {
@@ -2781,7 +2774,7 @@ BkPat(struct canvas * const canvasP,
 
 static drawFn PnPat;
 
-static void 
+static void
 PnPat(struct canvas * const canvasP,
       blitList *      const blitListP,
       int             const version) {
@@ -2793,7 +2786,7 @@ PnPat(struct canvas * const canvasP,
 
 static drawFn FillPat;
 
-static void 
+static void
 FillPat(struct canvas * const canvasP,
         blitList *      const blitListP,
         int             const version) {
@@ -2805,7 +2798,7 @@ FillPat(struct canvas * const canvasP,
 
 static drawFn PnSize;
 
-static void 
+static void
 PnSize(struct canvas * const canvasP,
        blitList *      const blitListP,
        int             const version) {
@@ -2820,7 +2813,7 @@ PnSize(struct canvas * const canvasP,
 
 static drawFn PnSize;
 
-static void 
+static void
 PnMode(struct canvas * const canvasP,
        blitList *      const blitListP,
        int             const version) {
@@ -2832,13 +2825,13 @@ PnMode(struct canvas * const canvasP,
     if (verbose)
         pm_message("pen transfer mode = %s",
             const_name(transfer_name, pen_mode));
-    
+
     pen_trf = transfer(pen_mode);
 }
 
 
 
-static void 
+static void
 readRgb(struct RGBColor * const rgb) {
     rgb->red = readWord();
     rgb->grn = readWord();
@@ -2849,14 +2842,14 @@ readRgb(struct RGBColor * const rgb) {
 
 static drawFn RGBFgCol;
 
-static void 
+static void
 RGBFgCol(struct canvas * const canvasP,
          blitList *      const blitListP,
          int             const version) {
 
     readRgb(&foreground);
     if (verbose)
-        pm_message("foreground now [%d,%d,%d]", 
+        pm_message("foreground now [%d,%d,%d]",
             foreground.red, foreground.grn, foreground.blu);
 }
 
@@ -2864,14 +2857,14 @@ RGBFgCol(struct canvas * const canvasP,
 
 static drawFn RGBBkCol;
 
-static void 
+static void
 RGBBkCol(struct canvas * const canvasP,
          blitList *      const blitListP,
          int             const version) {
 
     readRgb(&background);
     if (verbose)
-        pm_message("background now [%d,%d,%d]", 
+        pm_message("background now [%d,%d,%d]",
             background.red, background.grn, background.blu);
 }
 
@@ -2879,11 +2872,11 @@ RGBBkCol(struct canvas * const canvasP,
 
 #define PIXEL_INDEX(x,y) ((y) - picFrame.top) * rowlen + (x) - picFrame.left
 
-static void 
+static void
 draw_pixel(struct canvas *   const canvasP,
-           int               const x, 
-           int               const y, 
-           struct RGBColor * const clr, 
+           int               const x,
+           int               const y,
+           struct RGBColor * const clr,
            transfer_func           trf) {
 
     if (x < clip_rect.left || x >= clip_rect.right ||
@@ -2896,7 +2889,7 @@ draw_pixel(struct canvas *   const canvasP,
         dst.red = canvasP->planes.red[i];
         dst.grn = canvasP->planes.grn[i];
         dst.blu = canvasP->planes.blu[i];
- 
+
         (*trf)(clr, &dst);
 
         canvasP->planes.red[i] = dst.red;
@@ -2907,7 +2900,7 @@ draw_pixel(struct canvas *   const canvasP,
 
 
 
-static void 
+static void
 draw_pen_rect(struct canvas * const canvasP,
               struct Rect *   const r) {
 
@@ -2918,7 +2911,7 @@ draw_pen_rect(struct canvas * const canvasP,
     struct RGBColor dst;
 
     i = PIXEL_INDEX(r->left, r->top);  /* initial value */
-    
+
     for (y = r->top; y < r->bottom; y++) {
         for (x = r->left; x < r->right; x++) {
             dst.red = canvasP->planes.red[i];
@@ -2942,9 +2935,9 @@ draw_pen_rect(struct canvas * const canvasP,
 
 
 
-static void 
+static void
 draw_pen(struct canvas * const canvasP,
-         int             const x, 
+         int             const x,
          int             const y) {
 
     struct Rect penrect;
@@ -2972,11 +2965,11 @@ draw_pen(struct canvas * const canvasP,
  *
  * Paul Heckbert    3 Sep 85
  */
-static void 
+static void
 scan_line(struct canvas * const canvasP,
-          short           const x1, 
-          short           const y1, 
-          short           const x2, 
+          short           const x1,
+          short           const y1,
+          short           const x2,
           short           const y2) {
 
     int d, x, y, ax, ay, sx, sy, dx, dy;
@@ -3023,7 +3016,7 @@ scan_line(struct canvas * const canvasP,
 
 static drawFn Line;
 
-static void 
+static void
 Line(struct canvas * const canvasP,
      blitList *      const blitListP,
      int             const version) {
@@ -3041,7 +3034,7 @@ Line(struct canvas * const canvasP,
 
 static drawFn LineFrom;
 
-static void 
+static void
 LineFrom(struct canvas * const canvasP,
          blitList *      const blitListP,
          int             const version) {
@@ -3062,7 +3055,7 @@ LineFrom(struct canvas * const canvasP,
 
 static drawFn ShortLine;
 
-static void 
+static void
 ShortLine(struct canvas * const canvasP,
           blitList *      const blitListP,
           int             const version) {
@@ -3074,7 +3067,7 @@ ShortLine(struct canvas * const canvasP,
         pm_message("(%d,%d) delta (%d, %d)", p1.x, p1.y, current.x, current.y);
     current.x += p1.x;
     current.y += p1.y;
-    
+
     if (!blitListP)
         scan_line(canvasP, p1.x, p1.y, current.x, current.y);
 }
@@ -3083,7 +3076,7 @@ ShortLine(struct canvas * const canvasP,
 
 static drawFn ShortLineFrom;
 
-static void 
+static void
 ShortLineFrom(struct canvas * const canvasP,
               blitList *      const blitListP,
               int             const version) {
@@ -3103,12 +3096,12 @@ ShortLineFrom(struct canvas * const canvasP,
 
 
 
-static void 
+static void
 do_paintRect(struct canvas * const canvasP,
              struct Rect     const prect) {
 
     struct Rect rect;
-  
+
     if (verbose)
         dumpRect("painting", prect);
 
@@ -3121,7 +3114,7 @@ do_paintRect(struct canvas * const canvasP,
 
 static drawFn paintRect;
 
-static void 
+static void
 paintRect(struct canvas * const canvasP,
           blitList *      const blitListP,
           int             const version) {
@@ -3135,7 +3128,7 @@ paintRect(struct canvas * const canvasP,
 
 static drawFn paintSameRect;
 
-static void 
+static void
 paintSameRect(struct canvas * const canvasP,
               blitList *      const blitListP,
               int             const version) {
@@ -3146,7 +3139,7 @@ paintSameRect(struct canvas * const canvasP,
 
 
 
-static void 
+static void
 do_frameRect(struct canvas * const canvasP,
              struct Rect     const rect) {
 
@@ -3160,7 +3153,7 @@ do_frameRect(struct canvas * const canvasP,
             draw_pen(canvasP, x, rect.top);
             draw_pen(canvasP, x, rect.bottom - pen_height);
         }
-        
+
         for (y = rect.top; y <= rect.bottom - pen_height ; y += pen_height) {
             draw_pen(canvasP, rect.left, y);
             draw_pen(canvasP, rect.right - pen_width, y);
@@ -3172,7 +3165,7 @@ do_frameRect(struct canvas * const canvasP,
 
 static drawFn frameRect;
 
-static void 
+static void
 frameRect(struct canvas * const canvasP,
           blitList *      const blitListP,
           int             const version) {
@@ -3186,7 +3179,7 @@ frameRect(struct canvas * const canvasP,
 
 static drawFn frameSameRect;
 
-static void 
+static void
 frameSameRect(struct canvas * const canvasP,
               blitList *      const blitListP,
               int             const version) {
@@ -3199,7 +3192,7 @@ frameSameRect(struct canvas * const canvasP,
 
 /* a stupid shell sort - I'm so embarrassed  */
 
-static void 
+static void
 poly_sort(int const sort_index, struct Point points[]) {
   int d, i, j, temp;
 
@@ -3232,9 +3225,9 @@ poly_sort(int const sort_index, struct Point points[]) {
 
 /* Watch out for the lack of error checking in the next two functions ... */
 
-static void 
+static void
 scan_poly(struct canvas * const canvasP,
-          int             const np, 
+          int             const np,
           struct Point          pts[]) {
 
   int dx,dy,dxabs,dyabs,i,scan_index,j,k,px,py;
@@ -3260,7 +3253,7 @@ scan_poly(struct canvas * const canvasP,
     scan_index++;
   }
 
-#define sign(x) ((x) > 0 ? 1 : ((x)==0 ? 0:(-1)) )   
+#define sign(x) ((x) > 0 ? 1 : ((x)==0 ? 0:(-1)) )
 
   old_sdy = sy0 = sign(pts[1].y - pts[0].y);
   for (j=0; j<np; j++) {
@@ -3319,7 +3312,7 @@ scan_poly(struct canvas * const canvasP,
   if (sy0 + sdy == 0) scan_index--;
 
   poly_sort(scan_index, coord);
-  
+
   toggle = 0;
   for (i = 0; i < scan_index; i++) {
     if ((coord[i].y == coord[i+1].y) && (toggle == 0))
@@ -3332,12 +3325,12 @@ scan_poly(struct canvas * const canvasP,
       toggle = 0;
   }
 }
-  
+
 
 
 static drawFn paintPoly;
-  
-static void 
+
+static void
 paintPoly(struct canvas * const canvasP,
           blitList *      const blitListP,
           int             const version) {
@@ -3359,7 +3352,7 @@ paintPoly(struct canvas * const canvasP,
 
 static drawFn PnLocHFrac;
 
-static void 
+static void
 PnLocHFrac(struct canvas * const canvasP,
            blitList *      const blitListP,
            int             const version) {
@@ -3374,7 +3367,7 @@ PnLocHFrac(struct canvas * const canvasP,
 
 static drawFn TxMode;
 
-static void 
+static void
 TxMode(struct canvas * const canvasP,
        blitList *      const blitListP,
        int             const version) {
@@ -3386,7 +3379,7 @@ TxMode(struct canvas * const canvasP,
     if (verbose)
         pm_message("text transfer mode = %s",
             const_name(transfer_name, text_mode));
-    
+
     /* ignore the text mask bit 'cause we don't handle it yet */
     text_trf = transfer(text_mode & ~64);
 }
@@ -3395,7 +3388,7 @@ TxMode(struct canvas * const canvasP,
 
 static drawFn TxFont;
 
-static void 
+static void
 TxFont(struct canvas * const canvasP,
        blitList *      const blitListP,
        int             const version) {
@@ -3409,7 +3402,7 @@ TxFont(struct canvas * const canvasP,
 
 static drawFn TxFace;
 
-static void 
+static void
 TxFace(struct canvas * const canvasP,
        blitList *      const blitListP,
        int             const version) {
@@ -3423,7 +3416,7 @@ TxFace(struct canvas * const canvasP,
 
 static drawFn TxSize;
 
-static void 
+static void
 TxSize(struct canvas * const canvasP,
        blitList *      const blitListP,
        int             const version) {
@@ -3445,7 +3438,7 @@ skip_text(blitList * const blitListP) {
 
 
 
-static int 
+static int
 abs_value(int const x) {
     if (x < 0)
         return -x;
@@ -3455,9 +3448,9 @@ abs_value(int const x) {
 
 
 
-static struct font* 
-get_font(int const font, 
-         int const size, 
+static struct font*
+get_font(int const font,
+         int const size,
          int const style) {
 
     int closeness, bestcloseness;
@@ -3490,8 +3483,8 @@ get_font(int const font,
 
 /* This only does 0, 90, 180 and 270 degree rotations */
 
-static void 
-rotate(int * const x, 
+static void
+rotate(int * const x,
        int * const y) {
     int tmp;
 
@@ -3522,7 +3515,7 @@ rotate(int * const x,
 
 static void
 do_ps_text(struct canvas * const canvasP,
-           Word            const tx, 
+           Word            const tx,
            Word            const ty) {
 
     int len, width, i, w, h, x, y, rx, ry, o;
@@ -3563,7 +3556,7 @@ do_ps_text(struct canvas * const canvasP,
     for (i = 0; i < len; i++) {
         if (!(glyph = tfont->glyph[str[i]]))
             continue;
-        
+
         y = ty - glyph->height - glyph->y;
         for (h = 0; h < glyph->height; h++) {
             for (w = 0; w < glyph->width; w++) {
@@ -3592,7 +3585,7 @@ do_ps_text(struct canvas * const canvasP,
 static void
 do_text(struct canvas *  const canvasP,
         blitList *       const blitListP,
-        Word             const startx, 
+        Word             const startx,
         Word             const starty) {
 
     if (blitListP)
@@ -3615,11 +3608,11 @@ do_text(struct canvas *  const canvasP,
                     int dy;
                     int h;
                     for (h = 0, dy = y - glyph->height - glyph->y;
-                         h < glyph->height; 
+                         h < glyph->height;
                          ++h, ++dy) {
                         int w;
                         for (w = 0; w < glyph->width; ++w) {
-                            struct RGBColor * const colorP = 
+                            struct RGBColor * const colorP =
                                 glyph->bmap[h * glyph->width + w] ?
                                 &black : &white;
                             draw_pixel(canvasP,
@@ -3710,7 +3703,7 @@ DHDVText(struct canvas * const canvasP,
 static void
 directBits(struct canvas * const canvasP,
            blitList *      const blitListP,
-           unsigned int    const pictVersion, 
+           unsigned int    const pictVersion,
            bool            const skipRegion) {
 
     struct pixMap   p;
@@ -3718,13 +3711,11 @@ directBits(struct canvas * const canvasP,
     struct Rect     dstRect;
     struct raster   raster;
     Word            mode;
-    unsigned int    rectWidth;
 
     /* skip fake len, and fake EOF */
     skip(4);    /* Ptr baseAddr == 0x000000ff */
     readWord();    /* version */
     readRect(&p.Bounds);
-    rectWidth = p.Bounds.right - p.Bounds.left;
     p.packType = readWord();
     p.packSize = readLong();
     p.hRes = readLong();
@@ -3750,7 +3741,7 @@ directBits(struct canvas * const canvasP,
     if (verbose)
         pm_message("transfer mode = %s", const_name(transfer_name, mode));
 
-    if (skipRegion) 
+    if (skipRegion)
         skip_poly_or_region(canvasP, blitListP, pictVersion);
 
     unpackbits(ifp, &p.Bounds, 0, p.pixelSize, &raster);
@@ -3793,8 +3784,8 @@ DirectBitsRgn(struct canvas * const canvasP,
 static void
 do_pixmap(struct canvas * const canvasP,
           blitList *      const blitListP,
-          int             const version, 
-          Word            const rowBytes, 
+          int             const version,
+          Word            const rowBytes,
           int             const is_region) {
 /*----------------------------------------------------------------------------
    Do a paletted image.
@@ -3850,8 +3841,8 @@ static void
 do_bitmap(FILE *          const ifP,
           struct canvas * const canvasP,
           blitList *      const blitListP,
-          int             const version, 
-          int             const rowBytes, 
+          int             const version,
+          int             const rowBytes,
           int             const is_region) {
 /*----------------------------------------------------------------------------
    Do a bitmap.  That's one bit per pixel, 0 is white, 1 is black.
@@ -3866,7 +3857,7 @@ do_bitmap(FILE *          const ifP,
         /* This raster contains padding on the right to make a multiple
            of 16 pixels per row.
         */
-    static struct RGBColor color_table[] = { 
+    static struct RGBColor color_table[] = {
         {65535L, 65535L, 65535L}, {0, 0, 0} };
 
     readRect(&Bounds);
@@ -3921,7 +3912,7 @@ static void
 BitsRegion(struct canvas * const canvasP,
            blitList *      const blitListP,
            int             const version) {
-    
+
     Word rowBytesWord;
     bool pixMap;
     unsigned int rowBytes;
@@ -3978,18 +3969,18 @@ static struct opdef const optable[] = {
 /* 0x1f */  { "OpColor", NA, OpColor, "RGB OpColor for arithmetic modes" },
 /* 0x20 */  { "Line", 8, Line, "pnLoc (point), newPt (point)" },
 /* 0x21 */  { "LineFrom", 4, LineFrom, "newPt (point)" },
-/* 0x22 */  { "ShortLine", 6, ShortLine, 
+/* 0x22 */  { "ShortLine", 6, ShortLine,
               "pnLoc (point, dh, dv (-128 .. 127))" },
 /* 0x23 */  { "ShortLineFrom", 2, ShortLineFrom, "dh, dv (-128 .. 127)" },
 /* 0x24 */  RESERVED_OP(WORD_LEN),
 /* 0x25 */  RESERVED_OP(WORD_LEN),
 /* 0x26 */  RESERVED_OP(WORD_LEN),
 /* 0x27 */  RESERVED_OP(WORD_LEN),
-/* 0x28 */  { "LongText", NA, LongText, 
+/* 0x28 */  { "LongText", NA, LongText,
               "txLoc (point), count (0..255), text" },
 /* 0x29 */  { "DHText", NA, DHText, "dh (0..255), count (0..255), text" },
 /* 0x2a */  { "DVText", NA, DVText, "dv (0..255), count (0..255), text" },
-/* 0x2b */  { "DHDVText", NA, DHDVText, 
+/* 0x2b */  { "DHDVText", NA, DHDVText,
               "dh, dv (0..255), count (0..255), text" },
 /* 0x2c */  RESERVED_OP(WORD_LEN),
 /* 0x2d */  RESERVED_OP(WORD_LEN),
@@ -4101,23 +4092,23 @@ static struct opdef const optable[] = {
 /* 0x97 */  RESERVED_OP(WORD_LEN),
 /* 0x98 */  { "PackBitsRect", NA, BitsRect, "packed copybits, rect clipped" },
 /* 0x99 */  { "PackBitsRgn", NA, BitsRegion, "packed copybits, rgn clipped" },
-/* 0x9a */  { "DirectBitsRect", NA, DirectBitsRect, 
+/* 0x9a */  { "DirectBitsRect", NA, DirectBitsRect,
               "PixMap, srcRect, dstRect, int copymode, PixData" },
-/* 0x9b */  { "DirectBitsRgn", NA, DirectBitsRgn, 
+/* 0x9b */  { "DirectBitsRgn", NA, DirectBitsRgn,
               "PixMap, srcRect, dstRect, int copymode, maskRgn, PixData" },
 /* 0x9c */  RESERVED_OP(WORD_LEN),
 /* 0x9d */  RESERVED_OP(WORD_LEN),
 /* 0x9e */  RESERVED_OP(WORD_LEN),
 /* 0x9f */  RESERVED_OP(WORD_LEN),
 /* 0xa0 */  { "ShortComment", 2, ShortComment, "kind (word)" },
-/* 0xa1 */  { "LongComment", NA, LongComment, 
+/* 0xa1 */  { "LongComment", NA, LongComment,
               "kind (word), size (word), data" }
 };
 
 
 
 static void
-processOpcode(Word            const opcode, 
+processOpcode(Word            const opcode,
               struct canvas * const canvasP,
               blitList *      const blitListP,
               unsigned int    const version) {
@@ -4257,7 +4248,7 @@ interpretPict(FILE * const ofP) {
 
     while((opcode = get_op(version)) != 0xff)
         processOpcode(opcode, &canvas, fullres ? &blitList : NULL, version);
-    
+
     if (fullres) {
         if (blitList.unblittableText)
             pm_message("Warning: text is omitted from the output because "
@@ -4281,7 +4272,7 @@ loadDefaultFontDir(void) {
     int rc;
 
     rc = stat("fontdir", &statbuf);
-    
+
     if (rc == 0)
         load_fontdir("fontdir");
 }
diff --git a/converter/ppm/pjtoppm.c b/converter/ppm/pjtoppm.c
index 7b694fb3..b8b94f74 100644
--- a/converter/ppm/pjtoppm.c
+++ b/converter/ppm/pjtoppm.c
@@ -11,65 +11,87 @@
 */
 
 #include "ppm.h"
+#include "pm_c_util.h"
 #include "mallocvar.h"
 
 static char usage[] =  "[paintjetfile]";
 
-static int egetc ARGS((FILE *fp));
+
+
+static unsigned int
+uintProduct(unsigned int const multiplicand,
+            unsigned int const multiplier) {
+
+    if (UINT_MAX / multiplier < multiplicand)
+        pm_error("Airthmetic overflow");
+
+    return multiplicand * multiplier;
+}
+
+
+
 static int
-egetc(fp)
-    FILE *fp;
-{
+egetc(FILE * const ifP) {
     int c;
-    if ((c = fgetc(fp)) == -1)
+
+    c = fgetc(ifP);
+
+    if (c == -1)
         pm_error("unexpected end of file");
-    return(c);
+
+    return c;
 }
 
+
+
 int
-main(argc, argv)
-    int argc;
-    char *argv[];
-{
+main(int argc, const char ** argv) {
+
     int cmd, val;
     char buffer[BUFSIZ];
     int planes = 3, rows = -1, cols = -1;
-    int r = 0, c = 0, p = 0, i;
     unsigned char **image = NULL;
     int *imlen;
-    FILE *fp = stdin;
+    FILE * ifP;
     int mode;
     int argn;
     unsigned char bf[3];
-    pixel *pixrow;
+    pixel * pixrow;
+    int c;
+    int row;
+    int plane;
 
+    pm_proginit(&argc, argv);
 
-    ppm_init(&argc, argv);
     argn = 1;
     if (argn != argc)
-        fp = pm_openr(argv[argn++]);
+        ifP = pm_openr(argv[argn++]);
     else
-        fp = stdin;
+        ifP = stdin;
 
     if (argn != argc)
         pm_usage(usage);
 
-    while ((c = fgetc(fp)) != -1) {
+    row = 0;  /* initial value */
+    plane = 0;  /* initial value */
+
+    while ((c = fgetc(ifP)) != -1) {
         if (c != '\033')
             continue;
-        switch (c = egetc(fp)) {
+        switch (c = egetc(ifP)) {
         case 'E':   /* reset */
             break;
-        case '*':
-            cmd = egetc(fp);
+        case '*': {
+            unsigned int i;
+            cmd = egetc(ifP);
             for (i = 0; i < BUFSIZ; i++) {
-                if (!isdigit(c = egetc(fp)) && c != '+' && c != '-')
+                if (!isdigit(c = egetc(ifP)) && c != '+' && c != '-')
                     break;
                 buffer[i] = c;
             }
             if (i != 0) {
                 buffer[i] = '\0';
-                if (sscanf(buffer, "%d", &val) != 1) 
+                if (sscanf(buffer, "%d", &val) != 1)
                     pm_error("bad value `%s' at <ESC>*%c%c", buffer, cmd, c);
             }
             else
@@ -100,7 +122,7 @@ main(argc, argv)
                     break;
                 case 'U':   /* planes */
                     planes = val;
-                    if (planes != 3) 
+                    if (planes != 3)
                         pm_error("can handle only 3 plane files");
                     break;
                 case 'A':   /* begin raster */
@@ -126,38 +148,33 @@ main(argc, argv)
                     break;
                 case 'V':   /* send plane */
                 case 'W':   /* send last plane */
-                    if (rows == -1 || r >= rows || image == NULL) {
-                        if (rows == -1 || r >= rows)
+                    if (rows == -1 || row >= rows || image == NULL) {
+                        if (rows == -1 || row >= rows)
                             rows += 100;
                         if (image == NULL) {
-                            MALLOCARRAY(image, rows * planes);
-                            MALLOCARRAY(imlen, rows * planes);
-                        }
-                        else {
-                            image = (unsigned char **) 
-                                realloc(image, 
-                                        rows * planes * 
-                                        sizeof(unsigned char *));
-                            imlen = (int *) 
-                                realloc(imlen, rows * planes * sizeof(int));
+                            MALLOCARRAY(image, uintProduct(rows, planes));
+                            MALLOCARRAY(imlen, uintProduct(rows, planes));
+                        } else {
+                            REALLOCARRAY(image, uintProduct(rows, planes));
+                            REALLOCARRAY(imlen, uintProduct(rows, planes));
                         }
                     }
                     if (image == NULL || imlen == NULL)
                         pm_error("out of memory");
-                    if (p == planes) 
+                    if (plane >= planes)
                         pm_error("too many planes");
-                    cols = cols > val ? cols : val;
-                    imlen[r * planes + p] = val;
-                    MALLOCARRAY(image[r * planes + p], val);
-                    if (image[r * planes + p] == NULL) 
+                    cols = MAX(cols, val);
+                    imlen[row * planes + plane] = val;
+                    MALLOCARRAY(image[row * planes + plane], val);
+                    if (image[row * planes + plane] == NULL)
                         pm_error("out of memory");
-                    if (fread(image[r * planes + p], 1, val, fp) != val) 
+                    if (fread(image[row * planes + plane], 1, val, ifP) != val)
                         pm_error("short data");
                     if (c == 'V')
-                        p++;
+                        ++plane;
                     else {
-                        p = 0;
-                        r++;
+                        plane = 0;
+                        ++row;
                     }
                     break;
                 default:
@@ -166,7 +183,7 @@ main(argc, argv)
                 }
                 break;
             case 'p': /* Position */
-                if (p != 0) 
+                if (plane != 0)
                     pm_error("changed position in the middle of "
                              "transferring planes");
                 switch (c) {
@@ -175,15 +192,15 @@ main(argc, argv)
                     break;
                 case 'Y':
                     if (buffer[0] == '+')
-                        val = r + val;
+                        val = row + val;
                     if (buffer[0] == '-')
-                        val = r - val;
-                    for (; val > r; r++) 
-                        for (p = 0; p < 3; p++) {
-                            imlen[r * planes + p] = 0;
-                            image[r * planes + p] = NULL;
+                        val = row - val;
+                    for (; val > row; ++row)
+                        for (plane = 0; plane < 3; ++plane) {
+                            imlen[row * planes + plane] = 0;
+                            image[row * planes + plane] = NULL;
                         }
-                    r = val;
+                    row = val;
                     break;
                 default:
                     pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
@@ -192,62 +209,80 @@ main(argc, argv)
             default:
                 pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
                 break;
-            }
-        }
+             }
+        } /* case */
+        } /* switch */
     }
-    pm_close(fp);
-    rows = r;
+    pm_close(ifP);
+    rows = row;
     if (mode == 1) {
-        unsigned char *buf;
-        int newcols = 0;
-        newcols = 10240; /* It could not be larger that that! */
-        cols = 0;
-        for (r = 0; r < rows; r++) {
-            if (image[r * planes] == NULL)
+        int const newcols = 10240;  /* It could not be larger that that! */
+        unsigned char * buf;
+        unsigned int row;
+
+        for (row = 0, cols = 0; row < rows; ++row) {
+            unsigned int plane;
+            if (image[row * planes] == NULL)
                 continue;
-            for (p = 0; p < planes; p++) {
+            for (plane = 0; plane < planes; ++plane) {
+                unsigned int i;
+                unsigned int col;
                 MALLOCARRAY(buf, newcols);
-                if (buf == NULL) 
+                if (buf == NULL)
                     pm_error("out of memory");
-                for (i = 0, c = 0; c < imlen[p + r * planes]; c += 2)
-                    for (cmd = image[p + r * planes][c],
-                             val = image[p + r * planes][c+1]; 
-                         cmd >= 0 && i < newcols; cmd--, i++) 
+                for (i = 0, col = 0;
+                     col < imlen[plane + row * planes];
+                     col += 2)
+                    for (cmd = image[plane + row * planes][col],
+                             val = image[plane + row * planes][col+1];
+                         cmd >= 0 && i < newcols; cmd--, i++)
                         buf[i] = val;
-                cols = cols > i ? cols : i;
-                free(image[p + r * planes]);
-                /* 
-                 * This is less than what we have so it realloc should 
+                cols = MAX(cols, i);
+                free(image[plane + row * planes]);
+                /*
+                 * This is less than what we have so it realloc should
                  * not return null. Even if it does, tough! We will
                  * lose a line, and probably die on the next line anyway
                  */
-                image[p + r * planes] = (unsigned char *) realloc(buf, i);
+                image[plane + row * planes] = realloc(buf, i);
             }
         }
         cols *= 8;
     }
-            
-       
+
     ppm_writeppminit(stdout, cols, rows, (pixval) 255, 0);
     pixrow = ppm_allocrow(cols);
-    for (r = 0; r < rows; r++) {
-        if (image[r * planes] == NULL) {
-            for (c = 0; c < cols; c++)
-                PPM_ASSIGN(pixrow[c], 0, 0, 0);
+
+    for (row = 0; row < rows; ++row) {
+        if (image[row * planes] == NULL) {
+            unsigned int col;
+            for (col = 0; col < cols; ++col)
+                PPM_ASSIGN(pixrow[col], 0, 0, 0);
             continue;
         }
-        for (cmd = 0, c = 0; c < cols; c += 8, cmd++) 
-            for (i = 0; i < 8 && c + i < cols; i++) {
-                for (p = 0; p < planes; p++) 
-                    if (mode == 0 && cmd >= imlen[r * planes + p])
-                        bf[p] = 0;
-                    else
-                        bf[p] = (image[r * planes + p][cmd] & 
-                                 (1 << (7 - i))) ? 255 : 0;
-                PPM_ASSIGN(pixrow[c + i], bf[0], bf[1], bf[2]);
+        {
+            unsigned int col;
+            unsigned int cmd;
+            for (cmd = 0, col = 0; col < cols; col += 8, ++cmd) {
+                unsigned int i;
+                for (i = 0; i < 8 && col + i < cols; ++i) {
+                    unsigned int plane;
+                    for (plane = 0; plane < planes; ++plane)
+                        if (mode == 0 && cmd >= imlen[row * planes + plane])
+                            bf[plane] = 0;
+                        else
+                            bf[plane] = (image[row * planes + plane][cmd] &
+                                     (1 << (7 - i))) ? 255 : 0;
+                    PPM_ASSIGN(pixrow[col + i], bf[0], bf[1], bf[2]);
+                }
             }
-        ppm_writeppmrow(stdout, pixrow, cols, (pixval) 255, 0);
+        }
+        ppm_writeppmrow(stdout, pixrow, cols, 255, 0);
     }
     pm_close(stdout);
-    exit(0);
+
+    return 0;
 }
+
+
+
diff --git a/converter/ppm/ppmtoarbtxt.c b/converter/ppm/ppmtoarbtxt.c
index fc8927ce..6d4ed04e 100644
--- a/converter/ppm/ppmtoarbtxt.c
+++ b/converter/ppm/ppmtoarbtxt.c
@@ -734,15 +734,20 @@ interpretObjType(const char * const typstr) {
 
 
 static SkeletonObject *
-newIcSkelFromReplString(const char *       const objstr,
+newIcSkelFromReplString(const char *       const icolorObjstr,
                         SkeletonObjectType const objType) {
-
+/*----------------------------------------------------------------------------
+  A new skeleton for an integer color substitution specifier (class
+  OBJTYP_ICOLOR) whose replacement string (the stuff between the parentheses
+  in #(...)) says substitution type 'objType' and the rest of the
+  replacement string is 'icolorObjstr'.
+-----------------------------------------------------------------------------*/
     SkeletonObject * retval;
     unsigned int icolmin, icolmax;
     char formstr[MAX_OBJ_BUF];
-    unsigned int nOdata;
+    int nOdata;
 
-    nOdata = sscanf(objstr, "%*s%s%u%u", formstr, &icolmin, &icolmax);
+    nOdata = sscanf(icolorObjstr, "%s%u%u", formstr, &icolmin, &icolmax);
 
     if (nOdata == 3)
         retval = newIcolDataObj(objType, formstr, icolmin, icolmax);
@@ -758,16 +763,20 @@ newIcSkelFromReplString(const char *       const objstr,
 
 
 static SkeletonObject *
-newFcSkelFromReplString(const char *       const objstr,
+newFcSkelFromReplString(const char *       const fcolorObjstr,
                         SkeletonObjectType const objType) {
-
+/*----------------------------------------------------------------------------
+  A new skeleton for a floating point color substitution specifier (class
+  OBJTYP_FCOLOR) whose replacement string (the stuff between the parentheses
+  in #(...)) says substitution type 'objType' and the rest of the
+  replacement string is 'fcolorObjstr'.
+-----------------------------------------------------------------------------*/
     SkeletonObject * retval;
     double fcolmin, fcolmax;
     char formstr[MAX_OBJ_BUF];
-    unsigned int nOdata;
+    int nOdata;
 
-    nOdata = sscanf(objstr, "%*s%s%lf%lf", formstr,
-                    &fcolmin, &fcolmax);
+    nOdata = sscanf(fcolorObjstr, "%s%lf%lf", formstr, &fcolmin, &fcolmax);
 
     if (nOdata == 3)
         retval = newFcolDataObj(objType, formstr, fcolmin, fcolmax);
@@ -783,12 +792,19 @@ newFcSkelFromReplString(const char *       const objstr,
 
 
 static SkeletonObject *
-newISkelFromReplString(const char *       const objstr,
+newISkelFromReplString(const char *       const intObjstr,
                        SkeletonObjectType const objType) {
-
+/*----------------------------------------------------------------------------
+  A new skeleton for an integer substitution specifier (class OBJTYP_INT)
+  whose replacement string (the stuff between the parentheses in #(...))
+  says substitution type 'objType' and the rest of the replacement string is
+  'intObjstr'.
+-----------------------------------------------------------------------------*/
     SkeletonObject * retval;
     char formstr[MAX_OBJ_BUF];
-    unsigned int const nOdata = sscanf(objstr, "%*s%s", formstr);
+    int nOdata;
+
+    nOdata = sscanf(intObjstr, "%s", formstr);
     
     if (nOdata == 1)
         retval = newIdataObj(objType, formstr);
@@ -806,7 +822,7 @@ newISkelFromReplString(const char *       const objstr,
 static SkeletonObject *
 newSkeletonFromReplString(const char * const objstr) {
 /*----------------------------------------------------------------------------
-  Create a skeleton from the replacement string 'objstr' (the stuff
+  A new skeleton created from the replacement string 'objstr' (the stuff
   between the parentheses in #(...) ).
 
   Return NULL if it isn't a valid replacement string.
@@ -821,6 +837,7 @@ newSkeletonFromReplString(const char * const objstr) {
 
     SkeletonObject * retval;
     char typstr[MAX_OBJ_BUF];
+    int typlen;
     SkeletonObjectType objType;
     int conversionCt;
     char s1[MAX_OBJ_BUF];    /* Dry read. */
@@ -829,7 +846,8 @@ newSkeletonFromReplString(const char * const objstr) {
 
     typstr[0] = '\0';  /* initial value */
 
-    conversionCt = sscanf(objstr, "%s%s%f%f%s", typstr, s1, &f1, &f2, s2);
+    conversionCt = sscanf(objstr, "%s%n%s%f%f%s",
+                          typstr, &typlen, s1, &f1, &f2, s2);
     switch (conversionCt) {
     case 1: case 2: case 4:
         objType = interpretObjType(typstr);
@@ -840,13 +858,13 @@ newSkeletonFromReplString(const char * const objstr) {
 
     switch (objClass(objType)) {
     case OBJTYP_ICOLOR:
-        retval = newIcSkelFromReplString(objstr, objType);
+        retval = newIcSkelFromReplString(&objstr[typlen], objType);
         break;
     case OBJTYP_FCOLOR:
-        retval = newFcSkelFromReplString(objstr, objType);
+        retval = newFcSkelFromReplString(&objstr[typlen], objType);
         break;
     case OBJTYP_INT:
-        retval = newISkelFromReplString(objstr, objType);
+        retval = newISkelFromReplString(&objstr[typlen], objType);
         break;
     case OBJTYP_BDATA:
         retval = NULL;
diff --git a/converter/ppm/ppmtobmp.c b/converter/ppm/ppmtobmp.c
index 6d65d744..f89cec8d 100644
--- a/converter/ppm/ppmtobmp.c
+++ b/converter/ppm/ppmtobmp.c
@@ -13,6 +13,7 @@
  *
  */
 
+#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1    /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
@@ -29,9 +30,9 @@
 
 #define MAXCOLORS 256
 
-enum colortype {TRUECOLOR, PALETTE};
+typedef enum {TRUECOLOR, PALETTE} Colortype;
 
-struct rgb {
+struct Rgb {
     unsigned char red;
     unsigned char grn;
     unsigned char blu;
@@ -50,13 +51,13 @@ typedef struct {
     colorhash_table cht;
 
     /* Indices in the following array are the same as in 'cht', above. */
-    struct rgb bmpMap[MAXCOLORS];
-} colorMap;
+    struct Rgb bmpMap[MAXCOLORS];
+} ColorMap;
 
 
 
 static void
-freeColorMap(const colorMap * const colorMapP) {
+freeColorMap(const ColorMap * const colorMapP) {
 
     if (colorMapP->cht)
         ppm_freecolorhash(colorMapP->cht);
@@ -68,11 +69,11 @@ struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    const char * inputFilename;
-    int class;  /* C_WIN or C_OS2 */
-    unsigned int bppSpec;
-    unsigned int bpp;
-    const char * mapfile;
+    const char *  inputFilename;
+    enum bmpClass class;
+    unsigned int  bppSpec;
+    unsigned int  bpp;
+    const char *  mapfile;
 };
 
 
@@ -93,15 +94,15 @@ parseCommandLine(int argc, const char ** argv,
     unsigned int windowsSpec, os2Spec, mapfileSpec;
 
     unsigned int option_def_index;
-    
+
     MALLOCARRAY(option_def, 100);
 
     option_def_index = 0;   /* incremented by OPTENTRY */
     OPTENT3('w', "windows",   OPT_FLAG, NULL, &windowsSpec,            0);
     OPTENT3('o', "os2",       OPT_FLAG, NULL, &os2Spec,                0);
-    OPTENT3(0,   "bpp",       OPT_UINT, &cmdlineP->bpp, 
+    OPTENT3(0,   "bpp",       OPT_UINT, &cmdlineP->bpp,
             &cmdlineP->bppSpec,      0);
-    OPTENT3(0,   "mapfile",   OPT_STRING, &cmdlineP->mapfile, 
+    OPTENT3(0,   "mapfile",   OPT_STRING, &cmdlineP->mapfile,
             &mapfileSpec,             0);
 
     opt.opt_table = option_def;
@@ -110,18 +111,18 @@ parseCommandLine(int argc, const char ** argv,
 
     pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
 
-    if (windowsSpec && os2Spec) 
+    if (windowsSpec && os2Spec)
         pm_error("Can't specify both -windows and -os2 options.");
-    else if (windowsSpec) 
-        cmdlineP->class = C_WIN;
+    else if (windowsSpec)
+        cmdlineP->class = BMP_C_WIN_V1;
     else if (os2Spec)
-        cmdlineP->class = C_OS2;
-    else 
-        cmdlineP->class = C_WIN;
+        cmdlineP->class = BMP_C_OS2_1x;
+    else
+        cmdlineP->class = BMP_C_WIN_V1;
 
 
     if (cmdlineP->bppSpec) {
-        if (cmdlineP->bpp != 1 && cmdlineP->bpp != 4 && 
+        if (cmdlineP->bpp != 1 && cmdlineP->bpp != 4 &&
             cmdlineP->bpp != 8 && cmdlineP->bpp != 24)
         pm_error("Invalid -bpp value specified: %u.  The only values valid "
                  "in the BMP format are 1, 4, 8, and 24 bits per pixel",
@@ -135,7 +136,7 @@ parseCommandLine(int argc, const char ** argv,
         cmdlineP->inputFilename = pm_strdup("-");  /* he wants stdin */
     else if (argc - 1 == 1)
         cmdlineP->inputFilename = pm_strdup(argv[1]);
-    else 
+    else
         pm_error("Too many arguments.  The only argument accepted "
                  "is the input file specificaton");
 
@@ -153,8 +154,8 @@ freeCommandLine(struct CmdlineInfo const cmdline) {
 
 
 static void
-PutByte(FILE * const fp, unsigned char const v) {
-    if (putc(v, fp) == EOF) 
+putByte(FILE * const fp, unsigned char const v) {
+    if (putc(v, fp) == EOF)
         pm_error("Write of a byte to a file failed.");
 
     /* Note:  a Solaris/SPARC user reported on 2003.09.29 that the above
@@ -168,139 +169,166 @@ PutByte(FILE * const fp, unsigned char const v) {
 
 
 static void
-PutShort(FILE * const fp, short const v) {
-    if (pm_writelittleshort(fp, v) == -1) 
+putShort(FILE * const fp, short const v) {
+    if (pm_writelittleshort(fp, v) == -1)
         pm_error("Write of a halfword to a file failed.");
 }
 
 
 
 static void
-PutLong(FILE * const fp, long const v) {
+putLong(FILE * const fp, long const v) {
     if (pm_writelittlelong(fp, v) == -1)
         pm_error("Write of a word to a file failed.");
 }
 
 
-
-/*
- * BMP writing
- */
+/*----------------------------------------------------------------------------
+   BMP writing
+-----------------------------------------------------------------------------*/
 
 static unsigned int
-BMPwritefileheader(FILE *        const fp, 
+bmpWriteFileHeader(FILE *        const fp,
                    unsigned int  const cbSize,
                    unsigned int  const offBits) {
 /*----------------------------------------------------------------------------
   Return the number of bytes written.
 -----------------------------------------------------------------------------*/
-    PutByte(fp, 'B');
-    PutByte(fp, 'M');
+    putByte(fp, 'B');
+    putByte(fp, 'M');
 
     /* cbSize */
-    PutLong(fp, cbSize);
-    
+    putLong(fp, cbSize);
+
     /* xHotSpot */
-    PutShort(fp, 0);
-    
+    putShort(fp, 0);
+
     /* yHotSpot */
-    PutShort(fp, 0);
-    
+    putShort(fp, 0);
+
     /* offBits */
-    PutLong(fp, offBits);
-    
+    putLong(fp, offBits);
+
+    assert(BMPlenfileheader() == 14);
+
     return 14;
 }
 
 
 
-static int
-BMPwriteinfoheader(FILE *        const fp, 
-                   int           const class, 
-                   unsigned long const bitcount, 
-                   unsigned long const x, 
+static unsigned int
+bmpWriteInfoHeader(FILE *        const fp,
+                   enum bmpClass const class,
+                   unsigned long const bitcount,
+                   unsigned long const x,
                    unsigned long const y) {
 /*----------------------------------------------------------------------------
+  Write the INFO header.
+
   Return the number of bytes written.
 ----------------------------------------------------------------------------*/
     unsigned int cbFix;
 
     switch (class) {
-    case C_WIN: {
-        cbFix = 40;
-        PutLong(fp, cbFix);
-
-        PutLong(fp, x);         /* cx */
-        PutLong(fp, y);         /* cy */
-        PutShort(fp, 1);        /* cPlanes */
-        PutShort(fp, bitcount); /* cBitCount */
+    case BMP_C_WIN_V1:
+    case BMP_C_WIN_V2:
+    case BMP_C_WIN_V3:
+    case BMP_C_WIN_V4:
+    case BMP_C_WIN_V5:
+    {
+        cbFix = BMP_HDRLEN_WIN_V1;
+        putLong(fp, cbFix);
+
+        putLong(fp, x);         /* cx */
+        putLong(fp, y);         /* cy */
+        putShort(fp, 1);        /* cPlanes */
+        putShort(fp, bitcount); /* cBitCount */
 
         /*
          * We've written 16 bytes so far, need to write 24 more
          * for the required total of 40.
          */
 
-        PutLong(fp, 0);   /* Compression */
-        PutLong(fp, 0);   /* ImageSize */
-        PutLong(fp, 0);   /* XpixelsPerMeter */
-        PutLong(fp, 0);   /* YpixelsPerMeter */
-        PutLong(fp, 0);   /* ColorsUsed */
-        PutLong(fp, 0);   /* ColorsImportant */
+        putLong(fp, 0);   /* Compression */
+        putLong(fp, 0);   /* ImageSize */
+        putLong(fp, 0);   /* XpixelsPerMeter */
+        putLong(fp, 0);   /* YpixelsPerMeter */
+        putLong(fp, 0);   /* ColorsUsed */
+        putLong(fp, 0);   /* ColorsImportant */
+
+        assert(BMP_HDRLEN_WIN_V1 == 40);  /* We wrote 40 bytes */
+
+        if (class != BMP_C_WIN_V1) {
+            /* Invalid call to this function - we don't know how to write
+               these header extensions.
+            */
+            assert(false);
+        }
     }
     break;
-    case C_OS2: {
-        cbFix = 12;
-        PutLong(fp, cbFix);
-
-        PutShort(fp, x);        /* cx */
-        PutShort(fp, y);        /* cy */
-        PutShort(fp, 1);        /* cPlanes */
-        PutShort(fp, bitcount); /* cBitCount */
+    case BMP_C_OS2_1x: {
+        cbFix = BMP_HDRLEN_OS2_1x;
+        putLong(fp, cbFix);
+
+        putShort(fp, x);        /* cx */
+        putShort(fp, y);        /* cy */
+        putShort(fp, 1);        /* cPlanes */
+        putShort(fp, bitcount); /* cBitCount */
+
+        assert(BMP_HDRLEN_OS2_1x == 12);  /* We wrote 12 bytes */
     }
     break;
-    default:
-        pm_error(er_internal, "BMPwriteinfoheader");
+    case BMP_C_OS2_2x:
+        /* Invalid call to this function */
+        assert(false);
+        break;
     }
-
     return cbFix;
 }
 
 
 
-static int
-BMPwriteRgb(FILE * const fp, 
-            int    const class, 
-            pixval const R, 
-            pixval const G, 
-            pixval const B) {
+static unsigned int
+bmpWriteRgb(FILE *        const fp,
+            enum bmpClass const class,
+            pixval        const R,
+            pixval        const G,
+            pixval        const B) {
 /*----------------------------------------------------------------------------
   Return the number of bytes written.
 -----------------------------------------------------------------------------*/
+    unsigned int retval;
+
     switch (class) {
-    case C_WIN:
-        PutByte(fp, B);
-        PutByte(fp, G);
-        PutByte(fp, R);
-        PutByte(fp, 0);
-        return 4;
-    case C_OS2:
-        PutByte(fp, B);
-        PutByte(fp, G);
-        PutByte(fp, R);
-        return 3;
-    default:
-        pm_error(er_internal, "BMPwriteRgb");
-        return -1;  /* avoid compiler warning. */
+    case BMP_C_WIN_V1:
+    case BMP_C_WIN_V2:
+    case BMP_C_WIN_V3:
+    case BMP_C_WIN_V4:
+    case BMP_C_WIN_V5:
+        putByte(fp, B);
+        putByte(fp, G);
+        putByte(fp, R);
+        putByte(fp, 0);
+        retval = 4;
+        break;
+    case BMP_C_OS2_1x:
+    case BMP_C_OS2_2x:
+        putByte(fp, B);
+        putByte(fp, G);
+        putByte(fp, R);
+        retval = 3;
+        break;
     }
+    return retval;
 }
 
 
 
-static int
-BMPwriteColormap(FILE *           const ifP, 
-                 int              const class, 
+static unsigned int
+bmpWriteColormap(FILE *           const ifP,
+                 enum bmpClass    const class,
                  int              const bpp,
-                 const colorMap * const colorMapP) {
+                 const ColorMap * const colorMapP) {
 /*----------------------------------------------------------------------------
   Return the number of bytes written.
 -----------------------------------------------------------------------------*/
@@ -314,12 +342,12 @@ BMPwriteColormap(FILE *           const ifP,
 
     nbyte = 0;
     for (i = 0; i < colorMapP->count; ++i) {
-        const struct rgb * const mapEntryP = &colorMapP->bmpMap[i];
-        nbyte += BMPwriteRgb(ifP, class,
+        const struct Rgb * const mapEntryP = &colorMapP->bmpMap[i];
+        nbyte += bmpWriteRgb(ifP, class,
                              mapEntryP->red, mapEntryP->grn, mapEntryP->blu);
     }
     for (; i < ncolors; ++i)
-        nbyte += BMPwriteRgb(ifP, class, 0, 0, 0);
+        nbyte += bmpWriteRgb(ifP, class, 0, 0, 0);
 
     return nbyte;
 }
@@ -345,10 +373,10 @@ lookupColor(colorhash_table const cht,
 
 
 static void
-bmpWriteRow_palette(FILE *          const fp, 
-                    const pixel *   const row, 
+bmpWriteRow_palette(FILE *          const fp,
+                    const pixel *   const row,
                     unsigned int    const cols,
-                    unsigned short  const bpp, 
+                    unsigned short  const bpp,
                     colorhash_table const cht,
                     unsigned int *  const nBytesP) {
 /*----------------------------------------------------------------------------
@@ -357,7 +385,7 @@ bmpWriteRow_palette(FILE *          const fp,
    Return the number of bytes written as *nBytesP.
 -----------------------------------------------------------------------------*/
     BITSTREAM b;
-    
+
     b = pm_bitinit(fp, "w");
     if (b == NULL)
         pm_error("Failed to initialize output file for output");
@@ -365,9 +393,9 @@ bmpWriteRow_palette(FILE *          const fp,
         int rc;
         unsigned int nbyte;
         unsigned int col;
-        
+
         nbyte = 0;      /* initial value */
-        
+
         for (col = 0; col < cols; ++col) {
             unsigned int colorIndex;
             int rc;
@@ -385,10 +413,10 @@ bmpWriteRow_palette(FILE *          const fp,
         rc = pm_bitfini(b);
 
         nbyte += rc;
-                
+
         /* Make sure we write a multiple of 4 bytes.  */
         while (nbyte % 4 != 0) {
-            PutByte(fp, 0);
+            putByte(fp, 0);
             ++nbyte;
         }
         *nBytesP = nbyte;
@@ -398,13 +426,13 @@ bmpWriteRow_palette(FILE *          const fp,
 
 
 static void
-bmpWriteRow_truecolor(FILE *         const fp, 
-                      const pixel *  const row, 
+bmpWriteRow_truecolor(FILE *         const fp,
+                      const pixel *  const row,
                       unsigned long  const cols,
                       pixval         const maxval,
                       unsigned int * const nBytesP) {
 /*----------------------------------------------------------------------------
-  Write a row of a truecolor BMP image to the file 'fp'.  The row is 
+  Write a row of a truecolor BMP image to the file 'fp'.  The row is
   'row', which is 'cols' columns long.
 
 
@@ -413,20 +441,20 @@ bmpWriteRow_truecolor(FILE *         const fp,
     /* This works only for 24 bits per pixel.  To implement this for the
        general case (which is only hypothetical -- this program doesn't
        write any truecolor images except 24 bit and apparently no one
-       else does either), you would move this function into 
+       else does either), you would move this function into
        BMPwriterow_palette, which writes arbitrary bit strings.  But
        that would be a lot slower and less robust.
     */
 
     int nbyte;  /* Number of bytes we have written to file so far */
-    int col;  
-        
+    int col;
+
     nbyte = 0;  /* initial value */
     for (col = 0; col < cols; ++col) {
         /* We scale to the BMP maxval, which is always 255. */
-        PutByte(fp, PPM_GETB(row[col]) * 255 / maxval);
-        PutByte(fp, PPM_GETG(row[col]) * 255 / maxval);
-        PutByte(fp, PPM_GETR(row[col]) * 255 / maxval);
+        putByte(fp, PPM_GETB(row[col]) * 255 / maxval);
+        putByte(fp, PPM_GETG(row[col]) * 255 / maxval);
+        putByte(fp, PPM_GETR(row[col]) * 255 / maxval);
         nbyte += 3;
     }
 
@@ -434,25 +462,27 @@ bmpWriteRow_truecolor(FILE *         const fp,
      * Make sure we write a multiple of 4 bytes.
      */
     while (nbyte % 4) {
-        PutByte(fp, 0);
+        putByte(fp, 0);
         ++nbyte;
     }
-    
+
     *nBytesP = nbyte;
 }
 
 
 
-static int
-BMPwritebits(FILE *          const fp, 
-             unsigned long   const cols, 
-             unsigned long   const rows,
-             enum colortype  const colortype,
-             unsigned short  const cBitCount, 
-             const pixel **  const pixels, 
-             pixval          const maxval,
-             colorhash_table const cht) {
+static unsigned int
+bmpWriteRaster(FILE *          const fp,
+               unsigned long   const cols,
+               unsigned long   const rows,
+               Colortype       const colortype,
+               unsigned short  const cBitCount,
+               const pixel **  const pixels,
+               pixval          const maxval,
+               colorhash_table const cht) {
 /*----------------------------------------------------------------------------
+  Write the raster.
+
   Return the number of bytes written.
 -----------------------------------------------------------------------------*/
     unsigned int nbyte;
@@ -469,9 +499,9 @@ BMPwritebits(FILE *          const fp,
         unsigned int nBytesThisRow;
 
         if (colortype == PALETTE)
-            bmpWriteRow_palette(fp, pixels[row], cols, 
+            bmpWriteRow_palette(fp, pixels[row], cols,
                                 cBitCount, cht, &nBytesThisRow);
-        else 
+        else
             bmpWriteRow_truecolor(fp, pixels[row], cols, maxval,
                                   &nBytesThisRow);
 
@@ -486,15 +516,15 @@ BMPwritebits(FILE *          const fp,
 
 
 static void
-bmpEncode(FILE *           const ifP, 
-          int              const class, 
-          enum colortype   const colortype,
+bmpEncode(FILE *           const ifP,
+          enum bmpClass    const class,
+          Colortype        const colortype,
           unsigned int     const bpp,
-          int              const x, 
-          int              const y, 
-          const pixel **   const pixels, 
+          int              const x,
+          int              const y,
+          const pixel **   const pixels,
           pixval           const maxval,
-          const colorMap * const colorMapP) {
+          const ColorMap * const colorMapP) {
 /*----------------------------------------------------------------------------
   Write a BMP file of the given class.
 -----------------------------------------------------------------------------*/
@@ -509,16 +539,16 @@ bmpEncode(FILE *           const ifP,
         pm_message("Writing %u bits per pixel truecolor (no palette)", bpp);
 
     nbyte = 0;  /* initial value */
-    nbyte += BMPwritefileheader(ifP, cbSize, offbits);
-    nbyte += BMPwriteinfoheader(ifP, class, bpp, x, y);
+    nbyte += bmpWriteFileHeader(ifP, cbSize, offbits);
+    nbyte += bmpWriteInfoHeader(ifP, class, bpp, x, y);
     if (colortype == PALETTE)
-        nbyte += BMPwriteColormap(ifP, class, bpp, colorMapP);
+        nbyte += bmpWriteColormap(ifP, class, bpp, colorMapP);
 
     if (nbyte != offbits)
         pm_error(er_internal, "BmpEncode 1");
 
-    nbyte += BMPwritebits(ifP, x, y, colortype, bpp, pixels, maxval,
-                          colorMapP->cht);
+    nbyte += bmpWriteRaster(ifP, x, y, colortype, bpp, pixels, maxval,
+                            colorMapP->cht);
     if (nbyte != cbSize)
         pm_error(er_internal, "BmpEncode 2");
 }
@@ -526,7 +556,7 @@ bmpEncode(FILE *           const ifP,
 
 
 static void
-makeBilevelColorMap(colorMap * const colorMapP) {
+makeBilevelColorMap(ColorMap * const colorMapP) {
 
     colorMapP->count  = 2;
     colorMapP->cht    = NULL;
@@ -541,10 +571,10 @@ makeBilevelColorMap(colorMap * const colorMapP) {
 
 
 static void
-bmpEncodePbm(FILE *           const ifP, 
-             int              const class, 
-             int              const cols, 
-             int              const rows, 
+bmpEncodePbm(FILE *           const ifP,
+             enum bmpClass    const class,
+             int              const cols,
+             int              const rows,
              unsigned char ** const bitrow) {
 /*----------------------------------------------------------------------------
   Write a bi-level BMP file of the given class.
@@ -559,19 +589,19 @@ bmpEncodePbm(FILE *           const ifP,
     unsigned int const packedBytes  = adjustedCols / 8;
 
     unsigned long nbyte;
-    colorMap bilevelColorMap;
+    ColorMap bilevelColorMap;
     unsigned int row;
-    
+
     /* colortype == PALETTE */
     pm_message("Writing 1 bit per pixel with a black-white palette");
 
     nbyte = 0;  /* initial value */
-    nbyte += BMPwritefileheader(ifP, cbSize, offbits);
-    nbyte += BMPwriteinfoheader(ifP, class, 1, cols, rows);
+    nbyte += bmpWriteFileHeader(ifP, cbSize, offbits);
+    nbyte += bmpWriteInfoHeader(ifP, class, 1, cols, rows);
 
     makeBilevelColorMap(&bilevelColorMap);
 
-    nbyte += BMPwriteColormap(ifP, class, 1, &bilevelColorMap);
+    nbyte += bmpWriteColormap(ifP, class, 1, &bilevelColorMap);
 
     if (nbyte != offbits)
         pm_error(er_internal, "bmpEncodePbm 1");
@@ -583,7 +613,7 @@ bmpEncodePbm(FILE *           const ifP,
         if (bytesWritten != packedBytes){
             if (feof(ifP))
                 pm_error("End of file writing row %u of BMP raster.", row);
-            else 
+            else
                 pm_error("Error writing BMP raster.  Errno=%d (%s)",
                          errno, strerror(errno));
         }  else
@@ -597,7 +627,7 @@ bmpEncodePbm(FILE *           const ifP,
 
 
 static void
-makeHashFromBmpMap(const struct rgb * const bmpMap,
+makeHashFromBmpMap(const struct Rgb * const bmpMap,
                    unsigned int       const nColors,
                    colorhash_table *  const chtP) {
 
@@ -607,7 +637,7 @@ makeHashFromBmpMap(const struct rgb * const bmpMap,
     MALLOCARRAY_NOFAIL(chv, nColors);
 
     for (i = 0; i < nColors; ++i) {
-        const struct rgb * const mapEntryP = &bmpMap[i];
+        const struct Rgb * const mapEntryP = &bmpMap[i];
 
         PPM_ASSIGN(chv[i].color,
                    mapEntryP->red, mapEntryP->grn, mapEntryP->blu);
@@ -629,7 +659,7 @@ minBmpBitsForColorCount(unsigned int const colorCount) {
        implement and other bpp's have in fact been seen to confuse
        viewers.  There is an extended BMP format that has 16 bpp
        too, but this program doesn't know how to generate that
-       (see Bmptopnm.c, though).  
+       (see Bmptopnm.c, though).
     */
     if (minbits == 1)
         return 1;
@@ -646,7 +676,7 @@ minBmpBitsForColorCount(unsigned int const colorCount) {
 static void
 getMapFile(const char *   const mapFileName,
            unsigned int * const minimumBppP,
-           colorMap *     const colorMapP) {
+           ColorMap *     const colorMapP) {
 /*----------------------------------------------------------------------------
    Get the color map (palette) for the BMP from file 'mapFileName'.
 
@@ -655,7 +685,6 @@ getMapFile(const char *   const mapFileName,
    Return as *minimumBppP the minimum number of bits per pixel it will
    take to represent all the colors in the map in the BMP format.
 -----------------------------------------------------------------------------*/
-
     FILE * mapFileP;
     int cols, rows;
     pixval maxval;
@@ -675,12 +704,12 @@ getMapFile(const char *   const mapFileName,
                  MAXCOLORS, cols * rows);
 
     count = 0; /* initial value */
-    
+
     for (row = 0; row < rows; ++row) {
         unsigned int col;
         for (col = 0; col < cols; ++col) {
             pixel        const color     = pixels[row][col];
-            struct rgb * const mapEntryP = &colorMapP->bmpMap[count++];
+            struct Rgb * const mapEntryP = &colorMapP->bmpMap[count++];
 
             assert(count <= ARRAY_SIZE(colorMapP->bmpMap));
 
@@ -703,15 +732,15 @@ getMapFile(const char *   const mapFileName,
 
 
 static void
-analyzeColors(const pixel **    const pixels, 
-              int               const cols, 
-              int               const rows, 
-              pixval            const maxval, 
+analyzeColors(const pixel **    const pixels,
+              int               const cols,
+              int               const rows,
+              pixval            const maxval,
               unsigned int *    const minimumBppP,
-              colorMap *        const colorMapP) {
+              ColorMap *        const colorMapP) {
 /*----------------------------------------------------------------------------
   Look at the colors in the image 'pixels' and compute values to use in
-  representing those colors in a BMP image.  
+  representing those colors in a BMP image.
 
   First of all, count the distinct colors.  Return as *minimumBppP
   the minimum number of bits per pixel it will take to represent all
@@ -733,7 +762,7 @@ analyzeColors(const pixel **    const pixels,
     int colorCount;
 
     pm_message("analyzing colors...");
-    chv = ppm_computecolorhist((pixel**)pixels, cols, rows, MAXCOLORS, 
+    chv = ppm_computecolorhist((pixel**)pixels, cols, rows, MAXCOLORS,
                                &colorCount);
     colorMapP->count = colorCount;
     if (chv == NULL) {
@@ -751,12 +780,12 @@ analyzeColors(const pixel **    const pixels,
          * Now scale the maxval to 255 as required by BMP format.
          */
         for (i = 0; i < colorMapP->count; ++i) {
-            struct rgb * const mapEntryP = &colorMapP->bmpMap[i];
+            struct Rgb * const mapEntryP = &colorMapP->bmpMap[i];
             mapEntryP->red = (pixval) PPM_GETR(chv[i].color) * 255 / maxval;
             mapEntryP->grn = (pixval) PPM_GETG(chv[i].color) * 255 / maxval;
             mapEntryP->blu = (pixval) PPM_GETB(chv[i].color) * 255 / maxval;
         }
-    
+
         /* And make a hash table for fast lookup. */
         colorMapP->cht = ppm_colorhisttocolorhash(chv, colorMapP->count);
         ppm_freecolorhist(chv);
@@ -769,7 +798,7 @@ static void
 chooseColortypeBpp(bool             const userRequestsBpp,
                    unsigned int     const requestedBpp,
                    unsigned int     const minimumBpp,
-                   enum colortype * const colortypeP, 
+                   Colortype *      const colortypeP,
                    unsigned int *   const bitsPerPixelP) {
 /*----------------------------------------------------------------------------
    Determine whether the BMP raster should contain RGB values or palette
@@ -803,12 +832,12 @@ chooseColortypeBpp(bool             const userRequestsBpp,
             *bitsPerPixelP = requestedBpp;
     }
 
-    assert(*bitsPerPixelP == 1 || 
-           *bitsPerPixelP == 4 || 
-           *bitsPerPixelP == 8 || 
+    assert(*bitsPerPixelP == 1 ||
+           *bitsPerPixelP == 4 ||
+           *bitsPerPixelP == 8 ||
            *bitsPerPixelP == 24);
 
-    if (*bitsPerPixelP > 8) 
+    if (*bitsPerPixelP > 8)
         *colortypeP = TRUECOLOR;
     else {
         *colortypeP = PALETTE;
@@ -818,13 +847,13 @@ chooseColortypeBpp(bool             const userRequestsBpp,
 
 
 static void
-doPbm(FILE *       const ifP,
-      unsigned int const cols,
-      unsigned int const rows,
-      int          const format,
-      int          const class,
-      FILE *       const ofP) {
-    
+doPbm(FILE *        const ifP,
+      unsigned int  const cols,
+      unsigned int  const rows,
+      int           const format,
+      enum bmpClass const class,
+      FILE *        const ofP) {
+
     /* We read the raster directly from the input with
         pbm_readpbmrow_packed().  The raster format is almost
         identical, except that BMP specifies rows to be zero-filled to
@@ -835,7 +864,7 @@ doPbm(FILE *       const ifP,
     int const adjustedCols = (cols+31) /32 * 32;
     int const packedBytes  =  adjustedCols /8;
 
-    unsigned char ** bitrow;    
+    unsigned char ** bitrow;
     unsigned int row;
 
     bitrow = pbm_allocarray_packed(adjustedCols, rows);
@@ -848,15 +877,15 @@ doPbm(FILE *       const ifP,
         thisRow[packedBytes-2] = 0x00;
         thisRow[packedBytes-3] = 0x00;
         thisRow[packedBytes-4] = 0x00;
-        
+
         pbm_readpbmrow_packed(ifP, thisRow, cols, format);
 
         {
             unsigned int i;
-            for (i = 0; i < colChars; ++i) 
+            for (i = 0; i < colChars; ++i)
                 thisRow[i] = ~thisRow[i]; /* flip all pixels */
         }
-        /* This may seem unnecessary, because the color palette 
+        /* This may seem unnecessary, because the color palette
            (RGB[] in bmpEncodePbm) can be inverted for the same effect.
            However we take this precaution, for there is indication that
            some BMP viewers may get confused with that.
@@ -867,21 +896,21 @@ doPbm(FILE *       const ifP,
     }
 
     bmpEncodePbm(ofP, class, cols, rows, bitrow);
-}            
+}
 
 
 
 static void
-doPgmPpm(FILE *       const ifP,
-         unsigned int const cols,
-         unsigned int const rows,
-         pixval       const maxval,
-         int          const ppmFormat,
-         int          const class,
-         bool         const userRequestsBpp,
-         unsigned int const requestedBpp,
-         const char * const mapFileName,
-         FILE *       const ofP) {
+doPgmPpm(FILE *        const ifP,
+         unsigned int  const cols,
+         unsigned int  const rows,
+         pixval        const maxval,
+         int           const ppmFormat,
+         enum bmpClass const class,
+         bool          const userRequestsBpp,
+         unsigned int  const requestedBpp,
+         const char *  const mapFileName,
+         FILE *        const ofP) {
 
     /* PGM and PPM.  We read the input image into a PPM array, scan it
        to analyze the colors, and convert it to a BMP raster.  Logic
@@ -889,29 +918,29 @@ doPgmPpm(FILE *       const ifP,
     */
     unsigned int minimumBpp;
     unsigned int bitsPerPixel;
-    enum colortype colortype;
+    Colortype colortype;
     unsigned int row;
-    
+
     pixel ** pixels;
-    colorMap colorMap;
-    
+    ColorMap colorMap;
+
     pixels = ppm_allocarray(cols, rows);
-    
+
     for (row = 0; row < rows; ++row)
         ppm_readppmrow(ifP, pixels[row], cols, maxval, ppmFormat);
-    
+
     if (mapFileName)
         getMapFile(mapFileName, &minimumBpp, &colorMap);
     else
-        analyzeColors((const pixel**)pixels, cols, rows, maxval, 
+        analyzeColors((const pixel**)pixels, cols, rows, maxval,
                       &minimumBpp, &colorMap);
-    
+
     chooseColortypeBpp(userRequestsBpp, requestedBpp, minimumBpp,
                        &colortype, &bitsPerPixel);
-    
+
     bmpEncode(ofP, class, colortype, bitsPerPixel,
               cols, rows, (const pixel**)pixels, maxval, &colorMap);
-    
+
     freeColorMap(&colorMap);
 
     ppm_freearray(pixels, rows);
@@ -935,10 +964,11 @@ main(int           argc,
     parseCommandLine(argc, argv, &cmdline);
 
     ifP = pm_openr(cmdline.inputFilename);
-    
+
     ppm_readppminit(ifP, &cols, &rows, &maxval, &ppmFormat);
-    
-    if (PPM_FORMAT_TYPE(ppmFormat) == PBM_TYPE)
+
+    if ((PPM_FORMAT_TYPE(ppmFormat) == PBM_TYPE) &&
+        (!cmdline.bppSpec || cmdline.bpp == 1))
         doPbm(ifP, cols, rows, ppmFormat, cmdline.class, stdout);
     else
         doPgmPpm(ifP, cols, rows, maxval, ppmFormat,
@@ -952,3 +982,6 @@ main(int           argc,
 
     return 0;
 }
+
+
+
diff --git a/converter/ppm/ppmtogif.c b/converter/ppm/ppmtogif.c
index fa7d1dbe..8dd133b5 100644
--- a/converter/ppm/ppmtogif.c
+++ b/converter/ppm/ppmtogif.c
@@ -10,6 +10,7 @@
      - Pamtogif requires a user-specififed map file (-mapfile) to
        match the input in depth.
 */
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/ppm/ppmtoicr.c b/converter/ppm/ppmtoicr.c
index 3c8be421..1720f790 100644
--- a/converter/ppm/ppmtoicr.c
+++ b/converter/ppm/ppmtoicr.c
@@ -53,35 +53,6 @@ makeIcrColormap(colorhist_vector const chv,
 
 
 static int
-bppFromColorCt(unsigned int const colorCt) {
-
-    unsigned int bpp;
-
-    if (colorCt <= 2)
-        bpp = 1;
-    else if (colorCt <= 4)
-        bpp = 2;
-    else if (colorCt <= 8)
-        bpp = 3;
-    else if (colorCt <= 16)
-        bpp = 4;
-    else if (colorCt <= 32)
-        bpp = 5;
-    else if (colorCt <= 64)
-        bpp = 6;
-    else if (colorCt <= 128)
-        bpp = 7;
-    else if (colorCt <= 256)
-        bpp = 8;
-    else
-        assert(false);
-
-    return bpp;
-}
-
-
-
-static int
 colorIndexAtPosition(unsigned int    const x,
                      unsigned int    const y,
                      pixel **        const pixels,
@@ -166,7 +137,6 @@ main(int argc, const char ** const argv) {
     int rows, cols;
     int colorCt;
     int argn;
-    unsigned int bitsPerPixel;
     pixval maxval;
     colorhist_vector chv;
     char rgb[CLUTCOLORCT];
@@ -235,8 +205,6 @@ main(int argc, const char ** const argv) {
 
     makeIcrColormap(chv, colorCt, maxval, rgb);
 
-    bitsPerPixel = bppFromColorCt(colorCt);
-
     /* And make a hash table for fast lookup. */
     cht = ppm_colorhisttocolorhash(chv, colorCt);
 
diff --git a/converter/ppm/ppmtomitsu.c b/converter/ppm/ppmtomitsu.c
index e59f09b3..5b0b3245 100644
--- a/converter/ppm/ppmtomitsu.c
+++ b/converter/ppm/ppmtomitsu.c
@@ -274,7 +274,7 @@ static void
 doLookupTableColors(colorhist_vector const table,
                     unsigned int     const nColor,
                     hashinfo *       const colorhashtable) {
-                
+
     unsigned int colval;
     for (colval = 0; colval < nColor; ++colval) {
         struct hashinfo * const hashchain =
@@ -287,7 +287,7 @@ doLookupTableColors(colorhist_vector const table,
         datum(PPM_GETR((table[colval]).color));
         datum(PPM_GETG((table[colval]).color));
         datum(PPM_GETB((table[colval]).color));
-        
+
         hashrun = hashchain;  /* start at beginning of chain */
 
         if (hashrun->flag == -1) {
@@ -323,7 +323,7 @@ doLookupTableGrays(colorhist_vector const table,
         datum(PPM_GETB((table[colval]).color));
         datum(PPM_GETB((table[colval]).color));
         datum(PPM_GETB((table[colval]).color));
-        
+
         hashrun = hashchain;  /* start at beginning of chain */
 
         if (hashrun->flag == -1) {
@@ -410,7 +410,7 @@ writeColormapRaster(pixel **         const pixels,
             struct hashinfo * const hashchain =
                 &colorhashtable[myhash(pixrow[col])];
             struct hashinfo * p;
-                
+
             p = hashchain;
             while (!PPM_EQUAL((p->color), pixrow[col])) {
                 assert(p->next);
@@ -554,7 +554,7 @@ doTiny(FILE *           const ifP,
        int              const enlarge,
        int              const copy,
        struct mediasize const medias) {
-       
+
     pixel * pixelrow;
     unsigned char * redrow;
     unsigned char * grnrow;
@@ -653,7 +653,7 @@ main(int argc, char * argv[]) {
         tiny = TRUE;
     else
         pm_usage(usage);
-        ++argn;
+    ++argn;
     }
 
     if (argn < argc) {
@@ -690,7 +690,7 @@ main(int argc, char * argv[]) {
     }
 
     ppm_readppminit(ifP, &cols, &rows, &maxval, &format);
-    
+
     if (tiny) {
         doTiny(ifP, cols, rows, maxval, format,
                sharpness, enlarge, copy, medias);
@@ -707,7 +707,7 @@ main(int argc, char * argv[]) {
 
         /* first check wether we can use the lut transfer */
 
-        table = ppm_computecolorhist(pixels, cols, rows, MAXLUTCOL+1, 
+        table = ppm_computecolorhist(pixels, cols, rows, MAXLUTCOL+1,
                                      &nColor);
         if (table)
             useLookupTable(pixels, table, sharpness, enlarge, copy, medias,
diff --git a/converter/ppm/ppmtompeg/HISTORY b/converter/ppm/ppmtompeg/HISTORY
index c9f4932a..e6fb86d3 100644
--- a/converter/ppm/ppmtompeg/HISTORY
+++ b/converter/ppm/ppmtompeg/HISTORY
@@ -1,4 +1,4 @@
-The entire ppmtojpeg directory was adapted by Bryan from the package
+The entire ppmtompeg directory was adapted by Bryan from the package
 mpeg_encode-1.5b-src (subdirectory mpeg_encode) on March 30, 1999.  The 
 program was called mpeg_encode in that package.  It was dated August 16,
 1995 and came from ftp://mm-ftp.cs.berkeley.edu/pub/multimedia/mpeg/
diff --git a/converter/ppm/ppmtompeg/frame.c b/converter/ppm/ppmtompeg/frame.c
index 75b209f8..f91f2cc6 100644
--- a/converter/ppm/ppmtompeg/frame.c
+++ b/converter/ppm/ppmtompeg/frame.c
@@ -82,8 +82,10 @@ Resize_Array_Width(uint8 ** const inarray,
     int out_total;
     uint8 *inptr;
     uint8 *outptr;
+#ifdef DOING_INTERPOLATION
     uint8 pointA,pointB;
-    /* double slope,diff; */
+    double slope,diff;
+#endif
     
     for (i = 0; i < in_y; ++i) {     /* For each row */
         unsigned int j;
@@ -105,11 +107,15 @@ Resize_Array_Width(uint8 ** const inarray,
                     --inptr;
                 }
             } else {  
+#ifdef DOING_INTERPOLATION
                 pointA = *inptr;
+#endif
                 ++inptr;
+#ifdef DOING_INTERPOLATION
                 pointB = *inptr;
+#endif
                 --inptr;
-#if 0
+#ifdef DOING_INTERPOLATION
                 /*Interpolative solution */
                 slope = ((double)(pointB -pointA))/((double)(out_x));
                 diff = (((double)(out_total - in_total)));
@@ -159,8 +165,10 @@ Resize_Array_Height(uint8 ** const inarray,
     for(i=0; i < in_x; ++i){    /* for each column */
         int in_total;
         int out_total;
+#ifdef DOING_INTERPOLATION
         uint8 pointA, pointB;
         double slope, diff;
+#endif
         unsigned int j;
         int k;
 
@@ -180,15 +188,17 @@ Resize_Array_Height(uint8 ** const inarray,
                     --k;
                 }
             } else {  
+#ifdef DOING_INTERPOLATION
                 pointA = inarray[k][i];
-                if (k != (in_y -1)) {
+                if (k != (in_y - 1)) {
                     pointB = inarray[k+1][i];
                 } else
                     pointB = pointA;
                 /* Interpolative case */
                 slope = ((double)(pointB -pointA))/(double)(out_y);
                 diff = (double)(out_total - in_total);
-                /*  outarray[j][i] = (inarray[k][i] + (uint8)(slope*diff)); */
+                outarray[j][i] = (inarray[k][i] + (uint8)(slope*diff));
+#endif
                 /* Non-Interpolative case */
                 outarray[j][i] = inarray[k][i];
                 out_total = out_total + in_y;
diff --git a/converter/ppm/ppmtompeg/frametype.c b/converter/ppm/ppmtompeg/frametype.c
index b7daacc9..09afa403 100644
--- a/converter/ppm/ppmtompeg/frametype.c
+++ b/converter/ppm/ppmtompeg/frametype.c
@@ -264,7 +264,7 @@ ComputeFrameTable(unsigned int const numFramesArg) {
   'numFrames' == 0 means number of frames is not known at this time.
 -----------------------------------------------------------------------------*/
     int index;
-    FrameTable	*lastI, *lastIP, *firstB, *secondIP;
+    FrameTable	*lastIP, *firstB, *secondIP;
     FrameTable	*ptr;
     char typ;
     int table_size;
@@ -278,7 +278,6 @@ ComputeFrameTable(unsigned int const numFramesArg) {
 
     MALLOCARRAY_NOFAIL(frameTable, 1 + table_size);
 
-    lastI = NULL;
     lastIP = NULL;
     firstB = NULL;
     secondIP = NULL;
diff --git a/converter/ppm/ppmtompeg/gethostname.c b/converter/ppm/ppmtompeg/gethostname.c
index d20af17c..649fff91 100644
--- a/converter/ppm/ppmtompeg/gethostname.c
+++ b/converter/ppm/ppmtompeg/gethostname.c
@@ -1,3 +1,4 @@
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE   /* Make sure strdup() is in string.h */
 
diff --git a/converter/ppm/ppmtompeg/jrevdct.c b/converter/ppm/ppmtompeg/jrevdct.c
index bf9196c4..dd1f9fff 100644
--- a/converter/ppm/ppmtompeg/jrevdct.c
+++ b/converter/ppm/ppmtompeg/jrevdct.c
@@ -26,7 +26,11 @@
  * matrix, perhaps with the difference cases encoded.
  */
 
+#define _XOPEN_SOURCE 500  /* get M_PI in math.h */
+
 #include <memory.h>
+#include <math.h>
+
 #include "all.h"
 #include "dct.h"
 
@@ -1211,35 +1215,29 @@ mpeg_jrevdct_quick(data)
 */
 
 
-/* Here we use math.h to generate constants.  Compiler results may
-   vary a little */
-
-#ifndef PI
-#ifdef M_PI
-#define PI M_PI
-#else
-#define PI 3.14159265358979323846
-#endif
-#endif
-
 /* cosine transform matrix for 8x1 IDCT */
 static double itrans_coef[8][8];
 
-/* initialize DCT coefficient matrix */
 
-void init_idctref()
-{
-  int freq, time;
-  double scale;
-
-  for (freq=0; freq < 8; freq++)
-  {
-    scale = (freq == 0) ? sqrt(0.125) : 0.5;
-    for (time=0; time<8; time++)
-      itrans_coef[freq][time] = scale*cos((PI/8.0)*freq*(time + 0.5));
-  }
+
+void init_idctref() {
+/*----------------------------------------------------------------------------
+   initialize DCT coefficient matrix 
+-----------------------------------------------------------------------------*/
+    unsigned int freq;
+
+    for (freq=0; freq < 8; ++freq) {
+        double const scale = (freq == 0) ? sqrt(0.125) : 0.5;
+
+        unsigned int time;
+
+        for (time = 0; time < 8; ++time)
+            itrans_coef[freq][time] = scale*cos((M_PI/8.0)*freq*(time + 0.5));
+    }
 }
 
+
+
 /* perform IDCT matrix multiply for 8x8 coefficient block */
 
 void reference_rev_dct(block)
diff --git a/converter/ppm/ppmtompeg/mfwddct.c b/converter/ppm/ppmtompeg/mfwddct.c
index 75c3a718..9381e51c 100644
--- a/converter/ppm/ppmtompeg/mfwddct.c
+++ b/converter/ppm/ppmtompeg/mfwddct.c
@@ -15,6 +15,10 @@
  * instead of floating point.
  */
 
+#define _XOPEN_SOURCE 500  /* get M_PI in math.h */
+
+#include <math.h>
+
 #include "all.h"
 
 #include "dct.h"
@@ -375,26 +379,19 @@ mp_fwd_dct_block2(data, dest)
  *
  */
 
-#ifndef PI
-#ifdef M_PI
-#define PI M_PI
-#else
-#define PI 3.14159265358979323846
-#endif
-#endif
 
-void init_fdct()
-{
-  int i, j;
-  double s;
+void init_fdct() {
 
-  for (i=0; i<8; i++)
-  {
-    s = (i==0) ? sqrt(0.125) : 0.5;
+    unsigned int i;
 
-    for (j=0; j<8; j++)
-      trans_coef[i][j] = s * cos((PI/8.0)*i*(j+0.5));
-  }
+    for (i = 0; i < 8; ++i) {
+        double const s = i == 0 ? sqrt(0.125) : 0.5;
+
+        unsigned int j;
+
+        for (j = 0; j < 8; ++j)
+            trans_coef[i][j] = s * cos((M_PI/8.0) * i * (j+0.5));
+    }
 }
 
 
diff --git a/converter/ppm/ppmtompeg/mpeg.c b/converter/ppm/ppmtompeg/mpeg.c
index 24d337ed..33e1a9f9 100644
--- a/converter/ppm/ppmtompeg/mpeg.c
+++ b/converter/ppm/ppmtompeg/mpeg.c
@@ -30,6 +30,7 @@
  * HEADER FILES *
  *==============*/
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE   /* Make sure strdup() is in string.h */
 
diff --git a/converter/ppm/ppmtompeg/parallel.c b/converter/ppm/ppmtompeg/parallel.c
index 2835c67c..e3bcec1a 100644
--- a/converter/ppm/ppmtompeg/parallel.c
+++ b/converter/ppm/ppmtompeg/parallel.c
@@ -2149,7 +2149,6 @@ DecodeServer(int          const numInputFiles,
     int     otherSock;
     int     decodePortNum;
     int     frameReady;
-    boolean *ready;
     int     *waitMachine;
     int     *waitPort;
     int     *waitList;
@@ -2162,7 +2161,6 @@ DecodeServer(int          const numInputFiles,
 
     /* should keep list of port numbers to notify when frames become ready */
 
-    ready = (boolean *) calloc(numInputFiles, sizeof(boolean));
     waitMachine = (int *) calloc(numInputFiles, sizeof(int));
     waitPort = (int *) malloc(numMachines*sizeof(int));
     waitList = (int *) calloc(numMachines, sizeof(int));
diff --git a/converter/ppm/ppmtompeg/param.c b/converter/ppm/ppmtompeg/param.c
index 45605981..87fdfa6b 100644
--- a/converter/ppm/ppmtompeg/param.c
+++ b/converter/ppm/ppmtompeg/param.c
@@ -7,6 +7,8 @@
 
 /* COPYRIGHT INFORMATION IS AT THE END OF THIS FILE */
 
+#define _DEFAULT_SOURCE 1
+    /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500
     /* This makes sure popen() is in stdio.h.  In GNU libc 2.1.3, 
      _POSIX_C_SOURCE = 2 is sufficient, but on AIX 4.3, the higher level
diff --git a/converter/ppm/ppmtompeg/ppmtompeg.c b/converter/ppm/ppmtompeg/ppmtompeg.c
index cd94db39..837b1b9d 100644
--- a/converter/ppm/ppmtompeg/ppmtompeg.c
+++ b/converter/ppm/ppmtompeg/ppmtompeg.c
@@ -30,6 +30,7 @@
  * HEADER FILES *
  *==============*/
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 #define _BSD_SOURCE   /* Make sure strdup() is in string.h */
 
@@ -190,16 +191,17 @@ parseArgs(int     const argc,
             ++idx;
         } else if (streq(argv[idx], "-child")) {
             if (idx+7 < argc-1) {
-                int combinePortNumber;
-                    /* This used to be important information, when the child
-                       notified the combine server.  Now the master notifies
-                       the combine server after the child notifies the master
-                       it is done.  So this value is unused.
-                    */
                 cmdlineP->masterHostname = argv[idx+1];
                 cmdlineP->masterPortNumber = atoi(argv[idx+2]);
                 ioPortNumber = atoi(argv[idx+3]);
-                combinePortNumber = atoi(argv[idx+4]);
+                /*
+                  combinePortNumber = atoi(argv[idx+4]);
+
+                  This used to be important information, when the child
+                  notified the combine server.  Now the master notifies
+                  the combine server after the child notifies the master
+                  it is done.  So this value is unused.
+                */
                 decodePortNumber = atoi(argv[idx+5]);
                 machineNumber = atoi(argv[idx+6]);
                 remoteIO = atoi(argv[idx+7]);
diff --git a/converter/ppm/ppmtompeg/rate.c b/converter/ppm/ppmtompeg/rate.c
index c775e055..6ec330cf 100644
--- a/converter/ppm/ppmtompeg/rate.c
+++ b/converter/ppm/ppmtompeg/rate.c
@@ -389,7 +389,6 @@ targetRateControl(MpegFrame * const frame) {
     float tempX, tempY, tempZ;
     int result;
     int frameType;
-    const char *strPtr;
   
     minimumBits = (bit_rate / (8 * frameRateRounded));
   
@@ -445,14 +444,18 @@ targetRateControl(MpegFrame * const frame) {
     Qscale = (mquant > 31 ? 31 : mquant);
     Qscale = (Qscale < 1 ? 1 : Qscale);
   
-    /*   Print headers for Frame info */
-    strPtr = Frame_header1;
-    DBG_PRINT(("%s\n",strPtr));
-    strPtr = Frame_header2;
-    DBG_PRINT(("%s\n",strPtr));
-    strPtr = Frame_header3;
-    DBG_PRINT(("%s\n",strPtr));
-  
+#ifdef HEINOUS_DEBUG_MODE
+    {
+        const char * strPtr;
+        /*   Print headers for Frame info */
+        strPtr = Frame_header1;
+        DBG_PRINT(("%s\n",strPtr));
+        strPtr = Frame_header2;
+        DBG_PRINT(("%s\n",strPtr));
+        strPtr = Frame_header3;
+        DBG_PRINT(("%s\n",strPtr));
+    }
+#endif
     /*   Print Frame info */
     sprintf(rc_buffer, "%4d     %1c  %4d  %6d %7d  "
             "%2d %2d %2d   %2.2f  %6d %4d    %3d",
@@ -467,10 +470,13 @@ targetRateControl(MpegFrame * const frame) {
   
     /*  Print headers for Macroblock info */
     if (RC_MB_SAMPLE_RATE) {
+#ifdef HEINOUS_DEBUG_MODE
+        const char * strPtr;
         strPtr = MB_header1;
         DBG_PRINT(("%s\n",strPtr));
         strPtr = MB_header2;
         DBG_PRINT(("%s\n",strPtr));
+#endif
     }
 }
 
@@ -519,7 +525,6 @@ void
 updateRateControl(int const type) {
     int totalBits, frameComplexity, pctAllocUsed, pctGOPUsed;
     float avgQuant;
-    const char *strPtr;
 
     totalBits = rc_totalFrameBits;
     avgQuant = ((float) rc_totalQuant / (float) rc_numBlocks);
@@ -559,15 +564,18 @@ updateRateControl(int const type) {
         break;
     }
   
-  
-    /*  Print Frame info */
-    strPtr = Frame_trailer1;
-    DBG_PRINT(("%s\n",strPtr));
-    strPtr = Frame_trailer2;
-    DBG_PRINT(("%s\n",strPtr));
-    strPtr = Frame_trailer3;
-    DBG_PRINT(("%s\n",strPtr));
-  
+#ifdef HEINOUS_DEBUG_MODE
+    {  
+        /*  Print Frame info */
+        const char * strPtr;
+        strPtr = Frame_trailer1;
+        DBG_PRINT(("%s\n",strPtr));
+        strPtr = Frame_trailer2;
+        DBG_PRINT(("%s\n",strPtr));
+        strPtr = Frame_trailer3;
+        DBG_PRINT(("%s\n",strPtr));
+    }
+#endif  
     sprintf(rc_buffer, "%6d  %2.2f  %6d  %3d  %2.2f %7d   "
             "%3d %7d   %3d  %6d %6d",
             totalBits, avgQuant, frameComplexity, avg_act, N_act, 
diff --git a/converter/ppm/ppmtompeg/readframe.c b/converter/ppm/ppmtompeg/readframe.c
index 23752706..2a359b2f 100644
--- a/converter/ppm/ppmtompeg/readframe.c
+++ b/converter/ppm/ppmtompeg/readframe.c
@@ -17,6 +17,7 @@
  * HEADER FILES *
  *==============*/
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE   /* Make sure popen() is in stdio.h */
 #include "all.h"
 #include <time.h>
diff --git a/converter/ppm/ppmtompeg/specifics.c b/converter/ppm/ppmtompeg/specifics.c
index fb5e3649..1a1fda44 100644
--- a/converter/ppm/ppmtompeg/specifics.c
+++ b/converter/ppm/ppmtompeg/specifics.c
@@ -36,6 +36,7 @@
  * HEADER FILES *
  *==============*/
 
+#include "netpbm/mallocvar.h"
 #include "all.h"
 #include "mtypes.h"
 #include "frames.h"
@@ -239,7 +240,6 @@ FILE *fp;
   FrameSpecList *current, *new;
   char typ; 
   int fnum,snum, bnum, qs, newqs;
-  int num_scanned;
 
   fsl = MakeFslEntry();
   current = fsl;
@@ -274,7 +274,7 @@ FILE *fp;
       break;
     case 'B':
       lp += 6;
-      num_scanned = sscanf(lp, "%d %d", &bnum, &newqs);
+      sscanf(lp, "%d %d", &bnum, &newqs);
       if (qs == newqs) break;
       qs = newqs;
       AddBs(current, bnum, FALSE, qs);
@@ -289,116 +289,140 @@ FILE *fp;
   
 }
 
-/* Version 2 */
-void Parse_Specifics_File_v2(fp)
-FILE *fp;
-{
-  char line[1024], *lp;
-  FrameSpecList *current, *new;
-  char typ;
-  int fnum, snum, bnum, qs, newqs;
-  int num_scanned, fx=0, fy=0, sx=0, sy=0;
-  char kind[100];
-  Block_Specifics *new_blk;
-  boolean relative;
 
-  fsl = MakeFslEntry();
-  current = fsl;
 
-  while ((fgets(line,1023,fp))!=NULL) {
-    lp = &line[0];
-    while ((*lp == ' ') || (*lp == '\t')) lp++;
-    if (( *lp == '#' ) || (*lp=='\n')) {
-      continue;
+void
+Parse_Specifics_File_v2(FILE * const fP) {
+/*----------------------------------------------------------------------------
+   Parse Version 2 specific file.
+-----------------------------------------------------------------------------*/
+    char line[1024];
+    FrameSpecList * current;
+    int qs;
+    int numScanned;
+    int fx, fy, sx, sy;
+    char kind[100];
+    Block_Specifics *new_blk;
+    boolean relative;
+
+    fx = fy = sx = sy = 0;  /* initial value */
+
+    fsl = MakeFslEntry();
+    current = fsl;
+
+    while ((fgets(line,1023, fP))!=NULL) {
+        const char * lp;
+
+        lp = &line[0];  /* initial value */
+
+        while ((*lp == ' ') || (*lp == '\t'))
+            ++lp;
+        if (*lp == '#' || *lp == '\n') {
+            /* comment or blank line */
+        } else {
+            switch (my_upper(*lp)) {
+            case 'F': {
+                char typ;
+                FrameSpecList * new;
+                int fnum;
+
+                lp += 6;
+                sscanf(lp,"%d %c %d", &fnum, &typ, &qs);
+                new = MakeFslEntry();
+                if (current->framenum != -1) {
+                    current->next = new;
+                    current = new;
+                }
+                current->framenum = fnum;
+                current->frametype = CvtType(typ);
+                if (qs <= 0)
+                    qs = -1;
+                current->qscale = qs;
+            } break;
+            case 'S': {
+                int snum;
+                int newqs;
+                lp += 6;
+                sscanf(lp,"%d %d", &snum, &newqs);
+                if (qs == newqs)
+                    break;
+                qs = newqs;
+                AddSlc(current, snum, qs);
+            } break;
+            case 'B': {
+                int bnum;
+                int newqs;
+                lp += 6;
+                numScanned = 0;
+                bnum = atoi(lp);
+                SkipToSpace(lp);
+                while ((*lp != '-') && (*lp != '+') &&
+                       ((*lp < '0') || (*lp > '9')))
+                    ++lp;
+                relative = (*lp == '-' || *lp == '+');
+                newqs = atoi(lp);
+                SkipToSpace(lp);
+                if (EndString(lp)) {
+                    numScanned = 2;
+                } else {
+                    numScanned =
+                        2 + sscanf(lp, "%s %d %d %d %d",
+                                   kind, &fx, &fy, &sx, &sy); 
+                }
+
+                qs = newqs;
+                new_blk = AddBs(current, bnum, relative, qs);
+                if (numScanned > 2) {
+                    BlockMV * tmp;
+
+                    MALLOCVAR(tmp);
+
+                    switch (numScanned) {
+                    case 7:
+                        tmp->typ = TYP_BOTH;
+                        tmp->fx = fx;
+                        tmp->fy = fy;
+                        tmp->bx = sx;
+                        tmp->by = sy;
+                        new_blk->mv = tmp;
+                        break;
+                    case 3:
+                        tmp->typ = TYP_SKIP;
+                        new_blk->mv = tmp;
+                        break;
+                    case 5:
+                        if (my_upper(kind[0]) == 'B') {
+                            tmp->typ = TYP_BACK;
+                            tmp->bx = fx;
+                            tmp->by = fy;
+                        } else {
+                            tmp->typ = TYP_FORW;
+                            tmp->fx = fx;
+                            tmp->fy = fy;
+                        }
+                        new_blk->mv = tmp;
+                        break;
+                    default:
+                        fprintf(stderr,
+                                "Bug in specifics file!  "
+                                "Skipping short/long entry: %s\n",line);
+                        break;
+                    }
+                } else
+                    new_blk->mv = NULL;
+
+            } break;
+            case 'V':
+                fprintf(stderr,
+                        "Cannot specify version twice!  Taking first (%d).\n",
+                        version);
+                break;
+            default:
+                printf("What? *%s*\n",line);
+                break;
+            }
+        }
     }
-
-    switch (my_upper(*lp)) {
-    case 'F':
-      lp += 6;
-      sscanf(lp,"%d %c %d", &fnum, &typ, &qs);
-      new = MakeFslEntry();
-      if (current->framenum != -1) {
-	current->next = new;
-	current = new;
-      }
-      current->framenum = fnum;
-      current->frametype = CvtType(typ);
-      if (qs <= 0) qs = -1;
-      current->qscale = qs;
-      break;
-    case 'S':
-      lp += 6;
-      sscanf(lp,"%d %d", &snum, &newqs);
-      if (qs == newqs) break;
-      qs = newqs;
-      AddSlc(current, snum, qs);
-      break;
-    case 'B':
-      lp += 6;
-      num_scanned = 0;
-      bnum = atoi(lp);
-      SkipToSpace(lp);
-      while ((*lp != '-') && (*lp != '+') &&
-	     ((*lp < '0') || (*lp > '9'))) lp++;
-      relative = ((*lp == '-') || (*lp == '+'));
-      newqs = atoi(lp);
-      SkipToSpace(lp);
-      if (EndString(lp)) {
-	num_scanned = 2;
-      } else {
-	num_scanned = 2+sscanf(lp, "%s %d %d %d %d", kind, &fx, &fy, &sx, &sy); 
-      }
-
-      qs = newqs;
-      new_blk = AddBs(current, bnum, relative, qs);
-      if (num_scanned > 2) {
-	BlockMV *tmp;
-	tmp = (BlockMV *) malloc(sizeof(BlockMV));
-	switch (num_scanned) {
-	case 7:
-	  tmp->typ = TYP_BOTH;
-	  tmp->fx = fx;
-	  tmp->fy = fy;
-	  tmp->bx = sx;
-	  tmp->by = sy;
-	  new_blk->mv = tmp;
-	  break;
-	case 3:
-	  tmp->typ = TYP_SKIP;
-	  new_blk->mv = tmp;
-	  break;
-	case 5:
-	  if (my_upper(kind[0]) == 'B') {
-	    tmp->typ = TYP_BACK;
-	    tmp->bx = fx;
-	    tmp->by = fy;
-	  } else {
-	    tmp->typ = TYP_FORW;
-	    tmp->fx = fx;
-	    tmp->fy = fy;
-	  }
-	  new_blk->mv = tmp;
-	  break;
-	default:
-	  fprintf(stderr,
-		  "Bug in specifics file!  Skipping short/long entry: %s\n",line);
-	  break;
-	}
-      } else {
-	new_blk->mv = (BlockMV *) NULL;
-      }
-
-      break;
-    case 'V':
-      fprintf(stderr,
-	      "Cannot specify version twice!  Taking first (%d).\n",
-	      version);
-      break;
-    default:
-      printf("What? *%s*\n",line);
-      break;
-    }}
-  
 }
 
 
diff --git a/converter/ppm/ppmtoxpm.c b/converter/ppm/ppmtoxpm.c
index 38d99972..0e316928 100644
--- a/converter/ppm/ppmtoxpm.c
+++ b/converter/ppm/ppmtoxpm.c
@@ -33,6 +33,7 @@
 **    (base 93 not base 28 -> saves a lot of space for colorful xpms)
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/ppm/sldtoppm.c b/converter/ppm/sldtoppm.c
index 550eed5b..2dc049f8 100644
--- a/converter/ppm/sldtoppm.c
+++ b/converter/ppm/sldtoppm.c
@@ -50,7 +50,7 @@ struct spoint {
 
 /* Screen polygon */
 
-struct spolygon { 
+struct spolygon {
     int npoints,              /* Number of points in polygon */
           fill;           /* Fill type */
     struct spoint pt[11];         /* Actual points */
@@ -130,7 +130,7 @@ sli(void) {
 
 /*  SLIB  --  Input byte from slide file  */
 
-static int 
+static int
 slib(void) {
     unsigned char ch;
 
@@ -154,85 +154,127 @@ vscale(int * const px,
 
 
 
+static void
+upcase(const char * const sname,
+       char *       const uname) {
+
+    unsigned int i;
+    const char * ip;
+
+    for (i = 0, ip = sname; i < 31; ++i) {
+        char const ch = *ip++;
+
+        if (ch != EOS)
+            uname[i] = islower(ch) ? toupper(ch) : ch;
+    }
+    uname[i] = EOS;
+}
+
+
+
+static void
+skipBytes(FILE *       const fileP,
+          unsigned int const count) {
+
+    unsigned int i;
+
+    for (i = 0; i < count; ++i)
+        getc(fileP);
+}
+
+
+
+static void
+scanDirectory(FILE *       const slFileP,
+              long         const dirPos,
+              bool         const dirOnly,
+              const char * const uname,
+              bool *       const foundP) {
+/*----------------------------------------------------------------------------
+   Scan the directory at the current position in *slFileP, either listing
+   the directory ('dirOnly' true) or searching for a slide named
+   'uname' ('dirOnly' false).
+
+   'dirPos' is the offset in the file of the directory, i.e. the current
+   position of *slFileP.
+
+   In the latter case, return as *foundP whether the slide name is there.
+-----------------------------------------------------------------------------*/
+    bool found;
+    bool eof;
+    long pos;
+    unsigned char libent[36];
+
+    for (found = false, eof = false, pos = dirPos; !found && !eof; ) {
+        size_t readCt;
+        readCt = fread(libent, 36, 1, slFileP);
+        if (readCt != 1)
+            eof = true;
+        else {
+            /* The directory entry is 32 bytes of NUL-terminated slide name
+               followed by 4 bytes of offset of the next directory entry.
+            */
+            const char * const slideName = (const char *)(&libent[0]);
+            if (pm_strnlen(slideName, 32) == 32)
+                pm_error("Invalid input: slide name field is not "
+                         "nul-terminated");
+            else {
+                if (strlen(slideName) == 0)
+                    eof = true;
+                else {
+                    pos += 36;
+                    if (dirOnly) {
+                        pm_message("  %s", slideName);
+                    } else if (streq(slideName, uname)) {
+                        long const dpos =
+                            (((((libent[35] << 8) | libent[34]) << 8) |
+                              libent[33]) << 8) | libent[32];
+
+                        if ((slFileP == stdin) ||
+                            (fseek(slFileP, dpos, 0) == -1)) {
+
+                            skipBytes(slFileP, dpos - pos);
+                        }
+                        found = true;
+                    }
+                }
+            }
+        }
+    }
+    *foundP = found;
+}
+
 /*  SLIDEFIND  --  Find  a  slide  in  a  library  or,  if  DIRONLY is
            nonzero, print a directory listing of the  library.
            If  UCASEN  is nonzero, the requested slide name is
            converted to upper case. */
 
 static void
-slidefind(const char * const sname,
-          bool         const dironly,
+slidefind(const char * const slideName,
+          bool         const dirOnly,
           bool         const ucasen) {
 
-    char uname[32];
-    unsigned char libent[36];
-    long pos;
+    char uname[32];  /* upper case translation of 'slideName' */
+    char header[32]; /* (supposed) header read from file */
     bool found;
-    bool eof;
 
-    if (dironly)
+    if (dirOnly)
         pm_message("Slides in library:");
     else {
-        unsigned int i;
-        const char * ip;
-        
-        ip = sname; /* initial value */
-        
-        for (i = 0; i < 31; ++i) {
-            char const ch = *ip++;
-            if (ch == EOS)
-                break;
-
-            {
-                char const upperCh =
-                    ucasen && islower(ch) ? toupper(ch) : ch;
-                
-                uname[i] = upperCh;
-            }
-        }
-        uname[i] = EOS;
+        upcase(slideName, uname);
     }
-    
+
     /* Read slide library header and verify. */
-    
-    if ((fread(libent, 32, 1, slfile) != 1) ||
-        (!strneq((char *)libent, "AutoCAD Slide Library 1.0\015\012\32", 32))) {
+
+    if ((fread(header, 32, 1, slfile) != 1) ||
+        (!STRSEQ(header, "AutoCAD Slide Library 1.0\r\n\32"))) {
         pm_error("not an AutoCAD slide library file.");
     }
-    pos = 32;
-    
-    /* Search for a slide with the requested name or list the directory */
-    
-    for (found = false, eof = false; !found && !eof; ) {
-        size_t readCt;
-        readCt = fread(libent, 36, 1, slfile);
-        if (readCt != 1)
-            eof = true;
-        else if (strnlen((char *)libent, 32) == 0)
-            eof = true;
 
-        if (!eof) {
-            pos += 36;
-            if (dironly) {
-                pm_message("  %s", libent);
-            } else if (strneq((char *)libent, uname, 32)) {
-                long dpos;
-
-                dpos = (((((libent[35] << 8) | libent[34]) << 8) |
-                         libent[33]) << 8) | libent[32];
-        
-                if ((slfile == stdin) || (fseek(slfile, dpos, 0) == -1)) {
-                    dpos -= pos;
-
-                    while (dpos-- > 0)
-                        getc(slfile);
-                }
-                found = true;
-            }
-        }
-    }
-    if (!found && !dironly)
-        pm_error("slide '%s' not in library.", sname);
+    scanDirectory(slfile, 32, dirOnly, ucasen ? uname : slideName, &found);
+
+    if (!found && !dirOnly)
+        pm_error("slide '%s' not in library.", slideName);
 }
 
 
@@ -295,14 +337,14 @@ flood(struct spolygon * const poly,
         assert(poly->pt[i].x >= 0 && poly->pt[i].x < pixcols);
         assert(poly->pt[i].y >= 0 && poly->pt[i].y < pixrows);
         ppmd_line(pixels, pixcols, pixrows, pixmaxval,
-                  poly->pt[i].x, iydots - poly->pt[i].y, 
+                  poly->pt[i].x, iydots - poly->pt[i].y,
                   poly->pt[(i + 1) % poly->npoints].x,
                   iydots - poly->pt[(i + 1) % poly->npoints].y,
                   ppmd_fill_drawproc, handle);
     }
     ppmd_fill(pixels, pixcols, pixrows, pixmaxval,
               handle, PPMD_NULLDRAWPROC, (char *) &rgbcolor);
-    
+
     ppmd_fill_destroy(handle);
 }
 
@@ -333,11 +375,11 @@ slider(slvecfn   slvec,
     {"AutoCAD Slide\r\n\32", 86,2, 0,0, 0.0, 0};
     int curcolor = 7;             /* Current vector color */
     pixel rgbcolor;           /* Pixel used to clear pixmap */
-    
+
     lx = ly = 32000;
-    
+
     /* Process the header of the slide file.  */
-    
+
     sdrawkcab = false;            /* Initially guess byte order is OK */
     fread(slfrof.slh, 17, 1, slfile);
     fread(&slfrof.sntype, sizeof(char), 1, slfile);
@@ -361,12 +403,12 @@ slider(slvecfn   slvec,
         pm_error("incompatible slide file format");
 
     /* Build SDSAR value from long scaled version. */
-    
+
     ldsar = 0L;
     for (i = 3; i >= 0; --i)
         ldsar = (ldsar << 8) | ubfr[i];
     slfrof.sdsar = ((double) ldsar) / 1E7;
-    
+
     /* Examine the byte order test value.   If it's backwards, set the
        byte-reversal flag and correct all of the values we've read  in
        so far.
@@ -380,7 +422,7 @@ slider(slvecfn   slvec,
         rshort(slfrof.shwfill);
         #undef rshort
     }
-    
+
     /* Dump the header if we're blithering. */
 
     if (blither || info) {
@@ -455,29 +497,29 @@ slider(slvecfn   slvec,
         }
         iydots = sysize - 1;
     }
-    
+
     if (adjust) {
         pm_message(
             "Resized from %dx%d to %dx%d to correct pixel aspect ratio.",
             slfrof.sxdots + 1, slfrof.sydots + 1, ixdots + 1, iydots + 1);
     }
-    
+
     /* Allocate image buffer and clear it to black. */
-    
+
     pixels = ppm_allocarray(pixcols = ixdots + 1, pixrows = iydots + 1);
     PPM_ASSIGN(rgbcolor, 0, 0, 0);
     ppmd_filledrectangle(pixels, pixcols, pixrows, pixmaxval, 0, 0,
                          pixcols, pixrows, PPMD_NULLDRAWPROC,
                          (char *) &rgbcolor);
-    
+
     if ((rescale = slfrof.sxdots != ixdots ||
          slfrof.sydots != iydots ||
          slfrof.sdsar != dsar) != 0) {
-        
+
         /* Rescale all coords. so they'll look (more or less)
            right on this display.
         */
-        
+
         xfac = (ixdots + 1) * 0x10000L;
         xfac /= (long) (slfrof.sxdots + 1);
         yfac = (iydots + 1) * 0x10000L;
@@ -490,7 +532,7 @@ slider(slvecfn   slvec,
     }
 
     poly.npoints = 0;             /* No flood in progress. */
-    
+
     while ((cw = sli()) != 0xFC00) {
         switch (cw & 0xFF00) {
         case 0xFB00:          /*  Short vector compressed  */
@@ -508,10 +550,10 @@ slider(slvecfn   slvec,
             slx = vec.f.x;        /* Save scaled point */
             sly = vec.f.y;
             break;
-            
+
         case 0xFC00:          /*  End of file  */
             break;
-            
+
         case 0xFD00:          /*  Flood command  */
             vec.f.x = sli();
             vec.f.y = sli();
@@ -538,7 +580,7 @@ slider(slvecfn   slvec,
                 poly.npoints++;
             }
             break;
-            
+
         case 0xFE00:          /*  Common endpoint compressed  */
             vec.f.x = lx + extend(cw & 0xFF);
             vec.f.y = ly + slib();
@@ -553,7 +595,7 @@ slider(slvecfn   slvec,
             slx = vec.f.x;        /* Save scaled point */
             sly = vec.f.y;
             break;
-            
+
         case 0xFF00:          /*  Change color  */
             curcolor = cw & 0xFF;
             break;
@@ -661,7 +703,7 @@ main(int          argc,
     }
 
     /* If a file name is specified, open it.  Otherwise read from
-       standard input. 
+       standard input.
     */
 
     if (argn < argc) {
@@ -670,24 +712,24 @@ main(int          argc,
     } else {
         slfile = stdin;
     }
-    
+
     if (argn != argc) {           /* Extra bogus arguments ? */
         pm_usage(usage);
     }
-    
+
     /* If we're extracting an item from a slide library, position the
        input stream to the start of the chosen slide.
     */
- 
+
     if (dironly || slobber)
         slidefind(slobber, dironly, ucasen);
- 
+
     if (!dironly) {
         slider(draw, flood);
         ppm_writeppm(stdout, pixels, pixcols, pixrows, pixmaxval, 0);
     }
     pm_close(slfile);
     pm_close(stdout);
-    
+
     return 0;
 }
diff --git a/converter/ppm/tgatoppm.c b/converter/ppm/tgatoppm.c
index 95893089..662f741b 100644
--- a/converter/ppm/tgatoppm.c
+++ b/converter/ppm/tgatoppm.c
@@ -12,6 +12,7 @@
 ** implied warranty.
 */
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
@@ -29,7 +30,6 @@ static int mapped, rlencoded;
 
 static pixel ColorMap[MAXCOLORS];
 static gray AlphaMap[MAXCOLORS];
-static int RLE_count = 0, RLE_flag = 0;
 
 struct cmdlineInfo {
     /* All the information the user supplied in the command line,
@@ -78,14 +78,14 @@ parseCommandLine(int argc, char ** argv,
         cmdlineP->input_filename = "-";  /* he wants stdin */
     else if (argc - 1 == 1)
         cmdlineP->input_filename = strdup(argv[1]);
-    else 
+    else
         pm_error("Too many arguments.  The only argument accepted "
                  "is the input file specification");
 
     if (alpha_spec &&
         streq(cmdlineP->alpha_filename, "-"))
         cmdlineP->alpha_stdout = 1;
-    else 
+    else
         cmdlineP->alpha_stdout = 0;
 
     if (!alpha_spec)
@@ -102,89 +102,111 @@ getbyte(FILE * const ifP) {
 
     if ( fread( (char*) &c, 1, 1, ifP ) != 1 )
         pm_error( "EOF / read error" );
-    
+
     return c;
 }
 
 
 
+static int RLE_count = 0, RLE_flag = 0;
+
+
+
 static void
-get_pixel(FILE * const ifP, pixel * dest, int Size, gray *alpha_p) {
+handleRun(FILE * const ifP,
+          bool * const repeatP) {
+
+    if (rlencoded) {
+        if (RLE_count == 0) {
+            /* Have to restart run. */
+            unsigned char i;
+            i = getbyte(ifP);
+            RLE_flag = (i & 0x80);
+            if (RLE_flag == 0) {
+                /* Stream of unencoded pixels. */
+                RLE_count = i + 1;
+            } else {
+                /* Single pixel replicated. */
+                RLE_count = i - 127;
+            }
+            /* Decrement count & get pixel. */
+            --RLE_count;
+            *repeatP = false;
+        } else {
+            /* Have already read count & (at least) first pixel. */
+            --RLE_count;
+            if (RLE_flag != 0) {
+                /* Replicated pixels. */
+                *repeatP = true;
+            } else
+                *repeatP = false;
+        }
+    } else
+        *repeatP = false;
+}
 
-    static pixval Red, Grn, Blu;
-    static pixval Alpha;
-    unsigned char j, k;
+
+
+static void
+getPixel(FILE *  const ifP,
+         pixel * const dest,
+         int     const size,
+         gray *  const alphaP) {
+
+    static pixval red, grn, blu;
+    static pixval alpha;
     static unsigned int l;
+    unsigned char j, k;
+    bool repeat;
+        /* Next pixel is just a repeat (from an encoded run) */
 
-    /* Check if run length encoded. */
-    if ( rlencoded )
-	{
-	if ( RLE_count == 0 )
-	    { /* Have to restart run. */
-	    unsigned char i;
-	    i = getbyte( ifP );
-	    RLE_flag = ( i & 0x80 );
-	    if ( RLE_flag == 0 )
-		/* Stream of unencoded pixels. */
-		RLE_count = i + 1;
-	    else
-		/* Single pixel replicated. */
-		RLE_count = i - 127;
-	    /* Decrement count & get pixel. */
-	    --RLE_count;
-	    }
-	else
-	    { /* Have already read count & (at least) first pixel. */
-	    --RLE_count;
-	    if ( RLE_flag != 0 )
-		/* Replicated pixels. */
-		goto PixEncode;
-	    }
-	}
-    /* Read appropriate number of bytes, break into RGB. */
-    switch ( Size )
-	{
-	case 8:				/* Grayscale, read and triplicate. */
-	Red = Grn = Blu = l = getbyte( ifP );
-    Alpha = 0;
-	break;
-
-	case 16:			/* 5 bits each of red green and blue. */
-	case 15:			/* Watch byte order. */
-	j = getbyte( ifP );
-	k = getbyte( ifP );
-	l = ( (unsigned int) k << 8 ) + j;
-	Red = ( k & 0x7C ) >> 2;
-	Grn = ( ( k & 0x03 ) << 3 ) + ( ( j & 0xE0 ) >> 5 );
-	Blu = j & 0x1F;
-    Alpha = 0;
-	break;
-
-	case 32:            /* 8 bits each of blue, green, red, and alpha */
-	case 24:			/* 8 bits each of blue, green, and red. */
-	Blu = getbyte( ifP );
-	Grn = getbyte( ifP );
-	Red = getbyte( ifP );
-	if ( Size == 32 )
-	    Alpha = getbyte( ifP );
-    else
-        Alpha = 0;
-	l = 0;
-	break;
+    handleRun(ifP, &repeat);
 
-	default:
-	pm_error( "unknown pixel size (#2) - %d", Size );
-	}
+    if (repeat) {
+        /* Use red, grn, blu, alpha, and l from prior call to getPixel */
+    } else {
+        /* Read appropriate number of bytes, break into RGB. */
+        switch (size) {
+        case 8:             /* Grayscale, read and triplicate. */
+            red = grn = blu = l = getbyte(ifP);
+            alpha = 0;
+            break;
 
-PixEncode:
-    if ( mapped ) {
+        case 16:            /* 5 bits each of red green and blue. */
+        case 15:            /* Watch byte order. */
+            j = getbyte(ifP);
+            k = getbyte(ifP);
+            l = ((unsigned int)k << 8) + j;
+            red = (k & 0x7C) >> 2;
+            grn = ((k & 0x03) << 3) + ((j & 0xE0) >> 5);
+            blu = j & 0x1F;
+            alpha = 0;
+            break;
+
+        case 32:            /* 8 bits each of blue, green, red, and alpha */
+        case 24:            /* 8 bits each of blue, green, and red. */
+            blu = getbyte(ifP);
+            grn = getbyte(ifP);
+            red = getbyte(ifP);
+            if (size == 32)
+                alpha = getbyte(ifP);
+            else
+                alpha = 0;
+            l = 0;
+            break;
+
+        default:
+            pm_error("unknown pixel size (#2) - %d", size);
+        }
+    }
+    if (mapped) {
         *dest = ColorMap[l];
-        *alpha_p = AlphaMap[l];
+        *alphaP = AlphaMap[l];
     } else {
-        PPM_ASSIGN( *dest, Red, Grn, Blu );
-        *alpha_p = Alpha;
-    }
+        PPM_ASSIGN(*dest, red, grn, blu);
+        *alphaP = alpha;
     }
+}
 
 
 
@@ -216,7 +238,7 @@ readtga(FILE * const ifP, struct ImageHeader * tgaP) {
     tgaP->Rsrvd = ( flags & 0x10 ) >> 4;
     tgaP->OrgBit = ( flags & 0x20 ) >> 5;
     tgaP->IntrLve = ( flags & 0xc0 ) >> 6;
-    
+
     if ( tgaP->IdLength != 0 )
         fread( junk, 1, (int) tgaP->IdLength, ifP );
 }
@@ -230,14 +252,14 @@ get_map_entry(FILE * const ifP, pixel * Value, int Size, gray * Alpha) {
 
     /* Read appropriate number of bytes, break into rgb & put in map. */
     switch ( Size )
-	{
-	case 8:				/* Grayscale, read and triplicate. */
+    {
+    case 8:             /* Grayscale, read and triplicate. */
         r = g = b = getbyte( ifP );
         a = 0;
         break;
-        
-	case 16:			/* 5 bits each of red green and blue. */
-	case 15:			/* Watch for byte order. */
+
+    case 16:            /* 5 bits each of red green and blue. */
+    case 15:            /* Watch for byte order. */
         j = getbyte( ifP );
         k = getbyte( ifP );
         r = ( k & 0x7C ) >> 2;
@@ -245,21 +267,21 @@ get_map_entry(FILE * const ifP, pixel * Value, int Size, gray * Alpha) {
         b = j & 0x1F;
         a = 0;
         break;
-        
-	case 32:            /* 8 bits each of blue, green, red, and alpha */
-	case 24:			/* 8 bits each of blue green and red. */
+
+    case 32:            /* 8 bits each of blue, green, red, and alpha */
+    case 24:            /* 8 bits each of blue green and red. */
         b = getbyte( ifP );
         g = getbyte( ifP );
         r = getbyte( ifP );
         if ( Size == 32 )
             a = getbyte( ifP );
-        else 
+        else
             a = 0;
         break;
-        
-	default:
+
+    default:
         pm_error( "unknown colormap pixel size (#2) - %d", Size );
-	}
+    }
     PPM_ASSIGN( *Value, r, g, b );
     *Alpha = a;
 }
@@ -325,84 +347,89 @@ main(int argc, char * argv[]) {
 
     if (cmdline.alpha_stdout)
         alpha_file = stdout;
-    else if (cmdline.alpha_filename == NULL) 
+    else if (cmdline.alpha_filename == NULL)
         alpha_file = NULL;
     else
         alpha_file = pm_openw(cmdline.alpha_filename);
-    
-    if (cmdline.alpha_stdout) 
+
+    if (cmdline.alpha_stdout)
         imageout_file = NULL;
     else
         imageout_file = stdout;
-    
+
     /* Read the Targa file header. */
     readtga(ifP, &tga_head);
 
-    if (cmdline.headerdump) 
+    if (cmdline.headerdump)
         dumpHeader(tga_head);
 
     rows = ((int) tga_head.Height_lo) + ((int) tga_head.Height_hi) * 256;
     cols = ((int) tga_head.Width_lo)  + ((int) tga_head.Width_hi)  * 256;
 
     switch (tga_head.ImgType) {
-	case TGA_Map:
-	case TGA_RGB:
-	case TGA_Mono:
-	case TGA_RLEMap:
-	case TGA_RLERGB:
-	case TGA_RLEMono:
+    case TGA_Map:
+    case TGA_RGB:
+    case TGA_Mono:
+    case TGA_RLEMap:
+    case TGA_RLERGB:
+    case TGA_RLEMono:
+        break;
+    case TGA_CompMap:
+    case TGA_CompMap4:
+        pm_error("Targa image type %d (compressed color-mapped data). "
+                 "Cannot handle this format.", tga_head.ImgType);
         break;
-	default:
+    default:
         pm_error("unknown Targa image type %d", tga_head.ImgType);
-	}
-    
+    }
+
     if (tga_head.ImgType == TGA_Map ||
         tga_head.ImgType == TGA_RLEMap ||
         tga_head.ImgType == TGA_CompMap ||
         tga_head.ImgType == TGA_CompMap4)
-	{ /* Color-mapped image */
+    { /* Color-mapped image */
         if (tga_head.CoMapType != 1)
-            pm_error( 
+            pm_error(
                 "mapped image (type %d) with color map type != 1",
                 tga_head.ImgType );
         mapped = true;
         /* Figure maxval from CoSize. */
         switch (tga_head.CoSize) {
-	    case 8:
-	    case 24:
-	    case 32:
+        case 8:
+        case 24:
+        case 32:
             maxval = 255;
             break;
 
-	    case 15:
-	    case 16:
+        case 15:
+        case 16:
             maxval = 31;
             break;
 
-	    default:
-	    pm_error(
-		"unknown colormap pixel size - %d", tga_head.CoSize );
-	    }
-	} else { 
+        default:
+        pm_error(
+        "unknown colormap pixel size - %d", tga_head.CoSize );
+        }
+    } else {
         /* Not colormap, so figure maxval from PixelSize. */
         mapped = false;
         switch ( tga_head.PixelSize ) {
-	    case 8:
-	    case 24:
-	    case 32:
+        case 8:
+        case 24:
+        case 32:
             maxval = 255;
             break;
-            
-	    case 15:
-	    case 16:
+
+        case 15:
+        case 16:
             maxval = 31;
             break;
-            
-	    default:
+
+        default:
             pm_error("unknown pixel size - %d", tga_head.PixelSize);
-	    }
-	}
-    
+        }
+    }
+
     /* If required, read the color map information. */
     if ( tga_head.CoMapType != 0 ) {
         unsigned int i;
@@ -415,7 +442,7 @@ main(int argc, char * argv[]) {
         for (i = temp1; i < (temp1 + temp2); ++i)
             get_map_entry(ifP, &ColorMap[i], (int) tga_head.CoSize,
                           &AlphaMap[i]);
-	}
+    }
 
     /* Check run-length encoding. */
     if (tga_head.ImgType == TGA_RLEMap ||
@@ -424,7 +451,7 @@ main(int argc, char * argv[]) {
         rlencoded = 1;
     else
         rlencoded = 0;
-    
+
     /* Read the Targa file body and convert to portable format. */
     pixels = ppm_allocarray( cols, rows );
     alpha = pgm_allocarray( cols, rows );
@@ -436,10 +463,10 @@ main(int argc, char * argv[]) {
         realrow = truerow;
         if (tga_head.OrgBit == 0)
             realrow = rows - realrow - 1;
-        
+
         for (col = 0; col < cols; ++col)
-            get_pixel(ifP, &(pixels[realrow][col]), (int) tga_head.PixelSize,
-                      &(alpha[realrow][col]));
+            getPixel(ifP, &(pixels[realrow][col]), (int) tga_head.PixelSize,
+                     &(alpha[realrow][col]));
         if (tga_head.IntrLve == TGA_IL_Four)
             truerow += 4;
         else if (tga_head.IntrLve == TGA_IL_Two)
@@ -448,17 +475,20 @@ main(int argc, char * argv[]) {
             ++truerow;
         if (truerow >= rows)
             truerow = ++baserow;
-	}
+    }
     pm_close(ifP);
-    
-    if (imageout_file) 
+
+    if (imageout_file)
         ppm_writeppm(imageout_file, pixels, cols, rows, (pixval) maxval, 0);
     if (alpha_file)
         pgm_writepgm(alpha_file, alpha, cols, rows, (pixval) maxval, 0);
-    if (imageout_file) 
+    if (imageout_file)
         pm_close(imageout_file);
     if (alpha_file)
         pm_close(alpha_file);
 
     return 0;
 }
+
+
+
diff --git a/converter/ppm/winicontoppm.c b/converter/ppm/winicontoppm.c
index 6b1376b2..2c9015f5 100644
--- a/converter/ppm/winicontoppm.c
+++ b/converter/ppm/winicontoppm.c
@@ -14,6 +14,7 @@
 ** 03/2003 - Added 24+32 bpp capability.
 */
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/ppm/ximtoppm.c b/converter/ppm/ximtoppm.c
index ce5e6396..75faac69 100644
--- a/converter/ppm/ximtoppm.c
+++ b/converter/ppm/ximtoppm.c
@@ -10,6 +10,7 @@
 ** implied warranty.
 */
 
+#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
diff --git a/converter/ppm/xpmtoppm.c b/converter/ppm/xpmtoppm.c
index 27f17931..46101a09 100644
--- a/converter/ppm/xpmtoppm.c
+++ b/converter/ppm/xpmtoppm.c
@@ -3,6 +3,7 @@
    Copyright and history information is at end of file
 */
 
+#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
 #define _BSD_SOURCE   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
@@ -81,14 +82,14 @@ parseCommandLine(int argc, char ** argv,
         cmdlineP->input_filespec = NULL;  /* he wants stdin */
     else if (argc - 1 == 1)
         cmdlineP->input_filespec = strdup(argv[1]);
-    else 
+    else
         pm_error("Too many arguments.  The only argument accepted\n"
                  "is the input file specification");
 
-    if (cmdlineP->alpha_filename && 
+    if (cmdlineP->alpha_filename &&
         streq(cmdlineP->alpha_filename, "-"))
         cmdlineP->alpha_stdout = TRUE;
-    else 
+    else
         cmdlineP->alpha_stdout = FALSE;
 
 }
@@ -223,7 +224,7 @@ entryMatch(struct ColorNameHashTableEntry const entry,
 
     {
         unsigned int i;
-        
+
         for (i = 0; i < size; ++i) {
             if (name[i] != entry.colorName[i])
                 return false;
@@ -271,7 +272,7 @@ hash_find(const ColorNameHash *             const hashP,
     for (i = initialIndex;
          !entryMatch(hashP->table[i], name, hashP->nameSize);
          bumpIndex(&i, hashP->size, initialIndex));
-         
+
     *entryPP = &hashP->table[i];
 }
 
@@ -357,7 +358,7 @@ getLine(char * const line,
 
    If 'backup' is true, the "next line" is the previously read line, i.e.
    the one in that one-line buffer.  Otherwise, the "next line" is the next
-   line from the real file.  After reading the backed up line, we reset 
+   line from the real file.  After reading the backed up line, we reset
    'backup' to false.
 
    Return the line as a null terminated string in *line, which is an
@@ -370,7 +371,7 @@ getLine(char * const line,
                  "which is out of bounds");
 
     if (backup) {
-        strncpy(line, lastInputLine, size); 
+        strncpy(line, lastInputLine, size);
         backup = FALSE;
     } else {
         if (fgets(line, size, stream) == NULL)
@@ -397,7 +398,7 @@ getword(char * const output, char ** const cursorP) {
         strncpy(output, t1, t2 - t1);
     output[t2 - t1] = '\0';
     *cursorP = t2;
-}    
+}
 
 
 
@@ -437,7 +438,7 @@ validateColorName(const char * const name,
 
 static void
 interpretXpm3ColorTableLine(char               const line[],
-                            unsigned int       const seqNum, 
+                            unsigned int       const seqNum,
                             unsigned int       const charsPerPixel,
                             ColorNameHash *    const hashP) {
 /*----------------------------------------------------------------------------
@@ -457,22 +458,22 @@ interpretXpm3ColorTableLine(char               const line[],
 -----------------------------------------------------------------------------*/
     /* Note: this code seems to allow for multi-word color specifications,
        but I'm not aware that such are legal.  Ultimately, ppm_parsecolor()
-       interprets the name, and I believe it takes only single word 
+       interprets the name, and I believe it takes only single word
        color specifications.  -Bryan 2001.05.06.
     */
-    char str2[MAX_LINE+1];    
+    char str2[MAX_LINE+1];
     char * t1;
     char * t2;
     int endOfEntry;   /* boolean */
-    
+
     unsigned int curkey, key, highkey;  /* current color key */
-    bool lastwaskey;    
+    bool lastwaskey;
         /* The last token we processes was a key, and we have processed
            at least one token.
         */
     char curbuf[BUFSIZ];        /* current buffer */
     bool isTransparent;
-    
+
     const char * colorName;
         /* The 0-3 character name this color map line gives the color
            (i.e. the name that the raster uses).  This is NOT NUL-terminated.
@@ -484,20 +485,20 @@ interpretXpm3ColorTableLine(char               const line[],
     if (t1 == NULL)
         pm_error("A line that is supposed to be an entry in the color "
                  "table does not start with a quote.  The line is '%s'.  "
-                 "It is the %uth entry in the color table.", 
+                 "It is the %uth entry in the color table.",
                  line, seqNum);
     else
         ++t1;  /* Points now to first color number character */
-    
+
     validateColorName(t1, charsPerPixel);
     colorName = t1;
 
     t1 += charsPerPixel;
 
     /*
-     * read color keys and values 
+     * read color keys and values
      */
-    curkey = 0; 
+    curkey = 0;
     highkey = 1;
     lastwaskey = FALSE;
     t2 = t1;
@@ -511,8 +512,8 @@ interpretXpm3ColorTableLine(char               const line[],
             /* See if the word we got is a valid key (and get its key
                number if so)
             */
-            for (key = 1; 
-                 key <= NKEYS && !streq(xpmColorKeys[key - 1], str2); 
+            for (key = 1;
+                 key <= NKEYS && !streq(xpmColorKeys[key - 1], str2);
                  key++);
             isKey = (key <= NKEYS);
 
@@ -520,21 +521,21 @@ interpretXpm3ColorTableLine(char               const line[],
                 /* This word is a color specification (or "none" for
                    transparent).
                 */
-                if (!curkey) 
+                if (!curkey)
                     pm_error("Missing color key token in color table line "
                              "'%s' before '%s'.", line, str2);
-                if (!lastwaskey) 
+                if (!lastwaskey)
                     strcat(curbuf, " ");        /* append space */
-                if ( (strneq(str2, "None", 4)) 
+                if ( (strneq(str2, "None", 4))
                      || (strneq(str2, "none", 4)) ) {
                     /* This entry identifies the transparent color number */
                     strcat(curbuf, "#000000");  /* Make it black */
                     isTransparent = TRUE;
-                } else 
+                } else
                     strcat(curbuf, str2);       /* append buf */
                 lastwaskey = FALSE;
-            } else { 
-                /* This word is a key.  So we've seen the last of the 
+            } else {
+                /* This word is a key.  So we've seen the last of the
                    info for the previous key, and we must either put it
                    in the color map or ignore it if we already have a higher
                    color form in the colormap for this colormap entry.
@@ -560,7 +561,7 @@ interpretXpm3ColorTableLine(char               const line[],
         addToColorMap(hashP, colorName, curbuf, isTransparent);
         highkey = curkey;
     }
-    if (highkey == 1) 
+    if (highkey == 1)
         pm_error("C error scanning color table");
 }
 
@@ -587,10 +588,10 @@ readV3ColorTable(FILE *             const ifP,
         /* skip the comment line if any */
         if (strneq(line, "/*", 2))
             getLine(line, sizeof(line), ifP);
-            
+
         interpretXpm3ColorTableLine(line, seqNum, charsPerPixel,
                                     colorNameHashP);
-                                    
+
     }
     *colorNameHashPP = colorNameHashP;
 }
@@ -600,7 +601,7 @@ readV3ColorTable(FILE *             const ifP,
 static void
 readXpm3Header(FILE *             const ifP,
                unsigned int *     const widthP,
-               unsigned int *     const heightP, 
+               unsigned int *     const heightP,
                unsigned int *     const charsPerPixelP,
                ColorNameHash **   const colorNameHashPP) {
 /*----------------------------------------------------------------------------
@@ -619,14 +620,14 @@ readXpm3Header(FILE *             const ifP,
 -----------------------------------------------------------------------------*/
     char line[MAX_LINE+1];
     const char * xpm3_signature = "/* XPM */";
-    
+
     unsigned int width, height;
     unsigned int nColors;
     unsigned int charsPerPixel;
 
     /* Read the XPM signature comment */
     getLine(line, sizeof(line), ifP);
-    if (!strneq(line, xpm3_signature, strlen(xpm3_signature))) 
+    if (!strneq(line, xpm3_signature, strlen(xpm3_signature)))
         pm_error("Apparent XPM 3 file does not start with '/* XPM */'.  "
                  "First line is '%s'", xpm3_signature);
 
@@ -715,7 +716,7 @@ readV1ColorTable(FILE *           const ifP,
 static void
 readXpm1Header(FILE *           const ifP,
                unsigned int *   const widthP,
-               unsigned int *   const heightP, 
+               unsigned int *   const heightP,
                unsigned int *   const charsPerPixelP,
                ColorNameHash ** const colorNameHashPP) {
 /*----------------------------------------------------------------------------
@@ -723,11 +724,11 @@ readXpm1Header(FILE *           const ifP,
   getLine() stream is presently positioned to the beginning of the
   file and it is a Version 1 XPM file.  Leave the stream positioned
   after the header.
-  
+
   Return the information from the header the same as for readXpm3Header.
 -----------------------------------------------------------------------------*/
     int format, v;
-    bool processedStaticChar;  
+    bool processedStaticChar;
         /* We have read up to and interpreted the "static char..." line */
     char * t1;
     unsigned int nColors;
@@ -875,7 +876,7 @@ convertRow(char                  const line[],
                    "'%s'.  Ignoring this line.", line);
     } else {
         unsigned int col;
-    
+
         ++lineCursor; /* Skip to first character after quote */
 
         /* Handle pixels until a close quote, eol, or we've returned all
@@ -889,7 +890,7 @@ convertRow(char                  const line[],
 
             alpharow[col] = hash_isTransparent(colorNameHashP, lineCursor) ?
                 PBM_BLACK : PBM_WHITE;
-            
+
             lineCursor += charsPerPixel;
         }
         if (*lineCursor != '"')
@@ -902,7 +903,7 @@ convertRow(char                  const line[],
 static void
 convertRaster(FILE *                const ifP,
               unsigned int          const cols,
-              unsigned int          const rows, 
+              unsigned int          const rows,
               unsigned int          const charsPerPixel,
               const ColorNameHash * const colorNameHashP,
               FILE *                const imageOutFileP,
@@ -927,7 +928,7 @@ convertRaster(FILE *                const ifP,
         bool haveLine;
 
         for (haveLine = false; !haveLine; ) {
-            getLine(line, sizeof(line), ifP); 
+            getLine(line, sizeof(line), ifP);
 
             if (strneq(line, "/*", 2)) {
                 /* It's a comment.  Ignore it. */
@@ -938,22 +939,22 @@ convertRaster(FILE *                const ifP,
                    pixrow, alpharow);
 
         if (imageOutFileP)
-            ppm_writeppmrow(imageOutFileP, 
+            ppm_writeppmrow(imageOutFileP,
                             pixrow, cols, PPM_MAXMAXVAL, 0);
-            if (alphaOutFileP)
-                pbm_writepbmrow(alphaOutFileP, alpharow, cols, 0);
+        if (alphaOutFileP)
+            pbm_writepbmrow(alphaOutFileP, alpharow, cols, 0);
     }
 
     pbm_freerow(alpharow);
     ppm_freerow(pixrow);
 }
- 
+
 
 
 static void
 readXpmHeader(FILE *           const ifP,
               unsigned int *   const widthP,
-              unsigned int *   const heightP, 
+              unsigned int *   const heightP,
               unsigned int *   const charsPerPixelP,
               ColorNameHash ** const colorNameHashPP) {
 /*----------------------------------------------------------------------------
@@ -973,7 +974,7 @@ readXpmHeader(FILE *           const ifP,
     /* Read the header line */
     getLine(line, sizeof(line), ifP);
     backup = TRUE;  /* back up so next read reads this line again */
-    
+
     rc = sscanf(line, "/* %s */", str1);
     if (rc == 1 && strneq(str1, "XPM", 3)) {
         /* It's an XPM version 3 file */
@@ -986,7 +987,7 @@ readXpmHeader(FILE *           const ifP,
     *heightP        = height;
     *charsPerPixelP = charsPerPixel;
 }
- 
+
 
 
 int
@@ -1007,20 +1008,20 @@ main(int argc, char *argv[]) {
 
     verbose = cmdline.verbose;
 
-    if ( cmdline.input_filespec != NULL ) 
+    if ( cmdline.input_filespec != NULL )
         ifP = pm_openr( cmdline.input_filespec);
     else
         ifP = stdin;
 
     if (cmdline.alpha_stdout)
         alphaOutFileP = stdout;
-    else if (cmdline.alpha_filename == NULL) 
+    else if (cmdline.alpha_filename == NULL)
         alphaOutFileP = NULL;
     else {
         alphaOutFileP = pm_openw(cmdline.alpha_filename);
     }
 
-    if (cmdline.alpha_stdout) 
+    if (cmdline.alpha_stdout)
         imageOutFileP = NULL;
     else
         imageOutFileP = stdout;
@@ -1035,7 +1036,7 @@ main(int argc, char *argv[]) {
 
     convertRaster(ifP, cols, rows, charsPerPixel, colorNameHashP,
                   imageOutFileP, alphaOutFileP);
-    
+
     pm_close(ifP);
     if (imageOutFileP)
         pm_close(imageOutFileP);
@@ -1043,7 +1044,7 @@ main(int argc, char *argv[]) {
         pm_close(alphaOutFileP);
 
     hash_destroy(colorNameHashP);
-    
+
     return 0;
 }
 
@@ -1065,17 +1066,17 @@ main(int argc, char *argv[]) {
 **   Tue Apr 9 1991
 **
 ** Rainer Sinkwitz sinkwitz@ifi.unizh.ch - 21 Nov 91:
-**  - Bug fix, no advance of read ptr, would not read 
-**    colors like "ac c black" because it would find 
+**  - Bug fix, no advance of read ptr, would not read
+**    colors like "ac c black" because it would find
 **    the "c" of "ac" and then had problems with "c"
 **    as color.
-**    
+**
 **  - Now understands multiword X11 color names
-**  
+**
 **  - Now reads multiple color keys. Takes the color
 **    of the hightest available key. Lines no longer need
 **    to begin with key 'c'.
-**    
+**
 **  - expanded line buffer to from 500 to 2048 for bigger files
 */
 
diff --git a/converter/ppm/xvminitoppm.c b/converter/ppm/xvminitoppm.c
index d76bea87..ad207ae9 100644
--- a/converter/ppm/xvminitoppm.c
+++ b/converter/ppm/xvminitoppm.c
@@ -13,12 +13,13 @@
 #include <assert.h>
 #include <string.h>
 #include <errno.h>
+#include <stdbool.h>
 
 #include "pm_c_util.h"
 #include "nstring.h"
+#include "pm.h"
 #include "ppm.h"
 
-#define BUFSIZE 256
 
 
 typedef struct xvPalette {
@@ -53,25 +54,6 @@ parseCommandLine(int const argc,
 
 
 static void
-getLine(FILE * const ifP,
-        char * const buf,
-        size_t const size) {
-
-    char * rc;
-
-    rc = fgets(buf, size, ifP);
-    if (rc == NULL) {
-        if (ferror(ifP))
-            pm_error("read error.  fgets() failed, errno=%d (%s)",
-                     errno, strerror(errno));
-        else
-            pm_error("unexpected EOF");
-    }
-}
-
-
-
-static void
 makeXvPalette(xvPalette * const xvPaletteP) {
 
     unsigned int paletteIndex;
@@ -101,37 +83,53 @@ readXvHeader(FILE *         const ifP,
              unsigned int * const colsP,
              unsigned int * const rowsP,
              unsigned int * const maxvalP) {
-           
-    char buf[256];
+
+    char * buf;
+    size_t bufferSz;
+    int eof;
+    size_t lineLen;
     unsigned int cols, rows, maxval;
     int rc;
     bool endOfComments;
-    
-    getLine(ifP, buf, sizeof(buf));
 
-    if (!strneq(buf, "P7 332", 6))
+    buf = NULL;   /* initial value */
+    bufferSz = 0; /* initial value */
+
+    pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen);
+
+    if (eof || !strneq(buf, "P7 332", 6))
         pm_error("Input is not a XV thumbnail picture.  It does not "
                  "begin with the characters 'P7 332'.");
 
-    endOfComments = FALSE;
-    while (!endOfComments) {
-        getLine(ifP, buf, sizeof(buf));
+    for (endOfComments = false; !endOfComments; ) {
+        int eof;
+        size_t lineLen;
+        pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen);
+        if (eof)
+            pm_error("EOF before #END_OF_COMMENTS line");
         if (strneq(buf, "#END_OF_COMMENTS", 16))
-            endOfComments = TRUE;
+            endOfComments = true;
         else if (strneq(buf, "#BUILTIN", 8))
             pm_error("This program does not know how to "
                      "convert builtin XV thumbnail pictures");
     }
-    getLine(ifP, buf, sizeof(buf));
+    pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen);
+    if (eof)
+        pm_error("EOF where cols/rows/maxval line expected");
+
     rc = sscanf(buf, "%u %u %u", &cols, &rows, &maxval);
     if (rc != 3)
         pm_error("error parsing dimension info '%s'.  "
                  "It does not consist of 3 decimal numbers.", buf);
     if (maxval != 255)
         pm_error("bogus XV thumbnail maxval %u.  Should be 255", maxval);
+
     *colsP = cols;
     *rowsP = rows;
     *maxvalP = maxval;
+
+    if (buf)
+        free(buf);
 }
 
 
@@ -166,7 +164,7 @@ writePpm(FILE *             const ifP,
             else {
                 unsigned int const paletteIndex = byte;
                 assert(byte >= 0);
-                
+
                 PPM_ASSIGN(pixrow[col],
                            xvPaletteP->red[paletteIndex],
                            xvPaletteP->grn[paletteIndex],
@@ -181,7 +179,7 @@ writePpm(FILE *             const ifP,
 
 
 
-int 
+int
 main(int    argc,
      char * argv[]) {
 
@@ -190,7 +188,7 @@ main(int    argc,
     unsigned int cols, rows;
     pixval maxval;
     xvPalette xvPalette;
- 
+
     ppm_init(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
diff --git a/converter/ppm/yuvtoppm.c b/converter/ppm/yuvtoppm.c
index 151ff9f9..9c5d79c4 100644
--- a/converter/ppm/yuvtoppm.c
+++ b/converter/ppm/yuvtoppm.c
@@ -45,6 +45,7 @@ parseCommandLine(int argc, const char ** argv,
     optEntry * option_def;
         /* Instructions to OptParseOptions3 on how to parse our options */
     optStruct3 opt;
+    unsigned int option_def_index;
 
     MALLOCARRAY_NOFAIL(option_def, 100);
 
diff --git a/converter/tga.h b/converter/tga.h
index fdbe4047..1a65663d 100644
--- a/converter/tga.h
+++ b/converter/tga.h
@@ -44,3 +44,16 @@ typedef char ImageIDField[IMAGEIDFIELDMAXSIZE];
 #define TGA_IL_Four 2
 
 enum TGAbaseImageType {TGA_MAP_TYPE, TGA_MONO_TYPE, TGA_RGB_TYPE};
+
+enum TGAmapType {
+    /* A type of Targa color map */
+    TGA_MAPTYPE_SHORT,
+        /* Each entry in the color map is 2 bytes: 5 bits each of
+           red, green, and blue, and one optional alpha bit.
+        */
+    TGA_MAPTYPE_LONG
+        /* Each entry in the color map is 3 bytes: red, green, and blue,
+           plus one optional alpha byte
+        */
+};
+