Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/tiff/libtiff/tif_predict.c
Line
Count
Source (jump to first uncovered line)
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 "tiffiop.h"
31
#include "tif_predict.h"
32
33
12.3k
#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 swabHorAcc16(TIFF* tif, uint8_t* cp0, tmsize_t cc);
39
static int swabHorAcc32(TIFF* tif, uint8_t* cp0, tmsize_t cc);
40
static int horDiff8(TIFF* tif, uint8_t* cp0, tmsize_t cc);
41
static int horDiff16(TIFF* tif, uint8_t* cp0, tmsize_t cc);
42
static int horDiff32(TIFF* tif, uint8_t* cp0, tmsize_t cc);
43
static int swabHorDiff16(TIFF* tif, uint8_t* cp0, tmsize_t cc);
44
static int swabHorDiff32(TIFF* tif, uint8_t* cp0, tmsize_t cc);
45
static int fpAcc(TIFF* tif, uint8_t* cp0, tmsize_t cc);
46
static int fpDiff(TIFF* tif, uint8_t* cp0, tmsize_t cc);
47
static int PredictorDecodeRow(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s);
48
static int PredictorDecodeTile(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s);
49
static int PredictorEncodeRow(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s);
50
static int PredictorEncodeTile(TIFF* tif, uint8_t* bp0, tmsize_t cc0, uint16_t s);
51
52
static int
53
PredictorSetup(TIFF* tif)
54
1.08k
{
55
1.08k
  static const char module[] = "PredictorSetup";
56
57
1.08k
  TIFFPredictorState* sp = PredictorState(tif);
58
1.08k
  TIFFDirectory* td = &tif->tif_dir;
59
60
1.08k
  switch (sp->predictor)    /* no differencing */
61
1.08k
  {
62
1.08k
    case PREDICTOR_NONE:
63
1.08k
      return 1;
64
0
    case PREDICTOR_HORIZONTAL:
65
0
      if (td->td_bitspersample != 8
66
0
          && td->td_bitspersample != 16
67
0
          && td->td_bitspersample != 32) {
68
0
        TIFFErrorExt(tif->tif_clientdata, module,
69
0
            "Horizontal differencing \"Predictor\" not supported with %"PRIu16"-bit samples",
70
0
            td->td_bitspersample);
71
0
        return 0;
72
0
      }
73
0
      break;
74
0
    case PREDICTOR_FLOATINGPOINT:
75
0
      if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
76
0
        TIFFErrorExt(tif->tif_clientdata, module,
77
0
            "Floating point \"Predictor\" not supported with %"PRIu16" data format",
78
0
            td->td_sampleformat);
79
0
        return 0;
80
0
      }
81
0
                        if (td->td_bitspersample != 16
82
0
                            && td->td_bitspersample != 24
83
0
                            && td->td_bitspersample != 32
84
0
                            && td->td_bitspersample != 64) { /* Should 64 be allowed? */
85
0
                                TIFFErrorExt(tif->tif_clientdata, module,
86
0
                                             "Floating point \"Predictor\" not supported with %"PRIu16"-bit samples",
87
0
                                             td->td_bitspersample);
88
0
        return 0;
89
0
                            }
90
0
      break;
91
0
    default:
92
0
      TIFFErrorExt(tif->tif_clientdata, module,
93
0
          "\"Predictor\" value %d not supported",
94
0
          sp->predictor);
95
0
      return 0;
96
1.08k
  }
97
0
  sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
98
0
      td->td_samplesperpixel : 1);
99
  /*
100
   * Calculate the scanline/tile-width size in bytes.
101
   */
102
0
  if (isTiled(tif))
103
0
    sp->rowsize = TIFFTileRowSize(tif);
104
0
  else
105
0
    sp->rowsize = TIFFScanlineSize(tif);
106
0
  if (sp->rowsize == 0)
107
0
    return 0;
108
109
0
  return 1;
