Coverage Report

Created: 2026-06-30 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/libtiff/tif_getimage.c
Line
Count
Source
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
 */
30
#include "tiffiop.h"
31
#include <limits.h>
32
#include <math.h>
33
#include <stdio.h>
34
35
static int gtTileContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
36
static int gtTileSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
37
static int gtStripContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
38
static int gtStripSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
39
static int PickContigCase(TIFFRGBAImage *);
40
static int PickSeparateCase(TIFFRGBAImage *);
41
42
static int BuildMapUaToAa(TIFFRGBAImage *img);
43
static int BuildMapBitdepth16To8(TIFFRGBAImage *img);
44
45
static const char photoTag[] = "PhotometricInterpretation";
46
47
/*
48
 * Helper constants used in Orientation tag handling
49
 */
50
171k
#define FLIP_VERTICALLY 0x01
51
26.0k
#define FLIP_HORIZONTALLY 0x02
52
53
2.12k
#define EMSG_BUF_SIZE 1024
54
55
/*
56
 * Color conversion constants. We will define display types here.
57
 */
58
59
static const TIFFDisplay display_sRGB = {
60
    {/* XYZ -> luminance matrix */
61
     {3.2410f, -1.5374f, -0.4986f},
62
     {-0.9692f, 1.8760f, 0.0416f},
63
     {0.0556f, -0.2040f, 1.0570f}},
64
    100.0f,
65
    100.0f,
66
    100.0f, /* Light o/p for reference white */
67
    255,
68
    255,
69
    255, /* Pixel values for ref. white */
70
    1.0f,
71
    1.0f,
72
    1.0f, /* Residual light o/p for black pixel */
73
    2.4f,
74
    2.4f,
75
    2.4f, /* Gamma values for the three guns */
76
};
77
78
/*
79
 * Check the image to see if TIFFReadRGBAImage can deal with it.
80
 * 1/0 is returned according to whether or not the image can
81
 * be handled.  If 0 is returned, emsg contains the reason
82
 * why it is being rejected.
83
 */
84
int TIFFRGBAImageOK(TIFF *tif, char emsg[EMSG_BUF_SIZE])
85
27.3k
{
86
27.3k
    TIFFDirectory *td = &tif->tif_dir;
87
27.3k
    uint16_t photometric;
88
27.3k
    int colorchannels;
89
90
27.3k
    if (!tif->tif_decodestatus)
91
0
    {
92
0
        snprintf(emsg, EMSG_BUF_SIZE,
93
0
                 "Sorry, requested compression method is not configured");
94
0
        return (0);
95
0
    }
96
27.3k
    switch (td->td_bitspersample)
97
27.3k
    {
98
11.0k
        case 1:
99
11.6k
        case 2:
100
12.5k
        case 4:
101
27.0k
        case 8:
102
27.3k
        case 16:
103
27.3k
            break;
104
13
        default:
105
13
            snprintf(emsg, EMSG_BUF_SIZE,
106
13
                     "Sorry, can not handle images with %" PRIu16
107
13
                     "-bit samples",
108
13
                     td->td_bitspersample);
109
13
            return (0);
110
27.3k
    }
111
27.3k
    if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
112
13
    {
113
13
        snprintf(
114
13
            emsg, EMSG_BUF_SIZE,
115
13
            "Sorry, can not handle images with IEEE floating-point samples");
116
13
        return (0);
117
13
    }
118
27.3k
    colorchannels = td->td_samplesperpixel - td->td_extrasamples;
119
27.3k
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric))
120
0
    {
121
0
        switch (colorchannels)
122
0
        {
123
0
            case 1:
124
0
                photometric = PHOTOMETRIC_MINISBLACK;
125
0
                break;
126
0
            case 3:
127
0
                photometric = PHOTOMETRIC_RGB;
128
0
                break;
129
0
            default:
130
0
                snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
131
0
                         photoTag);
132
0
                return (0);
133
0
        }
134
0
    }
135
27.3k
    switch (photometric)
136
27.3k
    {
137
8.25k
        case PHOTOMETRIC_MINISWHITE:
138
11.7k
        case PHOTOMETRIC_MINISBLACK:
139
12.5k
        case PHOTOMETRIC_PALETTE:
140
12.5k
            if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
141
11.2k
                td->td_samplesperpixel != 1 && td->td_bitspersample < 8)
142
28
            {
143
28
                snprintf(
144
28
                    emsg, EMSG_BUF_SIZE,
145
28
                    "Sorry, can not handle contiguous data with %s=%" PRIu16
146
28
                    ", "
147
28
                    "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "",
148
28
                    photoTag, photometric, "Samples/pixel",
149
28
                    td->td_samplesperpixel, td->td_bitspersample);
150
28
                return (0);
151
28
            }
152
            /*
153
             * We should likely validate that any extra samples are either
154
             * to be ignored, or are alpha, and if alpha we should try to use
155
             * them.  But for now we won't bother with this.
156
             */
157
12.4k
            break;
158
12.4k
        case PHOTOMETRIC_YCBCR:
159
            /*
160
             * TODO: if at all meaningful and useful, make more complete
161
             * support check here, or better still, refactor to let supporting
162
             * code decide whether there is support and what meaningful
163
             * error to return
164
             */
165
5.85k
            break;
166
4.93k
        case PHOTOMETRIC_RGB:
167
4.93k
            if (colorchannels < 3)
168
90
            {
169
90
                snprintf(emsg, EMSG_BUF_SIZE,
170
90
                         "Sorry, can not handle RGB image with %s=%d",
171
90
                         "Color channels", colorchannels);
172
90
                return (0);
173
90
            }
174
4.84k
            break;
175
4.84k
        case PHOTOMETRIC_SEPARATED:
176
687
        {
177
687
            uint16_t inkset;
178
687
            TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
179
687
            if (inkset != INKSET_CMYK)
180
4
            {
181
4
                snprintf(emsg, EMSG_BUF_SIZE,
182
4
                         "Sorry, can not handle separated image with %s=%d",
183
4
                         "InkSet", inkset);
184
4
                return 0;
185
4
            }
186
683
            if (td->td_samplesperpixel < 4)
187
21
            {
188
21
                snprintf(
189
21
                    emsg, EMSG_BUF_SIZE,
190
21
                    "Sorry, can not handle separated image with %s=%" PRIu16,
191
21
                    "Samples/pixel", td->td_samplesperpixel);
192
21
                return 0;
193
21
            }
194
662
            break;
195
683
        }
196
662
        case PHOTOMETRIC_LOGL:
197
456
            if (td->td_compression != COMPRESSION_SGILOG)
198
7
            {
199
7
                snprintf(emsg, EMSG_BUF_SIZE,
200
7
                         "Sorry, LogL data must have %s=%d", "Compression",
201
7
                         COMPRESSION_SGILOG);
202
7
                return (0);
203
7
            }
204
449
            break;
205
1.11k
        case PHOTOMETRIC_LOGLUV:
206
1.11k
            if (td->td_compression != COMPRESSION_SGILOG &&
207
592
                td->td_compression != COMPRESSION_SGILOG24)
208
22
            {
209
22
                snprintf(emsg, EMSG_BUF_SIZE,
210
22
                         "Sorry, LogLuv data must have %s=%d or %d",
211
22
                         "Compression", COMPRESSION_SGILOG,
212
22
                         COMPRESSION_SGILOG24);
213
22
                return (0);
214
22
            }
215
1.09k
            if (td->td_planarconfig != PLANARCONFIG_CONTIG)
216
7
            {
217
7
                snprintf(emsg, EMSG_BUF_SIZE,
218
7
                         "Sorry, can not handle LogLuv images with %s=%" PRIu16,
219
7
                         "Planarconfiguration", td->td_planarconfig);
220
7
                return (0);
221
7
            }
222
1.08k
            if (td->td_samplesperpixel != 3 || colorchannels != 3)
223
41
            {
224
41
                snprintf(emsg, EMSG_BUF_SIZE,
225
41
                         "Sorry, can not handle image with %s=%" PRIu16
226
41
                         ", %s=%d",
227
41
                         "Samples/pixel", td->td_samplesperpixel,
228
41
                         "colorchannels", colorchannels);
229
41
                return 0;
230
41
            }
231
1.04k
            break;
232
1.04k
        case PHOTOMETRIC_CIELAB:
233
315
            if (td->td_samplesperpixel != 3 || colorchannels != 3 ||
234
253
                (td->td_bitspersample != 8 && td->td_bitspersample != 16))
235
65
            {
236
65
                snprintf(emsg, EMSG_BUF_SIZE,
237
65
                         "Sorry, can not handle image with %s=%" PRIu16
238
65
                         ", %s=%d and %s=%" PRIu16,
239
65
                         "Samples/pixel", td->td_samplesperpixel,
240
65
                         "colorchannels", colorchannels, "Bits/sample",
241
65
                         td->td_bitspersample);
242
65
                return 0;
243
65
            }
244
250
            break;
245
1.47k
        default:
246
1.47k
            snprintf(emsg, EMSG_BUF_SIZE,
247
1.47k
                     "Sorry, can not handle image with %s=%" PRIu16, photoTag,
248
1.47k
                     photometric);
249
1.47k
            return (0);
250
27.3k
    }
251
25.5k
    return (1);
252
27.3k
}
253
254
void TIFFRGBAImageEnd(TIFFRGBAImage *img)
255
25.5k
{
256
25.5k
    if (img->Map)
257
14
    {
258
14
        _TIFFfreeExt(img->tif, img->Map);
259
14
        img->Map = NULL;
260
14
    }
261
25.5k
    if (img->BWmap)
262
11.2k
    {
263
11.2k
        _TIFFfreeExt(img->tif, img->BWmap);
264
11.2k
        img->BWmap = NULL;
265
11.2k
    }
266
25.5k
    if (img->PALmap)
267
753
    {
268
753
        _TIFFfreeExt(img->tif, img->PALmap);
269
753
        img->PALmap = NULL;
270
753
    }
271
25.5k
    if (img->ycbcr)
272
4.36k
    {
273
4.36k
        _TIFFfreeExt(img->tif, img->ycbcr);
274
4.36k
        img->ycbcr = NULL;
275
4.36k
    }
276
25.5k
    if (img->cielab)
277
244
    {
278
244
        _TIFFfreeExt(img->tif, img->cielab);
279
244
        img->cielab = NULL;
280
244
    }
281
25.5k
    if (img->UaToAa)
282
135
    {
283
135
        _TIFFfreeExt(img->tif, img->UaToAa);
284
135
        img->UaToAa = NULL;
285
135
    }
286
25.5k
    if (img->Bitdepth16To8)
287
39
    {
288
39
        _TIFFfreeExt(img->tif, img->Bitdepth16To8);
289
39
        img->Bitdepth16To8 = NULL;
290
39
    }
291
292
25.5k
    if (img->redcmap)
293
755
    {
294
755
        _TIFFfreeExt(img->tif, img->redcmap);
295
755
        _TIFFfreeExt(img->tif, img->greencmap);
296
755
        _TIFFfreeExt(img->tif, img->bluecmap);
297
755
        img->redcmap = img->greencmap = img->bluecmap = NULL;
298
755
    }
299
25.5k
}
300
301
static int isCCITTCompression(TIFF *tif)
302
0
{
303
0
    uint16_t compress;
304
0
    TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
305
0
    return (compress == COMPRESSION_CCITTFAX3 ||
306
0
            compress == COMPRESSION_CCITTFAX4 ||
307
0
            compress == COMPRESSION_CCITTRLE ||
308
0
            compress == COMPRESSION_CCITTRLEW);
309
0
}
310
311
int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop,
312
                       char emsg[EMSG_BUF_SIZE])
313
27.3k
{
314
27.3k
    uint16_t *sampleinfo;
315
27.3k
    uint16_t extrasamples;
316
27.3k
    uint16_t planarconfig;
317
27.3k
    uint16_t compress;
318
27.3k
    int colorchannels;
319
27.3k
    uint16_t *red_orig, *green_orig, *blue_orig;
320
27.3k
    uint32_t n_color;
321
322
27.3k
    if (!TIFFRGBAImageOK(tif, emsg))
323
1.78k
        return 0;
324
325
    /* Initialize to normal values */
326
25.5k
    img->row_offset = 0;
327
25.5k
    img->col_offset = 0;
328
25.5k
    img->redcmap = NULL;
329
25.5k
    img->greencmap = NULL;
330
25.5k
    img->bluecmap = NULL;
331
25.5k
    img->Map = NULL;
332
25.5k
    img->BWmap = NULL;
333
25.5k
    img->PALmap = NULL;
334
25.5k
    img->ycbcr = NULL;
335
25.5k
    img->cielab = NULL;
336
25.5k
    img->UaToAa = NULL;
337
25.5k
    img->Bitdepth16To8 = NULL;
338
25.5k
    img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
339
340
25.5k
    img->tif = tif;
341
25.5k
    img->stoponerr = stop;
342
25.5k
    TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
343
25.5k
    switch (img->bitspersample)
344
25.5k
    {
345
9.41k
        case 1:
346
9.96k
        case 2:
347
10.8k
        case 4:
348
25.3k
        case 8:
349
25.5k
        case 16:
350
25.5k
            break;
351
0
        default:
352
0
            snprintf(emsg, EMSG_BUF_SIZE,
353
0
                     "Sorry, can not handle images with %" PRIu16
354
0
                     "-bit samples",
355
0
                     img->bitspersample);
356
0
            goto fail_return;
357
25.5k
    }
358
25.5k
    img->alpha = 0;
359
25.5k
    TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
360
25.5k
    TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples,
361
25.5k
                          &sampleinfo);
362
25.5k
    if (extrasamples >= 1)
363
2.92k
    {
364
2.92k
        switch (sampleinfo[0])
365
2.92k
        {
366
2.42k
            case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without
367
                                           */
368
2.42k
                if (img->samplesperpixel >
369
2.42k
                    3) /* correct info about alpha channel */
370
1.56k
                    img->alpha = EXTRASAMPLE_ASSOCALPHA;
371
2.42k
                break;
372
294
            case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
373
504
            case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
374
504
                img->alpha = sampleinfo[0];
375
504
                break;
376
0
            default:
377
0
                break;
378
2.92k
        }
379
2.92k
    }
380
381
25.5k
#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
382
25.5k
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
383
0
        img->photometric = PHOTOMETRIC_MINISWHITE;
384
385
25.5k
    if (extrasamples == 0 && img->samplesperpixel == 4 &&
386
438
        img->photometric == PHOTOMETRIC_RGB)
387
0
    {
388
0
        img->alpha = EXTRASAMPLE_ASSOCALPHA;
389
0
        extrasamples = 1;
390
0
    }
391
25.5k
#endif
392
393
25.5k
    colorchannels = img->samplesperpixel - extrasamples;
394
25.5k
    TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
395
25.5k
    TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
396
25.5k
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
397
0
    {
398
0
        switch (colorchannels)
399
0
        {
400
0
            case 1:
401
0
                if (isCCITTCompression(tif))
402
0
                    img->photometric = PHOTOMETRIC_MINISWHITE;
403
0
                else
404
0
                    img->photometric = PHOTOMETRIC_MINISBLACK;
405
0
                break;
406
0
            case 3:
407
0
                img->photometric = PHOTOMETRIC_RGB;
408
0
                break;
409
0
            default:
410
0
                snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
411
0
                         photoTag);
412
0
                goto fail_return;
413
0
        }
414
0
    }
415
25.5k
    switch (img->photometric)
416
25.5k
    {
417
755
        case PHOTOMETRIC_PALETTE:
418
755
            if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &red_orig, &green_orig,
419
755
                              &blue_orig))
420
0
            {
421
0
                snprintf(emsg, EMSG_BUF_SIZE,
422
0
                         "Missing required \"Colormap\" tag");
423
0
                goto fail_return;
424
0
            }
425
426
            /* copy the colormaps so we can modify them */
427
755
            n_color = (uint32_t)(1U << img->bitspersample);
