Coverage Report

Created: 2026-05-16 08:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/libtiff/tif_luv.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1997 Greg Ward Larson
3
 * Copyright (c) 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, Greg Larson and Silicon Graphics may not be used in any
10
 * advertising or publicity relating to the software without the specific,
11
 * prior written permission of Sam Leffler, Greg Larson 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, GREG LARSON OR SILICON GRAPHICS BE LIABLE
18
 * FOR 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 LOGLUV_SUPPORT
27
28
/*
29
 * TIFF Library.
30
 * LogLuv compression support for high dynamic range images.
31
 *
32
 * Contributed by Greg Larson.
33
 *
34
 * LogLuv image support uses the TIFF library to store 16 or 10-bit
35
 * log luminance values with 8 bits each of u and v or a 14-bit index.
36
 *
37
 * The codec can take as input and produce as output 32-bit IEEE float values
38
 * as well as 16-bit integer values.  A 16-bit luminance is interpreted
39
 * as a sign bit followed by a 15-bit integer that is converted
40
 * to and from a linear magnitude using the transformation:
41
 *
42
 *  L = 2^( (Le+.5)/256 - 64 )    # real from 15-bit
43
 *
44
 *  Le = floor( 256*(log2(L) + 64) )  # 15-bit from real
45
 *
46
 * The actual conversion to world luminance units in candelas per sq. meter
47
 * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
48
 * This value is usually set such that a reasonable exposure comes from
49
 * clamping decoded luminances above 1 to 1 in the displayed image.
50
 *
51
 * The 16-bit values for u and v may be converted to real values by dividing
52
 * each by 32768.  (This allows for negative values, which aren't useful as
53
 * far as we know, but are left in case of future improvements in human
54
 * color vision.)
55
 *
56
 * Conversion from (u,v), which is actually the CIE (u',v') system for
57
 * you color scientists, is accomplished by the following transformation:
58
 *
59
 *  u = 4*x / (-2*x + 12*y + 3)
60
 *  v = 9*y / (-2*x + 12*y + 3)
61
 *
62
 *  x = 9*u / (6*u - 16*v + 12)
63
 *  y = 4*v / (6*u - 16*v + 12)
64
 *
65
 * This process is greatly simplified by passing 32-bit IEEE floats
66
 * for each of three CIE XYZ coordinates.  The codec then takes care
67
 * of conversion to and from LogLuv, though the application is still
68
 * responsible for interpreting the TIFFTAG_STONITS calibration factor.
69
 *
70
 * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white
71
 * point of (x,y)=(1/3,1/3).  However, most color systems assume some other
72
 * white point, such as D65, and an absolute color conversion to XYZ then
73
 * to another color space with a different white point may introduce an
74
 * unwanted color cast to the image.  It is often desirable, therefore, to
75
 * perform a white point conversion that maps the input white to [1 1 1]
76
 * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT
77
 * tag value.  A decoder that demands absolute color calibration may use
78
 * this white point tag to get back the original colors, but usually it
79
 * will be ignored and the new white point will be used instead that
80
 * matches the output color space.
81
 *
82
 * Pixel information is compressed into one of two basic encodings, depending
83
 * on the setting of the compression tag, which is one of COMPRESSION_SGILOG
84
 * or COMPRESSION_SGILOG24.  For COMPRESSION_SGILOG, greyscale data is
85
 * stored as:
86
 *
87
 *   1       15
88
 *  |-+---------------|
89
 *
90
 * COMPRESSION_SGILOG color data is stored as:
91
 *
92
 *   1       15           8        8
93
 *  |-+---------------|--------+--------|
94
 *   S       Le           ue       ve
95
 *
96
 * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
97
 *
98
 *       10           14
99
 *  |----------|--------------|
100
 *       Le'          Ce
101
 *
102
 * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
103
 * encoded as an index for optimal color resolution.  The 10 log bits are
104
 * defined by the following conversions:
105
 *
106
 *  L = 2^((Le'+.5)/64 - 12)    # real from 10-bit
107
 *
108
 *  Le' = floor( 64*(log2(L) + 12) )  # 10-bit from real
109
 *
110
 * The 10 bits of the smaller format may be converted into the 15 bits of
111
 * the larger format by multiplying by 4 and adding 13314.  Obviously,
112
 * a smaller range of magnitudes is covered (about 5 orders of magnitude
113
 * instead of 38), and the lack of a sign bit means that negative luminances
114
 * are not allowed.  (Well, they aren't allowed in the real world, either,
115
 * but they are useful for certain types of image processing.)
116
 *
117
 * The desired user format is controlled by the setting the internal
118
 * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
119
 *  SGILOGDATAFMT_FLOAT       = IEEE 32-bit float XYZ values
120
 *  SGILOGDATAFMT_16BIT       = 16-bit integer encodings of logL, u and v
121
 * Raw data i/o is also possible using:
122
 *  SGILOGDATAFMT_RAW         = 32-bit unsigned integer with encoded pixel
123
 * In addition, the following decoding is provided for ease of display:
124
 *  SGILOGDATAFMT_8BIT        = 8-bit default RGB gamma-corrected values
125
 *
126
 * For grayscale images, we provide the following data formats:
127
 *  SGILOGDATAFMT_FLOAT       = IEEE 32-bit float Y values
128
 *  SGILOGDATAFMT_16BIT       = 16-bit integer w/ encoded luminance
129
 *  SGILOGDATAFMT_8BIT        = 8-bit gray monitor values
130
 *
131
 * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
132
 * scheme by separating the logL, u and v bytes for each row and applying
133
 * a PackBits type of compression.  Since the 24-bit encoding is not
134
 * adaptive, the 32-bit color format takes less space in many cases.
135
 *
136
 * Further control is provided over the conversion from higher-resolution
137
 * formats to final encoded values through the pseudo tag
138
 * TIFFTAG_SGILOGENCODE:
139
 *  SGILOGENCODE_NODITHER     = do not dither encoded values
140
 *  SGILOGENCODE_RANDITHER    = apply random dithering during encoding
141
 *
142
 * The default value of this tag is SGILOGENCODE_NODITHER for
143
 * COMPRESSION_SGILOG to maximize run-length encoding and
144
 * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn
145
 * quantization errors into noise.
146
 */
147
148
#include <limits.h>
149
#include <math.h>
150
#include <stdio.h>
151
#include <stdlib.h>
152
#include <time.h>
153
154
/*
155
 * State block for each open TIFF
156
 * file using LogLuv compression/decompression.
157
 */
158
typedef struct logLuvState LogLuvState;
159
160
struct logLuvState
161
{
162
    int encoder_state; /* 1 if encoder correctly initialized */
163
    int user_datafmt;  /* user data format */
164
    int encode_meth;   /* encoding method */
165
    int pixel_size;    /* bytes per pixel */
166
167
    uint8_t *tbuf;    /* translation buffer */
168
    tmsize_t tbuflen; /* buffer length */
169
    void (*tfunc)(LogLuvState *, uint8_t *, tmsize_t);
170
171
    TIFFVSetMethod vgetparent; /* super-class method */
172
    TIFFVSetMethod vsetparent; /* super-class method */
173
};
174
175
117k
#define DecoderState(tif) ((LogLuvState *)(tif)->tif_data)
176
0
#define EncoderState(tif) ((LogLuvState *)(tif)->tif_data)
177
178
12.2k
#define SGILOGDATAFMT_UNKNOWN -1
179
180
0
#define MINRUN 4 /* minimum run length */
181
182
/*
183
 * Decode a string of 16-bit gray pixels.
184
 */
185
static int LogL16Decode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
186
4.46k
{
187
4.46k
    static const char module[] = "LogL16Decode";
188
4.46k
    LogLuvState *sp = DecoderState(tif);
189
4.46k
    int shft;
190
4.46k
    tmsize_t i;
191
4.46k
    tmsize_t npixels;
192
4.46k
    unsigned char *bp;
193
4.46k
    int16_t *tp;
194
4.46k
    int16_t b;
195
4.46k
    tmsize_t cc;
196
4.46k
    int rc;
197
198
4.46k
    (void)s;
199
4.46k
    assert(s == 0);
200
4.46k
    assert(sp != NULL);
201
202
4.46k
    npixels = occ / sp->pixel_size;
203
204
4.46k
    if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
205
741
        tp = (int16_t *)op;
206
3.72k
    else
207
3.72k
    {
208
3.72k
        if (sp->tbuflen < npixels)
209
0
        {
210
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
211
0
            return (0);
212
0
        }
213
3.72k
        tp = (int16_t *)sp->tbuf;
214
3.72k
    }
215
4.46k
    _TIFFmemset((void *)tp, 0, (tmsize_t)((size_t)npixels * sizeof(tp[0])));
216
217
4.46k
    bp = (unsigned char *)tif->tif_rawcp;
218
4.46k
    cc = tif->tif_rawcc;
219
    /* get each byte string */
220
12.8k
    for (shft = 8; shft >= 0; shft -= 8)
221
8.72k
    {
222
25.9k
        for (i = 0; i < npixels && cc > 0;)
223
17.2k
        {
224
17.2k
            if (*bp >= 128)
225
6.30k
            { /* run */
226
6.30k
                if (cc < 2)
227
33
                    break;
228
6.27k
                rc = *bp++ + (2 - 128);
229
6.27k
                b = (int16_t)(*bp++ << shft);
230
6.27k
                cc -= 2;
231
259k
                while (rc-- && i < npixels)
232
252k
                    tp[i++] |= b;
233
6.27k
            }
234
10.9k
            else
235
10.9k
            {               /* non-run */
236
10.9k
                rc = *bp++; /* nul is noop */
237
48.2k
                while (--cc && rc-- && i < npixels)
238
37.2k
                    tp[i++] |= (int16_t)(*bp++ << shft);
239
10.9k
            }
240
17.2k
        }
241
8.72k
        if (i != npixels)
242
293
        {
243
293
            TIFFErrorExtR(tif, module,
244
293
                          "Not enough data at row %" PRIu32
245
293
                          " (short %" TIFF_SSIZE_FORMAT " pixels)",
246
293
                          tif->tif_dir.td_row, npixels - i);
247
293
            tif->tif_rawcp = (uint8_t *)bp;
248
293
            tif->tif_rawcc = cc;
249
293
            return (0);
250
293
        }
251
8.72k
    }
252
4.16k
    (*sp->tfunc)(sp, op, npixels);
253
4.16k
    tif->tif_rawcp = (uint8_t *)bp;
254
4.16k
    tif->tif_rawcc = cc;
255
4.16k
    return (1);
256
4.46k
}
257
258
/*
259
 * Decode a string of 24-bit pixels.
260
 */
