• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OSGeo / gdal / 12706066811

10 Jan 2025 08:38AM UTC coverage: 70.084% (-2.5%) from 72.549%
12706066811

Pull #11629

github

web-flow
Merge 9418dc48f into 0df468c56
Pull Request #11629: add uv documentation for python package

563296 of 803749 relevant lines covered (70.08%)

223434.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.56
/frmts/gtiff/libtiff/tif_predict.c
1
/*
2
 * Copyright (c) 1988-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
 * Predictor Tag Support (used by multiple codecs).
29
 */
30
#include "tif_predict.h"
31
#include "tiffiop.h"
32

33
#define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data)
34

35
static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
36
static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
37
static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
38
static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
39
static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
40
static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
41
static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
42
static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
43
static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
44
static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
45
static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
46
static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
47
static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
48
static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
49
static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc);
50
static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc);
51
static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
52
                              uint16_t s);
53
static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
54
                               uint16_t s);
55
static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
56
static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
57
                               uint16_t s);
58

59
static int PredictorSetup(TIFF *tif)
20,104✔
60
{
61
    static const char module[] = "PredictorSetup";
62

63
    TIFFPredictorState *sp = PredictorState(tif);
20,104✔
64
    TIFFDirectory *td = &tif->tif_dir;
20,104✔
65

66
    switch (sp->predictor) /* no differencing */
20,104✔
67
    {
68
        case PREDICTOR_NONE:
19,709✔
69
            return 1;
19,709✔
70
        case PREDICTOR_HORIZONTAL:
385✔
71
            if (td->td_bitspersample != 8 && td->td_bitspersample != 16 &&
385✔
72
                td->td_bitspersample != 32 && td->td_bitspersample != 64)
6✔
73
            {
74
                TIFFErrorExtR(tif, module,
×
75
                              "Horizontal differencing \"Predictor\" not "
76
                              "supported with %" PRIu16 "-bit samples",
77
                              td->td_bitspersample);
×
78
                return 0;
×
79
            }
80
            break;
385✔
81
        case PREDICTOR_FLOATINGPOINT:
9✔
82
            if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP)
9✔
83
            {
84
                TIFFErrorExtR(
×
85
                    tif, module,
86
                    "Floating point \"Predictor\" not supported with %" PRIu16
87
                    " data format",
88
                    td->td_sampleformat);
×
89
                return 0;
×
90
            }
91
            if (td->td_bitspersample != 16 && td->td_bitspersample != 24 &&
9✔
92
                td->td_bitspersample != 32 && td->td_bitspersample != 64)
9✔
93
            { /* Should 64 be allowed? */
94
                TIFFErrorExtR(
×
95
                    tif, module,
96
                    "Floating point \"Predictor\" not supported with %" PRIu16
97
                    "-bit samples",
98
                    td->td_bitspersample);
×
99
                return 0;
×
100
            }
101
            break;
9✔
102
        default:
1✔
103
            TIFFErrorExtR(tif, module, "\"Predictor\" value %d not supported",
1✔
104
                          sp->predictor);
105
            return 0;
1✔
106
    }
107
    sp->stride =
394✔
108
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
788✔
109
                                                    : 1);
394✔
110
    /*
111
     * Calculate the scanline/tile-width size in bytes.
112
     */
113
    if (isTiled(tif))
394✔
114
        sp->rowsize = TIFFTileRowSize(tif);
22✔
115
    else
116
        sp->rowsize = TIFFScanlineSize(tif);
372✔
117
    if (sp->rowsize == 0)
394✔
118
        return 0;
×
119

120
    return 1;
394✔
121
}
122

