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

  1. /* pbmtocmuwm.c - read a portable bitmap and produce a CMU window manager bitmap
  2. **
  3. ** Copyright (C) 1989 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. #include "cmuwm.h"
  15.  
  16. static void putinit ARGS(( int rows, int cols ));
  17. static void putbit ARGS(( bit b ));
  18. static void putrest ARGS(( void ));
  19. static void putitem ARGS(( void ));
  20.  
  21. int
  22. main( argc, argv )
  23.     int argc;
  24.     char* argv[];
  25.     {
  26.     FILE* ifp;
  27.     bit* bitrow;
  28.     register bit* bP;
  29.     int rows, cols, format, padright, row, col;
  30.  
  31.  
  32.     pbm_init( &argc, argv );
  33.  
  34.     if ( argc > 2 )
  35.     pm_usage( "[pbmfile]" );
  36.  
  37.     if ( argc == 2 )
  38.     ifp = pm_openr( argv[1] );
  39.     else
  40.     ifp = stdin;
  41.  
  42.     pbm_readpbminit( ifp, &cols, &rows, &format );
  43.     bitrow = pbm_allocrow( cols );
  44.     
  45.     /* Round cols up to the nearest multiple of 8. */
  46.     padright = ( ( cols + 7 ) / 8 ) * 8 - cols;
  47.  
  48.     putinit( rows, cols );
  49.     for ( row = 0; row < rows; row++ )
  50.     {
  51.     pbm_readpbmrow( ifp, bitrow, cols, format );
  52.         for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  53.         putbit( *bP );
  54.     for ( col = 0; col < padright; col++ )
  55.         putbit( 0 );
  56.         }
  57.  
  58.     pm_close( ifp );
  59.  
  60.     putrest( );
  61.  
  62.     exit( 0 );
  63.     }
  64.  
  65. static unsigned char item;
  66. static int bitsperitem, bitshift;
  67.  
  68. static void
  69. putinit( rows, cols )
  70.     int rows, cols;
  71.     {
  72.     if ( pm_writebiglong( stdout, CMUWM_MAGIC ) == -1 )
  73.     pm_error( "write error" );
  74.     if ( pm_writebiglong( stdout, cols ) == -1 )
  75.     pm_error( "write error" );
  76.     if ( pm_writebiglong( stdout, rows ) == -1 )
  77.     pm_error( "write error" );
  78.     if ( pm_writebigshort( stdout, (short) 1 ) == -1 )
  79.     pm_error( "write error" );
  80.  
  81.     item = 0;
  82.     bitsperitem = 0;
  83.     bitshift = 7;
  84.     }
  85.  
  86. #if __STDC__
  87. static void
  88. putbit( bit b )
  89. #else /*__STDC__*/
  90. static void
  91. putbit( b )
  92.     bit b;
  93. #endif /*__STDC__*/
  94.     {
  95.     if ( bitsperitem == 8 )
  96.     putitem( );
  97.     if ( b == PBM_WHITE )
  98.     item += 1 << bitshift;
  99.     bitsperitem++;
  100.     bitshift--;
  101.     }
  102.  
  103. static void
  104. putrest( )
  105.     {
  106.     if ( bitsperitem > 0 )
  107.     putitem( );
  108.     }
  109.  
  110. static void
  111. putitem( )
  112.     {
  113.     (void) putc( item, stdout );
  114.     item = 0;
  115.     bitsperitem = 0;
  116.     bitshift = 7;
  117.     }
  118.