about summary refs log tree commit diff
path: root/converter/pbm/icontopbm.c
blob: a0d1bd2bd6c3a2d4e83e9b45f2b57bb133b98db2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* icontopbm.c - read a Sun icon file and produce a portable bitmap
**
** Copyright (C) 1988 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <string.h>

#include "nstring.h"
#include "pbm.h"

/* size in bytes of a bitmap */
#define BitmapSize(width, height) (((((width) + 15) >> 3) &~ 1) * (height))

static void
ReadIconFile(FILE *                const file, 
             int *                 const widthP, 
             int *                 const heightP, 
             short unsigned int ** const dataP) {

    char variable[80+1];
    int ch;
    int status, value, i, data_length, gotsome;

    gotsome = 0;
    *widthP = *heightP = -1;
    for ( ; ; )
    {
        while ( ( ch = getc( file ) ) == ',' || ch == '\n' || ch == '\t' ||
                ch == ' ' )
            ;
        for ( i = 0;
              ch != '=' && ch != ',' && ch != '\n' && ch != '\t' && 
                  ch != ' ' && (i < (sizeof(variable) - 1));
              i++ )
        {
            variable[i] = ch;
            if ((ch = getc( file )) == EOF)
                pm_error( "invalid input file -- premature EOF" );
        }
        variable[i] = '\0';

        if ( streq( variable, "*/" )&& gotsome )
            break;

        if ( fscanf( file, "%d", &value ) != 1 )
            continue;

        if ( streq( variable, "Width" ) )
        {
            *widthP = value;
            gotsome = 1;
        }
        else if ( streq( variable, "Height" ) )
        {
            *heightP = value;
            gotsome = 1;
        }
        else if ( streq( variable, "Depth" )  )
        {
            if ( value != 1 )
                pm_error( "invalid depth" );
            gotsome = 1;
        }
        else if ( streq( variable, "Format_version" ) )
        {
            if ( value != 1 )
                pm_error( "invalid Format_version" );
            gotsome = 1;
        }
        else if ( streq( variable, "Valid_bits_per_item" ) )
        {
            if ( value != 16 )
                pm_error( "invalid Valid_bits_per_item" );
            gotsome = 1;
        }
    }

    if ( *widthP <= 0 )
        pm_error( "invalid width (must be positive): %d", *widthP );
    if ( *heightP <= 0 )
        pm_error( "invalid height (must be positive): %d", *heightP );

    data_length = BitmapSize( *widthP, *heightP );
    *dataP = (short unsigned int *) malloc( data_length );
    if ( *dataP == NULL )
        pm_error( "out of memory" );
    data_length /= sizeof( short );
    
    for ( i = 0 ; i < data_length; i++ )
    {
        if ( i == 0 )
            status = fscanf( file, " 0x%4hx", *dataP );
        else
            status = fscanf( file, ", 0x%4hx", *dataP + i );
        if ( status != 1 )
            pm_error( "error 4 scanning bits item" );
    }
}



int
main(int  argc, char ** argv) {

    FILE* ifp;
    bit* bitrow;
    register bit* bP;
    int rows, cols, row, col, shortcount, mask;
    short unsigned int * data;


    pbm_init( &argc, argv );

    if ( argc > 2 )
        pm_usage( "[iconfile]" );

    if ( argc == 2 )
        ifp = pm_openr( argv[1] );
    else
        ifp = stdin;

    ReadIconFile( ifp, &cols, &rows, &data );

    pm_close( ifp );

    pbm_writepbminit( stdout, cols, rows, 0 );
    bitrow = pbm_allocrow( cols );

    for ( row = 0; row < rows; row++ )
    {
        shortcount = 0;
        mask = 0x8000;
        for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
        {
            if ( shortcount >= 16 )
            {
                data++;
                shortcount = 0;
                mask = 0x8000;
            }
            *bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE;
            shortcount++;
            mask = mask >> 1;
        }
        data++;
        pbm_writepbmrow( stdout, bitrow, cols, 0 );
    }

    pm_close( stdout );
    exit( 0 );
}