| 1 |
/***************************************************************************** |
| 2 |
bmptopnm.c |
| 3 |
****************************************************************************** |
| 4 |
|
| 5 |
Bmptopnm - Converts from a Microsoft Windows or OS/2 .BMP file to a |
| 6 |
PBM, PGM, or PPM file. |
| 7 |
|
| 8 |
This program was formerly called Bmptoppm (and generated only PPM output). |
| 9 |
The name was changed in March 2002. |
| 10 |
|
| 11 |
Copyright (C) 1992 by David W. Sanderson. |
| 12 |
|
| 13 |
Permission to use, copy, modify, and distribute this software and its |
| 14 |
documentation for any purpose and without fee is hereby granted, |
| 15 |
provided that the above copyright notice appear in all copies and |
| 16 |
that both that copyright notice and this permission notice appear |
| 17 |
in supporting documentation. This software is provided "as is" |
| 18 |
without express or implied warranty. |
| 19 |
|
| 20 |
Note: From mid-2003 to mid-2007, this program would crash on any 16 |
| 21 |
bit BMP without transparency and no one reported it. Before that, it |
| 22 |
refused to even try to read a 16 bit BMP. I conclude that essentially |
| 23 |
nobody is using 16 bit BMP. |
| 24 |
|
| 25 |
*****************************************************************************/ |
| 26 |
#include <string.h> |
| 27 |
#include <limits.h> |
| 28 |
#include <assert.h> |
| 29 |
|
| 30 |
#include "pm_c_util.h" |
| 31 |
#include "mallocvar.h" |
| 32 |
#include "shhopt.h" |
| 33 |
#include "nstring.h" |
| 34 |
#include "pnm.h" |
| 35 |
#include "bmp.h" |
| 36 |
|
| 37 |
static xelval const bmpMaxval = 255; |
| 38 |
/* The maxval for intensity values in a BMP image -- either in a |
| 39 |
truecolor raster or in a colormap |
| 40 |
*/ |
| 41 |
|
| 42 |
enum rowOrder {BOTTOMUP, TOPDOWN}; |
| 43 |
|
| 44 |
struct bitPosition { |
| 45 |
/* mask and shift count to describe a set of bits in a binary value. |
| 46 |
|
| 47 |
Example: if 16 bits are laid out as XRRRRRGGGGGBBBBB then the shift |
| 48 |
count for the R component is 10 and the mask is 0000000000011111. |
| 49 |
|
| 50 |
A 'mask' of zero denotes absence of any bits; e.g. in the example |
| 51 |
above, the mask for the transparency component is zero because there |
| 52 |
is no transparency component . 'shift' is arbitrary in that case. |
| 53 |
*/ |
| 54 |
unsigned int shift; |
| 55 |
/* How many bits right you have to shift the value to get the subject |
| 56 |
bits in the least significant bit positions. |
| 57 |
*/ |
| 58 |
unsigned int mask; |
| 59 |
/* Has one bits in positions where the subject bits are after |
| 60 |
shifting. |
| 61 |
*/ |
| 62 |
}; |
| 63 |
|
| 64 |
struct pixelformat { |
| 65 |
/* The format of a pixel representation from the raster. i.e. which |
| 66 |
bits apply to red, green, blue, and transparency |
| 67 |
*/ |
| 68 |
struct bitPosition red; |
| 69 |
struct bitPosition blu; |
| 70 |
struct bitPosition grn; |
| 71 |
struct bitPosition trn; |
| 72 |
|
| 73 |
bool conventionalBgr; |
| 74 |
/* This means that the above bit positions are just the conventional |
| 75 |
BGR format -- one byte Blue, one byte Green, one byte Red, |
| 76 |
no alpha. Though it's totally redundant with the members above, |
| 77 |
this member speeds up computation: We've never actually seen |
| 78 |
a BMP file that doesn't use conventional BGR, and it doesn't |
| 79 |
require any masking or shifting at all to interpret. |
| 80 |
*/ |
| 81 |
}; |
| 82 |
|
| 83 |
typedef struct { |
| 84 |
/* These are all encodings of floating point */ |
| 85 |
unsigned long x; |
| 86 |
unsigned long y; |
| 87 |
unsigned long z; |
| 88 |
} cieXyz; |
| 89 |
|
| 90 |
typedef struct { |
| 91 |
cieXyz red; |
| 92 |
cieXyz grn; |
| 93 |
cieXyz blu; |
| 94 |
} cieXyzTriple; |
| 95 |
|
| 96 |
struct bmpInfoHeader { |
| 97 |
enum rowOrder rowOrder; |
| 98 |
unsigned int cols; |
| 99 |
unsigned int rows; |
| 100 |
unsigned int cBitCount; |
| 101 |
/* Number of bits in the BMP file that each pixel occupies. */ |
| 102 |
enum bmpClass class; |
| 103 |
bool bitFields; |
| 104 |
/* The raster values are arranged in arbitrary bit fields as |
| 105 |
described by the "mask" values in the header, rather than |
| 106 |
fixed formats. |
| 107 |
*/ |
| 108 |
unsigned int cmapSize; |
| 109 |
/* Size in bytes of the colormap (palette) in the BMP file. |
| 110 |
|
| 111 |
Zero means there is no colormap. |
| 112 |
*/ |
| 113 |
unsigned int imageSize; |
| 114 |
/* Size in bytes of the image data. We only reference this |
| 115 |
when the image is compressed. */ |
| 116 |
unsigned short cPlanes; |
| 117 |
BMPCompType compression; |
| 118 |
struct pixelformat pixelformat; |
| 119 |
cieXyzTriple endPoints; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
struct cmdlineInfo { |
| 125 |
/* All the information the user supplied in the command line, |
| 126 |
in a form easy for the program to use. |
| 127 |
*/ |
| 128 |
const char * inputFileName; |
| 129 |
unsigned int verbose; |
| 130 |
}; |
| 131 |
|
| 132 |
static const char * ifname; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static void |
| 137 |
parseCommandLine(int argc, const char ** argv, |
| 138 |
struct cmdlineInfo * const cmdlineP) { |
| 139 |
/*---------------------------------------------------------------------------- |
| 140 |
Note that the file spec array we return is stored in the storage that |
| 141 |
was passed to us as the argv array. |
| 142 |
-----------------------------------------------------------------------------*/ |
| 143 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 144 |
optStruct3 opt; |
| 145 |
|
| 146 |
unsigned int option_def_index; |
| 147 |
|
| 148 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 149 |
|
| 150 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 151 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); |
| 152 |
|
| 153 |
opt.opt_table = option_def; |
| 154 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 155 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 156 |
|
| 157 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 158 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 159 |
|
| 160 |
if (argc-1 == 0) |
| 161 |
cmdlineP->inputFileName = "-"; |
| 162 |
else if (argc-1 != 1) |
| 163 |
pm_error("Program takes zero or one argument (filename). You " |
| 164 |
"specified %d", argc-1); |
| 165 |
else |
| 166 |
cmdlineP->inputFileName = argv[1]; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static const char er_read[] = "%s: read error"; |
| 172 |
|
| 173 |
static int |
| 174 |
GetByte(FILE * const fp) { |
| 175 |
|
| 176 |
int v; |
| 177 |
|
| 178 |
if ((v = getc(fp)) == EOF) |
| 179 |
pm_error(er_read, ifname); |
| 180 |
|
| 181 |
return v; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static short |
| 187 |
GetShort(FILE * const fp) { |
| 188 |
|
| 189 |
short v; |
| 190 |
|
| 191 |
if (pm_readlittleshort(fp, &v) == -1) |
| 192 |
pm_error(er_read, ifname); |
| 193 |
|
| 194 |
return v; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
static short |
| 200 |
GetBigShort(FILE * const fp) { |
| 201 |
|
| 202 |
short v; |
| 203 |
|
| 204 |
if (pm_readbigshort(fp, &v) == -1) |
| 205 |
pm_error(er_read, ifname); |
| 206 |
|
| 207 |
return v; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static long |
| 213 |
GetLong(FILE * const fp) { |
| 214 |
|
| 215 |
long v; |
| 216 |
|
| 217 |
if (pm_readlittlelong(fp, &v) == -1) |
| 218 |
pm_error(er_read, ifname); |
| 219 |
|
| 220 |
return v; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
static cieXyz |
| 226 |
GetCieXyz(FILE * const ifP) { |
| 227 |
|
| 228 |
cieXyz retval; |
| 229 |
|
| 230 |
retval.x = GetLong(ifP); |
| 231 |
retval.y = GetLong(ifP); |
| 232 |
retval.z = GetLong(ifP); |
| 233 |
|
| 234 |
return retval; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static cieXyzTriple |
| 240 |
GetCieXyzTriple(FILE * const ifP) { |
| 241 |
|
| 242 |
cieXyzTriple retval; |
| 243 |
|
| 244 |
retval.red = GetCieXyz(ifP); |
| 245 |
retval.grn = GetCieXyz(ifP); |
| 246 |
retval.blu = GetCieXyz(ifP); |
| 247 |
|
| 248 |
return retval; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static struct pixelformat |
| 254 |
defaultPixelformat(unsigned int const bitCount) { |
| 255 |
|
| 256 |
struct pixelformat retval; |
| 257 |
|
| 258 |
switch (bitCount) { |
| 259 |
case 16: |
| 260 |
/* This layout is sometimes called "RGB555". A document from |
| 261 |
Microsoft says this is the default (when the "compression" |
| 262 |
field of the header says COMP_BITFIELDS). |
| 263 |
*/ |
| 264 |
retval.conventionalBgr = FALSE; |
| 265 |
retval.red.shift = 10; |
| 266 |
retval.grn.shift = 5; |
| 267 |
retval.blu.shift = 0; |
| 268 |
retval.trn.shift = 0; |
| 269 |
retval.red.mask = 0x1f; /* 5 bits */ |
| 270 |
retval.grn.mask = 0x1f; /* 5 bits */ |
| 271 |
retval.blu.mask = 0x1f; /* 5 bits */ |
| 272 |
retval.trn.mask = 0; |
| 273 |
break; |
| 274 |
case 24: |
| 275 |
case 32: |
| 276 |
retval.conventionalBgr = TRUE; |
| 277 |
retval.red.shift = 16; |
| 278 |
retval.grn.shift = 8; |
| 279 |
retval.blu.shift = 0; |
| 280 |
retval.trn.shift = 0; |
| 281 |
retval.red.mask = 0xff; /* 8 bits */ |
| 282 |
retval.grn.mask = 0xff; /* 8 bits */ |
| 283 |
retval.blu.mask = 0xff; /* 8 bits */ |
| 284 |
retval.trn.mask = 0; |
| 285 |
break; |
| 286 |
default: |
| 287 |
/* colormapped - masks are undefined */ |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
return retval; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
static void |
| 297 |
readOffBytes(FILE * const fp, unsigned int const nbytes) { |
| 298 |
/*---------------------------------------------------------------------------- |
| 299 |
Read 'nbytes' from file 'fp'. Abort program if read error. |
| 300 |
-----------------------------------------------------------------------------*/ |
| 301 |
int i; |
| 302 |
|
| 303 |
for(i = 0; i < nbytes; ++i) { |
| 304 |
int rc; |
| 305 |
rc = getc(fp); |
| 306 |
if (rc == EOF) |
| 307 |
pm_error(er_read, ifname); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
static void |
| 314 |
bmpReadfileheader(FILE * const ifP, |
| 315 |
unsigned int * const bytesReadP, |
| 316 |
unsigned int * const offBitsP) { |
| 317 |
|
| 318 |
unsigned long offBits; |
| 319 |
|
| 320 |
if (GetByte(ifP) != 'B') |
| 321 |
pm_error("'%s' is not a BMP file. (It doesn't start with 'BM')", |
| 322 |
ifname); |
| 323 |
if (GetByte(ifP) != 'M') |
| 324 |
pm_error("'%s' is not a BMP file. (It doesn't start with 'BM')", |
| 325 |
ifname); |
| 326 |
|
| 327 |
|
| 328 |
/* fileSize = */ GetLong(ifP); /* This is not always reliable. */ |
| 329 |
/* xHotSpot = */ GetShort(ifP); |
| 330 |
/* yHotSpot = */ GetShort(ifP); |
| 331 |
offBits = GetLong(ifP); |
| 332 |
|
| 333 |
*offBitsP = offBits; |
| 334 |
|
| 335 |
assert(BMPlenfileheader() == 14); |
| 336 |
|
| 337 |
*bytesReadP = 14; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static void |
| 343 |
readOs2InfoHeaderRest(FILE * const ifP, |
| 344 |
struct bmpInfoHeader * const headerP) { |
| 345 |
/*---------------------------------------------------------------------------- |
| 346 |
Read the rest of the info header, after its size field, of an OS2 BMP from |
| 347 |
*ifP. |
| 348 |
|
| 349 |
Add the information from it to *headerP, in particular these members: |
| 350 |
|
| 351 |
cols |
| 352 |
rows |
| 353 |
rowOrder |
| 354 |
cPlanes |
| 355 |
cBitCount |
| 356 |
cmapSize |
| 357 |
pixelformat |
| 358 |
compression |
| 359 |
-----------------------------------------------------------------------------*/ |
| 360 |
unsigned short colsField, rowsField; |
| 361 |
unsigned short planesField, bitCountField; |
| 362 |
|
| 363 |
pm_readlittleshortu(ifP, &colsField); |
| 364 |
if (colsField == 0) |
| 365 |
pm_error("Invalid BMP file: says width is zero"); |
| 366 |
else |
| 367 |
headerP->cols = colsField; |
| 368 |
|
| 369 |
pm_readlittleshortu(ifP, &rowsField); |
| 370 |
if (rowsField == 0) |
| 371 |
pm_error("Invalid BMP file: says height is zero"); |
| 372 |
else |
| 373 |
headerP->rows = rowsField; |
| 374 |
|
| 375 |
headerP->rowOrder = BOTTOMUP; |
| 376 |
pm_readlittleshortu(ifP, &planesField); |
| 377 |
headerP->cPlanes = planesField; |
| 378 |
pm_readlittleshortu(ifP, &bitCountField); |
| 379 |
headerP->cBitCount = bitCountField; |
| 380 |
/* I actually don't know if the OS/2 BMP format allows |
| 381 |
cBitCount > 8 or if it does, what it means, but ppmtobmp |
| 382 |
creates such BMPs, more or less as a byproduct of creating |
| 383 |
the same for Windows BMP, so we interpret cBitCount > 8 the |
| 384 |
same as for Windows. |
| 385 |
*/ |
| 386 |
if (headerP->cBitCount <= 8) |
| 387 |
headerP->cmapSize = 1 << headerP->cBitCount; |
| 388 |
else if (headerP->cBitCount == 24) |
| 389 |
headerP->cmapSize = 0; |
| 390 |
/* There is a 16 bit truecolor format, but we don't know how the |
| 391 |
bits are divided among red, green, and blue, so we can't handle it. |
| 392 |
*/ |
| 393 |
else |
| 394 |
pm_error("Unrecognized bits per pixel in OS/2 BMP file header: %d", |
| 395 |
headerP->cBitCount); |
| 396 |
|
| 397 |
headerP->pixelformat = defaultPixelformat(headerP->cBitCount); |
| 398 |
|
| 399 |
headerP->compression = BMPCOMP_RGB; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
static void |
| 405 |
validateCompression(unsigned long const compression, |
| 406 |
enum rowOrder const rowOrder, |
| 407 |
unsigned int const cBitCount) { |
| 408 |
|
| 409 |
if (compression != BMPCOMP_RGB && compression != BMPCOMP_BITFIELDS && |
| 410 |
compression != BMPCOMP_RLE4 && compression != BMPCOMP_RLE8) |
| 411 |
pm_error("Input has unknown encoding. " |
| 412 |
"Compression type code = %ld. The only ones we know " |
| 413 |
"are RGB (%u), BITFIELDS (%u), " |
| 414 |
"RLE4 (%u), and RLE8 (%u)", |
| 415 |
compression, BMPCOMP_RGB, BMPCOMP_BITFIELDS, |
| 416 |
BMPCOMP_RLE4, BMPCOMP_RLE8); |
| 417 |
|
| 418 |
if ((compression == BMPCOMP_RLE4 || compression == BMPCOMP_RLE8) && |
| 419 |
rowOrder == TOPDOWN ) |
| 420 |
pm_error("Invalid BMP header. Claims image is top-down and also " |
| 421 |
"compressed, which is an impossible combination."); |
| 422 |
|
| 423 |
if ((compression == BMPCOMP_RLE4 && cBitCount !=4 ) || |
| 424 |
(compression == BMPCOMP_RLE8 && cBitCount !=8 )) |
| 425 |
pm_error("Invalid BMP header. " |
| 426 |
"Compression type (%s) disagrees with " |
| 427 |
"number of bits per pixel (%u).", |
| 428 |
compression == BMPCOMP_RLE4 ? "RLE4" : "RLE8", |
| 429 |
cBitCount); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
static void |
| 435 |
readWindowsBasic40ByteInfoHeader(FILE * const ifP, |
| 436 |
struct bmpInfoHeader * const headerP) { |
| 437 |
/*---------------------------------------------------------------------------- |
| 438 |
Read from the file stream 'ifP' the basic BMP Info header. This does |
| 439 |
not include any Info header extension. The Info header is the data |
| 440 |
that comes after the BMP file header. |
| 441 |
|
| 442 |
Return the information from the info header as *headerP. |
| 443 |
-----------------------------------------------------------------------------*/ |
| 444 |
int colorsused; /* ColorsUsed value from header */ |
| 445 |
unsigned short planesField, bitCountField; |
| 446 |
int32_t colsField; |
| 447 |
|
| 448 |
pm_readlittlelong2(ifP, &colsField); |
| 449 |
|
| 450 |
if (colsField == 0) |
| 451 |
pm_error("Invalid BMP file: says width is zero"); |
| 452 |
else if (colsField < 0) |
| 453 |
pm_error("Invalid BMP file: says width is negative (%d)", colsField); |
| 454 |
else |
| 455 |
headerP->cols = (unsigned int)colsField; |
| 456 |
|
| 457 |
{ |
| 458 |
long const cy = GetLong(ifP); |
| 459 |
|
| 460 |
if (cy == 0) |
| 461 |
pm_error("Invalid BMP file: says height is zero"); |
| 462 |
if (cy < 0) { |
| 463 |
headerP->rowOrder = TOPDOWN; |
| 464 |
headerP->rows = - cy; |
| 465 |
} else { |
| 466 |
headerP->rowOrder = BOTTOMUP; |
| 467 |
headerP->rows = cy; |
| 468 |
} |
| 469 |
} |
| 470 |
pm_readlittleshortu(ifP, &planesField); |
| 471 |
headerP->cPlanes = planesField; |
| 472 |
pm_readlittleshortu(ifP, &bitCountField); |
| 473 |
headerP->cBitCount = bitCountField; |
| 474 |
{ |
| 475 |
unsigned long int const compression = GetLong(ifP); |
| 476 |
|
| 477 |
validateCompression(compression, headerP->rowOrder, |
| 478 |
headerP->cBitCount); |
| 479 |
|
| 480 |
headerP->bitFields = (compression == BMPCOMP_BITFIELDS); |
| 481 |
|
| 482 |
headerP->compression = compression; |
| 483 |
} |
| 484 |
/* And read the rest of the junk in the 40 byte header */ |
| 485 |
headerP->imageSize = GetLong(ifP); /* ImageSize */ |
| 486 |
GetLong(ifP); /* XpixelsPerMeter */ |
| 487 |
GetLong(ifP); /* YpixelsPerMeter */ |
| 488 |
colorsused = GetLong(ifP); /* ColorsUsed */ |
| 489 |
/* See comments in bmp.h for info about the definition of the following |
| 490 |
word and its relationship to the color map size (headerP->cmapSize). |
| 491 |
*/ |
| 492 |
/* colorsimportant = */ GetLong(ifP); /* ColorsImportant */ |
| 493 |
|
| 494 |
if (headerP->cBitCount <= 8) { |
| 495 |
if (colorsused != 0) { |
| 496 |
if (colorsused > 1 << headerP->cBitCount) |
| 497 |
pm_error("Invalid BMP header. Says %u bits per pixel, " |
| 498 |
"but %d colors used", |
| 499 |
headerP->cBitCount, colorsused); |
| 500 |
else if (colorsused == 1 && headerP->cBitCount == 1) { |
| 501 |
pm_message("Abnormal BMP header. Says 1 bit per pixel. " |
| 502 |
"Should have 2 colors, but says only 1 color used. " |
| 503 |
); |
| 504 |
headerP->cmapSize = colorsused; |
| 505 |
} |
| 506 |
else |
| 507 |
headerP->cmapSize = colorsused; |
| 508 |
} else |
| 509 |
headerP->cmapSize = 1 << headerP->cBitCount; |
| 510 |
} else if (headerP->cBitCount == 24 || |
| 511 |
headerP->cBitCount == 16 || |
| 512 |
headerP->cBitCount == 32) |
| 513 |
headerP->cmapSize = 0; |
| 514 |
else |
| 515 |
pm_error("Unrecognized bits per pixel in Windows BMP file header: %d", |
| 516 |
headerP->cBitCount); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
static unsigned int |
| 522 |
lsbZeroCount(unsigned int const mask) |
| 523 |
/*---------------------------------------------------------------------------- |
| 524 |
Return the number of consecutive zeroes in the mask 'mask', starting with |
| 525 |
the least significant bit and going up. E.g. for 0x20, it would be 5. |
| 526 |
|
| 527 |
Use GCC built-in when available. |
| 528 |
-----------------------------------------------------------------------------*/ |
| 529 |
|
| 530 |
#if HAVE_GCC_BITCOUNT |
| 531 |
{ |
| 532 |
return ( mask==0 ? sizeof(mask)*8 : __builtin_ctz(mask) ); |
| 533 |
} |
| 534 |
#else |
| 535 |
{ |
| 536 |
unsigned int i=0; |
| 537 |
|
| 538 |
while (((mask >> i) & 0x1) == 0 && i < sizeof(mask)*8) |
| 539 |
++i; |
| 540 |
|
| 541 |
return i; |
| 542 |
|
| 543 |
} |
| 544 |
#endif |
| 545 |
|
| 546 |
|
| 547 |
static struct bitPosition |
| 548 |
bitPositionFromMask(long const bmpMask) { |
| 549 |
struct bitPosition retval; |
| 550 |
|
| 551 |
retval.shift = lsbZeroCount(bmpMask); |
| 552 |
retval.mask = bmpMask >> retval.shift; |
| 553 |
|
| 554 |
return retval; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
static void |
| 560 |
computeConventionalBgr(struct pixelformat * const fP, |
| 561 |
unsigned int const bitCount) { |
| 562 |
|
| 563 |
switch (bitCount) { |
| 564 |
case 24: |
| 565 |
fP->conventionalBgr = |
| 566 |
fP->red.shift == 0 && fP->red.mask == 0xFF && |
| 567 |
fP->grn.shift == 8 && fP->grn.mask == 0xFF && |
| 568 |
fP->blu.shift == 16 && fP->blu.mask == 0xFF && |
| 569 |
fP->trn.mask == 0 |
| 570 |
; |
| 571 |
break; |
| 572 |
case 32: |
| 573 |
fP->conventionalBgr = |
| 574 |
fP->red.shift == 8 && fP->red.mask == 0xFF && |
| 575 |
fP->grn.shift == 16 && fP->grn.mask == 0xFF && |
| 576 |
fP->blu.shift == 24 && fP->blu.mask == 0xFF && |
| 577 |
fP->trn.mask == 0 |
| 578 |
; |
| 579 |
break; |
| 580 |
default: |
| 581 |
fP->conventionalBgr = FALSE; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
static void |
| 588 |
readV4InfoHeaderExtension(FILE * const ifP, |
| 589 |
struct bmpInfoHeader * const headerP, |
| 590 |
unsigned int * const bytesReadP) { |
| 591 |
|
| 592 |
unsigned long redMsk, grnMsk, bluMsk, trnMsk; |
| 593 |
|
| 594 |
redMsk = GetLong(ifP); |
| 595 |
grnMsk = GetLong(ifP); |
| 596 |
bluMsk = GetLong(ifP); |
| 597 |
trnMsk = GetLong(ifP); |
| 598 |
|
| 599 |
if (headerP->bitFields) { |
| 600 |
/* A document from Microsoft says on Windows 95 there is no |
| 601 |
transparency plane and (red, green, blue) must be either |
| 602 |
(5,5,5) or (5,6,5) for 16 bit and (8,8,8) for 32 bit. |
| 603 |
It calls these RGB555, RGB565, RGB888. |
| 604 |
*/ |
| 605 |
headerP->pixelformat.red = bitPositionFromMask(redMsk); |
| 606 |
headerP->pixelformat.grn = bitPositionFromMask(grnMsk); |
| 607 |
headerP->pixelformat.blu = bitPositionFromMask(bluMsk); |
| 608 |
headerP->pixelformat.trn = bitPositionFromMask(trnMsk); |
| 609 |
|
| 610 |
computeConventionalBgr(&headerP->pixelformat, headerP->cBitCount); |
| 611 |
} else |
| 612 |
headerP->pixelformat = defaultPixelformat(headerP->cBitCount); |
| 613 |
|
| 614 |
GetLong(ifP); /* Color space */ |
| 615 |
|
| 616 |
headerP->endPoints = GetCieXyzTriple(ifP); /* 36 bytes */ |
| 617 |
|
| 618 |
GetLong(ifP); /* GammaRed */ |
| 619 |
GetLong(ifP); /* GammaGreen */ |
| 620 |
GetLong(ifP); /* GammaBlue */ |
| 621 |
|
| 622 |
*bytesReadP = 68; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
static void |
| 628 |
readV5InfoHeaderExtension(FILE * const ifP, |
| 629 |
struct bmpInfoHeader * const headerP, |
| 630 |
unsigned int * const bytesReadP) { |
| 631 |
|
| 632 |
GetLong(ifP); /* Intent */ |
| 633 |
GetLong(ifP); /* ProfileData */ |
| 634 |
GetLong(ifP); /* ProfileSize */ |
| 635 |
GetLong(ifP); /* Reserved */ |
| 636 |
|
| 637 |
*bytesReadP = 16; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
static void |
| 643 |
defaultV4InfoHeaderExtension(struct bmpInfoHeader * const headerP) { |
| 644 |
|
| 645 |
headerP->pixelformat = defaultPixelformat(headerP->cBitCount); |
| 646 |
|
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
static void |
| 652 |
readWindowsInfoHeaderRest(FILE * const ifP, |
| 653 |
unsigned int const cInfoHeaderSize, |
| 654 |
struct bmpInfoHeader * const headerP) { |
| 655 |
/*---------------------------------------------------------------------------- |
| 656 |
Read the rest of the info header, after the length field, of a Windows BMP |
| 657 |
from *ifP. |
| 658 |
|
| 659 |
'cInfoHeaderSize' is the size of the info header, not counting its size |
| 660 |
field. Note that besides telling us how much data to read, this also |
| 661 |
implies which of the three major formats the data is in. |
| 662 |
|
| 663 |
Add the information from it to *headerP, in particular these members: |
| 664 |
|
| 665 |
cols |
| 666 |
rows |
| 667 |
rowOrder |
| 668 |
cPlanes |
| 669 |
cBitCount |
| 670 |
bitFields |
| 671 |
compression |
| 672 |
imageSize |
| 673 |
cmapSize |
| 674 |
pixelformat |
| 675 |
endPoints |
| 676 |
-----------------------------------------------------------------------------*/ |
| 677 |
/* There are 3 major formats of Windows |
| 678 |
BMP, identified by the 3 info header lengths. The original |
| 679 |
one is 40 bytes. The "V4 header" is 108 bytes and was |
| 680 |
new with Windows 95 and NT 4.0. The "V5 header" is 124 bytes |
| 681 |
and was new with Windows 98 and Windows 2000. |
| 682 |
*/ |
| 683 |
unsigned int bytesRead; |
| 684 |
|
| 685 |
readWindowsBasic40ByteInfoHeader(ifP, headerP); |
| 686 |
|
| 687 |
bytesRead = 40; |
| 688 |
|
| 689 |
if (cInfoHeaderSize >= BMP_HDRLEN_WIN_V4) { |
| 690 |
unsigned int v4BytesRead; |
| 691 |
readV4InfoHeaderExtension(ifP, headerP, &v4BytesRead); |
| 692 |
bytesRead += v4BytesRead; |
| 693 |
|
| 694 |
assert(bytesRead == BMP_HDRLEN_WIN_V4); |
| 695 |
} else |
| 696 |
defaultV4InfoHeaderExtension(headerP); |
| 697 |
|
| 698 |
if (cInfoHeaderSize >= BMP_HDRLEN_WIN_V5) { |
| 699 |
unsigned int v5BytesRead; |
| 700 |
readV5InfoHeaderExtension(ifP, headerP, &v5BytesRead); |
| 701 |
bytesRead += v5BytesRead; |
| 702 |
assert(bytesRead == BMP_HDRLEN_WIN_V5); |
| 703 |
} |
| 704 |
|
| 705 |
for (; bytesRead < cInfoHeaderSize;) { |
| 706 |
GetByte(ifP); |
| 707 |
++bytesRead; |
| 708 |
} |
| 709 |
|
| 710 |
assert(bytesRead == cInfoHeaderSize); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
static void |
| 716 |
bmpReadinfoheader(FILE * const ifP, |
| 717 |
unsigned int * const bytesReadP, |
| 718 |
struct bmpInfoHeader * const headerP, |
| 719 |
const char ** const errorP) { |
| 720 |
|
| 721 |
unsigned int const cInfoHeaderSize = GetLong(ifP); |
| 722 |
|
| 723 |
const char * error; |
| 724 |
|
| 725 |
BMPdetermineclass(cInfoHeaderSize, &headerP->class, &error); |
| 726 |
|
| 727 |
if (error) { |
| 728 |
pm_asprintf(errorP, "Cannot determine the class of BMP from the " |
| 729 |
"info header size %u. %s", cInfoHeaderSize, error); |
| 730 |
pm_strfree(error); |
| 731 |
} else { |
| 732 |
switch (headerP->class) { |
| 733 |
case BMP_C_WIN_V1: |
| 734 |
case BMP_C_WIN_V2: |
| 735 |
case BMP_C_WIN_V3: |
| 736 |
case BMP_C_WIN_V4: |
| 737 |
case BMP_C_WIN_V5: |
| 738 |
readWindowsInfoHeaderRest(ifP, cInfoHeaderSize, headerP); |
| 739 |
break; |
| 740 |
case BMP_C_OS2_1x: |
| 741 |
case BMP_C_OS2_2x: |
| 742 |
readOs2InfoHeaderRest(ifP, headerP); |
| 743 |
break; |
| 744 |
} |
| 745 |
*errorP = NULL; |
| 746 |
*bytesReadP = cInfoHeaderSize; |
| 747 |
} |
| 748 |
/* Part of our anti-arithmetic overflow strategy is to make sure height |
| 749 |
and width always fit in 16 bits, so they can be multiplied together. |
| 750 |
This shouldn't be a problem, since they come from 16 bit fields in |
| 751 |
the BMP info header. |
| 752 |
*/ |
| 753 |
assert(headerP->cols < (1<<16)); |
| 754 |
assert(headerP->rows < (1<<16)); |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
static void |
| 760 |
bmpReadColormap(FILE * const ifP, |
| 761 |
enum bmpClass const class, |
| 762 |
xel ** const colormapP, |
| 763 |
unsigned int const cmapSize, |
| 764 |
unsigned int * const bytesReadP) { |
| 765 |
/*---------------------------------------------------------------------------- |
| 766 |
Read the color map from the present position in the input BMP file |
| 767 |
*ifP. |
| 768 |
|
| 769 |
The map has 'cmapSize' entries in it. cmapSize == 0 means there is |
| 770 |
no color map. |
| 771 |
|
| 772 |
We return a color map as *colormapP. If there is no color map in the |
| 773 |
BMP, this is just an arbitrary color map. |
| 774 |
|
| 775 |
'class' is the class of BMP image - Windows or OS/2. |
| 776 |
-----------------------------------------------------------------------------*/ |
| 777 |
xel * const colormap = pnm_allocrow(MAX(1, cmapSize)); |
| 778 |
|
| 779 |
unsigned int i; |
| 780 |
unsigned int bytesRead; |
| 781 |
|
| 782 |
for (i = 0, bytesRead = 0; i < cmapSize; ++i) { |
| 783 |
/* There is a document that says the bytes are ordered R,G,B,Z, |
| 784 |
but in practice it appears to be the following instead: |
| 785 |
*/ |
| 786 |
unsigned int const b = GetByte(ifP); |
| 787 |
unsigned int const g = GetByte(ifP); |
| 788 |
unsigned int const r = GetByte(ifP); |
| 789 |
|
| 790 |
unsigned int j; |
| 791 |
|
| 792 |
PNM_ASSIGN(colormap[i], r, g, b); |
| 793 |
|
| 794 |
bytesRead += 3; |
| 795 |
|
| 796 |
for (j = 3; j < BMPlenrgb(class); ++j) { |
| 797 |
GetByte(ifP); |
| 798 |
bytesRead += 1; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
*colormapP = colormap; |
| 803 |
*bytesReadP = bytesRead; |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
static void |
| 809 |
extractBitFields(unsigned int const rasterval, |
| 810 |
struct pixelformat const pixelformat, |
| 811 |
pixval const maxval, |
| 812 |
pixval * const rP, |
| 813 |
pixval * const gP, |
| 814 |
pixval * const bP, |
| 815 |
pixval * const aP) { |
| 816 |
|
| 817 |
unsigned int const rbits = |
| 818 |
(rasterval >> pixelformat.red.shift) & pixelformat.red.mask; |
| 819 |
unsigned int const gbits = |
| 820 |
(rasterval >> pixelformat.grn.shift) & pixelformat.grn.mask; |
| 821 |
unsigned int const bbits = |
| 822 |
(rasterval >> pixelformat.blu.shift) & pixelformat.blu.mask; |
| 823 |
unsigned int const abits = |
| 824 |
(rasterval >> pixelformat.trn.shift) & pixelformat.trn.mask; |
| 825 |
|
| 826 |
*rP = pixelformat.red.mask > 0 ? |
| 827 |
(unsigned int) rbits * maxval / pixelformat.red.mask : 0; |
| 828 |
*gP = pixelformat.grn.mask > 0 ? |
| 829 |
(unsigned int) gbits * maxval / pixelformat.grn.mask : 0; |
| 830 |
*bP = pixelformat.blu.mask > 0 ? |
| 831 |
(unsigned int) bbits * maxval / pixelformat.blu.mask : 0; |
| 832 |
*aP = pixelformat.trn.mask > 0 ? |
| 833 |
(unsigned int) abits * maxval / pixelformat.trn.mask : 0; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
static void |
| 839 |
convertRow16(unsigned char const bmprow[], |
| 840 |
xel xelrow[], |
| 841 |
int const cols, |
| 842 |
struct pixelformat const pixelformat) { |
| 843 |
/* It's truecolor. */ |
| 844 |
|
| 845 |
unsigned int col; |
| 846 |
unsigned int cursor; |
| 847 |
cursor = 0; |
| 848 |
for (col=0; col < cols; ++col) { |
| 849 |
unsigned short const rasterval = (unsigned short) |
| 850 |
bmprow[cursor+1] << 8 | bmprow[cursor+0]; |
| 851 |
|
| 852 |
pixval r, g, b, a; |
| 853 |
|
| 854 |
extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a); |
| 855 |
|
| 856 |
PNM_ASSIGN(xelrow[col], r, g, b); |
| 857 |
|
| 858 |
cursor += 2; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
static void |
| 865 |
convertRow24(unsigned char const bmprow[], |
| 866 |
xel xelrow[], |
| 867 |
int const cols, |
| 868 |
struct pixelformat const pixelformat) { |
| 869 |
|
| 870 |
/* It's truecolor */ |
| 871 |
/* There is a document that gives a much different format for |
| 872 |
24 bit BMPs. But this seems to be the de facto standard, and is, |
| 873 |
with a little ambiguity and contradiction resolved, defined in the |
| 874 |
Microsoft BMP spec. |
| 875 |
*/ |
| 876 |
|
| 877 |
unsigned int col; |
| 878 |
unsigned int cursor; |
| 879 |
|
| 880 |
cursor = 0; |
| 881 |
for (col = 0; col < cols; ++col) { |
| 882 |
pixval r, g, b, a; |
| 883 |
|
| 884 |
if (pixelformat.conventionalBgr) { |
| 885 |
r = bmprow[cursor+2]; |
| 886 |
g = bmprow[cursor+1]; |
| 887 |
b = bmprow[cursor+0]; |
| 888 |
a = 0; |
| 889 |
} else { |
| 890 |
unsigned int const rasterval = |
| 891 |
(bmprow[cursor+0] << 16) + |
| 892 |
(bmprow[cursor+1] << 8) + |
| 893 |
(bmprow[cursor+2] << 0); |
| 894 |
|
| 895 |
extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a); |
| 896 |
} |
| 897 |
PNM_ASSIGN(xelrow[col], r, g, b); |
| 898 |
cursor += 3; |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
static void |
| 905 |
convertRow32(unsigned char const bmprow[], |
| 906 |
xel xelrow[], |
| 907 |
int const cols, |
| 908 |
struct pixelformat const pixelformat) { |
| 909 |
|
| 910 |
/* It's truecolor */ |
| 911 |
|
| 912 |
unsigned int col; |
| 913 |
unsigned int cursor; |
| 914 |
cursor = 0; |
| 915 |
for (col = 0; col < cols; ++col) { |
| 916 |
pixval r, g, b, a; |
| 917 |
|
| 918 |
if (pixelformat.conventionalBgr) { |
| 919 |
/* bmprow[cursor+3] is just padding */ |
| 920 |
r = bmprow[cursor+2]; |
| 921 |
g = bmprow[cursor+1]; |
| 922 |
b = bmprow[cursor+0]; |
| 923 |
a = 0; |
| 924 |
} else { |
| 925 |
unsigned int const rasterval = |
| 926 |
(bmprow[cursor+0] << 24) + |
| 927 |
(bmprow[cursor+1] << 16) + |
| 928 |
(bmprow[cursor+2] << 8) + |
| 929 |
(bmprow[cursor+3] << 0); |
| 930 |
|
| 931 |
extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a); |
| 932 |
} |
| 933 |
|
| 934 |
PNM_ASSIGN(xelrow[col], |
| 935 |
bmprow[cursor+2], bmprow[cursor+1], bmprow[cursor+0]); |
| 936 |
cursor += 4; |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
static void |
| 943 |
validateIndex(unsigned int const index, |
| 944 |
unsigned int const cmapSize ) { |
| 945 |
|
| 946 |
if (index >= cmapSize) |
| 947 |
pm_error("Error: invalid index to color palette."); |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
static void |
| 953 |
convertRow(unsigned char const bmprow[], |
| 954 |
xel xelrow[], |
| 955 |
int const cols, |
| 956 |
unsigned int const cBitCount, |
| 957 |
struct pixelformat const pixelformat, |
| 958 |
xel const colormap[], |
| 959 |
unsigned int const cmapSize |
| 960 |
) { |
| 961 |
/*---------------------------------------------------------------------------- |
| 962 |
Convert a row in raw BMP raster format bmprow[] to a row of xels xelrow[]. |
| 963 |
|
| 964 |
Use maxval 255 for the output xels. |
| 965 |
|
| 966 |
The BMP image has 'cBitCount' bits per pixel. |
| 967 |
|
| 968 |
If the image is colormapped, colormap[] is the colormap |
| 969 |
(colormap[i] is the color with color index i). |
| 970 |
-----------------------------------------------------------------------------*/ |
| 971 |
if (cBitCount == 24) |
| 972 |
convertRow24(bmprow, xelrow, cols, pixelformat); |
| 973 |
else if (cBitCount == 16) |
| 974 |
convertRow16(bmprow, xelrow, cols, pixelformat); |
| 975 |
else if (cBitCount == 32) |
| 976 |
convertRow32(bmprow, xelrow, cols, pixelformat); |
| 977 |
else if (cBitCount == 8) { |
| 978 |
/* It's a whole byte colormap index */ |
| 979 |
unsigned int col; |
| 980 |
for (col = 0; col < cols; ++col) { |
| 981 |
unsigned int const index = bmprow[col]; |
| 982 |
validateIndex(index, cmapSize); |
| 983 |
xelrow[col] = colormap[index]; |
| 984 |
} |
| 985 |
} else if (cBitCount == 1 || cBitCount == 2 || cBitCount == 4) { |
| 986 |
/* It's a bit field color index */ |
| 987 |
unsigned char const mask = ( 1 << cBitCount ) - 1; |
| 988 |
|
| 989 |
unsigned int col; |
| 990 |
|
| 991 |
for (col = 0; col < cols; ++col) { |
| 992 |
unsigned int const cursor = (col*cBitCount)/8; |
| 993 |
unsigned int const shift = 8 - ((col*cBitCount) % 8) - cBitCount; |
| 994 |
unsigned int const index = |
| 995 |
(bmprow[cursor] & (mask << shift)) >> shift; |
| 996 |
validateIndex(index, cmapSize); |
| 997 |
xelrow[col] = colormap[index]; |
| 998 |
} |
| 999 |
} else { |
| 1000 |
/* Every possible BMP bits per pixel is handled above */ |
| 1001 |
assert(false); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
static unsigned char ** |
| 1008 |
allocBmpRaster(unsigned int const rows, |
| 1009 |
unsigned int const bytesPerRow) { |
| 1010 |
|
| 1011 |
unsigned int const storageSize = |
| 1012 |
rows * sizeof(unsigned char *) + rows * bytesPerRow; |
| 1013 |
unsigned char ** bmpRaster; |
| 1014 |
unsigned int row; |
| 1015 |
unsigned char * startOfRows; |
| 1016 |
|
| 1017 |
/* The raster array consists of an array of pointers to the rows |
| 1018 |
followed by the rows of bytes, in a single allocated chunk of storage. |
| 1019 |
*/ |
| 1020 |
|
| 1021 |
if (UINT_MAX / (bytesPerRow + sizeof(unsigned char *)) < rows) |
| 1022 |
pm_error("raster is ridiculously large."); |
| 1023 |
|
| 1024 |
bmpRaster = (unsigned char **) malloc(storageSize); |
| 1025 |
|
| 1026 |
if (bmpRaster == NULL) |
| 1027 |
pm_error("Unable to allocate %u bytes for the BMP raster\n", |
| 1028 |
storageSize); |
| 1029 |
|
| 1030 |
startOfRows = (unsigned char *)(bmpRaster + rows); |
| 1031 |
|
| 1032 |
for (row = 0; row < rows; ++row) |
| 1033 |
bmpRaster[row] = startOfRows + row * bytesPerRow; |
| 1034 |
|
| 1035 |
return bmpRaster; |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
static void |
| 1041 |
readrow(FILE * const ifP, |
| 1042 |
unsigned int const row, |
| 1043 |
unsigned int const bytesPerRow, |
| 1044 |
unsigned char ** const bmpRaster, |
| 1045 |
unsigned int * const bytesReadP) { |
| 1046 |
|
| 1047 |
size_t bytesRead; |
| 1048 |
|
| 1049 |
assert(bytesPerRow > 0); |
| 1050 |
|
| 1051 |
bytesRead = fread(bmpRaster[row], 1, bytesPerRow, ifP); |
| 1052 |
|
| 1053 |
if (bytesRead < bytesPerRow) { |
| 1054 |
if (feof(ifP)) |
| 1055 |
pm_error("End of file reading row %u of BMP raster.", row); |
| 1056 |
else |
| 1057 |
pm_error("Error reading BMP raster. Errno=%d (%s)", |
| 1058 |
errno, strerror(errno)); |
| 1059 |
} |
| 1060 |
*bytesReadP += bytesRead; |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
static void |
| 1066 |
nybbleAlign(unsigned char * const bytes, |
| 1067 |
unsigned int const nybbleCt){ |
| 1068 |
/*---------------------------------------------------------------------------- |
| 1069 |
Shift the 'nybbleCt' nybbles of bytes[], after the first byte, one nybble |
| 1070 |
toward the left, with the first of those nybble shifting into the right half |
| 1071 |
of the first byte. Leave the left half of the first byte alone. |
| 1072 |
|
| 1073 |
Example: |
| 1074 |
|
| 1075 |
(Numbers in hex, 8 nybbles) |
| 1076 |
5? 13 7E 89 A1 |
| 1077 |
becomes 51 37 E8 9A 10 |
| 1078 |
-----------------------------------------------------------------------------*/ |
| 1079 |
unsigned int const fullByteCt = (nybbleCt + 1) / 2; |
| 1080 |
unsigned int i; |
| 1081 |
|
| 1082 |
bytes[0] >>= 4; |
| 1083 |
|
| 1084 |
for (i = 0; i < fullByteCt; ++i) |
| 1085 |
bytes[i] = bytes[i] << 4 | bytes[i+1] >> 4; |
| 1086 |
|
| 1087 |
if (nybbleCt % 2 == 0) { |
| 1088 |
/* There is a final right nybble. Shift it. */ |
| 1089 |
bytes[fullByteCt] <<= 4; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
enum rleStatus { ABS_MODE, ENC_MODE, END_OF_ROW, END_OF_BMP, DELTA }; |
| 1096 |
|
| 1097 |
static enum rleStatus |
| 1098 |
readRLEcode(FILE * const ifP, |
| 1099 |
unsigned int * const cntP, |
| 1100 |
unsigned char * const codeP) { |
| 1101 |
|
| 1102 |
unsigned short s; |
| 1103 |
enum rleStatus retval; |
| 1104 |
|
| 1105 |
s = GetBigShort(ifP); |
| 1106 |
|
| 1107 |
if (s == 0) retval = END_OF_ROW; |
| 1108 |
else if (s == 1) retval = END_OF_BMP; |
| 1109 |
else if (s == 2) retval = DELTA; |
| 1110 |
else if (s < 256) { |
| 1111 |
if (cntP) |
| 1112 |
*cntP = s & 0xff; |
| 1113 |
retval = ABS_MODE; |
| 1114 |
} else { |
| 1115 |
if (cntP && codeP) { |
| 1116 |
*cntP = (s >> 8) & 0xff; |
| 1117 |
*codeP = s & 0xff; |
| 1118 |
} |
| 1119 |
retval = ENC_MODE; |
| 1120 |
} |
| 1121 |
return retval; |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
static void |
| 1127 |
readrowRLE(FILE * const ifP, |
| 1128 |
unsigned int const row, |
| 1129 |
unsigned int const cols, |
| 1130 |
bool const lastrow, |
| 1131 |
BMPCompType const compression, |
| 1132 |
unsigned char ** const bmpRaster, |
| 1133 |
unsigned int * const bytesReadP) { |
| 1134 |
|
| 1135 |
bool const rle4 = (compression == BMPCOMP_RLE4); |
| 1136 |
int const pixelsPerRowMargin = rle4 ? cols % 2 : 0; |
| 1137 |
|
| 1138 |
char const err_decode[] = |
| 1139 |
"Error while decoding compressed BMP image. " |
| 1140 |
"%s. Row: %u Pixel: %u" ; |
| 1141 |
|
| 1142 |
unsigned int totalBytesRead; |
| 1143 |
unsigned int pixelsRead; |
| 1144 |
|
| 1145 |
/* There are RLE4 images with rows coded up to the byte boundary, |
| 1146 |
resulting in each row one pixel larger than the column length |
| 1147 |
stated in the BMP info header (header.cols) when the column length |
| 1148 |
is odd. |
| 1149 |
|
| 1150 |
pixelsPerRowMargin is a "wart" to provide for this case. |
| 1151 |
*/ |
| 1152 |
|
| 1153 |
totalBytesRead = 0; /* Initial value */ |
| 1154 |
pixelsRead = 0; /* Initial value */ |
| 1155 |
|
| 1156 |
while (true) { |
| 1157 |
unsigned int n; |
| 1158 |
/* decompressed bytes already read; current write point */ |
| 1159 |
unsigned int cnt; |
| 1160 |
unsigned char code; |
| 1161 |
|
| 1162 |
n = rle4 ? (pixelsRead + 1) / 2 : pixelsRead; |
| 1163 |
|
| 1164 |
switch (readRLEcode(ifP, &cnt, &code)) { |
| 1165 |
case ENC_MODE: { |
| 1166 |
unsigned int const byteCnt = rle4 ? (cnt + 1) /2 : cnt; |
| 1167 |
unsigned int i; |
| 1168 |
|
| 1169 |
if (pixelsRead + cnt > cols + pixelsPerRowMargin) |
| 1170 |
pm_error(err_decode, "Too many pixels in encoded mode", |
| 1171 |
row, pixelsRead ); |
| 1172 |
|
| 1173 |
for (i = 0; i < byteCnt; ++i) |
| 1174 |
bmpRaster[row][n+i] = code; |
| 1175 |
|
| 1176 |
if (rle4 && pixelsRead % 2 == 1) |
| 1177 |
/* previous read ended odd */ |
| 1178 |
nybbleAlign(&bmpRaster[row][n-1], cnt); |
| 1179 |
|
| 1180 |
pixelsRead += cnt; |
| 1181 |
totalBytesRead += 2; |
| 1182 |
} break; |
| 1183 |
|
| 1184 |
case ABS_MODE: { |
| 1185 |
unsigned int cmpBytesRead; /* compressed bytes read */ |
| 1186 |
/* align read-end to 16 bit boundary */ |
| 1187 |
unsigned int const bytesToRead = |
| 1188 |
rle4 ? (cnt + 3) / 4 * 2 : (cnt + 1) / 2 * 2; |
| 1189 |
|
| 1190 |
if (pixelsRead + cnt > cols + pixelsPerRowMargin) |
| 1191 |
pm_error(err_decode, "Too many pixels in absolute mode", |
| 1192 |
row, pixelsRead); |
| 1193 |
|
| 1194 |
cmpBytesRead = fread(&bmpRaster[row][n], |
| 1195 |
sizeof(char), bytesToRead, ifP); |
| 1196 |
|
| 1197 |
if (cmpBytesRead < bytesToRead) { |
| 1198 |
if (feof(ifP)) |
| 1199 |
pm_error("End of file reading row %u " |
| 1200 |
"of compressed BMP raster.", row); |
| 1201 |
else |
| 1202 |
pm_error("Error reading BMP raster. Errno=%d (%s)", |
| 1203 |
errno, strerror(errno)); |
| 1204 |
} |
| 1205 |
if (rle4 && pixelsRead % 2 == 1) /* previous read ended odd */ |
| 1206 |
nybbleAlign(&bmpRaster[row][n-1], cnt); |
| 1207 |
|
| 1208 |
pixelsRead += cnt; |
| 1209 |
totalBytesRead += cmpBytesRead + 2; |
| 1210 |
} break; |
| 1211 |
|
| 1212 |
case END_OF_ROW: { |
| 1213 |
if (cols == pixelsRead || |
| 1214 |
cols + pixelsPerRowMargin == pixelsRead) { |
| 1215 |
if (!lastrow) { |
| 1216 |
*bytesReadP += totalBytesRead + 2; |
| 1217 |
return; |
| 1218 |
} else if (readRLEcode(ifP, NULL, NULL) == END_OF_BMP) { |
| 1219 |
*bytesReadP += totalBytesRead +4; |
| 1220 |
return; |
| 1221 |
} else |
| 1222 |
/* lastrow and END_OF_BITMAP not detected */ |
| 1223 |
pm_error(err_decode, "End of bitmap not marked", |
| 1224 |
row, pixelsRead ); |
| 1225 |
} else |
| 1226 |
pm_error(err_decode, "Premature end of row", |
| 1227 |
row, pixelsRead); |
| 1228 |
} break; |
| 1229 |
|
| 1230 |
case END_OF_BMP: { |
| 1231 |
if (lastrow && (cols == pixelsRead || |
| 1232 |
cols + pixelsPerRowMargin == pixelsRead)){ |
| 1233 |
*bytesReadP += totalBytesRead + 2; |
| 1234 |
return; |
| 1235 |
} else |
| 1236 |
pm_error(err_decode, "Premature end of bitmap", |
| 1237 |
row, pixelsRead ); |
| 1238 |
/* Windows programs do not reject premature end of bitmap. |
| 1239 |
Rather, they set the remaining pixels of the raster to |
| 1240 |
an arbitrary value. In practice, images with incomplete |
| 1241 |
bitmaps are rare. |
| 1242 |
*/ |
| 1243 |
} break; |
| 1244 |
|
| 1245 |
case DELTA: { |
| 1246 |
/* Delta means "move the point (col,row) by the amount given |
| 1247 |
in the next two bytes." Like premature end of bitmap, the |
| 1248 |
official specs do not specify what value the skipped pixels |
| 1249 |
should be set to. Judging from Windows utilities, there is |
| 1250 |
no consensus within Microsoft either. |
| 1251 |
*/ |
| 1252 |
pm_error(err_decode, |
| 1253 |
"Delta code in compressed BMP image. " |
| 1254 |
"This program does not process deltas", |
| 1255 |
row, pixelsRead); |
| 1256 |
|
| 1257 |
} break; |
| 1258 |
|
| 1259 |
default: |
| 1260 |
pm_error("Internal error processing RLE code in row %u", row); |
| 1261 |
} |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
static void |
| 1268 |
bmpReadraster(FILE * const ifP, |
| 1269 |
unsigned int const cols, |
| 1270 |
unsigned int const rows, |
| 1271 |
enum rowOrder const rowOrder, |
| 1272 |
unsigned int const cBitCount, |
| 1273 |
BMPCompType const compression, |
| 1274 |
unsigned char *** const bmpRasterP, |
| 1275 |
unsigned int * const bytesReadP) { |
| 1276 |
/*---------------------------------------------------------------------------- |
| 1277 |
Read the raster from the BMP file on *ifP (which is positioned to the |
| 1278 |
raster). The raster is 'rows' rows of 'cols' columns, 'cBitCount' bits per |
| 1279 |
pixel, with rows in order 'rowOrder'. |
| 1280 |
|
| 1281 |
Return the raster in a newly malloced 2-dimensional array and return |
| 1282 |
a pointer to that array as *bmpRasterP. |
| 1283 |
|
| 1284 |
Leave the input file positioned immediately after the raster and return |
| 1285 |
as *bytesReadP the number of bytes we read from the file (i.e. the number |
| 1286 |
of bytes in the raster portion of the file). |
| 1287 |
-----------------------------------------------------------------------------*/ |
| 1288 |
unsigned int const bytesPerRow = |
| 1289 |
(compression == BMPCOMP_RLE4) ? cols / 2 + 2 : |
| 1290 |
(compression == BMPCOMP_RLE8) ? cols + 1 : |
| 1291 |
((cols * cBitCount + 31) / 32) * 4; |
| 1292 |
/* A BMP raster row is a multiple of 4 bytes, padded on the right |
| 1293 |
with don't cares. |
| 1294 |
*/ |
| 1295 |
unsigned char ** bmpRaster; |
| 1296 |
|
| 1297 |
assert(cols < (1<<16)); |
| 1298 |
assert(bytesPerRow < (1<<16)); |
| 1299 |
|
| 1300 |
bmpRaster = allocBmpRaster(rows, bytesPerRow); |
| 1301 |
|
| 1302 |
*bytesReadP = 0; |
| 1303 |
|
| 1304 |
/* row order BOTTOMUP is by far the most common case - the bottom |
| 1305 |
line is first in the file, the top line last. |
| 1306 |
|
| 1307 |
We have never actually seen TOPDOWN, except in a Microsoft spec |
| 1308 |
*/ |
| 1309 |
|
| 1310 |
switch(compression){ |
| 1311 |
case BMPCOMP_RGB: |
| 1312 |
case BMPCOMP_BITFIELDS: { |
| 1313 |
unsigned int i; |
| 1314 |
for (i = 0; i < rows; ++i) |
| 1315 |
readrow(ifP, rowOrder == TOPDOWN ? i : rows - i - 1, |
| 1316 |
bytesPerRow, bmpRaster, bytesReadP); |
| 1317 |
} break; |
| 1318 |
case BMPCOMP_RLE4: |
| 1319 |
case BMPCOMP_RLE8: { |
| 1320 |
unsigned int i; |
| 1321 |
/* Read all rows except last */ |
| 1322 |
assert(rows >= 1); |
| 1323 |
for (i = 0; i < rows - 1; ++i){ |
| 1324 |
readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1, |
| 1325 |
cols, FALSE, compression, bmpRaster, bytesReadP); |
| 1326 |
} |
| 1327 |
/* Read last row */ |
| 1328 |
readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1, |
| 1329 |
cols, TRUE, compression, bmpRaster, bytesReadP); |
| 1330 |
} break; |
| 1331 |
case BMPCOMP_JPEG: |
| 1332 |
pm_error("BMP file uses JPEG compression. We don't know how to " |
| 1333 |
"interpret that."); |
| 1334 |
break; |
| 1335 |
case BMPCOMP_PNG: |
| 1336 |
pm_error("BMP file uses PNG compression. We don't know how to " |
| 1337 |
"interpret that."); |
| 1338 |
break; |
| 1339 |
} |
| 1340 |
*bmpRasterP = bmpRaster; |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
static void |
| 1346 |
reportHeader(struct bmpInfoHeader const header, |
| 1347 |
unsigned int const offBits, |
| 1348 |
bool const verbose) { |
| 1349 |
|
| 1350 |
if (verbose) { |
| 1351 |
pm_message("BMP image header says:"); |
| 1352 |
pm_message(" Class of BMP: %s", BMPClassName(header.class)); |
| 1353 |
pm_message(" Width: %d pixels", header.cols); |
| 1354 |
pm_message(" Height: %d pixels", header.rows); |
| 1355 |
pm_message(" Depth: %d planes", header.cPlanes); |
| 1356 |
pm_message(" Row order: %s", |
| 1357 |
header.rowOrder == BOTTOMUP ? "bottom up" : "top down"); |
| 1358 |
pm_message(" Byte offset of raster within file: %u", offBits); |
| 1359 |
pm_message(" Bits per pixel in raster: %u", header.cBitCount); |
| 1360 |
pm_message(" Compression: %s", BMPCompTypeName(header.compression)); |
| 1361 |
pm_message(" Colors in color map: %u", header.cmapSize); |
| 1362 |
} else { |
| 1363 |
pm_message("%s BMP, %ux%ux%u", |
| 1364 |
BMPClassName(header.class), |
| 1365 |
header.cols, |
| 1366 |
header.rows, |
| 1367 |
header.cBitCount); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
static void |
| 1374 |
validateCPlanes(unsigned short const cPlanes) { |
| 1375 |
|
| 1376 |
if (cPlanes != 1) |
| 1377 |
pm_error("Error: invalid planes value in BMP header. Must be 1"); |
| 1378 |
} |
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
static void |
| 1383 |
analyzeColors(xel const colormap[], |
| 1384 |
unsigned int const cmapSize, |
| 1385 |
xelval const maxval, |
| 1386 |
bool * const grayPresentP, |
| 1387 |
bool * const colorPresentP) { |
| 1388 |
|
| 1389 |
if (cmapSize == 0) { |
| 1390 |
/* No colormap, and we're not about to search the entire raster, |
| 1391 |
so we just assume it's full color |
| 1392 |
*/ |
| 1393 |
*colorPresentP = TRUE; |
| 1394 |
*grayPresentP = TRUE; |
| 1395 |
} else { |
| 1396 |
unsigned int i; |
| 1397 |
|
| 1398 |
*colorPresentP = FALSE; /* initial assumption */ |
| 1399 |
*grayPresentP = FALSE; /* initial assumption */ |
| 1400 |
for (i = 0; i < cmapSize; ++i) { |
| 1401 |
if (PPM_ISGRAY(colormap[i])) { |
| 1402 |
if (PPM_GETR(colormap[i]) != 0 && |
| 1403 |
PPM_GETR(colormap[i]) != maxval) |
| 1404 |
*grayPresentP = TRUE; |
| 1405 |
} else |
| 1406 |
*colorPresentP = TRUE; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
static void |
| 1414 |
warnIfOffBitsWrong(struct bmpInfoHeader const bmpHeader, |
| 1415 |
unsigned int const offBits) { |
| 1416 |
|
| 1417 |
if (offBits != BMPoffbits(bmpHeader.class, bmpHeader.cBitCount, |
| 1418 |
bmpHeader.cmapSize)) { |
| 1419 |
|
| 1420 |
pm_message("warning: the BMP header says the raster starts " |
| 1421 |
"at offset %u bytes into the file (offbits), " |
| 1422 |
"but that there are %u bytes of information before " |
| 1423 |
"the raster. This inconsistency probably means the " |
| 1424 |
"input file is not a legal BMP file and is unusable.", |
| 1425 |
offBits, |
| 1426 |
BMPoffbits(bmpHeader.class, bmpHeader.cBitCount, |
| 1427 |
bmpHeader.cmapSize)); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
static void |
| 1434 |
readColorMap(FILE * const ifP, |
| 1435 |
struct bmpInfoHeader const bmpHeader, |
| 1436 |
xel ** const colorMapP, |
| 1437 |
unsigned int * const posP) { |
| 1438 |
|
| 1439 |
unsigned int bytesRead; |
| 1440 |
|
| 1441 |
bmpReadColormap(ifP, bmpHeader.class, |
| 1442 |
colorMapP, bmpHeader.cmapSize, &bytesRead); |
| 1443 |
|
| 1444 |
*posP += bytesRead; |
| 1445 |
} |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
static void |
| 1450 |
readRaster(FILE * const ifP, |
| 1451 |
struct bmpInfoHeader const bmpHeader, |
| 1452 |
unsigned char *** const bmpRasterP, |
| 1453 |
unsigned int * const posP) { |
| 1454 |
|
| 1455 |
unsigned int bytesRead; |
| 1456 |
|
| 1457 |
bmpReadraster(ifP, bmpHeader.cols, bmpHeader.rows, bmpHeader.rowOrder, |
| 1458 |
bmpHeader.cBitCount, bmpHeader.compression, |
| 1459 |
bmpRasterP, &bytesRead); |
| 1460 |
|
| 1461 |
*posP += bytesRead; |
| 1462 |
} |
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
static bool |
| 1467 |
isValidBmpBpp(unsigned int const cBitCount) { |
| 1468 |
|
| 1469 |
switch (cBitCount) { |
| 1470 |
case 1: |
| 1471 |
case 2: |
| 1472 |
case 4: |
| 1473 |
case 8: |
| 1474 |
case 16: |
| 1475 |
case 24: |
| 1476 |
case 32: |
| 1477 |
return true; |
| 1478 |
default: |
| 1479 |
return false; |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
static void |
| 1486 |
readBmp(FILE * const ifP, |
| 1487 |
unsigned char *** const bmpRasterP, |
| 1488 |
unsigned int * const colsP, |
| 1489 |
unsigned int * const rowsP, |
| 1490 |
bool * const grayPresentP, |
| 1491 |
bool * const colorPresentP, |
| 1492 |
unsigned int * const cBitCountP, |
| 1493 |
struct pixelformat * const pixelformatP, |
| 1494 |
xel ** const colormapP, |
| 1495 |
unsigned int * const cmapSizeP, |
| 1496 |
bool const verbose) { |
| 1497 |
|
| 1498 |
xel * colormap; /* malloc'ed */ |
| 1499 |
unsigned int pos; |
| 1500 |
/* Current byte position in the BMP file */ |
| 1501 |
|
| 1502 |
/* The following are all information from the BMP headers */ |
| 1503 |
|
| 1504 |
unsigned int offBits; |
| 1505 |
/* Byte offset into file of raster */ |
| 1506 |
struct bmpInfoHeader bmpHeader; |
| 1507 |
|
| 1508 |
pos = 0; /* Starting at the beginning ... */ |
| 1509 |
{ |
| 1510 |
unsigned int bytesRead; |
| 1511 |
bmpReadfileheader(ifP, &bytesRead, &offBits); |
| 1512 |
pos += bytesRead; |
| 1513 |
} |
| 1514 |
{ |
| 1515 |
unsigned int bytesRead; |
| 1516 |
const char * error; |
| 1517 |
bmpReadinfoheader(ifP, &bytesRead, &bmpHeader, &error); |
| 1518 |
if (error) |
| 1519 |
pm_error("Failed to read the BMP info header. Image may " |
| 1520 |
"not be a valid BMP. %s", error); |
| 1521 |
|
| 1522 |
if (verbose) |
| 1523 |
pm_message("Read %u bytes of header", bytesRead); |
| 1524 |
pos += bytesRead; |
| 1525 |
} |
| 1526 |
|
| 1527 |
reportHeader(bmpHeader, offBits, verbose); |
| 1528 |
|
| 1529 |
validateCPlanes(bmpHeader.cPlanes); |
| 1530 |
|
| 1531 |
warnIfOffBitsWrong(bmpHeader, offBits); |
| 1532 |
|
| 1533 |
readColorMap(ifP, bmpHeader, &colormap, &pos); |
| 1534 |
|
| 1535 |
analyzeColors(colormap, bmpHeader.cmapSize, bmpMaxval, |
| 1536 |
grayPresentP, colorPresentP); |
| 1537 |
|
| 1538 |
readOffBytes(ifP, offBits - pos); |
| 1539 |
|
| 1540 |
pos = offBits; |
| 1541 |
|
| 1542 |
readRaster(ifP, bmpHeader, bmpRasterP, &pos); |
| 1543 |
|
| 1544 |
if (fgetc(ifP) != EOF) |
| 1545 |
pm_message("warning: some image data remains unread."); |
| 1546 |
|
| 1547 |
if (!isValidBmpBpp(bmpHeader.cBitCount)) |
| 1548 |
pm_error("Invalid BMP image: 'cBitCount' field of header " |
| 1549 |
"(number of bits for each pixel in raster) is %u", |
| 1550 |
bmpHeader.cBitCount); |
| 1551 |
|
| 1552 |
*cBitCountP = bmpHeader.cBitCount; |
| 1553 |
|
| 1554 |
*colsP = bmpHeader.cols; |
| 1555 |
*rowsP = bmpHeader.rows; |
| 1556 |
*pixelformatP = bmpHeader.pixelformat; |
| 1557 |
*colormapP = colormap; |
| 1558 |
*cmapSizeP = bmpHeader.cmapSize; |
| 1559 |
} |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
static void |
| 1564 |
writeRasterGen(unsigned char ** const bmpRaster, |
| 1565 |
unsigned int const cols, |
| 1566 |
unsigned int const rows, |
| 1567 |
int const format, |
| 1568 |
unsigned int const cBitCount, |
| 1569 |
struct pixelformat const pixelformat, |
| 1570 |
xel const colormap[], |
| 1571 |
unsigned int const cmapSize) { |
| 1572 |
/*---------------------------------------------------------------------------- |
| 1573 |
Write the PNM raster to Standard Output, corresponding to the raw BMP |
| 1574 |
raster bmpRaster. Write the raster assuming the PNM image has |
| 1575 |
dimensions 'cols' by 'rows' and format 'format', with maxval 255. |
| 1576 |
|
| 1577 |
The BMP image has 'cBitCount' bits per pixel, arranged in format |
| 1578 |
'pixelformat'. |
| 1579 |
|
| 1580 |
If the image is colormapped, colormap[] is the colormap |
| 1581 |
(colormap[i] is the color with color index i). |
| 1582 |
|
| 1583 |
writeRasterPbm() is faster for a PBM image. |
| 1584 |
-----------------------------------------------------------------------------*/ |
| 1585 |
xel * xelrow; |
| 1586 |
unsigned int row; |
| 1587 |
|
| 1588 |
xelrow = pnm_allocrow(cols); |
| 1589 |
|
| 1590 |
for (row = 0; row < rows; ++row) { |
| 1591 |
convertRow(bmpRaster[row], xelrow, cols, cBitCount, pixelformat, |
| 1592 |
colormap, cmapSize); |
| 1593 |
pnm_writepnmrow(stdout, xelrow, cols, bmpMaxval, format, FALSE); |
| 1594 |
} |
| 1595 |
pnm_freerow(xelrow); |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
static void |
| 1601 |
writeRasterPbm(unsigned char ** const bmpRaster, |
| 1602 |
unsigned int const cols, |
| 1603 |
unsigned int const rows, |
| 1604 |
xel const colormap[]) { |
| 1605 |
/*---------------------------------------------------------------------------- |
| 1606 |
Write the PBM raster to Standard Output corresponding to the raw BMP |
| 1607 |
raster bmpRaster. Write the raster assuming the PBM image has |
| 1608 |
dimensions 'cols' by 'rows'. |
| 1609 |
|
| 1610 |
The BMP image has 'cBitCount' bits per pixel, arranged in format |
| 1611 |
'pixelformat'. |
| 1612 |
|
| 1613 |
The image must be colormapped; colormap[] is the colormap |
| 1614 |
(colormap[i] is the color with color index i). We cannot handle the |
| 1615 |
abnormal case in which colormap[0] and colormap[1] have the same |
| 1616 |
value (i.e. both white or both black.) |
| 1617 |
|
| 1618 |
We destroy *bmpRaster as a side effect. |
| 1619 |
-----------------------------------------------------------------------------*/ |
| 1620 |
unsigned int const colCharCt = pbm_packed_bytes(cols); |
| 1621 |
|
| 1622 |
unsigned int row; |
| 1623 |
enum colorFormat {BlackWhite, WhiteBlack}; |
| 1624 |
enum colorFormat colorformat; |
| 1625 |
|
| 1626 |
if (PPM_GETR(colormap[0]) > 0) |
| 1627 |
colorformat = WhiteBlack; |
| 1628 |
else |
| 1629 |
colorformat = BlackWhite; |
| 1630 |
|
| 1631 |
for (row = 0; row < rows; ++row){ |
| 1632 |
unsigned char * const bitrow = bmpRaster[row]; |
| 1633 |
|
| 1634 |
if (colorformat == BlackWhite) { |
| 1635 |
unsigned int i; |
| 1636 |
for (i = 0; i < colCharCt; ++i) |
| 1637 |
bitrow[i] = ~bitrow[i]; /* flip all pixels */ |
| 1638 |
} |
| 1639 |
|
| 1640 |
pbm_cleanrowend_packed(bitrow, cols); |
| 1641 |
pbm_writepbmrow_packed(stdout, bitrow, cols, FALSE); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
int |
| 1648 |
main(int argc, const char ** argv) { |
| 1649 |
|
| 1650 |
struct cmdlineInfo cmdline; |
| 1651 |
FILE * ifP; |
| 1652 |
int outputType; |
| 1653 |
|
| 1654 |
bool grayPresent, colorPresent; |
| 1655 |
/* These tell whether the image contains shades of gray other than |
| 1656 |
black and white and whether it has colors other than black, white, |
| 1657 |
and gray. |
| 1658 |
*/ |
| 1659 |
unsigned int cols, rows; |
| 1660 |
unsigned char ** bmpRaster; |
| 1661 |
/* The raster part of the BMP image, as a row x column array, with |
| 1662 |
each element being a raw byte from the BMP raster. Note that |
| 1663 |
bmpRaster[0] is really Row 0 -- the top row of the image, even |
| 1664 |
though the bottom row comes first in the BMP format. |
| 1665 |
*/ |
| 1666 |
unsigned int cBitCount; |
| 1667 |
/* Number of bits in BMP raster for each pixel */ |
| 1668 |
struct pixelformat pixelformat; |
| 1669 |
/* Format of the raster bits for a single pixel */ |
| 1670 |
xel * colormap; |
| 1671 |
/* Malloc'ed colormap (palette) from the BMP. Contents of map |
| 1672 |
undefined if not a colormapped BMP. |
| 1673 |
*/ |
| 1674 |
unsigned int cmapSize; |
| 1675 |
/* Number of colormap entries. From BMP header. Note that a file may |
| 1676 |
be 8 bits per pixel but have fewer than 256 colors. In the 1 bit |
| 1677 |
per pixel case, there should be 2 entries according to the official |
| 1678 |
specification, but we allow files with just 1. |
| 1679 |
*/ |
| 1680 |
|
| 1681 |
pm_proginit(&argc, argv); |
| 1682 |
|
| 1683 |
parseCommandLine(argc, argv, &cmdline); |
| 1684 |
|
| 1685 |
ifP = pm_openr(cmdline.inputFileName); |
| 1686 |
if (streq(cmdline.inputFileName, "-")) |
| 1687 |
ifname = "Standard Input"; |
| 1688 |
else |
| 1689 |
ifname = cmdline.inputFileName; |
| 1690 |
|
| 1691 |
readBmp(ifP, &bmpRaster, &cols, &rows, &grayPresent, &colorPresent, |
| 1692 |
&cBitCount, &pixelformat, &colormap, &cmapSize, |
| 1693 |
cmdline.verbose); |
| 1694 |
pm_close(ifP); |
| 1695 |
|
| 1696 |
if (colorPresent) { |
| 1697 |
outputType = PPM_TYPE; |
| 1698 |
pm_message("WRITING PPM IMAGE"); |
| 1699 |
} else if (grayPresent) { |
| 1700 |
outputType = PGM_TYPE; |
| 1701 |
pm_message("WRITING PGM IMAGE"); |
| 1702 |
} else { |
| 1703 |
outputType = PBM_TYPE; |
| 1704 |
pm_message("WRITING PBM IMAGE"); |
| 1705 |
} |
| 1706 |
|
| 1707 |
if (outputType == PBM_TYPE && cBitCount == 1){ |
| 1708 |
pbm_writepbminit(stdout, cols, rows, FALSE); |
| 1709 |
writeRasterPbm(bmpRaster, cols, rows, colormap); |
| 1710 |
} else { |
| 1711 |
pnm_writepnminit(stdout, cols, rows, bmpMaxval, outputType, FALSE); |
| 1712 |
writeRasterGen(bmpRaster, cols, rows, outputType, cBitCount, |
| 1713 |
pixelformat, colormap, cmapSize); |
| 1714 |
} |
| 1715 |
free(colormap); |
| 1716 |
free(bmpRaster); |
| 1717 |
|
| 1718 |
return 0; |
| 1719 |
} |
| 1720 |
|
| 1721 |
|
| 1722 |
|