home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / cmuwmtop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  2.7 KB  |  114 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. void
  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.     pbm_init( &argc, argv );
  31.  
  32.     if ( argc > 2 )
  33.     pm_usage( "[cmuwmfile]" );
  34.  
  35.     if ( argc == 2 )
  36.     ifp = pm_openr( argv[1] );
  37.     else
  38.     ifp = stdin;
  39.  
  40.     getinit( ifp, &cols, &rows, &depth, &padright );
  41.     if ( depth != 1 )
  42.     pm_error(
  43.         "CMU window manager file has depth of %d, must be 1",
  44.         (int) depth );
  45.  
  46.     pbm_writepbminit( stdout, cols, rows, 0 );
  47.     bitrow = pbm_allocrow( cols );
  48.  
  49.     for ( row = 0; row < rows; ++row )
  50.     {
  51.     /* Get data. */
  52.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  53.         *bP = getbit( ifp );
  54.     /* Discard line padding */
  55.         for ( col = 0; col < padright; ++col )
  56.         (void) getbit( ifp );
  57.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  58.     }
  59.  
  60.     pm_close( ifp );
  61.     pm_close( stdout );
  62.  
  63.     exit( 0 );
  64.     }
  65.  
  66. static int item, bitsperitem, bitshift;
  67.  
  68. static void
  69. getinit( file, colsP, rowsP, depthP, padrightP )
  70.     FILE* file;
  71.     int* colsP;
  72.     int* rowsP;
  73.     short* depthP;
  74.     int* padrightP;
  75.     {
  76.     long l;
  77.  
  78.     if ( pm_readbiglong( file, &l ) == -1 )
  79.     pm_error( "EOF / read error" );
  80.     if ( l != CMUWM_MAGIC )
  81.     pm_error( "bad magic number in CMU window manager file" );
  82.     if ( pm_readbiglong( file, &l ) == -1 )
  83.     pm_error( "EOF / read error" );
  84.     *colsP = (int) l;
  85.     if ( pm_readbiglong( file, &l ) == -1 )
  86.     pm_error( "EOF / read error" );
  87.     *rowsP = (int) l;
  88.     if ( pm_readbigshort( file, depthP ) == -1 )
  89.     pm_error( "EOF / read error" );
  90.     *padrightP = ( ( *colsP + 7 ) / 8 ) * 8 - *colsP;
  91.  
  92.     bitsperitem = 0;
  93.     }
  94.  
  95. static bit
  96. getbit( file )
  97.     FILE* file;
  98.     {
  99.     bit b;
  100.  
  101.     if ( bitsperitem == 0 )
  102.     {
  103.     item = getc( file );
  104.     if ( item == EOF )
  105.         pm_error( "EOF / read error" );
  106.     bitsperitem = 8;
  107.     bitshift = 7;
  108.     }
  109.     b = ( ( item >> bitshift) & 1 ) ? PBM_WHITE : PBM_BLACK;
  110.     --bitsperitem;
  111.     --bitshift;
  112.     return b;
  113.     }
  114.