about summary refs log tree commit diff
path: root/converter/other/xwdtopnm.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/other/xwdtopnm.c')
-rw-r--r--converter/other/xwdtopnm.c337
1 files changed, 187 insertions, 150 deletions
diff --git a/converter/other/xwdtopnm.c b/converter/other/xwdtopnm.c
index a99768b8..5f8a56c9 100644
--- a/converter/other/xwdtopnm.c
+++ b/converter/other/xwdtopnm.c
@@ -39,14 +39,14 @@
 #include "x10wd.h"
 #include "x11wd.h"
 
-struct compMask {
+struct CompMask {
     unsigned long red;
     unsigned long grn;
     unsigned long blu;
 };
 
 
-struct cmdlineInfo {
+struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
@@ -62,41 +62,40 @@ static bool debug;
 static bool verbose;
 
 #ifdef DEBUG_PIXEL
-static unsigned int pixel_count = 0;
+static unsigned int pixelCount = 0;
 #endif
 
 /* Byte-swapping junk. */
 
 static int
-zero_bits(const unsigned long mask) {
+zeroBits(unsigned long const mask) {
 /*----------------------------------------------------------------------------
    Return the number of consecutive zero bits at the least significant end
    of the binary representation of 'mask'.  E.g. if mask == 0x00fff800,
    we would return 11.
 -----------------------------------------------------------------------------*/
-    int i;
-    unsigned long shifted_mask;
+    unsigned int i;
+    unsigned long shiftedMask;
+
+    for (i = 0, shiftedMask = mask;
+         i < sizeof(mask)*8 && (shiftedMask & 0x00000001) == 0;
+         ++i, shiftedMask >>= 1 );
 
-    for (i=0, shifted_mask = mask;
-         i < sizeof(mask)*8 && (shifted_mask & 0x00000001) == 0;
-         i++, shifted_mask >>= 1 );
-    return(i);
+    return i;
 }
 
 
 
 static void
-parseCommandLine(int argc, char ** argv,
-                 struct cmdlineInfo * const cmdlineP) {
+parseCommandLine(int argc, const char ** argv,
+                 struct CmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    Note that many of the strings that this function returns in the
    *cmdline_p structure are actually in the supplied argv array.  And
    sometimes, one of these strings is actually just a suffix of an entry
    in argv!
 -----------------------------------------------------------------------------*/
-    optEntry * option_def;
-        /* Instructions to OptParseOptions3 on how to parse our options.
-         */
+    optEntry * option_def;   /* Used by OPTENT3 */
     optStruct3 opt;
 
     unsigned int option_def_index;
@@ -114,7 +113,7 @@ parseCommandLine(int argc, char ** argv,
     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_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
     if (argc - 1 == 0)
@@ -125,7 +124,7 @@ parseCommandLine(int argc, char ** argv,
         else
             cmdlineP->inputFilename = strdup(argv[1]);
     } else
-        pm_error("Too many arguments.  The only argument accepted\n"
+        pm_error("Too many arguments.  The only argument accepted "
                  "is the input file specification");
 }
 
@@ -141,19 +140,24 @@ processX10Header(X10WDFileHeader *  const h10P,
                  enum visualclass * const visualclassP,
                  int *              const formatP,
                  xel **             const colorsP,
-                 int *              const bits_per_pixelP,
-                 int *              const bits_per_itemP,
-                 struct compMask *  const compMaskP,
-                 enum byteorder *   const byte_orderP,
-                 enum byteorder *   const bit_orderP) {
-
-    int i;
-    X10Color* x10colors;
+                 int *              const bitsPerPixelP,
+                 int *              const bitsPerItemP,
+                 struct CompMask *  const compMaskP,
+                 enum byteorder *   const byteOrderP,
+                 enum byteorder *   const bitOrderP) {
+/*----------------------------------------------------------------------------
+  *h10P is a mapping of the raw bytes of the header.  Interpret and validate
+  it and return the information as *colsP, etc.
+
+  As a side effect, destroy *h10P.
+-----------------------------------------------------------------------------*/
+    unsigned int i;
+    X10Color * x10colors;
     bool grayscale;
-    bool byte_swap;
+    bool bytesSwapped;
 
     if (h10P->file_version != X10WD_FILE_VERSION) {
-        byte_swap = TRUE;
+        bytesSwapped = true;
         h10P->header_size     = pm_bs_long(h10P->header_size);
         h10P->file_version    = pm_bs_long(h10P->file_version);
         h10P->display_type    = pm_bs_long(h10P->display_type);
@@ -168,7 +172,7 @@ processX10Header(X10WDFileHeader *  const h10P,
         h10P->window_bdrwidth = pm_bs_short(h10P->window_bdrwidth);
         h10P->window_ncolors  = pm_bs_short(h10P->window_ncolors);
     } else
-        byte_swap = FALSE;
+        bytesSwapped = false;
 
     for (i = 0; i < h10P->header_size - sizeof(*h10P); ++i)
         if (getc(file) == EOF)
@@ -192,7 +196,7 @@ processX10Header(X10WDFileHeader *  const h10P,
             bytesRead = fread(&x10colors[i], sizeof(X10Color), 1, file);
             if (bytesRead != 1)
                 pm_error("couldn't read X10 XWD colormap");
-            if (byte_swap) {
+            if (bytesSwapped) {
                 x10colors[i].red   = pm_bs_short(x10colors[i].red);
                 x10colors[i].green = pm_bs_short(x10colors[i].green);
                 x10colors[i].blue  = pm_bs_short(x10colors[i].blue);
@@ -203,6 +207,14 @@ processX10Header(X10WDFileHeader *  const h10P,
         }
     }
 
+    if (h10P->pixmap_width < 0)
+        pm_error("XWD header says pixmap_width is negative: %d",
+                 h10P->pixmap_width);
+    if (h10P->pixmap_width > UINT_MAX/8 - 15)
+        pm_error("XWD header says pixmap_width is %d, "
+                 "which is too large for this program to compute",
+                 h10P->pixmap_width);
+
     if (h10P->display_planes == 1) {
         *formatP = PBM_TYPE;
         *visualclassP = StaticGray;
@@ -212,8 +224,8 @@ processX10Header(X10WDFileHeader *  const h10P,
         PNM_ASSIGN1((*colorsP)[1], *maxvalP);
         *padrightP =
             (((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width) * 8;
-        *bits_per_itemP = 16;
-        *bits_per_pixelP = 1;
+        *bitsPerItemP = 16;
+        *bitsPerPixelP = 1;
     } else if (h10P->window_ncolors == 0) {
         /* Must be grayscale. */
         unsigned int i;
@@ -233,8 +245,8 @@ processX10Header(X10WDFileHeader *  const h10P,
             PNM_ASSIGN1((*colorsP)[i], i);
         *padrightP =
             (((h10P->pixmap_width + 15) / 16) * 16 - h10P->pixmap_width) * 8;
-        *bits_per_itemP = 16;
-        *bits_per_pixelP = 1;
+        *bitsPerItemP = 16;
+        *bitsPerPixelP = 1;
     } else {
         *maxvalP = 65535;
 
@@ -255,20 +267,20 @@ processX10Header(X10WDFileHeader *  const h10P,
         }
 
         *padrightP = (h10P->pixmap_width & 1) * 8;
-        *bits_per_itemP = 8;
-        *bits_per_pixelP = 8;
+        *bitsPerItemP  = 8;
+        *bitsPerPixelP = 8;
     }
     *colsP = h10P->pixmap_width;
     *rowsP = h10P->pixmap_height;
-    *byte_orderP = MSBFirst;
-    *bit_orderP = LSBFirst;
+    *byteOrderP = MSBFirst;
+    *bitOrderP  = LSBFirst;
 }
 
 
 
 static void
-fixH11ByteOrder(X11WDFileHeader *  const h11P,
-                X11WDFileHeader ** const h11FixedPP) {
+fixH11ByteOrder(const X11WDFileHeader * const h11P,
+                X11WDFileHeader **      const h11FixedPP) {
 
     X11WDFileHeader * h11FixedP;
 
@@ -325,7 +337,7 @@ static void
 readX11Colormap(FILE *       const file,
                 unsigned int const nColors,
                 bool         const byteSwap,
-                bool         const cmapDump,
+                bool         const mustDumpCmap,
                 X11XColor**  const x11colorsP) {
 
     X11XColor * x11colors;
@@ -346,7 +358,7 @@ readX11Colormap(FILE *       const file,
             x11colors[i].blue  = pm_bs_short(x11colors[i].blue);
         }
     }
-    if (cmapDump)
+    if (mustDumpCmap)
         dumpX11Cmap(nColors, x11colors);
 
     *x11colorsP = x11colors;
@@ -467,7 +479,7 @@ reverseBits(unsigned long arg,
 
 static void
 computeComponentMasks(X11WDFileHeader * const h11P,
-                      struct compMask * const compMaskP) {
+                      struct CompMask * const compMaskP) {
 /*----------------------------------------------------------------------------
    You'd think the component (red, green, blue) masks in the header
    would just be right.  But we've seen a direct color image which has
@@ -490,6 +502,7 @@ computeComponentMasks(X11WDFileHeader * const h11P,
 }
 
 
+
 /* About TrueColor maxval:
 
    The X11 spec says that in TrueColor, you use the bits in the raster for a
@@ -517,31 +530,34 @@ computeComponentMasks(X11WDFileHeader * const h11P,
 
 
 static void
-processX11Header(X11WDFileHeader *  const h11P,
-                 FILE *             const fileP,
-                 bool               const cmapDump,
-                 int *              const colsP,
-                 int *              const rowsP,
-                 unsigned int *     const padrightP,
-                 xelval *           const maxvalP,
-                 enum visualclass * const visualclassP,
-                 int *              const formatP,
-                 xel **             const colorsP,
-                 int *              const bits_per_pixelP,
-                 int *              const bits_per_itemP,
-                 struct compMask *  const compMaskP,
-                 enum byteorder *   const byte_orderP,
-                 enum byteorder *   const bit_orderP) {
-
-    int i;
-    X11XColor* x11colors;
+processX11Header(const X11WDFileHeader *  const h11P,
+                 FILE *                   const fileP,
+                 bool                     const mustDumpCmap,
+                 int *                    const colsP,
+                 int *                    const rowsP,
+                 unsigned int *           const padrightP,
+                 xelval *                 const maxvalP,
+                 enum visualclass *       const visualclassP,
+                 int *                    const formatP,
+                 xel **                   const colorsP,
+                 int *                    const bitsPerPixelP,
+                 int *                    const bitsPerItemP,
+                 struct CompMask *        const compMaskP,
+                 enum byteorder *         const byteOrderP,
+                 enum byteorder *         const bitOrderP) {
+/*----------------------------------------------------------------------------
+  *h11P is a mapping of the raw bytes of the header.  Interpret and validate
+   it and return the information as *colsP, etc.
+-----------------------------------------------------------------------------*/
+    unsigned int i;
+    X11XColor * x11colors;
     bool grayscale;
-    bool const byte_swap = (h11P->file_version != X11WD_FILE_VERSION);
+    bool const bytesSwapped = (h11P->file_version != X11WD_FILE_VERSION);
     X11WDFileHeader * h11FixedP;
 
     fixH11ByteOrder(h11P, &h11FixedP);
 
-    if (byte_swap && verbose)
+    if (bytesSwapped && verbose)
         pm_message("Header is different endianness from this machine.");
 
     for (i = 0; i < h11FixedP->header_size - sizeof(*h11FixedP); ++i)
@@ -549,10 +565,10 @@ processX11Header(X11WDFileHeader *  const h11P,
             pm_error("couldn't read rest of X11 XWD file header");
 
     /* Check whether we can handle this dump. */
-    if (h11FixedP->pixmap_depth > 24)
-        pm_error( "can't handle X11 pixmap_depth > 24");
-    if (h11FixedP->bits_per_rgb > 24)
-        pm_error("can't handle X11 bits_per_rgb > 24");
+    if (h11FixedP->pixmap_depth > 32)
+        pm_error( "can't handle X11 pixmap_depth > 32");
+    if (h11FixedP->bits_per_rgb > 32)
+        pm_error("can't handle X11 bits_per_rgb > 32");
     if (h11FixedP->pixmap_format != ZPixmap && h11FixedP->pixmap_depth != 1)
         pm_error("can't handle X11 pixmap_format %d with depth != 1",
                  h11FixedP->pixmap_format);
@@ -567,11 +583,11 @@ processX11Header(X11WDFileHeader *  const h11P,
                  h11FixedP->bitmap_unit);
 
     if (h11FixedP->ncolors > 0) {
-        readX11Colormap(fileP, h11FixedP->ncolors, byte_swap, cmapDump,
+        readX11Colormap(fileP, h11FixedP->ncolors, bytesSwapped, mustDumpCmap,
                         &x11colors);
         grayscale = colormapAllGray(x11colors, h11FixedP->ncolors);
     } else
-        grayscale = TRUE;
+        grayscale = true;
 
     *visualclassP = (enum visualclass) h11FixedP->visual_class;
     if (*visualclassP == DirectColor) {
@@ -593,11 +609,14 @@ processX11Header(X11WDFileHeader *  const h11P,
         *formatP = PPM_TYPE;
 
         /* See discussion above about this maxval */
+        if (h11FixedP->bits_per_rgb > 16)
+            pm_error("Invalid bits_per_rgb for TrueColor image: %u. "
+                     "Maximum possible is 16", h11FixedP->bits_per_rgb);
         *maxvalP = pm_bitstomaxval(h11FixedP->bits_per_rgb);
     } else if (*visualclassP == StaticGray && h11FixedP->bits_per_pixel == 1) {
         *formatP = PBM_TYPE;
         *maxvalP = 1;
-        *colorsP = pnm_allocrow( 2 );
+        *colorsP = pnm_allocrow(2);
         PNM_ASSIGN1((*colorsP)[0], *maxvalP);
         PNM_ASSIGN1((*colorsP)[1], 0);
     } else if (*visualclassP == StaticGray) {
@@ -633,8 +652,25 @@ processX11Header(X11WDFileHeader *  const h11P,
         *maxvalP = 65535;
     }
 
+    if (h11FixedP->pixmap_width < 1)
+        pm_error("XWD header states zero width");
+
     *colsP = h11FixedP->pixmap_width;
+
+    if (h11FixedP->pixmap_height < 1)
+        pm_error("XWD header states zero height");
+
     *rowsP = h11FixedP->pixmap_height;
+
+    if (h11FixedP->bytes_per_line > UINT_MAX/8)
+        pm_error("XWD header says bytes per line is %u, "
+                 "which is too large for this program to compute",
+                 h11FixedP->bytes_per_line);
+    if (h11FixedP->pixmap_width > UINT_MAX/h11FixedP->bits_per_pixel)
+        pm_error("XWD header says there are %u pixels per row and "
+                 "%u bits per pixel, which is too many for this program "
+                 "to compute",
+                 h11FixedP->pixmap_width, h11FixedP->bits_per_pixel);
     *padrightP =
         h11FixedP->bytes_per_line * 8 -
         h11FixedP->pixmap_width * h11FixedP->bits_per_pixel;
@@ -671,18 +707,18 @@ processX11Header(X11WDFileHeader *  const h11P,
        We still don't see any logic in these fields at all, but we
        figure whichever one is greater (assuming both are meaningful)
        has to be the item size.  */
-    *bits_per_itemP  = MAX(h11FixedP->bitmap_pad, h11FixedP->bitmap_unit);
-    *bits_per_pixelP = h11FixedP->bits_per_pixel;
+    *bitsPerItemP  = MAX(h11FixedP->bitmap_pad, h11FixedP->bitmap_unit);
+    *bitsPerPixelP = h11FixedP->bits_per_pixel;
 
     if (*visualclassP == DirectColor) {
         /* Strange, but we've seen a Direct Color 24/32 image that
            says LSBFirst and it's a lie.  And Xwud renders it correctly.
         */
-        *byte_orderP = MSBFirst;
-        *bit_orderP = MSBFirst;
+        *byteOrderP = MSBFirst;
+        *bitOrderP  = MSBFirst;
     } else {
-        *byte_orderP = (enum byteorder) h11FixedP->byte_order;
-        *bit_orderP  = (enum byteorder) h11FixedP->bitmap_bit_order;
+        *byteOrderP = (enum byteorder) h11FixedP->byte_order;
+        *bitOrderP  = (enum byteorder) h11FixedP->bitmap_bit_order;
     }
     computeComponentMasks(h11FixedP, compMaskP);
 
@@ -692,21 +728,21 @@ processX11Header(X11WDFileHeader *  const h11P,
 
 
 static void
-getinit(FILE *             const ifP,
-        int *              const colsP,
-        int *              const rowsP,
-        unsigned int *     const padrightP,
-        xelval *           const maxvalP,
-        enum visualclass * const visualclassP,
-        int *              const formatP,
-        xel **             const colorsP,
-        int *              const bits_per_pixelP,
-        int *              const bits_per_itemP,
-        struct compMask *  const compMaskP,
-        enum byteorder *   const byte_orderP,
-        enum byteorder *   const bit_orderP,
-        bool               const headerDump,
-        bool               const cmapDump) {
+readXwdHeader(FILE *             const ifP,
+              int *              const colsP,
+              int *              const rowsP,
+              unsigned int *     const padrightP,
+              xelval *           const maxvalP,
+              enum visualclass * const visualclassP,
+              int *              const formatP,
+              xel **             const colorsP,
+              int *              const bitsPerPixelP,
+              int *              const bitsPerItemP,
+              struct CompMask *  const compMaskP,
+              enum byteorder *   const byteOrderP,
+              enum byteorder *   const bitOrderP,
+              bool               const headerDump,
+              bool               const mustDumpCmap) {
 /*----------------------------------------------------------------------------
    Read the header from the XWD image in input stream 'ifP'.  Leave
    the stream positioned to the beginning of the raster.
@@ -715,20 +751,20 @@ getinit(FILE *             const ifP,
 
    Return as *padrightP the number of additional bits of padding are
    at the end of each line of input.  This says the input stream
-   contains *colsP pixels of image data (at *bits_per_pixelP bits each)
+   contains *colsP pixels of image data (at *bitsPerPixelP bits each)
    plus *padrightP bits of padding.
 -----------------------------------------------------------------------------*/
     /* Assume X11 headers are larger than X10 ones. */
     unsigned char header[sizeof(X11WDFileHeader)];
-    X10WDFileHeader* h10P;
-    X11WDFileHeader* h11P;
+    X10WDFileHeader * h10P;
+    X11WDFileHeader * h11P;
     int rc;
 
     h10P = (X10WDFileHeader*) header;
     h11P = (X11WDFileHeader*) header;
     if ( sizeof(*h10P) > sizeof(*h11P) )
         pm_error("ARGH!  On this machine, X10 headers are larger than "
-                 "X11 headers!\n    You will have to re-write xwdtopnm." );
+                 "X11 headers!  You will have to re-write xwdtopnm." );
 
     /* We read an X10 header's worth of data from the file, then look
        at it to see if it looks like an X10 header.  If so we process
@@ -749,8 +785,8 @@ getinit(FILE *             const ifP,
             pm_message("Input is X10");
         processX10Header(h10P, ifP, colsP, rowsP, padrightP, maxvalP,
                          visualclassP, formatP,
-                         colorsP, bits_per_pixelP, bits_per_itemP,
-                         compMaskP, byte_orderP, bit_orderP);
+                         colorsP, bitsPerPixelP, bitsPerItemP,
+                         compMaskP, byteOrderP, bitOrderP);
     } else if (h11P->file_version == X11WD_FILE_VERSION ||
                pm_bs_long(h11P->file_version) == X11WD_FILE_VERSION) {
 
@@ -768,13 +804,14 @@ getinit(FILE *             const ifP,
         if (headerDump)
             dumpX11Header(h11P);
 
-        processX11Header(h11P, ifP, cmapDump,
+        processX11Header(h11P, ifP, mustDumpCmap,
                          colsP, rowsP, padrightP, maxvalP,
                          visualclassP, formatP,
-                         colorsP, bits_per_pixelP, bits_per_itemP,
-                         compMaskP, byte_orderP, bit_orderP);
+                         colorsP, bitsPerPixelP, bitsPerItemP,
+                         compMaskP, byteOrderP, bitOrderP);
     } else
-        pm_error("unknown XWD file version: %u", h11P->file_version);
+        pm_error("unknown XWD file version: %u.  "
+                 "Probably not an XWD file", h11P->file_version);
 }
 
 
@@ -967,7 +1004,7 @@ readItem(pixelReader * const rdrP) {
     }
         break;
     default:
-        pm_error("INTERNAL ERROR: impossible bits_per_item");
+        pm_error("INTERNAL ERROR: impossible bitsPerItem");
     }
 }
 
@@ -1066,21 +1103,21 @@ pixelReader_getpix(pixelReader * const rdrP) {
    pure binary cipher, with the first bit the most significant bit.
 
    The basic unit of storage in the input file is an "item."  An item
-   can be 1, 2, or 4 bytes, and 'bits_per_item' tells us which.  Each
+   can be 1, 2, or 4 bytes, and 'bitsPerItem' tells us which.  Each
    item can have its bytes stored in forward or reverse order, and
-   'byte_order' tells us which.  We have seen a Direct Color 24 bpp/32 bpi
-   image which said 'byte_order' == LSBFirst, but the byte order is
+   'byteOrder' tells us which.  We have seen a Direct Color 24 bpp/32 bpi
+   image which said 'byteOrder' == LSBFirst, but the byte order is
    nonetheless MSB first.
 
    Each item can contain one or more pixels, and may contain
-   fractional pixels.  'bits_per_pixel' tells us how many bits each
-   pixel has, and 'bits_per_pixel' is always less than or equal to
-   'bits_per_item', but not necessarily a factor of it.  Within an item,
+   fractional pixels.  'bitsPerPixel' tells us how many bits each
+   pixel has, and 'bitsPerPixel' is always less than or equal to
+   'bitsPerItem', but not necessarily a factor of it.  Within an item,
    after taking care of the endianness of its storage format, the pixels
-   may be arranged from left to right or right to left.  'bit_order' tells
+   may be arranged from left to right or right to left.  'bitOrder' tells
    us which.  We have also seen images in which the pixels are arranged
    from left to right within the items, but the RGB components within
-   each pixel are right to left and 'bit_order' is LSBFirst.
+   each pixel are right to left and 'bitOrder' is LSBFirst.
 
    But it's not that simple.  Sometimes dummy bits are added to the
    right edge of the image in order to make an integral number of
@@ -1109,15 +1146,15 @@ reportInfo(int              const cols,
            xelval           const maxval,
            enum visualclass const visualclass,
            int              const format,
-           int              const bits_per_pixel,
-           int              const bits_per_item,
-           struct compMask  const compMask,
-           enum byteorder   const byte_order,
-           enum byteorder   const bit_order) {
+           int              const bitsPerPixel,
+           int              const bitsPerItem,
+           struct CompMask  const compMask,
+           enum byteorder   const byteOrder,
+           enum byteorder   const bitOrder) {
 
     const char *visualclass_name;
-    const char *byte_order_name;
-    const char *bit_order_name;
+    const char *byteOrder_name;
+    const char *bitOrder_name;
     switch (visualclass) {
     case StaticGray:  visualclass_name="StaticGray";  break;
     case GrayScale:   visualclass_name="Grayscale";   break;
@@ -1127,25 +1164,25 @@ reportInfo(int              const cols,
     case DirectColor: visualclass_name="DirectColor"; break;
     default:          visualclass_name="(invalid)";    break;
     }
-    switch (byte_order) {
-    case MSBFirst: byte_order_name = "MSBFirst";  break;
-    case LSBFirst: byte_order_name = "LSBFirst";  break;
-    default:       byte_order_name = "(invalid)"; break;
+    switch (byteOrder) {
+    case MSBFirst: byteOrder_name = "MSBFirst";  break;
+    case LSBFirst: byteOrder_name = "LSBFirst";  break;
+    default:       byteOrder_name = "(invalid)"; break;
     }
-    switch (bit_order) {
-    case MSBFirst: bit_order_name = "MSBFirst";  break;
-    case LSBFirst: bit_order_name = "LSBFirst";  break;
-    default:       bit_order_name = "(invalid)"; break;
+    switch (bitOrder) {
+    case MSBFirst: bitOrder_name = "MSBFirst";  break;
+    case LSBFirst: bitOrder_name = "LSBFirst";  break;
+    default:       bitOrder_name = "(invalid)"; break;
     }
     pm_message("%d rows of %d columns with maxval %d",
                rows, cols, maxval);
     pm_message("padright=%u bits.  visualclass = %s.  format=%d (%c%c)",
                padright, visualclass_name,
                format, format/256, format%256);
-    pm_message("bits_per_pixel=%d; bits_per_item=%d",
-               bits_per_pixel, bits_per_item);
-    pm_message("byte_order=%s; bit_order=%s",
-               byte_order_name, bit_order_name);
+    pm_message("bitsPerPixel=%d; bitsPerItem=%d",
+               bitsPerPixel, bitsPerItem);
+    pm_message("byteOrder=%s; bitOrder=%s",
+               byteOrder_name, bitOrder_name);
     pm_message("component mask: red=0x%.8lx; grn=0x%.8lx; blu=0x%.8lx",
                compMask.red, compMask.grn, compMask.blu);
 }
@@ -1191,7 +1228,7 @@ static void
 convertRowDirect(pixelReader *   const pixelReaderP,
                  int             const cols,
                  const xel *     const colors,
-                 struct compMask const compMask,
+                 struct CompMask const compMask,
                  xel *           const xelrow) {
 
     unsigned int col;
@@ -1201,20 +1238,20 @@ convertRowDirect(pixelReader *   const pixelReaderP,
             /* This is a triplet of indices into the color map, packed
                into this bit string according to compMask
             */
-        unsigned int red_index, grn_index, blu_index;
+        unsigned int redIndex, grnIndex, bluIndex;
             /* These are indices into the color map, unpacked from 'pixel'.
              */
 
         pixel = pixelReader_getpix(pixelReaderP);
 
-        red_index = (pixel & compMask.red) >> zero_bits(compMask.red);
-        grn_index = (pixel & compMask.grn) >> zero_bits(compMask.grn);
-        blu_index = (pixel & compMask.blu) >> zero_bits(compMask.blu);
+        redIndex = (pixel & compMask.red) >> zeroBits(compMask.red);
+        grnIndex = (pixel & compMask.grn) >> zeroBits(compMask.grn);
+        bluIndex = (pixel & compMask.blu) >> zeroBits(compMask.blu);
 
         PPM_ASSIGN(xelrow[col],
-                   PPM_GETR(colors[red_index]),
-                   PPM_GETG(colors[grn_index]),
-                   PPM_GETB(colors[blu_index])
+                   PPM_GETR(colors[redIndex]),
+                   PPM_GETG(colors[grnIndex]),
+                   PPM_GETB(colors[bluIndex])
             );
     }
 }
@@ -1226,16 +1263,16 @@ convertRowTrueColor(pixelReader *   const pixelReaderP,
                     int             const cols,
                     pixval          const maxval,
                     const xel *     const colors,
-                    struct compMask const compMask,
+                    struct CompMask const compMask,
                     xel *           const xelrow) {
 
     unsigned int col;
     unsigned int red_shift, grn_shift, blu_shift;
     unsigned int red_maxval, grn_maxval, blu_maxval;
 
-    red_shift = zero_bits(compMask.red);
-    grn_shift = zero_bits(compMask.grn);
-    blu_shift = zero_bits(compMask.blu);
+    red_shift = zeroBits(compMask.red);
+    grn_shift = zeroBits(compMask.grn);
+    blu_shift = zeroBits(compMask.blu);
 
     red_maxval = compMask.red >> red_shift;
     grn_maxval = compMask.grn >> grn_shift;
@@ -1267,7 +1304,7 @@ convertRow(pixelReader *    const pixelReaderP,
            int              const cols,
            xelval           const maxval,
            int              const format,
-           struct compMask  const compMask,
+           struct CompMask  const compMask,
            const xel*       const colors,
            enum visualclass const visualclass) {
 /*----------------------------------------------------------------------------
@@ -1334,9 +1371,9 @@ reportOutputType(int const format) {
 
 
 int
-main(int argc, char *argv[]) {
+main(int argc, const char ** argv) {
 
-    struct cmdlineInfo cmdline;
+    struct CmdlineInfo cmdline;
     FILE * ifP;
     int rows, cols, format;
     unsigned int padright;
@@ -1344,14 +1381,14 @@ main(int argc, char *argv[]) {
     unsigned int row;
     int bitsPerPixel;
     int bitsPerItem;
-    struct compMask compMask;
+    struct CompMask compMask;
     xelval maxval;
     enum visualclass visualclass;
     enum byteorder byteOrder, bitOrder;
     xel * colors;  /* the color map */
     pixelReader pixelReader;
 
-    pnm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
@@ -1363,10 +1400,10 @@ main(int argc, char *argv[]) {
     else
         ifP = stdin;
 
-    getinit(ifP, &cols, &rows, &padright, &maxval, &visualclass, &format,
-            &colors, &bitsPerPixel, &bitsPerItem,
-            &compMask, &byteOrder, &bitOrder,
-            cmdline.headerdump, cmdline.cmapdump);
+    readXwdHeader(ifP, &cols, &rows, &padright, &maxval, &visualclass, &format,
+                  &colors, &bitsPerPixel, &bitsPerItem,
+                  &compMask, &byteOrder, &bitOrder,
+                  cmdline.headerdump, cmdline.cmapdump);
 
     warn16Bit(maxval);
 
@@ -1410,7 +1447,7 @@ main(int argc, char *argv[]) {
    doesn't match these hard-coded shift amounts, so we have replaced
    this whole switch thing.  -Bryan 00.03.01
 
-   switch (bits_per_pixel) {
+   switch (bitsPerPixel) {
 
    case 16:
        PPM_ASSIGN( *xP,