123
static int PredictorSetupDecode(TIFF *tif)
3,066✔
124
{
125
    TIFFPredictorState *sp = PredictorState(tif);
3,066✔
126
    TIFFDirectory *td = &tif->tif_dir;
3,066✔
127

128
    /* Note: when PredictorSetup() fails, the effets of setupdecode() */
129
    /* will not be "canceled" so setupdecode() might be robust to */
130
    /* be called several times. */
131
    if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
3,066✔
132
        return 0;
1✔
133

134
    if (sp->predictor == 2)
3,066✔
135
    {
136
        switch (td->td_bitspersample)
95✔
137
        {
138
            case 8:
75✔
139
                sp->decodepfunc = horAcc8;
75✔
140
                break;
75✔
141
            case 16:
17✔
142
                sp->decodepfunc = horAcc16;
17✔
143
                break;
17✔
144
            case 32:
2✔
145
                sp->decodepfunc = horAcc32;
2✔
146
                break;
2✔
147
            case 64:
1✔
148
                sp->decodepfunc = horAcc64;
1✔
149
                break;
1✔
150
        }
151
        /*
152
         * Override default decoding method with one that does the
153
         * predictor stuff.
154
         */
155
        if (tif->tif_decoderow != PredictorDecodeRow)
95✔
156
        {
157
            sp->decoderow = tif->tif_decoderow;
95✔
158
            tif->tif_decoderow = PredictorDecodeRow;
95✔
159
            sp->decodestrip = tif->tif_decodestrip;
95✔
160
            tif->tif_decodestrip = PredictorDecodeTile;
95✔
161
            sp->decodetile = tif->tif_decodetile;
95✔
162
            tif->tif_decodetile = PredictorDecodeTile;
95✔
163
        }
164

165
        /*
166
         * If the data is horizontally differenced 16-bit data that
167
         * requires byte-swapping, then it must be byte swapped before
168
         * the accumulation step.  We do this with a special-purpose
169
         * routine and override the normal post decoding logic that
170
         * the library setup when the directory was read.
171
         */
172
        if (tif->tif_flags & TIFF_SWAB)
95✔
173
        {
174
            if (sp->decodepfunc == horAcc16)
5✔
175
            {
176
                sp->decodepfunc = swabHorAcc16;
4✔
177
                tif->tif_postdecode = _TIFFNoPostDecode;
4✔
178
            }
179
            else if (sp->decodepfunc == horAcc32)
1✔
180
            {
181
                sp->decodepfunc = swabHorAcc32;
1✔
182
                tif->tif_postdecode = _TIFFNoPostDecode;
1✔
183
            }
184
            else if (sp->decodepfunc == horAcc64)
×
185
            {
186
                sp->decodepfunc = swabHorAcc64;
×
187
                tif->tif_postdecode = _TIFFNoPostDecode;
×
188
            }
189
        }
190
    }
191

192
    else if (sp->predictor == 3)
2,971✔
193
    {
194
        sp->decodepfunc = fpAcc;
7✔
195
        /*
196
         * Override default decoding method with one that does the
197
         * predictor stuff.
198
         */
199
        if (tif->tif_decoderow != PredictorDecodeRow)
7✔
200
        {
201
            sp->decoderow = tif->tif_decoderow;
7✔
202
            tif->tif_decoderow = PredictorDecodeRow;
7✔
203
            sp->decodestrip = tif->tif_decodestrip;
7✔
204
            tif->tif_decodestrip = PredictorDecodeTile;
7✔
205
            sp->decodetile = tif->tif_decodetile;
7✔
206
            tif->tif_decodetile = PredictorDecodeTile;
7✔
207
        }
208
        /*
209
         * The data should not be swapped outside of the floating
210
         * point predictor, the accumulation routine should return
211
         * bytes in the native order.
212
         */
213
        if (tif->tif_flags & TIFF_SWAB)
7✔
214
        {
215
            tif->tif_postdecode = _TIFFNoPostDecode;
2✔
216
        }
217
    }
218

219
    return 1;
3,066✔
220
}
221

222
static int PredictorSetupEncode(TIFF *tif)
17,036✔
223
{
224
    TIFFPredictorState *sp = PredictorState(tif);
17,036✔
225
    TIFFDirectory *td = &tif->tif_dir;
17,036✔
226

227
    if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
17,036✔
228
        return 0;
×
229

230
    if (sp->predictor == 2)
17,037✔
231
    {
232
        switch (td->td_bitspersample)
290✔
233
        {
234
            case 8:
275✔
235
                sp->encodepfunc = horDiff8;
275✔
236
                break;
275✔
237
            case 16:
12✔
238
                sp->encodepfunc = horDiff16;
12✔
239
                break;
12✔
240
            case 32:
2✔
241
                sp->encodepfunc = horDiff32;
2✔
242
                break;
2✔
243
            case 64:
1✔
244
                sp->encodepfunc = horDiff64;
1✔
245
                break;
1✔
246
        }
247
        /*
248
         * Override default encoding method with one that does the
249
         * predictor stuff.
250
         */
251
        if (tif->tif_encoderow != PredictorEncodeRow)
290✔
252
        {
253
            sp->encoderow = tif->tif_encoderow;
289✔
254
            tif->tif_encoderow = PredictorEncodeRow;
289✔
255
            sp->encodestrip = tif->tif_encodestrip;
289✔
256
            tif->tif_encodestrip = PredictorEncodeTile;
289✔
257
            sp->encodetile = tif->tif_encodetile;
289✔
258
            tif->tif_encodetile = PredictorEncodeTile;
289✔
259
        }
260

261
        /*
262
         * If the data is horizontally differenced 16-bit data that
263
         * requires byte-swapping, then it must be byte swapped after
264
         * the differentiation step.  We do this with a special-purpose
265
         * routine and override the normal post decoding logic that
266
         * the library setup when the directory was read.
267
         */
268
        if (tif->tif_flags & TIFF_SWAB)
290✔
269
        {
270
            if (sp->encodepfunc == horDiff16)
6✔
271
            {
272
                sp->encodepfunc = swabHorDiff16;
5✔
273
                tif->tif_postdecode = _TIFFNoPostDecode;
5✔
274
            }
275
            else if (sp->encodepfunc == horDiff32)
1✔
276
            {
277
                sp->encodepfunc = swabHorDiff32;
1✔
278
                tif->tif_postdecode = _TIFFNoPostDecode;
1✔
279
            }
280
            else if (sp->encodepfunc == horDiff64)
×
281
            {
282
                sp->encodepfunc = swabHorDiff64;
×
283
                tif->tif_postdecode = _TIFFNoPostDecode;
×
284
            }
285
        }
286
    }
287

288
    else if (sp->predictor == 3)
16,747✔
289
    {
290
        sp->encodepfunc = fpDiff;
2✔
291
        /*
292
         * Override default encoding method with one that does the
293
         * predictor stuff.
294
         */
295
        if (tif->tif_encoderow != PredictorEncodeRow)
2✔
296
        {
297
            sp->encoderow = tif->tif_encoderow;
2✔
298
            tif->tif_encoderow = PredictorEncodeRow;
2✔
299
            sp->encodestrip = tif->tif_encodestrip;
2✔
300
            tif->tif_encodestrip = PredictorEncodeTile;
2✔
301
            sp->encodetile = tif->tif_encodetile;
2✔
302
            tif->tif_encodetile = PredictorEncodeTile;
2✔
303
        }
304
        /*
305
         * The data should not be swapped outside of the floating
306
         * point predictor, the differentiation routine should return
307
         * bytes in the native order.
308
         */
309
        if (tif->tif_flags & TIFF_SWAB)
2✔
310
        {
311
            tif->tif_postdecode = _TIFFNoPostDecode;
1✔
312
        }
313
    }
314

315
    return 1;
17,037✔
316
}
317

