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