]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-tiff.c
2 * "$Id: image-tiff.c 6649 2007-07-11 21:46:42Z mike $"
4 * TIFF file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * _cupsImageReadTIFF() - Read a TIFF image file.
23 * Include necessary headers...
26 #include "image-private.h"
29 # include <tiff.h> /* TIFF image definitions */
35 * '_cupsImageReadTIFF()' - Read a TIFF image file.
38 int /* O - Read status */
40 cups_image_t
*img
, /* IO - cupsImage */
41 FILE *fp
, /* I - cupsImage file */
42 cups_icspace_t primary
, /* I - Primary choice for colorspace */
43 cups_icspace_t secondary
, /* I - Secondary choice for colorspace */
44 int saturation
, /* I - Color saturation (%) */
45 int hue
, /* I - Color hue (degrees) */
46 const cups_ib_t
*lut
) /* I - Lookup table for gamma/brightness */
48 TIFF
*tif
; /* TIFF file */
49 uint32 width
, height
; /* Size of image */
50 uint16 photometric
, /* Colorspace */
51 compression
, /* Type of compression */
52 orientation
, /* Orientation */
53 resunit
, /* Units for resolution */
54 samples
, /* Number of samples/pixel */
55 bits
, /* Number of bits/pixel */
56 inkset
, /* Ink set for color separations */
57 numinks
; /* Number of inks in set */
58 float xres
, /* Horizontal resolution */
59 yres
; /* Vertical resolution */
60 uint16
*redcmap
, /* Red colormap information */
61 *greencmap
, /* Green colormap information */
62 *bluecmap
; /* Blue colormap information */
63 int c
, /* Color index */
64 num_colors
, /* Number of colors */
65 bpp
, /* Bytes per pixel */
66 x
, y
, /* Current x & y */
67 row
, /* Current row in image */
68 xstart
, ystart
, /* Starting x & y */
69 xdir
, ydir
, /* X & y direction */
70 xcount
, ycount
, /* X & Y counters */
71 pstep
, /* Pixel step (= bpp or -2 * bpp) */
72 scanwidth
, /* Width of scanline */
73 r
, g
, b
, k
, /* Red, green, blue, and black values */
74 alpha
; /* cupsImage includes alpha? */
75 cups_ib_t
*in
, /* Input buffer */
76 *out
, /* Output buffer */
77 *p
, /* Pointer into buffer */
78 *scanline
, /* Scanline buffer */
79 *scanptr
, /* Pointer into scanline buffer */
80 bit
, /* Current bit */
81 pixel
, /* Current pixel */
82 zero
, /* Zero value (bitmaps) */
83 one
; /* One value (bitmaps) */
87 * Open the TIFF file and get the required parameters...
90 lseek(fileno(fp
), 0, SEEK_SET
); /* Work around "feature" in some stdio's */
92 if ((tif
= TIFFFdOpen(fileno(fp
), "", "r")) == NULL
)
94 fputs("DEBUG: TIFFFdOpen() failed!\n", stderr
);
99 if (!TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &width
))
101 fputs("DEBUG: No image width tag in the file!\n", stderr
);
107 if (!TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &height
))
109 fputs("DEBUG: No image height tag in the file!\n", stderr
);
115 if (!TIFFGetField(tif
, TIFFTAG_PHOTOMETRIC
, &photometric
))
117 fputs("DEBUG: No photometric tag in the file!\n", stderr
);
123 if (!TIFFGetField(tif
, TIFFTAG_COMPRESSION
, &compression
))
125 fputs("DEBUG: No compression tag in the file!\n", stderr
);
131 if (!TIFFGetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samples
))
134 if (!TIFFGetField(tif
, TIFFTAG_BITSPERSAMPLE
, &bits
))
138 * Get the image orientation...
141 if (!TIFFGetField(tif
, TIFFTAG_ORIENTATION
, &orientation
))
145 * Get the image resolution...
148 if (TIFFGetField(tif
, TIFFTAG_XRESOLUTION
, &xres
) &&
149 TIFFGetField(tif
, TIFFTAG_YRESOLUTION
, &yres
) &&
150 TIFFGetField(tif
, TIFFTAG_RESOLUTIONUNIT
, &resunit
))
152 if (resunit
== RESUNIT_INCH
)
157 else if (resunit
== RESUNIT_CENTIMETER
)
159 img
->xppi
= xres
* 2.54;
160 img
->yppi
= yres
* 2.54;
168 if (img
->xppi
== 0 || img
->yppi
== 0)
170 fputs("DEBUG: Bad TIFF resolution.\n", stderr
);
171 img
->xppi
= img
->yppi
= 128;
174 fprintf(stderr
, "DEBUG: TIFF resolution = %fx%f, units=%d\n",
175 xres
, yres
, resunit
);
176 fprintf(stderr
, "DEBUG: Stored resolution = %dx%d PPI\n",
177 img
->xppi
, img
->yppi
);
181 * See if the image has an alpha channel...
184 if (samples
== 2 || (samples
== 4 && photometric
== PHOTOMETRIC_RGB
))
190 * Check the size of the image...
193 if (width
== 0 || width
> CUPS_IMAGE_MAX_WIDTH
||
194 height
== 0 || height
> CUPS_IMAGE_MAX_HEIGHT
||
195 (bits
!= 1 && bits
!= 2 && bits
!= 4 && bits
!= 8) ||
196 samples
< 1 || samples
> 4)
198 fprintf(stderr
, "DEBUG: Bad TIFF dimensions %ux%ux%ux%u!\n",
199 (unsigned)width
, (unsigned)height
, (unsigned)bits
,
207 * Setup the image size and colorspace...
212 if (photometric
== PHOTOMETRIC_MINISBLACK
||
213 photometric
== PHOTOMETRIC_MINISWHITE
)
214 img
->colorspace
= secondary
;
215 else if (photometric
== PHOTOMETRIC_SEPARATED
&& primary
== CUPS_IMAGE_RGB_CMYK
)
216 img
->colorspace
= CUPS_IMAGE_CMYK
;
217 else if (primary
== CUPS_IMAGE_RGB_CMYK
)
218 img
->colorspace
= CUPS_IMAGE_RGB
;
220 img
->colorspace
= primary
;
222 fprintf(stderr
, "DEBUG: img->colorspace = %d\n", img
->colorspace
);
224 bpp
= cupsImageGetDepth(img
);
226 cupsImageSetMaxTiles(img
, 0);
229 * Set the X & Y start and direction according to the image orientation...
234 case ORIENTATION_TOPRIGHT
:
235 fputs("DEBUG: orientation = top-right\n", stderr
);
237 case ORIENTATION_RIGHTTOP
:
238 fputs("DEBUG: orientation = right-top\n", stderr
);
241 case ORIENTATION_TOPLEFT
:
242 fputs("DEBUG: orientation = top-left\n", stderr
);
244 case ORIENTATION_LEFTTOP
:
245 fputs("DEBUG: orientation = left-top\n", stderr
);
247 case ORIENTATION_BOTLEFT
:
248 fputs("DEBUG: orientation = bottom-left\n", stderr
);
250 case ORIENTATION_LEFTBOT
:
251 fputs("DEBUG: orientation = left-bottom\n", stderr
);
253 case ORIENTATION_BOTRIGHT
:
254 fputs("DEBUG: orientation = bottom-right\n", stderr
);
256 case ORIENTATION_RIGHTBOT
:
257 fputs("DEBUG: orientation = right-bottom\n", stderr
);
263 case ORIENTATION_TOPRIGHT
:
264 case ORIENTATION_RIGHTTOP
:
265 xstart
= img
->xsize
- 1;
271 case ORIENTATION_TOPLEFT
:
272 case ORIENTATION_LEFTTOP
:
278 case ORIENTATION_BOTLEFT
:
279 case ORIENTATION_LEFTBOT
:
282 ystart
= img
->ysize
- 1;
285 case ORIENTATION_BOTRIGHT
:
286 case ORIENTATION_RIGHTBOT
:
287 xstart
= img
->xsize
- 1;
289 ystart
= img
->ysize
- 1;
295 * Allocate a scanline buffer...
298 scanwidth
= TIFFScanlineSize(tif
);
299 scanline
= _TIFFmalloc(scanwidth
);
302 * Allocate input and output buffers...
305 if (orientation
< ORIENTATION_LEFTTOP
)
307 if (samples
> 1 || photometric
== PHOTOMETRIC_PALETTE
)
312 in
= malloc(img
->xsize
* 3 + 3);
313 out
= malloc(img
->xsize
* bpp
);
317 if (samples
> 1 || photometric
== PHOTOMETRIC_PALETTE
)
322 in
= malloc(img
->ysize
* 3 + 3);
323 out
= malloc(img
->ysize
* bpp
);
327 * Read the image. This is greatly complicated by the fact that TIFF
328 * supports literally hundreds of different colorspaces and orientations,
329 * each which must be handled separately...
332 fprintf(stderr
, "DEBUG: photometric = %d\n", photometric
);
333 fprintf(stderr
, "DEBUG: compression = %d\n", compression
);
337 case PHOTOMETRIC_MINISWHITE
:
338 case PHOTOMETRIC_MINISBLACK
:
339 if (photometric
== PHOTOMETRIC_MINISWHITE
)
350 if (orientation
< ORIENTATION_LEFTTOP
)
356 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
358 ycount
--, y
+= ydir
, row
++)
362 TIFFReadScanline(tif
, scanline
, row
, 0);
363 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
, bit
= 128;
365 xcount
--, p
+= pstep
)
383 TIFFReadScanline(tif
, scanline
, row
, 0);
384 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
, bit
= 0xc0;
386 xcount
--, p
+= pstep
)
388 pixel
= *scanptr
& bit
;
391 *p
= (255 * pixel
/ 3) ^ zero
;
404 TIFFReadScanline(tif
, scanline
, row
, 0);
405 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
, bit
= 0xf0;
407 xcount
--, p
+= pstep
)
411 *p
= (255 * ((*scanptr
& 0xf0) >> 4) / 15) ^ zero
;
416 *p
= (255 * (*scanptr
& 0x0f) / 15) ^ zero
;
422 else if (xdir
< 0 || zero
|| alpha
)
424 TIFFReadScanline(tif
, scanline
, row
, 0);
430 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
432 xcount
--, p
+= pstep
, scanptr
+= 2)
433 *p
= (scanptr
[1] * (255 - scanptr
[0]) +
434 (255 - scanptr
[1]) * 255) / 255;
438 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
440 xcount
--, p
+= pstep
, scanptr
+= 2)
441 *p
= (scanptr
[1] * scanptr
[0] +
442 (255 - scanptr
[1]) * 255) / 255;
449 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
451 xcount
--, p
+= pstep
, scanptr
++)
456 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
458 xcount
--, p
+= pstep
, scanptr
++)
464 TIFFReadScanline(tif
, in
, row
, 0);
466 if (img
->colorspace
== CUPS_IMAGE_WHITE
)
469 cupsImageLut(in
, img
->xsize
, lut
);
471 _cupsImagePutRow(img
, 0, y
, img
->xsize
, in
);
475 switch (img
->colorspace
)
480 case CUPS_IMAGE_RGB
:
481 cupsImageWhiteToRGB(in
, out
, img
->xsize
);
483 case CUPS_IMAGE_BLACK
:
484 cupsImageWhiteToBlack(in
, out
, img
->xsize
);
486 case CUPS_IMAGE_CMY
:
487 cupsImageWhiteToCMY(in
, out
, img
->xsize
);
489 case CUPS_IMAGE_CMYK
:
490 cupsImageWhiteToCMYK(in
, out
, img
->xsize
);
495 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
497 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
504 * Column major order...
507 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
509 xcount
--, x
+= xdir
, row
++)
513 TIFFReadScanline(tif
, scanline
, row
, 0);
514 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
, bit
= 128;
516 ycount
--, p
+= ydir
)
534 TIFFReadScanline(tif
, scanline
, row
, 0);
535 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
, bit
= 0xc0;
537 ycount
--, p
+= ydir
)
539 pixel
= *scanptr
& 0xc0;
543 *p
= (255 * pixel
/ 3) ^ zero
;
556 TIFFReadScanline(tif
, scanline
, row
, 0);
557 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
, bit
= 0xf0;
559 ycount
--, p
+= ydir
)
563 *p
= (255 * ((*scanptr
& 0xf0) >> 4) / 15) ^ zero
;
568 *p
= (255 * (*scanptr
& 0x0f) / 15) ^ zero
;
574 else if (ydir
< 0 || zero
|| alpha
)
576 TIFFReadScanline(tif
, scanline
, row
, 0);
582 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
584 ycount
--, p
+= ydir
, scanptr
+= 2)
585 *p
= (scanptr
[1] * (255 - scanptr
[0]) +
586 (255 - scanptr
[1]) * 255) / 255;
590 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
592 ycount
--, p
+= ydir
, scanptr
+= 2)
593 *p
= (scanptr
[1] * scanptr
[0] +
594 (255 - scanptr
[1]) * 255) / 255;
601 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
603 ycount
--, p
+= ydir
, scanptr
++)
608 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
610 ycount
--, p
+= ydir
, scanptr
++)
616 TIFFReadScanline(tif
, in
, row
, 0);
618 if (img
->colorspace
== CUPS_IMAGE_WHITE
)
621 cupsImageLut(in
, img
->ysize
, lut
);
623 _cupsImagePutCol(img
, x
, 0, img
->ysize
, in
);
627 switch (img
->colorspace
)
632 case CUPS_IMAGE_RGB
:
633 cupsImageWhiteToRGB(in
, out
, img
->ysize
);
635 case CUPS_IMAGE_BLACK
:
636 cupsImageWhiteToBlack(in
, out
, img
->ysize
);
638 case CUPS_IMAGE_CMY
:
639 cupsImageWhiteToCMY(in
, out
, img
->ysize
);
641 case CUPS_IMAGE_CMYK
:
642 cupsImageWhiteToCMYK(in
, out
, img
->ysize
);
647 cupsImageLut(out
, img
->ysize
* bpp
, lut
);
649 _cupsImagePutCol(img
, x
, 0, img
->ysize
, out
);
655 case PHOTOMETRIC_PALETTE
:
656 if (!TIFFGetField(tif
, TIFFTAG_COLORMAP
, &redcmap
, &greencmap
, &bluecmap
))
658 fputs("DEBUG: No colormap tag in the file!\n", stderr
);
663 num_colors
= 1 << bits
;
665 for (c
= 0; c
< num_colors
; c
++)
672 if (orientation
< ORIENTATION_LEFTTOP
)
678 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
680 ycount
--, y
+= ydir
, row
++)
684 TIFFReadScanline(tif
, scanline
, row
, 0);
685 for (xcount
= img
->xsize
, scanptr
= scanline
,
686 p
= in
+ xstart
* 3, bit
= 128;
688 xcount
--, p
+= pstep
)
714 TIFFReadScanline(tif
, scanline
, row
, 0);
715 for (xcount
= img
->xsize
, scanptr
= scanline
,
716 p
= in
+ xstart
* 3, bit
= 0xc0;
718 xcount
--, p
+= pstep
)
720 pixel
= *scanptr
& bit
;
724 p
[0] = redcmap
[pixel
];
725 p
[1] = greencmap
[pixel
];
726 p
[2] = bluecmap
[pixel
];
739 TIFFReadScanline(tif
, scanline
, row
, 0);
740 for (xcount
= img
->xsize
, scanptr
= scanline
,
741 p
= in
+ 3 * xstart
, bit
= 0xf0;
743 xcount
--, p
+= pstep
)
747 pixel
= (*scanptr
& 0xf0) >> 4;
748 p
[0] = redcmap
[pixel
];
749 p
[1] = greencmap
[pixel
];
750 p
[2] = bluecmap
[pixel
];
755 pixel
= *scanptr
++ & 0x0f;
756 p
[0] = redcmap
[pixel
];
757 p
[1] = greencmap
[pixel
];
758 p
[2] = bluecmap
[pixel
];
765 TIFFReadScanline(tif
, scanline
, row
, 0);
767 for (xcount
= img
->xsize
, p
= in
+ 3 * xstart
, scanptr
= scanline
;
769 xcount
--, p
+= pstep
)
771 p
[0] = redcmap
[*scanptr
];
772 p
[1] = greencmap
[*scanptr
];
773 p
[2] = bluecmap
[*scanptr
++];
777 switch (img
->colorspace
)
782 case CUPS_IMAGE_WHITE
:
783 cupsImageRGBToWhite(in
, out
, img
->xsize
);
785 case CUPS_IMAGE_RGB
:
786 cupsImageRGBToRGB(in
, out
, img
->xsize
);
788 case CUPS_IMAGE_BLACK
:
789 cupsImageRGBToBlack(in
, out
, img
->xsize
);
791 case CUPS_IMAGE_CMY
:
792 cupsImageRGBToCMY(in
, out
, img
->xsize
);
794 case CUPS_IMAGE_CMYK
:
795 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
800 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
802 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
808 * Column major order...
811 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
813 xcount
--, x
+= xdir
, row
++)
817 TIFFReadScanline(tif
, scanline
, row
, 0);
818 for (ycount
= img
->ysize
, scanptr
= scanline
,
819 p
= in
+ 3 * ystart
, bit
= 128;
821 ycount
--, p
+= ydir
)
847 TIFFReadScanline(tif
, scanline
, row
, 0);
848 for (ycount
= img
->ysize
, scanptr
= scanline
,
849 p
= in
+ 3 * ystart
, bit
= 0xc0;
851 ycount
--, p
+= ydir
)
853 pixel
= *scanptr
& 0xc0;
857 p
[0] = redcmap
[pixel
];
858 p
[1] = greencmap
[pixel
];
859 p
[2] = bluecmap
[pixel
];
872 TIFFReadScanline(tif
, scanline
, row
, 0);
873 for (ycount
= img
->ysize
, scanptr
= scanline
,
874 p
= in
+ 3 * ystart
, bit
= 0xf0;
876 ycount
--, p
+= ydir
)
880 pixel
= (*scanptr
& 0xf0) >> 4;
881 p
[0] = redcmap
[pixel
];
882 p
[1] = greencmap
[pixel
];
883 p
[2] = bluecmap
[pixel
];
888 pixel
= *scanptr
++ & 0x0f;
889 p
[0] = redcmap
[pixel
];
890 p
[1] = greencmap
[pixel
];
891 p
[2] = bluecmap
[pixel
];
898 TIFFReadScanline(tif
, scanline
, row
, 0);
900 for (ycount
= img
->ysize
, p
= in
+ 3 * ystart
, scanptr
= scanline
;
902 ycount
--, p
+= ydir
)
904 p
[0] = redcmap
[*scanptr
];
905 p
[1] = greencmap
[*scanptr
];
906 p
[2] = bluecmap
[*scanptr
++];
910 switch (img
->colorspace
)
915 case CUPS_IMAGE_WHITE
:
916 cupsImageRGBToWhite(in
, out
, img
->ysize
);
918 case CUPS_IMAGE_RGB
:
919 cupsImageRGBToRGB(in
, out
, img
->ysize
);
921 case CUPS_IMAGE_BLACK
:
922 cupsImageRGBToBlack(in
, out
, img
->ysize
);
924 case CUPS_IMAGE_CMY
:
925 cupsImageRGBToCMY(in
, out
, img
->ysize
);
927 case CUPS_IMAGE_CMYK
:
928 cupsImageRGBToCMYK(in
, out
, img
->ysize
);
933 cupsImageLut(out
, img
->ysize
* bpp
, lut
);
935 _cupsImagePutCol(img
, x
, 0, img
->ysize
, out
);
940 case PHOTOMETRIC_RGB
:
941 if (orientation
< ORIENTATION_LEFTTOP
)
947 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
949 ycount
--, y
+= ydir
, row
++)
953 TIFFReadScanline(tif
, scanline
, row
, 0);
954 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3, bit
= 0xf0;
956 xcount
--, p
+= pstep
)
958 if (*scanptr
& bit
& 0x88)
963 if (*scanptr
& bit
& 0x44)
968 if (*scanptr
& bit
& 0x22)
984 TIFFReadScanline(tif
, scanline
, row
, 0);
985 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
987 xcount
--, p
+= pstep
, scanptr
++)
989 pixel
= *scanptr
>> 2;
990 p
[0] = 255 * (pixel
& 3) / 3;
992 p
[1] = 255 * (pixel
& 3) / 3;
994 p
[2] = 255 * (pixel
& 3) / 3;
999 TIFFReadScanline(tif
, scanline
, row
, 0);
1000 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1002 xcount
-= 2, p
+= 2 * pstep
, scanptr
+= 3)
1005 p
[1] = 255 * (pixel
& 15) / 15;
1007 p
[0] = 255 * (pixel
& 15) / 15;
1009 p
[2] = 255 * ((pixel
>> 4) & 15) / 15;
1013 p
[pstep
+ 0] = 255 * (pixel
& 15) / 15;
1015 p
[pstep
+ 2] = 255 * (pixel
& 15) / 15;
1017 p
[pstep
+ 1] = 255 * (pixel
& 15) / 15;
1021 else if (xdir
< 0 || alpha
)
1023 TIFFReadScanline(tif
, scanline
, row
, 0);
1027 for (xcount
= img
->xsize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1029 xcount
--, p
+= pstep
, scanptr
+= 4)
1031 p
[0] = (scanptr
[0] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1032 p
[1] = (scanptr
[1] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1033 p
[2] = (scanptr
[2] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1038 for (xcount
= img
->xsize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1040 xcount
--, p
+= pstep
, scanptr
+= 3)
1049 TIFFReadScanline(tif
, in
, row
, 0);
1051 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1052 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
1054 switch (img
->colorspace
)
1059 case CUPS_IMAGE_WHITE
:
1060 cupsImageRGBToWhite(in
, out
, img
->xsize
);
1062 case CUPS_IMAGE_RGB
:
1063 cupsImageRGBToRGB(in
, out
, img
->xsize
);
1065 case CUPS_IMAGE_BLACK
:
1066 cupsImageRGBToBlack(in
, out
, img
->xsize
);
1068 case CUPS_IMAGE_CMY
:
1069 cupsImageRGBToCMY(in
, out
, img
->xsize
);
1071 case CUPS_IMAGE_CMYK
:
1072 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
1077 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
1079 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
1085 * Column major order...
1088 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
1090 xcount
--, x
+= xdir
, row
++)
1094 TIFFReadScanline(tif
, scanline
, row
, 0);
1095 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
* 3, bit
= 0xf0;
1097 ycount
--, p
+= pstep
)
1099 if (*scanptr
& bit
& 0x88)
1104 if (*scanptr
& bit
& 0x44)
1109 if (*scanptr
& bit
& 0x22)
1125 TIFFReadScanline(tif
, scanline
, row
, 0);
1126 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
* 3;
1128 ycount
--, p
+= pstep
, scanptr
++)
1130 pixel
= *scanptr
>> 2;
1131 p
[0] = 255 * (pixel
& 3) / 3;
1133 p
[1] = 255 * (pixel
& 3) / 3;
1135 p
[2] = 255 * (pixel
& 3) / 3;
1140 TIFFReadScanline(tif
, scanline
, row
, 0);
1141 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
* 3;
1143 ycount
-= 2, p
+= 2 * pstep
, scanptr
+= 3)
1146 p
[1] = 255 * (pixel
& 15) / 15;
1148 p
[0] = 255 * (pixel
& 15) / 15;
1150 p
[2] = 255 * ((pixel
>> 4) & 15) / 15;
1154 p
[pstep
+ 0] = 255 * (pixel
& 15) / 15;
1156 p
[pstep
+ 2] = 255 * (pixel
& 15) / 15;
1158 p
[pstep
+ 1] = 255 * (pixel
& 15) / 15;
1162 else if (ydir
< 0 || alpha
)
1164 TIFFReadScanline(tif
, scanline
, row
, 0);
1168 for (ycount
= img
->ysize
, p
= in
+ ystart
* 3, scanptr
= scanline
;
1170 ycount
--, p
+= pstep
, scanptr
+= 4)
1172 p
[0] = (scanptr
[0] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1173 p
[1] = (scanptr
[1] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1174 p
[2] = (scanptr
[2] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1179 for (ycount
= img
->ysize
, p
= in
+ ystart
* 3, scanptr
= scanline
;
1181 ycount
--, p
+= pstep
, scanptr
+= 3)
1190 TIFFReadScanline(tif
, in
, row
, 0);
1192 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1193 cupsImageRGBAdjust(in
, img
->ysize
, saturation
, hue
);
1195 switch (img
->colorspace
)
1200 case CUPS_IMAGE_WHITE
:
1201 cupsImageRGBToWhite(in
, out
, img
->ysize
);
1203 case CUPS_IMAGE_RGB
:
1204 cupsImageRGBToRGB(in
, out
, img
->ysize
);
1206 case CUPS_IMAGE_BLACK
:
1207 cupsImageRGBToBlack(in
, out
, img
->ysize
);
1209 case CUPS_IMAGE_CMY
:
1210 cupsImageRGBToCMY(in
, out
, img
->ysize
);
1212 case CUPS_IMAGE_CMYK
:
1213 cupsImageRGBToCMYK(in
, out
, img
->ysize
);
1218 cupsImageLut(out
, img
->ysize
* bpp
, lut
);
1220 _cupsImagePutCol(img
, x
, 0, img
->ysize
, out
);
1225 case PHOTOMETRIC_SEPARATED
:
1226 inkset
= INKSET_CMYK
;
1229 #ifdef TIFFTAG_NUMBEROFINKS
1230 if (!TIFFGetField(tif
, TIFFTAG_INKSET
, &inkset
) &&
1231 !TIFFGetField(tif
, TIFFTAG_NUMBEROFINKS
, &numinks
))
1233 if (!TIFFGetField(tif
, TIFFTAG_INKSET
, &inkset
))
1234 #endif /* TIFFTAG_NUMBEROFINKS */
1236 fputs("WARNING: No inkset or number-of-inks tag in the file!\n", stderr
);
1239 if (inkset
== INKSET_CMYK
|| numinks
== 4)
1241 if (orientation
< ORIENTATION_LEFTTOP
)
1244 * Row major order...
1247 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
1249 ycount
--, y
+= ydir
, row
++)
1253 TIFFReadScanline(tif
, scanline
, row
, 0);
1254 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3, bit
= 0xf0;
1256 xcount
--, p
+= pstep
)
1258 if (*scanptr
& bit
& 0x11)
1266 if (*scanptr
& bit
& 0x88)
1271 if (*scanptr
& bit
& 0x44)
1276 if (*scanptr
& bit
& 0x22)
1293 TIFFReadScanline(tif
, scanline
, row
, 0);
1294 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1296 xcount
--, p
+= pstep
, scanptr
++)
1299 k
= 255 * (pixel
& 3) / 3;
1309 b
= 255 - 255 * (pixel
& 3) / 3 - k
;
1318 g
= 255 - 255 * (pixel
& 3) / 3 - k
;
1327 r
= 255 - 255 * (pixel
& 3) / 3 - k
;
1339 TIFFReadScanline(tif
, scanline
, row
, 0);
1340 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1342 xcount
--, p
+= pstep
, scanptr
+= 2)
1345 k
= 255 * (pixel
& 15) / 15;
1355 b
= 255 - 255 * (pixel
& 15) / 15 - k
;
1364 g
= 255 - 255 * (pixel
& 15) / 15 - k
;
1373 r
= 255 - 255 * (pixel
& 15) / 15 - k
;
1383 else if (img
->colorspace
== CUPS_IMAGE_CMYK
)
1385 TIFFReadScanline(tif
, scanline
, row
, 0);
1386 _cupsImagePutRow(img
, 0, y
, img
->xsize
, scanline
);
1390 TIFFReadScanline(tif
, scanline
, row
, 0);
1392 for (xcount
= img
->xsize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1394 xcount
--, p
+= pstep
, scanptr
+= 4)
1405 r
= 255 - scanptr
[0] - k
;
1413 g
= 255 - scanptr
[1] - k
;
1421 b
= 255 - scanptr
[2] - k
;
1432 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1433 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
1435 switch (img
->colorspace
)
1440 case CUPS_IMAGE_WHITE
:
1441 cupsImageRGBToWhite(in
, out
, img
->xsize
);
1443 case CUPS_IMAGE_RGB
:
1444 cupsImageRGBToRGB(in
, out
, img
->xsize
);
1446 case CUPS_IMAGE_BLACK
:
1447 cupsImageRGBToBlack(in
, out
, img
->xsize
);
1449 case CUPS_IMAGE_CMY
:
1450 cupsImageRGBToCMY(in
, out
, img
->xsize
);
1452 case CUPS_IMAGE_CMYK
:
1453 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
1458 cupsImageLut(out
, img
->xsize
* 3, lut
);
1460 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
1466 * Column major order...
1469 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
1471 xcount
--, x
+= xdir
, row
++)
1475 TIFFReadScanline(tif
, scanline
, row
, 0);
1476 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ xstart
* 3, bit
= 0xf0;
1478 ycount
--, p
+= pstep
)
1480 if (*scanptr
& bit
& 0x11)
1488 if (*scanptr
& bit
& 0x88)
1493 if (*scanptr
& bit
& 0x44)
1498 if (*scanptr
& bit
& 0x22)
1515 TIFFReadScanline(tif
, scanline
, row
, 0);
1516 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1518 ycount
--, p
+= pstep
, scanptr
++)
1521 k
= 255 * (pixel
& 3) / 3;
1531 b
= 255 - 255 * (pixel
& 3) / 3 - k
;
1540 g
= 255 - 255 * (pixel
& 3) / 3 - k
;
1549 r
= 255 - 255 * (pixel
& 3) / 3 - k
;
1561 TIFFReadScanline(tif
, scanline
, row
, 0);
1562 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1564 ycount
--, p
+= pstep
, scanptr
+= 2)
1567 k
= 255 * (pixel
& 15) / 15;
1577 b
= 255 - 255 * (pixel
& 15) / 15 - k
;
1586 g
= 255 - 255 * (pixel
& 15) / 15 - k
;
1595 r
= 255 - 255 * (pixel
& 15) / 15 - k
;
1605 else if (img
->colorspace
== CUPS_IMAGE_CMYK
)
1607 TIFFReadScanline(tif
, scanline
, row
, 0);
1608 _cupsImagePutCol(img
, x
, 0, img
->ysize
, scanline
);
1612 TIFFReadScanline(tif
, scanline
, row
, 0);
1614 for (ycount
= img
->ysize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1616 ycount
--, p
+= pstep
, scanptr
+= 4)
1627 r
= 255 - scanptr
[0] - k
;
1635 g
= 255 - scanptr
[1] - k
;
1643 b
= 255 - scanptr
[2] - k
;
1654 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1655 cupsImageRGBAdjust(in
, img
->ysize
, saturation
, hue
);
1657 switch (img
->colorspace
)
1662 case CUPS_IMAGE_WHITE
:
1663 cupsImageRGBToWhite(in
, out
, img
->ysize
);
1665 case CUPS_IMAGE_RGB
:
1666 cupsImageRGBToRGB(in
, out
, img
->ysize
);
1668 case CUPS_IMAGE_BLACK
:
1669 cupsImageRGBToBlack(in
, out
, img
->ysize
);
1671 case CUPS_IMAGE_CMY
:
1672 cupsImageRGBToCMY(in
, out
, img
->ysize
);
1674 case CUPS_IMAGE_CMYK
:
1675 cupsImageRGBToCMYK(in
, out
, img
->ysize
);
1680 cupsImageLut(out
, img
->ysize
* bpp
, lut
);
1682 _cupsImagePutCol(img
, x
, 0, img
->ysize
, out
);
1690 _TIFFfree(scanline
);
1695 fputs("DEBUG: Unknown TIFF photometric value!\n", stderr
);
1700 * Free temporary buffers, close the TIFF file, and return.
1703 _TIFFfree(scanline
);
1710 #endif /* HAVE_LIBTIFF */
1714 * End of "$Id: image-tiff.c 6649 2007-07-11 21:46:42Z mike $".