Coverage Report

Created: 2026-06-07 07:20

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
166k
#define FLIP_VERTICALLY 0x01
51
26.6k
#define FLIP_HORIZONTALLY 0x02
52
53
2.71k
#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
28.5k
{
86
28.5k
    TIFFDirectory *td = &tif->tif_dir;
87
28.5k
    uint16_t photometric;
88
28.5k
    int colorchannels;
89
90
28.5k
    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
28.5k
    switch (td->td_bitspersample)
97
28.5k
    {
98
11.8k
        case 1:
99
12.4k
        case 2:
100
13.3k
        case 4:
101
28.2k
        case 8:
102
28.5k
        case 16:
103
28.5k
            break;
104
26
        default:
105
26
            snprintf(emsg, EMSG_BUF_SIZE,
106
26
                     "Sorry, can not handle images with %" PRIu16
107
26
                     "-bit samples",
108
26
                     td->td_bitspersample);
109
26
            return (0);
110
28.5k
    }
111
28.5k
    if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
112
20
    {
113
20
        snprintf(
114
20
            emsg, EMSG_BUF_SIZE,
115
20
            "Sorry, can not handle images with IEEE floating-point samples");
116
20
        return (0);
117
20
    }
118
28.4k
    colorchannels = td->td_samplesperpixel - td->td_extrasamples;
119
28.4k
    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
28.4k
    switch (photometric)
136
28.4k
    {
137
8.41k
        case PHOTOMETRIC_MINISWHITE:
138
11.9k
        case PHOTOMETRIC_MINISBLACK:
139
12.7k
        case PHOTOMETRIC_PALETTE:
140
12.7k
            if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
141
11.5k
                td->td_samplesperpixel != 1 && td->td_bitspersample < 8)
142
31
            {
143
31
                snprintf(
144
31
                    emsg, EMSG_BUF_SIZE,
145
31
                    "Sorry, can not handle contiguous data with %s=%" PRIu16
146
31
                    ", "
147
31
                    "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "",
148
31
                    photoTag, photometric, "Samples/pixel",
149
31
                    td->td_samplesperpixel, td->td_bitspersample);
150
31
                return (0);
151
31
            }
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.7k
            break;
158
12.7k
        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
6.14k
            break;
166
4.98k
        case PHOTOMETRIC_RGB:
167
4.98k
            if (colorchannels < 3)
168
113
            {
169
113
                snprintf(emsg, EMSG_BUF_SIZE,
170
113
                         "Sorry, can not handle RGB image with %s=%d",
171
113
                         "Color channels", colorchannels);
172
113
                return (0);
173
113
            }
174
4.86k
            break;
175
4.86k
        case PHOTOMETRIC_SEPARATED:
176
789
        {
177
789
            uint16_t inkset;
178
789
            TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
179
789
            if (inkset != INKSET_CMYK)
180
6
            {
181
6
                snprintf(emsg, EMSG_BUF_SIZE,
182
6
                         "Sorry, can not handle separated image with %s=%d",
183
6
                         "InkSet", inkset);
184
6
                return 0;
185
6
            }
186
783
            if (td->td_samplesperpixel < 4)
187
121
            {
188
121
                snprintf(
189
121
                    emsg, EMSG_BUF_SIZE,
190
121
                    "Sorry, can not handle separated image with %s=%" PRIu16,
191
121
                    "Samples/pixel", td->td_samplesperpixel);
192
121
                return 0;
193
121
            }
194
662
            break;
195
783
        }
196
662
        case PHOTOMETRIC_LOGL:
197
462
            if (td->td_compression != COMPRESSION_SGILOG)
198
11
            {
199
11
                snprintf(emsg, EMSG_BUF_SIZE,
200
11
                         "Sorry, LogL data must have %s=%d", "Compression",
201
11
                         COMPRESSION_SGILOG);
202
11
                return (0);
203
11
            }
204
451
            break;
205
1.16k
        case PHOTOMETRIC_LOGLUV:
206
1.16k
            if (td->td_compression != COMPRESSION_SGILOG &&
207
625
                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.14k
            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.13k
            if (td->td_samplesperpixel != 3 || colorchannels != 3)
223
47
            {
224
47
                snprintf(emsg, EMSG_BUF_SIZE,
225
47
                         "Sorry, can not handle image with %s=%" PRIu16
226
47
                         ", %s=%d",
227
47
                         "Samples/pixel", td->td_samplesperpixel,
228
47
                         "colorchannels", colorchannels);
229
47
                return 0;
230
47
            }
231
1.08k
            break;
232
1.08k
        case PHOTOMETRIC_CIELAB:
233
344
            if (td->td_samplesperpixel != 3 || colorchannels != 3 ||
234
257
                (td->td_bitspersample != 8 && td->td_bitspersample != 16))
235
93
            {
236
93
                snprintf(emsg, EMSG_BUF_SIZE,
237
93
                         "Sorry, can not handle image with %s=%" PRIu16
238
93
                         ", %s=%d and %s=%" PRIu16,
239
93
                         "Samples/pixel", td->td_samplesperpixel,
240
93
                         "colorchannels", colorchannels, "Bits/sample",
241
93
                         td->td_bitspersample);
242
93
                return 0;
243
93
            }
244
251
            break;
245
1.80k
        default:
246
1.80k
            snprintf(emsg, EMSG_BUF_SIZE,
247
1.80k
                     "Sorry, can not handle image with %s=%" PRIu16, photoTag,
248
1.80k
                     photometric);
249
1.80k
            return (0);
250
28.4k
    }
251
26.2k
    return (1);
252
28.4k
}
253
254
void TIFFRGBAImageEnd(TIFFRGBAImage *img)
255
26.2k
{
256
26.2k
    if (img->Map)
257
15
    {
258
15
        _TIFFfreeExt(img->tif, img->Map);
259
15
        img->Map = NULL;
260
15
    }
261
26.2k
    if (img->BWmap)
262
11.5k
    {
263
11.5k
        _TIFFfreeExt(img->tif, img->BWmap);
264
11.5k
        img->BWmap = NULL;
265
11.5k
    }
266
26.2k
    if (img->PALmap)
267
785
    {
268
785
        _TIFFfreeExt(img->tif, img->PALmap);
269
785
        img->PALmap = NULL;
270
785
    }
271
26.2k
    if (img->ycbcr)
272
4.60k
    {
273
4.60k
        _TIFFfreeExt(img->tif, img->ycbcr);
274
4.60k
        img->ycbcr = NULL;
275
4.60k
    }
276
26.2k
    if (img->cielab)
277
245
    {
278
245
        _TIFFfreeExt(img->tif, img->cielab);
279
245
        img->cielab = NULL;
280
245
    }
281
26.2k
    if (img->UaToAa)
282
137
    {
283
137
        _TIFFfreeExt(img->tif, img->UaToAa);
284
137
        img->UaToAa = NULL;
285
137
    }
286
26.2k
    if (img->Bitdepth16To8)
287
44
    {
288
44
        _TIFFfreeExt(img->tif, img->Bitdepth16To8);
289
44
        img->Bitdepth16To8 = NULL;
290
44
    }
291
292
26.2k
    if (img->redcmap)
293
787
    {
294
787
        _TIFFfreeExt(img->tif, img->redcmap);
295
787
        _TIFFfreeExt(img->tif, img->greencmap);
296
787
        _TIFFfreeExt(img->tif, img->bluecmap);
297
787
        img->redcmap = img->greencmap = img->bluecmap = NULL;
298
787
    }
299
26.2k
}
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
28.5k
{
314
28.5k
    uint16_t *sampleinfo;
315
28.5k
    uint16_t extrasamples;
316
28.5k
    uint16_t planarconfig;
317
28.5k
    uint16_t compress;
318
28.5k
    int colorchannels;
319
28.5k
    uint16_t *red_orig, *green_orig, *blue_orig;
320
28.5k
    uint32_t n_color;
321
322
28.5k
    if (!TIFFRGBAImageOK(tif, emsg))
323
2.30k
        return 0;
324
325
    /* Initialize to normal values */
326
26.2k
    img->row_offset = 0;
327
26.2k
    img->col_offset = 0;
328
26.2k
    img->redcmap = NULL;
329
26.2k
    img->greencmap = NULL;
330
26.2k
    img->bluecmap = NULL;
331
26.2k
    img->Map = NULL;
332
26.2k
    img->BWmap = NULL;
333
26.2k
    img->PALmap = NULL;
334
26.2k
    img->ycbcr = NULL;
335
26.2k
    img->cielab = NULL;
336
26.2k
    img->UaToAa = NULL;
337
26.2k
    img->Bitdepth16To8 = NULL;
338
26.2k
    img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
339
340
26.2k
    img->tif = tif;
341
26.2k
    img->stoponerr = stop;
342
26.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
343
26.2k
    switch (img->bitspersample)
344
26.2k
    {
345
9.68k
        case 1:
346
10.2k
        case 2:
347
11.2k
        case 4:
348
25.9k
        case 8:
349
26.2k
        case 16:
350
26.2k
            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
26.2k
    }
358
26.2k
    img->alpha = 0;
359
26.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
360
26.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples,
361
26.2k
                          &sampleinfo);
362
26.2k
    if (extrasamples >= 1)
363
2.99k
    {
364
2.99k
        switch (sampleinfo[0])
365
2.99k
        {
366
2.46k
            case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without
367
                                           */
368
2.46k
                if (img->samplesperpixel >
369
2.46k
                    3) /* correct info about alpha channel */
370
1.57k
                    img->alpha = EXTRASAMPLE_ASSOCALPHA;
371
2.46k
                break;
372
312
            case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
373
530
            case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
374
530
                img->alpha = sampleinfo[0];
375
530
                break;
376
0
            default:
377
0
                break;
378
2.99k
        }
379
2.99k
    }
380
381
26.2k
#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
382
26.2k
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
383
0
        img->photometric = PHOTOMETRIC_MINISWHITE;
384
385
26.2k
    if (extrasamples == 0 && img->samplesperpixel == 4 &&
386
435
        img->photometric == PHOTOMETRIC_RGB)
387
0
    {
388
0
        img->alpha = EXTRASAMPLE_ASSOCALPHA;
389
0
        extrasamples = 1;
390
0
    }
391
26.2k
#endif
392
393
26.2k
    colorchannels = img->samplesperpixel - extrasamples;
394
26.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
395
26.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
396
26.2k
    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
26.2k
    switch (img->photometric)
