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

  1. /* pbmtoybm.c - read a pbm and write a file for Bennet Yee's 'xbm' and 'face'
  2. ** programs.
  3. **
  4. ** Written by Jamie Zawinski based on code (C) 1988 by Jef Poskanzer.
  5. **
  6. ** Permission to use, copy, modify, and distribute this software and its
  7. ** documentation for any purpose and without fee is hereby granted, provided
  8. ** that the above copyright notice appear in all copies and that both that
  9. ** copyright notice and this permission notice appear in supporting
  10. ** documentation.  This software is provided "as is" without express or
  11. ** implied warranty.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include "pbm.h"
  16.  
  17. #define YBM_MAGIC  ( ( '!' << 8 ) | '!' )
  18.  
  19. static void putinit ARGS(( int cols, int rows ));
  20. static void putbit ARGS(( bit b ));
  21. static void putrest ARGS(( void ));
  22. static void putitem ARGS(( void ));
  23.  
  24. int
  25. main( argc, argv )
  26.     int argc;
  27.     char* argv[];
  28.     {
  29.     FILE* ifp;
  30.     bit* bitrow;
  31.     register bit* bP;
  32.     int rows, cols, format, padright, row, col;
  33.  
  34.  
  35.     pbm_init( &argc, argv );
  36.  
  37.     if ( argc > 2 )
  38.     pm_usage( "[pbmfile]" );
  39.     if ( argc == 2 )
  40.     ifp = pm_openr( argv[1] );
  41.     else
  42.     ifp = stdin;
  43.  
  44.     pbm_readpbminit( ifp, &cols, &rows, &format );
  45.     bitrow = pbm_allocrow( cols );
  46.     
  47.     /* Compute padding to round cols up to the nearest multiple of 16. */
  48.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  49.  
  50.     putinit( cols, rows );
  51.     for ( row = 0; row < rows; ++row )
  52.     {
  53.     pbm_readpbmrow( ifp, bitrow, cols, format );
  54.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  55.         putbit( *bP );
  56.     for ( col = 0; col < padright; ++col )
  57.         putbit( 0 );
  58.         }
  59.  
  60.     if ( ifp != stdin )
  61.     fclose( ifp );
  62.  
  63.     putrest( );
  64.  
  65.     exit( 0 );
  66.     }
  67.  
  68. static long item;
  69. static int bitsperitem, bitshift;
  70.  
  71. static void
  72. putinit( cols, rows )
  73.     int cols, rows;
  74.     {
  75.     pm_writebigshort( stdout, YBM_MAGIC );
  76.     pm_writebigshort( stdout, cols );
  77.     pm_writebigshort( stdout, rows );
  78.     item = 0;
  79.     bitsperitem = 0;
  80.     bitshift = 0;
  81.     }
  82.  
  83. #if __STDC__
  84. static void
  85. putbit( bit b )
  86. #else /*__STDC__*/
  87. static void
  88. putbit( b )
  89.     bit b;
  90. #endif /*__STDC__*/
  91.     {
  92.     if ( bitsperitem == 16 )
  93.     putitem( );
  94.     ++bitsperitem;
  95.     if ( b == PBM_BLACK )
  96.     item += 1 << bitshift;
  97.     ++bitshift;
  98.     }
  99.  
  100. static void
  101. putrest( )
  102.     {
  103.     if ( bitsperitem > 0 )
  104.     putitem( );
  105.     }
  106.  
  107. static void
  108. putitem( )
  109.     {
  110.     pm_writebigshort( stdout, item );
  111.     item = 0;
  112.     bitsperitem = 0;
  113.     bitshift = 0;
  114.     }
  115.