PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Extras/wxWidgets-2.9.0/src/tiff/tools/bmp2tiff.c

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