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

  1. /* mgrtopbm.c - read a MGR 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 "mgr.h"
  15.  
  16. static void getinit ARGS(( FILE* file, int* colsP, int* rowsP, int* 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, depth, padright, row, col;
  28.  
  29.  
  30.     pbm_init( &argc, argv );
  31.  
  32.     if ( argc > 2 )
  33.     pm_usage( "[mgrfile]" );
  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( "MGR file has depth of %d, must be 1", depth );
  43.  
  44.     pbm_writepbminit( stdout, cols, rows, 0 );
  45.     bitrow = pbm_allocrow( cols );
  46.  
  47.     for ( row = 0; row < rows; row++ )
  48.     {
  49.     /* Get data, bit-reversed within each byte. */
  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.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  56.     }
  57.  
  58.     pm_close( ifp );
  59.     pm_close( stdout );
  60.  
  61.     exit( 0 );
  62.     }
  63.  
  64.  
  65. static unsigned char item;
  66. static int bitsperitem, bitshift;
  67.  
  68. static void
  69. getinit( file, colsP, rowsP, depthP, padrightP )
  70.     FILE* file;
  71.     int* colsP;
  72.     int* rowsP;
  73.     int* depthP;
  74.     int* padrightP;
  75.     {
  76.     struct b_header head;
  77.     int pad;
  78.  
  79.     if ( fread( &head, sizeof(struct old_b_header), 1, file ) != 1 )
  80.     pm_perror( "reading header" );
  81.     if ( head.magic[0] == 'y' && head.magic[1] == 'z' )
  82.     { /* new style bitmap */
  83.     if ( fread( &head.depth, sizeof(head) - sizeof(struct old_b_header), 1, file ) != 1 )
  84.         pm_perror( "reading rest of header" );
  85.     *depthP = (int) head.depth - ' ';
  86.     pad = 8;
  87.     }
  88.     else if ( head.magic[0] == 'x' && head.magic[1] == 'z' )
  89.     { /* old style bitmap with 32-bit padding */
  90.     *depthP = 1;
  91.     pad = 32;
  92.     }
  93.     else if ( head.magic[0] == 'z' && head.magic[1] == 'z' )
  94.     { /* old style bitmap with 16-bit padding */
  95.     *depthP = 1;
  96.     pad = 16;
  97.     }
  98.     else if ( head.magic[0] == 'z' && head.magic[1] == 'y' )
  99.     { /* old style 8-bit pixmap with 16-bit padding */
  100.     *depthP = 8;
  101.     pad = 16;
  102.     }
  103.     else
  104.     pm_error(
  105.         "bad magic chars in MGR file: '%c%c'",
  106.         head.magic[0], head.magic[1] );
  107.     *colsP = ( ( (int) head.h_wide - ' ' ) << 6 ) + ( (int) head.l_wide - ' ' );
  108.     *rowsP = ( ( (int) head.h_high - ' ' ) << 6 ) + ( (int) head.l_high - ' ' );
  109.     *padrightP = ( ( *colsP + pad - 1 ) / pad ) * pad - *colsP;
  110.  
  111.     bitsperitem = 8;
  112.     }
  113.  
  114. static bit
  115. getbit( file )
  116.     FILE* file;
  117.     {
  118.     bit b;
  119.  
  120.     if ( bitsperitem == 8 )
  121.     {
  122.     item = getc( file );
  123.     bitsperitem = 0;
  124.     bitshift = 7;
  125.     }
  126.     bitsperitem++;
  127.     b = ( ( item >> bitshift) & 1 ) ? PBM_BLACK : PBM_WHITE;
  128.     bitshift--;
  129.     return b;
  130.     }
  131.