about summary refs log tree commit diff
path: root/converter
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-06-27 02:19:40 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-06-27 02:19:40 +0000
commit55b0a6537ca1780ebc11321c4891693977437a86 (patch)
tree1fcfb55c127232d0d6335d775b519b82c5abdf7f /converter
parentad48579b703e1db92abc9f792c1907d862165232 (diff)
downloadnetpbm-mirror-55b0a6537ca1780ebc11321c4891693977437a86.tar.gz
netpbm-mirror-55b0a6537ca1780ebc11321c4891693977437a86.tar.xz
netpbm-mirror-55b0a6537ca1780ebc11321c4891693977437a86.zip
Rebase 'advanced' series to 10.43
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@658 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter')
-rw-r--r--converter/other/Makefile2
-rw-r--r--converter/other/pamtohtmltbl.c105
-rw-r--r--converter/other/pamtompfont.c179
-rw-r--r--converter/other/pamtotga.c2
-rw-r--r--converter/other/pamtouil.c56
-rw-r--r--converter/other/pnmtopclxl.c2
-rw-r--r--converter/pbm/pbmtogo.c216
7 files changed, 386 insertions, 176 deletions
diff --git a/converter/other/Makefile b/converter/other/Makefile
index 3e2c2faa..3a6bb2af 100644
--- a/converter/other/Makefile
+++ b/converter/other/Makefile
@@ -80,7 +80,7 @@ endif
 PORTBINARIES =  bmptopnm fitstopnm \
 		gemtopnm giftopnm hdifftopam infotopam \
 		pamtodjvurle pamtofits pamtogif \
-		pamtohdiff pamtohtmltbl pamtooctaveimg \
+		pamtohdiff pamtohtmltbl pamtompfont pamtooctaveimg \
 		pamtopam pamtopfm pamtopnm pamtouil \
 		pamtoxvmini \
 		pbmtopgm pfmtopam \
diff --git a/converter/other/pamtohtmltbl.c b/converter/other/pamtohtmltbl.c
index 0afbfea0..70aa3b2d 100644
--- a/converter/other/pamtohtmltbl.c
+++ b/converter/other/pamtohtmltbl.c
@@ -2,16 +2,16 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "pam.h"
-#include "shhopt.h"
 #include "mallocvar.h"
+#include "shhopt.h"
+#include "pam.h"
 
 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 */
-    const char *transparent;  /* NULL if none */
+    const char * inputFileName;  /* '-' if stdin */
+    const char * transparent;  /* NULL if none */
     unsigned int verbose;
 };
 