318
#define REPEAT4(n, op)                                                         \
319
    switch (n)                                                                 \
320
    {                                                                          \
321
        default:                                                               \
322
        {                                                                      \
323
            tmsize_t i;                                                        \
324
            for (i = n - 4; i > 0; i--)                                        \
325
            {                                                                  \
326
                op;                                                            \
327
            }                                                                  \
328
        } /*-fallthrough*/                                                     \
329
        case 4:                                                                \
330
            op; /*-fallthrough*/                                               \
331
        case 3:                                                                \
332
            op; /*-fallthrough*/                                               \
333
        case 2:                                                                \
334
            op; /*-fallthrough*/                                               \
335
        case 1:                                                                \
336
            op; /*-fallthrough*/                                               \
337
        case 0:;                                                               \
338
    }
339

340
/* Remarks related to C standard compliance in all below functions : */
341
/* - to avoid any undefined behavior, we only operate on unsigned types */
342
/*   since the behavior of "overflows" is defined (wrap over) */
343
/* - when storing into the byte stream, we explicitly mask with 0xff so */
344
/*   as to make icc -check=conversions happy (not necessary by the standard) */
345

346
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
347
static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
67,005✔
348
{
349
    tmsize_t stride = PredictorState(tif)->stride;
67,005✔
350

351
    unsigned char *cp = (unsigned char *)cp0;
67,005✔
352
    if ((cc % stride) != 0)
67,005✔
353
    {
354
        TIFFErrorExtR(tif, "horAcc8", "%s", "(cc%stride)!=0");
×
355
        return 0;
×
356
    }
357

358
    if (cc > stride)
67,005✔
359
    {
360
        /*
361
         * Pipeline the most common cases.
362
         */
363
        if (stride == 3)
67,005✔
364
        {
365
            unsigned int cr = cp[0];
2,305✔
366
            unsigned int cg = cp[1];
2,305✔
367
            unsigned int cb = cp[2];
2,305✔
368
            tmsize_t i = stride;
2,305✔
369
            for (; i < cc; i += stride)
36,868✔
370
            {
371
                cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
34,563✔
372
                cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
34,563✔
373
                cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
34,563✔
374
            }
375
        }
376
        else if (stride == 4)
64,700✔
377
        {
378
            unsigned int cr = cp[0];
65✔
379
            unsigned int cg = cp[1];
65✔
380
            unsigned int cb = cp[2];
65✔
381
            unsigned int ca = cp[3];
65✔
382
            tmsize_t i = stride;
65✔
383
            for (; i < cc; i += stride)
2,052✔
384
            {
385
                cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
1,987✔
386
                cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
1,987✔
387
                cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
1,987✔
388
                cp[i + 3] = (unsigned char)((ca += cp[i + 3]) & 0xff);
1,987✔
389
            }
390
        }
391
        else
392
        {
393
            cc -= stride;
64,635✔
394
            do
395
            {
396
                REPEAT4(stride,
16,439,600✔
397
                        cp[stride] = (unsigned char)((cp[stride] + *cp) & 0xff);
398
                        cp++)
399
                cc -= stride;
16,439,600✔
400
            } while (cc > 0);
16,439,600✔
401
        }
402
    }
403
    return 1;
67,005✔
404
}
405