428
755
            img->redcmap = (uint16_t *)_TIFFmallocExt(
429
755
                tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
430
755
            img->greencmap = (uint16_t *)_TIFFmallocExt(
431
755
                tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
432
755
            img->bluecmap = (uint16_t *)_TIFFmallocExt(
433
755
                tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
434
755
            if (!img->redcmap || !img->greencmap || !img->bluecmap)
435
0
            {
436
0
                snprintf(emsg, EMSG_BUF_SIZE,
437
0
                         "Out of memory for colormap copy");
438
0
                goto fail_return;
439
0
            }
440
441
755
            _TIFFmemcpy(img->redcmap, red_orig, n_color * 2);
442
755
            _TIFFmemcpy(img->greencmap, green_orig, n_color * 2);
443
755
            _TIFFmemcpy(img->bluecmap, blue_orig, n_color * 2);
444
445
            /* fall through... */
446
8.99k
        case PHOTOMETRIC_MINISWHITE:
447
12.4k
        case PHOTOMETRIC_MINISBLACK:
448
12.4k
            if (planarconfig == PLANARCONFIG_CONTIG &&
449
11.2k
                img->samplesperpixel != 1 && img->bitspersample < 8)
450
0
            {
451
0
                snprintf(
452
0
                    emsg, EMSG_BUF_SIZE,
453
0
                    "Sorry, can not handle contiguous data with %s=%" PRIu16
454
0
                    ", "
455
0
                    "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16,
456
0
                    photoTag, img->photometric, "Samples/pixel",
457
0
                    img->samplesperpixel, img->bitspersample);
458
0
                goto fail_return;
459
0
            }
460
12.4k
            break;
461
12.4k
        case PHOTOMETRIC_YCBCR:
462
            /* It would probably be nice to have a reality check here. */
463
5.85k
            if (planarconfig == PLANARCONFIG_CONTIG)
464
                /* can rely on libjpeg to convert to RGB */
465
                /* XXX should restore current state on exit */
466
5.09k
                switch (compress)
467
5.09k
                {
468
1.44k
                    case COMPRESSION_JPEG:
469
                        /*
470
                         * TODO: when complete tests verify complete
471
                         * desubsampling and YCbCr handling, remove use of
472
                         * TIFFTAG_JPEGCOLORMODE in favor of tif_getimage.c
473
                         * native handling
474
                         */
475
1.44k
                        TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
476
1.44k
                                     JPEGCOLORMODE_RGB);
477
1.44k
                        img->photometric = PHOTOMETRIC_RGB;
478
1.44k
                        break;
479
3.64k
                    default:
480
3.64k
                        /* do nothing */;
481
3.64k
                        break;
482
5.09k
                }
483
            /*
484
             * TODO: if at all meaningful and useful, make more complete
485
             * support check here, or better still, refactor to let supporting
486
             * code decide whether there is support and what meaningful
487
             * error to return
488
             */
489
5.85k
            break;
490
5.85k
        case PHOTOMETRIC_RGB:
491
4.84k
            if (colorchannels < 3)
492
0
            {
493
0
                snprintf(emsg, EMSG_BUF_SIZE,
494
0
                         "Sorry, can not handle RGB image with %s=%d",
495
0
                         "Color channels", colorchannels);
496
0
                goto fail_return;
497
0
            }
498
4.84k
            break;
499
4.84k
        case PHOTOMETRIC_SEPARATED:
500
662
        {
501
662
            uint16_t inkset;
502
662
            TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
503
662
            if (inkset != INKSET_CMYK)
504
0
            {
505
0
                snprintf(
506
0
                    emsg, EMSG_BUF_SIZE,
507
0
                    "Sorry, can not handle separated image with %s=%" PRIu16,
508
0
                    "InkSet", inkset);
509
0
                goto fail_return;
510
0
            }
511
662
            if (img->samplesperpixel < 4)
512
0
            {
513
0
                snprintf(
514
0
                    emsg, EMSG_BUF_SIZE,
515
0
                    "Sorry, can not handle separated image with %s=%" PRIu16,
516
0
                    "Samples/pixel", img->samplesperpixel);
517
0
                goto fail_return;
518
0
            }
519
662
        }
520
662
        break;
521
662
        case PHOTOMETRIC_LOGL:
522
449
            if (compress != COMPRESSION_SGILOG)
523
0
            {
524
0
                snprintf(emsg, EMSG_BUF_SIZE,
525
0
                         "Sorry, LogL data must have %s=%d", "Compression",
526
0
                         COMPRESSION_SGILOG);
527
0
                goto fail_return;
528
0
            }
529
449
            TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
530
449
            img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
531
449
            img->bitspersample = 8;
532
449
            break;
533
1.04k
        case PHOTOMETRIC_LOGLUV:
534
1.04k
            if (compress != COMPRESSION_SGILOG &&
535
542
                compress != COMPRESSION_SGILOG24)
536
0
            {
537
0
                snprintf(emsg, EMSG_BUF_SIZE,
538
0
                         "Sorry, LogLuv data must have %s=%d or %d",
539
0
                         "Compression", COMPRESSION_SGILOG,
540
0
                         COMPRESSION_SGILOG24);
541
0
                goto fail_return;
542
0
            }
543
1.04k
            if (planarconfig != PLANARCONFIG_CONTIG)
544
0
            {
545
0
                snprintf(emsg, EMSG_BUF_SIZE,
546
0
                         "Sorry, can not handle LogLuv images with %s=%" PRIu16,
547
0
                         "Planarconfiguration", planarconfig);
548
0
                return (0);
549
0
            }
550
1.04k
            TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
551
1.04k
            img->photometric = PHOTOMETRIC_RGB; /* little white lie */
552
1.04k
            img->bitspersample = 8;
553
1.04k
            break;
554
250
        case PHOTOMETRIC_CIELAB:
555
250
            break;
556
0
        default:
557
0
            snprintf(emsg, EMSG_BUF_SIZE,
558
0
                     "Sorry, can not handle image with %s=%" PRIu16, photoTag,
559
0
                     img->photometric);
560
0
            goto fail_return;
561
25.5k
    }
562
25.5k
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
563
25.5k
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
564
25.5k
    TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
565
25.5k
    img->isContig =
566
25.5k
        !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
567
25.5k
    if (img->isContig)
568
21.5k
    {
569
21.5k
        if (!PickContigCase(img))
570
218
        {
571
218
            snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
572
218
            goto fail_return;
573
218
        }
574
21.5k
    }
575
4.08k
    else
576
4.08k
    {
577
4.08k
        if (!PickSeparateCase(img))
578
121
        {
579
121
            snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
580
121
            goto fail_return;
581
121
        }
582
4.08k
    }
583
25.2k
    return 1;
584
585
339
fail_return:
586
339
    TIFFRGBAImageEnd(img);
587
339
    return 0;
588
25.5k
}
589
590
int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
591
                     uint32_t h)
592
25.2k
{
593
25.2k
    if (img->get == NULL)
594
0
    {
595
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
596
0
                      "No \"get\" routine setup");
597
0
        return (0);
598
0
    }
599
25.2k
    if (img->put.any == NULL)
600
0
    {
601
0
        TIFFErrorExtR(
602
0
            img->tif, TIFFFileName(img->tif),
603
0
            "No \"put\" routine setupl; probably can not handle image format");
604
0
        return (0);
605
0
    }
606
    /* Verify raster height against image height.
607
     * Width is checked in img->get() function individually. */
608
25.2k
    if (0 <= img->row_offset && (uint32_t)img->row_offset < img->height)
609
25.2k
    {
610
25.2k
        uint32_t hx = img->height - (uint32_t)img->row_offset;
611
25.2k
        if (h > hx)
612
0
        {
613
            /* Adapt parameters to read only available lines and put image
614
             * at the bottom of the raster. */
615
0
            raster += (size_t)(h - hx) * w;
616
0
            h = hx;
617
0
        }
618
25.2k
    }
619
0
    else
620
0
    {
621
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
622
0
                      "Error in TIFFRGBAImageGet: row offset %d exceeds "
623
0
                      "image height %u",
624
0
                      img->row_offset, img->height);
625
0
        return 0;
626
0
    }
627
25.2k
    return (*img->get)(img, raster, w, h);
628
25.2k
}
629
630
/*
631
 * Read the specified image into an ABGR-format rastertaking in account
632
 * specified orientation.
633
 */
634
int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight,
635
                              uint32_t *raster, int orientation, int stop)
636
27.3k
{
637
27.3k
    char emsg[EMSG_BUF_SIZE] = "";
638
27.3k
    TIFFRGBAImage img;
639
27.3k
    int ok;
640
641
27.3k
    if (TIFFRGBAImageBegin(&img, tif, stop, emsg))
642
25.2k
    {
643
25.2k
        img.req_orientation = (uint16_t)orientation;
644
25.2k
        ok = TIFFRGBAImageGet(&img, raster, rwidth, rheight);
645
25.2k
        TIFFRGBAImageEnd(&img);
646
25.2k
    }
647
2.12k
    else
648
2.12k
    {
649
2.12k
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
650
2.12k
        ok = 0;
651
2.12k
    }
652
27.3k
    return (ok);
653
27.3k
}
654
655
/*
656
 * Read the specified image into an ABGR-format raster. Use bottom left
657
 * origin for raster by default.
658
 */
659
int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight,
660
                      uint32_t *raster, int stop)
661
27.3k
{
662
27.3k
    return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
663
27.3k
                                     ORIENTATION_BOTLEFT, stop);
664
27.3k
}
665
666
static int setorientation(TIFFRGBAImage *img)
667
25.2k
{
668
25.2k
    switch (img->orientation)
669
25.2k
    {
670
24.3k
        case ORIENTATION_TOPLEFT:
671
24.3k
        case ORIENTATION_LEFTTOP:
672
24.3k
            if (img->req_orientation == ORIENTATION_TOPRIGHT ||
673
24.3k
                img->req_orientation == ORIENTATION_RIGHTTOP)
674
0
                return FLIP_HORIZONTALLY;
675
24.3k
            else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
676
24.3k
                     img->req_orientation == ORIENTATION_RIGHTBOT)
677
0
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
678
24.3k
            else if (img->req_orientation == ORIENTATION_BOTLEFT ||
679
0
                     img->req_orientation == ORIENTATION_LEFTBOT)
680
24.3k
                return FLIP_VERTICALLY;
681
0
            else
682
0
                return 0;
683
172
        case ORIENTATION_TOPRIGHT:
684
323
        case ORIENTATION_RIGHTTOP:
685
323
            if (img->req_orientation == ORIENTATION_TOPLEFT ||
686
323
                img->req_orientation == ORIENTATION_LEFTTOP)
687
0
                return FLIP_HORIZONTALLY;
688
323
            else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
689
323
                     img->req_orientation == ORIENTATION_RIGHTBOT)
690
0
                return FLIP_VERTICALLY;
691
323
            else if (img->req_orientation == ORIENTATION_BOTLEFT ||
692
0
                     img->req_orientation == ORIENTATION_LEFTBOT)
693
323
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
694
0
            else
695
0
                return 0;
696
259
        case ORIENTATION_BOTRIGHT:
697
463
        case ORIENTATION_RIGHTBOT:
698
463
            if (img->req_orientation == ORIENTATION_TOPLEFT ||
699
463
                img->req_orientation == ORIENTATION_LEFTTOP)
700
0
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
701
463
            else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
702
463
                     img->req_orientation == ORIENTATION_RIGHTTOP)
703
0
                return FLIP_VERTICALLY;
704
463
            else if (img->req_orientation == ORIENTATION_BOTLEFT ||
705
0
                     img->req_orientation == ORIENTATION_LEFTBOT)
706
463
                return FLIP_HORIZONTALLY;
707
0
            else
708
0
                return 0;
709
48
        case ORIENTATION_BOTLEFT:
710
113
        case ORIENTATION_LEFTBOT:
711
113
            if (img->req_orientation == ORIENTATION_TOPLEFT ||
712
113
                img->req_orientation == ORIENTATION_LEFTTOP)
713
0
                return FLIP_VERTICALLY;
714
113
            else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
715
113
                     img->req_orientation == ORIENTATION_RIGHTTOP)
716
0
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
717
113
            else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
718
113
                     img->req_orientation == ORIENTATION_RIGHTBOT)
719
0
                return FLIP_HORIZONTALLY;
720
113
            else
721
113
                return 0;
722
0
        default: /* NOTREACHED */
723
0
            return 0;
724
25.2k
    }
725
25.2k
}
726
727
/*
728
 * Get an tile-organized image that has
729
 *  PlanarConfiguration contiguous if SamplesPerPixel > 1
730
 * or
731
 *  SamplesPerPixel == 1
732
 */
733
static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
734
                        uint32_t h)
735
3.59k
{
736
3.59k
    TIFF *tif = img->tif;
737
3.59k
    tileContigRoutine put = img->put.contig;
738
3.59k
    uint32_t col, row, y, rowstoread;
739
3.59k
    tmsize_t pos;
740
3.59k
    uint32_t tw, th;
741
3.59k
    unsigned char *buf = NULL;
742
3.59k
    int32_t fromskew, toskew;
743
3.59k
    uint32_t nrow;
744
3.59k
    int ret = 1, flip;
745
3.59k
    uint32_t this_tw, tocol;
746
3.59k
    int32_t this_toskew, leftmost_toskew;
747
3.59k
    int32_t leftmost_fromskew;
748
3.59k
    uint32_t leftmost_tw;
749
3.59k
    tmsize_t bufsize;
750
751
    /* If the raster is smaller than the image,
752
     * or if there is a col_offset, adapt the samples to be copied per row. */
753
3.59k
    uint32_t wmin;
754
755
3.59k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
756
3.59k
    {
757
3.59k
        wmin = TIFFmin(w, img->width - (uint32_t)img->col_offset);
758
3.59k
    }
759
0
    else
760
0
    {
761
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
762
0
                      "Error in gtTileContig: column offset %d exceeds "
763
0
                      "image width %u",
764
0
                      img->col_offset, img->width);
765
0
        return 0;
766
0
    }
767
3.59k
    bufsize = TIFFTileSize(tif);
768
3.59k
    if (bufsize == 0)
769
0
    {
770
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", "No space for tile buffer");
771
0
        return (0);
772
0
    }
773
774
3.59k
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
775
3.59k
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
776
777
3.59k
    flip = setorientation(img);
778
3.59k
    if (flip & FLIP_VERTICALLY)
779
3.43k
    {
780
3.43k
        if (((int64_t)tw + w) > INT_MAX)
781
0
        {
782
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
783
0
                          "unsupported tile size (too wide)");
784
0
            return (0);
785
0
        }
786
3.43k
        y = h - 1;
787
3.43k
        toskew = -(int32_t)(tw + w);
788
3.43k
    }
789
158
    else
790
158
    {
791
158
        if (tw > ((int64_t)INT_MAX + w) || w > ((int64_t)INT_MAX + tw))
792
0
        {
793
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
794
0
                          "unsupported tile size (too wide)");
795
0
            return (0);
796
0
        }
797
158
        y = 0;
798
158
        toskew = -(int32_t)(tw - w);
799
158
    }
800
801
3.59k
    if (tw == 0 || th == 0)
802
0
    {
803
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
804
0
        return (0);
805
0
    }
806
807
    /*
808
     *  Leftmost tile is clipped on left side if col_offset > 0.
809
     */
810
3.59k
    leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
811
3.59k
    leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
812
3.59k
    int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
813
3.59k
    if (skew_i64 > INT_MAX || skew_i64 < INT_MIN)
814
0
    {
815
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew",
816
0
                      skew_i64);
817
0
        return (0);
818
0
    }
819
3.59k
    leftmost_toskew = (int32_t)skew_i64;
820
32.4k
    for (row = 0; ret != 0 && row < h; row += nrow)
821
28.8k
    {
822
28.8k
        rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
823
28.8k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
824
28.8k
        fromskew = leftmost_fromskew;
825
28.8k
        this_tw = leftmost_tw;
826
28.8k
        this_toskew = leftmost_toskew;
827
28.8k
        tocol = 0;
828
28.8k
        col = (uint32_t)img->col_offset;
829
        /* wmin: only write imagewidth if raster is bigger. */
830
1.34M
        while (tocol < wmin)
831
1.31M
        {
832
1.31M
            if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col,
833
1.31M
                                            (uint32_t)row +
834
1.31M
                                                (uint32_t)img->row_offset,
835
1.31M
                                            0, 0) == (tmsize_t)(-1) &&
836
1.24M
                (buf == NULL || img->stoponerr))
837
817
            {
838
817
                ret = 0;
839
817
                break;
840
817
            }
841
1.31M
            pos = (((uint32_t)row + (uint32_t)img->row_offset) % th) *
842
1.31M
                      TIFFTileRowSize(tif) +
843
1.31M
                  ((tmsize_t)fromskew * img->samplesperpixel);
844
1.31M
            if (tocol + this_tw > wmin)
845
20.5k
            {
846
                /*
847
                 * Rightmost tile is clipped on right side.
848
                 */
849
20.5k
                fromskew = (int32_t)(tw - (wmin - tocol));
850
20.5k
                this_tw = (uint32_t)((int32_t)tw - fromskew);
851
20.5k
                this_toskew = (int32_t)(toskew + (int32_t)(uint32_t)fromskew);
852
20.5k
            }
853
1.31M
            tmsize_t roffset = (tmsize_t)y * w + tocol;
854
1.31M
            (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
855
1.31M
                   this_toskew, buf + pos);
856
1.31M
            tocol += this_tw;
857
1.31M
            col += this_tw;
858
            /*
859
             * After the leftmost tile, tiles are no longer clipped on left
860
             * side.
861
             */
862
1.31M
            fromskew = 0;
863
1.31M
            this_tw = tw;
864
1.31M
            this_toskew = toskew;
865
1.31M
        }
866
867
28.8k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
868
28.8k
                                                 : (int32_t)nrow);
869
28.8k
    }
870
3.59k
    _TIFFfreeExt(img->tif, buf);
871
872
3.59k
    if (flip & FLIP_HORIZONTALLY)
873
202
    {
874
202
        uint32_t line;
875
876
36.2k
        for (line = 0; line < h; line++)
877
36.0k
        {
878
36.0k
            uint32_t *left = raster + (size_t)line * w;
879
            /* Use wmin to only flip horizontally data in place and not complete
880
             * raster-row. */
881
36.0k
            uint32_t *right = left + wmin - 1;
882
883
16.1M
            while (left < right)
884
16.1M
            {
885
16.1M
                uint32_t temp = *left;
886
16.1M
                *left = *right;
887
16.1M
                *right = temp;
888
16.1M
                left++;
889
16.1M
                right--;
890
16.1M
            }
891
36.0k
        }
892
202
    }
893
894
3.59k
    return (ret);
895
3.59k
}
896
897
/*
898
 * Get an tile-organized image that has
899
 *   SamplesPerPixel > 1
900
 *   PlanarConfiguration separated
901
 * We assume that all such images are RGB.
902
 */
903
static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
904
                          uint32_t h)
