Coverage Report

Created: 2026-04-10 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/libtiff/tif_pixarlog.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1996-1997 Sam Leffler
3
 * Copyright (c) 1996 Pixar
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
 * Pixar, 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 Pixar, 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 PIXAR, 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
#include "tiffiop.h"
26
#ifdef PIXARLOG_SUPPORT
27
28
/*
29
 * TIFF Library.
30
 * PixarLog Compression Support
31
 *
32
 * Contributed by Dan McCoy.
33
 *
34
 * PixarLog film support uses the TIFF library to store companded
35
 * 11 bit values into a tiff file, which are compressed using the
36
 * zip compressor.
37
 *
38
 * The codec can take as input and produce as output 32-bit IEEE float values
39
 * as well as 16-bit or 8-bit unsigned integer values.
40
 *
41
 * On writing any of the above are converted into the internal
42
 * 11-bit log format.   In the case of  8 and 16 bit values, the
43
 * input is assumed to be unsigned linear color values that represent
44
 * the range 0-1.  In the case of IEEE values, the 0-1 range is assumed to
45
 * be the normal linear color range, in addition over 1 values are
46
 * accepted up to a value of about 25.0 to encode "hot" highlights and such.
47
 * The encoding is lossless for 8-bit values, slightly lossy for the
48
 * other bit depths.  The actual color precision should be better
49
 * than the human eye can perceive with extra room to allow for
50
 * error introduced by further image computation.  As with any quantized
51
 * color format, it is possible to perform image calculations which
52
 * expose the quantization error. This format should certainly be less
53
 * susceptible to such errors than standard 8-bit encodings, but more
54
 * susceptible than straight 16-bit or 32-bit encodings.
55
 *
56
 * On reading the internal format is converted to the desired output format.
57
 * The program can request which format it desires by setting the internal
58
 * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
59
 *  PIXARLOGDATAFMT_FLOAT     = provide IEEE float values.
60
 *  PIXARLOGDATAFMT_16BIT     = provide unsigned 16-bit integer values
61
 *  PIXARLOGDATAFMT_8BIT      = provide unsigned 8-bit integer values
62
 *
63
 * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
64
 * values with the difference that if there are exactly three or four channels
65
 * (rgb or rgba) it swaps the channel order (bgr or abgr).
66
 *
67
 * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
68
 * packed in 16-bit values.   However no tools are supplied for interpreting
69
 * these values.
70
 *
71
 * "hot" (over 1.0) areas written in floating point get clamped to
72
 * 1.0 in the integer data types.
73
 *
74
 * When the file is closed after writing, the bit depth and sample format
75
 * are set always to appear as if 8-bit data has been written into it.
76
 * That way a naive program unaware of the particulars of the encoding
77
 * gets the format it is most likely able to handle.
78
 *
79
 * The codec does it's own horizontal differencing step on the coded
80
 * values so the libraries predictor stuff should be turned off.
81
 * The codec also handle byte swapping the encoded values as necessary
82
 * since the library does not have the information necessary
83
 * to know the bit depth of the raw unencoded buffer.
84
 *
85
 * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc.
86
 * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT
87
 * as noted in http://trac.osgeo.org/gdal/ticket/3894.   FrankW - Jan'11
88
 */
89
90
#include "tif_predict.h"
91
#include "zlib.h"
92
93
#include <math.h>
94
#include <stdio.h>
95
#include <stdlib.h>
96
97
/* Tables for converting to/from 11 bit coded values */
98
99
0
#define TSIZE 2048   /* decode table size (11-bit tokens) */
100
0
#define TSIZEP1 2049 /* Plus one for slop */
101
0
#define ONE 1250     /* token value of 1.0 exactly */
102
0
#define RATIO 1.004  /* nominal ratio for log part */
103
104
0
#define CODE_MASK 0x7ff /* 11 bits. */
105
106
static float Fltsize;
107
static float LogK1, LogK2;
108
109
#define REPEAT(n, op)                                                          \
110
0
    {                                                                          \
111
0
        int i;                                                                 \
112
0
        i = n;                                                                 \
113
0
        do                                                                     \
114
0
        {                                                                      \
115
0
            i--;                                                               \
116
0
            op;                                                                \
117
0
        } while (i > 0);                                                       \
118
0
    }
119
120
static void horizontalAccumulateF(uint16_t *wp, tmsize_t n, int stride,
121
                                  float *op, float *ToLinearF)
122
0
{
123
0
    unsigned int cr, cg, cb, ca, mask;
124
0
    float t0, t1, t2, t3;
125
126
0
    if (n >= stride)
127
0
    {
128
0
        mask = CODE_MASK;
129
0
        if (stride == 3)
130
0
        {
131
0
            t0 = ToLinearF[cr = (wp[0] & mask)];
132
0
            t1 = ToLinearF[cg = (wp[1] & mask)];
133
0
            t2 = ToLinearF[cb = (wp[2] & mask)];
134
0
            op[0] = t0;
135
0
            op[1] = t1;
136
0
            op[2] = t2;
137
0
            n -= 3;
138
0
            while (n > 0)
139
0
            {
140
0
                wp += 3;
141
0
                op += 3;
142
0
                n -= 3;
143
0
                t0 = ToLinearF[(cr += wp[0]) & mask];
144
0
                t1 = ToLinearF[(cg += wp[1]) & mask];
145
0
                t2 = ToLinearF[(cb += wp[2]) & mask];
146
0
                op[0] = t0;
147
0
                op[1] = t1;
148
0
                op[2] = t2;
149
0
            }
150
0
        }
151
0
        else if (stride == 4)
152
0
        {
153
0
            t0 = ToLinearF[cr = (wp[0] & mask)];
154
0
            t1 = ToLinearF[cg = (wp[1] & mask)];
155
0
            t2 = ToLinearF[cb = (wp[2] & mask)];
156
0
            t3 = ToLinearF[ca = (wp[3] & mask)];
157
0
            op[0] = t0;
158
0
            op[1] = t1;
159
0
            op[2] = t2;
160
0
            op[3] = t3;
161
0
            n -= 4;
162
0
            while (n > 0)
163
0
            {
164
0
                wp += 4;
165
0
                op += 4;
166
0
                n -= 4;
167
0
                t0 = ToLinearF[(cr += wp[0]) & mask];
168
0
                t1 = ToLinearF[(cg += wp[1]) & mask];
169
0
                t2 = ToLinearF[(cb += wp[2]) & mask];
170
0
                t3 = ToLinearF[(ca += wp[3]) & mask];
171
0
                op[0] = t0;
172
0
                op[1] = t1;
173
0
                op[2] = t2;
174
0
                op[3] = t3;
175
0
            }
176
0
        }
177
0
        else
178
0
        {
179
0
            REPEAT(stride, *op = ToLinearF[*wp & mask]; wp++; op++)
180
0
            n -= stride;
181
0
            while (n > 0)
182
0
            {
183
0
                REPEAT(stride, *wp += wp[-stride]; *op = ToLinearF[*wp & mask];
184
0
                       wp++; op++)
185
0
                n -= stride;
186
0
            }
187
0
        }
188
0
    }
189
0
}
190
191
static void horizontalAccumulate12(uint16_t *wp, tmsize_t n, int stride,
192
                                   int16_t *op, float *ToLinearF)
193
0
{
194
0
    unsigned int cr, cg, cb, ca, mask;
195
0
    float t0, t1, t2, t3;
196
197
0
#define SCALE12 2048.0F
198
0
#define CLAMP12(t) (((t) < 3071) ? (int16_t)(uint16_t)(t) : (int16_t)3071)
199
200
0
    if (n >= stride)
201
0
    {
202
0
        mask = CODE_MASK;
203
0
        if (stride == 3)
204
0
        {
205
0
            t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
206
0
            t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
207
0
            t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
208
0
            op[0] = CLAMP12(t0);
209
0
            op[1] = CLAMP12(t1);
210
0
            op[2] = CLAMP12(t2);
211
0
            n -= 3;
212
0
            while (n > 0)
213
0
            {
214
0
                wp += 3;
215
0
                op += 3;
216
0
                n -= 3;
217
0
                t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
218
0
                t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
219
0
                t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
220
0
                op[0] = CLAMP12(t0);
221
0
                op[1] = CLAMP12(t1);
222
0
                op[2] = CLAMP12(t2);
223
0
            }
224
0
        }
225
0
        else if (stride == 4)
226
0
        {
227
0
            t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
228
0
            t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
229
0
            t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
230
0
            t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
231
0
            op[0] = CLAMP12(t0);
232
0
            op[1] = CLAMP12(t1);
233
0
            op[2] = CLAMP12(t2);
234
0
            op[3] = CLAMP12(t3);
235
0
            n -= 4;
236
0
            while (n > 0)
237
0
            {
238
0
                wp += 4;
239
0
                op += 4;
240
0
                n -= 4;
241
0
                t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
242
0
                t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
243
0
                t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
244
0
                t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
245
0
                op[0] = CLAMP12(t0);
246
0
                op[1] = CLAMP12(t1);
247
0
                op[2] = CLAMP12(t2);
248
0
                op[3] = CLAMP12(t3);
249
0
            }
250
0
        }
251
0
        else
252
0
        {
253
0
            REPEAT(stride, t0 = ToLinearF[*wp & mask] * SCALE12;
254
0
                   *op = CLAMP12(t0); wp++; op++)
255
0
            n -= stride;
256
0
            while (n > 0)
257
0
            {
258
0
                REPEAT(stride, *wp += wp[-stride];
259
0
                       t0 = ToLinearF[*wp & mask] * SCALE12; *op = CLAMP12(t0);
260
0
                       wp++; op++)
261
0
                n -= stride;
262
0
            }
263
0
        }
264
0
    }
265
0
}
266
267
static void horizontalAccumulate16(uint16_t *wp, tmsize_t n, int stride,
268
                                   uint16_t *op, uint16_t *ToLinear16)