406
static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
245✔
407
{
408
    uint16_t *wp = (uint16_t *)cp0;
245✔
409
    tmsize_t wc = cc / 2;
245✔
410

411
    TIFFSwabArrayOfShort(wp, wc);
245✔
412
    return horAcc16(tif, cp0, cc);
245✔
413
}
414

415
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
416
static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
1,736✔
417
{
418
    tmsize_t stride = PredictorState(tif)->stride;
1,736✔
419
    uint16_t *wp = (uint16_t *)cp0;
1,736✔
420
    tmsize_t wc = cc / 2;
1,736✔
421

422
    if ((cc % (2 * stride)) != 0)
1,736✔
423
    {
424
        TIFFErrorExtR(tif, "horAcc16", "%s", "cc%(2*stride))!=0");
×
425
        return 0;
×
426
    }
427

428
    if (wc > stride)
1,736✔
429
    {
430
        wc -= stride;
1,736✔
431
        do
432
        {
433
            REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] +
514,708✔
434
                                                     (unsigned int)wp[0]) &
435
                                                    0xffff);
436
                    wp++)
437
            wc -= stride;
514,708✔
438
        } while (wc > 0);
514,708✔
439
    }
440
    return 1;
1,736✔
441
}
442

443
static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
1✔
444
{
445
    uint32_t *wp = (uint32_t *)cp0;
1✔
446
    tmsize_t wc = cc / 4;
1✔
447

448
    TIFFSwabArrayOfLong(wp, wc);
1✔
449
    return horAcc32(tif, cp0, cc);
1✔
450
}
451

452
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
453
static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
2✔
454
{
455
    tmsize_t stride = PredictorState(tif)->stride;
2✔
456
    uint32_t *wp = (uint32_t *)cp0;
2✔
457
    tmsize_t wc = cc / 4;
2✔
458

459
    if ((cc % (4 * stride)) != 0)
2✔
460
    {
461
        TIFFErrorExtR(tif, "horAcc32", "%s", "cc%(4*stride))!=0");
×
462
        return 0;
×
463
    }
464

465
    if (wc > stride)
2✔
466
    {
467
        wc -= stride;
2✔
468
        do
469
        {
470
            REPEAT4(stride, wp[stride] += wp[0]; wp++)
10✔
471
            wc -= stride;
10✔
472
        } while (wc > 0);
10✔
473
    }
474
    return 1;
2✔
475
}
476

477
static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
×
478
{
479
    uint64_t *wp = (uint64_t *)cp0;
×
480
    tmsize_t wc = cc / 8;
×
481

482
    TIFFSwabArrayOfLong8(wp, wc);
×
483
    return horAcc64(tif, cp0, cc);
×
484
}
485

486
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
487
static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
1✔
488
{
489
    tmsize_t stride = PredictorState(tif)->stride;
1✔
490
    uint64_t *wp = (uint64_t *)cp0;
1✔
491
    tmsize_t wc = cc / 8;
1✔
492

493
    if ((cc % (8 * stride)) != 0)
1✔
494
    {
495
        TIFFErrorExtR(tif, "horAcc64", "%s", "cc%(8*stride))!=0");
×
496
        return 0;
×
497
    }
498

499
    if (wc > stride)
1✔
500
    {
501
        wc -= stride;
1✔
502
        do
503
        {
504
            REPEAT4(stride, wp[stride] += wp[0]; wp++)
1✔
505
            wc -= stride;
1✔
506
        } while (wc > 0);
1✔
507
    }
508
    return 1;
1✔
509
}
510

511
/*
512
 * Floating point predictor accumulation routine.
513
 */
514
static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc)
1,529✔
515
{
516
    tmsize_t stride = PredictorState(tif)->stride;
1,529✔
517
    uint32_t bps = tif->tif_dir.td_bitspersample / 8;
1,529✔
518
    tmsize_t wc = cc / bps;
1,529✔
519
    tmsize_t count = cc;
1,529✔
520
    uint8_t *cp = (uint8_t *)cp0;
1,529✔
521
    uint8_t *tmp;
522

523
    if (cc % (bps * stride) != 0)
1,529✔
524
    {
525
        TIFFErrorExtR(tif, "fpAcc", "%s", "cc%(bps*stride))!=0");
×
526
        return 0;
×
527
    }
528

529
    tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
1,529✔
530
    if (!tmp)
1,529✔
531
        return 0;
×
532

533
    while (count > stride)
1,193,630✔
534
    {
535
        REPEAT4(stride,
1,192,100✔
536
                cp[stride] = (unsigned char)((cp[stride] + cp[0]) & 0xff);
537
                cp++)
538
        count -= stride;
1,192,100✔
539
    }
540

541
    _TIFFmemcpy(tmp, cp0, cc);
1,529✔
542
    cp = (uint8_t *)cp0;
1,529✔
543
    for (count = 0; count < wc; count++)
299,933✔
544
    {
545
        uint32_t byte;
546
        for (byte = 0; byte < bps; byte++)
1,492,040✔
547
        {
548
#if WORDS_BIGENDIAN
549
            cp[bps * count + byte] = tmp[byte * wc + count];
550
#else
551
            cp[bps * count + byte] = tmp[(bps - byte - 1) * wc + count];
1,193,630✔
552
#endif
553
        }
554
    }
555
    _TIFFfreeExt(tif, tmp);
1,529✔
556
    return 1;
1,529✔
557
}
558