261
static int LogLuvDecode24(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
262
25.7k
{
263
25.7k
    static const char module[] = "LogLuvDecode24";
264
25.7k
    LogLuvState *sp = DecoderState(tif);
265
25.7k
    tmsize_t cc;
266
25.7k
    tmsize_t i;
267
25.7k
    tmsize_t npixels;
268
25.7k
    unsigned char *bp;
269
25.7k
    uint32_t *tp;
270
271
25.7k
    (void)s;
272
25.7k
    assert(s == 0);
273
25.7k
    assert(sp != NULL);
274
275
25.7k
    npixels = occ / sp->pixel_size;
276
277
25.7k
    if (sp->user_datafmt == SGILOGDATAFMT_RAW)
278
446
        tp = (uint32_t *)op;
279
25.3k
    else
280
25.3k
    {
281
25.3k
        if (sp->tbuflen < npixels)
282
0
        {
283
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
284
0
            return (0);
285
0
        }
286
25.3k
        tp = (uint32_t *)sp->tbuf;
287
25.3k
    }
288
    /* copy to array of uint32_t */
289
25.7k
    bp = (unsigned char *)tif->tif_rawcp;
290
25.7k
    cc = tif->tif_rawcc;
291
105k
    for (i = 0; i < npixels && cc >= 3; i++)
292
79.6k
    {
293
79.6k
        tp[i] = (uint32_t)bp[0] << 16 | (uint32_t)bp[1] << 8 | bp[2];
294
79.6k
        bp += 3;
295
79.6k
        cc -= 3;
296
79.6k
    }
297
25.7k
    tif->tif_rawcp = (uint8_t *)bp;
298
25.7k
    tif->tif_rawcc = cc;
299
25.7k
    if (i != npixels)
300
612
    {
301
612
        TIFFErrorExtR(tif, module,
302
612
                      "Not enough data at row %" PRIu32
303
612
                      " (short %" TIFF_SSIZE_FORMAT " pixels)",
304
612
                      tif->tif_dir.td_row, npixels - i);
305
612
        return (0);
306
612
    }
307
25.1k
    (*sp->tfunc)(sp, op, npixels);
308
25.1k
    return (1);
309
25.7k
}
310
311
/*
312
 * Decode a string of 32-bit pixels.
313
 */
314
static int LogLuvDecode32(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
315
11.6k
{
316
11.6k
    static const char module[] = "LogLuvDecode32";
317
11.6k
    LogLuvState *sp;
318
11.6k
    int shft;
319
11.6k
    tmsize_t i;
320
11.6k
    tmsize_t npixels;
321
11.6k
    unsigned char *bp;
322
11.6k
    uint32_t *tp;
323
11.6k
    uint32_t b;
324
11.6k
    tmsize_t cc;
325
11.6k
    int rc;
326
327
11.6k
    (void)s;
328
11.6k
    assert(s == 0);
329
11.6k
    sp = DecoderState(tif);
330
11.6k
    assert(sp != NULL);
331
332
11.6k
    npixels = occ / sp->pixel_size;
333
334
11.6k
    if (sp->user_datafmt == SGILOGDATAFMT_RAW)
335
675
        tp = (uint32_t *)op;
336
10.9k
    else
337
10.9k
    {
338
10.9k
        if (sp->tbuflen < npixels)
339
0
        {
340
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
341
0
            return (0);
342
0
        }
343
10.9k
        tp = (uint32_t *)sp->tbuf;
344
10.9k
    }
345
11.6k
    _TIFFmemset((void *)tp, 0, (tmsize_t)((size_t)npixels * sizeof(tp[0])));
346
347
11.6k
    bp = (unsigned char *)tif->tif_rawcp;
348
11.6k
    cc = tif->tif_rawcc;
349
    /* get each byte string */
350
56.6k
    for (shft = 24; shft >= 0; shft -= 8)
351
45.5k
    {
352
138k
        for (i = 0; i < npixels && cc > 0;)
353
93.4k
        {
354
93.4k
            if (*bp >= 128)
355
30.0k
            { /* run */
356
30.0k
                if (cc < 2)
357
42
                    break;
358
29.9k
                rc = *bp++ + (2 - 128);
359
29.9k
                b = (uint32_t)*bp++ << shft;
360
29.9k
                cc -= 2;
361
664k
                while (rc-- && i < npixels)
362
634k
                    tp[i++] |= b;
363
29.9k
            }
364
63.4k
            else
365
63.4k
            {               /* non-run */
366
63.4k
                rc = *bp++; /* nul is noop */
367
249k
                while (--cc && rc-- && i < npixels)
368
186k
                    tp[i++] |= (uint32_t)*bp++ << shft;
369
63.4k
            }
370
93.4k
        }
371
45.5k
        if (i != npixels)
372
528
        {
373
528
            TIFFErrorExtR(tif, module,
374
528
                          "Not enough data at row %" PRIu32
375
528
                          " (short %" TIFF_SSIZE_FORMAT " pixels)",
376
528
                          tif->tif_dir.td_row, npixels - i);
377
528
            tif->tif_rawcp = (uint8_t *)bp;
378
528
            tif->tif_rawcc = cc;
379
528
            return (0);
380
528
        }
381
45.5k
    }
382
11.0k
    (*sp->tfunc)(sp, op, npixels);
383
11.0k
    tif->tif_rawcp = (uint8_t *)bp;
384
11.0k
    tif->tif_rawcc = cc;
385
11.0k
    return (1);
386
11.6k
}
387
388
/*
389
 * Decode a strip of pixels.  We break it into rows to
390
 * maintain synchrony with the encode algorithm, which
391
 * is row by row.
392
 */
393
static int LogLuvDecodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
394
1.53k
{
395
1.53k
    tmsize_t rowlen = TIFFScanlineSize(tif);
396
397
1.53k
    if (rowlen == 0)
398
0
        return 0;
399
400
1.53k
    assert(cc % rowlen == 0);
401
41.6k
    while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
402
40.1k
    {
403
40.1k
        bp += rowlen;
404
40.1k
        cc -= rowlen;
405
40.1k
    }
406
1.53k
    return (cc == 0);
407
1.53k
}
408
409
/*
410
 * Decode a tile of pixels.  We break it into rows to
411
 * maintain synchrony with the encode algorithm, which
412
 * is row by row.
413
 */
414
static int LogLuvDecodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
415
29
{
416
29
    tmsize_t rowlen = TIFFTileRowSize(tif);
417
418
29
    if (rowlen == 0)
419
0
        return 0;
420
421
29
    assert(cc % rowlen == 0);
422
213
    while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
423
184
    {
424
184
        bp += rowlen;
425
184
        cc -= rowlen;
426
184
    }
427
29
    return (cc == 0);
428
29
}
429
430
/*
431
 * Encode a row of 16-bit pixels.
432
 */
433
static int LogL16Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
434
0
{
435
0
    static const char module[] = "LogL16Encode";
436
0
    LogLuvState *sp = EncoderState(tif);
437
0
    int shft;
438
0
    tmsize_t i;
439
0
    tmsize_t j;
440
0
    tmsize_t npixels;
441
0
    uint8_t *op;
442
0
    int16_t *tp;
443
0
    int16_t b;
444
0
    tmsize_t occ;
445
0
    int rc = 0, mask;
446
0
    tmsize_t beg;
447
448
0
    (void)s;
449
0
    assert(s == 0);
450
0
    assert(sp != NULL);
451
0
    npixels = cc / sp->pixel_size;
452
453
0
    if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
454
0
        tp = (int16_t *)bp;
455
0
    else
456
0
    {
457
0
        tp = (int16_t *)sp->tbuf;
458
0
        if (sp->tbuflen < npixels)
459
0
        {
460
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
461
0
            return (0);
462
0
        }
463
0
        (*sp->tfunc)(sp, bp, npixels);
464
0
    }
465
    /* compress each byte string */
466
0
    op = tif->tif_rawcp;
467
0
    occ = tif->tif_rawdatasize - tif->tif_rawcc;
468
0
    for (shft = 8; shft >= 0; shft -= 8)
469
0
    {
470
0
        for (i = 0; i < npixels; i += rc)
471
0
        {
472
0
            if (occ < 4)
473
0
            {
474
0
                tif->tif_rawcp = op;
475
0
                tif->tif_rawcc = tif->tif_rawdatasize - occ;
476
0
                if (!TIFFFlushData1(tif))
477
0
                    return (0);
478
0
                op = tif->tif_rawcp;
479
0
                occ = tif->tif_rawdatasize - tif->tif_rawcc;
480
0
            }
481
0
            mask = 0xff << shft; /* find next run */
482
0
            for (beg = i; beg < npixels; beg += rc)
483
0
            {
484
0
                b = (int16_t)(tp[beg] & mask);
485
0
                rc = 1;
486
0
                while (rc < 127 + 2 && beg + rc < npixels &&
487
0
                       (tp[beg + rc] & mask) == b)
488
0
                    rc++;
489
0
                if (rc >= MINRUN)
490
0
                    break; /* long enough */
491
0
            }
492
0
            if (beg - i > 1 && beg - i < MINRUN)
493
0
            {
494
0
                b = (int16_t)(tp[i] & mask); /*check short run */
495
0
                j = i + 1;
496
0
                while ((tp[j++] & mask) == b)
497
0
                    if (j == beg)
498
0
                    {
499
0
                        *op++ = (uint8_t)(128 - 2 + j - i);
500
0
                        *op++ = (uint8_t)(b >> shft);
501
0
                        occ -= 2;
502
0
                        i = beg;
503
0
                        break;
504
0
                    }
505
0
            }
506
0
            while (i < beg)
507
0
            { /* write out non-run */
508
0
                if ((j = beg - i) > 127)
509
0
                    j = 127;
510
0
                if (occ < j + 3)
511
0
                {
512
0
                    tif->tif_rawcp = op;
513
0
                    tif->tif_rawcc = tif->tif_rawdatasize - occ;
514
0
                    if (!TIFFFlushData1(tif))
515
0
                        return (0);
516
0
                    op = tif->tif_rawcp;
517
0
                    occ = tif->tif_rawdatasize - tif->tif_rawcc;
518
0
                }
519
0
                *op++ = (uint8_t)j;
520
0
                occ--;
521
0
                while (j--)
522
0
                {
523
0
                    *op++ = (uint8_t)(tp[i++] >> shft & 0xff);
524
0
                    occ--;
525
0
                }
526
0
            }
527
0
            if (rc >= MINRUN)
528
0
            { /* write out run */
529
0
                *op++ = (uint8_t)(128 - 2 + rc);
530
0
                *op++ = (uint8_t)(tp[beg] >> shft & 0xff);
531
0
                occ -= 2;
532
0
            }
533
0
            else
534
0
                rc = 0;
535
0
        }
536
0
    }
537
0
    tif->tif_rawcp = op;
538
0
    tif->tif_rawcc = tif->tif_rawdatasize - occ;
539
540
0
    return (1);
541
0
}
542
543
/*
544
 * Encode a row of 24-bit pixels.
545
 */
546
static int LogLuvEncode24(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
547
0
{
548
0
    static const char module[] = "LogLuvEncode24";
549
0
    LogLuvState *sp = EncoderState(tif);
550
0
    tmsize_t i;
551
0
    tmsize_t npixels;
552
0
    tmsize_t occ;
553
0
    uint8_t *op;
554
0
    uint32_t *tp;
555
556
0
    (void)s;
557
0
    assert(s == 0);
558
0
    assert(sp != NULL);
559
0
    npixels = cc / sp->pixel_size;
560
561
0
    if (sp->user_datafmt == SGILOGDATAFMT_RAW)
562
0
        tp = (uint32_t *)bp;
563
0
    else
564
0
    {
565
0
        tp = (uint32_t *)sp->tbuf;
566
0
        if (sp->tbuflen < npixels)
567
0
        {
568
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
569
0
            return (0);
570
0
        }
571
0
        (*sp->tfunc)(sp, bp, npixels);
572
0
    }
573
    /* write out encoded pixels */
574
0
    op = tif->tif_rawcp;
575
0
    occ = tif->tif_rawdatasize - tif->tif_rawcc;
576
0
    for (i = npixels; i--;)
577
0
    {
578
0
        if (occ < 3)
579
0
        {
580
0
            tif->tif_rawcp = op;
581
0
            tif->tif_rawcc = tif->tif_rawdatasize - occ;
582
0
            if (!TIFFFlushData1(tif))
583
0
                return (0);
584
0
            op = tif->tif_rawcp;
585
0
            occ = tif->tif_rawdatasize - tif->tif_rawcc;
586
0
        }
587
0
        *op++ = (uint8_t)(*tp >> 16);
588
0
        *op++ = (uint8_t)(*tp >> 8 & 0xff);
589
0
        *op++ = (uint8_t)(*tp++ & 0xff);
590
0
        occ -= 3;
591
0
    }
592
0
    tif->tif_rawcp = op;
593
0
    tif->tif_rawcc = tif->tif_rawdatasize - occ;
594
595
0
    return (1);
596
0
}
597
598
/*
599
 * Encode a row of 32-bit pixels.
600
 */
601
static int LogLuvEncode32(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
602
0
{
603
0
    static const char module[] = "LogLuvEncode32";
604
0
    LogLuvState *sp = EncoderState(tif);
605
0
    int shft;
606
0
    tmsize_t i;
607
0
    tmsize_t j;
608
0
    tmsize_t npixels;
609
0
    uint8_t *op;
610
0
    uint32_t *tp;
611
0
    uint32_t b;
612
0
    tmsize_t occ;
613
0
    int rc = 0;
614
0
    tmsize_t beg;
615
616
0
    (void)s;
617
0
    assert(s == 0);
618
0
    assert(sp != NULL);
619
620
0
    npixels = cc / sp->pixel_size;
621
622
0
    if (sp->user_datafmt == SGILOGDATAFMT_RAW)
623
0
        tp = (uint32_t *)bp;
624
0
    else
625
0
    {
626
0
        tp = (uint32_t *)sp->tbuf;
627
0
        if (sp->tbuflen < npixels)
628
0
        {
629
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
630
0
            return (0);
631
0
        }
632
0
        (*sp->tfunc)(sp, bp, npixels);
633
0
    }
634
    /* compress each byte string */
635
0
    op = tif->tif_rawcp;
636
0
    occ = tif->tif_rawdatasize - tif->tif_rawcc;
637
0
    for (shft = 24; shft >= 0; shft -= 8)
638
0
    {
639
0
        const uint32_t mask = 0xffU << shft; /* find next run */
640
0
        for (i = 0; i < npixels; i += rc)
641
0
        {
642
0
            if (occ < 4)
643
0
            {
644
0
                tif->tif_rawcp = op;
645
0
                tif->tif_rawcc = tif->tif_rawdatasize - occ;
646
0
                if (!TIFFFlushData1(tif))
647
0
                    return (0);
648
0
                op = tif->tif_rawcp;
649
0
                occ = tif->tif_rawdatasize - tif->tif_rawcc;
650
0
            }
651
0
            for (beg = i; beg < npixels; beg += rc)
652
0
            {
653
0
                b = tp[beg] & mask;
654
0
                rc = 1;
655
0
                while (rc < 127 + 2 && beg + rc < npixels &&
656
0
                       (tp[beg + rc] & mask) == b)
657
0
                    rc++;
658
0
                if (rc >= MINRUN)
659
0
                    break; /* long enough */
660
0
            }
661
0
            if (beg - i > 1 && beg - i < MINRUN)
662
0
            {
663
0
                b = tp[i] & mask; /* check short run */
664
0
                j = i + 1;
665
0
                while ((tp[j++] & mask) == b)
666
0
                    if (j == beg)
667
0
                    {
668
0
                        *op++ = (uint8_t)(128 - 2 + j - i);
669
0
                        *op++ = (uint8_t)(b >> shft);
670
0
                        occ -= 2;
671
0
                        i = beg;
672
0
                        break;
673
0
                    }
674
0
            }
675
0
            while (i < beg)
676
0
            { /* write out non-run */
677
0
                if ((j = beg - i) > 127)
678
0
                    j = 127;
679
0
                if (occ < j + 3)
680
0
                {
681
0
                    tif->tif_rawcp = op;
682
0
                    tif->tif_rawcc = tif->tif_rawdatasize - occ;
683
0
                    if (!TIFFFlushData1(tif))
684
0
                        return (0);
685
0
                    op = tif->tif_rawcp;
686
0
                    occ = tif->tif_rawdatasize - tif->tif_rawcc;
687
0
                }
688
0
                *op++ = (uint8_t)j;
689
0
                occ--;
690
0
                while (j--)
691
0
                {
692
0
                    *op++ = (uint8_t)(tp[i++] >> shft & 0xff);
693
0
                    occ--;
694
0
                }
695
0
            }
696
0
            if (rc >= MINRUN)
697
0
            { /* write out run */
698
0
                *op++ = (uint8_t)(128 - 2 + rc);
699
0
                *op++ = (uint8_t)(tp[beg] >> shft & 0xff);
700
0
                occ -= 2;
701
0
            }
702
0
            else
703
0
                rc = 0;
704
0
        }
705
0
    }
706
0
    tif->tif_rawcp = op;
707
0
    tif->tif_rawcc = tif->tif_rawdatasize - occ;
708
709
0
    return (1);
710
0
}
711
712
/*
713
 * Encode a strip of pixels.  We break it into rows to
714
 * avoid encoding runs across row boundaries.
715
 */
716
static int LogLuvEncodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
717
0
{
718
0
    tmsize_t rowlen = TIFFScanlineSize(tif);
719
720
0
    if (rowlen == 0)
721
0
        return 0;
722
723
0
    assert(cc % rowlen == 0);
724
0
    while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
725
0
    {
726
0
        bp += rowlen;
727
0
        cc -= rowlen;
728
0
    }
729
0
    return (cc == 0);
730
0
}
731
732
/*
733
 * Encode a tile of pixels.  We break it into rows to
734
 * avoid encoding runs across row boundaries.
735
 */
736
static int LogLuvEncodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
737
0
{
738
0
    tmsize_t rowlen = TIFFTileRowSize(tif);
739
740
0
    if (rowlen == 0)
741
0
        return 0;
742
743
0
    assert(cc % rowlen == 0);
744
0
    while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
745
0
    {
746
0
        bp += rowlen;
747
0
        cc -= rowlen;
748
0
    }
749
0
    return (cc == 0);
750
0
}
751
752
/*
753
 * Encode/Decode functions for converting to and from user formats.
754
 */
755
756
#include "uvcode.h"
757
758
#ifndef UVSCALE
759
#define U_NEU 0.210526316
760
#define V_NEU 0.473684211
761
#define UVSCALE 410.
762
#endif
763
764
#ifndef M_LN2
765
#define M_LN2 0.69314718055994530942
766
#endif
767
#ifndef M_PI
768
#define M_PI 3.14159265358979323846
769
#endif
770
771
0
#define TIFF_RAND_MAX 32767
772
773
// From POSIX.1-2001 as an example of an implementation of rand()
774
static uint32_t _TIFFRand(void)
775
0
{
776
0
    static uint32_t nCounter = 0;
777
0
    if (!nCounter)
778
0
        nCounter = (uint32_t)(time(NULL) & UINT32_MAX);
779
0
    ++nCounter;
780
0
    uint32_t nCounterLocal =
781
0
        (uint32_t)(((uint64_t)(nCounter) * 1103515245U + 12345U) & UINT32_MAX);
782
0
    nCounter = nCounterLocal;
783
0
    return (nCounterLocal / 65536U) % (TIFF_RAND_MAX + 1);
784
0
}
785
786
static int tiff_itrunc(double x, int m)
787
0
{
788
0
    if (m == SGILOGENCODE_NODITHER)
789
0
        return (int)x;
790
0
    return (int)(x + _TIFFRand() * (1. / TIFF_RAND_MAX) - .5);
791
0
}
792
793
#if !LOGLUV_PUBLIC
794
static
795
#endif
796
    double LogL16toY(int p16) /* compute luminance from 16-bit LogL */
797
268k
{
798
268k
    int Le = p16 & 0x7fff;
799
268k
    double Y;
800
801
268k
    if (!Le)
802
51.7k
        return (0.);
803
216k
    Y = exp(M_LN2 / 256. * (Le + .5) - M_LN2 * 64.);
804
216k
    return (!(p16 & 0x8000) ? Y : -Y);
805
268k
}
806
807
#if !LOGLUV_PUBLIC
808
static
809
#endif
810
    int LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */
811
0
{
812
0
    if (Y >= 1.8371976e19)
813
0
        return (0x7fff);
814
0
    if (Y <= -1.8371976e19)
815
0
        return (0xffff);
816
0
    if (Y > 5.4136769e-20)
817
0
        return tiff_itrunc(256. * (log2(Y) + 64.), em);
818
0
    if (Y < -5.4136769e-20)
819
0
        return (~0x7fff | tiff_itrunc(256. * (log2(-Y) + 64.), em));
820
0
    return (0);
821
0
}
822
823
static void L16toY(LogLuvState *sp, uint8_t *op, tmsize_t n)
824
1.09k
{
825
1.09k
    int16_t *l16 = (int16_t *)sp->tbuf;
826
1.09k
    float *yp = (float *)op;
827
828
26.9k
    while (n-- > 0)
829
25.8k
        *yp++ = (float)LogL16toY(*l16++);
830
1.09k
}
831
832
static void L16toGry(LogLuvState *sp, uint8_t *op, tmsize_t n)
833
2.35k
{
834
2.35k
    int16_t *l16 = (int16_t *)sp->tbuf;
835
2.35k
    uint8_t *gp = (uint8_t *)op;
836
837
62.3k
    while (n-- > 0)
838
59.9k
    {
839
59.9k
        double Y = LogL16toY(*l16++);
840
59.9k
        *gp++ = (uint8_t)((Y <= 0.)   ? 0
841
59.9k
                          : (Y >= 1.) ? 255
842
10.1k
                                      : (int)(256. * sqrt(Y)));
843
59.9k
    }
844
2.35k
}
845
846
static void L16fromY(LogLuvState *sp, uint8_t *op, tmsize_t n)
847
0
{
848
0
    int16_t *l16 = (int16_t *)sp->tbuf;
849
0
    float *yp = (float *)op;
850
851
0
    while (n-- > 0)
852
0
        *l16++ = (int16_t)(LogL16fromY((double)*yp++, sp->encode_meth));
853
0
}
854
855
#if !LOGLUV_PUBLIC
856
static
857
#endif
858
    void XYZtoRGB24(float *xyz, uint8_t *rgb)
859
69.2k
{
860
69.2k
    double r, g, b;
861
    /* assume CCIR-709 primaries */
862
69.2k
    r = 2.690 * (double)xyz[0] + -1.276 * (double)xyz[1] +
863
69.2k
        -0.414 * (double)xyz[2];
864
69.2k
    g = -1.022 * (double)xyz[0] + 1.978 * (double)xyz[1] +
865
69.2k
        0.044 * (double)xyz[2];
866
69.2k
    b = 0.061 * (double)xyz[0] + -0.224 * (double)xyz[1] +
867
69.2k
        1.163 * (double)xyz[2];
868
    /* assume 2.0 gamma for speed */
869
    /* could use integer sqrt approx., but this is probably faster */
870
69.2k
    rgb[0] = (uint8_t)((r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256. * sqrt(r)));
871
69.2k
    rgb[1] = (uint8_t)((g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256. * sqrt(g)));
872
69.2k
    rgb[2] = (uint8_t)((b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256. * sqrt(b)));
873
69.2k
}
874
875
#if !LOGLUV_PUBLIC
876
static
877
#endif
878
    double LogL10toY(int p10) /* compute luminance from 10-bit LogL */
879
50.3k
{
880
50.3k
    if (p10 == 0)
881
15.0k
        return (0.);
882
35.3k
    return (exp(M_LN2 / 64. * (p10 + .5) - M_LN2 * 12.));
883
50.3k
}
884
885
#if !LOGLUV_PUBLIC
886
static
887
#endif
888
    int LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */
889
0
{
890
0
    if (Y >= 15.742)
891
0
        return (0x3ff);
892
0
    else if (Y <= .00024283)
893
0
        return (0);
894
0
    else
895
0
        return tiff_itrunc(64. * (log2(Y) + 12.), em);
896
0
}
897
898
0
#define NANGLES 100
899
#define uv2ang(u, v)                                                           \
900
0
    ((NANGLES * .499999999 / M_PI) * atan2((v) - V_NEU, (u) - U_NEU) +         \
901
0
     .5 * NANGLES)
902
903
static int oog_encode(double u, double v) /* encode out-of-gamut chroma */
904
0
{
905
0
    static int oog_table[NANGLES];
906
0
    static int initialized = 0;
907
0
    int i;
908
909
0
    if (!initialized)
910
0
    { /* set up perimeter table */
911
0
        double eps[NANGLES], ua, va, ang, epsa;
912
0
        int ui, vi, ustep;
913
0
        for (i = NANGLES; i--;)
914
0
            eps[i] = 2.;
915
0
        for (vi = UV_NVS; vi--;)
916
0
        {
917
0
            va = (double)UV_VSTART + ((double)vi + .5) * (double)UV_SQSIZ;
918
0
            ustep = uv_row[vi].nus - 1;
919
0
            if (vi == UV_NVS - 1 || vi == 0 || ustep <= 0)
920
0
                ustep = 1;
921
0
            for (ui = uv_row[vi].nus - 1; ui >= 0; ui -= ustep)
922
0
            {
923
0
                ua = (double)uv_row[vi].ustart +
924
0
                     ((double)ui + .5) * (double)UV_SQSIZ;
925
0
                ang = uv2ang(ua, va);
926
0
                i = (int)ang;
927
0
                epsa = fabs(ang - (i + .5));
928
0
                if (epsa < eps[i])
929
0
                {
930
0
                    oog_table[i] = uv_row[vi].ncum + ui;
931
0
                    eps[i] = epsa;
932
0
                }
933
0
            }
934
0
        }
935
0
        for (i = NANGLES; i--;) /* fill any holes */
936
0
            if (eps[i] > 1.5)
937
0
            {
938
0
                int i1, i2;
939
0
                for (i1 = 1; i1 < NANGLES / 2; i1++)
940
0
                    if (eps[(i + i1) % NANGLES] < 1.5)
941
0
                        break;
942
0
                for (i2 = 1; i2 < NANGLES / 2; i2++)
943
0
                    if (eps[(i + NANGLES - i2) % NANGLES] < 1.5)
944
0
                        break;
945
0
                if (i1 < i2)
946
0
                    oog_table[i] = oog_table[(i + i1) % NANGLES];
947
0
                else
948
0
                    oog_table[i] = oog_table[(i + NANGLES - i2) % NANGLES];
949
0
            }
950
0
        initialized = 1;
951
0
    }
952
0
    i = (int)uv2ang(u, v); /* look up hue angle */
953
0
    return (oog_table[i]);
954
0
}
955
956
#undef uv2ang
957
#undef NANGLES
958
959
#if !LOGLUV_PUBLIC
960
static
961
#endif
962
    int uv_encode(double u, double v, int em) /* encode (u',v') coordinates */
963
0
{
964
0
    unsigned int vi;
965
0
    int ui;
966
967
    /* check for NaN */
968
0
    if (isnan(u) || isnan(v))
969
0
    {
970
0
        u = U_NEU;
971
0
        v = V_NEU;
972
0
    }
973
974
0
    if ((double)v < (double)UV_VSTART)
975
0
        return oog_encode(u, v);
976
0
    vi = (unsigned int)tiff_itrunc(
977
0
        ((double)v - (double)UV_VSTART) * (1. / (double)UV_SQSIZ), em);
978
0
    if (vi >= UV_NVS)
979
0
        return oog_encode(u, v);
980
0
    if ((double)u < (double)uv_row[vi].ustart)
981
0
        return oog_encode(u, v);
982
0
    ui = tiff_itrunc(
983
0
        ((double)u - (double)uv_row[vi].ustart) * (1. / (double)UV_SQSIZ), em);
984
0
    if (ui >= uv_row[vi].nus)
985
0
        return oog_encode(u, v);
986
987
0
    return (uv_row[vi].ncum + ui);
988
0
}
989
990
#if !LOGLUV_PUBLIC
991
static
992
#endif
993
    int uv_decode(double *up, double *vp, int c) /* decode (u',v') index */
994
59.8k
{
995
59.8k
    unsigned int upper, lower;
996
59.8k
    int ui;
997
59.8k
    unsigned int vi;
998
999
59.8k
    if (c < 0 || c >= UV_NDIVS)
1000
5.02k
        return (-1);
1001
54.8k
    lower = 0; /* binary search */
1002
54.8k
    upper = UV_NVS;
1003
452k
    while (upper - lower > 1)
1004
399k
    {
1005
399k
        vi = (lower + upper) >> 1;
1006
399k
        ui = c - uv_row[vi].ncum;
1007
399k
        if (ui > 0)
1008
155k
            lower = vi;
1009
244k
        else if (ui < 0)
1010
242k
            upper = vi;
1011
2.18k
        else
1012
2.18k
        {
1013
2.18k
            lower = vi;
1014
2.18k
            break;
1015
2.18k
        }
1016
399k
    }
1017
54.8k
    vi = lower;
1018
54.8k
    ui = c - uv_row[vi].ncum;
1019
54.8k
    *up = (double)uv_row[vi].ustart + ((double)ui + .5) * (double)UV_SQSIZ;
1020
54.8k
    *vp = (double)UV_VSTART + ((double)vi + .5) * (double)UV_SQSIZ;
1021
54.8k
    return (0);
1022
59.8k
}
1023
1024
#if !LOGLUV_PUBLIC
1025
static
1026
#endif
1027
    void LogLuv24toXYZ(uint32_t p, float *XYZ)
1028
50.3k
{
1029
50.3k
    int Ce;
1030
50.3k
    double L, u, v, s, x, y;
1031
    /* decode luminance */
1032
50.3k
    L = LogL10toY(p >> 14 & 0x3ff);
1033
50.3k
    if (L <= 0.)
1034
15.0k
    {
1035
15.0k
        XYZ[0] = XYZ[1] = XYZ[2] = 0.;
1036
15.0k
        return;
1037
15.0k
    }
1038
    /* decode color */
1039
35.3k
    Ce = p & 0x3fff;
1040
35.3k
    if (uv_decode(&u, &v, Ce) < 0)
1041
3.19k
    {
1042
3.19k
        u = U_NEU;
1043
3.19k
        v = V_NEU;
1044
3.19k
    }
1045
35.3k
    s = 1. / (6. * u - 16. * v + 12.);
1046
35.3k
    x = 9. * u * s;
1047
35.3k
    y = 4. * v * s;
1048
    /* convert to XYZ */
1049
35.3k
    XYZ[0] = (float)(x / y * L);
1050
35.3k
    XYZ[1] = (float)L;
1051
35.3k
    XYZ[2] = (float)((1. - x - y) / y * L);
1052
35.3k
}
1053
1054
#if !LOGLUV_PUBLIC
1055
static
1056
#endif
1057
    uint32_t LogLuv24fromXYZ(float *XYZ, int em)
1058
0
{
1059
0
    int Le, Ce;
1060
0
    double u, v, s;
1061
    /* encode luminance */
1062
0
    Le = LogL10fromY((double)XYZ[1], em);
1063
    /* encode color */
1064
0
    s = (double)XYZ[0] + 15. * (double)XYZ[1] + 3. * (double)XYZ[2];
1065
0
    if (!Le || s <= 0.)
1066
0
    {
1067
0
        u = U_NEU;
1068
0
        v = V_NEU;
1069
0
    }
1070
0
    else
1071
0
    {
1072
0
        u = 4. * (double)XYZ[0] / s;
1073
0
        v = 9. * (double)XYZ[1] / s;
1074
0
    }
1075
0
    Ce = uv_encode(u, v, em);
1076
0
    if (Ce < 0) /* never happens */
1077
0
        Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
1078
    /* combine encodings */
1079
0
    return (uint32_t)Le << 14 | (uint32_t)Ce;
1080
0
}
1081
1082
static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1083
3.89k
{
1084
3.89k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1085
3.89k
    float *xyz = (float *)op;
1086
1087
34.0k
    while (n-- > 0)
1088
30.1k
    {
1089
30.1k
        LogLuv24toXYZ(*luv, xyz);
1090
30.1k
        xyz += 3;
1091
30.1k
        luv++;
1092
30.1k
    }
1093
3.89k
}
1094
1095
static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1096
12.7k
{
1097
12.7k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1098
12.7k
    int16_t *luv3 = (int16_t *)op;
1099
1100
37.2k
    while (n-- > 0)
1101
24.4k
    {
1102
24.4k
        double u, v;
1103
1104
24.4k
        *luv3++ = (int16_t)((*luv >> 12 & 0xffd) + 13314);
1105
24.4k
        if (uv_decode(&u, &v, *luv & 0x3fff) < 0)
1106
1.82k
        {
1107
1.82k
            u = U_NEU;
1108
1.82k
            v = V_NEU;
1109
1.82k
        }
1110
24.4k
        *luv3++ = (int16_t)(u * (1 << 15));
1111
24.4k
        *luv3++ = (int16_t)(v * (1 << 15));
1112
24.4k
        luv++;
1113
24.4k
    }
1114
12.7k
}
1115
1116
static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
1117
8.03k
{
1118
8.03k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1119
8.03k
    uint8_t *rgb = (uint8_t *)op;
1120
1121
28.2k
    while (n-- > 0)
1122
20.2k
    {
1123
20.2k
        float xyz[3];
1124
1125
20.2k
        LogLuv24toXYZ(*luv++, xyz);
1126
20.2k
        XYZtoRGB24(xyz, rgb);
1127
20.2k
        rgb += 3;
1128
20.2k
    }
1129
8.03k
}
1130
1131
static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1132
0
{
1133
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1134
0
    float *xyz = (float *)op;
1135
1136
0
    while (n-- > 0)
1137
0
    {
1138
0
        *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth);
1139
0
        xyz += 3;
1140
0
    }
1141
0
}
1142
1143
static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1144
0
{
1145
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1146
0
    int16_t *luv3 = (int16_t *)op;
1147
1148
0
    while (n-- > 0)
1149
0
    {
1150
0
        int Le, Ce;
1151
1152
0
        if (luv3[0] <= 0)
1153
0
            Le = 0;
1154
0
        else if (luv3[0] >= (1 << 12) + 3314)
1155
0
            Le = (1 << 10) - 1;
1156
0
        else if (sp->encode_meth == SGILOGENCODE_NODITHER)
1157
0
            Le = (luv3[0] - 3314) >> 2;
1158
0
        else
1159
0
            Le = tiff_itrunc(.25 * (luv3[0] - 3314.), sp->encode_meth);
1160
1161
0
        Ce = uv_encode((luv3[1] + .5) / (1 << 15), (luv3[2] + .5) / (1 << 15),
1162
0
                       sp->encode_meth);
1163
0
        if (Ce < 0) /* never happens */
1164
0
            Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
1165
0
        *luv++ = (uint32_t)Le << 14 | (uint32_t)Ce;
1166
0
        luv3 += 3;
1167
0
    }
1168
0
}
1169
1170
#if !LOGLUV_PUBLIC
1171
static
1172
#endif
1173
    void LogLuv32toXYZ(uint32_t p, float *XYZ)
1174
182k
{
1175
182k
    double L, u, v, s, x, y;
1176
    /* decode luminance */
1177
182k
    L = LogL16toY((int)p >> 16);
1178
182k
    if (L <= 0.)
1179
135k
    {
1180
135k
        XYZ[0] = XYZ[1] = XYZ[2] = 0.;
1181
135k
        return;
1182
135k
    }
1183
    /* decode color */
1184
46.7k
    u = 1. / UVSCALE * ((p >> 8 & 0xff) + .5);
1185
46.7k
    v = 1. / UVSCALE * ((p & 0xff) + .5);
1186
46.7k
    s = 1. / (6. * u - 16. * v + 12.);
1187
46.7k
    x = 9. * u * s;
1188
46.7k
    y = 4. * v * s;
1189
    /* convert to XYZ */
1190
46.7k
    XYZ[0] = (float)(x / y * L);
1191
46.7k
    XYZ[1] = (float)L;
1192
46.7k
    XYZ[2] = (float)((1. - x - y) / y * L);
1193
46.7k
}
1194
1195
#if !LOGLUV_PUBLIC
1196
static
1197
#endif
1198
    uint32_t LogLuv32fromXYZ(float *XYZ, int em)
1199
0
{
1200
0
    unsigned int Le, ue, ve;
1201
0
    double u, v, s;
1202
    /* encode luminance */
1203
0
    Le = (unsigned int)LogL16fromY((double)XYZ[1], em);
1204
    /* encode color */
1205
0
    s = (double)XYZ[0] + 15. * (double)XYZ[1] + 3. * (double)XYZ[2];
1206
0
    if (!Le || s <= 0.)
1207
0
    {
1208
0
        u = U_NEU;
1209
0
        v = V_NEU;
1210
0
    }
1211
0
    else
1212
0
    {
1213
0
        u = 4. * (double)XYZ[0] / s;
1214
0
        v = 9. * (double)XYZ[1] / s;
1215
0
    }
1216
0
    if (u <= 0.)
1217
0
        ue = 0;
1218
0
    else
1219
0
        ue = (unsigned int)tiff_itrunc(UVSCALE * u, em);
1220
0
    if (ue > 255)
1221
0
        ue = 255;
1222
0
    if (v <= 0.)
1223
0
        ve = 0;
1224
0
    else
1225
0
        ve = (unsigned int)tiff_itrunc(UVSCALE * v, em);
1226
0
    if (ve > 255)
1227
0
        ve = 255;
1228
    /* combine encodings */
1229
0
    return (Le << 16 | ue << 8 | ve);
1230
0
}
1231
1232
static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1233
6.43k
{
1234
6.43k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1235
6.43k
    float *xyz = (float *)op;
1236
1237
140k
    while (n-- > 0)
1238
133k
    {
1239
133k
        LogLuv32toXYZ(*luv++, xyz);
1240
133k
        xyz += 3;
1241
133k
    }
1242
6.43k
}
1243
1244
static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1245
1.52k
{
1246
1.52k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1247
1.52k
    int16_t *luv3 = (int16_t *)op;
1248
1249
6.68k
    while (n-- > 0)
1250
5.15k
    {
1251
5.15k
        double u, v;
1252
1253
5.15k
        *luv3++ = (int16_t)(*luv >> 16);
1254
5.15k
        u = 1. / UVSCALE * ((*luv >> 8 & 0xff) + .5);
1255
5.15k
        v = 1. / UVSCALE * ((*luv & 0xff) + .5);
1256
5.15k
        *luv3++ = (int16_t)(u * (1 << 15));
1257
5.15k
        *luv3++ = (int16_t)(v * (1 << 15));
1258
5.15k
        luv++;
1259
5.15k
    }
1260
1.52k
}
1261
1262
static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
1263
2.49k
{
1264
2.49k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1265
2.49k
    uint8_t *rgb = (uint8_t *)op;
1266
1267
51.4k
    while (n-- > 0)
1268
48.9k
    {
1269
48.9k
        float xyz[3];
1270
1271
48.9k
        LogLuv32toXYZ(*luv++, xyz);
1272
48.9k
        XYZtoRGB24(xyz, rgb);
1273
48.9k
        rgb += 3;
1274
48.9k
    }
1275
2.49k
}
1276
1277
static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1278
0
{
1279
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1280
0
    float *xyz = (float *)op;
1281
1282
0
    while (n-- > 0)
1283
0
    {
1284
0
        *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth);
1285
0
        xyz += 3;
1286
0
    }
1287
0
}
1288
1289
static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1290
0
{
1291
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1292
0
    int16_t *luv3 = (int16_t *)op;
1293
1294
0
    if (sp->encode_meth == SGILOGENCODE_NODITHER)
1295
0
    {
1296
0
        while (n-- > 0)
1297
0
        {
1298
0
            *luv++ =
1299
0
                (uint32_t)luv3[0] << 16 |
1300
0
                ((uint32_t)luv3[1] * (uint32_t)(UVSCALE + .5) >> 7 & 0xff00) |
1301
0
                ((uint32_t)luv3[2] * (uint32_t)(UVSCALE + .5) >> 15 & 0xff);
1302
0
            luv3 += 3;
1303
0
        }
1304
0
        return;
1305
0
    }
1306
0
    while (n-- > 0)
1307
0
    {
1308
0
        *luv++ =
1309
0
            (uint32_t)luv3[0] << 16 |
1310
0
            (tiff_itrunc(luv3[1] * (UVSCALE / (1 << 15)), sp->encode_meth)
1311
0
                 << 8 &
1312
0
             0xff00) |
1313
0
            (tiff_itrunc(luv3[2] * (UVSCALE / (1 << 15)), sp->encode_meth) &
1314
0
             0xff);
1315
0
        luv3 += 3;
1316
0
    }
1317
0
}
1318
1319
static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n)
1320
1.78k
{
1321
1.78k
    (void)sp;
1322
1.78k
    (void)op;
1323
1.78k
    (void)n;
1324
1.78k
}
1325
1326
static int LogL16GuessDataFmt(TIFFDirectory *td)
1327
131
{
1328
256
#define PACK(s, b, f) (((b) << 6) | ((s) << 3) | (f))
1329
131
    switch (
1330
131
        PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat))
1331
131
    {
1332
47
        case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
1333
47
            return (SGILOGDATAFMT_FLOAT);
1334
0
        case PACK(1, 16, SAMPLEFORMAT_VOID):
1335
2
        case PACK(1, 16, SAMPLEFORMAT_INT):
1336
22
        case PACK(1, 16, SAMPLEFORMAT_UINT):
1337
22
            return (SGILOGDATAFMT_16BIT);
1338
0
        case PACK(1, 8, SAMPLEFORMAT_VOID):
1339
54
        case PACK(1, 8, SAMPLEFORMAT_UINT):
1340
54
            return (SGILOGDATAFMT_8BIT);
1341
8
        default:
1342
8
            break;
1343
131
    }
1344
8
#undef PACK
1345
8
    return (SGILOGDATAFMT_UNKNOWN);
1346
131
}
1347
1348
static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2)
1349
1.42k
{
1350
1.42k
    return _TIFFMultiplySSize(NULL, m1, m2, NULL);
1351
1.42k
}
1352
1353
static int LogL16InitState(TIFF *tif)
1354
238
{
1355
238
    static const char module[] = "LogL16InitState";
1356
238
    TIFFDirectory *td = &tif->tif_dir;
1357
238
    LogLuvState *sp = DecoderState(tif);
1358
1359
238
    assert(sp != NULL);
1360
238
    assert(td->td_photometric == PHOTOMETRIC_LOGL);
1361
1362
238
    if (td->td_samplesperpixel != 1)
1363
49
    {
1364
49
        TIFFErrorExtR(tif, module,
1365
49
                      "Sorry, can not handle LogL image with %s=%" PRIu16,
1366
49
                      "Samples/pixel", td->td_samplesperpixel);
1367
49
        return 0;
1368
49
    }
1369
1370
    /* for some reason, we can't do this in TIFFInitLogL16 */
1371
189
    if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
1372
131
        sp->user_datafmt = LogL16GuessDataFmt(td);
1373
189
    switch (sp->user_datafmt)
1374
189
    {
1375
47
        case SGILOGDATAFMT_FLOAT:
1376
47
            sp->pixel_size = sizeof(float);
1377
47
            break;
1378
22
        case SGILOGDATAFMT_16BIT:
1379
22
            sp->pixel_size = sizeof(int16_t);
1380
22
            break;
1381
112
        case SGILOGDATAFMT_8BIT:
1382
112
            sp->pixel_size = sizeof(uint8_t);
1383
112
            break;
1384
8
        default:
1385
8
            TIFFErrorExtR(tif, module,
1386
8
                          "No support for converting user data format to LogL");
1387
8
            return (0);
1388
189
    }
1389
181
    if (isTiled(tif))
1390
3
        sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
1391
178
    else if (td->td_rowsperstrip < td->td_imagelength)
1392
2
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
1393
176
    else
1394
176
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
1395
181
    if (multiply_ms(sp->tbuflen, sizeof(int16_t)) == 0 ||
1396
181
        (sp->tbuf = (uint8_t *)_TIFFmallocExt(
1397
181
             tif, (tmsize_t)((size_t)sp->tbuflen * sizeof(int16_t)))) == NULL)
1398
0
    {
1399
0
        TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
1400
0
        return (0);
1401
0
    }
1402
181
    return (1);
1403
181
}
1404
1405
static int LogLuvGuessDataFmt(TIFFDirectory *td)
1406
524
{
1407
524
    int guess;
1408
1409
    /*
1410
     * If the user didn't tell us their datafmt,
1411
     * take our best guess from the bitspersample.
1412
     */
1413
1.11k
#define PACK(a, b) (((a) << 3) | (b))
1414
524
    switch (PACK(td->td_bitspersample, td->td_sampleformat))
1415
524
    {
1416
116
        case PACK(32, SAMPLEFORMAT_IEEEFP):
1417
116
            guess = SGILOGDATAFMT_FLOAT;
1418
116
            break;
1419
11
        case PACK(32, SAMPLEFORMAT_VOID):
1420
99
        case PACK(32, SAMPLEFORMAT_UINT):
1421
143
        case PACK(32, SAMPLEFORMAT_INT):
1422
143
            guess = SGILOGDATAFMT_RAW;
1423
143
            break;
1424
0
        case PACK(16, SAMPLEFORMAT_VOID):
1425
14
        case PACK(16, SAMPLEFORMAT_INT):
1426
166
        case PACK(16, SAMPLEFORMAT_UINT):
1427
166
            guess = SGILOGDATAFMT_16BIT;
1428
166
            break;
1429
0
        case PACK(8, SAMPLEFORMAT_VOID):
1430
39
        case PACK(8, SAMPLEFORMAT_UINT):
1431
39
            guess = SGILOGDATAFMT_8BIT;
1432
39
            break;
1433
60
        default:
1434
60
            guess = SGILOGDATAFMT_UNKNOWN;
1435
60
            break;
1436
524
#undef PACK
1437
524
    }
1438
    /*
1439
     * Double-check samples per pixel.
1440
     */
1441
524
    switch (td->td_samplesperpixel)
1442
524
    {
1443
67
        case 1:
1444
67
            if (guess != SGILOGDATAFMT_RAW)
1445
12
                guess = SGILOGDATAFMT_UNKNOWN;
1446
67
            break;
1447
235
        case 3:
1448
235
            if (guess == SGILOGDATAFMT_RAW)
1449
15
                guess = SGILOGDATAFMT_UNKNOWN;
1450
235
            break;
1451
222
        default:
1452
222
            guess = SGILOGDATAFMT_UNKNOWN;
1453
222
            break;
1454
524
    }
1455
524
    return (guess);
1456
524
}
1457
1458
static int LogLuvInitState(TIFF *tif)
1459
792
{
1460
792
    static const char module[] = "LogLuvInitState";
1461
792
    TIFFDirectory *td = &tif->tif_dir;
1462
792
    LogLuvState *sp = DecoderState(tif);
1463
1464
792
    assert(sp != NULL);
1465
792
    assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
1466
1467
    /* for some reason, we can't do this in TIFFInitLogLuv */
1468
792
    if (td->td_planarconfig != PLANARCONFIG_CONTIG)
1469
3
    {
1470
3
        TIFFErrorExtR(tif, module,
1471
3
                      "SGILog compression cannot handle non-contiguous data");
1472
3
        return (0);
1473
3
    }
1474
789
    if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
1475
524
        sp->user_datafmt = LogLuvGuessDataFmt(td);
1476
789
    switch (sp->user_datafmt)
1477
789
    {
1478
90
        case SGILOGDATAFMT_FLOAT:
1479
90
            sp->pixel_size = 3 * sizeof(float);
1480
90
            break;
1481
119
        case SGILOGDATAFMT_16BIT:
1482
119
            sp->pixel_size = 3 * sizeof(int16_t);
1483
119
            break;
1484
55
        case SGILOGDATAFMT_RAW:
1485
55
            sp->pixel_size = sizeof(uint32_t);
1486
55
            break;
1487
267
        case SGILOGDATAFMT_8BIT:
1488
267
            sp->pixel_size = 3 * sizeof(uint8_t);
1489
267
            break;
1490
258
        default:
1491
258
            TIFFErrorExtR(
1492
258
                tif, module,
1493
258
                "No support for converting user data format to LogLuv");
1494
258
            return (0);
1495
789
    }
1496
531
    if (isTiled(tif))
1497
13
        sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
1498
518
    else if (td->td_rowsperstrip < td->td_imagelength)
1499
4
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
1500
514
    else
1501
514
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
1502
531
    if (multiply_ms(sp->tbuflen, sizeof(uint32_t)) == 0 ||
1503
531
        (sp->tbuf = (uint8_t *)_TIFFmallocExt(
1504
531
             tif, (tmsize_t)((size_t)sp->tbuflen * sizeof(uint32_t)))) == NULL)
1505
0
    {
1506
0
        TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
1507
0
        return (0);
1508
0
    }
1509
531
    return (1);
1510
531
}
1511
1512
static int LogLuvFixupTags(TIFF *tif)
1513
10.8k
{
1514
10.8k
    (void)tif;
1515
10.8k
    return (1);
1516
10.8k
}
1517
1518
static int LogLuvSetupDecode(TIFF *tif)
1519
1.19k
{
1520
1.19k
    static const char module[] = "LogLuvSetupDecode";
1521
1.19k
    LogLuvState *sp = DecoderState(tif);
1522
1.19k
    TIFFDirectory *td = &tif->tif_dir;
1523
1524
1.19k
    tif->tif_postdecode = _TIFFNoPostDecode;
1525
1.19k
    switch (td->td_photometric)
1526
1.19k
    {
1527
792
        case PHOTOMETRIC_LOGLUV:
1528
792
            if (!LogLuvInitState(tif))
1529
261
                break;
1530
531
            if (td->td_compression == COMPRESSION_SGILOG24)
1531
282
            {
1532
282
                tif->tif_decoderow = LogLuvDecode24;
1533
282
                switch (sp->user_datafmt)
1534
282
                {
1535
33
                    case SGILOGDATAFMT_FLOAT:
1536
33
                        sp->tfunc = Luv24toXYZ;
1537
33
                        break;
1538
89
                    case SGILOGDATAFMT_16BIT:
1539
89
                        sp->tfunc = Luv24toLuv48;
1540
89
                        break;
1541
141
                    case SGILOGDATAFMT_8BIT:
1542
141
                        sp->tfunc = Luv24toRGB;
1543
141
                        break;
1544
19
                    default:
1545
19
                        break;
1546
282
                }
1547
282
            }
1548
249
            else
1549
249
            {
1550
249
                tif->tif_decoderow = LogLuvDecode32;
1551
249
                switch (sp->user_datafmt)
1552
249
                {
1553
57
                    case SGILOGDATAFMT_FLOAT:
1554
57
                        sp->tfunc = Luv32toXYZ;
1555
57
                        break;
1556
30
                    case SGILOGDATAFMT_16BIT:
1557
30
                        sp->tfunc = Luv32toLuv48;
1558
30
                        break;
1559
126
                    case SGILOGDATAFMT_8BIT:
1560
126
                        sp->tfunc = Luv32toRGB;
1561
126
                        break;
1562
36
                    default:
1563
36
                        break;
1564
249
                }
1565
249
            }
1566
531
            return (1);
1567
238
        case PHOTOMETRIC_LOGL:
1568
238
            if (!LogL16InitState(tif))
1569
57
                break;
1570
181
            tif->tif_decoderow = LogL16Decode;
1571
181
            switch (sp->user_datafmt)
1572
181
            {
1573
47
                case SGILOGDATAFMT_FLOAT:
1574
47
                    sp->tfunc = L16toY;
1575
47
                    break;
1576
112
                case SGILOGDATAFMT_8BIT:
1577
112
                    sp->tfunc = L16toGry;
1578
112
                    break;
1579
22
                default:
1580
22
                    break;
1581
181
            }
1582
181
            return (1);
1583
168
        default:
1584
168
            TIFFErrorExtR(tif, module,
1585
168
                          "Inappropriate photometric interpretation %" PRIu16
1586
168
                          " for SGILog compression; %s",
1587
168
                          td->td_photometric, "must be either LogLUV or LogL");
1588
168
            break;
1589
1.19k
    }
1590
486
    return (0);
1591
1.19k
}
1592
1593
static int LogLuvSetupEncode(TIFF *tif)
1594
0
{
1595
0
    static const char module[] = "LogLuvSetupEncode";
1596
0
    LogLuvState *sp = EncoderState(tif);
1597
0
    TIFFDirectory *td = &tif->tif_dir;
1598
1599
0
    switch (td->td_photometric)
1600
0
    {
1601
0
        case PHOTOMETRIC_LOGLUV:
1602
0
            if (!LogLuvInitState(tif))
1603
0
                return (0);
1604
0
            if (td->td_compression == COMPRESSION_SGILOG24)
1605
0
            {
1606
0
                tif->tif_encoderow = LogLuvEncode24;
1607
0
                switch (sp->user_datafmt)
1608
0
                {
1609
0
                    case SGILOGDATAFMT_FLOAT:
1610
0
                        sp->tfunc = Luv24fromXYZ;
1611
0
                        break;
1612
0
                    case SGILOGDATAFMT_16BIT:
1613
0
                        sp->tfunc = Luv24fromLuv48;
1614
0
                        break;
1615
0
                    case SGILOGDATAFMT_RAW:
1616
0
                        break;
1617
0
                    default:
1618
0
                        goto notsupported;
1619
0
                }
1620
0
            }
1621
0
            else
1622
0
            {
1623
0
                tif->tif_encoderow = LogLuvEncode32;
1624
0
                switch (sp->user_datafmt)
1625
0
                {
1626
0
                    case SGILOGDATAFMT_FLOAT:
1627
0
                        sp->tfunc = Luv32fromXYZ;
1628
0
                        break;
1629
0
                    case SGILOGDATAFMT_16BIT:
1630
0
                        sp->tfunc = Luv32fromLuv48;
1631
0
                        break;
1632
0
                    case SGILOGDATAFMT_RAW:
1633
0
                        break;
1634
0
                    default:
1635
0
                        goto notsupported;
1636
0
                }
1637
0
            }
1638
0
            break;
1639
0
        case PHOTOMETRIC_LOGL:
1640
0
            if (!LogL16InitState(tif))
1641
0
                return (0);
1642
0
            tif->tif_encoderow = LogL16Encode;
1643
0
            switch (sp->user_datafmt)
1644
0
            {
1645
0
                case SGILOGDATAFMT_FLOAT:
1646
0
                    sp->tfunc = L16fromY;
1647
0
                    break;
1648
0
                case SGILOGDATAFMT_16BIT:
1649
0
                    break;
1650
0
                default:
1651
0
                    goto notsupported;
1652
0
            }
1653
0
            break;
1654
0
        default:
1655
0
            TIFFErrorExtR(tif, module,
1656
0
                          "Inappropriate photometric interpretation %" PRIu16
1657
0
                          " for SGILog compression; %s",
1658
0
                          td->td_photometric, "must be either LogLUV or LogL");
1659
0
            return (0);
1660
0
    }
1661
0
    sp->encoder_state = 1;
1662
0
    return (1);
1663
0
notsupported:
1664
0
    TIFFErrorExtR(tif, module,
1665
0
                  "SGILog compression supported only for %s, or raw data",
1666
0
                  td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
1667
0
    return (0);
1668
0
}
1669
1670
static void LogLuvClose(TIFF *tif)
1671
0
{
1672
0
    LogLuvState *sp = (LogLuvState *)tif->tif_data;
1673
0
    TIFFDirectory *td = &tif->tif_dir;
1674
1675
0
    assert(sp != 0);
1676
    /*
1677
     * For consistency, we always want to write out the same
1678
     * bitspersample and sampleformat for our TIFF file,
1679
     * regardless of the data format being used by the application.
1680
     * Since this routine is called after tags have been set but
1681
     * before they have been recorded in the file, we reset them here.
1682
     * Note: this is really a nasty approach. See PixarLogClose
1683
     */
1684
0
    if (sp->encoder_state)
1685
0
    {
1686
        /* See PixarLogClose. Might avoid issues with tags whose size depends
1687
         * on those below, but not completely sure this is enough. */
1688
0
        td->td_samplesperpixel =
1689
0
            (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
1690
0
        td->td_bitspersample = 16;
1691
0
        td->td_sampleformat = SAMPLEFORMAT_INT;
1692
0
    }
1693
0
}
1694
1695
static void LogLuvCleanup(TIFF *tif)
1696
10.9k
{
1697
10.9k
    LogLuvState *sp = (LogLuvState *)tif->tif_data;
1698
1699
10.9k
    assert(sp != 0);
1700
1701
10.9k
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
1702
10.9k
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
1703
1704
10.9k
    if (sp->tbuf)
1705
712
        _TIFFfreeExt(tif, sp->tbuf);
1706
10.9k
    _TIFFfreeExt(tif, sp);
1707
10.9k
    tif->tif_data = NULL;
1708
1709
10.9k
    _TIFFSetDefaultCompressionState(tif);
1710
10.9k
}
1711
1712
static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap)
1713
73.4k
{
1714
73.4k
    static const char module[] = "LogLuvVSetField";
1715
73.4k
    LogLuvState *sp = DecoderState(tif);
1716
73.4k
    int bps, fmt;
1717
1718
73.4k
    switch (tag)
1719
73.4k
    {
1720
892
        case TIFFTAG_SGILOGDATAFMT:
1721
892
            sp->user_datafmt = (int)va_arg(ap, int);
1722
            /*
1723
             * Tweak the TIFF header so that the rest of libtiff knows what
1724
             * size of data will be passed between app and library, and
1725
             * assume that the app knows what it is doing and is not
1726
             * confused by these header manipulations...
1727
             */
1728
892
            switch (sp->user_datafmt)
1729
892
            {
1730
0
                case SGILOGDATAFMT_FLOAT:
1731
0
                    bps = 32;
1732
0
                    fmt = SAMPLEFORMAT_IEEEFP;
1733
0
                    break;
1734
0
                case SGILOGDATAFMT_16BIT:
1735
0
                    bps = 16;
1736
0
                    fmt = SAMPLEFORMAT_INT;
1737
0
                    break;
1738
0
                case SGILOGDATAFMT_RAW:
1739
0
                    bps = 32;
1740
0
                    fmt = SAMPLEFORMAT_UINT;
1741
0
                    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1742
0
                    break;
1743
892
                case SGILOGDATAFMT_8BIT:
1744
892
                    bps = 8;
1745
892
                    fmt = SAMPLEFORMAT_UINT;
1746
892
                    break;
1747
0
                default:
1748
0
                    TIFFErrorExtR(
1749
0
                        tif, tif->tif_name,
1750
0
                        "Unknown data format %d for LogLuv compression",
1751
0
                        sp->user_datafmt);
1752
0
                    return (0);
1753
892
            }
1754
892
            TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1755
892
            TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt);
1756
            /*
1757
             * Must recalculate sizes should bits/sample change.
1758
             */
1759
892
            tif->tif_dir.td_tilesize =
1760
892
                isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)-1;
1761
892
            tif->tif_dir.td_scanlinesize = TIFFScanlineSize(tif);
1762
892
            return (1);
1763
0
        case TIFFTAG_SGILOGENCODE:
1764
0
            sp->encode_meth = (int)va_arg(ap, int);
1765
0
            if (sp->encode_meth != SGILOGENCODE_NODITHER &&
1766
0
                sp->encode_meth != SGILOGENCODE_RANDITHER)
1767
0
            {
1768
0
                TIFFErrorExtR(tif, module,
1769
0
                              "Unknown encoding %d for LogLuv compression",
1770
0
                              sp->encode_meth);
1771
0
                return (0);
1772
0
            }
1773
0
            return (1);
1774
72.5k
        default:
1775
72.5k
            return (*sp->vsetparent)(tif, tag, ap);
1776
73.4k
    }
