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