about summary refs log tree commit diff
path: root/converter/pbm/xbmtopbm.c
blob: 63be16efae5064611f069da5ef734d94ae90dd38 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* xbmtopbm.c - read an X bitmap file and produce a PBM image
**
** 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 <assert.h>
#include <string.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.h"
#include "pbm.h"
#include "bitreverse.h"



#define MAX_LINE 500

static unsigned int hexTable[256];
    /* Hexadecimal ASCII translation table.  Constant */

static void
initHexTable(void) {

    unsigned int i;

    for (i = 0; i < 256; ++i)
        hexTable[i] = 256;

    hexTable['0'] =  0;
    hexTable['1'] =  1;
    hexTable['2'] =  2;
    hexTable['3'] =  3;
    hexTable['4'] =  4;
    hexTable['5'] =  5;
    hexTable['6'] =  6;
    hexTable['7'] =  7;
    hexTable['8'] =  8;
    hexTable['9'] =  9;
    hexTable['A'] = 10;
    hexTable['B'] = 11;
    hexTable['C'] = 12;
    hexTable['D'] = 13;
    hexTable['E'] = 14;
    hexTable['F'] = 15;
    hexTable['a'] = 10;
    hexTable['b'] = 11;
    hexTable['c'] = 12;
    hexTable['d'] = 13;
    hexTable['e'] = 14;
    hexTable['f'] = 15;
}



static void
parseWidthHeightLine(const char *   const line,
                     bool *         const gotWidthP,
                     unsigned int * const widthP,
                     bool *         const gotHeightP,
                     unsigned int * const heightP) {

    int rc;
    char nameAndType[MAX_LINE];
    unsigned int value;

    rc = sscanf(line, "#define %s %u", nameAndType, &value);
    if (rc == 2) {
        const char * underscorePos = strrchr(nameAndType, '_');
        const char * type;
        if (underscorePos)
            type = underscorePos + 1;
        else
            type = nameAndType;
        if (STREQ(type, "width")) {
            *gotWidthP = TRUE;
            *widthP = value;
        } else if (STREQ(type, "height")) {
            *gotHeightP = TRUE;
            *heightP = value;
        }
    }
}



static void
parseDeclaration(const char * const line,
                 bool *       const isDeclarationP,
                 bool *       const version10P) {
/*----------------------------------------------------------------------------
   Parse the XBM file line 'line' as the first line of the data structure
   declaration, i.e. the one that looks like this:

      static unsigned char myImage = {

   Return as *isDeclarationP whether the line actually is such a line,
   and if so, return as nameAndType what the variable name ('myImage'
   in the example) is and as *version10P whether it's of the type used
   by X10 as opposed to X11.
-----------------------------------------------------------------------------*/
    char nameAndType[MAX_LINE];
    int rc;
        
    rc = sscanf(line, "static short %s = {", nameAndType);
    if (rc == 1) {
        *version10P     = TRUE;
        *isDeclarationP = TRUE;
    } else {
        int rc;
        rc = sscanf(line, "static char %s = {", nameAndType);
        if (rc == 1) {
            *version10P     = FALSE;
            *isDeclarationP = TRUE;
        } else {
            int rc;
            rc = sscanf(line, "static unsigned char %s = {", nameAndType);
            if (rc == 1) {
                *version10P     = FALSE;
                *isDeclarationP = TRUE;
            } else
                *isDeclarationP = FALSE;
        }
    }
}



static void
getXbmHeader(FILE *         const ifP,
             unsigned int * const widthP,
             unsigned int * const heightP,
             bool *         const version10P) {

    bool foundDeclaration;
        /* In scanning through the bitmap file, we have found the first
           line of the C declaration of the array (the "static char ..."
           or whatever line)
        */
    bool gotWidth, gotHeight;
        /* We found the line in the bitmap file that gives the width
           or height, respectively, of the image (and have set
           *widthP or *heightP to the value in it).
        */

    bool eof;
        /* We've encountered end of file while searching file */

    gotWidth = FALSE;
    gotHeight = FALSE;
    foundDeclaration = FALSE;    /* Haven't found it yet; haven't even looked*/
    eof = FALSE;                 /* Haven't encountered end of file yet */

    while (!foundDeclaration && !eof) {
        char * rc;
        char line[MAX_LINE];

        rc = fgets(line, MAX_LINE, ifP);
        if (rc == NULL)
            eof = TRUE;
        else {
            if (strlen(line) == MAX_LINE - 1)
                pm_error("A line in the input file is %u characters long.  "
                         "%u is the maximum we can handle",
                         strlen(line), MAX_LINE-1);

            parseWidthHeightLine(line, &gotWidth, widthP, &gotHeight, heightP);

            parseDeclaration(line, &foundDeclaration, version10P);
        }
    }

    if (!foundDeclaration) 
        pm_error("Unable to find a line in the file containing the start "
                 "of C array declaration (\"static char\" or whatever)");

    if (!gotWidth)
        pm_error("Unable to find the #define statement that gives the "
                 "width of the image, before the data structure "
                 "declaration.");
    if (!gotHeight)
        pm_error("Unable to find the #define statement that gives the "
                 "height of the image, before the data structure "
                 "declaration.");
}



