/* libpbm3.c - pbm utility library part 3 ** ** 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 "libpbm.h" void pbm_writepbminit( FILE* file, int cols, int rows, int forceplain ) { if ( ! forceplain ) fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, RPBM_MAGIC2, cols, rows ); else fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, PBM_MAGIC2, cols, rows ); } static void pbm_writepbmrowraw( FILE* file, bit* bitrow, int cols ) { int col, bitshift; unsigned char item; bit* bP; bitshift = 7; item = 0; for ( col = 0, bP = bitrow; col < cols; ++col, ++bP ) { if ( *bP ) item += 1 << bitshift; --bitshift; if ( bitshift == -1 ) { (void) putc( item, file ); bitshift = 7; item = 0; } } if ( bitshift != 7 ) (void) putc( item, file ); } static void pbm_writepbmrowplain( FILE* file, bit* bitrow, int cols ) { int col, charcount; bit* bP; charcount = 0; for ( col = 0, bP = bitrow; col < cols; ++col, ++bP ) { if ( charcount >= 70 ) { (void) putc( '\n', file ); charcount = 0; } (void) putc( *bP ? '1' : '0', file ); ++charcount; } (void) putc( '\n', file ); } void pbm_writepbmrow( FILE* file, bit* bitrow, int cols, int forceplain ) { if ( ! forceplain ) pbm_writepbmrowraw( file, bitrow, cols ); else pbm_writepbmrowplain( file, bitrow, cols ); } void pbm_writepbm( FILE* file, bit** bits, int cols, int rows, int forceplain ) { int row; pbm_writepbminit( file, cols, rows, forceplain ); for ( row = 0; row < rows; ++row ) pbm_writepbmrow( file, bits[row], cols, forceplain ); }