]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-tiff.c
Import cups.org releases
[thirdparty/cups.git] / filter / image-tiff.c
1 /*
2 * "$Id$"
3 *
4 * TIFF file routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2002 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * ImageReadTIFF() - Read a TIFF image file.
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include "image.h"
36
37 #ifdef HAVE_LIBTIFF
38 # include <tiff.h> /* TIFF image definitions */
39 # include <tiffio.h>
40 # include <unistd.h>
41
42
43 /*
44 * 'ImageReadTIFF()' - Read a TIFF image file.
45 */
46
47 int /* O - Read status */
48 ImageReadTIFF(image_t *img, /* IO - Image */
49 FILE *fp, /* I - Image file */
50 int primary, /* I - Primary choice for colorspace */
51 int secondary, /* I - Secondary choice for colorspace */
52 int saturation, /* I - Color saturation (%) */
53 int hue, /* I - Color hue (degrees) */
54 const ib_t *lut) /* I - Lookup table for gamma/brightness */
55 {
56 TIFF *tif; /* TIFF file */
57 uint32 width, height; /* Size of image */
58 uint16 photometric, /* Colorspace */
59 compression, /* Type of compression */
60 orientation, /* Orientation */
61 resunit, /* Units for resolution */
62 samples, /* Number of samples/pixel */
63 bits, /* Number of bits/pixel */
64 inkset, /* Ink set for color separations */
65 numinks; /* Number of inks in set */
66 float xres, /* Horizontal resolution */
67 yres; /* Vertical resolution */
68 uint16 *redcmap, /* Red colormap information */
69 *greencmap, /* Green colormap information */
70 *bluecmap; /* Blue colormap information */
71 int c, /* Color index */
72 num_colors, /* Number of colors */
73 bpp, /* Bytes per pixel */
74 x, y, /* Current x & y */
75 row, /* Current row in image */
76 xstart, ystart, /* Starting x & y */
77 xdir, ydir, /* X & y direction */
78 xcount, ycount, /* X & Y counters */
79 pstep, /* Pixel step (= bpp or -2 * bpp) */
80 scanwidth, /* Width of scanline */
81 r, g, b, k, /* Red, green, blue, and black values */
82 alpha; /* Image includes alpha? */
83 ib_t *in, /* Input buffer */
84 *out, /* Output buffer */
85 *p, /* Pointer into buffer */
86 *scanline, /* Scanline buffer */
87 *scanptr, /* Pointer into scanline buffer */
88 bit, /* Current bit */
89 pixel, /* Current pixel */
90 zero, /* Zero value (bitmaps) */
91 one; /* One value (bitmaps) */
92
93
94 /*
95 * Open the TIFF file and get the required parameters...
96 */
97
98 lseek(fileno(fp), 0, SEEK_SET); /* Work around "feature" in some stdio's */
99
100 if ((tif = TIFFFdOpen(fileno(fp), "", "r")) == NULL)
101 {
102 fputs("ERROR: TIFFFdOpen() failed!\n", stderr);
103 fclose(fp);
104 return (-1);
105 }
106
107 if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width))
108 {
109 fputs("ERROR: No image width tag in the file!\n", stderr);
110 TIFFClose(tif);
111 fclose(fp);
112 return (-1);
113 }
114
115 if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height))
116 {
117 fputs("ERROR: No image height tag in the file!\n", stderr);
118 TIFFClose(tif);
119 fclose(fp);
120 return (-1);
121 }
122
123 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric))
124 {
125 fputs("ERROR: No photometric tag in the file!\n", stderr);
126 TIFFClose(tif);
127 fclose(fp);
128 return (-1);
129 }
130
131 if (!TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression))
132 {
133 fputs("ERROR: No compression tag in the file!\n", stderr);
134 TIFFClose(tif);
135 fclose(fp);
136 return (-1);
137 }
138
139 if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples))
140 samples = 1;
141
142 if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bits))
143 bits = 1;
144
145 /*
146 * Get the image orientation...
147 */
148
149 if (!TIFFGetField(tif, TIFFTAG_ORIENTATION, &orientation))
150 orientation = 0;
151
152 /*
153 * Get the image resolution...
154 */
155
156 if (TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres) &&
157 TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres) &&
158 TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &resunit))
159 {
160 if (resunit == RESUNIT_INCH)
161 {
162 img->xppi = xres;
163 img->yppi = yres;
164 }
165 else if (resunit == RESUNIT_CENTIMETER)
166 {
167 img->xppi = xres * 2.54;
168 img->yppi = yres * 2.54;
169 }
170 else
171 {
172 img->xppi = 128;
173 img->yppi = 128;
174 }
175
176 fprintf(stderr, "DEBUG: TIFF resolution = %fx%f, units=%d\n",
177 xres, yres, resunit);
178 fprintf(stderr, "DEBUG: Stored resolution = %dx%d PPI\n",
179 img->xppi, img->yppi);
180 }
181
182 /*
183 * See if the image has an alpha channel...
184 */
185
186 if (samples == 2 || (samples == 4 && photometric == PHOTOMETRIC_RGB))
187 alpha = 1;
188 else
189 alpha = 0;
190
191 /*
192 * Setup the image size and colorspace...
193 */
194
195 img->xsize = width;
196 img->ysize = height;
197 if (photometric == PHOTOMETRIC_MINISBLACK ||
198 photometric == PHOTOMETRIC_MINISWHITE)
199 img->colorspace = secondary;
200 else if (photometric == PHOTOMETRIC_SEPARATED && primary == IMAGE_RGB_CMYK)
201 img->colorspace = IMAGE_CMYK;
202 else if (primary == IMAGE_RGB_CMYK)
203 img->colorspace = IMAGE_RGB;
204 else
205 img->colorspace = primary;
206
207 fprintf(stderr, "DEBUG: img->colorspace = %d\n", img->colorspace);
208
209 bpp = ImageGetDepth(img);
210
211 ImageSetMaxTiles(img, 0);
212
213 /*
214 * Set the X & Y start and direction according to the image orientation...
215 */
216
217 switch (orientation)
218 {
219 case ORIENTATION_TOPRIGHT :
220 fputs("DEBUG: orientation = top-right\n", stderr);
221 break;
222 case ORIENTATION_RIGHTTOP :
223 fputs("DEBUG: orientation = right-top\n", stderr);
224 break;
225 default :
226 case ORIENTATION_TOPLEFT :
227 fputs("DEBUG: orientation = top-left\n", stderr);
228 break;
229 case ORIENTATION_LEFTTOP :
230 fputs("DEBUG: orientation = left-top\n", stderr);
231 break;
232 case ORIENTATION_BOTLEFT :
233 fputs("DEBUG: orientation = bottom-left\n", stderr);
234 break;
235 case ORIENTATION_LEFTBOT :
236 fputs("DEBUG: orientation = left-bottom\n", stderr);
237 break;
238 case ORIENTATION_BOTRIGHT :
239 fputs("DEBUG: orientation = bottom-right\n", stderr);
240 break;
241 case ORIENTATION_RIGHTBOT :
242 fputs("DEBUG: orientation = right-bottom\n", stderr);
243 break;
244 }
245
246 switch (orientation)
247 {
248 case ORIENTATION_TOPRIGHT :
249 case ORIENTATION_RIGHTTOP :
250 xstart = img->xsize - 1;
251 xdir = -1;
252 ystart = 0;
253 ydir = 1;
254 break;
255 default :
256 case ORIENTATION_TOPLEFT :
257 case ORIENTATION_LEFTTOP :
258 xstart = 0;
259 xdir = 1;
260 ystart = 0;
261 ydir = 1;
262 break;
263 case ORIENTATION_BOTLEFT :
264 case ORIENTATION_LEFTBOT :
265 xstart = 0;
266 xdir = 1;
267 ystart = img->ysize - 1;
268 ydir = -1;
269 break;
270 case ORIENTATION_BOTRIGHT :
271 case ORIENTATION_RIGHTBOT :
272 xstart = img->xsize - 1;
273 xdir = -1;
274 ystart = img->ysize - 1;
275 ydir = -1;
276 break;
277 }
278
279 /*
280 * Allocate a scanline buffer...
281 */
282
283 scanwidth = TIFFScanlineSize(tif);
284 scanline = _TIFFmalloc(scanwidth);
285
286 /*
287 * Allocate input and output buffers...
288 */
289
290 if (orientation < ORIENTATION_LEFTTOP)
291 {
292 if (samples > 1 || photometric == PHOTOMETRIC_PALETTE)
293 pstep = xdir * 3;
294 else
295 pstep = xdir;
296
297 in = malloc(img->xsize * 3 + 3);
298 out = malloc(img->xsize * bpp);
299 }
300 else
301 {
302 if (samples > 1 || photometric == PHOTOMETRIC_PALETTE)
303 pstep = ydir * 3;
304 else
305 pstep = ydir;
306
307 in = malloc(img->ysize * 3 + 3);
308 out = malloc(img->ysize * bpp);
309 }
310
311 /*
312 * Read the image. This is greatly complicated by the fact that TIFF
313 * supports literally hundreds of different colorspaces and orientations,
314 * each which must be handled separately...
315 */
316
317 fprintf(stderr, "DEBUG: photometric = %d\n", photometric);
318 fprintf(stderr, "DEBUG: compression = %d\n", compression);
319
320 switch (photometric)
321 {
322 case PHOTOMETRIC_MINISWHITE :
323 case PHOTOMETRIC_MINISBLACK :
324 if (photometric == PHOTOMETRIC_MINISWHITE)
325 {
326 zero = 255;
327 one = 0;
328 }
329 else
330 {
331 zero = 0;
332 one = 255;
333 }
334
335 if (orientation < ORIENTATION_LEFTTOP)
336 {
337 /*
338 * Row major order...
339 */
340
341 for (y = ystart, ycount = img->ysize, row = 0;
342 ycount > 0;
343 ycount --, y += ydir, row ++)
344 {
345 if (bits == 1)
346 {
347 TIFFReadScanline(tif, scanline, row, 0);
348 for (xcount = img->xsize, scanptr = scanline, p = in + xstart, bit = 128;
349 xcount > 0;
350 xcount --, p += pstep)
351 {
352 if (*scanptr & bit)
353 *p = one;
354 else
355 *p = zero;
356
357 if (bit > 1)
358 bit >>= 1;
359 else
360 {
361 bit = 128;
362 scanptr ++;
363 }
364 }
365 }
366 else if (bits == 2)
367 {
368 TIFFReadScanline(tif, scanline, row, 0);
369 for (xcount = img->xsize, scanptr = scanline, p = in + xstart, bit = 0xc0;
370 xcount > 0;
371 xcount --, p += pstep)
372 {
373 pixel = *scanptr & bit;
374 while (pixel > 3)
375 pixel >>= 2;
376 *p = (255 * pixel / 3) ^ zero;
377
378 if (bit > 3)
379 bit >>= 2;
380 else
381 {
382 bit = 0xc0;
383 scanptr ++;
384 }
385 }
386 }
387 else if (bits == 4)
388 {
389 TIFFReadScanline(tif, scanline, row, 0);
390 for (xcount = img->xsize, scanptr = scanline, p = in + xstart, bit = 0xf0;
391 xcount > 0;
392 xcount --, p += pstep)
393 {
394 if (bit == 0xf0)
395 {
396 *p = (255 * ((*scanptr & 0xf0) >> 4) / 15) ^ zero;
397 bit = 0x0f;
398 }
399 else
400 {
401 *p = (255 * (*scanptr & 0x0f) / 15) ^ zero;
402 bit = 0xf0;
403 scanptr ++;
404 }
405 }
406 }
407 else if (xdir < 0 || zero || alpha)
408 {
409 TIFFReadScanline(tif, scanline, row, 0);
410
411 if (alpha)
412 {
413 if (zero)
414 {
415 for (xcount = img->xsize, p = in + xstart, scanptr = scanline;
416 xcount > 0;
417 xcount --, p += pstep, scanptr += 2)
418 *p = (scanptr[1] * (255 - scanptr[0]) +
419 (255 - scanptr[1]) * 255) / 255;
420 }
421 else
422 {
423 for (xcount = img->xsize, p = in + xstart, scanptr = scanline;
424 xcount > 0;
425 xcount --, p += pstep, scanptr += 2)
426 *p = (scanptr[1] * scanptr[0] +
427 (255 - scanptr[1]) * 255) / 255;
428 }
429 }
430 else
431 {
432 if (zero)
433 {
434 for (xcount = img->xsize, p = in + xstart, scanptr = scanline;
435 xcount > 0;
436 xcount --, p += pstep, scanptr ++)
437 *p = 255 - *scanptr;
438 }
439 else
440 {
441 for (xcount = img->xsize, p = in + xstart, scanptr = scanline;
442 xcount > 0;
443 xcount --, p += pstep, scanptr ++)
444 *p = *scanptr;
445 }
446 }
447 }
448 else
449 TIFFReadScanline(tif, in, row, 0);
450
451 if (img->colorspace == IMAGE_WHITE)
452 {
453 if (lut)
454 ImageLut(in, img->xsize, lut);
455
456 ImagePutRow(img, 0, y, img->xsize, in);
457 }
458 else
459 {
460 switch (img->colorspace)
461 {
462 case IMAGE_RGB :
463 ImageWhiteToRGB(in, out, img->xsize);
464 break;
465 case IMAGE_BLACK :
466 ImageWhiteToBlack(in, out, img->xsize);
467 break;
468 case IMAGE_CMY :
469 ImageWhiteToCMY(in, out, img->xsize);
470 break;
471 case IMAGE_CMYK :
472 ImageWhiteToCMYK(in, out, img->xsize);
473 break;
474 }
475
476 if (lut)
477 ImageLut(out, img->xsize * bpp, lut);
478
479 ImagePutRow(img, 0, y, img->xsize, out);
480 }
481 }
482 }
483 else
484 {
485 /*
486 * Column major order...
487 */
488
489 for (x = xstart, xcount = img->xsize, row = 0;
490 xcount > 0;
491 xcount --, x += xdir, row ++)
492 {
493 if (bits == 1)
494 {
495 TIFFReadScanline(tif, scanline, row, 0);
496 for (ycount = img->ysize, scanptr = scanline, p = in + ystart, bit = 128;
497 ycount > 0;
498 ycount --, p += ydir)
499 {
500 if (*scanptr & bit)
501 *p = one;
502 else
503 *p = zero;
504
505 if (bit > 1)
506 bit >>= 1;
507 else
508 {
509 bit = 128;
510 scanptr ++;
511 }
512 }
513 }
514 else if (bits == 2)
515 {
516 TIFFReadScanline(tif, scanline, row, 0);
517 for (ycount = img->ysize, scanptr = scanline, p = in + ystart, bit = 0xc0;
518 ycount > 0;
519 ycount --, p += ydir)
520 {
521 pixel = *scanptr & 0xc0;
522 while (pixel > 3)
523 pixel >>= 2;
524
525 *p = (255 * pixel / 3) ^ zero;
526
527 if (bit > 3)
528 bit >>= 2;
529 else
530 {
531 bit = 0xc0;
532 scanptr ++;
533 }
534 }
535 }
536 else if (bits == 4)
537 {
538 TIFFReadScanline(tif, scanline, row, 0);
539 for (ycount = img->ysize, scanptr = scanline, p = in + ystart, bit = 0xf0;
540 ycount > 0;
541 ycount --, p += ydir)
542 {
543 if (bit == 0xf0)
544 {
545 *p = (255 * ((*scanptr & 0xf0) >> 4) / 15) ^ zero;
546 bit = 0x0f;
547 }
548 else
549 {
550 *p = (255 * (*scanptr & 0x0f) / 15) ^ zero;
551 bit = 0xf0;
552 scanptr ++;
553 }
554 }
555 }
556 else if (ydir < 0 || zero || alpha)
557 {
558 TIFFReadScanline(tif, scanline, row, 0);
559
560 if (alpha)
561 {
562 if (zero)
563 {
564 for (ycount = img->ysize, p = in + ystart, scanptr = scanline;
565 ycount > 0;
566 ycount --, p += ydir, scanptr += 2)
567 *p = (scanptr[1] * (255 - scanptr[0]) +
568 (255 - scanptr[1]) * 255) / 255;
569 }
570 else
571 {
572 for (ycount = img->ysize, p = in + ystart, scanptr = scanline;
573 ycount > 0;
574 ycount --, p += ydir, scanptr += 2)
575 *p = (scanptr[1] * scanptr[0] +
576 (255 - scanptr[1]) * 255) / 255;
577 }
578 }
579 else
580 {
581 if (zero)
582 {
583 for (ycount = img->ysize, p = in + ystart, scanptr = scanline;
584 ycount > 0;
585 ycount --, p += ydir, scanptr ++)
586 *p = 255 - *scanptr;
587 }
588 else
589 {
590 for (ycount = img->ysize, p = in + ystart, scanptr = scanline;
591 ycount > 0;
592 ycount --, p += ydir, scanptr ++)
593 *p = *scanptr;
594 }
595 }
596 }
597 else
598 TIFFReadScanline(tif, in, row, 0);
599
600 if (img->colorspace == IMAGE_WHITE)
601 {
602 if (lut)
603 ImageLut(in, img->ysize, lut);
604
605 ImagePutCol(img, x, 0, img->ysize, in);
606 }
607 else
608 {
609 switch (img->colorspace)
610 {
611 case IMAGE_RGB :
612 ImageWhiteToRGB(in, out, img->ysize);
613 break;
614 case IMAGE_BLACK :
615 ImageWhiteToBlack(in, out, img->ysize);
616 break;
617 case IMAGE_CMY :
618 ImageWhiteToCMY(in, out, img->ysize);
619 break;
620 case IMAGE_CMYK :
621 ImageWhiteToCMYK(in, out, img->ysize);
622 break;
623 }
624
625 if (lut)
626 ImageLut(out, img->ysize * bpp, lut);
627
628 ImagePutCol(img, x, 0, img->ysize, out);
629 }
630 }
631 }
632 break;
633
634 case PHOTOMETRIC_PALETTE :
635 if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &redcmap, &greencmap, &bluecmap))
636 {
637 fputs("ERROR: No colormap tag in the file!\n", stderr);
638 fclose(fp);
639 return (-1);
640 }
641
642 num_colors = 1 << bits;
643
644 for (c = 0; c < num_colors; c ++)
645 {
646 redcmap[c] >>= 8;
647 greencmap[c] >>= 8;
648 bluecmap[c] >>= 8;
649 }
650
651 if (orientation < ORIENTATION_LEFTTOP)
652 {
653 /*
654 * Row major order...
655 */
656
657 for (y = ystart, ycount = img->ysize, row = 0;
658 ycount > 0;
659 ycount --, y += ydir, row ++)
660 {
661 if (bits == 1)
662 {
663 TIFFReadScanline(tif, scanline, row, 0);
664 for (xcount = img->xsize, scanptr = scanline,
665 p = in + xstart * 3, bit = 128;
666 xcount > 0;
667 xcount --, p += pstep)
668 {
669 if (*scanptr & bit)
670 {
671 p[0] = redcmap[1];
672 p[1] = greencmap[1];
673 p[2] = bluecmap[1];
674 }
675 else
676 {
677 p[0] = redcmap[0];
678 p[1] = greencmap[0];
679 p[2] = bluecmap[0];
680 }
681
682 if (bit > 1)
683 bit >>= 1;
684 else
685 {
686 bit = 128;
687 scanptr ++;
688 }
689 }
690 }
691 else if (bits == 2)
692 {
693 TIFFReadScanline(tif, scanline, row, 0);
694 for (xcount = img->xsize, scanptr = scanline,
695 p = in + xstart * 3, bit = 0xc0;
696 xcount > 0;
697 xcount --, p += pstep)
698 {
699 pixel = *scanptr & bit;
700 while (pixel > 3)
701 pixel >>= 2;
702
703 p[0] = redcmap[pixel];
704 p[1] = greencmap[pixel];
705 p[2] = bluecmap[pixel];
706
707 if (bit > 3)
708 bit >>= 2;
709 else
710 {
711 bit = 0xc0;
712 scanptr ++;
713 }
714 }
715 }
716 else if (bits == 4)
717 {
718 TIFFReadScanline(tif, scanline, row, 0);
719 for (xcount = img->xsize, scanptr = scanline,
720 p = in + 3 * xstart, bit = 0xf0;
721 xcount > 0;
722 xcount --, p += pstep)
723 {
724 if (bit == 0xf0)
725 {
726 pixel = (*scanptr & 0xf0) >> 4;
727 p[0] = redcmap[pixel];
728 p[1] = greencmap[pixel];
729 p[2] = bluecmap[pixel];
730 bit = 0x0f;
731 }
732 else
733 {
734 pixel = *scanptr++ & 0x0f;
735 p[0] = redcmap[pixel];
736 p[1] = greencmap[pixel];
737 p[2] = bluecmap[pixel];
738 bit = 0xf0;
739 }
740 }
741 }
742 else
743 {
744 TIFFReadScanline(tif, scanline, row, 0);
745
746 for (xcount = img->xsize, p = in + 3 * xstart, scanptr = scanline;
747 xcount > 0;
748 xcount --, p += pstep)
749 {
750 p[0] = redcmap[*scanptr];
751 p[1] = greencmap[*scanptr];
752 p[2] = bluecmap[*scanptr++];
753 }
754 }
755
756 if (img->colorspace == IMAGE_RGB)
757 {
758 if (lut)
759 ImageLut(in, img->xsize * 3, lut);
760
761 ImagePutRow(img, 0, y, img->xsize, in);
762 }
763 else
764 {
765 switch (img->colorspace)
766 {
767 case IMAGE_WHITE :
768 ImageRGBToWhite(in, out, img->xsize);
769 break;
770 case IMAGE_BLACK :
771 ImageRGBToBlack(in, out, img->xsize);
772 break;
773 case IMAGE_CMY :
774 ImageRGBToCMY(in, out, img->xsize);
775 break;
776 case IMAGE_CMYK :
777 ImageRGBToCMYK(in, out, img->xsize);
778 break;
779 }
780
781 if (lut)
782 ImageLut(out, img->xsize * bpp, lut);
783
784 ImagePutRow(img, 0, y, img->xsize, out);
785 }
786 }
787 }
788 else
789 {
790 /*
791 * Column major order...
792 */
793
794 for (x = xstart, xcount = img->xsize, row = 0;
795 xcount > 0;
796 xcount --, x += xdir, row ++)
797 {
798 if (bits == 1)
799 {
800 TIFFReadScanline(tif, scanline, row, 0);
801 for (ycount = img->ysize, scanptr = scanline,
802 p = in + 3 * ystart, bit = 128;
803 ycount > 0;
804 ycount --, p += ydir)
805 {
806 if (*scanptr & bit)
807 {
808 p[0] = redcmap[1];
809 p[1] = greencmap[1];
810 p[2] = bluecmap[1];
811 }
812 else
813 {
814 p[0] = redcmap[0];
815 p[1] = greencmap[0];
816 p[2] = bluecmap[0];
817 }
818
819 if (bit > 1)
820 bit >>= 1;
821 else
822 {
823 bit = 128;
824 scanptr ++;
825 }
826 }
827 }
828 else if (bits == 2)
829 {
830 TIFFReadScanline(tif, scanline, row, 0);
831 for (ycount = img->ysize, scanptr = scanline,
832 p = in + 3 * ystart, bit = 0xc0;
833 ycount > 0;
834 ycount --, p += ydir)
835 {
836 pixel = *scanptr & 0xc0;
837 while (pixel > 3)
838 pixel >>= 2;
839
840 p[0] = redcmap[pixel];
841 p[1] = greencmap[pixel];
842 p[2] = bluecmap[pixel];
843
844 if (bit > 3)
845 bit >>= 2;
846 else
847 {
848 bit = 0xc0;
849 scanptr ++;
850 }
851 }
852 }
853 else if (bits == 4)
854 {
855 TIFFReadScanline(tif, scanline, row, 0);
856 for (ycount = img->ysize, scanptr = scanline,
857 p = in + 3 * ystart, bit = 0xf0;
858 ycount > 0;
859 ycount --, p += ydir)
860 {
861 if (bit == 0xf0)
862 {
863 pixel = (*scanptr & 0xf0) >> 4;
864 p[0] = redcmap[pixel];
865 p[1] = greencmap[pixel];
866 p[2] = bluecmap[pixel];
867 bit = 0x0f;
868 }
869 else
870 {
871 pixel = *scanptr++ & 0x0f;
872 p[0] = redcmap[pixel];
873 p[1] = greencmap[pixel];
874 p[2] = bluecmap[pixel];
875 bit = 0xf0;
876 }
877 }
878 }
879 else
880 {
881 TIFFReadScanline(tif, scanline, row, 0);
882
883 for (ycount = img->ysize, p = in + 3 * ystart, scanptr = scanline;
884 ycount > 0;
885 ycount --, p += ydir)
886 {
887 p[0] = redcmap[*scanptr];
888 p[1] = greencmap[*scanptr];
889 p[2] = bluecmap[*scanptr++];
890 }
891 }
892
893 if (img->colorspace == IMAGE_RGB)
894 {
895 if (lut)
896 ImageLut(in, img->ysize * 3, lut);
897
898 ImagePutCol(img, x, 0, img->ysize, in);
899 }
900 else
901 {
902 switch (img->colorspace)
903 {
904 case IMAGE_WHITE :
905 ImageRGBToWhite(in, out, img->ysize);
906 break;
907 case IMAGE_BLACK :
908 ImageRGBToBlack(in, out, img->ysize);
909 break;
910 case IMAGE_CMY :
911 ImageRGBToCMY(in, out, img->ysize);
912 break;
913 case IMAGE_CMYK :
914 ImageRGBToCMYK(in, out, img->ysize);
915 break;
916 }
917
918 if (lut)
919 ImageLut(out, img->ysize * bpp, lut);
920
921 ImagePutCol(img, x, 0, img->ysize, out);
922 }
923 }
924 }
925 break;
926
927 case PHOTOMETRIC_RGB :
928 if (orientation < ORIENTATION_LEFTTOP)
929 {
930 /*
931 * Row major order...
932 */
933
934 for (y = ystart, ycount = img->ysize, row = 0;
935 ycount > 0;
936 ycount --, y += ydir, row ++)
937 {
938 if (bits == 1)
939 {
940 TIFFReadScanline(tif, scanline, row, 0);
941 for (xcount = img->xsize, scanptr = scanline, p = in + xstart * 3, bit = 0xf0;
942 xcount > 0;
943 xcount --, p += pstep)
944 {
945 if (*scanptr & bit & 0x88)
946 p[0] = 255;
947 else
948 p[0] = 0;
949
950 if (*scanptr & bit & 0x44)
951 p[1] = 255;
952 else
953 p[1] = 0;
954
955 if (*scanptr & bit & 0x22)
956 p[2] = 255;
957 else
958 p[2] = 0;
959
960 if (bit == 0xf0)
961 bit = 0x0f;
962 else
963 {
964 bit = 0xf0;
965 scanptr ++;
966 }
967 }
968 }
969 else if (bits == 2)
970 {
971 TIFFReadScanline(tif, scanline, row, 0);
972 for (xcount = img->xsize, scanptr = scanline, p = in + xstart * 3;
973 xcount > 0;
974 xcount --, p += pstep, scanptr ++)
975 {
976 pixel = *scanptr >> 2;
977 p[0] = 255 * (pixel & 3) / 3;
978 pixel >>= 2;
979 p[1] = 255 * (pixel & 3) / 3;
980 pixel >>= 2;
981 p[2] = 255 * (pixel & 3) / 3;
982 }
983 }
984 else if (bits == 4)
985 {
986 TIFFReadScanline(tif, scanline, row, 0);
987 for (xcount = img->xsize, scanptr = scanline, p = in + xstart * 3;
988 xcount > 0;
989 xcount -= 2, p += 2 * pstep, scanptr += 3)
990 {
991 pixel = scanptr[0];
992 p[1] = 255 * (pixel & 15) / 15;
993 pixel >>= 4;
994 p[0] = 255 * (pixel & 15) / 15;
995 pixel = scanptr[1];
996 p[2] = 255 * ((pixel >> 4) & 15) / 15;
997
998 if (xcount > 1)
999 {
1000 p[pstep + 0] = 255 * (pixel & 15) / 15;
1001 pixel = scanptr[2];
1002 p[pstep + 2] = 255 * (pixel & 15) / 15;
1003 pixel >>= 4;
1004 p[pstep + 1] = 255 * (pixel & 15) / 15;
1005 }
1006 }
1007 }
1008 else if (xdir < 0 || alpha)
1009 {
1010 TIFFReadScanline(tif, scanline, row, 0);
1011
1012 if (alpha)
1013 {
1014 for (xcount = img->xsize, p = in + xstart * 3, scanptr = scanline;
1015 xcount > 0;
1016 xcount --, p += pstep, scanptr += 4)
1017 {
1018 p[0] = (scanptr[0] * scanptr[3] + 255 * (255 - scanptr[3])) / 255;
1019 p[1] = (scanptr[1] * scanptr[3] + 255 * (255 - scanptr[3])) / 255;
1020 p[2] = (scanptr[2] * scanptr[3] + 255 * (255 - scanptr[3])) / 255;
1021 }
1022 }
1023 else
1024 {
1025 for (xcount = img->xsize, p = in + xstart * 3, scanptr = scanline;
1026 xcount > 0;
1027 xcount --, p += pstep, scanptr += 3)
1028 {
1029 p[0] = scanptr[0];
1030 p[1] = scanptr[1];
1031 p[2] = scanptr[2];
1032 }
1033 }
1034 }
1035 else
1036 TIFFReadScanline(tif, in, row, 0);
1037
1038 if ((saturation != 100 || hue != 0) && bpp > 1)
1039 ImageRGBAdjust(in, img->xsize, saturation, hue);
1040
1041 if (img->colorspace == IMAGE_RGB)
1042 {
1043 if (lut)
1044 ImageLut(in, img->xsize * 3, lut);
1045
1046 ImagePutRow(img, 0, y, img->xsize, in);
1047 }
1048 else
1049 {
1050 switch (img->colorspace)
1051 {
1052 case IMAGE_WHITE :
1053 ImageRGBToWhite(in, out, img->xsize);
1054 break;
1055 case IMAGE_BLACK :
1056 ImageRGBToBlack(in, out, img->xsize);
1057 break;
1058 case IMAGE_CMY :
1059 ImageRGBToCMY(in, out, img->xsize);
1060 break;
1061 case IMAGE_CMYK :
1062 ImageRGBToCMYK(in, out, img->xsize);
1063 break;
1064 }
1065
1066 if (lut)
1067 ImageLut(out, img->xsize * bpp, lut);
1068
1069 ImagePutRow(img, 0, y, img->xsize, out);
1070 }
1071 }
1072 }
1073 else
1074 {
1075 /*
1076 * Column major order...
1077 */
1078
1079 for (x = xstart, xcount = img->xsize, row = 0;
1080 xcount > 0;
1081 xcount --, x += xdir, row ++)
1082 {
1083 if (bits == 1)
1084 {
1085 TIFFReadScanline(tif, scanline, row, 0);
1086 for (ycount = img->ysize, scanptr = scanline, p = in + ystart * 3, bit = 0xf0;
1087 ycount > 0;
1088 ycount --, p += pstep)
1089 {
1090 if (*scanptr & bit & 0x88)
1091 p[0] = 255;
1092 else
1093 p[0] = 0;
1094
1095 if (*scanptr & bit & 0x44)
1096 p[1] = 255;
1097 else
1098 p[1] = 0;
1099
1100 if (*scanptr & bit & 0x22)
1101 p[2] = 255;
1102 else
1103 p[2] = 0;
1104
1105 if (bit == 0xf0)
1106 bit = 0x0f;
1107 else
1108 {
1109 bit = 0xf0;
1110 scanptr ++;
1111 }
1112 }
1113 }
1114 else if (bits == 2)
1115 {
1116 TIFFReadScanline(tif, scanline, row, 0);
1117 for (ycount = img->ysize, scanptr = scanline, p = in + ystart * 3;
1118 ycount > 0;
1119 ycount --, p += pstep, scanptr ++)
1120 {
1121 pixel = *scanptr >> 2;
1122 p[0] = 255 * (pixel & 3) / 3;
1123 pixel >>= 2;
1124 p[1] = 255 * (pixel & 3) / 3;
1125 pixel >>= 2;
1126 p[2] = 255 * (pixel & 3) / 3;
1127 }
1128 }
1129 else if (bits == 4)
1130 {
1131 TIFFReadScanline(tif, scanline, row, 0);
1132 for (ycount = img->ysize, scanptr = scanline, p = in + ystart * 3;
1133 ycount > 0;
1134 ycount -= 2, p += 2 * pstep, scanptr += 3)
1135 {
1136 pixel = scanptr[0];
1137 p[1] = 255 * (pixel & 15) / 15;
1138 pixel >>= 4;
1139 p[0] = 255 * (pixel & 15) / 15;
1140 pixel = scanptr[1];
1141 p[2] = 255 * ((pixel >> 4) & 15) / 15;
1142
1143 if (ycount > 1)
1144 {
1145 p[pstep + 0] = 255 * (pixel & 15) / 15;
1146 pixel = scanptr[2];
1147 p[pstep + 2] = 255 * (pixel & 15) / 15;
1148 pixel >>= 4;
1149 p[pstep + 1] = 255 * (pixel & 15) / 15;
1150 }
1151 }
1152 }
1153 else if (ydir < 0 || alpha)
1154 {
1155 TIFFReadScanline(tif, scanline, row, 0);
1156
1157 if (alpha)
1158 {
1159 for (ycount = img->ysize, p = in + ystart * 3, scanptr = scanline;
1160 ycount > 0;
1161 ycount --, p += pstep, scanptr += 4)
1162 {
1163 p[0] = (scanptr[0] * scanptr[3] + 255 * (255 - scanptr[3])) / 255;
1164 p[1] = (scanptr[1] * scanptr[3] + 255 * (255 - scanptr[3])) / 255;
1165 p[2] = (scanptr[2] * scanptr[3] + 255 * (255 - scanptr[3])) / 255;
1166 }
1167 }
1168 else
1169 {
1170 for (ycount = img->ysize, p = in + ystart * 3, scanptr = scanline;
1171 ycount > 0;
1172 ycount --, p += pstep, scanptr += 3)
1173 {
1174 p[0] = scanptr[0];
1175 p[1] = scanptr[1];
1176 p[2] = scanptr[2];
1177 }
1178 }
1179 }
1180 else
1181 TIFFReadScanline(tif, in, row, 0);
1182
1183 if ((saturation != 100 || hue != 0) && bpp > 1)
1184 ImageRGBAdjust(in, img->ysize, saturation, hue);
1185
1186 if (img->colorspace == IMAGE_RGB)
1187 {
1188 if (lut)
1189 ImageLut(in, img->ysize * 3, lut);
1190
1191 ImagePutCol(img, x, 0, img->ysize, in);
1192 }
1193 else
1194 {
1195 switch (img->colorspace)
1196 {
1197 case IMAGE_WHITE :
1198 ImageRGBToWhite(in, out, img->ysize);
1199 break;
1200 case IMAGE_BLACK :
1201 ImageRGBToBlack(in, out, img->ysize);
1202 break;
1203 case IMAGE_CMY :
1204 ImageRGBToCMY(in, out, img->ysize);
1205 break;
1206 case IMAGE_CMYK :
1207 ImageRGBToCMYK(in, out, img->ysize);
1208 break;
1209 }
1210
1211 if (lut)
1212 ImageLut(out, img->ysize * bpp, lut);
1213
1214 ImagePutCol(img, x, 0, img->ysize, out);
1215 }
1216 }
1217 }
1218 break;
1219
1220 case PHOTOMETRIC_SEPARATED :
1221 inkset = INKSET_CMYK;
1222 numinks = 4;
1223
1224 #ifdef TIFFTAG_NUMBEROFINKS
1225 if (!TIFFGetField(tif, TIFFTAG_INKSET, &inkset) &&
1226 !TIFFGetField(tif, TIFFTAG_NUMBEROFINKS, &numinks))
1227 #else
1228 if (!TIFFGetField(tif, TIFFTAG_INKSET, &inkset))
1229 #endif /* TIFFTAG_NUMBEROFINKS */
1230 {
1231 fputs("WARNING: No inkset or number-of-inks tag in the file!\n", stderr);
1232 }
1233
1234 if (inkset == INKSET_CMYK || numinks == 4)
1235 {
1236 if (orientation < ORIENTATION_LEFTTOP)
1237 {
1238 /*
1239 * Row major order...
1240 */
1241
1242 for (y = ystart, ycount = img->ysize, row = 0;
1243 ycount > 0;
1244 ycount --, y += ydir, row ++)
1245 {
1246 if (bits == 1)
1247 {
1248 TIFFReadScanline(tif, scanline, row, 0);
1249 for (xcount = img->xsize, scanptr = scanline, p = in + xstart * 3, bit = 0xf0;
1250 xcount > 0;
1251 xcount --, p += pstep)
1252 {
1253 if (*scanptr & bit & 0x11)
1254 {
1255 p[0] = 0;
1256 p[1] = 0;
1257 p[2] = 0;
1258 }
1259 else
1260 {
1261 if (*scanptr & bit & 0x88)
1262 p[0] = 0;
1263 else
1264 p[0] = 255;
1265
1266 if (*scanptr & bit & 0x44)
1267 p[1] = 0;
1268 else
1269 p[1] = 255;
1270
1271 if (*scanptr & bit & 0x22)
1272 p[2] = 0;
1273 else
1274 p[2] = 255;
1275 }
1276
1277 if (bit == 0xf0)
1278 bit = 0x0f;
1279 else
1280 {
1281 bit = 0xf0;
1282 scanptr ++;
1283 }
1284 }
1285 }
1286 else if (bits == 2)
1287 {
1288 TIFFReadScanline(tif, scanline, row, 0);
1289 for (xcount = img->xsize, scanptr = scanline, p = in + xstart * 3;
1290 xcount > 0;
1291 xcount --, p += pstep, scanptr ++)
1292 {
1293 pixel = *scanptr;
1294 k = 255 * (pixel & 3) / 3;
1295 if (k == 255)
1296 {
1297 p[0] = 0;
1298 p[1] = 0;
1299 p[2] = 0;
1300 }
1301 else
1302 {
1303 pixel >>= 2;
1304 b = 255 - 255 * (pixel & 3) / 3 - k;
1305 if (b < 0)
1306 p[2] = 0;
1307 else if (b < 256)
1308 p[2] = b;
1309 else
1310 p[2] = 255;
1311
1312 pixel >>= 2;
1313 g = 255 - 255 * (pixel & 3) / 3 - k;
1314 if (g < 0)
1315 p[1] = 0;
1316 else if (g < 256)
1317 p[1] = g;
1318 else
1319 p[1] = 255;
1320
1321 pixel >>= 2;
1322 r = 255 - 255 * (pixel & 3) / 3 - k;
1323 if (r < 0)
1324 p[0] = 0;
1325 else if (r < 256)
1326 p[0] = r;
1327 else
1328 p[0] = 255;
1329 }
1330 }
1331 }
1332 else if (bits == 4)
1333 {
1334 TIFFReadScanline(tif, scanline, row, 0);
1335 for (xcount = img->xsize, scanptr = scanline, p = in + xstart * 3;
1336 xcount > 0;
1337 xcount --, p += pstep, scanptr += 2)
1338 {
1339 pixel = scanptr[1];
1340 k = 255 * (pixel & 15) / 15;
1341 if (k == 255)
1342 {
1343 p[0] = 0;
1344 p[1] = 0;
1345 p[2] = 0;
1346 }
1347 else
1348 {
1349 pixel >>= 4;
1350 b = 255 - 255 * (pixel & 15) / 15 - k;
1351 if (b < 0)
1352 p[2] = 0;
1353 else if (b < 256)
1354 p[2] = b;
1355 else
1356 p[2] = 255;
1357
1358 pixel = scanptr[0];
1359 g = 255 - 255 * (pixel & 15) / 15 - k;
1360 if (g < 0)
1361 p[1] = 0;
1362 else if (g < 256)
1363 p[1] = g;
1364 else
1365 p[1] = 255;
1366
1367 pixel >>= 4;
1368 r = 255 - 255 * (pixel & 15) / 15 - k;
1369 if (r < 0)
1370 p[0] = 0;
1371 else if (r < 256)
1372 p[0] = r;
1373 else
1374 p[0] = 255;
1375 }
1376 }
1377 }
1378 else if (img->colorspace == IMAGE_CMYK)
1379 {
1380 TIFFReadScanline(tif, scanline, row, 0);
1381 ImagePutRow(img, 0, y, img->xsize, scanline);
1382 }
1383 else
1384 {
1385 TIFFReadScanline(tif, scanline, row, 0);
1386
1387 for (xcount = img->xsize, p = in + xstart * 3, scanptr = scanline;
1388 xcount > 0;
1389 xcount --, p += pstep, scanptr += 4)
1390 {
1391 k = scanptr[3];
1392 if (k == 255)
1393 {
1394 p[0] = 0;
1395 p[1] = 0;
1396 p[2] = 0;
1397 }
1398 else
1399 {
1400 r = 255 - scanptr[0] - k;
1401 if (r < 0)
1402 p[0] = 0;
1403 else if (r < 256)
1404 p[0] = r;
1405 else
1406 p[0] = 255;
1407
1408 g = 255 - scanptr[1] - k;
1409 if (g < 0)
1410 p[1] = 0;
1411 else if (g < 256)
1412 p[1] = g;
1413 else
1414 p[1] = 255;
1415
1416 b = 255 - scanptr[2] - k;
1417 if (b < 0)
1418 p[2] = 0;
1419 else if (b < 256)
1420 p[2] = b;
1421 else
1422 p[2] = 255;
1423 }
1424 }
1425 }
1426
1427 if ((saturation != 100 || hue != 0) && bpp > 1)
1428 ImageRGBAdjust(in, img->xsize, saturation, hue);
1429
1430 if (img->colorspace == IMAGE_RGB)
1431 {
1432 if (lut)
1433 ImageLut(in, img->xsize * 3, lut);
1434
1435 ImagePutRow(img, 0, y, img->xsize, in);
1436 }
1437 else if (img->colorspace == IMAGE_WHITE)
1438 {
1439 switch (img->colorspace)
1440 {
1441 case IMAGE_WHITE :
1442 ImageRGBToWhite(in, out, img->xsize);
1443 break;
1444 case IMAGE_BLACK :
1445 ImageRGBToBlack(in, out, img->xsize);
1446 break;
1447 case IMAGE_CMY :
1448 ImageRGBToCMY(in, out, img->xsize);
1449 break;
1450 case IMAGE_CMYK :
1451 ImageRGBToCMYK(in, out, img->xsize);
1452 break;
1453 }
1454
1455 if (lut)
1456 ImageLut(out, img->xsize * 3, lut);
1457
1458 ImagePutRow(img, 0, y, img->xsize, out);
1459 }
1460 }
1461 }
1462 else
1463 {
1464 /*
1465 * Column major order...
1466 */
1467
1468 for (x = xstart, xcount = img->xsize, row = 0;
1469 xcount > 0;
1470 xcount --, x += xdir, row ++)
1471 {
1472 if (bits == 1)
1473 {
1474 TIFFReadScanline(tif, scanline, row, 0);
1475 for (ycount = img->ysize, scanptr = scanline, p = in + xstart * 3, bit = 0xf0;
1476 ycount > 0;
1477 ycount --, p += pstep)
1478 {
1479 if (*scanptr & bit & 0x11)
1480 {
1481 p[0] = 0;
1482 p[1] = 0;
1483 p[2] = 0;
1484 }
1485 else
1486 {
1487 if (*scanptr & bit & 0x88)
1488 p[0] = 0;
1489 else
1490 p[0] = 255;
1491
1492 if (*scanptr & bit & 0x44)
1493 p[1] = 0;
1494 else
1495 p[1] = 255;
1496
1497 if (*scanptr & bit & 0x22)
1498 p[2] = 0;
1499 else
1500 p[2] = 255;
1501 }
1502
1503 if (bit == 0xf0)
1504 bit = 0x0f;
1505 else
1506 {
1507 bit = 0xf0;
1508 scanptr ++;
1509 }
1510 }
1511 }
1512 else if (bits == 2)
1513 {
1514 TIFFReadScanline(tif, scanline, row, 0);
1515 for (ycount = img->ysize, scanptr = scanline, p = in + xstart * 3;
1516 ycount > 0;
1517 ycount --, p += pstep, scanptr ++)
1518 {
1519 pixel = *scanptr;
1520 k = 255 * (pixel & 3) / 3;
1521 if (k == 255)
1522 {
1523 p[0] = 0;
1524 p[1] = 0;
1525 p[2] = 0;
1526 }
1527 else
1528 {
1529 pixel >>= 2;
1530 b = 255 - 255 * (pixel & 3) / 3 - k;
1531 if (b < 0)
1532 p[2] = 0;
1533 else if (b < 256)
1534 p[2] = b;
1535 else
1536 p[2] = 255;
1537
1538 pixel >>= 2;
1539 g = 255 - 255 * (pixel & 3) / 3 - k;
1540 if (g < 0)
1541 p[1] = 0;
1542 else if (g < 256)
1543 p[1] = g;
1544 else
1545 p[1] = 255;
1546
1547 pixel >>= 2;
1548 r = 255 - 255 * (pixel & 3) / 3 - k;
1549 if (r < 0)
1550 p[0] = 0;
1551 else if (r < 256)
1552 p[0] = r;
1553 else
1554 p[0] = 255;
1555 }
1556 }
1557 }
1558 else if (bits == 4)
1559 {
1560 TIFFReadScanline(tif, scanline, row, 0);
1561 for (ycount = img->ysize, scanptr = scanline, p = in + xstart * 3;
1562 ycount > 0;
1563 ycount --, p += pstep, scanptr += 2)
1564 {
1565 pixel = scanptr[1];
1566 k = 255 * (pixel & 15) / 15;
1567 if (k == 255)
1568 {
1569 p[0] = 0;
1570 p[1] = 0;
1571 p[2] = 0;
1572 }
1573 else
1574 {
1575 pixel >>= 4;
1576 b = 255 - 255 * (pixel & 15) / 15 - k;
1577 if (b < 0)
1578 p[2] = 0;
1579 else if (b < 256)
1580 p[2] = b;
1581 else
1582 p[2] = 255;
1583
1584 pixel = scanptr[0];
1585 g = 255 - 255 * (pixel & 15) / 15 - k;
1586 if (g < 0)
1587 p[1] = 0;
1588 else if (g < 256)
1589 p[1] = g;
1590 else
1591 p[1] = 255;
1592
1593 pixel >>= 4;
1594 r = 255 - 255 * (pixel & 15) / 15 - k;
1595 if (r < 0)
1596 p[0] = 0;
1597 else if (r < 256)
1598 p[0] = r;
1599 else
1600 p[0] = 255;
1601 }
1602 }
1603 }
1604 else if (img->colorspace == IMAGE_CMYK)
1605 {
1606 TIFFReadScanline(tif, scanline, row, 0);
1607 ImagePutCol(img, x, 0, img->ysize, scanline);
1608 }
1609 else
1610 {
1611 TIFFReadScanline(tif, scanline, row, 0);
1612
1613 for (ycount = img->ysize, p = in + xstart * 3, scanptr = scanline;
1614 ycount > 0;
1615 ycount --, p += pstep, scanptr += 4)
1616 {
1617 k = scanptr[3];
1618 if (k == 255)
1619 {
1620 p[0] = 0;
1621 p[1] = 0;
1622 p[2] = 0;
1623 }
1624 else
1625 {
1626 r = 255 - scanptr[0] - k;
1627 if (r < 0)
1628 p[0] = 0;
1629 else if (r < 256)
1630 p[0] = r;
1631 else
1632 p[0] = 255;
1633
1634 g = 255 - scanptr[1] - k;
1635 if (g < 0)
1636 p[1] = 0;
1637 else if (g < 256)
1638 p[1] = g;
1639 else
1640 p[1] = 255;
1641
1642 b = 255 - scanptr[2] - k;
1643 if (b < 0)
1644 p[2] = 0;
1645 else if (b < 256)
1646 p[2] = b;
1647 else
1648 p[2] = 255;
1649 }
1650 }
1651 }
1652
1653 if ((saturation != 100 || hue != 0) && bpp > 1)
1654 ImageRGBAdjust(in, img->ysize, saturation, hue);
1655
1656 if (img->colorspace == IMAGE_RGB)
1657 {
1658 if (lut)
1659 ImageLut(in, img->ysize * 3, lut);
1660
1661 ImagePutCol(img, x, 0, img->ysize, in);
1662 }
1663 else if (img->colorspace == IMAGE_WHITE)
1664 {
1665 switch (img->colorspace)
1666 {
1667 case IMAGE_WHITE :
1668 ImageRGBToWhite(in, out, img->ysize);
1669 break;
1670 case IMAGE_BLACK :
1671 ImageRGBToBlack(in, out, img->ysize);
1672 break;
1673 case IMAGE_CMY :
1674 ImageRGBToCMY(in, out, img->ysize);
1675 break;
1676 case IMAGE_CMYK :
1677 ImageRGBToCMYK(in, out, img->ysize);
1678 break;
1679 }
1680
1681 if (lut)
1682 ImageLut(out, img->ysize * bpp, lut);
1683
1684 ImagePutCol(img, x, 0, img->ysize, out);
1685 }
1686 }
1687 }
1688
1689 break;
1690 }
1691
1692 default :
1693 _TIFFfree(scanline);
1694 free(in);
1695 free(out);
1696
1697 TIFFClose(tif);
1698 fputs("ERROR: Unknown TIFF photometric value!\n", stderr);
1699 return (-1);
1700 }
1701
1702 /*
1703 * Free temporary buffers, close the TIFF file, and return.
1704 */
1705
1706 _TIFFfree(scanline);
1707 free(in);
1708 free(out);
1709
1710 TIFFClose(tif);
1711 return (0);
1712 }
1713
1714
1715 #endif /* HAVE_LIBTIFF */
1716
1717
1718 /*
1719 * End of "$Id$".
1720 */