Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/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
57.8k
#define DecoderState(tif) ((LogLuvState *)(tif)->tif_data)
176
0
#define EncoderState(tif) ((LogLuvState *)(tif)->tif_data)
177
178
1.78k
#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
13.3k
{
187
13.3k
    static const char module[] = "LogL16Decode";
188
13.3k
    LogLuvState *sp = DecoderState(tif);
189
13.3k
    int shft;
190
13.3k
    tmsize_t i;
191
13.3k
    tmsize_t npixels;
192
13.3k
    unsigned char *bp;
193
13.3k
    int16_t *tp;
194
13.3k
    int16_t b;
195
13.3k
    tmsize_t cc;
196
13.3k
    int rc;
197
198
13.3k
    (void)s;
199
13.3k
    assert(s == 0);
200
13.3k
    assert(sp != NULL);
201
202
13.3k
    npixels = occ / sp->pixel_size;
203
204
13.3k
    if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
205
0
        tp = (int16_t *)op;
206
13.3k
    else
207
13.3k
    {
208
13.3k
        if (sp->tbuflen < npixels)
209
0
        {
210
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
211
0
            return (0);
212
0
        }
213
13.3k
        tp = (int16_t *)sp->tbuf;
214
13.3k
    }
215
13.3k
    _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0]));
216
217
13.3k
    bp = (unsigned char *)tif->tif_rawcp;
218
13.3k
    cc = tif->tif_rawcc;
219
    /* get each byte string */
220
39.7k
    for (shft = 8; shft >= 0; shft -= 8)
221
26.6k
    {
222
158k
        for (i = 0; i < npixels && cc > 0;)
223
131k
        {
224
131k
            if (*bp >= 128)
225
52.2k
            { /* run */
226
52.2k
                if (cc < 2)
227
31
                    break;
228
52.2k
                rc = *bp++ + (2 - 128);
229
52.2k
                b = (int16_t)(*bp++ << shft);
230
52.2k
                cc -= 2;
231
1.87M
                while (rc-- && i < npixels)
232
1.82M
                    tp[i++] |= b;
233
52.2k
            }
234
79.2k
            else
235
79.2k
            {               /* non-run */
236
79.2k
                rc = *bp++; /* nul is noop */
237
602k
                while (--cc && rc-- && i < npixels)
238
522k
                    tp[i++] |= (int16_t)*bp++ << shft;
239
79.2k
            }
240
131k
        }
241
26.6k
        if (i != npixels)
242
183
        {
243
183
            TIFFErrorExtR(tif, module,
244
183
                          "Not enough data at row %" PRIu32
245
183
                          " (short %" TIFF_SSIZE_FORMAT " pixels)",
246
183
                          tif->tif_row, npixels - i);
247
183
            tif->tif_rawcp = (uint8_t *)bp;
248
183
            tif->tif_rawcc = cc;
249
183
            return (0);
250
183
        }
251
26.6k
    }
252
13.1k
    (*sp->tfunc)(sp, op, npixels);
253
13.1k
    tif->tif_rawcp = (uint8_t *)bp;
254
13.1k
    tif->tif_rawcc = cc;
255
13.1k
    return (1);
256
13.3k
}
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
19.0k
{
263
19.0k
    static const char module[] = "LogLuvDecode24";
264
19.0k
    LogLuvState *sp = DecoderState(tif);
265
19.0k
    tmsize_t cc;
266
19.0k
    tmsize_t i;
267
19.0k
    tmsize_t npixels;
268
19.0k
    unsigned char *bp;
269
19.0k
    uint32_t *tp;
270
271
19.0k
    (void)s;
272
19.0k
    assert(s == 0);
273
19.0k
    assert(sp != NULL);
274
275
19.0k
    npixels = occ / sp->pixel_size;
276
277
19.0k
    if (sp->user_datafmt == SGILOGDATAFMT_RAW)
278
0
        tp = (uint32_t *)op;
279
19.0k
    else
280
19.0k
    {
281
19.0k
        if (sp->tbuflen < npixels)
282
0
        {
283
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
284
0
            return (0);
285
0
        }
286
19.0k
        tp = (uint32_t *)sp->tbuf;
287
19.0k
    }
288
    /* copy to array of uint32_t */
289
19.0k
    bp = (unsigned char *)tif->tif_rawcp;
290
19.0k
    cc = tif->tif_rawcc;
291
76.1k
    for (i = 0; i < npixels && cc >= 3; i++)
292
57.0k
    {
293
57.0k
        tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
294
57.0k
        bp += 3;
295
57.0k
        cc -= 3;
296
57.0k
    }
297
19.0k
    tif->tif_rawcp = (uint8_t *)bp;
298
19.0k
    tif->tif_rawcc = cc;
299
19.0k
    if (i != npixels)
300
177
    {
301
177
        TIFFErrorExtR(tif, module,
302
177
                      "Not enough data at row %" PRIu32
303
177
                      " (short %" TIFF_SSIZE_FORMAT " pixels)",
304
177
                      tif->tif_row, npixels - i);
305
177
        return (0);
306
177
    }
307
18.9k
    (*sp->tfunc)(sp, op, npixels);
308
18.9k
    return (1);
309
19.0k
}
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
15.6k
{
316
15.6k
    static const char module[] = "LogLuvDecode32";
317
15.6k
    LogLuvState *sp;
318
15.6k
    int shft;
319
15.6k
    tmsize_t i;
320
15.6k
    tmsize_t npixels;
321
15.6k
    unsigned char *bp;
322
15.6k
    uint32_t *tp;
323
15.6k
    uint32_t b;
324
15.6k
    tmsize_t cc;
325
15.6k
    int rc;
326
327
15.6k
    (void)s;
328
15.6k
    assert(s == 0);
329
15.6k
    sp = DecoderState(tif);
330
15.6k
    assert(sp != NULL);
331
332
15.6k
    npixels = occ / sp->pixel_size;
333
334
15.6k
    if (sp->user_datafmt == SGILOGDATAFMT_RAW)
335
0
        tp = (uint32_t *)op;
336
15.6k
    else
337
15.6k
    {
338
15.6k
        if (sp->tbuflen < npixels)
339
0
        {
340
0
            TIFFErrorExtR(tif, module, "Translation buffer too short");
341
0
            return (0);
342
0
        }
343
15.6k
        tp = (uint32_t *)sp->tbuf;
344
15.6k
    }
345
15.6k
    _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0]));
346
347
15.6k
    bp = (unsigned char *)tif->tif_rawcp;
348
15.6k
    cc = tif->tif_rawcc;
349
    /* get each byte string */
350
77.8k
    for (shft = 24; shft >= 0; shft -= 8)