905
1.87k
{
906
1.87k
    TIFF *tif = img->tif;
907
1.87k
    tileSeparateRoutine put = img->put.separate;
908
1.87k
    uint32_t col, row, y, rowstoread;
909
1.87k
    tmsize_t pos;
910
1.87k
    uint32_t tw, th;
911
1.87k
    unsigned char *buf = NULL;
912
1.87k
    unsigned char *p0 = NULL;
913
1.87k
    unsigned char *p1 = NULL;
914
1.87k
    unsigned char *p2 = NULL;
915
1.87k
    unsigned char *pa = NULL;
916
1.87k
    tmsize_t tilesize;
917
1.87k
    tmsize_t bufsize;
918
1.87k
    int32_t fromskew, toskew;
919
1.87k
    int alpha = img->alpha;
920
1.87k
    uint32_t nrow;
921
1.87k
    int ret = 1, flip;
922
1.87k
    uint16_t colorchannels;
923
1.87k
    uint32_t this_tw, tocol;
924
1.87k
    int32_t this_toskew, leftmost_toskew;
925
1.87k
    int32_t leftmost_fromskew;
926
1.87k
    uint32_t leftmost_tw;
927
928
    /* If the raster is smaller than the image,
929
     * or if there is a col_offset, adapt the samples to be copied per row. */
930
1.87k
    uint32_t wmin;
931
1.87k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
932
1.87k
    {
933
1.87k
        wmin = TIFFmin(w, img->width - (uint32_t)img->col_offset);
934
1.87k
    }
935
0
    else
936
0
    {
937
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
938
0
                      "Error in gtTileSeparate: column offset %d exceeds "
939
0
                      "image width %u",
940
0
                      img->col_offset, img->width);
941
0
        return 0;
942
0
    }
943
944
1.87k
    tilesize = TIFFTileSize(tif);
945
1.87k
    bufsize =
946
1.87k
        _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate");
947
1.87k
    if (bufsize == 0)
948
0
    {
949
0
        return (0);
950
0
    }
951
952
1.87k
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
953
1.87k
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
954
955
1.87k
    flip = setorientation(img);
956
1.87k
    if (flip & FLIP_VERTICALLY)
957
1.76k
    {
958
1.76k
        if (((int64_t)tw + w) > INT_MAX)
959
0
        {
960
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
961
0
                          "unsupported tile size (too wide)");
962
0
            return (0);
963
0
        }
964
1.76k
        y = h - 1;
965
1.76k
        toskew = -(int32_t)(tw + w);
966
1.76k
    }
967
107
    else
968
107
    {
969
107
        if (tw > ((int64_t)INT_MAX + w) || w > ((int64_t)INT_MAX + tw))
970
0
        {
971
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
972
0
                          "unsupported tile size (too wide)");
973
0
            return (0);
974
0
        }
975
107
        y = 0;
976
107
        toskew = -(int32_t)(tw - w);
977
107
    }
978
979
1.87k
    switch (img->photometric)
980
1.87k
    {
981
75
        case PHOTOMETRIC_MINISWHITE:
982
303
        case PHOTOMETRIC_MINISBLACK:
983
303
        case PHOTOMETRIC_PALETTE:
984
303
            colorchannels = 1;
985
303
            break;
986
987
1.56k
        default:
988
1.56k
            colorchannels = 3;
989
1.56k
            break;
990
1.87k
    }
991
992
1.87k
    if (tw == 0 || th == 0)
993
0
    {
994
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
995
0
        return (0);
996
0
    }
997
998
    /*
999
     *  Leftmost tile is clipped on left side if col_offset > 0.
1000
     */
1001
1.87k
    leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
1002
1.87k
    leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
1003
1.87k
    int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
1004
1.87k
    if (skew_i64 > INT_MAX || skew_i64 < INT_MIN)
1005
0
    {
1006
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew",
1007
0
                      skew_i64);
1008
0
        return (0);
1009
0
    }
1010
1.87k
    leftmost_toskew = (int32_t)skew_i64;
1011
10.8k
    for (row = 0; ret != 0 && row < h; row += nrow)
1012
8.93k
    {
1013
8.93k
        rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
1014
8.93k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1015
8.93k
        fromskew = leftmost_fromskew;
1016
8.93k
        this_tw = leftmost_tw;
1017
8.93k
        this_toskew = leftmost_toskew;
1018
8.93k
        tocol = 0;
1019
8.93k
        col = (uint32_t)img->col_offset;
1020
        /* wmin: only write imagewidth if raster is bigger. */
1021
49.3k
        while (tocol < wmin)
1022
40.7k
        {
1023
40.7k
            if (buf == NULL)
1024
1.87k
            {
1025
1.87k
                if (_TIFFReadTileAndAllocBuffer(
1026
1.87k
                        tif, (void **)&buf, bufsize, col,
1027
1.87k
                        (uint32_t)((int32_t)row + img->row_offset), 0,
1028
1.87k
                        0) == (tmsize_t)(-1) &&
1029
318
                    (buf == NULL || img->stoponerr))
1030
318
                {
1031
318
                    ret = 0;
1032
318
                    break;
1033
318
                }
1034
1.55k
                p0 = buf;
1035
1.55k
                if (colorchannels == 1)
1036
97
                {
1037
97
                    p2 = p1 = p0;
1038
97
                    pa = (alpha ? (p0 + 3 * tilesize) : NULL);
1039
97
                }
1040
1.45k
                else
1041
1.45k
                {
1042
1.45k
                    p1 = p0 + tilesize;
1043
1.45k
                    p2 = p1 + tilesize;
1044
1.45k
                    pa = (alpha ? (p2 + tilesize) : NULL);
1045
1.45k
                }
1046
1.55k
            }
1047
38.8k
            else if (TIFFReadTile(tif, p0, col,
1048
38.8k
                                  (uint32_t)((int32_t)row + img->row_offset), 0,
1049
38.8k
                                  0) == (tmsize_t)(-1) &&
1050
36.3k
                     img->stoponerr)
1051
0
            {
1052
0
                ret = 0;
1053
0
                break;
1054
0
            }
1055
40.4k
            if (colorchannels > 1 &&
1056
39.0k
                TIFFReadTile(tif, p1, col,
1057
39.0k
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1058
39.0k
                             (uint16_t)1) == (tmsize_t)(-1) &&
1059
37.9k
                img->stoponerr)
1060
0
            {
1061
0
                ret = 0;
1062
0
                break;
1063
0
            }
1064
40.4k
            if (colorchannels > 1 &&
1065
39.0k
                TIFFReadTile(tif, p2, col,
1066
39.0k
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1067
39.0k
                             (uint16_t)2) == (tmsize_t)(-1) &&
1068
25.7k
                img->stoponerr)
1069
0
            {
1070
0
                ret = 0;
1071
0
                break;
1072
0
            }
1073
40.4k
            if (alpha &&
1074
1.46k
                TIFFReadTile(tif, pa, col,
1075
1.46k
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1076
1.46k
                             (uint16_t)colorchannels) == (tmsize_t)(-1) &&
1077
1.45k
                img->stoponerr)
1078
0
            {
1079
0
                ret = 0;
1080
0
                break;
1081
0
            }
1082
1083
            /* For SEPARATE the pos-offset is per sample and should not be
1084
             * multiplied by img->samplesperpixel. */
1085
40.4k
            pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) % th) *
1086
40.4k
                      TIFFTileRowSize(tif) +
1087
40.4k
                  (tmsize_t)fromskew;
1088
40.4k
            if (tocol + this_tw > wmin)
1089
8.49k
            {
1090
                /*
1091
                 * Rightmost tile is clipped on right side.
1092
                 */
1093
8.49k
                fromskew = (int32_t)(tw - (wmin - tocol));
1094
8.49k
                this_tw = (uint32_t)((int32_t)tw - fromskew);
1095
8.49k
                this_toskew = (int32_t)(toskew + (int32_t)(uint32_t)fromskew);
1096
8.49k
            }
1097
40.4k
            tmsize_t roffset = (tmsize_t)y * w + tocol;
1098
40.4k
            (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
1099
40.4k
                   this_toskew, p0 + pos, p1 + pos, p2 + pos,
1100
40.4k
                   (alpha ? (pa + pos) : NULL));
1101
40.4k
            tocol += this_tw;
1102
40.4k
            col += this_tw;
1103
            /*
1104
             * After the leftmost tile, tiles are no longer clipped on left
1105
             * side.
1106
             */
1107
40.4k
            fromskew = 0;
1108
40.4k
            this_tw = tw;
1109
40.4k
            this_toskew = toskew;
1110
40.4k
        }
1111
1112
8.93k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1113
8.93k
                                                 : (int32_t)nrow);
1114
8.93k
    }
1115
1116
1.87k
    if (flip & FLIP_HORIZONTALLY)
1117
125
    {
1118
125
        uint32_t line;
1119
1120
27.9k
        for (line = 0; line < h; line++)
1121
27.8k
        {
1122
27.8k
            uint32_t *left = raster + (size_t)line * w;
1123
            /* Use wmin to only flip horizontally data in place and not complete
1124
             * raster-row. */
1125
27.8k
            uint32_t *right = left + wmin - 1;
1126
1127
22.5M
            while (left < right)
1128
22.4M
            {
1129
22.4M
                uint32_t temp = *left;
1130
22.4M
                *left = *right;
1131
22.4M
                *right = temp;
1132
22.4M
                left++;
1133
22.4M
                right--;
1134
22.4M
            }
1135
27.8k
        }
1136
125
    }
1137
1138
1.87k
    _TIFFfreeExt(img->tif, buf);
1139
1.87k
    return (ret);
1140
1.87k
}
1141
1142
/*
1143
 * Get a strip-organized image that has
1144
 *  PlanarConfiguration contiguous if SamplesPerPixel > 1
1145
 * or
1146
 *  SamplesPerPixel == 1
1147
 */
1148
static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
1149
                         uint32_t h)
1150
17.6k
{
1151
17.6k
    TIFF *tif = img->tif;
1152
17.6k
    tileContigRoutine put = img->put.contig;
1153
17.6k
    uint32_t row, y, nrow, nrowsub, rowstoread;
1154
17.6k
    tmsize_t pos;
1155
17.6k
    unsigned char *buf = NULL;
1156
17.6k
    uint32_t rowsperstrip;
1157
17.6k
    uint16_t subsamplinghor, subsamplingver;
1158
17.6k
    uint32_t imagewidth = img->width;
1159
17.6k
    tmsize_t scanline;
1160
    /* fromskew, toskew are the increments within the input image or the raster
1161
     * from the end of a line to the start of the next line to read or write. */
1162
17.6k
    int32_t fromskew, toskew;
1163
17.6k
    int ret = 1, flip;
1164
17.6k
    tmsize_t maxstripsize;
1165
1166
    /* If the raster is smaller than the image,
1167
     * or if there is a col_offset, adapt the samples to be copied per row. */
1168
17.6k
    uint32_t wmin;
1169
17.6k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
1170
17.6k
    {
1171
17.6k
        wmin = TIFFmin(w, imagewidth - (uint32_t)img->col_offset);
1172
17.6k
    }
1173
0
    else
1174
0
    {
1175
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
1176
0
                      "Error in gtStripContig: column offset %d exceeds "
1177
0
                      "image width %u",
1178
0
                      img->col_offset, imagewidth);
1179
0
        return 0;
1180
0
    }
1181
1182
17.6k
    TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor,
1183
17.6k
                          &subsamplingver);
1184
17.6k
    if (subsamplingver == 0)
1185
6
    {
1186
6
        TIFFErrorExtR(tif, TIFFFileName(tif),
1187
6
                      "Invalid vertical YCbCr subsampling");
1188
6
        return (0);
1189
6
    }
1190
1191
17.6k
    maxstripsize = TIFFStripSize(tif);
1192
1193
17.6k
    flip = setorientation(img);
1194
17.6k
    if (flip & FLIP_VERTICALLY)
1195
17.4k
    {
1196
17.4k
        if (w > INT_MAX / 2)
1197
0
        {
1198
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
1199
0
            return (0);
1200
0
        }
1201
17.4k
        y = h - 1;
1202
        /* Skew back to the raster row before the currently written row
1203
         * -> one raster width plus copied image pixels. */
1204
17.4k
        toskew = -(int32_t)(w + wmin);
1205
17.4k
    }
1206
214
    else
1207
214
    {
1208
214
        y = 0;
1209
        /* Skew forward to the end of the raster width of the row currently
1210
         * copied. */
1211
214
        toskew = (int32_t)(w - wmin);
1212
214
    }
1213
1214
17.6k
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1215
17.6k
    if (rowsperstrip == 0)
1216
0
    {
1217
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1218
0
        return (0);
1219
0
    }
1220
1221
17.6k
    scanline = TIFFScanlineSize(tif);
1222
17.6k
    fromskew = (int32_t)(w < imagewidth ? imagewidth - w : 0);
1223
99.6k
    for (row = 0; row < h; row += nrow)
1224
87.0k
    {
1225
87.0k
        uint32_t temp;
1226
87.0k
        rowstoread = rowsperstrip -
1227
87.0k
                     (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip;
1228
87.0k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1229
87.0k
        nrowsub = nrow;
1230
87.0k
        if ((nrowsub % subsamplingver) != 0)
1231
39.0k
            nrowsub += subsamplingver - nrowsub % subsamplingver;
1232
87.0k
        temp =
1233
87.0k
            (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrowsub;
1234
87.0k
        if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))
1235
0
        {
1236
0
            TIFFErrorExtR(tif, TIFFFileName(tif),
1237
0
                          "Integer overflow in gtStripContig");
1238
0
            return 0;
1239
0
        }
1240
87.0k
        if (_TIFFReadEncodedStripAndAllocBuffer(
1241
87.0k
                tif,
1242
87.0k
                TIFFComputeStrip(tif,
1243
87.0k
                                 (uint32_t)((int32_t)row + img->row_offset), 0),
1244
87.0k
                (void **)(&buf), maxstripsize,
1245
87.0k
                (tmsize_t)((size_t)temp * (size_t)scanline)) ==
1246
87.0k
                (tmsize_t)(-1) &&
1247
65.3k
            (buf == NULL || img->stoponerr))
1248
5.03k
        {
1249
5.03k
            ret = 0;
1250
5.03k
            break;
1251
5.03k
        }
1252
1253
81.9k
        pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) %
1254
81.9k
                         rowsperstrip) *
1255
81.9k
                  scanline +
1256
81.9k
              ((tmsize_t)img->col_offset * img->samplesperpixel);
1257
81.9k
        tmsize_t roffset = (tmsize_t)y * w;
1258
81.9k
        (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
1259
81.9k
               buf + pos);
1260
81.9k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1261
81.9k
                                                 : (int32_t)nrow);
1262
81.9k
    }
1263
1264
17.6k
    if (flip & FLIP_HORIZONTALLY)
1265
275
    {
1266
        /* Flips the complete raster matrix horizontally. If raster width is
1267
         * larger than image width, data are moved horizontally to the right
1268
         * side.
1269
         * Use wmin to only flip data in place. */
1270
275
        uint32_t line;
1271
1272
41.8k
        for (line = 0; line < h; line++)
1273
41.6k
        {
1274
41.6k
            uint32_t *left = raster + (size_t)line * w;
1275
            /* Use wmin to only flip horizontally data in place and not complete
1276
             * raster-row. */
1277
41.6k
            uint32_t *right = left + wmin - 1;
1278
1279
9.72M
            while (left < right)
1280
9.68M
            {
1281
9.68M
                uint32_t temp = *left;
1282
9.68M
                *left = *right;
1283
9.68M
                *right = temp;
1284
9.68M
                left++;
1285
9.68M
                right--;
1286
9.68M
            }
1287
41.6k
        }
1288
275
    }
1289
1290
17.6k
    _TIFFfreeExt(img->tif, buf);
1291
17.6k
    return (ret);
1292
17.6k
}
1293
1294
/*
1295
 * Get a strip-organized image with
1296
 *   SamplesPerPixel > 1
1297
 *   PlanarConfiguration separated
1298
 * We assume that all such images are RGB.
1299
 */
1300
static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
1301
                           uint32_t h)
1302
2.09k
{
1303
2.09k
    TIFF *tif = img->tif;
1304
2.09k
    tileSeparateRoutine put = img->put.separate;
1305
2.09k
    unsigned char *buf = NULL;
1306
2.09k
    unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;
1307
2.09k
    uint32_t row, y, nrow, rowstoread;
1308
2.09k
    tmsize_t pos;
1309
2.09k
    tmsize_t scanline;
1310
2.09k
    uint32_t rowsperstrip, offset_row;
1311
2.09k
    uint32_t imagewidth = img->width;
1312
2.09k
    tmsize_t stripsize;
1313
2.09k
    tmsize_t bufsize;
1314
2.09k
    int32_t fromskew, toskew;
1315
2.09k
    int alpha = img->alpha;
1316
2.09k
    int ret = 1, flip;
1317
2.09k
    uint16_t colorchannels;
1318
1319
    /* If the raster is smaller than the image,
1320
     * or if there is a col_offset, adapt the samples to be copied per row. */
1321
2.09k
    uint32_t wmin;
1322
2.09k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
1323
2.09k
    {
1324
2.09k
        wmin = TIFFmin(w, imagewidth - (uint32_t)img->col_offset);
1325
2.09k
    }
1326
0
    else
1327
0
    {
1328
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
1329
0
                      "Error in gtStripSeparate: column offset %d exceeds "
1330
0
                      "image width %u",
1331
0
                      img->col_offset, imagewidth);
1332
0
        return 0;
1333
0
    }
1334
1335
2.09k
    stripsize = TIFFStripSize(tif);
1336
2.09k
    bufsize =
