home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / pbm / Part2 / pbmtocbm.c next >
C/C++ Source or Header  |  1991-08-07  |  2KB  |  97 lines

  1. /* pbmtocbm.c - read a portable bitmap and produce a compact 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;
  22.     int rows, cols, row, col;
  23.  
  24.     if ( argc > 2 )
  25.     {
  26.     fprintf( stderr, "usage:  %s [pbmfile]\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.     bits = pbm_readpbm( ifd, &cols, &rows );
  43.  
  44.     if ( ifd != stdin )
  45.     fclose( ifd );
  46.     
  47.     putchar( (char) 42 );
  48.     putchar( (char) 23 );
  49.     putchar( (char) ( ( cols >> 8 ) & 0xff ) );
  50.     putchar( (char) ( cols & 0xff ) );
  51.     putchar( (char) ( ( rows >> 8 ) & 0xff ) );
  52.     putchar( (char) ( rows & 0xff ) );
  53.  
  54.     putinit( );
  55.     for ( row = 0; row < rows; row++ )
  56.         for ( col = 0; col < cols; col++ )
  57.         putbit( bits[row][col] );
  58.     putrest( );
  59.  
  60.     exit( 0 );
  61.     }
  62.  
  63.  
  64. int item, bitsperitem, bitshift;
  65.  
  66. putinit( )
  67.     {
  68.     bitsperitem = 0;
  69.     item = 0;
  70.     bitshift = 7;
  71.     }
  72.  
  73. putbit( b )
  74. bit b;
  75.     {
  76.     if ( bitsperitem == 8 )
  77.     putitem( );
  78.     bitsperitem++;
  79.     if ( b )
  80.     item += 1 << bitshift;
  81.     bitshift--;
  82.     }
  83.  
  84. putrest( )
  85.     {
  86.     if ( bitsperitem > 0 )
  87.     putitem( );
  88.     }
  89.  
  90. putitem( )
  91.     {
  92.     putchar( (char) item );
  93.     bitsperitem = 0;
  94.     item = 0;
  95.     bitshift = 7;
  96.     }
  97.