1
/* icontopbm.c - read a Sun icon file and produce a portable bitmap
3
** Copyright (C) 1988 by Jef Poskanzer.
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
17
static void ReadIconFile ARGS(( FILE* file, int* width, int* height, short** data ));
27
int rows, cols, row, col, shortcount, mask;
31
pbm_init( &argc, argv );
34
pm_usage( "[iconfile]" );
37
ifp = pm_openr( argv[1] );
41
ReadIconFile( ifp, &cols, &rows, &data );
45
pbm_writepbminit( stdout, cols, rows, 0 );
46
bitrow = pbm_allocrow( cols );
48
for ( row = 0; row < rows; row++ )
52
for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
54
if ( shortcount >= 16 )
60
*bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE;
65
pbm_writepbmrow( stdout, bitrow, cols, 0 );
72
/* size in bytes of a bitmap */
73
#define BitmapSize(width, height) (((((width) + 15) >> 3) &~ 1) * (height))
76
ReadIconFile( file, width, height, data )
82
char variable[81], ch;
83
int status, value, i, data_length, gotsome;
86
*width = *height = -1;
89
while ( ( ch = getc( file ) ) == ',' || ch == '\n' || ch == '\t' ||
93
ch != '=' && ch != ',' && ch != '\n' && ch != '\t' && ch != ' ';
101
if ( strcmp( variable, "*/" ) == 0 && gotsome )
104
if ( fscanf( file, "%d", &value ) != 1 )
107
if ( strcmp( variable, "Width" ) == 0 )
112
else if ( strcmp( variable, "Height" ) == 0 )
117
else if ( strcmp( variable, "Depth" ) == 0 )
120
pm_error( "invalid depth" );
123
else if ( strcmp( variable, "Format_version" ) == 0 )
126
pm_error( "invalid Format_version" );
129
else if ( strcmp( variable, "Valid_bits_per_item" ) == 0 )
132
pm_error( "invalid Valid_bits_per_item" );
138
pm_error( "invalid width: %d", *width );
140
pm_error( "invalid height: %d", *height );
142
if ( *width > INT_MAX - 16 || *width < 0)
143
pm_error( "invalid width: %d", *width);
145
overflow2(*width + 16, *height);
147
data_length = BitmapSize( *width, *height );
148
*data = (short*) malloc( data_length );
150
pm_error( "out of memory" );
151
data_length /= sizeof( short );
153
for ( i = 0 ; i < data_length; i++ )
156
status = fscanf( file, " 0x%4hx", *data );
158
status = fscanf( file, ", 0x%4hx", *data + i );
160
pm_error( "error 4 scanning bits item" );