1337
2.09k
        _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate");
1338
2.09k
    if (bufsize == 0)
1339
0
    {
1340
0
        return (0);
1341
0
    }
1342
1343
2.09k
    flip = setorientation(img);
1344
2.09k
    if (flip & FLIP_VERTICALLY)
1345
1.99k
    {
1346
1.99k
        if (w > INT_MAX / 2)
1347
0
        {
1348
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
1349
0
            return (0);
1350
0
        }
1351
1.99k
        y = h - 1;
1352
        /* Skew back to the raster row before the currently written row
1353
         * -> one raster width plus one image width. */
1354
1.99k
        toskew = -(int32_t)(w + wmin);
1355
1.99k
    }
1356
97
    else
1357
97
    {
1358
97
        y = 0;
1359
        /* Skew forward to the end of the raster width of the row currently
1360
         * written. */
1361
97
        toskew = (int32_t)(w - wmin);
1362
97
    }
1363
1364
2.09k
    switch (img->photometric)
1365
2.09k
    {
1366
237
        case PHOTOMETRIC_MINISWHITE:
1367
572
        case PHOTOMETRIC_MINISBLACK:
1368
572
        case PHOTOMETRIC_PALETTE:
1369
572
            colorchannels = 1;
1370
572
            break;
1371
1372
1.51k
        default:
1373
1.51k
            colorchannels = 3;
1374
1.51k
            break;
1375
2.09k
    }
1376
1377
2.09k
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1378
2.09k
    if (rowsperstrip == 0)
1379
0
    {
1380
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1381
0
        return (0);
1382
0
    }
1383
1384
2.09k
    scanline = TIFFScanlineSize(tif);
1385
2.09k
    fromskew = (int32_t)(w < imagewidth ? imagewidth - w : 0);
1386
3.95k
    for (row = 0; row < h; row += nrow)
1387
2.09k
    {
1388
2.09k
        uint32_t temp;
1389
2.09k
        rowstoread =
1390
2.09k
            rowsperstrip -
1391
2.09k
            ((uint32_t)((int32_t)row + img->row_offset) % rowsperstrip);
1392
2.09k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1393
2.09k
        offset_row = (uint32_t)((int32_t)row + img->row_offset);
1394
2.09k
        temp = (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrow;
1395
2.09k
        if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))
1396
0
        {
1397
0
            TIFFErrorExtR(tif, TIFFFileName(tif),
1398
0
                          "Integer overflow in gtStripSeparate");
1399
0
            return 0;
1400
0
        }
1401
2.09k
        if (buf == NULL)
1402
2.09k
        {
1403
2.09k
            if (_TIFFReadEncodedStripAndAllocBuffer(
1404
2.09k
                    tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf,
1405
2.09k
                    bufsize, temp * scanline) == (tmsize_t)(-1) &&
1406
1.13k
                (buf == NULL || img->stoponerr))
1407
226
            {
1408
226
                ret = 0;
1409
226
                break;
1410
226
            }
1411
1.86k
            p0 = buf;
1412
1.86k
            if (colorchannels == 1)
1413
505
            {
1414
505
                p2 = p1 = p0;
1415
505
                pa = (alpha ? (p0 + 3 * stripsize) : NULL);
1416
505
            }
1417
1.36k
            else
1418
1.36k
            {
1419
1.36k
                p1 = p0 + stripsize;
1420
1.36k
                p2 = p1 + stripsize;
1421
1.36k
                pa = (alpha ? (p2 + stripsize) : NULL);
1422
1.36k
            }
1423
1.86k
        }
1424
0
        else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
1425
0
                                      p0, temp * scanline) == (tmsize_t)(-1) &&
1426
0
                 img->stoponerr)
1427
0
        {
1428
0
            ret = 0;
1429
0
            break;
1430
0
        }
1431
1.86k
        if (colorchannels > 1 &&
1432
1.36k
            TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1,
1433
1.36k
                                 temp * scanline) == (tmsize_t)(-1) &&
1434
1.09k
            img->stoponerr)
1435
0
        {
1436
0
            ret = 0;
1437
0
            break;
1438
0
        }
1439
1.86k
        if (colorchannels > 1 &&
1440
1.36k
            TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2,
1441
1.36k
                                 temp * scanline) == (tmsize_t)(-1) &&
1442
1.15k
            img->stoponerr)
1443
0
        {
1444
0
            ret = 0;
1445
0
            break;
1446
0
        }
1447
1.86k
        if (alpha)
1448
633
        {
1449
633
            if (TIFFReadEncodedStrip(
1450
633
                    tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa,
1451
633
                    temp * scanline) == (tmsize_t)(-1) &&
1452
370
                img->stoponerr)
1453
0
            {
1454
0
                ret = 0;
1455
0
                break;
1456
0
            }
1457
633
        }
1458
1459
        /* For SEPARATE the pos-offset is per sample and should not be
1460
         * multiplied by img->samplesperpixel. */
1461
1.86k
        pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) %
1462
1.86k
                         rowsperstrip) *
1463
1.86k
                  scanline +
1464
1.86k
              (tmsize_t)img->col_offset;
1465
1.86k
        tmsize_t roffset = (tmsize_t)y * w;
1466
1.86k
        (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
1467
1.86k
               p0 + pos, p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL));
1468
1.86k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1469
1.86k
                                                 : (int32_t)nrow);
1470
1.86k
    }
1471
1472
2.09k
    if (flip & FLIP_HORIZONTALLY)
1473
184
    {
1474
184
        uint32_t line;
1475
1476
26.1k
        for (line = 0; line < h; line++)
1477
26.0k
        {
1478
26.0k
            uint32_t *left = raster + (size_t)line * w;
1479
            /* Use wmin to only flip horizontally data in place and not complete
1480
             * raster-row. */
1481
26.0k
            uint32_t *right = left + wmin - 1;
1482
1483
10.4M
            while (left < right)
1484
10.4M
            {
1485
10.4M
                uint32_t temp = *left;
1486
10.4M
                *left = *right;
1487
10.4M
                *right = temp;
1488
10.4M
                left++;
1489
10.4M
                right--;
1490
10.4M
            }
1491
26.0k
        }
1492
184
    }
1493
1494
2.09k
    _TIFFfreeExt(img->tif, buf);
1495
2.09k
    return (ret);
1496
2.09k
}
1497
1498
/*
1499
 * The following routines move decoded data returned
1500
 * from the TIFF library into rasters filled with packed
1501
 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
1502
 *
1503
 * The routines have been created according to the most
1504
 * important cases and optimized.  PickContigCase and
1505
 * PickSeparateCase analyze the parameters and select
1506
 * the appropriate "get" and "put" routine to use.
1507
 */
1508
#define REPEAT8(op)                                                            \
1509
407M
    REPEAT4(op);                                                               \
1510
407M
    REPEAT4(op)
1511
#define REPEAT4(op)                                                            \
1512
815M
    REPEAT2(op);                                                               \
1513
815M
    REPEAT2(op)
1514
#define REPEAT2(op)                                                            \
1515
1.70G
    op;                                                                        \
1516
1.70G
    op
1517
#define CASE8(x, op)                                                           \
1518
18.7M
    switch (x)                                                                 \
1519
18.7M
    {                                                                          \
1520
799k
        case 7:                                                                \
1521
799k
            op; /*-fallthrough*/                                               \
1522
1.45M
        case 6:                                                                \
1523
1.45M
            op; /*-fallthrough*/                                               \
1524
2.04M
        case 5:                                                                \
1525
2.04M
            op; /*-fallthrough*/                                               \
1526
3.49M
        case 4:                                                                \
1527
3.49M
            op; /*-fallthrough*/                                               \
1528
5.95M
        case 3:                                                                \
1529
5.95M
            op; /*-fallthrough*/                                               \
1530
8.69M
        case 2:                                                                \
1531
8.69M
            op; /*-fallthrough*/                                               \
1532
18.7M
        case 1:                                                                \
1533
18.7M
            op;                                                                \
1534
18.7M
            break;                                                             \
1535
8.69M
        default:                                                               \
1536
0
            break;                                                             \
1537
18.7M
    }
1538
#define CASE4(x, op)                                                           \
1539
63.9k
    switch (x)                                                                 \
1540
63.9k
    {                                                                          \
1541
17.4k
        case 3:                                                                \
1542
17.4k
            op; /*-fallthrough*/                                               \
1543
24.4k
        case 2:                                                                \
1544
24.4k
            op; /*-fallthrough*/                                               \
1545
63.9k
        case 1:                                                                \
1546
63.9k
            op;                                                                \
1547
63.9k
            break;                                                             \
1548
24.4k
        default:                                                               \
1549
0
            break;                                                             \
1550
63.9k
    }
1551
#define NOP
1552
1553
#define UNROLL8(w, op1, op2)                                                   \
1554
24.0M
    {                                                                          \
1555
24.0M
        uint32_t _x;                                                           \
1556
431M
        for (_x = w; _x >= 8; _x -= 8)                                         \
1557
407M
        {                                                                      \
1558
407M
            op1;                                                               \
1559
407M
            REPEAT8(op2);                                                      \
1560
407M
        }                                                                      \
1561
24.0M
        if (_x > 0)                                                            \
1562
24.0M
        {                                                                      \
1563
18.7M
            op1;                                                               \
1564
18.7M
            CASE8(_x, op2);                                                    \
1565
18.7M
        }                                                                      \
1566
24.0M
    }
1567
#define UNROLL4(w, op1, op2)                                                   \
1568
104k
    {                                                                          \
1569
104k
        uint32_t _x;                                                           \
1570
1.30M
        for (_x = w; _x >= 4; _x -= 4)                                         \
1571
1.20M
        {                                                                      \
1572
1.20M
            op1;                                                               \
1573
1.20M
            REPEAT4(op2);                                                      \
1574
1.20M
        }                                                                      \
1575
104k
        if (_x > 0)                                                            \
1576
104k
        {                                                                      \
1577
63.9k
            op1;                                                               \
1578
63.9k
            CASE4(_x, op2);                                                    \
1579
63.9k
        }                                                                      \
1580
104k
    }
1581
#define UNROLL2(w, op1, op2)                                                   \
1582
223k
    {                                                                          \
1583
223k
        uint32_t _x;                                                           \
1584
68.6M
        for (_x = w; _x >= 2; _x -= 2)                                         \
1585
68.4M
        {                                                                      \
1586
68.4M
            op1;                                                               \
1587
68.4M
            REPEAT2(op2);                                                      \
1588
68.4M
        }                                                                      \
1589
223k
        if (_x)                                                                \
1590
223k
        {                                                                      \
1591
112k
            op1;                                                               \
1592
112k
            op2;                                                               \
1593
112k
        }                                                                      \
1594
223k
    }
1595
1596
#define SKEW(r, g, b, skew)                                                    \
1597
1.73M
    {                                                                          \
1598
1.73M
        r += skew;                                                             \
1599
1.73M
        g += skew;                                                             \
1600
1.73M
        b += skew;                                                             \
1601
1.73M
    }
1602
#define SKEW4(r, g, b, a, skew)                                                \
1603
131k
    {                                                                          \
1604
131k
        r += skew;                                                             \
1605
131k
        g += skew;                                                             \
1606
131k
        b += skew;                                                             \
1607
131k
        a += skew;                                                             \
1608
131k
    }
1609
1610
140M
#define A1 (((uint32_t)0xffL) << 24)
1611
#define PACK(r, g, b)                                                          \
1612
140M
    ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1)
1613
#define PACK4(r, g, b, a)                                                      \
1614
39.9M
    ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) |            \
1615
39.9M
     ((uint32_t)(a) << 24))
1616
1617
#define DECLAREContigPutFunc(name)                                             \
1618
    static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
1619
                     uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
1620
                     unsigned char *pp)
1621
1622
/*
1623
 * 8-bit palette => colormap/RGB
1624
 */
1625
DECLAREContigPutFunc(put8bitcmaptile)
1626
35
{
1627
35
    uint32_t **PALmap = img->PALmap;
1628
35
    int samplesperpixel = img->samplesperpixel;
1629
1630
35
    (void)y;
1631
3.60k
    for (; h > 0; --h)
1632
3.56k
    {
1633
1.34M
        for (x = w; x > 0; --x)
1634
1.33M
        {
1635
1.33M
            *cp++ = PALmap[*pp][0];
1636
1.33M
            pp += samplesperpixel;
1637
1.33M
        }
1638
3.56k
        cp += toskew;
1639
3.56k
        pp += fromskew;
1640
3.56k
    }
1641
35
}
1642
1643
/*
1644
 * 4-bit palette => colormap/RGB
1645
 */
1646
DECLAREContigPutFunc(put4bitcmaptile)
1647
88
{
1648
88
    uint32_t **PALmap = img->PALmap;
1649
1650
88
    (void)x;
1651
88
    (void)y;
1652
88
    fromskew /= 2;
1653
3.48k
    for (; h > 0; --h)
1654
3.39k
    {
1655
3.39k
        uint32_t *bw;
1656
3.39k
        UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
1657
3.39k
        cp += toskew;
1658
3.39k
        pp += fromskew;
1659
3.39k
    }
1660
88
}
1661
1662
/*
1663
 * 2-bit palette => colormap/RGB
1664
 */
1665
DECLAREContigPutFunc(put2bitcmaptile)
1666
213
{
1667
213
    uint32_t **PALmap = img->PALmap;
1668
1669
213
    (void)x;
1670
213
    (void)y;
1671
213
    fromskew /= 4;
1672
9.81k
    for (; h > 0; --h)
1673
9.60k
    {
1674
9.60k
        uint32_t *bw;
1675
9.60k
        UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
1676
9.60k
        cp += toskew;
1677
9.60k
        pp += fromskew;
1678
9.60k
    }
1679
213
}
1680
1681
/*
1682
 * 1-bit palette => colormap/RGB
1683
 */
1684
DECLAREContigPutFunc(put1bitcmaptile)
1685
717
{
1686
717
    uint32_t **PALmap = img->PALmap;
1687
1688
717
    (void)x;
1689
717
    (void)y;
1690
717
    fromskew /= 8;
1691
15.9k
    for (; h > 0; --h)
1692
15.2k
    {
1693
15.2k
        uint32_t *bw;
1694
15.2k
        UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
1695
15.2k
        cp += toskew;
1696
15.2k
        pp += fromskew;
1697
15.2k
    }
1698
717
}
1699
1700
/*
1701
 * 8-bit greyscale => colormap/RGB
1702
 */
1703
DECLAREContigPutFunc(putgreytile)
1704
5.90k
{
1705
5.90k
    int samplesperpixel = img->samplesperpixel;
1706
5.90k
    uint32_t **BWmap = img->BWmap;
1707
1708
5.90k
    (void)y;
1709
673k
    for (; h > 0; --h)
1710
667k
    {
1711
173M
        for (x = w; x > 0; --x)
1712
172M
        {
1713
172M
            *cp++ = BWmap[*pp][0];
1714
172M
            pp += samplesperpixel;
1715
172M
        }
1716
667k
        cp += toskew;
1717
667k
        pp += fromskew;
1718
667k
    }
1719
5.90k
}
1720
1721
/*
1722
 * 8-bit greyscale with associated alpha => colormap/RGBA
1723
 */
1724
DECLAREContigPutFunc(putagreytile)
1725
88
{
1726
88
    int samplesperpixel = img->samplesperpixel;
1727
88
    uint32_t **BWmap = img->BWmap;
1728
1729
88
    (void)y;
1730
8.80k
    for (; h > 0; --h)
1731
8.72k
    {
1732
311k
        for (x = w; x > 0; --x)
1733
303k
        {
1734
303k
            *cp++ = BWmap[*pp][0] & ((uint32_t)*(pp + 1) << 24 | ~A1);
1735
303k
            pp += samplesperpixel;
1736
303k
        }
1737
8.72k
        cp += toskew;
1738
8.72k
        pp += fromskew;
1739
8.72k
    }
1740
88
}
1741
1742
/*
1743
 * 16-bit greyscale => colormap/RGB
1744
 */
1745
DECLAREContigPutFunc(put16bitbwtile)
1746
752
{
1747
752
    int samplesperpixel = img->samplesperpixel;
1748
752
    uint32_t **BWmap = img->BWmap;
1749
    /* Convert pixel skew to byte skew (16-bit samples) */
1750
752
    const tmsize_t fromskewLocal =
1751
752
        (tmsize_t)fromskew * (tmsize_t)(2 * samplesperpixel);
1752
1753
752
    (void)y;
1754
233k
    for (; h > 0; --h)
1755
233k
    {
1756
233k
        uint16_t *wp = (uint16_t *)pp;
1757
1758
49.6M
        for (x = w; x > 0; --x)
1759
49.3M
        {
1760
            /* use high order byte of 16bit value */
1761
1762
49.3M
            *cp++ = BWmap[*wp >> 8][0];
1763
49.3M
            pp += 2 * samplesperpixel;
1764
49.3M
            wp += samplesperpixel;
1765
49.3M
        }
1766
233k
        cp += toskew;
1767
233k
        pp += fromskewLocal;
1768
233k
    }
1769
752
}
1770
1771
/*
1772
 * 1-bit bilevel => colormap/RGB
1773
 */
1774
DECLAREContigPutFunc(put1bitbwtile)
1775
1.34M
{
1776
1.34M
    uint32_t **BWmap = img->BWmap;
1777
1778
1.34M
    (void)x;
1779
1.34M
    (void)y;
1780
1.34M
    fromskew /= 8;
1781
21.8M
    for (; h > 0; --h)
1782
20.5M
    {
1783
20.5M
        uint32_t *bw;
1784
20.5M
        UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
1785
20.5M
        cp += toskew;
1786
20.5M
        pp += fromskew;
1787
20.5M
    }
1788
1.34M
}
1789
1790
/*
1791
 * 2-bit greyscale => colormap/RGB
1792
 */
