Coverage Report

Created: 2025-08-28 06:57

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