home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pbm / Part4 / xxxtopbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  2.1 KB  |  110 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, 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.     for ( row = 0; row < rows; row++ )
  47.         for ( col = 0; col < cols; col += 8 )
  48.         for ( subcol = col + 7; subcol >= col; subcol-- )
  49.         bits[row][subcol] = getbit( ifd );
  50.  
  51.     if ( ifd != stdin )
  52.     fclose( ifd );
  53.     
  54.     pbm_writepbm( stdout, bits, cols, rows );
  55.  
  56.     exit( 0 );
  57.     }
  58.  
  59.  
  60. int item, bitsperitem, bitshift;
  61.  
  62. getinit( file, colp, rowp )
  63. FILE *file;
  64. int *colp, *rowp;
  65.     {
  66.     if ( getc( file ) != 109 )
  67.     {
  68.     fprintf( stderr, "Bad magic number 1.\n" );
  69.     exit( 1 );
  70.     }
  71.     if ( getc( file ) != 1 )
  72.     {
  73.     fprintf( stderr, "Bad magic number 2.\n" );
  74.     exit( 1 );
  75.     }
  76.     *colp = getc( file );
  77.     *colp += getc( file ) << 8;
  78.     *rowp = getc( file );
  79.     *rowp += getc( file ) << 8;
  80.     bitsperitem = 8;
  81.     if ( getc( file ) != 0 )
  82.     {
  83.     fprintf( stderr, "Bad magic number 3.\n" );
  84.     exit( 1 );
  85.     }
  86.     if ( getc( file ) != 46 )
  87.     {
  88.     fprintf( stderr, "Bad magic number 4.\n" );
  89.     exit( 1 );
  90.     }
  91.     }
  92.  
  93. bit
  94. getbit( file )
  95. FILE *file;
  96.     {
  97.     bit b;
  98.  
  99.     if ( bitsperitem == 8 )
  100.     {
  101.     item = getc( file );
  102.     bitsperitem = 0;
  103.     bitshift = 7;
  104.     }
  105.     bitsperitem++;
  106.     b = ( item >> bitshift) & 1;
  107.     bitshift--;
  108.     return ( b );
  109.     }
  110.