about summary refs log tree commit diff
path: root/converter/ppm/winicontoppm.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2019-06-28 23:07:55 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2019-06-28 23:07:55 +0000
commit11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32 (patch)
tree7c40f096dd973943ef563ec87b2a68d8205db4fb /converter/ppm/winicontoppm.c
parent89c6ec14eb7514630aea5abc4b90b51d1473d33a (diff)
downloadnetpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.tar.gz
netpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.tar.xz
netpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.zip
Promote Stable to Super_stable
git-svn-id: http://svn.code.sf.net/p/netpbm/code/super_stable@3640 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/ppm/winicontoppm.c')
-rw-r--r--converter/ppm/winicontoppm.c822
1 files changed, 428 insertions, 394 deletions
diff --git a/converter/ppm/winicontoppm.c b/converter/ppm/winicontoppm.c
index ad859c94..6b1376b2 100644
--- a/converter/ppm/winicontoppm.c
+++ b/converter/ppm/winicontoppm.c
@@ -31,10 +31,9 @@
 #define MAJVERSION 0
 #define MINVERSION 4
 
-static int file_offset = 0;    /* not actually used, but useful for debug */
+static int fileOffset = 0;    /* not actually used, but useful for debug */
 static const char     er_read[] = "%s: read error";
 static const char *   infname;