416
26.2k
    {
417
787
        case PHOTOMETRIC_PALETTE:
418
787
            if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &red_orig, &green_orig,
419
787
                              &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
787
            n_color = (uint32_t)(1U << img->bitspersample);
428
787
            img->redcmap = (uint16_t *)_TIFFmallocExt(
429
787
                tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
430
787
            img->greencmap = (uint16_t *)_TIFFmallocExt(
431
787
                tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
432
787
            img->bluecmap = (uint16_t *)_TIFFmallocExt(
433
787
                tif, (tmsize_t)(sizeof(uint16_t) * (size_t)n_color));
434
787
            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
787
            _TIFFmemcpy(img->redcmap, red_orig, n_color * 2);
442
787
            _TIFFmemcpy(img->greencmap, green_orig, n_color * 2);
443
787
            _TIFFmemcpy(img->bluecmap, blue_orig, n_color * 2);
444
445
            /* fall through... */
446
9.18k
        case PHOTOMETRIC_MINISWHITE:
447
12.7k
        case PHOTOMETRIC_MINISBLACK:
448
12.7k
            if (planarconfig == PLANARCONFIG_CONTIG &&
449
11.5k
                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.7k
            break;
461
12.7k
        case PHOTOMETRIC_YCBCR:
462
            /* It would probably be nice to have a reality check here. */
463
6.14k
            if (planarconfig == PLANARCONFIG_CONTIG)
464
                /* can rely on libjpeg to convert to RGB */
465
                /* XXX should restore current state on exit */
466
5.40k
                switch (compress)
467
5.40k
                {
468
1.48k
                    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.48k
                        TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
476
1.48k
                                     JPEGCOLORMODE_RGB);
477
1.48k
                        img->photometric = PHOTOMETRIC_RGB;
478
1.48k
                        break;
479
3.91k
                    default:
480
3.91k
                        /* do nothing */;
481
3.91k
                        break;
482
5.40k
                }
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
6.14k
            break;
490
6.14k
        case PHOTOMETRIC_RGB:
491
4.86k
            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.86k
            break;
499
4.86k
        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
451
            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
451
            TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
530
451
            img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
531
451
            img->bitspersample = 8;
532
451
            break;
533
1.08k
        case PHOTOMETRIC_LOGLUV:
534
1.08k
            if (compress != COMPRESSION_SGILOG &&
535
570
                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.08k
            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.08k
            TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
551
1.08k
            img->photometric = PHOTOMETRIC_RGB; /* little white lie */
552
1.08k
            img->bitspersample = 8;
553
1.08k
            break;
554
251
        case PHOTOMETRIC_CIELAB:
555
251
            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
26.2k
    }
562
26.2k
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
563
26.2k
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
564
26.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
565
26.2k
    img->isContig =
566
26.2k
        !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
567
26.2k
    if (img->isContig)
568
22.2k
    {
569
22.2k
        if (!PickContigCase(img))
570
302
        {
571
302
            snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
572
302
            goto fail_return;
573
302
        }
574
22.2k
    }
575
4.01k
    else
576
4.01k
    {
577
4.01k
        if (!PickSeparateCase(img))
578
116
        {
579
116
            snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
580
116
            goto fail_return;
581
116
        }
582
4.01k
    }
583
25.8k
    return 1;
584
585
418
fail_return:
586
418
    TIFFRGBAImageEnd(img);
587
418
    return 0;
588
26.2k
}
589
590
int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
591
                     uint32_t h)
592
25.8k
{
593
25.8k
    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.8k
    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.8k
    if (0 <= img->row_offset && (uint32_t)img->row_offset < img->height)
609
25.8k
    {
610
25.8k
        uint32_t hx = img->height - (uint32_t)img->row_offset;
611
25.8k
        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.8k
    }
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.8k
    return (*img->get)(img, raster, w, h);
628
25.8k
}
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
28.5k
{
637
28.5k
    char emsg[EMSG_BUF_SIZE] = "";
638
28.5k
    TIFFRGBAImage img;
639
28.5k
    int ok;
640
641
28.5k
    if (TIFFRGBAImageBegin(&img, tif, stop, emsg))
642
25.8k
    {
643
25.8k
        img.req_orientation = (uint16_t)orientation;
644
25.8k
        ok = TIFFRGBAImageGet(&img, raster, rwidth, rheight);
645
25.8k
        TIFFRGBAImageEnd(&img);
646
25.8k
    }
647
2.71k
    else
648
2.71k
    {
649
2.71k
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
650
2.71k
        ok = 0;
651
2.71k
    }
652
28.5k
    return (ok);
653
28.5k
}
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
28.5k
{
662
28.5k
    return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
663
28.5k
                                     ORIENTATION_BOTLEFT, stop);
664
28.5k
}
665
666
static int setorientation(TIFFRGBAImage *img)
667
25.8k
{
668
25.8k
    switch (img->orientation)
669
25.8k
    {
670
24.8k
        case ORIENTATION_TOPLEFT:
671
24.8k
        case ORIENTATION_LEFTTOP:
672
24.8k
            if (img->req_orientation == ORIENTATION_TOPRIGHT ||
673
24.8k
                img->req_orientation == ORIENTATION_RIGHTTOP)
674
0
                return FLIP_HORIZONTALLY;
675
24.8k
            else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
676
24.8k
                     img->req_orientation == ORIENTATION_RIGHTBOT)
677
0
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
678
24.8k
            else if (img->req_orientation == ORIENTATION_BOTLEFT ||
679
0
                     img->req_orientation == ORIENTATION_LEFTBOT)
680
24.8k
                return FLIP_VERTICALLY;
681
0
            else
682
0
                return 0;
683
182
        case ORIENTATION_TOPRIGHT:
684
336
        case ORIENTATION_RIGHTTOP:
685
336
            if (img->req_orientation == ORIENTATION_TOPLEFT ||
686
336
                img->req_orientation == ORIENTATION_LEFTTOP)
687
0
                return FLIP_HORIZONTALLY;
688
336
            else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
689
336
                     img->req_orientation == ORIENTATION_RIGHTBOT)
690
0
                return FLIP_VERTICALLY;
691
336
            else if (img->req_orientation == ORIENTATION_BOTLEFT ||
692
0
                     img->req_orientation == ORIENTATION_LEFTBOT)
693
336
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
694
0
            else
695
0
                return 0;
696
262
        case ORIENTATION_BOTRIGHT:
697
466
        case ORIENTATION_RIGHTBOT:
698
466
            if (img->req_orientation == ORIENTATION_TOPLEFT ||
699
466
                img->req_orientation == ORIENTATION_LEFTTOP)
700
0
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
701
466
            else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
702
466
                     img->req_orientation == ORIENTATION_RIGHTTOP)
703
0
                return FLIP_VERTICALLY;
704
466
            else if (img->req_orientation == ORIENTATION_BOTLEFT ||
705
0
                     img->req_orientation == ORIENTATION_LEFTBOT)
706
466
                return FLIP_HORIZONTALLY;
707
0
            else
708
0
                return 0;
709
47
        case ORIENTATION_BOTLEFT:
710
114
        case ORIENTATION_LEFTBOT:
711
114
            if (img->req_orientation == ORIENTATION_TOPLEFT ||
712
114
                img->req_orientation == ORIENTATION_LEFTTOP)
713
0
                return FLIP_VERTICALLY;
714
114
            else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
715
114
                     img->req_orientation == ORIENTATION_RIGHTTOP)
716
0
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
717
114
            else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
718
114
                     img->req_orientation == ORIENTATION_RIGHTBOT)
719
0
                return FLIP_HORIZONTALLY;
720
114
            else
721
114
                return 0;
722
0
        default: /* NOTREACHED */
723
0
            return 0;
724
25.8k
    }
725
25.8k
}
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.69k
{
736
3.69k
    TIFF *tif = img->tif;
737
3.69k
    tileContigRoutine put = img->put.contig;
738
3.69k
    uint32_t col, row, y, rowstoread;
739
3.69k
    tmsize_t pos;
740
3.69k
    uint32_t tw, th;
741
3.69k
    unsigned char *buf = NULL;
742
3.69k
    int32_t fromskew, toskew;
743
3.69k
    uint32_t nrow;
744
3.69k
    int ret = 1, flip;
745
3.69k
    uint32_t this_tw, tocol;
746
3.69k
    int32_t this_toskew, leftmost_toskew;
747
3.69k
    int32_t leftmost_fromskew;
748
3.69k
    uint32_t leftmost_tw;
749
3.69k
    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.69k
    uint32_t wmin;
754
755
3.69k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
756
3.69k
    {
757
3.69k
        wmin = TIFFmin(w, img->width - (uint32_t)img->col_offset);
758
3.69k
    }
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.69k
    bufsize = TIFFTileSize(tif);
768
3.69k
    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.69k
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
775
3.69k
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
776
777
3.69k
    flip = setorientation(img);
778
3.69k
    if (flip & FLIP_VERTICALLY)
779
3.53k
    {
780
3.53k
        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.53k
        y = h - 1;
787
3.53k
        toskew = -(int32_t)(tw + w);
788
3.53k
    }
789
156
    else
790
156
    {
791
156
        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
156
        y = 0;
798
156
        toskew = -(int32_t)(tw - w);
799
156
    }
800
801
3.69k
    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.69k
    leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
811
3.69k
    leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
812
3.69k
    int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
813
3.69k
    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.69k
    leftmost_toskew = (int32_t)skew_i64;
820
33.1k
    for (row = 0; ret != 0 && row < h; row += nrow)
821
29.4k
    {
822
29.4k
        rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
823
29.4k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
824
29.4k
        fromskew = leftmost_fromskew;
825
29.4k
        this_tw = leftmost_tw;
826
29.4k
        this_toskew = leftmost_toskew;
827
29.4k
        tocol = 0;
828
29.4k
        col = (uint32_t)img->col_offset;
829
        /* wmin: only write imagewidth if raster is bigger. */
830
1.38M
        while (tocol < wmin)
831
1.35M
        {
832
1.35M
            if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col,
833
1.35M
                                            (uint32_t)row +
834
1.35M
                                                (uint32_t)img->row_offset,
835
1.35M
                                            0, 0) == (tmsize_t)(-1) &&
836
1.28M
                (buf == NULL || img->stoponerr))
837
915
            {
838
915
                ret = 0;
839
915
                break;
840
915
            }
841
1.35M
            pos = (((uint32_t)row + (uint32_t)img->row_offset) % th) *
842
1.35M
                      TIFFTileRowSize(tif) +
843
1.35M
                  ((tmsize_t)fromskew * img->samplesperpixel);
844
1.35M
            if (tocol + this_tw > wmin)
845
21.1k
            {
846
                /*
847
                 * Rightmost tile is clipped on right side.
848
                 */
849
21.1k
                fromskew = (int32_t)(tw - (wmin - tocol));
850
21.1k
                this_tw = (uint32_t)((int32_t)tw - fromskew);
851
21.1k
                this_toskew = (int32_t)(toskew + (int32_t)(uint32_t)fromskew);
852
21.1k
            }
853
1.35M
            tmsize_t roffset = (tmsize_t)y * w + tocol;
854
1.35M
            (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
855
1.35M
                   this_toskew, buf + pos);
856
1.35M
            tocol += this_tw;
857
1.35M
            col += this_tw;
858
            /*
859
             * After the leftmost tile, tiles are no longer clipped on left
860
             * side.
861
             */
862
1.35M
            fromskew = 0;
863
1.35M
            this_tw = tw;
864
1.35M
            this_toskew = toskew;
865
1.35M
        }
866
867
29.4k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
868
29.4k
                                                 : (int32_t)nrow);
869
29.4k
    }
870
3.69k
    _TIFFfreeExt(img->tif, buf);
871
872
3.69k
    if (flip & FLIP_HORIZONTALLY)
873
195
    {
874
195
        uint32_t line;
875
876
34.3k
        for (line = 0; line < h; line++)
877
34.1k
        {
878
34.1k
            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
34.1k
            uint32_t *right = left + wmin - 1;
882
883
15.1M
            while (left < right)
884
15.0M
            {
885
15.0M
                uint32_t temp = *left;
886
15.0M
                *left = *right;
887
15.0M
                *right = temp;
888
15.0M
                left++;
889
15.0M
                right--;
890
15.0M
            }
891
34.1k
        }
892
195
    }
893
894
3.69k
    return (ret);