1777
73.4k
}
1778
1779
static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap)
1780
106k
{
1781
106k
    LogLuvState *sp = (LogLuvState *)tif->tif_data;
1782
1783
106k
    switch (tag)
1784
106k
    {
1785
0
        case TIFFTAG_SGILOGDATAFMT:
1786
0
            *va_arg(ap, int *) = sp->user_datafmt;
1787
0
            return (1);
1788
106k
        default:
1789
106k
            return (*sp->vgetparent)(tif, tag, ap);
1790
106k
    }
1791
106k
}
1792
1793
static const TIFFField LogLuvFields[] = {
1794
    {TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
1795
     TRUE, FALSE, "SGILogDataFmt", NULL},
1796
    {TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
1797
     TRUE, FALSE, "SGILogEncode", NULL}};
1798
1799
int TIFFInitSGILog(TIFF *tif, int scheme)
1800
10.9k
{
1801
10.9k
    static const char module[] = "TIFFInitSGILog";
1802
10.9k
    LogLuvState *sp;
1803
1804
10.9k
    assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
1805
1806
    /*
1807
     * Merge codec-specific tag information.
1808
     */
1809
10.9k
    if (!_TIFFMergeFields(tif, LogLuvFields, TIFFArrayCount(LogLuvFields)))
1810
0
    {
1811
0
        TIFFErrorExtR(tif, module, "Merging SGILog codec-specific tags failed");
1812
0
        return 0;
1813
0
    }
1814
1815
    /*
1816
     * Allocate state block so tag methods have storage to record values.
1817
     */
1818
10.9k
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LogLuvState));
1819
10.9k
    if (tif->tif_data == NULL)
