about summary refs log tree commit diff
path: root/converter/pbm/ybmtopbm.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-03-04 03:32:48 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-03-04 03:32:48 +0000
commit75efa33cf45746a51ccbf842185ac9dbcd2b4390 (patch)
treec3ef12716ac51122d3c80545c026d3182bd2a311 /converter/pbm/ybmtopbm.c
parent18261af3f1456a5db5fb200d022260e9738efb6c (diff)
downloadnetpbm-mirror-75efa33cf45746a51ccbf842185ac9dbcd2b4390.tar.gz
netpbm-mirror-75efa33cf45746a51ccbf842185ac9dbcd2b4390.tar.xz
netpbm-mirror-75efa33cf45746a51ccbf842185ac9dbcd2b4390.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1139 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/pbm/ybmtopbm.c')
-rw-r--r--converter/pbm/ybmtopbm.c165
1 files changed, 89 insertions, 76 deletions
diff --git a/converter/pbm/ybmtopbm.c b/converter/pbm/ybmtopbm.c
index 739e168a..5ef29501 100644
--- a/converter/pbm/ybmtopbm.c
+++ b/converter/pbm/ybmtopbm.c
@@ -10,104 +10,117 @@
 ** implied warranty.
 */
 
-#include <stdio.h>
+#include "pm.h"
 #include "pbm.h"
 
-static void getinit ARGS(( FILE* file, short* colsP, short* rowsP, short* depthP, short* padrightP ));
-static bit getbit ARGS(( FILE* file ));
+static short const ybmMagic = ( ( '!' << 8 ) | '!' );
 
-#define YBM_MAGIC  ( ( '!' << 8 ) | '!' )
 
-int
-main( argc, argv )
-    int argc;
-    char* argv[];
-    {
-    FILE* ifp;
-    bit* bitrow;
-    register bit* bP;
-    short rows, cols, padright, row, col;
-    short depth;
 
-    pbm_init( &argc, argv );
 
-    if ( argc > 2 )
-	pm_usage( "[ybmfile]" );
-
-    if ( argc == 2 )
-	ifp = pm_openr( argv[1] );
-    else
-	ifp = stdin;
+static int item;
+static int bitsperitem, bitshift;
 
-    getinit( ifp, &cols, &rows, &depth, &padright );
-    if ( depth != 1 )
-	pm_error(
-	    "YBM file has depth of %d, must be 1",
-	    (int) depth );
 
-    pbm_writepbminit( stdout, cols, rows, 0 );
-    bitrow = pbm_allocrow( cols );
 
-    for ( row = 0; row < rows; ++row )
-	{
-	/* Get data. */
-        for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
-	    *bP = getbit( ifp );
-	/* Discard line padding */
-        for ( col = 0; col < padright; ++col )
-	    (void) getbit( ifp );
-	pbm_writepbmrow( stdout, bitrow, cols, 0 );
-	}
+static void
+getinit(FILE *  const ifP,
+        short * const colsP,
+        short * const rowsP,
+        short * const depthP,
+        short * const padrightP) {
 
-    pm_close( ifp );
-    pm_close( stdout );
+    short magic;
+    int rc;
 
-    exit( 0 );
-    }
+    rc = pm_readbigshort(ifP, &magic);
+    if (rc == -1)
+        pm_error("EOF / read error");
 
-static int item;
-static int bitsperitem, bitshift;
+    if (magic != ybmMagic)
+        pm_error("bad magic number in YBM file");
 
-static void
-getinit( file, colsP, rowsP, depthP, padrightP )
-    FILE* file;
-    short* colsP;
-    short* rowsP;
-    short* depthP;
-    short* padrightP;
-    {
-    short magic;
+    rc = pm_readbigshort(ifP, colsP);
+    if (rc == -1 )
+        pm_error("EOF / read error");
 
-    if ( pm_readbigshort( file, &magic ) == -1 )
-	pm_error( "EOF / read error" );
-    if ( magic != YBM_MAGIC )
-	pm_error( "bad magic number in YBM file" );
-    if ( pm_readbigshort( file, colsP ) == -1 )
-	pm_error( "EOF / read error" );
-      if ( pm_readbigshort( file, rowsP ) == -1 )
-	pm_error( "EOF / read error" );
+    rc = pm_readbigshort(ifP, rowsP);
+    if (rc == -1)
+        pm_error("EOF / read error");
 
     *depthP = 1;
-    *padrightP = ( ( *colsP + 15 ) / 16 ) * 16 - *colsP;
+    *padrightP = ((*colsP + 15) / 16) * 16 - *colsP;
     bitsperitem = 0;
-    }
+}
+
+
 
 static bit
-getbit( file )
-    FILE* file;
-    {
+getbit(FILE * const ifP) {
+
     bit b;
 
-    if ( bitsperitem == 0 )
-	{
-	item = getc(file) | getc(file)<<8;
-	if ( item == EOF )
-	    pm_error( "EOF / read error" );
-	bitsperitem = 16;
-	bitshift = 0;
-	}
-    b = ( ( item >> bitshift) & 1 ) ? PBM_BLACK : PBM_WHITE;
+    if (bitsperitem == 0) {
+        item = getc(ifP) | getc(ifP) << 8;
+        if (item == EOF)
+            pm_error("EOF / read error");
+        bitsperitem = 16;
+        bitshift = 0;
+    }
+
+    b = ((item >> bitshift) & 1 ) ? PBM_BLACK : PBM_WHITE;
     --bitsperitem;
     ++bitshift;
     return b;
+}
+
+
+
+int
+main(int argc, const char * argv[]) {
+
+    FILE * ifP;
+    bit * bitrow;
+    short rows, cols, padright;
+    unsigned int row;
+    short depth;
+    const char * inputFile;
+
+    pm_proginit(&argc, argv);
+
+    if (argc-1 < 1)
+        inputFile = "-";
+    else {
+        inputFile = argv[1];
+
+        if (argc-1 > 2)
+            pm_error("Too many arguments.  The only argument is the optional "
+                     "input file name");
     }
+
+    ifP = pm_openr(inputFile);
+
+    getinit(ifP, &cols, &rows, &depth, &padright);
+    if (depth != 1)
+        pm_error("YBM file has depth of %u, must be 1", (unsigned)depth);
+    
+    pbm_writepbminit(stdout, cols, rows, 0);
+
+    bitrow = pbm_allocrow(cols);
+
+    for (row = 0; row < rows; ++row) {
+        /* Get data. */
+        unsigned int col;
+        for (col = 0; col < cols; ++col)
+            bitrow[col] = getbit(ifP);
+        /* Discard line padding */
+        for (col = 0; col < padright; ++col)
+            getbit(ifP);
+        pbm_writepbmrow(stdout, bitrow, cols, 0);
+    }
+
+    pm_close(ifP);
+    pm_close(stdout);
+
+    return 0;
+}