351
62.3k
    {
352
119k
        for (i = 0; i < npixels && cc > 0;)
353
57.0k
        {
354
57.0k
            if (*bp >= 128)
355
17.1k
            { /* run */
356
17.1k
                if (cc < 2)
357
37
                    break;
358
17.1k
                rc = *bp++ + (2 - 128);
359
17.1k
                b = (uint32_t)*bp++ << shft;
360
17.1k
                cc -= 2;
361
1.03M
                while (rc-- && i < npixels)
362
1.01M
                    tp[i++] |= b;
363
17.1k
            }
364
39.9k
            else
365
39.9k
            {               /* non-run */
366
39.9k
                rc = *bp++; /* nul is noop */
367
156k
                while (--cc && rc-- && i < npixels)
368
116k
                    tp[i++] |= (uint32_t)*bp++ << shft;
369
39.9k
            }
370
57.0k
        }
371
62.3k
        if (i != npixels)
372
232
        {
373
232
            TIFFErrorExtR(tif, module,
374
232
                          "Not enough data at row %" PRIu32
375
232
                          " (short %" TIFF_SSIZE_FORMAT " pixels)",
376
232
                          tif->tif_row, npixels - i);
377
232
            tif->tif_rawcp = (uint8_t *)bp;
378
232
            tif->tif_rawcc = cc;
379
232
            return (0);
380
232
        }
381
62.3k
    }
382
15.4k
    (*sp->tfunc)(sp, op, npixels);
383
15.4k
    tif->tif_rawcp = (uint8_t *)bp;
384
15.4k
    tif->tif_rawcc = cc;
385
15.4k
    return (1);
386
15.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
356
{
395
356
    tmsize_t rowlen = TIFFScanlineSize(tif);
396
397
356
    if (rowlen == 0)
398
0
        return 0;
399
400
356
    assert(cc % rowlen == 0);
401
30.5k
    while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
402
30.1k
    {
403
30.1k
        bp += rowlen;
404
30.1k
        cc -= rowlen;
405
30.1k
    }
406
356
    return (cc == 0);
407
356
}
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
54
{
416
54
    tmsize_t rowlen = TIFFTileRowSize(tif);
417
418
54
    if (rowlen == 0)
419
0
        return 0;
420
421
54
    assert(cc % rowlen == 0);
422
2.34k
    while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
423
2.28k
    {
424
2.28k
        bp += rowlen;
425
2.28k
        cc -= rowlen;
426
2.28k
    }
427
54
    return (cc == 0);
428
54
}
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
#undef log2 /* Conflict with C'99 function */
771
0
#define log2(x) ((1. / M_LN2) * log(x))
772
#undef exp2 /* Conflict with C'99 function */
773
#define exp2(x) exp(M_LN2 *(x))
774
775
0
#define TIFF_RAND_MAX 32767
776
777
// From POSIX.1-2001 as an example of an implementation of rand()
778
static uint32_t _TIFFRand(void)
779
0
{
780
0
    static uint32_t nCounter = 0;
781
0
    if (!nCounter)
782
0
        nCounter = (uint32_t)(time(NULL) & UINT32_MAX);
783
0
    ++nCounter;
784
0
    uint32_t nCounterLocal =
785
0
        (uint32_t)(((uint64_t)(nCounter)*1103515245U + 12345U) & UINT32_MAX);
786
0
    nCounter = nCounterLocal;
787
0
    return (nCounterLocal / 65536U) % (TIFF_RAND_MAX + 1);
788
0
}
789
790
static int tiff_itrunc(double x, int m)
791
0
{
792
0
    if (m == SGILOGENCODE_NODITHER)
793
0
        return (int)x;
794
0
    return (int)(x + _TIFFRand() * (1. / TIFF_RAND_MAX) - .5);
795
0
}
796
797
#if !LOGLUV_PUBLIC
798
static
799
#endif
800
    double
801
    LogL16toY(int p16) /* compute luminance from 16-bit LogL */
802
1.44M
{
803
1.44M
    int Le = p16 & 0x7fff;
804
1.44M
    double Y;
805
806
1.44M
    if (!Le)
807
25.8k
        return (0.);
808
1.41M
    Y = exp(M_LN2 / 256. * (Le + .5) - M_LN2 * 64.);
809
1.41M
    return (!(p16 & 0x8000) ? Y : -Y);
810
1.44M
}
811
812
#if !LOGLUV_PUBLIC
813
static
814
#endif
815
    int
816
    LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */
817
0
{
818
0
    if (Y >= 1.8371976e19)
819
0
        return (0x7fff);
820
0
    if (Y <= -1.8371976e19)
821
0
        return (0xffff);
822
0
    if (Y > 5.4136769e-20)
823
0
        return tiff_itrunc(256. * (log2(Y) + 64.), em);
824
0
    if (Y < -5.4136769e-20)
825
0
        return (~0x7fff | tiff_itrunc(256. * (log2(-Y) + 64.), em));
826
0
    return (0);
827
0
}
828
829
static void L16toY(LogLuvState *sp, uint8_t *op, tmsize_t n)
830
13.1k
{
831
13.1k
    int16_t *l16 = (int16_t *)sp->tbuf;
832
13.1k
    float *yp = (float *)op;
833
834
1.18M
    while (n-- > 0)
835
1.16M
        *yp++ = (float)LogL16toY(*l16++);
836
13.1k
}
837
838
static void L16toGry(LogLuvState *sp, uint8_t *op, tmsize_t n)
839
0
{
840
0
    int16_t *l16 = (int16_t *)sp->tbuf;
841
0
    uint8_t *gp = (uint8_t *)op;
842
843
0
    while (n-- > 0)
844
0
    {
845
0
        double Y = LogL16toY(*l16++);
846
0
        *gp++ = (uint8_t)((Y <= 0.)   ? 0
847
0
                          : (Y >= 1.) ? 255
848
0
                                      : (int)(256. * sqrt(Y)));
849
0
    }
850
0
}
851
852
static void L16fromY(LogLuvState *sp, uint8_t *op, tmsize_t n)
853
0
{
854
0
    int16_t *l16 = (int16_t *)sp->tbuf;
855
0
    float *yp = (float *)op;
856
857
0
    while (n-- > 0)
858
0
        *l16++ = (int16_t)(LogL16fromY(*yp++, sp->encode_meth));
859
0
}
860
861
#if !LOGLUV_PUBLIC
862
static
863
#endif
864
    void
865
    XYZtoRGB24(float *xyz, uint8_t *rgb)
