Coverage Report

Created: 2026-05-16 06:16

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
0
#define FLIP_VERTICALLY 0x01
51
0
#define FLIP_HORIZONTALLY 0x02
52
53
0
#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
0
{
86
0
    TIFFDirectory *td = &tif->tif_dir;
87
0
    uint16_t photometric;
88
0
    int colorchannels;
89
90
0
    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
0
    switch (td->td_bitspersample)
97
0
    {
98
0
        case 1:
99
0
        case 2:
100
0
        case 4:
101
0
        case 8:
102
0
        case 16:
103
0
            break;
104
0
        default:
105
0
            snprintf(emsg, EMSG_BUF_SIZE,
106
0
                     "Sorry, can not handle images with %" PRIu16
107
0
                     "-bit samples",
108
0
                     td->td_bitspersample);
109
0
            return (0);
110
0
    }
111
0
    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
0
    colorchannels = td->td_samplesperpixel - td->td_extrasamples;
119
0
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric))
120
0
    {
121
0
        switch (colorchannels)
122
0
        {
123
0
            case 1:
124
0
                photometric = PHOTOMETRIC_MINISBLACK;
125
0
                break;
126
0
            case 3:
127
0
                photometric = PHOTOMETRIC_RGB;
128
0
                break;
129
0
            default:
130
0
                snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
131
0
                         photoTag);
132
0
                return (0);
133
0
        }
134
0
    }
135
0
    switch (photometric)
136
0
    {
137
0
        case PHOTOMETRIC_MINISWHITE:
138
0
        case PHOTOMETRIC_MINISBLACK:
139
0
        case PHOTOMETRIC_PALETTE:
140
0
            if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
141
0
                td->td_samplesperpixel != 1 && td->td_bitspersample < 8)
142
0
            {
143
0
                snprintf(
144
0
                    emsg, EMSG_BUF_SIZE,
145
0
                    "Sorry, can not handle contiguous data with %s=%" PRIu16
146
0
                    ", "
147
0
                    "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "",
148
0
                    photoTag, photometric, "Samples/pixel",
149
0
                    td->td_samplesperpixel, td->td_bitspersample);
150
0
                return (0);
151
0
            }
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
0
            break;
158
0
        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
0
            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
0
        case PHOTOMETRIC_SEPARATED:
176
0
        {
177
0
            uint16_t inkset;
178
0
            TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
179
0
            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
0
            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
0
            break;
195
0
        }
196
0
        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
0
        case PHOTOMETRIC_LOGLUV:
206
0
            if (td->td_compression != COMPRESSION_SGILOG &&
207
0
                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
0
            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
0
            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
0
            break;
232
0
        case PHOTOMETRIC_CIELAB:
233
0
            if (td->td_samplesperpixel != 3 || colorchannels != 3 ||
234
0
                (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
0
            break;
245
0
        default:
246
0
            snprintf(emsg, EMSG_BUF_SIZE,
247
0
                     "Sorry, can not handle image with %s=%" PRIu16, photoTag,
248
0
                     photometric);
249
0
            return (0);
250
0
    }
251
0
    return (1);
252
0
}
253
254
void TIFFRGBAImageEnd(TIFFRGBAImage *img)
255
0
{
256
0
    if (img->Map)
257
0
    {
258
0
        _TIFFfreeExt(img->tif, img->Map);
259
0
        img->Map = NULL;
260
0
    }
261
0
    if (img->BWmap)
262
0
    {
263
0
        _TIFFfreeExt(img->tif, img->BWmap);
264
0
        img->BWmap = NULL;
265
0
    }
266
0
    if (img->PALmap)
267
0
    {
268
0
        _TIFFfreeExt(img->tif, img->PALmap);
269
0
        img->PALmap = NULL;
270
0
    }
271
0
    if (img->ycbcr)
272
0
    {
273
0
        _TIFFfreeExt(img->tif, img->ycbcr);
274
0
        img->ycbcr = NULL;
275
0
    }
276
0
    if (img->cielab)
277
0
    {
278
0
        _TIFFfreeExt(img->tif, img->cielab);
279
0
        img->cielab = NULL;
280
0
    }
281
0
    if (img->UaToAa)
282
0
    {
283
0
        _TIFFfreeExt(img->tif, img->UaToAa);
284
0
        img->UaToAa = NULL;
285
0
    }
286
0
    if (img->Bitdepth16To8)
287
0
    {
288
0
        _TIFFfreeExt(img->tif, img->Bitdepth16To8);
289
0
        img->Bitdepth16To8 = NULL;
290
0
    }
291
292
0
    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
0
}
300
301
static int isCCITTCompression(TIFF *tif)
302
0
{
303
0
    uint16_t compress;
304
0
    TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
305
0
    return (compress == COMPRESSION_CCITTFAX3 ||
306
0
            compress == COMPRESSION_CCITTFAX4 ||
307
0
            compress == COMPRESSION_CCITTRLE ||
308
0
            compress == COMPRESSION_CCITTRLEW);
309
0
}
310
311
int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop,
312
                       char emsg[EMSG_BUF_SIZE])
313
0
{
314
0
    uint16_t *sampleinfo;
315
0
    uint16_t extrasamples;
316
0
    uint16_t planarconfig;
317
0
    uint16_t compress;
318
0
    int colorchannels;
319
0
    uint16_t *red_orig, *green_orig, *blue_orig;
320
0
    uint32_t n_color;
321
322
0
    if (!TIFFRGBAImageOK(tif, emsg))
323
0
        return 0;
324
325
    /* Initialize to normal values */
326
0
    img->row_offset = 0;
327
0
    img->col_offset = 0;
328
0
    img->redcmap = NULL;
329
0
    img->greencmap = NULL;
330
0
    img->bluecmap = NULL;
331
0
    img->Map = NULL;
332
0
    img->BWmap = NULL;
333
0
    img->PALmap = NULL;
334
0
    img->ycbcr = NULL;
335
0
    img->cielab = NULL;
336
0
    img->UaToAa = NULL;
337
0
    img->Bitdepth16To8 = NULL;
338
0
    img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
339
340
0
    img->tif = tif;
341
0
    img->stoponerr = stop;
342
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
343
0
    switch (img->bitspersample)
344
0
    {
345
0
        case 1:
346
0
        case 2:
347
0
        case 4:
348
0
        case 8:
349
0
        case 16:
350
0
            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
0
    }
358
0
    img->alpha = 0;
359
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
360
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples,
361
0
                          &sampleinfo);
362
0
    if (extrasamples >= 1)
363
0
    {
364
0
        switch (sampleinfo[0])
365
0
        {
366
0
            case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without
367
                                           */
368
0
                if (img->samplesperpixel >
369
0
                    3) /* correct info about alpha channel */
370
0
                    img->alpha = EXTRASAMPLE_ASSOCALPHA;
371
0
                break;
372
0
            case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
373
0
            case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
374
0
                img->alpha = sampleinfo[0];
375
0
                break;
376
0
            default:
377
0
                break;
378
0
        }
379
0
    }
380
381
0
#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
382
0
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
383
0
        img->photometric = PHOTOMETRIC_MINISWHITE;
384
385
0
    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
0
#endif
392
393
0
    colorchannels = img->samplesperpixel - extrasamples;
394
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
395
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
396
0
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
397
0
    {
398
0
        switch (colorchannels)
399
0
        {
400
0
            case 1:
401
0
                if (isCCITTCompression(tif))
402
0
                    img->photometric = PHOTOMETRIC_MINISWHITE;
403
0
                else
404
0
                    img->photometric = PHOTOMETRIC_MINISBLACK;
405
0
                break;
406
0
            case 3:
407
0
                img->photometric = PHOTOMETRIC_RGB;
408
0
                break;
409
0
            default:
410
0
                snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
411
0
                         photoTag);
412
0
                goto fail_return;
413
0
        }
414
0
    }
415
0
    switch (img->photometric)
416
0
    {
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
0
        case PHOTOMETRIC_MINISWHITE:
447
0
        case PHOTOMETRIC_MINISBLACK:
448
0
            if (planarconfig == PLANARCONFIG_CONTIG &&
449
0
                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
0
            break;
461
0
        case PHOTOMETRIC_YCBCR:
462
            /* It would probably be nice to have a reality check here. */
463
0
            if (planarconfig == PLANARCONFIG_CONTIG)
464
                /* can rely on libjpeg to convert to RGB */
465
                /* XXX should restore current state on exit */
466
0
                switch (compress)
467
0
                {
468
0
                    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
0
                        TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
476
0
                                     JPEGCOLORMODE_RGB);
477
0
                        img->photometric = PHOTOMETRIC_RGB;
478
0
                        break;
479
0
                    default:
480
0
                        /* do nothing */;
481
0
                        break;
482
0
                }
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
0
            break;
490
0
        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
0
        case PHOTOMETRIC_SEPARATED:
500
0
        {
501
0
            uint16_t inkset;
502
0
            TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
503
0
            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
0
            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
0
        }
520
0
        break;
521
0
        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
0
        case PHOTOMETRIC_LOGLUV:
534
0
            if (compress != COMPRESSION_SGILOG &&
535
0
                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
0
            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
0
            TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
551
0
            img->photometric = PHOTOMETRIC_RGB; /* little white lie */
552
0
            img->bitspersample = 8;
553
0
            break;
554
0
        case PHOTOMETRIC_CIELAB:
555
0
            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
0
    }
562
0
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
563
0
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
564
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
565
0
    img->isContig =
566
0
        !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
567
0
    if (img->isContig)
568
0
    {
569
0
        if (!PickContigCase(img))
570
0
        {
571
0
            snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
572
0
            goto fail_return;
573
0
        }
574
0
    }
575
0
    else
576
0
    {
577
0
        if (!PickSeparateCase(img))
578
0
        {
579
0
            snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
580
0
            goto fail_return;
581
0
        }
582
0
    }
583
0
    return 1;
584
585
0
fail_return:
586
0
    TIFFRGBAImageEnd(img);
587
0
    return 0;
588
0
}
589
590
int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
591
                     uint32_t h)
592
0
{
593
0
    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
0
    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
0
    if (0 <= img->row_offset && (uint32_t)img->row_offset < img->height)
609
0
    {
610
0
        uint32_t hx = img->height - (uint32_t)img->row_offset;
611
0
        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
0
    }
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
0
    return (*img->get)(img, raster, w, h);
628
0
}
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
0
{
637
0
    char emsg[EMSG_BUF_SIZE] = "";
638
0
    TIFFRGBAImage img;
639
0
    int ok;
640
641
0
    if (TIFFRGBAImageBegin(&img, tif, stop, emsg))
642
0
    {
643
0
        img.req_orientation = (uint16_t)orientation;
644
0
        ok = TIFFRGBAImageGet(&img, raster, rwidth, rheight);
645
0
        TIFFRGBAImageEnd(&img);
646
0
    }
647
0
    else
648
0
    {
649
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
650
0
        ok = 0;
651
0
    }
652
0
    return (ok);
653
0
}
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
0
{
668
0
    switch (img->orientation)
669
0
    {
670
0
        case ORIENTATION_TOPLEFT:
671
0
        case ORIENTATION_LEFTTOP:
672
0
            if (img->req_orientation == ORIENTATION_TOPRIGHT ||
673
0
                img->req_orientation == ORIENTATION_RIGHTTOP)
674
0
                return FLIP_HORIZONTALLY;
675
0
            else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
676
0
                     img->req_orientation == ORIENTATION_RIGHTBOT)
677
0
                return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
678
0
            else if (img->req_orientation == ORIENTATION_BOTLEFT ||
679
0
                     img->req_orientation == ORIENTATION_LEFTBOT)
680
0
                return FLIP_VERTICALLY;
681
0
            else
682
0
                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
0
        case ORIENTATION_BOTRIGHT:
697
0
        case ORIENTATION_RIGHTBOT:
698
0
            if (img->req_orientation == ORIENTATION_TOPLEFT ||
699
0
                img->req_orientation == ORIENTATION_LEFTTOP)
700
0
                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
0
    }
725
0
}
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
0
{
736
0
    TIFF *tif = img->tif;
737
0
    tileContigRoutine put = img->put.contig;
738
0
    uint32_t col, row, y, rowstoread;
739
0
    tmsize_t pos;
740
0
    uint32_t tw, th;
741
0
    unsigned char *buf = NULL;
742
0
    int32_t fromskew, toskew;
743
0
    uint32_t nrow;
744
0
    int ret = 1, flip;
745
0
    uint32_t this_tw, tocol;
746
0
    int32_t this_toskew, leftmost_toskew;
747
0
    int32_t leftmost_fromskew;
748
0
    uint32_t leftmost_tw;
749
0
    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
0
    uint32_t wmin;
754
755
0
    if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
756
0
    {
757
0
        wmin = TIFFmin(w, img->width - (uint32_t)img->col_offset);
758
0
    }
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
0
    bufsize = TIFFTileSize(tif);
768
0
    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
0
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
775
0
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
776
777
0
    flip = setorientation(img);
778
0
    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
0
    else
790
0
    {
791
0
        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
0
        y = 0;
798
0
        toskew = -(int32_t)(tw - w);
799
0
    }
800
801
0
    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
0
    leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
811
0
    leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
812
0
    int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
813
0
    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
0
    leftmost_toskew = (int32_t)skew_i64;
820
0
    for (row = 0; ret != 0 && row < h; row += nrow)
821
0
    {
822
0
        rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
823
0
        nrow = (row + rowstoread > h ? h - row : rowstoread);
824
0
        fromskew = leftmost_fromskew;
825
0
        this_tw = leftmost_tw;
826
0
        this_toskew = leftmost_toskew;
827
0
        tocol = 0;
828
0
        col = (uint32_t)img->col_offset;
829
        /* wmin: only write imagewidth if raster is bigger. */
830
0
        while (tocol < wmin)
831
0
        {
832
0
            if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col,
833
0
                                            (uint32_t)row +
834
0
                                                (uint32_t)img->row_offset,
835
0
                                            0, 0) == (tmsize_t)(-1) &&
836
0
                (buf == NULL || img->stoponerr))
837
0
            {
838
0
                ret = 0;
839
0
                break;
840
0
            }
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
0
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
868
0
                                                 : (int32_t)nrow);