static void
getHexByte(FILE *         const ifP,
           unsigned int * const valueP) {

    int c1, c2;
    unsigned int value;

    c1 = getc(ifP);
    c2 = getc(ifP);
    if (c1 == EOF || c2 == EOF)
        pm_error("EOF / read error");

    assert(c1 >= 0); assert(c1 < 256);
    assert(c2 >= 0); assert(c2 < 256);
    
    value = (hexTable[c1] << 4) + hexTable[c2];
    if (value >= 256)
        pm_error("Invalid XBM input.  What should be a two digit "
                 "hexadecimal cipher is instead '%c%c'", c1, c2);

    *valueP = value;
}


                     
static void
readX10Raster(FILE *          const ifP,
              unsigned int    const rasterLength,
              unsigned char * const data,
              unsigned int    const bytesPerLine,
              bool            const mustPad) {

    unsigned int bytesDone;
    unsigned char * p;

    for (bytesDone = 0, p = &data[0];
         bytesDone < rasterLength;
         bytesDone += 2) {

        unsigned int value1;
        unsigned int value2;

        while (getc(ifP) != 'x') {}  /* Read up through the 'x' in 0x1234 */

        getHexByte(ifP, &value1);  /* Read first two hex digits */
        getHexByte(ifP, &value2);  /* Read last two hex digits */

        *p++ = value2;

        if (!mustPad || ((bytesDone + 2) % bytesPerLine))
            *p++ = value1;
    }
}



static void
readX11Raster(FILE * const ifP,
              unsigned int const rasterLength,
              unsigned char * data) {

    unsigned int i;

    for (i = 0; i < rasterLength; ++i) {
        unsigned int value;
        int c;

        /* Read up through the 'x' in 0x12 */
        while ((c = getc(ifP))) {
            if (c == EOF)
                pm_error("EOF where 0x expected");
            else if (toupper(c) == 'X')
                break;
        }

        getHexByte(ifP, &value);  /* Read the two hex digits */

        assert(value < 256);

        data[i] = value;
    }
}



static void
readBitmapFile(FILE *           const ifP,
               unsigned int *   const widthP,
               unsigned int *   const heightP,
               unsigned char ** const dataP) {

    bool version10;
    unsigned int rasterLength;
    unsigned int width, height;
    unsigned char * data;

    unsigned int bytesPerLine;
    bool mustPad;

    getXbmHeader(ifP, &width, &height, &version10);

    *widthP = width;
    *heightP = height;

    mustPad = (width % 16 >= 1 && width % 16 <= 8 && version10);

    bytesPerLine = (width + 7) / 8 + (mustPad ? 1 : 0);
    
    rasterLength = bytesPerLine * height;

    MALLOCARRAY(data, rasterLength);
    if (data == NULL)
        pm_error("Unable to allocate memory for the %u-byte raster",
                 rasterLength);

    if (version10)
        readX10Raster(ifP, rasterLength, data, bytesPerLine, mustPad);
    else
        readX11Raster(ifP, rasterLength, data);

    *dataP = data;
}



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

    FILE * ifP;
    bit * bitrow;
    unsigned int rows, cols;
    unsigned int row;
    unsigned char * data;
    const char * inputFileName;
    unsigned char * p;
        /* Cursor in raster data data[] */
    
    initHexTable();

    pbm_init(&argc, argv);

    if (argc-1 > 1)
        pm_error("The only possible argument is the input file name.  "
                 "You specified %u arguments", argc-1);
    
    if (argc-1 > 0)
        inputFileName = argv[1];
    else
        inputFileName = "-";

    ifP = pm_openr(inputFileName);

    readBitmapFile(ifP, &cols, &rows, &data);

    pm_close(ifP);

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

    p = &data[0];  /* Start at beginning of raster */

    for (row = 0; row < rows; ++row) {
        unsigned int const bytesPerRow = pbm_packed_bytes(cols);
        unsigned int i;
        
        for (i = 0; i < bytesPerRow; ++i)
            bitrow[i] = bitreverse[*p++];
            
        if (cols % 8 > 0) {
            bitrow[bytesPerRow] >>= 8 - cols % 8;
            bitrow[bytesPerRow] <<= 8 - cols % 8;
        }
            
        pbm_writepbmrow_packed(stdout, bitrow, cols, 0);
    }

    free(data);
    pm_close(stdout);

    return 0;
}

/*  CHANGE HISTORY:

  99.09.08 bryanh    Recognize "static unsigned char" declaration.

  06.10 (afu)
   Changed bitrow from plain to raw, write function from pbm_writepbmrow()
   to pbm_writepbmrow_packed().
   Retired bitwise transformation functions.

*/