110
0
}
111
112
static int
113
PredictorSetupDecode(TIFF* tif)
114
0
{
115
0
  TIFFPredictorState* sp = PredictorState(tif);
116
0
  TIFFDirectory* td = &tif->tif_dir;
117
118
  /* Note: when PredictorSetup() fails, the effets of setupdecode() */
119
  /* will not be "canceled" so setupdecode() might be robust to */
120
  /* be called several times. */
121
0
  if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
122
0
    return 0;
123
124
0
  if (sp->predictor == 2) {
125
0
    switch (td->td_bitspersample) {
126
0
      case 8:  sp->decodepfunc = horAcc8; break;
127
0
      case 16: sp->decodepfunc = horAcc16; break;
128
0
      case 32: sp->decodepfunc = horAcc32; break;
129
0
    }
130
    /*
131
     * Override default decoding method with one that does the
132
     * predictor stuff.
133
     */
134
0
                if( tif->tif_decoderow != PredictorDecodeRow )
135
0
                {
136
0
                    sp->decoderow = tif->tif_decoderow;
137
0
                    tif->tif_decoderow = PredictorDecodeRow;
138
0
                    sp->decodestrip = tif->tif_decodestrip;
139
0
                    tif->tif_decodestrip = PredictorDecodeTile;
140
0
                    sp->decodetile = tif->tif_decodetile;
141
0
                    tif->tif_decodetile = PredictorDecodeTile;
142
0
                }
143
144
    /*
145
     * If the data is horizontally differenced 16-bit data that
146
     * requires byte-swapping, then it must be byte swapped before
147
     * the accumulation step.  We do this with a special-purpose
148
     * routine and override the normal post decoding logic that
149
     * the library setup when the directory was read.
150
     */
151
0
    if (tif->tif_flags & TIFF_SWAB) {
152
0
      if (sp->decodepfunc == horAcc16) {
153
0
        sp->decodepfunc = swabHorAcc16;
154
0
        tif->tif_postdecode = _TIFFNoPostDecode;
155
0
            } else if (sp->decodepfunc == horAcc32) {
156
0
        sp->decodepfunc = swabHorAcc32;
157
0
        tif->tif_postdecode = _TIFFNoPostDecode;
158
0
            }
159
0
    }
160
0
  }
161
162
0
  else if (sp->predictor == 3) {
163
0
    sp->decodepfunc = fpAcc;
164
    /*
165
     * Override default decoding method with one that does the
166
     * predictor stuff.
167
     */
168
0
                if( tif->tif_decoderow != PredictorDecodeRow )
169
0
                {
170
0
                    sp->decoderow = tif->tif_decoderow;
171
0
                    tif->tif_decoderow = PredictorDecodeRow;
172
0
                    sp->decodestrip = tif->tif_decodestrip;
173
0
                    tif->tif_decodestrip = PredictorDecodeTile;
174
0
                    sp->decodetile = tif->tif_decodetile;
175
0
                    tif->tif_decodetile = PredictorDecodeTile;
176
0
                }
177
    /*
178
     * The data should not be swapped outside of the floating
179
     * point predictor, the accumulation routine should return
180
     * byres in the native order.
181
     */
182
0
    if (tif->tif_flags & TIFF_SWAB) {
183
0
      tif->tif_postdecode = _TIFFNoPostDecode;
184
0
    }
185
    /*
186
     * Allocate buffer to keep the decoded bytes before
187
     * rearranging in the right order
188
     */
189
0
  }
190
191
0
  return 1;
192
0
}
193
194
static int
195
PredictorSetupEncode(TIFF* tif)
196
1.08k
{
197
1.08k
  TIFFPredictorState* sp = PredictorState(tif);
198
1.08k
  TIFFDirectory* td = &tif->tif_dir;
199
200
1.08k
  if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
201
0
    return 0;
202
203
1.08k
  if (sp->predictor == 2) {
204
0
    switch (td->td_bitspersample) {
205
0
      case 8:  sp->encodepfunc = horDiff8; break;
206
0
      case 16: sp->encodepfunc = horDiff16; break;
207
0
      case 32: sp->encodepfunc = horDiff32; break;
208
0
    }
209
    /*
210
     * Override default encoding method with one that does the
211
     * predictor stuff.
212
     */
213
0
                if( tif->tif_encoderow != PredictorEncodeRow )
214
0
                {
215
0
                    sp->encoderow = tif->tif_encoderow;
216
0
                    tif->tif_encoderow = PredictorEncodeRow;
217
0
                    sp->encodestrip = tif->tif_encodestrip;
218
0
                    tif->tif_encodestrip = PredictorEncodeTile;
219
0
                    sp->encodetile = tif->tif_encodetile;
220
0
                    tif->tif_encodetile = PredictorEncodeTile;
221
0
                }
222
223
                /*
224
                 * If the data is horizontally differenced 16-bit data that
225
                 * requires byte-swapping, then it must be byte swapped after
226
                 * the differentiation step.  We do this with a special-purpose
227
                 * routine and override the normal post decoding logic that
228
                 * the library setup when the directory was read.
229
                 */
230
0
                if (tif->tif_flags & TIFF_SWAB) {
231
0
                    if (sp->encodepfunc == horDiff16) {
232
0
                            sp->encodepfunc = swabHorDiff16;
233
0
                            tif->tif_postdecode = _TIFFNoPostDecode;
234
0
                    } else if (sp->encodepfunc == horDiff32) {
235
0
                            sp->encodepfunc = swabHorDiff32;
236
0
                            tif->tif_postdecode = _TIFFNoPostDecode;
237
0
                    }
238
0
                }
239
0
        }
240
241
1.08k
  else if (sp->predictor == 3) {
242
0
    sp->encodepfunc = fpDiff;
243
    /*
244
     * Override default encoding method with one that does the
245
     * predictor stuff.
246
     */
247
0
                if( tif->tif_encoderow != PredictorEncodeRow )
248
0
                {
249
0
                    sp->encoderow = tif->tif_encoderow;
250
0
                    tif->tif_encoderow = PredictorEncodeRow;
251
0
                    sp->encodestrip = tif->tif_encodestrip;
252
0
                    tif->tif_encodestrip = PredictorEncodeTile;
253
0
                    sp->encodetile = tif->tif_encodetile;
254
0
                    tif->tif_encodetile = PredictorEncodeTile;
255
0
                }
256
0
  }
257
258
1.08k
  return 1;
259
1.08k
}
260
261
#define REPEAT4(n, op)    \
262
0
    switch (n) {   \
263
0
    default: { \
264
0
        tmsize_t i; for (i = n-4; i > 0; i--) { op; } }  /*-fallthrough*/  \
265
0
    case 4:  op; /*-fallthrough*/ \
266
0
    case 3:  op; /*-fallthrough*/ \
267
0
    case 2:  op; /*-fallthrough*/ \
268
0
    case 1:  op; /*-fallthrough*/ \
269
0
    case 0:  ;      \
270
0
    }