869
0
    }
870
0
    _TIFFfreeExt(img->tif, buf);
871
872
0
    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
0
    return (ret);
895
0
}
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
0
{
906
0
    TIFF *tif = img->tif;
907
0
    tileSeparateRoutine put = img->put.separate;
908
0
    uint32_t col, row, y, rowstoread;
909
0
    tmsize_t pos;
910
0
    uint32_t tw, th;
911
0
    unsigned char *buf = NULL;
912
0
    unsigned char *p0 = NULL;
913
0
    unsigned char *p1 = NULL;
914
0
    unsigned char *p2 = NULL;
915
0
    unsigned char *pa = NULL;
916
0
    tmsize_t tilesize;
917
0
    tmsize_t bufsize;
918
0
    int32_t fromskew, toskew;
919
0
    int alpha = img->alpha;
920
0
    uint32_t nrow;
921
0
    int ret = 1, flip;
922
0
    uint16_t colorchannels;
923
0
    uint32_t this_tw, tocol;
924
0
    int32_t this_toskew, leftmost_toskew;
925
0
    int32_t leftmost_fromskew;
926
0
    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
0
    uint32_t wmin;
931
0
    if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
932
0
    {
933
0
        wmin = TIFFmin(w, img->width - (uint32_t)img->col_offset);
934
0
    }
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
0
    tilesize = TIFFTileSize(tif);
945
0
    bufsize =
946
0
        _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate");
947
0
    if (bufsize == 0)
948
0
    {
949
0
        return (0);
950
0
    }
951
952
0
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
953
0
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
954
955
0
    flip = setorientation(img);
956
0
    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
0
    else
968
0
    {
969
0
        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
0
        y = 0;
976
0
        toskew = -(int32_t)(tw - w);
977
0
    }
978
979
0
    switch (img->photometric)
980
0
    {
981
0
        case PHOTOMETRIC_MINISWHITE:
982
0
        case PHOTOMETRIC_MINISBLACK:
983
0
        case PHOTOMETRIC_PALETTE:
984
0
            colorchannels = 1;
985
0
            break;
986
987
0
        default:
988
0
            colorchannels = 3;
989
0
            break;
990
0
    }
991
992
0
    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
0
    leftmost_fromskew = (int32_t)((uint32_t)img->col_offset % tw);
1002
0
    leftmost_tw = (uint32_t)((int32_t)tw - leftmost_fromskew);
1003
0
    int64_t skew_i64 = (int64_t)toskew + (int64_t)(int32_t)leftmost_fromskew;
1004
0
    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
0
    leftmost_toskew = (int32_t)skew_i64;
1011
0
    for (row = 0; ret != 0 && row < h; row += nrow)
1012
0
    {
1013
0
        rowstoread = th - ((uint32_t)row + (uint32_t)img->row_offset) % th;
1014
0
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1015
0
        fromskew = leftmost_fromskew;
1016
0
        this_tw = leftmost_tw;
1017
0
        this_toskew = leftmost_toskew;
1018
0
        tocol = 0;
1019
0
        col = (uint32_t)img->col_offset;
1020
        /* wmin: only write imagewidth if raster is bigger. */
1021
0
        while (tocol < wmin)
1022
0
        {
1023
0
            if (buf == NULL)
1024
0
            {
1025
0
                if (_TIFFReadTileAndAllocBuffer(
1026
0
                        tif, (void **)&buf, bufsize, col,
1027
0
                        (uint32_t)((int32_t)row + img->row_offset), 0,
1028
0
                        0) == (tmsize_t)(-1) &&
1029
0
                    (buf == NULL || img->stoponerr))
1030
0
                {
1031
0
                    ret = 0;
1032
0
                    break;
1033
0
                }
1034
0
                p0 = buf;
1035
0
                if (colorchannels == 1)
1036
0
                {
1037
0
                    p2 = p1 = p0;
1038
0
                    pa = (alpha ? (p0 + 3 * tilesize) : NULL);
1039
0
                }
1040
0
                else
1041
0
                {
1042
0
                    p1 = p0 + tilesize;
1043
0
                    p2 = p1 + tilesize;
1044
0
                    pa = (alpha ? (p2 + tilesize) : NULL);
1045
0
                }
1046
0
            }
1047
0
            else if (TIFFReadTile(tif, p0, col,
1048
0
                                  (uint32_t)((int32_t)row + img->row_offset), 0,
1049
0
                                  0) == (tmsize_t)(-1) &&
1050
0
                     img->stoponerr)
1051
0
            {
1052
0
                ret = 0;
1053
0
                break;
1054
0
            }
1055
0
            if (colorchannels > 1 &&
1056
0
                TIFFReadTile(tif, p1, col,
1057
0
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1058
0
                             (uint16_t)1) == (tmsize_t)(-1) &&
1059
0
                img->stoponerr)
1060
0
            {
1061
0
                ret = 0;
1062
0
                break;
1063
0
            }
1064
0
            if (colorchannels > 1 &&
1065
0
                TIFFReadTile(tif, p2, col,
1066
0
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1067
0
                             (uint16_t)2) == (tmsize_t)(-1) &&
1068
0
                img->stoponerr)
1069
0
            {
1070
0
                ret = 0;
1071
0
                break;
1072
0
            }
1073
0
            if (alpha &&
1074
0
                TIFFReadTile(tif, pa, col,
1075
0
                             (uint32_t)((int32_t)row + img->row_offset), 0,
1076
0
                             (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
0
            pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) % th) *
1086
0
                      TIFFTileRowSize(tif) +
1087
0
                  (tmsize_t)fromskew;
1088
0
            if (tocol + this_tw > wmin)
1089
0
            {
1090
                /*
1091
                 * Rightmost tile is clipped on right side.
1092
                 */
1093
0
                fromskew = (int32_t)(tw - (wmin - tocol));
1094
0
                this_tw = (uint32_t)((int32_t)tw - fromskew);
1095
0
                this_toskew = (int32_t)(toskew + (int32_t)(uint32_t)fromskew);
1096
0
            }
1097
0
            tmsize_t roffset = (tmsize_t)y * w + tocol;
1098
0
            (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
1099
0
                   this_toskew, p0 + pos, p1 + pos, p2 + pos,
1100
0
                   (alpha ? (pa + pos) : NULL));
1101
0
            tocol += this_tw;
1102
0
            col += this_tw;
1103
            /*
1104
             * After the leftmost tile, tiles are no longer clipped on left
1105
             * side.
1106
             */
1107
0
            fromskew = 0;
1108
0
            this_tw = tw;
1109
0
            this_toskew = toskew;
1110
0
        }
1111
1112
0
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1113
0
                                                 : (int32_t)nrow);
1114
0
    }
1115
1116
0
    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
0
    _TIFFfreeExt(img->tif, buf);
1139
0
    return (ret);
1140
0
}
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
0
{
1151
0
    TIFF *tif = img->tif;
1152
0
    tileContigRoutine put = img->put.contig;
1153
0
    uint32_t row, y, nrow, nrowsub, rowstoread;
1154
0
    tmsize_t pos;
1155
0
    unsigned char *buf = NULL;
1156
0
    uint32_t rowsperstrip;
1157
0
    uint16_t subsamplinghor, subsamplingver;
1158
0
    uint32_t imagewidth = img->width;
1159
0
    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
0
    int32_t fromskew, toskew;
1163
0
    int ret = 1, flip;
1164
0
    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
0
    uint32_t wmin;
1169
0
    if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
1170
0
    {
1171
0
        wmin = TIFFmin(w, imagewidth - (uint32_t)img->col_offset);
1172
0
    }
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
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor,
1183
0
                          &subsamplingver);
1184
0
    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
0
    maxstripsize = TIFFStripSize(tif);
1192
1193
0
    flip = setorientation(img);
1194
0
    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
0
    else
1207
0
    {
1208
0
        y = 0;
1209
        /* Skew forward to the end of the raster width of the row currently
1210
         * copied. */
1211
0
        toskew = (int32_t)(w - wmin);
1212
0
    }
1213
1214
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1215
0
    if (rowsperstrip == 0)
1216
0
    {
1217
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1218
0
        return (0);
1219
0
    }
1220
1221
0
    scanline = TIFFScanlineSize(tif);
1222
0
    fromskew = (int32_t)(w < imagewidth ? imagewidth - w : 0);
1223
0
    for (row = 0; row < h; row += nrow)
1224
0
    {
1225
0
        uint32_t temp;
1226
0
        rowstoread = rowsperstrip -
1227
0
                     (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip;
1228
0
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1229
0
        nrowsub = nrow;
1230
0
        if ((nrowsub % subsamplingver) != 0)
1231
0
            nrowsub += subsamplingver - nrowsub % subsamplingver;
1232
0
        temp =
1233
0
            (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrowsub;
1234
0
        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
0
        if (_TIFFReadEncodedStripAndAllocBuffer(
1241
0
                tif,
1242
0
                TIFFComputeStrip(tif,
1243
0
                                 (uint32_t)((int32_t)row + img->row_offset), 0),
1244
0
                (void **)(&buf), maxstripsize,
1245
0
                (tmsize_t)((size_t)temp * (size_t)scanline)) ==
1246
0
                (tmsize_t)(-1) &&
1247
0
            (buf == NULL || img->stoponerr))
1248
0
        {
1249
0
            ret = 0;
1250
0
            break;
1251
0
        }
1252
1253
0
        pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) %
1254
0
                         rowsperstrip) *
1255
0
                  scanline +
1256
0
              ((tmsize_t)img->col_offset * img->samplesperpixel);
1257
0
        tmsize_t roffset = (tmsize_t)y * w;
1258
0
        (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
1259
0
               buf + pos);
1260
0
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1261
0
                                                 : (int32_t)nrow);
1262
0
    }
1263
1264
0
    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
0
    _TIFFfreeExt(img->tif, buf);
1291
0
    return (ret);
1292
0
}
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
0
{
1303
0
    TIFF *tif = img->tif;
1304
0
    tileSeparateRoutine put = img->put.separate;
1305
0
    unsigned char *buf = NULL;
1306
0
    unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;
1307
0
    uint32_t row, y, nrow, rowstoread;
1308
0
    tmsize_t pos;
1309
0
    tmsize_t scanline;
1310
0
    uint32_t rowsperstrip, offset_row;
1311
0
    uint32_t imagewidth = img->width;
1312
0
    tmsize_t stripsize;
1313
0
    tmsize_t bufsize;
1314
0
    int32_t fromskew, toskew;
1315
0
    int alpha = img->alpha;
1316
0
    int ret = 1, flip;
1317
0
    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
0
    uint32_t wmin;
1322
0
    if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
1323
0
    {
1324
0
        wmin = TIFFmin(w, imagewidth - (uint32_t)img->col_offset);
1325
0
    }
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
0
    stripsize = TIFFStripSize(tif);
1336
0
    bufsize =
1337
0
        _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate");
1338
0
    if (bufsize == 0)
1339
0
    {
1340
0
        return (0);
1341
0
    }
1342
1343
0
    flip = setorientation(img);
1344
0
    if (flip & FLIP_VERTICALLY)
1345
0
    {
1346
0
        if (w > INT_MAX / 2)
1347
0
        {
1348
0
            TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
1349
0
            return (0);
1350
0
        }
1351
0
        y = h - 1;
1352
        /* Skew back to the raster row before the currently written row
1353
         * -> one raster width plus one image width. */
1354
0
        toskew = -(int32_t)(w + wmin);
1355
0
    }
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
0
    switch (img->photometric)
1365
0
    {
1366
0
        case PHOTOMETRIC_MINISWHITE:
1367
0
        case PHOTOMETRIC_MINISBLACK:
1368
0
        case PHOTOMETRIC_PALETTE:
1369
0
            colorchannels = 1;
1370
0
            break;
1371
1372
0
        default:
1373
0
            colorchannels = 3;
1374
0
            break;
1375
0
    }
1376
1377
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1378
0
    if (rowsperstrip == 0)
1379
0
    {
1380
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
1381
0
        return (0);
1382
0
    }
1383
1384
0
    scanline = TIFFScanlineSize(tif);
1385
0
    fromskew = (int32_t)(w < imagewidth ? imagewidth - w : 0);
1386
0
    for (row = 0; row < h; row += nrow)
1387
0
    {
1388
0
        uint32_t temp;
1389
0
        rowstoread =
1390
0
            rowsperstrip -
1391
0
            ((uint32_t)((int32_t)row + img->row_offset) % rowsperstrip);
1392
0
        nrow = (row + rowstoread > h ? h - row : rowstoread);
1393
0
        offset_row = (uint32_t)((int32_t)row + img->row_offset);
1394
0
        temp = (uint32_t)((int32_t)row + img->row_offset) % rowsperstrip + nrow;
1395
0
        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
0
        if (buf == NULL)
1402
0
        {
1403
0
            if (_TIFFReadEncodedStripAndAllocBuffer(
1404
0
                    tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf,
1405
0
                    bufsize, temp * scanline) == (tmsize_t)(-1) &&
1406
0
                (buf == NULL || img->stoponerr))
1407
0
            {
1408
0
                ret = 0;
1409
0
                break;
1410
0
            }
1411
0
            p0 = buf;
1412
0
            if (colorchannels == 1)
1413
0
            {
1414
0
                p2 = p1 = p0;
1415
0
                pa = (alpha ? (p0 + 3 * stripsize) : NULL);
1416
0
            }
1417
0
            else
1418
0
            {
1419
0
                p1 = p0 + stripsize;
1420
0
                p2 = p1 + stripsize;
1421
0
                pa = (alpha ? (p2 + stripsize) : NULL);
1422
0
            }
1423
0
        }
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
0
        if (colorchannels > 1 &&
1432
0
            TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1,
1433
0
                                 temp * scanline) == (tmsize_t)(-1) &&
1434
0
            img->stoponerr)
1435
0
        {
1436
0
            ret = 0;
1437
0
            break;
1438
0
        }
1439
0
        if (colorchannels > 1 &&
1440
0
            TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2,
1441
0
                                 temp * scanline) == (tmsize_t)(-1) &&
1442
0
            img->stoponerr)
