home *** CD-ROM | disk | FTP | other *** search
- /* xxxtopbm.c - read an xxx bitmap and write 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 <stdio.h>
- #include "pbm.h"
-
- main( argc, argv )
- int argc;
- char *argv[];
- {
- FILE *ifd;
- bit **bits, getbit();
- int rows, cols, row, col, subcol;
-
- if ( argc > 2 )
- {
- fprintf( stderr, "usage: %s [xxxfile]\n", argv[0] );
- exit( 1 );
- }
-
- if ( argc == 2 )
- {
- ifd = fopen( argv[1], "r" );
- if ( ifd == NULL )
- {
- fprintf( stderr, "%s: can't open.\n", argv[1] );
- exit( 1 );
- }
- }
- else
- ifd = stdin;
-
- getinit( ifd, &cols, &rows );
-
- bits = pbm_allocarray( cols, rows );
-
- for ( row = 0; row < rows; row++ )
- for ( col = 0; col < cols; col += 8 )
- for ( subcol = col + 7; subcol >= col; subcol-- )
- bits[row][subcol] = getbit( ifd );
-
- if ( ifd != stdin )
- fclose( ifd );
-
- pbm_writepbm( stdout, bits, cols, rows );
-
- exit( 0 );
- }
-
-
- int item, bitsperitem, bitshift;
-
- getinit( file, colp, rowp )
- FILE *file;
- int *colp, *rowp;
- {
- if ( getc( file ) != 109 )
- {
- fprintf( stderr, "Bad magic number 1.\n" );
- exit( 1 );
- }
- if ( getc( file ) != 1 )
- {
- fprintf( stderr, "Bad magic number 2.\n" );
- exit( 1 );
- }
- *colp = getc( file );
- *colp += getc( file ) << 8;
- *rowp = getc( file );
- *rowp += getc( file ) << 8;
- bitsperitem = 8;
- if ( getc( file ) != 0 )
- {
- fprintf( stderr, "Bad magic number 3.\n" );
- exit( 1 );
- }
- if ( getc( file ) != 46 )
- {
- fprintf( stderr, "Bad magic number 4.\n" );
- exit( 1 );
- }
- }
-
- bit
- getbit( file )
- FILE *file;
- {
- bit b;
-
- if ( bitsperitem == 8 )
- {
- item = getc( file );
- bitsperitem = 0;
- bitshift = 7;
- }
- bitsperitem++;
- b = ( item >> bitshift) & 1;
- bitshift--;
- return ( b );
- }
-