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

  1. /* cmuwmtopbm.c - read a CMU window manager bitmap and produce a portable 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 getinit ARGS(( FILE* file, int* colsP, int* rowsP, short* depthP, int* padrightP ));
  17. static bit getbit ARGS(( FILE* file ));
  18.  
  19. int
  20. main( argc, argv )
  21.     int argc;
  22.     char* argv[];
  23.     {
  24.     FILE* ifp;
  25.     bit* bitrow;
  26.     register bit* bP;
  27.     int rows, cols, padright, row, col;
  28.     short depth;
  29.  
  30.  
  31.     pbm_init( &argc, argv );
  32.  
  33.     if ( argc > 2 )
  34.     pm_usage( "[cmuwmfile]" );
  35.  
  36.     if ( argc == 2 )
  37.     ifp = pm_openr( argv[1] );
  38.     else
  39.     ifp = stdin;
  40.  
  41.     getinit( ifp, &cols, &rows, &depth, &padright );
  42.     if ( depth != 1 )
  43.     pm_error(
  44.         "CMU window manager file has depth of %d, must be 1",
  45.         (int) depth );
  46.  
  47.     pbm_writepbminit( stdout, cols, rows, 0 );
  48.     bitrow = pbm_allocrow( cols );
  49.  
  50.     for ( row = 0; row < rows; ++row )
  51.     {
  52.     /* Get data. */
  53.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  54.         *bP = getbit( ifp );
  55.     /* Discard line padding */
  56.         for ( col = 0; col < padright; ++col )
  57.         (void) getbit( ifp );
  58.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  59.     }
  60.  
  61.     pm_close( ifp );
  62.     pm_close( stdout );
  63.  
  64.     exit( 0 );
  65.     }
  66.  
  67. static int item, bitsperitem, bitshift;
  68.  
  69. static void
  70. getinit( file, colsP, rowsP, depthP, padrightP )
  71.     FILE* file;
  72.     int* colsP;
  73.     int* rowsP;
  74.     short* depthP;
  75.     int* padrightP;
  76.     {
  77.     long l;
  78.  
  79.     if ( pm_readbiglong( file, &l ) == -1 )
  80.     pm_error( "EOF / read error" );
  81.     if ( l != CMUWM_MAGIC )
  82.     pm_error( "bad magic number in CMU window manager file" );
  83.     if ( pm_readbiglong( file, &l ) == -1 )
  84.     pm_error( "EOF / read error" );
  85.     *colsP = (int) l;
  86.     if ( pm_readbiglong( file, &l ) == -1 )
  87.     pm_error( "EOF / read error" );
  88.     *rowsP = (int) l;
  89.     if ( pm_readbigshort( file, depthP ) == -1 )
  90.     pm_error( "EOF / read error" );
  91.     *padrightP = ( ( *colsP + 7 ) / 8 ) * 8 - *colsP;
  92.  
  93.     bitsperitem = 0;
  94.     }
  95.  
  96. static bit
  97. getbit( file )
  98.     FILE* file;
  99.     {
  100.     bit b;
  101.  
  102.     if ( bitsperitem == 0 )
  103.     {
  104.     item = getc( file );
  105.     if ( item == EOF )
  106.         pm_error( "EOF / read error" );
  107.     bitsperitem = 8;
  108.     bitshift = 7;
  109.     }
  110.     b = ( ( item >> bitshift) & 1 ) ? PBM_WHITE : PBM_BLACK;
  111.     --bitsperitem;
  112.     --bitshift;
  113.     return b;
  114.     }
  115.