about summary refs log tree commit diff
path: root/converter/pbm/macptopbm.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/macptopbm.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/macptopbm.c')
-rw-r--r--converter/pbm/macptopbm.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/converter/pbm/macptopbm.c b/converter/pbm/macptopbm.c
new file mode 100644
index 00000000..f4a341d3
--- /dev/null
+++ b/converter/pbm/macptopbm.c
@@ -0,0 +1,140 @@
+/* macptopbm.c - read a MacPaint file and produce a portable bitmap
+**
+** Copyright (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 "pbm.h"
+#include "macp.h"
+
+static void ReadMacPaintFile ARGS(( FILE* file, int extraskip, int* scanLineP, unsigned char Pic[MAX_LINES][BYTES_WIDE] ));
+
+static unsigned char Pic[MAX_LINES][BYTES_WIDE];
+
+int
+main( argc, argv )
+    int argc;
+    char* argv[];
+    {
+    FILE* ifp;
+    bit* bitrow;
+    int argn, extraskip, scanLine, rows, cols, row, bcol, i;
+    const char* usage = "[-extraskip N] [macpfile]";
+
+
+    pbm_init( &argc, argv );
+
+    argn = 1;
+    extraskip = 0;
+
+    /* Check for flags. */
+    if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
+	{
+	if ( pm_keymatch( argv[argn], "-extraskip", 2 ) )
+	    {
+	    argn++;
+	    if ( argn == argc || sscanf( argv[argn], "%d", &extraskip ) != 1 )
+		pm_usage( usage );
+	    }
+	else
+	    pm_usage( usage );
+	argn++;
+	}
+
+    if ( argn < argc )
+	{
+	ifp = pm_openr( argv[argn] );
+	argn++;
+	}
+    else
+	ifp = stdin;
+
+    if ( argn != argc )
+	pm_usage( usage );
+
+    ReadMacPaintFile( ifp, extraskip, &scanLine, Pic );
+
+    pm_close( ifp );
+
+    cols = BYTES_WIDE * 8;
+    rows = scanLine;
+    pbm_writepbminit( stdout, cols, rows, 0 );
+    bitrow = pbm_allocrow( cols );
+
+    for ( row = 0; row < rows; row++ )
+	{
+	for ( bcol = 0; bcol < BYTES_WIDE; bcol++ )
+	    for ( i = 0; i < 8; i++ )
+		bitrow[bcol * 8 + i] =
+		    ( (Pic[row][bcol] >> (7 - i)) & 1 ) ? PBM_BLACK : PBM_WHITE;
+	pbm_writepbmrow( stdout, bitrow, cols, 0 );
+	}
+
+    pm_close( stdout );
+    exit( 0 );
+    }
+
+/*
+** Some of the following routine is:
+**
+**                Copyright 1987 by Patrick J. Naughton
+**                         All Rights Reserved
+** 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.
+*/
+
+static void
+ReadMacPaintFile( file, extraskip, scanLineP, Pic )
+    FILE* file;
+    int extraskip;
+    int* scanLineP;
+    unsigned char Pic[MAX_LINES][BYTES_WIDE];
+    {
+    unsigned int i, j, k;
+    unsigned char ch;
+
+    /* Skip over the header. */
+    for ( i = 0; i < extraskip; i++ )
+	getc( file );
+    for ( i = 0; i < HEADER_LENGTH; i++ )
+	getc( file );
+
+    *scanLineP = 0;
+    k = 0;
+
+    while ( *scanLineP < MAX_LINES )
+	{
+	ch = (unsigned char) getc( file );	/* Count byte */
+	i = (unsigned int) ch;
+	if ( ch < 0x80 )
+	    {	/* Unpack next (I+1) chars as is */
+	    for ( j = 0; j <= i; j++ )
+		if ( *scanLineP < MAX_LINES )
+		    {
+		    Pic[*scanLineP][k++] = (unsigned char) getc( file );
+		    if ( ! (k %= BYTES_WIDE) )
+			*scanLineP += 1;
+		    }
+	    }
+	else
+	    {	/* Repeat next char (2's comp I) times */
+	    ch = getc( file );
+	    for ( j = 0; j <= 256 - i; j++ )
+		if ( *scanLineP < MAX_LINES )
+		    {
+		    Pic[*scanLineP][k++] = (unsigned char) ch;
+		    if ( ! (k %= BYTES_WIDE) )
+			*scanLineP += 1;
+		    }
+	    }
+	}
+    }