269
0
{
270
0
    unsigned int cr, cg, cb, ca, mask;
271
272
0
    if (n >= stride)
273
0
    {
274
0
        mask = CODE_MASK;
275
0
        if (stride == 3)
276
0
        {
277
0
            op[0] = ToLinear16[cr = (wp[0] & mask)];
278
0
            op[1] = ToLinear16[cg = (wp[1] & mask)];
279
0
            op[2] = ToLinear16[cb = (wp[2] & mask)];
280
0
            n -= 3;
281
0
            while (n > 0)
282
0
            {
283
0
                wp += 3;
284
0
                op += 3;
285
0
                n -= 3;
286
0
                op[0] = ToLinear16[(cr += wp[0]) & mask];
287
0
                op[1] = ToLinear16[(cg += wp[1]) & mask];
288
0
                op[2] = ToLinear16[(cb += wp[2]) & mask];
289
0
            }
290
0
        }
291
0
        else if (stride == 4)
292
0
        {
293
0
            op[0] = ToLinear16[cr = (wp[0] & mask)];
294
0
            op[1] = ToLinear16[cg = (wp[1] & mask)];
295
0
            op[2] = ToLinear16[cb = (wp[2] & mask)];
296
0
            op[3] = ToLinear16[ca = (wp[3] & mask)];
297
0
            n -= 4;
298
0
            while (n > 0)
299
0
            {
300
0
                wp += 4;
301
0
                op += 4;
302
0
                n -= 4;
303
0
                op[0] = ToLinear16[(cr += wp[0]) & mask];
304
0
                op[1] = ToLinear16[(cg += wp[1]) & mask];
305
0
                op[2] = ToLinear16[(cb += wp[2]) & mask];
306
0
                op[3] = ToLinear16[(ca += wp[3]) & mask];
307
0
            }
308
0
        }
309
0
        else
310
0
        {
311
0
            REPEAT(stride, *op = ToLinear16[*wp & mask]; wp++; op++)
312
0
            n -= stride;
313
0
            while (n > 0)
314
0
            {
315
0
                REPEAT(stride, *wp += wp[-stride]; *op = ToLinear16[*wp & mask];
316
0
                       wp++; op++)
317
0
                n -= stride;
318
0
            }
319
0
        }
320
0
    }
321
0
}
322
323
/*
324
 * Returns the log encoded 11-bit values with the horizontal
325
 * differencing undone.
326
 */
327
static void horizontalAccumulate11(uint16_t *wp, tmsize_t n, int stride,
328
                                   uint16_t *op)
329
0
{
330
0
    unsigned int cr, cg, cb, ca, mask;
331
332
0
    if (n >= stride)
333
0
    {
334
0
        mask = CODE_MASK;
335
0
        if (stride == 3)
336
0
        {
337
0
            op[0] = wp[0];
338
0
            op[1] = wp[1];
339
0
            op[2] = wp[2];
340
0
            cr = wp[0];
341
0
            cg = wp[1];
342
0
            cb = wp[2];
343
0
            n -= 3;
344
0
            while (n > 0)
345
0
            {
346
0
                wp += 3;
347
0
                op += 3;
348
0
                n -= 3;
349
0
                op[0] = (uint16_t)((cr += wp[0]) & mask);
350
0
                op[1] = (uint16_t)((cg += wp[1]) & mask);
351
0
                op[2] = (uint16_t)((cb += wp[2]) & mask);
352
0
            }
353
0
        }
354
0
        else if (stride == 4)
355
0
        {
356
0
            op[0] = wp[0];
357
0
            op[1] = wp[1];
358
0
            op[2] = wp[2];
359
0
            op[3] = wp[3];
360
0
            cr = wp[0];
361
0
            cg = wp[1];
362
0
            cb = wp[2];
363
0
            ca = wp[3];
364
0
            n -= 4;
365
0
            while (n > 0)
366
0
            {
367
0
                wp += 4;
368
0
                op += 4;
369
0
                n -= 4;
370
0
                op[0] = (uint16_t)((cr += wp[0]) & mask);
371
0
                op[1] = (uint16_t)((cg += wp[1]) & mask);
372
0
                op[2] = (uint16_t)((cb += wp[2]) & mask);
373
0
                op[3] = (uint16_t)((ca += wp[3]) & mask);
374
0
            }
375
0
        }
376
0
        else
377
0
        {
378
0
            REPEAT(stride, *op = (uint16_t)(*wp & mask); wp++; op++)
379
0
            n -= stride;
380
0
            while (n > 0)
381
0
            {
382
0
                REPEAT(stride, *wp += wp[-stride]; *op = (uint16_t)(*wp & mask);
383
0
                       wp++; op++)
384
0
                n -= stride;
385
0
            }
386
0
        }
387
0
    }
388
0
}
389
390
static void horizontalAccumulate8(uint16_t *wp, tmsize_t n, int stride,
391
                                  unsigned char *op, unsigned char *ToLinear8)
392
0
{
393
0
    unsigned int cr, cg, cb, ca, mask;
394
395
0
    if (n >= stride)
396
0
    {
397
0
        mask = CODE_MASK;
398
0
        if (stride == 3)
399
0
        {
400
0
            op[0] = ToLinear8[cr = (wp[0] & mask)];
401
0
            op[1] = ToLinear8[cg = (wp[1] & mask)];
402
0
            op[2] = ToLinear8[cb = (wp[2] & mask)];
403
0
            n -= 3;
404
0
            while (n > 0)
405
0
            {
406
0
                n -= 3;
407
0
                wp += 3;
408
0
                op += 3;
409
0
                op[0] = ToLinear8[(cr += wp[0]) & mask];
410
0
                op[1] = ToLinear8[(cg += wp[1]) & mask];
411
0
                op[2] = ToLinear8[(cb += wp[2]) & mask];
412
0
            }
413
0
        }
414
0
        else if (stride == 4)
415
0
        {
416
0
            op[0] = ToLinear8[cr = (wp[0] & mask)];
417
0
            op[1] = ToLinear8[cg = (wp[1] & mask)];
418
0
            op[2] = ToLinear8[cb = (wp[2] & mask)];
419
0
            op[3] = ToLinear8[ca = (wp[3] & mask)];
420
0
            n -= 4;
421
0
            while (n > 0)
422
0
            {
423
0
                n -= 4;
424
0
                wp += 4;
425
0
                op += 4;
426
0
                op[0] = ToLinear8[(cr += wp[0]) & mask];
427
0
                op[1] = ToLinear8[(cg += wp[1]) & mask];
428
0
                op[2] = ToLinear8[(cb += wp[2]) & mask];
429
0
                op[3] = ToLinear8[(ca += wp[3]) & mask];
430
0
            }
431
0
        }
432
0
        else
433
0
        {
434
0
            REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++)
435
0
            n -= stride;
436
0
            while (n > 0)
437
0
            {
438
0
                REPEAT(stride, *wp += wp[-stride]; *op = ToLinear8[*wp & mask];
439
0
                       wp++; op++)
440
0
                n -= stride;
441
0
            }
442
0
        }
443
0
    }
444
0
}
445
446
static void horizontalAccumulate8abgr(uint16_t *wp, tmsize_t n, int stride,
447
                                      unsigned char *op,
448
                                      unsigned char *ToLinear8)