271
272
/* Remarks related to C standard compliance in all below functions : */
273
/* - to avoid any undefined behavior, we only operate on unsigned types */
274
/*   since the behavior of "overflows" is defined (wrap over) */
275
/* - when storing into the byte stream, we explicitly mask with 0xff so */
276
/*   as to make icc -check=conversions happy (not necessary by the standard) */
277
278
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
279
static int
280
horAcc8(TIFF* tif, uint8_t* cp0, tmsize_t cc)
281
0
{
282
0
  tmsize_t stride = PredictorState(tif)->stride;
283
284
0
  unsigned char* cp = (unsigned char*) cp0;
285
0
    if((cc%stride)!=0)
286
0
    {
287
0
        TIFFErrorExt(tif->tif_clientdata, "horAcc8",
288
0
                     "%s", "(cc%stride)!=0");
289
0
        return 0;
290
0
    }
291
292
0
  if (cc > stride) {
293
    /*
294
     * Pipeline the most common cases.
295
     */
296
0
    if (stride == 3)  {
297
0
      unsigned int cr = cp[0];
298
0
      unsigned int cg = cp[1];
299
0
      unsigned int cb = cp[2];
300
0
      cc -= 3;
301
0
      cp += 3;
302
0
      while (cc>0) {
303
0
        cp[0] = (unsigned char) ((cr += cp[0]) & 0xff);
304
0
        cp[1] = (unsigned char) ((cg += cp[1]) & 0xff);
305
0
        cp[2] = (unsigned char) ((cb += cp[2]) & 0xff);
306
0
        cc -= 3;
307
0
        cp += 3;
308
0
      }
309
0
    } else if (stride == 4)  {
310
0
      unsigned int cr = cp[0];
311
0
      unsigned int cg = cp[1];
312
0
      unsigned int cb = cp[2];
313
0
      unsigned int ca = cp[3];
314
0
      cc -= 4;
315
0
      cp += 4;
316
0
      while (cc>0) {
317
0
        cp[0] = (unsigned char) ((cr += cp[0]) & 0xff);
318
0
        cp[1] = (unsigned char) ((cg += cp[1]) & 0xff);
319
0
        cp[2] = (unsigned char) ((cb += cp[2]) & 0xff);
320
0
        cp[3] = (unsigned char) ((ca += cp[3]) & 0xff);
321
0
        cc -= 4;
322
0
        cp += 4;
323
0
      }
324
0
    } else  {
325
0
      cc -= stride;
326
0
      do {
327
0
        REPEAT4(stride, cp[stride] =
328
0
          (unsigned char) ((cp[stride] + *cp) & 0xff); cp++)
329
0
        cc -= stride;
330
0
      } while (cc>0);
331
0
    }
332
0
  }
333
0
  return 1;
334
0
}
335
336
static int
337
swabHorAcc16(TIFF* tif, uint8_t* cp0, tmsize_t cc)
338
0
{
339
0
  uint16_t* wp = (uint16_t*) cp0;
340
0
  tmsize_t wc = cc / 2;
341
342
0
        TIFFSwabArrayOfShort(wp, wc);
343
0
        return horAcc16(tif, cp0, cc);
344
0
}
345
346
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
347
static int
348
horAcc16(TIFF* tif, uint8_t* cp0, tmsize_t cc)
349
0
{
350
0
  tmsize_t stride = PredictorState(tif)->stride;
351
0
  uint16_t* wp = (uint16_t*) cp0;
352
0
  tmsize_t wc = cc / 2;
353
354
0
    if((cc%(2*stride))!=0)
355
0
    {
356
0
        TIFFErrorExt(tif->tif_clientdata, "horAcc16",
357
0
                     "%s", "cc%(2*stride))!=0");
358
0
        return 0;
359
0
    }
360
361
0
  if (wc > stride) {
362
0
    wc -= stride;
363
0
    do {
364
0
      REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] + (unsigned int)wp[0]) & 0xffff); wp++)
