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