449
0
{
450
0
    unsigned int cr, cg, cb, ca, mask;
451
0
    unsigned char t0, t1, t2, t3;
452
453
0
    if (n >= stride)
454
0
    {
455
0
        mask = CODE_MASK;
456
0
        if (stride == 3)
457
0
        {
458
0
            op[0] = 0;
459
0
            t1 = ToLinear8[cb = (wp[2] & mask)];
460
0
            t2 = ToLinear8[cg = (wp[1] & mask)];
461
0
            t3 = ToLinear8[cr = (wp[0] & mask)];
462
0
            op[1] = t1;
463
0
            op[2] = t2;
464
0
            op[3] = t3;
465
0
            n -= 3;
466
0
            while (n > 0)
467
0
            {
468
0
                n -= 3;
469
0
                wp += 3;
470
0
                op += 4;
471
0
                op[0] = 0;
472
0
                t1 = ToLinear8[(cb += wp[2]) & mask];
473
0
                t2 = ToLinear8[(cg += wp[1]) & mask];
474
0
                t3 = ToLinear8[(cr += wp[0]) & mask];
475
0
                op[1] = t1;
476
0
                op[2] = t2;
477
0
                op[3] = t3;
478
0
            }
479
0
        }
480
0
        else if (stride == 4)
481
0
        {
482
0
            t0 = ToLinear8[ca = (wp[3] & mask)];
483
0
            t1 = ToLinear8[cb = (wp[2] & mask)];
484
0
            t2 = ToLinear8[cg = (wp[1] & mask)];
485
0
            t3 = ToLinear8[cr = (wp[0] & mask)];
486
0
            op[0] = t0;
487
0
            op[1] = t1;
488
0
            op[2] = t2;
489
0
            op[3] = t3;
490
0
            n -= 4;
491
0
            while (n > 0)
492
0
            {
493
0
                n -= 4;
494
0
                wp += 4;
495
0
                op += 4;
496
0
                t0 = ToLinear8[(ca += wp[3]) & mask];
497
0
                t1 = ToLinear8[(cb += wp[2]) & mask];
498
0
                t2 = ToLinear8[(cg += wp[1]) & mask];
499
0
                t3 = ToLinear8[(cr += wp[0]) & mask];
500
0
                op[0] = t0;
501
0
                op[1] = t1;
502
0
                op[2] = t2;
503
0
                op[3] = t3;
504
0
            }
505
0
        }
506
0
        else
507
0
        {
508
0
            REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++)
509
0
            n -= stride;
510
0
            while (n > 0)
511
0
            {
512
0
                REPEAT(stride, *wp += wp[-stride]; *op = ToLinear8[*wp & mask];
513
0
                       wp++; op++)
514
0
                n -= stride;
515
0
            }
516
0
        }
517
0
    }
518
0
}
519
520
/*
521
 * State block for each open TIFF
522
 * file using PixarLog compression/decompression.
523
 */
524
typedef struct
525
{
526
    TIFFPredictorState predict;
527
    z_stream stream;
528
    tmsize_t tbuf_size; /* only set/used on reading for now */
529
    uint16_t *tbuf;
530
    uint16_t stride;
531
    int state;
532
    int user_datafmt;
533
    int quality;
534
0
#define PLSTATE_INIT 1
535
536
    TIFFVSetMethod vgetparent; /* super-class method */
537
    TIFFVSetMethod vsetparent; /* super-class method */
538
539
    float *ToLinearF;
540
    uint16_t *ToLinear16;
541
    unsigned char *ToLinear8;
542
    uint16_t *FromLT2;
543
    uint16_t *From14; /* Really for 16-bit data, but we shift down 2 */
544
    uint16_t *From8;
545
546
} PixarLogState;
547
548
static int PixarLogMakeTables(TIFF *tif, PixarLogState *sp)
549
0
{
550
551
    /*
552
     *    We make several tables here to convert between various external
553
     *    representations (float, 16-bit, and 8-bit) and the internal
554
     *    11-bit companded representation.  The 11-bit representation has two
555
     *    distinct regions.  A linear bottom end up through .018316 in steps
556
     *    of about .000073, and a region of constant ratio up to about 25.
557
     *    These floating point numbers are stored in the main table ToLinearF.
558
     *    All other tables are derived from this one.  The tables (and the
559
     *    ratios) are continuous at the internal seam.
560
     */
561
562
0
    int nlin, lt2size;
563
0
    int i, j;
564
0
    double b, c, linstep, v;
565
0
    float *ToLinearF;
566
0
    uint16_t *ToLinear16;
567
0
    unsigned char *ToLinear8;
568
0
    uint16_t *FromLT2;
569
0
    uint16_t *From14; /* Really for 16-bit data, but we shift down 2 */
570
0
    uint16_t *From8;
571
572
0
    c = log(RATIO);
573
0
    nlin = (int)(1. / c); /* nlin must be an integer */
574
0
    c = 1. / nlin;
575
0
    b = exp(-c * ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
576
0
    linstep = b * c * exp(1.);
577
578
0
    LogK1 = (float)(1. / c); /* if (v >= 2)  token = k1*log(v*k2) */
579
0
    LogK2 = (float)(1. / b);
580
0
    lt2size = (int)(2. / linstep) + 1;
581
0
    FromLT2 = (uint16_t *)_TIFFmallocExt(
582
0
        tif, (tmsize_t)((unsigned long)lt2size * sizeof(uint16_t)));
583
0
    From14 = (uint16_t *)_TIFFmallocExt(tif, 16384 * sizeof(uint16_t));
584
0
    From8 = (uint16_t *)_TIFFmallocExt(tif, 256 * sizeof(uint16_t));
585
0
    ToLinearF = (float *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(float));
586
0
    ToLinear16 = (uint16_t *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(uint16_t));
587
0
    ToLinear8 =
588
0
        (unsigned char *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(unsigned char));
589
0
    if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
590
0
        ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL)
591
0
    {
592
0
        if (FromLT2)
593
0
            _TIFFfreeExt(tif, FromLT2);
594
0
        if (From14)
595
0
            _TIFFfreeExt(tif, From14);
596
0
        if (From8)
597
0
            _TIFFfreeExt(tif, From8);
598
0
        if (ToLinearF)
599
0
            _TIFFfreeExt(tif, ToLinearF);
600
0
        if (ToLinear16)
601
0
            _TIFFfreeExt(tif, ToLinear16);
602
0
        if (ToLinear8)
603
0
            _TIFFfreeExt(tif, ToLinear8);
604
0
        sp->FromLT2 = NULL;
605
0
        sp->From14 = NULL;
606
0
        sp->From8 = NULL;
607
0
        sp->ToLinearF = NULL;
608
0
        sp->ToLinear16 = NULL;
609
0
        sp->ToLinear8 = NULL;
610
0
        return 0;
611
0
    }
612
613
0
    j = 0;
614
615
0
    for (i = 0; i < nlin; i++)
616
0
    {
617
0
        v = i * linstep;
618
0
        ToLinearF[j++] = (float)v;
619
0
    }
620
621
0
    for (i = nlin; i < TSIZE; i++)
622
0
        ToLinearF[j++] = (float)(b * exp(c * i));
623
624
0
    ToLinearF[2048] = ToLinearF[2047];
625
626
0
    for (i = 0; i < TSIZEP1; i++)
627
0
    {
628
0
        v = ToLinearF[i] * 65535.0 + 0.5;
629
0
        ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16_t)v;
630
0
        v = ToLinearF[i] * 255.0 + 0.5;
631
0
        ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v;
632
0
    }
633
634
0
    j = 0;
635
0
    for (i = 0; i < lt2size; i++)
636
0
    {
637
0
        if ((i * linstep) * (i * linstep) > ToLinearF[j] * ToLinearF[j + 1])
638
0
            j++;
639
0
        FromLT2[i] = (uint16_t)j;
640
0
    }
641
642
    /*
643
     * Since we lose info anyway on 16-bit data, we set up a 14-bit
644
     * table and shift 16-bit values down two bits on input.
645
     * saves a little table space.
646
     */
647
0
    j = 0;
648
0
    for (i = 0; i < 16384; i++)
649
0
    {
650
0
        while ((i / 16383.) * (i / 16383.) > ToLinearF[j] * ToLinearF[j + 1])
651
0
            j++;
652
0
        From14[i] = (uint16_t)j;
653
0
    }
654
655
0
    j = 0;
656
0
    for (i = 0; i < 256; i++)
657
0
    {
658
0
        while ((i / 255.) * (i / 255.) > ToLinearF[j] * ToLinearF[j + 1])
659
0
            j++;
660
0
        From8[i] = (uint16_t)j;
661
0
    }
662
663
0
    Fltsize = (float)(lt2size / 2);
664
665
0
    sp->ToLinearF = ToLinearF;
