Coverage Report

Created: 2025-06-12 06:52

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