1793
DECLAREContigPutFunc(put2bitbwtile)
1794
909
{
1795
909
    uint32_t **BWmap = img->BWmap;
1796
1797
909
    (void)x;
1798
909
    (void)y;
1799
909
    fromskew /= 4;
1800
96.2k
    for (; h > 0; --h)
1801
95.3k
    {
1802
95.3k
        uint32_t *bw;
1803
95.3k
        UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
1804
95.3k
        cp += toskew;
1805
95.3k
        pp += fromskew;
1806
95.3k
    }
1807
909
}
1808
1809
/*
1810
 * 4-bit greyscale => colormap/RGB
1811
 */
1812
DECLAREContigPutFunc(put4bitbwtile)
1813
1.16k
{
1814
1.16k
    uint32_t **BWmap = img->BWmap;
1815
1816
1.16k
    (void)x;
1817
1.16k
    (void)y;
1818
1.16k
    fromskew /= 2;
1819
221k
    for (; h > 0; --h)
1820
220k
    {
1821
220k
        uint32_t *bw;
1822
220k
        UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
1823
220k
        cp += toskew;
1824
220k
        pp += fromskew;
1825
220k
    }
1826
1.16k
}
1827
1828
/*
1829
 * 8-bit packed samples, no Map => RGB
1830
 */
1831
DECLAREContigPutFunc(putRGBcontig8bittile)
1832
35.4k
{
1833
35.4k
    int samplesperpixel = img->samplesperpixel;
1834
1835
35.4k
    (void)x;
1836
35.4k
    (void)y;
1837
35.4k
    const tmsize_t fromskewLocal =
1838
35.4k
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1839
1.49M
    for (; h > 0; --h)
1840
1.45M
    {
1841
1.45M
        UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]);
1842
1.45M
                pp += samplesperpixel);
1843
1.45M
        cp += toskew;
1844
1.45M
        pp += fromskewLocal;
1845
1.45M
    }
1846
35.4k
}
1847
1848
/*
1849
 * 8-bit packed samples => RGBA w/ associated alpha
1850
 * (known to have Map == NULL)
1851
 */
1852
DECLAREContigPutFunc(putRGBAAcontig8bittile)
1853
490
{
1854
490
    int samplesperpixel = img->samplesperpixel;
1855
1856
490
    (void)x;
1857
490
    (void)y;
1858
490
    const tmsize_t fromskewLocal =
1859
490
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1860
158k
    for (; h > 0; --h)
1861
158k
    {
1862
158k
        UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
1863
158k
                pp += samplesperpixel);
1864
158k
        cp += toskew;
1865
158k
        pp += fromskewLocal;
1866
158k
    }
1867
490
}
1868
1869
/*
1870
 * 8-bit packed samples => RGBA w/ unassociated alpha
1871
 * (known to have Map == NULL)
1872
 */
1873
DECLAREContigPutFunc(putRGBUAcontig8bittile)
1874
68
{
1875
68
    int samplesperpixel = img->samplesperpixel;
1876
68
    (void)y;
1877
68
    const tmsize_t fromskewLocal =
1878
68
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1879
6.56k
    for (; h > 0; --h)
1880
6.49k
    {
1881
6.49k
        uint32_t r, g, b, a;
1882
6.49k
        uint8_t *m;
1883
802k
        for (x = w; x > 0; --x)
1884
795k
        {
1885
795k
            a = pp[3];
1886
795k
            m = img->UaToAa + ((size_t)a << 8);
1887
795k
            r = m[pp[0]];
1888
795k
            g = m[pp[1]];
1889
795k
            b = m[pp[2]];
1890
795k
            *cp++ = PACK4(r, g, b, a);
1891
795k
            pp += samplesperpixel;
1892
795k
        }
1893
6.49k
        cp += toskew;
1894
6.49k
        pp += fromskewLocal;
1895
6.49k
    }
1896
68
}
1897
1898
/*
1899
 * 16-bit packed samples => RGB
1900
 */
1901
DECLAREContigPutFunc(putRGBcontig16bittile)
1902
0
{
1903
0
    int samplesperpixel = img->samplesperpixel;
1904
0
    uint16_t *wp = (uint16_t *)pp;
1905
0
    (void)y;
1906
0
    const tmsize_t fromskewLocal =
1907
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1908
0
    for (; h > 0; --h)
1909
0
    {
1910
0
        for (x = w; x > 0; --x)
1911
0
        {
1912
0
            *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
1913
0
                         img->Bitdepth16To8[wp[2]]);
1914
0
            wp += samplesperpixel;
1915
0
        }
1916
0
        cp += toskew;
1917
0
        wp += fromskewLocal;
1918
0
    }
1919
0
}
1920
1921
/*
1922
 * 16-bit packed samples => RGBA w/ associated alpha
1923
 * (known to have Map == NULL)
1924
 */
1925
DECLAREContigPutFunc(putRGBAAcontig16bittile)
1926
0
{
1927
0
    int samplesperpixel = img->samplesperpixel;
1928
0
    uint16_t *wp = (uint16_t *)pp;
1929
0
    (void)y;
1930
0
    const tmsize_t fromskewLocal =
1931
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1932
0
    for (; h > 0; --h)
1933
0
    {
1934
0
        for (x = w; x > 0; --x)
1935
0
        {
1936
0
            *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
1937
0
                          img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]);
1938
0
            wp += samplesperpixel;
1939
0
        }
1940
0
        cp += toskew;
1941
0
        wp += fromskewLocal;
1942
0
    }
1943
0
}
1944
1945
/*
1946
 * 16-bit packed samples => RGBA w/ unassociated alpha
1947
 * (known to have Map == NULL)
1948
 */
1949
DECLAREContigPutFunc(putRGBUAcontig16bittile)
1950
0
{
1951
0
    int samplesperpixel = img->samplesperpixel;
1952
0
    uint16_t *wp = (uint16_t *)pp;
1953
0
    (void)y;
1954
0
    const tmsize_t fromskewLocal =
1955
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1956
0
    for (; h > 0; --h)
1957
0
    {
1958
0
        uint32_t r, g, b, a;
1959
0
        uint8_t *m;
1960
0
        for (x = w; x > 0; --x)
1961
0
        {
1962
0
            a = img->Bitdepth16To8[wp[3]];
1963
0
            m = img->UaToAa + ((size_t)a << 8);
1964
0
            r = m[img->Bitdepth16To8[wp[0]]];
1965
0
            g = m[img->Bitdepth16To8[wp[1]]];
1966
0
            b = m[img->Bitdepth16To8[wp[2]]];
1967
0
            *cp++ = PACK4(r, g, b, a);
1968
0
            wp += samplesperpixel;
1969
0
        }
1970
0
        cp += toskew;
1971
0
        wp += fromskewLocal;
1972
0
    }
1973
0
}
1974
1975
/*
1976
 * 8-bit packed CMYK samples w/o Map => RGB
1977
 *
1978
 * NB: The conversion of CMYK->RGB is *very* crude.
1979
 */
1980
DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
1981
528
{
1982
528
    int samplesperpixel = img->samplesperpixel;
1983
528
    uint16_t r, g, b, k;
1984
1985
528
    (void)x;
1986
528
    (void)y;
1987
528
    const tmsize_t fromskewLocal =
1988
528
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1989
110k
    for (; h > 0; --h)
1990
110k
    {
1991
110k
        UNROLL8(w, NOP, k = (uint16_t)(255 - pp[3]);
1992
110k
                r = (uint16_t)((k * (uint16_t)(255 - pp[0])) / 255);
1993
110k
                g = (uint16_t)((k * (uint16_t)(255 - pp[1])) / 255);
1994
110k
                b = (uint16_t)((k * (uint16_t)(255 - pp[2])) / 255);
1995
110k
                *cp++ = PACK(r, g, b); pp += samplesperpixel);
1996
110k
        cp += toskew;
1997
110k
        pp += fromskewLocal;
1998
110k
    }
1999
528
}
2000
2001
/*
2002
 * 8-bit packed CMYK samples w/Map => RGB
2003
 *
2004
 * NB: The conversion of CMYK->RGB is *very* crude.
2005
 */
2006
DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
2007
0
{
2008
0
    int samplesperpixel = img->samplesperpixel;
2009
0
    TIFFRGBValue *Map = img->Map;
2010
0
    unsigned int r, g, b, k;
2011
2012
0
    (void)y;
2013
0
    const tmsize_t fromskewLocal =
2014
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
2015
0
    for (; h > 0; --h)
2016
0
    {
2017
0
        for (x = w; x > 0; --x)
2018
0
        {
2019
0
            k = 255U - pp[3];
2020
0
            r = (k * (255U - pp[0])) / 255U;
2021
0
            g = (k * (255U - pp[1])) / 255U;
2022
0
            b = (k * (255U - pp[2])) / 255U;
2023
0
            *cp++ = PACK(Map[r], Map[g], Map[b]);
2024
0
            pp += samplesperpixel;
2025
0
        }
2026
0
        pp += fromskewLocal;
2027
0
        cp += toskew;
2028
0
    }
2029
0
}
2030
2031
#define DECLARESepPutFunc(name)                                                \
2032
    static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
2033
                     uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
2034
                     unsigned char *r, unsigned char *g, unsigned char *b,     \
2035
                     unsigned char *a)
2036
2037
/*
2038
 * 8-bit unpacked samples => RGB
2039
 */
2040
DECLARESepPutFunc(putRGBseparate8bittile)
2041
39.6k
{
2042
39.6k
    (void)img;
2043
39.6k
    (void)x;
2044
39.6k
    (void)y;
2045
39.6k
    (void)a;
2046
1.66M
    for (; h > 0; --h)
2047
1.62M
    {
2048
1.62M
        UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
2049
1.62M
        SKEW(r, g, b, fromskew);
2050
1.62M
        cp += toskew;
2051
1.62M
    }
2052
39.6k
}
2053
2054
/*
2055
 * 8-bit unpacked samples => RGBA w/ associated alpha
2056
 */
2057
DECLARESepPutFunc(putRGBAAseparate8bittile)
2058
1.95k
{
2059
1.95k
    (void)img;
2060
1.95k
    (void)x;
2061
1.95k
    (void)y;
2062
106k
    for (; h > 0; --h)
2063
105k
    {
2064
105k
        UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
2065
105k
        SKEW4(r, g, b, a, fromskew);
2066
105k
        cp += toskew;
2067
105k
    }
2068
1.95k
}
2069
2070
/*
2071
 * 8-bit unpacked CMYK samples => RGBA
2072
 */
2073
DECLARESepPutFunc(putCMYKseparate8bittile)
2074
87
{
2075
87
    (void)img;
2076
87
    (void)y;
2077
24.5k
    for (; h > 0; --h)
2078
24.4k
    {
2079
24.4k
        unsigned int rv, gv, bv, kv;
2080
39.2M
        for (x = w; x > 0; --x)
2081
39.1M
        {
2082
39.1M
            kv = 255U - *a++;
2083
39.1M
            rv = (kv * (255U - *r++)) / 255U;
2084
39.1M
            gv = (kv * (255U - *g++)) / 255U;
2085
39.1M
            bv = (kv * (255U - *b++)) / 255U;
2086
39.1M
            *cp++ = PACK4(rv, gv, bv, 255);
2087
39.1M
        }
2088
24.4k
        SKEW4(r, g, b, a, fromskew);
2089
24.4k
        cp += toskew;
2090
24.4k
    }
2091
87
}
2092
2093
/*
2094
 * 8-bit unpacked samples => RGBA w/ unassociated alpha
2095
 */
2096
DECLARESepPutFunc(putRGBUAseparate8bittile)
2097
57
{
2098
57
    (void)img;
2099
57
    (void)y;
2100
1.82k
    for (; h > 0; --h)
2101
1.76k
    {
2102
1.76k
        uint32_t rv, gv, bv, av;
2103
1.76k
        uint8_t *m;
2104
8.63k
        for (x = w; x > 0; --x)
2105
6.86k
        {
2106
6.86k
            av = *a++;
2107
6.86k
            m = img->UaToAa + ((size_t)av << 8);
2108
6.86k
            rv = m[*r++];
2109
6.86k
            gv = m[*g++];
2110
6.86k
            bv = m[*b++];
2111
6.86k
            *cp++ = PACK4(rv, gv, bv, av);
2112
6.86k
        }
2113
1.76k
        SKEW4(r, g, b, a, fromskew);
2114
1.76k
        cp += toskew;
2115
1.76k
    }
2116
57
}
2117
2118
/*
2119
 * 16-bit unpacked samples => RGB
2120
 */
2121
DECLARESepPutFunc(putRGBseparate16bittile)
2122
0
{
2123
0
    uint16_t *wr = (uint16_t *)r;
2124
0
    uint16_t *wg = (uint16_t *)g;
2125
0
    uint16_t *wb = (uint16_t *)b;
2126
0
    (void)img;
2127
0
    (void)y;
2128
0
    (void)a;
2129
0
    for (; h > 0; --h)
2130
0
    {
2131
0
        for (x = 0; x < w; x++)
2132
0
            *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
2133
0
                         img->Bitdepth16To8[*wb++]);
2134
0
        SKEW(wr, wg, wb, fromskew);
2135
0
        cp += toskew;
2136
0
    }
2137
0
}
2138
2139
/*
2140
 * 16-bit unpacked samples => RGBA w/ associated alpha
2141
 */
2142
DECLARESepPutFunc(putRGBAAseparate16bittile)
2143
0
{
2144
0
    uint16_t *wr = (uint16_t *)r;
2145
0
    uint16_t *wg = (uint16_t *)g;
2146
0
    uint16_t *wb = (uint16_t *)b;
2147
0
    uint16_t *wa = (uint16_t *)a;
2148
0
    (void)img;
2149
0
    (void)y;
2150
0
    for (; h > 0; --h)
2151
0
    {
2152
0
        for (x = 0; x < w; x++)
2153
0
            *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
2154
0
                          img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]);
2155
0
        SKEW4(wr, wg, wb, wa, fromskew);
2156
0
        cp += toskew;
2157
0
    }
2158
0
}
2159
2160
/*
2161
 * 16-bit unpacked samples => RGBA w/ unassociated alpha
2162
 */
2163
DECLARESepPutFunc(putRGBUAseparate16bittile)
2164
0
{
2165
0
    uint16_t *wr = (uint16_t *)r;
2166
0
    uint16_t *wg = (uint16_t *)g;
2167
0
    uint16_t *wb = (uint16_t *)b;
2168
0
    uint16_t *wa = (uint16_t *)a;
2169
0
    (void)img;
2170
0
    (void)y;
2171
0
    for (; h > 0; --h)
2172
0
    {
2173
0
        uint32_t r2, g2, b2, a2;
2174
0
        uint8_t *m;
2175
0
        for (x = w; x > 0; --x)
2176
0
        {
2177
0
            a2 = img->Bitdepth16To8[*wa++];
2178
0
            m = img->UaToAa + ((size_t)a2 << 8);
2179
0
            r2 = m[img->Bitdepth16To8[*wr++]];
2180
0
            g2 = m[img->Bitdepth16To8[*wg++]];
2181
0
            b2 = m[img->Bitdepth16To8[*wb++]];
2182
0
            *cp++ = PACK4(r2, g2, b2, a2);
2183
0
        }
2184
0
        SKEW4(wr, wg, wb, wa, fromskew);
2185
0
        cp += toskew;
2186
0
    }
2187
0
}
2188
2189
/*
2190
 * 8-bit packed CIE L*a*b 1976 samples => RGB
2191
 */
2192
DECLAREContigPutFunc(putcontig8bitCIELab8)
2193
884
{
2194
884
    float X, Y, Z;
2195
884
    uint32_t r, g, b;
2196
884
    (void)y;
2197
884
    const tmsize_t fromskewLocal = (tmsize_t)fromskew * (tmsize_t)3;
2198
56.7k
    for (; h > 0; --h)
2199
55.8k
    {
2200
5.72M
        for (x = w; x > 0; --x)
2201
5.66M
        {
2202
5.66M
            TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0],
2203
5.66M
                            (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z);
2204
5.66M
            TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
2205
5.66M
            *cp++ = PACK(r, g, b);
2206
5.66M
            pp += 3;
2207
5.66M
        }
2208
55.8k
        cp += toskew;
2209
55.8k
        pp += fromskewLocal;
2210
55.8k
    }
2211
884
}
2212
2213
/*
2214
 * 16-bit packed CIE L*a*b 1976 samples => RGB
2215
 */
2216
DECLAREContigPutFunc(putcontig8bitCIELab16)
2217
0
{
2218
0
    float X, Y, Z;
2219
0
    uint32_t r, g, b;
2220
0
    uint16_t *wp = (uint16_t *)pp;
2221
0
    (void)y;
2222
0
    const tmsize_t fromskewLocal = (tmsize_t)fromskew * (tmsize_t)3;
2223
0
    for (; h > 0; --h)
2224
0
    {
2225
0
        for (x = w; x > 0; --x)
2226
0
        {
2227
0
            TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1],
2228
0
                              (int16_t)wp[2], &X, &Y, &Z);
2229
0
            TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
2230
0
            *cp++ = PACK(r, g, b);
2231
0
            wp += 3;
2232
0
        }
2233
0
        cp += toskew;
2234
0
        wp += fromskewLocal;
2235
0
    }