365
0
      wc -= stride;
366
0
    } while (wc > 0);
367
0
  }
368
0
  return 1;
369
0
}
370
371
static int
372
swabHorAcc32(TIFF* tif, uint8_t* cp0, tmsize_t cc)
373
0
{
374
0
  uint32_t* wp = (uint32_t*) cp0;
375
0
  tmsize_t wc = cc / 4;
376
377
0
        TIFFSwabArrayOfLong(wp, wc);
378
0
  return horAcc32(tif, cp0, cc);
379
0
}
380
381
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
382
static int
383
horAcc32(TIFF* tif, uint8_t* cp0, tmsize_t cc)
384
0
{
385
0
  tmsize_t stride = PredictorState(tif)->stride;
386
0
  uint32_t* wp = (uint32_t*) cp0;
387
0
  tmsize_t wc = cc / 4;
388
389
0
    if((cc%(4*stride))!=0)
390
0
    {
391
0
        TIFFErrorExt(tif->tif_clientdata, "horAcc32",
392
0
                     "%s", "cc%(4*stride))!=0");
393
0
        return 0;
394
0
    }
395
396
0
  if (wc > stride) {
397
0
    wc -= stride;
398
0
    do {
399
0
      REPEAT4(stride, wp[stride] += wp[0]; wp++)
400
0
      wc -= stride;
401
0
    } while (wc > 0);
402
0
  }
403
0
  return 1;
404
0
}
405
406
/*
407
 * Floating point predictor accumulation routine.
408
 */
409
static int
410
fpAcc(TIFF* tif, uint8_t* cp0, tmsize_t cc)
411
0
{
412
0
  tmsize_t stride = PredictorState(tif)->stride;
413
0
  uint32_t bps = tif->tif_dir.td_bitspersample / 8;
414
0
  tmsize_t wc = cc / bps;
415
0
  tmsize_t count = cc;
416
0
  uint8_t *cp = (uint8_t *) cp0;
417
0
  uint8_t *tmp;
418
419
0
    if(cc%(bps*stride)!=0)
420
0
    {
421
0
        TIFFErrorExt(tif->tif_clientdata, "fpAcc",
422
0
                     "%s", "cc%(bps*stride))!=0");
423
0
        return 0;
424
0
    }
425
426
0
    tmp = (uint8_t *)_TIFFmalloc(cc);
427
0
  if (!tmp)
428
0
    return 0;
429
430
0
  while (count > stride) {
431
0
    REPEAT4(stride, cp[stride] =
432
0
                        (unsigned char) ((cp[stride] + cp[0]) & 0xff); cp++)
433
0
    count -= stride;
434
0
  }
435
436
0
  _TIFFmemcpy(tmp, cp0, cc);
437
0
  cp = (uint8_t *) cp0;
438
0
  for (count = 0; count < wc; count++) {
439
0
    uint32_t byte;
440
0
    for (byte = 0; byte < bps; byte++) {
441
      #if WORDS_BIGENDIAN
442
      cp[bps * count + byte] = tmp[byte * wc + count];
443
      #else
444
0
      cp[bps * count + byte] =
445
0
        tmp[(bps - byte - 1) * wc + count];
446
0
      #endif
447
0
    }
448
0
  }
449
0
  _TIFFfree(tmp);
450
0
    return 1;
451
0
}
452
453
/*
454
 * Decode a scanline and apply the predictor routine.
455
 */