1443
0
        {
1444
0
            ret = 0;
1445
0
            break;
1446
0
        }
1447
0
        if (alpha)
1448
0
        {
1449
0
            if (TIFFReadEncodedStrip(
1450
0
                    tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa,
1451
0
                    temp * scanline) == (tmsize_t)(-1) &&
1452
0
                img->stoponerr)
1453
0
            {
1454
0
                ret = 0;
1455
0
                break;
1456
0
            }
1457
0
        }
1458
1459
        /* For SEPARATE the pos-offset is per sample and should not be
1460
         * multiplied by img->samplesperpixel. */
1461
0
        pos = (tmsize_t)((uint32_t)((int32_t)row + img->row_offset) %
1462
0
                         rowsperstrip) *
1463
0
                  scanline +
1464
0
              (tmsize_t)img->col_offset;
1465
0
        tmsize_t roffset = (tmsize_t)y * w;
1466
0
        (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
1467
0
               p0 + pos, p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL));
1468
0
        y += (uint32_t)((flip & FLIP_VERTICALLY) ? -(int32_t)nrow
1469
0
                                                 : (int32_t)nrow);
1470
0
    }
1471
1472
0
    if (flip & FLIP_HORIZONTALLY)
1473
0
    {
1474
0
        uint32_t line;
1475
1476
0
        for (line = 0; line < h; line++)
1477
0
        {
1478
0
            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
0
            uint32_t *right = left + wmin - 1;
1482
1483
0
            while (left < right)
1484
0
            {
1485
0
                uint32_t temp = *left;
1486
0
                *left = *right;
1487
0
                *right = temp;
1488
0
                left++;
1489
0
                right--;
1490
0
            }
1491
0
        }
1492
0
    }
1493
1494
0
    _TIFFfreeExt(img->tif, buf);
1495
0
    return (ret);
1496
0
}
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
0
    REPEAT4(op);                                                               \
1510
0
    REPEAT4(op)
1511
#define REPEAT4(op)                                                            \
1512
0
    REPEAT2(op);                                                               \
1513
0
    REPEAT2(op)
1514
#define REPEAT2(op)                                                            \
1515
0
    op;                                                                        \
1516
0
    op
1517
#define CASE8(x, op)                                                           \
1518
0
    switch (x)                                                                 \
1519
0
    {                                                                          \
1520
0
        case 7:                                                                \
1521
0
            op; /*-fallthrough*/                                               \
1522
0
        case 6:                                                                \
1523
0
            op; /*-fallthrough*/                                               \
1524
0
        case 5:                                                                \
1525
0
            op; /*-fallthrough*/                                               \
1526
0
        case 4:                                                                \
1527
0
            op; /*-fallthrough*/                                               \
1528
0
        case 3:                                                                \
1529
0
            op; /*-fallthrough*/                                               \
1530
0
        case 2:                                                                \
1531
0
            op; /*-fallthrough*/                                               \
1532
0
        case 1:                                                                \
1533
0
            op;                                                                \
1534
0
            break;                                                             \
1535
0
        default:                                                               \
1536
0
            break;                                                             \
1537
0
    }
1538
#define CASE4(x, op)                                                           \
1539
0
    switch (x)                                                                 \
1540
0
    {                                                                          \
1541
0
        case 3:                                                                \
1542
0
            op; /*-fallthrough*/                                               \
1543
0
        case 2:                                                                \
1544
0
            op; /*-fallthrough*/                                               \
1545
0
        case 1:                                                                \
1546
0
            op;                                                                \
1547
0
            break;                                                             \
1548
0
        default:                                                               \
1549
0
            break;                                                             \
1550
0
    }
1551
#define NOP
1552
1553
#define UNROLL8(w, op1, op2)                                                   \
1554
0
    {                                                                          \
1555
0
        uint32_t _x;                                                           \
1556
0
        for (_x = w; _x >= 8; _x -= 8)                                         \
1557
0
        {                                                                      \
1558
0
            op1;                                                               \
1559
0
            REPEAT8(op2);                                                      \
1560
0
        }                                                                      \
1561
0
        if (_x > 0)                                                            \
1562
0
        {                                                                      \
1563
0
            op1;                                                               \
1564
0
            CASE8(_x, op2);                                                    \
1565
0
        }                                                                      \
1566
0
    }
1567
#define UNROLL4(w, op1, op2)                                                   \
1568
0
    {                                                                          \
1569
0
        uint32_t _x;                                                           \
1570
0
        for (_x = w; _x >= 4; _x -= 4)                                         \
1571
0
        {                                                                      \
1572
0
            op1;                                                               \
1573
0
            REPEAT4(op2);                                                      \
1574
0
        }                                                                      \
1575
0
        if (_x > 0)                                                            \
1576
0
        {                                                                      \
1577
0
            op1;                                                               \
1578
0
            CASE4(_x, op2);                                                    \
1579
0
        }                                                                      \
1580
0
    }
1581
#define UNROLL2(w, op1, op2)                                                   \
1582
0
    {                                                                          \
1583
0
        uint32_t _x;                                                           \
1584
0
        for (_x = w; _x >= 2; _x -= 2)                                         \
1585
0
        {                                                                      \
1586
0
            op1;                                                               \
1587
0
            REPEAT2(op2);                                                      \
1588
0
        }                                                                      \
1589
0
        if (_x)                                                                \
1590
0
        {                                                                      \
1591
0
            op1;                                                               \
1592
0
            op2;                                                               \
1593
0
        }                                                                      \
1594
0
    }
1595
1596
#define SKEW(r, g, b, skew)                                                    \
1597
0
    {                                                                          \
1598
0
        r += skew;                                                             \
1599
0
        g += skew;                                                             \
1600
0
        b += skew;                                                             \
1601
0
    }
1602
#define SKEW4(r, g, b, a, skew)                                                \
1603
0
    {                                                                          \
1604
0
        r += skew;                                                             \
1605
0
        g += skew;                                                             \
1606
0
        b += skew;                                                             \
1607
0
        a += skew;                                                             \
1608
0
    }
1609
1610
0
#define A1 (((uint32_t)0xffL) << 24)
1611
#define PACK(r, g, b)                                                          \
1612
0
    ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1)
1613
#define PACK4(r, g, b, a)                                                      \
1614
0
    ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) |            \
1615
0
     ((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
1630
0
    (void)y;
1631
0
    for (; h > 0; --h)
1632
0
    {
1633
0
        for (x = w; x > 0; --x)
1634
0
        {
1635
0
            *cp++ = PALmap[*pp][0];
1636
0
            pp += samplesperpixel;
1637
0
        }
1638
0
        cp += toskew;
1639
0
        pp += fromskew;
1640
0
    }
1641
0
}
1642
1643
/*
1644
 * 4-bit palette => colormap/RGB
1645
 */
1646
DECLAREContigPutFunc(put4bitcmaptile)
1647
0
{
1648
0
    uint32_t **PALmap = img->PALmap;
1649
1650
0
    (void)x;
1651
0
    (void)y;
1652
0
    fromskew /= 2;
1653
0
    for (; h > 0; --h)
1654
0
    {
1655
0
        uint32_t *bw;
1656
0
        UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
1657
0
        cp += toskew;
1658
0
        pp += fromskew;
1659
0
    }
1660
0
}
1661
1662
/*
1663
 * 2-bit palette => colormap/RGB
1664
 */
1665
DECLAREContigPutFunc(put2bitcmaptile)
1666
0
{
1667
0
    uint32_t **PALmap = img->PALmap;
1668
1669
0
    (void)x;
1670
0
    (void)y;
1671
0
    fromskew /= 4;
1672
0
    for (; h > 0; --h)
1673
0
    {
1674
0
        uint32_t *bw;
1675
0
        UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
1676
0
        cp += toskew;
1677
0
        pp += fromskew;
1678
0
    }
1679
0
}
1680
1681
/*
1682
 * 1-bit palette => colormap/RGB
1683
 */
1684
DECLAREContigPutFunc(put1bitcmaptile)
1685
0
{
1686
0
    uint32_t **PALmap = img->PALmap;
1687
1688
0
    (void)x;
1689
0
    (void)y;
1690
0
    fromskew /= 8;
1691
0
    for (; h > 0; --h)
1692
0
    {
1693
0
        uint32_t *bw;
1694
0
        UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
1695
0
        cp += toskew;
1696
0
        pp += fromskew;
1697
0
    }
1698
0
}
1699
1700
/*
1701
 * 8-bit greyscale => colormap/RGB
1702
 */
1703
DECLAREContigPutFunc(putgreytile)
1704
0
{
1705
0
    int samplesperpixel = img->samplesperpixel;
1706
0
    uint32_t **BWmap = img->BWmap;
1707
1708
0
    (void)y;
1709
0
    for (; h > 0; --h)
1710
0
    {
1711
0
        for (x = w; x > 0; --x)
1712
0
        {
1713
0
            *cp++ = BWmap[*pp][0];
1714
0
            pp += samplesperpixel;
1715
0
        }
1716
0
        cp += toskew;
1717
0
        pp += fromskew;
1718
0
    }
1719
0
}
1720
1721
/*
1722
 * 8-bit greyscale with associated alpha => colormap/RGBA
1723
 */
1724
DECLAREContigPutFunc(putagreytile)
1725
0
{
1726
0
    int samplesperpixel = img->samplesperpixel;
1727
0
    uint32_t **BWmap = img->BWmap;
1728
1729
0
    (void)y;
1730
0
    for (; h > 0; --h)
1731
0
    {
1732
0
        for (x = w; x > 0; --x)
1733
0
        {
1734
0
            *cp++ = BWmap[*pp][0] & ((uint32_t)*(pp + 1) << 24 | ~A1);
1735
0
            pp += samplesperpixel;
1736
0
        }
1737
0
        cp += toskew;
1738
0
        pp += fromskew;
1739
0
    }
1740
0
}
1741
1742
/*
1743
 * 16-bit greyscale => colormap/RGB
1744
 */
1745
DECLAREContigPutFunc(put16bitbwtile)
1746
0
{
1747
0
    int samplesperpixel = img->samplesperpixel;
1748
0
    uint32_t **BWmap = img->BWmap;
1749
    /* Convert pixel skew to byte skew (16-bit samples) */
1750
0
    const tmsize_t fromskewLocal =
1751
0
        (tmsize_t)fromskew * (tmsize_t)(2 * samplesperpixel);
1752
1753
0
    (void)y;
1754
0
    for (; h > 0; --h)
1755
0
    {
1756
0
        uint16_t *wp = (uint16_t *)pp;
1757
1758
0
        for (x = w; x > 0; --x)
1759
0
        {
1760
            /* use high order byte of 16bit value */
1761
1762
0
            *cp++ = BWmap[*wp >> 8][0];
1763
0
            pp += 2 * samplesperpixel;
1764
0
            wp += samplesperpixel;
1765
0
        }
1766
0
        cp += toskew;
1767
0
        pp += fromskewLocal;
1768
0
    }
1769
0
}
1770
1771
/*
1772
 * 1-bit bilevel => colormap/RGB
1773
 */
1774
DECLAREContigPutFunc(put1bitbwtile)
1775
0
{
1776
0
    uint32_t **BWmap = img->BWmap;
1777
1778
0
    (void)x;
1779
0
    (void)y;
1780
0
    fromskew /= 8;
1781
0
    for (; h > 0; --h)
1782
0
    {
1783
0
        uint32_t *bw;
1784
0
        UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
1785
0
        cp += toskew;
1786
0
        pp += fromskew;
1787
0
    }
1788
0
}
1789
1790
/*
1791
 * 2-bit greyscale => colormap/RGB
1792
 */
1793
DECLAREContigPutFunc(put2bitbwtile)
1794
0
{
1795
0
    uint32_t **BWmap = img->BWmap;
1796
1797
0
    (void)x;
1798
0
    (void)y;
1799
0
    fromskew /= 4;
1800
0
    for (; h > 0; --h)
1801
0
    {
1802
0
        uint32_t *bw;
1803
0
        UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
1804
0
        cp += toskew;
1805
0
        pp += fromskew;
1806
0
    }
1807
0
}
1808
1809
/*
1810
 * 4-bit greyscale => colormap/RGB
1811
 */
1812
DECLAREContigPutFunc(put4bitbwtile)
1813
0
{
1814
0
    uint32_t **BWmap = img->BWmap;
1815
1816
0
    (void)x;
1817
0
    (void)y;
1818
0
    fromskew /= 2;
1819
0
    for (; h > 0; --h)
1820
0
    {
1821
0
        uint32_t *bw;
1822
0
        UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
1823
0
        cp += toskew;
1824
0
        pp += fromskew;
1825
0
    }
1826
0
}
1827
1828
/*
1829
 * 8-bit packed samples, no Map => RGB
1830
 */
1831
DECLAREContigPutFunc(putRGBcontig8bittile)
1832
0
{
1833
0
    int samplesperpixel = img->samplesperpixel;
1834
1835
0
    (void)x;
1836
0
    (void)y;
1837
0
    const tmsize_t fromskewLocal =
1838
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1839
0
    for (; h > 0; --h)
1840
0
    {
1841
0
        UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]);
1842
0
                pp += samplesperpixel);
