Debian Codegraph @

cupsfilters/image-tiff.c

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