456
static int
457
PredictorDecodeRow(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s)
458
0
{
459
0
  TIFFPredictorState *sp = PredictorState(tif);
460
461
0
  assert(sp != NULL);
462
0
  assert(sp->decoderow != NULL);
463
0
  assert(sp->decodepfunc != NULL);  
464
465
0
  if ((*sp->decoderow)(tif, op0, occ0, s)) {
466
0
    return (*sp->decodepfunc)(tif, op0, occ0);
467
0
  } else
468
0
    return 0;
469
0
}
470
471
/*
472
 * Decode a tile/strip and apply the predictor routine.
473
 * Note that horizontal differencing must be done on a
474
 * row-by-row basis.  The width of a "row" has already
475
 * been calculated at pre-decode time according to the
476
 * strip/tile dimensions.
477
 */
478
static int
479
PredictorDecodeTile(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s)
480
0
{
481
0
  TIFFPredictorState *sp = PredictorState(tif);
482
483
0
  assert(sp != NULL);
484
0
  assert(sp->decodetile != NULL);
485
486
0
  if ((*sp->decodetile)(tif, op0, occ0, s)) {
487
0
    tmsize_t rowsize = sp->rowsize;
488
0
    assert(rowsize > 0);
489
0
    if((occ0%rowsize) !=0)
490
0
        {
491
0
            TIFFErrorExt(tif->tif_clientdata, "PredictorDecodeTile",
492
0
                         "%s", "occ0%rowsize != 0");
493
0
            return 0;
494
0
        }
495
0
    assert(sp->decodepfunc != NULL);
496
0
    while (occ0 > 0) {
497
0
      if( !(*sp->decodepfunc)(tif, op0, rowsize) )
498
0
                return 0;
499
0
      occ0 -= rowsize;
500
0
      op0 += rowsize;
501
0
    }
502
0
    return 1;
503
0
  } else
504
0
    return 0;
505
0
}
506
507
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
508
static int
509
horDiff8(TIFF* tif, uint8_t* cp0, tmsize_t cc)
510
0
{
511
0
  TIFFPredictorState* sp = PredictorState(tif);
512
0
  tmsize_t stride = sp->stride;
513
0
  unsigned char* cp = (unsigned char*) cp0;
514
515
0
    if((cc%stride)!=0)
516
0
    {
517
0
        TIFFErrorExt(tif->tif_clientdata, "horDiff8",
518
0
                     "%s", "(cc%stride)!=0");
519
0
        return 0;
520
0
    }
521
522
0
  if (cc > stride) {
523
0
    cc -= stride;
524
    /*
525
     * Pipeline the most common cases.
526
     */
527
0
    if (stride == 3) {
528
0
      unsigned int r1, g1, b1;
529
0
      unsigned int r2 = cp[0];
530
0
      unsigned int g2 = cp[1];
531
0
      unsigned  int b2 = cp[2];
532
0
      do {
533
0
        r1 = cp[3]; cp[3] = (unsigned char)((r1-r2)&0xff); r2 = r1;
534
0
        g1 = cp[4]; cp[4] = (unsigned char)((g1-g2)&0xff); g2 = g1;
535
0
        b1 = cp[5]; cp[5] = (unsigned char)((b1-b2)&0xff); b2 = b1;
536
0
        cp += 3;
537
0
      } while ((cc -= 3) > 0);
538
0
    } else if (stride == 4) {
539
0
      unsigned int r1, g1, b1, a1;
540
0
      unsigned int r2 = cp[0];
541
0
      unsigned int g2 = cp[1];
542
0
      unsigned int b2 = cp[2];
543
0
      unsigned int a2 = cp[3];
544
0
      do {
545
0
        r1 = cp[4]; cp[4] = (unsigned char)((r1-r2)&0xff); r2 = r1;
546
0
        g1 = cp[5]; cp[5] = (unsigned char)((g1-g2)&0xff); g2 = g1;
547
0
        b1 = cp[6]; cp[6] = (unsigned char)((b1-b2)&0xff); b2 = b1;
548
0
        a1 = cp[7]; cp[7] = (unsigned char)((a1-a2)&0xff); a2 = a1;
549
0
        cp += 4;
550
0
      } while ((cc -= 4) > 0);
551
0
    } else {
552
0
      cp += cc - 1;
553
0
      do {
554
0
        REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
555
0
      } while ((cc -= stride) > 0);
556
0
    }
557
0
  }
558
0
  return 1;
559
0
}
560
561
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
562
static int
563
horDiff16(TIFF* tif, uint8_t* cp0, tmsize_t cc)
564
0
{
565
0
  TIFFPredictorState* sp = PredictorState(tif);
566
0
  tmsize_t stride = sp->stride;
567
0
  uint16_t *wp = (uint16_t*) cp0;
568
0
  tmsize_t wc = cc/2;
569
570
0
    if((cc%(2*stride))!=0)
571
0
    {
572
0
        TIFFErrorExt(tif->tif_clientdata, "horDiff8",
573
0
                     "%s", "(cc%(2*stride))!=0");
574
0
        return 0;
575
0
    }
576
577
0
  if (wc > stride) {
578
0
    wc -= stride;
579
0
    wp += wc - 1;
580
0
    do {
581
0
      REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] - (unsigned int)wp[0]) & 0xffff); wp--)
