home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / brushtopbm.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  2KB  |  108 lines

  1. /* brushtopbm.c - read a doodle brush file and write a portable bitmap
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14.  
  15. static void getinit ARGS(( FILE* file, int* colsP, int* rowsP ));
  16. static bit getbit ARGS(( FILE* file ));
  17.  
  18. int
  19. main( argc, argv )
  20.     int argc;
  21.     char* argv[];
  22.     {
  23.     FILE* ifp;
  24.     bit* bitrow;
  25.     register bit* bP;
  26.     int rows, cols, padright, row, col;
  27.  
  28.  
  29.     pbm_init( &argc, argv );
  30.  
  31.     if ( argc > 2 )
  32.     pm_usage( "[brushfile]" );
  33.  
  34.     if ( argc == 2 )
  35.     ifp = pm_openr( argv[1] );
  36.     else
  37.     ifp = stdin;
  38.  
  39.     getinit( ifp, &cols, &rows );
  40.  
  41.     pbm_writepbminit( stdout, cols, rows, 0 );
  42.     bitrow = pbm_allocrow( cols );
  43.  
  44.     /* Compute padding to round cols up to the next multiple of 16. */
  45.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  46.  
  47.     for ( row = 0; row < rows; ++row )
  48.     {
  49.     /* Get data. */
  50.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  51.         *bP = getbit( ifp );
  52.     /* Discard line padding. */
  53.         for ( col = 0; col < padright; ++col )
  54.         (void) getbit( ifp );
  55.     /* Write row. */
  56.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  57.     }
  58.  
  59.     pm_close( ifp );
  60.     pm_close( stdout );
  61.     
  62.     exit( 0 );
  63.     }
  64.  
  65.  
  66. static int item, bitsperitem, bitshift;
  67.  
  68. static void
  69. getinit( file, colsP, rowsP )
  70.     FILE* file;
  71.     int* colsP;
  72.     int* rowsP;
  73.     {
  74.     int i;
  75.  
  76.     if ( getc( file ) != 1 )
  77.     pm_error( "bad magic number 1" );
  78.     if ( getc( file ) != 0 )
  79.     pm_error( "bad magic number 2" );
  80.     *colsP = getc( file ) << 8;
  81.     *colsP += getc( file );
  82.     *rowsP = getc( file ) << 8;
  83.     *rowsP += getc( file );
  84.     bitsperitem = 8;
  85.  
  86.     /* Junk rest of header. */
  87.     for ( i = 0; i < 10; ++i )  /* 10 is just a guess at the header size */
  88.     (void) getc( file );
  89.     }
  90.  
  91. static bit
  92. getbit( file )
  93.     FILE* file;
  94.     {
  95.     bit b;
  96.  
  97.     if ( bitsperitem == 8 )
  98.     {
  99.     item = getc( file );
  100.     bitsperitem = 0;
  101.     bitshift = 7;
  102.     }
  103.     ++bitsperitem;
  104.     b = ( ( item >> bitshift) & 1 ) ? PBM_WHITE : PBM_BLACK;
  105.     --bitshift;
  106.     return b;
  107.     }
  108.