]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-tiff.c
4 * TIFF file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2002 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * ImageReadTIFF() - Read a TIFF image file.
32 * Include necessary headers...
38 # include <tiff.h> /* TIFF image definitions */
44 * 'ImageReadTIFF()' - Read a TIFF image file.
47 int /* O - Read status */
48 ImageReadTIFF(image_t
*img
, /* IO - Image */
49 FILE *fp
, /* I - Image file */
50 int primary
, /* I - Primary choice for colorspace */
51 int secondary
, /* I - Secondary choice for colorspace */
52 int saturation
, /* I - Color saturation (%) */
53 int hue
, /* I - Color hue (degrees) */
54 const ib_t
*lut
) /* I - Lookup table for gamma/brightness */
56 TIFF
*tif
; /* TIFF file */
57 uint32 width
, height
; /* Size of image */
58 uint16 photometric
, /* Colorspace */
59 compression
, /* Type of compression */
60 orientation
, /* Orientation */
61 resunit
, /* Units for resolution */
62 samples
, /* Number of samples/pixel */
63 bits
, /* Number of bits/pixel */
64 inkset
, /* Ink set for color separations */
65 numinks
; /* Number of inks in set */
66 float xres
, /* Horizontal resolution */
67 yres
; /* Vertical resolution */
68 uint16
*redcmap
, /* Red colormap information */
69 *greencmap
, /* Green colormap information */
70 *bluecmap
; /* Blue colormap information */
71 int c
, /* Color index */
72 num_colors
, /* Number of colors */
73 bpp
, /* Bytes per pixel */
74 x
, y
, /* Current x & y */
75 row
, /* Current row in image */
76 xstart
, ystart
, /* Starting x & y */
77 xdir
, ydir
, /* X & y direction */
78 xcount
, ycount
, /* X & Y counters */
79 pstep
, /* Pixel step (= bpp or -2 * bpp) */
80 scanwidth
, /* Width of scanline */
81 r
, g
, b
, k
, /* Red, green, blue, and black values */
82 alpha
; /* Image includes alpha? */
83 ib_t
*in
, /* Input buffer */
84 *out
, /* Output buffer */
85 *p
, /* Pointer into buffer */
86 *scanline
, /* Scanline buffer */
87 *scanptr
, /* Pointer into scanline buffer */
88 bit
, /* Current bit */
89 pixel
, /* Current pixel */
90 zero
, /* Zero value (bitmaps) */
91 one
; /* One value (bitmaps) */
95 * Open the TIFF file and get the required parameters...
98 lseek(fileno(fp
), 0, SEEK_SET
); /* Work around "feature" in some stdio's */
100 if ((tif
= TIFFFdOpen(fileno(fp
), "", "r")) == NULL
)
102 fputs("ERROR: TIFFFdOpen() failed!\n", stderr
);
107 if (!TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &width
))
109 fputs("ERROR: No image width tag in the file!\n", stderr
);
115 if (!TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &height
))
117 fputs("ERROR: No image height tag in the file!\n", stderr
);
123 if (!TIFFGetField(tif
, TIFFTAG_PHOTOMETRIC
, &photometric
))
125 fputs("ERROR: No photometric tag in the file!\n", stderr
);
131 if (!TIFFGetField(tif
, TIFFTAG_COMPRESSION
, &compression
))
133 fputs("ERROR: No compression tag in the file!\n", stderr
);
139 if (!TIFFGetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samples
))
142 if (!TIFFGetField(tif
, TIFFTAG_BITSPERSAMPLE
, &bits
))
146 * Get the image orientation...
149 if (!TIFFGetField(tif
, TIFFTAG_ORIENTATION
, &orientation
))
153 * Get the image resolution...
156 if (TIFFGetField(tif
, TIFFTAG_XRESOLUTION
, &xres
) &&
157 TIFFGetField(tif
, TIFFTAG_YRESOLUTION
, &yres
) &&
158 TIFFGetField(tif
, TIFFTAG_RESOLUTIONUNIT
, &resunit
))
160 if (resunit
== RESUNIT_INCH
)
165 else if (resunit
== RESUNIT_CENTIMETER
)
167 img
->xppi
= xres
* 2.54;
168 img
->yppi
= yres
* 2.54;
176 fprintf(stderr
, "DEBUG: TIFF resolution = %fx%f, units=%d\n",
177 xres
, yres
, resunit
);
178 fprintf(stderr
, "DEBUG: Stored resolution = %dx%d PPI\n",
179 img
->xppi
, img
->yppi
);
183 * See if the image has an alpha channel...
186 if (samples
== 2 || (samples
== 4 && photometric
== PHOTOMETRIC_RGB
))
192 * Setup the image size and colorspace...
197 if (photometric
== PHOTOMETRIC_MINISBLACK
||
198 photometric
== PHOTOMETRIC_MINISWHITE
)
199 img
->colorspace
= secondary
;
200 else if (photometric
== PHOTOMETRIC_SEPARATED
&& primary
== IMAGE_RGB_CMYK
)
201 img
->colorspace
= IMAGE_CMYK
;
202 else if (primary
== IMAGE_RGB_CMYK
)
203 img
->colorspace
= IMAGE_RGB
;
205 img
->colorspace
= primary
;
207 fprintf(stderr
, "DEBUG: img->colorspace = %d\n", img
->colorspace
);
209 bpp
= ImageGetDepth(img
);
211 ImageSetMaxTiles(img
, 0);
214 * Set the X & Y start and direction according to the image orientation...
219 case ORIENTATION_TOPRIGHT
:
220 fputs("DEBUG: orientation = top-right\n", stderr
);
222 case ORIENTATION_RIGHTTOP
:
223 fputs("DEBUG: orientation = right-top\n", stderr
);
226 case ORIENTATION_TOPLEFT
:
227 fputs("DEBUG: orientation = top-left\n", stderr
);
229 case ORIENTATION_LEFTTOP
:
230 fputs("DEBUG: orientation = left-top\n", stderr
);
232 case ORIENTATION_BOTLEFT
:
233 fputs("DEBUG: orientation = bottom-left\n", stderr
);
235 case ORIENTATION_LEFTBOT
:
236 fputs("DEBUG: orientation = left-bottom\n", stderr
);
238 case ORIENTATION_BOTRIGHT
:
239 fputs("DEBUG: orientation = bottom-right\n", stderr
);
241 case ORIENTATION_RIGHTBOT
:
242 fputs("DEBUG: orientation = right-bottom\n", stderr
);
248 case ORIENTATION_TOPRIGHT
:
249 case ORIENTATION_RIGHTTOP
:
250 xstart
= img
->xsize
- 1;
256 case ORIENTATION_TOPLEFT
:
257 case ORIENTATION_LEFTTOP
:
263 case ORIENTATION_BOTLEFT
:
264 case ORIENTATION_LEFTBOT
:
267 ystart
= img
->ysize
- 1;
270 case ORIENTATION_BOTRIGHT
:
271 case ORIENTATION_RIGHTBOT
:
272 xstart
= img
->xsize
- 1;
274 ystart
= img
->ysize
- 1;
280 * Allocate a scanline buffer...
283 scanwidth
= TIFFScanlineSize(tif
);
284 scanline
= _TIFFmalloc(scanwidth
);
287 * Allocate input and output buffers...
290 if (orientation
< ORIENTATION_LEFTTOP
)
292 if (samples
> 1 || photometric
== PHOTOMETRIC_PALETTE
)
297 in
= malloc(img
->xsize
* 3 + 3);
298 out
= malloc(img
->xsize
* bpp
);
302 if (samples
> 1 || photometric
== PHOTOMETRIC_PALETTE
)
307 in
= malloc(img
->ysize
* 3 + 3);
308 out
= malloc(img
->ysize
* bpp
);
312 * Read the image. This is greatly complicated by the fact that TIFF
313 * supports literally hundreds of different colorspaces and orientations,
314 * each which must be handled separately...
317 fprintf(stderr
, "DEBUG: photometric = %d\n", photometric
);
318 fprintf(stderr
, "DEBUG: compression = %d\n", compression
);
322 case PHOTOMETRIC_MINISWHITE
:
323 case PHOTOMETRIC_MINISBLACK
:
324 if (photometric
== PHOTOMETRIC_MINISWHITE
)
335 if (orientation
< ORIENTATION_LEFTTOP
)
341 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
343 ycount
--, y
+= ydir
, row
++)
347 TIFFReadScanline(tif
, scanline
, row
, 0);
348 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
, bit
= 128;
350 xcount
--, p
+= pstep
)
368 TIFFReadScanline(tif
, scanline
, row
, 0);
369 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
, bit
= 0xc0;
371 xcount
--, p
+= pstep
)
373 pixel
= *scanptr
& bit
;
376 *p
= (255 * pixel
/ 3) ^ zero
;
389 TIFFReadScanline(tif
, scanline
, row
, 0);
390 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
, bit
= 0xf0;
392 xcount
--, p
+= pstep
)
396 *p
= (255 * ((*scanptr
& 0xf0) >> 4) / 15) ^ zero
;
401 *p
= (255 * (*scanptr
& 0x0f) / 15) ^ zero
;
407 else if (xdir
< 0 || zero
|| alpha
)
409 TIFFReadScanline(tif
, scanline
, row
, 0);
415 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
417 xcount
--, p
+= pstep
, scanptr
+= 2)
418 *p
= (scanptr
[1] * (255 - scanptr
[0]) +
419 (255 - scanptr
[1]) * 255) / 255;
423 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
425 xcount
--, p
+= pstep
, scanptr
+= 2)
426 *p
= (scanptr
[1] * scanptr
[0] +
427 (255 - scanptr
[1]) * 255) / 255;
434 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
436 xcount
--, p
+= pstep
, scanptr
++)
441 for (xcount
= img
->xsize
, p
= in
+ xstart
, scanptr
= scanline
;
443 xcount
--, p
+= pstep
, scanptr
++)
449 TIFFReadScanline(tif
, in
, row
, 0);
451 if (img
->colorspace
== IMAGE_WHITE
)
454 ImageLut(in
, img
->xsize
, lut
);
456 ImagePutRow(img
, 0, y
, img
->xsize
, in
);
460 switch (img
->colorspace
)
463 ImageWhiteToRGB(in
, out
, img
->xsize
);
466 ImageWhiteToBlack(in
, out
, img
->xsize
);
469 ImageWhiteToCMY(in
, out
, img
->xsize
);
472 ImageWhiteToCMYK(in
, out
, img
->xsize
);
477 ImageLut(out
, img
->xsize
* bpp
, lut
);
479 ImagePutRow(img
, 0, y
, img
->xsize
, out
);
486 * Column major order...
489 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
491 xcount
--, x
+= xdir
, row
++)
495 TIFFReadScanline(tif
, scanline
, row
, 0);
496 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
, bit
= 128;
498 ycount
--, p
+= ydir
)
516 TIFFReadScanline(tif
, scanline
, row
, 0);
517 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
, bit
= 0xc0;
519 ycount
--, p
+= ydir
)
521 pixel
= *scanptr
& 0xc0;
525 *p
= (255 * pixel
/ 3) ^ zero
;
538 TIFFReadScanline(tif
, scanline
, row
, 0);
539 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
, bit
= 0xf0;
541 ycount
--, p
+= ydir
)
545 *p
= (255 * ((*scanptr
& 0xf0) >> 4) / 15) ^ zero
;
550 *p
= (255 * (*scanptr
& 0x0f) / 15) ^ zero
;
556 else if (ydir
< 0 || zero
|| alpha
)
558 TIFFReadScanline(tif
, scanline
, row
, 0);
564 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
566 ycount
--, p
+= ydir
, scanptr
+= 2)
567 *p
= (scanptr
[1] * (255 - scanptr
[0]) +
568 (255 - scanptr
[1]) * 255) / 255;
572 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
574 ycount
--, p
+= ydir
, scanptr
+= 2)
575 *p
= (scanptr
[1] * scanptr
[0] +
576 (255 - scanptr
[1]) * 255) / 255;
583 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
585 ycount
--, p
+= ydir
, scanptr
++)
590 for (ycount
= img
->ysize
, p
= in
+ ystart
, scanptr
= scanline
;
592 ycount
--, p
+= ydir
, scanptr
++)
598 TIFFReadScanline(tif
, in
, row
, 0);
600 if (img
->colorspace
== IMAGE_WHITE
)
603 ImageLut(in
, img
->ysize
, lut
);
605 ImagePutCol(img
, x
, 0, img
->ysize
, in
);
609 switch (img
->colorspace
)
612 ImageWhiteToRGB(in
, out
, img
->ysize
);
615 ImageWhiteToBlack(in
, out
, img
->ysize
);
618 ImageWhiteToCMY(in
, out
, img
->ysize
);
621 ImageWhiteToCMYK(in
, out
, img
->ysize
);
626 ImageLut(out
, img
->ysize
* bpp
, lut
);
628 ImagePutCol(img
, x
, 0, img
->ysize
, out
);
634 case PHOTOMETRIC_PALETTE
:
635 if (!TIFFGetField(tif
, TIFFTAG_COLORMAP
, &redcmap
, &greencmap
, &bluecmap
))
637 fputs("ERROR: No colormap tag in the file!\n", stderr
);
642 num_colors
= 1 << bits
;
644 for (c
= 0; c
< num_colors
; c
++)
651 if (orientation
< ORIENTATION_LEFTTOP
)
657 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
659 ycount
--, y
+= ydir
, row
++)
663 TIFFReadScanline(tif
, scanline
, row
, 0);
664 for (xcount
= img
->xsize
, scanptr
= scanline
,
665 p
= in
+ xstart
* 3, bit
= 128;
667 xcount
--, p
+= pstep
)
693 TIFFReadScanline(tif
, scanline
, row
, 0);
694 for (xcount
= img
->xsize
, scanptr
= scanline
,
695 p
= in
+ xstart
* 3, bit
= 0xc0;
697 xcount
--, p
+= pstep
)
699 pixel
= *scanptr
& bit
;
703 p
[0] = redcmap
[pixel
];
704 p
[1] = greencmap
[pixel
];
705 p
[2] = bluecmap
[pixel
];
718 TIFFReadScanline(tif
, scanline
, row
, 0);
719 for (xcount
= img
->xsize
, scanptr
= scanline
,
720 p
= in
+ 3 * xstart
, bit
= 0xf0;
722 xcount
--, p
+= pstep
)
726 pixel
= (*scanptr
& 0xf0) >> 4;
727 p
[0] = redcmap
[pixel
];
728 p
[1] = greencmap
[pixel
];
729 p
[2] = bluecmap
[pixel
];
734 pixel
= *scanptr
++ & 0x0f;
735 p
[0] = redcmap
[pixel
];
736 p
[1] = greencmap
[pixel
];
737 p
[2] = bluecmap
[pixel
];
744 TIFFReadScanline(tif
, scanline
, row
, 0);
746 for (xcount
= img
->xsize
, p
= in
+ 3 * xstart
, scanptr
= scanline
;
748 xcount
--, p
+= pstep
)
750 p
[0] = redcmap
[*scanptr
];
751 p
[1] = greencmap
[*scanptr
];
752 p
[2] = bluecmap
[*scanptr
++];
756 if (img
->colorspace
== IMAGE_RGB
)
759 ImageLut(in
, img
->xsize
* 3, lut
);
761 ImagePutRow(img
, 0, y
, img
->xsize
, in
);
765 switch (img
->colorspace
)
768 ImageRGBToWhite(in
, out
, img
->xsize
);
771 ImageRGBToBlack(in
, out
, img
->xsize
);
774 ImageRGBToCMY(in
, out
, img
->xsize
);
777 ImageRGBToCMYK(in
, out
, img
->xsize
);
782 ImageLut(out
, img
->xsize
* bpp
, lut
);
784 ImagePutRow(img
, 0, y
, img
->xsize
, out
);
791 * Column major order...
794 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
796 xcount
--, x
+= xdir
, row
++)
800 TIFFReadScanline(tif
, scanline
, row
, 0);
801 for (ycount
= img
->ysize
, scanptr
= scanline
,
802 p
= in
+ 3 * ystart
, bit
= 128;
804 ycount
--, p
+= ydir
)
830 TIFFReadScanline(tif
, scanline
, row
, 0);
831 for (ycount
= img
->ysize
, scanptr
= scanline
,
832 p
= in
+ 3 * ystart
, bit
= 0xc0;
834 ycount
--, p
+= ydir
)
836 pixel
= *scanptr
& 0xc0;
840 p
[0] = redcmap
[pixel
];
841 p
[1] = greencmap
[pixel
];
842 p
[2] = bluecmap
[pixel
];
855 TIFFReadScanline(tif
, scanline
, row
, 0);
856 for (ycount
= img
->ysize
, scanptr
= scanline
,
857 p
= in
+ 3 * ystart
, bit
= 0xf0;
859 ycount
--, p
+= ydir
)
863 pixel
= (*scanptr
& 0xf0) >> 4;
864 p
[0] = redcmap
[pixel
];
865 p
[1] = greencmap
[pixel
];
866 p
[2] = bluecmap
[pixel
];
871 pixel
= *scanptr
++ & 0x0f;
872 p
[0] = redcmap
[pixel
];
873 p
[1] = greencmap
[pixel
];
874 p
[2] = bluecmap
[pixel
];
881 TIFFReadScanline(tif
, scanline
, row
, 0);
883 for (ycount
= img
->ysize
, p
= in
+ 3 * ystart
, scanptr
= scanline
;
885 ycount
--, p
+= ydir
)
887 p
[0] = redcmap
[*scanptr
];
888 p
[1] = greencmap
[*scanptr
];
889 p
[2] = bluecmap
[*scanptr
++];
893 if (img
->colorspace
== IMAGE_RGB
)
896 ImageLut(in
, img
->ysize
* 3, lut
);
898 ImagePutCol(img
, x
, 0, img
->ysize
, in
);
902 switch (img
->colorspace
)
905 ImageRGBToWhite(in
, out
, img
->ysize
);
908 ImageRGBToBlack(in
, out
, img
->ysize
);
911 ImageRGBToCMY(in
, out
, img
->ysize
);
914 ImageRGBToCMYK(in
, out
, img
->ysize
);
919 ImageLut(out
, img
->ysize
* bpp
, lut
);
921 ImagePutCol(img
, x
, 0, img
->ysize
, out
);
927 case PHOTOMETRIC_RGB
:
928 if (orientation
< ORIENTATION_LEFTTOP
)
934 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
936 ycount
--, y
+= ydir
, row
++)
940 TIFFReadScanline(tif
, scanline
, row
, 0);
941 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3, bit
= 0xf0;
943 xcount
--, p
+= pstep
)
945 if (*scanptr
& bit
& 0x88)
950 if (*scanptr
& bit
& 0x44)
955 if (*scanptr
& bit
& 0x22)
971 TIFFReadScanline(tif
, scanline
, row
, 0);
972 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
974 xcount
--, p
+= pstep
, scanptr
++)
976 pixel
= *scanptr
>> 2;
977 p
[0] = 255 * (pixel
& 3) / 3;
979 p
[1] = 255 * (pixel
& 3) / 3;
981 p
[2] = 255 * (pixel
& 3) / 3;
986 TIFFReadScanline(tif
, scanline
, row
, 0);
987 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
989 xcount
-= 2, p
+= 2 * pstep
, scanptr
+= 3)
992 p
[1] = 255 * (pixel
& 15) / 15;
994 p
[0] = 255 * (pixel
& 15) / 15;
996 p
[2] = 255 * ((pixel
>> 4) & 15) / 15;
1000 p
[pstep
+ 0] = 255 * (pixel
& 15) / 15;
1002 p
[pstep
+ 2] = 255 * (pixel
& 15) / 15;
1004 p
[pstep
+ 1] = 255 * (pixel
& 15) / 15;
1008 else if (xdir
< 0 || alpha
)
1010 TIFFReadScanline(tif
, scanline
, row
, 0);
1014 for (xcount
= img
->xsize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1016 xcount
--, p
+= pstep
, scanptr
+= 4)
1018 p
[0] = (scanptr
[0] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1019 p
[1] = (scanptr
[1] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1020 p
[2] = (scanptr
[2] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1025 for (xcount
= img
->xsize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1027 xcount
--, p
+= pstep
, scanptr
+= 3)
1036 TIFFReadScanline(tif
, in
, row
, 0);
1038 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1039 ImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
1041 if (img
->colorspace
== IMAGE_RGB
)
1044 ImageLut(in
, img
->xsize
* 3, lut
);
1046 ImagePutRow(img
, 0, y
, img
->xsize
, in
);
1050 switch (img
->colorspace
)
1053 ImageRGBToWhite(in
, out
, img
->xsize
);
1056 ImageRGBToBlack(in
, out
, img
->xsize
);
1059 ImageRGBToCMY(in
, out
, img
->xsize
);
1062 ImageRGBToCMYK(in
, out
, img
->xsize
);
1067 ImageLut(out
, img
->xsize
* bpp
, lut
);
1069 ImagePutRow(img
, 0, y
, img
->xsize
, out
);
1076 * Column major order...
1079 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
1081 xcount
--, x
+= xdir
, row
++)
1085 TIFFReadScanline(tif
, scanline
, row
, 0);
1086 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
* 3, bit
= 0xf0;
1088 ycount
--, p
+= pstep
)
1090 if (*scanptr
& bit
& 0x88)
1095 if (*scanptr
& bit
& 0x44)
1100 if (*scanptr
& bit
& 0x22)
1116 TIFFReadScanline(tif
, scanline
, row
, 0);
1117 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
* 3;
1119 ycount
--, p
+= pstep
, scanptr
++)
1121 pixel
= *scanptr
>> 2;
1122 p
[0] = 255 * (pixel
& 3) / 3;
1124 p
[1] = 255 * (pixel
& 3) / 3;
1126 p
[2] = 255 * (pixel
& 3) / 3;
1131 TIFFReadScanline(tif
, scanline
, row
, 0);
1132 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ ystart
* 3;
1134 ycount
-= 2, p
+= 2 * pstep
, scanptr
+= 3)
1137 p
[1] = 255 * (pixel
& 15) / 15;
1139 p
[0] = 255 * (pixel
& 15) / 15;
1141 p
[2] = 255 * ((pixel
>> 4) & 15) / 15;
1145 p
[pstep
+ 0] = 255 * (pixel
& 15) / 15;
1147 p
[pstep
+ 2] = 255 * (pixel
& 15) / 15;
1149 p
[pstep
+ 1] = 255 * (pixel
& 15) / 15;
1153 else if (ydir
< 0 || alpha
)
1155 TIFFReadScanline(tif
, scanline
, row
, 0);
1159 for (ycount
= img
->ysize
, p
= in
+ ystart
* 3, scanptr
= scanline
;
1161 ycount
--, p
+= pstep
, scanptr
+= 4)
1163 p
[0] = (scanptr
[0] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1164 p
[1] = (scanptr
[1] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1165 p
[2] = (scanptr
[2] * scanptr
[3] + 255 * (255 - scanptr
[3])) / 255;
1170 for (ycount
= img
->ysize
, p
= in
+ ystart
* 3, scanptr
= scanline
;
1172 ycount
--, p
+= pstep
, scanptr
+= 3)
1181 TIFFReadScanline(tif
, in
, row
, 0);
1183 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1184 ImageRGBAdjust(in
, img
->ysize
, saturation
, hue
);
1186 if (img
->colorspace
== IMAGE_RGB
)
1189 ImageLut(in
, img
->ysize
* 3, lut
);
1191 ImagePutCol(img
, x
, 0, img
->ysize
, in
);
1195 switch (img
->colorspace
)
1198 ImageRGBToWhite(in
, out
, img
->ysize
);
1201 ImageRGBToBlack(in
, out
, img
->ysize
);
1204 ImageRGBToCMY(in
, out
, img
->ysize
);
1207 ImageRGBToCMYK(in
, out
, img
->ysize
);
1212 ImageLut(out
, img
->ysize
* bpp
, lut
);
1214 ImagePutCol(img
, x
, 0, img
->ysize
, out
);
1220 case PHOTOMETRIC_SEPARATED
:
1221 inkset
= INKSET_CMYK
;
1224 #ifdef TIFFTAG_NUMBEROFINKS
1225 if (!TIFFGetField(tif
, TIFFTAG_INKSET
, &inkset
) &&
1226 !TIFFGetField(tif
, TIFFTAG_NUMBEROFINKS
, &numinks
))
1228 if (!TIFFGetField(tif
, TIFFTAG_INKSET
, &inkset
))
1229 #endif /* TIFFTAG_NUMBEROFINKS */
1231 fputs("WARNING: No inkset or number-of-inks tag in the file!\n", stderr
);
1234 if (inkset
== INKSET_CMYK
|| numinks
== 4)
1236 if (orientation
< ORIENTATION_LEFTTOP
)
1239 * Row major order...
1242 for (y
= ystart
, ycount
= img
->ysize
, row
= 0;
1244 ycount
--, y
+= ydir
, row
++)
1248 TIFFReadScanline(tif
, scanline
, row
, 0);
1249 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3, bit
= 0xf0;
1251 xcount
--, p
+= pstep
)
1253 if (*scanptr
& bit
& 0x11)
1261 if (*scanptr
& bit
& 0x88)
1266 if (*scanptr
& bit
& 0x44)
1271 if (*scanptr
& bit
& 0x22)
1288 TIFFReadScanline(tif
, scanline
, row
, 0);
1289 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1291 xcount
--, p
+= pstep
, scanptr
++)
1294 k
= 255 * (pixel
& 3) / 3;
1304 b
= 255 - 255 * (pixel
& 3) / 3 - k
;
1313 g
= 255 - 255 * (pixel
& 3) / 3 - k
;
1322 r
= 255 - 255 * (pixel
& 3) / 3 - k
;
1334 TIFFReadScanline(tif
, scanline
, row
, 0);
1335 for (xcount
= img
->xsize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1337 xcount
--, p
+= pstep
, scanptr
+= 2)
1340 k
= 255 * (pixel
& 15) / 15;
1350 b
= 255 - 255 * (pixel
& 15) / 15 - k
;
1359 g
= 255 - 255 * (pixel
& 15) / 15 - k
;
1368 r
= 255 - 255 * (pixel
& 15) / 15 - k
;
1378 else if (img
->colorspace
== IMAGE_CMYK
)
1380 TIFFReadScanline(tif
, scanline
, row
, 0);
1381 ImagePutRow(img
, 0, y
, img
->xsize
, scanline
);
1385 TIFFReadScanline(tif
, scanline
, row
, 0);
1387 for (xcount
= img
->xsize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1389 xcount
--, p
+= pstep
, scanptr
+= 4)
1400 r
= 255 - scanptr
[0] - k
;
1408 g
= 255 - scanptr
[1] - k
;
1416 b
= 255 - scanptr
[2] - k
;
1427 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1428 ImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
1430 if (img
->colorspace
== IMAGE_RGB
)
1433 ImageLut(in
, img
->xsize
* 3, lut
);
1435 ImagePutRow(img
, 0, y
, img
->xsize
, in
);
1437 else if (img
->colorspace
== IMAGE_WHITE
)
1439 switch (img
->colorspace
)
1442 ImageRGBToWhite(in
, out
, img
->xsize
);
1445 ImageRGBToBlack(in
, out
, img
->xsize
);
1448 ImageRGBToCMY(in
, out
, img
->xsize
);
1451 ImageRGBToCMYK(in
, out
, img
->xsize
);
1456 ImageLut(out
, img
->xsize
* 3, lut
);
1458 ImagePutRow(img
, 0, y
, img
->xsize
, out
);
1465 * Column major order...
1468 for (x
= xstart
, xcount
= img
->xsize
, row
= 0;
1470 xcount
--, x
+= xdir
, row
++)
1474 TIFFReadScanline(tif
, scanline
, row
, 0);
1475 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ xstart
* 3, bit
= 0xf0;
1477 ycount
--, p
+= pstep
)
1479 if (*scanptr
& bit
& 0x11)
1487 if (*scanptr
& bit
& 0x88)
1492 if (*scanptr
& bit
& 0x44)
1497 if (*scanptr
& bit
& 0x22)
1514 TIFFReadScanline(tif
, scanline
, row
, 0);
1515 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1517 ycount
--, p
+= pstep
, scanptr
++)
1520 k
= 255 * (pixel
& 3) / 3;
1530 b
= 255 - 255 * (pixel
& 3) / 3 - k
;
1539 g
= 255 - 255 * (pixel
& 3) / 3 - k
;
1548 r
= 255 - 255 * (pixel
& 3) / 3 - k
;
1560 TIFFReadScanline(tif
, scanline
, row
, 0);
1561 for (ycount
= img
->ysize
, scanptr
= scanline
, p
= in
+ xstart
* 3;
1563 ycount
--, p
+= pstep
, scanptr
+= 2)
1566 k
= 255 * (pixel
& 15) / 15;
1576 b
= 255 - 255 * (pixel
& 15) / 15 - k
;
1585 g
= 255 - 255 * (pixel
& 15) / 15 - k
;
1594 r
= 255 - 255 * (pixel
& 15) / 15 - k
;
1604 else if (img
->colorspace
== IMAGE_CMYK
)
1606 TIFFReadScanline(tif
, scanline
, row
, 0);
1607 ImagePutCol(img
, x
, 0, img
->ysize
, scanline
);
1611 TIFFReadScanline(tif
, scanline
, row
, 0);
1613 for (ycount
= img
->ysize
, p
= in
+ xstart
* 3, scanptr
= scanline
;
1615 ycount
--, p
+= pstep
, scanptr
+= 4)
1626 r
= 255 - scanptr
[0] - k
;
1634 g
= 255 - scanptr
[1] - k
;
1642 b
= 255 - scanptr
[2] - k
;
1653 if ((saturation
!= 100 || hue
!= 0) && bpp
> 1)
1654 ImageRGBAdjust(in
, img
->ysize
, saturation
, hue
);
1656 if (img
->colorspace
== IMAGE_RGB
)
1659 ImageLut(in
, img
->ysize
* 3, lut
);
1661 ImagePutCol(img
, x
, 0, img
->ysize
, in
);
1663 else if (img
->colorspace
== IMAGE_WHITE
)
1665 switch (img
->colorspace
)
1668 ImageRGBToWhite(in
, out
, img
->ysize
);
1671 ImageRGBToBlack(in
, out
, img
->ysize
);
1674 ImageRGBToCMY(in
, out
, img
->ysize
);
1677 ImageRGBToCMYK(in
, out
, img
->ysize
);
1682 ImageLut(out
, img
->ysize
* bpp
, lut
);
1684 ImagePutCol(img
, x
, 0, img
->ysize
, out
);
1693 _TIFFfree(scanline
);
1698 fputs("ERROR: Unknown TIFF photometric value!\n", stderr
);
1703 * Free temporary buffers, close the TIFF file, and return.
1706 _TIFFfree(scanline
);
1715 #endif /* HAVE_LIBTIFF */