2236
0
}
2237
2238
/*
2239
 * YCbCr -> RGB conversion and packing routines.
2240
 */
2241
2242
#define YCbCrtoRGB(dst, Y)                                                     \
2243
90.0M
    {                                                                          \
2244
90.0M
        uint32_t r, g, b;                                                      \
2245
90.0M
        TIFFYCbCrtoRGB(img->ycbcr, (Y), (int32_t)Cb, (int32_t)Cr, &r, &g, &b); \
2246
90.0M
        dst = PACK(r, g, b);                                                   \
2247
90.0M
    }
2248
2249
/*
2250
 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
2251
 */
2252
DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
2253
175
{
2254
175
    uint32_t *cp1 = cp + w + toskew;
2255
175
    uint32_t *cp2 = cp1 + w + toskew;
2256
175
    uint32_t *cp3 = cp2 + w + toskew;
2257
175
    const tmsize_t incr = 3 * (tmsize_t)w + 4 * (tmsize_t)toskew;
2258
2259
175
    (void)y;
2260
    /* adjust fromskew */
2261
175
    const tmsize_t fromskewLocal =
2262
175
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 4 + 2);
2263
175
    if ((h & 3) == 0 && (w & 3) == 0)
2264
34
    {
2265
1.29k
        for (; h >= 4; h -= 4)
2266
1.25k
        {
2267
1.25k
            x = w >> 2;
2268
1.25k
            do
2269
29.1k
            {
2270
29.1k
                int32_t Cb = pp[16];
2271
29.1k
                int32_t Cr = pp[17];
2272
2273
29.1k
                YCbCrtoRGB(cp[0], pp[0]);
2274
29.1k
                YCbCrtoRGB(cp[1], pp[1]);
2275
29.1k
                YCbCrtoRGB(cp[2], pp[2]);
2276
29.1k
                YCbCrtoRGB(cp[3], pp[3]);
2277
29.1k
                YCbCrtoRGB(cp1[0], pp[4]);
2278
29.1k
                YCbCrtoRGB(cp1[1], pp[5]);
2279
29.1k
                YCbCrtoRGB(cp1[2], pp[6]);
2280
29.1k
                YCbCrtoRGB(cp1[3], pp[7]);
2281
29.1k
                YCbCrtoRGB(cp2[0], pp[8]);
2282
29.1k
                YCbCrtoRGB(cp2[1], pp[9]);
2283
29.1k
                YCbCrtoRGB(cp2[2], pp[10]);
2284
29.1k
                YCbCrtoRGB(cp2[3], pp[11]);
2285
29.1k
                YCbCrtoRGB(cp3[0], pp[12]);
2286
29.1k
                YCbCrtoRGB(cp3[1], pp[13]);
2287
29.1k
                YCbCrtoRGB(cp3[2], pp[14]);
2288
29.1k
                YCbCrtoRGB(cp3[3], pp[15]);
2289
2290
29.1k
                cp += 4;
2291
29.1k
                cp1 += 4;
2292
29.1k
                cp2 += 4;
2293
29.1k
                cp3 += 4;
2294
29.1k
                pp += 18;
2295
29.1k
            } while (--x);
2296
1.25k
            cp += incr;
2297
1.25k
            cp1 += incr;
2298
1.25k
            cp2 += incr;
2299
1.25k
            cp3 += incr;
2300
1.25k
            pp += fromskewLocal;
2301
1.25k
        }
2302
34
    }
2303
141
    else
2304
141
    {
2305
5.44k
        while (h > 0)
2306
5.44k
        {
2307
187k
            for (x = w; x > 0;)
2308
182k
            {
2309
182k
                int32_t Cb = pp[16];
2310
182k
                int32_t Cr = pp[17];
2311
182k
                switch (x)
2312
182k
                {
2313
177k
                    default:
2314
177k
                        switch (h)
2315
177k
                        {
2316
173k
                            default:
2317
173k
                                YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
2318
174k
                            case 3:
2319
174k
                                YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
2320
175k
                            case 2:
2321
175k
                                YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
2322
177k
                            case 1:
2323
177k
                                YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
2324
177k
                        } /* FALLTHROUGH */
2325
178k
                    case 3:
2326
178k
                        switch (h)
2327
178k
                        {
2328
174k
                            default:
2329
174k
                                YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
2330
175k
                            case 3:
2331
175k
                                YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
2332
176k
                            case 2:
2333
176k
                                YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
2334
178k
                            case 1:
2335
178k
                                YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
2336
178k
                        } /* FALLTHROUGH */
2337
180k
                    case 2:
2338
180k
                        switch (h)
2339
180k
                        {
2340
176k
                            default:
2341
176k
                                YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
2342
177k
                            case 3:
2343
177k
                                YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */
2344
178k
                            case 2:
2345
178k
                                YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
2346
180k
                            case 1:
2347
180k
                                YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
2348
180k
                        } /* FALLTHROUGH */
2349
182k
                    case 1:
2350
182k
                        switch (h)
2351
182k
                        {
2352
178k
                            default:
2353
178k
                                YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
2354
179k
                            case 3:
2355
179k
                                YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */
2356
180k
                            case 2:
2357
180k
                                YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
2358
182k
                            case 1:
2359
182k
                                YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
2360
182k
                        } /* FALLTHROUGH */
2361
182k
                }
2362
182k
                if (x < 4)
2363
4.51k
                {
2364
4.51k
                    cp += x;
2365
4.51k
                    cp1 += x;
2366
4.51k
                    cp2 += x;
2367
4.51k
                    cp3 += x;
2368
4.51k
                    x = 0;
2369
4.51k
                }
2370
177k
                else
2371
177k
                {
2372
177k
                    cp += 4;
2373
177k
                    cp1 += 4;
2374
177k
                    cp2 += 4;
2375
177k
                    cp3 += 4;
2376
177k
                    x -= 4;
2377
177k
                }
2378
182k
                pp += 18;
2379
182k
            }
2380
5.44k
            if (h <= 4)
2381
141
                break;
2382
5.30k
            h -= 4;
2383
5.30k
            cp += incr;
2384
5.30k
            cp1 += incr;
2385
5.30k
            cp2 += incr;
2386
5.30k
            cp3 += incr;
2387
5.30k
            pp += fromskewLocal;
2388
5.30k
        }
2389
141
    }
2390
175
}
2391
2392
/*
2393
 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
2394
 */
2395
DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
2396
124
{
2397
124
    uint32_t *cp1 = cp + w + toskew;
2398
124
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2399
2400
124
    (void)y;
2401
124
    const tmsize_t fromskewLocal =
2402
124
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 2 + 2);
2403
124
    if ((w & 3) == 0 && (h & 1) == 0)
2404
37
    {
2405
3.76k
        for (; h >= 2; h -= 2)
2406
3.72k
        {
2407
3.72k
            x = w >> 2;
2408
3.72k
            do
2409
162k
            {
2410
162k
                int32_t Cb = pp[8];
2411
162k
                int32_t Cr = pp[9];
2412
2413
162k
                YCbCrtoRGB(cp[0], pp[0]);
2414
162k
                YCbCrtoRGB(cp[1], pp[1]);
2415
162k
                YCbCrtoRGB(cp[2], pp[2]);
2416
162k
                YCbCrtoRGB(cp[3], pp[3]);
2417
162k
                YCbCrtoRGB(cp1[0], pp[4]);
2418
162k
                YCbCrtoRGB(cp1[1], pp[5]);
2419
162k
                YCbCrtoRGB(cp1[2], pp[6]);
2420
162k
                YCbCrtoRGB(cp1[3], pp[7]);
2421
2422
162k
                cp += 4;
2423
162k
                cp1 += 4;
2424
162k
                pp += 10;
2425
162k
            } while (--x);
2426
3.72k
            cp += incr;
2427
3.72k
            cp1 += incr;
2428
3.72k
            pp += fromskewLocal;
2429
3.72k
        }
2430
37
    }
2431
87
    else
2432
87
    {
2433
7.80k
        while (h > 0)
2434
7.80k
        {
2435
543k
            for (x = w; x > 0;)
2436
535k
            {
2437
535k
                int32_t Cb = pp[8];
2438
535k
                int32_t Cr = pp[9];
2439
535k
                switch (x)
2440
535k
                {
2441
527k
                    default:
2442
527k
                        switch (h)
2443
527k
                        {
2444
526k
                            default:
2445
526k
                                YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
2446
527k
                            case 1:
2447
527k
                                YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
2448
527k
                        } /* FALLTHROUGH */
2449
530k
                    case 3:
2450
530k
                        switch (h)
2451
530k
                        {
2452
529k
                            default:
2453
529k
                                YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
2454
530k
                            case 1:
2455
530k
                                YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
2456
530k
                        } /* FALLTHROUGH */
2457
532k
                    case 2:
2458
532k
                        switch (h)
2459
532k
                        {
2460
531k
                            default:
2461
531k
                                YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
2462
532k
                            case 1:
2463
532k
                                YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
2464
532k
                        } /* FALLTHROUGH */
2465
535k
                    case 1:
2466
535k
                        switch (h)
2467
535k
                        {
2468
534k
                            default:
2469
534k
                                YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
2470
535k
                            case 1:
2471
535k
                                YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
2472
535k
                        } /* FALLTHROUGH */
2473
535k
                }
2474
535k
                if (x < 4)
2475
7.64k
                {
2476
7.64k
                    cp += x;
2477
7.64k
                    cp1 += x;
2478
7.64k
                    x = 0;
2479
7.64k
                }
2480
527k
                else
2481
527k
                {
2482
527k
                    cp += 4;
2483
527k
                    cp1 += 4;
2484
527k
                    x -= 4;
2485
527k
                }
2486
535k
                pp += 10;
2487
535k
            }
2488
7.80k
            if (h <= 2)
2489
87
                break;
2490
7.71k
            h -= 2;
2491
7.71k
            cp += incr;
2492
7.71k
            cp1 += incr;
2493
7.71k
            pp += fromskewLocal;
2494
7.71k
        }
2495
87
    }
2496
124
}
2497
2498
/*
2499
 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
2500
 */
2501
DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
2502
90
{
2503
90
    (void)y;
2504
90
    const tmsize_t fromskewLocal =
2505
90
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 1 + 2);
2506
90
    do
2507
4.29k
    {
2508
4.29k
        x = w >> 2;
2509
103k
        while (x > 0)
2510
98.9k
        {
2511
98.9k
            int32_t Cb = pp[4];
2512
98.9k
            int32_t Cr = pp[5];
2513
2514
98.9k
            YCbCrtoRGB(cp[0], pp[0]);
2515
98.9k
            YCbCrtoRGB(cp[1], pp[1]);
2516
98.9k
            YCbCrtoRGB(cp[2], pp[2]);
2517
98.9k
            YCbCrtoRGB(cp[3], pp[3]);
2518
2519
98.9k
            cp += 4;
2520
98.9k
            pp += 6;
2521
98.9k
            x--;
2522
98.9k
        }
2523
2524
4.29k
        if ((w & 3) != 0)
2525
3.02k
        {
2526
3.02k
            int32_t Cb = pp[4];
2527
3.02k
            int32_t Cr = pp[5];
2528
2529
3.02k
            switch ((w & 3))
2530
3.02k
            {
2531
849
                case 3:
2532
849
                    YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/
2533
1.77k
                case 2:
2534
1.77k
                    YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/
2535
3.02k
                case 1:
2536
3.02k
                    YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/
2537
3.02k
                case 0:
2538
3.02k
                    break;
2539
0
                default:
2540
0
                    break;
2541
3.02k
            }
2542
2543
3.02k
            cp += (w & 3);
2544
3.02k
            pp += 6;
2545
3.02k
        }
2546
2547
4.29k
        cp += toskew;
2548
4.29k
        pp += fromskewLocal;
2549
4.29k
    } while (--h);
2550
90
}
2551
2552
/*
2553
 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
2554
 */
2555
DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
2556
1.22k
{
2557
1.22k
    uint32_t *cp2;
2558
1.22k
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2559
1.22k
    (void)y;
2560
1.22k
    const tmsize_t fromskewLocal =
2561
1.22k
        (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 2 + 2);
2562
1.22k
    cp2 = cp + w + toskew;
2563
108k
    while (h >= 2)
2564
107k
    {
2565
107k
        x = w;
2566
20.0M
        while (x >= 2)
2567
19.8M
        {
2568
19.8M
            uint32_t Cb = pp[4];
2569
19.8M
            uint32_t Cr = pp[5];
2570
19.8M
            YCbCrtoRGB(cp[0], pp[0]);
2571
19.8M
            YCbCrtoRGB(cp[1], pp[1]);
2572
19.8M
            YCbCrtoRGB(cp2[0], pp[2]);
2573
19.8M
            YCbCrtoRGB(cp2[1], pp[3]);
2574
19.8M
            cp += 2;
2575
19.8M
            cp2 += 2;
2576
19.8M
            pp += 6;
2577
19.8M
            x -= 2;
2578
19.8M
        }
2579
107k
        if (x == 1)
2580
43.3k
        {
2581
43.3k
            uint32_t Cb = pp[4];
2582
43.3k
            uint32_t Cr = pp[5];
2583
43.3k
            YCbCrtoRGB(cp[0], pp[0]);
2584
43.3k
            YCbCrtoRGB(cp2[0], pp[2]);
2585
43.3k
            cp++;
2586
43.3k
            cp2++;
2587
43.3k
            pp += 6;
2588
43.3k
        }
2589
107k
        cp += incr;
2590
107k
        cp2 += incr;
2591
107k
        pp += fromskewLocal;
2592
107k
        h -= 2;
2593
107k
    }
2594
1.22k
    if (h == 1)
2595
228
    {
2596
228
        x = w;
2597
32.3k
        while (x >= 2)
2598
32.1k
        {
2599
32.1k
            uint32_t Cb = pp[4];
2600
32.1k
            uint32_t Cr = pp[5];
2601
32.1k
            YCbCrtoRGB(cp[0], pp[0]);
2602
32.1k
            YCbCrtoRGB(cp[1], pp[1]);
2603
32.1k
            cp += 2;
2604
32.1k
            cp2 += 2;
2605
32.1k
            pp += 6;
2606
32.1k
            x -= 2;
2607
32.1k
        }
2608
228
        if (x == 1)
2609
80
        {
2610
80
            uint32_t Cb = pp[4];
2611
80
            uint32_t Cr = pp[5];
2612
80
            YCbCrtoRGB(cp[0], pp[0]);
2613
80
        }
2614
228
    }
2615
1.22k
}
2616
2617
/*
2618
 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
2619
 */
2620
DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
2621
54
{
2622
54
    (void)y;
2623
54
    const tmsize_t fromskewLocal =
2624
54
        (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 1 + 2);
2625
54
    do
2626
2.54k
    {
2627
2.54k
        x = w >> 1;
2628
59.5k
        while (x > 0)
2629
56.9k
        {
2630
56.9k
            int32_t Cb = pp[2];
2631
56.9k
            int32_t Cr = pp[3];
2632
2633
56.9k
            YCbCrtoRGB(cp[0], pp[0]);
2634
56.9k
            YCbCrtoRGB(cp[1], pp[1]);
2635
2636
56.9k
            cp += 2;
2637
56.9k
            pp += 4;
2638
56.9k
            x--;
2639
56.9k
        }
2640
2641
2.54k
        if ((w & 1) != 0)
2642
1.63k
        {
2643
1.63k
            int32_t Cb = pp[2];
2644
1.63k
            int32_t Cr = pp[3];
2645
2646
1.63k
            YCbCrtoRGB(cp[0], pp[0]);
2647
2648
1.63k
            cp += 1;
2649
1.63k
            pp += 4;
2650
1.63k
        }
2651
2652
2.54k
        cp += toskew;
2653
2.54k
        pp += fromskewLocal;
2654
2.54k
    } while (--h);
2655
54
}
2656
2657
/*
2658
 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
2659
 */
2660
DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
2661
38
{
2662
38
    uint32_t *cp2;
2663
38
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2664
38
    (void)y;
2665
38
    const tmsize_t fromskewLocal =
2666
38
        (tmsize_t)(fromskew / 1) * (tmsize_t)(1 * 2 + 2);
2667
38
    cp2 = cp + w + toskew;
2668
1.14k
    while (h >= 2)
2669
1.10k
    {
2670
1.10k
        x = w;
2671
1.10k
        do
2672
342k
        {
2673
342k
            uint32_t Cb = pp[2];
2674
342k
            uint32_t Cr = pp[3];
2675
342k
            YCbCrtoRGB(cp[0], pp[0]);
2676
342k
            YCbCrtoRGB(cp2[0], pp[1]);
2677
342k
            cp++;
2678
342k
            cp2++;
2679
342k
            pp += 4;
2680
342k
        } while (--x);
2681
1.10k
        cp += incr;
2682
1.10k
        cp2 += incr;
2683
1.10k
        pp += fromskewLocal;
2684
1.10k
        h -= 2;
2685
1.10k
    }
2686
38
    if (h == 1)
2687
29
    {
2688
29
        x = w;
2689
29
        do
2690
829
        {
2691
829
            uint32_t Cb = pp[2];
2692
829
            uint32_t Cr = pp[3];
2693
829
            YCbCrtoRGB(cp[0], pp[0]);
2694
829
            cp++;
2695
829
            pp += 4;
2696
829
        } while (--x);
2697
29
    }
2698
38
}
2699
2700
/*
2701
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2702
 */