895
3.69k
}
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.82k
{
906
1.82k
    TIFF *tif = img->tif;
907
1.82k
    tileSeparateRoutine put = img->put.separate;
908
1.82k
    uint32_t col, row, y, rowstoread;
909
1.82k
    tmsize_t pos;
910
1.82k
    uint32_t tw, th;
911
1.82k
    unsigned char *buf = NULL;
912
1.82k
    unsigned char *p0 = NULL;
913
1.82k
    unsigned char *p1 = NULL;
914
1.82k
    unsigned char *p2 = NULL;
915
1.82k
    unsigned char *pa = NULL;
916
1.82k
    tmsize_t tilesize;
917
1.82k
    tmsize_t bufsize;
918
1.82k
    int32_t fromskew, toskew;
919
1.82k
    int alpha = img->alpha;
920
1.82k
    uint32_t nrow;
921
1.82k
    int ret = 1, flip;
922
1.82k
    uint16_t colorchannels;
923
1.82k
    uint32_t this_tw, tocol;
924
1.82k
    int32_t this_toskew, leftmost_toskew;
925
1.82k
    int32_t leftmost_fromskew;
926
1.82k
    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.82k
    uint32_t wmin;
931
1.82k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
932
1.82k
    {
933
1.82k
        wmin = TIFFmin(w, img->width - (uint32_t)img->col_offset);
934
1.82k
    }
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.82k
    tilesize = TIFFTileSize(tif);
945
1.82k
    bufsize =
946
1.82k
        _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate");
947
1.82k
    if (bufsize == 0)
948
0
    {
949
0
        return (0);
950
0
    }
951
952
1.82k
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
953
1.82k
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
954
955
1.82k
    flip = setorientation(img);
956
1.82k
    if (flip & FLIP_VERTICALLY)
957
1.72k
    {
958
1.72k
        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.72k
        y = h - 1;
965
1.72k
        toskew = -(int32_t)(tw + w);
966
1.72k
    }
967
106
    else
968
106
    {
969
106
        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
106
        y = 0;
976
106
        toskew = -(int32_t)(tw - w);
977
106
    }
978
979
1.82k
    switch (img->photometric)
980
1.82k
    {
981
79
        case PHOTOMETRIC_MINISWHITE:
982
309
        case PHOTOMETRIC_MINISBLACK:
983
309
        case PHOTOMETRIC_PALETTE:
984
309
            colorchannels = 1;
985
309
            break;
986
987
1.51k
        default:
988
1.51k
            colorchannels = 3;
989
1.51k
            break;
990
1.82k
    }
991
992
1.82k
    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.82k
    leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
1002
1.82k
    leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
1003
1.82k
    int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
1004
1.82k
    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.82k
    leftmost_toskew = (int32_t)skew_i64;
1011
5.47k
    for (row = 0; ret != 0 && row < h; row += nrow)
1012
3.64k
    {
1013
3.64k
        rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
1014
3.64k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1015
3.64k
        fromskew = leftmost_fromskew;
1016
3.64k
        this_tw = leftmost_tw;
1017
3.64k
        this_toskew = leftmost_toskew;
1018
3.64k
        tocol = 0;
1019
3.64k
        col = (uint32_t)img->col_offset;
1020
        /* wmin: only write imagewidth if raster is bigger. */
1021
22.9k
        while (tocol < wmin)
1022
19.6k
        {
1023
19.6k
            if (buf == NULL)
1024
1.82k
            {
1025
1.82k
                if (_TIFFReadTileAndAllocBuffer(
1026
1.82k
                        tif, (void **)&buf, bufsize, col,
1027
1.82k
                        (uint32_t)((int32_t)row + img->row_offset), 0,
1028
1.82k
                        0) == (tmsize_t)(-1) &&
1029
340
                    (buf == NULL || img->stoponerr))
1030
340
                {
1031
340
                    ret = 0;
1032
340
                    break;
1033
340
                }
1034
1.48k
                p0 = buf;
1035
1.48k
                if (colorchannels == 1)
1036
98
                {
1037
98
                    p2 = p1 = p0;
1038
98
                    pa = (alpha ? (p0 + 3 * tilesize) : NULL);
1039
98
                }
1040
1.38k
                else
1041
1.38k
                {
1042
1.38k
                    p1 = p0 + tilesize;
1043
1.38k
                    p2 = p1 + tilesize;
1044
1.38k
                    pa = (alpha ? (p2 + tilesize) : NULL);
1045
1.38k
                }
1046
1.48k
            }
1047
17.8k
            else if (TIFFReadTile(tif, p0, col,
1048
17.8k
                                  (uint32_t)((int32_t)row + img->row_offset), 0,
1049
17.8k
                                  0) == (tmsize_t)(-1) &&
1050
16.7k
                     img->stoponerr)
1051
0
            {
1052
0
                ret = 0;
1053
0
                break;
1054
0
            }
1055
19.3k
            if (colorchannels > 1 &&
1056
17.9k
                TIFFReadTile(tif, p1, col,
1057
17.9k
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1058
17.9k
                             (uint16_t)1) == (tmsize_t)(-1) &&
1059
17.4k
                img->stoponerr)
1060
0
            {
1061
0
                ret = 0;
1062
0
                break;
1063
0
            }
1064
19.3k
            if (colorchannels > 1 &&
1065
17.9k
                TIFFReadTile(tif, p2, col,
1066
17.9k
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1067
17.9k
                             (uint16_t)2) == (tmsize_t)(-1) &&
1068
17.6k
                img->stoponerr)
1069
0
            {
1070
0
                ret = 0;
1071
0
                break;
1072
0
            }
1073
19.3k
            if (alpha &&
1074
1.42k
                TIFFReadTile(tif, pa, col,
1075
1.42k
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1076
1.42k
                             (uint16_t)colorchannels) == (tmsize_t)(-1) &&
1077
1.41k
                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
19.3k
            pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) % th) *
1086
19.3k
                      TIFFTileRowSize(tif) +
1087
19.3k
                  (tmsize_t)fromskew;
1088
19.3k
            if (tocol + this_tw > wmin)
1089
3.17k
            {
1090
                /*
1091
                 * Rightmost tile is clipped on right side.
1092
                 */
1093
3.17k
                fromskew = (int32_t)(tw - (wmin - tocol));
1094
3.17k
                this_tw = (uint32_t)((int32_t)tw - fromskew);
1095
3.17k
                this_toskew = (int32_t)(toskew + (int32_t)(uint32_t)fromskew);
1096
3.17k
            }
1097
19.3k
            tmsize_t roffset = (tmsize_t)y * w + tocol;
1098
19.3k
            (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
1099
19.3k
                   this_toskew, p0 + pos, p1 + pos, p2 + pos,
1100
19.3k
                   (alpha ? (pa + pos) : NULL));
1101
19.3k
            tocol += this_tw;
1102
19.3k
            col += this_tw;
1103
            /*
1104
             * After the leftmost tile, tiles are no longer clipped on left
1105
             * side.
1106
             */
1107
19.3k
            fromskew = 0;
1108
19.3k
            this_tw = tw;
1109
19.3k
            this_toskew = toskew;
1110
19.3k
        }
1111
1112
3.64k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1113
3.64k
                                                 : (int32_t)nrow);
1114
3.64k
    }
1115
1116
1.82k
    if (flip & FLIP_HORIZONTALLY)
1117
126
    {
1118
126
        uint32_t line;
1119
1120
27.3k
        for (line = 0; line < h; line++)
1121
27.2k
        {
1122
27.2k
            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.2k
            uint32_t *right = left + wmin - 1;
1126
1127
22.4M
            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.2k
        }
1136
126
    }
1137
1138
1.82k
    _TIFFfreeExt(img->tif, buf);
1139
1.82k
    return (ret);
1140
1.82k
}
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
18.2k
{
1151
18.2k
    TIFF *tif = img->tif;
1152
18.2k
    tileContigRoutine put = img->put.contig;
1153
18.2k
    uint32_t row, y, nrow, nrowsub, rowstoread;
1154
18.2k
    tmsize_t pos;
1155
18.2k
    unsigned char *buf = NULL;
1156
18.2k
    uint32_t rowsperstrip;
1157
18.2k
    uint16_t subsamplinghor, subsamplingver;
1158
18.2k
    uint32_t imagewidth = img->width;
1159
18.2k
    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
18.2k
    int32_t fromskew, toskew;
1163
18.2k
    int ret = 1, flip;
1164
18.2k
    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
18.2k
    uint32_t wmin;
1169
18.2k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
1170
18.2k
    {
1171
18.2k
        wmin = TIFFmin(w, imagewidth - (uint32_t)img->col_offset);
1172
18.2k
    }
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
18.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor,
1183
18.2k
                          &subsamplingver);
1184
18.2k
    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
18.2k
    maxstripsize = TIFFStripSize(tif);
1192
1193
18.2k
    flip = setorientation(img);
1194
18.2k
    if (flip & FLIP_VERTICALLY)
1195
17.9k
    {
1196
17.9k
        if (w > INT_MAX / 2)
1197
0
        {
1198
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
1199
0
            return (0);
1200
0
        }
1201
17.9k
        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.9k
        toskew = -(int32_t)(w + wmin);
1205
17.9k
    }
1206
221
    else
1207
221
    {
1208
221
        y = 0;
1209
        /* Skew forward to the end of the raster width of the row currently
1210
         * copied. */
1211
221
        toskew = (int32_t)(w - wmin);
1212
221
    }
1213
1214
18.2k
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1215
18.2k
    if (rowsperstrip == 0)
1216
0
    {
1217
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1218
0
        return (0);
1219
0
    }
1220
1221
18.2k
    scanline = TIFFScanlineSize(tif);
1222
18.2k
    fromskew = (int32_t)(w < imagewidth ? imagewidth - w : 0);
1223
99.1k
    for (row = 0; row < h; row += nrow)
1224
86.6k
    {
1225
86.6k
        uint32_t temp;
1226
86.6k
        rowstoread = rowsperstrip -
1227
86.6k
                     (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip;
1228
86.6k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1229
86.6k
        nrowsub = nrow;
1230
86.6k
        if ((nrowsub % subsamplingver) != 0)
1231
38.6k
            nrowsub += subsamplingver - nrowsub % subsamplingver;
1232
86.6k
        temp =
1233
86.6k
            (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrowsub;
1234
86.6k
        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
86.6k
        if (_TIFFReadEncodedStripAndAllocBuffer(
1241
86.6k
                tif,
1242
86.6k
                TIFFComputeStrip(tif,
1243
86.6k
                                 (uint32_t)((int32_t)row + img->row_offset), 0),
1244
86.6k
                (void **)(&buf), maxstripsize,
1245
86.6k
                (tmsize_t)((size_t)temp * (size_t)scanline)) ==
1246
86.6k
                (tmsize_t)(-1) &&
1247
65.9k
            (buf == NULL || img->stoponerr))
1248
5.69k
        {
1249
5.69k
            ret = 0;
1250
5.69k
            break;
1251
5.69k
        }
1252
1253
80.9k
        pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) %
1254
80.9k
                         rowsperstrip) *
1255
80.9k
                  scanline +
1256
80.9k
              ((tmsize_t)img->col_offset * img->samplesperpixel);
1257
80.9k
        tmsize_t roffset = (tmsize_t)y * w;
1258
80.9k
        (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
1259
80.9k
               buf + pos);
1260
80.9k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1261
80.9k
                                                 : (int32_t)nrow);
1262
80.9k
    }
1263
1264
18.2k
    if (flip & FLIP_HORIZONTALLY)
1265
301
    {
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
301
        uint32_t line;
1271
1272
49.4k
        for (line = 0; line < h; line++)
1273
49.1k
        {
1274
49.1k
            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
49.1k
            uint32_t *right = left + wmin - 1;
1278
1279
9.52M
            while (left < right)
1280
9.47M
            {
1281
9.47M
                uint32_t temp = *left;
1282
9.47M
                *left = *right;
1283
9.47M
                *right = temp;
1284
9.47M
                left++;
1285
9.47M
                right--;
1286
9.47M
            }
1287
49.1k
        }
1288
301
    }
1289
1290
18.2k
    _TIFFfreeExt(img->tif, buf);
1291
18.2k
    return (ret);
