about summary refs log tree commit diff
path: root/converter/ppm/pcxtoppm.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/ppm/pcxtoppm.c')
-rw-r--r--converter/ppm/pcxtoppm.c532
1 files changed, 262 insertions, 270 deletions
diff --git a/converter/ppm/pcxtoppm.c b/converter/ppm/pcxtoppm.c
index e252ba22..f06dd4e8 100644
--- a/converter/ppm/pcxtoppm.c
+++ b/converter/ppm/pcxtoppm.c
@@ -31,6 +31,9 @@
  *  - moved check of 16-color-palette into pcx_16col_to_ppm(),
  *    now checks if it contains only a single color
  */
+#include <stdbool.h>
+#include <limits.h>
+
 #include "pm_c_util.h"
 #include "mallocvar.h"
 #include "shhopt.h"
@@ -52,30 +55,31 @@ static pixel stdPalette[16];
 static void
 generateStdPalette(void) {
 
-    int i;
-    for (i = 0; i < 16; i++)
+    unsigned int i;
+
+    for (i = 0; i < 16; ++i)
         PPM_ASSIGN(stdPalette[i], StdRed[i], StdGreen[i], StdBlue[i]);
 }
 
 
 
-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;  /* '-' if stdin */
-    unsigned int verbose;  
+    const char * inputFileNm;  /* '-' if stdin */
+    unsigned int verbose;
     unsigned int stdpalette;
 };
 
 
 
 static void
