Debian Codegraph @

cupsfilters/image-tiff.c

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