866
0
{
867
0
    double r, g, b;
868
    /* assume CCIR-709 primaries */
869
0
    r = 2.690 * xyz[0] + -1.276 * xyz[1] + -0.414 * xyz[2];
870
0
    g = -1.022 * xyz[0] + 1.978 * xyz[1] + 0.044 * xyz[2];
871
0
    b = 0.061 * xyz[0] + -0.224 * xyz[1] + 1.163 * xyz[2];
872
    /* assume 2.0 gamma for speed */
873
    /* could use integer sqrt approx., but this is probably faster */
874
0
    rgb[0] = (uint8_t)((r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256. * sqrt(r)));
875
0
    rgb[1] = (uint8_t)((g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256. * sqrt(g)));
876
0
    rgb[2] = (uint8_t)((b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256. * sqrt(b)));
877
0
}
878
879
#if !LOGLUV_PUBLIC
880
static
881
#endif
882
    double
883
    LogL10toY(int p10) /* compute luminance from 10-bit LogL */
884
55.9k
{
885
55.9k
    if (p10 == 0)
886
11.7k
        return (0.);
887
44.2k
    return (exp(M_LN2 / 64. * (p10 + .5) - M_LN2 * 12.));
888
55.9k
}
889
890
#if !LOGLUV_PUBLIC
891
static
892
#endif
893
    int
894
    LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */
895
0
{
896
0
    if (Y >= 15.742)
897
0
        return (0x3ff);
898
0
    else if (Y <= .00024283)
899
0
        return (0);
900
0
    else
901
0
        return tiff_itrunc(64. * (log2(Y) + 12.), em);
902
0
}
903
904
0
#define NANGLES 100
905
#define uv2ang(u, v)                                                           \
906
0
    ((NANGLES * .499999999 / M_PI) * atan2((v)-V_NEU, (u)-U_NEU) + .5 * NANGLES)
907
908
static int oog_encode(double u, double v) /* encode out-of-gamut chroma */
909
0
{
910
0
    static int oog_table[NANGLES];
911
0
    static int initialized = 0;
912
0
    int i;
913
914
0
    if (!initialized)
915
0
    { /* set up perimeter table */
916
0
        double eps[NANGLES], ua, va, ang, epsa;
917
0
        int ui, vi, ustep;
918
0
        for (i = NANGLES; i--;)
919
0
            eps[i] = 2.;
920
0
        for (vi = UV_NVS; vi--;)
921
0
        {
922
0
            va = UV_VSTART + (vi + .5) * UV_SQSIZ;
923
0
            ustep = uv_row[vi].nus - 1;
924
0
            if (vi == UV_NVS - 1 || vi == 0 || ustep <= 0)
925
0
                ustep = 1;
926
0
            for (ui = uv_row[vi].nus - 1; ui >= 0; ui -= ustep)
927
0
            {
928
0
                ua = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ;
929
0
                ang = uv2ang(ua, va);
930
0
                i = (int)ang;
931
0
                epsa = fabs(ang - (i + .5));
932
0
                if (epsa < eps[i])
933
0
                {
934
0
                    oog_table[i] = uv_row[vi].ncum + ui;
935
0
                    eps[i] = epsa;
936
0
                }
937
0
            }
938
0
        }
939
0
        for (i = NANGLES; i--;) /* fill any holes */
940
0
            if (eps[i] > 1.5)
941
0
            {
942
0
                int i1, i2;
943
0
                for (i1 = 1; i1 < NANGLES / 2; i1++)
944
0
                    if (eps[(i + i1) % NANGLES] < 1.5)
945
0
                        break;
946
0
                for (i2 = 1; i2 < NANGLES / 2; i2++)
947
0
                    if (eps[(i + NANGLES - i2) % NANGLES] < 1.5)
948
0
                        break;
949
0
                if (i1 < i2)
950
0
                    oog_table[i] = oog_table[(i + i1) % NANGLES];
951
0
                else
952
0
                    oog_table[i] = oog_table[(i + NANGLES - i2) % NANGLES];
953
0
            }
954
0
        initialized = 1;
955
0
    }
956
0
    i = (int)uv2ang(u, v); /* look up hue angle */
957
0
    return (oog_table[i]);
958
0
}
959
960
#undef uv2ang
961
#undef NANGLES
962
963
#if !LOGLUV_PUBLIC
964
static
965
#endif
966
    int
967
    uv_encode(double u, double v, int em) /* encode (u',v') coordinates */
968
0
{
969
0
    unsigned int vi;
970
0
    int ui;
971
972
    /* check for NaN */
973
0
    if (u != u || v != v)
974
0
    {
975
0
        u = U_NEU;
976
0
        v = V_NEU;
977
0
    }
978
979
0
    if (v < UV_VSTART)
980
0
        return oog_encode(u, v);
981
0
    vi = tiff_itrunc((v - UV_VSTART) * (1. / UV_SQSIZ), em);
982
0
    if (vi >= UV_NVS)
983
0
        return oog_encode(u, v);
984
0
    if (u < uv_row[vi].ustart)
985
0
        return oog_encode(u, v);
986
0
    ui = tiff_itrunc((u - uv_row[vi].ustart) * (1. / UV_SQSIZ), em);
987
0
    if (ui >= uv_row[vi].nus)
988
0
        return oog_encode(u, v);
989
990
0
    return (uv_row[vi].ncum + ui);
991
0
}
992
993
#if !LOGLUV_PUBLIC
994
static
995
#endif
996
    int
997
    uv_decode(double *up, double *vp, int c) /* decode (u',v') index */
998
44.2k
{
999
44.2k
    unsigned int upper, lower;
1000
44.2k
    int ui;
1001
44.2k
    unsigned int vi;
1002
1003
44.2k
    if (c < 0 || c >= UV_NDIVS)
1004
6.57k
        return (-1);
1005
37.6k
    lower = 0; /* binary search */
1006
37.6k
    upper = UV_NVS;
1007
313k
    while (upper - lower > 1)
1008
277k
    {
1009
277k
        vi = (lower + upper) >> 1;
1010
277k
        ui = c - uv_row[vi].ncum;
1011
277k
        if (ui > 0)
1012
133k
            lower = vi;
1013
143k
        else if (ui < 0)
1014
142k
            upper = vi;
1015
1.38k
        else
1016
1.38k
        {
1017
1.38k
            lower = vi;
1018
1.38k
            break;
1019
1.38k
        }
1020
277k
    }
1021
37.6k
    vi = lower;
1022
37.6k
    ui = c - uv_row[vi].ncum;
1023
37.6k
    *up = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ;
1024
37.6k
    *vp = UV_VSTART + (vi + .5) * UV_SQSIZ;
1025
37.6k
    return (0);
1026
44.2k
}
1027
1028
#if !LOGLUV_PUBLIC
1029
static
1030
#endif
1031
    void
1032
    LogLuv24toXYZ(uint32_t p, float *XYZ)