582
0
      wc -= stride;
583
0
    } while (wc > 0);
584
0
  }
585
0
  return 1;
586
0
}
587
588
static int
589
swabHorDiff16(TIFF* tif, uint8_t* cp0, tmsize_t cc)
590
0
{
591
0
    uint16_t* wp = (uint16_t*) cp0;
592
0
    tmsize_t wc = cc / 2;
593
594
0
    if( !horDiff16(tif, cp0, cc) )
595
0
        return 0;
596
597
0
    TIFFSwabArrayOfShort(wp, wc);
598
0
    return 1;
599
0
}
600
601
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
602
static int
603
horDiff32(TIFF* tif, uint8_t* cp0, tmsize_t cc)
604
0
{
605
0
  TIFFPredictorState* sp = PredictorState(tif);
606
0
  tmsize_t stride = sp->stride;
607
0
  uint32_t *wp = (uint32_t*) cp0;
608
0
  tmsize_t wc = cc/4;
609
610
0
    if((cc%(4*stride))!=0)
611
0
    {
612
0
        TIFFErrorExt(tif->tif_clientdata, "horDiff32",
613
0
                     "%s", "(cc%(4*stride))!=0");
614
0
        return 0;
615
0
    }
616
617
0
  if (wc > stride) {
618
0
    wc -= stride;
619
0
    wp += wc - 1;
620
0
    do {
621
0
      REPEAT4(stride, wp[stride] -= wp[0]; wp--)
622
0
      wc -= stride;
623
0
    } while (wc > 0);
624
0
  }
625
0
  return 1;
626
0
}
627
628
static int
629
swabHorDiff32(TIFF* tif, uint8_t* cp0, tmsize_t cc)
630
0
{
631
0
    uint32_t* wp = (uint32_t*) cp0;
632
0
    tmsize_t wc = cc / 4;
633
634
0
    if( !horDiff32(tif, cp0, cc) )
635
0
        return 0;
636
637
0
    TIFFSwabArrayOfLong(wp, wc);
638
0
    return 1;
639
0
}
640
641
/*
642
 * Floating point predictor differencing routine.
643
 */