1843
0
        cp += toskew;
1844
0
        pp += fromskewLocal;
1845
0
    }
1846
0
}
1847
1848
/*
1849
 * 8-bit packed samples => RGBA w/ associated alpha
1850
 * (known to have Map == NULL)
1851
 */
1852
DECLAREContigPutFunc(putRGBAAcontig8bittile)
1853
0
{
1854
0
    int samplesperpixel = img->samplesperpixel;
1855
1856
0
    (void)x;
1857
0
    (void)y;
1858
0
    const tmsize_t fromskewLocal =
1859
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1860
0
    for (; h > 0; --h)
1861
0
    {
1862
0
        UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
1863
0
                pp += samplesperpixel);
1864
0
        cp += toskew;
1865
0
        pp += fromskewLocal;
1866
0
    }
1867
0
}
1868
1869
/*
1870
 * 8-bit packed samples => RGBA w/ unassociated alpha
1871
 * (known to have Map == NULL)
1872
 */
1873
DECLAREContigPutFunc(putRGBUAcontig8bittile)
1874
0
{
1875
0
    int samplesperpixel = img->samplesperpixel;
1876
0
    (void)y;
1877
0
    const tmsize_t fromskewLocal =
1878
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1879
0
    for (; h > 0; --h)
1880
0
    {
1881
0
        uint32_t r, g, b, a;
1882
0
        uint8_t *m;
1883
0
        for (x = w; x > 0; --x)
1884
0
        {
1885
0
            a = pp[3];
1886
0
            m = img->UaToAa + ((size_t)a << 8);
1887
0
            r = m[pp[0]];
1888
0
            g = m[pp[1]];
1889
0
            b = m[pp[2]];
1890
0
            *cp++ = PACK4(r, g, b, a);
1891
0
            pp += samplesperpixel;
1892
0
        }
1893
0
        cp += toskew;
1894
0
        pp += fromskewLocal;
1895
0
    }
1896
0
}
1897
1898
/*
1899
 * 16-bit packed samples => RGB
1900
 */
1901
DECLAREContigPutFunc(putRGBcontig16bittile)
1902
0
{
1903
0
    int samplesperpixel = img->samplesperpixel;
1904
0
    uint16_t *wp = (uint16_t *)pp;
1905
0
    (void)y;
1906
0
    const tmsize_t fromskewLocal =
1907
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1908
0
    for (; h > 0; --h)
1909
0
    {
1910
0
        for (x = w; x > 0; --x)
1911
0
        {
1912
0
            *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
1913
0
                         img->Bitdepth16To8[wp[2]]);
1914
0
            wp += samplesperpixel;
1915
0
        }
1916
0
        cp += toskew;
1917
0
        wp += fromskewLocal;
1918
0
    }
1919
0
}
1920
1921
/*
1922
 * 16-bit packed samples => RGBA w/ associated alpha
1923
 * (known to have Map == NULL)
1924
 */
1925
DECLAREContigPutFunc(putRGBAAcontig16bittile)
1926
0
{
1927
0
    int samplesperpixel = img->samplesperpixel;
1928
0
    uint16_t *wp = (uint16_t *)pp;
1929
0
    (void)y;
1930
0
    const tmsize_t fromskewLocal =
1931
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1932
0
    for (; h > 0; --h)
1933
0
    {
1934
0
        for (x = w; x > 0; --x)
1935
0
        {
1936
0
            *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
1937
0
                          img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]);
1938
0
            wp += samplesperpixel;
1939
0
        }
1940
0
        cp += toskew;
1941
0
        wp += fromskewLocal;
1942
0
    }
1943
0
}
1944
1945
/*
1946
 * 16-bit packed samples => RGBA w/ unassociated alpha
1947
 * (known to have Map == NULL)
1948
 */
1949
DECLAREContigPutFunc(putRGBUAcontig16bittile)
1950
0
{
1951
0
    int samplesperpixel = img->samplesperpixel;
1952
0
    uint16_t *wp = (uint16_t *)pp;
1953
0
    (void)y;
1954
0
    const tmsize_t fromskewLocal =
1955
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1956
0
    for (; h > 0; --h)
1957
0
    {
1958
0
        uint32_t r, g, b, a;
1959
0
        uint8_t *m;
1960
0
        for (x = w; x > 0; --x)
1961
0
        {
1962
0
            a = img->Bitdepth16To8[wp[3]];
1963
0
            m = img->UaToAa + ((size_t)a << 8);
1964
0
            r = m[img->Bitdepth16To8[wp[0]]];
1965
0
            g = m[img->Bitdepth16To8[wp[1]]];
1966
0
            b = m[img->Bitdepth16To8[wp[2]]];
1967
0
            *cp++ = PACK4(r, g, b, a);
1968
0
            wp += samplesperpixel;
1969
0
        }
1970
0
        cp += toskew;
1971
0
        wp += fromskewLocal;
1972
0
    }
1973
0
}
1974
1975
/*
1976
 * 8-bit packed CMYK samples w/o Map => RGB
1977
 *
1978
 * NB: The conversion of CMYK->RGB is *very* crude.
1979
 */
1980
DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
1981
0
{
1982
0
    int samplesperpixel = img->samplesperpixel;
1983
0
    uint16_t r, g, b, k;
1984
1985
0
    (void)x;
1986
0
    (void)y;
1987
0
    const tmsize_t fromskewLocal =
1988
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
1989
0
    for (; h > 0; --h)
1990
0
    {
1991
0
        UNROLL8(w, NOP, k = (uint16_t)(255 - pp[3]);
1992
0
                r = (uint16_t)((k * (uint16_t)(255 - pp[0])) / 255);
1993
0
                g = (uint16_t)((k * (uint16_t)(255 - pp[1])) / 255);
1994
0
                b = (uint16_t)((k * (uint16_t)(255 - pp[2])) / 255);
1995
0
                *cp++ = PACK(r, g, b); pp += samplesperpixel);
1996
0
        cp += toskew;
1997
0
        pp += fromskewLocal;
1998
0
    }
1999
0
}
2000
2001
/*
2002
 * 8-bit packed CMYK samples w/Map => RGB
2003
 *
2004
 * NB: The conversion of CMYK->RGB is *very* crude.
2005
 */
2006
DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
2007
0
{
2008
0
    int samplesperpixel = img->samplesperpixel;
2009
0
    TIFFRGBValue *Map = img->Map;
2010
0
    unsigned int r, g, b, k;
2011
2012
0
    (void)y;
2013
0
    const tmsize_t fromskewLocal =
2014
0
        (tmsize_t)fromskew * (tmsize_t)samplesperpixel;
2015
0
    for (; h > 0; --h)
2016
0
    {
2017
0
        for (x = w; x > 0; --x)
2018
0
        {
2019
0
            k = 255U - pp[3];
2020
0
            r = (k * (255U - pp[0])) / 255U;
2021
0
            g = (k * (255U - pp[1])) / 255U;
2022
0
            b = (k * (255U - pp[2])) / 255U;
2023
0
            *cp++ = PACK(Map[r], Map[g], Map[b]);
2024
0
            pp += samplesperpixel;
2025
0
        }
2026
0
        pp += fromskewLocal;
2027
0
        cp += toskew;
2028
0
    }
2029
0
}
2030
2031
#define DECLARESepPutFunc(name)                                                \
2032
    static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
2033
                     uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
2034
                     unsigned char *r, unsigned char *g, unsigned char *b,     \
2035
                     unsigned char *a)
2036
2037
/*
2038
 * 8-bit unpacked samples => RGB
2039
 */
2040
DECLARESepPutFunc(putRGBseparate8bittile)
2041
0
{
2042
0
    (void)img;
2043
0
    (void)x;
2044
0
    (void)y;
2045
0
    (void)a;
2046
0
    for (; h > 0; --h)
2047
0
    {
2048
0
        UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
2049
0
        SKEW(r, g, b, fromskew);
2050
0
        cp += toskew;
2051
0
    }
2052
0
}
2053
2054
/*
2055
 * 8-bit unpacked samples => RGBA w/ associated alpha
2056
 */
2057
DECLARESepPutFunc(putRGBAAseparate8bittile)
2058
0
{
2059
0
    (void)img;
2060
0
    (void)x;
2061
0
    (void)y;
2062
0
    for (; h > 0; --h)
2063
0
    {
2064
0
        UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
2065
0
        SKEW4(r, g, b, a, fromskew);
2066
0
        cp += toskew;
2067
0
    }
2068
0
}
2069
2070
/*
2071
 * 8-bit unpacked CMYK samples => RGBA
2072
 */
2073
DECLARESepPutFunc(putCMYKseparate8bittile)
2074
0
{
2075
0
    (void)img;
2076
0
    (void)y;
2077
0
    for (; h > 0; --h)
2078
0
    {
2079
0
        unsigned int rv, gv, bv, kv;
2080
0
        for (x = w; x > 0; --x)
2081
0
        {
2082
0
            kv = 255U - *a++;
2083
0
            rv = (kv * (255U - *r++)) / 255U;
2084
0
            gv = (kv * (255U - *g++)) / 255U;
2085
0
            bv = (kv * (255U - *b++)) / 255U;
2086
0
            *cp++ = PACK4(rv, gv, bv, 255);
2087
0
        }
2088
0
        SKEW4(r, g, b, a, fromskew);
2089
0
        cp += toskew;
2090
0
    }
2091
0
}
2092
2093
/*
2094
 * 8-bit unpacked samples => RGBA w/ unassociated alpha
2095
 */
2096
DECLARESepPutFunc(putRGBUAseparate8bittile)
2097
0
{
2098
0
    (void)img;
2099
0
    (void)y;
2100
0
    for (; h > 0; --h)
2101
0
    {
2102
0
        uint32_t rv, gv, bv, av;
2103
0
        uint8_t *m;
2104
0
        for (x = w; x > 0; --x)
2105
0
        {
2106
0
            av = *a++;
2107
0
            m = img->UaToAa + ((size_t)av << 8);
2108
0
            rv = m[*r++];
2109
0
            gv = m[*g++];
2110
0
            bv = m[*b++];
2111
0
            *cp++ = PACK4(rv, gv, bv, av);
2112
0
        }
2113
0
        SKEW4(r, g, b, a, fromskew);
2114
0
        cp += toskew;
2115
0
    }
2116
0
}
2117
2118
/*
2119
 * 16-bit unpacked samples => RGB
2120
 */
2121
DECLARESepPutFunc(putRGBseparate16bittile)
2122
0
{
2123
0
    uint16_t *wr = (uint16_t *)r;
2124
0
    uint16_t *wg = (uint16_t *)g;
2125
0
    uint16_t *wb = (uint16_t *)b;
2126
0
    (void)img;
2127
0
    (void)y;
2128
0
    (void)a;
2129
0
    for (; h > 0; --h)
2130
0
    {
2131
0
        for (x = 0; x < w; x++)
2132
0
            *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
2133
0
                         img->Bitdepth16To8[*wb++]);
2134
0
        SKEW(wr, wg, wb, fromskew);
2135
0
        cp += toskew;
2136
0
    }
2137
0
}
2138
2139
/*
2140
 * 16-bit unpacked samples => RGBA w/ associated alpha
2141
 */
2142
DECLARESepPutFunc(putRGBAAseparate16bittile)
2143
0
{
2144
0
    uint16_t *wr = (uint16_t *)r;
2145
0
    uint16_t *wg = (uint16_t *)g;
2146
0
    uint16_t *wb = (uint16_t *)b;
2147
0
    uint16_t *wa = (uint16_t *)a;
2148
0
    (void)img;
2149
0
    (void)y;
2150
0
    for (; h > 0; --h)
2151
0
    {
2152
0
        for (x = 0; x < w; x++)
2153
0
            *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
2154
0
                          img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]);
2155
0
        SKEW4(wr, wg, wb, wa, fromskew);