@@ -19,8 +19,8 @@ struct cmdlineInfo {
 
 
 static void
-parseCommandLine ( int argc, char ** argv,
-                   struct cmdlineInfo *cmdlineP ) {
+parseCommandLine(int argc, char ** argv,
+                 struct cmdlineInfo * const cmdlineP) {
 /*----------------------------------------------------------------------------
    parse program command line described in Unix standard form by argc
    and argv.  Return the information in the options as *cmdlineP.  
@@ -31,7 +31,7 @@ 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));
+    optEntry * option_def;
         /* Instructions to optParseOptions3 on how to parse our options.
          */
     optStruct3 opt;
@@ -40,6 +40,8 @@ parseCommandLine ( int argc, char ** argv,
 
     unsigned int transparentSpec;
 
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
     option_def_index = 0;   /* incremented by OPTENT3 */
     OPTENT3(0, "verbose",     OPT_FLAG,   NULL,                  
             &cmdlineP->verbose,       0 );
@@ -58,9 +60,9 @@ parseCommandLine ( int argc, char ** argv,
         cmdlineP->transparent = NULL;
 
     if (argc-1 < 1)
-        cmdlineP->inputFilespec = "-";
+        cmdlineP->inputFileName = "-";
     else if (argc-1 == 1)
-        cmdlineP->inputFilespec = argv[1];
+        cmdlineP->inputFileName = argv[1];
     else
         pm_error("Too many arguments.  Program takes at most one argument: "
                  "input file name");
@@ -135,31 +137,30 @@ findSameColorRectangle(struct pam *   const pamP,
     mx=0; my=0;
     cnx = pamP->width - col; cny = pamP->height - row;
 
-    for (i=0; (!mx)||(!my); i++) {
+    for (i = 0; !mx || !my; ++i) {
         int j;
-        /*fprintf(stderr,"\n[%d]",i);*/
-        for (j=0; j<=i; j++) {
+        for (j = 0; j <= i; ++j) {
             if (!my) {
-                if (i>=cny) 
-                    my=cny;
-                else
-                    if (((!mx) || (j<mx)) && (j < cnx)) {
-                        /*fprintf(stderr,"%d/%d ",j,i);*/
+                if (i >= cny) 
+                    my = cny;
+                else {
+                    if ((!mx || j < mx) && (j < cnx)) {
                         if (!pnm_tupleequal(pamP, tuples[row+i][col+j],
                                             rectangleColor)) 
                             my = i;
                     }
+                }
             }
             if (!mx) {
-                if (i>=cnx) 
-                    mx=cnx;
-                else
-                    if (((!my) || (j<my)) && (j < cny)) {
-                        /*fprintf(stderr,"%d/%d ",i,j);*/
+                if (i >= cnx) 
+                    mx = cnx;
+                else {
+                    if ((!my || (j < my)) && (j < cny)) {
                         if (!pnm_tupleequal(pamP, tuples[row+j][col+i],
                                             rectangleColor)) 
                             mx = i;
                     }
+                }
             }
         }
     }
@@ -170,7 +171,8 @@ findSameColorRectangle(struct pam *   const pamP,
 
 
 static bool **
-allocOutputtedArray(unsigned int const width, unsigned int const height) {
+allocOutputtedArray(unsigned int const width,
+                    unsigned int const height) {
 
     bool ** outputted;
     unsigned int row;
@@ -180,15 +182,9 @@ allocOutputtedArray(unsigned int const width, unsigned int const height) {
         pm_error("Unable to allocate space for 'outputted' array");
 
     for (row = 0; row < height; ++row) {
-        unsigned int col;
-
         MALLOCARRAY(outputted[row], width);
         if (outputted[row] == NULL)
             pm_error("Unable to allocate space for 'outputted' array");
-
-        for (col = 0; col < width ; ++col)
-          outputted[row][col] = FALSE;
-
     }
     return outputted;
 }
@@ -196,7 +192,8 @@ allocOutputtedArray(unsigned int const width, unsigned int const height) {
 
 
 static void
-freeOutputtedArray(bool ** const outputted, unsigned int const height) {
+freeOutputtedArray(bool **       const outputted,
+                   unsigned int const height) {
 
     unsigned int row;
 
@@ -206,13 +203,40 @@ freeOutputtedArray(bool ** const outputted, unsigned int const height) {
 
 
 
+                       
 static void
-markOutputted(bool ** const outputted,
+markNotOutputted(bool **      const outputted,
+                 unsigned int const upperLeftCol,
+                 unsigned int const upperLeftRow,
+                 unsigned int const width,
+                 unsigned int const height) {
+/*----------------------------------------------------------------------------
+   Mark every pixel in the specified rectangle as not having been output
+   yet.
+-----------------------------------------------------------------------------*/
+    unsigned int const lowerRightCol = upperLeftCol + width;
+    unsigned int const lowerRightRow = upperLeftRow + height;
+    unsigned int row;
+    
+    for (row = upperLeftRow; row < lowerRightRow; ++row) {
+        unsigned int col;
+        for (col = upperLeftCol; col < lowerRightCol; ++col) 
+            outputted[row][col] = FALSE;
+    }
+}
+
+
+
+static void
+markOutputted(bool **      const outputted,
               unsigned int const upperLeftCol,
               unsigned int const upperLeftRow,
               unsigned int const width,
               unsigned int const height) {
-
+/*----------------------------------------------------------------------------
+   Mark every pixel in the specified rectangle as having been output
+   already.
+-----------------------------------------------------------------------------*/
     unsigned int const lowerRightCol = upperLeftCol + width;
     unsigned int const lowerRightRow = upperLeftRow + height;
     unsigned int row;
@@ -232,16 +256,19 @@ main(int argc, char **argv) {
     FILE * ifP;
     struct pam inpam;
     tuple ** tuples;
-    int row;
+    unsigned int row;
     unsigned int rectWidth, rectHeight;
     bool ** outputted;
+        /* Two dimensional array.  outputted[ROW][COL] means the pixel
+           at row ROW, column COL has already been outputted.
+        */
     tuple transparentColor;
 
     pnm_init(&argc, argv);
 
     parseCommandLine(argc, argv, &cmdline);
 
-    ifP = pm_openr(cmdline.inputFilespec);
+    ifP = pm_openr(cmdline.inputFileName);
 
     tuples = pnm_readpam(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
 
@@ -259,8 +286,12 @@ main(int argc, char **argv) {
 
     printf("<TABLE WIDTH=%d HEIGHT=%d BORDER=0 CELLSPACING=0 CELLPADDING=0>\n",
            inpam.width, inpam.height);
+
+    markNotOutputted(outputted, 0, 0, inpam.width, inpam.height);
+        /* No pixel has been outputted yet */
+
     for (row = 0; row < inpam.height; ++row) {
-        int col;
+        unsigned int col;
         printf("<TR>\n");
         pripix(&inpam, tuples[row][0], 1, 1, transparentColor); 
         markOutputted(outputted, 0, row, 1, 1);
@@ -270,7 +301,7 @@ main(int argc, char **argv) {
                 findSameColorRectangle(&inpam, tuples, row, col, 
                                        &rectWidth, &rectHeight);
                 if (cmdline.verbose)
-                    pm_message("[%d/%d] [%d/%d]",
+                    pm_message("[%u/%u] [%u/%u]",
                                col, row, rectWidth, rectHeight);
                 pripix(&inpam, tuples[row][col], rectWidth, rectHeight, 
                        transparentColor);
@@ -286,5 +317,5 @@ main(int argc, char **argv) {
     pnm_freepamarray(tuples, &inpam);
     freeOutputtedArray(outputted, inpam.height);
 
-    exit(0);
+    return 0;
 }
diff --git a/converter/other/pamtompfont.c b/converter/other/pamtompfont.c
new file mode 100644
index 00000000..b8d11aea
--- /dev/null
+++ b/converter/other/pamtompfont.c
@@ -0,0 +1,179 @@
+/*----------------------------------------------------------------------------
+                               pamtompfont
+------------------------------------------------------------------------------
+  Part of the Netpbm package.
+
+  Convert a PAM image to an Mplayer bitmap font.
+
+  It is obvious that this format was designed to be an image format and
+  adopted by Mplayer for it's fonts (before Mplayer got the ability to
+  use Freetype to read standard font formats such as TrueType).  But
+  I have no idea what the format was originally.
+
+  In the Mplayer font subset of the format, the image is always grayscale
+  (one byte per pixel) with no palette.
+
+  By Bryan Henderson, San Jose CA 2008.05.18
+
+  Contributed to the public domain by its author.
+-----------------------------------------------------------------------------*/
+
+#include <string.h>
+#include <assert.h>
+
+#include "mallocvar.h"
+#include "shhopt.h"
+#include "pm.h"
+#include "pam.h"
+
+struct cmdlineInfo {
+    /* All the information the user supplied in the command line,
+       in a form easy for the program to use.
+    */
+    const char * inputFilename;
+};
+
+
+static void
+parseCommandLine(int argc, char ** argv,
+                 struct cmdlineInfo *cmdlineP) {
+/*----------------------------------------------------------------------------
+   Note that the file spec array we return is stored in the storage that
+   was passed to us as the argv array.
+-----------------------------------------------------------------------------*/
+    optEntry *option_def;
+        /* Instructions to OptParseOptions3 on how to parse our options.
+         */
+    optStruct3 opt;
+
+    unsigned int option_def_index;
+
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
+    option_def_index = 0;   /* incremented by OPTENTRY */
+
+    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 */
+
+    optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
+        /* Uses and sets argc, argv, and some of *cmdlineP and others. */
+
+    if (argc-1 == 0) 
+        cmdlineP->inputFilename = "-";
+    else if (argc-1 != 1)
+        pm_error("Program takes zero or one argument (filename).  You "
+                 "specified %d", argc-1);
+    else
+        cmdlineP->inputFilename = argv[1];
+}
+
+
+
+static void
+validateInput(struct pam * const inpamP) {
+
+    /* The image format does provide for RGB images, but Mplayer doesn't
+       understand that format (and doesn't even recognize it as something
+       it doesn't understand)
+    */
+
+    if (inpamP->depth != 1)
+        pm_error("Input must have depth 1.  This image's depth is %u",
+                 inpamP->depth);
+}
+
+
+
+static void
+writeMpFontHeader(FILE *       const ofP,
+                  struct pam * const inpamP) {
+/*----------------------------------------------------------------------------
+   Write the 32 byte header.
+-----------------------------------------------------------------------------*/
+    fwrite("mhwanh", 1, 6, ofP);  /* Signature */
+
+    fputc(0, ofP);  /* pad */
+    fputc(0, ofP);  /* pad */
+
+    /* Write the old 16 bit width field.  Zero means use the 32 bit one
+       below instead.
+    */
+    pm_writebigshort(ofP, 0);
+
+    /* Height */
+    pm_writebigshort(ofP, inpamP->height);
+
+    /* Number of colors in palette.  Zero means not paletted image */
+    pm_writebigshort(ofP, 0);
+
+    {
+        unsigned int i;
+        for (i = 0; i < 14; ++i)
+            fputc(0, ofP);  /* pad */
+    }
+    /* Width */
+    pm_writebiglong(ofP, inpamP->width);
+}
+
+
+
+static void
+convertRaster(struct pam * const inpamP,
+              FILE *       const ofP) {
+            
+    tuple * tuplerow;
+    unsigned char * outrow;
+    unsigned int row;
+
+    assert(inpamP->depth == 1);
+
+    tuplerow = pnm_allocpamrow(inpamP);
+
+    MALLOCARRAY(outrow, inpamP->width);
+
+    if (outrow == NULL)
+        pm_error("Unable to allocate space for a %u-column output buffer",
+                 inpamP->width);
+
+    for (row = 0; row < inpamP->height; ++row) {
+        unsigned int col;
+
+        pnm_readpamrow(inpamP, tuplerow);
+
+        for (col = 0; col < inpamP->width; ++col) {
+            outrow[col] =
+                pnm_scalesample(tuplerow[col][0], inpamP->maxval, 255);
+        }
+        
+        fwrite(outrow, 1, inpamP->width, ofP);
+    }
+    free(outrow);
+    pnm_freepamrow(tuplerow);
+}
+
+
+
+int
+main(int argc, char *argv[]) {
+
+    struct cmdlineInfo cmdline;
+    FILE * ifP;
+    struct pam inpam;   /* Input PAM image */
+
+    pnm_init(&argc, argv);
+
+    parseCommandLine(argc, argv, &cmdline);
+
+    ifP = pm_openr(cmdline.inputFilename);
+
+    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
+
+    validateInput(&inpam);
+
+    writeMpFontHeader(stdout, &inpam);
+
+    convertRaster(&inpam, stdout);
+
+    return 0;
+}
diff --git a/converter/other/pamtotga.c b/converter/other/pamtotga.c
index 6c8769ed..addd2d91 100644
--- a/converter/other/pamtotga.c
+++ b/converter/other/pamtotga.c
@@ -270,7 +270,7 @@ computeRunlengths(struct pam * const pamP,
 
 static void
 computeOutName(struct cmdlineInfo const cmdline, 
-               const char ** const outNameP) {
+               const char **      const outNameP) {
     
     char * workarea;
 
diff --git a/converter/other/pamtouil.c b/converter/other/pamtouil.c
index 8a80c901..3e67da7d 100644
--- a/converter/other/pamtouil.c
+++ b/converter/other/pamtouil.c
@@ -34,18 +34,18 @@ struct cmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    const char *inputFilespec;  /* Filespecs of input files */
-    char *outname;         /* output filename, less "_icon" */
+    const char * inputFilespec;  /* Filespecs of input files */
+    char * outname;         /* output filename, less "_icon" */
     unsigned int verbose;
 };
 
 
 
 typedef struct {    /* character-pixel mapping */
-    const char* cixel;    /* character string printed for pixel */
-    const char* rgbname;  /* ascii rgb color, either mnemonic or #rgb value */
-    const char* uilname;  /* same, with spaces replaced by underbars */
-    bool        transparent;
+    const char * cixel;    /* character string printed for pixel */
+    const char * rgbname;  /* ascii rgb color, either mnemonic or #rgb value */
+    const char * uilname;  /* same, with spaces replaced by underbars */
+    bool         transparent;
 } cixel_map;
 
 
@@ -154,7 +154,8 @@ genNumstr(int  const number,
 
 
 static const char * 
-uilName(const char * const rgbname, bool const transparent) {
+uilName(const char * const rgbname,
+        bool         const transparent) {
 /*----------------------------------------------------------------------------
    Return a string in newly malloc'ed storage which is an appropriate 
    color name for the UIL palette.  It is the same as the rgb name,
@@ -200,8 +201,8 @@ genCmap(struct pam *   const pamP,
     unsigned int colorIndex;
     {
         /* Figure out how many characters per pixel we'll be using.
-        ** Don't want to be forced to link with libm.a, so using a
-        ** division loop rather than a log function.  
+           Don't want to be forced to link with libm.a, so using a
+           division loop rather than a log function.  
         */
         unsigned int i;
         for (*charsppP = 0, i = ncolors; i > 0; ++(*charsppP))
@@ -209,7 +210,7 @@ genCmap(struct pam *   const pamP,
     }
 
     /* Generate the character-pixel string and the rgb name for each colormap
-    ** entry.
+       entry.
     */
     for (colorIndex = 0; colorIndex < ncolors; ++colorIndex) {
         bool nameAlreadyInCmap;
@@ -247,12 +248,12 @@ genCmap(struct pam *   const pamP,
             /* Make the entry a cross-reference to the earlier entry */
             cmap[colorIndex].uilname = NULL;
             cmap[colorIndex].rgbname = NULL;
-            cmap[colorIndex].cixel = cmap[indexOfName].cixel;
+            cmap[colorIndex].cixel   = cmap[indexOfName].cixel;
         } else {
             cmap[colorIndex].uilname = uilName(colorname, transparent);
             cmap[colorIndex].rgbname = strdup(colorname);
             if (cmap[colorIndex].rgbname == NULL)
-                pm_error( "out of memory allocating color name" );
+                pm_error("out of memory allocating color name");
             
             cmap[colorIndex].transparent = transparent;
             
@@ -329,15 +330,16 @@ writeRaster(struct pam *  const pamP,
             unsigned int  const ncolors,
             tuplehash     const cht, 
             unsigned int  const charspp) {
-    
-    int row;
-    /* Write out the ascii character-pixel image. */
+/*----------------------------------------------------------------------------
+   Write out the ascii character-pixel image.
+-----------------------------------------------------------------------------*/
+    unsigned int row;
 
     printf("\n");
     printf("%s_icon : exported icon( color_table = %s_rgb,\n",
            outname, outname);
     for (row = 0; row < pamP->height; ++row) {
-        int col;
+        unsigned int col;
 
         printf("    '");
         for (col = 0; col < pamP->width; ++col) {
@@ -360,27 +362,19 @@ writeRaster(struct pam *  const pamP,
 }
 
 
-static void
-freeString(const char * const s) {
-/*----------------------------------------------------------------------------
-   This is just free(), but with type checking for const char *.
------------------------------------------------------------------------------*/
-    free((void *)s);
-}
-
-
 
 static void
-freeCmap(cixel_map cmap[], unsigned int const ncolors) {
+freeCmap(cixel_map    const cmap[],
+         unsigned int const ncolors) {
 
-    int i;
+    unsigned int i;
 
     for (i = 0; i < ncolors; ++i) {
         cixel_map const cmapEntry = cmap[i];
         if (cmapEntry.uilname)
-            freeString(cmapEntry.uilname);
+            strfree(cmapEntry.uilname);
         if (cmapEntry.rgbname)
-            freeString(cmapEntry.rgbname);
+            strfree(cmapEntry.rgbname);
     }
 }
 
@@ -391,8 +385,8 @@ main(int argc, char *argv[]) {
 
     struct cmdlineInfo cmdline;
     struct pam pam;   /* Input PAM image */
-    FILE* ifP;
-    tuple** tuples;
+    FILE * ifP;
+    tuple ** tuples;
     unsigned int ncolors;
     tuplehash cht;
     tupletable chv;
diff --git a/converter/other/pnmtopclxl.c b/converter/other/pnmtopclxl.c
index 0737bf15..9819ebe4 100644
--- a/converter/other/pnmtopclxl.c
+++ b/converter/other/pnmtopclxl.c
@@ -178,7 +178,7 @@ parseCommandLine(int argc, char ** argv,
         cmdlineP->sourceP->name = "-";
         cmdlineP->sourceP->next = NULL;
     } else {
-        int i;
+        unsigned int i;
         InputSource ** nextLinkP;
 
         nextLinkP = &cmdlineP->sourceP;
diff --git a/converter/pbm/pbmtogo.c b/converter/pbm/pbmtogo.c
index e10fb2ff..b4f3f372 100644
--- a/converter/pbm/pbmtogo.c
+++ b/converter/pbm/pbmtogo.c
@@ -1,11 +1,11 @@
 /* pbmtogo.c - read a PBM image and produce a GraphOn terminal raster file
-**	
-**	Rev 1.1 was based on pbmtolj.c
+**      
+**      Rev 1.1 was based on pbmtolj.c
 **
-**	Bo Thide', Swedish Institute of Space Physics, bt@irfu.se
-**				   
+**      Bo Thide', Swedish Institute of Space Physics, bt@irfu.se
+**                                 
 **
-** $Log:	pbmtogo.c,v $
+** $Log:        pbmtogo.c,v $
  * Revision 1.5  89/11/25  00:24:12  00:24:12  root (Bo Thide)
  * Bug found: The byte after 64 repeated bytes sometimes lost. Fixed.
  * 
@@ -35,63 +35,125 @@
 ** implied warranty.
 */
 
+#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 
 #include "pm_c_util.h"
 #include "pbm.h"
 
-#define BUFSIZE 132	/* GraphOn has 132 byte/1056 bit wide raster lines */
-#define REPEAT_CURRENT_LINE_MASK	0x00 
-#define SKIP_AND_PLOT_MASK		0x40 
-#define REPEAT_PLOT_MASK		0x80 
-#define PLOT_ARBITRARY_DATA_MASK	0xc0 
+#define GRAPHON_WIDTH 1056 /* GraphOn has 1056 bit wide raster lines */
+#define GRAPHON_WIDTH_BYTES (GRAPHON_WIDTH / 8)
+#define REPEAT_CURRENT_LINE_MASK        0x00 
+#define SKIP_AND_PLOT_MASK              0x40 
+#define REPEAT_PLOT_MASK                0x80 
+#define PLOT_ARBITRARY_DATA_MASK        0xc0 
 #define MAX_REPEAT 64
 
-static unsigned char *scanlineptr;		/* Pointer to current scan line byte */
+static unsigned char * scanlineptr;
+    /* Pointer to current scan line byte */
+
+static int item, bitsperitem, bitshift;
+
+static void
+putinit(void) {
+
+    /* Enter graphics window */
+    printf("\0331");
+
+    /* Erase graphics window */
+    printf("\033\014");
+
+    /* Set graphics window in raster mode */
+    printf("\033r");
+
+    /* Select standard Tek coding **/
+    printf("\033[=11l");
+
+    bitsperitem = 1;
+    item = 0;
+    bitshift = 7;
+}
+
+
+
+static void
+putitem(void) {
+
+    *scanlineptr++ = item;
+    bitsperitem = 0;
+    item = 0;
+}
+
+
+
+static void
+putbit(bit const b) {
+
+    if (b == PBM_BLACK)
+        item += 1 << bitshift;
+
+    bitshift--;
+
+    if (bitsperitem == 8)
+    {
+        putitem();
+        bitshift = 7;
+    }
+    bitsperitem++;
+}
 
-static void putinit ARGS(( void ));
-static void putbit ARGS(( bit b ));
-static void putrest ARGS(( void ));
-static void putitem ARGS(( void ));
 
-int
-main( argc, argv )
-     int argc;
-     char* argv[];
-{
-    FILE* ifp;
-    bit* bitrow;
-    register bit* bP;
-    int argn, rows, cols, format, rucols, padright, row, col;
-    int nbyte, bytesperrow, ecount, ucount, nout, i, linerepeat;
-    int	olditem;
-    unsigned char oldscanline[BUFSIZE];
-    unsigned char newscanline[BUFSIZE];
-    unsigned char diff[BUFSIZE];
-    unsigned char buffer[BUFSIZE];
-    unsigned char outbuffer[2*(BUFSIZE+1)];	/* Worst case.  Should malloc */
-    const char* usage = "[-c] [pbmfile]";
 
+static void
+putrest(void) {
+
+    if (bitsperitem > 1)
+        putitem();
 
-    pbm_init( &argc, argv );
+    /* end raster downloading */
+    printf("\033\134");
 
-    argn = 2;
+    /* Exit raster mode */
+    printf("\033t");
+
+    /* Exit graphics window
+       printf("\0332"); */
+}
 
-    /* Check for flags. */
-    if (argc > argn + 1)
-      pm_usage(usage);
 
-    if (argc == argn)
-      ifp = pm_openr( argv[argn-1] );
+
+int
+main(int           argc,
+     const char ** argv) {
+
+    FILE * ifP;
+    bit * bitrow;
+    bit * bP;
+    int rows, cols, format, rucols, padright, row, col;
+    int nbyte, bytesperrow, ecount, ucount, nout, i, linerepeat;
+    int olditem;
+    unsigned char oldscanline[GRAPHON_WIDTH_BYTES];
+    unsigned char newscanline[GRAPHON_WIDTH_BYTES];
+    unsigned char diff[GRAPHON_WIDTH_BYTES];
+    unsigned char buffer[GRAPHON_WIDTH_BYTES];
+    unsigned char outbuffer[2*(GRAPHON_WIDTH_BYTES+1)];     /* Worst case. */
+
+    pm_proginit(&argc, argv);
+
+    if (argc-1 == 0)
+      ifP = stdin;
+    else if (argc-1 == 1)
+      ifP = pm_openr(argv[1]);
     else
-      ifp = stdin;
+        pm_error("There is at most one argument: input file name.  "
+                 "You specified %u", argc-1);
 
-    pbm_readpbminit(ifp, &cols, &rows, &format);
+    pbm_readpbminit(ifP, &cols, &rows, &format);
 
-    if (cols > 1056)
+    if (cols > GRAPHON_WIDTH)
         pm_error("Image is wider (%u pixels) than a Graphon terminal "
-                 "(%u pixels)", cols, 1056);
+                 "(%u pixels)", cols, GRAPHON_WIDTH);
 
     bitrow = pbm_allocrow(cols);
 
@@ -101,7 +163,7 @@ main( argc, argv )
     rucols = rucols * 8;
     padright = rucols - cols;
 
-    for (i = 0; i < BUFSIZE; ++i )
+    for (i = 0; i < GRAPHON_WIDTH_BYTES; ++i )
       buffer[i] = 0;
     putinit();
 
@@ -113,12 +175,14 @@ main( argc, argv )
     for (row = 0; row < rows; row++) {
         /* Store scan line data in the new scan line vector */
         scanlineptr = newscanline;
-        pbm_readpbmrow(ifp, bitrow, cols, format);
+        pbm_readpbmrow(ifP, bitrow, cols, format);
         /* Transfer raster graphics */
         for (col = 0, bP = bitrow; col < cols; col++, bP++)
           putbit(*bP);
         for (col = 0; col < padright; col++)
           putbit(0);
+
+        assert(bytesperrow <= GRAPHON_WIDTH_BYTES);
         
         /* XOR data from the new scan line with data from old scan line */
         for (i = 0; i < bytesperrow; i++)
@@ -240,69 +304,11 @@ main( argc, argv )
           oldscanline[i] = newscanline[i];
     }
     putchar(linerepeat);        /* For the last line(s) to be plotted */
-    pm_close(ifp);
+    pm_close(ifP);
     putrest();
-    exit(0);
-}
-
-
-
-static int item, bitsperitem, bitshift;
 
-static void
-putinit()
-{
-  /* Enter graphics window */
-  printf("\0331");
-
-  /* Erase graphics window */
-  printf("\033\014");
-
-  /* Set graphics window in raster mode */
-  printf("\033r");
-
-  /* Select standard Tek coding **/
-  printf("\033[=11l");
-
-  bitsperitem = 1;
-  item = 0;
-  bitshift = 7;
-}
-
-static void
-putbit(bit b)
-{
-  if (b == PBM_BLACK)
-    item += 1 << bitshift;
-  bitshift--;
-  if (bitsperitem == 8)
-  {
-    putitem();
-    bitshift = 7;
-  }
-  bitsperitem++;
+    return 0;
 }
 
-static void
-putrest()
-{
-  if (bitsperitem > 1)
-      putitem();
-
-  /* end raster downloading */
-  printf("\033\134");
 
-  /* Exit raster mode */
-  printf("\033t");
 
-  /* Exit graphics window
-  printf("\0332"); */
-}
-
-static void
-putitem()
-  {
-  *scanlineptr++ = item;
-  bitsperitem = 0;
-  item = 0;
-  }