2703
DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
2704
35
{
2705
35
    (void)y;
2706
35
    const tmsize_t fromskewLocal =
2707
35
        (tmsize_t)(fromskew / 1) * (tmsize_t)(1 * 1 + 2);
2708
35
    do
2709
1.23k
    {
2710
1.23k
        x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
2711
1.23k
        do
2712
250k
        {
2713
250k
            int32_t Cb = pp[1];
2714
250k
            int32_t Cr = pp[2];
2715
2716
250k
            YCbCrtoRGB(*cp++, pp[0]);
2717
2718
250k
            pp += 3;
2719
250k
        } while (--x);
2720
1.23k
        cp += toskew;
2721
1.23k
        pp += fromskewLocal;
2722
1.23k
    } while (--h);
2723
35
}
2724
2725
/*
2726
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2727
 */
2728
DECLARESepPutFunc(putseparate8bitYCbCr11tile)
2729
566
{
2730
566
    (void)y;
2731
566
    (void)a;
2732
    /* TODO: naming of input vars is still off, change obfuscating declaration
2733
     * inside define, or resolve obfuscation */
2734
112k
    for (; h > 0; --h)
2735
111k
    {
2736
111k
        x = w;
2737
111k
        do
2738
26.9M
        {
2739
26.9M
            uint32_t dr, dg, db;
2740
26.9M
            TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db);
2741
26.9M
            *cp++ = PACK(dr, dg, db);
2742
26.9M
        } while (--x);
2743
111k
        SKEW(r, g, b, fromskew);
2744
111k
        cp += toskew;
2745
111k
    }
2746
566
}
2747
#undef YCbCrtoRGB
2748
2749
static int isInRefBlackWhiteRange(float f)
2750
26.0k
{
2751
26.0k
    return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF;
2752
26.0k
}
2753
2754
static int initYCbCrConversion(TIFFRGBAImage *img)
2755
4.36k
{
2756
4.36k
    static const char module[] = "initYCbCrConversion";
2757
2758
4.36k
    float *luma, *refBlackWhite;
2759
2760
4.36k
    if (img->ycbcr == NULL)
2761
4.36k
    {
2762
4.36k
        img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt(
2763
4.36k
            img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) +
2764
4.36k
                          4 * 256 * sizeof(TIFFRGBValue) +
2765
4.36k
                          2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t));
2766
4.36k
        if (img->ycbcr == NULL)
2767
0
        {
2768
0
            TIFFErrorExtR(img->tif, module,
2769
0
                          "No space for YCbCr->RGB conversion state");
2770
0
            return (0);
2771
0
        }
2772
4.36k
    }
2773
2774
4.36k
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
2775
4.36k
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
2776
4.36k
                          &refBlackWhite);
2777
2778
    /* Do some validation to avoid later issues. Detect NaN for now */
2779
    /* and also if lumaGreen is zero since we divide by it later */
2780
4.36k
    if (isnan(luma[0]) || isnan(luma[1]) || TIFF_FLOAT_EQ(luma[1], 0.0f) ||
2781
4.36k
        isnan(luma[2]))
2782
17
    {
2783
17
        TIFFErrorExtR(img->tif, module,
2784
17
                      "Invalid values for YCbCrCoefficients tag");
2785
17
        return (0);
2786
17
    }
2787
2788
4.34k
    if (!isInRefBlackWhiteRange(refBlackWhite[0]) ||
2789
4.34k
        !isInRefBlackWhiteRange(refBlackWhite[1]) ||
2790
4.33k
        !isInRefBlackWhiteRange(refBlackWhite[2]) ||
2791
4.33k
        !isInRefBlackWhiteRange(refBlackWhite[3]) ||
2792
4.33k
        !isInRefBlackWhiteRange(refBlackWhite[4]) ||
2793
4.32k
        !isInRefBlackWhiteRange(refBlackWhite[5]))
2794
25
    {
2795
25
        TIFFErrorExtR(img->tif, module,
2796
25
                      "Invalid values for ReferenceBlackWhite tag");
2797
25
        return (0);
2798
25
    }
2799
2800
4.32k
    if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
2801
0
        return (0);
2802
4.32k
    return (1);
2803
4.32k
}
2804
2805
static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img)
2806
246
{
2807
246
    static const char module[] = "initCIELabConversion";
2808
2809
246
    float *whitePoint;
2810
246
    float refWhite[3];
2811
2812
246
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
2813
246
    if (TIFF_FLOAT_EQ(whitePoint[1], 0.0f))
2814
2
    {
2815
2
        TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag.");
2816
2
        return NULL;
2817
2
    }
2818
2819
244
    if (!img->cielab)
2820
244
    {
2821
244
        img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt(
2822
244
            img->tif, sizeof(TIFFCIELabToRGB));
2823
244
        if (!img->cielab)
2824
0
        {
2825
0
            TIFFErrorExtR(img->tif, module,
2826
0
                          "No space for CIE L*a*b*->RGB conversion state.");
2827
0
            return NULL;
2828
0
        }
2829
244
    }
2830
2831
244
    refWhite[1] = 100.0f;
2832
244
    refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
2833
244
    refWhite[2] =
2834
244
        (1.0f - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];
2835
244
    if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0)
2836
0
    {
2837
0
        TIFFErrorExtR(img->tif, module,
2838
0
                      "Failed to initialize CIE L*a*b*->RGB conversion state.");
2839
0
        _TIFFfreeExt(img->tif, img->cielab);
2840
0
        return NULL;
2841
0
    }
2842
2843
244
    if (img->bitspersample == 8)
2844
240
        return putcontig8bitCIELab8;
2845
4
    else if (img->bitspersample == 16)
2846
4
        return putcontig8bitCIELab16;
2847
0
    return NULL;
2848
244
}
2849
2850
/*
2851
 * Greyscale images with less than 8 bits/sample are handled
2852
 * with a table to avoid lots of shifts and masks.  The table
2853
 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
2854
 * pixel values simply by indexing into the table with one
2855
 * number.
2856
 */
2857
static int makebwmap(TIFFRGBAImage *img)
2858
11.2k
{
2859
11.2k
    TIFFRGBValue *Map = img->Map;
2860
11.2k
    int bitspersample = img->bitspersample;
2861
11.2k
    int nsamples = 8 / bitspersample;
2862
11.2k
    int i;
2863
11.2k
    uint32_t *p;
2864
2865
11.2k
    if (nsamples == 0)
2866
238
        nsamples = 1;
2867
2868
11.2k
    img->BWmap = (uint32_t **)_TIFFmallocExt(
2869
11.2k
        img->tif,
2870
11.2k
        (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
2871
11.2k
                   ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
2872
11.2k
    if (img->BWmap == NULL)
2873
0
    {
2874
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
2875
0
                      "No space for B&W mapping table");
2876
0
        return (0);
2877
0
    }
2878
11.2k
    p = (uint32_t *)(img->BWmap + 256);
2879
2.90M
    for (i = 0; i < 256; i++)
2880
2.89M
    {
2881
2.89M
        TIFFRGBValue c;
2882
2.89M
        img->BWmap[i] = p;
2883
2.89M
        switch (bitspersample)
2884
2.89M
        {
2885
0
#define GREY(x)                                                                \
2886
16.6M
    c = Map[x];                                                                \
2887
16.6M
    *p++ = PACK(c, c, c);
2888
1.90M
            case 1:
2889
1.90M
                GREY(i >> 7);
2890
1.90M
                GREY((i >> 6) & 1);
2891
1.90M
                GREY((i >> 5) & 1);
2892
1.90M
                GREY((i >> 4) & 1);
2893
1.90M
                GREY((i >> 3) & 1);
2894
1.90M
                GREY((i >> 2) & 1);
2895
1.90M
                GREY((i >> 1) & 1);
2896
1.90M
                GREY(i & 1);
2897
1.90M
                break;
2898
80.6k
            case 2:
2899
80.6k
                GREY(i >> 6);
2900
80.6k
                GREY((i >> 4) & 3);
2901
80.6k
                GREY((i >> 2) & 3);
2902
80.6k
                GREY(i & 3);
2903
80.6k
                break;
2904
200k
            case 4:
2905
200k
                GREY(i >> 4);
2906
200k
                GREY(i & 0xf);
2907
200k
                break;
2908
647k
            case 8:
2909
708k
            case 16:
2910
708k
                GREY(i);
2911
708k
                break;
2912
0
            default:
2913
0
                break;
2914
2.89M
        }
2915
2.89M
#undef GREY
2916
2.89M
    }
2917
11.2k
    return (1);
2918
11.2k
}
2919
2920
/*
2921
 * Construct a mapping table to convert from the range
2922
 * of the data samples to [0,255] --for display.  This
2923
 * process also handles inverting B&W images when needed.
2924
 */
2925
static int setupMap(TIFFRGBAImage *img)
2926
11.3k
{
2927
11.3k
    int32_t x, range;
2928
2929
11.3k
    range = (int32_t)((1U << img->bitspersample) - 1U);
2930
2931
    /* treat 16 bit the same as eight bit */
2932
11.3k
    if (img->bitspersample == 16)
2933
238
        range = 255;
2934
2935
11.3k
    img->Map = (TIFFRGBValue *)_TIFFmallocExt(
2936
11.3k
        img->tif, (tmsize_t)((size_t)(range + 1) * sizeof(TIFFRGBValue)));
2937
11.3k
    if (img->Map == NULL)
2938
0
    {
2939
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
2940
0
                      "No space for photometric conversion table");
2941
0
        return (0);
2942
0
    }
2943
11.3k
    if (img->photometric == PHOTOMETRIC_MINISWHITE)
2944
7.92k
    {
2945
459k
        for (x = 0; x <= range; x++)
2946
451k
            img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range);
2947
7.92k
    }
2948
3.39k
    else
2949
3.39k
    {
2950
288k
        for (x = 0; x <= range; x++)
2951
285k
            img->Map[x] = (TIFFRGBValue)((x * 255) / range);
2952
3.39k
    }
2953
11.3k
    if (img->bitspersample <= 16 &&
2954
11.3k
        (img->photometric == PHOTOMETRIC_MINISBLACK ||
2955
7.93k
         img->photometric == PHOTOMETRIC_MINISWHITE))
2956
11.2k
    {
2957
        /*
2958
         * Use photometric mapping table to construct
2959
         * unpacking tables for samples <= 8 bits.
2960
         */
2961
11.2k
        if (!makebwmap(img))
2962
0
            return (0);
2963
        /* no longer need Map, free it */
2964
11.2k
        _TIFFfreeExt(img->tif, img->Map);
2965
11.2k
        img->Map = NULL;
2966
11.2k
    }
2967
11.3k
    return (1);
2968
11.3k
}
2969
2970
static int checkcmap(TIFFRGBAImage *img)
2971
753
{
2972
753
    uint16_t *r = img->redcmap;
2973
753
    uint16_t *g = img->greencmap;
2974
753
    uint16_t *b = img->bluecmap;
2975
753
    long n = 1L << img->bitspersample;
2976
2977
7.45k
    while (n-- > 0)
2978
7.32k
        if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
2979
624
            return (16);
2980
129
    return (8);
2981
753
}
2982
2983
static void cvtcmap(TIFFRGBAImage *img)
2984
624
{
2985
624
    uint16_t *r = img->redcmap;
2986
624
    uint16_t *g = img->greencmap;
2987
624
    uint16_t *b = img->bluecmap;
2988
624
    long i;
2989
2990
7.75k
    for (i = (1L << img->bitspersample) - 1; i >= 0; i--)
2991
7.12k
    {
2992
21.3k
#define CVT(x) ((uint16_t)((x) >> 8))
2993
7.12k
        r[i] = CVT(r[i]);
2994
7.12k
        g[i] = CVT(g[i]);
2995
7.12k
        b[i] = CVT(b[i]);
2996
7.12k
#undef CVT
2997
7.12k
    }
2998
624
}
2999
3000
/*
3001
 * Palette images with <= 8 bits/sample are handled
3002
 * with a table to avoid lots of shifts and masks.  The table
3003
 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
3004
 * pixel values simply by indexing into the table with one
3005
 * number.
3006
 */
3007
static int makecmap(TIFFRGBAImage *img)
3008
753
{
3009
753
    int bitspersample = img->bitspersample;
3010
753
    int nsamples = 8 / bitspersample;
3011
753
    uint16_t *r = img->redcmap;
3012
753
    uint16_t *g = img->greencmap;
3013
753
    uint16_t *b = img->bluecmap;
3014
753
    uint32_t *p;
3015
753
    int i;
3016
3017
753
    img->PALmap = (uint32_t **)_TIFFmallocExt(
3018
753
        img->tif,
3019
753
        (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
3020
753
                   ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
3021
753
    if (img->PALmap == NULL)
3022
0
    {
3023
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
3024
0
                      "No space for Palette mapping table");
3025
0
        return (0);
3026
0
    }
3027
753
    p = (uint32_t *)(img->PALmap + 256);
3028
193k
    for (i = 0; i < 256; i++)
3029
192k
    {
3030
192k
        TIFFRGBValue c;
3031
192k
        img->PALmap[i] = p;
3032
192k
#define CMAP(x)                                                                \
3033
1.09M
    c = (TIFFRGBValue)x;                                                       \
3034
1.09M
    *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff);
3035
192k
        switch (bitspersample)
3036
192k
        {
3037
100k
            case 1:
3038
100k
                CMAP(i >> 7);
3039
100k
                CMAP((i >> 6) & 1);
3040
100k
                CMAP((i >> 5) & 1);
3041
100k
                CMAP((i >> 4) & 1);
3042
100k
                CMAP((i >> 3) & 1);
3043
100k
                CMAP((i >> 2) & 1);
3044
100k
                CMAP((i >> 1) & 1);
3045
100k
                CMAP(i & 1);
3046
100k
                break;
3047
58.6k
            case 2:
3048
58.6k
                CMAP(i >> 6);
3049
58.6k
                CMAP((i >> 4) & 3);
3050
58.6k
                CMAP((i >> 2) & 3);
3051
58.6k
                CMAP(i & 3);
3052
58.6k
                break;
3053
23.2k
            case 4:
3054
23.2k
                CMAP(i >> 4);
3055
23.2k
                CMAP(i & 0xf);
3056
23.2k
                break;
3057
9.98k
            case 8:
3058
9.98k
                CMAP(i);
3059
9.98k
                break;
3060
0
            default:
3061
0
                break;
3062
192k
        }
3063
192k
#undef CMAP
3064
192k
    }
3065
753
    return (1);
3066
753
}
3067
3068
/*
3069
 * Construct any mapping table used
3070
 * by the associated put routine.
3071
 */
3072
static int buildMap(TIFFRGBAImage *img)
3073
12.8k
{
3074
12.8k
    switch (img->photometric)
3075
12.8k
    {
3076
0
        case PHOTOMETRIC_RGB:
3077
0
        case PHOTOMETRIC_YCBCR:
3078
552
        case PHOTOMETRIC_SEPARATED:
3079
552
            if (img->bitspersample == 8)
3080
538
                break;
3081
            /* fall through... */
3082
3.39k
        case PHOTOMETRIC_MINISBLACK:
3083
11.3k
        case PHOTOMETRIC_MINISWHITE:
3084
11.3k
            if (!setupMap(img))
3085
0
                return (0);
3086
11.3k
            break;
3087
11.3k
        case PHOTOMETRIC_PALETTE:
3088
            /*
3089
             * Convert 16-bit colormap to 8-bit (unless it looks
3090
             * like an old-style 8-bit colormap).
3091
             */
3092
753
            if (checkcmap(img) == 16)
3093
624
                cvtcmap(img);
3094
129
            else
3095
129
                TIFFWarningExtR(img->tif, TIFFFileName(img->tif),
3096
129
                                "Assuming 8-bit colormap");
3097
            /*
3098
             * Use mapping table and colormap to construct
3099
             * unpacking tables for samples < 8 bits.
3100
             */
3101
753
            if (img->bitspersample <= 8 && !makecmap(img))
3102
0
                return (0);
3103
753
            break;
3104
753
        default:
3105
246
            break;
3106
12.8k
    }
3107
12.8k
    return (1);
3108
12.8k
}
3109
3110
/*
3111
 * Select the appropriate conversion routine for packed data.
3112
 */
3113
static int PickContigCase(TIFFRGBAImage *img)
3114
21.5k
{
3115
21.5k
    img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
3116
21.5k
    img->put.contig = NULL;
3117
21.5k
    switch (img->photometric)
3118
21.5k
    {
3119
5.00k
        case PHOTOMETRIC_RGB:
3120
5.00k
            switch (img->bitspersample)
3121
5.00k
            {
3122
4.89k
                case 8:
3123
4.89k
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
3124
645
                        img->samplesperpixel >= 4)
3125
516
                        img->put.contig = putRGBAAcontig8bittile;
3126
4.38k
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
3127
82
                             img->samplesperpixel >= 4)
3128
70
                    {
3129
70
                        if (BuildMapUaToAa(img))
3130
70
                            img->put.contig = putRGBUAcontig8bittile;
3131
70
                    }
3132
4.31k
                    else if (img->samplesperpixel >= 3)
3133
4.31k
                        img->put.contig = putRGBcontig8bittile;
3134
4.89k
                    break;
3135
13
                case 16:
3136
13
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
3137
5
                        img->samplesperpixel >= 4)
3138
5
                    {
3139
5
                        if (BuildMapBitdepth16To8(img))
3140
5
                            img->put.contig = putRGBAAcontig16bittile;
3141
5
                    }
3142
8
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
3143
0
                             img->samplesperpixel >= 4)
3144
0
                    {
3145
0
                        if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
3146
0
                            img->put.contig = putRGBUAcontig16bittile;
3147
0
                    }