2156
0
        cp += toskew;
2157
0
    }
2158
0
}
2159
2160
/*
2161
 * 16-bit unpacked samples => RGBA w/ unassociated alpha
2162
 */
2163
DECLARESepPutFunc(putRGBUAseparate16bittile)
2164
0
{
2165
0
    uint16_t *wr = (uint16_t *)r;
2166
0
    uint16_t *wg = (uint16_t *)g;
2167
0
    uint16_t *wb = (uint16_t *)b;
2168
0
    uint16_t *wa = (uint16_t *)a;
2169
0
    (void)img;
2170
0
    (void)y;
2171
0
    for (; h > 0; --h)
2172
0
    {
2173
0
        uint32_t r2, g2, b2, a2;
2174
0
        uint8_t *m;
2175
0
        for (x = w; x > 0; --x)
2176
0
        {
2177
0
            a2 = img->Bitdepth16To8[*wa++];
2178
0
            m = img->UaToAa + ((size_t)a2 << 8);
2179
0
            r2 = m[img->Bitdepth16To8[*wr++]];
2180
0
            g2 = m[img->Bitdepth16To8[*wg++]];
2181
0
            b2 = m[img->Bitdepth16To8[*wb++]];
2182
0
            *cp++ = PACK4(r2, g2, b2, a2);
2183
0
        }
2184
0
        SKEW4(wr, wg, wb, wa, fromskew);
2185
0
        cp += toskew;
2186
0
    }
2187
0
}
2188
2189
/*
2190
 * 8-bit packed CIE L*a*b 1976 samples => RGB
2191
 */
2192
DECLAREContigPutFunc(putcontig8bitCIELab8)
2193
0
{
2194
0
    float X, Y, Z;
2195
0
    uint32_t r, g, b;
2196
0
    (void)y;
2197
0
    const tmsize_t fromskewLocal = (tmsize_t)fromskew * (tmsize_t)3;
2198
0
    for (; h > 0; --h)
2199
0
    {
2200
0
        for (x = w; x > 0; --x)
2201
0
        {
2202
0
            TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0],
2203
0
                            (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z);
2204
0
            TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
2205
0
            *cp++ = PACK(r, g, b);
2206
0
            pp += 3;
2207
0
        }
2208
0
        cp += toskew;
2209
0
        pp += fromskewLocal;
2210
0
    }
2211
0
}
2212
2213
/*
2214
 * 16-bit packed CIE L*a*b 1976 samples => RGB
2215
 */
2216
DECLAREContigPutFunc(putcontig8bitCIELab16)
2217
0
{
2218
0
    float X, Y, Z;
2219
0
    uint32_t r, g, b;
2220
0
    uint16_t *wp = (uint16_t *)pp;
2221
0
    (void)y;
2222
0
    const tmsize_t fromskewLocal = (tmsize_t)fromskew * (tmsize_t)3;
2223
0
    for (; h > 0; --h)
2224
0
    {
2225
0
        for (x = w; x > 0; --x)
2226
0
        {
2227
0
            TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1],
2228
0
                              (int16_t)wp[2], &X, &Y, &Z);
2229
0
            TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
2230
0
            *cp++ = PACK(r, g, b);
2231
0
            wp += 3;
2232
0
        }
2233
0
        cp += toskew;
2234
0
        wp += fromskewLocal;
2235
0
    }
2236
0
}
2237
2238
/*
2239
 * YCbCr -> RGB conversion and packing routines.
2240
 */
2241
2242
#define YCbCrtoRGB(dst, Y)                                                     \
2243
0
    {                                                                          \
2244
0
        uint32_t r, g, b;                                                      \
2245
0
        TIFFYCbCrtoRGB(img->ycbcr, (Y), (int32_t)Cb, (int32_t)Cr, &r, &g, &b); \
2246
0
        dst = PACK(r, g, b);                                                   \
2247
0
    }
2248
2249
/*
2250
 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
2251
 */
2252
DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
2253
0
{
2254
0
    uint32_t *cp1 = cp + w + toskew;
2255
0
    uint32_t *cp2 = cp1 + w + toskew;
2256
0
    uint32_t *cp3 = cp2 + w + toskew;
2257
0
    const tmsize_t incr = 3 * (tmsize_t)w + 4 * (tmsize_t)toskew;
2258
2259
0
    (void)y;
2260
    /* adjust fromskew */
2261
0
    const tmsize_t fromskewLocal =
2262
0
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 4 + 2);
2263
0
    if ((h & 3) == 0 && (w & 3) == 0)
2264
0
    {
2265
0
        for (; h >= 4; h -= 4)
2266
0
        {
2267
0
            x = w >> 2;
2268
0
            do
2269
0
            {
2270
0
                int32_t Cb = pp[16];
2271
0
                int32_t Cr = pp[17];
2272
2273
0
                YCbCrtoRGB(cp[0], pp[0]);
2274
0
                YCbCrtoRGB(cp[1], pp[1]);
2275
0
                YCbCrtoRGB(cp[2], pp[2]);
2276
0
                YCbCrtoRGB(cp[3], pp[3]);
2277
0
                YCbCrtoRGB(cp1[0], pp[4]);
2278
0
                YCbCrtoRGB(cp1[1], pp[5]);
2279
0
                YCbCrtoRGB(cp1[2], pp[6]);
2280
0
                YCbCrtoRGB(cp1[3], pp[7]);
2281
0
                YCbCrtoRGB(cp2[0], pp[8]);
2282
0
                YCbCrtoRGB(cp2[1], pp[9]);
2283
0
                YCbCrtoRGB(cp2[2], pp[10]);
2284
0
                YCbCrtoRGB(cp2[3], pp[11]);
2285
0
                YCbCrtoRGB(cp3[0], pp[12]);
2286
0
                YCbCrtoRGB(cp3[1], pp[13]);
2287
0
                YCbCrtoRGB(cp3[2], pp[14]);
2288
0
                YCbCrtoRGB(cp3[3], pp[15]);
2289
2290
0
                cp += 4;
2291
0
                cp1 += 4;
2292
0
                cp2 += 4;
2293
0
                cp3 += 4;
2294
0
                pp += 18;
2295
0
            } while (--x);
2296
0
            cp += incr;
2297
0
            cp1 += incr;
2298
0
            cp2 += incr;
2299
0
            cp3 += incr;
2300
0
            pp += fromskewLocal;
2301
0
        }
2302
0
    }
2303
0
    else
2304
0
    {
2305
0
        while (h > 0)
2306
0
        {
2307
0
            for (x = w; x > 0;)
2308
0
            {
2309
0
                int32_t Cb = pp[16];
2310
0
                int32_t Cr = pp[17];
2311
0
                switch (x)
2312
0
                {
2313
0
                    default:
2314
0
                        switch (h)
2315
0
                        {
2316
0
                            default:
2317
0
                                YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
2318
0
                            case 3:
2319
0
                                YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
2320
0
                            case 2:
2321
0
                                YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
2322
0
                            case 1:
2323
0
                                YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
2324
0
                        } /* FALLTHROUGH */
2325
0
                    case 3:
2326
0
                        switch (h)
2327
0
                        {
2328
0
                            default:
2329
0
                                YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
2330
0
                            case 3:
2331
0
                                YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
2332
0
                            case 2:
2333
0
                                YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
2334
0
                            case 1:
2335
0
                                YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
2336
0
                        } /* FALLTHROUGH */
2337
0
                    case 2:
2338
0
                        switch (h)
2339
0
                        {
2340
0
                            default:
2341
0
                                YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
2342
0
                            case 3:
2343
0
                                YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */
2344
0
                            case 2:
2345
0
                                YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
2346
0
                            case 1:
2347
0
                                YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
2348
0
                        } /* FALLTHROUGH */
2349
0
                    case 1:
2350
0
                        switch (h)
2351
0
                        {
2352
0
                            default:
2353
0
                                YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
2354
0
                            case 3:
2355
0
                                YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */
2356
0
                            case 2:
2357
0
                                YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
2358
0
                            case 1:
2359
0
                                YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
2360
0
                        } /* FALLTHROUGH */
2361
0
                }
2362
0
                if (x < 4)
2363
0
                {
2364
0
                    cp += x;
2365
0
                    cp1 += x;
2366
0
                    cp2 += x;
2367
0
                    cp3 += x;
2368
0
                    x = 0;
2369
0
                }
2370
0
                else
2371
0
                {
2372
0
                    cp += 4;
2373
0
                    cp1 += 4;
2374
0
                    cp2 += 4;
2375
0
                    cp3 += 4;
2376
0
                    x -= 4;
2377
0
                }
2378
0
                pp += 18;
2379
0
            }
2380
0
            if (h <= 4)
2381
0
                break;
2382
0
            h -= 4;
2383
0
            cp += incr;
2384
0
            cp1 += incr;
2385
0
            cp2 += incr;
2386
0
            cp3 += incr;
2387
0
            pp += fromskewLocal;
2388
0
        }
2389
0
    }
2390
0
}
2391
2392
/*
2393
 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
2394
 */
2395
DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
2396
0
{
2397
0
    uint32_t *cp1 = cp + w + toskew;
2398
0
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2399
2400
0
    (void)y;
2401
0
    const tmsize_t fromskewLocal =
2402
0
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 2 + 2);
2403
0
    if ((w & 3) == 0 && (h & 1) == 0)
2404
0
    {
2405
0
        for (; h >= 2; h -= 2)
2406
0
        {
2407
0
            x = w >> 2;
2408
0
            do
2409
0
            {
2410
0
                int32_t Cb = pp[8];
2411
0
                int32_t Cr = pp[9];
2412
2413
0
                YCbCrtoRGB(cp[0], pp[0]);
2414
0
                YCbCrtoRGB(cp[1], pp[1]);
2415
0
                YCbCrtoRGB(cp[2], pp[2]);
2416
0
                YCbCrtoRGB(cp[3], pp[3]);
2417
0
                YCbCrtoRGB(cp1[0], pp[4]);
2418
0
                YCbCrtoRGB(cp1[1], pp[5]);
2419
0
                YCbCrtoRGB(cp1[2], pp[6]);
2420
0
                YCbCrtoRGB(cp1[3], pp[7]);
2421
2422
0
                cp += 4;
2423
0
                cp1 += 4;
2424
0
                pp += 10;
2425
0
            } while (--x);
2426
0
            cp += incr;
2427
0
            cp1 += incr;
2428
0
            pp += fromskewLocal;
2429
0
        }
2430
0
    }
2431
0
    else
2432
0
    {
2433
0
        while (h > 0)
2434
0
        {
2435
0
            for (x = w; x > 0;)
2436
0
            {
2437
0
                int32_t Cb = pp[8];
2438
0
                int32_t Cr = pp[9];
2439
0
                switch (x)
2440
0
                {
2441
0
                    default:
2442
0
                        switch (h)
2443
0
                        {
2444
0
                            default:
2445
0
                                YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
2446
0
                            case 1:
2447
0
                                YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
2448
0
                        } /* FALLTHROUGH */
2449
0
                    case 3:
2450
0
                        switch (h)
2451
0
                        {
2452
0
                            default:
2453
0
                                YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
2454
0
                            case 1:
2455
0
                                YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
2456
0
                        } /* FALLTHROUGH */
2457
0
                    case 2:
2458
0
                        switch (h)
2459
0
                        {
2460
0
                            default:
2461
0
                                YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
2462
0
                            case 1:
2463
0
                                YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
2464
0
                        } /* FALLTHROUGH */
2465
0
                    case 1:
2466
0
                        switch (h)
2467
0
                        {
2468
0
                            default:
2469
0
                                YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
2470
0
                            case 1:
2471
0
                                YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
2472
0
                        } /* FALLTHROUGH */
2473
0
                }
2474
0
                if (x < 4)
2475
0
                {
2476
0
                    cp += x;
2477
0
                    cp1 += x;
2478
0
                    x = 0;
2479
0
                }
2480
0
                else
2481
0
                {
2482
0
                    cp += 4;
2483
0
                    cp1 += 4;
2484
0
                    x -= 4;
2485
0
                }
2486
0
                pp += 10;
2487
0
            }
2488
0
            if (h <= 2)
2489
0
                break;
2490
0
            h -= 2;
2491
0
            cp += incr;
2492
0
            cp1 += incr;
2493
0
            pp += fromskewLocal;
2494
0
        }
2495
0
    }
2496
0
}
2497
2498
/*
2499
 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
2500
 */