559
/*
560
 * Decode a scanline and apply the predictor routine.
561
 */
562
static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
×
563
                              uint16_t s)
564
{
565
    TIFFPredictorState *sp = PredictorState(tif);
×
566

567
    assert(sp != NULL);
×
568
    assert(sp->decoderow != NULL);
×
569
    assert(sp->decodepfunc != NULL);
×
570

571
    if ((*sp->decoderow)(tif, op0, occ0, s))
×
572
    {
573
        return (*sp->decodepfunc)(tif, op0, occ0);
×
574
    }
575
    else
576
        return 0;
×
577
}
578

579
/*
580
 * Decode a tile/strip and apply the predictor routine.
581
 * Note that horizontal differencing must be done on a
582
 * row-by-row basis.  The width of a "row" has already
583
 * been calculated at pre-decode time according to the
584
 * strip/tile dimensions.
585
 */
586
static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
484✔
587
                               uint16_t s)
588
{
589
    TIFFPredictorState *sp = PredictorState(tif);
484✔
590

591
    assert(sp != NULL);
484✔
592
    assert(sp->decodetile != NULL);
484✔
593

594
    if ((*sp->decodetile)(tif, op0, occ0, s))
484✔
595
    {
596
        tmsize_t rowsize = sp->rowsize;
484✔
597
        assert(rowsize > 0);
484✔
598
        if ((occ0 % rowsize) != 0)
484✔
599
        {
600
            TIFFErrorExtR(tif, "PredictorDecodeTile", "%s",
×
601
                          "occ0%rowsize != 0");
602
            return 0;
×
603
        }
604
        assert(sp->decodepfunc != NULL);
484✔
605
        while (occ0 > 0)
70,757✔
606
        {
607
            if (!(*sp->decodepfunc)(tif, op0, rowsize))
70,273✔
608
                return 0;
×
609
            occ0 -= rowsize;
70,273✔
610
            op0 += rowsize;
70,273✔
611
        }
612
        return 1;
484✔
613
    }
614
    else
615
        return 0;
×
616
}
617

618
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
619
static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
67,472✔
620
{
621
    TIFFPredictorState *sp = PredictorState(tif);
67,472✔
622
    tmsize_t stride = sp->stride;
67,472✔
623
    unsigned char *cp = (unsigned char *)cp0;
67,472✔
624

625
    if ((cc % stride) != 0)
67,472✔
626
    {
627
        TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%stride)!=0");
×
628
        return 0;
×
629
    }
630

631
    if (cc > stride)
67,472✔
632
    {
633
        cc -= stride;
67,436✔
634
        /*
635
         * Pipeline the most common cases.
636
         */
637
        if (stride == 3)
67,436✔
638
        {
639
            unsigned int r1, g1, b1;
640
            unsigned int r2 = cp[0];
897✔
641
            unsigned int g2 = cp[1];
897✔
642
            unsigned int b2 = cp[2];
897✔
643
            do
644
            {
645
                r1 = cp[3];
267,395✔
646
                cp[3] = (unsigned char)((r1 - r2) & 0xff);
267,395✔
647
                r2 = r1;
267,395✔
648
                g1 = cp[4];
267,395✔
649
                cp[4] = (unsigned char)((g1 - g2) & 0xff);
267,395✔
650
                g2 = g1;
267,395✔
651
                b1 = cp[5];
267,395✔
652
                cp[5] = (unsigned char)((b1 - b2) & 0xff);
267,395✔
653
                b2 = b1;
267,395✔
654
                cp += 3;
267,395✔
655
            } while ((cc -= 3) > 0);
267,395✔
656
        }
657
        else if (stride == 4)
66,539✔
658
        {
659
            unsigned int r1, g1, b1, a1;
660
            unsigned int r2 = cp[0];
33✔
661
            unsigned int g2 = cp[1];
33✔
662
            unsigned int b2 = cp[2];
33✔
663
            unsigned int a2 = cp[3];
33✔
664
            do
665
            {
666
                r1 = cp[4];
995✔
667
                cp[4] = (unsigned char)((r1 - r2) & 0xff);
995✔
668
                r2 = r1;
995✔
669
                g1 = cp[5];
995✔
670
                cp[5] = (unsigned char)((g1 - g2) & 0xff);
995✔
671
                g2 = g1;
995✔
672
                b1 = cp[6];
995✔
673
                cp[6] = (unsigned char)((b1 - b2) & 0xff);
995✔
674
                b2 = b1;
995✔
675
                a1 = cp[7];
995✔
676
                cp[7] = (unsigned char)((a1 - a2) & 0xff);
995✔
677
                a2 = a1;
995✔
678
                cp += 4;
995✔
679
            } while ((cc -= 4) > 0);
995✔
680
        }
681
        else
682
        {
683
            cp += cc - 1;
66,506✔
684
            do
685
            {
686
                REPEAT4(stride,
13,242,500✔
687
                        cp[stride] =
688
                            (unsigned char)((cp[stride] - cp[0]) & 0xff);
689
                        cp--)
690
            } while ((cc -= stride) > 0);
13,242,500✔
691
        }
692
    }