1033
55.9k
{
1034
55.9k
    int Ce;
1035
55.9k
    double L, u, v, s, x, y;
1036
    /* decode luminance */
1037
55.9k
    L = LogL10toY(p >> 14 & 0x3ff);
1038
55.9k
    if (L <= 0.)
1039
11.7k
    {
1040
11.7k
        XYZ[0] = XYZ[1] = XYZ[2] = 0.;
1041
11.7k
        return;
1042
11.7k
    }
1043
    /* decode color */
1044
44.2k
    Ce = p & 0x3fff;
1045
44.2k
    if (uv_decode(&u, &v, Ce) < 0)
1046
6.57k
    {
1047
6.57k
        u = U_NEU;
1048
6.57k
        v = V_NEU;
1049
6.57k
    }
1050
44.2k
    s = 1. / (6. * u - 16. * v + 12.);
1051
44.2k
    x = 9. * u * s;
1052
44.2k
    y = 4. * v * s;
1053
    /* convert to XYZ */
1054
44.2k
    XYZ[0] = (float)(x / y * L);
1055
44.2k
    XYZ[1] = (float)L;
1056
44.2k
    XYZ[2] = (float)((1. - x - y) / y * L);
1057
44.2k
}
1058
1059
#if !LOGLUV_PUBLIC
1060
static
1061
#endif
1062
    uint32_t
1063
    LogLuv24fromXYZ(float *XYZ, int em)
1064
0
{
1065
0
    int Le, Ce;
1066
0
    double u, v, s;
1067
    /* encode luminance */
1068
0
    Le = LogL10fromY(XYZ[1], em);
1069
    /* encode color */
1070
0
    s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2];
1071
0
    if (!Le || s <= 0.)
1072
0
    {
1073
0
        u = U_NEU;
1074
0
        v = V_NEU;
1075
0
    }
1076
0
    else
1077
0
    {
1078
0
        u = 4. * XYZ[0] / s;
1079
0
        v = 9. * XYZ[1] / s;
1080
0
    }
1081
0
    Ce = uv_encode(u, v, em);
1082
0
    if (Ce < 0) /* never happens */
1083
0
        Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
1084
    /* combine encodings */
1085
0
    return (Le << 14 | Ce);
1086
0
}
1087
1088
static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1089
18.9k
{
1090
18.9k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1091
18.9k
    float *xyz = (float *)op;
1092
1093
74.8k
    while (n-- > 0)
1094
55.9k
    {
1095
55.9k
        LogLuv24toXYZ(*luv, xyz);
1096
55.9k
        xyz += 3;
1097
55.9k
        luv++;
1098
55.9k
    }
1099
18.9k
}
1100
1101
static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1102
0
{
1103
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1104
0
    int16_t *luv3 = (int16_t *)op;
1105
1106
0
    while (n-- > 0)
1107
0
    {
1108
0
        double u, v;
1109
1110
0
        *luv3++ = (int16_t)((*luv >> 12 & 0xffd) + 13314);
1111
0
        if (uv_decode(&u, &v, *luv & 0x3fff) < 0)
1112
0
        {
1113
0
            u = U_NEU;
1114
0
            v = V_NEU;
1115
0
        }
1116
0
        *luv3++ = (int16_t)(u * (1L << 15));
1117
0
        *luv3++ = (int16_t)(v * (1L << 15));
1118
0
        luv++;
1119
0
    }
1120
0
}
1121
1122
static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
1123
0
{
1124
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1125
0
    uint8_t *rgb = (uint8_t *)op;
1126
1127
0
    while (n-- > 0)
1128
0
    {
1129
0
        float xyz[3];
1130
1131
0
        LogLuv24toXYZ(*luv++, xyz);
1132
0
        XYZtoRGB24(xyz, rgb);
1133
0
        rgb += 3;
1134
0
    }
1135
0
}
1136
1137
static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1138
0
{
1139
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1140
0
    float *xyz = (float *)op;
1141
1142
0
    while (n-- > 0)
1143
0
    {
1144
0
        *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth);
1145
0
        xyz += 3;
1146
0
    }
1147
0
}
1148
1149
static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1150
0
{
1151
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1152
0
    int16_t *luv3 = (int16_t *)op;
1153
1154
0
    while (n-- > 0)
1155
0
    {
1156
0
        int Le, Ce;
1157
1158
0
        if (luv3[0] <= 0)
1159
0
            Le = 0;
1160
0
        else if (luv3[0] >= (1 << 12) + 3314)
1161
0
            Le = (1 << 10) - 1;
1162
0
        else if (sp->encode_meth == SGILOGENCODE_NODITHER)
1163
0
            Le = (luv3[0] - 3314) >> 2;
1164
0
        else
1165
0
            Le = tiff_itrunc(.25 * (luv3[0] - 3314.), sp->encode_meth);
1166
1167
0
        Ce = uv_encode((luv3[1] + .5) / (1 << 15), (luv3[2] + .5) / (1 << 15),
1168
0
                       sp->encode_meth);
1169
0
        if (Ce < 0) /* never happens */
1170
0
            Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
1171
0
        *luv++ = (uint32_t)Le << 14 | Ce;
1172
0
        luv3 += 3;
1173
0
    }
1174
0
}
1175
1176
#if !LOGLUV_PUBLIC
1177
static
1178
#endif
1179
    void
1180
    LogLuv32toXYZ(uint32_t p, float *XYZ)
1181
275k
{
1182
275k
    double L, u, v, s, x, y;
1183
    /* decode luminance */
1184
275k
    L = LogL16toY((int)p >> 16);
1185
275k
    if (L <= 0.)
1186
233k
    {
1187
233k
        XYZ[0] = XYZ[1] = XYZ[2] = 0.;
1188
233k
        return;
1189
233k
    }
1190
    /* decode color */
1191
42.3k
    u = 1. / UVSCALE * ((p >> 8 & 0xff) + .5);
1192
42.3k
    v = 1. / UVSCALE * ((p & 0xff) + .5);
1193
42.3k
    s = 1. / (6. * u - 16. * v + 12.);
1194
42.3k
    x = 9. * u * s;
1195
42.3k
    y = 4. * v * s;
1196
    /* convert to XYZ */
1197
42.3k
    XYZ[0] = (float)(x / y * L);
1198
42.3k
    XYZ[1] = (float)L;
1199
42.3k
    XYZ[2] = (float)((1. - x - y) / y * L);
1200
42.3k
}
1201
1202
#if !LOGLUV_PUBLIC
1203
static
1204
#endif
1205
    uint32_t
1206
    LogLuv32fromXYZ(float *XYZ, int em)
1207
0
{
1208
0
    unsigned int Le, ue, ve;
1209
0
    double u, v, s;
1210
    /* encode luminance */
1211
0
    Le = (unsigned int)LogL16fromY(XYZ[1], em);
1212
    /* encode color */
1213
0
    s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2];
1214
0
    if (!Le || s <= 0.)