2501
DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
2502
0
{
2503
0
    (void)y;
2504
0
    const tmsize_t fromskewLocal =
2505
0
        (tmsize_t)(fromskew / 4) * (tmsize_t)(4 * 1 + 2);
2506
0
    do
2507
0
    {
2508
0
        x = w >> 2;
2509
0
        while (x > 0)
2510
0
        {
2511
0
            int32_t Cb = pp[4];
2512
0
            int32_t Cr = pp[5];
2513
2514
0
            YCbCrtoRGB(cp[0], pp[0]);
2515
0
            YCbCrtoRGB(cp[1], pp[1]);
2516
0
            YCbCrtoRGB(cp[2], pp[2]);
2517
0
            YCbCrtoRGB(cp[3], pp[3]);
2518
2519
0
            cp += 4;
2520
0
            pp += 6;
2521
0
            x--;
2522
0
        }
2523
2524
0
        if ((w & 3) != 0)
2525
0
        {
2526
0
            int32_t Cb = pp[4];
2527
0
            int32_t Cr = pp[5];
2528
2529
0
            switch ((w & 3))
2530
0
            {
2531
0
                case 3:
2532
0
                    YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/
2533
0
                case 2:
2534
0
                    YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/
2535
0
                case 1:
2536
0
                    YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/
2537
0
                case 0:
2538
0
                    break;
2539
0
                default:
2540
0
                    break;
2541
0
            }
2542
2543
0
            cp += (w & 3);
2544
0
            pp += 6;
2545
0
        }
2546
2547
0
        cp += toskew;
2548
0
        pp += fromskewLocal;
2549
0
    } while (--h);
2550
0
}
2551
2552
/*
2553
 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
2554
 */
2555
DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
2556
0
{
2557
0
    uint32_t *cp2;
2558
0
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2559
0
    (void)y;
2560
0
    const tmsize_t fromskewLocal =
2561
0
        (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 2 + 2);
2562
0
    cp2 = cp + w + toskew;
2563
0
    while (h >= 2)
2564
0
    {
2565
0
        x = w;
2566
0
        while (x >= 2)
2567
0
        {
2568
0
            uint32_t Cb = pp[4];
2569
0
            uint32_t Cr = pp[5];
2570
0
            YCbCrtoRGB(cp[0], pp[0]);
2571
0
            YCbCrtoRGB(cp[1], pp[1]);
2572
0
            YCbCrtoRGB(cp2[0], pp[2]);
2573
0
            YCbCrtoRGB(cp2[1], pp[3]);
2574
0
            cp += 2;
2575
0
            cp2 += 2;
2576
0
            pp += 6;
2577
0
            x -= 2;
2578
0
        }
2579
0
        if (x == 1)
2580
0
        {
2581
0
            uint32_t Cb = pp[4];
2582
0
            uint32_t Cr = pp[5];
2583
0
            YCbCrtoRGB(cp[0], pp[0]);
2584
0
            YCbCrtoRGB(cp2[0], pp[2]);
2585
0
            cp++;
2586
0
            cp2++;
2587
0
            pp += 6;
2588
0
        }
2589
0
        cp += incr;
2590
0
        cp2 += incr;
2591
0
        pp += fromskewLocal;
2592
0
        h -= 2;
2593
0
    }
2594
0
    if (h == 1)
2595
0
    {
2596
0
        x = w;
2597
0
        while (x >= 2)
2598
0
        {
2599
0
            uint32_t Cb = pp[4];
2600
0
            uint32_t Cr = pp[5];
2601
0
            YCbCrtoRGB(cp[0], pp[0]);
2602
0
            YCbCrtoRGB(cp[1], pp[1]);
2603
0
            cp += 2;
2604
0
            cp2 += 2;
2605
0
            pp += 6;
2606
0
            x -= 2;
2607
0
        }
2608
0
        if (x == 1)
2609
0
        {
2610
0
            uint32_t Cb = pp[4];
2611
0
            uint32_t Cr = pp[5];
2612
0
            YCbCrtoRGB(cp[0], pp[0]);
2613
0
        }
2614
0
    }
2615
0
}
2616
2617
/*
2618
 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
2619
 */
2620
DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
2621
0
{
2622
0
    (void)y;
2623
0
    const tmsize_t fromskewLocal =
2624
0
        (tmsize_t)(fromskew / 2) * (tmsize_t)(2 * 1 + 2);
2625
0
    do
2626
0
    {
2627
0
        x = w >> 1;
2628
0
        while (x > 0)
2629
0
        {
2630
0
            int32_t Cb = pp[2];
2631
0
            int32_t Cr = pp[3];
2632
2633
0
            YCbCrtoRGB(cp[0], pp[0]);
2634
0
            YCbCrtoRGB(cp[1], pp[1]);
2635
2636
0
            cp += 2;
2637
0
            pp += 4;
2638
0
            x--;
2639
0
        }
2640
2641
0
        if ((w & 1) != 0)
2642
0
        {
2643
0
            int32_t Cb = pp[2];
2644
0
            int32_t Cr = pp[3];
2645
2646
0
            YCbCrtoRGB(cp[0], pp[0]);
2647
2648
0
            cp += 1;
2649
0
            pp += 4;
2650
0
        }
2651
2652
0
        cp += toskew;
2653
0
        pp += fromskewLocal;
2654
0
    } while (--h);
2655
0
}
2656
2657
/*
2658
 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
2659
 */
2660
DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
2661
0
{
2662
0
    uint32_t *cp2;
2663
0
    const tmsize_t incr = 2 * (tmsize_t)toskew + (tmsize_t)w;
2664
0
    (void)y;
2665
0
    const tmsize_t fromskewLocal =
2666
0
        (tmsize_t)(fromskew / 1) * (tmsize_t)(1 * 2 + 2);
2667
0
    cp2 = cp + w + toskew;
2668
0
    while (h >= 2)
2669
0
    {
2670
0
        x = w;
2671
0
        do
2672
0
        {
2673
0
            uint32_t Cb = pp[2];
2674
0
            uint32_t Cr = pp[3];
2675
0
            YCbCrtoRGB(cp[0], pp[0]);
2676
0
            YCbCrtoRGB(cp2[0], pp[1]);
2677
0
            cp++;
2678
0
            cp2++;
2679
0
            pp += 4;
2680
0
        } while (--x);
2681
0
        cp += incr;
2682
0
        cp2 += incr;
2683
0
        pp += fromskewLocal;
2684
0
        h -= 2;
2685
0
    }
2686
0
    if (h == 1)
2687
0
    {
2688
0
        x = w;
2689
0
        do
2690
0
        {
2691
0
            uint32_t Cb = pp[2];
2692
0
            uint32_t Cr = pp[3];
2693
0
            YCbCrtoRGB(cp[0], pp[0]);
2694
0
            cp++;
2695
0
            pp += 4;
2696
0
        } while (--x);
2697
0
    }
2698
0
}
2699
2700
/*
2701
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2702
 */
2703
DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
2704
0
{
2705
0
    (void)y;
2706
0
    const tmsize_t fromskewLocal =
2707
0
        (tmsize_t)(fromskew / 1) * (tmsize_t)(1 * 1 + 2);
2708
0
    do
2709
0
    {
2710
0
        x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
2711
0
        do
2712
0
        {
2713
0
            int32_t Cb = pp[1];
2714
0
            int32_t Cr = pp[2];
2715
2716
0
            YCbCrtoRGB(*cp++, pp[0]);
2717
2718
0
            pp += 3;
2719
0
        } while (--x);
2720
0
        cp += toskew;
2721
0
        pp += fromskewLocal;
2722
0
    } while (--h);
2723
0
}
2724
2725
/*
2726
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2727
 */
2728
DECLARESepPutFunc(putseparate8bitYCbCr11tile)
2729
0
{
2730
0
    (void)y;
2731
0
    (void)a;
2732
    /* TODO: naming of input vars is still off, change obfuscating declaration
2733
     * inside define, or resolve obfuscation */
2734
0
    for (; h > 0; --h)
2735
0
    {
2736
0
        x = w;
2737
0
        do
2738
0
        {
2739
0
            uint32_t dr, dg, db;
2740
0
            TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db);
2741
0
            *cp++ = PACK(dr, dg, db);
2742
0
        } while (--x);
2743
0
        SKEW(r, g, b, fromskew);
2744
0
        cp += toskew;
2745
0
    }
2746
0
}
2747
#undef YCbCrtoRGB
2748
2749
static int isInRefBlackWhiteRange(float f)
2750
0
{
2751
0
    return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF;
2752
0
}
2753
2754
static int initYCbCrConversion(TIFFRGBAImage *img)
2755
0
{
2756
0
    static const char module[] = "initYCbCrConversion";
2757
2758
0
    float *luma, *refBlackWhite;
2759
2760
0
    if (img->ycbcr == NULL)
2761
0
    {
2762
0
        img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt(
2763
0
            img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) +
2764
0
                          4 * 256 * sizeof(TIFFRGBValue) +
2765
0
                          2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t));
2766
0
        if (img->ycbcr == NULL)
2767
0
        {
2768
0
            TIFFErrorExtR(img->tif, module,
2769
0
                          "No space for YCbCr->RGB conversion state");
2770
0
            return (0);
2771
0
        }
2772
0
    }
2773
2774
0
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
2775
0
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
2776
0
                          &refBlackWhite);
2777
2778
    /* Do some validation to avoid later issues. Detect NaN for now */
2779
    /* and also if lumaGreen is zero since we divide by it later */
2780
0
    if (isnan(luma[0]) || isnan(luma[1]) || TIFF_FLOAT_EQ(luma[1], 0.0f) ||
2781
0
        isnan(luma[2]))
2782
0
    {
2783
0
        TIFFErrorExtR(img->tif, module,
2784
0
                      "Invalid values for YCbCrCoefficients tag");
2785
0
        return (0);
2786
0
    }
2787
2788
0
    if (!isInRefBlackWhiteRange(refBlackWhite[0]) ||
2789
0
        !isInRefBlackWhiteRange(refBlackWhite[1]) ||
2790
0
        !isInRefBlackWhiteRange(refBlackWhite[2]) ||
2791
0
        !isInRefBlackWhiteRange(refBlackWhite[3]) ||
2792
0
        !isInRefBlackWhiteRange(refBlackWhite[4]) ||
2793
0
        !isInRefBlackWhiteRange(refBlackWhite[5]))
2794
0
    {
2795
0
        TIFFErrorExtR(img->tif, module,
2796
0
                      "Invalid values for ReferenceBlackWhite tag");
2797
0
        return (0);
2798
0
    }
2799
2800
0
    if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
2801
0
        return (0);
2802
0
    return (1);
2803
0
}
2804
2805
static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img)
2806
0
{
2807
0
    static const char module[] = "initCIELabConversion";
2808
2809
0
    float *whitePoint;
2810
0
    float refWhite[3];
2811
2812
0
    TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
2813
0
    if (TIFF_FLOAT_EQ(whitePoint[1], 0.0f))
2814
0
    {
2815
0
        TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag.");
2816
0
        return NULL;
2817
0
    }
2818
2819
0
    if (!img->cielab)
2820
0
    {
2821
0
        img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt(
2822
0
            img->tif, sizeof(TIFFCIELabToRGB));
2823
0
        if (!img->cielab)
2824
0
        {
2825
0
            TIFFErrorExtR(img->tif, module,
2826
0
                          "No space for CIE L*a*b*->RGB conversion state.");
2827
0
            return NULL;
2828
0
        }
2829
0
    }
2830
2831
0
    refWhite[1] = 100.0f;
2832
0
    refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
2833
0
    refWhite[2] =
2834
0
        (1.0f - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];
2835
0
    if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0)
2836
0
    {
2837
0
        TIFFErrorExtR(img->tif, module,
2838
0
                      "Failed to initialize CIE L*a*b*->RGB conversion state.");
2839
0
        _TIFFfreeExt(img->tif, img->cielab);
2840
0
        return NULL;
2841
0
    }
2842
2843
0
    if (img->bitspersample == 8)
2844
0
        return putcontig8bitCIELab8;
2845
0
    else if (img->bitspersample == 16)
2846
0
        return putcontig8bitCIELab16;
2847
0
    return NULL;
2848
0
}
2849
2850
/*
2851
 * Greyscale images with less than 8 bits/sample are handled
2852
 * with a table to avoid lots of shifts and masks.  The table
2853
 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
2854
 * pixel values simply by indexing into the table with one
2855
 * number.
2856
 */