693
    return 1;
67,472✔
694
}
695

696
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
697
static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
1,014✔
698
{
699
    TIFFPredictorState *sp = PredictorState(tif);
1,014✔
700
    tmsize_t stride = sp->stride;
1,014✔
701
    uint16_t *wp = (uint16_t *)cp0;
1,014✔
702
    tmsize_t wc = cc / 2;
1,014✔
703

704
    if ((cc % (2 * stride)) != 0)
1,014✔
705
    {
706
        TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%(2*stride))!=0");
×
707
        return 0;
×
708
    }
709

710
    if (wc > stride)
1,014✔
711
    {
712
        wc -= stride;
1,014✔
713
        wp += wc - 1;
1,014✔
714
        do
715
        {
716
            REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] -
126,596✔
717
                                                     (unsigned int)wp[0]) &
718
                                                    0xffff);
719
                    wp--)
720
            wc -= stride;
126,596✔
721
        } while (wc > 0);
126,596✔
722
    }
723
    return 1;
1,014✔
724
}
725

726
static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
379✔
727
{
728
    uint16_t *wp = (uint16_t *)cp0;
379✔
729
    tmsize_t wc = cc / 2;
379✔
730

731
    if (!horDiff16(tif, cp0, cc))
379✔
732
        return 0;
×
733

734
    TIFFSwabArrayOfShort(wp, wc);
379✔
735
    return 1;
379✔
736
}
737

738
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
739
static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
2✔
740
{
741
    TIFFPredictorState *sp = PredictorState(tif);
2✔
742
    tmsize_t stride = sp->stride;
2✔
743
    uint32_t *wp = (uint32_t *)cp0;
2✔
744
    tmsize_t wc = cc / 4;
2✔
745

746
    if ((cc % (4 * stride)) != 0)
2✔
747
    {
748
        TIFFErrorExtR(tif, "horDiff32", "%s", "(cc%(4*stride))!=0");
×
749
        return 0;
×
750
    }
751

752
    if (wc > stride)
2✔
753
    {
754
        wc -= stride;
2✔
755
        wp += wc - 1;
2✔
756
        do
757
        {
758
            REPEAT4(stride, wp[stride] -= wp[0]; wp--)
10✔
759
            wc -= stride;
10✔
760
        } while (wc > 0);
10✔
761
    }
762
    return 1;
2✔
763
}
764

765
static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
1✔
766
{
767
    uint32_t *wp = (uint32_t *)cp0;
1✔
768
    tmsize_t wc = cc / 4;
1✔
769

770
    if (!horDiff32(tif, cp0, cc))
1✔
771
        return 0;
×
772

773
    TIFFSwabArrayOfLong(wp, wc);
1✔
774
    return 1;
1✔
775
}
776

777
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
778
static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
1✔
779
{
780
    TIFFPredictorState *sp = PredictorState(tif);
1✔
781
    tmsize_t stride = sp->stride;
1✔
782
    uint64_t *wp = (uint64_t *)cp0;
1✔
783
    tmsize_t wc = cc / 8;
1✔
784

785
    if ((cc % (8 * stride)) != 0)
1✔
786
    {
787
        TIFFErrorExtR(tif, "horDiff64", "%s", "(cc%(8*stride))!=0");
×
788
        return 0;
×
789
    }
790

791
    if (wc > stride)
1✔
792
    {
793
        wc -= stride;
1✔
794
        wp += wc - 1;
1✔
795
        do
796
        {
797
            REPEAT4(stride, wp[stride] -= wp[0]; wp--)
1✔
798
            wc -= stride;
1✔
799
        } while (wc > 0);
1✔
800
    }
801
    return 1;
1✔
802
}
803

