| 1 |
/* xbmtopbm.c - read an X bitmap file and produce a PBM image |
| 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 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include "pm_c_util.h" |
| 18 |
#include "mallocvar.h" |
| 19 |
#include "nstring.h" |
| 20 |
#include "pbm.h" |
| 21 |
#include "bitreverse.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#define MAX_LINE 500 |
| 26 |
|
| 27 |
static unsigned int hexTable[256]; |
| 28 |
/* Hexadecimal ASCII translation table. Constant */ |
| 29 |
|
| 30 |
static void |
| 31 |
initHexTable(void) { |
| 32 |
|
| 33 |
unsigned int i; |
| 34 |
|
| 35 |
for (i = 0; i < 256; ++i) |
| 36 |
hexTable[i] = 256; |
| 37 |
|
| 38 |
hexTable['0'] = 0; |
| 39 |
hexTable['1'] = 1; |
| 40 |
hexTable['2'] = 2; |
| 41 |
hexTable['3'] = 3; |
| 42 |
hexTable['4'] = 4; |
| 43 |
hexTable['5'] = 5; |
| 44 |
hexTable['6'] = 6; |
| 45 |
hexTable['7'] = 7; |
| 46 |
hexTable['8'] = 8; |
| 47 |
hexTable['9'] = 9; |
| 48 |
hexTable['A'] = 10; |
| 49 |
hexTable['B'] = 11; |
| 50 |
hexTable['C'] = 12; |
| 51 |
hexTable['D'] = 13; |
| 52 |
hexTable['E'] = 14; |
| 53 |
hexTable['F'] = 15; |
| 54 |
hexTable['a'] = 10; |
| 55 |
hexTable['b'] = 11; |
| 56 |
hexTable['c'] = 12; |
| 57 |
hexTable['d'] = 13; |
| 58 |
hexTable['e'] = 14; |
| 59 |
hexTable['f'] = 15; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static void |
| 65 |
parseWidthHeightLine(const char * const line, |
| 66 |
bool * const gotWidthP, |
| 67 |
unsigned int * const widthP, |
| 68 |
bool * const gotHeightP, |
| 69 |
unsigned int * const heightP) { |
| 70 |
|
| 71 |
int rc; |
| 72 |
char nameAndType[MAX_LINE]; |
| 73 |
unsigned int value; |
| 74 |
|
| 75 |
rc = sscanf(line, "#define %s %u", nameAndType, &value); |
| 76 |
if (rc == 2) { |
| 77 |
const char * underscorePos = strrchr(nameAndType, '_'); |
| 78 |
const char * type; |
| 79 |
if (underscorePos) |
| 80 |
type = underscorePos + 1; |
| 81 |
else |
| 82 |
type = nameAndType; |
| 83 |
if (streq(type, "width")) { |
| 84 |
*gotWidthP = TRUE; |
| 85 |
*widthP = value; |
| 86 |
} else if (streq(type, "height")) { |
| 87 |
*gotHeightP = TRUE; |
| 88 |
*heightP = value; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static void |
| 96 |
parseDeclaration(const char * const line, |
| 97 |
bool * const isDeclarationP, |
| 98 |
bool * const version10P) { |
| 99 |
/*---------------------------------------------------------------------------- |
| 100 |
Parse the XBM file line 'line' as the first line of the data structure |
| 101 |
declaration, i.e. the one that looks like this: |
| 102 |
|
| 103 |
static unsigned char myImage = { |
| 104 |
|
| 105 |
Return as *isDeclarationP whether the line actually is such a line, |
| 106 |
and if so, return as nameAndType what the variable name ('myImage' |
| 107 |
in the example) is and as *version10P whether it's of the type used |
| 108 |
by X10 as opposed to X11. |
| 109 |
-----------------------------------------------------------------------------*/ |
| 110 |
char nameAndType[MAX_LINE]; |
| 111 |
int rc; |
| 112 |
|
| 113 |
rc = sscanf(line, "static short %s = {", nameAndType); |
| 114 |
if (rc == 1) { |
| 115 |
*version10P = TRUE; |
| 116 |
*isDeclarationP = TRUE; |
| 117 |
} else { |
| 118 |
int rc; |
| 119 |
rc = sscanf(line, "static unsigned short %s = {", nameAndType); |
| 120 |
if (rc == 1) { |
| 121 |
/* This is apparently not legal X10 XBM; we recognize it as an |
| 122 |
extension. Many non-Netpbm programs won't. |
| 123 |
*/ |
| 124 |
*version10P = TRUE; |
| 125 |
*isDeclarationP = TRUE; |
| 126 |
} else { |
| 127 |
int rc; |
| 128 |
rc = sscanf(line, "static char %s = {", nameAndType); |
| 129 |
if (rc == 1) { |
| 130 |
*version10P = FALSE; |
| 131 |
*isDeclarationP = TRUE; |
| 132 |
} else { |
| 133 |
int rc; |
| 134 |
rc = sscanf(line, "static unsigned char %s = {", nameAndType); |
| 135 |
if (rc == 1) { |
| 136 |
*version10P = FALSE; |
| 137 |
*isDeclarationP = TRUE; |
| 138 |
} else |
| 139 |
*isDeclarationP = FALSE; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static void |
| 148 |
getXbmHeader(FILE * const ifP, |
| 149 |
unsigned int * const widthP, |
| 150 |
unsigned int * const heightP, |
| 151 |
bool * const version10P) { |
| 152 |
|
| 153 |
bool foundDeclaration; |
| 154 |
/* In scanning through the bitmap file, we have found the first |
| 155 |
line of the C declaration of the array (the "static char ..." |
| 156 |
or whatever line) |
| 157 |
*/ |
| 158 |
bool gotWidth, gotHeight; |
| 159 |
/* We found the line in the bitmap file that gives the width |
| 160 |
or height, respectively, of the image (and have set |
| 161 |
*widthP or *heightP to the value in it). |
| 162 |
*/ |
| 163 |
|
| 164 |
bool eof; |
| 165 |
/* We've encountered end of file while searching file */ |
| 166 |
|
| 167 |
gotWidth = FALSE; |
| 168 |
gotHeight = FALSE; |
| 169 |
foundDeclaration = FALSE; /* Haven't found it yet; haven't even looked*/ |
| 170 |
eof = FALSE; /* Haven't encountered end of file yet */ |
| 171 |
|
| 172 |
while (!foundDeclaration && !eof) { |
| 173 |
char * rc; |
| 174 |
char line[MAX_LINE]; |
| 175 |
|
| 176 |
rc = fgets(line, MAX_LINE, ifP); |
| 177 |
if (rc == NULL) |
| 178 |
eof = TRUE; |
| 179 |
else { |
| 180 |
if (strlen(line) == MAX_LINE - 1) |
| 181 |
pm_error("A line in the input file is %u characters long. " |
| 182 |
"%u is the maximum we can handle", |
| 183 |
(unsigned)strlen(line), MAX_LINE-1); |
| 184 |
|
| 185 |
parseWidthHeightLine(line, &gotWidth, widthP, &gotHeight, heightP); |
| 186 |
|
| 187 |
parseDeclaration(line, &foundDeclaration, version10P); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if (!foundDeclaration) |
| 192 |
pm_error("Unable to find a line in the file containing the start " |
| 193 |
"of C array declaration (\"static char\" or whatever)"); |
| 194 |
|
| 195 |
if (!gotWidth) |
| 196 |
pm_error("Unable to find the #define statement that gives the " |
| 197 |
"width of the image, before the data structure " |
| 198 |
"declaration."); |
| 199 |
if (!gotHeight) |
| 200 |
pm_error("Unable to find the #define statement that gives the " |
| 201 |
"height of the image, before the data structure " |
| 202 |
"declaration."); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
static void |
| 208 |
getHexByte(FILE * const ifP, |
| 209 |
unsigned int * const valueP) { |
| 210 |
|
| 211 |
int c1, c2; |
| 212 |
unsigned int value; |
| 213 |
|
| 214 |
c1 = getc(ifP); |
| 215 |
c2 = getc(ifP); |
| 216 |
if (c1 == EOF || c2 == EOF) |
| 217 |
pm_error("EOF / read error"); |
| 218 |
|
| 219 |
assert(c1 >= 0); assert(c1 < 256); |
| 220 |
assert(c2 >= 0); assert(c2 < 256); |
| 221 |
|
| 222 |
value = (hexTable[c1] << 4) + hexTable[c2]; |
| 223 |
if (value >= 256) |
| 224 |
pm_error("Invalid XBM input. What should be a two digit " |
| 225 |
"hexadecimal cipher is instead '%c%c'", c1, c2); |
| 226 |
|
| 227 |
*valueP = value; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
static void |
| 233 |
readX10Raster(FILE * const ifP, |
| 234 |
unsigned int const rasterLength, |
| 235 |
unsigned char * const data, |
| 236 |
unsigned int const bytesPerLine, |
| 237 |
bool const mustPad) { |
| 238 |
|
| 239 |
unsigned int bytesDone; |
| 240 |
unsigned char * p; |
| 241 |
|
| 242 |
for (bytesDone = 0, p = &data[0]; |
| 243 |
bytesDone < rasterLength; |
| 244 |
bytesDone += 2) { |
| 245 |
|
| 246 |
unsigned int value1; |
| 247 |
unsigned int value2; |
| 248 |
|
| 249 |
while (getc(ifP) != 'x') {} /* Read up through the 'x' in 0x1234 */ |
| 250 |
|
| 251 |
getHexByte(ifP, &value1); /* Read first two hex digits */ |
| 252 |
getHexByte(ifP, &value2); /* Read last two hex digits */ |
| 253 |
|
| 254 |
*p++ = value2; |
| 255 |
|
| 256 |
if (!mustPad || ((bytesDone + 2) % bytesPerLine)) |
| 257 |
*p++ = value1; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
static void |
| 264 |
readX11Raster(FILE * const ifP, |
| 265 |
unsigned int const rasterLength, |
| 266 |
unsigned char * data) { |
| 267 |
|
| 268 |
unsigned int i; |
| 269 |
|
| 270 |
for (i = 0; i < rasterLength; ++i) { |
| 271 |
unsigned int value; |
| 272 |
int c; |
| 273 |
|
| 274 |
/* Read up through the 'x' in 0x12 */ |
| 275 |
while ((c = getc(ifP))) { |
| 276 |
if (c == EOF) |
| 277 |
pm_error("EOF where 0x expected"); |
| 278 |
else if (toupper(c) == 'X') |
| 279 |
break; |
| 280 |
} |
| 281 |
|
| 282 |
getHexByte(ifP, &value); /* Read the two hex digits */ |
| 283 |
|
| 284 |
assert(value < 256); |
| 285 |
|
| 286 |
data[i] = value; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
static void |
| 293 |
readBitmapFile(FILE * const ifP, |
| 294 |
unsigned int * const widthP, |
| 295 |
unsigned int * const heightP, |
| 296 |
unsigned char ** const dataP) { |
| 297 |
|
| 298 |
bool version10; |
| 299 |
unsigned int rasterLength; |
| 300 |
unsigned int width, height; |
| 301 |
unsigned char * data; |
| 302 |
|
| 303 |
unsigned int bytesPerLine; |
| 304 |
bool mustPad; |
| 305 |
|
| 306 |
getXbmHeader(ifP, &width, &height, &version10); |
| 307 |
|
| 308 |
*widthP = width; |
| 309 |
*heightP = height; |
| 310 |
|
| 311 |
mustPad = (width % 16 >= 1 && width % 16 <= 8 && version10); |
| 312 |
|
| 313 |
bytesPerLine = (width + 7) / 8 + (mustPad ? 1 : 0); |
| 314 |
|
| 315 |
rasterLength = bytesPerLine * height; |
| 316 |
|
| 317 |
MALLOCARRAY(data, rasterLength); |
| 318 |
if (data == NULL) |
| 319 |
pm_error("Unable to allocate memory for the %u-byte raster", |
| 320 |
rasterLength); |
| 321 |
|
| 322 |
if (version10) |
| 323 |
readX10Raster(ifP, rasterLength, data, bytesPerLine, mustPad); |
| 324 |
else |
| 325 |
readX11Raster(ifP, rasterLength, data); |
| 326 |
|
| 327 |
*dataP = data; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
int |
| 333 |
main(int argc, |
| 334 |
char * argv[]) { |
| 335 |
|
| 336 |
FILE * ifP; |
| 337 |
bit * bitrow; |
| 338 |
unsigned int rows, cols; |
| 339 |
unsigned int row; |
| 340 |
unsigned char * data; |
| 341 |
const char * inputFileName; |
| 342 |
unsigned char * p; |
| 343 |
/* Cursor in raster data data[] */ |
| 344 |
|
| 345 |
initHexTable(); |
| 346 |
|
| 347 |
pbm_init(&argc, argv); |
| 348 |
|
| 349 |
if (argc-1 > 1) |
| 350 |
pm_error("The only possible argument is the input file name. " |
| 351 |
"You specified %u arguments", argc-1); |
| 352 |
|
| 353 |
if (argc-1 > 0) |
| 354 |
inputFileName = argv[1]; |
| 355 |
else |
| 356 |
inputFileName = "-"; |
| 357 |
|
| 358 |
ifP = pm_openr(inputFileName); |
| 359 |
|
| 360 |
readBitmapFile(ifP, &cols, &rows, &data); |
| 361 |
|
| 362 |
pm_close(ifP); |
| 363 |
|
| 364 |
pbm_writepbminit(stdout, cols, rows, 0); |
| 365 |
bitrow = pbm_allocrow_packed(cols); |
| 366 |
|
| 367 |
p = &data[0]; /* Start at beginning of raster */ |
| 368 |
|
| 369 |
for (row = 0; row < rows; ++row) { |
| 370 |
unsigned int const bytesPerRow = pbm_packed_bytes(cols); |
| 371 |
unsigned int i; |
| 372 |
|
| 373 |
for (i = 0; i < bytesPerRow; ++i) |
| 374 |
bitrow[i] = bitreverse[*p++]; |
| 375 |
|
| 376 |
pbm_cleanrowend_packed(bitrow, cols); |
| 377 |
pbm_writepbmrow_packed(stdout, bitrow, cols, 0); |
| 378 |
} |
| 379 |
|
| 380 |
pbm_freerow(bitrow); |
| 381 |
free(data); |
| 382 |
pm_close(stdout); |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
/* CHANGE HISTORY: |
| 390 |
|
| 391 |
99.09.08 bryanh Recognize "static unsigned char" declaration. |
| 392 |
|
| 393 |
06.10 (afu) |
| 394 |
Changed bitrow from plain to raw, write function from pbm_writepbmrow() |
| 395 |
to pbm_writepbmrow_packed(). |
| 396 |
Retired bitwise transformation functions. |
| 397 |
|
| 398 |
*/ |
| 399 |
|