home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / pbm / Part1 / icontopbm.c < prev    next >
C/C++ Source or Header  |  1991-08-07  |  4KB  |  210 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, firsttime, value, i, data_length;
  91.  
  92.     if ( file == NULL )
  93.         return ( -1 );
  94.  
  95.     if ( getc( file ) != '/' )
  96.     {
  97.     fprintf( stderr, "Error 1 scanning beginning of initial section.\n" );
  98.     return ( -1 );
  99.     }
  100.     if ( getc( file ) != '*' )
  101.     {
  102.     fprintf( stderr, "Error 2 scanning beginning of initial section.\n" );
  103.     return ( -1 );
  104.     }
  105.     while ( ( ch = getc( file ) ) == '\n' | ch == '\t' | ch == ' ' )
  106.     ;
  107.     ungetc( ch, stdin );
  108.  
  109.     *width = *height = -1;
  110.     firsttime = -1;
  111.     for ( ; ; )
  112.     {
  113.     if ( firsttime )
  114.         firsttime = 0;
  115.     else
  116.         if ( getc( file ) != ',' )
  117.         break;
  118.     while ( ( ch = getc( file ) ) == '\n' | ch == '\t' | ch == ' ' )
  119.         ;
  120.     for ( i = 0; ch != '='; i++ )
  121.         {
  122.         variable[i] = ch;
  123.         ch = getc( file );
  124.         }
  125.     variable[i] = '\0';
  126.  
  127.     if ( fscanf( file, "%d", &value ) != 1 )
  128.         break;
  129.  
  130.     if ( strcmp( variable, "Width" ) == 0 )
  131.         *width = value;
  132.     else if ( strcmp( variable, "Height" ) == 0 )
  133.             *height = value;
  134.     else if ( strcmp( variable, "Depth" ) == 0 )
  135.             {
  136.         if ( value != 1 )
  137.         {
  138.         fprintf( stderr, "Invalid depth.\n" );
  139.         return ( -1 );
  140.         }
  141.         }
  142.     else if ( strcmp( variable, "Format_version" ) == 0 )
  143.             {
  144.         if ( value != 1 )
  145.         {
  146.         fprintf( stderr, "Invalid Format_version.\n" );
  147.         return ( -1 );
  148.         }
  149.         }
  150.     else if ( strcmp( variable, "Valid_bits_per_item" ) == 0 )
  151.             {
  152.         if ( value != 16 )
  153.         {
  154.         fprintf( stderr, "Invalid Valid_bits_per_item.\n" );
  155.         return ( -1 );
  156.         }
  157.         }
  158.     }
  159.  
  160.     for ( ; ; )
  161.     {
  162.     while ( getc( file ) != '*' )
  163.         ;
  164.     if ( ( ch = getc( file ) ) == '/' )
  165.         break;
  166.     ungetc( ch, stdin );
  167.     }
  168.     if ( getc( file ) != '\n' )
  169.     {
  170.     fprintf( stderr, "Error 3 scanning end of initial section.\n" );
  171.     return ( -1 );
  172.     }
  173.  
  174.     if ( *width <= 0 )
  175.     {
  176.     fprintf( stderr, "Invalid width: %d.\n", *width );
  177.     return ( -1 );
  178.     }
  179.     
  180.     if ( *height <= 0 )
  181.     {
  182.     fprintf( stderr, "Invalid height: %d.\n", *height );
  183.     return ( -1 );
  184.     }
  185.  
  186.     data_length = BitmapSize( *width, *height );
  187.     *data = (short *) malloc( data_length );
  188.     data_length /= sizeof( short );
  189.     if ( *data == NULL )
  190.         {
  191.         return ( -1 );
  192.     }
  193.     
  194.     for ( i = 0 ; i < data_length; i++ )
  195.     {
  196.     if ( i == 0 )
  197.         status = fscanf( file, " 0x%4hx", *data );
  198.     else
  199.         status = fscanf( file, ", 0x%4hx", *data + i );
  200.     if ( status != 1 )
  201.         {
  202.         free( *data );
  203.         fprintf( stderr, "Error 4 scanning bits item.\n" );
  204.         return ( -1 );
  205.         }
  206.         }
  207.  
  208.     return ( 0 );
  209.     }
  210.