1292
18.2k
}
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.07k
{
1303
2.07k
    TIFF *tif = img->tif;
1304
2.07k
    tileSeparateRoutine put = img->put.separate;
1305
2.07k
    unsigned char *buf = NULL;
1306
2.07k
    unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;
1307
2.07k
    uint32_t row, y, nrow, rowstoread;
1308
2.07k
    tmsize_t pos;
1309
2.07k
    tmsize_t scanline;
1310
2.07k
    uint32_t rowsperstrip, offset_row;
1311
2.07k
    uint32_t imagewidth = img->width;
1312
2.07k
    tmsize_t stripsize;
1313
2.07k
    tmsize_t bufsize;
1314
2.07k
    int32_t fromskew, toskew;
1315
2.07k
    int alpha = img->alpha;
1316
2.07k
    int ret = 1, flip;
1317
2.07k
    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.07k
    uint32_t wmin;
1322
2.07k
    if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
1323
2.07k
    {
1324
2.07k
        wmin = TIFFmin(w, imagewidth - (uint32_t)img->col_offset);
1325
2.07k
    }
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.07k
    stripsize = TIFFStripSize(tif);
1336
2.07k
    bufsize =
1337
2.07k
        _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate");
1338
2.07k
    if (bufsize == 0)
1339
0
    {
1340
0
        return (0);
1341
0
    }
1342
1343
2.07k
    flip = setorientation(img);
1344
2.07k
    if (flip & FLIP_VERTICALLY)
1345
1.97k
    {
1346
1.97k
        if (w > INT_MAX / 2)
1347
0
        {
1348
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
1349
0
            return (0);
1350
0
        }
1351
1.97k
        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.97k
        toskew = -(int32_t)(w + wmin);
1355
1.97k
    }
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.07k
    switch (img->photometric)
1365
2.07k
    {
1366
240
        case PHOTOMETRIC_MINISWHITE:
1367
569
        case PHOTOMETRIC_MINISBLACK:
1368
569
        case PHOTOMETRIC_PALETTE:
1369
569
            colorchannels = 1;
1370
569
            break;
1371
1372
1.50k
        default:
1373
1.50k
            colorchannels = 3;
1374
1.50k
            break;
1375
2.07k
    }
1376
1377
2.07k
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1378
2.07k
    if (rowsperstrip == 0)
1379
0
    {
1380
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1381
0
        return (0);
1382
0
    }
1383
1384
2.07k
    scanline = TIFFScanlineSize(tif);
1385
2.07k
    fromskew = (int32_t)(w < imagewidth ? imagewidth - w : 0);
1386
3.91k
    for (row = 0; row < h; row += nrow)
1387
2.07k
    {
1388
2.07k
        uint32_t temp;
1389
2.07k
        rowstoread =
1390
2.07k
            rowsperstrip -
1391
2.07k
            ((uint32_t)((int32_t)row + img->row_offset) % rowsperstrip);
1392
2.07k
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1393
2.07k
        offset_row = (uint32_t)((int32_t)row + img->row_offset);
1394
2.07k
        temp = (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrow;
1395
2.07k
        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.07k
        if (buf == NULL)
1402
2.07k
        {
1403
2.07k
            if (_TIFFReadEncodedStripAndAllocBuffer(
1404
2.07k
                    tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf,
1405
2.07k
                    bufsize, temp * scanline) == (tmsize_t)(-1) &&
1406
1.12k
                (buf == NULL || img->stoponerr))
1407
231
            {
1408
231
                ret = 0;
1409
231
                break;
1410
231
            }
1411
1.84k
            p0 = buf;
1412
1.84k
            if (colorchannels == 1)
1413
503
            {
1414
503
                p2 = p1 = p0;
1415
503
                pa = (alpha ? (p0 + 3 * stripsize) : NULL);
1416
503
            }
1417
1.33k
            else
1418
1.33k
            {
1419
1.33k
                p1 = p0 + stripsize;
1420
1.33k
                p2 = p1 + stripsize;
1421
1.33k
                pa = (alpha ? (p2 + stripsize) : NULL);
1422
1.33k
            }
1423
1.84k
        }
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.84k
        if (colorchannels > 1 &&
1432
1.33k
            TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1,
1433
1.33k
                                 temp * scanline) == (tmsize_t)(-1) &&
1434
1.09k
            img->stoponerr)
1435
0
        {
1436
0
            ret = 0;
1437
0
            break;
1438
0
        }
1439
1.84k
        if (colorchannels > 1 &&
1440
1.33k
            TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2,
1441
1.33k
                                 temp * scanline) == (tmsize_t)(-1) &&
1442
1.13k
            img->stoponerr)
1443
0
        {
1444
0
            ret = 0;
1445
0
            break;
1446
0
        }
1447
1.84k
        if (alpha)
1448
627
        {
1449
627
            if (TIFFReadEncodedStrip(
1450
627
                    tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa,
1451
627
                    temp * scanline) == (tmsize_t)(-1) &&
1452
365
                img->stoponerr)
1453
0
            {
1454
0
                ret = 0;
1455
0
                break;
1456
0
            }
1457
627
        }
1458
1459
        /* For SEPARATE the pos-offset is per sample and should not be
1460
         * multiplied by img->samplesperpixel. */
1461
1.84k
        pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) %
1462
1.84k
                         rowsperstrip) *
1463
1.84k
                  scanline +
1464
1.84k
              (tmsize_t)img->col_offset;
1465
1.84k
        tmsize_t roffset = (tmsize_t)y * w;
1466
1.84k
        (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
1467
1.84k
               p0 + pos, p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL));
1468
1.84k
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1469
1.84k
                                                 : (int32_t)nrow);
1470
1.84k
    }
1471
1472
2.07k
    if (flip & FLIP_HORIZONTALLY)
1473
180
    {
1474
180
        uint32_t line;
1475
1476
26.6k
        for (line = 0; line < h; line++)
1477
26.4k
        {
1478
26.4k
            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.4k
            uint32_t *right = left + wmin - 1;
1482
1483
10.6M
            while (left < right)
1484
10.6M
            {
1485
10.6M
                uint32_t temp = *left;
1486
10.6M
                *left = *right;
1487
10.6M
                *right = temp;
1488
10.6M
                left++;
1489
10.6M
                right--;
1490
10.6M
            }
1491
26.4k
        }
1492
180
    }
1493
1494
2.07k
    _TIFFfreeExt(img->tif, buf);
1495
2.07k
    return (ret);
1496
2.07k
}
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
343M
    REPEAT4(op);                                                               \
1510
343M
    REPEAT4(op)
1511
#define REPEAT4(op)                                                            \
1512
688M
    REPEAT2(op);                                                               \
1513
688M
    REPEAT2(op)
1514
#define REPEAT2(op)                                                            \
1515
1.44G
    op;                                                                        \
1516
1.44G
    op
1517
#define CASE8(x, op)                                                           \
1518
17.1M
    switch (x)                                                                 \
1519
17.1M
    {                                                                          \
1520
797k
        case 7:                                                                \
1521
797k
            op; /*-fallthrough*/                                               \
1522
1.55M
        case 6:                                                                \
1523
1.55M
            op; /*-fallthrough*/                                               \
1524
2.05M
        case 5:                                                                \
1525
2.05M
            op; /*-fallthrough*/                                               \
1526
3.22M
        case 4:                                                                \
1527
3.22M
            op; /*-fallthrough*/                                               \
1528
5.72M
        case 3:                                                                \
1529
5.72M
            op; /*-fallthrough*/                                               \
1530
8.06M
        case 2:                                                                \
1531
8.06M
            op; /*-fallthrough*/                                               \
1532
17.1M
        case 1:                                                                \
1533
17.1M
            op;                                                                \
1534
17.1M
            break;                                                             \
1535
8.06M
        default:                                                               \
1536
0
            break;                                                             \
1537
17.1M
    }
1538
#define CASE4(x, op)                                                           \
1539
63.9k
    switch (x)                                                                 \
1540
63.9k
    {                                                                          \
1541
17.0k
        case 3:                                                                \
1542
17.0k
            op; /*-fallthrough*/                                               \
1543
24.1k
        case 2:                                                                \
1544
24.1k
            op; /*-fallthrough*/                                               \
1545
63.9k
        case 1:                                                                \
1546
63.9k
            op;                                                                \
1547
63.9k
            break;                                                             \
1548
24.1k
        default:                                                               \
1549
0
            break;                                                             \
1550
63.9k
    }
1551
#define NOP
1552
1553
#define UNROLL8(w, op1, op2)                                                   \
1554
22.2M
    {                                                                          \
1555
22.2M
        uint32_t _x;                                                           \
1556
365M
        for (_x = w; _x >= 8; _x -= 8)                                         \
1557
343M
        {                                                                      \
1558
343M
            op1;                                                               \
1559
343M
            REPEAT8(op2);                                                      \
1560
343M
        }                                                                      \
1561
22.2M
        if (_x > 0)                                                            \
1562
22.2M
        {                                                                      \
1563
17.1M
            op1;                                                               \
1564
17.1M
            CASE8(_x, op2);                                                    \
1565
17.1M
        }                                                                      \
1566
22.2M
    }
1567
#define UNROLL4(w, op1, op2)                                                   \
1568
100k
    {                                                                          \
1569
100k
        uint32_t _x;                                                           \
1570
1.23M
        for (_x = w; _x >= 4; _x -= 4)                                         \
1571
1.13M
        {                                                                      \
1572
1.13M
            op1;                                                               \
1573
1.13M
            REPEAT4(op2);                                                      \
1574
1.13M
        }                                                                      \
1575
100k
        if (_x > 0)                                                            \
1576
100k
        {                                                                      \
1577
63.9k
            op1;                                                               \
1578
63.9k
            CASE4(_x, op2);                                                    \
1579
63.9k
        }                                                                      \
1580
100k
    }
1581
#define UNROLL2(w, op1, op2)                                                   \
1582
221k
    {                                                                          \
1583
221k
        uint32_t _x;                                                           \
1584
66.6M
        for (_x = w; _x >= 2; _x -= 2)                                         \
1585
66.4M
        {                                                                      \
1586
66.4M
            op1;                                                               \
1587
66.4M
            REPEAT2(op2);                                                      \
1588
66.4M
        }                                                                      \
1589
221k
        if (_x)                                                                \
1590
221k
        {                                                                      \
1591
112k
            op1;                                                               \
1592
112k
            op2;                                                               \
1593
112k
        }                                                                      \
1594
221k
    }
1595
1596
#define SKEW(r, g, b, skew)                                                    \
1597
581k
    {                                                                          \
1598
581k
        r += skew;                                                             \
1599
581k
        g += skew;                                                             \
1600
581k
        b += skew;                                                             \
1601
581k
    }
1602
#define SKEW4(r, g, b, a, skew)                                                \
1603
124k
    {                                                                          \
1604
124k
        r += skew;                                                             \
1605
124k
        g += skew;                                                             \
1606
124k
        b += skew;                                                             \
1607
124k
        a += skew;                                                             \
1608
124k
    }
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
35.8M
    ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) |            \