1215
0
    {
1216
0
        u = U_NEU;
1217
0
        v = V_NEU;
1218
0
    }
1219
0
    else
1220
0
    {
1221
0
        u = 4. * XYZ[0] / s;
1222
0
        v = 9. * XYZ[1] / s;
1223
0
    }
1224
0
    if (u <= 0.)
1225
0
        ue = 0;
1226
0
    else
1227
0
        ue = tiff_itrunc(UVSCALE * u, em);
1228
0
    if (ue > 255)
1229
0
        ue = 255;
1230
0
    if (v <= 0.)
1231
0
        ve = 0;
1232
0
    else
1233
0
        ve = tiff_itrunc(UVSCALE * v, em);
1234
0
    if (ve > 255)
1235
0
        ve = 255;
1236
    /* combine encodings */
1237
0
    return (Le << 16 | ue << 8 | ve);
1238
0
}
1239
1240
static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1241
15.4k
{
1242
15.4k
    uint32_t *luv = (uint32_t *)sp->tbuf;
1243
15.4k
    float *xyz = (float *)op;
1244
1245
291k
    while (n-- > 0)
1246
275k
    {
1247
275k
        LogLuv32toXYZ(*luv++, xyz);
1248
275k
        xyz += 3;
1249
275k
    }
1250
15.4k
}
1251
1252
static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1253
0
{
1254
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1255
0
    int16_t *luv3 = (int16_t *)op;
1256
1257
0
    while (n-- > 0)
1258
0
    {
1259
0
        double u, v;
1260
1261
0
        *luv3++ = (int16_t)(*luv >> 16);
1262
0
        u = 1. / UVSCALE * ((*luv >> 8 & 0xff) + .5);
1263
0
        v = 1. / UVSCALE * ((*luv & 0xff) + .5);
1264
0
        *luv3++ = (int16_t)(u * (1L << 15));
1265
0
        *luv3++ = (int16_t)(v * (1L << 15));
1266
0
        luv++;
1267
0
    }
1268
0
}
1269
1270
static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
1271
0
{
1272
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1273
0
    uint8_t *rgb = (uint8_t *)op;
1274
1275
0
    while (n-- > 0)
1276
0
    {
1277
0
        float xyz[3];
1278
1279
0
        LogLuv32toXYZ(*luv++, xyz);
1280
0
        XYZtoRGB24(xyz, rgb);
1281
0
        rgb += 3;
1282
0
    }
1283
0
}
1284
1285
static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1286
0
{
1287
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1288
0
    float *xyz = (float *)op;
1289
1290
0
    while (n-- > 0)
1291
0
    {
1292
0
        *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth);
1293
0
        xyz += 3;
1294
0
    }
1295
0
}
1296
1297
static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1298
0
{
1299
0
    uint32_t *luv = (uint32_t *)sp->tbuf;
1300
0
    int16_t *luv3 = (int16_t *)op;
1301
1302
0
    if (sp->encode_meth == SGILOGENCODE_NODITHER)
1303
0
    {
1304
0
        while (n-- > 0)
1305
0
        {
1306
0
            *luv++ = (uint32_t)luv3[0] << 16 |
1307
0
                     (luv3[1] * (uint32_t)(UVSCALE + .5) >> 7 & 0xff00) |
1308
0
                     (luv3[2] * (uint32_t)(UVSCALE + .5) >> 15 & 0xff);
1309
0
            luv3 += 3;
1310
0
        }
1311
0
        return;
1312
0
    }
1313
0
    while (n-- > 0)
1314
0
    {
1315
0
        *luv++ =
1316
0
            (uint32_t)luv3[0] << 16 |
1317
0
            (tiff_itrunc(luv3[1] * (UVSCALE / (1 << 15)), sp->encode_meth)
1318
0
                 << 8 &
1319
0
             0xff00) |
1320
0
            (tiff_itrunc(luv3[2] * (UVSCALE / (1 << 15)), sp->encode_meth) &
1321
0
             0xff);
1322
0
        luv3 += 3;
1323
0
    }
1324
0
}
1325
1326
static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n)
1327
0
{
1328
0
    (void)sp;
1329
0
    (void)op;
1330
0
    (void)n;
1331
0
}
1332
1333
static int LogL16GuessDataFmt(TIFFDirectory *td)
1334
0
{
1335
0
#define PACK(s, b, f) (((b) << 6) | ((s) << 3) | (f))
1336
0
    switch (
1337
0
        PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat))
1338
0
    {
1339
0
        case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
1340
0
            return (SGILOGDATAFMT_FLOAT);
1341
0
        case PACK(1, 16, SAMPLEFORMAT_VOID):
1342
0
        case PACK(1, 16, SAMPLEFORMAT_INT):
1343
0
        case PACK(1, 16, SAMPLEFORMAT_UINT):
1344
0
            return (SGILOGDATAFMT_16BIT);
1345
0
        case PACK(1, 8, SAMPLEFORMAT_VOID):
1346
0
        case PACK(1, 8, SAMPLEFORMAT_UINT):
1347
0
            return (SGILOGDATAFMT_8BIT);
1348
0
        default:
1349
0
            break;
1350
0
    }
1351
0
#undef PACK
1352
0
    return (SGILOGDATAFMT_UNKNOWN);
1353
0
}
1354
1355
static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2)
1356
1.51k
{
1357
1.51k
    return _TIFFMultiplySSize(NULL, m1, m2, NULL);
1358
1.51k
}
1359
1360
static int LogL16InitState(TIFF *tif)
1361
228
{
1362
228
    static const char module[] = "LogL16InitState";
1363
228
    TIFFDirectory *td = &tif->tif_dir;
1364
228
    LogLuvState *sp = DecoderState(tif);
1365
1366
228
    assert(sp != NULL);
1367
228
    assert(td->td_photometric == PHOTOMETRIC_LOGL);
1368
1369
228
    if (td->td_samplesperpixel != 1)
1370
0
    {
1371
0
        TIFFErrorExtR(tif, module,
1372
0
                      "Sorry, can not handle LogL image with %s=%" PRIu16,
1373
0
                      "Samples/pixel", td->td_samplesperpixel);
1374
0
        return 0;
1375
0
    }
1376
1377
    /* for some reason, we can't do this in TIFFInitLogL16 */
1378
228
    if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
1379
0
        sp->user_datafmt = LogL16GuessDataFmt(td);
1380
228
    switch (sp->user_datafmt)
1381
228
    {
1382
228
        case SGILOGDATAFMT_FLOAT:
1383
228
            sp->pixel_size = sizeof(float);
1384
228
            break;
1385
0
        case SGILOGDATAFMT_16BIT:
1386
0
            sp->pixel_size = sizeof(int16_t);
1387
0
            break;
1388
0
        case SGILOGDATAFMT_8BIT:
1389
0
            sp->pixel_size = sizeof(uint8_t);
1390
0
            break;
1391
0
        default:
1392
0
            TIFFErrorExtR(tif, module,
1393
0
                          "No support for converting user data format to LogL");
1394
0
            return (0);
1395
228
    }
1396
228
    if (isTiled(tif))
1397
10
        sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
1398
218
    else if (td->td_rowsperstrip < td->td_imagelength)
1399
1
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
1400
217
    else
1401
217
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
1402
228
    if (multiply_ms(sp->tbuflen, sizeof(int16_t)) == 0 ||
1403
228
        (sp->tbuf = (uint8_t *)_TIFFmallocExt(
1404
228
             tif, sp->tbuflen * sizeof(int16_t))) == NULL)
1405
0
    {
1406
0
        TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
1407
0
        return (0);
1408
0
    }
1409
228
    return (1);
1410
228
}
1411
1412
static int LogLuvGuessDataFmt(TIFFDirectory *td)
1413
0
{
1414
0
    int guess;
1415
1416
    /*
1417
     * If the user didn't tell us their datafmt,
1418
     * take our best guess from the bitspersample.
1419
     */
1420
0
#define PACK(a, b) (((a) << 3) | (b))
1421
0
    switch (PACK(td->td_bitspersample, td->td_sampleformat))
1422
0
    {
1423
0
        case PACK(32, SAMPLEFORMAT_IEEEFP):
1424
0
            guess = SGILOGDATAFMT_FLOAT;
1425
0
            break;
1426
0
        case PACK(32, SAMPLEFORMAT_VOID):
1427
0
        case PACK(32, SAMPLEFORMAT_UINT):
1428
0
        case PACK(32, SAMPLEFORMAT_INT):
1429
0
            guess = SGILOGDATAFMT_RAW;
1430
0
            break;
1431
0
        case PACK(16, SAMPLEFORMAT_VOID):
1432
0
        case PACK(16, SAMPLEFORMAT_INT):
1433
0
        case PACK(16, SAMPLEFORMAT_UINT):
1434
0
            guess = SGILOGDATAFMT_16BIT;
1435
0
            break;
1436
0
        case PACK(8, SAMPLEFORMAT_VOID):
1437
0
        case PACK(8, SAMPLEFORMAT_UINT):
1438
0
            guess = SGILOGDATAFMT_8BIT;
1439
0
            break;
1440
0
        default:
1441
0
            guess = SGILOGDATAFMT_UNKNOWN;
1442
0
            break;
1443
0
#undef PACK
1444
0
    }
1445
    /*
1446
     * Double-check samples per pixel.
1447
     */
1448
0
    switch (td->td_samplesperpixel)
1449
0
    {
1450
0
        case 1:
1451
0
            if (guess != SGILOGDATAFMT_RAW)
1452
0
                guess = SGILOGDATAFMT_UNKNOWN;
1453
0
            break;
1454
0
        case 3:
1455
0
            if (guess == SGILOGDATAFMT_RAW)
1456
0
                guess = SGILOGDATAFMT_UNKNOWN;
1457
0
            break;
1458
0
        default:
1459
0
            guess = SGILOGDATAFMT_UNKNOWN;
1460
0
            break;
1461
0
    }
1462
0
    return (guess);
1463
0
}
1464
1465
static int LogLuvInitState(TIFF *tif)
1466
529
{
1467
529
    static const char module[] = "LogLuvInitState";
1468
529
    TIFFDirectory *td = &tif->tif_dir;
1469
529
    LogLuvState *sp = DecoderState(tif);
1470
1471
529
    assert(sp != NULL);
1472
529
    assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
1473
1474
    /* for some reason, we can't do this in TIFFInitLogLuv */
1475
529
    if (td->td_planarconfig != PLANARCONFIG_CONTIG)
1476
0
    {
1477
0
        TIFFErrorExtR(tif, module,
1478
0
                      "SGILog compression cannot handle non-contiguous data");
1479
0
        return (0);
1480
0
    }
1481
529
    if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
1482
0
        sp->user_datafmt = LogLuvGuessDataFmt(td);
1483
529
    switch (sp->user_datafmt)
1484
529
    {
1485
529
        case SGILOGDATAFMT_FLOAT:
1486
529
            sp->pixel_size = 3 * sizeof(float);
1487
529
            break;
1488
0
        case SGILOGDATAFMT_16BIT:
1489
0
            sp->pixel_size = 3 * sizeof(int16_t);
1490
0
            break;
1491
0
        case SGILOGDATAFMT_RAW:
1492
0
            sp->pixel_size = sizeof(uint32_t);
1493
0
            break;
1494
0
        case SGILOGDATAFMT_8BIT:
1495
0
            sp->pixel_size = 3 * sizeof(uint8_t);
1496
0
            break;
1497
0
        default:
1498
0
            TIFFErrorExtR(
1499
0
                tif, module,
1500
0
                "No support for converting user data format to LogLuv");
1501
0
            return (0);
1502
529
    }
1503
529
    if (isTiled(tif))
1504
31
        sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
1505
498
    else if (td->td_rowsperstrip < td->td_imagelength)
1506
1
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
1507
497
    else
1508
497
        sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
1509
529
    if (multiply_ms(sp->tbuflen, sizeof(uint32_t)) == 0 ||
1510
529
        (sp->tbuf = (uint8_t *)_TIFFmallocExt(
1511
529
             tif, sp->tbuflen * sizeof(uint32_t))) == NULL)
1512
0
    {
1513
0
        TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
1514
0
        return (0);
1515
0
    }
1516
529
    return (1);
1517
529
}
1518
1519
static int LogLuvFixupTags(TIFF *tif)
1520
965
{
1521
965
    (void)tif;
1522
965
    return (1);
1523
965
}
1524
1525
static int LogLuvSetupDecode(TIFF *tif)
1526
760
{
1527
760
    static const char module[] = "LogLuvSetupDecode";
1528
760
    LogLuvState *sp = DecoderState(tif);
1529
760
    TIFFDirectory *td = &tif->tif_dir;
1530
1531
760
    tif->tif_postdecode = _TIFFNoPostDecode;
1532
760
    switch (td->td_photometric)
1533
760
    {
1534
529
        case PHOTOMETRIC_LOGLUV:
1535
529
            if (!LogLuvInitState(tif))
1536
0
                break;
1537
529
            if (td->td_compression == COMPRESSION_SGILOG24)
1538
224
            {
1539
224
                tif->tif_decoderow = LogLuvDecode24;
1540
224
                switch (sp->user_datafmt)
1541
224
                {
1542
224
                    case SGILOGDATAFMT_FLOAT:
1543
224
                        sp->tfunc = Luv24toXYZ;
1544
224
                        break;
1545
0
                    case SGILOGDATAFMT_16BIT:
1546
0
                        sp->tfunc = Luv24toLuv48;
1547
0
                        break;
1548
0
                    case SGILOGDATAFMT_8BIT:
1549
0
                        sp->tfunc = Luv24toRGB;
1550
0
                        break;
1551
0
                    default:
1552
0
                        break;
1553
224
                }
1554
224
            }
1555
305
            else
1556
305
            {
1557
305
                tif->tif_decoderow = LogLuvDecode32;
1558
305
                switch (sp->user_datafmt)
1559
305
                {
1560
305
                    case SGILOGDATAFMT_FLOAT:
1561
305
                        sp->tfunc = Luv32toXYZ;
1562
305
                        break;
1563
0
                    case SGILOGDATAFMT_16BIT:
1564
0
                        sp->tfunc = Luv32toLuv48;
1565
0
                        break;
1566
0
                    case SGILOGDATAFMT_8BIT:
1567
0
                        sp->tfunc = Luv32toRGB;
1568
0
                        break;
1569
0
                    default:
1570
0
                        break;
1571
305
                }
1572
305
            }
1573
529
            return (1);
1574
228
        case PHOTOMETRIC_LOGL:
1575
228
            if (!LogL16InitState(tif))
1576
0
                break;
1577
228
            tif->tif_decoderow = LogL16Decode;
1578
228
            switch (sp->user_datafmt)
1579
228
            {
1580
228
                case SGILOGDATAFMT_FLOAT:
1581
228
                    sp->tfunc = L16toY;
1582
228
                    break;
1583
0
                case SGILOGDATAFMT_8BIT:
1584
0
                    sp->tfunc = L16toGry;
1585
0
                    break;
1586
0
                default:
1587
0
                    break;
1588
228
            }
1589
228
            return (1);
1590
3
        default:
1591
3
            TIFFErrorExtR(tif, module,
1592
3
                          "Inappropriate photometric interpretation %" PRIu16
1593
3
                          " for SGILog compression; %s",
1594
3
                          td->td_photometric, "must be either LogLUV or LogL");
1595
3
            break;
1596
760
    }
1597
3
    return (0);
1598
760
}
1599
1600
static int LogLuvSetupEncode(TIFF *tif)
1601
0
{
1602
0
    static const char module[] = "LogLuvSetupEncode";
1603
0
    LogLuvState *sp = EncoderState(tif);
1604
0
    TIFFDirectory *td = &tif->tif_dir;
1605
1606
0
    switch (td->td_photometric)
1607
0
    {
1608
0
        case PHOTOMETRIC_LOGLUV:
1609
0
            if (!LogLuvInitState(tif))
1610
0
                return (0);
1611
0
            if (td->td_compression == COMPRESSION_SGILOG24)
1612
0
            {
1613
0
                tif->tif_encoderow = LogLuvEncode24;
1614
0
                switch (sp->user_datafmt)
1615
0
                {
1616
0
                    case SGILOGDATAFMT_FLOAT:
1617
0
                        sp->tfunc = Luv24fromXYZ;
1618
0
                        break;
1619
0
                    case SGILOGDATAFMT_16BIT:
1620
0
                        sp->tfunc = Luv24fromLuv48;
1621
0
                        break;
1622
0
                    case SGILOGDATAFMT_RAW:
1623
0
                        break;
1624
0
                    default:
1625
0
                        goto notsupported;
1626
0
                }
1627
0
            }
1628
0
            else
1629
0
            {
1630
0
                tif->tif_encoderow = LogLuvEncode32;
1631
0
                switch (sp->user_datafmt)
1632
0
                {
1633
0
                    case SGILOGDATAFMT_FLOAT:
1634
0
                        sp->tfunc = Luv32fromXYZ;
1635
0
                        break;
1636
0
                    case SGILOGDATAFMT_16BIT:
1637
0
                        sp->tfunc = Luv32fromLuv48;
1638
0
                        break;
1639
0
                    case SGILOGDATAFMT_RAW:
1640
0
                        break;
1641
0
                    default:
1642
0
                        goto notsupported;
1643
0
                }
1644
0
            }
1645
0
            break;
1646
0
        case PHOTOMETRIC_LOGL:
1647
0
            if (!LogL16InitState(tif))
1648
0
                return (0);
1649
0
            tif->tif_encoderow = LogL16Encode;
1650
0
            switch (sp->user_datafmt)
1651
0
            {
1652
0
                case SGILOGDATAFMT_FLOAT:
1653
0
                    sp->tfunc = L16fromY;
1654
0
                    break;
1655
0
                case SGILOGDATAFMT_16BIT:
1656
0
                    break;
1657
0
                default:
1658
0
                    goto notsupported;
1659
0
            }
1660
0
            break;
1661
0
        default:
1662
0
            TIFFErrorExtR(tif, module,
1663
0
                          "Inappropriate photometric interpretation %" PRIu16
1664
0
                          " for SGILog compression; %s",
1665
0
                          td->td_photometric, "must be either LogLUV or LogL");
1666
0
            return (0);
1667
0
    }
1668
0
    sp->encoder_state = 1;
1669
0
    return (1);
1670
0
notsupported:
1671
0
    TIFFErrorExtR(tif, module,
1672
0
                  "SGILog compression supported only for %s, or raw data",
1673
0
                  td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
1674
0
    return (0);
1675
0
}
1676
1677
static void LogLuvClose(TIFF *tif)
1678
0
{
1679
0
    LogLuvState *sp = (LogLuvState *)tif->tif_data;
1680
0
    TIFFDirectory *td = &tif->tif_dir;
1681
1682
0
    assert(sp != 0);
1683
    /*
1684
     * For consistency, we always want to write out the same
1685
     * bitspersample and sampleformat for our TIFF file,
1686
     * regardless of the data format being used by the application.
1687
     * Since this routine is called after tags have been set but
1688
     * before they have been recorded in the file, we reset them here.
1689
     * Note: this is really a nasty approach. See PixarLogClose
1690
     */
1691
0
    if (sp->encoder_state)
1692
0
    {
1693
        /* See PixarLogClose. Might avoid issues with tags whose size depends
1694
         * on those below, but not completely sure this is enough. */
1695
0
        td->td_samplesperpixel =
1696
0
            (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
1697
0
        td->td_bitspersample = 16;
1698
0
        td->td_sampleformat = SAMPLEFORMAT_INT;
1699
0
    }
1700
0
}
1701
1702
static void LogLuvCleanup(TIFF *tif)
1703
1.02k
{
1704
1.02k
    LogLuvState *sp = (LogLuvState *)tif->tif_data;
1705
1706
1.02k
    assert(sp != 0);
1707
1708
1.02k
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
1709
1.02k
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
1710
1711
1.02k
    if (sp->tbuf)
1712
757
        _TIFFfreeExt(tif, sp->tbuf);
1713
1.02k
    _TIFFfreeExt(tif, sp);
1714
1.02k
    tif->tif_data = NULL;
1715
1716
1.02k
    _TIFFSetDefaultCompressionState(tif);
1717
1.02k
}
1718
1719
static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap)
1720
8.16k
{
1721
8.16k
    static const char module[] = "LogLuvVSetField";
1722
8.16k
    LogLuvState *sp = DecoderState(tif);
1723
8.16k
    int bps, fmt;
1724
1725
8.16k
    switch (tag)
1726
8.16k
    {
1727
875
        case TIFFTAG_SGILOGDATAFMT:
1728
875
            sp->user_datafmt = (int)va_arg(ap, int);
1729
            /*
1730
             * Tweak the TIFF header so that the rest of libtiff knows what
1731
             * size of data will be passed between app and library, and
1732
             * assume that the app knows what it is doing and is not
1733
             * confused by these header manipulations...
1734
             */
1735
875
            switch (sp->user_datafmt)
1736
875
            {
1737
875
                case SGILOGDATAFMT_FLOAT:
1738
875
                    bps = 32;
1739
875
                    fmt = SAMPLEFORMAT_IEEEFP;
1740
875
                    break;
1741
0
                case SGILOGDATAFMT_16BIT:
1742
0
                    bps = 16;
1743
0
                    fmt = SAMPLEFORMAT_INT;
1744
0
                    break;
1745
0
                case SGILOGDATAFMT_RAW:
1746
0
                    bps = 32;
1747
0
                    fmt = SAMPLEFORMAT_UINT;
1748
0
                    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1749
0
                    break;
1750
0
                case SGILOGDATAFMT_8BIT:
1751
0
                    bps = 8;
1752
0
                    fmt = SAMPLEFORMAT_UINT;
1753
0
                    break;
1754
0
                default:
1755
0
                    TIFFErrorExtR(
1756
0
                        tif, tif->tif_name,
1757
0
                        "Unknown data format %d for LogLuv compression",
1758
0
                        sp->user_datafmt);
1759
0
                    return (0);
1760
875
            }
1761
875
            TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1762
875
            TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt);
1763
            /*
1764
             * Must recalculate sizes should bits/sample change.
1765
             */
1766
875
            tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)-1;
1767
875
            tif->tif_scanlinesize = TIFFScanlineSize(tif);
1768
875
            return (1);
1769
0
        case TIFFTAG_SGILOGENCODE:
1770
0
            sp->encode_meth = (int)va_arg(ap, int);
1771
0
            if (sp->encode_meth != SGILOGENCODE_NODITHER &&
1772
0
                sp->encode_meth != SGILOGENCODE_RANDITHER)
1773
0
            {
1774
0
                TIFFErrorExtR(tif, module,
1775
0
                              "Unknown encoding %d for LogLuv compression",
1776
0
                              sp->encode_meth);
1777
0
                return (0);
1778
0
            }
1779
0
            return (1);
1780
7.29k
        default:
1781
7.29k
            return (*sp->vsetparent)(tif, tag, ap);
1782
8.16k
    }
1783
8.16k
}
1784
1785
static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap)
1786
10.1k
{
1787
10.1k
    LogLuvState *sp = (LogLuvState *)tif->tif_data;
1788
1789
10.1k
    switch (tag)
1790
10.1k
    {
1791
0
        case TIFFTAG_SGILOGDATAFMT:
1792
0
            *va_arg(ap, int *) = sp->user_datafmt;
1793
0
            return (1);
1794
10.1k
        default:
1795
10.1k
            return (*sp->vgetparent)(tif, tag, ap);
1796
10.1k
    }
1797
10.1k
}
1798
1799
static const TIFFField LogLuvFields[] = {
1800
    {TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
1801
     TRUE, FALSE, "SGILogDataFmt", NULL},
1802
    {TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
1803
     TRUE, FALSE, "SGILogEncode", NULL}};
1804
1805
int TIFFInitSGILog(TIFF *tif, int scheme)
1806
1.02k
{
1807
1.02k
    static const char module[] = "TIFFInitSGILog";
1808
1.02k
    LogLuvState *sp;
1809
1810
1.02k
    assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
1811
1812
    /*
1813
     * Merge codec-specific tag information.
1814
     */
1815
1.02k
    if (!_TIFFMergeFields(tif, LogLuvFields, TIFFArrayCount(LogLuvFields)))
1816
0
    {
1817
0
        TIFFErrorExtR(tif, module, "Merging SGILog codec-specific tags failed");
1818
0
        return 0;
1819
0
    }
1820
1821
    /*
1822
     * Allocate state block so tag methods have storage to record values.
1823
     */
1824
1.02k
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LogLuvState));
1825
1.02k
    if (tif->tif_data == NULL)
1826
0
        goto bad;
1827
1.02k
    sp = (LogLuvState *)tif->tif_data;
1828
1.02k
    _TIFFmemset((void *)sp, 0, sizeof(*sp));
1829
1.02k
    sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
1830
1.02k
    sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? SGILOGENCODE_RANDITHER
1831
1.02k
                                                       : SGILOGENCODE_NODITHER;
1832
1.02k
    sp->tfunc = _logLuvNop;
1833
1834
    /*
1835
     * Install codec methods.
1836
     * NB: tif_decoderow & tif_encoderow are filled
1837
     *     in at setup time.
1838
     */
1839
1.02k
    tif->tif_fixuptags = LogLuvFixupTags;
1840
1.02k
    tif->tif_setupdecode = LogLuvSetupDecode;
1841
1.02k
    tif->tif_decodestrip = LogLuvDecodeStrip;
1842
1.02k
    tif->tif_decodetile = LogLuvDecodeTile;
1843
1.02k
    tif->tif_setupencode = LogLuvSetupEncode;
1844
1.02k
    tif->tif_encodestrip = LogLuvEncodeStrip;
1845
1.02k
    tif->tif_encodetile = LogLuvEncodeTile;
1846
1.02k
    tif->tif_close = LogLuvClose;
1847
1.02k
    tif->tif_cleanup = LogLuvCleanup;
1848
1849
    /*
1850
     * Override parent get/set field methods.
1851
     */
1852
1.02k
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
1853
1.02k
    tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */
1854
1.02k
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
1855
1.02k
    tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */
1856
1857
1.02k
    return (1);
1858
0
bad:
1859
0
    TIFFErrorExtR(tif, module, "%s: No space for LogLuv state block",
1860
0
                  tif->tif_name);
1861
0
    return (0);
1862
1.02k
}
1863
#endif /* LOGLUV_SUPPORT */