| 1 |
/* |
| 2 |
* Project: libtiff tools |
| 3 |
* Purpose: Convert Windows BMP files in TIFF. |
| 4 |
* Author: Andrey Kiselev, [email protected] |
| 5 |
* |
| 6 |
****************************************************************************** |
| 7 |
* Copyright (c) 2004, Andrey Kiselev <[email protected]> |
| 8 |
* |
| 9 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 10 |
* its documentation for any purpose is hereby granted without fee, provided |
| 11 |
* that (i) the above copyright notices and this permission notice appear in |
| 12 |
* all copies of the software and related documentation, and (ii) the names of |
| 13 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 14 |
* publicity relating to the software without the specific, prior written |
| 15 |
* permission of Sam Leffler and Silicon Graphics. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 19 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 20 |
* |
| 21 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 22 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 23 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 24 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 25 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 26 |
* OF THIS SOFTWARE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "tif_config.h" |
| 30 |
|
| 31 |
#include <ctype.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <sys/types.h> |
| 38 |
|
| 39 |
#ifdef HAVE_UNISTD_H |
| 40 |
#include <unistd.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#if HAVE_FCNTL_H |
| 44 |
#include <fcntl.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#if HAVE_SYS_TYPES_H |
| 48 |
#include <sys/types.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#if HAVE_IO_H |
| 52 |
#include <io.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef NEED_LIBPORT |
| 56 |
#include "libport.h" |
| 57 |
#endif |
| 58 |
|
| 59 |
#include "tiffio.h" |
| 60 |
#include "tiffiop.h" |
| 61 |
|
| 62 |
#ifndef O_BINARY |
| 63 |
#define O_BINARY 0 |
| 64 |
#endif |
| 65 |
|
| 66 |
enum BMPType |
| 67 |
{ |
| 68 |
BMPT_WIN4, /* BMP used in Windows 3.0/NT 3.51/95 */ |
| 69 |
BMPT_WIN5, /* BMP used in Windows NT 4.0/98/Me/2000/XP */ |
| 70 |
BMPT_OS21, /* BMP used in OS/2 PM 1.x */ |
| 71 |
BMPT_OS22 /* BMP used in OS/2 PM 2.x */ |
| 72 |
}; |
| 73 |
|
| 74 |
/* |
| 75 |
* Bitmap file consists of a BMPFileHeader structure followed by a |
| 76 |
* BMPInfoHeader structure. An array of BMPColorEntry structures (also called |
| 77 |
* a colour table) follows the bitmap information header structure. The colour |
| 78 |
* table is followed by a second array of indexes into the colour table (the |
| 79 |
* actual bitmap data). Data may be compressed, for 4-bpp and 8-bpp used RLE |
| 80 |
* compression. |
| 81 |
* |
| 82 |
* +---------------------+ |
| 83 |
* | BMPFileHeader | |
| 84 |
* +---------------------+ |
| 85 |
* | BMPInfoHeader | |
| 86 |
* +---------------------+ |
| 87 |
* | BMPColorEntry array | |
| 88 |
* +---------------------+ |
| 89 |
* | Colour-index array | |
| 90 |
* +---------------------+ |
| 91 |
* |
| 92 |
* All numbers stored in Intel order with least significant byte first. |
| 93 |
*/ |
| 94 |
|
| 95 |
enum BMPComprMethod |
| 96 |
{ |
| 97 |
BMPC_RGB = 0L, /* Uncompressed */ |
| 98 |
BMPC_RLE8 = 1L, /* RLE for 8 bpp images */ |
| 99 |
BMPC_RLE4 = 2L, /* RLE for 4 bpp images */ |
| 100 |
BMPC_BITFIELDS = 3L, /* Bitmap is not compressed and the colour table |
| 101 |
* consists of three DWORD color masks that specify |
| 102 |
* the red, green, and blue components of each |
| 103 |
* pixel. This is valid when used with |
| 104 |
* 16- and 32-bpp bitmaps. */ |
| 105 |
BMPC_JPEG = 4L, /* Indicates that the image is a JPEG image. */ |
| 106 |
BMPC_PNG = 5L /* Indicates that the image is a PNG image. */ |
| 107 |
}; |
| 108 |
|
| 109 |
enum BMPLCSType /* Type of logical color space. */ |
| 110 |
{ |
| 111 |
BMPLT_CALIBRATED_RGB = 0, /* This value indicates that endpoints and |
| 112 |
* gamma values are given in the appropriate |
| 113 |
* fields. */ |
| 114 |
BMPLT_DEVICE_RGB = 1, |
| 115 |
BMPLT_DEVICE_CMYK = 2 |
| 116 |
}; |
| 117 |
|
| 118 |
typedef struct |
| 119 |
{ |
| 120 |
int32_t iCIEX; |
| 121 |
int32_t iCIEY; |
| 122 |
int32_t iCIEZ; |
| 123 |
} BMPCIEXYZ; |
| 124 |
|
| 125 |
typedef struct /* This structure contains the x, y, and z */ |
| 126 |
{ /* coordinates of the three colors that */ |
| 127 |
/* correspond */ |
| 128 |
BMPCIEXYZ iCIERed; /* to the red, green, and blue endpoints for */ |
| 129 |
BMPCIEXYZ iCIEGreen; /* a specified logical color space. */ |
| 130 |
BMPCIEXYZ iCIEBlue; |
| 131 |
} BMPCIEXYZTriple; |
| 132 |
|
| 133 |
typedef struct |
| 134 |
{ |
| 135 |
char bType[2]; /* Signature "BM" */ |
| 136 |
uint32_t iSize; /* Size in bytes of the bitmap file. Should |
| 137 |
* always be ignored while reading because |
| 138 |
* of error in Windows 3.0 SDK's description |
| 139 |
* of this field */ |
| 140 |
uint16_t iReserved1; /* Reserved, set as 0 */ |
| 141 |
uint16_t iReserved2; /* Reserved, set as 0 */ |
| 142 |
uint32_t iOffBits; /* Offset of the image from file start in bytes */ |
| 143 |
} BMPFileHeader; |
| 144 |
|
| 145 |
/* File header size in bytes: */ |
| 146 |
const int BFH_SIZE = 14; |
| 147 |
|
| 148 |
typedef struct |
| 149 |
{ |
| 150 |
uint32_t iSize; /* Size of BMPInfoHeader structure in bytes. |
| 151 |
* Should be used to determine start of the |
| 152 |
* colour table */ |
| 153 |
int32_t iWidth; /* Image width */ |
| 154 |
int32_t iHeight; /* Image height. If positive, image has bottom |
| 155 |
* left origin, if negative --- top left. */ |
| 156 |
int16_t iPlanes; /* Number of image planes (must be set to 1) */ |
| 157 |
int16_t iBitCount; /* Number of bits per pixel (1, 4, 8, 16, 24 |
| 158 |
* or 32). If 0 then the number of bits per |
| 159 |
* pixel is specified or is implied by the |
| 160 |
* JPEG or PNG format. */ |
| 161 |
uint32_t iCompression; /* Compression method */ |
| 162 |
uint32_t iSizeImage; /* Size of uncomressed image in bytes. May |
| 163 |
* be 0 for BMPC_RGB bitmaps. If iCompression |
| 164 |
* is BI_JPEG or BI_PNG, iSizeImage indicates |
| 165 |
* the size of the JPEG or PNG image buffer. */ |
| 166 |
int32_t iXPelsPerMeter; /* X resolution, pixels per meter (0 if not used) */ |
| 167 |
int32_t iYPelsPerMeter; /* Y resolution, pixels per meter (0 if not used) */ |
| 168 |
uint32_t iClrUsed; /* Size of colour table. If 0, iBitCount should |
| 169 |
* be used to calculate this value |
| 170 |
* (1<<iBitCount). This value should be |
| 171 |
* unsigned for proper shifting. */ |
| 172 |
int32_t iClrImportant; /* Number of important colours. If 0, all |
| 173 |
* colours are required */ |
| 174 |
|
| 175 |
/* |
| 176 |
* Fields above should be used for bitmaps, compatible with Windows NT 3.51 |
| 177 |
* and earlier. Windows 98/Me, Windows 2000/XP introduces additional fields: |
| 178 |
*/ |
| 179 |
|
| 180 |
int32_t iRedMask; /* Colour mask that specifies the red component |
| 181 |
* of each pixel, valid only if iCompression |
| 182 |
* is set to BI_BITFIELDS. */ |
| 183 |
int32_t iGreenMask; /* The same for green component */ |
| 184 |
int32_t iBlueMask; /* The same for blue component */ |
| 185 |
int32_t iAlphaMask; /* Colour mask that specifies the alpha |
| 186 |
* component of each pixel. */ |
| 187 |
uint32_t iCSType; /* Colour space of the DIB. */ |
| 188 |
BMPCIEXYZTriple sEndpoints; /* This member is ignored unless the iCSType |
| 189 |
* member specifies BMPLT_CALIBRATED_RGB. */ |
| 190 |
int32_t iGammaRed; /* Toned response curve for red. This member |
| 191 |
* is ignored unless color values are |
| 192 |
* calibrated RGB values and iCSType is set to |
| 193 |
* BMPLT_CALIBRATED_RGB. Specified |
| 194 |
* in 16^16 format. */ |
| 195 |
int32_t iGammaGreen; /* Toned response curve for green. */ |
| 196 |
int32_t iGammaBlue; /* Toned response curve for blue. */ |
| 197 |
} BMPInfoHeader; |
| 198 |
|
| 199 |
/* |
| 200 |
* Info header size in bytes: |
| 201 |
*/ |
| 202 |
const unsigned int BIH_WIN4SIZE = 40; /* for BMPT_WIN4 */ |
| 203 |
const unsigned int BIH_WIN5SIZE = 57; /* for BMPT_WIN5 */ |
| 204 |
const unsigned int BIH_OS21SIZE = 12; /* for BMPT_OS21 */ |
| 205 |
const unsigned int BIH_OS22SIZE = 64; /* for BMPT_OS22 */ |
| 206 |
|
| 207 |
/* |
| 208 |
* We will use plain byte array instead of this structure, but declaration |
| 209 |
* provided for reference |
| 210 |
*/ |
| 211 |
typedef struct |
| 212 |
{ |
| 213 |
char bBlue; |
| 214 |
char bGreen; |
| 215 |
char bRed; |
| 216 |
char bReserved; /* Must be 0 */ |
| 217 |
} BMPColorEntry; |
| 218 |
|
| 219 |
static uint16_t compression = (uint16_t)-1; |
| 220 |
static int jpegcolormode = JPEGCOLORMODE_RGB; |
| 221 |
static int quality = 75; /* JPEG quality */ |
| 222 |
static uint16_t predictor = 0; |
| 223 |
|
| 224 |
static void usage(void); |
| 225 |
static int processCompressOptions(char *); |
| 226 |
static void rearrangePixels(char *, uint32_t, uint32_t); |
| 227 |
|
| 228 |
int main(int argc, char *argv[]) |
| 229 |
{ |
| 230 |
uint32_t width, length; |
| 231 |
uint16_t nbands = 1; /* number of bands in input image */ |
| 232 |
uint16_t depth = 8; /* bits per pixel in input image */ |
| 233 |
uint32_t rowsperstrip = (uint32_t)-1; |
| 234 |
uint16_t photometric = PHOTOMETRIC_MINISBLACK; |
| 235 |
int fd = 0; |
| 236 |
_TIFF_stat_s instat; |
| 237 |
char *outfilename = NULL, *infilename = NULL; |
| 238 |
TIFF *out = NULL; |
| 239 |
|
| 240 |
BMPFileHeader file_hdr; |
| 241 |
BMPInfoHeader info_hdr; |
| 242 |
int bmp_type; |
| 243 |
uint32_t clr_tbl_size, n_clr_elems = 3; |
| 244 |
unsigned char *clr_tbl; |
| 245 |
unsigned short *red_tbl = NULL, *green_tbl = NULL, *blue_tbl = NULL; |
| 246 |
uint32_t row, clr; |
| 247 |
|
| 248 |
int c; |
| 249 |
#if !HAVE_DECL_OPTARG |
| 250 |
extern int optind; |
| 251 |
extern char *optarg; |
| 252 |
#endif |
| 253 |
|
| 254 |
while ((c = getopt(argc, argv, "c:r:o:h")) != -1) |
| 255 |
{ |
| 256 |
switch (c) |
| 257 |
{ |
| 258 |
case 'c': /* compression scheme */ |
| 259 |
if (!processCompressOptions(optarg)) |
| 260 |
usage(); |
| 261 |
break; |
| 262 |
case 'r': /* rows/strip */ |
| 263 |
rowsperstrip = atoi(optarg); |
| 264 |
break; |
| 265 |
case 'o': |
| 266 |
outfilename = optarg; |
| 267 |
break; |
| 268 |
case 'h': |
| 269 |
usage(); |
| 270 |
default: |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
if (argc - optind < 2) |
| 276 |
usage(); |
| 277 |
|
| 278 |
if (outfilename == NULL) |
| 279 |
outfilename = argv[argc - 1]; |
| 280 |
out = TIFFOpen(outfilename, "w"); |
| 281 |
if (out == NULL) |
| 282 |
{ |
| 283 |
TIFFError(infilename, "Cannot open file %s for output", outfilename); |
| 284 |
goto bad3; |
| 285 |
} |
| 286 |
|
| 287 |
while (optind < argc - 1) |
| 288 |
{ |
| 289 |
infilename = argv[optind]; |
| 290 |
optind++; |
| 291 |
|
| 292 |
fd = open(infilename, O_RDONLY | O_BINARY, 0); |
| 293 |
if (fd < 0) |
| 294 |
{ |
| 295 |
TIFFError(infilename, "Cannot open input file"); |
| 296 |
return -1; |
| 297 |
} |
| 298 |
|
| 299 |
if (read(fd, file_hdr.bType, 2) != 2) |
| 300 |
{ |
| 301 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 302 |
strerror(errno)); |
| 303 |
goto bad; |
| 304 |
} |
| 305 |
if (file_hdr.bType[0] != 'B' || file_hdr.bType[1] != 'M') |
| 306 |
{ |
| 307 |
TIFFError(infilename, "File is not BMP"); |
| 308 |
goto bad; |
| 309 |
} |
| 310 |
|
| 311 |
/* -------------------------------------------------------------------- |
| 312 |
*/ |
| 313 |
/* Read the BMPFileHeader. We need iOffBits value only */ |
| 314 |
/* -------------------------------------------------------------------- |
| 315 |
*/ |
| 316 |
if (_TIFF_lseek_f(fd, 10, SEEK_SET) == (_TIFF_off_t)-1) |
| 317 |
{ |
| 318 |
TIFFError(infilename, "Failed to seek to offset"); |
| 319 |
goto bad; |
| 320 |
} |
| 321 |
if (read(fd, &file_hdr.iOffBits, 4) != 4) |
| 322 |
{ |
| 323 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 324 |
strerror(errno)); |
| 325 |
goto bad; |
| 326 |
} |
| 327 |
#ifdef WORDS_BIGENDIAN |
| 328 |
TIFFSwabLong(&file_hdr.iOffBits); |
| 329 |
#endif |
| 330 |
if (_TIFF_fstat_f(fd, &instat) == -1) |
| 331 |
{ |
| 332 |
TIFFError(infilename, "Failed obtain file information"); |
| 333 |
goto bad; |
| 334 |
} |
| 335 |
file_hdr.iSize = instat.st_size; |
| 336 |
|
| 337 |
/* -------------------------------------------------------------------- |
| 338 |
*/ |
| 339 |
/* Read the BMPInfoHeader. */ |
| 340 |
/* -------------------------------------------------------------------- |
| 341 |
*/ |
| 342 |
|
| 343 |
if (_TIFF_lseek_f(fd, BFH_SIZE, SEEK_SET) == (_TIFF_off_t)-1) |
| 344 |
{ |
| 345 |
TIFFError(infilename, "Failed to seek to offset"); |
| 346 |
goto bad; |
| 347 |
} |
| 348 |
if (read(fd, &info_hdr.iSize, 4) != 4) |
| 349 |
{ |
| 350 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 351 |
strerror(errno)); |
| 352 |
goto bad; |
| 353 |
} |
| 354 |
#ifdef WORDS_BIGENDIAN |
| 355 |
TIFFSwabLong(&info_hdr.iSize); |
| 356 |
#endif |
| 357 |
|
| 358 |
if (info_hdr.iSize == BIH_WIN4SIZE) |
| 359 |
bmp_type = BMPT_WIN4; |
| 360 |
else if (info_hdr.iSize == BIH_OS21SIZE) |
| 361 |
bmp_type = BMPT_OS21; |
| 362 |
else if (info_hdr.iSize == BIH_OS22SIZE || info_hdr.iSize == 16) |
| 363 |
bmp_type = BMPT_OS22; |
| 364 |
else |
| 365 |
bmp_type = BMPT_WIN5; |
| 366 |
|
| 367 |
if (bmp_type == BMPT_WIN4 || bmp_type == BMPT_WIN5 || |
| 368 |
bmp_type == BMPT_OS22) |
| 369 |
{ |
| 370 |
if ((read(fd, &info_hdr.iWidth, 4) != 4) || |
| 371 |
(read(fd, &info_hdr.iHeight, 4) != 4) || |
| 372 |
(read(fd, &info_hdr.iPlanes, 2) != 2) || |
| 373 |
(read(fd, &info_hdr.iBitCount, 2) != 2) || |
| 374 |
(read(fd, &info_hdr.iCompression, 4) != 4) || |
| 375 |
(read(fd, &info_hdr.iSizeImage, 4) != 4) || |
| 376 |
(read(fd, &info_hdr.iXPelsPerMeter, 4) != 4) || |
| 377 |
(read(fd, &info_hdr.iYPelsPerMeter, 4) != 4) || |
| 378 |
(read(fd, &info_hdr.iClrUsed, 4) != 4) || |
| 379 |
(read(fd, &info_hdr.iClrImportant, 4) != 4)) |
| 380 |
{ |
| 381 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 382 |
strerror(errno)); |
| 383 |
goto bad; |
| 384 |
} |
| 385 |
#ifdef WORDS_BIGENDIAN |
| 386 |
TIFFSwabLong((uint32_t *)&info_hdr.iWidth); |
| 387 |
TIFFSwabLong((uint32_t *)&info_hdr.iHeight); |
| 388 |
TIFFSwabShort((uint16_t *)&info_hdr.iPlanes); |
| 389 |
TIFFSwabShort((uint16_t *)&info_hdr.iBitCount); |
| 390 |
TIFFSwabLong((uint32_t *)&info_hdr.iCompression); |
| 391 |
TIFFSwabLong((uint32_t *)&info_hdr.iSizeImage); |
| 392 |
TIFFSwabLong((uint32_t *)&info_hdr.iXPelsPerMeter); |
| 393 |
TIFFSwabLong((uint32_t *)&info_hdr.iYPelsPerMeter); |
| 394 |
TIFFSwabLong((uint32_t *)&info_hdr.iClrUsed); |
| 395 |
TIFFSwabLong((uint32_t *)&info_hdr.iClrImportant); |
| 396 |
#endif |
| 397 |
n_clr_elems = 4; |
| 398 |
} |
| 399 |
|
| 400 |
if (bmp_type == BMPT_OS22) |
| 401 |
{ |
| 402 |
/* |
| 403 |
* FIXME: different info in different documents |
| 404 |
* regarding this! |
| 405 |
*/ |
| 406 |
n_clr_elems = 3; |
| 407 |
} |
| 408 |
|
| 409 |
if (bmp_type == BMPT_OS21) |
| 410 |
{ |
| 411 |
int16_t iShort; |
| 412 |
|
| 413 |
if (read(fd, &iShort, 2) != 2) |
| 414 |
{ |
| 415 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 416 |
strerror(errno)); |
| 417 |
goto bad; |
| 418 |
} |
| 419 |
#ifdef WORDS_BIGENDIAN |
| 420 |
TIFFSwabShort((uint16_t *)&iShort); |
| 421 |
#endif |
| 422 |
info_hdr.iWidth = iShort; |
| 423 |
if (read(fd, &iShort, 2) != 2) |
| 424 |
{ |
| 425 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 426 |
strerror(errno)); |
| 427 |
goto bad; |
| 428 |
} |
| 429 |
#ifdef WORDS_BIGENDIAN |
| 430 |
TIFFSwabShort((uint16_t *)&iShort); |
| 431 |
#endif |
| 432 |
info_hdr.iHeight = iShort; |
| 433 |
if (read(fd, &iShort, 2) != 2) |
| 434 |
{ |
| 435 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 436 |
strerror(errno)); |
| 437 |
goto bad; |
| 438 |
} |
| 439 |
#ifdef WORDS_BIGENDIAN |
| 440 |
TIFFSwabShort((uint16_t *)&iShort); |
| 441 |
#endif |
| 442 |
info_hdr.iPlanes = iShort; |
| 443 |
if (read(fd, &iShort, 2) != 2) |
| 444 |
{ |
| 445 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 446 |
strerror(errno)); |
| 447 |
goto bad; |
| 448 |
} |
| 449 |
#ifdef WORDS_BIGENDIAN |
| 450 |
TIFFSwabShort((uint16_t *)&iShort); |
| 451 |
#endif |
| 452 |
info_hdr.iBitCount = iShort; |
| 453 |
info_hdr.iCompression = BMPC_RGB; |
| 454 |
n_clr_elems = 3; |
| 455 |
} |
| 456 |
|
| 457 |
if (info_hdr.iBitCount != 1 && info_hdr.iBitCount != 4 && |
| 458 |
info_hdr.iBitCount != 8 && info_hdr.iBitCount != 16 && |
| 459 |
info_hdr.iBitCount != 24 && info_hdr.iBitCount != 32) |
| 460 |
{ |
| 461 |
TIFFError(infilename, "Cannot process BMP file with bit count %d", |
| 462 |
info_hdr.iBitCount); |
| 463 |
close(fd); |
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
width = info_hdr.iWidth; |
| 468 |
length = (info_hdr.iHeight > 0) ? info_hdr.iHeight : -info_hdr.iHeight; |
| 469 |
if (width <= 0 || length <= 0) |
| 470 |
{ |
| 471 |
TIFFError(infilename, "Invalid dimensions of BMP file"); |
| 472 |
close(fd); |
| 473 |
return -1; |
| 474 |
} |
| 475 |
|
| 476 |
switch (info_hdr.iBitCount) |
| 477 |
{ |
| 478 |
case 1: |
| 479 |
case 4: |
| 480 |
case 8: |
| 481 |
nbands = 1; |
| 482 |
depth = info_hdr.iBitCount; |
| 483 |
photometric = PHOTOMETRIC_PALETTE; |
| 484 |
/* Allocate memory for colour table and read it. */ |
| 485 |
if (info_hdr.iClrUsed) |
| 486 |
clr_tbl_size = ((uint32_t)(1 << depth) < info_hdr.iClrUsed) |
| 487 |
? (uint32_t)(1 << depth) |
| 488 |
: info_hdr.iClrUsed; |
| 489 |
else |
| 490 |
clr_tbl_size = 1 << depth; |
| 491 |
clr_tbl = |
| 492 |
(unsigned char *)_TIFFmalloc(n_clr_elems * clr_tbl_size); |
| 493 |
if (!clr_tbl) |
| 494 |
{ |
| 495 |
TIFFError(infilename, |
| 496 |
"Can't allocate space for color table"); |
| 497 |
goto bad; |
| 498 |
} |
| 499 |
|
| 500 |
if (_TIFF_lseek_f(fd, BFH_SIZE + info_hdr.iSize, SEEK_SET) == |
| 501 |
(_TIFF_off_t)-1) |
| 502 |
{ |
| 503 |
TIFFError(infilename, "Failed to seek to offset"); |
| 504 |
goto bad; |
| 505 |
} |
| 506 |
if (read(fd, clr_tbl, n_clr_elems * clr_tbl_size) != |
| 507 |
(long)(n_clr_elems * clr_tbl_size)) |
| 508 |
{ |
| 509 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 510 |
strerror(errno)); |
| 511 |
goto bad; |
| 512 |
} |
| 513 |
|
| 514 |
red_tbl = (unsigned short *)_TIFFmalloc( |
| 515 |
((tmsize_t)1) << depth * sizeof(unsigned short)); |
| 516 |
if (!red_tbl) |
| 517 |
{ |
| 518 |
TIFFError(infilename, |
| 519 |
"Can't allocate space for red component table"); |
| 520 |
_TIFFfree(clr_tbl); |
| 521 |
goto bad1; |
| 522 |
} |
| 523 |
green_tbl = (unsigned short *)_TIFFmalloc( |
| 524 |
((tmsize_t)1) << depth * sizeof(unsigned short)); |
| 525 |
if (!green_tbl) |
| 526 |
{ |
| 527 |
TIFFError(infilename, |
| 528 |
"Can't allocate space for green component table"); |
| 529 |
_TIFFfree(clr_tbl); |
| 530 |
goto bad2; |
| 531 |
} |
| 532 |
blue_tbl = (unsigned short *)_TIFFmalloc( |
| 533 |
((tmsize_t)1) << depth * sizeof(unsigned short)); |
| 534 |
if (!blue_tbl) |
| 535 |
{ |
| 536 |
TIFFError(infilename, |
| 537 |
"Can't allocate space for blue component table"); |
| 538 |
_TIFFfree(clr_tbl); |
| 539 |
goto bad3; |
| 540 |
} |
| 541 |
|
| 542 |
for (clr = 0; clr < clr_tbl_size; clr++) |
| 543 |
{ |
| 544 |
red_tbl[clr] = 257 * clr_tbl[clr * n_clr_elems + 2]; |
| 545 |
green_tbl[clr] = 257 * clr_tbl[clr * n_clr_elems + 1]; |
| 546 |
blue_tbl[clr] = 257 * clr_tbl[clr * n_clr_elems]; |
| 547 |
} |
| 548 |
|
| 549 |
_TIFFfree(clr_tbl); |
| 550 |
break; |
| 551 |
case 16: |
| 552 |
case 24: |
| 553 |
nbands = 3; |
| 554 |
depth = info_hdr.iBitCount / nbands; |
| 555 |
photometric = PHOTOMETRIC_RGB; |
| 556 |
break; |
| 557 |
case 32: |
| 558 |
nbands = 3; |
| 559 |
depth = 8; |
| 560 |
photometric = PHOTOMETRIC_RGB; |
| 561 |
break; |
| 562 |
default: |
| 563 |
break; |
| 564 |
} |
| 565 |
|
| 566 |
/* -------------------------------------------------------------------- |
| 567 |
*/ |
| 568 |
/* Create output file. */ |
| 569 |
/* -------------------------------------------------------------------- |
| 570 |
*/ |
| 571 |
|
| 572 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); |
| 573 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length); |
| 574 |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 575 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nbands); |
| 576 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, depth); |
| 577 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 578 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); |
| 579 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, |
| 580 |
TIFFDefaultStripSize(out, rowsperstrip)); |
| 581 |
|
| 582 |
if (red_tbl && green_tbl && blue_tbl) |
| 583 |
{ |
| 584 |
TIFFSetField(out, TIFFTAG_COLORMAP, red_tbl, green_tbl, blue_tbl); |
| 585 |
} |
| 586 |
|
| 587 |
if (compression == (uint16_t)-1) |
| 588 |
compression = COMPRESSION_PACKBITS; |
| 589 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 590 |
switch (compression) |
| 591 |
{ |
| 592 |
case COMPRESSION_JPEG: |
| 593 |
if (photometric == PHOTOMETRIC_RGB && |
| 594 |
jpegcolormode == JPEGCOLORMODE_RGB) |
| 595 |
photometric = PHOTOMETRIC_YCBCR; |
| 596 |
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); |
| 597 |
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); |
| 598 |
break; |
| 599 |
case COMPRESSION_LZW: |
| 600 |
case COMPRESSION_ADOBE_DEFLATE: |
| 601 |
case COMPRESSION_DEFLATE: |
| 602 |
if (predictor != 0) |
| 603 |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); |
| 604 |
break; |
| 605 |
} |
| 606 |
|
| 607 |
/* -------------------------------------------------------------------- |
| 608 |
*/ |
| 609 |
/* Read uncompressed image data. */ |
| 610 |
/* -------------------------------------------------------------------- |
| 611 |
*/ |
| 612 |
|
| 613 |
if (info_hdr.iCompression == BMPC_RGB) |
| 614 |
{ |
| 615 |
uint32_t offset, size; |
| 616 |
char *scanbuf; |
| 617 |
|
| 618 |
/* XXX: Avoid integer overflow. We can calculate size |
| 619 |
* in one step using |
| 620 |
* |
| 621 |
* size = ((width * info_hdr.iBitCount + 31) & ~31) / 8 |
| 622 |
* |
| 623 |
* formulae, but we should check for overflow |
| 624 |
* conditions during calculation. |
| 625 |
*/ |
| 626 |
size = width * info_hdr.iBitCount + 31; |
| 627 |
if (!width || !info_hdr.iBitCount || |
| 628 |
(size - 31) / info_hdr.iBitCount != width) |
| 629 |
{ |
| 630 |
TIFFError(infilename, "Wrong image parameters; can't " |
| 631 |
"allocate space for scanline buffer"); |
| 632 |
goto bad3; |
| 633 |
} |
| 634 |
size = (size & ~31) / 8; |
| 635 |
|
| 636 |
scanbuf = (char *)_TIFFmalloc(size); |
| 637 |
if (!scanbuf) |
| 638 |
{ |
| 639 |
TIFFError(infilename, |
| 640 |
"Can't allocate space for scanline buffer"); |
| 641 |
goto bad3; |
| 642 |
} |
| 643 |
|
| 644 |
for (row = 0; row < length; row++) |
| 645 |
{ |
| 646 |
if (info_hdr.iHeight > 0) |
| 647 |
offset = file_hdr.iOffBits + (length - row - 1) * size; |
| 648 |
else |
| 649 |
offset = file_hdr.iOffBits + row * size; |
| 650 |
if (_TIFF_lseek_f(fd, offset, SEEK_SET) == (_TIFF_off_t)-1) |
| 651 |
{ |
| 652 |
TIFFError(infilename, "scanline %lu: Seek error", |
| 653 |
(unsigned long)row); |
| 654 |
break; |
| 655 |
} |
| 656 |
|
| 657 |
if (read(fd, scanbuf, size) != (long)size) |
| 658 |
{ |
| 659 |
TIFFError(infilename, "scanline %lu: Read error", |
| 660 |
(unsigned long)row); |
| 661 |
break; |
| 662 |
} |
| 663 |
|
| 664 |
rearrangePixels(scanbuf, width, info_hdr.iBitCount); |
| 665 |
|
| 666 |
if (TIFFWriteScanline(out, scanbuf, row, 0) < 0) |
| 667 |
{ |
| 668 |
TIFFError(infilename, "scanline %lu: Write error", |
| 669 |
(unsigned long)row); |
| 670 |
break; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
_TIFFfree(scanbuf); |
| 675 |
|
| 676 |
/* -------------------------------------------------------------------- |
| 677 |
*/ |
| 678 |
/* Read compressed image data. */ |
| 679 |
/* -------------------------------------------------------------------- |
| 680 |
*/ |
| 681 |
} |
| 682 |
else if (info_hdr.iCompression == BMPC_RLE8 || |
| 683 |
info_hdr.iCompression == BMPC_RLE4) |
| 684 |
{ |
| 685 |
uint32_t i, j, k, runlength; |
| 686 |
uint32_t compr_size, uncompr_size; |
| 687 |
unsigned char *comprbuf; |
| 688 |
unsigned char *uncomprbuf; |
| 689 |
|
| 690 |
compr_size = file_hdr.iSize - file_hdr.iOffBits; |
| 691 |
uncompr_size = width * length; |
| 692 |
/* Detect int overflow */ |
| 693 |
if (uncompr_size / width != length) |
| 694 |
{ |
| 695 |
TIFFError(infilename, "Invalid dimensions of BMP file"); |
| 696 |
close(fd); |
| 697 |
return -1; |
| 698 |
} |
| 699 |
if ((compr_size == 0) || (compr_size > ((uint32_t)~0) >> 1) || |
| 700 |
(uncompr_size == 0) || (uncompr_size > ((uint32_t)~0) >> 1)) |
| 701 |
{ |
| 702 |
TIFFError(infilename, "Invalid dimensions of BMP file"); |
| 703 |
close(fd); |
| 704 |
return -1; |
| 705 |
} |
| 706 |
comprbuf = (unsigned char *)_TIFFmalloc(compr_size); |
| 707 |
if (!comprbuf) |
| 708 |
{ |
| 709 |
TIFFError( |
| 710 |
infilename, |
| 711 |
"Can't allocate space for compressed scanline buffer"); |
| 712 |
goto bad3; |
| 713 |
} |
| 714 |
uncomprbuf = (unsigned char *)_TIFFmalloc(uncompr_size); |
| 715 |
if (!uncomprbuf) |
| 716 |
{ |
| 717 |
TIFFError( |
| 718 |
infilename, |
| 719 |
"Can't allocate space for uncompressed scanline buffer"); |
| 720 |
goto bad3; |
| 721 |
} |
| 722 |
|
| 723 |
if (_TIFF_lseek_f(fd, file_hdr.iOffBits, SEEK_SET) == |
| 724 |
(_TIFF_off_t)-1) |
| 725 |
{ |
| 726 |
TIFFError(infilename, "Failed to seek to offset"); |
| 727 |
goto bad3; |
| 728 |
} |
| 729 |
if (read(fd, comprbuf, compr_size) != (long)compr_size) |
| 730 |
{ |
| 731 |
TIFFError(infilename, "Failed to read from file (%s)", |
| 732 |
strerror(errno)); |
| 733 |
goto bad; |
| 734 |
} |
| 735 |
i = 0; |
| 736 |
j = 0; |
| 737 |
if (info_hdr.iBitCount == 8) |
| 738 |
{ /* RLE8 */ |
| 739 |
while (j < uncompr_size && i < compr_size) |
| 740 |
{ |
| 741 |
if (comprbuf[i]) |
| 742 |
{ |
| 743 |
runlength = comprbuf[i++]; |
| 744 |
while (runlength > 0 && j < uncompr_size && |
| 745 |
i < compr_size) |
| 746 |
{ |
| 747 |
uncomprbuf[j++] = comprbuf[i]; |
| 748 |
runlength--; |
| 749 |
} |
| 750 |
i++; |
| 751 |
} |
| 752 |
else |
| 753 |
{ |
| 754 |
i++; |
| 755 |
if (comprbuf[i] == 0) /* Next scanline */ |
| 756 |
i++; |
| 757 |
else if (comprbuf[i] == 1) /* End of image */ |
| 758 |
break; |
| 759 |
else if (comprbuf[i] == 2) |
| 760 |
{ /* Move to... */ |
| 761 |
i++; |
| 762 |
if (i < compr_size - 1) |
| 763 |
{ |
| 764 |
j += comprbuf[i] + comprbuf[i + 1] * width; |
| 765 |
i += 2; |
| 766 |
} |
| 767 |
else |
| 768 |
break; |
| 769 |
} |
| 770 |
else |
| 771 |
{ /* Absolute mode */ |
| 772 |
runlength = comprbuf[i++]; |
| 773 |
for (k = 0; k < runlength && j < uncompr_size && |
| 774 |
i < compr_size; |
| 775 |
k++) |
| 776 |
uncomprbuf[j++] = comprbuf[i++]; |
| 777 |
if (k & 0x01) |
| 778 |
i++; |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
else |
| 784 |
{ /* RLE4 */ |
| 785 |
while (j < uncompr_size && i < compr_size) |
| 786 |
{ |
| 787 |
if (comprbuf[i]) |
| 788 |
{ |
| 789 |
runlength = comprbuf[i++]; |
| 790 |
while (runlength > 0 && j < uncompr_size && |
| 791 |
i < compr_size) |
| 792 |
{ |
| 793 |
if (runlength & 0x01) |
| 794 |
uncomprbuf[j++] = (comprbuf[i] & 0xF0) >> 4; |
| 795 |
else |
| 796 |
uncomprbuf[j++] = comprbuf[i] & 0x0F; |
| 797 |
runlength--; |
| 798 |
} |
| 799 |
i++; |
| 800 |
} |
| 801 |
else |
| 802 |
{ |
| 803 |
i++; |
| 804 |
if (comprbuf[i] == 0) /* Next scanline */ |
| 805 |
i++; |
| 806 |
else if (comprbuf[i] == 1) /* End of image */ |
| 807 |
break; |
| 808 |
else if (comprbuf[i] == 2) |
| 809 |
{ /* Move to... */ |
| 810 |
i++; |
| 811 |
if (i < compr_size - 1) |
| 812 |
{ |
| 813 |
j += comprbuf[i] + comprbuf[i + 1] * width; |
| 814 |
i += 2; |
| 815 |
} |
| 816 |
else |
| 817 |
break; |
| 818 |
} |
| 819 |
else |
| 820 |
{ /* Absolute mode */ |
| 821 |
runlength = comprbuf[i++]; |
| 822 |
for (k = 0; k < runlength && j < uncompr_size && |
| 823 |
i < compr_size; |
| 824 |
k++) |
| 825 |
{ |
| 826 |
if (k & 0x01) |
| 827 |
uncomprbuf[j++] = comprbuf[i++] & 0x0F; |
| 828 |
else |
| 829 |
uncomprbuf[j++] = (comprbuf[i] & 0xF0) >> 4; |
| 830 |
} |
| 831 |
if (k & 0x01) |
| 832 |
i++; |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
_TIFFfree(comprbuf); |
| 839 |
|
| 840 |
for (row = 0; row < length; row++) |
| 841 |
{ |
| 842 |
if (TIFFWriteScanline(out, |
| 843 |
uncomprbuf + (length - row - 1) * width, |
| 844 |
row, 0) < 0) |
| 845 |
{ |
| 846 |
TIFFError(infilename, "scanline %lu: Write error.\n", |
| 847 |
(unsigned long)row); |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
_TIFFfree(uncomprbuf); |
| 852 |
} |
| 853 |
TIFFWriteDirectory(out); |
| 854 |
if (blue_tbl) |
| 855 |
{ |
| 856 |
_TIFFfree(blue_tbl); |
| 857 |
blue_tbl = NULL; |
| 858 |
} |
| 859 |
if (green_tbl) |
| 860 |
{ |
| 861 |
_TIFFfree(green_tbl); |
| 862 |
green_tbl = NULL; |
| 863 |
} |
| 864 |
if (red_tbl) |
| 865 |
{ |
| 866 |
_TIFFfree(red_tbl); |
| 867 |
red_tbl = NULL; |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
bad3: |
| 872 |
if (blue_tbl) |
| 873 |
_TIFFfree(blue_tbl); |
| 874 |
bad2: |
| 875 |
if (green_tbl) |
| 876 |
_TIFFfree(green_tbl); |
| 877 |
bad1: |
| 878 |
if (red_tbl) |
| 879 |
_TIFFfree(red_tbl); |
| 880 |
bad: |
| 881 |
close(fd); |
| 882 |
|
| 883 |
if (out) |
| 884 |
TIFFClose(out); |
| 885 |
return 0; |
| 886 |
} |
| 887 |
|
| 888 |
/* |
| 889 |
* Image data in BMP file stored in BGR (or ABGR) format. We should rearrange |
| 890 |
* pixels to RGB (RGBA) format. |
| 891 |
*/ |
| 892 |
static void rearrangePixels(char *buf, uint32_t width, uint32_t bit_count) |
| 893 |
{ |
| 894 |
char tmp; |
| 895 |
uint32_t i; |
| 896 |
|
| 897 |
switch (bit_count) |
| 898 |
{ |
| 899 |
case 16: /* FIXME: need a sample file */ |
| 900 |
break; |
| 901 |
case 24: |
| 902 |
for (i = 0; i < width; i++, buf += 3) |
| 903 |
{ |
| 904 |
tmp = *buf; |
| 905 |
*buf = *(buf + 2); |
| 906 |
*(buf + 2) = tmp; |
| 907 |
} |
| 908 |
break; |
| 909 |
case 32: |
| 910 |
{ |
| 911 |
char *buf1 = buf; |
| 912 |
|
| 913 |
for (i = 0; i < width; i++, buf += 4) |
| 914 |
{ |
| 915 |
tmp = *buf; |
| 916 |
*buf1++ = *(buf + 2); |
| 917 |
*buf1++ = *(buf + 1); |
| 918 |
*buf1++ = tmp; |
| 919 |
} |
| 920 |
} |
| 921 |
break; |
| 922 |
default: |
| 923 |
break; |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
static int processCompressOptions(char *opt) |
| 928 |
{ |
| 929 |
if (strcmp(opt, "none") == 0) |
| 930 |
compression = COMPRESSION_NONE; |
| 931 |
else if (strcmp(opt, "packbits") == 0) |
| 932 |
compression = COMPRESSION_PACKBITS; |
| 933 |
else if (strncmp(opt, "jpeg", 4) == 0) |
| 934 |
{ |
| 935 |
char *cp = strchr(opt, ':'); |
| 936 |
|
| 937 |
compression = COMPRESSION_JPEG; |
| 938 |
while (cp) |
| 939 |
{ |
| 940 |
if (isdigit((int)cp[1])) |
| 941 |
quality = atoi(cp + 1); |
| 942 |
else if (cp[1] == 'r') |
| 943 |
jpegcolormode = JPEGCOLORMODE_RAW; |
| 944 |
else |
| 945 |
usage(); |
| 946 |
|
| 947 |
cp = strchr(cp + 1, ':'); |
| 948 |
} |
| 949 |
} |
| 950 |
else if (strncmp(opt, "lzw", 3) == 0) |
| 951 |
{ |
| 952 |
char *cp = strchr(opt, ':'); |
| 953 |
if (cp) |
| 954 |
predictor = atoi(cp + 1); |
| 955 |
compression = COMPRESSION_LZW; |
| 956 |
} |
| 957 |
else if (strncmp(opt, "zip", 3) == 0) |
| 958 |
{ |
| 959 |
char *cp = strchr(opt, ':'); |
| 960 |
if (cp) |
| 961 |
predictor = atoi(cp + 1); |
| 962 |
compression = COMPRESSION_ADOBE_DEFLATE; |
| 963 |
} |
| 964 |
else |
| 965 |
return (0); |
| 966 |
return (1); |
| 967 |
} |
| 968 |
|
| 969 |
static char *stuff[] = { |
| 970 |
"bmp2tiff --- convert Windows BMP files to TIFF", |
| 971 |
"usage: bmp2tiff [options] input.bmp [input2.bmp ...] output.tif", |
| 972 |
"where options are:", |
| 973 |
" -r # make each strip have no more than # rows", |
| 974 |
"", |
| 975 |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding", |
| 976 |
" -c zip[:opts] compress output with deflate encoding", |
| 977 |
" -c jpeg[:opts]compress output with JPEG encoding", |
| 978 |
" -c packbits compress output with packbits encoding", |
| 979 |
" -c none use no compression algorithm on output", |
| 980 |
"", |
| 981 |
"JPEG options:", |
| 982 |
" # set compression quality level (0-100, default 75)", |
| 983 |
" r output color image as RGB rather than YCbCr", |
| 984 |
"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. " |
| 985 |
"quality", |
| 986 |
"", |
| 987 |
"LZW and deflate options:", |
| 988 |
" # set predictor value", |
| 989 |
"For example, -c lzw:2 to get LZW-encoded data with horizontal " |
| 990 |
"differencing", |
| 991 |
" -o out.tif write output to out.tif", |
| 992 |
" -h this help message", |
| 993 |
NULL}; |
| 994 |
|
| 995 |
static void usage(void) |
| 996 |
{ |
| 997 |
char buf[BUFSIZ]; |
| 998 |
int i; |
| 999 |
|
| 1000 |
setbuf(stderr, buf); |
| 1001 |
fprintf(stderr, "%s\n\n", TIFFGetVersion()); |
| 1002 |
for (i = 0; stuff[i] != NULL; i++) |
| 1003 |
fprintf(stderr, "%s\n", stuff[i]); |
| 1004 |
exit(-1); |
| 1005 |
} |