1615
35.8M
     ((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
4.17k
    for (; h > 0; --h)
1632
4.13k
    {
1633
1.80M
        for (x = w; x > 0; --x)
1634
1.80M
        {
1635
1.80M
            *cp++ = PALmap[*pp][0];
1636
1.80M
            pp += samplesperpixel;
1637
1.80M
        }
1638
4.13k
        cp += toskew;
1639
4.13k
        pp += fromskew;
1640
4.13k
    }
1641
35
}
1642
1643
/*
1644
 * 4-bit palette => colormap/RGB
1645
 */
1646
DECLAREContigPutFunc(put4bitcmaptile)
1647
82
{
1648
82
    uint32_t **PALmap = img->PALmap;
1649
1650
82
    (void)x;
1651
82
    (void)y;
1652
82
    fromskew /= 2;
1653
3.27k
    for (; h > 0; --h)
1654
3.18k
    {
1655
3.18k
        uint32_t *bw;
1656
3.18k
        UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
1657
3.18k
        cp += toskew;
1658
3.18k
        pp += fromskew;
1659
3.18k
    }
1660
82
}
1661
1662
/*
1663
 * 2-bit palette => colormap/RGB
1664
 */
1665
DECLAREContigPutFunc(put2bitcmaptile)
1666
208
{
1667
208
    uint32_t **PALmap = img->PALmap;
1668
1669
208
    (void)x;
1670
208
    (void)y;
1671
208
    fromskew /= 4;
1672
9.75k
    for (; h > 0; --h)
1673
9.54k
    {
1674
9.54k
        uint32_t *bw;
1675
9.54k
        UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
1676
9.54k
        cp += toskew;
1677
9.54k
        pp += fromskew;
1678
9.54k
    }
1679
208
}
1680
1681
/*
1682
 * 1-bit palette => colormap/RGB
1683
 */
1684
DECLAREContigPutFunc(put1bitcmaptile)
1685
725
{
1686
725
    uint32_t **PALmap = img->PALmap;
1687
1688
725
    (void)x;
1689
725
    (void)y;
1690
725
    fromskew /= 8;
1691
16.8k
    for (; h > 0; --h)
1692
16.1k
    {
1693
16.1k
        uint32_t *bw;
1694
16.1k
        UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
1695
16.1k
        cp += toskew;
1696
16.1k
        pp += fromskew;
1697
16.1k
    }
1698
725
}
1699
1700
/*
1701
 * 8-bit greyscale => colormap/RGB
1702
 */
1703
DECLAREContigPutFunc(putgreytile)
1704
5.80k
{
1705
5.80k
    int samplesperpixel = img->samplesperpixel;
1706
5.80k
    uint32_t **BWmap = img->BWmap;
1707
1708
5.80k
    (void)y;
1709
662k
    for (; h > 0; --h)
1710
656k
    {
1711
165M
        for (x = w; x > 0; --x)
1712
164M
        {
1713
164M
            *cp++ = BWmap[*pp][0];
1714
164M
            pp += samplesperpixel;
1715
164M
        }
1716
656k
        cp += toskew;
1717
656k
        pp += fromskew;
1718
656k
    }
1719
5.80k
}
1720
1721
/*
1722
 * 8-bit greyscale with associated alpha => colormap/RGBA
1723
 */
1724
DECLAREContigPutFunc(putagreytile)
1725
85
{
1726
85
    int samplesperpixel = img->samplesperpixel;
1727
85
    uint32_t **BWmap = img->BWmap;
1728
1729
85
    (void)y;
1730
8.67k
    for (; h > 0; --h)
1731
8.58k
    {
1732
311k
        for (x = w; x > 0; --x)
1733
302k
        {
1734
302k
            *cp++ = BWmap[*pp][0] & ((uint32_t)*(pp + 1) << 24 | ~A1);
1735
302k
            pp += samplesperpixel;
1736
302k
        }
1737
8.58k
        cp += toskew;
1738
8.58k
        pp += fromskew;
1739
8.58k
    }
1740
85
}
1741
1742
/*
1743
 * 16-bit greyscale => colormap/RGB
1744
 */
1745
DECLAREContigPutFunc(put16bitbwtile)
1746
904
{
1747
904
    int samplesperpixel = img->samplesperpixel;
1748
904
    uint32_t **BWmap = img->BWmap;
1749
    /* Convert pixel skew to byte skew (16-bit samples) */
1750
904
    const tmsize_t fromskewLocal =
1751
904
        (tmsize_t)fromskew * (tmsize_t)(2 * samplesperpixel);
1752
1753
904
    (void)y;
1754
240k
    for (; h > 0; --h)
1755
239k
    {
1756
239k
        uint16_t *wp = (uint16_t *)pp;
1757
1758
49.6M
        for (x = w; x > 0; --x)
1759
49.4M
        {
1760
            /* use high order byte of 16bit value */
1761
1762
49.4M
            *cp++ = BWmap[*wp >> 8][0];
1763
49.4M
            pp += 2 * samplesperpixel;
1764
49.4M
            wp += samplesperpixel;
1765
49.4M
        }
1766
239k
        cp += toskew;
1767
239k
        pp += fromskewLocal;
1768
239k
    }
1769
904
}
1770
1771
/*
1772
 * 1-bit bilevel => colormap/RGB
1773
 */
1774
DECLAREContigPutFunc(put1bitbwtile)
1775
1.38M
{
1776
1.38M
    uint32_t **BWmap = img->BWmap;
1777
1778
1.38M
    (void)x;
1779
1.38M
    (void)y;
1780
1.38M
    fromskew /= 8;
1781
21.4M
    for (; h > 0; --h)
1782
20.0M
    {
1783
20.0M
        uint32_t *bw;
1784
20.0M
        UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
1785
20.0M
        cp += toskew;
1786
20.0M
        pp += fromskew;
1787
20.0M
    }
1788
1.38M
}
1789
1790
/*
1791
 * 2-bit greyscale => colormap/RGB
1792
 */
1793
DECLAREContigPutFunc(put2bitbwtile)
1794
590
{
1795
590
    uint32_t **BWmap = img->BWmap;
1796
1797
590
    (void)x;
1798
590
    (void)y;
1799
590
    fromskew /= 4;
1800
91.3k
    for (; h > 0; --h)
1801
90.7k
    {
1802
90.7k
        uint32_t *bw;
1803
90.7k
        UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
1804
90.7k
        cp += toskew;
1805
90.7k
        pp += fromskew;
1806
90.7k
    }
1807
590
}
1808
1809
/*
1810
 * 4-bit greyscale => colormap/RGB
1811
 */
1812
DECLAREContigPutFunc(put4bitbwtile)
1813
1.15k
{
1814
1.15k
    uint32_t **BWmap = img->BWmap;
1815
1816
1.15k
    (void)x;
1817
1.15k
    (void)y;
1818
1.15k
    fromskew /= 2;
1819
219k
    for (; h > 0; --h)
1820
217k
    {
1821
217k
        uint32_t *bw;
1822
217k
        UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
1823
217k
        cp += toskew;
1824
217k
        pp += fromskew;
1825
217k
    }
1826
1.15k
}
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.43M
    for (; h > 0; --h)
1840
1.39M
    {
1841
1.39M
        UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]);
1842
1.39M
                pp += samplesperpixel);
1843
1.39M
        cp += toskew;
1844
1.39M
        pp += fromskewLocal;
1845
1.39M
    }
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
472
{
1854
472
    int samplesperpixel = img->samplesperpixel;
1855
1856
472
    (void)x;
1857
472
    (void)y;
1858
472
    const tmsize_t fromskewLocal =
1859
472
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1860
153k
    for (; h > 0; --h)
1861
153k
    {
1862
153k
        UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
1863
153k
                pp += samplesperpixel);
1864
153k
        cp += toskew;
1865
153k
        pp += fromskewLocal;
1866
153k
    }
1867
472
}
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.50k
    for (; h > 0; --h)
1880
6.44k
    {
1881
6.44k
        uint32_t r, g, b, a;
1882
6.44k
        uint8_t *m;
1883
795k
        for (x = w; x > 0; --x)
1884
789k
        {
1885
789k
            a = pp[3];
1886
789k
            m = img->UaToAa + ((size_t)a << 8);
1887
789k
            r = m[pp[0]];
1888
789k
            g = m[pp[1]];
1889
789k
            b = m[pp[2]];
1890
789k
            *cp++ = PACK4(r, g, b, a);
1891
789k
            pp += samplesperpixel;
1892
789k
        }
1893
6.44k
        cp += toskew;
1894
6.44k
        pp += fromskewLocal;
1895
6.44k
    }
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
520
{
1982
520
    int samplesperpixel = img->samplesperpixel;
1983
520
    uint16_t r, g, b, k;
1984
1985
520
    (void)x;
1986
520
    (void)y;
1987
520
    const tmsize_t fromskewLocal =
1988
520
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1989
109k
    for (; h > 0; --h)
1990
109k
    {
1991
109k
        UNROLL8(w, NOP, k = (uint16_t)(255 - pp[3]);
1992
109k
                r = (uint16_t)((k * (uint16_t)(255 - pp[0])) / 255);
1993
109k
                g = (uint16_t)((k * (uint16_t)(255 - pp[1])) / 255);
1994
109k
                b = (uint16_t)((k * (uint16_t)(255 - pp[2])) / 255);
1995
109k
                *cp++ = PACK(r, g, b); pp += samplesperpixel);
1996
109k
        cp += toskew;
1997
109k
        pp += fromskewLocal;
1998
109k
    }
1999
520
}
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
18.5k
{
2042
18.5k
    (void)img;
2043
18.5k
    (void)x;
2044
18.5k
    (void)y;
2045
18.5k
    (void)a;
2046
488k
    for (; h > 0; --h)
2047
470k
    {
2048
470k
        UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
2049
470k
        SKEW(r, g, b, fromskew);
2050
470k
        cp += toskew;
2051
470k
    }
2052
18.5k
}
2053
2054
/*
2055
 * 8-bit unpacked samples => RGBA w/ associated alpha
2056
 */
2057
DECLARESepPutFunc(putRGBAAseparate8bittile)
2058
1.90k
{
2059
1.90k
    (void)img;
2060
1.90k
    (void)x;
2061
1.90k
    (void)y;
2062
101k
    for (; h > 0; --h)
2063
99.8k
    {
2064
99.8k
        UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
2065
99.8k
        SKEW4(r, g, b, a, fromskew);
2066
99.8k
        cp += toskew;
2067
99.8k
    }
2068
1.90k
}
2069
2070
/*
2071
 * 8-bit unpacked CMYK samples => RGBA
2072
 */
2073
DECLARESepPutFunc(putCMYKseparate8bittile)
2074
87
{
2075
87
    (void)img;
2076
87
    (void)y;
2077
22.7k
    for (; h > 0; --h)
2078
22.6k
    {
2079
22.6k
        unsigned int rv, gv, bv, kv;
2080
35.0M
        for (x = w; x > 0; --x)
2081
35.0M
        {
2082
35.0M
            kv = 255U - *a++;
2083
35.0M
            rv = (kv * (255U - *r++)) / 255U;
2084
35.0M
            gv = (kv * (255U - *g++)) / 255U;
2085
35.0M
            bv = (kv * (255U - *b++)) / 255U;
2086
35.0M
            *cp++ = PACK4(rv, gv, bv, 255);
2087
35.0M
        }
2088
22.6k
        SKEW4(r, g, b, a, fromskew);
2089
22.6k
        cp += toskew;
2090
22.6k
    }
2091
87
}
2092
2093
/*
2094
 * 8-bit unpacked samples => RGBA w/ unassociated alpha
2095
 */
2096
DECLARESepPutFunc(putRGBUAseparate8bittile)
2097
58
{
2098
58
    (void)img;
2099
58
    (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.87k
        {
2106
6.87k
            av = *a++;
2107
6.87k
            m = img->UaToAa + ((size_t)av << 8);
2108
6.87k
            rv = m[*r++];
2109
6.87k
            gv = m[*g++];
2110
6.87k
            bv = m[*b++];
2111
6.87k
            *cp++ = PACK4(rv, gv, bv, av);
2112
6.87k
        }
2113
1.76k
        SKEW4(r, g, b, a, fromskew);
2114
1.76k
        cp += toskew;
2115
1.76k
    }
2116
58
}
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
918
{
2194
918
    float X, Y, Z;
2195
918
    uint32_t r, g, b;
2196
918
    (void)y;
2197
918
    const tmsize_t fromskewLocal = (tmsize_t)fromskew * (tmsize_t)3;
2198
58.2k
    for (; h > 0; --h)
2199
57.3k
    {
2200
5.74M
        for (x = w; x > 0; --x)
2201
5.68M
        {
2202
5.68M
            TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0],
2203
5.68M
                            (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z);
2204
5.68M
            TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
2205
5.68M
            *cp++ = PACK(r, g, b);
2206
5.68M
            pp += 3;
2207
5.68M
        }
2208
57.3k
        cp += toskew;
2209
57.3k
        pp += fromskewLocal;
2210
57.3k
    }
2211
918
}
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
89.5M
    {                                                                          \
2244
89.5M
        uint32_t r, g, b;                                                      \
2245
89.5M
        TIFFYCbCrtoRGB(img->ycbcr, (Y), (int32_t)Cb, (int32_t)Cr, &r, &g, &b); \
2246
89.5M
        dst = PACK(r, g, b);                                                   \
2247
89.5M
    }
2248
2249
/*
2250
 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
2251
 */
2252
DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
2253
173
{
2254
173
    uint32_t *cp1 = cp + w + toskew;
2255
173
    uint32_t *cp2 = cp1 + w + toskew;
2256
173
    uint32_t *cp3 = cp2 + w + toskew;
2257
173
    const tmsize_t incr = 3 * (tmsize_t)w + 4 * (tmsize_t)toskew;
2258
2259
173
    (void)y;
2260
    /* adjust fromskew */
2261
173
    const tmsize_t fromskewLocal =
2262
173
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 4 + 2);
2263
173
    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