-static FILE *   ifp;
 
 struct cmdlineInfo {
     /* All the information the user supplied in the command line,
@@ -53,8 +52,8 @@ struct cmdlineInfo {
 
 
 static void
-parseCommandLine ( int argc, char ** argv,
-                   struct cmdlineInfo *cmdlineP ) {
+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.  
@@ -65,13 +64,15 @@ 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 optParseOptions3 on how to parse our options.
+    optEntry * option_def;
+        /* Instructions to pm_optParseOptions3 on how to parse our options.
          */
     optStruct3 opt;
 
     unsigned int option_def_index;
 
+    MALLOCARRAY(option_def, 100);
+
     option_def_index = 0;   /* incremented by OPTENT3 */
     OPTENT3(0, "allicons",     OPT_FLAG,   NULL,                  
             &cmdlineP->allicons,       0 );
@@ -88,10 +89,9 @@ parseCommandLine ( int argc, char ** argv,
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
     opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
 
-    optParseOptions3( &argc, argv, opt, sizeof(opt), 0);
+    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
-
     if (argc-1 < 1) 
         cmdlineP->inputFilespec = "-";
     else
@@ -116,37 +116,37 @@ parseCommandLine ( int argc, char ** argv,
 
 
 static int 
-GetByte(void) {
+GetByte(FILE * const ifP) {
+
     int v;
    
-    if ((v = getc(ifp)) == EOF)
-    {
+    v = getc(ifP);
+    if (v == EOF)
         pm_error(er_read, infname);
-    }
    
     return v;
 }
+
+
    
 static short 
-GetShort(void) {
+GetShort(FILE * const ifP) {
+
     short v;
    
-    if (pm_readlittleshort(ifp, &v) == -1)
-    {
-        pm_error(er_read, infname);
-    }
-   
+    pm_readlittleshort(ifP, &v);
+
     return v;
 }
+
+
    
 static long 
-GetLong(void) {
+GetLong(FILE * const ifP) {
+
     long v;
    
-    if (pm_readlittlelong(ifp, &v) == -1)
-    {
-        pm_error(er_read, infname);
-    }
+    pm_readlittlelong(ifP, &v);
    
     return v;
 }
@@ -158,14 +158,18 @@ GetLong(void) {
  * functions.
  */
 static u1 
-readU1 (void) {
-    file_offset++;
-    return GetByte();
+readU1(FILE * const ifP) {
+
+    ++fileOffset;
+
+    return GetByte(ifP);
 }
 
+
+
 static u1 * 
-readU1String (int length)
-{
+readU1String (FILE *       const ifP,
+              unsigned int const length) {
    
     u1 * string;
     
@@ -173,150 +177,173 @@ readU1String (int length)
     if (string == NULL)
         pm_error("out of memory");
 
-    fread(string,sizeof(u1),length,ifp);
+    fread(string, sizeof(u1), length, ifP);
     string[length] = 0;
-    file_offset += length * sizeof(u1);
+    fileOffset += length * sizeof(u1);
+
     return string;
 }
 
+
+
 static u2 
-readU2 (void) {
-    file_offset +=2;
-    return GetShort();
+readU2 (FILE * const ifP) {
+
+    fileOffset +=2;
+
+    return GetShort(ifP);
 }
 
+
+
 static u4 
-readU4 (void) {
-    file_offset += 4;
-    return GetLong();
+readU4 (FILE * const ifP) {
+
+    fileOffset += 4;
+
+    return GetLong(ifP);
 }
 
+
+
 static IC_Entry 
-readICEntry (void) 
-{
-    IC_Entry entry;
+readICEntry(FILE * const ifP) {
+
+    IC_Entry entryP;
 
-    MALLOCVAR(entry);
+    MALLOCVAR(entryP);
 
-    if (entry == NULL)
+    if (entryP == NULL)
         pm_error("Unable to allcoate memory for IC entry");
 
-    entry->width         = readU1();
-    entry->height        = readU1();
-    entry->color_count   = readU1();
-    entry->reserved      = readU1();
-    entry->planes        = readU2();
-    entry->bitcount      = readU2();
-    entry->size_in_bytes = readU4();
-    entry->file_offset   = readU4();
-    entry->colors        = NULL;
-    entry->ih            = NULL;
-    entry->xorBitmap     = NULL;
-    entry->andBitmap     = NULL;
+    entryP->width         = readU1(ifP);
+    entryP->height        = readU1(ifP);
+    entryP->color_count   = readU1(ifP);
+    entryP->reserved      = readU1(ifP);
+    entryP->planes        = readU2(ifP);
+    entryP->bitcount      = readU2(ifP);
+    entryP->size_in_bytes = readU4(ifP);
+    entryP->file_offset   = readU4(ifP);
+    entryP->colors        = NULL;
+    entryP->ih            = NULL;
+    entryP->xorBitmap     = NULL;
+    entryP->andBitmap     = NULL;
     
-    return entry;
+    return entryP;
 }
 
 
 
 static IC_InfoHeader 
-readInfoHeader (IC_Entry entry) 
-{
-    IC_InfoHeader ih;
+readInfoHeader (FILE *   const ifP,
+                IC_Entry const entryP) {
+
+    IC_InfoHeader ihP;
 
-    MALLOCVAR(ih);
+    MALLOCVAR(ihP);
     
-    if (ih == NULL)
+    if (ihP == NULL)
         pm_error("Unable to allocate memory for info header");
 
-    ih->size            = readU4();
-    ih->width           = readU4();
-    ih->height          = readU4();
-    ih->planes          = readU2();
-    ih->bitcount        = readU2();
-    ih->compression     = readU4();
-    ih->imagesize       = readU4();
-    ih->x_pixels_per_m  = readU4();
-    ih->y_pixels_per_m  = readU4();
-    ih->colors_used     = readU4();
-    ih->colors_important = readU4();
+    ihP->size             = readU4(ifP);
+    ihP->width            = readU4(ifP);
+    ihP->height           = readU4(ifP);
+    ihP->planes           = readU2(ifP);
+    ihP->bitcount         = readU2(ifP);
+    ihP->compression      = readU4(ifP);
+    ihP->imagesize        = readU4(ifP);
+    ihP->x_pixels_per_m   = readU4(ifP);
+    ihP->y_pixels_per_m   = readU4(ifP);
+    ihP->colors_used      = readU4(ifP);
+    ihP->colors_important = readU4(ifP);
     
-    if (!entry->bitcount) entry->bitcount = ih->bitcount;
-    if (entry->color_count == 0 && 
-        entry->bitcount <= 8) entry->color_count = 256;
-    if (ih->compression) {
+    if (!entryP->bitcount)
+        entryP->bitcount = ihP->bitcount;
+    
+    if (entryP->color_count == 0 && entryP->bitcount <= 8)
+        entryP->color_count = 256;
+
+    if (ihP->compression) {
         pm_error("Can't handle compressed icons");
     }
-    return ih;
+    return ihP;
 }
 
-/*
- * I don't know why this isn't the same as the spec, it just <b>isn't</b>
- * The colors honestly seem to be stored BGR.  Bizarre.
- * 
- * I've checked this in the BMP code for bmptoppm and the gimp.  Guess the
- * spec I have is just plain wrong.
- */
+
+
 static IC_Color 
-readICColor (void) 
-{
-    IC_Color col;
+readICColor(FILE * const ifP)  {
+
+    IC_Color colorP;
 
-    MALLOCVAR(col);
+    MALLOCVAR(colorP);
 
-    if (col == NULL)
+    if (colorP == NULL)
         pm_error("Unable to allocate memory for color");
 
-    col->blue     = readU1();
-    col->green    = readU1();
-    col->red      = readU1();
-    col->reserved = readU1();
-    return col;
+    /* I don't know why this isn't the same as the spec, it just isn't.
+       The colors honestly seem to be stored BGR.  Bizarre.
+      
+       I've checked this in the BMP code for bmptoppm and the gimp.  Guess the
+       spec I have is just plain wrong.
+    */
+
+    colorP->blue     = readU1(ifP);
+    colorP->green    = readU1(ifP);
+    colorP->red      = readU1(ifP);
+    colorP->reserved = readU1(ifP);
+
+    return colorP;
 }
    
 
 
-/*
- * Depending on if the image is stored as 1bpp, 4bpp or 8bpp, the 
- * encoding mechanism is different.
- * 
- * 8bpp => 1 byte/palette index.
- * 4bpp => High Nibble, Low Nibble
- * 1bpp => 1 palette value per bit, high bit 1st.
- */
 static u1 * 
-read1Bitmap (int width, int height) 
-{
-    int tmp;
-    int xBytes;
+read1Bitmap (FILE *       const ifP,
+             unsigned int const width,
+             unsigned int const height) {
+
+    unsigned int row;
+    unsigned int xByteCt;
     u1 * bitmap;
-    int wt = width;
+    unsigned int wt;
 
     MALLOCARRAY(bitmap, width * height);
     if (bitmap == NULL)
         pm_error("out of memory");
 
-    wt >>= 3;
-    if (wt & 3) {
-        wt = (wt & ~3) + 4;
+    /* Depending on if the image is stored as 1bpp, 4bpp or 8bpp, the 
+       encoding mechanism is different.
+  
+       8bpp => 1 byte/palette index.
+       4bpp => High Nibble, Low Nibble
+       1bpp => 1 palette value per bit, high bit 1st.
+    */
+
+    wt = width >> 3;
+
+    if ((wt & 0x3) != 0) {
+        wt = (wt & ~0x3) + 4;
     }
-    xBytes = wt;
-    for (tmp = 0; tmp<height; tmp++ ) {
-        int x;
-        int rowByte = 0;
-        int xOrVal = 128;
-        u1 * row = readU1String(xBytes);
-        for (x = 0; x< width; x++) {
-            *(bitmap+((height-tmp-1)*width) + (x)) = 
-                (row[rowByte] & xOrVal) / xOrVal;
-            if (xOrVal == 1) {
-                xOrVal = 128;
-                rowByte++;
-            } else {
+    xByteCt = wt;
+
+    for (row = 0; row < height; ++row) {
+        u1 * const imgRow = readU1String(ifP, xByteCt);
+
+        unsigned int col;
+        unsigned int rowByte;
+        unsigned int xOrVal;
+        
+        for (col = 0, rowByte = 0, xOrVal = 0x80; col < width; ++col) {
+            *(bitmap+((height - row - 1) * width) + col) = 
+                (imgRow[rowByte] & xOrVal) / xOrVal;
+            if (xOrVal == 0x01) {
+                xOrVal = 0x80;
+                ++rowByte;
+            } else
                 xOrVal >>= 1;
-            }
         }
-        free(row);
+        free(imgRow);
     }
     return bitmap;
 }
@@ -324,43 +351,46 @@ read1Bitmap (int width, int height)
 
    
 static u1 * 
-read4Bitmap (int width, int height) 
-{
-    int tmp;
+read4Bitmap (FILE *       const ifP,
+             unsigned int const width,
+             unsigned int const height) {
+
+    unsigned int row;
     u1 * bitmap;
 
-    int wt = width;
-    int xBytes;
+    unsigned int wt;
+    unsigned int xByteCt;
 
     MALLOCARRAY(bitmap, width * height);
     if (bitmap == NULL)
         pm_error("out of memory");
 
+    wt = width >> 1;
 
-    wt >>= 1;
-    if (wt & 3) {
-        wt = (wt & ~3) + 4;
+    if (wt & 0x3) {
+        wt = (wt & ~0x3) + 4;
     }
-    xBytes = wt;
-    for (tmp = 0; tmp<height ; tmp++ ) {
-        int rowByte = 0;
-        int bottom = 1;
-        int x;
-        u1 * row = readU1String(xBytes);
-        for (x = 0; x< width; x++) {
-            /*
-             * 2 nibbles, 2 values.
-             */
+    xByteCt = wt;
+
+    for (row = 0; row < height; ++row) {
+        u1 * const imgRow = readU1String(ifP, xByteCt);
+
+        unsigned int rowByte;
+        bool bottom;
+        unsigned int col;
+        for (col = 0, rowByte = 0, bottom = true; col < width; ++col) {
+            /* 2 nibbles, 2 values */
             if (bottom) {
-                *(bitmap+((height-tmp-1)*width) + (x)) = 
-                    (row[rowByte] & 0xF0) >> 4;
+                *(bitmap + ((height - row - 1) * width) + col) = 
+                    (imgRow[rowByte] & 0xF0) >> 4;
             } else {
-                *(bitmap+((height-tmp-1)*width) + (x)) = (row[rowByte] & 0xF);
-                rowByte++;
+                *(bitmap + ((height - row -1) * width) + col) =
+                    (imgRow[rowByte] & 0xF);
+                ++rowByte;
             }
             bottom = !bottom;
         }
-    free(row);
+        free(imgRow);
     }
     return bitmap;
 }
@@ -368,52 +398,57 @@ read4Bitmap (int width, int height)
 
    
 static u1 * 
-read8Bitmap (int width, int height) 
-{
-    int tmp;
-    unsigned int xBytes;
-    unsigned int wt = width;
+read8Bitmap (FILE *       const ifP,
+             unsigned int const width,
+             unsigned int const height) {
+
+    unsigned int row;
+    unsigned int xByteCt;
+    unsigned int wt;
     u1 * bitmap;
    
     MALLOCARRAY(bitmap, width * height);
     if (bitmap == NULL)
         pm_error("out of memory");
 
-    if (wt & 3) {
-        wt = (wt & ~3) + 4;
+    wt = width;
+    if (wt & 0x3) {
+        wt = (wt & ~0x3) + 4;
     }
-    xBytes = wt;
-    for (tmp = 0; tmp<height ; tmp++ ) {
-        int rowByte = 0;
-        int x;
-        u1 * row = readU1String(xBytes);
-        for ( x = 0; x< width; x++) {
-            *(bitmap+((height-tmp-1)*width) + (x)) = row[rowByte];
-            rowByte++;
-        }
-        free(row);
+    xByteCt = wt;
+
+    for (row = 0; row < height; ++row) {
+        u1 * imgRow = readU1String(ifP, xByteCt);
+
+        unsigned int rowByte;
+        unsigned int col;
+        for (col = 0, rowByte = 0; col < width; ++col)
+            *(bitmap + ((height - row - 1) * width) + col) = imgRow[rowByte++];
+
+        free(imgRow);
     }
     return bitmap;
 }
 
 
 
-/*
- * Read a true color bitmap. (24/32 bits)
- * 
- * The output routine deplanarizes it for us, we keep it flat here.
- */
 static u1 *
-readXBitmap (int const width, 
-             int const height, 
-             int const bpp) {
-    int          const bytes  = bpp >> 3;
-    unsigned int const xBytes = width * bytes;
+readXBitmap (FILE *       const ifP,
+             unsigned int const width, 
+             unsigned int const height, 
+             unsigned int const bpp) {
+/*----------------------------------------------------------------------------
+  Read a true color bitmap. (24/32 bits)
+  
+  The output routine deplanarizes it for us, we keep it flat here.
+-----------------------------------------------------------------------------*/
+    unsigned int const byteCt = bpp >> 3;
+    unsigned int const xByteCt = width * byteCt;
 
     u1 * bitmap;
         /* remember - bmp (dib) stored upside down, so reverse */
 
-    MALLOCARRAY(bitmap, bytes * width * height);
+    MALLOCARRAY(bitmap, byteCt * width * height);
     if (bitmap == NULL)
         pm_error("out of memory allocating bitmap array");
 
@@ -421,12 +456,12 @@ readXBitmap (int const width,
         unsigned int i;
         u1 * bitcurptr;
 
-        for (i = 0, bitcurptr = &bitmap[bytes * width * (height-1)];
+        for (i = 0, bitcurptr = &bitmap[byteCt * width * (height-1)];
              i < height; 
-             ++i, bitcurptr -= xBytes) {
+             ++i, bitcurptr -= xByteCt) {
 
-            u1 * const row = readU1String(xBytes);
-            memcpy(bitcurptr, row, xBytes);
+            u1 * const row = readU1String(ifP, xByteCt);
+            memcpy(bitcurptr, row, xByteCt);
             free(row);
         }
     }
@@ -436,80 +471,70 @@ readXBitmap (int const width,
 
 
 static MS_Ico 
-readIconFile (bool const verbose) {
-    int iter,iter2;
+readIconFile(FILE * const ifP,
+             bool   const verbose) {
+
+    unsigned int i;
 
     MS_Ico MSIconData;
 
     MALLOCVAR(MSIconData);
    
-    /*
-     * reserved - should equal 0.
-     */
-    MSIconData->reserved = readU2();
-    /*
-     * Type - should equal 1
-     */
-    MSIconData->type     = readU2();
-    /*
-     * count - no of icons in file..
-     */
-    MSIconData->count    = readU2();
-    /*
-     * Allocate "count" array of entries.
-     */
+    MSIconData->reserved = readU2(ifP);  /* should be 0 */
+    MSIconData->type     = readU2(ifP);  /* should be 1 */
+    MSIconData->count    = readU2(ifP);  /* # icons in file */
+
     if (verbose) 
         pm_message("Icon file contains %d icons.", MSIconData->count);
 
     MALLOCARRAY(MSIconData->entries, MSIconData->count);
     if (MSIconData->entries == NULL)
         pm_error("out of memory");
-    /*
-     * Read in each of the entries
-     */
-    for (iter = 0;iter < MSIconData->count ; iter++ ) {
-        MSIconData->entries[iter] = readICEntry();
-    }
-    /* After that, we have to read in the infoheader, color map (if
-     * any) and the actual bit/pix maps for the icons.  
-     */
+
+    /* Read in each of the entries */
+    for (i = 0; i < MSIconData->count; ++i)
+        MSIconData->entries[i] = readICEntry(ifP);
+
+    /* Read in the infoheader, color map (if any) and the actual bit/pix maps
+       for the icons.
+    */
     if (verbose) 
-        fprintf (stderr,"#\tColors\tBPP\tWidth\tHeight\n");
-    for (iter = 0;iter < MSIconData->count ; iter++ ) {
-        int bpp;
-        MSIconData->entries[iter]->ih =
-            readInfoHeader (MSIconData->entries[iter]);
+        pm_message("#\tColors\tBPP\tWidth\tHeight\n");
+
+    for (i = 0; i < MSIconData->count; ++i) {
+        unsigned int bpp;  /* bits per pixel */
+
+        MSIconData->entries[i]->ih =
+            readInfoHeader(ifP, MSIconData->entries[i]);
        
-        /* What's the bits per pixel? */
-        bpp = MSIconData->entries[iter]->bitcount; 
+        bpp = MSIconData->entries[i]->bitcount; 
+
         /* Read the palette, if appropriate */
         switch (bpp) {
         case 24:
         case 32:
             /* 24/32 bpp icon has no palette */
             break;
-        default:
-            MALLOCARRAY(MSIconData->entries[iter]->colors, 
-                        MSIconData->entries[iter]->color_count);
-            if (MSIconData->entries[iter]->colors == NULL)
+        default: {
+            unsigned int j;
+
+            MALLOCARRAY(MSIconData->entries[i]->colors, 
+                        MSIconData->entries[i]->color_count);
+            if (MSIconData->entries[i]->colors == NULL)
                 pm_error("out of memory");
 
-            for (iter2 = 0;
-                 iter2 < MSIconData->entries[iter]->color_count ; 
-                 iter2++ ) {
-                MSIconData->entries[iter]->colors[iter2] = readICColor();
-            }
-            break;
+            for (j = 0; j < MSIconData->entries[i]->color_count; ++j)
+                MSIconData->entries[i]->colors[j] = readICColor(ifP);
+        }
         }
         if (verbose) {
-            char cols_text[10];
-            sprintf (cols_text, "%d", MSIconData->entries[iter]->color_count);
-            fprintf (stderr,
-                     "%d\t%s\t%d\t%d\t%d\n", iter,
-                     MSIconData->entries[iter]->color_count ? 
-                     cols_text : "TRUE",
-                     bpp, MSIconData->entries[iter]->width, 
-                     MSIconData->entries[iter]->height);
+            char colsText[10];
+            sprintf (colsText, "%d", MSIconData->entries[i]->color_count);
+            pm_message("%d\t%s\t%d\t%d\t%d\n", i,
+                       MSIconData->entries[i]->color_count ? 
+                       colsText : "TRUE",
+                       bpp, MSIconData->entries[i]->width, 
+                       MSIconData->entries[i]->height);
         }
         /* Pixels are stored bottom-up, left-to-right. Pixel lines are
          * padded with zeros to end on a 32bit (4byte) boundary. Every
@@ -528,35 +553,40 @@ readIconFile (bool const verbose) {
              */
             switch (bpp) {
             case 1:
-                MSIconData->entries[iter]->xorBitmap = 
-                    read1Bitmap(MSIconData->entries[iter]->width,
-                                MSIconData->entries[iter]->height);
+                MSIconData->entries[i]->xorBitmap = 
+                    read1Bitmap(ifP,
+                                MSIconData->entries[i]->width,
+                                MSIconData->entries[i]->height);
                 break;
             case 4:
-                MSIconData->entries[iter]->xorBitmap = 
-                    read4Bitmap(MSIconData->entries[iter]->width,
-                                MSIconData->entries[iter]->height);
+                MSIconData->entries[i]->xorBitmap = 
+                    read4Bitmap(ifP,
+                                MSIconData->entries[i]->width,
+                                MSIconData->entries[i]->height);
                 break;
             case 8:
-                MSIconData->entries[iter]->xorBitmap = 
-                    read8Bitmap(MSIconData->entries[iter]->width,
-                                MSIconData->entries[iter]->height);
+                MSIconData->entries[i]->xorBitmap = 
+                    read8Bitmap(ifP,
+                                MSIconData->entries[i]->width,
+                                MSIconData->entries[i]->height);
                 break;
             case 24:
             case 32:
-                MSIconData->entries[iter]->xorBitmap = 
-                    readXBitmap(MSIconData->entries[iter]->width,
-                                MSIconData->entries[iter]->height,bpp);
+                MSIconData->entries[i]->xorBitmap = 
+                    readXBitmap(ifP,
+                                MSIconData->entries[i]->width,
+                                MSIconData->entries[i]->height,bpp);
                 break;
             default:
-                pm_error("Uncatered bit depth %d",bpp);
+                pm_error("Uncatered bit depth %u", bpp);
             }
             /*
              * Read AND Bitmap
              */
-            MSIconData->entries[iter]->andBitmap = 
-                read1Bitmap(MSIconData->entries[iter]->width,
-                            MSIconData->entries[iter]->height);
+            MSIconData->entries[i]->andBitmap = 
+                read1Bitmap(ifP,
+                            MSIconData->entries[i]->width,
+                            MSIconData->entries[i]->height);
         }
       
     }
@@ -566,15 +596,14 @@ readIconFile (bool const verbose) {
 
 
 static char * 
-trimOutputName(const char inputName[])
-{
+trimmedOutputName(const char inputName[]) {
     /*
      * Just trim off the final ".ppm", if there is one, else return as is.
      * oh, for =~ ... :)
      */
-    char * outFile = strdup(inputName);
-    if (streq(outFile + (strlen (outFile) - 4), ".ppm")) {
-        *(outFile + (strlen (outFile) - 4)) = 0;
+    char * const outFile = strdup(inputName);
+    if (streq(outFile + (strlen(outFile) - 4), ".ppm")) {
+        *(outFile + (strlen(outFile) - 4)) = 0;
     }
     return outFile;
 
@@ -585,29 +614,34 @@ trimOutputName(const char inputName[])
 static int 
 getBestQualityIcon(MS_Ico MSIconData)
 {
-    int x,best,best_size,best_bpp,bpp,size;
-    IC_Entry entry;
-
-    best_size = best_bpp = 0;
-    for (x = 0; x < MSIconData->count; x++) {
-        entry =  MSIconData->entries[x];
-        size = entry->width * entry->height;
-        bpp  = entry->bitcount ? entry->bitcount : entry->ih->bitcount;
-        if (size > best_size) {
-            best = x;
-            best_size = size;
-        } else if (size == best_size && bpp > best_bpp) {
-            best = x;
-            best_bpp = bpp;
+    unsigned int i;
+    unsigned int best;
+    unsigned int bestSize;
+    unsigned int bestBpp;
+
+    for (i = 0, bestSize = 0, bestBpp = 0; i < MSIconData->count; ++i) {
+        IC_Entry const entryP = MSIconData->entries[i];
+        unsigned int const size = entryP->width * entryP->height;
+        unsigned int const bpp  =
+            entryP->bitcount ? entryP->bitcount : entryP->ih->bitcount;
+
+        if (size > bestSize) {
+            best = i;
+            bestSize = size;
+        } else if (size == bestSize && bpp > bestBpp) {
+            best = i;
+            bestBpp = bpp;
         }
     }
     return best;
 }
 
+
+
 static void
 writeXors(FILE *   const multiOutF,
-          char           outputFileBase[], 
-          IC_Entry const entry,
+          char *   const outputFileBase,
+          IC_Entry const entryP,
           int      const entryNum,
           bool     const multiple, 
           bool     const xor) {
@@ -625,85 +659,79 @@ writeXors(FILE *   const multiOutF,
    we are to open a file using outputFileBase[] and 'entryNum' and 'xor'
    to derive its name, and close it afterward.
 -----------------------------------------------------------------------------*/
-    FILE * outF;
-    pixel ** ppm_array;
-    int row;
-    int pel_size;
-    const char *outputFile;
-    int maxval;
-    int forcetext;
+    FILE * ofP;
+    pixel ** pixArray;
+    unsigned int row;
+    const char * outputFileName;
 
     if (multiOutF) {
-        outF = multiOutF;
-        outputFile = strdup("");
+        ofP = multiOutF;
+        outputFileName = strdup("");
     } else {
         if (outputFileBase) {
             if (multiple) {
-                asprintfN(&outputFile, "%s%s_%d.ppm",
-                          outputFileBase,(xor ? "_xor" : ""), entryNum);
+                pm_asprintf(&outputFileName, "%s%s_%d.ppm",
+                            outputFileBase,(xor ? "_xor" : ""), entryNum);
             } else { 
-                asprintfN(&outputFile, "%s%s.ppm",
-                          outputFileBase,(xor ? "_xor" : ""));
+                pm_asprintf(&outputFileName, "%s%s.ppm",
+                            outputFileBase,(xor ? "_xor" : ""));
             }
         } else
-            outputFile = strdup("-");
+            outputFileName = strdup("-");
         
-        outF = pm_openw(outputFile);
+        ofP = pm_openw(outputFileName);
     }
     /* 
-     * allocate an array to save the bmp data into.
-     * note that entry->height will be 1/2 entry->ih->height,
-     * as the latter adds "and" and "xor" height.
-     */
-    ppm_array = ppm_allocarray(entry->width, entry->height);
-    for (row=0; row < entry->height; row++) {
-        u1 * xorRow;
-        int col;
-        switch (entry->bitcount) {
+       Allocate an array to save the bmp data into.
+       note that entry->height will be 1/2 entry->ih->height,
+       as the latter adds "and" and "xor" height.
+    */
+    pixArray = ppm_allocarray(entryP->width, entryP->height);
+    for (row = 0; row < entryP->height; ++row) {
+        switch (entryP->bitcount) {
         case 24:
-        case 32:
-            pel_size = entry->bitcount >> 3;
-            xorRow = entry->xorBitmap + row * entry->width * pel_size;
-            for (col=0; col < entry->width*pel_size;col+=pel_size) {
-                PPM_ASSIGN(ppm_array[row][col/pel_size],
-                           xorRow[col+2],xorRow[col+1],xorRow[col]);
-            }
-            break;
-        default:
-            xorRow = entry->xorBitmap + row * entry->width;
-            for (col=0; col < entry->width; col++) {
-                int colorIndex;
-                IC_Color color;
-                colorIndex  = xorRow[col];
-                color = entry->colors[colorIndex];
-                PPM_ASSIGN(ppm_array[row][col],
-                           color->red,color->green,color->blue);
+        case 32: {
+            unsigned int const pixelSize = entryP->bitcount >> 3;
+            u1 * const xorRow =
+                entryP->xorBitmap + row * entryP->width * pixelSize;
+            unsigned int col;
+            for (col = 0; col < entryP->width * pixelSize; col += pixelSize)
+                PPM_ASSIGN(pixArray[row][col/pixelSize],
+                           xorRow[col+2], xorRow[col+1], xorRow[col]);
+        } break;
+        default: {
+            u1 * const xorRow = entryP->xorBitmap + row * entryP->width;
+            unsigned int col;
+            for (col = 0; col < entryP->width; ++col) {
+                unsigned int const colorIndex = xorRow[col];
+                IC_Color const colorP = entryP->colors[colorIndex];
+                PPM_ASSIGN(pixArray[row][col],
+                           colorP->red, colorP->green, colorP->blue);
             }
-            break;
+        } break;
         }
     }    
     
-    maxval = 255;
-    forcetext = 0;
-
-    ppm_writeppm(outF,ppm_array,entry->width, entry->height, 
-                 (pixval) maxval, forcetext);
-    ppm_freearray(ppm_array,entry->height);
+    ppm_writeppm(ofP, pixArray, entryP->width, entryP->height, 
+                 255 /* maxval */, false /* text */);
+    ppm_freearray(pixArray, entryP->height);
 
-    strfree(outputFile);
+    pm_strfree(outputFileName);
     
     if (!multiOutF) 
-        pm_close(outF);
+        pm_close(ofP);
 }
             
 
 
 static void
-writeAnds(FILE * const multiOutF, 
-          char outputFileBase[], IC_Entry const entry, int const entryNum, 
-          bool multiple) {
+writeAnds(FILE *       const multiOutF, 
+          char         const outputFileBase[],
+          IC_Entry     const entryP,
+          unsigned int const entryNum, 
+          bool         const multiple) {
 /*----------------------------------------------------------------------------
-   Write the "and" image (i.e. the alpha mask) of the image 'IC_Entry' out.
+   Write the "and" image (i.e. the alpha mask) of the image *entryP out.
 
    'multiple' means this is one of multiple images that are being written.
    'entryNum' is the sequence number within the winicon file of the image
@@ -714,114 +742,122 @@ writeAnds(FILE * const multiOutF,
    we are to open a file using outputFileBase[] and 'entryNum' and 'xor'
    to derive its name, and close it afterward.
 -----------------------------------------------------------------------------*/
-    FILE * outF;
-    bit ** pbm_array;
-    u1 * andRow;
-    int row;
+    FILE * ofP;
+    bit ** bitArray;
+    unsigned int row;
 
     if (multiOutF)
-        outF = multiOutF;
+        ofP = multiOutF;
     else {
-        const char *outputFile;
+        const char * outputFileName;
 
         assert(outputFileBase);
 
         if (multiple) 
-            asprintfN(&outputFile, "%s_and_%d.pbm", outputFileBase, entryNum);
+            pm_asprintf(&outputFileName, "%s_and_%u.pbm",
+                        outputFileBase, entryNum);
         else 
-            asprintfN(&outputFile, "%s_and.pbm", outputFileBase);
-        outF = pm_openw(outputFile);
-        strfree(outputFile);
+            pm_asprintf(&outputFileName, "%s_and.pbm", outputFileBase);
+        ofP = pm_openw(outputFileName);
+        pm_strfree(outputFileName);
     }
-    pbm_array = pbm_allocarray(entry->width, entry->height);
-    for (row=0; row < entry->height; row++) {
-        int col;
-        andRow = entry->andBitmap + row * entry->width;
-        for (col=0; col < entry->width; col++) {
+    bitArray = pbm_allocarray(entryP->width, entryP->height);
+    for (row = 0; row < entryP->height; ++row) {
+        u1 * const andRow = entryP->andBitmap + row * entryP->width;
+        unsigned int col;
+        for (col = 0; col < entryP->width; ++col) {
             /* Note: black is transparent in a Netpbm alpha mask */
-            pbm_array[row][col] = andRow[col] ? PBM_BLACK: PBM_WHITE;
+            bitArray[row][col] = andRow[col] ? PBM_BLACK: PBM_WHITE;
         }
     }
 
-    pbm_writepbm(outF, pbm_array, entry->width, entry->height, 0);
+    pbm_writepbm(ofP, bitArray, entryP->width, entryP->height, 0);
 
-    pbm_freearray(pbm_array, entry->height);
+    pbm_freearray(bitArray, entryP->height);
     if (!multiOutF)
-        pm_close (outF);     
+        pm_close(ofP);
 }
 
 
 
 static void
-openMultiXor(char          outputFileBase[], 
+openMultiXor(char    const outputFileBase[], 
              bool    const writeands,
              FILE ** const multiOutFP) {
 
-    const char *outputFile;
+    const char * outputFileName;
 
     if (outputFileBase) {
-        asprintfN(&outputFile, "%s%s.ppm",
-                  outputFileBase, (writeands ? "_xor" : ""));
+        pm_asprintf(&outputFileName, "%s%s.ppm",
+                    outputFileBase, (writeands ? "_xor" : ""));
     } else
-        outputFile = strdup("-");
+        outputFileName = strdup("-");
 
-    /*
-     * Open the output file now, it'll stay open the whole time.
-     */
-    *multiOutFP = pm_openw(outputFile);
+    *multiOutFP = pm_openw(outputFileName);
 
-    strfree(outputFile);
+    pm_strfree(outputFileName);
 }
 
 
 
 static void
-openMultiAnd(char outputFileBase[], FILE ** const multiAndOutFP) {
+openMultiAnd(char    const outputFileBase[],
+             FILE ** const multiAndOutFP) {
 
-    const char *outputFile;
+    const char * outputFileName;
 
     assert(outputFileBase);
 
-    asprintfN(&outputFile, "%s_and.pbm", outputFileBase);
+    pm_asprintf(&outputFileName, "%s_and.pbm", outputFileBase);
     
-    *multiAndOutFP = pm_openw(outputFile);
+    *multiAndOutFP = pm_openw(outputFileName);
 
-    strfree(outputFile);
+    pm_strfree(outputFileName);
 }
 
-static void free_iconentry(IC_Entry entry) {
-    int x;
-    if (entry->colors && entry->color_count) {
-        for (x=0;x<entry->color_count;x++) free(entry->colors[x]);
-        free(entry->colors);
+
+
+static void
+freeIconentry(IC_Entry const entryP) {
+
+    if (entryP->colors && entryP->color_count) {
+        unsigned int i;
+        for (i = 0; i <entryP->color_count; ++i)
+            free(entryP->colors[i]);
+        free(entryP->colors);
     }
-    if (entry->andBitmap) free(entry->andBitmap);
-    if (entry->xorBitmap) free(entry->xorBitmap);
-    if (entry->ih) free(entry->ih);
-    free(entry);
+    if (entryP->andBitmap) free(entryP->andBitmap);
+    if (entryP->xorBitmap) free(entryP->xorBitmap);
+    if (entryP->ih) free(entryP->ih);
+    free(entryP);
 }
 
-static void free_icondata(MS_Ico MSIconData)
-{
-    int x;
-    for (x=0;x<MSIconData->count;x++) {
-    free_iconentry(MSIconData->entries[x]);
-    }
-    free(MSIconData);
+
+
+static void
+freeIcondata(MS_Ico const MSIconDataP) {
+
+    unsigned int i;
+    for (i = 0; i < MSIconDataP->count; ++i)
+        freeIconentry(MSIconDataP->entries[i]);
+
+    free(MSIconDataP);
 }
 
 
+
 int 
-main(int argc, char *argv[]) {
+main(int argc, const char *argv[]) {
 
     struct cmdlineInfo cmdline;
-    int startEntry, endEntry;
-    MS_Ico MSIconData;
+    FILE * ifP;
+    unsigned int startEntry, endEntry;
+    MS_Ico MSIconDataP;
     char * outputFileBase;
     FILE * multiOutF;
     FILE * multiAndOutF;
    
-    ppm_init (&argc, argv);
+    pm_proginit(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
@@ -832,13 +868,13 @@ main(int argc, char *argv[]) {
     if (streq(cmdline.outputFilespec, "-"))
         outputFileBase = NULL;
     else
-        outputFileBase = trimOutputName(cmdline.outputFilespec);
+        outputFileBase = trimmedOutputName(cmdline.outputFilespec);
 
-    ifp = pm_openr(cmdline.inputFilespec);
+    ifP = pm_openr(cmdline.inputFilespec);
 
     infname = cmdline.inputFilespec;
 
-    MSIconData = readIconFile(cmdline.verbose);
+    MSIconDataP = readIconFile(ifP, cmdline.verbose);
     /*
      * Now we've read the icon file in (Hopefully! :)
      * Go through each of the entries, and write out files of the
@@ -852,18 +888,15 @@ main(int argc, char *argv[]) {
     /*
      * If allicons is set, we want everything, if not, just go through once.
      */
-    startEntry = 0;
-    if (cmdline.allicons) {
-        endEntry = MSIconData->count;
-    } else {
-        endEntry = 1;
-    }
-    /*
-     * If bestqual is set, find the icon with highest size & bpp.
-     */
     if (cmdline.bestqual) {
-        startEntry = getBestQualityIcon(MSIconData);
+        startEntry = getBestQualityIcon(MSIconDataP);
         endEntry = startEntry+1;
+    } else {
+        startEntry = 0;
+        if (cmdline.allicons)
+            endEntry = MSIconDataP->count;
+        else
+            endEntry = 1;
     }
    
     if (cmdline.multippm) 
@@ -877,24 +910,25 @@ main(int argc, char *argv[]) {
         multiAndOutF = NULL;
 
     {
-        int entryNum;
+        unsigned int entryNum;
 
-        for (entryNum = startEntry ; entryNum < endEntry ; entryNum++ ) {
-            IC_Entry const entry = MSIconData->entries[entryNum];
+        for (entryNum = startEntry; entryNum < endEntry; ++entryNum) {
+            IC_Entry const entryP = MSIconDataP->entries[entryNum];
 
-            writeXors(multiOutF, outputFileBase, entry, entryNum, 
+            writeXors(multiOutF, outputFileBase, entryP, entryNum, 
                       cmdline.allicons, cmdline.writeands);
             if (cmdline.writeands)
                 writeAnds(multiAndOutF, outputFileBase, 
-                          entry, entryNum, cmdline.allicons);
+                          entryP, entryNum, cmdline.allicons);
         }
     }
     if (multiOutF)
-        pm_close (multiOutF);    
+        pm_close(multiOutF);    
     if (multiAndOutF)
         pm_close(multiAndOutF);
     
     /* free up the image data here. */
-    free_icondata(MSIconData);
+    freeIcondata(MSIconDataP);
+
     return 0;
 }