666
0
    sp->ToLinear16 = ToLinear16;
667
0
    sp->ToLinear8 = ToLinear8;
668
0
    sp->FromLT2 = FromLT2;
669
0
    sp->From14 = From14;
670
0
    sp->From8 = From8;
671
672
0
    return 1;
673
0
}
674
675
0
#define PixarLogDecoderState(tif) ((PixarLogState *)(tif)->tif_data)
676
0
#define PixarLogEncoderState(tif) ((PixarLogState *)(tif)->tif_data)
677
678
static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
679
static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
680
681
0
#define PIXARLOGDATAFMT_UNKNOWN -1
682
683
static int PixarLogGuessDataFmt(TIFFDirectory *td)
684
0
{
685
0
    int guess = PIXARLOGDATAFMT_UNKNOWN;
686
0
    int format = td->td_sampleformat;
687
688
    /* If the user didn't tell us his datafmt,
689
     * take our best guess from the bitspersample.
690
     */
691
0
    switch (td->td_bitspersample)
692
0
    {
693
0
        case 32:
694
0
            if (format == SAMPLEFORMAT_IEEEFP)
695
0
                guess = PIXARLOGDATAFMT_FLOAT;
696
0
            break;
697
0
        case 16:
698
0
            if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
699
0
                guess = PIXARLOGDATAFMT_16BIT;
700
0
            break;
701
0
        case 12:
702
0
            if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
703
0
                guess = PIXARLOGDATAFMT_12BITPICIO;
704
0
            break;
705
0
        case 11:
706
0
            if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
707
0
                guess = PIXARLOGDATAFMT_11BITLOG;
708
0
            break;
709
0
        case 8:
710
0
            if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
711
0
                guess = PIXARLOGDATAFMT_8BIT;
712
0
            break;
713
0
        default:
714
0
            break;
715
0
    }
716
717
0
    return guess;
718
0
}
719
720
static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2)
721
0
{
722
0
    return _TIFFMultiplySSize(NULL, m1, m2, NULL);
723
0
}
724
725
static tmsize_t add_ms(tmsize_t m1, tmsize_t m2)
726
0
{
727
0
    assert(m1 >= 0 && m2 >= 0);
728
    /* if either input is zero, assume overflow already occurred */
729
0
    if (m1 == 0 || m2 == 0)
730
0
        return 0;
731
0
    else if (m1 > TIFF_TMSIZE_T_MAX - m2)
732
0
        return 0;
733
734
0
    return m1 + m2;
735
0
}
736
737
static int PixarLogFixupTags(TIFF *tif)
738
0
{
739
0
    (void)tif;
740
0
    return (1);
741
0
}
742
743
static int PixarLogSetupDecode(TIFF *tif)
744
0
{
745
0
    static const char module[] = "PixarLogSetupDecode";
746
0
    TIFFDirectory *td = &tif->tif_dir;
747
0
    PixarLogState *sp = PixarLogDecoderState(tif);
748
0
    tmsize_t tbuf_size;
749
0
    uint32_t strip_height;
750
751
0
    assert(sp != NULL);
752
753
    /* This function can possibly be called several times by */
754
    /* PredictorSetupDecode() if this function succeeds but */
755
    /* PredictorSetup() fails */
756
0
    if ((sp->state & PLSTATE_INIT) != 0)
757
0
        return 1;
758
759
0
    strip_height = td->td_rowsperstrip;
760
0
    if (strip_height > td->td_imagelength)
761
0
        strip_height = td->td_imagelength;
762
763
    /* Make sure no byte swapping happens on the data
764
     * after decompression. */
765
0
    tif->tif_postdecode = _TIFFNoPostDecode;
766
767
    /* for some reason, we can't do this in TIFFInitPixarLog */
768
769
0
    sp->stride =
770
0
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
771
0
                                                    : 1);
772
0
    tbuf_size = multiply_ms(
773
0
        multiply_ms(multiply_ms(sp->stride, td->td_imagewidth), strip_height),
774
0
        sizeof(uint16_t));
775
    /* add one more stride in case input ends mid-stride */
776
0
    tbuf_size = add_ms(tbuf_size, sizeof(uint16_t) * sp->stride);
777
0
    if (tbuf_size == 0)
778
0
        return (0); /* TODO: this is an error return without error report
779
                       through TIFFErrorExt */
780
0
    sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size);
781
0
    if (sp->tbuf == NULL)
782
0
        return (0);
783
0
    sp->tbuf_size = tbuf_size;
784
0
    if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
785
0
        sp->user_datafmt = PixarLogGuessDataFmt(td);
786
0
    if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
787
0
    {
788
0
        _TIFFfreeExt(tif, sp->tbuf);
789
0
        sp->tbuf = NULL;
790
0
        sp->tbuf_size = 0;
791
0
        TIFFErrorExtR(tif, module,
792
0
                      "PixarLog compression can't handle bits depth/data "
793
0
                      "format combination (depth: %" PRIu16 ")",
794
0
                      td->td_bitspersample);
795
0
        return (0);
796
0
    }
797
798
0
    if (inflateInit(&sp->stream) != Z_OK)
799
0
    {
800
0
        _TIFFfreeExt(tif, sp->tbuf);
801
0
        sp->tbuf = NULL;
802
0
        sp->tbuf_size = 0;
803
0
        TIFFErrorExtR(tif, module, "%s",
804
0
                      sp->stream.msg ? sp->stream.msg : "(null)");
805
0
        return (0);
806
0
    }
807
0
    else
808
0
    {
809
0
        sp->state |= PLSTATE_INIT;
810
0
        return (1);
811
0
    }
812
0
}
813
814
/*
815
 * Setup state for decoding a strip.
816
 */