644
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
645
static int
646
fpDiff(TIFF* tif, uint8_t* cp0, tmsize_t cc)
647
0
{
648
0
  tmsize_t stride = PredictorState(tif)->stride;
649
0
  uint32_t bps = tif->tif_dir.td_bitspersample / 8;
650
0
  tmsize_t wc = cc / bps;
651
0
  tmsize_t count;
652
0
  uint8_t *cp = (uint8_t *) cp0;
653
0
  uint8_t *tmp;
654
655
0
    if((cc%(bps*stride))!=0)
656
0
    {
657
0
        TIFFErrorExt(tif->tif_clientdata, "fpDiff",
658
0
                     "%s", "(cc%(bps*stride))!=0");
659
0
        return 0;
660
0
    }
661
662
0
    tmp = (uint8_t *)_TIFFmalloc(cc);
663
0
  if (!tmp)
664
0
    return 0;
665
666
0
  _TIFFmemcpy(tmp, cp0, cc);
667
0
  for (count = 0; count < wc; count++) {
668
0
    uint32_t byte;
669
0
    for (byte = 0; byte < bps; byte++) {
670
      #if WORDS_BIGENDIAN
671
      cp[byte * wc + count] = tmp[bps * count + byte];
672
      #else
673
0
      cp[(bps - byte - 1) * wc + count] =
674
0
        tmp[bps * count + byte];
675
0
      #endif
676
0
    }
677
0
  }
678
0
  _TIFFfree(tmp);
679
680
0
  cp = (uint8_t *) cp0;
681
0
  cp += cc - stride - 1;
682
0
  for (count = cc; count > stride; count -= stride)
683
0
    REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
684
0
    return 1;
685
0
}
686
687
static int
688
PredictorEncodeRow(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s)
689
0
{
690
0
  TIFFPredictorState *sp = PredictorState(tif);
691
692
0
  assert(sp != NULL);
693
0
  assert(sp->encodepfunc != NULL);
694
0
  assert(sp->encoderow != NULL);
695
696
  /* XXX horizontal differencing alters user's data XXX */
697
0
  if( !(*sp->encodepfunc)(tif, bp, cc) )
698
0
        return 0;
699
0
  return (*sp->encoderow)(tif, bp, cc, s);
700
0
}
701
702
static int
703
PredictorEncodeTile(TIFF* tif, uint8_t* bp0, tmsize_t cc0, uint16_t s)
704
0
{
705
0
  static const char module[] = "PredictorEncodeTile";
706
0
  TIFFPredictorState *sp = PredictorState(tif);
707
0
        uint8_t *working_copy;
708
0
  tmsize_t cc = cc0, rowsize;
709
0
  unsigned char* bp;
710
0
        int result_code;
711
712
0
  assert(sp != NULL);
713
0
  assert(sp->encodepfunc != NULL);
714
0
  assert(sp->encodetile != NULL);
715
716
        /* 
717
         * Do predictor manipulation in a working buffer to avoid altering
718
         * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
719
         */
720
0
        working_copy = (uint8_t*) _TIFFmalloc(cc0);
721
0
        if( working_copy == NULL )
722
0
        {
723
0
            TIFFErrorExt(tif->tif_clientdata, module, 
724
0
                         "Out of memory allocating %" PRId64 " byte temp buffer.",
725
0
                         (int64_t) cc0 );
726
0
            return 0;
727
0
        }
728
0
        memcpy( working_copy, bp0, cc0 );
729
0
        bp = working_copy;
730
731
0
  rowsize = sp->rowsize;
732
0
  assert(rowsize > 0);
733
0
  if((cc0%rowsize)!=0)
734
0
    {
735
0
        TIFFErrorExt(tif->tif_clientdata, "PredictorEncodeTile",
736
0
                     "%s", "(cc0%rowsize)!=0");
737
0
        _TIFFfree( working_copy );
738
0
        return 0;
739
0
    }
740
0
  while (cc > 0) {
741
0
    (*sp->encodepfunc)(tif, bp, rowsize);
742
0
    cc -= rowsize;
743
0
    bp += rowsize;
744
0
  }
745
0
  result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
746
747
0
        _TIFFfree( working_copy );
748
749
0
        return result_code;
750
0
}
751
752
#define FIELD_PREDICTOR (FIELD_CODEC+0)   /* XXX */
753
754
static const TIFFField predictFields[] = {
755
    { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL },
756
};
757
758
static int
759
PredictorVSetField(TIFF* tif, uint32_t tag, va_list ap)
760
7.66k
{
761
7.66k
  TIFFPredictorState *sp = PredictorState(tif);
762
763
7.66k
  assert(sp != NULL);
764
7.66k
  assert(sp->vsetparent != NULL);
765
766
7.66k
  switch (tag) {
767
0
  case TIFFTAG_PREDICTOR:
768
0
    sp->predictor = (uint16_t) va_arg(ap, uint16_vap);
769
0
    TIFFSetFieldBit(tif, FIELD_PREDICTOR);
770
0
    break;
771
7.66k
  default:
772
7.66k
    return (*sp->vsetparent)(tif, tag, ap);
773
7.66k
  }
774
0
  tif->tif_flags |= TIFF_DIRTYDIRECT;
775
0
  return 1;
776
7.66k
}
777
778
static int
779
PredictorVGetField(TIFF* tif, uint32_t tag, va_list ap)
780
0
{
781
0
  TIFFPredictorState *sp = PredictorState(tif);
782
783
0
  assert(sp != NULL);
784
0
  assert(sp->vgetparent != NULL);
785
786
0
  switch (tag) {
787
0
  case TIFFTAG_PREDICTOR:
788
0
    *va_arg(ap, uint16_t*) = (uint16_t)sp->predictor;
789
0
    break;
790
0
  default:
791
0
    return (*sp->vgetparent)(tif, tag, ap);
792
0
  }
793
0
  return 1;
794
0
}
795
796
static void
797
PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
798
0
{
799
0
  TIFFPredictorState* sp = PredictorState(tif);
800
801
0
  (void) flags;
802
0
  if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
803
0
    fprintf(fd, "  Predictor: ");
804
0
    switch (sp->predictor) {
805
0
      case 1: fprintf(fd, "none "); break;
806
0
      case 2: fprintf(fd, "horizontal differencing "); break;
807
0
      case 3: fprintf(fd, "floating point predictor "); break;
808
0
    }
809
0
    fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
810
0
  }
