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

  1. /* icontopbm.c - read a Sun icon file and produce 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 "pbm.h"
  14.  
  15. static void ReadIconFile ARGS(( FILE* file, int* width, int* height, short** data ));
  16.  
  17. int
  18. main( argc, argv )
  19.     int argc;
  20.     char* argv[];
  21.     {
  22.     FILE* ifp;
  23.     bit* bitrow;
  24.     register bit* bP;
  25.     int rows, cols, row, col, shortcount, mask;
  26.     short* data;
  27.  
  28.  
  29.     pbm_init( &argc, argv );
  30.  
  31.     if ( argc > 2 )
  32.     pm_usage( "[iconfile]" );
  33.  
  34.     if ( argc == 2 )
  35.     ifp = pm_openr( argv[1] );
  36.     else
  37.     ifp = stdin;
  38.  
  39.     ReadIconFile( ifp, &cols, &rows, &data );
  40.  
  41.     pm_close( ifp );
  42.  
  43.     pbm_writepbminit( stdout, cols, rows, 0 );
  44.     bitrow = pbm_allocrow( cols );
  45.  
  46.     for ( row = 0; row < rows; row++ )
  47.     {
  48.     shortcount = 0;
  49.     mask = 0x8000;
  50.     for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  51.         {
  52.         if ( shortcount >= 16 )
  53.         {
  54.         data++;
  55.         shortcount = 0;
  56.         mask = 0x8000;
  57.         }
  58.         *bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE;
  59.         shortcount++;
  60.         mask = mask >> 1;
  61.         }
  62.     data++;
  63.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  64.     }
  65.  
  66.     pm_close( stdout );
  67.     exit( 0 );
  68.     }
  69.  
  70. /* size in bytes of a bitmap */
  71. #define BitmapSize(width, height) (((((width) + 15) >> 3) &~ 1) * (height))
  72.  
  73. static void
  74. ReadIconFile( file, width, height, data )
  75.     FILE* file;
  76.     int* width;
  77.     int* height;
  78.     short** data;
  79.     {
  80.     char variable[81], ch;
  81.     int status, value, i, data_length, gotsome;
  82.  
  83.     gotsome = 0;
  84.     *width = *height = -1;
  85.     for ( ; ; )
  86.     {
  87.     while ( ( ch = getc( file ) ) == ',' || ch == '\n' || ch == '\t' ||
  88.         ch == ' ' )
  89.         ;
  90.     for ( i = 0;
  91.           ch != '=' && ch != ',' && ch != '\n' && ch != '\t' && ch != ' ';
  92.           i++ )
  93.         {
  94.         variable[i] = ch;
  95.         ch = getc( file );
  96.         }
  97.     variable[i] = '\0';
  98.  
  99.     if ( strcmp( variable, "*/" ) == 0 && gotsome )
  100.         break;
  101.  
  102.     if ( fscanf( file, "%d", &value ) != 1 )
  103.         continue;
  104.  
  105.     if ( strcmp( variable, "Width" ) == 0 )
  106.         {
  107.         *width = value;
  108.         gotsome = 1;
  109.         }
  110.     else if ( strcmp( variable, "Height" ) == 0 )
  111.         {
  112.             *height = value;
  113.         gotsome = 1;
  114.         }
  115.     else if ( strcmp( variable, "Depth" ) == 0 )
  116.             {
  117.         if ( value != 1 )
  118.         pm_error( "invalid depth" );
  119.         gotsome = 1;
  120.         }
  121.     else if ( strcmp( variable, "Format_version" ) == 0 )
  122.             {
  123.         if ( value != 1 )
  124.         pm_error( "invalid Format_version" );
  125.         gotsome = 1;
  126.         }
  127.     else if ( strcmp( variable, "Valid_bits_per_item" ) == 0 )
  128.             {
  129.         if ( value != 16 )
  130.         pm_error( "invalid Valid_bits_per_item" );
  131.         gotsome = 1;
  132.         }
  133.     }
  134.  
  135.     if ( *width <= 0 )
  136.     pm_error( "invalid width: %d", *width );
  137.     if ( *height <= 0 )
  138.     pm_error( "invalid height: %d", *height );
  139.  
  140.     data_length = BitmapSize( *width, *height );
  141.     *data = (short*) malloc( data_length );
  142.     if ( *data == NULL )
  143.         pm_error( "out of memory" );
  144.     data_length /= sizeof( short );
  145.     
  146.     for ( i = 0 ; i < data_length; i++ )
  147.     {
  148.     if ( i == 0 )
  149.         status = fscanf( file, " 0x%4hx", *data );
  150.     else
  151.         status = fscanf( file, ", 0x%4hx", *data + i );
  152.     if ( status != 1 )
  153.         pm_error( "error 4 scanning bits item" );
  154.         }
  155.     }
  156.