817
static int PixarLogPreDecode(TIFF *tif, uint16_t s)
818
0
{
819
0
    static const char module[] = "PixarLogPreDecode";
820
0
    PixarLogState *sp = PixarLogDecoderState(tif);
821
822
0
    (void)s;
823
0
    assert(sp != NULL);
824
0
    sp->stream.next_in = tif->tif_rawdata;
825
0
    assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
826
         we need to simplify this code to reflect a ZLib that is likely updated
827
         to deal with 8byte memory sizes, though this code will respond
828
         appropriately even before we simplify it */
829
0
    sp->stream.avail_in = (uInt)tif->tif_rawcc;
830
0
    if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
831
0
    {
832
0
        TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
833
0
        return (0);
834
0
    }
835
0
    return (inflateReset(&sp->stream) == Z_OK);
836
0
}
837
838
static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
839
0
{
840
0
    static const char module[] = "PixarLogDecode";
841
0
    TIFFDirectory *td = &tif->tif_dir;
842
0
    PixarLogState *sp = PixarLogDecoderState(tif);
843
0
    tmsize_t i;
844
0
    tmsize_t nsamples;
845
0
    tmsize_t llen;
846
0
    uint16_t *up;
847
848
0
    switch (sp->user_datafmt)
849
0
    {
850
0
        case PIXARLOGDATAFMT_FLOAT:
851
0
            nsamples = (tmsize_t)((uint64_t)occ /
852
0
                                  sizeof(float)); /* XXX float == 32 bits */
853
0
            break;
854
0
        case PIXARLOGDATAFMT_16BIT:
855
0
        case PIXARLOGDATAFMT_12BITPICIO:
856
0
        case PIXARLOGDATAFMT_11BITLOG:
857
0
            nsamples =
858
0
                (tmsize_t)((uint64_t)occ /
859
0
                           sizeof(uint16_t)); /* XXX uint16_t == 16 bits */
860
0
            break;
861
0
        case PIXARLOGDATAFMT_8BIT:
862
0
        case PIXARLOGDATAFMT_8BITABGR:
863
0
            nsamples = occ;
864
0
            break;
865
0
        default:
866
0
            TIFFErrorExtR(tif, module,
867
0
                          "%" PRIu16 " bit input not supported in PixarLog",
868
0
                          td->td_bitspersample);
869
0
            memset(op, 0, (size_t)occ);
870
0
            return 0;
871
0
    }
872
873
0
    llen = (tmsize_t)sp->stride * td->td_imagewidth;
874
875
0
    (void)s;
876
0
    assert(sp != NULL);
877
878
0
    sp->stream.next_in = tif->tif_rawcp;
879
0
    sp->stream.avail_in = (uInt)tif->tif_rawcc;
880
881
0
    sp->stream.next_out = (unsigned char *)sp->tbuf;
882
0
    assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
883
         we need to simplify this code to reflect a ZLib that is likely updated
884
         to deal with 8byte memory sizes, though this code will respond
885
         appropriately even before we simplify it */
886
0
    sp->stream.avail_out = (uInt)((unsigned long)nsamples * sizeof(uint16_t));
887
0
    if (sp->stream.avail_out != (unsigned long)nsamples * sizeof(uint16_t))
888
0
    {
889
0
        TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
890
0
        memset(op, 0, (size_t)occ);
891
0
        return (0);
892
0
    }
893
    /* Check that we will not fill more than what was allocated */
894
0
    if ((tmsize_t)sp->stream.avail_out > sp->tbuf_size)
895
0
    {
896
0
        TIFFErrorExtR(tif, module, "sp->stream.avail_out > sp->tbuf_size");
897
0
        memset(op, 0, (size_t)occ);
898
0
        return (0);
899
0
    }
900
0
    do
901
0
    {
902
0
        int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
903
0
        if (state == Z_STREAM_END)
904
0
        {
905
0
            break; /* XXX */
906
0
        }
907
0
        if (state == Z_DATA_ERROR)
908
0
        {
909
0
            TIFFErrorExtR(
910
0
                tif, module, "Decoding error at scanline %" PRIu32 ", %s",
911
0
                tif->tif_row, sp->stream.msg ? sp->stream.msg : "(null)");
912
0
            memset(op, 0, (size_t)occ);
913
0
            return (0);
914
0
        }
915
0
        if (state != Z_OK)
916
0
        {
917
0
            TIFFErrorExtR(tif, module, "ZLib error: %s",
918
0
                          sp->stream.msg ? sp->stream.msg : "(null)");
919
0
            memset(op, 0, (size_t)occ);
920
0
            return (0);
921
0
        }
922
0
    } while (sp->stream.avail_out > 0);
923
924
    /* hopefully, we got all the bytes we needed */
925
0
    if (sp->stream.avail_out != 0)
926
0
    {
927
0
        TIFFErrorExtR(tif, module,
928
0
                      "Not enough data at scanline %" PRIu32
929
0
                      " (short %u bytes)",
930
0
                      tif->tif_row, sp->stream.avail_out);
931
0
        memset(op, 0, (size_t)occ);
932
0
        return (0);
933
0
    }
934
935
0
    tif->tif_rawcp = sp->stream.next_in;
936
0
    tif->tif_rawcc = sp->stream.avail_in;
937
938
0
    up = sp->tbuf;
939
    /* Swap bytes in the data if from a different endian machine. */
940
0
    if (tif->tif_flags & TIFF_SWAB)
941
0
        TIFFSwabArrayOfShort(up, nsamples);
942
943
    /*
944
     * if llen is not an exact multiple of nsamples, the decode operation
945
     * may overflow the output buffer, so truncate it enough to prevent
946
     * that but still salvage as much data as possible.
947
     */
948
0
    if (nsamples % llen)
949
0
    {
950
0
        TIFFWarningExtR(tif, module,
951
0
                        "stride %" TIFF_SSIZE_FORMAT
952
0
                        " is not a multiple of sample count, "
953
0
                        "%" TIFF_SSIZE_FORMAT ", data truncated.",
954
0
                        llen, nsamples);
955
0
        nsamples -= nsamples % llen;
956
0
    }
957
958
0
    for (i = 0; i < nsamples; i += llen, up += llen)
959
0
    {
960
0
        switch (sp->user_datafmt)
961
0
        {
962
0
            case PIXARLOGDATAFMT_FLOAT:
963
0
                horizontalAccumulateF(up, llen, sp->stride, (float *)op,
964
0
                                      sp->ToLinearF);
965
0
                op += (unsigned long)llen * sizeof(float);
966
0
                break;
967
0
            case PIXARLOGDATAFMT_16BIT:
968
0
                horizontalAccumulate16(up, llen, sp->stride, (uint16_t *)op,
969
0
                                       sp->ToLinear16);
970
0
                op += (unsigned long)llen * sizeof(uint16_t);
971
0
                break;
972
0
            case PIXARLOGDATAFMT_12BITPICIO:
973
0
                horizontalAccumulate12(up, llen, sp->stride, (int16_t *)op,
974
0
                                       sp->ToLinearF);
975
0
                op += (unsigned long)llen * sizeof(int16_t);
976
0
                break;
977
0
            case PIXARLOGDATAFMT_11BITLOG:
978
0
                horizontalAccumulate11(up, llen, sp->stride, (uint16_t *)op);
979
0
                op += (unsigned long)llen * sizeof(uint16_t);
980
0
                break;
981
0
            case PIXARLOGDATAFMT_8BIT:
982
0
                horizontalAccumulate8(up, llen, sp->stride, (unsigned char *)op,
983
0
                                      sp->ToLinear8);
984
0
                op += (unsigned long)llen * sizeof(unsigned char);
985
0
                break;
986
0
            case PIXARLOGDATAFMT_8BITABGR:
987
0
                horizontalAccumulate8abgr(up, llen, sp->stride,
988
0
                                          (unsigned char *)op, sp->ToLinear8);
989
0
                op += (unsigned long)llen * sizeof(unsigned char);
990
0
                break;
991
0
            default:
992
0
                TIFFErrorExtR(tif, module, "Unsupported bits/sample: %" PRIu16,
993
0
                              td->td_bitspersample);
994
0
                memset(op, 0, (size_t)occ);
995
0
                return (0);
996
0
        }
997
0
    }
998
999
0
    return (1);
1000
0
}
1001
1002
static int PixarLogSetupEncode(TIFF *tif)
1003
0
{
1004
0
    static const char module[] = "PixarLogSetupEncode";
1005
0
    TIFFDirectory *td = &tif->tif_dir;
1006
0
    PixarLogState *sp = PixarLogEncoderState(tif);
1007
0
    tmsize_t tbuf_size;
1008
1009
0
    assert(sp != NULL);
1010
1011
    /* for some reason, we can't do this in TIFFInitPixarLog */
1012
1013
0
    sp->stride =
1014
0
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1015
0
                                                    : 1);
1016
0
    tbuf_size =
1017
0
        multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
1018
0
                                td->td_rowsperstrip),
1019
0
                    sizeof(uint16_t));
1020
0
    if (tbuf_size == 0)
1021
0
        return (0); /* TODO: this is an error return without error report
1022
                       through TIFFErrorExt */
1023
0
    sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size);
1024
0
    if (sp->tbuf == NULL)
1025
0
        return (0);
1026
0
    if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
1027
0
        sp->user_datafmt = PixarLogGuessDataFmt(td);
1028
0
    if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
1029
0
    {
1030
0
        TIFFErrorExtR(tif, module,
1031
0
                      "PixarLog compression can't handle %" PRIu16
1032
0
                      " bit linear encodings",
1033
0
                      td->td_bitspersample);
1034
0
        return (0);
1035
0
    }
1036
1037
0
    if (deflateInit(&sp->stream, sp->quality) != Z_OK)
1038
0
    {
1039
0
        TIFFErrorExtR(tif, module, "%s",
1040
0
                      sp->stream.msg ? sp->stream.msg : "(null)");
1041
0
        return (0);
1042
0
    }
1043
0
    else
1044
0
    {
1045
0
        sp->state |= PLSTATE_INIT;
1046
0
        return (1);
1047
0
    }
1048
0
}
1049
1050
/*
1051
 * Reset encoding state at the start of a strip.
1052
 */
1053
static int PixarLogPreEncode(TIFF *tif, uint16_t s)
1054
0
{
1055
0
    static const char module[] = "PixarLogPreEncode";
1056
0
    PixarLogState *sp = PixarLogEncoderState(tif);
1057
1058
0
    (void)s;
1059
0
    assert(sp != NULL);
1060
0
    sp->stream.next_out = tif->tif_rawdata;
1061
0
    assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
1062
         we need to simplify this code to reflect a ZLib that is likely updated
1063
         to deal with 8byte memory sizes, though this code will respond
1064
         appropriately even before we simplify it */
1065
0
    sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
1066
0
    if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
1067
0
    {
1068
0
        TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
1069
0
        return (0);
1070
0
    }
1071
0
    return (deflateReset(&sp->stream) == Z_OK);
1072
0
}
1073
1074
static void horizontalDifferenceF(float *ip, tmsize_t n, int stride,
1075
                                  uint16_t *wp, uint16_t *FromLT2)