804
static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
×
805
{
806
    uint64_t *wp = (uint64_t *)cp0;
×
807
    tmsize_t wc = cc / 8;
×
808

809
    if (!horDiff64(tif, cp0, cc))
×
810
        return 0;
×
811

812
    TIFFSwabArrayOfLong8(wp, wc);
×
813
    return 1;
×
814
}
815

816
/*
817
 * Floating point predictor differencing routine.
818
 */
819
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
820
static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc)
21✔
821
{
822
    tmsize_t stride = PredictorState(tif)->stride;
21✔
823
    uint32_t bps = tif->tif_dir.td_bitspersample / 8;
21✔
824
    tmsize_t wc = cc / bps;
21✔
825
    tmsize_t count;
826
    uint8_t *cp = (uint8_t *)cp0;
21✔
827
    uint8_t *tmp;
828

829
    if ((cc % (bps * stride)) != 0)
21✔
830
    {
831
        TIFFErrorExtR(tif, "fpDiff", "%s", "(cc%(bps*stride))!=0");
×
832
        return 0;
×
833
    }
834

835
    tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
21✔
836
    if (!tmp)
21✔
837
        return 0;
×
838

839
    _TIFFmemcpy(tmp, cp0, cc);
21✔
840
    for (count = 0; count < wc; count++)
425✔
841
    {
842
        uint32_t byte;
843
        for (byte = 0; byte < bps; byte++)
2,036✔
844
        {
845
#if WORDS_BIGENDIAN
846
            cp[byte * wc + count] = tmp[bps * count + byte];
847
#else
848
            cp[(bps - byte - 1) * wc + count] = tmp[bps * count + byte];
1,632✔
849
#endif
850
        }
851
    }
852
    _TIFFfreeExt(tif, tmp);
21✔
853

854
    cp = (uint8_t *)cp0;
21✔
855
    cp += cc - stride - 1;
21✔
856
    for (count = cc; count > stride; count -= stride)
1,632✔
857
        REPEAT4(stride,
1,611✔
858
                cp[stride] = (unsigned char)((cp[stride] - cp[0]) & 0xff);
859
                cp--)
860
    return 1;
21✔
861
}
862

863
static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
×
864
{
865
    TIFFPredictorState *sp = PredictorState(tif);
×
866

867
    assert(sp != NULL);
×
868
    assert(sp->encodepfunc != NULL);
×
869
    assert(sp->encoderow != NULL);
×
870

871
    /* XXX horizontal differencing alters user's data XXX */
872
    if (!(*sp->encodepfunc)(tif, bp, cc))
×
873
        return 0;
×
874
    return (*sp->encoderow)(tif, bp, cc, s);
×
875
}
876

877
static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
311✔
878
                               uint16_t s)
879
{
880
    static const char module[] = "PredictorEncodeTile";
881
    TIFFPredictorState *sp = PredictorState(tif);
311✔
882
    uint8_t *working_copy;
883
    tmsize_t cc = cc0, rowsize;
311✔
884
    unsigned char *bp;
885
    int result_code;
886

887
    assert(sp != NULL);
311✔
888
    assert(sp->encodepfunc != NULL);
311✔
889
    assert(sp->encodetile != NULL);
311✔
890

891
    /*
892
     * Do predictor manipulation in a working buffer to avoid altering
893
     * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
894
     */
895
    working_copy = (uint8_t *)_TIFFmallocExt(tif, cc0);
311✔
896
    if (working_copy == NULL)
311✔
897
    {
898
        TIFFErrorExtR(tif, module,
×
899
                      "Out of memory allocating %" PRId64 " byte temp buffer.",
900
                      (int64_t)cc0);
901
        return 0;
×
902
    }
903
    memcpy(working_copy, bp0, cc0);
311✔
904
    bp = working_copy;
311✔
905

906
    rowsize = sp->rowsize;
311✔
907
    assert(rowsize > 0);
311✔
908
    if ((cc0 % rowsize) != 0)
311✔
909
    {
910
        TIFFErrorExtR(tif, "PredictorEncodeTile", "%s", "(cc0%rowsize)!=0");
×
911
        _TIFFfreeExt(tif, working_copy);
×
912
        return 0;
×
913
    }
914
    while (cc > 0)
68,817✔
915
    {
916
        (*sp->encodepfunc)(tif, bp, rowsize);
68,506✔
917
        cc -= rowsize;
68,506✔
918
        bp += rowsize;
68,506✔
919
    }
920
    result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
311✔
921

922
    _TIFFfreeExt(tif, working_copy);
311✔
923

924
    return result_code;
311✔
925
}
926

927
#define FIELD_PREDICTOR (FIELD_CODEC + 0) /* XXX */
928

929
static const TIFFField predictFields[] = {
930
    {TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16,
931
     TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL},
932
};
933

