2c600d0cab5d6bd2accb524dc3cc770494d75dbb
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_getimage.c
1 /* $Id: tif_getimage.c,v 1.98 2016-11-18 02:47:45 bfriesen Exp $ */
2
3 /*
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library
29 *
30 * Read and return a packed RGBA image.
31 */
32
33 #include <precomp.h>
34 //#include <stdio.h>
35
36 static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
37 static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
38 static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
39 static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
40 static int PickContigCase(TIFFRGBAImage*);
41 static int PickSeparateCase(TIFFRGBAImage*);
42
43 static int BuildMapUaToAa(TIFFRGBAImage* img);
44 static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
45
46 static const char photoTag[] = "PhotometricInterpretation";
47
48 /*
49 * Helper constants used in Orientation tag handling
50 */
51 #define FLIP_VERTICALLY 0x01
52 #define FLIP_HORIZONTALLY 0x02
53
54 /*
55 * Color conversion constants. We will define display types here.
56 */
57
58 static const TIFFDisplay display_sRGB = {
59 { /* XYZ -> luminance matrix */
60 { 3.2410F, -1.5374F, -0.4986F },
61 { -0.9692F, 1.8760F, 0.0416F },
62 { 0.0556F, -0.2040F, 1.0570F }
63 },
64 100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
65 255, 255, 255, /* Pixel values for ref. white */
66 1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
67 2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
68 };
69
70 /*
71 * Check the image to see if TIFFReadRGBAImage can deal with it.
72 * 1/0 is returned according to whether or not the image can
73 * be handled. If 0 is returned, emsg contains the reason
74 * why it is being rejected.
75 */
76 int
77 TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
78 {
79 TIFFDirectory* td = &tif->tif_dir;
80 uint16 photometric;
81 int colorchannels;
82
83 if (!tif->tif_decodestatus) {
84 sprintf(emsg, "Sorry, requested compression method is not configured");
85 return (0);
86 }
87 switch (td->td_bitspersample) {
88 case 1:
89 case 2:
90 case 4:
91 case 8:
92 case 16:
93 break;
94 default:
95 sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
96 td->td_bitspersample);
97 return (0);
98 }
99 if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP) {
100 sprintf(emsg, "Sorry, can not handle images with IEEE floating-point samples");
101 return (0);
102 }
103 colorchannels = td->td_samplesperpixel - td->td_extrasamples;
104 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
105 switch (colorchannels) {
106 case 1:
107 photometric = PHOTOMETRIC_MINISBLACK;
108 break;
109 case 3:
110 photometric = PHOTOMETRIC_RGB;
111 break;
112 default:
113 sprintf(emsg, "Missing needed %s tag", photoTag);
114 return (0);
115 }
116 }
117 switch (photometric) {
118 case PHOTOMETRIC_MINISWHITE:
119 case PHOTOMETRIC_MINISBLACK:
120 case PHOTOMETRIC_PALETTE:
121 if (td->td_planarconfig == PLANARCONFIG_CONTIG
122 && td->td_samplesperpixel != 1
123 && td->td_bitspersample < 8 ) {
124 sprintf(emsg,
125 "Sorry, can not handle contiguous data with %s=%d, "
126 "and %s=%d and Bits/Sample=%d",
127 photoTag, photometric,
128 "Samples/pixel", td->td_samplesperpixel,
129 td->td_bitspersample);
130 return (0);
131 }
132 /*
133 * We should likely validate that any extra samples are either
134 * to be ignored, or are alpha, and if alpha we should try to use
135 * them. But for now we won't bother with this.
136 */
137 break;
138 case PHOTOMETRIC_YCBCR:
139 /*
140 * TODO: if at all meaningful and useful, make more complete
141 * support check here, or better still, refactor to let supporting
142 * code decide whether there is support and what meaningfull
143 * error to return
144 */
145 break;
146 case PHOTOMETRIC_RGB:
147 if (colorchannels < 3) {
148 sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
149 "Color channels", colorchannels);
150 return (0);
151 }
152 break;
153 case PHOTOMETRIC_SEPARATED:
154 {
155 uint16 inkset;
156 TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
157 if (inkset != INKSET_CMYK) {
158 sprintf(emsg,
159 "Sorry, can not handle separated image with %s=%d",
160 "InkSet", inkset);
161 return 0;
162 }
163 if (td->td_samplesperpixel < 4) {
164 sprintf(emsg,
165 "Sorry, can not handle separated image with %s=%d",
166 "Samples/pixel", td->td_samplesperpixel);
167 return 0;
168 }
169 break;
170 }
171 case PHOTOMETRIC_LOGL:
172 if (td->td_compression != COMPRESSION_SGILOG) {
173 sprintf(emsg, "Sorry, LogL data must have %s=%d",
174 "Compression", COMPRESSION_SGILOG);
175 return (0);
176 }
177 break;
178 case PHOTOMETRIC_LOGLUV:
179 if (td->td_compression != COMPRESSION_SGILOG &&
180 td->td_compression != COMPRESSION_SGILOG24) {
181 sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
182 "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
183 return (0);
184 }
185 if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
186 sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
187 "Planarconfiguration", td->td_planarconfig);
188 return (0);
189 }
190 if ( td->td_samplesperpixel != 3 || colorchannels != 3 ) {
191 sprintf(emsg,
192 "Sorry, can not handle image with %s=%d, %s=%d",
193 "Samples/pixel", td->td_samplesperpixel,
194 "colorchannels", colorchannels);
195 return 0;
196 }
197 break;
198 case PHOTOMETRIC_CIELAB:
199 if ( td->td_samplesperpixel != 3 || colorchannels != 3 || td->td_bitspersample != 8 ) {
200 sprintf(emsg,
201 "Sorry, can not handle image with %s=%d, %s=%d and %s=%d",
202 "Samples/pixel", td->td_samplesperpixel,
203 "colorchannels", colorchannels,
204 "Bits/sample", td->td_bitspersample);
205 return 0;
206 }
207 break;
208 default:
209 sprintf(emsg, "Sorry, can not handle image with %s=%d",
210 photoTag, photometric);
211 return (0);
212 }
213 return (1);
214 }
215
216 void
217 TIFFRGBAImageEnd(TIFFRGBAImage* img)
218 {
219 if (img->Map) {
220 _TIFFfree(img->Map);
221 img->Map = NULL;
222 }
223 if (img->BWmap) {
224 _TIFFfree(img->BWmap);
225 img->BWmap = NULL;
226 }
227 if (img->PALmap) {
228 _TIFFfree(img->PALmap);
229 img->PALmap = NULL;
230 }
231 if (img->ycbcr) {
232 _TIFFfree(img->ycbcr);
233 img->ycbcr = NULL;
234 }
235 if (img->cielab) {
236 _TIFFfree(img->cielab);
237 img->cielab = NULL;
238 }
239 if (img->UaToAa) {
240 _TIFFfree(img->UaToAa);
241 img->UaToAa = NULL;
242 }
243 if (img->Bitdepth16To8) {
244 _TIFFfree(img->Bitdepth16To8);
245 img->Bitdepth16To8 = NULL;
246 }
247
248 if( img->redcmap ) {
249 _TIFFfree( img->redcmap );
250 _TIFFfree( img->greencmap );
251 _TIFFfree( img->bluecmap );
252 img->redcmap = img->greencmap = img->bluecmap = NULL;
253 }
254 }
255
256 static int
257 isCCITTCompression(TIFF* tif)
258 {
259 uint16 compress;
260 TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
261 return (compress == COMPRESSION_CCITTFAX3 ||
262 compress == COMPRESSION_CCITTFAX4 ||
263 compress == COMPRESSION_CCITTRLE ||
264 compress == COMPRESSION_CCITTRLEW);
265 }
266
267 int
268 TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
269 {
270 uint16* sampleinfo;
271 uint16 extrasamples;
272 uint16 planarconfig;
273 uint16 compress;
274 int colorchannels;
275 uint16 *red_orig, *green_orig, *blue_orig;
276 int n_color;
277
278 if( !TIFFRGBAImageOK(tif, emsg) )
279 return 0;
280
281 /* Initialize to normal values */
282 img->row_offset = 0;
283 img->col_offset = 0;
284 img->redcmap = NULL;
285 img->greencmap = NULL;
286 img->bluecmap = NULL;
287 img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
288
289 img->tif = tif;
290 img->stoponerr = stop;
291 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
292 switch (img->bitspersample) {
293 case 1:
294 case 2:
295 case 4:
296 case 8:
297 case 16:
298 break;
299 default:
300 sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
301 img->bitspersample);
302 goto fail_return;
303 }
304 img->alpha = 0;
305 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
306 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
307 &extrasamples, &sampleinfo);
308 if (extrasamples >= 1)
309 {
310 switch (sampleinfo[0]) {
311 case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
312 if (img->samplesperpixel > 3) /* correct info about alpha channel */
313 img->alpha = EXTRASAMPLE_ASSOCALPHA;
314 break;
315 case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
316 case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
317 img->alpha = sampleinfo[0];
318 break;
319 }
320 }
321
322 #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
323 if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
324 img->photometric = PHOTOMETRIC_MINISWHITE;
325
326 if( extrasamples == 0
327 && img->samplesperpixel == 4
328 && img->photometric == PHOTOMETRIC_RGB )
329 {
330 img->alpha = EXTRASAMPLE_ASSOCALPHA;
331 extrasamples = 1;
332 }
333 #endif
334
335 colorchannels = img->samplesperpixel - extrasamples;
336 TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
337 TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
338 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
339 switch (colorchannels) {
340 case 1:
341 if (isCCITTCompression(tif))
342 img->photometric = PHOTOMETRIC_MINISWHITE;
343 else
344 img->photometric = PHOTOMETRIC_MINISBLACK;
345 break;
346 case 3:
347 img->photometric = PHOTOMETRIC_RGB;
348 break;
349 default:
350 sprintf(emsg, "Missing needed %s tag", photoTag);
351 goto fail_return;
352 }
353 }
354 switch (img->photometric) {
355 case PHOTOMETRIC_PALETTE:
356 if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
357 &red_orig, &green_orig, &blue_orig)) {
358 sprintf(emsg, "Missing required \"Colormap\" tag");
359 goto fail_return;
360 }
361
362 /* copy the colormaps so we can modify them */
363 n_color = (1U << img->bitspersample);
364 img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
365 img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
366 img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
367 if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
368 sprintf(emsg, "Out of memory for colormap copy");
369 goto fail_return;
370 }
371
372 _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
373 _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
374 _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
375
376 /* fall through... */
377 case PHOTOMETRIC_MINISWHITE:
378 case PHOTOMETRIC_MINISBLACK:
379 if (planarconfig == PLANARCONFIG_CONTIG
380 && img->samplesperpixel != 1
381 && img->bitspersample < 8 ) {
382 sprintf(emsg,
383 "Sorry, can not handle contiguous data with %s=%d, "
384 "and %s=%d and Bits/Sample=%d",
385 photoTag, img->photometric,
386 "Samples/pixel", img->samplesperpixel,
387 img->bitspersample);
388 goto fail_return;
389 }
390 break;
391 case PHOTOMETRIC_YCBCR:
392 /* It would probably be nice to have a reality check here. */
393 if (planarconfig == PLANARCONFIG_CONTIG)
394 /* can rely on libjpeg to convert to RGB */
395 /* XXX should restore current state on exit */
396 switch (compress) {
397 case COMPRESSION_JPEG:
398 /*
399 * TODO: when complete tests verify complete desubsampling
400 * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
401 * favor of tif_getimage.c native handling
402 */
403 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
404 img->photometric = PHOTOMETRIC_RGB;
405 break;
406 default:
407 /* do nothing */;
408 break;
409 }
410 /*
411 * TODO: if at all meaningful and useful, make more complete
412 * support check here, or better still, refactor to let supporting
413 * code decide whether there is support and what meaningfull
414 * error to return
415 */
416 break;
417 case PHOTOMETRIC_RGB:
418 if (colorchannels < 3) {
419 sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
420 "Color channels", colorchannels);
421 goto fail_return;
422 }
423 break;
424 case PHOTOMETRIC_SEPARATED:
425 {
426 uint16 inkset;
427 TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
428 if (inkset != INKSET_CMYK) {
429 sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
430 "InkSet", inkset);
431 goto fail_return;
432 }
433 if (img->samplesperpixel < 4) {
434 sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
435 "Samples/pixel", img->samplesperpixel);
436 goto fail_return;
437 }
438 }
439 break;
440 case PHOTOMETRIC_LOGL:
441 if (compress != COMPRESSION_SGILOG) {
442 sprintf(emsg, "Sorry, LogL data must have %s=%d",
443 "Compression", COMPRESSION_SGILOG);
444 goto fail_return;
445 }
446 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
447 img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
448 img->bitspersample = 8;
449 break;
450 case PHOTOMETRIC_LOGLUV:
451 if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
452 sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
453 "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
454 goto fail_return;
455 }
456 if (planarconfig != PLANARCONFIG_CONTIG) {
457 sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
458 "Planarconfiguration", planarconfig);
459 return (0);
460 }
461 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
462 img->photometric = PHOTOMETRIC_RGB; /* little white lie */
463 img->bitspersample = 8;
464 break;
465 case PHOTOMETRIC_CIELAB:
466 break;
467 default:
468 sprintf(emsg, "Sorry, can not handle image with %s=%d",
469 photoTag, img->photometric);
470 goto fail_return;
471 }
472 img->Map = NULL;
473 img->BWmap = NULL;
474 img->PALmap = NULL;
475 img->ycbcr = NULL;
476 img->cielab = NULL;
477 img->UaToAa = NULL;
478 img->Bitdepth16To8 = NULL;
479 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
480 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
481 TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
482 img->isContig =
483 !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
484 if (img->isContig) {
485 if (!PickContigCase(img)) {
486 sprintf(emsg, "Sorry, can not handle image");
487 goto fail_return;
488 }
489 } else {
490 if (!PickSeparateCase(img)) {
491 sprintf(emsg, "Sorry, can not handle image");
492 goto fail_return;
493 }
494 }
495 return 1;
496
497 fail_return:
498 _TIFFfree( img->redcmap );
499 _TIFFfree( img->greencmap );
500 _TIFFfree( img->bluecmap );
501 img->redcmap = img->greencmap = img->bluecmap = NULL;
502 return 0;
503 }
504
505 int
506 TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
507 {
508 if (img->get == NULL) {
509 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
510 return (0);
511 }
512 if (img->put.any == NULL) {
513 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
514 "No \"put\" routine setupl; probably can not handle image format");
515 return (0);
516 }
517 return (*img->get)(img, raster, w, h);
518 }
519
520 /*
521 * Read the specified image into an ABGR-format rastertaking in account
522 * specified orientation.
523 */
524 int
525 TIFFReadRGBAImageOriented(TIFF* tif,
526 uint32 rwidth, uint32 rheight, uint32* raster,
527 int orientation, int stop)
528 {
529 char emsg[1024] = "";
530 TIFFRGBAImage img;
531 int ok;
532
533 if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
534 img.req_orientation = (uint16)orientation;
535 /* XXX verify rwidth and rheight against width and height */
536 ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
537 rwidth, img.height);
538 TIFFRGBAImageEnd(&img);
539 } else {
540 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
541 ok = 0;
542 }
543 return (ok);
544 }
545
546 /*
547 * Read the specified image into an ABGR-format raster. Use bottom left
548 * origin for raster by default.
549 */
550 int
551 TIFFReadRGBAImage(TIFF* tif,
552 uint32 rwidth, uint32 rheight, uint32* raster, int stop)
553 {
554 return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
555 ORIENTATION_BOTLEFT, stop);
556 }
557
558 static int
559 setorientation(TIFFRGBAImage* img)
560 {
561 switch (img->orientation) {
562 case ORIENTATION_TOPLEFT:
563 case ORIENTATION_LEFTTOP:
564 if (img->req_orientation == ORIENTATION_TOPRIGHT ||
565 img->req_orientation == ORIENTATION_RIGHTTOP)
566 return FLIP_HORIZONTALLY;
567 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
568 img->req_orientation == ORIENTATION_RIGHTBOT)
569 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
570 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
571 img->req_orientation == ORIENTATION_LEFTBOT)
572 return FLIP_VERTICALLY;
573 else
574 return 0;
575 case ORIENTATION_TOPRIGHT:
576 case ORIENTATION_RIGHTTOP:
577 if (img->req_orientation == ORIENTATION_TOPLEFT ||
578 img->req_orientation == ORIENTATION_LEFTTOP)
579 return FLIP_HORIZONTALLY;
580 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
581 img->req_orientation == ORIENTATION_RIGHTBOT)
582 return FLIP_VERTICALLY;
583 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
584 img->req_orientation == ORIENTATION_LEFTBOT)
585 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
586 else
587 return 0;
588 case ORIENTATION_BOTRIGHT:
589 case ORIENTATION_RIGHTBOT:
590 if (img->req_orientation == ORIENTATION_TOPLEFT ||
591 img->req_orientation == ORIENTATION_LEFTTOP)
592 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
593 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
594 img->req_orientation == ORIENTATION_RIGHTTOP)
595 return FLIP_VERTICALLY;
596 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
597 img->req_orientation == ORIENTATION_LEFTBOT)
598 return FLIP_HORIZONTALLY;
599 else
600 return 0;
601 case ORIENTATION_BOTLEFT:
602 case ORIENTATION_LEFTBOT:
603 if (img->req_orientation == ORIENTATION_TOPLEFT ||
604 img->req_orientation == ORIENTATION_LEFTTOP)
605 return FLIP_VERTICALLY;
606 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
607 img->req_orientation == ORIENTATION_RIGHTTOP)
608 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
609 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
610 img->req_orientation == ORIENTATION_RIGHTBOT)
611 return FLIP_HORIZONTALLY;
612 else
613 return 0;
614 default: /* NOTREACHED */
615 return 0;
616 }
617 }
618
619 /*
620 * Get an tile-organized image that has
621 * PlanarConfiguration contiguous if SamplesPerPixel > 1
622 * or
623 * SamplesPerPixel == 1
624 */
625 static int
626 gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
627 {
628 TIFF* tif = img->tif;
629 tileContigRoutine put = img->put.contig;
630 uint32 col, row, y, rowstoread;
631 tmsize_t pos;
632 uint32 tw, th;
633 unsigned char* buf;
634 int32 fromskew, toskew;
635 uint32 nrow;
636 int ret = 1, flip;
637 uint32 this_tw, tocol;
638 int32 this_toskew, leftmost_toskew;
639 int32 leftmost_fromskew;
640 uint32 leftmost_tw;
641
642 buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
643 if (buf == 0) {
644 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
645 return (0);
646 }
647 _TIFFmemset(buf, 0, TIFFTileSize(tif));
648 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
649 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
650
651 flip = setorientation(img);
652 if (flip & FLIP_VERTICALLY) {
653 y = h - 1;
654 toskew = -(int32)(tw + w);
655 }
656 else {
657 y = 0;
658 toskew = -(int32)(tw - w);
659 }
660
661 /*
662 * Leftmost tile is clipped on left side if col_offset > 0.
663 */
664 leftmost_fromskew = img->col_offset % tw;
665 leftmost_tw = tw - leftmost_fromskew;
666 leftmost_toskew = toskew + leftmost_fromskew;
667 for (row = 0; row < h; row += nrow)
668 {
669 rowstoread = th - (row + img->row_offset) % th;
670 nrow = (row + rowstoread > h ? h - row : rowstoread);
671 fromskew = leftmost_fromskew;
672 this_tw = leftmost_tw;
673 this_toskew = leftmost_toskew;
674 tocol = 0;
675 col = img->col_offset;
676 while (tocol < w)
677 {
678 if (TIFFReadTile(tif, buf, col,
679 row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
680 {
681 ret = 0;
682 break;
683 }
684 pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif) + \
685 ((tmsize_t) fromskew * img->samplesperpixel);
686 if (tocol + this_tw > w)
687 {
688 /*
689 * Rightmost tile is clipped on right side.
690 */
691 fromskew = tw - (w - tocol);
692 this_tw = tw - fromskew;
693 this_toskew = toskew + fromskew;
694 }
695 (*put)(img, raster+y*w+tocol, tocol, y, this_tw, nrow, fromskew, this_toskew, buf + pos);
696 tocol += this_tw;
697 col += this_tw;
698 /*
699 * After the leftmost tile, tiles are no longer clipped on left side.
700 */
701 fromskew = 0;
702 this_tw = tw;
703 this_toskew = toskew;
704 }
705
706 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
707 }
708 _TIFFfree(buf);
709
710 if (flip & FLIP_HORIZONTALLY) {
711 uint32 line;
712
713 for (line = 0; line < h; line++) {
714 uint32 *left = raster + (line * w);
715 uint32 *right = left + w - 1;
716
717 while ( left < right ) {
718 uint32 temp = *left;
719 *left = *right;
720 *right = temp;
721 left++;
722 right--;
723 }
724 }
725 }
726
727 return (ret);
728 }
729
730 /*
731 * Get an tile-organized image that has
732 * SamplesPerPixel > 1
733 * PlanarConfiguration separated
734 * We assume that all such images are RGB.
735 */
736 static int
737 gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
738 {
739 TIFF* tif = img->tif;
740 tileSeparateRoutine put = img->put.separate;
741 uint32 col, row, y, rowstoread;
742 tmsize_t pos;
743 uint32 tw, th;
744 unsigned char* buf;
745 unsigned char* p0;
746 unsigned char* p1;
747 unsigned char* p2;
748 unsigned char* pa;
749 tmsize_t tilesize;
750 tmsize_t bufsize;
751 int32 fromskew, toskew;
752 int alpha = img->alpha;
753 uint32 nrow;
754 int ret = 1, flip;
755 uint16 colorchannels;
756 uint32 this_tw, tocol;
757 int32 this_toskew, leftmost_toskew;
758 int32 leftmost_fromskew;
759 uint32 leftmost_tw;
760
761 tilesize = TIFFTileSize(tif);
762 bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
763 if (bufsize == 0) {
764 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
765 return (0);
766 }
767 buf = (unsigned char*) _TIFFmalloc(bufsize);
768 if (buf == 0) {
769 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
770 return (0);
771 }
772 _TIFFmemset(buf, 0, bufsize);
773 p0 = buf;
774 p1 = p0 + tilesize;
775 p2 = p1 + tilesize;
776 pa = (alpha?(p2+tilesize):NULL);
777 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
778 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
779
780 flip = setorientation(img);
781 if (flip & FLIP_VERTICALLY) {
782 y = h - 1;
783 toskew = -(int32)(tw + w);
784 }
785 else {
786 y = 0;
787 toskew = -(int32)(tw - w);
788 }
789
790 switch( img->photometric )
791 {
792 case PHOTOMETRIC_MINISWHITE:
793 case PHOTOMETRIC_MINISBLACK:
794 case PHOTOMETRIC_PALETTE:
795 colorchannels = 1;
796 p2 = p1 = p0;
797 break;
798
799 default:
800 colorchannels = 3;
801 break;
802 }
803
804 /*
805 * Leftmost tile is clipped on left side if col_offset > 0.
806 */
807 leftmost_fromskew = img->col_offset % tw;
808 leftmost_tw = tw - leftmost_fromskew;
809 leftmost_toskew = toskew + leftmost_fromskew;
810 for (row = 0; row < h; row += nrow)
811 {
812 rowstoread = th - (row + img->row_offset) % th;
813 nrow = (row + rowstoread > h ? h - row : rowstoread);
814 fromskew = leftmost_fromskew;
815 this_tw = leftmost_tw;
816 this_toskew = leftmost_toskew;
817 tocol = 0;
818 col = img->col_offset;
819 while (tocol < w)
820 {
821 if (TIFFReadTile(tif, p0, col,
822 row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
823 {
824 ret = 0;
825 break;
826 }
827 if (colorchannels > 1
828 && TIFFReadTile(tif, p1, col,
829 row+img->row_offset,0,1) == (tmsize_t)(-1)
830 && img->stoponerr)
831 {
832 ret = 0;
833 break;
834 }
835 if (colorchannels > 1
836 && TIFFReadTile(tif, p2, col,
837 row+img->row_offset,0,2) == (tmsize_t)(-1)
838 && img->stoponerr)
839 {
840 ret = 0;
841 break;
842 }
843 if (alpha
844 && TIFFReadTile(tif,pa,col,
845 row+img->row_offset,0,colorchannels) == (tmsize_t)(-1)
846 && img->stoponerr)
847 {
848 ret = 0;
849 break;
850 }
851
852 pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif) + \
853 ((tmsize_t) fromskew * img->samplesperpixel);
854 if (tocol + this_tw > w)
855 {
856 /*
857 * Rightmost tile is clipped on right side.
858 */
859 fromskew = tw - (w - tocol);
860 this_tw = tw - fromskew;
861 this_toskew = toskew + fromskew;
862 }
863 (*put)(img, raster+y*w+tocol, tocol, y, this_tw, nrow, fromskew, this_toskew, \
864 p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
865 tocol += this_tw;
866 col += this_tw;
867 /*
868 * After the leftmost tile, tiles are no longer clipped on left side.
869 */
870 fromskew = 0;
871 this_tw = tw;
872 this_toskew = toskew;
873 }
874
875 y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
876 }
877
878 if (flip & FLIP_HORIZONTALLY) {
879 uint32 line;
880
881 for (line = 0; line < h; line++) {
882 uint32 *left = raster + (line * w);
883 uint32 *right = left + w - 1;
884
885 while ( left < right ) {
886 uint32 temp = *left;
887 *left = *right;
888 *right = temp;
889 left++;
890 right--;
891 }
892 }
893 }
894
895 _TIFFfree(buf);
896 return (ret);
897 }
898
899 /*
900 * Get a strip-organized image that has
901 * PlanarConfiguration contiguous if SamplesPerPixel > 1
902 * or
903 * SamplesPerPixel == 1
904 */
905 static int
906 gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
907 {
908 TIFF* tif = img->tif;
909 tileContigRoutine put = img->put.contig;
910 uint32 row, y, nrow, nrowsub, rowstoread;
911 tmsize_t pos;
912 unsigned char* buf;
913 uint32 rowsperstrip;
914 uint16 subsamplinghor,subsamplingver;
915 uint32 imagewidth = img->width;
916 tmsize_t scanline;
917 int32 fromskew, toskew;
918 int ret = 1, flip;
919
920 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
921 if( subsamplingver == 0 ) {
922 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Invalid vertical YCbCr subsampling");
923 return (0);
924 }
925
926 buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
927 if (buf == 0) {
928 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
929 return (0);
930 }
931 _TIFFmemset(buf, 0, TIFFStripSize(tif));
932
933 flip = setorientation(img);
934 if (flip & FLIP_VERTICALLY) {
935 y = h - 1;
936 toskew = -(int32)(w + w);
937 } else {
938 y = 0;
939 toskew = -(int32)(w - w);
940 }
941
942 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
943
944 scanline = TIFFScanlineSize(tif);
945 fromskew = (w < imagewidth ? imagewidth - w : 0);
946 for (row = 0; row < h; row += nrow)
947 {
948 rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
949 nrow = (row + rowstoread > h ? h - row : rowstoread);
950 nrowsub = nrow;
951 if ((nrowsub%subsamplingver)!=0)
952 nrowsub+=subsamplingver-nrowsub%subsamplingver;
953 if (TIFFReadEncodedStrip(tif,
954 TIFFComputeStrip(tif,row+img->row_offset, 0),
955 buf,
956 ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
957 && img->stoponerr)
958 {
959 ret = 0;
960 break;
961 }
962
963 pos = ((row + img->row_offset) % rowsperstrip) * scanline + \
964 ((tmsize_t) img->col_offset * img->samplesperpixel);
965 (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
966 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
967 }
968
969 if (flip & FLIP_HORIZONTALLY) {
970 uint32 line;
971
972 for (line = 0; line < h; line++) {
973 uint32 *left = raster + (line * w);
974 uint32 *right = left + w - 1;
975
976 while ( left < right ) {
977 uint32 temp = *left;
978 *left = *right;
979 *right = temp;
980 left++;
981 right--;
982 }
983 }
984 }
985
986 _TIFFfree(buf);
987 return (ret);
988 }
989
990 /*
991 * Get a strip-organized image with
992 * SamplesPerPixel > 1
993 * PlanarConfiguration separated
994 * We assume that all such images are RGB.
995 */
996 static int
997 gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
998 {
999 TIFF* tif = img->tif;
1000 tileSeparateRoutine put = img->put.separate;
1001 unsigned char *buf;
1002 unsigned char *p0, *p1, *p2, *pa;
1003 uint32 row, y, nrow, rowstoread;
1004 tmsize_t pos;
1005 tmsize_t scanline;
1006 uint32 rowsperstrip, offset_row;
1007 uint32 imagewidth = img->width;
1008 tmsize_t stripsize;
1009 tmsize_t bufsize;
1010 int32 fromskew, toskew;
1011 int alpha = img->alpha;
1012 int ret = 1, flip;
1013 uint16 colorchannels;
1014
1015 stripsize = TIFFStripSize(tif);
1016 bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
1017 if (bufsize == 0) {
1018 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
1019 return (0);
1020 }
1021 p0 = buf = (unsigned char *)_TIFFmalloc(bufsize);
1022 if (buf == 0) {
1023 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
1024 return (0);
1025 }
1026 _TIFFmemset(buf, 0, bufsize);
1027 p1 = p0 + stripsize;
1028 p2 = p1 + stripsize;
1029 pa = (alpha?(p2+stripsize):NULL);
1030
1031 flip = setorientation(img);
1032 if (flip & FLIP_VERTICALLY) {
1033 y = h - 1;
1034 toskew = -(int32)(w + w);
1035 }
1036 else {
1037 y = 0;
1038 toskew = -(int32)(w - w);
1039 }
1040
1041 switch( img->photometric )
1042 {
1043 case PHOTOMETRIC_MINISWHITE:
1044 case PHOTOMETRIC_MINISBLACK:
1045 case PHOTOMETRIC_PALETTE:
1046 colorchannels = 1;
1047 p2 = p1 = p0;
1048 break;
1049
1050 default:
1051 colorchannels = 3;
1052 break;
1053 }
1054
1055 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1056 scanline = TIFFScanlineSize(tif);
1057 fromskew = (w < imagewidth ? imagewidth - w : 0);
1058 for (row = 0; row < h; row += nrow)
1059 {
1060 rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
1061 nrow = (row + rowstoread > h ? h - row : rowstoread);
1062 offset_row = row + img->row_offset;
1063 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
1064 p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
1065 && img->stoponerr)
1066 {
1067 ret = 0;
1068 break;
1069 }
1070 if (colorchannels > 1
1071 && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
1072 p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
1073 && img->stoponerr)
1074 {
1075 ret = 0;
1076 break;
1077 }
1078 if (colorchannels > 1
1079 && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
1080 p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
1081 && img->stoponerr)
1082 {
1083 ret = 0;
1084 break;
1085 }
1086 if (alpha)
1087 {
1088 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
1089 pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
1090 && img->stoponerr)
1091 {
1092 ret = 0;
1093 break;
1094 }
1095 }
1096
1097 pos = ((row + img->row_offset) % rowsperstrip) * scanline + \
1098 ((tmsize_t) img->col_offset * img->samplesperpixel);
1099 (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
1100 p2 + pos, (alpha?(pa+pos):NULL));
1101 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
1102 }
1103
1104 if (flip & FLIP_HORIZONTALLY) {
1105 uint32 line;
1106
1107 for (line = 0; line < h; line++) {
1108 uint32 *left = raster + (line * w);
1109 uint32 *right = left + w - 1;
1110
1111 while ( left < right ) {
1112 uint32 temp = *left;
1113 *left = *right;
1114 *right = temp;
1115 left++;
1116 right--;
1117 }
1118 }
1119 }
1120
1121 _TIFFfree(buf);
1122 return (ret);
1123 }
1124
1125 /*
1126 * The following routines move decoded data returned
1127 * from the TIFF library into rasters filled with packed
1128 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
1129 *
1130 * The routines have been created according to the most
1131 * important cases and optimized. PickContigCase and
1132 * PickSeparateCase analyze the parameters and select
1133 * the appropriate "get" and "put" routine to use.
1134 */
1135 #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
1136 #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
1137 #define REPEAT2(op) op; op
1138 #define CASE8(x,op) \
1139 switch (x) { \
1140 case 7: op; case 6: op; case 5: op; \
1141 case 4: op; case 3: op; case 2: op; \
1142 case 1: op; \
1143 }
1144 #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
1145 #define NOP
1146
1147 #define UNROLL8(w, op1, op2) { \
1148 uint32 _x; \
1149 for (_x = w; _x >= 8; _x -= 8) { \
1150 op1; \
1151 REPEAT8(op2); \
1152 } \
1153 if (_x > 0) { \
1154 op1; \
1155 CASE8(_x,op2); \
1156 } \
1157 }
1158 #define UNROLL4(w, op1, op2) { \
1159 uint32 _x; \
1160 for (_x = w; _x >= 4; _x -= 4) { \
1161 op1; \
1162 REPEAT4(op2); \
1163 } \
1164 if (_x > 0) { \
1165 op1; \
1166 CASE4(_x,op2); \
1167 } \
1168 }
1169 #define UNROLL2(w, op1, op2) { \
1170 uint32 _x; \
1171 for (_x = w; _x >= 2; _x -= 2) { \
1172 op1; \
1173 REPEAT2(op2); \
1174 } \
1175 if (_x) { \
1176 op1; \
1177 op2; \
1178 } \
1179 }
1180
1181 #define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
1182 #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
1183
1184 #define A1 (((uint32)0xffL)<<24)
1185 #define PACK(r,g,b) \
1186 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
1187 #define PACK4(r,g,b,a) \
1188 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
1189 #define W2B(v) (((v)>>8)&0xff)
1190 /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
1191 #define PACKW(r,g,b) \
1192 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
1193 #define PACKW4(r,g,b,a) \
1194 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
1195
1196 #define DECLAREContigPutFunc(name) \
1197 static void name(\
1198 TIFFRGBAImage* img, \
1199 uint32* cp, \
1200 uint32 x, uint32 y, \
1201 uint32 w, uint32 h, \
1202 int32 fromskew, int32 toskew, \
1203 unsigned char* pp \
1204 )
1205
1206 /*
1207 * 8-bit palette => colormap/RGB
1208 */
1209 DECLAREContigPutFunc(put8bitcmaptile)
1210 {
1211 uint32** PALmap = img->PALmap;
1212 int samplesperpixel = img->samplesperpixel;
1213
1214 (void) y;
1215 while (h-- > 0) {
1216 for (x = w; x-- > 0;)
1217 {
1218 *cp++ = PALmap[*pp][0];
1219 pp += samplesperpixel;
1220 }
1221 cp += toskew;
1222 pp += fromskew;
1223 }
1224 }
1225
1226 /*
1227 * 4-bit palette => colormap/RGB
1228 */
1229 DECLAREContigPutFunc(put4bitcmaptile)
1230 {
1231 uint32** PALmap = img->PALmap;
1232
1233 (void) x; (void) y;
1234 fromskew /= 2;
1235 while (h-- > 0) {
1236 uint32* bw;
1237 UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
1238 cp += toskew;
1239 pp += fromskew;
1240 }
1241 }
1242
1243 /*
1244 * 2-bit palette => colormap/RGB
1245 */
1246 DECLAREContigPutFunc(put2bitcmaptile)
1247 {
1248 uint32** PALmap = img->PALmap;
1249
1250 (void) x; (void) y;
1251 fromskew /= 4;
1252 while (h-- > 0) {
1253 uint32* bw;
1254 UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
1255 cp += toskew;
1256 pp += fromskew;
1257 }
1258 }
1259
1260 /*
1261 * 1-bit palette => colormap/RGB
1262 */
1263 DECLAREContigPutFunc(put1bitcmaptile)
1264 {
1265 uint32** PALmap = img->PALmap;
1266
1267 (void) x; (void) y;
1268 fromskew /= 8;
1269 while (h-- > 0) {
1270 uint32* bw;
1271 UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
1272 cp += toskew;
1273 pp += fromskew;
1274 }
1275 }
1276
1277 /*
1278 * 8-bit greyscale => colormap/RGB
1279 */
1280 DECLAREContigPutFunc(putgreytile)
1281 {
1282 int samplesperpixel = img->samplesperpixel;
1283 uint32** BWmap = img->BWmap;
1284
1285 (void) y;
1286 while (h-- > 0) {
1287 for (x = w; x-- > 0;)
1288 {
1289 *cp++ = BWmap[*pp][0];
1290 pp += samplesperpixel;
1291 }
1292 cp += toskew;
1293 pp += fromskew;
1294 }
1295 }
1296
1297 /*
1298 * 8-bit greyscale with associated alpha => colormap/RGBA
1299 */
1300 DECLAREContigPutFunc(putagreytile)
1301 {
1302 int samplesperpixel = img->samplesperpixel;
1303 uint32** BWmap = img->BWmap;
1304
1305 (void) y;
1306 while (h-- > 0) {
1307 for (x = w; x-- > 0;)
1308 {
1309 *cp++ = BWmap[*pp][0] & (*(pp+1) << 24 | ~A1);
1310 pp += samplesperpixel;
1311 }
1312 cp += toskew;
1313 pp += fromskew;
1314 }
1315 }
1316
1317 /*
1318 * 16-bit greyscale => colormap/RGB
1319 */
1320 DECLAREContigPutFunc(put16bitbwtile)
1321 {
1322 int samplesperpixel = img->samplesperpixel;
1323 uint32** BWmap = img->BWmap;
1324
1325 (void) y;
1326 while (h-- > 0) {
1327 uint16 *wp = (uint16 *) pp;
1328
1329 for (x = w; x-- > 0;)
1330 {
1331 /* use high order byte of 16bit value */
1332
1333 *cp++ = BWmap[*wp >> 8][0];
1334 pp += 2 * samplesperpixel;
1335 wp += samplesperpixel;
1336 }
1337 cp += toskew;
1338 pp += fromskew;
1339 }
1340 }
1341
1342 /*
1343 * 1-bit bilevel => colormap/RGB
1344 */
1345 DECLAREContigPutFunc(put1bitbwtile)
1346 {
1347 uint32** BWmap = img->BWmap;
1348
1349 (void) x; (void) y;
1350 fromskew /= 8;
1351 while (h-- > 0) {
1352 uint32* bw;
1353 UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
1354 cp += toskew;
1355 pp += fromskew;
1356 }
1357 }
1358
1359 /*
1360 * 2-bit greyscale => colormap/RGB
1361 */
1362 DECLAREContigPutFunc(put2bitbwtile)
1363 {
1364 uint32** BWmap = img->BWmap;
1365
1366 (void) x; (void) y;
1367 fromskew /= 4;
1368 while (h-- > 0) {
1369 uint32* bw;
1370 UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
1371 cp += toskew;
1372 pp += fromskew;
1373 }
1374 }
1375
1376 /*
1377 * 4-bit greyscale => colormap/RGB
1378 */
1379 DECLAREContigPutFunc(put4bitbwtile)
1380 {
1381 uint32** BWmap = img->BWmap;
1382
1383 (void) x; (void) y;
1384 fromskew /= 2;
1385 while (h-- > 0) {
1386 uint32* bw;
1387 UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
1388 cp += toskew;
1389 pp += fromskew;
1390 }
1391 }
1392
1393 /*
1394 * 8-bit packed samples, no Map => RGB
1395 */
1396 DECLAREContigPutFunc(putRGBcontig8bittile)
1397 {
1398 int samplesperpixel = img->samplesperpixel;
1399
1400 (void) x; (void) y;
1401 fromskew *= samplesperpixel;
1402 while (h-- > 0) {
1403 UNROLL8(w, NOP,
1404 *cp++ = PACK(pp[0], pp[1], pp[2]);
1405 pp += samplesperpixel);
1406 cp += toskew;
1407 pp += fromskew;
1408 }
1409 }
1410
1411 /*
1412 * 8-bit packed samples => RGBA w/ associated alpha
1413 * (known to have Map == NULL)
1414 */
1415 DECLAREContigPutFunc(putRGBAAcontig8bittile)
1416 {
1417 int samplesperpixel = img->samplesperpixel;
1418
1419 (void) x; (void) y;
1420 fromskew *= samplesperpixel;
1421 while (h-- > 0) {
1422 UNROLL8(w, NOP,
1423 *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
1424 pp += samplesperpixel);
1425 cp += toskew;
1426 pp += fromskew;
1427 }
1428 }
1429
1430 /*
1431 * 8-bit packed samples => RGBA w/ unassociated alpha
1432 * (known to have Map == NULL)
1433 */
1434 DECLAREContigPutFunc(putRGBUAcontig8bittile)
1435 {
1436 int samplesperpixel = img->samplesperpixel;
1437 (void) y;
1438 fromskew *= samplesperpixel;
1439 while (h-- > 0) {
1440 uint32 r, g, b, a;
1441 uint8* m;
1442 for (x = w; x-- > 0;) {
1443 a = pp[3];
1444 m = img->UaToAa+((size_t) a<<8);
1445 r = m[pp[0]];
1446 g = m[pp[1]];
1447 b = m[pp[2]];
1448 *cp++ = PACK4(r,g,b,a);
1449 pp += samplesperpixel;
1450 }
1451 cp += toskew;
1452 pp += fromskew;
1453 }
1454 }
1455
1456 /*
1457 * 16-bit packed samples => RGB
1458 */
1459 DECLAREContigPutFunc(putRGBcontig16bittile)
1460 {
1461 int samplesperpixel = img->samplesperpixel;
1462 uint16 *wp = (uint16 *)pp;
1463 (void) y;
1464 fromskew *= samplesperpixel;
1465 while (h-- > 0) {
1466 for (x = w; x-- > 0;) {
1467 *cp++ = PACK(img->Bitdepth16To8[wp[0]],
1468 img->Bitdepth16To8[wp[1]],
1469 img->Bitdepth16To8[wp[2]]);
1470 wp += samplesperpixel;
1471 }
1472 cp += toskew;
1473 wp += fromskew;
1474 }
1475 }
1476
1477 /*
1478 * 16-bit packed samples => RGBA w/ associated alpha
1479 * (known to have Map == NULL)
1480 */
1481 DECLAREContigPutFunc(putRGBAAcontig16bittile)
1482 {
1483 int samplesperpixel = img->samplesperpixel;
1484 uint16 *wp = (uint16 *)pp;
1485 (void) y;
1486 fromskew *= samplesperpixel;
1487 while (h-- > 0) {
1488 for (x = w; x-- > 0;) {
1489 *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
1490 img->Bitdepth16To8[wp[1]],
1491 img->Bitdepth16To8[wp[2]],
1492 img->Bitdepth16To8[wp[3]]);
1493 wp += samplesperpixel;
1494 }
1495 cp += toskew;
1496 wp += fromskew;
1497 }
1498 }
1499
1500 /*
1501 * 16-bit packed samples => RGBA w/ unassociated alpha
1502 * (known to have Map == NULL)
1503 */
1504 DECLAREContigPutFunc(putRGBUAcontig16bittile)
1505 {
1506 int samplesperpixel = img->samplesperpixel;
1507 uint16 *wp = (uint16 *)pp;
1508 (void) y;
1509 fromskew *= samplesperpixel;
1510 while (h-- > 0) {
1511 uint32 r,g,b,a;
1512 uint8* m;
1513 for (x = w; x-- > 0;) {
1514 a = img->Bitdepth16To8[wp[3]];
1515 m = img->UaToAa+((size_t) a<<8);
1516 r = m[img->Bitdepth16To8[wp[0]]];
1517 g = m[img->Bitdepth16To8[wp[1]]];
1518 b = m[img->Bitdepth16To8[wp[2]]];
1519 *cp++ = PACK4(r,g,b,a);
1520 wp += samplesperpixel;
1521 }
1522 cp += toskew;
1523 wp += fromskew;
1524 }
1525 }
1526
1527 /*
1528 * 8-bit packed CMYK samples w/o Map => RGB
1529 *
1530 * NB: The conversion of CMYK->RGB is *very* crude.
1531 */
1532 DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
1533 {
1534 int samplesperpixel = img->samplesperpixel;
1535 uint16 r, g, b, k;
1536
1537 (void) x; (void) y;
1538 fromskew *= samplesperpixel;
1539 while (h-- > 0) {
1540 UNROLL8(w, NOP,
1541 k = 255 - pp[3];
1542 r = (k*(255-pp[0]))/255;
1543 g = (k*(255-pp[1]))/255;
1544 b = (k*(255-pp[2]))/255;
1545 *cp++ = PACK(r, g, b);
1546 pp += samplesperpixel);
1547 cp += toskew;
1548 pp += fromskew;
1549 }
1550 }
1551
1552 /*
1553 * 8-bit packed CMYK samples w/Map => RGB
1554 *
1555 * NB: The conversion of CMYK->RGB is *very* crude.
1556 */
1557 DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
1558 {
1559 int samplesperpixel = img->samplesperpixel;
1560 TIFFRGBValue* Map = img->Map;
1561 uint16 r, g, b, k;
1562
1563 (void) y;
1564 fromskew *= samplesperpixel;
1565 while (h-- > 0) {
1566 for (x = w; x-- > 0;) {
1567 k = 255 - pp[3];
1568 r = (k*(255-pp[0]))/255;
1569 g = (k*(255-pp[1]))/255;
1570 b = (k*(255-pp[2]))/255;
1571 *cp++ = PACK(Map[r], Map[g], Map[b]);
1572 pp += samplesperpixel;
1573 }
1574 pp += fromskew;
1575 cp += toskew;
1576 }
1577 }
1578
1579 #define DECLARESepPutFunc(name) \
1580 static void name(\
1581 TIFFRGBAImage* img,\
1582 uint32* cp,\
1583 uint32 x, uint32 y, \
1584 uint32 w, uint32 h,\
1585 int32 fromskew, int32 toskew,\
1586 unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
1587 )
1588
1589 /*
1590 * 8-bit unpacked samples => RGB
1591 */
1592 DECLARESepPutFunc(putRGBseparate8bittile)
1593 {
1594 (void) img; (void) x; (void) y; (void) a;
1595 while (h-- > 0) {
1596 UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
1597 SKEW(r, g, b, fromskew);
1598 cp += toskew;
1599 }
1600 }
1601
1602 /*
1603 * 8-bit unpacked samples => RGBA w/ associated alpha
1604 */
1605 DECLARESepPutFunc(putRGBAAseparate8bittile)
1606 {
1607 (void) img; (void) x; (void) y;
1608 while (h-- > 0) {
1609 UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
1610 SKEW4(r, g, b, a, fromskew);
1611 cp += toskew;
1612 }
1613 }
1614
1615 /*
1616 * 8-bit unpacked CMYK samples => RGBA
1617 */
1618 DECLARESepPutFunc(putCMYKseparate8bittile)
1619 {
1620 (void) img; (void) y;
1621 while (h-- > 0) {
1622 uint32 rv, gv, bv, kv;
1623 for (x = w; x-- > 0;) {
1624 kv = 255 - *a++;
1625 rv = (kv*(255-*r++))/255;
1626 gv = (kv*(255-*g++))/255;
1627 bv = (kv*(255-*b++))/255;
1628 *cp++ = PACK4(rv,gv,bv,255);
1629 }
1630 SKEW4(r, g, b, a, fromskew);
1631 cp += toskew;
1632 }
1633 }
1634
1635 /*
1636 * 8-bit unpacked samples => RGBA w/ unassociated alpha
1637 */
1638 DECLARESepPutFunc(putRGBUAseparate8bittile)
1639 {
1640 (void) img; (void) y;
1641 while (h-- > 0) {
1642 uint32 rv, gv, bv, av;
1643 uint8* m;
1644 for (x = w; x-- > 0;) {
1645 av = *a++;
1646 m = img->UaToAa+((size_t) av<<8);
1647 rv = m[*r++];
1648 gv = m[*g++];
1649 bv = m[*b++];
1650 *cp++ = PACK4(rv,gv,bv,av);
1651 }
1652 SKEW4(r, g, b, a, fromskew);
1653 cp += toskew;
1654 }
1655 }
1656
1657 /*
1658 * 16-bit unpacked samples => RGB
1659 */
1660 DECLARESepPutFunc(putRGBseparate16bittile)
1661 {
1662 uint16 *wr = (uint16*) r;
1663 uint16 *wg = (uint16*) g;
1664 uint16 *wb = (uint16*) b;
1665 (void) img; (void) y; (void) a;
1666 while (h-- > 0) {
1667 for (x = 0; x < w; x++)
1668 *cp++ = PACK(img->Bitdepth16To8[*wr++],
1669 img->Bitdepth16To8[*wg++],
1670 img->Bitdepth16To8[*wb++]);
1671 SKEW(wr, wg, wb, fromskew);
1672 cp += toskew;
1673 }
1674 }
1675
1676 /*
1677 * 16-bit unpacked samples => RGBA w/ associated alpha
1678 */
1679 DECLARESepPutFunc(putRGBAAseparate16bittile)
1680 {
1681 uint16 *wr = (uint16*) r;
1682 uint16 *wg = (uint16*) g;
1683 uint16 *wb = (uint16*) b;
1684 uint16 *wa = (uint16*) a;
1685 (void) img; (void) y;
1686 while (h-- > 0) {
1687 for (x = 0; x < w; x++)
1688 *cp++ = PACK4(img->Bitdepth16To8[*wr++],
1689 img->Bitdepth16To8[*wg++],
1690 img->Bitdepth16To8[*wb++],
1691 img->Bitdepth16To8[*wa++]);
1692 SKEW4(wr, wg, wb, wa, fromskew);
1693 cp += toskew;
1694 }
1695 }
1696
1697 /*
1698 * 16-bit unpacked samples => RGBA w/ unassociated alpha
1699 */
1700 DECLARESepPutFunc(putRGBUAseparate16bittile)
1701 {
1702 uint16 *wr = (uint16*) r;
1703 uint16 *wg = (uint16*) g;
1704 uint16 *wb = (uint16*) b;
1705 uint16 *wa = (uint16*) a;
1706 (void) img; (void) y;
1707 while (h-- > 0) {
1708 uint32 r2,g2,b2,a2;
1709 uint8* m;
1710 for (x = w; x-- > 0;) {
1711 a2 = img->Bitdepth16To8[*wa++];
1712 m = img->UaToAa+((size_t) a2<<8);
1713 r2 = m[img->Bitdepth16To8[*wr++]];
1714 g2 = m[img->Bitdepth16To8[*wg++]];
1715 b2 = m[img->Bitdepth16To8[*wb++]];
1716 *cp++ = PACK4(r2,g2,b2,a2);
1717 }
1718 SKEW4(wr, wg, wb, wa, fromskew);
1719 cp += toskew;
1720 }
1721 }
1722
1723 /*
1724 * 8-bit packed CIE L*a*b 1976 samples => RGB
1725 */
1726 DECLAREContigPutFunc(putcontig8bitCIELab)
1727 {
1728 float X, Y, Z;
1729 uint32 r, g, b;
1730 (void) y;
1731 fromskew *= 3;
1732 while (h-- > 0) {
1733 for (x = w; x-- > 0;) {
1734 TIFFCIELabToXYZ(img->cielab,
1735 (unsigned char)pp[0],
1736 (signed char)pp[1],
1737 (signed char)pp[2],
1738 &X, &Y, &Z);
1739 TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
1740 *cp++ = PACK(r, g, b);
1741 pp += 3;
1742 }
1743 cp += toskew;
1744 pp += fromskew;
1745 }
1746 }
1747
1748 /*
1749 * YCbCr -> RGB conversion and packing routines.
1750 */
1751
1752 #define YCbCrtoRGB(dst, Y) { \
1753 uint32 r, g, b; \
1754 TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
1755 dst = PACK(r, g, b); \
1756 }
1757
1758 /*
1759 * 8-bit packed YCbCr samples => RGB
1760 * This function is generic for different sampling sizes,
1761 * and can handle blocks sizes that aren't multiples of the
1762 * sampling size. However, it is substantially less optimized
1763 * than the specific sampling cases. It is used as a fallback
1764 * for difficult blocks.
1765 */
1766 #ifdef notdef
1767 static void putcontig8bitYCbCrGenericTile(
1768 TIFFRGBAImage* img,
1769 uint32* cp,
1770 uint32 x, uint32 y,
1771 uint32 w, uint32 h,
1772 int32 fromskew, int32 toskew,
1773 unsigned char* pp,
1774 int h_group,
1775 int v_group )
1776
1777 {
1778 uint32* cp1 = cp+w+toskew;
1779 uint32* cp2 = cp1+w+toskew;
1780 uint32* cp3 = cp2+w+toskew;
1781 int32 incr = 3*w+4*toskew;
1782 int32 Cb, Cr;
1783 int group_size = v_group * h_group + 2;
1784
1785 (void) y;
1786 fromskew = (fromskew * group_size) / h_group;
1787
1788 for( yy = 0; yy < h; yy++ )
1789 {
1790 unsigned char *pp_line;
1791 int y_line_group = yy / v_group;
1792 int y_remainder = yy - y_line_group * v_group;
1793
1794 pp_line = pp + v_line_group *
1795
1796
1797 for( xx = 0; xx < w; xx++ )
1798 {
1799 Cb = pp
1800 }
1801 }
1802 for (; h >= 4; h -= 4) {
1803 x = w>>2;
1804 do {
1805 Cb = pp[16];
1806 Cr = pp[17];
1807
1808 YCbCrtoRGB(cp [0], pp[ 0]);
1809 YCbCrtoRGB(cp [1], pp[ 1]);
1810 YCbCrtoRGB(cp [2], pp[ 2]);
1811 YCbCrtoRGB(cp [3], pp[ 3]);
1812 YCbCrtoRGB(cp1[0], pp[ 4]);
1813 YCbCrtoRGB(cp1[1], pp[ 5]);
1814 YCbCrtoRGB(cp1[2], pp[ 6]);
1815 YCbCrtoRGB(cp1[3], pp[ 7]);
1816 YCbCrtoRGB(cp2[0], pp[ 8]);
1817 YCbCrtoRGB(cp2[1], pp[ 9]);
1818 YCbCrtoRGB(cp2[2], pp[10]);
1819 YCbCrtoRGB(cp2[3], pp[11]);
1820 YCbCrtoRGB(cp3[0], pp[12]);
1821 YCbCrtoRGB(cp3[1], pp[13]);
1822 YCbCrtoRGB(cp3[2], pp[14]);
1823 YCbCrtoRGB(cp3[3], pp[15]);
1824
1825 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
1826 pp += 18;
1827 } while (--x);
1828 cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
1829 pp += fromskew;
1830 }
1831 }
1832 #endif
1833
1834 /*
1835 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
1836 */
1837 DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
1838 {
1839 uint32* cp1 = cp+w+toskew;
1840 uint32* cp2 = cp1+w+toskew;
1841 uint32* cp3 = cp2+w+toskew;
1842 int32 incr = 3*w+4*toskew;
1843
1844 (void) y;
1845 /* adjust fromskew */
1846 fromskew = (fromskew * 18) / 4;
1847 if ((h & 3) == 0 && (w & 3) == 0) {
1848 for (; h >= 4; h -= 4) {
1849 x = w>>2;
1850 do {
1851 int32 Cb = pp[16];
1852 int32 Cr = pp[17];
1853
1854 YCbCrtoRGB(cp [0], pp[ 0]);
1855 YCbCrtoRGB(cp [1], pp[ 1]);
1856 YCbCrtoRGB(cp [2], pp[ 2]);
1857 YCbCrtoRGB(cp [3], pp[ 3]);
1858 YCbCrtoRGB(cp1[0], pp[ 4]);
1859 YCbCrtoRGB(cp1[1], pp[ 5]);
1860 YCbCrtoRGB(cp1[2], pp[ 6]);
1861 YCbCrtoRGB(cp1[3], pp[ 7]);
1862 YCbCrtoRGB(cp2[0], pp[ 8]);
1863 YCbCrtoRGB(cp2[1], pp[ 9]);
1864 YCbCrtoRGB(cp2[2], pp[10]);
1865 YCbCrtoRGB(cp2[3], pp[11]);
1866 YCbCrtoRGB(cp3[0], pp[12]);
1867 YCbCrtoRGB(cp3[1], pp[13]);
1868 YCbCrtoRGB(cp3[2], pp[14]);
1869 YCbCrtoRGB(cp3[3], pp[15]);
1870
1871 cp += 4;
1872 cp1 += 4;
1873 cp2 += 4;
1874 cp3 += 4;
1875 pp += 18;
1876 } while (--x);
1877 cp += incr;
1878 cp1 += incr;
1879 cp2 += incr;
1880 cp3 += incr;
1881 pp += fromskew;
1882 }
1883 } else {
1884 while (h > 0) {
1885 for (x = w; x > 0;) {
1886 int32 Cb = pp[16];
1887 int32 Cr = pp[17];
1888 switch (x) {
1889 default:
1890 switch (h) {
1891 default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
1892 case 3: YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
1893 case 2: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
1894 case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
1895 } /* FALLTHROUGH */
1896 case 3:
1897 switch (h) {
1898 default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
1899 case 3: YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
1900 case 2: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
1901 case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
1902 } /* FALLTHROUGH */
1903 case 2:
1904 switch (h) {
1905 default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
1906 case 3: YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
1907 case 2: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
1908 case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
1909 } /* FALLTHROUGH */
1910 case 1:
1911 switch (h) {
1912 default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
1913 case 3: YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
1914 case 2: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
1915 case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
1916 } /* FALLTHROUGH */
1917 }
1918 if (x < 4) {
1919 cp += x; cp1 += x; cp2 += x; cp3 += x;
1920 x = 0;
1921 }
1922 else {
1923 cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
1924 x -= 4;
1925 }
1926 pp += 18;
1927 }
1928 if (h <= 4)
1929 break;
1930 h -= 4;
1931 cp += incr;
1932 cp1 += incr;
1933 cp2 += incr;
1934 cp3 += incr;
1935 pp += fromskew;
1936 }
1937 }
1938 }
1939
1940 /*
1941 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
1942 */
1943 DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
1944 {
1945 uint32* cp1 = cp+w+toskew;
1946 int32 incr = 2*toskew+w;
1947
1948 (void) y;
1949 fromskew = (fromskew * 10) / 4;
1950 if ((w & 3) == 0 && (h & 1) == 0) {
1951 for (; h >= 2; h -= 2) {
1952 x = w>>2;
1953 do {
1954 int32 Cb = pp[8];
1955 int32 Cr = pp[9];
1956
1957 YCbCrtoRGB(cp [0], pp[0]);
1958 YCbCrtoRGB(cp [1], pp[1]);
1959 YCbCrtoRGB(cp [2], pp[2]);
1960 YCbCrtoRGB(cp [3], pp[3]);
1961 YCbCrtoRGB(cp1[0], pp[4]);
1962 YCbCrtoRGB(cp1[1], pp[5]);
1963 YCbCrtoRGB(cp1[2], pp[6]);
1964 YCbCrtoRGB(cp1[3], pp[7]);
1965
1966 cp += 4;
1967 cp1 += 4;
1968 pp += 10;
1969 } while (--x);
1970 cp += incr;
1971 cp1 += incr;
1972 pp += fromskew;
1973 }
1974 } else {
1975 while (h > 0) {
1976 for (x = w; x > 0;) {
1977 int32 Cb = pp[8];
1978 int32 Cr = pp[9];
1979 switch (x) {
1980 default:
1981 switch (h) {
1982 default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
1983 case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
1984 } /* FALLTHROUGH */
1985 case 3:
1986 switch (h) {
1987 default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
1988 case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
1989 } /* FALLTHROUGH */
1990 case 2:
1991 switch (h) {
1992 default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
1993 case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
1994 } /* FALLTHROUGH */
1995 case 1:
1996 switch (h) {
1997 default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
1998 case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
1999 } /* FALLTHROUGH */
2000 }
2001 if (x < 4) {
2002 cp += x; cp1 += x;
2003 x = 0;
2004 }
2005 else {
2006 cp += 4; cp1 += 4;
2007 x -= 4;
2008 }
2009 pp += 10;
2010 }
2011 if (h <= 2)
2012 break;
2013 h -= 2;
2014 cp += incr;
2015 cp1 += incr;
2016 pp += fromskew;
2017 }
2018 }
2019 }
2020
2021 /*
2022 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
2023 */
2024 DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
2025 {
2026 (void) y;
2027 /* XXX adjust fromskew */
2028 do {
2029 x = w>>2;
2030 while(x>0) {
2031 int32 Cb = pp[4];
2032 int32 Cr = pp[5];
2033
2034 YCbCrtoRGB(cp [0], pp[0]);
2035 YCbCrtoRGB(cp [1], pp[1]);
2036 YCbCrtoRGB(cp [2], pp[2]);
2037 YCbCrtoRGB(cp [3], pp[3]);
2038
2039 cp += 4;
2040 pp += 6;
2041 x--;
2042 }
2043
2044 if( (w&3) != 0 )
2045 {
2046 int32 Cb = pp[4];
2047 int32 Cr = pp[5];
2048
2049 switch( (w&3) ) {
2050 case 3: YCbCrtoRGB(cp [2], pp[2]);
2051 case 2: YCbCrtoRGB(cp [1], pp[1]);
2052 case 1: YCbCrtoRGB(cp [0], pp[0]);
2053 case 0: break;
2054 }
2055
2056 cp += (w&3);
2057 pp += 6;
2058 }
2059
2060 cp += toskew;
2061 pp += fromskew;
2062 } while (--h);
2063
2064 }
2065
2066 /*
2067 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
2068 */
2069 DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
2070 {
2071 uint32* cp2;
2072 int32 incr = 2*toskew+w;
2073 (void) y;
2074 fromskew = (fromskew / 2) * 6;
2075 cp2 = cp+w+toskew;
2076 while (h>=2) {
2077 x = w;
2078 while (x>=2) {
2079 uint32 Cb = pp[4];
2080 uint32 Cr = pp[5];
2081 YCbCrtoRGB(cp[0], pp[0]);
2082 YCbCrtoRGB(cp[1], pp[1]);
2083 YCbCrtoRGB(cp2[0], pp[2]);
2084 YCbCrtoRGB(cp2[1], pp[3]);
2085 cp += 2;
2086 cp2 += 2;
2087 pp += 6;
2088 x -= 2;
2089 }
2090 if (x==1) {
2091 uint32 Cb = pp[4];
2092 uint32 Cr = pp[5];
2093 YCbCrtoRGB(cp[0], pp[0]);
2094 YCbCrtoRGB(cp2[0], pp[2]);
2095 cp ++ ;
2096 cp2 ++ ;
2097 pp += 6;
2098 }
2099 cp += incr;
2100 cp2 += incr;
2101 pp += fromskew;
2102 h-=2;
2103 }
2104 if (h==1) {
2105 x = w;
2106 while (x>=2) {
2107 uint32 Cb = pp[4];
2108 uint32 Cr = pp[5];
2109 YCbCrtoRGB(cp[0], pp[0]);
2110 YCbCrtoRGB(cp[1], pp[1]);
2111 cp += 2;
2112 cp2 += 2;
2113 pp += 6;
2114 x -= 2;
2115 }
2116 if (x==1) {
2117 uint32 Cb = pp[4];
2118 uint32 Cr = pp[5];
2119 YCbCrtoRGB(cp[0], pp[0]);
2120 }
2121 }
2122 }
2123
2124 /*
2125 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
2126 */
2127 DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
2128 {
2129 (void) y;
2130 fromskew = (fromskew * 4) / 2;
2131 do {
2132 x = w>>1;
2133 while(x>0) {
2134 int32 Cb = pp[2];
2135 int32 Cr = pp[3];
2136
2137 YCbCrtoRGB(cp[0], pp[0]);
2138 YCbCrtoRGB(cp[1], pp[1]);
2139
2140 cp += 2;
2141 pp += 4;
2142 x --;
2143 }
2144
2145 if( (w&1) != 0 )
2146 {
2147 int32 Cb = pp[2];
2148 int32 Cr = pp[3];
2149
2150 YCbCrtoRGB(cp[0], pp[0]);
2151
2152 cp += 1;
2153 pp += 4;
2154 }
2155
2156 cp += toskew;
2157 pp += fromskew;
2158 } while (--h);
2159 }
2160
2161 /*
2162 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
2163 */
2164 DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
2165 {
2166 uint32* cp2;
2167 int32 incr = 2*toskew+w;
2168 (void) y;
2169 fromskew = (fromskew / 2) * 4;
2170 cp2 = cp+w+toskew;
2171 while (h>=2) {
2172 x = w;
2173 do {
2174 uint32 Cb = pp[2];
2175 uint32 Cr = pp[3];
2176 YCbCrtoRGB(cp[0], pp[0]);
2177 YCbCrtoRGB(cp2[0], pp[1]);
2178 cp ++;
2179 cp2 ++;
2180 pp += 4;
2181 } while (--x);
2182 cp += incr;
2183 cp2 += incr;
2184 pp += fromskew;
2185 h-=2;
2186 }
2187 if (h==1) {
2188 x = w;
2189 do {
2190 uint32 Cb = pp[2];
2191 uint32 Cr = pp[3];
2192 YCbCrtoRGB(cp[0], pp[0]);
2193 cp ++;
2194 pp += 4;
2195 } while (--x);
2196 }
2197 }
2198
2199 /*
2200 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2201 */
2202 DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
2203 {
2204 (void) y;
2205 fromskew *= 3;
2206 do {
2207 x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
2208 do {
2209 int32 Cb = pp[1];
2210 int32 Cr = pp[2];
2211
2212 YCbCrtoRGB(*cp++, pp[0]);
2213
2214 pp += 3;
2215 } while (--x);
2216 cp += toskew;
2217 pp += fromskew;
2218 } while (--h);
2219 }
2220
2221 /*
2222 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2223 */
2224 DECLARESepPutFunc(putseparate8bitYCbCr11tile)
2225 {
2226 (void) y;
2227 (void) a;
2228 /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
2229 while (h-- > 0) {
2230 x = w;
2231 do {
2232 uint32 dr, dg, db;
2233 TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
2234 *cp++ = PACK(dr,dg,db);
2235 } while (--x);
2236 SKEW(r, g, b, fromskew);
2237 cp += toskew;
2238 }
2239 }
2240 #undef YCbCrtoRGB
2241
2242 static int
2243 initYCbCrConversion(TIFFRGBAImage* img)
2244 {
2245 static const char module[] = "initYCbCrConversion";
2246
2247 float *luma, *refBlackWhite;
2248
2249 if (img->ycbcr == NULL) {
2250 img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
2251 TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))
2252 + 4*256*sizeof (TIFFRGBValue)
2253 + 2*256*sizeof (int)
2254 + 3*256*sizeof (int32)
2255 );
2256 if (img->ycbcr == NULL) {
2257 TIFFErrorExt(img->tif->tif_clientdata, module,
2258 "No space for YCbCr->RGB conversion state");
2259 return (0);
2260 }
2261 }
2262
2263 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
2264 TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
2265 &refBlackWhite);
2266 if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
2267 return(0);
2268 return (1);
2269 }
2270
2271 static tileContigRoutine
2272 initCIELabConversion(TIFFRGBAImage* img)
2273 {
2274 static const char module[] = "initCIELabConversion";
2275
2276 float *whitePoint;
2277 float refWhite[3];
2278
2279 if (!img->cielab) {
2280 img->cielab = (TIFFCIELabToRGB *)
2281 _TIFFmalloc(sizeof(TIFFCIELabToRGB));
2282 if (!img->cielab) {
2283 TIFFErrorExt(img->tif->tif_clientdata, module,
2284 "No space for CIE L*a*b*->RGB conversion state.");
2285 return NULL;
2286 }
2287 }
2288
2289 TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
2290 refWhite[1] = 100.0F;
2291 refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
2292 refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
2293 / whitePoint[1] * refWhite[1];
2294 if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
2295 TIFFErrorExt(img->tif->tif_clientdata, module,
2296 "Failed to initialize CIE L*a*b*->RGB conversion state.");
2297 _TIFFfree(img->cielab);
2298 return NULL;
2299 }
2300
2301 return putcontig8bitCIELab;
2302 }
2303
2304 /*
2305 * Greyscale images with less than 8 bits/sample are handled
2306 * with a table to avoid lots of shifts and masks. The table
2307 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
2308 * pixel values simply by indexing into the table with one
2309 * number.
2310 */
2311 static int
2312 makebwmap(TIFFRGBAImage* img)
2313 {
2314 TIFFRGBValue* Map = img->Map;
2315 int bitspersample = img->bitspersample;
2316 int nsamples = 8 / bitspersample;
2317 int i;
2318 uint32* p;
2319
2320 if( nsamples == 0 )
2321 nsamples = 1;
2322
2323 img->BWmap = (uint32**) _TIFFmalloc(
2324 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
2325 if (img->BWmap == NULL) {
2326 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
2327 return (0);
2328 }
2329 p = (uint32*)(img->BWmap + 256);
2330 for (i = 0; i < 256; i++) {
2331 TIFFRGBValue c;
2332 img->BWmap[i] = p;
2333 switch (bitspersample) {
2334 #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
2335 case 1:
2336 GREY(i>>7);
2337 GREY((i>>6)&1);
2338 GREY((i>>5)&1);
2339 GREY((i>>4)&1);
2340 GREY((i>>3)&1);
2341 GREY((i>>2)&1);
2342 GREY((i>>1)&1);
2343 GREY(i&1);
2344 break;
2345 case 2:
2346 GREY(i>>6);
2347 GREY((i>>4)&3);
2348 GREY((i>>2)&3);
2349 GREY(i&3);
2350 break;
2351 case 4:
2352 GREY(i>>4);
2353 GREY(i&0xf);
2354 break;
2355 case 8:
2356 case 16:
2357 GREY(i);
2358 break;
2359 }
2360 #undef GREY
2361 }
2362 return (1);
2363 }
2364
2365 /*
2366 * Construct a mapping table to convert from the range
2367 * of the data samples to [0,255] --for display. This
2368 * process also handles inverting B&W images when needed.
2369 */
2370 static int
2371 setupMap(TIFFRGBAImage* img)
2372 {
2373 int32 x, range;
2374
2375 range = (int32)((1L<<img->bitspersample)-1);
2376
2377 /* treat 16 bit the same as eight bit */
2378 if( img->bitspersample == 16 )
2379 range = (int32) 255;
2380
2381 img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
2382 if (img->Map == NULL) {
2383 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
2384 "No space for photometric conversion table");
2385 return (0);
2386 }
2387 if (img->photometric == PHOTOMETRIC_MINISWHITE) {
2388 for (x = 0; x <= range; x++)
2389 img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
2390 } else {
2391 for (x = 0; x <= range; x++)
2392 img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
2393 }
2394 if (img->bitspersample <= 16 &&
2395 (img->photometric == PHOTOMETRIC_MINISBLACK ||
2396 img->photometric == PHOTOMETRIC_MINISWHITE)) {
2397 /*
2398 * Use photometric mapping table to construct
2399 * unpacking tables for samples <= 8 bits.
2400 */
2401 if (!makebwmap(img))
2402 return (0);
2403 /* no longer need Map, free it */
2404 _TIFFfree(img->Map);
2405 img->Map = NULL;
2406 }
2407 return (1);
2408 }
2409
2410 static int
2411 checkcmap(TIFFRGBAImage* img)
2412 {
2413 uint16* r = img->redcmap;
2414 uint16* g = img->greencmap;
2415 uint16* b = img->bluecmap;
2416 long n = 1L<<img->bitspersample;
2417
2418 while (n-- > 0)
2419 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
2420 return (16);
2421 return (8);
2422 }
2423
2424 static void
2425 cvtcmap(TIFFRGBAImage* img)
2426 {
2427 uint16* r = img->redcmap;
2428 uint16* g = img->greencmap;
2429 uint16* b = img->bluecmap;
2430 long i;
2431
2432 for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
2433 #define CVT(x) ((uint16)((x)>>8))
2434 r[i] = CVT(r[i]);
2435 g[i] = CVT(g[i]);
2436 b[i] = CVT(b[i]);
2437 #undef CVT
2438 }
2439 }
2440
2441 /*
2442 * Palette images with <= 8 bits/sample are handled
2443 * with a table to avoid lots of shifts and masks. The table
2444 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
2445 * pixel values simply by indexing into the table with one
2446 * number.
2447 */
2448 static int
2449 makecmap(TIFFRGBAImage* img)
2450 {
2451 int bitspersample = img->bitspersample;
2452 int nsamples = 8 / bitspersample;
2453 uint16* r = img->redcmap;
2454 uint16* g = img->greencmap;
2455 uint16* b = img->bluecmap;
2456 uint32 *p;
2457 int i;
2458
2459 img->PALmap = (uint32**) _TIFFmalloc(
2460 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
2461 if (img->PALmap == NULL) {
2462 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
2463 return (0);
2464 }
2465 p = (uint32*)(img->PALmap + 256);
2466 for (i = 0; i < 256; i++) {
2467 TIFFRGBValue c;
2468 img->PALmap[i] = p;
2469 #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
2470 switch (bitspersample) {
2471 case 1:
2472 CMAP(i>>7);
2473 CMAP((i>>6)&1);
2474 CMAP((i>>5)&1);
2475 CMAP((i>>4)&1);
2476 CMAP((i>>3)&1);
2477 CMAP((i>>2)&1);
2478 CMAP((i>>1)&1);
2479 CMAP(i&1);
2480 break;
2481 case 2:
2482 CMAP(i>>6);
2483 CMAP((i>>4)&3);
2484 CMAP((i>>2)&3);
2485 CMAP(i&3);
2486 break;
2487 case 4:
2488 CMAP(i>>4);
2489 CMAP(i&0xf);
2490 break;
2491 case 8:
2492 CMAP(i);
2493 break;
2494 }
2495 #undef CMAP
2496 }
2497 return (1);
2498 }
2499
2500 /*
2501 * Construct any mapping table used
2502 * by the associated put routine.
2503 */
2504 static int
2505 buildMap(TIFFRGBAImage* img)
2506 {
2507 switch (img->photometric) {
2508 case PHOTOMETRIC_RGB:
2509 case PHOTOMETRIC_YCBCR:
2510 case PHOTOMETRIC_SEPARATED:
2511 if (img->bitspersample == 8)
2512 break;
2513 /* fall through... */
2514 case PHOTOMETRIC_MINISBLACK:
2515 case PHOTOMETRIC_MINISWHITE:
2516 if (!setupMap(img))
2517 return (0);
2518 break;
2519 case PHOTOMETRIC_PALETTE:
2520 /*
2521 * Convert 16-bit colormap to 8-bit (unless it looks
2522 * like an old-style 8-bit colormap).
2523 */
2524 if (checkcmap(img) == 16)
2525 cvtcmap(img);
2526 else
2527 TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
2528 /*
2529 * Use mapping table and colormap to construct
2530 * unpacking tables for samples < 8 bits.
2531 */
2532 if (img->bitspersample <= 8 && !makecmap(img))
2533 return (0);
2534 break;
2535 }
2536 return (1);
2537 }
2538
2539 /*
2540 * Select the appropriate conversion routine for packed data.
2541 */
2542 static int
2543 PickContigCase(TIFFRGBAImage* img)
2544 {
2545 img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
2546 img->put.contig = NULL;
2547 switch (img->photometric) {
2548 case PHOTOMETRIC_RGB:
2549 switch (img->bitspersample) {
2550 case 8:
2551 if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
2552 img->samplesperpixel >= 4)
2553 img->put.contig = putRGBAAcontig8bittile;
2554 else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
2555 img->samplesperpixel >= 4)
2556 {
2557 if (BuildMapUaToAa(img))
2558 img->put.contig = putRGBUAcontig8bittile;
2559 }
2560 else if( img->samplesperpixel >= 3 )
2561 img->put.contig = putRGBcontig8bittile;
2562 break;
2563 case 16:
2564 if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
2565 img->samplesperpixel >=4 )
2566 {
2567 if (BuildMapBitdepth16To8(img))
2568 img->put.contig = putRGBAAcontig16bittile;
2569 }
2570 else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
2571 img->samplesperpixel >=4 )
2572 {
2573 if (BuildMapBitdepth16To8(img) &&
2574 BuildMapUaToAa(img))
2575 img->put.contig = putRGBUAcontig16bittile;
2576 }
2577 else if( img->samplesperpixel >=3 )
2578 {
2579 if (BuildMapBitdepth16To8(img))
2580 img->put.contig = putRGBcontig16bittile;
2581 }
2582 break;
2583 }
2584 break;
2585 case PHOTOMETRIC_SEPARATED:
2586 if (img->samplesperpixel >=4 && buildMap(img)) {
2587 if (img->bitspersample == 8) {
2588 if (!img->Map)
2589 img->put.contig = putRGBcontig8bitCMYKtile;
2590 else
2591 img->put.contig = putRGBcontig8bitCMYKMaptile;
2592 }
2593 }
2594 break;
2595 case PHOTOMETRIC_PALETTE:
2596 if (buildMap(img)) {
2597 switch (img->bitspersample) {
2598 case 8:
2599 img->put.contig = put8bitcmaptile;
2600 break;
2601 case 4:
2602 img->put.contig = put4bitcmaptile;
2603 break;
2604 case 2:
2605 img->put.contig = put2bitcmaptile;
2606 break;
2607 case 1:
2608 img->put.contig = put1bitcmaptile;
2609 break;
2610 }
2611 }
2612 break;
2613 case PHOTOMETRIC_MINISWHITE:
2614 case PHOTOMETRIC_MINISBLACK:
2615 if (buildMap(img)) {
2616 switch (img->bitspersample) {
2617 case 16:
2618 img->put.contig = put16bitbwtile;
2619 break;
2620 case 8:
2621 if (img->alpha && img->samplesperpixel == 2)
2622 img->put.contig = putagreytile;
2623 else
2624 img->put.contig = putgreytile;
2625 break;
2626 case 4:
2627 img->put.contig = put4bitbwtile;
2628 break;
2629 case 2:
2630 img->put.contig = put2bitbwtile;
2631 break;
2632 case 1:
2633 img->put.contig = put1bitbwtile;
2634 break;
2635 }
2636 }
2637 break;
2638 case PHOTOMETRIC_YCBCR:
2639 if ((img->bitspersample==8) && (img->samplesperpixel==3))
2640 {
2641 if (initYCbCrConversion(img)!=0)
2642 {
2643 /*
2644 * The 6.0 spec says that subsampling must be
2645 * one of 1, 2, or 4, and that vertical subsampling
2646 * must always be <= horizontal subsampling; so
2647 * there are only a few possibilities and we just
2648 * enumerate the cases.
2649 * Joris: added support for the [1,2] case, nonetheless, to accommodate
2650 * some OJPEG files
2651 */
2652 uint16 SubsamplingHor;
2653 uint16 SubsamplingVer;
2654 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
2655 switch ((SubsamplingHor<<4)|SubsamplingVer) {
2656 case 0x44:
2657 img->put.contig = putcontig8bitYCbCr44tile;
2658 break;
2659 case 0x42:
2660 img->put.contig = putcontig8bitYCbCr42tile;
2661 break;
2662 case 0x41:
2663 img->put.contig = putcontig8bitYCbCr41tile;
2664 break;
2665 case 0x22:
2666 img->put.contig = putcontig8bitYCbCr22tile;
2667 break;
2668 case 0x21:
2669 img->put.contig = putcontig8bitYCbCr21tile;
2670 break;
2671 case 0x12:
2672 img->put.contig = putcontig8bitYCbCr12tile;
2673 break;
2674 case 0x11:
2675 img->put.contig = putcontig8bitYCbCr11tile;
2676 break;
2677 }
2678 }
2679 }
2680 break;
2681 case PHOTOMETRIC_CIELAB:
2682 if (img->samplesperpixel == 3 && buildMap(img)) {
2683 if (img->bitspersample == 8)
2684 img->put.contig = initCIELabConversion(img);
2685 break;
2686 }
2687 }
2688 return ((img->get!=NULL) && (img->put.contig!=NULL));
2689 }
2690
2691 /*
2692 * Select the appropriate conversion routine for unpacked data.
2693 *
2694 * NB: we assume that unpacked single channel data is directed
2695 * to the "packed routines.
2696 */
2697 static int
2698 PickSeparateCase(TIFFRGBAImage* img)
2699 {
2700 img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
2701 img->put.separate = NULL;
2702 switch (img->photometric) {
2703 case PHOTOMETRIC_MINISWHITE:
2704 case PHOTOMETRIC_MINISBLACK:
2705 /* greyscale images processed pretty much as RGB by gtTileSeparate */
2706 case PHOTOMETRIC_RGB:
2707 switch (img->bitspersample) {
2708 case 8:
2709 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2710 img->put.separate = putRGBAAseparate8bittile;
2711 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2712 {
2713 if (BuildMapUaToAa(img))
2714 img->put.separate = putRGBUAseparate8bittile;
2715 }
2716 else
2717 img->put.separate = putRGBseparate8bittile;
2718 break;
2719 case 16:
2720 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2721 {
2722 if (BuildMapBitdepth16To8(img))
2723 img->put.separate = putRGBAAseparate16bittile;
2724 }
2725 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2726 {
2727 if (BuildMapBitdepth16To8(img) &&
2728 BuildMapUaToAa(img))
2729 img->put.separate = putRGBUAseparate16bittile;
2730 }
2731 else
2732 {
2733 if (BuildMapBitdepth16To8(img))
2734 img->put.separate = putRGBseparate16bittile;
2735 }
2736 break;
2737 }
2738 break;
2739 case PHOTOMETRIC_SEPARATED:
2740 if (img->bitspersample == 8 && img->samplesperpixel == 4)
2741 {
2742 img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
2743 img->put.separate = putCMYKseparate8bittile;
2744 }
2745 break;
2746 case PHOTOMETRIC_YCBCR:
2747 if ((img->bitspersample==8) && (img->samplesperpixel==3))
2748 {
2749 if (initYCbCrConversion(img)!=0)
2750 {
2751 uint16 hs, vs;
2752 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
2753 switch ((hs<<4)|vs) {
2754 case 0x11:
2755 img->put.separate = putseparate8bitYCbCr11tile;
2756 break;
2757 /* TODO: add other cases here */
2758 }
2759 }
2760 }
2761 break;
2762 }
2763 return ((img->get!=NULL) && (img->put.separate!=NULL));
2764 }
2765
2766 static int
2767 BuildMapUaToAa(TIFFRGBAImage* img)
2768 {
2769 static const char module[]="BuildMapUaToAa";
2770 uint8* m;
2771 uint16 na,nv;
2772 assert(img->UaToAa==NULL);
2773 img->UaToAa=_TIFFmalloc(65536);
2774 if (img->UaToAa==NULL)
2775 {
2776 TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
2777 return(0);
2778 }
2779 m=img->UaToAa;
2780 for (na=0; na<256; na++)
2781 {
2782 for (nv=0; nv<256; nv++)
2783 *m++=(uint8)((nv*na+127)/255);
2784 }
2785 return(1);
2786 }
2787
2788 static int
2789 BuildMapBitdepth16To8(TIFFRGBAImage* img)
2790 {
2791 static const char module[]="BuildMapBitdepth16To8";
2792 uint8* m;
2793 uint32 n;
2794 assert(img->Bitdepth16To8==NULL);
2795 img->Bitdepth16To8=_TIFFmalloc(65536);
2796 if (img->Bitdepth16To8==NULL)
2797 {
2798 TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
2799 return(0);
2800 }
2801 m=img->Bitdepth16To8;
2802 for (n=0; n<65536; n++)
2803 *m++=(uint8)((n+128)/257);
2804 return(1);
2805 }
2806
2807
2808 /*
2809 * Read a whole strip off data from the file, and convert to RGBA form.
2810 * If this is the last strip, then it will only contain the portion of
2811 * the strip that is actually within the image space. The result is
2812 * organized in bottom to top form.
2813 */
2814
2815
2816 int
2817 TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
2818
2819 {
2820 char emsg[1024] = "";
2821 TIFFRGBAImage img;
2822 int ok;
2823 uint32 rowsperstrip, rows_to_read;
2824
2825 if( TIFFIsTiled( tif ) )
2826 {
2827 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2828 "Can't use TIFFReadRGBAStrip() with tiled file.");
2829 return (0);
2830 }
2831
2832 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
2833 if( (row % rowsperstrip) != 0 )
2834 {
2835 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2836 "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
2837 return (0);
2838 }
2839
2840 if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
2841
2842 img.row_offset = row;
2843 img.col_offset = 0;
2844
2845 if( row + rowsperstrip > img.height )
2846 rows_to_read = img.height - row;
2847 else
2848 rows_to_read = rowsperstrip;
2849
2850 ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
2851
2852 TIFFRGBAImageEnd(&img);
2853 } else {
2854 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2855 ok = 0;
2856 }
2857
2858 return (ok);
2859 }
2860
2861 /*
2862 * Read a whole tile off data from the file, and convert to RGBA form.
2863 * The returned RGBA data is organized from bottom to top of tile,
2864 * and may include zeroed areas if the tile extends off the image.
2865 */
2866
2867 int
2868 TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
2869
2870 {
2871 char emsg[1024] = "";
2872 TIFFRGBAImage img;
2873 int ok;
2874 uint32 tile_xsize, tile_ysize;
2875 uint32 read_xsize, read_ysize;
2876 uint32 i_row;
2877
2878 /*
2879 * Verify that our request is legal - on a tile file, and on a
2880 * tile boundary.
2881 */
2882
2883 if( !TIFFIsTiled( tif ) )
2884 {
2885 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2886 "Can't use TIFFReadRGBATile() with stripped file.");
2887 return (0);
2888 }
2889
2890 TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
2891 TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
2892 if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
2893 {
2894 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2895 "Row/col passed to TIFFReadRGBATile() must be top"
2896 "left corner of a tile.");
2897 return (0);
2898 }
2899
2900 /*
2901 * Setup the RGBA reader.
2902 */
2903
2904 if (!TIFFRGBAImageOK(tif, emsg)
2905 || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
2906 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2907 return( 0 );
2908 }
2909
2910 /*
2911 * The TIFFRGBAImageGet() function doesn't allow us to get off the
2912 * edge of the image, even to fill an otherwise valid tile. So we
2913 * figure out how much we can read, and fix up the tile buffer to
2914 * a full tile configuration afterwards.
2915 */
2916
2917 if( row + tile_ysize > img.height )
2918 read_ysize = img.height - row;
2919 else
2920 read_ysize = tile_ysize;
2921
2922 if( col + tile_xsize > img.width )
2923 read_xsize = img.width - col;
2924 else
2925 read_xsize = tile_xsize;
2926
2927 /*
2928 * Read the chunk of imagery.
2929 */
2930
2931 img.row_offset = row;
2932 img.col_offset = col;
2933
2934 ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
2935
2936 TIFFRGBAImageEnd(&img);
2937
2938 /*
2939 * If our read was incomplete we will need to fix up the tile by
2940 * shifting the data around as if a full tile of data is being returned.
2941 *
2942 * This is all the more complicated because the image is organized in
2943 * bottom to top format.
2944 */
2945
2946 if( read_xsize == tile_xsize && read_ysize == tile_ysize )
2947 return( ok );
2948
2949 for( i_row = 0; i_row < read_ysize; i_row++ ) {
2950 memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
2951 raster + (read_ysize - i_row - 1) * read_xsize,
2952 read_xsize * sizeof(uint32) );
2953 _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
2954 0, sizeof(uint32) * (tile_xsize - read_xsize) );
2955 }
2956
2957 for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
2958 _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
2959 0, sizeof(uint32) * tile_xsize );
2960 }
2961
2962 return (ok);
2963 }
2964
2965 /* vim: set ts=8 sts=8 sw=8 noet: */
2966 /*
2967 * Local Variables:
2968 * mode: c
2969 * c-basic-offset: 8
2970 * fill-column: 78
2971 * End:
2972 */