1076
0
{
1077
0
    int32_t r1, g1, b1, a1, r2, g2, b2, a2, mask;
1078
0
    float fltsize = Fltsize;
1079
1080
0
#define CLAMP(v)                                                               \
1081
0
    ((v < (float)0.)     ? 0                                                   \
1082
0
     : (v < (float)2.)   ? FromLT2[(int)(v * fltsize)]                         \
1083
0
     : (v > (float)24.2) ? 2047                                                \
1084
0
                         : LogK1 * log(v * LogK2) + 0.5)
1085
1086
0
    mask = CODE_MASK;
1087
0
    if (n >= stride)
1088
0
    {
1089
0
        if (stride == 3)
1090
0
        {
1091
0
            r2 = wp[0] = (uint16_t)CLAMP(ip[0]);
1092
0
            g2 = wp[1] = (uint16_t)CLAMP(ip[1]);
1093
0
            b2 = wp[2] = (uint16_t)CLAMP(ip[2]);
1094
0
            n -= 3;
1095
0
            while (n > 0)
1096
0
            {
1097
0
                n -= 3;
1098
0
                wp += 3;
1099
0
                ip += 3;
1100
0
                r1 = (int32_t)CLAMP(ip[0]);
1101
0
                wp[0] = (uint16_t)((r1 - r2) & mask);
1102
0
                r2 = r1;
1103
0
                g1 = (int32_t)CLAMP(ip[1]);
1104
0
                wp[1] = (uint16_t)((g1 - g2) & mask);
1105
0
                g2 = g1;
1106
0
                b1 = (int32_t)CLAMP(ip[2]);
1107
0
                wp[2] = (uint16_t)((b1 - b2) & mask);
1108
0
                b2 = b1;
1109
0
            }
1110
0
        }
1111
0
        else if (stride == 4)
1112
0
        {
1113
0
            r2 = wp[0] = (uint16_t)CLAMP(ip[0]);
1114
0
            g2 = wp[1] = (uint16_t)CLAMP(ip[1]);
1115
0
            b2 = wp[2] = (uint16_t)CLAMP(ip[2]);
1116
0
            a2 = wp[3] = (uint16_t)CLAMP(ip[3]);
1117
0
            n -= 4;
1118
0
            while (n > 0)
1119
0
            {
1120
0
                n -= 4;
1121
0
                wp += 4;
1122
0
                ip += 4;
1123
0
                r1 = (int32_t)CLAMP(ip[0]);
1124
0
                wp[0] = (uint16_t)((r1 - r2) & mask);
1125
0
                r2 = r1;
1126
0
                g1 = (int32_t)CLAMP(ip[1]);
1127
0
                wp[1] = (uint16_t)((g1 - g2) & mask);
1128
0
                g2 = g1;
1129
0
                b1 = (int32_t)CLAMP(ip[2]);
1130
0
                wp[2] = (uint16_t)((b1 - b2) & mask);
1131
0
                b2 = b1;
1132
0
                a1 = (int32_t)CLAMP(ip[3]);
1133
0
                wp[3] = (uint16_t)((a1 - a2) & mask);
1134
0
                a2 = a1;
1135
0
            }
1136
0
        }
1137
0
        else
1138
0
        {
1139
0
            REPEAT(stride, wp[0] = (uint16_t)CLAMP(ip[0]); wp++; ip++)
1140
0
            n -= stride;
1141
0
            while (n > 0)
1142
0
            {
1143
0
                REPEAT(stride,
1144
0
                       wp[0] = (uint16_t)(((int32_t)CLAMP(ip[0]) -
1145
0
                                           (int32_t)CLAMP(ip[-stride])) &
1146
0
                                          mask);
1147
0
                       wp++; ip++)
1148
0
                n -= stride;
1149
0
            }
1150
0
        }
1151
0
    }
1152
0
}
1153
1154
static void horizontalDifference16(unsigned short *ip, tmsize_t n, int stride,
1155
                                   unsigned short *wp, uint16_t *From14)
1156
0
{
1157
0
    int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1158
1159
/* assumption is unsigned pixel values */
1160
0
#undef CLAMP
1161
0
#define CLAMP(v) From14[(v) >> 2]
1162
1163
0
    mask = CODE_MASK;
1164
0
    if (n >= stride)
1165
0
    {
1166
0
        if (stride == 3)
1167
0
        {
1168
0
            r2 = wp[0] = CLAMP(ip[0]);
1169
0
            g2 = wp[1] = CLAMP(ip[1]);
1170
0
            b2 = wp[2] = CLAMP(ip[2]);
1171
0
            n -= 3;
1172
0
            while (n > 0)
1173
0
            {
1174
0
                n -= 3;
1175
0
                wp += 3;
1176
0
                ip += 3;
1177
0
                r1 = CLAMP(ip[0]);
1178
0
                wp[0] = (uint16_t)((r1 - r2) & mask);
1179
0
                r2 = r1;
1180
0
                g1 = CLAMP(ip[1]);
1181
0
                wp[1] = (uint16_t)((g1 - g2) & mask);
1182
0
                g2 = g1;
1183
0
                b1 = CLAMP(ip[2]);
1184
0
                wp[2] = (uint16_t)((b1 - b2) & mask);
1185
0
                b2 = b1;
1186
0
            }
1187
0
        }
1188
0
        else if (stride == 4)
1189
0
        {
1190
0
            r2 = wp[0] = CLAMP(ip[0]);
1191
0
            g2 = wp[1] = CLAMP(ip[1]);
1192
0
            b2 = wp[2] = CLAMP(ip[2]);
1193
0
            a2 = wp[3] = CLAMP(ip[3]);
1194
0
            n -= 4;
1195
0
            while (n > 0)
1196
0
            {
1197
0
                n -= 4;
1198
0
                wp += 4;
1199
0
                ip += 4;
1200
0
                r1 = CLAMP(ip[0]);
1201
0
                wp[0] = (uint16_t)((r1 - r2) & mask);
1202
0
                r2 = r1;
1203
0
                g1 = CLAMP(ip[1]);
1204
0
                wp[1] = (uint16_t)((g1 - g2) & mask);
1205
0
                g2 = g1;
1206
0
                b1 = CLAMP(ip[2]);
1207
0
                wp[2] = (uint16_t)((b1 - b2) & mask);
1208
0
                b2 = b1;
1209
0
                a1 = CLAMP(ip[3]);
1210
0
                wp[3] = (uint16_t)((a1 - a2) & mask);
1211
0
                a2 = a1;
1212
0
            }
1213
0
        }
1214
0
        else
1215
0
        {
1216
0
            REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
1217
0
            n -= stride;
1218
0
            while (n > 0)
1219
0
            {
1220
0
                REPEAT(stride,
1221
0
                       wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) &
1222
0
                                          mask);
1223
0
                       wp++; ip++)
1224
0
                n -= stride;
1225
0
            }
1226
0
        }
1227
0
    }
1228
0
}
1229
1230
static void horizontalDifference8(unsigned char *ip, tmsize_t n, int stride,
1231
                                  unsigned short *wp, uint16_t *From8)
1232
0
{
1233
0
    int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1234
1235
0
#undef CLAMP
1236
0
#define CLAMP(v) (From8[(v)])
1237
1238
0
    mask = CODE_MASK;
1239
0
    if (n >= stride)
1240
0
    {
1241
0
        if (stride == 3)
1242
0
        {
1243
0
            r2 = wp[0] = CLAMP(ip[0]);
1244
0
            g2 = wp[1] = CLAMP(ip[1]);
1245
0
            b2 = wp[2] = CLAMP(ip[2]);
1246
0
            n -= 3;
1247
0
            while (n > 0)
1248
0
            {
1249
0
                n -= 3;
1250
0
                r1 = CLAMP(ip[3]);
1251
0
                wp[3] = (uint16_t)((r1 - r2) & mask);
1252
0
                r2 = r1;
1253
0
                g1 = CLAMP(ip[4]);
1254
0
                wp[4] = (uint16_t)((g1 - g2) & mask);
1255
0
                g2 = g1;
1256
0
                b1 = CLAMP(ip[5]);
1257
0
                wp[5] = (uint16_t)((b1 - b2) & mask);
1258
0
                b2 = b1;
1259
0
                wp += 3;
1260
0
                ip += 3;
1261
0
            }
1262
0
        }
1263
0
        else if (stride == 4)
1264
0
        {
1265
0
            r2 = wp[0] = CLAMP(ip[0]);
1266
0
            g2 = wp[1] = CLAMP(ip[1]);
1267
0
            b2 = wp[2] = CLAMP(ip[2]);
1268
0
            a2 = wp[3] = CLAMP(ip[3]);
1269
0
            n -= 4;
1270
0
            while (n > 0)
1271
0
            {
1272
0
                n -= 4;
1273
0
                r1 = CLAMP(ip[4]);
1274
0
                wp[4] = (uint16_t)((r1 - r2) & mask);
1275
0
                r2 = r1;
1276
0
                g1 = CLAMP(ip[5]);
1277
0
                wp[5] = (uint16_t)((g1 - g2) & mask);
1278
0
                g2 = g1;
1279
0
                b1 = CLAMP(ip[6]);
1280
0
                wp[6] = (uint16_t)((b1 - b2) & mask);
1281
0
                b2 = b1;
1282
0
                a1 = CLAMP(ip[7]);
1283
0
                wp[7] = (uint16_t)((a1 - a2) & mask);
1284
0
                a2 = a1;
1285
0
                wp += 4;
1286
0
                ip += 4;
1287
0
            }
1288
0
        }
1289
0
        else
1290
0
        {
1291
0
            REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
1292
0
            n -= stride;
1293
0
            while (n > 0)
1294
0
            {
1295
0
                REPEAT(stride,
1296
0
                       wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) &
1297
0
                                          mask);
1298
0
                       wp++; ip++)
1299
0
                n -= stride;
1300
0
            }
1301
0
        }