139
    else
2304
139
    {
2305
5.45k
        while (h > 0)
2306
5.45k
        {
2307
189k
            for (x = w; x > 0;)
2308
183k
            {
2309
183k
                int32_t Cb = pp[16];
2310
183k
                int32_t Cr = pp[17];
2311
183k
                switch (x)
2312
183k
                {
2313
179k
                    default:
2314
179k
                        switch (h)
2315
179k
                        {
2316
175k
                            default:
2317
175k
                                YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
2318
176k
                            case 3:
2319
176k
                                YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
2320
177k
                            case 2:
2321
177k
                                YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
2322
179k
                            case 1:
2323
179k
                                YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
2324
179k
                        } /* FALLTHROUGH */
2325
180k
                    case 3:
2326
180k
                        switch (h)
2327
180k
                        {
2328
176k
                            default:
2329
176k
                                YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
2330
177k
                            case 3:
2331
177k
                                YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
2332
178k
                            case 2:
2333
178k
                                YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
2334
180k
                            case 1:
2335
180k
                                YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
2336
180k
                        } /* FALLTHROUGH */
2337
181k
                    case 2:
2338
181k
                        switch (h)
2339
181k
                        {
2340
177k
                            default:
2341
177k
                                YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
2342
178k
                            case 3:
2343
178k
                                YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */
2344
179k
                            case 2:
2345
179k
                                YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
2346
181k
                            case 1:
2347
181k
                                YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
2348
181k
                        } /* FALLTHROUGH */
2349
183k
                    case 1:
2350
183k
                        switch (h)
2351
183k
                        {
2352
179k
                            default:
2353
179k
                                YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
2354
180k
                            case 3:
2355
180k
                                YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */
2356
181k
                            case 2:
2357
181k
                                YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
2358
183k
                            case 1:
2359
183k
                                YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
2360
183k
                        } /* FALLTHROUGH */
2361
183k
                }
2362
183k
                if (x < 4)
2363
4.46k
                {
2364
4.46k
                    cp += x;
2365
4.46k
                    cp1 += x;
2366
4.46k
                    cp2 += x;
2367
4.46k
                    cp3 += x;
2368
4.46k
                    x = 0;
2369
4.46k
                }
2370
179k
                else
2371
179k
                {
2372
179k
                    cp += 4;
2373
179k
                    cp1 += 4;
2374
179k
                    cp2 += 4;
2375
179k
                    cp3 += 4;
2376
179k
                    x -= 4;
2377
179k
                }
2378
183k
                pp += 18;
2379
183k
            }
2380
5.45k
            if (h <= 4)
2381
139
                break;
2382
5.31k
            h -= 4;
2383
5.31k
            cp += incr;
2384
5.31k
            cp1 += incr;
2385
5.31k
            cp2 += incr;
2386
5.31k
            cp3 += incr;
2387
5.31k
            pp += fromskewLocal;
2388
5.31k
        }
2389
139
    }
2390
173
}
2391
2392
/*
2393
 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
2394
 */
2395
DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
2396
123
{
2397
123
    uint32_t *cp1 = cp + w + toskew;
2398
123
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2399
2400
123
    (void)y;
2401
123
    const tmsize_t fromskewLocal =
2402
123
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 2 + 2);
2403
123
    if ((w & 3) == 0 && (h & 1) == 0)
2404
36
    {
2405
3.75k
        for (; h >= 2; h -= 2)
2406
3.71k
        {
2407
3.71k
            x = w >> 2;
2408
3.71k
            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.71k
            cp += incr;
2427
3.71k
            cp1 += incr;
2428
3.71k
            pp += fromskewLocal;
2429
3.71k
        }
2430
36
    }
2431
87
    else
2432
87
    {
2433
7.75k
        while (h > 0)
2434
7.75k
        {
2435
539k
            for (x = w; x > 0;)
2436
532k
            {
2437
532k
                int32_t Cb = pp[8];
2438
532k
                int32_t Cr = pp[9];
2439
532k
                switch (x)
2440
532k
                {
2441
524k
                    default:
2442
524k
                        switch (h)
2443
524k
                        {
2444
523k
                            default:
2445
523k
                                YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
2446
524k
                            case 1:
2447
524k
                                YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
2448
524k
                        } /* FALLTHROUGH */
2449
527k
                    case 3:
2450
527k
                        switch (h)
2451
527k
                        {
2452
525k
                            default:
2453
525k
                                YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
2454
527k
                            case 1:
2455
527k
                                YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
2456
527k
                        } /* FALLTHROUGH */
2457
529k
                    case 2:
2458
529k
                        switch (h)
2459
529k
                        {
2460
528k
                            default:
2461
528k
                                YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
2462
529k
                            case 1:
2463
529k
                                YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
2464
529k
                        } /* FALLTHROUGH */
2465
532k
                    case 1:
2466
532k
                        switch (h)
2467
532k
                        {
2468
531k
                            default:
2469
531k
                                YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
2470
532k
                            case 1:
2471
532k
                                YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
2472
532k
                        } /* FALLTHROUGH */
2473
532k
                }
2474
532k
                if (x < 4)
2475
7.59k
                {
2476
7.59k
                    cp += x;
2477
7.59k
                    cp1 += x;
2478
7.59k
                    x = 0;
2479
7.59k
                }
2480
524k
                else
2481
524k
                {
2482
524k
                    cp += 4;
2483
524k
                    cp1 += 4;
2484
524k
                    x -= 4;
2485
524k
                }
2486
532k
                pp += 10;
2487
532k
            }
2488
7.75k
            if (h <= 2)
2489
87
                break;
2490
7.67k
            h -= 2;
2491
7.67k
            cp += incr;
2492
7.67k
            cp1 += incr;
2493
7.67k
            pp += fromskewLocal;
2494
7.67k
        }
2495
87
    }
2496
123
}
2497
2498
/*
2499
 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
2500
 */
2501
DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
2502
97
{
2503
97
    (void)y;
2504
97
    const tmsize_t fromskewLocal =
2505
97
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 1 + 2);
2506
97
    do
2507
4.64k
    {
2508
4.64k
        x = w >> 2;
2509
108k
        while (x > 0)
2510
104k
        {
2511
104k
            int32_t Cb = pp[4];
2512
104k
            int32_t Cr = pp[5];
2513
2514
104k
            YCbCrtoRGB(cp[0], pp[0]);
2515
104k
            YCbCrtoRGB(cp[1], pp[1]);
2516
104k
            YCbCrtoRGB(cp[2], pp[2]);
2517
104k
            YCbCrtoRGB(cp[3], pp[3]);
2518
2519
104k
            cp += 4;
2520
104k
            pp += 6;
2521
104k
            x--;
2522
104k
        }
2523
2524
4.64k
        if ((w & 3) != 0)
2525
3.35k
        {
2526
3.35k
            int32_t Cb = pp[4];
2527
3.35k
            int32_t Cr = pp[5];
2528
2529
3.35k
            switch ((w & 3))
2530
3.35k
            {
2531
849
                case 3:
2532
849
                    YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/
2533
2.05k
                case 2:
2534
2.05k
                    YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/
2535
3.35k
                case 1:
2536
3.35k
                    YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/
2537
3.35k
                case 0:
2538
3.35k
                    break;
2539
0
                default:
2540
0
                    break;
2541
3.35k
            }
2542
2543
3.35k
            cp += (w & 3);
2544
3.35k
            pp += 6;
2545
3.35k
        }
2546
2547
4.64k
        cp += toskew;
2548
4.64k
        pp += fromskewLocal;
2549
4.64k
    } while (--h);
2550
97
}
2551
2552
/*
2553
 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
2554
 */
2555
DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
2556
1.24k
{
2557
1.24k
    uint32_t *cp2;
2558
1.24k
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2559
1.24k
    (void)y;
2560
1.24k
    const tmsize_t fromskewLocal =
2561
1.24k
        (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 2 + 2);
2562
1.24k
    cp2 = cp + w + toskew;
2563
109k
    while (h >= 2)
2564
108k
    {
2565
108k
        x = w;
2566
19.8M
        while (x >= 2)
2567
19.7M
        {
2568
19.7M
            uint32_t Cb = pp[4];
2569
19.7M
            uint32_t Cr = pp[5];
2570
19.7M
            YCbCrtoRGB(cp[0], pp[0]);
2571
19.7M
            YCbCrtoRGB(cp[1], pp[1]);
2572
19.7M
            YCbCrtoRGB(cp2[0], pp[2]);
2573
19.7M
            YCbCrtoRGB(cp2[1], pp[3]);
2574
19.7M
            cp += 2;
2575
19.7M
            cp2 += 2;
2576
19.7M
            pp += 6;
2577
19.7M
            x -= 2;
2578
19.7M
        }
2579
108k
        if (x == 1)
2580
42.8k
        {
2581
42.8k
            uint32_t Cb = pp[4];
2582
42.8k
            uint32_t Cr = pp[5];
2583
42.8k
            YCbCrtoRGB(cp[0], pp[0]);
2584
42.8k
            YCbCrtoRGB(cp2[0], pp[2]);
2585
42.8k
            cp++;
2586
42.8k
            cp2++;
2587
42.8k
            pp += 6;
2588
42.8k
        }
2589
108k
        cp += incr;
2590
108k
        cp2 += incr;
2591
108k
        pp += fromskewLocal;
2592
108k
        h -= 2;
2593
108k
    }
2594
1.24k
    if (h == 1)
2595
204
    {
2596
204
        x = w;
2597
31.1k
        while (x >= 2)
2598
30.9k
        {
2599
30.9k
            uint32_t Cb = pp[4];
2600
30.9k
            uint32_t Cr = pp[5];
2601
30.9k
            YCbCrtoRGB(cp[0], pp[0]);
2602
30.9k
            YCbCrtoRGB(cp[1], pp[1]);
2603
30.9k
            cp += 2;
2604
30.9k
            cp2 += 2;
2605
30.9k
            pp += 6;
2606
30.9k
            x -= 2;
2607
30.9k
        }
2608
204
        if (x == 1)
2609
78
        {
2610
78
            uint32_t Cb = pp[4];
2611
78
            uint32_t Cr = pp[5];
2612
78
            YCbCrtoRGB(cp[0], pp[0]);
2613
78
        }
2614
204
    }
2615
1.24k
}
2616
2617
/*
2618
 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
2619
 */
2620
DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
2621
52
{
2622
52
    (void)y;
2623
52
    const tmsize_t fromskewLocal =
2624
52
        (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 1 + 2);
2625
52
    do
2626
2.39k
    {
2627
2.39k
        x = w >> 1;
2628
56.8k
        while (x > 0)
2629
54.4k
        {
2630
54.4k
            int32_t Cb = pp[2];
2631
54.4k
            int32_t Cr = pp[3];
2632
2633
54.4k
            YCbCrtoRGB(cp[0], pp[0]);
2634
54.4k
            YCbCrtoRGB(cp[1], pp[1]);
2635
2636
54.4k
            cp += 2;
2637
54.4k
            pp += 4;
2638
54.4k
            x--;
2639
54.4k
        }
2640
2641
2.39k
        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.39k
        cp += toskew;
2653
2.39k
        pp += fromskewLocal;
2654
2.39k
    } while (--h);
2655
52
}
2656
2657
/*
2658
 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
2659
 */
