~ubuntu-branches/ubuntu/jaunty/netpbm-free/jaunty

« back to all changes in this revision

Viewing changes to pbm/icontopbm.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2004-07-29 20:25:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040729202546-5x43bcfsdlt7dspl
Tags: upstream-10.0
ImportĀ upstreamĀ versionĀ 10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <string.h>
 
14
#include <limits.h>
 
15
#include "pbm.h"
 
16
 
 
17
static void ReadIconFile ARGS(( FILE* file, int* width, int* height, short** data ));
 
18
 
 
19
int
 
20
main( argc, argv )
 
21
    int argc;
 
22
    char* argv[];
 
23
    {
 
24
    FILE* ifp;
 
25
    bit* bitrow;
 
26
    register bit* bP;
 
27
    int rows, cols, row, col, shortcount, mask;
 
28
    short* data;
 
29
 
 
30
 
 
31
    pbm_init( &argc, argv );
 
32
 
 
33
    if ( argc > 2 )
 
34
        pm_usage( "[iconfile]" );
 
35
 
 
36
    if ( argc == 2 )
 
37
        ifp = pm_openr( argv[1] );
 
38
    else
 
39
        ifp = stdin;
 
40
 
 
41
    ReadIconFile( ifp, &cols, &rows, &data );
 
42
 
 
43
    pm_close( ifp );
 
44
 
 
45
    pbm_writepbminit( stdout, cols, rows, 0 );
 
46
    bitrow = pbm_allocrow( cols );
 
47
 
 
48
    for ( row = 0; row < rows; row++ )
 
49
        {
 
50
        shortcount = 0;
 
51
        mask = 0x8000;
 
52
        for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
 
53
            {
 
54
            if ( shortcount >= 16 )
 
55
                {
 
56
                data++;
 
57
                shortcount = 0;
 
58
                mask = 0x8000;
 
59
                }
 
60
            *bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE;
 
61
            shortcount++;
 
62
            mask = mask >> 1;
 
63
            }
 
64
        data++;
 
65
        pbm_writepbmrow( stdout, bitrow, cols, 0 );
 
66
        }
 
67
 
 
68
    pm_close( stdout );
 
69
    exit( 0 );
 
70
    }
 
71
 
 
72
/* size in bytes of a bitmap */
 
73
#define BitmapSize(width, height) (((((width) + 15) >> 3) &~ 1) * (height))
 
74
 
 
75
static void
 
76
ReadIconFile( file, width, height, data )
 
77
    FILE* file;
 
78
    int* width;
 
79
    int* height;
 
80
    short** data;
 
81
    {
 
82
    char variable[81], ch;
 
83
    int status, value, i, data_length, gotsome;
 
84
 
 
85
    gotsome = 0;
 
86
    *width = *height = -1;
 
87
    for ( ; ; )
 
88
        {
 
89
        while ( ( ch = getc( file ) ) == ',' || ch == '\n' || ch == '\t' ||
 
90
                ch == ' ' )
 
91
            ;
 
92
        for ( i = 0;
 
93
              ch != '=' && ch != ',' && ch != '\n' && ch != '\t' && ch != ' ';
 
94
              i++ )
 
95
            {
 
96
            variable[i] = ch;
 
97
            ch = getc( file );
 
98
            }
 
99
        variable[i] = '\0';
 
100
 
 
101
        if ( strcmp( variable, "*/" ) == 0 && gotsome )
 
102
            break;
 
103
 
 
104
        if ( fscanf( file, "%d", &value ) != 1 )
 
105
            continue;
 
106
 
 
107
        if ( strcmp( variable, "Width" ) == 0 )
 
108
            {
 
109
            *width = value;
 
110
            gotsome = 1;
 
111
            }
 
112
        else if ( strcmp( variable, "Height" ) == 0 )
 
113
            {
 
114
            *height = value;
 
115
            gotsome = 1;
 
116
            }
 
117
        else if ( strcmp( variable, "Depth" ) == 0 )
 
118
            {
 
119
            if ( value != 1 )
 
120
                pm_error( "invalid depth" );
 
121
            gotsome = 1;
 
122
            }
 
123
        else if ( strcmp( variable, "Format_version" ) == 0 )
 
124
            {
 
125
            if ( value != 1 )
 
126
                pm_error( "invalid Format_version" );
 
127
            gotsome = 1;
 
128
            }
 
129
        else if ( strcmp( variable, "Valid_bits_per_item" ) == 0 )
 
130
            {
 
131
            if ( value != 16 )
 
132
                pm_error( "invalid Valid_bits_per_item" );
 
133
            gotsome = 1;
 
134
            }
 
135
        }
 
136
 
 
137
    if ( *width <= 0 )
 
138
        pm_error( "invalid width: %d", *width );
 
139
    if ( *height <= 0 )
 
140
        pm_error( "invalid height: %d", *height );
 
141
 
 
142
    if ( *width > INT_MAX - 16 || *width < 0)
 
143
        pm_error( "invalid width: %d", *width);
 
144
 
 
145
    overflow2(*width + 16, *height);
 
146
 
 
147
    data_length = BitmapSize( *width, *height );
 
148
    *data = (short*) malloc( data_length );
 
149
    if ( *data == NULL )
 
150
        pm_error( "out of memory" );
 
151
    data_length /= sizeof( short );
 
152
    
 
153
    for ( i = 0 ; i < data_length; i++ )
 
154
        {
 
155
        if ( i == 0 )
 
156
            status = fscanf( file, " 0x%4hx", *data );
 
157
        else
 
158
            status = fscanf( file, ", 0x%4hx", *data + i );
 
159
        if ( status != 1 )
 
160
            pm_error( "error 4 scanning bits item" );
 
161
        }
 
162
    }