1820
0
        goto bad;
1821
10.9k
    sp = (LogLuvState *)tif->tif_data;
1822
10.9k
    _TIFFmemset((void *)sp, 0, sizeof(*sp));
1823
10.9k
    sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
1824
10.9k
    sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? SGILOGENCODE_RANDITHER
1825
10.9k
                                                       : SGILOGENCODE_NODITHER;
1826
10.9k
    sp->tfunc = _logLuvNop;
1827
1828
    /*
1829
     * Install codec methods.
1830
     * NB: tif_decoderow & tif_encoderow are filled
1831
     *     in at setup time.
1832
     */
1833
10.9k
    tif->tif_fixuptags = LogLuvFixupTags;
1834
10.9k
    tif->tif_setupdecode = LogLuvSetupDecode;
1835
10.9k
    tif->tif_decodestrip = LogLuvDecodeStrip;
1836
10.9k
    tif->tif_decodetile = LogLuvDecodeTile;
1837
10.9k
    tif->tif_setupencode = LogLuvSetupEncode;
1838
10.9k
    tif->tif_encodestrip = LogLuvEncodeStrip;
1839
10.9k
    tif->tif_encodetile = LogLuvEncodeTile;
1840
10.9k
    tif->tif_close = LogLuvClose;
1841
10.9k
    tif->tif_cleanup = LogLuvCleanup;
1842
1843
    /*
1844
     * Override parent get/set field methods.
1845
     */
1846
10.9k
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
1847
10.9k
    tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */
1848
10.9k
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
1849
10.9k
    tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */
1850
1851
10.9k
    return (1);
1852
0
bad:
1853
0
    TIFFErrorExtR(tif, module, "%s: No space for LogLuv state block",
1854
0
                  tif->tif_name);
1855
0
    return (0);
1856
10.9k
}
1857
#endif /* LOGLUV_SUPPORT */