1302
0
    }
1303
0
}
1304
1305
/*
1306
 * Encode a chunk of pixels.
1307
 */
1308
static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1309
0
{
1310
0
    static const char module[] = "PixarLogEncode";
1311
0
    TIFFDirectory *td = &tif->tif_dir;
1312
0
    PixarLogState *sp = PixarLogEncoderState(tif);
1313
0
    tmsize_t i;
1314
0
    tmsize_t n;
1315
0
    tmsize_t llen;
1316
0
    unsigned short *up;
1317
1318
0
    (void)s;
1319
1320
0
    switch (sp->user_datafmt)
1321
0
    {
1322
0
        case PIXARLOGDATAFMT_FLOAT:
1323
0
            n = (tmsize_t)((unsigned long)cc /
1324
0
                           sizeof(float)); /* XXX float == 32 bits */
1325
0
            break;
1326
0
        case PIXARLOGDATAFMT_16BIT:
1327
0
        case PIXARLOGDATAFMT_12BITPICIO:
1328
0
        case PIXARLOGDATAFMT_11BITLOG:
1329
0
            n = (tmsize_t)((unsigned long)cc /
1330
0
                           sizeof(uint16_t)); /* XXX uint16_t == 16 bits */
1331
0
            break;
1332
0
        case PIXARLOGDATAFMT_8BIT:
1333
0
        case PIXARLOGDATAFMT_8BITABGR:
1334
0
            n = cc;
1335
0
            break;
1336
0
        default:
1337
0
            TIFFErrorExtR(tif, module,
1338
0
                          "%" PRIu16 " bit input not supported in PixarLog",
1339
0
                          td->td_bitspersample);
1340
0
            return 0;
1341
0
    }
1342
1343
0
    llen = (tmsize_t)sp->stride * td->td_imagewidth;
1344
    /* Check against the number of elements (of size uint16_t) of sp->tbuf */
1345
0
    if (n > ((tmsize_t)td->td_rowsperstrip * llen))
1346
0
    {
1347
0
        TIFFErrorExtR(tif, module, "Too many input bytes provided");
1348
0
        return 0;
1349
0
    }
1350
1351
0
    for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen)
1352
0
    {
1353
0
        switch (sp->user_datafmt)
1354
0
        {
1355
0
            case PIXARLOGDATAFMT_FLOAT:
1356
0
                horizontalDifferenceF((float *)bp, llen, sp->stride, up,
1357
0
                                      sp->FromLT2);
1358
0
                bp += (unsigned long)llen * sizeof(float);
1359
0
                break;
1360
0
            case PIXARLOGDATAFMT_16BIT:
1361
0
                horizontalDifference16((uint16_t *)bp, llen, sp->stride, up,
1362
0
                                       sp->From14);
1363
0
                bp += (unsigned long)llen * sizeof(uint16_t);
1364
0
                break;
1365
0
            case PIXARLOGDATAFMT_8BIT:
1366
0
                horizontalDifference8((unsigned char *)bp, llen, sp->stride, up,
1367
0
                                      sp->From8);
1368
0
                bp += (unsigned long)llen * sizeof(unsigned char);
1369
0
                break;
1370
0
            default:
1371
0
                TIFFErrorExtR(tif, module,
1372
0
                              "%" PRIu16 " bit input not supported in PixarLog",
1373
0
                              td->td_bitspersample);
1374
0
                return 0;
1375
0
        }
1376
0
    }
1377
1378
0
    sp->stream.next_in = (unsigned char *)sp->tbuf;
1379
0
    assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
1380
         we need to simplify this code to reflect a ZLib that is likely updated
1381
         to deal with 8byte memory sizes, though this code will respond
1382
         appropriately even before we simplify it */
1383
0
    sp->stream.avail_in = (uInt)((unsigned long)n * sizeof(uint16_t));
1384
0
    if ((sp->stream.avail_in / sizeof(uint16_t)) != (unsigned long)n)
1385
0
    {
1386
0
        TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
1387
0
        return (0);
1388
0
    }
1389
1390
0
    do
1391
0
    {
1392
0
        if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
1393
0
        {
1394
0
            TIFFErrorExtR(tif, module, "Encoder error: %s",
1395
0
                          sp->stream.msg ? sp->stream.msg : "(null)");
1396
0
            return (0);
1397
0
        }
1398
0
        if (sp->stream.avail_out == 0)
1399
0
        {
1400
0
            tif->tif_rawcc = tif->tif_rawdatasize;
1401
0
            if (!TIFFFlushData1(tif))
1402
0
                return 0;
1403
0
            sp->stream.next_out = tif->tif_rawdata;
1404
0
            sp->stream.avail_out =
1405
0
                (uInt)tif
1406
0
                    ->tif_rawdatasize; /* this is a safe typecast, as check is
1407
                                          made already in PixarLogPreEncode */
1408
0
        }
1409
0
    } while (sp->stream.avail_in > 0);
1410
0
    return (1);
1411
0
}
1412
1413
/*
1414
 * Finish off an encoded strip by flushing the last
1415
 * string and tacking on an End Of Information code.
1416
 */
