home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pbm / Part2 / pbmtoicon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  2.4 KB  |  121 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 <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, rucols, padleft, padright, row, col;
  23.     char ch;
  24.  
  25.     if ( argc > 2 )
  26.     {
  27.     fprintf( stderr, "usage:  %s [pbmfile]\n", argv[0] );
  28.     exit( 1 );
  29.     }
  30.  
  31.     if ( argc == 2 )
  32.     {
  33.         ifd = fopen( argv[1], "r" );
  34.         if ( ifd == NULL )
  35.         {
  36.         fprintf( stderr, "%s: can't open.\n", argv[1] );
  37.         exit( 1 );
  38.         }
  39.     }
  40.     else
  41.     ifd = stdin;
  42.  
  43.     bits = pbm_readpbm( ifd, &cols, &rows );
  44.  
  45.     if ( ifd != stdin )
  46.     fclose( ifd );
  47.     
  48.     /* Round cols up to the nearest multiple of 16. */
  49.     rucols = ( cols + 15 ) / 16;
  50.     rucols = rucols * 16;
  51.     padleft = ( rucols - cols ) / 2;
  52.     padright = rucols - cols - padleft;
  53.  
  54.     printf( "/* Format_version=1, Width=%d, Height=%d", rucols, rows );
  55.     printf( ", Depth=1, Valid_bits_per_item=16\n */\n" );
  56.  
  57.     putinit( );
  58.     for ( row = 0; row < rows; row++ )
  59.     {
  60.     for ( col = 0; col < padleft; col++ )
  61.         putbit( 0 );
  62.         for ( col = 0; col < cols; col++ )
  63.         putbit( bits[row][col] );
  64.     for ( col = 0; col < padright; col++ )
  65.         putbit( 0 );
  66.         }
  67.     putrest( );
  68.  
  69.     exit( 0 );
  70.     }
  71.  
  72.  
  73. int item, bitsperitem, bitshift, itemsperline, firstitem;
  74.  
  75. putinit( )
  76.     {
  77.     itemsperline = 0;
  78.     bitsperitem = 0;
  79.     item = 0;
  80.     bitshift = 15;
  81.     firstitem = 1;
  82.     }
  83.  
  84. putbit( b )
  85. bit b;
  86.     {
  87.     if ( bitsperitem == 16 )
  88.     putitem( );
  89.     bitsperitem++;
  90.     if ( b )
  91.     item += 1 << bitshift;
  92.     bitshift--;
  93.     }
  94.  
  95. putrest( )
  96.     {
  97.     if ( bitsperitem > 0 )
  98.     putitem( );
  99.     putchar( '\n' );
  100.     }
  101.  
  102. putitem( )
  103.     {
  104.     if ( firstitem )
  105.     firstitem = 0;
  106.     else
  107.     putchar( ',' );
  108.     if ( itemsperline == 8 )
  109.     {
  110.     putchar( '\n' );
  111.     itemsperline = 0;
  112.     }
  113.     if ( itemsperline == 0 )
  114.     putchar( '\t' );
  115.     itemsperline++;
  116.     printf( "0x%04x", item );
  117.     bitsperitem = 0;
  118.     item = 0;
  119.     bitshift = 15;
  120.     }
  121.