3148
8
                    else if (img->samplesperpixel >= 3)
3149
8
                    {
3150
8
                        if (BuildMapBitdepth16To8(img))
3151
8
                            img->put.contig = putRGBcontig16bittile;
3152
8
                    }
3153
13
                    break;
3154
92
                default:
3155
92
                    break;
3156
5.00k
            }
3157
5.00k
            break;
3158
5.00k
        case PHOTOMETRIC_SEPARATED:
3159
552
            if (img->samplesperpixel >= 4 && buildMap(img))
3160
552
            {
3161
552
                if (img->bitspersample == 8)
3162
538
                {
3163
538
                    if (!img->Map)
3164
538
                        img->put.contig = putRGBcontig8bitCMYKtile;
3165
0
                    else
3166
0
                        img->put.contig = putRGBcontig8bitCMYKMaptile;
3167
538
                }
3168
552
            }
3169
552
            break;
3170
753
        case PHOTOMETRIC_PALETTE:
3171
753
            if (buildMap(img))
3172
753
            {
3173
753
                switch (img->bitspersample)
3174
753
                {
3175
39
                    case 8:
3176
39
                        img->put.contig = put8bitcmaptile;
3177
39
                        break;
3178
91
                    case 4:
3179
91
                        img->put.contig = put4bitcmaptile;
3180
91
                        break;
3181
229
                    case 2:
3182
229
                        img->put.contig = put2bitcmaptile;
3183
229
                        break;
3184
394
                    case 1:
3185
394
                        img->put.contig = put1bitcmaptile;
3186
394
                        break;
3187
0
                    default:
3188
0
                        break;
3189
753
                }
3190
753
            }
3191
753
            break;
3192
7.92k
        case PHOTOMETRIC_MINISWHITE:
3193
11.2k
        case PHOTOMETRIC_MINISBLACK:
3194
11.2k
            if (buildMap(img))
3195
11.2k
            {
3196
11.2k
                switch (img->bitspersample)
3197
11.2k
                {
3198
238
                    case 16:
3199
238
                        img->put.contig = put16bitbwtile;
3200
238
                        break;
3201
2.52k
                    case 8:
3202
2.52k
                        if (img->alpha && img->samplesperpixel == 2)
3203
90
                            img->put.contig = putagreytile;
3204
2.43k
                        else
3205
2.43k
                            img->put.contig = putgreytile;
3206
2.52k
                        break;
3207
782
                    case 4:
3208
782
                        img->put.contig = put4bitbwtile;
3209
782
                        break;
3210
315
                    case 2:
3211
315
                        img->put.contig = put2bitbwtile;
3212
315
                        break;
3213
7.43k
                    case 1:
3214
7.43k
                        img->put.contig = put1bitbwtile;
3215
7.43k
                        break;
3216
0
                    default:
3217
0
                        break;
3218
11.2k
                }
3219
11.2k
            }
3220
11.2k
            break;
3221
11.2k
        case PHOTOMETRIC_YCBCR:
3222
3.65k
            if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
3223
3.62k
            {
3224
3.62k
                if (initYCbCrConversion(img) != 0)
3225
3.59k
                {
3226
                    /*
3227
                     * The 6.0 spec says that subsampling must be
3228
                     * one of 1, 2, or 4, and that vertical subsampling
3229
                     * must always be <= horizontal subsampling; so
3230
                     * there are only a few possibilities and we just
3231
                     * enumerate the cases.
3232
                     * Joris: added support for the [1,2] case, nonetheless, to
3233
                     * accommodate some OJPEG files
3234
                     */
3235
3.59k
                    uint16_t SubsamplingHor;
3236
3.59k
                    uint16_t SubsamplingVer;
3237
3.59k
                    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
3238
3.59k
                                          &SubsamplingHor, &SubsamplingVer);
3239
                    /* Validate that the image dimensions are compatible with
3240
                    the subsampling block. All putcontig8bitYCbCrXYtile routines
3241
                    assume width >= X and height >= Y. */
3242
3.59k
                    if (img->width < SubsamplingHor ||
3243
3.57k
                        img->height < SubsamplingVer)
3244
35
                    {
3245
35
                        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
3246
35
                                      "YCbCr subsampling (%u,%u) incompatible "
3247
35
                                      "with image size %ux%u",
3248
35
                                      SubsamplingHor, SubsamplingVer,
3249
35
                                      img->width, img->height);
3250
35
                        return (0);
3251
35
                    }
3252
3.55k
                    switch ((SubsamplingHor << 4) | SubsamplingVer)
3253
3.55k
                    {
3254
183
                        case 0x44:
3255
183
                            img->put.contig = putcontig8bitYCbCr44tile;
3256
183
                            break;
3257
128
                        case 0x42:
3258
128
                            img->put.contig = putcontig8bitYCbCr42tile;
3259
128
                            break;
3260
93
                        case 0x41:
3261
93
                            img->put.contig = putcontig8bitYCbCr41tile;
3262
93
                            break;
3263
2.62k
                        case 0x22:
3264
2.62k
                            img->put.contig = putcontig8bitYCbCr22tile;
3265
2.62k
                            break;
3266
62
                        case 0x21:
3267
62
                            img->put.contig = putcontig8bitYCbCr21tile;
3268
62
                            break;
3269
47
                        case 0x12:
3270
47
                            img->put.contig = putcontig8bitYCbCr12tile;
3271
47
                            break;
3272
409
                        case 0x11:
3273
409
                            img->put.contig = putcontig8bitYCbCr11tile;
3274
409
                            break;
3275
10
                        default:
3276
10
                            break;
3277
3.55k
                    }
3278
3.55k
                }
3279
3.62k
            }
3280
3.62k
            break;
3281
3.62k
        case PHOTOMETRIC_CIELAB:
3282
246
            if (img->samplesperpixel == 3 && buildMap(img))
3283
246
            {
3284
246
                if (img->bitspersample == 8 || img->bitspersample == 16)
3285
246
                    img->put.contig = initCIELabConversion(img);
3286
246
                break;
3287
246
            }
3288
0
            break;
3289
0
        default:
3290
0
            break;
3291
21.5k
    }
3292
21.4k
    return ((img->get != NULL) && (img->put.contig != NULL));
3293
21.5k
}
3294
3295
/*
3296
 * Select the appropriate conversion routine for unpacked data.
3297
 *
3298
 * NB: we assume that unpacked single channel data is directed
3299
 *   to the "packed routines.
3300
 */
3301
static int PickSeparateCase(TIFFRGBAImage *img)
3302
4.08k
{
3303
4.08k
    img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
3304
4.08k
    img->put.separate = NULL;
3305
4.08k
    switch (img->photometric)
3306
4.08k
    {
3307
314
        case PHOTOMETRIC_MINISWHITE:
3308
878
        case PHOTOMETRIC_MINISBLACK:
3309
            /* greyscale images processed pretty much as RGB by gtTileSeparate
3310
             */
3311
3.21k
        case PHOTOMETRIC_RGB:
3312
3.21k
            switch (img->bitspersample)
3313
3.21k
            {
3314
3.18k
                case 8:
3315
3.18k
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
3316
709
                        img->put.separate = putRGBAAseparate8bittile;
3317
2.47k
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
3318
62
                    {
3319
62
                        if (BuildMapUaToAa(img))
3320
62
                            img->put.separate = putRGBUAseparate8bittile;
3321
62
                    }
3322
2.41k
                    else
3323
2.41k
                        img->put.separate = putRGBseparate8bittile;
3324
3.18k
                    break;
3325
26
                case 16:
3326
26
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
3327
13
                    {
3328
13
                        if (BuildMapBitdepth16To8(img))
3329
13
                            img->put.separate = putRGBAAseparate16bittile;
3330
13
                    }
3331
13
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
3332
3
                    {
3333
3
                        if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
3334
3
                            img->put.separate = putRGBUAseparate16bittile;
3335
3
                    }
3336
10
                    else
3337
10
                    {
3338
10
                        if (BuildMapBitdepth16To8(img))
3339
10
                            img->put.separate = putRGBseparate16bittile;
3340
10
                    }
3341
26
                    break;
3342
7
                default:
3343
7
                    break;
3344
3.21k
            }
3345
3.21k
            break;
3346
3.21k
        case PHOTOMETRIC_SEPARATED:
3347
110
            if (img->bitspersample == 8 && img->samplesperpixel == 4)
3348
97
            {
3349
                /* Not alpha, but seems like the only way to get 4th band */
3350
97
                img->alpha = 1;
3351
97
                img->put.separate = putCMYKseparate8bittile;
3352
97
            }
3353
110
            break;
3354
752
        case PHOTOMETRIC_YCBCR:
3355
752
            if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
3356
738
            {
3357
738
                if (initYCbCrConversion(img) != 0)
3358
730
                {
3359
730
                    uint16_t hs, vs;
3360
730
                    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
3361
730
                                          &hs, &vs);
3362
730
                    switch ((hs << 4) | vs)
3363
730
                    {
3364
657
                        case 0x11:
3365
657
                            img->put.separate = putseparate8bitYCbCr11tile;
3366
657
                            break;
3367
                            /* TODO: add other cases here */
3368
73
                        default:
3369
73
                            break;
3370
730
                    }
3371
730
                }
3372
738
            }
3373
752
            break;
3374
752
        default:
3375
6
            break;
3376
4.08k
    }
3377
4.08k
    return ((img->get != NULL) && (img->put.separate != NULL));
3378
4.08k
}
3379
3380
static int BuildMapUaToAa(TIFFRGBAImage *img)
3381
135
{
3382
135
    static const char module[] = "BuildMapUaToAa";
3383
135
    uint8_t *m;
3384
135
    uint16_t na, nv;
3385
135
    assert(img->UaToAa == NULL);
3386
135
    img->UaToAa = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
3387
135
    if (img->UaToAa == NULL)
3388
0
    {
3389
0
        TIFFErrorExtR(img->tif, module, "Out of memory");
3390
0
        return (0);
3391
0
    }
3392
135
    m = img->UaToAa;
3393
34.6k
    for (na = 0; na < 256; na++)
3394
34.5k
    {
3395
8.88M
        for (nv = 0; nv < 256; nv++)
3396
8.84M
            *m++ = (uint8_t)((nv * na + 127) / 255);
3397
34.5k
    }
3398
135
    return (1);
3399
135
}
3400
3401
static int BuildMapBitdepth16To8(TIFFRGBAImage *img)
3402
39
{
3403
39
    static const char module[] = "BuildMapBitdepth16To8";
3404
39
    uint8_t *m;
3405
39
    uint32_t n;
3406
39
    assert(img->Bitdepth16To8 == NULL);
3407
39
    img->Bitdepth16To8 = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
3408
39
    if (img->Bitdepth16To8 == NULL)
3409
0
    {
3410
0
        TIFFErrorExtR(img->tif, module, "Out of memory");
3411
0
        return (0);
3412
0
    }
3413
39
    m = img->Bitdepth16To8;
3414
2.55M
    for (n = 0; n < 65536; n++)
3415
2.55M
        *m++ = (uint8_t)((n + 128) / 257);
3416
39
    return (1);
3417
39
}
3418
3419
/*
3420
 * Read a whole strip off data from the file, and convert to RGBA form.
3421
 * If this is the last strip, then it will only contain the portion of
3422
 * the strip that is actually within the image space.  The result is
3423
 * organized in bottom to top form.
3424
 */
3425
3426
int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster)
3427
3428
0
{
3429
0
    return TIFFReadRGBAStripExt(tif, row, raster, 0);
3430
0
}
3431
3432
int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster,
3433
                         int stop_on_error)
3434
3435
0
{
3436
0
    char emsg[EMSG_BUF_SIZE] = "";
3437
0
    TIFFRGBAImage img;
3438
0
    int ok;
3439
0
    uint32_t rowsperstrip, rows_to_read;
3440
3441
0
    if (TIFFIsTiled(tif))
3442
0
    {
3443
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3444
0
                      "Can't use TIFFReadRGBAStrip() with tiled file.");
3445
0
        return (0);
3446
0
    }
3447
3448
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
3449
3450
0
    if (rowsperstrip == 0)
3451
0
    {
3452
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
3453
0
        return (0);
3454
0
    }
3455
3456
0
    if ((row % rowsperstrip) != 0)
3457
0
    {
3458
0
        TIFFErrorExtR(
3459
0
            tif, TIFFFileName(tif),
3460
0
            "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
3461
0
        return (0);
3462
0
    }
3463
3464
0
    if (TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
3465
0
    {
3466
0
        if (row >= img.height)
3467
0
        {
3468
0
            TIFFErrorExtR(tif, TIFFFileName(tif),
3469
0
                          "Invalid row passed to TIFFReadRGBAStrip().");
3470
0
            TIFFRGBAImageEnd(&img);
3471
0
            return (0);
3472
0
        }
3473
3474
0
        img.row_offset = (int)row;
3475
0
        img.col_offset = 0;
3476
3477
0
        if (row + rowsperstrip > img.height)
3478
0
            rows_to_read = img.height - row;
3479
0
        else
3480
0
            rows_to_read = rowsperstrip;
3481
3482
0
        ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read);
3483
3484
0
        TIFFRGBAImageEnd(&img);
3485
0
    }
3486
0
    else
3487
0
    {
3488
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
3489
0
        ok = 0;
3490
0
    }
3491
3492
0
    return (ok);
3493
0
}
3494
3495
/*
3496
 * Read a whole tile off data from the file, and convert to RGBA form.
3497
 * The returned RGBA data is organized from bottom to top of tile,
3498
 * and may include zeroed areas if the tile extends off the image.
3499
 */
3500
3501
int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster)
3502
3503
0
{
3504
0
    return TIFFReadRGBATileExt(tif, col, row, raster, 0);
3505
0
}
3506
3507
int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster,
3508
                        int stop_on_error)
3509
0
{
3510
0
    char emsg[EMSG_BUF_SIZE] = "";
3511
0
    TIFFRGBAImage img;
3512
0
    int ok;
3513
0
    uint32_t tile_xsize, tile_ysize;
3514
0
    uint32_t read_xsize, read_ysize;
3515
0
    uint32_t i_row;
3516
3517
    /*
3518
     * Verify that our request is legal - on a tile file, and on a
3519
     * tile boundary.
3520
     */
3521
3522
0
    if (!TIFFIsTiled(tif))
3523
0
    {
3524
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3525
0
                      "Can't use TIFFReadRGBATile() with striped file.");
3526
0
        return (0);
3527
0
    }
3528
3529
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
3530
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
3531
0
    if (tile_xsize == 0 || tile_ysize == 0)
3532
0
    {
3533
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3534
0
                      "tile_xsize or tile_ysize is zero");
3535
0
        return (0);
3536
0
    }
3537
3538
0
    if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)
3539
0
    {
3540
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3541
0
                      "Row/col passed to TIFFReadRGBATile() must be top"
3542
0
                      "left corner of a tile.");
3543
0
        return (0);
3544
0
    }
3545
3546
    /*
3547
     * Setup the RGBA reader.
3548
     */
3549
3550
0
    if (!TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
3551
0
    {
3552
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
3553
0
        return (0);
3554
0
    }
3555
3556
0
    if (col >= img.width || row >= img.height)
3557
0
    {
3558
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3559
0
                      "Invalid row/col passed to TIFFReadRGBATile().");
3560
0
        TIFFRGBAImageEnd(&img);
3561
0
        return (0);
3562
0
    }
3563
3564
    /*
3565
     * The TIFFRGBAImageGet() function doesn't allow us to get off the
3566
     * edge of the image, even to fill an otherwise valid tile.  So we
3567
     * figure out how much we can read, and fix up the tile buffer to
3568
     * a full tile configuration afterwards.
3569
     */
3570
3571
0
    if (row + tile_ysize > img.height)
3572
0
        read_ysize = img.height - row;
3573
0
    else
3574
0
        read_ysize = tile_ysize;
3575
3576
0
    if (col + tile_xsize > img.width)
3577
0
        read_xsize = img.width - col;
3578
0
    else
3579
0
        read_xsize = tile_xsize;
3580
3581
    /*
3582
     * Read the chunk of imagery.
3583
     */
3584
3585
0
    img.row_offset = (int)row;
3586
0
    img.col_offset = (int)col;
3587
3588
0
    ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize);
3589
3590
0
    TIFFRGBAImageEnd(&img);
3591
3592
    /*
3593
     * If our read was incomplete we will need to fix up the tile by
3594
     * shifting the data around as if a full tile of data is being returned.
3595
     *
3596
     * This is all the more complicated because the image is organized in
3597
     * bottom to top format.
3598
     */
3599
3600
0
    if (read_xsize == tile_xsize && read_ysize == tile_ysize)
3601
0
        return (ok);
3602
3603
0
    for (i_row = 0; i_row < read_ysize; i_row++)
3604
0
    {
3605
0
        memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize,
3606
0
                raster + (size_t)(read_ysize - i_row - 1) * read_xsize,
3607
0
                read_xsize * sizeof(uint32_t));
3608
0
        _TIFFmemset(
3609
0
            raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize + read_xsize,
3610
0
            0,
3611
0
            (tmsize_t)(sizeof(uint32_t) * (size_t)(tile_xsize - read_xsize)));
3612
0
    }
3613
3614
0
    for (i_row = read_ysize; i_row < tile_ysize; i_row++)
3615
0
    {
3616
0
        _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0,
3617
0
                    (tmsize_t)(sizeof(uint32_t) * (size_t)tile_xsize));
3618
0
    }
3619
3620
0
    return (ok);
3621
0
}