2857
static int makebwmap(TIFFRGBAImage *img)
2858
0
{
2859
0
    TIFFRGBValue *Map = img->Map;
2860
0
    int bitspersample = img->bitspersample;
2861
0
    int nsamples = 8 / bitspersample;
2862
0
    int i;
2863
0
    uint32_t *p;
2864
2865
0
    if (nsamples == 0)
2866
0
        nsamples = 1;
2867
2868
0
    img->BWmap = (uint32_t **)_TIFFmallocExt(
2869
0
        img->tif,
2870
0
        (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
2871
0
                   ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
2872
0
    if (img->BWmap == NULL)
2873
0
    {
2874
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
2875
0
                      "No space for B&W mapping table");
2876
0
        return (0);
2877
0
    }
2878
0
    p = (uint32_t *)(img->BWmap + 256);
2879
0
    for (i = 0; i < 256; i++)
2880
0
    {
2881
0
        TIFFRGBValue c;
2882
0
        img->BWmap[i] = p;
2883
0
        switch (bitspersample)
2884
0
        {
2885
0
#define GREY(x)                                                                \
2886
0
    c = Map[x];                                                                \
2887
0
    *p++ = PACK(c, c, c);
2888
0
            case 1:
2889
0
                GREY(i >> 7);
2890
0
                GREY((i >> 6) & 1);
2891
0
                GREY((i >> 5) & 1);
2892
0
                GREY((i >> 4) & 1);
2893
0
                GREY((i >> 3) & 1);
2894
0
                GREY((i >> 2) & 1);
2895
0
                GREY((i >> 1) & 1);
2896
0
                GREY(i & 1);
2897
0
                break;
2898
0
            case 2:
2899
0
                GREY(i >> 6);
2900
0
                GREY((i >> 4) & 3);
2901
0
                GREY((i >> 2) & 3);
2902
0
                GREY(i & 3);
2903
0
                break;
2904
0
            case 4:
2905
0
                GREY(i >> 4);
2906
0
                GREY(i & 0xf);
2907
0
                break;
2908
0
            case 8:
2909
0
            case 16:
2910
0
                GREY(i);
2911
0
                break;
2912
0
            default:
2913
0
                break;
2914
0
        }
2915
0
#undef GREY
2916
0
    }
2917
0
    return (1);
2918
0
}
2919
2920
/*
2921
 * Construct a mapping table to convert from the range
2922
 * of the data samples to [0,255] --for display.  This
2923
 * process also handles inverting B&W images when needed.
2924
 */
2925
static int setupMap(TIFFRGBAImage *img)
2926
0
{
2927
0
    int32_t x, range;
2928
2929
0
    range = (int32_t)((1U << img->bitspersample) - 1U);
2930
2931
    /* treat 16 bit the same as eight bit */
2932
0
    if (img->bitspersample == 16)
2933
0
        range = 255;
2934
2935
0
    img->Map = (TIFFRGBValue *)_TIFFmallocExt(
2936
0
        img->tif, (tmsize_t)((size_t)(range + 1) * sizeof(TIFFRGBValue)));
2937
0
    if (img->Map == NULL)
2938
0
    {
2939
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
2940
0
                      "No space for photometric conversion table");
2941
0
        return (0);
2942
0
    }
2943
0
    if (img->photometric == PHOTOMETRIC_MINISWHITE)
2944
0
    {
2945
0
        for (x = 0; x <= range; x++)
2946
0
            img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range);
2947
0
    }
2948
0
    else
2949
0
    {
2950
0
        for (x = 0; x <= range; x++)
2951
0
            img->Map[x] = (TIFFRGBValue)((x * 255) / range);
2952
0
    }
2953
0
    if (img->bitspersample <= 16 &&
2954
0
        (img->photometric == PHOTOMETRIC_MINISBLACK ||
2955
0
         img->photometric == PHOTOMETRIC_MINISWHITE))
2956
0
    {
2957
        /*
2958
         * Use photometric mapping table to construct
2959
         * unpacking tables for samples <= 8 bits.
2960
         */
2961
0
        if (!makebwmap(img))
2962
0
            return (0);
2963
        /* no longer need Map, free it */
2964
0
        _TIFFfreeExt(img->tif, img->Map);
2965
0
        img->Map = NULL;
2966
0
    }
2967
0
    return (1);
2968
0
}
2969
2970
static int checkcmap(TIFFRGBAImage *img)
2971
0
{
2972
0
    uint16_t *r = img->redcmap;
2973
0
    uint16_t *g = img->greencmap;
2974
0
    uint16_t *b = img->bluecmap;
2975
0
    long n = 1L << img->bitspersample;
2976
2977
0
    while (n-- > 0)
2978
0
        if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
2979
0
            return (16);
2980
0
    return (8);
2981
0
}
2982
2983
static void cvtcmap(TIFFRGBAImage *img)
2984
0
{
2985
0
    uint16_t *r = img->redcmap;
2986
0
    uint16_t *g = img->greencmap;
2987
0
    uint16_t *b = img->bluecmap;
2988
0
    long i;
2989
2990
0
    for (i = (1L << img->bitspersample) - 1; i >= 0; i--)
2991
0
    {
2992
0
#define CVT(x) ((uint16_t)((x) >> 8))
2993
0
        r[i] = CVT(r[i]);
2994
0
        g[i] = CVT(g[i]);
2995
0
        b[i] = CVT(b[i]);
2996
0
#undef CVT
2997
0
    }
2998
0
}
2999
3000
/*
3001
 * Palette images with <= 8 bits/sample are handled
3002
 * with a table to avoid lots of shifts and masks.  The table
3003
 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
3004
 * pixel values simply by indexing into the table with one
3005
 * number.
3006
 */
3007
static int makecmap(TIFFRGBAImage *img)
3008
0
{
3009
0
    int bitspersample = img->bitspersample;
3010
0
    int nsamples = 8 / bitspersample;
3011
0
    uint16_t *r = img->redcmap;
3012
0
    uint16_t *g = img->greencmap;
3013
0
    uint16_t *b = img->bluecmap;
3014
0
    uint32_t *p;
3015
0
    int i;
3016
3017
0
    img->PALmap = (uint32_t **)_TIFFmallocExt(
3018
0
        img->tif,
3019
0
        (tmsize_t)((size_t)256 * sizeof(uint32_t *) +
3020
0
                   ((size_t)256 * (size_t)nsamples * sizeof(uint32_t))));
3021
0
    if (img->PALmap == NULL)
3022
0
    {
3023
0
        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
3024
0
                      "No space for Palette mapping table");
3025
0
        return (0);
3026
0
    }
3027
0
    p = (uint32_t *)(img->PALmap + 256);
3028
0
    for (i = 0; i < 256; i++)
3029
0
    {
3030
0
        TIFFRGBValue c;
3031
0
        img->PALmap[i] = p;
3032
0
#define CMAP(x)                                                                \
3033
0
    c = (TIFFRGBValue)x;                                                       \
3034
0
    *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff);
3035
0
        switch (bitspersample)
3036
0
        {
3037
0
            case 1:
3038
0
                CMAP(i >> 7);
3039
0
                CMAP((i >> 6) & 1);
3040
0
                CMAP((i >> 5) & 1);
3041
0
                CMAP((i >> 4) & 1);
3042
0
                CMAP((i >> 3) & 1);
3043
0
                CMAP((i >> 2) & 1);
3044
0
                CMAP((i >> 1) & 1);
3045
0
                CMAP(i & 1);
3046
0
                break;
3047
0
            case 2:
3048
0
                CMAP(i >> 6);
3049
0
                CMAP((i >> 4) & 3);
3050
0
                CMAP((i >> 2) & 3);
3051
0
                CMAP(i & 3);
3052
0
                break;
3053
0
            case 4:
3054
0
                CMAP(i >> 4);
3055
0
                CMAP(i & 0xf);
3056
0
                break;
3057
0
            case 8:
3058
0
                CMAP(i);
3059
0
                break;
3060
0
            default:
3061
0
                break;
3062
0
        }
3063
0
#undef CMAP
3064
0
    }
3065
0
    return (1);
3066
0
}
3067
3068
/*
3069
 * Construct any mapping table used
3070
 * by the associated put routine.
3071
 */
3072
static int buildMap(TIFFRGBAImage *img)
3073
0
{
3074
0
    switch (img->photometric)
3075
0
    {
3076
0
        case PHOTOMETRIC_RGB:
3077
0
        case PHOTOMETRIC_YCBCR:
3078
0
        case PHOTOMETRIC_SEPARATED:
3079
0
            if (img->bitspersample == 8)
3080
0
                break;
3081
            /* fall through... */
3082
0
        case PHOTOMETRIC_MINISBLACK:
3083
0
        case PHOTOMETRIC_MINISWHITE:
3084
0
            if (!setupMap(img))
3085
0
                return (0);
3086
0
            break;
3087
0
        case PHOTOMETRIC_PALETTE:
3088
            /*
3089
             * Convert 16-bit colormap to 8-bit (unless it looks
3090
             * like an old-style 8-bit colormap).
3091
             */
3092
0
            if (checkcmap(img) == 16)
3093
0
                cvtcmap(img);
3094
0
            else
3095
0
                TIFFWarningExtR(img->tif, TIFFFileName(img->tif),
3096
0
                                "Assuming 8-bit colormap");
3097
            /*
3098
             * Use mapping table and colormap to construct
3099
             * unpacking tables for samples < 8 bits.
3100
             */
3101
0
            if (img->bitspersample <= 8 && !makecmap(img))
3102
0
                return (0);
3103
0
            break;
3104
0
        default:
3105
0
            break;
3106
0
    }
3107
0
    return (1);
3108
0
}
3109
3110
/*
3111
 * Select the appropriate conversion routine for packed data.
3112
 */
3113
static int PickContigCase(TIFFRGBAImage *img)
3114
0
{
3115
0
    img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
3116
0
    img->put.contig = NULL;
3117
0
    switch (img->photometric)
3118
0
    {
3119
0
        case PHOTOMETRIC_RGB:
3120
0
            switch (img->bitspersample)
3121
0
            {
3122
0
                case 8:
3123
0
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
3124
0
                        img->samplesperpixel >= 4)
3125
0
                        img->put.contig = putRGBAAcontig8bittile;
3126
0
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
3127
0
                             img->samplesperpixel >= 4)
3128
0
                    {
3129
0
                        if (BuildMapUaToAa(img))
3130
0
                            img->put.contig = putRGBUAcontig8bittile;
3131
0
                    }
3132
0
                    else if (img->samplesperpixel >= 3)
3133
0
                        img->put.contig = putRGBcontig8bittile;
3134
0
                    break;
3135
0
                case 16:
3136
0
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
3137
0
                        img->samplesperpixel >= 4)
3138
0
                    {
3139
0
                        if (BuildMapBitdepth16To8(img))
3140
0
                            img->put.contig = putRGBAAcontig16bittile;
3141
0
                    }
3142
0
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
3143
0
                             img->samplesperpixel >= 4)
3144
0
                    {
3145
0
                        if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
3146
0
                            img->put.contig = putRGBUAcontig16bittile;
3147
0
                    }
3148
0
                    else if (img->samplesperpixel >= 3)
3149
0
                    {
3150
0
                        if (BuildMapBitdepth16To8(img))
3151
0
                            img->put.contig = putRGBcontig16bittile;
3152
0
                    }
3153
0
                    break;
3154
0
                default:
3155
0
                    break;
3156
0
            }
3157
0
            break;
3158
0
        case PHOTOMETRIC_SEPARATED:
3159
0
            if (img->samplesperpixel >= 4 && buildMap(img))
3160
0
            {
3161
0
                if (img->bitspersample == 8)
3162
0
                {
3163
0
                    if (!img->Map)
3164
0
                        img->put.contig = putRGBcontig8bitCMYKtile;
3165
0
                    else
3166
0
                        img->put.contig = putRGBcontig8bitCMYKMaptile;
3167
0
                }
3168
0
            }
3169
0
            break;
3170
0
        case PHOTOMETRIC_PALETTE:
3171
0
            if (buildMap(img))
3172
0
            {
3173
0
                switch (img->bitspersample)
3174
0
                {
3175
0
                    case 8:
3176
0
                        img->put.contig = put8bitcmaptile;
3177
0
                        break;
3178
0
                    case 4:
3179
0
                        img->put.contig = put4bitcmaptile;
3180
0
                        break;
3181
0
                    case 2:
3182
0
                        img->put.contig = put2bitcmaptile;
3183
0
                        break;
3184
0
                    case 1:
3185
0
                        img->put.contig = put1bitcmaptile;
3186
0
                        break;
3187
0
                    default:
3188
0
                        break;
3189
0
                }
3190
0
            }
3191
0
            break;
3192
0
        case PHOTOMETRIC_MINISWHITE:
3193
0
        case PHOTOMETRIC_MINISBLACK:
3194
0
            if (buildMap(img))
3195
0
            {
3196
0
                switch (img->bitspersample)
3197
0
                {
3198
0
                    case 16:
3199
0
                        img->put.contig = put16bitbwtile;
3200
0
                        break;
3201
0
                    case 8:
3202
0
                        if (img->alpha && img->samplesperpixel == 2)
3203
0
                            img->put.contig = putagreytile;
3204
0
                        else
3205
0
                            img->put.contig = putgreytile;
3206
0
                        break;
3207
0
                    case 4:
3208
0
                        img->put.contig = put4bitbwtile;
3209
0
                        break;
3210
0
                    case 2:
3211
0
                        img->put.contig = put2bitbwtile;
3212
0
                        break;
3213
0
                    case 1:
3214
0
                        img->put.contig = put1bitbwtile;
3215
0
                        break;
3216
0
                    default:
3217
0
                        break;
3218
0
                }
3219
0
            }
3220
0
            break;
3221
0
        case PHOTOMETRIC_YCBCR:
3222
0
            if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
3223
0
            {
3224
0
                if (initYCbCrConversion(img) != 0)
3225
0
                {
3226
                    /*
3227
                     * The 6.0 spec says that subsampling must be
3228
                     * one of 1, 2, or 4, and that vertical subsampling
3229
                     * must always be <= horizontal subsampling; so
3230
                     * there are only a few possibilities and we just
3231
                     * enumerate the cases.
3232
                     * Joris: added support for the [1,2] case, nonetheless, to
3233
                     * accommodate some OJPEG files
3234
                     */
3235
0
                    uint16_t SubsamplingHor;
3236
0
                    uint16_t SubsamplingVer;
3237
0
                    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
3238
0
                                          &SubsamplingHor, &SubsamplingVer);
3239
                    /* Validate that the image dimensions are compatible with
3240
                    the subsampling block. All putcontig8bitYCbCrXYtile routines
3241
                    assume width >= X and height >= Y. */
3242
0
                    if (img->width < SubsamplingHor ||
3243
0
                        img->height < SubsamplingVer)
3244
0
                    {
3245
0
                        TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
3246
0
                                      "YCbCr subsampling (%u,%u) incompatible "
3247
0
                                      "with image size %ux%u",
3248
0
                                      SubsamplingHor, SubsamplingVer,
3249
0
                                      img->width, img->height);
3250
0
                        return (0);
3251
0
                    }
