home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / pbm3 / part1 / icontopbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  3.7 KB  |  192 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 <stdio.h>
  14. #include <sys/types.h>
  15. #include "pbm.h"
  16.  
  17. main( argc, argv )
  18. int argc;
  19. char *argv[];
  20.     {
  21.     FILE *ifd;
  22.     bit **bits;
  23.     int rows, cols, row, col, shortcount, mask;
  24.     short *data;
  25.  
  26.     if ( argc > 2 )
  27.     {
  28.     fprintf( stderr, "usage: %s [iconfile]\n", argv[0] );
  29.     exit( 1 );
  30.     }
  31.  
  32.     if ( argc == 2 )
  33.     {
  34.     ifd = fopen( argv[1], "r" );
  35.     if ( ifd == NULL )
  36.         {
  37.         fprintf( stderr, "%s: can't open.\n", argv[1] );
  38.         exit( 1 );
  39.         }
  40.     }
  41.     else
  42.     ifd = stdin;
  43.  
  44.     if ( ReadIconFile( ifd, &cols, &rows, &data ) < 0 )
  45.     {
  46.     fprintf( stderr, "%s: can't load.\n", argv[1] );
  47.     exit( 1 );
  48.     }
  49.  
  50.     if ( ifd != stdin )
  51.     fclose( ifd );
  52.  
  53.     bits = pbm_allocarray( cols, rows );
  54.  
  55.     for ( row = 0; row < rows; row++ )
  56.     {
  57.     shortcount = 0;
  58.     mask = 0x8000;
  59.     for ( col = 0; col < cols; col++ )
  60.         {
  61.         if ( shortcount >= 16 )
  62.         {
  63.         data++;
  64.         shortcount = 0;
  65.         mask = 0x8000;
  66.         }
  67.         bits[row][col] = ( ( *data & mask ) ? 1 : 0 );
  68.         shortcount++;
  69.         mask = mask >> 1;
  70.         }
  71.     data++;
  72.     }
  73.  
  74.     pbm_writepbm( stdout, bits, cols, rows );
  75.  
  76.     exit( 0 );
  77.     }
  78.  
  79.  
  80. /* size in bytes of a bitmap */
  81. #define BitmapSize(width, height) (((((width) + 15) >> 3) &~ 1) * (height))
  82.  
  83. int
  84. ReadIconFile( file, width, height, data )
  85. FILE *file;
  86. int *width, *height;
  87. short **data;
  88.     {
  89.     char variable[81], ch;
  90.     int status, value, i, data_length, gotsome;
  91.  
  92.     if ( file == NULL )
  93.         return -1;
  94.  
  95.     gotsome = 0;
  96.     *width = *height = -1;
  97.     for ( ; ; )
  98.     {
  99.     while ( ( ch = getc( file ) ) == ',' | ch == '\n' | ch == '\t' |
  100.         ch == ' ' )
  101.         ;
  102.     for ( i = 0;
  103.           ch != '=' & ch != ',' & ch != '\n' & ch != '\t' & ch != ' ';
  104.           i++ )
  105.         {
  106.         variable[i] = ch;
  107.         ch = getc( file );
  108.         }
  109.     variable[i] = '\0';
  110.  
  111.     if ( strcmp( variable, "*/" ) == 0 & gotsome )
  112.         break;
  113.  
  114.     if ( fscanf( file, "%d", &value ) != 1 )
  115.         continue;
  116.  
  117.     if ( strcmp( variable, "Width" ) == 0 )
  118.         {
  119.         *width = value;
  120.         gotsome = 1;
  121.         }
  122.     else if ( strcmp( variable, "Height" ) == 0 )
  123.         {
  124.             *height = value;
  125.         gotsome = 1;
  126.         }
  127.     else if ( strcmp( variable, "Depth" ) == 0 )
  128.             {
  129.         if ( value != 1 )
  130.         {
  131.         fprintf( stderr, "Invalid depth.\n" );
  132.         return -1;
  133.         }
  134.         gotsome = 1;
  135.         }
  136.     else if ( strcmp( variable, "Format_version" ) == 0 )
  137.             {
  138.         if ( value != 1 )
  139.         {
  140.         fprintf( stderr, "Invalid Format_version.\n" );
  141.         return -1;
  142.         }
  143.         gotsome = 1;
  144.         }
  145.     else if ( strcmp( variable, "Valid_bits_per_item" ) == 0 )
  146.             {
  147.         if ( value != 16 )
  148.         {
  149.         fprintf( stderr, "Invalid Valid_bits_per_item.\n" );
  150.         return -1;
  151.         }
  152.         gotsome = 1;
  153.         }
  154.     }
  155.  
  156.     if ( *width <= 0 )
  157.     {
  158.     fprintf( stderr, "Invalid width: %d.\n", *width );
  159.     return -1;
  160.     }
  161.     
  162.     if ( *height <= 0 )
  163.     {
  164.     fprintf( stderr, "Invalid height: %d.\n", *height );
  165.     return -1;
  166.     }
  167.  
  168.     data_length = BitmapSize( *width, *height );
  169.     *data = (short *) malloc( data_length );
  170.     data_length /= sizeof( short );
  171.     if ( *data == NULL )
  172.         {
  173.         return -1;
  174.     }
  175.     
  176.     for ( i = 0 ; i < data_length; i++ )
  177.     {
  178.     if ( i == 0 )
  179.         status = fscanf( file, " 0x%4hx", *data );
  180.     else
  181.         status = fscanf( file, ", 0x%4hx", *data + i );
  182.     if ( status != 1 )
  183.         {
  184.         free( *data );
  185.         fprintf( stderr, "Error 4 scanning bits item.\n" );
  186.         return -1;
  187.         }
  188.         }
  189.  
  190.     return 0;
  191.     }
  192.