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