3252
0
                    switch ((SubsamplingHor << 4) | SubsamplingVer)
3253
0
                    {
3254
0
                        case 0x44:
3255
0
                            img->put.contig = putcontig8bitYCbCr44tile;
3256
0
                            break;
3257
0
                        case 0x42:
3258
0
                            img->put.contig = putcontig8bitYCbCr42tile;
3259
0
                            break;
3260
0
                        case 0x41:
3261
0
                            img->put.contig = putcontig8bitYCbCr41tile;
3262
0
                            break;
3263
0
                        case 0x22:
3264
0
                            img->put.contig = putcontig8bitYCbCr22tile;
3265
0
                            break;
3266
0
                        case 0x21:
3267
0
                            img->put.contig = putcontig8bitYCbCr21tile;
3268
0
                            break;
3269
0
                        case 0x12:
3270
0
                            img->put.contig = putcontig8bitYCbCr12tile;
3271
0
                            break;
3272
0
                        case 0x11:
3273
0
                            img->put.contig = putcontig8bitYCbCr11tile;
3274
0
                            break;
3275
0
                        default:
3276
0
                            break;
3277
0
                    }
3278
0
                }
3279
0
            }
3280
0
            break;
3281
0
        case PHOTOMETRIC_CIELAB:
3282
0
            if (img->samplesperpixel == 3 && buildMap(img))
3283
0
            {
3284
0
                if (img->bitspersample == 8 || img->bitspersample == 16)
3285
0
                    img->put.contig = initCIELabConversion(img);
3286
0
                break;
3287
0
            }
3288
0
            break;
3289
0
        default:
3290
0
            break;
3291
0
    }
3292
0
    return ((img->get != NULL) && (img->put.contig != NULL));
3293
0
}
3294
3295
/*
3296
 * Select the appropriate conversion routine for unpacked data.
3297
 *
3298
 * NB: we assume that unpacked single channel data is directed
3299
 *   to the "packed routines.
3300
 */
3301
static int PickSeparateCase(TIFFRGBAImage *img)
3302
0
{
3303
0
    img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
3304
0
    img->put.separate = NULL;
3305
0
    switch (img->photometric)
3306
0
    {
3307
0
        case PHOTOMETRIC_MINISWHITE:
3308
0
        case PHOTOMETRIC_MINISBLACK:
3309
            /* greyscale images processed pretty much as RGB by gtTileSeparate
3310
             */
3311
0
        case PHOTOMETRIC_RGB:
3312
0
            switch (img->bitspersample)
3313
0
            {
3314
0
                case 8:
3315
0
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
3316
0
                        img->put.separate = putRGBAAseparate8bittile;
3317
0
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
3318
0
                    {
3319
0
                        if (BuildMapUaToAa(img))
3320
0
                            img->put.separate = putRGBUAseparate8bittile;
3321
0
                    }
3322
0
                    else
3323
0
                        img->put.separate = putRGBseparate8bittile;
3324
0
                    break;
3325
0
                case 16:
3326
0
                    if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
3327
0
                    {
3328
0
                        if (BuildMapBitdepth16To8(img))
3329
0
                            img->put.separate = putRGBAAseparate16bittile;
3330
0
                    }
3331
0
                    else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
3332
0
                    {
3333
0
                        if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
3334
0
                            img->put.separate = putRGBUAseparate16bittile;
3335
0
                    }
3336
0
                    else
3337
0
                    {
3338
0
                        if (BuildMapBitdepth16To8(img))
3339
0
                            img->put.separate = putRGBseparate16bittile;
3340
0
                    }
3341
0
                    break;
3342
0
                default:
3343
0
                    break;
3344
0
            }
3345
0
            break;
3346
0
        case PHOTOMETRIC_SEPARATED:
3347
0
            if (img->bitspersample == 8 && img->samplesperpixel == 4)
3348
0
            {
3349
                /* Not alpha, but seems like the only way to get 4th band */
3350
0
                img->alpha = 1;
3351
0
                img->put.separate = putCMYKseparate8bittile;
3352
0
            }
3353
0
            break;
3354
0
        case PHOTOMETRIC_YCBCR:
3355
0
            if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
3356
0
            {
3357
0
                if (initYCbCrConversion(img) != 0)
3358
0
                {
3359
0
                    uint16_t hs, vs;
3360
0
                    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
3361
0
                                          &hs, &vs);
3362
0
                    switch ((hs << 4) | vs)
3363
0
                    {
3364
0
                        case 0x11:
3365
0
                            img->put.separate = putseparate8bitYCbCr11tile;
3366
0
                            break;
3367
                            /* TODO: add other cases here */
3368
0
                        default:
3369
0
                            break;
3370
0
                    }
3371
0
                }
3372
0
            }
3373
0
            break;
3374
0
        default:
3375
0
            break;
3376
0
    }
3377
0
    return ((img->get != NULL) && (img->put.separate != NULL));
3378
0
}
3379
3380
static int BuildMapUaToAa(TIFFRGBAImage *img)
3381
0
{
3382
0
    static const char module[] = "BuildMapUaToAa";
3383
0
    uint8_t *m;
3384
0
    uint16_t na, nv;
3385
0
    assert(img->UaToAa == NULL);
3386
0
    img->UaToAa = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
3387
0
    if (img->UaToAa == NULL)
3388
0
    {
3389
0
        TIFFErrorExtR(img->tif, module, "Out of memory");
3390
0
        return (0);
3391
0
    }
3392
0
    m = img->UaToAa;
3393
0
    for (na = 0; na < 256; na++)
3394
0
    {
3395
0
        for (nv = 0; nv < 256; nv++)
3396
0
            *m++ = (uint8_t)((nv * na + 127) / 255);
3397
0
    }
3398
0
    return (1);
3399
0
}
3400
3401
static int BuildMapBitdepth16To8(TIFFRGBAImage *img)
3402
0
{
3403
0
    static const char module[] = "BuildMapBitdepth16To8";
3404
0
    uint8_t *m;
3405
0
    uint32_t n;
3406
0
    assert(img->Bitdepth16To8 == NULL);
3407
0
    img->Bitdepth16To8 = (uint8_t *)_TIFFmallocExt(img->tif, 65536);
3408
0
    if (img->Bitdepth16To8 == NULL)
3409
0
    {
3410
0
        TIFFErrorExtR(img->tif, module, "Out of memory");
3411
0
        return (0);
3412
0
    }
3413
0
    m = img->Bitdepth16To8;
3414
0
    for (n = 0; n < 65536; n++)
3415
0
        *m++ = (uint8_t)((n + 128) / 257);
3416
0
    return (1);
3417
0
}
3418
3419
/*
3420
 * Read a whole strip off data from the file, and convert to RGBA form.
3421
 * If this is the last strip, then it will only contain the portion of
3422
 * the strip that is actually within the image space.  The result is
3423
 * organized in bottom to top form.
3424
 */
3425
3426
int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster)
3427
3428
0
{
3429
0
    return TIFFReadRGBAStripExt(tif, row, raster, 0);
3430
0
}
3431
3432
int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster,
3433
                         int stop_on_error)
3434
3435
0
{
3436
0
    char emsg[EMSG_BUF_SIZE] = "";
3437
0
    TIFFRGBAImage img;
3438
0
    int ok;
3439
0
    uint32_t rowsperstrip, rows_to_read;
3440
3441
0
    if (TIFFIsTiled(tif))
3442
0
    {
3443
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3444
0
                      "Can't use TIFFReadRGBAStrip() with tiled file.");
3445
0
        return (0);
3446
0
    }
3447
3448
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
3449
3450
0
    if (rowsperstrip == 0)
3451
0
    {
3452
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
3453
0
        return (0);
3454
0
    }
3455
3456
0
    if ((row % rowsperstrip) != 0)
3457
0
    {
3458
0
        TIFFErrorExtR(
3459
0
            tif, TIFFFileName(tif),
3460
0
            "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
3461
0
        return (0);
3462
0
    }
3463
3464
0
    if (TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
3465
0
    {
3466
0
        if (row >= img.height)
3467
0
        {
3468
0
            TIFFErrorExtR(tif, TIFFFileName(tif),
3469
0
                          "Invalid row passed to TIFFReadRGBAStrip().");
3470
0
            TIFFRGBAImageEnd(&img);
3471
0
            return (0);
3472
0
        }
3473
3474
0
        img.row_offset = (int)row;
3475
0
        img.col_offset = 0;
3476
3477
0
        if (row + rowsperstrip > img.height)
3478
0
            rows_to_read = img.height - row;
3479
0
        else
3480
0
            rows_to_read = rowsperstrip;
3481
3482
0
        ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read);
3483
3484
0
        TIFFRGBAImageEnd(&img);
3485
0
    }
3486
0
    else
3487
0
    {
3488
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
3489
0
        ok = 0;
3490
0
    }
3491
3492
0
    return (ok);
3493
0
}
3494
3495
/*
3496
 * Read a whole tile off data from the file, and convert to RGBA form.
3497
 * The returned RGBA data is organized from bottom to top of tile,
3498
 * and may include zeroed areas if the tile extends off the image.
3499
 */
3500
3501
int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster)
3502
3503
0
{
3504
0
    return TIFFReadRGBATileExt(tif, col, row, raster, 0);
3505
0
}
3506
3507
int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster,
3508
                        int stop_on_error)
3509
0
{
3510
0
    char emsg[EMSG_BUF_SIZE] = "";
3511
0
    TIFFRGBAImage img;
3512
0
    int ok;
3513
0
    uint32_t tile_xsize, tile_ysize;
3514
0
    uint32_t read_xsize, read_ysize;
3515
0
    uint32_t i_row;
3516
3517
    /*
3518
     * Verify that our request is legal - on a tile file, and on a
3519
     * tile boundary.
3520
     */
3521
3522
0
    if (!TIFFIsTiled(tif))
3523
0
    {
3524
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3525
0
                      "Can't use TIFFReadRGBATile() with striped file.");
3526
0
        return (0);
3527
0
    }
3528
3529
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
3530
0
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
3531
0
    if (tile_xsize == 0 || tile_ysize == 0)
3532
0
    {
3533
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3534
0
                      "tile_xsize or tile_ysize is zero");
3535
0
        return (0);
3536
0
    }
3537
3538
0
    if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)
3539
0
    {
3540
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3541
0
                      "Row/col passed to TIFFReadRGBATile() must be top"
3542
0
                      "left corner of a tile.");
3543
0
        return (0);
3544
0
    }
3545
3546
    /*
3547
     * Setup the RGBA reader.
3548
     */
3549
3550
0
    if (!TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
3551
0
    {
3552
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
3553
0
        return (0);
3554
0
    }
3555
3556
0
    if (col >= img.width || row >= img.height)
3557
0
    {
3558
0
        TIFFErrorExtR(tif, TIFFFileName(tif),
3559
0
                      "Invalid row/col passed to TIFFReadRGBATile().");
3560
0
        TIFFRGBAImageEnd(&img);
3561
0
        return (0);
3562
0
    }
3563
3564
    /*
3565
     * The TIFFRGBAImageGet() function doesn't allow us to get off the
3566
     * edge of the image, even to fill an otherwise valid tile.  So we
3567
     * figure out how much we can read, and fix up the tile buffer to
3568
     * a full tile configuration afterwards.
3569
     */
3570
3571
0
    if (row + tile_ysize > img.height)
3572
0
        read_ysize = img.height - row;
3573
0
    else
3574
0
        read_ysize = tile_ysize;
3575
3576
0
    if (col + tile_xsize > img.width)
3577
0
        read_xsize = img.width - col;
3578
0
    else
3579
0
        read_xsize = tile_xsize;
3580
3581
    /*
3582
     * Read the chunk of imagery.
3583
     */
3584
3585
0
    img.row_offset = (int)row;
3586
0
    img.col_offset = (int)col;
3587
3588
0
    ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize);
3589
3590
0
    TIFFRGBAImageEnd(&img);
3591
3592
    /*
3593
     * If our read was incomplete we will need to fix up the tile by
3594
     * shifting the data around as if a full tile of data is being returned.
3595
     *
3596
     * This is all the more complicated because the image is organized in
3597
     * bottom to top format.
3598
     */
3599
3600
0
    if (read_xsize == tile_xsize && read_ysize == tile_ysize)
3601
0
        return (ok);
3602
3603
0
    for (i_row = 0; i_row < read_ysize; i_row++)
3604
0
    {
3605
0
        memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize,
3606
0
                raster + (size_t)(read_ysize - i_row - 1) * read_xsize,
3607
0
                read_xsize * sizeof(uint32_t));
3608
0
        _TIFFmemset(
3609
0
            raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize + read_xsize,
3610
0
            0,
3611
0
            (tmsize_t)(sizeof(uint32_t) * (size_t)(tile_xsize - read_xsize)));
3612
0
    }
3613
3614
0
    for (i_row = read_ysize; i_row < tile_ysize; i_row++)
3615
0
    {
3616
0
        _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0,
3617
0
                    (tmsize_t)(sizeof(uint32_t) * (size_t)tile_xsize));
3618
0
    }
3619
3620
0
    return (ok);
3621
0
}