home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / pbm3 / part3 / xxxtopbm.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.3 KB  |  115 lines

  1. /* xxxtopbm.c - read an xxx bitmap and write a portable bitmap
  2. **
  3. ** Copyright (C) 1988 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 <stdio.h>
  14. #include "pbm.h"
  15.  
  16. main( argc, argv )
  17. int argc;
  18. char *argv[];
  19.     {
  20.     FILE *ifd;
  21.     bit **bits, getbit();
  22.     int rows, cols, padright, row, col, subcol;
  23.  
  24.     if ( argc > 2 )
  25.     {
  26.     fprintf( stderr, "usage:  %s [xxxfile]\n", argv[0] );
  27.     exit( 1 );
  28.     }
  29.  
  30.     if ( argc == 2 )
  31.     {
  32.         ifd = fopen( argv[1], "r" );
  33.         if ( ifd == NULL )
  34.         {
  35.         fprintf( stderr, "%s: can't open.\n", argv[1] );
  36.         exit( 1 );
  37.         }
  38.     }
  39.     else
  40.     ifd = stdin;
  41.  
  42.     getinit( ifd, &cols, &rows );
  43.  
  44.     bits = pbm_allocarray( cols, rows );
  45.  
  46.     /* Compute padding to round cols up to the next multiple of 16. */
  47.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  48.  
  49.     for ( row = 0; row < rows; row++ )
  50.     {
  51.     /* Get data, bit-reversed within each byte. */
  52.         for ( col = 0; col < cols; col += 8 )
  53.         for ( subcol = col + 7; subcol >= col; subcol-- )
  54.         bits[row][subcol] = getbit( ifd );
  55.     /* Discard line padding */
  56.         for ( col = 0; col < padright; col ++ )
  57.         (void) getbit( ifd );
  58.     }
  59.  
  60.     if ( ifd != stdin )
  61.     fclose( ifd );
  62.     
  63.     pbm_writepbm( stdout, bits, cols, rows );
  64.  
  65.     exit( 0 );
  66.     }
  67.  
  68.  
  69. int item, bitsperitem, bitshift;
  70.  
  71. getinit( file, colp, rowp )
  72. FILE *file;
  73. int *colp, *rowp;
  74.     {
  75.     int i;
  76.  
  77.     if ( getc( file ) != 109 )
  78.     {
  79.     fprintf( stderr, "Bad magic number 1.\n" );
  80.     exit( 1 );
  81.     }
  82.     if ( getc( file ) != 1 )
  83.     {
  84.     fprintf( stderr, "Bad magic number 2.\n" );
  85.     exit( 1 );
  86.     }
  87.     *colp = getc( file );
  88.     *colp += getc( file ) << 8;
  89.     *rowp = getc( file );
  90.     *rowp += getc( file ) << 8;
  91.     bitsperitem = 8;
  92.  
  93.     /* Junk rest of header. */
  94.     for ( i = 0; i < 2; i++ )
  95.         (void) getc( file );
  96.     }
  97.  
  98. bit
  99. getbit( file )
  100. FILE *file;
  101.     {
  102.     bit b;
  103.  
  104.     if ( bitsperitem == 8 )
  105.     {
  106.     item = getc( file );
  107.     bitsperitem = 0;
  108.     bitshift = 7;
  109.     }
  110.     bitsperitem++;
  111.     b = ( item >> bitshift) & 1;
  112.     bitshift--;
  113.     return b;
  114.     }
  115.