Coverage Report

Created: 2026-10-28 07:04

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