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

  1. /* pbmtoicon.c - read a portable bitmap and produce a Sun icon file
  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 "pbm.h"
  14.  
  15. static void putinit ARGS(( void ));
  16. static void putbit ARGS(( bit b ));
  17. static void putrest ARGS(( void ));
  18. static void putitem ARGS(( void ));
  19.  
  20. int
  21. main( argc, argv )
  22.     int argc;
  23.     char* argv[];
  24.     {
  25.     FILE* ifp;
  26.     bit* bitrow;
  27.     register bit* bP;
  28.     int rows, cols, format, pad, padleft, padright, row, col;
  29.  
  30.  
  31.     pbm_init( &argc, argv );
  32.  
  33.     if ( argc > 2 )
  34.     pm_usage( "[pbmfile]" );
  35.  
  36.     if ( argc == 2 )
  37.     ifp = pm_openr( argv[1] );
  38.     else
  39.     ifp = stdin;
  40.  
  41.     pbm_readpbminit( ifp, &cols, &rows, &format );
  42.     bitrow = pbm_allocrow( cols );
  43.     
  44.     /* Round cols up to the nearest multiple of 16. */
  45.     pad = ( ( cols + 15 ) / 16 ) * 16 - cols;
  46.     padleft = pad / 2;
  47.     padright = pad - padleft;
  48.  
  49.     printf( "/* Format_version=1, Width=%d, Height=%d", cols + pad, rows );
  50.     printf( ", Depth=1, Valid_bits_per_item=16\n */\n" );
  51.  
  52.     putinit( );
  53.     for ( row = 0; row < rows; ++row )
  54.     {
  55.     pbm_readpbmrow( ifp, bitrow, cols, format );
  56.     for ( col = 0; col < padleft; ++col )
  57.         putbit( 0 );
  58.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  59.         putbit( *bP );
  60.     for ( col = 0; col < padright; ++col )
  61.         putbit( 0 );
  62.         }
  63.  
  64.     pm_close( ifp );
  65.  
  66.     putrest( );
  67.  
  68.     exit( 0 );
  69.     }
  70.  
  71. static int item, bitsperitem, bitshift, itemsperline, firstitem;
  72.  
  73. static void
  74. putinit( )
  75.     {
  76.     itemsperline = 0;
  77.     bitsperitem = 0;
  78.     item = 0;
  79.     bitshift = 15;
  80.     firstitem = 1;
  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.     putchar( '\n' );
  106.     }
  107.  
  108. static void
  109. putitem( )
  110.     {
  111.     char* hexits = "0123456789abcdef";
  112.  
  113.     if ( firstitem )
  114.     firstitem = 0;
  115.     else
  116.     putchar( ',' );
  117.     if ( itemsperline == 8 )
  118.     {
  119.     putchar( '\n' );
  120.     itemsperline = 0;
  121.     }
  122.     if ( itemsperline == 0 )
  123.     putchar( '\t' );
  124.     putchar( '0' );
  125.     putchar( 'x' );
  126.     putchar( hexits[item >> 12] );
  127.     putchar( hexits[( item >> 8 ) & 15] );
  128.     putchar( hexits[( item >> 4 ) & 15] );
  129.     putchar( hexits[item & 15] );
  130.     ++itemsperline;
  131.     bitsperitem = 0;
  132.     item = 0;
  133.     bitshift = 15;
  134.     }
  135.