-parseCommandLine ( int argc, char ** argv,
-                   struct cmdlineInfo *cmdlineP ) {
+parseCommandLine(int argc, const char ** argv,
+                 struct CmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
-   parse program command line described in Unix standard form by argc
-   and argv.  Return the information in the options as *cmdlineP.  
+   Parse program command line described in Unix standard form by argc
+   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.
@@ -83,30 +87,30 @@ parseCommandLine ( int argc, char ** argv,
    Note that the strings we return are stored in the storage that
    was passed to us as the argv array.  We also trash *argv.
 -----------------------------------------------------------------------------*/
-    optEntry *option_def = malloc( 100*sizeof( optEntry ) );
-        /* Instructions to pm_optParseOptions3 on how to parse our options.
-         */
+    optEntry * option_def;
     optStruct3 opt;
 
     unsigned int option_def_index;
 
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
     option_def_index = 0;   /* incremented by OPTENT3 */
-    OPTENT3(0, "stdpalette",     OPT_FLAG,   NULL,                  
+    OPTENT3(0, "stdpalette",     OPT_FLAG,   NULL,
             &cmdlineP->stdpalette,    0 );
-    OPTENT3(0, "verbose",        OPT_FLAG,   NULL,                  
+    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 */
+    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_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdline_p and others. */
 
     if (argc-1 < 1)
-        cmdlineP->inputFilespec = "-";
+        cmdlineP->inputFileNm = "-";
     else if (argc-1 == 1)
-        cmdlineP->inputFilespec = argv[1];
+        cmdlineP->inputFileNm = argv[1];
     else
         pm_error("Program takes at most one argument "
                  "(input file specification).  You specified %d",
@@ -115,13 +119,13 @@ parseCommandLine ( int argc, char ** argv,
 
 
 
-struct pcxHeader {
+struct PcxHeader {
     int Version;
     /* Xmin, Ymin, Xmax, and Ymax are positions in some field (in units of
        pixels) of the edges of the image.  They may be negative.  You can
        derive the image width and height from these.
     */
-    short Xmin;            
+    short Xmin;
     short Ymin;
     short Xmax;
     short Ymax;
@@ -141,23 +145,8 @@ struct pcxHeader {
 
 
 
-static void
-readInt(FILE * const ifP, short * const retvalP) {
-/*----------------------------------------------------------------------------
-   Read a 2-byte little-endian word from the file.
------------------------------------------------------------------------------*/
-    int rc;
-
-    rc = pm_readlittleshort(ifP, retvalP);
-
-    if (rc != 0)
-        pm_error("EOF/error reading integer from input file.");
-}
-
-
-
 static int
-GetByte(FILE * const fp) {
+getByte(FILE * const fp) {
 
     int    c;
 
@@ -170,55 +159,55 @@ GetByte(FILE * const fp) {
 
 
 static void
-readPcxHeader(FILE *             const ifP, 
-              struct pcxHeader * const pcxHeaderP) {
+readPcxHeader(FILE *             const ifP,
+              struct PcxHeader * const pcxHeaderP) {
 /*----------------------------------------------------------------------------
    Read the PCX header
 -----------------------------------------------------------------------------*/
-    if (GetByte(ifP) != PCX_MAGIC)
+    if (getByte(ifP) != PCX_MAGIC)
         pm_error("bad magic number - not a PCX file");
 
-    pcxHeaderP->Version = GetByte(ifP);  /* get version # */
+    pcxHeaderP->Version = getByte(ifP);  /* get version # */
 
-    pcxHeaderP->Encoding = GetByte(ifP);
+    pcxHeaderP->Encoding = getByte(ifP);
     if (pcxHeaderP->Encoding != 1)    /* check for PCX run length encoding   */
         pm_error("unknown encoding scheme: %d", pcxHeaderP->Encoding);
 
-    pcxHeaderP->BitsPerPixel= GetByte(ifP);
-    readInt(ifP, &pcxHeaderP->Xmin);
-    readInt(ifP, &pcxHeaderP->Ymin);
-    readInt(ifP, &pcxHeaderP->Xmax);
-    readInt(ifP, &pcxHeaderP->Ymax);
+    pcxHeaderP->BitsPerPixel= getByte(ifP);
+    pm_readlittleshort(ifP, &pcxHeaderP->Xmin);
+    pm_readlittleshort(ifP, &pcxHeaderP->Ymin);
+    pm_readlittleshort(ifP, &pcxHeaderP->Xmax);
+    pm_readlittleshort(ifP, &pcxHeaderP->Ymax);
 
     if (pcxHeaderP->Xmax < pcxHeaderP->Xmin)
         pm_error("Invalid PCX input:  minimum X value (%d) is greater than "
-                 "maximum X value (%d).", 
+                 "maximum X value (%d).",
                  pcxHeaderP->Xmin, pcxHeaderP->Xmax);
     if (pcxHeaderP->Ymax < pcxHeaderP->Ymin)
         pm_error("Invalid PCX input:  minimum Y value (%d) is greater than "
-                 "maximum Y value (%d).", 
+                 "maximum Y value (%d).",
                  pcxHeaderP->Ymin, pcxHeaderP->Ymax);
 
-    readInt(ifP, &pcxHeaderP->HorizontalResolution);  
-    readInt(ifP, &pcxHeaderP->VerticalResolution);  
+    pm_readlittleshort(ifP, &pcxHeaderP->HorizontalResolution);
+    pm_readlittleshort(ifP, &pcxHeaderP->VerticalResolution);
 
     {
-        int i;
+        unsigned int i;
         /*
          * get the 16-color color map
          */
-        for (i = 0; i < 16; i++) {
-            unsigned int const r = GetByte(ifP);
-            unsigned int const g = GetByte(ifP);
-            unsigned int const b = GetByte(ifP);
+        for (i = 0; i < 16; ++i) {
+            unsigned int const r = getByte(ifP);
+            unsigned int const g = getByte(ifP);
+            unsigned int const b = getByte(ifP);
             PPM_ASSIGN(pcxHeaderP->cmap16[i], r, g, b);
         }
     }
 
-    GetByte(ifP);                /* skip reserved byte       */
-    pcxHeaderP->Planes      = GetByte(ifP);     /* # of color planes        */
-    readInt(ifP, &pcxHeaderP->BytesPerLine);
-    readInt(ifP, &pcxHeaderP->PaletteInfo);
+    getByte(ifP);                /* skip reserved byte       */
+    pcxHeaderP->Planes = getByte(ifP);     /* # of color planes        */
+    pm_readlittleshort(ifP, &pcxHeaderP->BytesPerLine);
+    pm_readlittleshort(ifP, &pcxHeaderP->PaletteInfo);
 
     /* Read past a bunch of reserved space in the header.  We have read
        70 bytes of the header so far.  We would just seek here, except that
@@ -228,28 +217,29 @@ readPcxHeader(FILE *             const ifP,
         unsigned int pos;
 
         for (pos = 70; pos < PCX_HDR_SIZE; ++pos)
-            GetByte(ifP);
+            getByte(ifP);
     }
 }
 
 
 
-static void 
-reportPcxHeader(struct pcxHeader const pcxHeader) {
+static void
+reportPcxHeader(struct PcxHeader const pcxHeader) {
 
     pm_message("Version: %d", pcxHeader.Version);
     pm_message("BitsPerPixel: %d", pcxHeader.BitsPerPixel);
-    pm_message("Xmin: %d   Ymin: %d   Xmax: %d   Ymax: %d", 
+    pm_message("Xmin: %d   Ymin: %d   Xmax: %d   Ymax: %d",
                pcxHeader.Xmin, pcxHeader.Ymin, pcxHeader.Xmax, pcxHeader.Ymax);
-    pm_message("Planes: %d    BytesPerLine: %d    PaletteInfo: %d", 
-               pcxHeader.Planes, pcxHeader.BytesPerLine, 
+    pm_message("Planes: %d    BytesPerLine: %d    PaletteInfo: %d",
+               pcxHeader.Planes, pcxHeader.BytesPerLine,
                pcxHeader.PaletteInfo);
     pm_message("Color map in image:  (index: r/g/b)");
+
     if (pcxHeader.BitsPerPixel < 8) {
         unsigned int colorIndex;
         for (colorIndex = 0; colorIndex < 16; ++colorIndex) {
             pixel const p = pcxHeader.cmap16[colorIndex];
-            pm_message("  %u: %u/%u/%u", colorIndex, 
+            pm_message("  %u: %u/%u/%u", colorIndex,
                        PPM_GETR(p), PPM_GETG(p), PPM_GETB(p));
         }
     }
@@ -278,42 +268,39 @@ allBlackPalette(pixel cmap16[]) {
 
 
 
-/*
- *  read a single row encoded row, handles encoding across rows
- */
 static void
-GetPCXRow(FILE * const ifP, unsigned char * const pcxrow, 
-          int const bytesperline) {
+getPCXRow(FILE *          const ifP,
+          unsigned char * const pcxrow,
+          unsigned int    const bytesperline) {
 /*----------------------------------------------------------------------------
    Read one row from the PCX raster.
-   
-   The PCX raster is run length encoded as follows:  If the upper two
-   bits of a byte are 11, the lower 6 bits are a repetition count for the
-   raster byte that follows.  If the upper two bits are not 11, the byte 
-   _is_ a raster byte, with repetition count 1.
-
-   A run can't span rows, but it can span planes within a row.  That's
-   why 'repetitionsLeft' and 'c' are static variables in this
-   subroutine.
+
+   The PCX raster is run length encoded as follows: If the upper two bits of a
+   byte are 11, the lower 6 bits are a repetition count for the raster byte
+   that follows.  If the upper two bits are not 11, the byte _is_ a raster
+   byte, with repetition count 1.
+
+   A run can't span rows, but it can span planes within a row.  That's why
+   'repetitionsLeft' and 'c' are static variables in this subroutine.
 -----------------------------------------------------------------------------*/
     static int repetitionsLeft = 0;
     static int c;
-    int bytesGenerated;
+    unsigned int bytesGenerated;
 
     bytesGenerated = 0;
-    while( bytesGenerated < bytesperline ) {
-        if(repetitionsLeft > 0) {
+    while(bytesGenerated < bytesperline) {
+        if (repetitionsLeft > 0) {
             pcxrow[bytesGenerated++] = c;
             --repetitionsLeft;
         } else {
-            c = GetByte(ifP);
+            c = getByte(ifP);
             if ((c & 0xc0) != 0xc0)
                 /* This is a 1-shot byte, not a repetition count */
                 pcxrow[bytesGenerated++] = c;
             else {
                 /* This is a repetition count for the following byte */
                 repetitionsLeft = c & 0x3f;
-                c = GetByte(ifP);
+                c = getByte(ifP);
             }
         }
     }
@@ -321,187 +308,185 @@ GetPCXRow(FILE * const ifP, unsigned char * const pcxrow,
 
 
 
-/*
- * convert packed pixel format into 1 pixel per byte
- */
 static void
-pcx_unpack_pixels(pixels, bitplanes, bytesperline, planes, bitsperpixel)
-    unsigned char   *pixels;
-    unsigned char   *bitplanes;
-    int             bytesperline;
-    int             planes;
-    int             bitsperpixel;
-{
-    register int        bits;
+pcxUnpackPixels(unsigned char * const pixels,
+                unsigned char * const bitplanes,
+                unsigned int    const bytesperline,
+                unsigned int    const planes,
+                unsigned int    const bitsperpixel) {
+/*----------------------------------------------------------------------------
+   Convert packed pixel format in bitplanes[] into 1 pixel per byte
+   in pixels[].
+-----------------------------------------------------------------------------*/
+    unsigned int i;
 
     if (planes != 1)
         pm_error("can't handle packed pixels with more than 1 plane" );
-#if 0
-    if (bitsperpixel == 8)
-    {
-        while (--bytesperline >= 0)
-            *pixels++ = *bitplanes++;
-    }
-    else
-#endif
-        if (bitsperpixel == 4)
-        {
-            while (--bytesperline >= 0)
-            {
-                bits        = *bitplanes++;
-                *pixels++   = (bits >> 4) & 0x0f;
-                *pixels++   = (bits     ) & 0x0f;
-            }
-        }
-        else if (bitsperpixel == 2)
-        {
-            while (--bytesperline >= 0)
-            {
-                bits        = *bitplanes++;
-                *pixels++   = (bits >> 6) & 0x03;
-                *pixels++   = (bits >> 4) & 0x03;
-                *pixels++   = (bits >> 2) & 0x03;
-                *pixels++   = (bits     ) & 0x03;
-            }
-        }
-        else if (bitsperpixel == 1)
-        {
-            while (--bytesperline >= 0)
-            {
-                bits        = *bitplanes++;
-                *pixels++   = ((bits & 0x80) != 0);
-                *pixels++   = ((bits & 0x40) != 0);
-                *pixels++   = ((bits & 0x20) != 0);
-                *pixels++   = ((bits & 0x10) != 0);
-                *pixels++   = ((bits & 0x08) != 0);
-                *pixels++   = ((bits & 0x04) != 0);
-                *pixels++   = ((bits & 0x02) != 0);
-                *pixels++   = ((bits & 0x01) != 0);
-            }
+
+    for (i = 0; i < bytesperline; ++i) {
+        unsigned int const bits = bitplanes[i];
+
+        switch (bitsperpixel) {
+        case 4:
+            pixels[2*i + 0] = (bits >> 4) & 0x0f;
+            pixels[2*i + 1] = (bits     ) & 0x0f;
+            break;
+        case 2:
+            pixels[i*4 + 0] = (bits >> 6) & 0x03;
+            pixels[i*4 + 1] = (bits >> 4) & 0x03;
+            pixels[i*4 + 2] = (bits >> 2) & 0x03;
+            pixels[i*4 + 3] = (bits     ) & 0x03;
+            break;
+        case 1:
+            pixels[i*8 + 0]  = ((bits & 0x80) != 0);
+            pixels[i*8 + 1]  = ((bits & 0x40) != 0);
+            pixels[i*8 + 2]  = ((bits & 0x20) != 0);
+            pixels[i*8 + 3]  = ((bits & 0x10) != 0);
+            pixels[i*8 + 4]  = ((bits & 0x08) != 0);
+            pixels[i*8 + 5]  = ((bits & 0x04) != 0);
+            pixels[i*8 + 6]  = ((bits & 0x02) != 0);
+            pixels[i*8 + 7]  = ((bits & 0x01) != 0);
+            break;
+        default:
+        pm_error("pcxUnpackPixels - can't handle %u bits per pixel",
+                 bitsperpixel);
         }
-        else
-            pm_error("pcx_unpack_pixels - can't handle %d bits per pixel", 
-                     bitsperpixel);
+    }
 }
 
 
 
-/*
- * convert multi-plane format into 1 pixel per byte
- */
 static void
-pcx_planes_to_pixels(pixels, bitplanes, bytesperline, planes, bitsperpixel)
-    unsigned char   *pixels;
-    unsigned char   *bitplanes;
-    int             bytesperline;
-    int             planes;
-    int             bitsperpixel;
-{
-    int  i, j;
-    int  npixels;
-    unsigned char    *p;
+pcxPlanesToPixels(unsigned char * const pixels,
+                  unsigned char * const bitPlanes,
+                  unsigned int    const bytesPerLine,
+                  unsigned int    const planes,
+                  unsigned int    const bitsPerPixel) {
+/*----------------------------------------------------------------------------
+   Convert multi-plane format into 1 pixel per byte.
+-----------------------------------------------------------------------------*/
+    unsigned int const npixels = (bytesPerLine * 8) / bitsPerPixel;
+
+    unsigned int  i;
 
     if (planes > 4)
-        pm_error("can't handle more than 4 planes" );
-    if (bitsperpixel != 1)
-        pm_error("can't handle more than 1 bit per pixel" );
+        pm_error("can't handle more than 4 planes");
+    if (bitsPerPixel != 1)
+        pm_error("can't handle more than 1 bit per pixel");
 
-    /*
-     * clear the pixel buffer
-     */
-    npixels = (bytesperline * 8) / bitsperpixel;
-    p    = pixels;
-    while (--npixels >= 0)
-        *p++ = 0;
+    /* Clear the pixel buffer - initial value */
+    for (i = 0; i < npixels; ++i)
+        pixels[i] = 0;
 
-    /*
-     * do the format conversion
-     */
-    for (i = 0; i < planes; i++)
-    {
-        int pixbit, bits, mask;
-
-        p    = pixels;
-        pixbit    = (1 << i);
-        for (j = 0; j < bytesperline; j++)
-        {
-            bits = *bitplanes++;
-            for (mask = 0x80; mask != 0; mask >>= 1, p++)
+    for (i = 0; i < planes; ++i) {
+        unsigned int const pixbit = (1 << i);
+
+        unsigned int j;
+
+        for (j = 0; j < bytesPerLine; ++j) {
+            unsigned int const bits = bitPlanes[j];
+
+            unsigned int mask;
+            unsigned int k;
+
+            for (mask = 0x80, k = 0; mask != 0; mask >>= 1, ++k)
                 if (bits & mask)
-                    *p |= pixbit;
+                    pixels[k] |= pixbit;
         }
     }
 }
 
 
 
-static void
-pcx_16col_to_ppm(FILE *       const ifP,
-                 unsigned int const headerCols,
-                 unsigned int const rows,
-                 unsigned int const BytesPerLine,
-                 unsigned int const BitsPerPixel,
-                 unsigned int const Planes,
-                 pixel *      const cmap) {
+static bool
+paletteIsOk(const pixel * const cmap,
+            unsigned int  const colorCt) {
 
-    unsigned int cols;
-    int row, col, rawcols, colors;
-    unsigned char * pcxrow;
-    unsigned char * rawrow;
-    pixel * ppmrow;
     bool paletteOk;
+    unsigned int col;
 
-    paletteOk = FALSE;
+    paletteOk = false;  /* initial assumption */
 
     /* check if palette is ok  */
-    colors = (1 << BitsPerPixel) * (1 << Planes);
-    for (col = 0; col < colors - 1; ++col) {
+    for (col = 0; col < colorCt - 1; ++col) {
         if (!PPM_EQUAL(cmap[col], cmap[col+1])) {
-            paletteOk = TRUE;
-            break;
+            paletteOk = true;
         }
     }
+    return paletteOk;
+}
+
+
+
+static void
+pcx16ColToPpm(FILE *       const ifP,
+              unsigned int const headerCols,
+              unsigned int const rows,
+              unsigned int const bytesPerLine,
+              unsigned int const bitsPerPixel,
+              unsigned int const planes,
+              pixel *      const cmap) {
+
+    unsigned int const colors = (1 << bitsPerPixel) * (1 << planes);
+    bool const paletteOk = paletteIsOk(cmap, colors);
+
+    unsigned int cols;
+    unsigned int row;
+    unsigned int rawcols;
+    unsigned char * pcxrow;
+    unsigned char * rawrow;
+    pixel * ppmrow;
+
     if (!paletteOk) {
         unsigned int col;
+
         pm_message("warning - useless header palette, "
                    "using builtin standard palette");
+
         for (col = 0; col < colors; ++col)
             PPM_ASSIGN(cmap[col], StdRed[col], StdGreen[col], StdBlue[col]);
     }
 
-    /*  BytesPerLine should be >= BitsPerPixel * cols / 8  */
-    rawcols = BytesPerLine * 8 / BitsPerPixel;
+    if (bytesPerLine > UINT_MAX/8)
+        pm_error("Image too wide to compute (%u bytes per line)",
+                 bytesPerLine);
+
+    /*  bytesPerLine should be >= bBitsPerPixel * cols / 8  */
+    rawcols = bytesPerLine * 8 / bitsPerPixel;
+
     if (headerCols > rawcols) {
-        pm_message("warning - BytesPerLine = %d, "
-                   "truncating image to %d pixels",
-                   BytesPerLine, rawcols);
+        pm_message("warning - BytesPerLine = %u, "
+                   "truncating image to %u pixels",
+                   bytesPerLine, rawcols);
         cols = rawcols;
     } else
         cols = headerCols;
 
-    MALLOCARRAY(pcxrow, Planes * BytesPerLine);
+    MALLOCARRAY(pcxrow, planes * bytesPerLine);
     if (pcxrow == NULL)
-        pm_error("Out of memory");
+        pm_error("Can't get memory for %u planes, %u bytes per line",
+                 planes, bytesPerLine);
     MALLOCARRAY(rawrow, rawcols);
     if (rawrow == NULL)
-        pm_error("Out of memory");
+        pm_error("Can't get memory for %u columns", rawcols);
 
     ppmrow = ppm_allocrow(cols);
 
     for (row = 0; row < rows; ++row) {
         unsigned int col;
 
-        GetPCXRow(ifP, pcxrow, Planes * BytesPerLine);
+        getPCXRow(ifP, pcxrow, planes * bytesPerLine);
 
-        if (Planes == 1)
-            pcx_unpack_pixels(rawrow, pcxrow, BytesPerLine, 
-                              Planes, BitsPerPixel);
+        if (planes == 1)
+            pcxUnpackPixels(rawrow, pcxrow, bytesPerLine,
+                            planes, bitsPerPixel);
         else
-            pcx_planes_to_pixels(rawrow, pcxrow, BytesPerLine, 
-                                 Planes, BitsPerPixel);
+            pcxPlanesToPixels(rawrow, pcxrow, bytesPerLine,
+                              planes, bitsPerPixel);
 
         for (col = 0; col < cols; ++col)
             ppmrow[col] = cmap[rawrow[col]];
+
         ppm_writeppmrow(stdout, ppmrow, cols, PCX_MAXVAL, 0);
     }
     ppm_freerow(ppmrow);
@@ -512,10 +497,10 @@ pcx_16col_to_ppm(FILE *       const ifP,
 
 
 static void
-pcx_256col_to_ppm(FILE *       const ifP,
-                  unsigned int const headerCols,
-                  unsigned int const rows,
-                  unsigned int const BytesPerLine) {
+pcx256ColToPpm(FILE *       const ifP,
+               unsigned int const headerCols,
+               unsigned int const rows,
+               unsigned int const bytesPerLine) {
 
     unsigned int cols;
     pixel colormap[256];
@@ -524,37 +509,37 @@ pcx_256col_to_ppm(FILE *       const ifP,
     unsigned char colormapSignature;
     unsigned int row;
 
-    if (headerCols > BytesPerLine) {
+    if (headerCols > bytesPerLine) {
         pm_message("warning - BytesPerLine = %u, "
                    "truncating image to %u pixels",
-                   BytesPerLine,  BytesPerLine);
-        cols = BytesPerLine;
+                   bytesPerLine,  bytesPerLine);
+        cols = bytesPerLine;
     } else
         cols = headerCols;
 
-    MALLOCARRAY2(image, rows, BytesPerLine);
+    MALLOCARRAY2(image, rows, bytesPerLine);
 
     for (row = 0; row < rows; ++row)
-        GetPCXRow(ifP, image[row], BytesPerLine);
+        getPCXRow(ifP, image[row], bytesPerLine);
 
     /*
      * 256 color images have their color map at the end of the file
      * preceded by a magic byte
      */
-    colormapSignature = GetByte(ifP);
+    colormapSignature = getByte(ifP);
     if (colormapSignature != PCX_256_COLORS)
         pm_error("bad color map signature.  In a 1-plane PCX image "
                  "such as this, we expect a magic number of %u in the byte "
                  "following the raster, to introduce the color map.  "
-                 "Instead, this image has %u.", 
+                 "Instead, this image has %u.",
                  PCX_256_COLORS, colormapSignature);
     else {
         unsigned int colorIndex;
-        
+
         for (colorIndex = 0; colorIndex < 256; ++colorIndex) {
-            pixval const r = GetByte(ifP);
-            pixval const g = GetByte(ifP);
-            pixval const b = GetByte(ifP);
+            pixval const r = getByte(ifP);
+            pixval const g = getByte(ifP);
+            pixval const b = getByte(ifP);
             PPM_ASSIGN(colormap[colorIndex], r, g, b);
         }
     }
@@ -574,51 +559,56 @@ pcx_256col_to_ppm(FILE *       const ifP,
 
 
 static void
-pcx_truecol_to_ppm(FILE *       const ifP,
-                   unsigned int const headerCols,
-                   unsigned int const rows,
-                   unsigned int const BytesPerLine,
-                   unsigned int const Planes) {
-
-    unsigned int cols;
-    unsigned char *redrow, *greenrow, *bluerow, *intensityrow;
-    pixel * ppmrow;
-    unsigned int row;
-    
-    if (headerCols > BytesPerLine) {
+pcxTruecolToPpm(FILE *       const ifP,
+                unsigned int const headerCols,
+                unsigned int const rows,
+                unsigned int const bytesPerLine,
+                unsigned int const planes) {
+
+    unsigned int    cols;
+    unsigned char * redrow;
+    unsigned char * grnrow;
+    unsigned char * blurow;
+    unsigned char * intensityrow;
+    pixel *         ppmrow;
+    unsigned int    row;
+
+    if (headerCols > bytesPerLine) {
         pm_message("warning - BytesPerLine = %u, "
                    "truncating image to %u pixels",
-                   BytesPerLine,  BytesPerLine);
-        cols = BytesPerLine;
+                   bytesPerLine, bytesPerLine);
+        cols = bytesPerLine;
     } else
         cols = headerCols;
 
-    MALLOCARRAY(redrow, BytesPerLine);
-    MALLOCARRAY(greenrow, BytesPerLine);
-    MALLOCARRAY(bluerow, BytesPerLine);
+    MALLOCARRAY(redrow, bytesPerLine);
+    MALLOCARRAY(grnrow, bytesPerLine);
+    MALLOCARRAY(blurow, bytesPerLine);
 
-    if (redrow == NULL || greenrow == NULL || bluerow == NULL)
-        pm_error("out of memory");
+    if (redrow == NULL || grnrow == NULL || blurow == NULL)
+        pm_error("Can't get memory for %u-byte row buffer", bytesPerLine);
 
-    if (Planes == 4) {
-        MALLOCARRAY(intensityrow, BytesPerLine);
+    if (planes == 4) {
+        MALLOCARRAY(intensityrow, bytesPerLine);
         if (intensityrow == NULL)
-            pm_error("out of memory");
+            pm_error("Can't get memory for %u-byte row buffer", bytesPerLine);
     } else
         intensityrow = NULL;
 
     ppmrow = ppm_allocrow(cols);
     for (row = 0; row < rows; ++row) {
         unsigned int col;
-        GetPCXRow(ifP, redrow, BytesPerLine);
-        GetPCXRow(ifP, greenrow, BytesPerLine);
-        GetPCXRow(ifP, bluerow, BytesPerLine);
-        if( intensityrow )
-            GetPCXRow(ifP, intensityrow, BytesPerLine);
+
+        getPCXRow(ifP, redrow, bytesPerLine);
+        getPCXRow(ifP, grnrow, bytesPerLine);
+        getPCXRow(ifP, blurow, bytesPerLine);
+        if (intensityrow)
+            getPCXRow(ifP, intensityrow, bytesPerLine);
+
         for (col = 0; col < cols; ++col) {
             unsigned int const r = redrow[col];
-            unsigned int const g = greenrow[col];
-            unsigned int const b = bluerow[col];
+            unsigned int const g = grnrow[col];
+            unsigned int const b = blurow[col];
             unsigned int const i = intensityrow ? intensityrow[col] : 256;
 
             PPM_ASSIGN(ppmrow[col],
@@ -630,29 +620,29 @@ pcx_truecol_to_ppm(FILE *       const ifP,
     ppm_freerow(ppmrow);
     if (intensityrow)
         free(intensityrow);
-    free(bluerow);
-    free(greenrow);
+    free(blurow);
+    free(grnrow);
     free(redrow);
 }
 
 
 
 int
-main(int argc, char *argv[]) {
+main(int argc, const char *argv[]) {
 
     FILE * ifP;
-    struct cmdlineInfo cmdline;
-    struct pcxHeader pcxHeader;
+    struct CmdlineInfo cmdline;
+    struct PcxHeader pcxHeader;
     unsigned int Width, Height;
     pixel * cmap16;
 
-    ppm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
     generateStdPalette();
 
     parseCommandLine(argc, argv, &cmdline);
 
-    ifP = pm_openr(cmdline.inputFilespec);
+    ifP = pm_openr(cmdline.inputFileNm);
 
     readPcxHeader(ifP, &pcxHeader);
 
@@ -668,31 +658,32 @@ main(int argc, char *argv[]) {
         cmap16 = pcxHeader.cmap16;
 
     ppm_writeppminit(stdout, Width, Height, PCX_MAXVAL, 0);
+
     switch (pcxHeader.BitsPerPixel) {
     case 1:
         if(pcxHeader.Planes >= 1 && pcxHeader.Planes <= 4)
-            pcx_16col_to_ppm(ifP, Width, Height, pcxHeader.BytesPerLine, 
-                             pcxHeader.BitsPerPixel, pcxHeader.Planes, cmap16);
+            pcx16ColToPpm(ifP, Width, Height, pcxHeader.BytesPerLine,
+                          pcxHeader.BitsPerPixel, pcxHeader.Planes, cmap16);
         else
             goto fail;
         break;
     case 2:
     case 4:
         if (pcxHeader.Planes == 1)
-            pcx_16col_to_ppm(ifP, Width, Height, pcxHeader.BytesPerLine, 
-                             pcxHeader.BitsPerPixel, pcxHeader.Planes, cmap16);
+            pcx16ColToPpm(ifP, Width, Height, pcxHeader.BytesPerLine,
+                          pcxHeader.BitsPerPixel, pcxHeader.Planes, cmap16);
         else
             goto fail;
         break;
     case 8:
         switch(pcxHeader.Planes) {
         case 1:
-            pcx_256col_to_ppm(ifP, Width, Height, pcxHeader.BytesPerLine);
+            pcx256ColToPpm(ifP, Width, Height, pcxHeader.BytesPerLine);
             break;
         case 3:
         case 4:
-            pcx_truecol_to_ppm(ifP, Width, Height, 
-                               pcxHeader.BytesPerLine, pcxHeader.Planes);
+            pcxTruecolToPpm(ifP, Width, Height,
+                            pcxHeader.BytesPerLine, pcxHeader.Planes);
             break;
         default:
             goto fail;
@@ -704,8 +695,9 @@ main(int argc, char *argv[]) {
                  pcxHeader.BitsPerPixel, pcxHeader.Planes);
     }
     pm_close(ifP);
-    
+
     return 0;
 }
 
 
+