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