LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_getimage.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1991-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25/*
26 * TIFF Library
27 *
28 * Read and return a packed RGBA image.
29 */
35#include "tiffiop.h"
36#include <limits.h>
37#include <math.h>
38#include <stdio.h>
39
40static int gtTileContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
41static int gtTileSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
42static int gtStripContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
43static int gtStripSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
44static int PickContigCase(TIFFRGBAImage *);
46
47static int BuildMapUaToAa(TIFFRGBAImage *img);
49
56static const char photoTag[] = "PhotometricInterpretation";
57
58/*
59 * Helper constants used in Orientation tag handling
60 */
68#define FLIP_VERTICALLY 0x01
76#define FLIP_HORIZONTALLY 0x02
77
86#define EMSG_BUF_SIZE 1024
87
88/*
89 * Color conversion constants. We will define display types here.
90 */
91
98static const TIFFDisplay display_sRGB = {
99 {/* XYZ -> luminance matrix */
100 {3.2410f, -1.5374f, -0.4986f},
101 {-0.9692f, 1.8760f, 0.0416f},
102 {0.0556f, -0.2040f, 1.0570f}},
103 100.0f,
104 100.0f,
105 100.0f, /* Light o/p for reference white */
106 255,
107 255,
108 255, /* Pixel values for ref. white */
109 1.0f,
110 1.0f,
111 1.0f, /* Residual light o/p for black pixel */
112 2.4f,
113 2.4f,
114 2.4f, /* Gamma values for the three guns */
115};
116
123static char const *const orientationStrings[] = {
124 "not valid", /*0 for error printout */
125 "TOPLEFT", /*1 row 0 top, col 0 lhs */
126 "TOPRIGHT", /*2 row 0 top, col 0 rhs */
127 "BOTRIGHT", /*3 row 0 bottom, col 0 rhs */
128 "BOTLEFT", /*4 row 0 bottom, col 0 lhs */
129 "LEFTTOP", /*5 row 0 lhs, col 0 top */
130 "RIGHTTOP", /*6 row 0 rhs, col 0 top */
131 "RIGHTBOT", /*7 row 0 rhs, col 0 bottom */
132 "LEFTBOT" /*8 row 0 lhs, col 0 bottom */
133};
144static uint16_t checkedOrientation(uint16_t orientation)
145{
146 if (orientation < 1 || orientation > 8)
147 return 0;
148 return orientation;
149}
150
151/*
152 * Check the image to see if TIFFReadRGBAImage can deal with it.
153 * 1/0 is returned according to whether or not the image can
154 * be handled. If 0 is returned, emsg contains the reason
155 * why it is being rejected.
156 */
157int TIFFRGBAImageOK(TIFF *tif, char emsg[EMSG_BUF_SIZE])
158{
159 TIFFDirectory *td = &tif->tif_dir;
160 uint16_t photometric;
161 int colorchannels;
162
163 if (!tif->tif_decodestatus)
164 {
165 snprintf(emsg, EMSG_BUF_SIZE,
166 "Sorry, requested compression method is not configured");
167 return 0;
168 }
169 switch (td->td_bitspersample)
170 {
171 case 1:
172 case 2:
173 case 4:
174 case 8:
175 case 16:
176 break;
177 default:
178 snprintf(emsg, EMSG_BUF_SIZE,
179 "Sorry, can not handle images with %" PRIu16
180 "-bit samples",
181 td->td_bitspersample);
182 return 0;
183 }
185 {
186 snprintf(
187 emsg, EMSG_BUF_SIZE,
188 "Sorry, can not handle images with IEEE floating-point samples");
189 return 0;
190 }
191 colorchannels = td->td_samplesperpixel - td->td_extrasamples;
192 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric))
193 {
194 switch (colorchannels)
195 {
196 case 1:
197 photometric = PHOTOMETRIC_MINISBLACK;
198 break;
199 case 3:
200 photometric = PHOTOMETRIC_RGB;
201 break;
202 default:
203 snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
204 photoTag);
205 return 0;
206 }
207 }
208 switch (photometric)
209 {
214 td->td_samplesperpixel != 1 && td->td_bitspersample < 8)
215 {
216 snprintf(
217 emsg, EMSG_BUF_SIZE,
218 "Sorry, can not handle contiguous data with %s=%" PRIu16
219 ", "
220 "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "",
221 photoTag, photometric, "Samples/pixel",
223 return 0;
224 }
225 /*
226 * We should likely validate that any extra samples are either
227 * to be ignored, or are alpha, and if alpha we should try to use
228 * them. But for now we won't bother with this.
229 */
230 break;
232 /*
233 * TODO: if at all meaningful and useful, make more complete
234 * support check here, or better still, refactor to let supporting
235 * code decide whether there is support and what meaningful
236 * error to return
237 */
238 break;
239 case PHOTOMETRIC_RGB:
240 if (colorchannels < 3)
241 {
242 snprintf(emsg, EMSG_BUF_SIZE,
243 "Sorry, can not handle RGB image with %s=%d",
244 "Color channels", colorchannels);
245 return 0;
246 }
247 break;
249 {
250 uint16_t inkset;
252 if (inkset != INKSET_CMYK)
253 {
254 snprintf(emsg, EMSG_BUF_SIZE,
255 "Sorry, can not handle separated image with %s=%d",
256 "InkSet", inkset);
257 return 0;
258 }
259 if (td->td_samplesperpixel < 4)
260 {
261 snprintf(
262 emsg, EMSG_BUF_SIZE,
263 "Sorry, can not handle separated image with %s=%" PRIu16,
264 "Samples/pixel", td->td_samplesperpixel);
265 return 0;
266 }
267 break;
268 }
269 case PHOTOMETRIC_LOGL:
271 {
272 snprintf(emsg, EMSG_BUF_SIZE,
273 "Sorry, LogL data must have %s=%d", "Compression",
275 return 0;
276 }
277 break;
281 {
282 snprintf(emsg, EMSG_BUF_SIZE,
283 "Sorry, LogLuv data must have %s=%d or %d",
284 "Compression", COMPRESSION_SGILOG,
286 return 0;
287 }
289 {
290 snprintf(emsg, EMSG_BUF_SIZE,
291 "Sorry, can not handle LogLuv images with %s=%" PRIu16,
292 "Planarconfiguration", td->td_planarconfig);
293 return 0;
294 }
295 if (td->td_samplesperpixel != 3 || colorchannels != 3)
296 {
297 snprintf(emsg, EMSG_BUF_SIZE,
298 "Sorry, can not handle image with %s=%" PRIu16
299 ", %s=%d",
300 "Samples/pixel", td->td_samplesperpixel,
301 "colorchannels", colorchannels);
302 return 0;
303 }
304 break;
306 if (td->td_samplesperpixel != 3 || colorchannels != 3 ||
307 (td->td_bitspersample != 8 && td->td_bitspersample != 16))
308 {
309 snprintf(emsg, EMSG_BUF_SIZE,
310 "Sorry, can not handle image with %s=%" PRIu16
311 ", %s=%d and %s=%" PRIu16,
312 "Samples/pixel", td->td_samplesperpixel,
313 "colorchannels", colorchannels, "Bits/sample",
314 td->td_bitspersample);
315 return 0;
316 }
317 break;
318 default:
319 snprintf(emsg, EMSG_BUF_SIZE,
320 "Sorry, can not handle image with %s=%" PRIu16, photoTag,
321 photometric);
322 return 0;
323 }
324 return 1;
325}
326
328{
329 if (img->Map)
330 {
331 _TIFFfreeExt(img->tif, img->Map);
332 img->Map = NULL;
333 }
334 if (img->BWmap)
335 {
336 _TIFFfreeExt(img->tif, img->BWmap);
337 img->BWmap = NULL;
338 }
339 if (img->PALmap)
340 {
341 _TIFFfreeExt(img->tif, img->PALmap);
342 img->PALmap = NULL;
343 }
344 if (img->ycbcr)
345 {
346 _TIFFfreeExt(img->tif, img->ycbcr);
347 img->ycbcr = NULL;
348 }
349 if (img->cielab)
350 {
351 _TIFFfreeExt(img->tif, img->cielab);
352 img->cielab = NULL;
353 }
354 if (img->UaToAa)
355 {
356 _TIFFfreeExt(img->tif, img->UaToAa);
357 img->UaToAa = NULL;
358 }
359 if (img->Bitdepth16To8)
360 {
361 _TIFFfreeExt(img->tif, img->Bitdepth16To8);
362 img->Bitdepth16To8 = NULL;
363 }
364
365 if (img->redcmap)
366 {
367 _TIFFfreeExt(img->tif, img->redcmap);
368 _TIFFfreeExt(img->tif, img->greencmap);
369 _TIFFfreeExt(img->tif, img->bluecmap);
370 img->redcmap = img->greencmap = img->bluecmap = NULL;
371 }
372}
373
385static int isCCITTCompression(TIFF *tif)
386{
387 uint16_t compress;
388 TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
389 return (compress == COMPRESSION_CCITTFAX3 ||
390 compress == COMPRESSION_CCITTFAX4 ||
391 compress == COMPRESSION_CCITTRLE ||
392 compress == COMPRESSION_CCITTRLEW);
393}
394
395int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop,
396 char emsg[EMSG_BUF_SIZE])
397{
398 uint16_t *sampleinfo;
399 uint16_t extrasamples;
400 uint16_t planarconfig;
401 uint16_t compress;
402 int colorchannels;
403 uint16_t *red_orig, *green_orig, *blue_orig;
404 uint32_t n_color;
405
406 if (!TIFFRGBAImageOK(tif, emsg))
407 return 0;
408
409 /* Initialize to normal values */
410 img->row_offset = 0;
411 img->col_offset = 0;
412 img->redcmap = NULL;
413 img->greencmap = NULL;
414 img->bluecmap = NULL;
415 img->Map = NULL;
416 img->BWmap = NULL;
417 img->PALmap = NULL;
418 img->ycbcr = NULL;
419 img->cielab = NULL;
420 img->UaToAa = NULL;
421 img->Bitdepth16To8 = NULL;
422 img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
423
424 img->tif = tif;
425 img->stoponerr = stop;
427 switch (img->bitspersample)
428 {
429 case 1:
430 case 2:
431 case 4:
432 case 8:
433 case 16:
434 break;
435 default:
436 snprintf(emsg, EMSG_BUF_SIZE,
437 "Sorry, can not handle images with %" PRIu16
438 "-bit samples",
439 img->bitspersample);
440 goto fail_return;
441 }
442 img->alpha = 0;
445 &sampleinfo);
446 if (extrasamples >= 1)
447 {
448 switch (sampleinfo[0])
449 {
450 case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without
451 */
452 if (img->samplesperpixel >
453 3) /* correct info about alpha channel */
455 break;
456 case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
457 case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
458 img->alpha = sampleinfo[0];
459 break;
460 default:
461 break;
462 }
463 }
464
465#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
468
469 if (extrasamples == 0 && img->samplesperpixel == 4 &&
471 {
473 extrasamples = 1;
474 }
475#endif
476
477 colorchannels = img->samplesperpixel - extrasamples;
479 TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
481 {
482 switch (colorchannels)
483 {
484 case 1:
485 if (isCCITTCompression(tif))
487 else
489 break;
490 case 3:
492 break;
493 default:
494 snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
495 photoTag);
496 goto fail_return;
497 }
498 }
499 switch (img->photometric)
500 {
502 if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &red_orig, &green_orig,
503 &blue_orig))
504 {
505 snprintf(emsg, EMSG_BUF_SIZE,
506 "Missing required \"Colormap\" tag");
507 goto fail_return;
508 }
509
510 /* copy the colormaps so we can modify them */
511 n_color = tif->tif_dir.td_colormap_count;
512 img->redcmap = (uint16_t *)_TIFFmallocExt(
513 tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
514 img->greencmap = (uint16_t *)_TIFFmallocExt(
515 tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
516 img->bluecmap = (uint16_t *)_TIFFmallocExt(
517 tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
518 if (!img->redcmap || !img->greencmap || !img->bluecmap)
519 {
520 snprintf(emsg, EMSG_BUF_SIZE,
521 "Out of memory for colormap copy");
522 goto fail_return;
523 }
524
525 _TIFFmemcpy(img->redcmap, red_orig, n_color * 2);
526 _TIFFmemcpy(img->greencmap, green_orig, n_color * 2);
527 _TIFFmemcpy(img->bluecmap, blue_orig, n_color * 2);
528
529 /* fall through... */
532 if (planarconfig == PLANARCONFIG_CONTIG &&
533 img->samplesperpixel != 1 && img->bitspersample < 8)
534 {
535 snprintf(
536 emsg, EMSG_BUF_SIZE,
537 "Sorry, can not handle contiguous data with %s=%" PRIu16
538 ", "
539 "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16,
540 photoTag, img->photometric, "Samples/pixel",
541 img->samplesperpixel, img->bitspersample);
542 goto fail_return;
543 }
544 break;
546 /* It would probably be nice to have a reality check here. */
547 if (planarconfig == PLANARCONFIG_CONTIG)
548 /* can rely on libjpeg to convert to RGB */
549 /* XXX should restore current state on exit */
550 switch (compress)
551 {
552 case COMPRESSION_JPEG:
553 /*
554 * TODO: when complete tests verify complete
555 * desubsampling and YCbCr handling, remove use of
556 * TIFFTAG_JPEGCOLORMODE in favor of tif_getimage.c
557 * native handling
558 */
562 break;
563 default:
564 /* do nothing */;
565 break;
566 }
567 /*
568 * TODO: if at all meaningful and useful, make more complete
569 * support check here, or better still, refactor to let supporting
570 * code decide whether there is support and what meaningful
571 * error to return
572 */
573 break;
574 case PHOTOMETRIC_RGB:
575 if (colorchannels < 3)
576 {
577 snprintf(emsg, EMSG_BUF_SIZE,
578 "Sorry, can not handle RGB image with %s=%d",
579 "Color channels", colorchannels);
580 goto fail_return;
581 }
582 break;
584 {
585 uint16_t inkset;
587 if (inkset != INKSET_CMYK)
588 {
589 snprintf(
590 emsg, EMSG_BUF_SIZE,
591 "Sorry, can not handle separated image with %s=%" PRIu16,
592 "InkSet", inkset);
593 goto fail_return;
594 }
595 if (img->samplesperpixel < 4)
596 {
597 snprintf(
598 emsg, EMSG_BUF_SIZE,
599 "Sorry, can not handle separated image with %s=%" PRIu16,
600 "Samples/pixel", img->samplesperpixel);
601 goto fail_return;
602 }
603 }
604 break;
605 case PHOTOMETRIC_LOGL:
606 if (compress != COMPRESSION_SGILOG)
607 {
608 snprintf(emsg, EMSG_BUF_SIZE,
609 "Sorry, LogL data must have %s=%d", "Compression",
611 goto fail_return;
612 }
614 img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
615 img->bitspersample = 8;
616 break;
618 if (compress != COMPRESSION_SGILOG &&
619 compress != COMPRESSION_SGILOG24)
620 {
621 snprintf(emsg, EMSG_BUF_SIZE,
622 "Sorry, LogLuv data must have %s=%d or %d",
623 "Compression", COMPRESSION_SGILOG,
625 goto fail_return;
626 }
627 if (planarconfig != PLANARCONFIG_CONTIG)
628 {
629 snprintf(emsg, EMSG_BUF_SIZE,
630 "Sorry, can not handle LogLuv images with %s=%" PRIu16,
631 "Planarconfiguration", planarconfig);
632 return 0;
633 }
635 img->photometric = PHOTOMETRIC_RGB; /* little white lie */
636 img->bitspersample = 8;
637 break;
639 break;
640 default:
641 snprintf(emsg, EMSG_BUF_SIZE,
642 "Sorry, can not handle image with %s=%" PRIu16, photoTag,
643 img->photometric);
644 goto fail_return;
645 }
649 img->isContig =
650 !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
651 if (img->isContig)
652 {
653 if (!PickContigCase(img))
654 {
655 snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
656 goto fail_return;
657 }
658 }
659 else
660 {
661 if (!PickSeparateCase(img))
662 {
663 snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
664 goto fail_return;
665 }
666 }
667 return 1;
668
669fail_return:
670 TIFFRGBAImageEnd(img);
671 return 0;
672}
673
674int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t rwidth,
675 uint32_t rheight)
676{
677 if (img->get == NULL)
678 {
679 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
680 "No \"get\" routine setup");
681 return 0;
682 }
683 if (img->put.any == NULL)
684 {
686 img->tif, TIFFFileName(img->tif),
687 "No \"put\" routine setupl; probably can not handle image format");
688 return 0;
689 }
690 /* Check and adaptation of parameters is done within (img->get) function. */
691 return (*img->get)(img, raster, rwidth, rheight);
692}
693
694/*
695 * Read the specified image into an ABGR-format raster taking in account
696 * specified orientation.
697 */
698int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight,
699 uint32_t *raster, int orientation, int stop)
700{
701 char emsg[EMSG_BUF_SIZE] = "";
702 TIFFRGBAImage img;
703 int ok;
704
705 if (TIFFRGBAImageBegin(&img, tif, stop, emsg))
706 {
707 img.req_orientation = (uint16_t)orientation;
708 ok = TIFFRGBAImageGet(&img, raster, rwidth, rheight);
709 TIFFRGBAImageEnd(&img);
710 }
711 else
712 {
713 TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
714 ok = 0;
715 }
716 return ok;
717}
718
719/*
720 * Read the specified image into an ABGR-format raster. Use bottom left
721 * origin for raster by default.
722 */
723int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight,
724 uint32_t *raster, int stop)
725{
726 return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
727 ORIENTATION_BOTLEFT, stop);
728}
729
744{
745 int flip_ret = 0;
746 if (checkedOrientation(img->req_orientation) == 0)
747 {
749 "TIFFRGBAImage ERROR: Requested orientation %d is not "
750 "within allowed range of image orientations 1 to 8. "
751 "Using default",
752 img->req_orientation);
753 img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
754 }
755 switch (img->orientation)
756 {
761 flip_ret = FLIP_HORIZONTALLY;
762 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
765 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
767 flip_ret = FLIP_VERTICALLY;
768 else
769 flip_ret = 0;
770 break;
775 flip_ret = FLIP_HORIZONTALLY;
776 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
778 flip_ret = FLIP_VERTICALLY;
779 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
782 else
783 flip_ret = 0;
784 break;
790 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
792 flip_ret = FLIP_VERTICALLY;
793 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
795 flip_ret = FLIP_HORIZONTALLY;
796 else
797 flip_ret = 0;
798 break;
803 flip_ret = FLIP_VERTICALLY;
804 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
807 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
809 flip_ret = FLIP_HORIZONTALLY;
810 else
811 flip_ret = 0;
812 break;
813 default: /* NOTREACHED */
814 flip_ret = 0;
815 break;
816 }
817 /*
818 * TIFFRGBAImage cannot rotate images because of its interface restrictions.
819 * (see last comment in issue #29)
820 * Therefore, check for allowed combinations of img->orientation and
821 * img->req_orientation (requested orientation from the user).
822 * The img->req_orientation also defines in which corner of the
823 * two dimensional "raster" matrix the image will be moved
824 * (i.e. which raster-corner will be the raster origin).
825 */
826 static const char warnMsg[] =
827 "TIFFRGBAImage cannot rotate but only flip. Therefore, image "
828 "pixel will be moved to %s corner of the raster matrix as origin "
829 "but the image pixel orientation will result in %s";
830 if ((img->req_orientation <= ORIENTATION_BOTLEFT) &&
832 {
834 img->tif, TIFFFileName(img->tif), warnMsg,
837 (uint16_t)(img->req_orientation + 4))]);
838 }
839 else if ((img->req_orientation >= ORIENTATION_LEFTTOP) &&
841 {
843 img->tif, TIFFFileName(img->tif), warnMsg,
846 (uint16_t)(img->req_orientation - 4))]);
847 }
848 return flip_ret;
849}
850
851/* Calculate (initialize) all parameters for gt-functions to put the image area
852 * into the raster according to available image and raster sizes as well as to
853 * requested orientation and raster-origin.
854 * Furthermore, row_offset and col_offset is checked to be 0 or positive and
855 * req_orientation value is checked to be in enummeration range. */
889static int calcCopyToRasterParameter(TIFFRGBAImage *img, uint32_t *raster,
890 uint32_t rwidth, uint32_t rheight,
891 uint32_t *pwcpy, uint32_t *phcpy,
892 int *pflip, uint32_t **pp_raster_start,
893 int32_t *ptoskew, uint32_t *py,
894 bool isTiled, uint32_t tilewidth,
895 const char *funcName)
896{
897 /* If the raster height is smaller than the image height,
898 * or if there is a row_offset, adapt the number of lines to be copied. */
899 if (img->row_offset < 0 || (uint32_t)img->row_offset >= img->height)
900 {
901 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
902 "Error in %s: row offset %d exceeds "
903 "image height %u",
904 funcName, img->row_offset, img->height);
905 return 0;
906 }
907 *phcpy = TIFFmin(rheight, img->height - (uint32_t)img->row_offset);
908
909 /* If the raster is smaller than the image,
910 * or if there is a col_offset, adapt the number of samples to be copied per
911 * row. */
912 if (img->col_offset < 0 || (uint32_t)img->col_offset >= img->width)
913 {
914 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
915 "Error in %s: column offset %d exceeds "
916 "image width %u",
917 funcName, img->col_offset, img->width);
918 return 0;
919 }
920 *pwcpy = TIFFmin(rwidth, img->width - (uint32_t)img->col_offset);
921
922 /* Determine flipping and check req_orientation value. */
923 *pflip = setorientation(img);
924
925 /* -- Set raster-origin --
926 * Set pointer into raster buffer according img->req_orientation, which
927 * defines in which corner of the two dimensional "raster" matrix the image
928 * will be moved (i.e. which raster-corner will be the raster origin).
929 * Furthermore, if vertical flipping is required adapt pointer into raster
930 * to the last line. Then, write the lines from the bottom line to the top
931 * line of the image area within the raster buffer.
932 */
933 switch (img->req_orientation)
934 {
937 *pp_raster_start = raster;
938 break;
941 *pp_raster_start = raster + ((size_t)rwidth - *pwcpy);
942 break;
945 *pp_raster_start = raster + ((size_t)rheight - *phcpy) * rwidth +
946 ((size_t)rwidth - *pwcpy);
947 break;
950 *pp_raster_start = raster + ((size_t)rheight - *phcpy) * rwidth;
951 break;
952 default: /* NOTREACHED */
953 return 0;
954 }
955
956 /* Set increment (toskew) to next or previous line in raster.
957 * And initialize row index (y) of the image area within the raster
958 * buffer.
959 * Tiled images are read tile by tile and therefore toskew has to be
960 * calculated using tile-width.
961 */
962 const uint32_t cpywidth = isTiled ? tilewidth : *pwcpy;
963 if (*pflip & FLIP_VERTICALLY)
964 {
965 if ((int64_t)rwidth + cpywidth > INT_MAX)
966 {
967 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
968 "Width overflow in %s", funcName);
969 return 0;
970 }
971 /* Skew back to the raster row before the currently written row
972 * -> one raster width plus copied image pixels. */
973 *ptoskew = -(int32_t)(rwidth + cpywidth);
974 *py = *phcpy - 1; /* start with last image line in raster */
975 }
976 else
977 {
978 /* Skew forward to the end of the raster width of the row currently
979 * copied. */
980 if (cpywidth > ((int64_t)INT_MAX + rwidth) ||
981 rwidth > ((int64_t)INT_MAX + cpywidth))
982 {
983 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
984 "Width underflow in %s", funcName);
985 return 0;
986 }
987 *ptoskew = (int32_t)(rwidth - cpywidth);
988 *py = 0; /* start with first image line in raster */
989 }
990 return 1;
991}
992
993/*
994 * The sub-byte put routines (bitspersample 1/2/4) always begin reading at a
995 * byte boundary, so a horizontal offset whose bit position is not a multiple
996 * of 8 would make them emit pixels shifted left within the first byte. Reject
997 * such a request rather than return wrong content. bitoffset is the bit
998 * position of the first sample to read (a multiple of 8 for bps >= 8).
999 */
1013static int checkColOffsetByteAligned(TIFFRGBAImage *img, const char *module,
1014 uint64_t bitoffset)
1015{
1016 if ((bitoffset % 8) != 0)
1017 {
1018 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
1019 "%s: column offset %d does not align to a byte boundary "
1020 "for bitspersample %u / samplesperpixel %u",
1021 module, img->col_offset, (unsigned)img->bitspersample,
1022 (unsigned)img->samplesperpixel);
1023 return 0;
1024 }
1025 return 1;
1026}
1027
1028/*
1029 * Get an tile-organized image that has
1030 * PlanarConfiguration contiguous if SamplesPerPixel > 1
1031 * or
1032 * SamplesPerPixel == 1
1033 */
1063static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t rwidth,
1064 uint32_t rheight)
1065{
1066 TIFF *tif = img->tif;
1067 tileContigRoutine put = img->put.contig;
1068 uint32_t col, row, y, rowstoread;
1069 tmsize_t pos;
1070 uint32_t tw, th;
1071 unsigned char *buf = NULL;
1072 int32_t fromskew, toskew;
1073 uint32_t nrow;
1074 int ret = 1, flip;
1075 uint32_t this_tw, tocol;
1076 int32_t this_toskew, leftmost_toskew;
1077 int32_t leftmost_fromskew;
1078 uint32_t leftmost_tw;
1079 tmsize_t bufsize;
1080
1081 uint32_t wcpy; /* samples to copy in raster width direction */
1082 uint32_t hcpy; /* lines to copy in raster height direction */
1083 uint32_t *p_raster_start; /* pointer into raster where imagedata shall start
1084 to be written */
1085
1086 /* For explanation of some local parameters refer to gtStripContig() */
1087
1090
1091 if (tw == 0 || th == 0)
1092 {
1093 TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
1094 return 0;
1095 }
1096
1097 if (!calcCopyToRasterParameter(img, raster, rwidth, rheight, &wcpy, &hcpy,
1098 &flip, &p_raster_start, &toskew, &y, true,
1099 tw, "gtTileContig"))
1100 {
1101 return 0;
1102 }
1103
1104 bufsize = TIFFTileSize(tif);
1105 if (bufsize == 0)
1106 {
1107 TIFFErrorExtR(tif, TIFFFileName(tif), "%s", "No space for tile buffer");
1108 return 0;
1109 }
1110
1111 /*
1112 * Reading the image data tile by tile.
1113 * Leftmost tile is clipped on left side if col_offset > 0.
1114 */
1115 leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
1116 leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
1117 int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
1118 if (skew_i64 > INT_MAX || skew_i64 < INT_MIN)
1119 {
1120 TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew",
1121 skew_i64);
1122 return 0;
1123 }
1124 leftmost_toskew = (int32_t)skew_i64;
1125 if (!checkColOffsetByteAligned(img, "gtTileContig",
1126 (uint64_t)(uint32_t)leftmost_fromskew *
1127 img->samplesperpixel *
1128 img->bitspersample))
1129 return 0;
1130
1131 /* Determine if YCbCr subsampling requires block-aware pos calculation. */
1132 int ycbcr_subsampled = 0;
1133 uint16_t ycbcr_ss_h = 1, ycbcr_ss_v = 1;
1134 uint32_t ycbcr_bpr = 0; /* blocks per tile row */
1135 uint32_t ycbcr_bsz = 0; /* bytes per block */
1136 if (img->photometric == PHOTOMETRIC_YCBCR && !isUpSampled(tif))
1137 {
1139 &ycbcr_ss_v);
1140 if (ycbcr_ss_h == 0 || ycbcr_ss_v == 0)
1141 {
1142 TIFFErrorExtR(tif, TIFFFileName(tif),
1143 "Invalid YCbCr subsampling (%d, %d)", (int)ycbcr_ss_h,
1144 (int)ycbcr_ss_v);
1145 return 0;
1146 }
1147 if (ycbcr_ss_h > 1 || ycbcr_ss_v > 1)
1148 {
1149 ycbcr_subsampled = 1;
1150 ycbcr_bpr = TIFFhowmany_32(tw, ycbcr_ss_h);
1151 ycbcr_bsz = (uint32_t)ycbcr_ss_h * ycbcr_ss_v + 2;
1152 /* A column offset inside a sampling block cannot be
1153 * represented by the packed YCbCr put routines. */
1154 if (((uint32_t)img->col_offset % ycbcr_ss_h) != 0)
1155 {
1156 TIFFErrorExtR(tif, TIFFFileName(tif),
1157 "gtTileContig: column offset %d does not align "
1158 "to the YCbCr subsampling block",
1159 img->col_offset);
1160 return 0;
1161 }
1162 }
1163 }
1164
1165 for (row = 0; ret != 0 && row < hcpy; row += nrow)
1166 {
1167 rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
1168 nrow = row + rowstoread > hcpy ? hcpy - row : rowstoread;
1169 fromskew = leftmost_fromskew;
1170 this_tw = leftmost_tw;
1171 this_toskew = leftmost_toskew;
1172 tocol = 0;
1173 col = (uint32_t)img->col_offset;
1174 /* wcpy: only write imagewidth if raster is bigger. */
1175 while (tocol < wcpy)
1176 {
1177 if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col,
1178 (uint32_t)row +
1179 (uint32_t)img->row_offset,
1180 0, 0) == (tmsize_t)(-1) &&
1181 (buf == NULL || img->stoponerr))
1182 {
1183 ret = 0;
1184 break;
1185 }
1186 /* Compute the tile-internal start offset in 64-bit so that it
1187 * cannot wrap on 32-bit builds, and reject a start offset that
1188 * lies outside the tile buffer. This only validates where the
1189 * put routine starts reading, not how far it reads. */
1190 const uint32_t trow =
1191 ((uint32_t)row + (uint32_t)img->row_offset) % th;
1192 uint64_t pos64;
1193 if (ycbcr_subsampled)
1194 {
1195 pos64 = (uint64_t)(trow / ycbcr_ss_v) * ycbcr_bpr * ycbcr_bsz +
1196 (uint64_t)((uint32_t)fromskew / ycbcr_ss_h) * ycbcr_bsz;
1197 }
1198 else
1199 {
1200 const tmsize_t trowsize = TIFFTileRowSize(tif);
1201 if (trowsize <= 0)
1202 {
1203 ret = 0;
1204 break;
1205 }
1206 pos64 = (uint64_t)trow * (uint64_t)trowsize +
1207 ((uint64_t)(uint32_t)fromskew * img->samplesperpixel *
1208 img->bitspersample) /
1209 8;
1210 }
1211 if (pos64 >= (uint64_t)bufsize)
1212 {
1213 TIFFErrorExtR(tif, TIFFFileName(tif),
1214 "gtTileContig: tile offset out of range");
1215 ret = 0;
1216 break;
1217 }
1218 pos = (tmsize_t)pos64;
1219 if (tocol + this_tw > wcpy)
1220 {
1221 /*
1222 * Rightmost tile is clipped on right side.
1223 */
1224 fromskew = (int32_t)(tw - (wcpy - tocol));
1225 this_tw = (uint32_t)((int32_t)tw - fromskew);
1226 this_toskew = (int32_t)(toskew + (int32_t)(uint32_t)fromskew);
1227 }
1228 tmsize_t roffset = (tmsize_t)y * rwidth + tocol;
1229 (*put)(img, p_raster_start + roffset, tocol, y, this_tw, nrow,
1230 fromskew, this_toskew, buf + pos);
1231 tocol += this_tw;
1232 col += this_tw;
1233 /*
1234 * After the leftmost tile, tiles are no longer clipped on left
1235 * side.
1236 */
1237 fromskew = 0;
1238 this_tw = tw;
1239 this_toskew = toskew;
1240 }
1241
1242 y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1243 : (int32_t)nrow);
1244 }
1245 _TIFFfreeExt(img->tif, buf);
1246
1247 if (flip & FLIP_HORIZONTALLY)
1248 {
1249 uint32_t line;
1250
1251 for (line = 0; line < hcpy; line++)
1252 {
1253 uint32_t *left = p_raster_start + (size_t)line * rwidth;
1254 /* Use wcpy to only flip horizontally data in place and not complete
1255 * raster-row. */
1256 uint32_t *right = left + wcpy - 1;
1257
1258 while (left < right)
1259 {
1260 uint32_t temp = *left;
1261 *left = *right;
1262 *right = temp;
1263 left++;
1264 right--;
1265 }
1266 }
1267 }
1268
1269 return ret;
1270}
1271
1272/*
1273 * Get an tile-organized image that has
1274 * SamplesPerPixel > 1
1275 * PlanarConfiguration separated
1276 * We assume that all such images are RGB.
1277 */
1302static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t rwidth,
1303 uint32_t rheight)
1304{
1305 TIFF *tif = img->tif;
1306 tileSeparateRoutine put = img->put.separate;
1307 uint32_t col, row, y, rowstoread;
1308 tmsize_t pos;
1309 uint32_t tw, th;
1310 unsigned char *buf = NULL;
1311 unsigned char *p0 = NULL;
1312 unsigned char *p1 = NULL;
1313 unsigned char *p2 = NULL;
1314 unsigned char *pa = NULL;
1315 tmsize_t tilesize;
1316 tmsize_t bufsize;
1317 int32_t fromskew, toskew;
1318 int alpha = img->alpha;
1319 uint32_t nrow;
1320 int ret = 1, flip;
1321 uint16_t colorchannels;
1322 uint32_t this_tw, tocol;
1323 int32_t this_toskew, leftmost_toskew;
1324 int32_t leftmost_fromskew;
1325 uint32_t leftmost_tw;
1326
1327 uint32_t wcpy; /* samples to copy in raster width direction */
1328 uint32_t hcpy; /* lines to copy in raster height direction */
1329 uint32_t *p_raster_start; /* pointer into raster where imagedata shall start
1330 to be written */
1331
1332 /* For explanation of some local parameters refer to gtStripContig() */
1333
1336
1337 if (tw == 0 || th == 0)
1338 {
1339 TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
1340 return 0;
1341 }
1342
1343 if (!calcCopyToRasterParameter(img, raster, rwidth, rheight, &wcpy, &hcpy,
1344 &flip, &p_raster_start, &toskew, &y, true,
1345 tw, "gtTileSeparate"))
1346 {
1347 return 0;
1348 }
1349
1350 tilesize = TIFFTileSize(tif);
1351 bufsize =
1352 _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate");
1353 if (bufsize == 0)
1354 return 0;
1355
1356 switch (img->photometric)
1357 {
1361 colorchannels = 1;
1362 break;
1363
1364 default:
1365 colorchannels = 3;
1366 break;
1367 }
1368
1369 /*
1370 * Leftmost tile is clipped on left side if col_offset > 0.
1371 */
1372 leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
1373 leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
1374 int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
1375 if (skew_i64 > INT_MAX || skew_i64 < INT_MIN)
1376 {
1377 TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew",
1378 skew_i64);
1379 return 0;
1380 }
1381 leftmost_toskew = (int32_t)skew_i64;
1382 if (!checkColOffsetByteAligned(img, "gtTileSeparate",
1383 (uint64_t)(uint32_t)leftmost_fromskew *
1384 img->bitspersample))
1385 return 0;
1386 for (row = 0; ret != 0 && row < hcpy; row += nrow)
1387 {
1388 rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
1389 nrow = row + rowstoread > hcpy ? hcpy - row : rowstoread;
1390 fromskew = leftmost_fromskew;
1391 this_tw = leftmost_tw;
1392 this_toskew = leftmost_toskew;
1393 tocol = 0;
1394 col = (uint32_t)img->col_offset;
1395 /* wcpy: only write imagewidth if raster is bigger. */
1396 while (tocol < wcpy)
1397 {
1398 if (buf == NULL)
1399 {
1401 tif, (void **)&buf, bufsize, col,
1402 (uint32_t)((int32_t)row + img->row_offset), 0,
1403 0) == (tmsize_t)(-1) &&
1404 (buf == NULL || img->stoponerr))
1405 {
1406 ret = 0;
1407 break;
1408 }
1409 p0 = buf;
1410 if (colorchannels == 1)
1411 {
1412 p2 = p1 = p0;
1413 pa = (alpha ? (p0 + 3 * tilesize) : NULL);
1414 }
1415 else
1416 {
1417 p1 = p0 + tilesize;
1418 p2 = p1 + tilesize;
1419 pa = (alpha ? (p2 + tilesize) : NULL);
1420 }
1421 }
1422 else if (TIFFReadTile(tif, p0, col,
1423 (uint32_t)((int32_t)row + img->row_offset), 0,
1424 0) == (tmsize_t)(-1) &&
1425 img->stoponerr)
1426 {
1427 ret = 0;
1428 break;
1429 }
1430 if (colorchannels > 1 &&
1431 TIFFReadTile(tif, p1, col,
1432 (uint32_t)((int32_t)row + img->row_offset), 0,
1433 (uint16_t)1) == (tmsize_t)(-1) &&
1434 img->stoponerr)
1435 {
1436 ret = 0;
1437 break;
1438 }
1439 if (colorchannels > 1 &&
1440 TIFFReadTile(tif, p2, col,
1441 (uint32_t)((int32_t)row + img->row_offset), 0,
1442 (uint16_t)2) == (tmsize_t)(-1) &&
1443 img->stoponerr)
1444 {
1445 ret = 0;
1446 break;
1447 }
1448 if (alpha &&
1449 TIFFReadTile(tif, pa, col,
1450 (uint32_t)((int32_t)row + img->row_offset), 0,
1451 (uint16_t)colorchannels) == (tmsize_t)(-1) &&
1452 img->stoponerr)
1453 {
1454 ret = 0;
1455 break;
1456 }
1457
1458 /* For SEPARATE the pos-offset is per sample and should not be
1459 * multiplied by img->samplesperpixel. */
1460 /* Compute the tile-internal start offset in 64-bit as in
1461 * gtTileContig(). Each sample plane is tilesize bytes, so the
1462 * offset must lie within one plane. */
1463 const uint32_t trow =
1464 (uint32_t)((int32_t)row + img->row_offset) % th;
1465 const tmsize_t trowsize = TIFFTileRowSize(tif);
1466 if (trowsize <= 0)
1467 {
1468 ret = 0;
1469 break;
1470 }
1471 const uint64_t pos64 =
1472 (uint64_t)trow * (uint64_t)trowsize +
1473 ((uint64_t)(uint32_t)fromskew * img->bitspersample) / 8;
1474 if (pos64 >= (uint64_t)tilesize)
1475 {
1476 TIFFErrorExtR(tif, TIFFFileName(tif),
1477 "gtTileSeparate: tile offset out of range");
1478 ret = 0;
1479 break;
1480 }
1481 pos = (tmsize_t)pos64;
1482 if (tocol + this_tw > wcpy)
1483 {
1484 /*
1485 * Rightmost tile is clipped on right side.
1486 */
1487 fromskew = (int32_t)(tw - (wcpy - tocol));
1488 this_tw = (uint32_t)((int32_t)tw - fromskew);
1489 this_toskew = (int32_t)(toskew + (int32_t)(uint32_t)fromskew);
1490 }
1491 tmsize_t roffset = (tmsize_t)y * rwidth + tocol;
1492 (*put)(img, p_raster_start + roffset, tocol, y, this_tw, nrow,
1493 fromskew, this_toskew, p0 + pos, p1 + pos, p2 + pos,
1494 alpha ? pa + pos : NULL);
1495 tocol += this_tw;
1496 col += this_tw;
1497 /*
1498 * After the leftmost tile, tiles are no longer clipped on left
1499 * side.
1500 */
1501 fromskew = 0;
1502 this_tw = tw;
1503 this_toskew = toskew;
1504 }
1505
1506 y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1507 : (int32_t)nrow);
1508 }
1509
1510 if (flip & FLIP_HORIZONTALLY)
1511 {
1512 uint32_t line;
1513
1514 for (line = 0; line < hcpy; line++)
1515 {
1516 uint32_t *left = p_raster_start + (size_t)line * rwidth;
1517 /* Use wcpy to only flip horizontally data in place and not complete
1518 * raster-row. */
1519 uint32_t *right = left + wcpy - 1;
1520
1521 while (left < right)
1522 {
1523 uint32_t temp = *left;
1524 *left = *right;
1525 *right = temp;
1526 left++;
1527 right--;
1528 }
1529 }
1530 }
1531
1532 _TIFFfreeExt(img->tif, buf);
1533 return ret;
1534}
1535
1536/*
1537 * Get a strip-organized image that has
1538 * PlanarConfiguration contiguous if SamplesPerPixel > 1
1539 * or
1540 * SamplesPerPixel == 1
1541 */
1574static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t rwidth,
1575 uint32_t rheight)
1576{
1577 TIFF *tif = img->tif;
1578 tileContigRoutine put = img->put.contig;
1579 uint32_t row, y, nrow, nrowsub, rowstoread;
1580 tmsize_t pos;
1581 unsigned char *buf = NULL;
1582 uint32_t rowsperstrip;
1583 uint16_t subsamplinghor, subsamplingver;
1584 tmsize_t scanline;
1585 /* fromskew, toskew are the increments within the input image or the raster
1586 * from the end of a line to the start of the next line to read or write. */
1587 int32_t fromskew, toskew;
1588 int ret = 1, flip;
1589 tmsize_t maxstripsize;
1590 int ycbcr_subsampled = 0;
1591 uint32_t ycbcr_bpr = 0; /* blocks per strip row */
1592 uint32_t ycbcr_bsz = 0; /* bytes per block */
1593 tmsize_t size_to_read;
1594
1595 uint32_t wcpy; /* samples to copy in raster width direction */
1596 uint32_t hcpy; /* lines to copy in raster height direction */
1597 uint32_t *p_raster_start; /* pointer into raster where imagedata shall start
1598 to be written (image area within raster) */
1599
1600 if (!calcCopyToRasterParameter(img, raster, rwidth, rheight, &wcpy, &hcpy,
1601 &flip, &p_raster_start, &toskew, &y, false,
1602 0, "gtStripContig"))
1603 {
1604 return 0;
1605 }
1606
1607 if (!checkColOffsetByteAligned(img, "gtStripContig",
1608 (uint64_t)(uint32_t)img->col_offset *
1609 img->samplesperpixel *
1610 img->bitspersample))
1611 return 0;
1612
1613 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor,
1614 &subsamplingver);
1615 if (subsamplinghor == 0 || subsamplingver == 0)
1616 {
1617 TIFFErrorExtR(tif, TIFFFileName(tif), "Invalid YCbCr subsampling");
1618 return 0;
1619 }
1620 if (img->photometric == PHOTOMETRIC_YCBCR && !isUpSampled(tif) &&
1621 (subsamplinghor > 1 || subsamplingver > 1))
1622 {
1623 ycbcr_subsampled = 1;
1624 ycbcr_bpr = TIFFhowmany_32(img->width, subsamplinghor);
1625 ycbcr_bsz = (uint32_t)subsamplinghor * subsamplingver + 2;
1626 /* A column offset inside a sampling block cannot be represented by
1627 * the packed YCbCr put routines. */
1628 if (((uint32_t)img->col_offset % subsamplinghor) != 0)
1629 {
1630 TIFFErrorExtR(tif, TIFFFileName(tif),
1631 "gtStripContig: column offset %d does not align to "
1632 "the YCbCr subsampling block",
1633 img->col_offset);
1634 return 0;
1635 }
1636 }
1637
1638 maxstripsize = TIFFStripSize(tif);
1639
1640 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1641 if (rowsperstrip == 0)
1642 {
1643 TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1644 return 0;
1645 }
1646
1647 scanline = TIFFScanlineSize(tif);
1648 if (wcpy < img->width && (img->width - wcpy) > (uint32_t)INT32_MAX)
1649 {
1650 TIFFErrorExtR(tif, TIFFFileName(tif),
1651 "Too large fromskew value (width=%" PRIu32 ")",
1652 img->width);
1653 return 0;
1654 }
1655 fromskew = (int32_t)(wcpy < img->width ? img->width - wcpy : 0);
1656
1657 for (row = 0; row < hcpy; row += nrow)
1658 {
1659 uint32_t temp;
1660 /* rowstoread = number of rows within current strip from
1661 * current row till end of strip.
1662 * -> rowsFromRowToStripEnd */
1663 rowstoread = rowsperstrip -
1664 (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip;
1665 /* nrow, nrowsub = number of rows to read within current row-loop.
1666 * -> rowsToRead, rowsToReadSub */
1667 nrow = row + rowstoread > hcpy ? hcpy - row : rowstoread;
1668 nrowsub = nrow;
1669 if ((nrowsub % subsamplingver) != 0)
1670 nrowsub += subsamplingver - nrowsub % subsamplingver;
1671 /* temp = number of rows to read from beginning of current strip over
1672 * current row till last row to read within current strip.
1673 * -> rowsToReadFromStripStart */
1674 temp =
1675 (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrowsub;
1676 size_to_read = TIFFVStripSize(tif, temp);
1677 if (size_to_read <= 0)
1678 {
1679 TIFFErrorExtR(tif, TIFFFileName(tif),
1680 "Invalid strip read size in gtStripContig");
1681 return 0;
1682 }
1684 tif,
1685 TIFFComputeStrip(tif,
1686 (uint32_t)((int32_t)row + img->row_offset), 0),
1687 (void **)(&buf), maxstripsize,
1688 size_to_read) == (tmsize_t)(-1) &&
1689 (buf == NULL || img->stoponerr))
1690 {
1691 ret = 0;
1692 break;
1693 }
1694 /* pos = read offset into strip-buffer where to start reading.
1695 * Compute it in 64-bit so that it cannot wrap on 32-bit builds, and
1696 * reject a start offset that lies outside the strip buffer. This
1697 * only validates where the put routine starts reading. */
1698 const uint32_t row_in_strip =
1699 (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip;
1700 uint64_t pos64;
1701 if (ycbcr_subsampled)
1702 {
1703 pos64 = (uint64_t)(row_in_strip / subsamplingver) * ycbcr_bpr *
1704 ycbcr_bsz +
1705 (uint64_t)((uint32_t)img->col_offset / subsamplinghor) *
1706 ycbcr_bsz;
1707 }
1708 else
1709 {
1710 pos64 = (uint64_t)row_in_strip * (uint64_t)scanline +
1711 ((uint64_t)(uint32_t)img->col_offset *
1712 img->samplesperpixel * img->bitspersample) /
1713 8;
1714 }
1715 if (pos64 >= (uint64_t)maxstripsize)
1716 {
1717 TIFFErrorExtR(tif, TIFFFileName(tif),
1718 "gtStripContig: strip offset out of range");
1719 ret = 0;
1720 break;
1721 }
1722 pos = (tmsize_t)pos64;
1723 /* roffset = offset into raster, where to write. */
1724 tmsize_t roffset = (tmsize_t)y * rwidth;
1725 (*put)(img, p_raster_start + roffset, 0, y, wcpy, nrow, fromskew,
1726 toskew, buf + pos);
1727 /* y = row-index into image area within raster where to write
1728 * (i.e. for offset calculations). */
1729 y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1730 : (int32_t)nrow);
1731 }
1732
1733 if (flip & FLIP_HORIZONTALLY)
1734 {
1735 /* Flips the image area within the raster buffer horizontally.
1736 * Use wcpy to only flip data in place. */
1737 uint32_t line;
1738
1739 for (line = 0; line < hcpy; line++)
1740 {
1741 uint32_t *left = p_raster_start + (size_t)line * rwidth;
1742 /* Use wcpy to only flip horizontally data in place and not complete
1743 * raster-row. */
1744 uint32_t *right = left + wcpy - 1;
1745
1746 while (left < right)
1747 {
1748 uint32_t temp = *left;
1749 *left = *right;
1750 *right = temp;
1751 left++;
1752 right--;
1753 }
1754 }
1755 }
1756
1757 _TIFFfreeExt(img->tif, buf);
1758 return ret;
1759}
1760
1761/*
1762 * Get a strip-organized image with
1763 * SamplesPerPixel > 1
1764 * PlanarConfiguration separated
1765 * We assume that all such images are RGB.
1766 */
1792static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster,
1793 uint32_t rwidth, uint32_t rheight)
1794{
1795 TIFF *tif = img->tif;
1796 tileSeparateRoutine put = img->put.separate;
1797 unsigned char *buf = NULL;
1798 unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;
1799 uint32_t row, y, nrow, rowstoread;
1800 tmsize_t pos;
1801 tmsize_t scanline;
1802 uint32_t rowsperstrip, offset_row;
1803 tmsize_t stripsize;
1804 tmsize_t bufsize;
1805 int32_t fromskew, toskew;
1806 int alpha = img->alpha; /* local variable is faster */
1807 int ret = 1, flip;
1808 uint16_t colorchannels;
1809 uint32_t wcpy; /* samples to copy in raster width direction */
1810 uint32_t hcpy; /* lines to copy in raster height direction */
1811 uint32_t *p_raster_start; /* pointer into raster where imagedata shall start
1812 to be written */
1813
1814 /* For explanation of local parameters refer to gtStripContig() */
1815 if (!calcCopyToRasterParameter(img, raster, rwidth, rheight, &wcpy, &hcpy,
1816 &flip, &p_raster_start, &toskew, &y, false,
1817 0, "gtStripSeparate"))
1818 {
1819 return 0;
1820 }
1821
1822 if (!checkColOffsetByteAligned(img, "gtStripSeparate",
1823 (uint64_t)(uint32_t)img->col_offset *
1824 img->bitspersample))
1825 return 0;
1826
1827 stripsize = TIFFStripSize(tif);
1828 bufsize =
1829 _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate");
1830 if (bufsize == 0)
1831 return 0;
1832
1833 switch (img->photometric)
1834 {
1838 colorchannels = 1;
1839 break;
1840
1841 default:
1842 colorchannels = 3;
1843 break;
1844 }
1845
1846 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1847 if (rowsperstrip == 0)
1848 {
1849 TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1850 return 0;
1851 }
1852
1853 scanline = TIFFScanlineSize(tif);
1854 if (wcpy < img->width && (img->width - wcpy) > (uint32_t)INT32_MAX)
1855 {
1856 TIFFErrorExtR(tif, TIFFFileName(tif),
1857 "Too large fromskew value (width=%" PRIu32 ")",
1858 img->width);
1859 return 0;
1860 }
1861 fromskew = (int32_t)(wcpy < img->width ? img->width - wcpy : 0);
1862 for (row = 0; row < hcpy; row += nrow)
1863 {
1864 uint32_t temp;
1865 rowstoread =
1866 rowsperstrip -
1867 ((uint32_t)((int32_t)row + img->row_offset) % rowsperstrip);
1868 nrow = row + rowstoread > hcpy ? hcpy - row : rowstoread;
1869 offset_row = (uint32_t)((int32_t)row + img->row_offset);
1870 temp = (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrow;
1871 if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))
1872 {
1873 TIFFErrorExtR(tif, TIFFFileName(tif),
1874 "Integer overflow in gtStripSeparate");
1875 return 0;
1876 }
1877 if (buf == NULL)
1878 {
1880 tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf,
1881 bufsize, temp * scanline) == (tmsize_t)(-1) &&
1882 (buf == NULL || img->stoponerr))
1883 {
1884 ret = 0;
1885 break;
1886 }
1887 p0 = buf;
1888 if (colorchannels == 1)
1889 {
1890 p2 = p1 = p0;
1891 pa = (alpha ? (p0 + 3 * stripsize) : NULL);
1892 }
1893 else
1894 {
1895 p1 = p0 + stripsize;
1896 p2 = p1 + stripsize;
1897 pa = (alpha ? (p2 + stripsize) : NULL);
1898 }
1899 }
1900 else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
1901 p0, temp * scanline) == (tmsize_t)(-1) &&
1902 img->stoponerr)
1903 {
1904 ret = 0;
1905 break;
1906 }
1907 if (colorchannels > 1 &&
1908 TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1,
1909 temp * scanline) == (tmsize_t)(-1) &&
1910 img->stoponerr)
1911 {
1912 ret = 0;
1913 break;
1914 }
1915 if (colorchannels > 1 &&
1916 TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2,
1917 temp * scanline) == (tmsize_t)(-1) &&
1918 img->stoponerr)
1919 {
1920 ret = 0;
1921 break;
1922 }
1923 if (alpha)
1924 {
1926 tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa,
1927 temp * scanline) == (tmsize_t)(-1) &&
1928 img->stoponerr)
1929 {
1930 ret = 0;
1931 break;
1932 }
1933 }
1934
1935 /* For SEPARATE the pos-offset is per sample and should not be
1936 * multiplied by img->samplesperpixel. Compute it in 64-bit as in
1937 * gtStripContig(); each sample plane is stripsize bytes, so the
1938 * offset must lie within one plane. */
1939 const uint64_t pos64 =
1940 (uint64_t)((uint32_t)((int32_t)row + img->row_offset) %
1941 rowsperstrip) *
1942 (uint64_t)scanline +
1943 ((uint64_t)(uint32_t)img->col_offset * img->bitspersample) / 8;
1944 if (pos64 >= (uint64_t)stripsize)
1945 {
1946 TIFFErrorExtR(tif, TIFFFileName(tif),
1947 "gtStripSeparate: strip offset out of range");
1948 ret = 0;
1949 break;
1950 }
1951 pos = (tmsize_t)pos64;
1952 tmsize_t roffset = (tmsize_t)y * rwidth;
1953 (*put)(img, p_raster_start + roffset, 0, y, wcpy, nrow, fromskew,
1954 toskew, p0 + pos, p1 + pos, p2 + pos,
1955 (alpha ? (pa + pos) : NULL));
1956 y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1957 : (int32_t)nrow);
1958 }
1959
1960 if (flip & FLIP_HORIZONTALLY)
1961 {
1962 uint32_t line;
1963
1964 for (line = 0; line < hcpy; line++)
1965 {
1966 uint32_t *left = p_raster_start + (size_t)line * rwidth;
1967 /* Use wcpy to only flip horizontally data in place and not complete
1968 * raster-row. */
1969 uint32_t *right = left + wcpy - 1;
1970
1971 while (left < right)
1972 {
1973 uint32_t temp = *left;
1974 *left = *right;
1975 *right = temp;
1976 left++;
1977 right--;
1978 }
1979 }
1980 }
1981
1982 _TIFFfreeExt(img->tif, buf);
1983 return ret;
1984}
1985
1986/*
1987 * The following routines move decoded data returned
1988 * from the TIFF library into rasters filled with packed
1989 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
1990 *
1991 * The routines have been created according to the most
1992 * important cases and optimized. PickContigCase and
1993 * PickSeparateCase analyze the parameters and select
1994 * the appropriate "get" and "put" routine to use.
1995 */
2005#define REPEAT8(op) \
2006 REPEAT4(op); \
2007 REPEAT4(op)
2017#define REPEAT4(op) \
2018 REPEAT2(op); \
2019 REPEAT2(op)
2029#define REPEAT2(op) \
2030 op; \
2031 op
2042#define CASE8(x, op) \
2043 switch (x) \
2044 { \
2045 case 7: \
2046 op; /*-fallthrough*/ \
2047 case 6: \
2048 op; /*-fallthrough*/ \
2049 case 5: \
2050 op; /*-fallthrough*/ \
2051 case 4: \
2052 op; /*-fallthrough*/ \
2053 case 3: \
2054 op; /*-fallthrough*/ \
2055 case 2: \
2056 op; /*-fallthrough*/ \
2057 case 1: \
2058 op; \
2059 break; \
2060 default: \
2061 break; \
2062 }
2073#define CASE4(x, op) \
2074 switch (x) \
2075 { \
2076 case 3: \
2077 op; /*-fallthrough*/ \
2078 case 2: \
2079 op; /*-fallthrough*/ \
2080 case 1: \
2081 op; \
2082 break; \
2083 default: \
2084 break; \
2085 }
2093#define NOP
2094
2107#define UNROLL8(w, op1, op2) \
2108 { \
2109 uint32_t _x; \
2110 for (_x = w; _x >= 8; _x -= 8) \
2111 { \
2112 op1; \
2113 REPEAT8(op2); \
2114 } \
2115 if (_x > 0) \
2116 { \
2117 op1; \
2118 CASE8(_x, op2); \
2119 } \
2120 }
2133#define UNROLL4(w, op1, op2) \
2134 { \
2135 uint32_t _x; \
2136 for (_x = w; _x >= 4; _x -= 4) \
2137 { \
2138 op1; \
2139 REPEAT4(op2); \
2140 } \
2141 if (_x > 0) \
2142 { \
2143 op1; \
2144 CASE4(_x, op2); \
2145 } \
2146 }
2159#define UNROLL2(w, op1, op2) \
2160 { \
2161 uint32_t _x; \
2162 for (_x = w; _x >= 2; _x -= 2) \
2163 { \
2164 op1; \
2165 REPEAT2(op2); \
2166 } \
2167 if (_x) \
2168 { \
2169 op1; \
2170 op2; \
2171 } \
2172 }
2173
2186#define SKEW(r, g, b, skew) \
2187 { \
2188 r += skew; \
2189 g += skew; \
2190 b += skew; \
2191 }
2205#define SKEW4(r, g, b, a, skew) \
2206 { \
2207 r += skew; \
2208 g += skew; \
2209 b += skew; \
2210 a += skew; \
2211 }
2212
2220#define A1 (((uint32_t)0xffL) << 24)
2233#define PACK(r, g, b) \
2234 ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1)
2248#define PACK4(r, g, b, a) \
2249 ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | \
2250 ((uint32_t)(a) << 24))
2251
2261#define DECLAREContigPutFunc(name) \
2262 static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
2263 uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
2264 unsigned char *pp)
2265
2266/*
2267 * 8-bit palette => colormap/RGB
2268 */
2298{
2299 uint32_t **PALmap = img->PALmap;
2300 int samplesperpixel = img->samplesperpixel;
2301 /* Convert pixel skew to byte skew (8-bit samples) */
2302 const tmsize_t fromskewLocal =
2303 (tmsize_t)fromskew * +(tmsize_t)samplesperpixel;
2304
2305 (void)y;
2306 for (; h > 0; --h)
2307 {
2308 for (x = w; x > 0; --x)
2309 {
2310 *cp++ = PALmap[*pp][0];
2311 pp += samplesperpixel;
2312 }
2313 cp += toskew;
2314 pp += fromskewLocal;
2315 }
2316}
2317
2318/*
2319 * 4-bit palette => colormap/RGB
2320 */
2350{
2351 uint32_t **PALmap = img->PALmap;
2352
2353 (void)x;
2354 (void)y;
2355 fromskew /= 2;
2356 for (; h > 0; --h)
2357 {
2358 uint32_t *bw;
2359 UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
2360 cp += toskew;
2361 pp += fromskew;
2362 }
2363}
2364
2365/*
2366 * 2-bit palette => colormap/RGB
2367 */
2397{
2398 uint32_t **PALmap = img->PALmap;
2399
2400 (void)x;
2401 (void)y;
2402 fromskew /= 4;
2403 for (; h > 0; --h)
2404 {
2405 uint32_t *bw;
2406 UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
2407 cp += toskew;
2408 pp += fromskew;
2409 }
2410}
2411
2412/*
2413 * 1-bit palette => colormap/RGB
2414 */
2444{
2445 uint32_t **PALmap = img->PALmap;
2446
2447 (void)x;
2448 (void)y;
2449 fromskew /= 8;
2450 for (; h > 0; --h)
2451 {
2452 uint32_t *bw;
2453 UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
2454 cp += toskew;
2455 pp += fromskew;
2456 }
2457}
2458
2459/*
2460 * 8-bit greyscale => colormap/RGB
2461 */
2491{
2492 int samplesperpixel = img->samplesperpixel;
2493 uint32_t **BWmap = img->BWmap;
2494 /* Convert pixel skew to byte skew (8-bit samples) */
2495 const tmsize_t fromskewLocal =
2496 (tmsize_t)fromskew * +(tmsize_t)samplesperpixel;
2497
2498 (void)y;
2499 for (; h > 0; --h)
2500 {
2501 for (x = w; x > 0; --x)
2502 {
2503 *cp++ = BWmap[*pp][0];
2504 pp += samplesperpixel;
2505 }
2506 cp += toskew;
2507 pp += fromskewLocal;
2508 }
2509}
2510
2511/*
2512 * 8-bit greyscale with associated alpha => colormap/RGBA
2513 */
2544{
2545 int samplesperpixel = img->samplesperpixel;
2546 uint32_t **BWmap = img->BWmap;
2547 /* Convert pixel skew to byte skew (8-bit samples) */
2548 const tmsize_t fromskewLocal =
2549 (tmsize_t)fromskew * +(tmsize_t)samplesperpixel;
2550
2551 (void)y;
2552 for (; h > 0; --h)
2553 {
2554 for (x = w; x > 0; --x)
2555 {
2556 *cp++ = BWmap[*pp][0] & ((uint32_t)*(pp + 1) << 24 | ~A1);
2557 pp += samplesperpixel;
2558 }
2559 cp += toskew;
2560 pp += fromskewLocal;
2561 }
2562}
2563
2564/*
2565 * 16-bit greyscale => colormap/RGB
2566 */
2596{
2597 int samplesperpixel = img->samplesperpixel;
2598 uint32_t **BWmap = img->BWmap;
2599 /* Convert pixel skew to byte skew (16-bit samples) */
2600 const tmsize_t fromskewLocal =
2601 (tmsize_t)fromskew * (tmsize_t)(2 * samplesperpixel);
2602
2603 (void)y;
2604 for (; h > 0; --h)
2605 {
2606 uint16_t *wp = (uint16_t *)pp;
2607
2608 for (x = w; x > 0; --x)
2609 {
2610 /* use high order byte of 16bit value */
2611
2612 *cp++ = BWmap[*wp >> 8][0];
2613 pp += 2 * samplesperpixel;
2614 wp += samplesperpixel;
2615 }
2616 cp += toskew;
2617 pp += fromskewLocal;
2618 }
2619}
2620
2621/*
2622 * 1-bit bilevel => colormap/RGB
2623 */
2653{
2654 uint32_t **BWmap = img->BWmap;
2655
2656 (void)x;
2657 (void)y;
2658 fromskew /= 8;
2659 for (; h > 0; --h)
2660 {
2661 uint32_t *bw;
2662 UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
2663 cp += toskew;
2664 pp += fromskew;
2665 }
2666}
2667
2668/*
2669 * 2-bit greyscale => colormap/RGB
2670 */
2700{
2701 uint32_t **BWmap = img->BWmap;
2702
2703 (void)x;
2704 (void)y;
2705 fromskew /= 4;
2706 for (; h > 0; --h)
2707 {
2708 uint32_t *bw;
2709 UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
2710 cp += toskew;
2711 pp += fromskew;
2712 }
2713}
2714
2715/*
2716 * 4-bit greyscale => colormap/RGB
2717 */
2747{
2748 uint32_t **BWmap = img->BWmap;
2749
2750 (void)x;
2751 (void)y;
2752 fromskew /= 2;
2753 for (; h > 0; --h)
2754 {
2755 uint32_t *bw;
2756 UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
2757 cp += toskew;
2758 pp += fromskew;
2759 }
2760}
2761
2762/*
2763 * 8-bit packed samples, no Map => RGB
2764 */
2794{
2795 int samplesperpixel = img->samplesperpixel;
2796
2797 (void)x;
2798 (void)y;
2799 const tmsize_t fromskewLocal =
2800 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
2801 for (; h > 0; --h)
2802 {
2803 UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]);
2804 pp += samplesperpixel);
2805 cp += toskew;
2806 pp += fromskewLocal;
2807 }
2808}
2809
2810/*
2811 * 8-bit packed samples => RGBA w/ associated alpha
2812 * (known to have Map == NULL)
2813 */
2844{
2845 int samplesperpixel = img->samplesperpixel;
2846
2847 (void)x;
2848 (void)y;
2849 const tmsize_t fromskewLocal =
2850 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
2851 for (; h > 0; --h)
2852 {
2853 UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
2854 pp += samplesperpixel);
2855 cp += toskew;
2856 pp += fromskewLocal;
2857 }
2858}
2859
2860/*
2861 * 8-bit packed samples => RGBA w/ unassociated alpha
2862 * (known to have Map == NULL)
2863 */
2894{
2895 int samplesperpixel = img->samplesperpixel;
2896 (void)y;
2897 const tmsize_t fromskewLocal =
2898 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
2899 for (; h > 0; --h)
2900 {
2901 uint32_t r, g, b, a;
2902 uint8_t *m;
2903 for (x = w; x > 0; --x)
2904 {
2905 a = pp[3];
2906 m = img->UaToAa + ((size_t)a << 8);
2907 r = m[pp[0]];
2908 g = m[pp[1]];
2909 b = m[pp[2]];
2910 *cp++ = PACK4(r, g, b, a);
2911 pp += samplesperpixel;
2912 }
2913 cp += toskew;
2914 pp += fromskewLocal;
2915 }
2916}
2917
2918/*
2919 * 16-bit packed samples => RGB
2920 */
2950{
2951 int samplesperpixel = img->samplesperpixel;
2952 uint16_t *wp = (uint16_t *)pp;
2953 (void)y;
2954 const tmsize_t fromskewLocal =
2955 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
2956 for (; h > 0; --h)
2957 {
2958 for (x = w; x > 0; --x)
2959 {
2960 *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
2961 img->Bitdepth16To8[wp[2]]);
2962 wp += samplesperpixel;
2963 }
2964 cp += toskew;
2965 wp += fromskewLocal;
2966 }
2967}
2968
2969/*
2970 * 16-bit packed samples => RGBA w/ associated alpha
2971 * (known to have Map == NULL)
2972 */
3003{
3004 int samplesperpixel = img->samplesperpixel;
3005 uint16_t *wp = (uint16_t *)pp;
3006 (void)y;
3007 const tmsize_t fromskewLocal =
3008 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
3009 for (; h > 0; --h)
3010 {
3011 for (x = w; x > 0; --x)
3012 {
3013 *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
3014 img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]);
3015 wp += samplesperpixel;
3016 }
3017 cp += toskew;
3018 wp += fromskewLocal;
3019 }
3020}
3021
3022/*
3023 * 16-bit packed samples => RGBA w/ unassociated alpha
3024 * (known to have Map == NULL)
3025 */
3056{
3057 int samplesperpixel = img->samplesperpixel;
3058 uint16_t *wp = (uint16_t *)pp;
3059 (void)y;
3060 const tmsize_t fromskewLocal =
3061 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
3062 for (; h > 0; --h)
3063 {
3064 uint32_t r, g, b, a;
3065 uint8_t *m;
3066 for (x = w; x > 0; --x)
3067 {
3068 a = img->Bitdepth16To8[wp[3]];
3069 m = img->UaToAa + ((size_t)a << 8);
3070 r = m[img->Bitdepth16To8[wp[0]]];
3071 g = m[img->Bitdepth16To8[wp[1]]];
3072 b = m[img->Bitdepth16To8[wp[2]]];
3073 *cp++ = PACK4(r, g, b, a);
3074 wp += samplesperpixel;
3075 }
3076 cp += toskew;
3077 wp += fromskewLocal;
3078 }
3079}
3080
3081/*
3082 * 8-bit packed CMYK samples w/o Map => RGB
3083 *
3084 * NB: The conversion of CMYK->RGB is *very* crude.
3085 */
3115{
3116 int samplesperpixel = img->samplesperpixel;
3117 uint16_t r, g, b, k;
3118
3119 (void)x;
3120 (void)y;
3121 const tmsize_t fromskewLocal =
3122 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
3123 for (; h > 0; --h)
3124 {
3125 UNROLL8(w, NOP, k = (uint16_t)(255 - pp[3]);
3126 r = (uint16_t)((k * (uint16_t)(255 - pp[0])) / 255);
3127 g = (uint16_t)((k * (uint16_t)(255 - pp[1])) / 255);
3128 b = (uint16_t)((k * (uint16_t)(255 - pp[2])) / 255);
3129 *cp++ = PACK(r, g, b); pp += samplesperpixel);
3130 cp += toskew;
3131 pp += fromskewLocal;
3132 }
3133}
3134
3135/*
3136 * 8-bit packed CMYK samples w/Map => RGB
3137 *
3138 * NB: The conversion of CMYK->RGB is *very* crude.
3139 */
3169{
3170 int samplesperpixel = img->samplesperpixel;
3171 TIFFRGBValue *Map = img->Map;
3172 unsigned int r, g, b, k;
3173
3174 (void)y;
3175 const tmsize_t fromskewLocal =
3176 (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
3177 for (; h > 0; --h)
3178 {
3179 for (x = w; x > 0; --x)
3180 {
3181 k = 255U - pp[3];
3182 r = (k * (255U - pp[0])) / 255U;
3183 g = (k * (255U - pp[1])) / 255U;
3184 b = (k * (255U - pp[2])) / 255U;
3185 *cp++ = PACK(Map[r], Map[g], Map[b]);
3186 pp += samplesperpixel;
3187 }
3188 pp += fromskewLocal;
3189 cp += toskew;
3190 }
3191}
3192
3202#define DECLARESepPutFunc(name) \
3203 static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
3204 uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
3205 unsigned char *r, unsigned char *g, unsigned char *b, \
3206 unsigned char *a)
3207
3208/*
3209 * 8-bit unpacked samples => RGB
3210 */
3245{
3246 (void)img;
3247 (void)x;
3248 (void)y;
3249 (void)a;
3250 for (; h > 0; --h)
3251 {
3252 UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
3253 SKEW(r, g, b, fromskew);
3254 cp += toskew;
3255 }
3256}
3257
3258/*
3259 * 8-bit unpacked samples => RGBA w/ associated alpha
3260 */
3295{
3296 (void)img;
3297 (void)x;
3298 (void)y;
3299 for (; h > 0; --h)
3300 {
3301 UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
3302 SKEW4(r, g, b, a, fromskew);
3303 cp += toskew;
3304 }
3305}
3306
3307/*
3308 * 8-bit unpacked CMYK samples => RGBA
3309 */
3342{
3343 (void)img;
3344 (void)y;
3345 for (; h > 0; --h)
3346 {
3347 unsigned int rv, gv, bv, kv;
3348 for (x = w; x > 0; --x)
3349 {
3350 kv = 255U - *a++;
3351 rv = (kv * (255U - *r++)) / 255U;
3352 gv = (kv * (255U - *g++)) / 255U;
3353 bv = (kv * (255U - *b++)) / 255U;
3354 *cp++ = PACK4(rv, gv, bv, 255);
3355 }
3356 SKEW4(r, g, b, a, fromskew);
3357 cp += toskew;
3358 }
3359}
3360
3361/*
3362 * 8-bit unpacked samples => RGBA w/ unassociated alpha
3363 */
3399{
3400 (void)img;
3401 (void)y;
3402 for (; h > 0; --h)
3403 {
3404 uint32_t rv, gv, bv, av;
3405 uint8_t *m;
3406 for (x = w; x > 0; --x)
3407 {
3408 av = *a++;
3409 m = img->UaToAa + ((size_t)av << 8);
3410 rv = m[*r++];
3411 gv = m[*g++];
3412 bv = m[*b++];
3413 *cp++ = PACK4(rv, gv, bv, av);
3414 }
3415 SKEW4(r, g, b, a, fromskew);
3416 cp += toskew;
3417 }
3418}
3419
3420/*
3421 * 16-bit unpacked samples => RGB
3422 */
3457{
3458 uint16_t *wr = (uint16_t *)r;
3459 uint16_t *wg = (uint16_t *)g;
3460 uint16_t *wb = (uint16_t *)b;
3461 (void)img;
3462 (void)y;
3463 (void)a;
3464 for (; h > 0; --h)
3465 {
3466 for (x = 0; x < w; x++)
3467 *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
3468 img->Bitdepth16To8[*wb++]);
3469 SKEW(wr, wg, wb, fromskew);
3470 cp += toskew;
3471 }
3472}
3473
3474/*
3475 * 16-bit unpacked samples => RGBA w/ associated alpha
3476 */
3511{
3512 uint16_t *wr = (uint16_t *)r;
3513 uint16_t *wg = (uint16_t *)g;
3514 uint16_t *wb = (uint16_t *)b;
3515 uint16_t *wa = (uint16_t *)a;
3516 (void)img;
3517 (void)y;
3518 for (; h > 0; --h)
3519 {
3520 for (x = 0; x < w; x++)
3521 *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
3522 img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]);
3523 SKEW4(wr, wg, wb, wa, fromskew);
3524 cp += toskew;
3525 }
3526}
3527
3528/*
3529 * 16-bit unpacked samples => RGBA w/ unassociated alpha
3530 */
3566{
3567 uint16_t *wr = (uint16_t *)r;
3568 uint16_t *wg = (uint16_t *)g;
3569 uint16_t *wb = (uint16_t *)b;
3570 uint16_t *wa = (uint16_t *)a;
3571 (void)img;
3572 (void)y;
3573 for (; h > 0; --h)
3574 {
3575 uint32_t r2, g2, b2, a2;
3576 uint8_t *m;
3577 for (x = w; x > 0; --x)
3578 {
3579 a2 = img->Bitdepth16To8[*wa++];
3580 m = img->UaToAa + ((size_t)a2 << 8);
3581 r2 = m[img->Bitdepth16To8[*wr++]];
3582 g2 = m[img->Bitdepth16To8[*wg++]];
3583 b2 = m[img->Bitdepth16To8[*wb++]];
3584 *cp++ = PACK4(r2, g2, b2, a2);
3585 }
3586 SKEW4(wr, wg, wb, wa, fromskew);
3587 cp += toskew;
3588 }
3589}
3590
3591/*
3592 * 8-bit packed CIE L*a*b 1976 samples => RGB
3593 */
3623{
3624 float X, Y, Z;
3625 uint32_t r, g, b;
3626 (void)y;
3627 const tmsize_t fromskewLocal = (tmsize_t)fromskew * (tmsize_t)3;
3628 for (; h > 0; --h)
3629 {
3630 for (x = w; x > 0; --x)
3631 {
3632 TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0],
3633 (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z);
3634 TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
3635 *cp++ = PACK(r, g, b);
3636 pp += 3;
3637 }
3638 cp += toskew;
3639 pp += fromskewLocal;
3640 }
3641}
3642
3643/*
3644 * 16-bit packed CIE L*a*b 1976 samples => RGB
3645 */
3675{
3676 float X, Y, Z;
3677 uint32_t r, g, b;
3678 uint16_t *wp = (uint16_t *)pp;
3679 (void)y;
3680 const tmsize_t fromskewLocal = (tmsize_t)fromskew * (tmsize_t)3;
3681 for (; h > 0; --h)
3682 {
3683 for (x = w; x > 0; --x)
3684 {
3685 TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1],
3686 (int16_t)wp[2], &X, &Y, &Z);
3687 TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
3688 *cp++ = PACK(r, g, b);
3689 wp += 3;
3690 }
3691 cp += toskew;
3692 wp += fromskewLocal;
3693 }
3694}
3695
3696/*
3697 * YCbCr -> RGB conversion and packing routines.
3698 */
3699
3711#define YCbCrtoRGB(dst, Y) \
3712 { \
3713 uint32_t r, g, b; \
3714 TIFFYCbCrtoRGB(img->ycbcr, (Y), (int32_t)Cb, (int32_t)Cr, &r, &g, &b); \
3715 dst = PACK(r, g, b); \
3716 }
3717
3718/*
3719 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
3720 */
3750{
3751 uint32_t *cp1 = cp + w + toskew;
3752 uint32_t *cp2 = cp1 + w + toskew;
3753 uint32_t *cp3 = cp2 + w + toskew;
3754 const tmsize_t incr = 3 * (tmsize_t)w + 4 * (tmsize_t)toskew;
3755
3756 (void)y;
3757 /* adjust fromskew */
3758 const tmsize_t fromskewLocal =
3759 (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 4 + 2);
3760 if ((h & 3) == 0 && (w & 3) == 0)
3761 {
3762 for (; h >= 4; h -= 4)
3763 {
3764 x = w >> 2;
3765 do
3766 {
3767 int32_t Cb = pp[16];
3768 int32_t Cr = pp[17];
3769
3770 YCbCrtoRGB(cp[0], pp[0]);
3771 YCbCrtoRGB(cp[1], pp[1]);
3772 YCbCrtoRGB(cp[2], pp[2]);
3773 YCbCrtoRGB(cp[3], pp[3]);
3774 YCbCrtoRGB(cp1[0], pp[4]);
3775 YCbCrtoRGB(cp1[1], pp[5]);
3776 YCbCrtoRGB(cp1[2], pp[6]);
3777 YCbCrtoRGB(cp1[3], pp[7]);
3778 YCbCrtoRGB(cp2[0], pp[8]);
3779 YCbCrtoRGB(cp2[1], pp[9]);
3780 YCbCrtoRGB(cp2[2], pp[10]);
3781 YCbCrtoRGB(cp2[3], pp[11]);
3782 YCbCrtoRGB(cp3[0], pp[12]);
3783 YCbCrtoRGB(cp3[1], pp[13]);
3784 YCbCrtoRGB(cp3[2], pp[14]);
3785 YCbCrtoRGB(cp3[3], pp[15]);
3786
3787 cp += 4;
3788 cp1 += 4;
3789 cp2 += 4;
3790 cp3 += 4;
3791 pp += 18;
3792 } while (--x);
3793 cp += incr;
3794 cp1 += incr;
3795 cp2 += incr;
3796 cp3 += incr;
3797 pp += fromskewLocal;
3798 }
3799 }
3800 else
3801 {
3802 while (h > 0)
3803 {
3804 for (x = w; x > 0;)
3805 {
3806 int32_t Cb = pp[16];
3807 int32_t Cr = pp[17];
3808 switch (x)
3809 {
3810 default:
3811 switch (h)
3812 {
3813 default:
3814 YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
3815 case 3:
3816 YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
3817 case 2:
3818 YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
3819 case 1:
3820 YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
3821 } /* FALLTHROUGH */
3822 case 3:
3823 switch (h)
3824 {
3825 default:
3826 YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
3827 case 3:
3828 YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
3829 case 2:
3830 YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
3831 case 1:
3832 YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
3833 } /* FALLTHROUGH */
3834 case 2:
3835 switch (h)
3836 {
3837 default:
3838 YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
3839 case 3:
3840 YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */
3841 case 2:
3842 YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
3843 case 1:
3844 YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
3845 } /* FALLTHROUGH */
3846 case 1:
3847 switch (h)
3848 {
3849 default:
3850 YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
3851 case 3:
3852 YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */
3853 case 2:
3854 YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
3855 case 1:
3856 YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
3857 } /* FALLTHROUGH */
3858 }
3859 if (x < 4)
3860 {
3861 cp += x;
3862 cp1 += x;
3863 cp2 += x;
3864 cp3 += x;
3865 x = 0;
3866 }
3867 else
3868 {
3869 cp += 4;
3870 cp1 += 4;
3871 cp2 += 4;
3872 cp3 += 4;
3873 x -= 4;
3874 }
3875 pp += 18;
3876 }
3877 if (h <= 4)
3878 break;
3879 h -= 4;
3880 cp += incr;
3881 cp1 += incr;
3882 cp2 += incr;
3883 cp3 += incr;
3884 pp += fromskewLocal;
3885 }
3886 }
3887}
3888
3889/*
3890 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
3891 */
3921{
3922 uint32_t *cp1 = cp + w + toskew;
3923 const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
3924
3925 (void)y;
3926 const tmsize_t fromskewLocal =
3927 (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 2 + 2);
3928 if ((w & 3) == 0 && (h & 1) == 0)
3929 {
3930 for (; h >= 2; h -= 2)
3931 {
3932 x = w >> 2;
3933 do
3934 {
3935 int32_t Cb = pp[8];
3936 int32_t Cr = pp[9];
3937
3938 YCbCrtoRGB(cp[0], pp[0]);
3939 YCbCrtoRGB(cp[1], pp[1]);
3940 YCbCrtoRGB(cp[2], pp[2]);
3941 YCbCrtoRGB(cp[3], pp[3]);
3942 YCbCrtoRGB(cp1[0], pp[4]);
3943 YCbCrtoRGB(cp1[1], pp[5]);
3944 YCbCrtoRGB(cp1[2], pp[6]);
3945 YCbCrtoRGB(cp1[3], pp[7]);
3946
3947 cp += 4;
3948 cp1 += 4;
3949 pp += 10;
3950 } while (--x);
3951 cp += incr;
3952 cp1 += incr;
3953 pp += fromskewLocal;
3954 }
3955 }
3956 else
3957 {
3958 while (h > 0)
3959 {
3960 for (x = w; x > 0;)
3961 {
3962 int32_t Cb = pp[8];
3963 int32_t Cr = pp[9];
3964 switch (x)
3965 {
3966 default:
3967 switch (h)
3968 {
3969 default:
3970 YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
3971 case 1:
3972 YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
3973 } /* FALLTHROUGH */
3974 case 3:
3975 switch (h)
3976 {
3977 default:
3978 YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
3979 case 1:
3980 YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
3981 } /* FALLTHROUGH */
3982 case 2:
3983 switch (h)
3984 {
3985 default:
3986 YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
3987 case 1:
3988 YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
3989 } /* FALLTHROUGH */
3990 case 1:
3991 switch (h)
3992 {
3993 default:
3994 YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
3995 case 1:
3996 YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
3997 } /* FALLTHROUGH */
3998 }
3999 if (x < 4)
4000 {
4001 cp += x;
4002 cp1 += x;
4003 x = 0;
4004 }
4005 else
4006 {
4007 cp += 4;
4008 cp1 += 4;
4009 x -= 4;
4010 }
4011 pp += 10;
4012 }
4013 if (h <= 2)
4014 break;
4015 h -= 2;
4016 cp += incr;
4017 cp1 += incr;
4018 pp += fromskewLocal;
4019 }
4020 }
4021}
4022
4023/*
4024 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
4025 */
4055{
4056 (void)y;
4057 const tmsize_t fromskewLocal =
4058 (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 1 + 2);
4059 do
4060 {
4061 x = w >> 2;
4062 while (x > 0)
4063 {
4064 int32_t Cb = pp[4];
4065 int32_t Cr = pp[5];
4066
4067 YCbCrtoRGB(cp[0], pp[0]);
4068 YCbCrtoRGB(cp[1], pp[1]);
4069 YCbCrtoRGB(cp[2], pp[2]);
4070 YCbCrtoRGB(cp[3], pp[3]);
4071
4072 cp += 4;
4073 pp += 6;
4074 x--;
4075 }
4076
4077 if ((w & 3) != 0)
4078 {
4079 int32_t Cb = pp[4];
4080 int32_t Cr = pp[5];
4081
4082 switch ((w & 3))
4083 {
4084 case 3:
4085 YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/
4086 case 2:
4087 YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/
4088 case 1:
4089 YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/
4090 case 0:
4091 break;
4092 default:
4093 break;
4094 }
4095
4096 cp += (w & 3);
4097 pp += 6;
4098 }
4099
4100 cp += toskew;
4101 pp += fromskewLocal;
4102 } while (--h);
4103}
4104
4105/*
4106 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
4107 */
4137{
4138 uint32_t *cp2;
4139 const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
4140 (void)y;
4141 const tmsize_t fromskewLocal =
4142 (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 2 + 2);
4143 cp2 = cp + w + toskew;
4144 while (h >= 2)
4145 {
4146 x = w;
4147 while (x >= 2)
4148 {
4149 uint32_t Cb = pp[4];
4150 uint32_t Cr = pp[5];
4151 YCbCrtoRGB(cp[0], pp[0]);
4152 YCbCrtoRGB(cp[1], pp[1]);
4153 YCbCrtoRGB(cp2[0], pp[2]);
4154 YCbCrtoRGB(cp2[1], pp[3]);
4155 cp += 2;
4156 cp2 += 2;
4157 pp += 6;
4158 x -= 2;
4159 }
4160 if (x == 1)
4161 {
4162 uint32_t Cb = pp[4];
4163 uint32_t Cr = pp[5];
4164 YCbCrtoRGB(cp[0], pp[0]);
4165 YCbCrtoRGB(cp2[0], pp[2]);
4166 cp++;
4167 cp2++;
4168 pp += 6;
4169 }
4170 cp += incr;
4171 cp2 += incr;
4172 pp += fromskewLocal;
4173 h -= 2;
4174 }
4175 if (h == 1)
4176 {
4177 x = w;
4178 while (x >= 2)
4179 {
4180 uint32_t Cb = pp[4];
4181 uint32_t Cr = pp[5];
4182 YCbCrtoRGB(cp[0], pp[0]);
4183 YCbCrtoRGB(cp[1], pp[1]);
4184 cp += 2;
4185 cp2 += 2;
4186 pp += 6;
4187 x -= 2;
4188 }
4189 if (x == 1)
4190 {
4191 uint32_t Cb = pp[4];
4192 uint32_t Cr = pp[5];
4193 YCbCrtoRGB(cp[0], pp[0]);
4194 }
4195 }
4196}
4197
4198/*
4199 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
4200 */
4230{
4231 (void)y;
4232 const tmsize_t fromskewLocal =
4233 (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 1 + 2);
4234 do
4235 {
4236 x = w >> 1;
4237 while (x > 0)
4238 {
4239 int32_t Cb = pp[2];
4240 int32_t Cr = pp[3];
4241
4242 YCbCrtoRGB(cp[0], pp[0]);
4243 YCbCrtoRGB(cp[1], pp[1]);
4244
4245 cp += 2;
4246 pp += 4;
4247 x--;
4248 }
4249
4250 if ((w & 1) != 0)
4251 {
4252 int32_t Cb = pp[2];
4253 int32_t Cr = pp[3];
4254
4255 YCbCrtoRGB(cp[0], pp[0]);
4256
4257 cp += 1;
4258 pp += 4;
4259 }
4260
4261 cp += toskew;
4262 pp += fromskewLocal;
4263 } while (--h);
4264}
4265
4266/*
4267 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
4268 */
4298{
4299 uint32_t *cp2;
4300 const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
4301 (void)y;
4302 const tmsize_t fromskewLocal =
4303 (tmsize_t)(fromskew / 1) * (tmsize_t)(1 * 2 + 2);
4304 cp2 = cp + w + toskew;
4305 while (h >= 2)
4306 {
4307 x = w;
4308 do
4309 {
4310 uint32_t Cb = pp[2];
4311 uint32_t Cr = pp[3];
4312 YCbCrtoRGB(cp[0], pp[0]);
4313 YCbCrtoRGB(cp2[0], pp[1]);
4314 cp++;
4315 cp2++;
4316 pp += 4;
4317 } while (--x);
4318 cp += incr;
4319 cp2 += incr;
4320 pp += fromskewLocal;
4321 h -= 2;
4322 }
4323 if (h == 1)
4324 {
4325 x = w;
4326 do
4327 {
4328 uint32_t Cb = pp[2];
4329 uint32_t Cr = pp[3];
4330 YCbCrtoRGB(cp[0], pp[0]);
4331 cp++;
4332 pp += 4;
4333 } while (--x);
4334 }
4335}
4336
4337/*
4338 * 8-bit packed YCbCr samples w/ no subsampling => RGB
4339 */
4369{
4370 (void)y;
4371 const tmsize_t fromskewLocal =
4372 (tmsize_t)(fromskew / 1) * (tmsize_t)(1 * 1 + 2);
4373 do
4374 {
4375 x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
4376 do
4377 {
4378 int32_t Cb = pp[1];
4379 int32_t Cr = pp[2];
4380
4381 YCbCrtoRGB(*cp++, pp[0]);
4382
4383 pp += 3;
4384 } while (--x);
4385 cp += toskew;
4386 pp += fromskewLocal;
4387 } while (--h);
4388}
4389
4390/*
4391 * 8-bit packed YCbCr samples w/ no subsampling => RGB
4392 */
4424{
4425 (void)y;
4426 (void)a;
4427 /* TODO: naming of input vars is still off, change obfuscating declaration
4428 * inside define, or resolve obfuscation */
4429 for (; h > 0; --h)
4430 {
4431 x = w;
4432 do
4433 {
4434 uint32_t dr, dg, db;
4435 TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db);
4436 *cp++ = PACK(dr, dg, db);
4437 } while (--x);
4438 SKEW(r, g, b, fromskew);
4439 cp += toskew;
4440 }
4441}
4442#undef YCbCrtoRGB
4443
4454static int isInRefBlackWhiteRange(float f)
4455{
4456 return (f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF);
4457}
4458
4477{
4478 static const char module[] = "initYCbCrConversion";
4479
4480 float *luma, *refBlackWhite;
4481
4482 if (img->ycbcr == NULL)
4483 {
4485 img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) +
4486 4 * 256 * sizeof(TIFFRGBValue) +
4487 2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t));
4488 if (img->ycbcr == NULL)
4489 {
4490 TIFFErrorExtR(img->tif, module,
4491 "No space for YCbCr->RGB conversion state");
4492 return 0;
4493 }
4494 }
4495
4498 &refBlackWhite);
4499
4500 /* Do some validation to avoid later issues. Detect NaN for now */
4501 /* and also if lumaGreen is zero since we divide by it later */
4502 if (isnan(luma[0]) || isnan(luma[1]) || TIFF_FLOAT_EQ(luma[1], 0.0f) ||
4503 isnan(luma[2]))
4504 {
4505 TIFFErrorExtR(img->tif, module,
4506 "Invalid values for YCbCrCoefficients tag");
4507 return 0;
4508 }
4509
4510 if (!isInRefBlackWhiteRange(refBlackWhite[0]) ||
4511 !isInRefBlackWhiteRange(refBlackWhite[1]) ||
4512 !isInRefBlackWhiteRange(refBlackWhite[2]) ||
4513 !isInRefBlackWhiteRange(refBlackWhite[3]) ||
4514 !isInRefBlackWhiteRange(refBlackWhite[4]) ||
4515 !isInRefBlackWhiteRange(refBlackWhite[5]))
4516 {
4517 TIFFErrorExtR(img->tif, module,
4518 "Invalid values for ReferenceBlackWhite tag");
4519 return 0;
4520 }
4521
4522 if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
4523 return 0;
4524 return 1;
4525}
4526
4546{
4547 static const char module[] = "initCIELabConversion";
4548
4549 float *whitePoint;
4550 float refWhite[3];
4551
4552 TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
4553 if (TIFF_FLOAT_EQ(whitePoint[1], 0.0f))
4554 {
4555 TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag.");
4556 return NULL;
4557 }
4558
4559 if (!img->cielab)
4560 {
4562 img->tif, sizeof(TIFFCIELabToRGB));
4563 if (!img->cielab)
4564 {
4565 TIFFErrorExtR(img->tif, module,
4566 "No space for CIE L*a*b*->RGB conversion state.");
4567 return NULL;
4568 }
4569 }
4570
4571 refWhite[1] = 100.0f;
4572 refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
4573 refWhite[2] =
4574 (1.0f - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];
4575 if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0)
4576 {
4577 TIFFErrorExtR(img->tif, module,
4578 "Failed to initialize CIE L*a*b*->RGB conversion state.");
4579 _TIFFfreeExt(img->tif, img->cielab);
4580 return NULL;
4581 }
4582
4583 if (img->bitspersample == 8)
4584 return putcontig8bitCIELab8;
4585 else if (img->bitspersample == 16)
4586 return putcontig8bitCIELab16;
4587 return NULL;
4588}
4589
4590/*
4591 * Greyscale images with less than 8 bits/sample are handled
4592 * with a table to avoid lots of shifts and masks. The table
4593 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
4594 * pixel values simply by indexing into the table with one
4595 * number.
4596 */
4610static int makebwmap(TIFFRGBAImage *img)
4611{
4612 TIFFRGBValue *Map = img->Map;
4613 int bitspersample = img->bitspersample;
4614 int nsamples = 8 / bitspersample;
4615 int i;
4616 uint32_t *p;
4617
4618 if (nsamples == 0)
4619 nsamples = 1;
4620
4621 img->BWmap = (uint32_t **)_TIFFmallocExt(
4622 img->tif,
4623 (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
4624 ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
4625 if (img->BWmap == NULL)
4626 {
4627 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
4628 "No space for B&W mapping table");
4629 return 0;
4630 }
4631 p = (uint32_t *)(img->BWmap + 256);
4632 for (i = 0; i < 256; i++)
4633 {
4634 TIFFRGBValue c;
4635 img->BWmap[i] = p;
4636 switch (bitspersample)
4637 {
4638#define GREY(x) \
4639 c = Map[x]; \
4640 *p++ = PACK(c, c, c);
4641 case 1:
4642 GREY(i >> 7);
4643 GREY((i >> 6) & 1);
4644 GREY((i >> 5) & 1);
4645 GREY((i >> 4) & 1);
4646 GREY((i >> 3) & 1);
4647 GREY((i >> 2) & 1);
4648 GREY((i >> 1) & 1);
4649 GREY(i & 1);
4650 break;
4651 case 2:
4652 GREY(i >> 6);
4653 GREY((i >> 4) & 3);
4654 GREY((i >> 2) & 3);
4655 GREY(i & 3);
4656 break;
4657 case 4:
4658 GREY(i >> 4);
4659 GREY(i & 0xf);
4660 break;
4661 case 8:
4662 case 16:
4663 GREY(i);
4664 break;
4665 default:
4666 break;
4667 }
4668#undef GREY
4669 }
4670 return 1;
4671}
4672
4673/*
4674 * Construct a mapping table to convert from the range
4675 * of the data samples to [0,255] --for display. This
4676 * process also handles inverting B&W images when needed.
4677 */
4692static int setupMap(TIFFRGBAImage *img)
4693{
4694 int32_t x, range;
4695
4696 range = (int32_t)((1U << img->bitspersample) - 1U);
4697
4698 /* treat 16 bit the same as eight bit */
4699 if (img->bitspersample == 16)
4700 range = 255;
4701
4703 img->tif, (tmsize_t)((size_t)(range + 1) * sizeof(TIFFRGBValue)));
4704 if (img->Map == NULL)
4705 {
4706 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
4707 "No space for photometric conversion table");
4708 return 0;
4709 }
4711 {
4712 for (x = 0; x <= range; x++)
4713 img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range);
4714 }
4715 else
4716 {
4717 for (x = 0; x <= range; x++)
4718 img->Map[x] = (TIFFRGBValue)((x * 255) / range);
4719 }
4720 if (img->bitspersample <= 16 &&
4723 {
4724 /*
4725 * Use photometric mapping table to construct
4726 * unpacking tables for samples <= 8 bits.
4727 */
4728 if (!makebwmap(img))
4729 return 0;
4730 /* no longer need Map, free it */
4731 _TIFFfreeExt(img->tif, img->Map);
4732 img->Map = NULL;
4733 }
4734 return 1;
4735}
4736
4749static int checkcmap(TIFFRGBAImage *img)
4750{
4751 uint16_t *r = img->redcmap;
4752 uint16_t *g = img->greencmap;
4753 uint16_t *b = img->bluecmap;
4754 /* The three copies were made with td_colormap_count entries each in
4755 * TIFFRGBAImageBegin(); that is how many there are to look at. */
4756 uint32_t n = img->tif->tif_dir.td_colormap_count;
4757
4758 while (n-- > 0)
4759 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
4760 return 16;
4761 return 8;
4762}
4763
4775static void cvtcmap(TIFFRGBAImage *img)
4776{
4777 uint16_t *r = img->redcmap;
4778 uint16_t *g = img->greencmap;
4779 uint16_t *b = img->bluecmap;
4780 uint32_t i;
4781
4782 for (i = img->tif->tif_dir.td_colormap_count; i > 0;)
4783 {
4784 --i;
4785#define CVT(x) ((uint16_t)((x) >> 8))
4786 r[i] = CVT(r[i]);
4787 g[i] = CVT(g[i]);
4788 b[i] = CVT(b[i]);
4789#undef CVT
4790 }
4791}
4792
4793/*
4794 * Palette images with <= 8 bits/sample are handled
4795 * with a table to avoid lots of shifts and masks. The table
4796 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
4797 * pixel values simply by indexing into the table with one
4798 * number.
4799 */
4812static int makecmap(TIFFRGBAImage *img)
4813{
4814 int bitspersample = img->bitspersample;
4815 int nsamples = 8 / bitspersample;
4816 uint16_t *r = img->redcmap;
4817 uint16_t *g = img->greencmap;
4818 uint16_t *b = img->bluecmap;
4819 uint32_t *p;
4820 int i;
4821
4822 img->PALmap = (uint32_t **)_TIFFmallocExt(
4823 img->tif,
4824 (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
4825 ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
4826 if (img->PALmap == NULL)
4827 {
4828 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
4829 "No space for Palette mapping table");
4830 return 0;
4831 }
4832 p = (uint32_t *)(img->PALmap + 256);
4833 for (i = 0; i < 256; i++)
4834 {
4835 TIFFRGBValue c;
4836 img->PALmap[i] = p;
4837#define CMAP(x) \
4838 c = (TIFFRGBValue)x; \
4839 *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff);
4840 switch (bitspersample)
4841 {
4842 case 1:
4843 CMAP(i >> 7);
4844 CMAP((i >> 6) & 1);
4845 CMAP((i >> 5) & 1);
4846 CMAP((i >> 4) & 1);
4847 CMAP((i >> 3) & 1);
4848 CMAP((i >> 2) & 1);
4849 CMAP((i >> 1) & 1);
4850 CMAP(i & 1);
4851 break;
4852 case 2:
4853 CMAP(i >> 6);
4854 CMAP((i >> 4) & 3);
4855 CMAP((i >> 2) & 3);
4856 CMAP(i & 3);
4857 break;
4858 case 4:
4859 CMAP(i >> 4);
4860 CMAP(i & 0xf);
4861 break;
4862 case 8:
4863 CMAP(i);
4864 break;
4865 default:
4866 break;
4867 }
4868#undef CMAP
4869 }
4870 return 1;
4871}
4872
4873/*
4874 * Construct any mapping table used
4875 * by the associated put routine.
4876 */
4893static int buildMap(TIFFRGBAImage *img)
4894{
4895 switch (img->photometric)
4896 {
4897 case PHOTOMETRIC_RGB:
4898 case PHOTOMETRIC_YCBCR:
4900 if (img->bitspersample == 8)
4901 break;
4902 /* fall through... */
4905 if (!setupMap(img))
4906 return 0;
4907 break;
4909 /*
4910 * Convert 16-bit colormap to 8-bit (unless it looks
4911 * like an old-style 8-bit colormap).
4912 */
4913 if (checkcmap(img) == 16)
4914 cvtcmap(img);
4915 else
4916 TIFFWarningExtR(img->tif, TIFFFileName(img->tif),
4917 "Assuming 8-bit colormap");
4918 /*
4919 * Use mapping table and colormap to construct
4920 * unpacking tables for samples < 8 bits.
4921 */
4922 if (img->bitspersample <= 8 && !makecmap(img))
4923 return 0;
4924 break;
4925 default:
4926 break;
4927 }
4928 return 1;
4929}
4930
4931/*
4932 * Select the appropriate conversion routine for packed data.
4933 */
4950{
4951 img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
4952 img->put.contig = NULL;
4953 switch (img->photometric)
4954 {
4955 case PHOTOMETRIC_RGB:
4956 switch (img->bitspersample)
4957 {
4958 case 8:
4959 if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
4960 img->samplesperpixel >= 4)
4962 else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
4963 img->samplesperpixel >= 4)
4964 {
4965 if (BuildMapUaToAa(img))
4967 }
4968 else if (img->samplesperpixel >= 3)
4970 break;
4971 case 16:
4972 if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
4973 img->samplesperpixel >= 4)
4974 {
4975 if (BuildMapBitdepth16To8(img))
4977 }
4978 else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
4979 img->samplesperpixel >= 4)
4980 {
4981 if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
4983 }
4984 else if (img->samplesperpixel >= 3)
4985 {
4986 if (BuildMapBitdepth16To8(img))
4988 }
4989 break;
4990 default:
4991 break;
4992 }
4993 break;
4995 if (img->samplesperpixel >= 4 && buildMap(img))
4996 {
4997 if (img->bitspersample == 8)
4998 {
4999 if (!img->Map)
5001 else
5003 }
5004 }
5005 break;
5007 if (buildMap(img))
5008 {
5009 switch (img->bitspersample)
5010 {
5011 case 8:
5012 img->put.contig = put8bitcmaptile;
5013 break;
5014 case 4:
5015 img->put.contig = put4bitcmaptile;
5016 break;
5017 case 2:
5018 img->put.contig = put2bitcmaptile;
5019 break;
5020 case 1:
5021 img->put.contig = put1bitcmaptile;
5022 break;
5023 default:
5024 break;
5025 }
5026 }
5027 break;
5030 if (buildMap(img))
5031 {
5032 switch (img->bitspersample)
5033 {
5034 case 16:
5035 img->put.contig = put16bitbwtile;
5036 break;
5037 case 8:
5038 if (img->alpha && img->samplesperpixel == 2)
5039 img->put.contig = putagreytile;
5040 else
5041 img->put.contig = putgreytile;
5042 break;
5043 case 4:
5044 img->put.contig = put4bitbwtile;
5045 break;
5046 case 2:
5047 img->put.contig = put2bitbwtile;
5048 break;
5049 case 1:
5050 img->put.contig = put1bitbwtile;
5051 break;
5052 default:
5053 break;
5054 }
5055 }
5056 break;
5057 case PHOTOMETRIC_YCBCR:
5058 if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
5059 {
5060 if (initYCbCrConversion(img) != 0)
5061 {
5062 /*
5063 * The 6.0 spec says that subsampling must be
5064 * one of 1, 2, or 4, and that vertical subsampling
5065 * must always be <= horizontal subsampling; so
5066 * there are only a few possibilities and we just
5067 * enumerate the cases.
5068 * Joris: added support for the [1,2] case, nonetheless, to
5069 * accommodate some OJPEG files
5070 */
5071 uint16_t SubsamplingHor;
5072 uint16_t SubsamplingVer;
5074 &SubsamplingHor, &SubsamplingVer);
5075 /* Validate that the image dimensions are compatible with
5076 the subsampling block. All putcontig8bitYCbCrXYtile routines
5077 assume width >= X and height >= Y. */
5078 if (img->width < SubsamplingHor ||
5079 img->height < SubsamplingVer)
5080 {
5081 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
5082 "YCbCr subsampling (%u,%u) incompatible "
5083 "with image size %ux%u",
5084 SubsamplingHor, SubsamplingVer,
5085 img->width, img->height);
5086 return 0;
5087 }
5088 switch ((SubsamplingHor << 4) | SubsamplingVer)
5089 {
5090 case 0x44:
5092 break;
5093 case 0x42:
5095 break;
5096 case 0x41:
5098 break;
5099 case 0x22:
5101 break;
5102 case 0x21:
5104 break;
5105 case 0x12:
5107 break;
5108 case 0x11:
5110 break;
5111 default:
5112 break;
5113 }
5114 }
5115 }
5116 break;
5117 case PHOTOMETRIC_CIELAB:
5118 if (img->samplesperpixel == 3 && buildMap(img))
5119 {
5120 if (img->bitspersample == 8 || img->bitspersample == 16)
5121 img->put.contig = initCIELabConversion(img);
5122 break;
5123 }
5124 break;
5125 default:
5126 break;
5127 }
5128 return ((img->get != NULL) && (img->put.contig != NULL));
5129}
5130
5131/*
5132 * Select the appropriate conversion routine for unpacked data.
5133 *
5134 * NB: we assume that unpacked single channel data is directed
5135 * to the "packed routines.
5136 */
5152{
5154 img->put.separate = NULL;
5155 switch (img->photometric)
5156 {
5159 /* greyscale images processed pretty much as RGB by gtTileSeparate
5160 */
5161 case PHOTOMETRIC_RGB:
5162 switch (img->bitspersample)
5163 {
5164 case 8:
5165 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
5167 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
5168 {
5169 if (BuildMapUaToAa(img))
5171 }
5172 else
5174 break;
5175 case 16:
5176 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
5177 {
5178 if (BuildMapBitdepth16To8(img))
5180 }
5181 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
5182 {
5183 if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
5185 }
5186 else
5187 {
5188 if (BuildMapBitdepth16To8(img))
5190 }
5191 break;
5192 default:
5193 break;
5194 }
5195 break;
5197 if (img->bitspersample == 8 && img->samplesperpixel == 4)
5198 {
5199 /* Not alpha, but seems like the only way to get 4th band */
5200 img->alpha = 1;
5202 }
5203 break;
5204 case PHOTOMETRIC_YCBCR:
5205 if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
5206 {
5207 if (initYCbCrConversion(img) != 0)
5208 {
5209 uint16_t hs, vs;
5211 &hs, &vs);
5212 switch ((hs << 4) | vs)
5213 {
5214 case 0x11:
5216 break;
5217 /* TODO: add other cases here */
5218 default:
5219 break;
5220 }
5221 }
5222 }
5223 break;
5224 default:
5225 break;
5226 }
5227 return ((img->get != NULL) && (img->put.separate != NULL));
5228}
5229
5243{
5244 static const char module[] = "BuildMapUaToAa";
5245 uint8_t *m;
5246 uint16_t na, nv;
5247 assert(img->UaToAa == NULL);
5248 img->UaToAa = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
5249 if (img->UaToAa == NULL)
5250 {
5251 TIFFErrorExtR(img->tif, module, "Out of memory");
5252 return 0;
5253 }
5254 m = img->UaToAa;
5255 for (na = 0; na < 256; na++)
5256 {
5257 for (nv = 0; nv < 256; nv++)
5258 *m++ = (uint8_t)((nv * na + 127) / 255);
5259 }
5260 return 1;
5261}
5262
5276{
5277 static const char module[] = "BuildMapBitdepth16To8";
5278 uint8_t *m;
5279 uint32_t n;
5280 assert(img->Bitdepth16To8 == NULL);
5281 img->Bitdepth16To8 = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
5282 if (img->Bitdepth16To8 == NULL)
5283 {
5284 TIFFErrorExtR(img->tif, module, "Out of memory");
5285 return 0;
5286 }
5287 m = img->Bitdepth16To8;
5288 for (n = 0; n < 65536; n++)
5289 *m++ = (uint8_t)((n + 128) / 257);
5290 return 1;
5291}
5292
5293/*
5294 * Read a whole strip off data from the file, and convert to RGBA form.
5295 * If this is the last strip, then it will only contain the portion of
5296 * the strip that is actually within the image space. The result is
5297 * organized in bottom to top form.
5298 */
5299
5300int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster)
5301
5302{
5303 return TIFFReadRGBAStripExt(tif, row, raster, 0);
5304}
5305
5306int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster,
5307 int stop_on_error)
5308
5309{
5310 char emsg[EMSG_BUF_SIZE] = "";
5311 TIFFRGBAImage img;
5312 int ok;
5313 uint32_t rowsperstrip, rows_to_read;
5314
5315 if (TIFFIsTiled(tif))
5316 {
5317 TIFFErrorExtR(tif, TIFFFileName(tif),
5318 "Can't use TIFFReadRGBAStrip() with tiled file.");
5319 return 0;
5320 }
5321
5322 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
5323
5324 if (rowsperstrip == 0)
5325 {
5326 TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
5327 return 0;
5328 }
5329
5330 if ((row % rowsperstrip) != 0)
5331 {
5333 tif, TIFFFileName(tif),
5334 "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
5335 return 0;
5336 }
5337
5338 if (TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
5339 {
5340 if (row >= img.height)
5341 {
5342 TIFFErrorExtR(tif, TIFFFileName(tif),
5343 "Invalid row passed to TIFFReadRGBAStrip().");
5344 TIFFRGBAImageEnd(&img);
5345 return 0;
5346 }
5347
5348 img.row_offset = (int)row;
5349 img.col_offset = 0;
5350
5351 if (row + rowsperstrip > img.height)
5352 rows_to_read = img.height - row;
5353 else
5354 rows_to_read = rowsperstrip;
5355
5356 ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read);
5357
5358 TIFFRGBAImageEnd(&img);
5359 }
5360 else
5361 {
5362 TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
5363 ok = 0;
5364 }
5365
5366 return ok;
5367}
5368
5369/*
5370 * Read a whole tile off data from the file, and convert to RGBA form.
5371 * The returned RGBA data is organized from bottom to top of tile,
5372 * and may include zeroed areas if the tile extends off the image.
5373 */
5374
5375int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster)
5376
5377{
5378 return TIFFReadRGBATileExt(tif, col, row, raster, 0);
5379}
5380
5381int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster,
5382 int stop_on_error)
5383{
5384 char emsg[EMSG_BUF_SIZE] = "";
5385 TIFFRGBAImage img;
5386 int ok;
5387 uint32_t tile_xsize, tile_ysize;
5388 uint32_t read_xsize, read_ysize;
5389 uint32_t i_row;
5390
5391 /*
5392 * Verify that our request is legal - on a tile file, and on a
5393 * tile boundary.
5394 */
5395
5396 if (!TIFFIsTiled(tif))
5397 {
5398 TIFFErrorExtR(tif, TIFFFileName(tif),
5399 "Can't use TIFFReadRGBATile() with striped file.");
5400 return 0;
5401 }
5402
5403 TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
5404 TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
5405 if (tile_xsize == 0 || tile_ysize == 0)
5406 {
5407 TIFFErrorExtR(tif, TIFFFileName(tif),
5408 "tile_xsize or tile_ysize is zero");
5409 return 0;
5410 }
5411
5412 if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)
5413 {
5414 TIFFErrorExtR(tif, TIFFFileName(tif),
5415 "Row/col passed to TIFFReadRGBATile() must be top"
5416 "left corner of a tile.");
5417 return 0;
5418 }
5419
5420 /*
5421 * Setup the RGBA reader.
5422 */
5423
5424 if (!TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
5425 {
5426 TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
5427 return 0;
5428 }
5429
5430 if (col >= img.width || row >= img.height)
5431 {
5432 TIFFErrorExtR(tif, TIFFFileName(tif),
5433 "Invalid row/col passed to TIFFReadRGBATile().");
5434 TIFFRGBAImageEnd(&img);
5435 return 0;
5436 }
5437
5438 /*
5439 * The TIFFRGBAImageGet() function doesn't allow us to get off the
5440 * edge of the image, even to fill an otherwise valid tile. So we
5441 * figure out how much we can read, and fix up the tile buffer to
5442 * a full tile configuration afterwards.
5443 */
5444
5445 if (row + tile_ysize > img.height)
5446 read_ysize = img.height - row;
5447 else
5448 read_ysize = tile_ysize;
5449
5450 if (col + tile_xsize > img.width)
5451 read_xsize = img.width - col;
5452 else
5453 read_xsize = tile_xsize;
5454
5455 /*
5456 * Read the chunk of imagery.
5457 */
5458
5459 img.row_offset = (int)row;
5460 img.col_offset = (int)col;
5461
5462 ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize);
5463
5464 TIFFRGBAImageEnd(&img);
5465
5466 /*
5467 * If our read was incomplete we will need to fix up the tile by
5468 * shifting the data around as if a full tile of data is being returned.
5469 *
5470 * This is all the more complicated because the image is organized in
5471 * bottom to top format.
5472 */
5473
5474 if (read_xsize == tile_xsize && read_ysize == tile_ysize)
5475 return ok;
5476
5477 for (i_row = 0; i_row < read_ysize; i_row++)
5478 {
5479 memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize,
5480 raster + (size_t)(read_ysize - i_row - 1) * read_xsize,
5481 read_xsize * sizeof(uint32_t));
5483 raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize + read_xsize,
5484 0,
5485 (tmsize_t)(sizeof(uint32_t) * (size_t)(tile_xsize - read_xsize)));
5486 }
5487
5488 for (i_row = read_ysize; i_row < tile_ysize; i_row++)
5489 {
5490 _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0,
5491 (tmsize_t)(sizeof(uint32_t) * (size_t)tile_xsize));
5492 }
5493
5494 return ok;
5495}
5496
tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
Check and multiply two signed-size operands.
Definition tif_aux.c:91
void TIFFCIELab16ToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a, int32_t b, float *X, float *Y, float *Z)
Convert 16-bit TIFF CIE L*a*b* samples to XYZ.
Definition tif_color.c:68
tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip, void **buf, tmsize_t bufsizetoalloc, tmsize_t size_to_read)
Decode after deferred allocation of caller-owned output storage.
Definition tif_read.c:769
tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf, tmsize_t bufsizetoalloc, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Decode after deferred allocation of caller-owned output storage.
Definition tif_read.c:1350
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
#define FLIP_VERTICALLY
Select reversed raster row order.
static int gtTileContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t)
Read contiguous tiles into a clipped RGBA raster.
static int checkColOffsetByteAligned(TIFFRGBAImage *img, const char *module, uint64_t bitoffset)
Reject a packed-sample offset between byte boundaries.
static void putRGBAAcontig8bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBAAcontig8bittile samples into RGBA words.
static void put1bitcmaptile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 1bitcmaptile samples into RGBA words.
#define EMSG_BUF_SIZE
Reserve 1024 bytes for RGBA rejection messages.
static void putcontig8bitYCbCr12tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitYCbCr12tile samples into RGBA words.
static int isInRefBlackWhiteRange(float f)
Check a reference endpoint against conversion integer bounds.
#define SKEW4(r, g, b, a, skew)
Advance four plane pointers by a source skew.
static void putRGBUAcontig16bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBUAcontig16bittile samples into RGBA words.
static void put16bitbwtile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 16bitbwtile samples into RGBA words.
static void putgreytile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack greytile samples into RGBA words.
static const TIFFDisplay display_sRGB
Describe the fixed display transform used for Lab output.
static int gtStripSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t)
Read separate-plane strips into a clipped RGBA raster.
#define UNROLL8(w, op1, op2)
Traverse complete eight-pixel groups and their remainder.
static int gtStripContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t)
Read contiguous strips into a clipped RGBA raster.
static int setupMap(TIFFRGBAImage *img)
Build sample scaling and grayscale unpacking maps.
#define GREY(x)
Append a grayscale-map entry to the expansion table.
static char const *const orientationStrings[]
Map checked orientation numbers to diagnostic labels.
static void put8bitcmaptile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 8bitcmaptile samples into RGBA words.
static void put1bitbwtile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 1bitbwtile samples into RGBA words.
static int setorientation(TIFFRGBAImage *img)
Choose horizontal and vertical raster flips.
#define YCbCrtoRGB(dst, Y)
Convert a local YCbCr group and pack an opaque output word.
static int buildMap(TIFFRGBAImage *img)
Build the lookup maps needed by a selected photometric case.
static void putcontig8bitYCbCr44tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitYCbCr44tile samples into RGBA words.
static void putRGBseparate16bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack RGBseparate16bittile samples into RGBA words.
static int PickSeparateCase(TIFFRGBAImage *)
Select separate-plane get/put callbacks for the conversion.
static void putcontig8bitYCbCr21tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitYCbCr21tile samples into RGBA words.
static void putcontig8bitYCbCr42tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitYCbCr42tile samples into RGBA words.
#define NOP
Supply an empty loop operation.
static void putcontig8bitYCbCr22tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitYCbCr22tile samples into RGBA words.
static uint16_t checkedOrientation(uint16_t orientation)
Validate a TIFF orientation number.
static int PickContigCase(TIFFRGBAImage *)
Select contiguous get/put callbacks for the conversion.
#define PACK4(r, g, b, a)
Pack RGB and alpha bytes.
static void putRGBcontig8bitCMYKtile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBcontig8bitCMYKtile samples into RGBA words.
static void put2bitcmaptile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 2bitcmaptile samples into RGBA words.
#define UNROLL4(w, op1, op2)
Traverse complete four-pixel groups and their remainder.
static int gtTileSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t)
Read separate-plane tiles into a clipped RGBA raster.
static void putcontig8bitYCbCr11tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitYCbCr11tile samples into RGBA words.
static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img)
Prepare Lab state and select an 8- or 16-bit packer.
static void putRGBcontig8bitCMYKMaptile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBcontig8bitCMYKMaptile samples into RGBA words.
#define PACK(r, g, b)
Pack RGB bytes with opaque alpha.
#define DECLAREContigPutFunc(name)
Declare the concrete contiguous packer signature.
static void putcontig8bitCIELab8(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitCIELab8 samples into RGBA words.
static int isCCITTCompression(TIFF *tif)
Recognise CCITT fax compression schemes.
static void putRGBcontig16bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBcontig16bittile samples into RGBA words.
static void putCMYKseparate8bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack CMYKseparate8bittile samples into RGBA words.
#define SKEW(r, g, b, skew)
Advance three plane pointers by a source skew.
static int calcCopyToRasterParameter(TIFFRGBAImage *img, uint32_t *raster, uint32_t rwidth, uint32_t rheight, uint32_t *pwcpy, uint32_t *phcpy, int *pflip, uint32_t **pp_raster_start, int32_t *ptoskew, uint32_t *py, bool isTiled, uint32_t tilewidth, const char *funcName)
Compute clipped raster placement and signed row steps.
static void putRGBAAseparate16bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack RGBAAseparate16bittile samples into RGBA words.
static int makebwmap(TIFFRGBAImage *img)
Build byte-indexed packed grayscale expansion tables.
static int initYCbCrConversion(TIFFRGBAImage *img)
Allocate and initialise RGBA YCbCr contribution tables.
static void putRGBAAseparate8bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack RGBAAseparate8bittile samples into RGBA words.
static void putagreytile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack agreytile samples into RGBA words.
static int BuildMapUaToAa(TIFFRGBAImage *img)
Build the unassociated-to-associated eight-bit channel map.
static void cvtcmap(TIFFRGBAImage *img)
Reduce all copied palette entries to their high byte.
static void putRGBUAseparate8bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack RGBUAseparate8bittile samples into RGBA words.
#define UNROLL2(w, op1, op2)
Traverse complete two-pixel groups and their remainder.
static int checkcmap(TIFFRGBAImage *img)
Infer whether copied palette entries use eight or sixteen bits.
static void putcontig8bitYCbCr41tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitYCbCr41tile samples into RGBA words.
static void putRGBcontig8bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBcontig8bittile samples into RGBA words.
#define FLIP_HORIZONTALLY
Select reversal within copied raster rows.
static void put4bitbwtile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 4bitbwtile samples into RGBA words.
static int makecmap(TIFFRGBAImage *img)
Build byte-indexed packed palette expansion tables.
#define DECLARESepPutFunc(name)
Declare the concrete separate-plane packer signature.
#define CMAP(x)
Append a palette-map entry to the expansion table.
static void putcontig8bitCIELab16(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack contig8bitCIELab16 samples into RGBA words.
static void putseparate8bitYCbCr11tile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack separate8bitYCbCr11tile samples into RGBA words.
#define CVT(x)
Reduce a palette entry to its high byte.
static void putRGBUAcontig8bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBUAcontig8bittile samples into RGBA words.
static void putRGBAAcontig16bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack RGBAAcontig16bittile samples into RGBA words.
static int BuildMapBitdepth16To8(TIFFRGBAImage *img)
Build rounded sixteen-to-eight-bit sample reduction.
static void put4bitcmaptile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 4bitcmaptile samples into RGBA words.
static void putRGBseparate8bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack RGBseparate8bittile samples into RGBA words.
static void put2bitbwtile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Pack 2bitbwtile samples into RGBA words.
static void putRGBUAseparate16bittile(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Pack RGBUAseparate16bittile samples into RGBA words.
static const char photoTag[]
Name the photometric tag in diagnostic messages.
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define isUpSampled(tif)
Test whether the codec reports upsampled output.
Definition tiffiop.h:869
#define TIFFroundup_32(x, y)
Round a 32-bit value up to a multiple using the guarded quotient.
Definition tiffiop.h:1102
#define TIFF_FLOAT_EQ(x, y)
Test whether a float difference is not greater than zero in magnitude.
Definition tiffiop.h:1256
#define TIFFmin(A, B)
Select the first operand if it is smaller, otherwise the second.
Definition tiffiop.h:1226
#define TIFFhowmany_32(x, y)
Compute a guarded 32-bit ceiling quotient.
Definition tiffiop.h:1056
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW void TIFFCIELabToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a, int32_t b, float *X, float *Y, float *Z)
Convert 8-bit TIFF CIE L*a*b* samples to XYZ.
Definition tif_color.c:50
void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *ycbcr, uint32_t Y, int32_t Cb, int32_t Cr, uint32_t *r, uint32_t *g, uint32_t *b)
Convert an 8-bit YCbCr sample triplet to clipped RGB.
Definition tif_color.c:289
int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *ycbcr, float *luma, float *refBlackWhite)
Initialise caller-allocated 8-bit YCbCr conversion tables.
Definition tif_color.c:354
int TIFFCIELabToRGBInit(TIFFCIELabToRGB *cielab, const TIFFDisplay *display, float *refWhite)
Initialise caller-owned CIE Lab conversion tables and reference white.
Definition tif_color.c:162
void TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z, uint32_t *r, uint32_t *g, uint32_t *b)
Convert XYZ tristimulus values to display RGB channels.
Definition tif_color.c:112
unsigned char TIFFRGBValue
Represent an 8-bit RGB channel value.
Definition tiffio.h:276
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
Definition tif_warning.c:85
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:307
tmsize_t TIFFStripSize(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
Definition tif_strip.c:244
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:383
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested strip height.
Definition tif_strip.c:182
tmsize_t TIFFTileRowSize(TIFF *tif)
Compute the packed byte count of one tile row.
Definition tif_tile.c:276
uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)
Compute a strip index from a row and optional sample plane.
Definition tif_strip.c:41
tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
Read decoded samples from one strip.
Definition tif_read.c:708
tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Decode the tile containing image coordinates.
Definition tif_read.c:1265
const char * TIFFFileName(TIFF *tif)
Return the handle's diagnostic filename.
Definition tif_open.c:835
int TIFFIsTiled(TIFF *tif)
Test whether the current image uses tiles.
Definition tif_open.c:896
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
#define SGILOGDATAFMT_8BIT
8-bit RGB monitor values.
Definition tiff.h:1625
#define TIFFTAG_SGILOGDATAFMT
Pseudo-tag: SGILog user data format.
Definition tiff.h:1617
#define TIFFTAG_JPEGCOLORMODE
Pseudo-tag: Auto RGB<=>YCbCr convert?
Definition tiff.h:1547
#define JPEGCOLORMODE_RGB
Do auto conversion.
Definition tiff.h:1551
void(* tileSeparateRoutine)(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
Convert a decoded separate-plane region through a caller-selectable packer.
Definition tiffio.h:496
void(* tileContigRoutine)(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, unsigned char *pp)
Convert a decoded contiguous region through a caller-selectable packer.
Definition tiffio.h:453
int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight, uint32_t *raster, int stop)
Read the current image into a packed RGBA raster.
int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster)
Read and convert one whole strip to packed RGBA.
int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster)
Read and convert one whole tile to packed RGBA.
int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop, char emsg[1024])
Initialise an RGBA reader and select conversion methods.
void TIFFRGBAImageEnd(TIFFRGBAImage *img)
Release RGBA conversion tables and copied colormaps.
int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster, int stop_on_error)
Read and convert one whole strip to packed RGBA.
int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster, int stop_on_error)
Read and convert one whole tile to packed RGBA.
int TIFFRGBAImageOK(TIFF *tif, char emsg[1024])
Check preliminary RGBA compatibility of the current directory.
int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight, uint32_t *raster, int orientation, int stop)
Read the current image into a packed RGBA raster.
int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t rwidth, uint32_t rheight)
Read and convert a selected image area through installed callbacks.
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Set a tag value in the current in-memory directory.
Definition tif_dir.c:1627
int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag,...)
Query a present tag or a supported default.
Definition tif_aux.c:591
int TIFFGetField(TIFF *tif, uint32_t tag,...)
Query a present tag in the current directory.
Definition tif_dir.c:2099
#define TIFFTAG_BITSPERSAMPLE
TIFF tag: Bits per channel (sample).
Definition tiff.h:298
#define ORIENTATION_BOTRIGHT
Row 0 bottom, col 0 rhs.
Definition tiff.h:444
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
#define ORIENTATION_LEFTTOP
Row 0 lhs, col 0 top.
Definition tiff.h:448
#define ORIENTATION_BOTLEFT
Row 0 bottom, col 0 lhs.
Definition tiff.h:446
#define TIFFTAG_WHITEPOINT
TIFF tag: Image white point.
Definition tiff.h:556
#define EXTRASAMPLE_UNASSALPHA
Unassociated alpha data (introduced in TIFF 6.0).
Definition tiff.h:606
#define COMPRESSION_CCITTFAX3
CCITT Group 3 fax encoding.
Definition tiff.h:306
#define COMPRESSION_CCITTRLE
CCITT modified Huffman RLE.
Definition tiff.h:304
#define EXTRASAMPLE_ASSOCALPHA
Associated alpha data (introduced in TIFF 6.0).
Definition tiff.h:604
#define ORIENTATION_TOPLEFT
Row 0 top, col 0 lhs.
Definition tiff.h:440
#define ORIENTATION_RIGHTBOT
Row 0 rhs, col 0 bottom.
Definition tiff.h:452
#define ORIENTATION_RIGHTTOP
Row 0 rhs, col 0 top.
Definition tiff.h:450
#define TIFFTAG_SAMPLESPERPIXEL
TIFF tag: Samples per pixel.
Definition tiff.h:456
#define PHOTOMETRIC_LOGL
CIE Log2(L).
Definition tiff.h:406
#define TIFFTAG_INKSET
TIFF tag: Inks in separated image (introduced in TIFF 6.0).
Definition tiff.h:586
#define COMPRESSION_SGILOG
SGI Log Luminance RLE.
Definition tiff.h:358
#define COMPRESSION_CCITTRLEW
CCITT modified Huffman RLE with word alignment.
Definition tiff.h:329
#define TIFFTAG_COLORMAP
TIFF tag: RGB map for palette image.
Definition tiff.h:560
#define PHOTOMETRIC_CIELAB
1976 CIE L*a*b* (introduced in TIFF 6.0).
Definition tiff.h:398
#define EXTRASAMPLE_UNSPECIFIED
Unspecified data (introduced in TIFF 6.0).
Definition tiff.h:602
#define TIFFTAG_PHOTOMETRIC
TIFF tag: Photometric interpretation.
Definition tiff.h:382
#define TIFFTAG_TILELENGTH
TIFF tag: Tile height in pixels (introduced in TIFF 6.0).
Definition tiff.h:566
#define PHOTOMETRIC_PALETTE
Color map indexed.
Definition tiff.h:390
#define TIFFTAG_EXTRASAMPLES
TIFF tag: Info about extra samples (introduced in TIFF 6.0).
Definition tiff.h:600
#define TIFFTAG_IMAGEWIDTH
TIFF tag: Image width in pixels.
Definition tiff.h:294
#define PHOTOMETRIC_SEPARATED
Color separations (introduced in TIFF 6.0).
Definition tiff.h:394
#define TIFFTAG_YCBCRCOEFFICIENTS
TIFF tag: RGB -> YCbCr transform (introduced in TIFF 6.0).
Definition tiff.h:727
#define TIFFTAG_ORIENTATION
TIFF tag: Image orientation (obsoleted by TIFF 5.0).
Definition tiff.h:438
#define PHOTOMETRIC_LOGLUV
CIE Log2(L) (u',v').
Definition tiff.h:408
#define SAMPLEFORMAT_IEEEFP
IEEE floating point data (introduced in TIFF 6.0).
Definition tiff.h:614
#define PHOTOMETRIC_YCBCR
CCIR 601 (introduced in TIFF 6.0).
Definition tiff.h:396
#define ORIENTATION_TOPRIGHT
Row 0 top, col 0 rhs.
Definition tiff.h:442
#define INKSET_CMYK
Cyan-magenta-yellow-black color (introduced in TIFF 6.0).
Definition tiff.h:588
#define PHOTOMETRIC_MINISWHITE
Min value is white.
Definition tiff.h:384
#define TIFFTAG_YCBCRSUBSAMPLING
TIFF tag: YCbCr subsampling factors (introduced in TIFF 6.0).
Definition tiff.h:729
#define TIFFTAG_ROWSPERSTRIP
TIFF tag: Rows per strip of data.
Definition tiff.h:458
#define ORIENTATION_LEFTBOT
Row 0 lhs, col 0 bottom.
Definition tiff.h:454
#define COMPRESSION_SGILOG24
SGI Log 24-bit packed.
Definition tiff.h:360
#define TIFFTAG_REFERENCEBLACKWHITE
TIFF tag: Colorimetry info (introduced in TIFF 6.0).
Definition tiff.h:737
#define TIFFTAG_TILEWIDTH
TIFF tag: Tile width in pixels (introduced in TIFF 6.0).
Definition tiff.h:564
#define TIFFTAG_IMAGELENGTH
TIFF tag: Image height in pixels.
Definition tiff.h:296
#define TIFFTAG_COMPRESSION
TIFF tag: Data compression technique.
Definition tiff.h:300
#define PHOTOMETRIC_MINISBLACK
Min value is black.
Definition tiff.h:386
#define PHOTOMETRIC_RGB
RGB color model.
Definition tiff.h:388
#define TIFFTAG_PLANARCONFIG
TIFF tag: Storage organization.
Definition tiff.h:470
#define COMPRESSION_CCITTFAX4
CCITT Group 4 fax encoding.
Definition tiff.h:310
#define COMPRESSION_JPEG
JPEG DCT compression (introduced after TIFF 6.0).
Definition tiff.h:318
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
#define TIFF_TMSIZE_T_MAX
Define the largest positive size representable by tmsize_t.
Definition tiffio.h:91
Hold CIE L*a*b*-to-RGB conversion parameters and tables.
Definition tiffio.h:361
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint16_t td_compression
COMPRESSION_* codec identifier.
Definition tif_dir.h:172
uint16_t td_extrasamples
Number of extra samples described by td_sampleinfo.
Definition tif_dir.h:232
uint16_t td_bitspersample
Bits in each sample.
Definition tif_dir.h:168
uint32_t td_colormap_count
Number of entries in each ColorMap plane that the directory uses, zero when none is installed.
Definition tif_dir.h:223
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
uint16_t td_sampleformat
SAMPLEFORMAT_* interpretation of samples.
Definition tif_dir.h:170
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
Describe the matrix, luminance, and gamma of an RGB display.
Definition tiffio.h:287
Hold lookup-table pointers for 8-bit YCbCr-to-RGB conversion.
Definition tiffio.h:334
Hold the state and selected callbacks for an RGBA image reader.
Definition tiffio.h:517
uint32_t ** PALmap
Reader-owned palette expansion tables, or NULL.
Definition tiffio.h:599
int col_offset
Nonnegative starting source column, measured in pixels.
Definition tiffio.h:616
uint16_t req_orientation
Requested output raster orientation.
Definition tiffio.h:541
uint16_t * bluecmap
Reader-owned blue palette copy, or NULL.
Definition tiffio.h:549
uint16_t photometric
Source PhotometricInterpretation tag value.
Definition tiffio.h:543
TIFF * tif
Borrowed TIFF handle; must outlive this reader state.
Definition tiffio.h:519
tileContigRoutine contig
Put callback for contiguous source samples.
Definition tiffio.h:589
uint16_t * greencmap
Reader-owned green palette copy, or NULL.
Definition tiffio.h:547
uint16_t samplesperpixel
Source samples per pixel.
Definition tiffio.h:537
uint8_t * UaToAa
Reader-owned unassociated-to-associated alpha lookup table, or NULL.
Definition tiffio.h:609
union _TIFFRGBAImage::@3 put
Select the RGBA packer signature matching the image organisation.
TIFFCIELabToRGB * cielab
Reader-owned CIE L*a*b* conversion state, or NULL.
Definition tiffio.h:603
uint16_t bitspersample
Source bits per sample.
Definition tiffio.h:535
int alpha
ExtraSamples alpha interpretation, or zero when none is selected.
Definition tiffio.h:529
uint32_t height
Source image height in pixels.
Definition tiffio.h:533
uint16_t orientation
Source Orientation tag value.
Definition tiffio.h:539
uint16_t * redcmap
Reader-owned red palette copy, or NULL.
Definition tiffio.h:545
uint32_t width
Source image width in pixels.
Definition tiffio.h:531
void(* any)(TIFFRGBAImage *img)
Reserve a generic storage view of the selected packer.
Definition tiffio.h:587
uint8_t * Bitdepth16To8
Reader-owned 16-bit-to-8-bit sample lookup table, or NULL.
Definition tiffio.h:611
uint32_t ** BWmap
Reader-owned black/white expansion tables, or NULL.
Definition tiffio.h:597
TIFFRGBValue * Map
Reader-owned sample mapping table, or NULL.
Definition tiffio.h:595
int isContig
Nonzero for contiguous samples; zero for separate sample planes.
Definition tiffio.h:525
int row_offset
Nonnegative starting source row, measured in pixels.
Definition tiffio.h:614
TIFFYCbCrToRGB * ycbcr
Reader-owned YCbCr conversion block, or NULL.
Definition tiffio.h:601
int stoponerr
Nonzero to stop reading when an error is encountered.
Definition tiffio.h:521
int(* get)(TIFFRGBAImage *img, uint32_t *raster, uint32_t rwidth, uint32_t rheight)
Read and pack a raster through the selected RGBA state.
Definition tiffio.h:570
tileSeparateRoutine separate
Put callback for separate source planes.
Definition tiffio.h:591
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
int tif_decodestatus
Nonzero unless the selected scheme is a known but unconfigured codec; not a setup result.
Definition tiffiop.h:624
Declare private library state, callbacks, and implementation helpers.