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

  1. /* pbmtomgr.c - read a portable bitmap and produce a MGR 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 "mgr.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.     struct b_header head;
  73.  
  74.     head.magic[0] = 'y';
  75.     head.magic[1] = 'z';
  76.     head.h_wide = ( ( cols >> 6 ) & 0x3f ) + ' ';
  77.     head.l_wide = ( cols & 0x3f ) + ' ';
  78.     head.h_high = ( ( rows >> 6 ) & 0x3f ) + ' ';
  79.     head.l_high = ( rows & 0x3f ) + ' ';
  80.     head.depth = ( 1 & 0x3f ) + ' ';
  81.     head._reserved = ' ';
  82.     fwrite( &head, sizeof(head), 1, stdout );
  83.  
  84.     item = 0;
  85.     bitsperitem = 0;
  86.     bitshift = 7;
  87.     }
  88.  
  89. #if __STDC__
  90. static void
  91. putbit( bit b )
  92. #else /*__STDC__*/
  93. static void
  94. putbit( b )
  95.     bit b;
  96. #endif /*__STDC__*/
  97.     {
  98.     if ( bitsperitem == 8 )
  99.     putitem( );
  100.     ++bitsperitem;
  101.     if ( b == PBM_BLACK )
  102.     item += 1 << bitshift;
  103.     --bitshift;
  104.     }
  105.  
  106. static void
  107. putrest( )
  108.     {
  109.     if ( bitsperitem > 0 )
  110.     putitem( );
  111.     }
  112.  
  113. static void
  114. putitem( )
  115.     {
  116.     fwrite( &item, sizeof(item), 1, stdout );
  117.     item = 0;
  118.     bitsperitem = 0;
  119.     bitshift = 7;
  120.     }
  121.