1417
1418
static int PixarLogPostEncode(TIFF *tif)
1419
0
{
1420
0
    static const char module[] = "PixarLogPostEncode";
1421
0
    PixarLogState *sp = PixarLogEncoderState(tif);
1422
0
    int state;
1423
1424
0
    sp->stream.avail_in = 0;
1425
1426
0
    do
1427
0
    {
1428
0
        state = deflate(&sp->stream, Z_FINISH);
1429
0
        switch (state)
1430
0
        {
1431
0
            case Z_STREAM_END:
1432
0
            case Z_OK:
1433
0
                if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
1434
0
                {
1435
0
                    tif->tif_rawcc =
1436
0
                        tif->tif_rawdatasize - sp->stream.avail_out;
1437
0
                    if (!TIFFFlushData1(tif))
1438
0
                        return 0;
1439
0
                    sp->stream.next_out = tif->tif_rawdata;
1440
0
                    sp->stream.avail_out =
1441
0
                        (uInt)tif->tif_rawdatasize; /* this is a safe typecast,
1442
                                                       as check is made already
1443
                                                       in PixarLogPreEncode */
1444
0
                }
1445
0
                break;
1446
0
            default:
1447
0
                TIFFErrorExtR(tif, module, "ZLib error: %s",
1448
0
                              sp->stream.msg ? sp->stream.msg : "(null)");
1449
0
                return (0);
1450
0
        }
1451
0
    } while (state != Z_STREAM_END);
1452
0
    return (1);
1453
0
}
1454
1455
static void PixarLogClose(TIFF *tif)
1456
0
{
1457
0
    PixarLogState *sp = (PixarLogState *)tif->tif_data;
1458
0
    TIFFDirectory *td = &tif->tif_dir;
1459
1460
0
    assert(sp != 0);
1461
    /* In a really sneaky (and really incorrect, and untruthful, and
1462
     * troublesome, and error-prone) maneuver that completely goes against
1463
     * the spirit of TIFF, and breaks TIFF, on close, we covertly
1464
     * modify both bitspersample and sampleformat in the directory to
1465
     * indicate 8-bit linear.  This way, the decode "just works" even for
1466
     * readers that don't know about PixarLog, or how to set
1467
     * the PIXARLOGDATFMT pseudo-tag.
1468
     */
1469
1470
0
    if (sp->state & PLSTATE_INIT)
1471
0
    {
1472
        /* We test the state to avoid an issue such as in
1473
         * http://bugzilla.maptools.org/show_bug.cgi?id=2604
1474
         * What appends in that case is that the bitspersample is 1 and
1475
         * a TransferFunction is set. The size of the TransferFunction
1476
         * depends on 1<<bitspersample. So if we increase it, an access
1477
         * out of the buffer will happen at directory flushing.
1478
         * Another option would be to clear those targs.
1479
         */
1480
0
        td->td_bitspersample = 8;
1481
0
        td->td_sampleformat = SAMPLEFORMAT_UINT;
1482
0
    }
1483
0
}
1484
1485
static void PixarLogCleanup(TIFF *tif)
1486
0
{
1487
0
    PixarLogState *sp = (PixarLogState *)tif->tif_data;
1488
1489
0
    assert(sp != 0);
1490
1491
0
    (void)TIFFPredictorCleanup(tif);
1492
1493
0
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
1494
0
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
1495
1496
0
    if (sp->FromLT2)
1497
0
        _TIFFfreeExt(tif, sp->FromLT2);
1498
0
    if (sp->From14)
1499
0
        _TIFFfreeExt(tif, sp->From14);
1500
0
    if (sp->From8)
1501
0
        _TIFFfreeExt(tif, sp->From8);
1502
0
    if (sp->ToLinearF)
1503
0
        _TIFFfreeExt(tif, sp->ToLinearF);
1504
0
    if (sp->ToLinear16)
1505
0
        _TIFFfreeExt(tif, sp->ToLinear16);
1506
0
    if (sp->ToLinear8)
1507
0
        _TIFFfreeExt(tif, sp->ToLinear8);
1508
0
    if (sp->state & PLSTATE_INIT)
1509
0
    {
1510
0
        if (tif->tif_mode == O_RDONLY)
1511
0
            inflateEnd(&sp->stream);
1512
0
        else
1513
0
            deflateEnd(&sp->stream);
1514
0
    }
1515
0
    if (sp->tbuf)
1516
0
        _TIFFfreeExt(tif, sp->tbuf);
1517
0
    _TIFFfreeExt(tif, sp);
1518
0
    tif->tif_data = NULL;
1519
1520
0
    _TIFFSetDefaultCompressionState(tif);
1521
0
}
1522
1523
static int PixarLogVSetField(TIFF *tif, uint32_t tag, va_list ap)
1524
0
{
1525
0
    static const char module[] = "PixarLogVSetField";
1526
0
    PixarLogState *sp = (PixarLogState *)tif->tif_data;
1527
0
    int result;
1528
1529
0
    switch (tag)
1530
0
    {
1531
0
        case TIFFTAG_PIXARLOGQUALITY:
1532
0
            sp->quality = (int)va_arg(ap, int);
1533
0
            if (tif->tif_mode != O_RDONLY && (sp->state & PLSTATE_INIT))
1534
0
            {
1535
0
                if (deflateParams(&sp->stream, sp->quality,
1536
0
                                  Z_DEFAULT_STRATEGY) != Z_OK)
1537
0
                {
1538
0
                    TIFFErrorExtR(tif, module, "ZLib error: %s",
1539
0
                                  sp->stream.msg ? sp->stream.msg : "(null)");
1540
0
                    return (0);
1541
0
                }
1542
0
            }
1543
0
            return (1);
1544
0
        case TIFFTAG_PIXARLOGDATAFMT:
1545
0
            sp->user_datafmt = (int)va_arg(ap, int);
1546
            /* Tweak the TIFF header so that the rest of libtiff knows what
1547
             * size of data will be passed between app and library, and
1548
             * assume that the app knows what it is doing and is not
1549
             * confused by these header manipulations...
1550
             */
1551
0
            switch (sp->user_datafmt)
1552
0
            {
1553
0
                case PIXARLOGDATAFMT_8BIT:
1554
0
                case PIXARLOGDATAFMT_8BITABGR:
1555
0
                    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
1556
0
                    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1557
0
                    break;
1558
0
                case PIXARLOGDATAFMT_11BITLOG:
1559
0
                    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1560
0
                    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1561
0
                    break;
1562
0
                case PIXARLOGDATAFMT_12BITPICIO:
1563
0
                    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1564
0
                    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
1565
0
                    break;
1566
0
                case PIXARLOGDATAFMT_16BIT:
1567
0
                    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1568
0
                    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1569
0
                    break;
1570
0
                case PIXARLOGDATAFMT_FLOAT:
1571
0
                    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
1572
0
                    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT,
1573
0
                                 SAMPLEFORMAT_IEEEFP);
1574
0
                    break;
1575
0
                default:
1576
0
                    break;
1577
0
            }
1578
            /*
1579
             * Must recalculate sizes should bits/sample change.
1580
             */
1581
0
            tif->tif_tilesize =
1582
0
                isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
1583
0
            tif->tif_scanlinesize = TIFFScanlineSize(tif);
1584
0
            result = 1; /* NB: pseudo tag */
1585
0
            break;
1586
0
        default:
1587
0
            result = (*sp->vsetparent)(tif, tag, ap);
1588
0
    }
1589
0
    return (result);
1590
0
}
1591
1592
static int PixarLogVGetField(TIFF *tif, uint32_t tag, va_list ap)
1593
0
{
1594
0
    PixarLogState *sp = (PixarLogState *)tif->tif_data;
1595
1596
0
    switch (tag)
1597
0
    {
1598
0
        case TIFFTAG_PIXARLOGQUALITY:
1599
0
            *va_arg(ap, int *) = sp->quality;
1600
0
            break;
1601
0
        case TIFFTAG_PIXARLOGDATAFMT:
1602
0
            *va_arg(ap, int *) = sp->user_datafmt;
1603
0
            break;
1604
0
        default:
1605
0
            return (*sp->vgetparent)(tif, tag, ap);
1606
0
    }
1607
0
    return (1);
1608
0
}
1609
1610
static const TIFFField pixarlogFields[] = {
1611
    {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
1612
     FALSE, FALSE, "", NULL},
1613
    {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
1614
     FALSE, FALSE, "", NULL}};
1615
1616
int TIFFInitPixarLog(TIFF *tif, int scheme)
1617
0
{
1618
0
    static const char module[] = "TIFFInitPixarLog";
1619
1620
0
    PixarLogState *sp;
1621
1622
0
    (void)scheme;
1623
0
    assert(scheme == COMPRESSION_PIXARLOG);
1624
1625
    /*
1626
     * Merge codec-specific tag information.
1627
     */
1628
0
    if (!_TIFFMergeFields(tif, pixarlogFields, TIFFArrayCount(pixarlogFields)))
1629
0
    {
1630
0
        TIFFErrorExtR(tif, module,
1631
0
                      "Merging PixarLog codec-specific tags failed");
1632
0
        return 0;
1633
0
    }
1634
1635
    /*
1636
     * Allocate state block so tag methods have storage to record values.
1637
     */
1638
0
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(PixarLogState));
1639
0
    if (tif->tif_data == NULL)
1640
0
        goto bad;
1641
0
    sp = (PixarLogState *)tif->tif_data;
1642
0
    _TIFFmemset(sp, 0, sizeof(*sp));
1643
0
    sp->stream.data_type = Z_BINARY;
1644
0
    sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
1645
1646
    /*
1647
     * Install codec methods.
1648
     */
1649
0
    tif->tif_fixuptags = PixarLogFixupTags;
1650
0
    tif->tif_setupdecode = PixarLogSetupDecode;
1651
0
    tif->tif_predecode = PixarLogPreDecode;
1652
0
    tif->tif_decoderow = PixarLogDecode;
1653
0
    tif->tif_decodestrip = PixarLogDecode;
1654
0
    tif->tif_decodetile = PixarLogDecode;
1655
0
    tif->tif_setupencode = PixarLogSetupEncode;
1656
0
    tif->tif_preencode = PixarLogPreEncode;
1657
0
    tif->tif_postencode = PixarLogPostEncode;
1658
0
    tif->tif_encoderow = PixarLogEncode;
1659
0
    tif->tif_encodestrip = PixarLogEncode;
1660
0
    tif->tif_encodetile = PixarLogEncode;
1661
0
    tif->tif_close = PixarLogClose;
1662
0
    tif->tif_cleanup = PixarLogCleanup;
1663
1664
    /* Override SetField so we can handle our private pseudo-tag */
1665
0
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
1666
0
    tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
1667
0
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
1668
0
    tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
1669
1670
    /* Default values for codec-specific fields */
1671
0
    sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
1672
0
    sp->state = 0;
1673
1674
    /* we don't wish to use the predictor,
1675
     * the default is none, which predictor value 1
1676
     */
1677
0
    (void)TIFFPredictorInit(tif);
1678
1679
    /*
1680
     * build the companding tables
1681
     */
1682
0
    PixarLogMakeTables(tif, sp);
1683
1684
0
    return (1);
1685
0
bad:
1686
0
    TIFFErrorExtR(tif, module, "No space for PixarLog state block");
1687
0
    return (0);
1688
0
}
1689
#endif /* PIXARLOG_SUPPORT */