2660
DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
2661
41
{
2662
41
    uint32_t *cp2;
2663
41
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2664
41
    (void)y;
2665
41
    const tmsize_t fromskewLocal =
2666
41
        (tmsize_t)(fromskew / 1) * (tmsize_t)(1 * 2 + 2);
2667
41
    cp2 = cp + w + toskew;
2668
1.16k
    while (h >= 2)
2669
1.12k
    {
2670
1.12k
        x = w;
2671
1.12k
        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.12k
        cp += incr;
2682
1.12k
        cp2 += incr;
2683
1.12k
        pp += fromskewLocal;
2684
1.12k
        h -= 2;
2685
1.12k
    }
2686
41
    if (h == 1)
2687
30
    {
2688
30
        x = w;
2689
30
        do
2690
831
        {
2691
831
            uint32_t Cb = pp[2];
2692
831
            uint32_t Cr = pp[3];
2693
831
            YCbCrtoRGB(cp[0], pp[0]);
2694
831
            cp++;
2695
831
            pp += 4;
2696
831
        } while (--x);
2697
30
    }
2698
41
}
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
558
{
2730
558
    (void)y;
2731
558
    (void)a;
2732
    /* TODO: naming of input vars is still off, change obfuscating declaration
2733
     * inside define, or resolve obfuscation */
2734
111k
    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
558
}
2747
#undef YCbCrtoRGB
2748
2749
static int isInRefBlackWhiteRange(float f)
2750
27.4k
{
2751
27.4k
    return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF;
2752
27.4k
}
2753
2754
static int initYCbCrConversion(TIFFRGBAImage *img)
2755
4.60k
{
2756
4.60k
    static const char module[] = "initYCbCrConversion";
2757
2758
4.60k
    float *luma, *refBlackWhite;
2759
2760
4.60k
    if (img->ycbcr == NULL)
2761
4.60k
    {
2762
4.60k
        img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt(
2763
4.60k
            img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) +
2764
4.60k
                          4 * 256 * sizeof(TIFFRGBValue) +
2765
4.60k
                          2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t));
2766
4.60k
        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.60k
    }
2773
2774
4.60k
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
2775
4.60k
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
2776
4.60k
                          &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.60k
    if (isnan(luma[0]) || isnan(luma[1]) || TIFF_FLOAT_EQ(luma[1], 0.0f) ||
2781
4.60k
        isnan(luma[2]))
2782
18
    {
2783
18
        TIFFErrorExtR(img->tif, module,
2784
18
                      "Invalid values for YCbCrCoefficients tag");
2785
18
        return (0);
2786
18
    }
2787
2788
4.59k
    if (!isInRefBlackWhiteRange(refBlackWhite[0]) ||
2789
4.58k
        !isInRefBlackWhiteRange(refBlackWhite[1]) ||
2790
4.57k
        !isInRefBlackWhiteRange(refBlackWhite[2]) ||
2791
4.57k
        !isInRefBlackWhiteRange(refBlackWhite[3]) ||
2792
4.56k
        !isInRefBlackWhiteRange(refBlackWhite[4]) ||
2793
4.56k
        !isInRefBlackWhiteRange(refBlackWhite[5]))
2794
32
    {
2795
32
        TIFFErrorExtR(img->tif, module,
2796
32
                      "Invalid values for ReferenceBlackWhite tag");
2797
32
        return (0);
2798
32
    }
2799
2800
4.55k
    if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
2801
0
        return (0);
2802
4.55k
    return (1);
2803
4.55k
}
2804
2805
static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img)
2806
247
{
2807
247
    static const char module[] = "initCIELabConversion";
2808
2809
247
    float *whitePoint;
2810
247
    float refWhite[3];
2811
2812
247
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
2813
247
    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
245
    if (!img->cielab)
2820
245
    {
2821
245
        img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt(
2822
245
            img->tif, sizeof(TIFFCIELabToRGB));
2823
245
        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
245
    }
2830
2831
245
    refWhite[1] = 100.0f;
2832
245
    refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
2833
245
    refWhite[2] =
2834
245
        (1.0f - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];
2835
245
    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
245
    if (img->bitspersample == 8)
2844
242
        return putcontig8bitCIELab8;
2845
3
    else if (img->bitspersample == 16)
2846
3
        return putcontig8bitCIELab16;
2847
0
    return NULL;
2848
245
}
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.5k
{
2859
11.5k
    TIFFRGBValue *Map = img->Map;
2860
11.5k
    int bitspersample = img->bitspersample;
2861
11.5k
    int nsamples = 8 / bitspersample;
2862
11.5k
    int i;
2863
11.5k
    uint32_t *p;
2864
2865
11.5k
    if (nsamples == 0)
2866
242
        nsamples = 1;
2867
2868
11.5k
    img->BWmap = (uint32_t **)_TIFFmallocExt(
2869
11.5k
        img->tif,
2870
11.5k
        (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
2871
11.5k
                   ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
2872
11.5k
    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.5k
    p = (uint32_t *)(img->BWmap + 256);
2879
2.96M
    for (i = 0; i < 256; i++)
2880
2.95M
    {
2881
2.95M
        TIFFRGBValue c;
2882
2.95M
        img->BWmap[i] = p;
2883
2.95M
        switch (bitspersample)
2884
2.95M
        {
2885
0
#define GREY(x)                                                                \
2886
17.0M
    c = Map[x];                                                                \
2887
17.0M
    *p++ = PACK(c, c, c);
2888
1.95M
            case 1:
2889
1.95M
                GREY(i >> 7);
2890
1.95M
                GREY((i >> 6) & 1);
2891
1.95M
                GREY((i >> 5) & 1);
2892
1.95M
                GREY((i >> 4) & 1);
2893
1.95M
                GREY((i >> 3) & 1);
2894
1.95M
                GREY((i >> 2) & 1);
2895
1.95M
                GREY((i >> 1) & 1);
2896
1.95M
                GREY(i & 1);
2897
1.95M
                break;
2898
86.0k
            case 2:
2899
86.0k
                GREY(i >> 6);
2900
86.0k
                GREY((i >> 4) & 3);
2901
86.0k
                GREY((i >> 2) & 3);
2902
86.0k
                GREY(i & 3);
2903
86.0k
                break;
2904
212k
            case 4:
2905
212k
                GREY(i >> 4);
2906
212k
                GREY(i & 0xf);
2907
212k
                break;
2908
643k
            case 8:
2909
705k
            case 16:
2910
705k
                GREY(i);
2911
705k
                break;
2912
0
            default:
2913
0
                break;
2914
2.95M
        }
2915
2.95M
#undef GREY
2916
2.95M
    }
2917
11.5k
    return (1);
2918
11.5k
}
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.5k
{
2927
11.5k
    int32_t x, range;
2928
2929
11.5k
    range = (int32_t)((1U << img->bitspersample) - 1U);
2930
2931
    /* treat 16 bit the same as eight bit */
2932
11.5k
    if (img->bitspersample == 16)
2933
243
        range = 255;
2934
2935
11.5k
    img->Map = (TIFFRGBValue *)_TIFFmallocExt(
2936
11.5k
        img->tif, (tmsize_t)((size_t)(range + 1) * sizeof(TIFFRGBValue)));
2937
11.5k
    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.5k
    if (img->photometric == PHOTOMETRIC_MINISWHITE)
2944
8.07k
    {
2945
450k
        for (x = 0; x <= range; x++)
2946
442k
            img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range);
2947
8.07k
    }
2948
3.48k
    else
2949
3.48k
    {
2950
296k
        for (x = 0; x <= range; x++)
2951
293k
            img->Map[x] = (TIFFRGBValue)((x * 255) / range);
2952
3.48k
    }
2953
11.5k
    if (img->bitspersample <= 16 &&
2954
11.5k
        (img->photometric == PHOTOMETRIC_MINISBLACK ||
2955
8.09k
         img->photometric == PHOTOMETRIC_MINISWHITE))
2956
11.5k
    {
2957
        /*
2958
         * Use photometric mapping table to construct
2959
         * unpacking tables for samples <= 8 bits.
2960
         */
2961
11.5k
        if (!makebwmap(img))
2962
0
            return (0);
2963
        /* no longer need Map, free it */
2964
11.5k
        _TIFFfreeExt(img->tif, img->Map);
2965
11.5k
        img->Map = NULL;
2966
11.5k
    }
2967
11.5k
    return (1);
2968
11.5k
}
2969
2970
static int checkcmap(TIFFRGBAImage *img)
2971
785
{
2972
785
    uint16_t *r = img->redcmap;
2973
785
    uint16_t *g = img->greencmap;
2974
785
    uint16_t *b = img->bluecmap;
2975
785
    long n = 1L << img->bitspersample;
2976
2977
7.16k
    while (n-- > 0)
2978
7.04k
        if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
2979
659
            return (16);
2980
126
    return (8);
2981
785
}
2982
2983
static void cvtcmap(TIFFRGBAImage *img)
2984
659
{
2985
659
    uint16_t *r = img->redcmap;
2986
659
    uint16_t *g = img->greencmap;
2987
659
    uint16_t *b = img->bluecmap;
2988
659
    long i;
2989
2990
8.51k
    for (i = (1L << img->bitspersample) - 1; i >= 0; i--)
2991
7.86k
    {
2992
23.5k
#define CVT(x) ((uint16_t)((x) >> 8))
2993
7.86k
        r[i] = CVT(r[i]);
2994
7.86k
        g[i] = CVT(g[i]);
2995
7.86k
        b[i] = CVT(b[i]);
2996
7.86k
#undef CVT
2997
7.86k
    }
2998
659
}
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
785
{
3009
785
    int bitspersample = img->bitspersample;
3010
785
    int nsamples = 8 / bitspersample;
3011
785
    uint16_t *r = img->redcmap;
3012
785
    uint16_t *g = img->greencmap;
3013
785
    uint16_t *b = img->bluecmap;
3014
785
    uint32_t *p;
3015
785
    int i;
3016
3017
785
    img->PALmap = (uint32_t **)_TIFFmallocExt(
3018
785
        img->tif,
3019
785
        (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
3020
785
                   ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
3021
785
    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
785
    p = (uint32_t *)(img->PALmap + 256);
3028
201k
    for (i = 0; i < 256; i++)
3029
200k
    {
3030
200k
        TIFFRGBValue c;
3031
200k
        img->PALmap[i] = p;
3032
200k
#define CMAP(x)                                                                \
3033
1.14M
    c = (TIFFRGBValue)x;                                                       \
3034
1.14M
    *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff);
3035
200k
        switch (bitspersample)
3036
200k
        {
3037
105k
            case 1:
3038
105k
                CMAP(i >> 7);
3039
105k
                CMAP((i >> 6) & 1);
3040
105k
                CMAP((i >> 5) & 1);
3041
105k
                CMAP((i >> 4) & 1);
3042
105k
                CMAP((i >> 3) & 1);
3043
105k
                CMAP((i >> 2) & 1);
3044
105k
                CMAP((i >> 1) & 1);
3045
105k
                CMAP(i & 1);
3046
105k
                break;
3047
60.9k
            case 2:
3048
60.9k
                CMAP(i >> 6);
3049
60.9k
                CMAP((i >> 4) & 3);
3050
60.9k
                CMAP((i >> 2) & 3);
3051
60.9k
                CMAP(i & 3);
3052
60.9k
                break;
3053
24.3k
            case 4:
3054
24.3k
                CMAP(i >> 4);
3055
24.3k
                CMAP(i & 0xf);
3056
24.3k
                break;
3057
10.2k
            case 8:
3058
10.2k
                CMAP(i);
3059
10.2k
                break;
3060
0
            default:
3061
0
                break;
3062
200k
        }
3063
200k
#undef CMAP
3064
200k
    }
3065
785
    return (1);
3066
785
}
3067
3068
/*
3069
 * Construct any mapping table used
3070
 * by the associated put routine.
3071
 */
3072
static int buildMap(TIFFRGBAImage *img)
3073
13.1k
{
3074
13.1k
    switch (img->photometric)
3075
13.1k
    {
3076
0
        case PHOTOMETRIC_RGB:
3077
0
        case PHOTOMETRIC_YCBCR:
3078
552
        case PHOTOMETRIC_SEPARATED:
3079
552
            if (img->bitspersample == 8)
3080
537
                break;
3081
            /* fall through... */
3082
3.48k
        case PHOTOMETRIC_MINISBLACK:
3083
11.5k
        case PHOTOMETRIC_MINISWHITE:
3084
11.5k
            if (!setupMap(img))
3085
0
                return (0);
3086
11.5k
            break;
3087
11.5k
        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
785
            if (checkcmap(img) == 16)
3093
659
                cvtcmap(img);
3094
126
            else
3095
126
                TIFFWarningExtR(img->tif, TIFFFileName(img->tif),
3096
126
                                "Assuming 8-bit colormap");
3097
            /*
3098
             * Use mapping table and colormap to construct
3099
             * unpacking tables for samples < 8 bits.
3100
             */
3101
785
            if (img->bitspersample <= 8 && !makecmap(img))
3102
0
                return (0);
3103
785
            break;
3104
785
        default:
3105
247
            break;
3106
13.1k
    }
3107
13.1k
    return (1);
3108
13.1k
}
3109
3110
/*
3111
 * Select the appropriate conversion routine for packed data.
3112
 */
3113
static int PickContigCase(TIFFRGBAImage *img)
3114
22.2k
{
3115
22.2k
    img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
3116
22.2k
    img->put.contig = NULL;
3117
22.2k
    switch (img->photometric)
3118
22.2k
    {
3119
5.16k
        case PHOTOMETRIC_RGB:
3120
5.16k
            switch (img->bitspersample)
3121
5.16k
            {
3122
5.03k
                case 8:
3123
5.03k
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
3124
664
                        img->samplesperpixel >= 4)
3125
523
                        img->put.contig = putRGBAAcontig8bittile;
3126
4.51k
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
3127
85
                             img->samplesperpixel >= 4)
3128
70
                    {
3129
70
                        if (BuildMapUaToAa(img))
3130
70
                            img->put.contig = putRGBUAcontig8bittile;
3131
70
                    }
3132
4.44k
                    else if (img->samplesperpixel >= 3)
3133
4.44k
                        img->put.contig = putRGBcontig8bittile;
3134
5.03k
                    break;
3135
17
                case 16:
3136
17
                    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
12
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
3143
2
                             img->samplesperpixel >= 4)
3144
2
                    {
3145
2
                        if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
3146
2
                            img->put.contig = putRGBUAcontig16bittile;
3147
2
                    }
3148
10
                    else if (img->samplesperpixel >= 3)
3149
10
                    {
3150
10
                        if (BuildMapBitdepth16To8(img))
3151
10
                            img->put.contig = putRGBcontig16bittile;
3152
10
                    }
3153
17
                    break;
3154
113
                default:
3155
113
                    break;
3156
5.16k
            }
3157
5.16k
            break;
3158
5.16k
        case PHOTOMETRIC_SEPARATED:
3159
552
            if (img->samplesperpixel >= 4 && buildMap(img))
3160
552
            {
3161
552
                if (img->bitspersample == 8)
3162
537
                {
3163
537
                    if (!img->Map)
3164
537
                        img->put.contig = putRGBcontig8bitCMYKtile;
3165
0
                    else
3166
0
                        img->put.contig = putRGBcontig8bitCMYKMaptile;
3167
537
                }
3168
552
            }
3169
552
            break;
3170
785
        case PHOTOMETRIC_PALETTE:
3171
785
            if (buildMap(img))
3172
785
            {
3173
785
                switch (img->bitspersample)
3174
785
                {
3175
40
                    case 8:
3176
40
                        img->put.contig = put8bitcmaptile;
3177
40
                        break;
3178
95
                    case 4:
3179
95
                        img->put.contig = put4bitcmaptile;
3180
95
                        break;
3181
238
                    case 2:
3182
238
                        img->put.contig = put2bitcmaptile;
3183
238
                        break;
3184
412
                    case 1:
3185
412
                        img->put.contig = put1bitcmaptile;
3186
412
                        break;
3187
0
                    default:
3188
0
                        break;
3189
785
                }
3190
785
            }
3191
785
            break;
3192
8.07k
        case PHOTOMETRIC_MINISWHITE:
3193
11.5k
        case PHOTOMETRIC_MINISBLACK:
3194
11.5k
            if (buildMap(img))
3195
11.5k
            {
3196
11.5k
                switch (img->bitspersample)
3197
11.5k
                {
3198
242
                    case 16:
3199
242
                        img->put.contig = put16bitbwtile;
3200
242
                        break;
3201
2.51k
                    case 8:
3202
2.51k
                        if (img->alpha && img->samplesperpixel == 2)
3203
89
                            img->put.contig = putagreytile;
3204
2.42k
                        else
3205
2.42k
                            img->put.contig = putgreytile;
3206
2.51k
                        break;
3207
830
                    case 4:
3208
830
                        img->put.contig = put4bitbwtile;
3209
830
                        break;
3210
336
                    case 2:
3211
336
                        img->put.contig = put2bitbwtile;
3212
336
                        break;
3213
7.62k
                    case 1:
3214
7.62k
                        img->put.contig = put1bitbwtile;
3215
7.62k
                        break;
3216
0
                    default:
3217
0
                        break;
3218
11.5k
                }
3219
11.5k
            }
3220
11.5k
            break;
3221
11.5k
        case PHOTOMETRIC_YCBCR:
3222
3.92k
            if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
3223
3.88k
            {
3224
3.88k
                if (initYCbCrConversion(img) != 0)
3225
3.83k
                {
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.83k
                    uint16_t SubsamplingHor;
3236
3.83k
                    uint16_t SubsamplingVer;
3237
3.83k
                    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
3238
3.83k
                                          &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.83k
                    if (img->width < SubsamplingHor ||
3243
3.78k
                        img->height < SubsamplingVer)
3244
76
                    {
3245
76
                        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
3246
76
                                      "YCbCr subsampling (%u,%u) incompatible "
3247
76
                                      "with image size %ux%u",
3248
76
                                      SubsamplingHor, SubsamplingVer,
3249
76
                                      img->width, img->height);
3250
76
                        return (0);
3251
76
                    }
3252
3.76k
                    switch ((SubsamplingHor << 4) | SubsamplingVer)
3253
3.76k
                    {
3254
187
                        case 0x44:
3255
187
                            img->put.contig = putcontig8bitYCbCr44tile;
3256
187
                            break;
3257
126
                        case 0x42:
3258
126
                            img->put.contig = putcontig8bitYCbCr42tile;
3259
126
                            break;
3260
101
                        case 0x41:
3261
101
                            img->put.contig = putcontig8bitYCbCr41tile;
3262
101
                            break;
3263
2.81k
                        case 0x22:
3264
2.81k
                            img->put.contig = putcontig8bitYCbCr22tile;
3265
2.81k
                            break;
3266
62
                        case 0x21:
3267
62
                            img->put.contig = putcontig8bitYCbCr21tile;
3268
62
                            break;
3269
50
                        case 0x12:
3270
50
                            img->put.contig = putcontig8bitYCbCr12tile;
3271
50
                            break;
3272
412
                        case 0x11:
3273
412
                            img->put.contig = putcontig8bitYCbCr11tile;
3274
412
                            break;
3275
11
                        default:
3276
11
                            break;
3277
3.76k
                    }
3278
3.76k
                }
3279
3.88k
            }
