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>2006-08-19 03:12:28 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
commit1fd361a1ea06e44286c213ca1f814f49306fdc43 (patch)
tree64c8c96cf54d8718847339a403e5e67b922e8c3f /converter/pbm/ybmtopbm.c
downloadnetpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.gz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.xz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.zip
Create Subversion repository
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/pbm/ybmtopbm.c')
-rw-r--r--converter/pbm/ybmtopbm.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/converter/pbm/ybmtopbm.c b/converter/pbm/ybmtopbm.c
new file mode 100644
index 00000000..739e168a
--- /dev/null
+++ b/converter/pbm/ybmtopbm.c
@@ -0,0 +1,113 @@
+/* ybmtopbm.c - read a file from Bennet Yee's 'xbm' program and write a pbm.
+**
+** Written by Jamie Zawinski based on code (C) 1988 by Jef Poskanzer.
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted, provided
+** that the above copyright notice appear in all copies and that both that
+** copyright notice and this permission notice appear in supporting
+** documentation.  This software is provided "as is" without express or
+** implied warranty.
+*/
+
+#include <stdio.h>
+#include "pbm.h"
+
+static void getinit ARGS(( FILE* file, short* colsP, short* rowsP, short* depthP, short* padrightP ));
+static bit getbit ARGS(( FILE* file ));
+
+#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;
+
+    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 );
+	}
+
+    pm_close( ifp );
+    pm_close( stdout );
+
+    exit( 0 );
+    }
+
+static int item;
+static int bitsperitem, bitshift;
+
+static void
+getinit( file, colsP, rowsP, depthP, padrightP )
+    FILE* file;
+    short* colsP;
+    short* rowsP;
+    short* depthP;
+    short* padrightP;
+    {
+    short magic;
+
+    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" );
+
+    *depthP = 1;
+    *padrightP = ( ( *colsP + 15 ) / 16 ) * 16 - *colsP;
+    bitsperitem = 0;
+    }
+
+static bit
+getbit( file )
+    FILE* file;
+    {
+    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;
+    --bitsperitem;
+    ++bitshift;
+    return b;
+    }