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