934
static int PredictorVSetField(TIFF *tif, uint32_t tag, va_list ap)
170,518✔
935
{
936
    TIFFPredictorState *sp = PredictorState(tif);
170,518✔
937

938
    assert(sp != NULL);
170,518✔
939
    assert(sp->vsetparent != NULL);
170,518✔
940

941
    switch (tag)
170,518✔
942
    {
943
        case TIFFTAG_PREDICTOR:
5,752✔
944
            sp->predictor = (uint16_t)va_arg(ap, uint16_vap);
5,752✔
945
            TIFFSetFieldBit(tif, FIELD_PREDICTOR);
5,752✔
946
            break;
5,752✔
947
        default:
164,766✔
948
            return (*sp->vsetparent)(tif, tag, ap);
164,766✔
949
    }
950
    tif->tif_flags |= TIFF_DIRTYDIRECT;
5,752✔
951
    return 1;
5,752✔
952
}
953

954
static int PredictorVGetField(TIFF *tif, uint32_t tag, va_list ap)
342,194✔
955
{
956
    TIFFPredictorState *sp = PredictorState(tif);
342,194✔
957

958
    assert(sp != NULL);
342,194✔
959
    assert(sp->vgetparent != NULL);
342,194✔
960

961
    switch (tag)
342,194✔
962
    {
963
        case TIFFTAG_PREDICTOR:
20,877✔
964
            *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor;
20,877✔
965
            break;
20,877✔
966
        default:
321,317✔
967
            return (*sp->vgetparent)(tif, tag, ap);
321,317✔
968
    }
969
    return 1;
20,877✔
970
}
971

972
static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags)
×
973
{
974
    TIFFPredictorState *sp = PredictorState(tif);
×
975

976
    (void)flags;
977
    if (TIFFFieldSet(tif, FIELD_PREDICTOR))
×
978
    {
979
        fprintf(fd, "  Predictor: ");
×
980
        switch (sp->predictor)
×
981
        {
982
            case 1:
×
983
                fprintf(fd, "none ");
×
984
                break;
×
985
            case 2:
×
986
                fprintf(fd, "horizontal differencing ");
×
987
                break;
×
988
            case 3:
×
989
                fprintf(fd, "floating point predictor ");
×
990
                break;
×
991
        }
992
        fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
×
993
    }
994
    if (sp->printdir)
×
995
        (*sp->printdir)(tif, fd, flags);
×
996
}
×
997

998
int TIFFPredictorInit(TIFF *tif)
26,559✔
999
{
1000
    TIFFPredictorState *sp = PredictorState(tif);
26,559✔
1001

1002
    assert(sp != 0);
26,559✔
1003

1004
    /*
1005
     * Merge codec-specific tag information.
1006
     */
1007
    if (!_TIFFMergeFields(tif, predictFields, TIFFArrayCount(predictFields)))
26,559✔
1008
    {
1009
        TIFFErrorExtR(tif, "TIFFPredictorInit",
×
1010
                      "Merging Predictor codec-specific tags failed");
1011
        return 0;
×
1012
    }
1013

1014
    /*
1015
     * Override parent get/set field methods.
1016
     */
1017
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
26,557✔
1018
    tif->tif_tagmethods.vgetfield =
26,557✔
1019
        PredictorVGetField; /* hook for predictor tag */
1020
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
26,557✔
1021
    tif->tif_tagmethods.vsetfield =
26,557✔
1022
        PredictorVSetField; /* hook for predictor tag */
1023
    sp->printdir = tif->tif_tagmethods.printdir;
26,557✔
1024
    tif->tif_tagmethods.printdir =
26,557✔
1025
        PredictorPrintDir; /* hook for predictor tag */
1026

1027
    sp->setupdecode = tif->tif_setupdecode;
26,557✔
1028
    tif->tif_setupdecode = PredictorSetupDecode;
26,557✔
1029
    sp->setupencode = tif->tif_setupencode;
26,557✔
1030
    tif->tif_setupencode = PredictorSetupEncode;
26,557✔
1031

1032
    sp->predictor = 1;      /* default value */
26,557✔
1033
    sp->encodepfunc = NULL; /* no predictor routine */
26,557✔
1034
    sp->decodepfunc = NULL; /* no predictor routine */
26,557✔
1035
    return 1;
26,557✔
1036
}
1037

1038
int TIFFPredictorCleanup(TIFF *tif)
26,555✔
1039
{
1040
    TIFFPredictorState *sp = PredictorState(tif);
26,555✔
1041

1042
    assert(sp != 0);
26,555✔
1043

1044
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
26,555✔
1045
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
26,555✔
1046
    tif->tif_tagmethods.printdir = sp->printdir;
26,555✔
1047
    tif->tif_setupdecode = sp->setupdecode;
26,555✔
1048
    tif->tif_setupencode = sp->setupencode;
26,555✔
1049

1050
    return 1;
26,555✔
1051
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc