Debian Codegraph @

cupsfilters/image-tiff.c

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