3280
3.84k
            break;
3281
3.84k
        case PHOTOMETRIC_CIELAB:
3282
247
            if (img->samplesperpixel == 3 && buildMap(img))
3283
247
            {
3284
247
                if (img->bitspersample == 8 || img->bitspersample == 16)
3285
247
                    img->put.contig = initCIELabConversion(img);
3286
247
                break;
3287
247
            }
3288
0
            break;
3289
0
        default:
3290
0
            break;
3291
22.2k
    }
3292
22.1k
    return ((img->get != NULL) && (img->put.contig != NULL));
3293
22.2k
}
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.01k
{
3303
4.01k
    img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
3304
4.01k
    img->put.separate = NULL;
3305
4.01k
    switch (img->photometric)
3306
4.01k
    {
3307
321
        case PHOTOMETRIC_MINISWHITE:
3308
881
        case PHOTOMETRIC_MINISBLACK:
3309
            /* greyscale images processed pretty much as RGB by gtTileSeparate
3310
             */
3311
3.16k
        case PHOTOMETRIC_RGB:
3312
3.16k
            switch (img->bitspersample)
3313
3.16k
            {
3314
3.12k
                case 8:
3315
3.12k
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
3316
711
                        img->put.separate = putRGBAAseparate8bittile;
3317
2.41k
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
3318
62
                    {
3319
62
                        if (BuildMapUaToAa(img))
3320
62
                            img->put.separate = putRGBUAseparate8bittile;
3321
62
                    }
3322
2.35k
                    else
3323
2.35k
                        img->put.separate = putRGBseparate8bittile;
3324
3.12k
                    break;
3325
27
                case 16:
3326
27
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
3327
13
                    {
3328
13
                        if (BuildMapBitdepth16To8(img))
3329
13
                            img->put.separate = putRGBAAseparate16bittile;
3330
13
                    }
3331
14
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
3332
3
                    {
3333
3
                        if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
3334
3
                            img->put.separate = putRGBUAseparate16bittile;
3335
3
                    }
3336
11
                    else
3337
11
                    {
3338
11
                        if (BuildMapBitdepth16To8(img))
3339
11
                            img->put.separate = putRGBseparate16bittile;
3340
11
                    }
3341
27
                    break;
3342
7
                default:
3343
7
                    break;
3344
3.16k
            }
3345
3.16k
            break;
3346
3.16k
        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
739
        case PHOTOMETRIC_YCBCR:
3355
739
            if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
3356
725
            {
3357
725
                if (initYCbCrConversion(img) != 0)
3358
722
                {
3359
722
                    uint16_t hs, vs;
3360
722
                    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
3361
722
                                          &hs, &vs);
3362
722
                    switch ((hs << 4) | vs)
3363
722
                    {
3364
649
                        case 0x11:
3365
649
                            img->put.separate = putseparate8bitYCbCr11tile;
3366
649
                            break;
3367
                            /* TODO: add other cases here */
3368
73
                        default:
3369
73
                            break;
3370
722
                    }
3371
722
                }
3372
725
            }
3373
739
            break;
3374
739
        default:
3375
6
            break;
3376
4.01k
    }
3377
4.01k
    return ((img->get != NULL) && (img->put.separate != NULL));
3378
4.01k
}
3379
3380
static int BuildMapUaToAa(TIFFRGBAImage *img)
3381
137
{
3382
137
    static const char module[] = "BuildMapUaToAa";
3383
137
    uint8_t *m;
3384
137
    uint16_t na, nv;
3385
137
    assert(img->UaToAa == NULL);
3386
137
    img->UaToAa = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
3387
137
    if (img->UaToAa == NULL)
3388
0
    {
3389
0
        TIFFErrorExtR(img->tif, module, "Out of memory");
3390
0
        return (0);
3391
0
    }
3392
137
    m = img->UaToAa;
3393
35.2k
    for (na = 0; na < 256; na++)
3394
35.0k
    {
3395
9.01M
        for (nv = 0; nv < 256; nv++)
3396
8.97M
            *m++ = (uint8_t)((nv * na + 127) / 255);
3397
35.0k
    }
3398
137
    return (1);
3399
137
}
3400
3401
static int BuildMapBitdepth16To8(TIFFRGBAImage *img)
3402
44
{
3403
44
    static const char module[] = "BuildMapBitdepth16To8";
3404
44
    uint8_t *m;
3405
44
    uint32_t n;
3406
44
    assert(img->Bitdepth16To8 == NULL);
3407
44
    img->Bitdepth16To8 = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
3408
44
    if (img->Bitdepth16To8 == NULL)
3409
0
    {
3410
0
        TIFFErrorExtR(img->tif, module, "Out of memory");
3411
0
        return (0);
3412
0
    }
3413
44
    m = img->Bitdepth16To8;
3414
2.88M
    for (n = 0; n < 65536; n++)
3415
2.88M
        *m++ = (uint8_t)((n + 128) / 257);
3416
44
    return (1);
3417
44
}
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
}