Coverage Report

Created: 2026-04-10 07:04

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