811
0
  if (sp->printdir)
812
0
    (*sp->printdir)(tif, fd, flags);
813
0
}
814
815
int
816
TIFFPredictorInit(TIFF* tif)
817
1.27k
{
818
1.27k
  TIFFPredictorState* sp = PredictorState(tif);
819
820
1.27k
  assert(sp != 0);
821
822
  /*
823
   * Merge codec-specific tag information.
824
   */
825
1.27k
  if (!_TIFFMergeFields(tif, predictFields,
826
1.27k
            TIFFArrayCount(predictFields))) {
827
0
    TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
828
0
        "Merging Predictor codec-specific tags failed");
829
0
    return 0;
830
0
  }
831
832
  /*
833
   * Override parent get/set field methods.
834
   */
835
1.27k
  sp->vgetparent = tif->tif_tagmethods.vgetfield;
836
1.27k
  tif->tif_tagmethods.vgetfield =
837
1.27k
            PredictorVGetField;/* hook for predictor tag */
838
1.27k
  sp->vsetparent = tif->tif_tagmethods.vsetfield;
839
1.27k
  tif->tif_tagmethods.vsetfield =
840
1.27k
      PredictorVSetField;/* hook for predictor tag */
841
1.27k
  sp->printdir = tif->tif_tagmethods.printdir;
842
1.27k
  tif->tif_tagmethods.printdir =
843
1.27k
            PredictorPrintDir;  /* hook for predictor tag */
844
845
1.27k
  sp->setupdecode = tif->tif_setupdecode;
846
1.27k
  tif->tif_setupdecode = PredictorSetupDecode;
847
1.27k
  sp->setupencode = tif->tif_setupencode;
848
1.27k
  tif->tif_setupencode = PredictorSetupEncode;
849
850
1.27k
  sp->predictor = 1;      /* default value */
851
1.27k
  sp->encodepfunc = NULL;     /* no predictor routine */
852
1.27k
  sp->decodepfunc = NULL;     /* no predictor routine */
853
1.27k
  return 1;
854
1.27k
}
855
856
int
857
TIFFPredictorCleanup(TIFF* tif)
858
1.27k
{
859
1.27k
  TIFFPredictorState* sp = PredictorState(tif);
860
861
1.27k
  assert(sp != 0);
862
863
1.27k
  tif->tif_tagmethods.vgetfield = sp->vgetparent;
864
1.27k
  tif->tif_tagmethods.vsetfield = sp->vsetparent;
865
1.27k
  tif->tif_tagmethods.printdir = sp->printdir;
866
1.27k
  tif->tif_setupdecode = sp->setupdecode;
867
1.27k
  tif->tif_setupencode = sp->setupencode;
868
869
1.27k
  return 1;
870
1.27k
}
871
872
/* vim: set ts=8 sts=8 sw=8 noet: */
873
/*
874
 * Local Variables:
875
 * mode: c
876
 * c-basic-offset: 8
877
 * fill-column: 78
878
 * End:
879
 */