1 /* $Id: tif_luv.c,v 1.49 2017-07-24 12:47:30 erouault Exp $ */
4 * Copyright (c) 1997 Greg Ward Larson
5 * Copyright (c) 1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any
12 * advertising or publicity relating to the software without the specific,
13 * prior written permission of Sam Leffler, Greg Larson and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE
20 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32 * LogLuv compression support for high dynamic range images.
34 * Contributed by Greg Larson.
36 * LogLuv image support uses the TIFF library to store 16 or 10-bit
37 * log luminance values with 8 bits each of u and v or a 14-bit index.
39 * The codec can take as input and produce as output 32-bit IEEE float values
40 * as well as 16-bit integer values. A 16-bit luminance is interpreted
41 * as a sign bit followed by a 15-bit integer that is converted
42 * to and from a linear magnitude using the transformation:
44 * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit
46 * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real
48 * The actual conversion to world luminance units in candelas per sq. meter
49 * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
50 * This value is usually set such that a reasonable exposure comes from
51 * clamping decoded luminances above 1 to 1 in the displayed image.
53 * The 16-bit values for u and v may be converted to real values by dividing
54 * each by 32768. (This allows for negative values, which aren't useful as
55 * far as we know, but are left in case of future improvements in human
58 * Conversion from (u,v), which is actually the CIE (u',v') system for
59 * you color scientists, is accomplished by the following transformation:
61 * u = 4*x / (-2*x + 12*y + 3)
62 * v = 9*y / (-2*x + 12*y + 3)
64 * x = 9*u / (6*u - 16*v + 12)
65 * y = 4*v / (6*u - 16*v + 12)
67 * This process is greatly simplified by passing 32-bit IEEE floats
68 * for each of three CIE XYZ coordinates. The codec then takes care
69 * of conversion to and from LogLuv, though the application is still
70 * responsible for interpreting the TIFFTAG_STONITS calibration factor.
72 * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white
73 * point of (x,y)=(1/3,1/3). However, most color systems assume some other
74 * white point, such as D65, and an absolute color conversion to XYZ then
75 * to another color space with a different white point may introduce an
76 * unwanted color cast to the image. It is often desirable, therefore, to
77 * perform a white point conversion that maps the input white to [1 1 1]
78 * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT
79 * tag value. A decoder that demands absolute color calibration may use
80 * this white point tag to get back the original colors, but usually it
81 * will be ignored and the new white point will be used instead that
82 * matches the output color space.
84 * Pixel information is compressed into one of two basic encodings, depending
85 * on the setting of the compression tag, which is one of COMPRESSION_SGILOG
86 * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is
92 * COMPRESSION_SGILOG color data is stored as:
95 * |-+---------------|--------+--------|
98 * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
101 * |----------|--------------|
104 * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
105 * encoded as an index for optimal color resolution. The 10 log bits are
106 * defined by the following conversions:
108 * L = 2^((Le'+.5)/64 - 12) # real from 10-bit
110 * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real
112 * The 10 bits of the smaller format may be converted into the 15 bits of
113 * the larger format by multiplying by 4 and adding 13314. Obviously,
114 * a smaller range of magnitudes is covered (about 5 orders of magnitude
115 * instead of 38), and the lack of a sign bit means that negative luminances
116 * are not allowed. (Well, they aren't allowed in the real world, either,
117 * but they are useful for certain types of image processing.)
119 * The desired user format is controlled by the setting the internal
120 * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
121 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values
122 * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v
123 * Raw data i/o is also possible using:
124 * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel
125 * In addition, the following decoding is provided for ease of display:
126 * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values
128 * For grayscale images, we provide the following data formats:
129 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values
130 * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance
131 * SGILOGDATAFMT_8BIT = 8-bit gray monitor values
133 * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
134 * scheme by separating the logL, u and v bytes for each row and applying
135 * a PackBits type of compression. Since the 24-bit encoding is not
136 * adaptive, the 32-bit color format takes less space in many cases.
138 * Further control is provided over the conversion from higher-resolution
139 * formats to final encoded values through the pseudo tag
140 * TIFFTAG_SGILOGENCODE:
141 * SGILOGENCODE_NODITHER = do not dither encoded values
142 * SGILOGENCODE_RANDITHER = apply random dithering during encoding
144 * The default value of this tag is SGILOGENCODE_NODITHER for
145 * COMPRESSION_SGILOG to maximize run-length encoding and
146 * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn
147 * quantization errors into noise.
155 * State block for each open TIFF
156 * file using LogLuv compression/decompression.
158 typedef struct logLuvState LogLuvState
;
161 int encoder_state
; /* 1 if encoder correctly initialized */
162 int user_datafmt
; /* user data format */
163 int encode_meth
; /* encoding method */
164 int pixel_size
; /* bytes per pixel */
166 uint8
* tbuf
; /* translation buffer */
167 tmsize_t tbuflen
; /* buffer length */
168 void (*tfunc
)(LogLuvState
*, uint8
*, tmsize_t
);
170 TIFFVSetMethod vgetparent
; /* super-class method */
171 TIFFVSetMethod vsetparent
; /* super-class method */
174 #define DecoderState(tif) ((LogLuvState*) (tif)->tif_data)
175 #define EncoderState(tif) ((LogLuvState*) (tif)->tif_data)
177 #define SGILOGDATAFMT_UNKNOWN -1
179 #define MINRUN 4 /* minimum run length */
182 * Decode a string of 16-bit gray pixels.
185 LogL16Decode(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
187 static const char module
[] = "LogL16Decode";
188 LogLuvState
* sp
= DecoderState(tif
);
201 npixels
= occ
/ sp
->pixel_size
;
203 if (sp
->user_datafmt
== SGILOGDATAFMT_16BIT
)
206 if(sp
->tbuflen
< npixels
) {
207 TIFFErrorExt(tif
->tif_clientdata
, module
,
208 "Translation buffer too short");
211 tp
= (int16
*) sp
->tbuf
;
213 _TIFFmemset((void*) tp
, 0, npixels
*sizeof (tp
[0]));
215 bp
= (unsigned char*) tif
->tif_rawcp
;
217 /* get each byte string */
218 for (shft
= 2*8; (shft
-= 8) >= 0; ) {
219 for (i
= 0; i
< npixels
&& cc
> 0; ) {
220 if (*bp
>= 128) { /* run */
223 rc
= *bp
++ + (2-128);
224 b
= (int16
)(*bp
++ << shft
);
226 while (rc
-- && i
< npixels
)
228 } else { /* non-run */
229 rc
= *bp
++; /* nul is noop */
230 while (--cc
&& rc
-- && i
< npixels
)
231 tp
[i
++] |= (int16
)*bp
++ << shft
;
235 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
236 TIFFErrorExt(tif
->tif_clientdata
, module
,
237 "Not enough data at row %lu (short %I64d pixels)",
238 (unsigned long) tif
->tif_row
,
239 (unsigned __int64
) (npixels
- i
));
241 TIFFErrorExt(tif
->tif_clientdata
, module
,
242 "Not enough data at row %lu (short %llu pixels)",
243 (unsigned long) tif
->tif_row
,
244 (unsigned long long) (npixels
- i
));
246 tif
->tif_rawcp
= (uint8
*) bp
;
251 (*sp
->tfunc
)(sp
, op
, npixels
);
252 tif
->tif_rawcp
= (uint8
*) bp
;
258 * Decode a string of 24-bit pixels.
261 LogLuvDecode24(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
263 static const char module
[] = "LogLuvDecode24";
264 LogLuvState
* sp
= DecoderState(tif
);
274 npixels
= occ
/ sp
->pixel_size
;
276 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
279 if(sp
->tbuflen
< npixels
) {
280 TIFFErrorExt(tif
->tif_clientdata
, module
,
281 "Translation buffer too short");
284 tp
= (uint32
*) sp
->tbuf
;
286 /* copy to array of uint32 */
287 bp
= (unsigned char*) tif
->tif_rawcp
;
289 for (i
= 0; i
< npixels
&& cc
>= 3; i
++) {
290 tp
[i
] = bp
[0] << 16 | bp
[1] << 8 | bp
[2];
294 tif
->tif_rawcp
= (uint8
*) bp
;
297 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
298 TIFFErrorExt(tif
->tif_clientdata
, module
,
299 "Not enough data at row %lu (short %I64d pixels)",
300 (unsigned long) tif
->tif_row
,
301 (unsigned __int64
) (npixels
- i
));
303 TIFFErrorExt(tif
->tif_clientdata
, module
,
304 "Not enough data at row %lu (short %llu pixels)",
305 (unsigned long) tif
->tif_row
,
306 (unsigned long long) (npixels
- i
));
310 (*sp
->tfunc
)(sp
, op
, npixels
);
315 * Decode a string of 32-bit pixels.
318 LogLuvDecode32(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
320 static const char module
[] = "LogLuvDecode32";
332 sp
= DecoderState(tif
);
335 npixels
= occ
/ sp
->pixel_size
;
337 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
340 if(sp
->tbuflen
< npixels
) {
341 TIFFErrorExt(tif
->tif_clientdata
, module
,
342 "Translation buffer too short");
345 tp
= (uint32
*) sp
->tbuf
;
347 _TIFFmemset((void*) tp
, 0, npixels
*sizeof (tp
[0]));
349 bp
= (unsigned char*) tif
->tif_rawcp
;
351 /* get each byte string */
352 for (shft
= 4*8; (shft
-= 8) >= 0; ) {
353 for (i
= 0; i
< npixels
&& cc
> 0; ) {
354 if (*bp
>= 128) { /* run */
357 rc
= *bp
++ + (2-128);
358 b
= (uint32
)*bp
++ << shft
;
360 while (rc
-- && i
< npixels
)
362 } else { /* non-run */
363 rc
= *bp
++; /* nul is noop */
364 while (--cc
&& rc
-- && i
< npixels
)
365 tp
[i
++] |= (uint32
)*bp
++ << shft
;
369 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
370 TIFFErrorExt(tif
->tif_clientdata
, module
,
371 "Not enough data at row %lu (short %I64d pixels)",
372 (unsigned long) tif
->tif_row
,
373 (unsigned __int64
) (npixels
- i
));
375 TIFFErrorExt(tif
->tif_clientdata
, module
,
376 "Not enough data at row %lu (short %llu pixels)",
377 (unsigned long) tif
->tif_row
,
378 (unsigned long long) (npixels
- i
));
380 tif
->tif_rawcp
= (uint8
*) bp
;
385 (*sp
->tfunc
)(sp
, op
, npixels
);
386 tif
->tif_rawcp
= (uint8
*) bp
;
392 * Decode a strip of pixels. We break it into rows to
393 * maintain synchrony with the encode algorithm, which
397 LogLuvDecodeStrip(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
399 tmsize_t rowlen
= TIFFScanlineSize(tif
);
404 assert(cc
%rowlen
== 0);
405 while (cc
&& (*tif
->tif_decoderow
)(tif
, bp
, rowlen
, s
)) {
413 * Decode a tile of pixels. We break it into rows to
414 * maintain synchrony with the encode algorithm, which
418 LogLuvDecodeTile(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
420 tmsize_t rowlen
= TIFFTileRowSize(tif
);
425 assert(cc
%rowlen
== 0);
426 while (cc
&& (*tif
->tif_decoderow
)(tif
, bp
, rowlen
, s
)) {
434 * Encode a row of 16-bit pixels.
437 LogL16Encode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
439 static const char module
[] = "LogL16Encode";
440 LogLuvState
* sp
= EncoderState(tif
);
454 npixels
= cc
/ sp
->pixel_size
;
456 if (sp
->user_datafmt
== SGILOGDATAFMT_16BIT
)
459 tp
= (int16
*) sp
->tbuf
;
460 if(sp
->tbuflen
< npixels
) {
461 TIFFErrorExt(tif
->tif_clientdata
, module
,
462 "Translation buffer too short");
465 (*sp
->tfunc
)(sp
, bp
, npixels
);
467 /* compress each byte string */
469 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
470 for (shft
= 2*8; (shft
-= 8) >= 0; )
471 for (i
= 0; i
< npixels
; i
+= rc
) {
474 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
475 if (!TIFFFlushData1(tif
))
478 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
480 mask
= 0xff << shft
; /* find next run */
481 for (beg
= i
; beg
< npixels
; beg
+= rc
) {
482 b
= (int16
) (tp
[beg
] & mask
);
484 while (rc
< 127+2 && beg
+rc
< npixels
&&
485 (tp
[beg
+rc
] & mask
) == b
)
488 break; /* long enough */
490 if (beg
-i
> 1 && beg
-i
< MINRUN
) {
491 b
= (int16
) (tp
[i
] & mask
);/*check short run */
493 while ((tp
[j
++] & mask
) == b
)
495 *op
++ = (uint8
)(128-2+j
-i
);
496 *op
++ = (uint8
)(b
>> shft
);
502 while (i
< beg
) { /* write out non-run */
503 if ((j
= beg
-i
) > 127) j
= 127;
506 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
507 if (!TIFFFlushData1(tif
))
510 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
512 *op
++ = (uint8
) j
; occ
--;
514 *op
++ = (uint8
) (tp
[i
++] >> shft
& 0xff);
518 if (rc
>= MINRUN
) { /* write out run */
519 *op
++ = (uint8
) (128-2+rc
);
520 *op
++ = (uint8
) (tp
[beg
] >> shft
& 0xff);
526 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
532 * Encode a row of 24-bit pixels.
535 LogLuvEncode24(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
537 static const char module
[] = "LogLuvEncode24";
538 LogLuvState
* sp
= EncoderState(tif
);
547 npixels
= cc
/ sp
->pixel_size
;
549 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
552 tp
= (uint32
*) sp
->tbuf
;
553 if(sp
->tbuflen
< npixels
) {
554 TIFFErrorExt(tif
->tif_clientdata
, module
,
555 "Translation buffer too short");
558 (*sp
->tfunc
)(sp
, bp
, npixels
);
560 /* write out encoded pixels */
562 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
563 for (i
= npixels
; i
--; ) {
566 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
567 if (!TIFFFlushData1(tif
))
570 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
572 *op
++ = (uint8
)(*tp
>> 16);
573 *op
++ = (uint8
)(*tp
>> 8 & 0xff);
574 *op
++ = (uint8
)(*tp
++ & 0xff);
578 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
584 * Encode a row of 32-bit pixels.
587 LogLuvEncode32(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
589 static const char module
[] = "LogLuvEncode32";
590 LogLuvState
* sp
= EncoderState(tif
);
605 npixels
= cc
/ sp
->pixel_size
;
607 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
610 tp
= (uint32
*) sp
->tbuf
;
611 if(sp
->tbuflen
< npixels
) {
612 TIFFErrorExt(tif
->tif_clientdata
, module
,
613 "Translation buffer too short");
616 (*sp
->tfunc
)(sp
, bp
, npixels
);
618 /* compress each byte string */
620 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
621 for (shft
= 4*8; (shft
-= 8) >= 0; )
622 for (i
= 0; i
< npixels
; i
+= rc
) {
625 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
626 if (!TIFFFlushData1(tif
))
629 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
631 mask
= 0xff << shft
; /* find next run */
632 for (beg
= i
; beg
< npixels
; beg
+= rc
) {
635 while (rc
< 127+2 && beg
+rc
< npixels
&&
636 (tp
[beg
+rc
] & mask
) == b
)
639 break; /* long enough */
641 if (beg
-i
> 1 && beg
-i
< MINRUN
) {
642 b
= tp
[i
] & mask
; /* check short run */
644 while ((tp
[j
++] & mask
) == b
)
646 *op
++ = (uint8
)(128-2+j
-i
);
647 *op
++ = (uint8
)(b
>> shft
);
653 while (i
< beg
) { /* write out non-run */
654 if ((j
= beg
-i
) > 127) j
= 127;
657 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
658 if (!TIFFFlushData1(tif
))
661 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
663 *op
++ = (uint8
) j
; occ
--;
665 *op
++ = (uint8
)(tp
[i
++] >> shft
& 0xff);
669 if (rc
>= MINRUN
) { /* write out run */
670 *op
++ = (uint8
) (128-2+rc
);
671 *op
++ = (uint8
)(tp
[beg
] >> shft
& 0xff);
677 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
683 * Encode a strip of pixels. We break it into rows to
684 * avoid encoding runs across row boundaries.
687 LogLuvEncodeStrip(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
689 tmsize_t rowlen
= TIFFScanlineSize(tif
);
694 assert(cc
%rowlen
== 0);
695 while (cc
&& (*tif
->tif_encoderow
)(tif
, bp
, rowlen
, s
) == 1) {
703 * Encode a tile of pixels. We break it into rows to
704 * avoid encoding runs across row boundaries.
707 LogLuvEncodeTile(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
709 tmsize_t rowlen
= TIFFTileRowSize(tif
);
714 assert(cc
%rowlen
== 0);
715 while (cc
&& (*tif
->tif_encoderow
)(tif
, bp
, rowlen
, s
) == 1) {
723 * Encode/Decode functions for converting to and from user formats.
729 #define U_NEU 0.210526316
730 #define V_NEU 0.473684211
735 #define M_LN2 0.69314718055994530942
738 #define M_PI 3.14159265358979323846
740 #undef log2 /* Conflict with C'99 function */
741 #define log2(x) ((1./M_LN2)*log(x))
742 #undef exp2 /* Conflict with C'99 function */
743 #define exp2(x) exp(M_LN2*(x))
745 #define itrunc(x,m) ((m)==SGILOGENCODE_NODITHER ? \
747 (int)((x) + rand()*(1./RAND_MAX) - .5))
753 LogL16toY(int p16
) /* compute luminance from 16-bit LogL */
755 int Le
= p16
& 0x7fff;
760 Y
= exp(M_LN2
/256.*(Le
+.5) - M_LN2
*64.);
761 return (!(p16
& 0x8000) ? Y
: -Y
);
768 LogL16fromY(double Y
, int em
) /* get 16-bit LogL from Y */
770 if (Y
>= 1.8371976e19
)
772 if (Y
<= -1.8371976e19
)
774 if (Y
> 5.4136769e-20)
775 return itrunc(256.*(log2(Y
) + 64.), em
);
776 if (Y
< -5.4136769e-20)
777 return (~0x7fff | itrunc(256.*(log2(-Y
) + 64.), em
));
782 L16toY(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
784 int16
* l16
= (int16
*) sp
->tbuf
;
785 float* yp
= (float*) op
;
788 *yp
++ = (float)LogL16toY(*l16
++);
792 L16toGry(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
794 int16
* l16
= (int16
*) sp
->tbuf
;
795 uint8
* gp
= (uint8
*) op
;
798 double Y
= LogL16toY(*l16
++);
799 *gp
++ = (uint8
) ((Y
<= 0.) ? 0 : (Y
>= 1.) ? 255 : (int)(256.*sqrt(Y
)));
804 L16fromY(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
806 int16
* l16
= (int16
*) sp
->tbuf
;
807 float* yp
= (float*) op
;
810 *l16
++ = (int16
) (LogL16fromY(*yp
++, sp
->encode_meth
));
817 XYZtoRGB24(float xyz
[3], uint8 rgb
[3])
820 /* assume CCIR-709 primaries */
821 r
= 2.690*xyz
[0] + -1.276*xyz
[1] + -0.414*xyz
[2];
822 g
= -1.022*xyz
[0] + 1.978*xyz
[1] + 0.044*xyz
[2];
823 b
= 0.061*xyz
[0] + -0.224*xyz
[1] + 1.163*xyz
[2];
824 /* assume 2.0 gamma for speed */
825 /* could use integer sqrt approx., but this is probably faster */
826 rgb
[0] = (uint8
)((r
<=0.) ? 0 : (r
>= 1.) ? 255 : (int)(256.*sqrt(r
)));
827 rgb
[1] = (uint8
)((g
<=0.) ? 0 : (g
>= 1.) ? 255 : (int)(256.*sqrt(g
)));
828 rgb
[2] = (uint8
)((b
<=0.) ? 0 : (b
>= 1.) ? 255 : (int)(256.*sqrt(b
)));
835 LogL10toY(int p10
) /* compute luminance from 10-bit LogL */
839 return (exp(M_LN2
/64.*(p10
+.5) - M_LN2
*12.));
846 LogL10fromY(double Y
, int em
) /* get 10-bit LogL from Y */
850 else if (Y
<= .00024283)
853 return itrunc(64.*(log2(Y
) + 12.), em
);
857 #define uv2ang(u, v) ( (NANGLES*.499999999/M_PI) \
858 * atan2((v)-V_NEU,(u)-U_NEU) + .5*NANGLES )
861 oog_encode(double u
, double v
) /* encode out-of-gamut chroma */
863 static int oog_table
[NANGLES
];
864 static int initialized
= 0;
867 if (!initialized
) { /* set up perimeter table */
868 double eps
[NANGLES
], ua
, va
, ang
, epsa
;
870 for (i
= NANGLES
; i
--; )
872 for (vi
= UV_NVS
; vi
--; ) {
873 va
= UV_VSTART
+ (vi
+.5)*UV_SQSIZ
;
874 ustep
= uv_row
[vi
].nus
-1;
875 if (vi
== UV_NVS
-1 || vi
== 0 || ustep
<= 0)
877 for (ui
= uv_row
[vi
].nus
-1; ui
>= 0; ui
-= ustep
) {
878 ua
= uv_row
[vi
].ustart
+ (ui
+.5)*UV_SQSIZ
;
879 ang
= uv2ang(ua
, va
);
881 epsa
= fabs(ang
- (i
+.5));
883 oog_table
[i
] = uv_row
[vi
].ncum
+ ui
;
888 for (i
= NANGLES
; i
--; ) /* fill any holes */
891 for (i1
= 1; i1
< NANGLES
/2; i1
++)
892 if (eps
[(i
+i1
)%NANGLES
] < 1.5)
894 for (i2
= 1; i2
< NANGLES
/2; i2
++)
895 if (eps
[(i
+NANGLES
-i2
)%NANGLES
] < 1.5)
899 oog_table
[(i
+i1
)%NANGLES
];
902 oog_table
[(i
+NANGLES
-i2
)%NANGLES
];
906 i
= (int) uv2ang(u
, v
); /* look up hue angle */
907 return (oog_table
[i
]);
917 uv_encode(double u
, double v
, int em
) /* encode (u',v') coordinates */
922 return oog_encode(u
, v
);
923 vi
= itrunc((v
- UV_VSTART
)*(1./UV_SQSIZ
), em
);
925 return oog_encode(u
, v
);
926 if (u
< uv_row
[vi
].ustart
)
927 return oog_encode(u
, v
);
928 ui
= itrunc((u
- uv_row
[vi
].ustart
)*(1./UV_SQSIZ
), em
);
929 if (ui
>= uv_row
[vi
].nus
)
930 return oog_encode(u
, v
);
932 return (uv_row
[vi
].ncum
+ ui
);
939 uv_decode(double *up
, double *vp
, int c
) /* decode (u',v') index */
944 if (c
< 0 || c
>= UV_NDIVS
)
946 lower
= 0; /* binary search */
948 while (upper
- lower
> 1) {
949 vi
= (lower
+ upper
) >> 1;
950 ui
= c
- uv_row
[vi
].ncum
;
961 ui
= c
- uv_row
[vi
].ncum
;
962 *up
= uv_row
[vi
].ustart
+ (ui
+.5)*UV_SQSIZ
;
963 *vp
= UV_VSTART
+ (vi
+.5)*UV_SQSIZ
;
971 LogLuv24toXYZ(uint32 p
, float XYZ
[3])
974 double L
, u
, v
, s
, x
, y
;
975 /* decode luminance */
976 L
= LogL10toY(p
>>14 & 0x3ff);
978 XYZ
[0] = XYZ
[1] = XYZ
[2] = 0.;
983 if (uv_decode(&u
, &v
, Ce
) < 0) {
984 u
= U_NEU
; v
= V_NEU
;
986 s
= 1./(6.*u
- 16.*v
+ 12.);
990 XYZ
[0] = (float)(x
/y
* L
);
992 XYZ
[2] = (float)((1.-x
-y
)/y
* L
);
999 LogLuv24fromXYZ(float XYZ
[3], int em
)
1003 /* encode luminance */
1004 Le
= LogL10fromY(XYZ
[1], em
);
1006 s
= XYZ
[0] + 15.*XYZ
[1] + 3.*XYZ
[2];
1007 if (!Le
|| s
<= 0.) {
1014 Ce
= uv_encode(u
, v
, em
);
1015 if (Ce
< 0) /* never happens */
1016 Ce
= uv_encode(U_NEU
, V_NEU
, SGILOGENCODE_NODITHER
);
1017 /* combine encodings */
1018 return (Le
<< 14 | Ce
);
1022 Luv24toXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1024 uint32
* luv
= (uint32
*) sp
->tbuf
;
1025 float* xyz
= (float*) op
;
1028 LogLuv24toXYZ(*luv
, xyz
);
1035 Luv24toLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1037 uint32
* luv
= (uint32
*) sp
->tbuf
;
1038 int16
* luv3
= (int16
*) op
;
1043 *luv3
++ = (int16
)((*luv
>> 12 & 0xffd) + 13314);
1044 if (uv_decode(&u
, &v
, *luv
&0x3fff) < 0) {
1048 *luv3
++ = (int16
)(u
* (1L<<15));
1049 *luv3
++ = (int16
)(v
* (1L<<15));
1055 Luv24toRGB(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1057 uint32
* luv
= (uint32
*) sp
->tbuf
;
1058 uint8
* rgb
= (uint8
*) op
;
1063 LogLuv24toXYZ(*luv
++, xyz
);
1064 XYZtoRGB24(xyz
, rgb
);
1070 Luv24fromXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1072 uint32
* luv
= (uint32
*) sp
->tbuf
;
1073 float* xyz
= (float*) op
;
1076 *luv
++ = LogLuv24fromXYZ(xyz
, sp
->encode_meth
);
1082 Luv24fromLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1084 uint32
* luv
= (uint32
*) sp
->tbuf
;
1085 int16
* luv3
= (int16
*) op
;
1092 else if (luv3
[0] >= (1<<12)+3314)
1094 else if (sp
->encode_meth
== SGILOGENCODE_NODITHER
)
1095 Le
= (luv3
[0]-3314) >> 2;
1097 Le
= itrunc(.25*(luv3
[0]-3314.), sp
->encode_meth
);
1099 Ce
= uv_encode((luv3
[1]+.5)/(1<<15), (luv3
[2]+.5)/(1<<15),
1101 if (Ce
< 0) /* never happens */
1102 Ce
= uv_encode(U_NEU
, V_NEU
, SGILOGENCODE_NODITHER
);
1103 *luv
++ = (uint32
)Le
<< 14 | Ce
;
1112 LogLuv32toXYZ(uint32 p
, float XYZ
[3])
1114 double L
, u
, v
, s
, x
, y
;
1115 /* decode luminance */
1116 L
= LogL16toY((int)p
>> 16);
1118 XYZ
[0] = XYZ
[1] = XYZ
[2] = 0.;
1122 u
= 1./UVSCALE
* ((p
>>8 & 0xff) + .5);
1123 v
= 1./UVSCALE
* ((p
& 0xff) + .5);
1124 s
= 1./(6.*u
- 16.*v
+ 12.);
1127 /* convert to XYZ */
1128 XYZ
[0] = (float)(x
/y
* L
);
1130 XYZ
[2] = (float)((1.-x
-y
)/y
* L
);
1137 LogLuv32fromXYZ(float XYZ
[3], int em
)
1139 unsigned int Le
, ue
, ve
;
1141 /* encode luminance */
1142 Le
= (unsigned int)LogL16fromY(XYZ
[1], em
);
1144 s
= XYZ
[0] + 15.*XYZ
[1] + 3.*XYZ
[2];
1145 if (!Le
|| s
<= 0.) {
1152 if (u
<= 0.) ue
= 0;
1153 else ue
= itrunc(UVSCALE
*u
, em
);
1154 if (ue
> 255) ue
= 255;
1155 if (v
<= 0.) ve
= 0;
1156 else ve
= itrunc(UVSCALE
*v
, em
);
1157 if (ve
> 255) ve
= 255;
1158 /* combine encodings */
1159 return (Le
<< 16 | ue
<< 8 | ve
);
1163 Luv32toXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1165 uint32
* luv
= (uint32
*) sp
->tbuf
;
1166 float* xyz
= (float*) op
;
1169 LogLuv32toXYZ(*luv
++, xyz
);
1175 Luv32toLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1177 uint32
* luv
= (uint32
*) sp
->tbuf
;
1178 int16
* luv3
= (int16
*) op
;
1183 *luv3
++ = (int16
)(*luv
>> 16);
1184 u
= 1./UVSCALE
* ((*luv
>>8 & 0xff) + .5);
1185 v
= 1./UVSCALE
* ((*luv
& 0xff) + .5);
1186 *luv3
++ = (int16
)(u
* (1L<<15));
1187 *luv3
++ = (int16
)(v
* (1L<<15));
1193 Luv32toRGB(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1195 uint32
* luv
= (uint32
*) sp
->tbuf
;
1196 uint8
* rgb
= (uint8
*) op
;
1201 LogLuv32toXYZ(*luv
++, xyz
);
1202 XYZtoRGB24(xyz
, rgb
);
1208 Luv32fromXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1210 uint32
* luv
= (uint32
*) sp
->tbuf
;
1211 float* xyz
= (float*) op
;
1214 *luv
++ = LogLuv32fromXYZ(xyz
, sp
->encode_meth
);
1220 Luv32fromLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1222 uint32
* luv
= (uint32
*) sp
->tbuf
;
1223 int16
* luv3
= (int16
*) op
;
1225 if (sp
->encode_meth
== SGILOGENCODE_NODITHER
) {
1227 *luv
++ = (uint32
)luv3
[0] << 16 |
1228 (luv3
[1]*(uint32
)(UVSCALE
+.5) >> 7 & 0xff00) |
1229 (luv3
[2]*(uint32
)(UVSCALE
+.5) >> 15 & 0xff);
1235 *luv
++ = (uint32
)luv3
[0] << 16 |
1236 (itrunc(luv3
[1]*(UVSCALE
/(1<<15)), sp
->encode_meth
) << 8 & 0xff00) |
1237 (itrunc(luv3
[2]*(UVSCALE
/(1<<15)), sp
->encode_meth
) & 0xff);
1243 _logLuvNop(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1245 (void) sp
; (void) op
; (void) n
;
1249 LogL16GuessDataFmt(TIFFDirectory
*td
)
1251 #define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f))
1252 switch (PACK(td
->td_samplesperpixel
, td
->td_bitspersample
, td
->td_sampleformat
)) {
1253 case PACK(1, 32, SAMPLEFORMAT_IEEEFP
):
1254 return (SGILOGDATAFMT_FLOAT
);
1255 case PACK(1, 16, SAMPLEFORMAT_VOID
):
1256 case PACK(1, 16, SAMPLEFORMAT_INT
):
1257 case PACK(1, 16, SAMPLEFORMAT_UINT
):
1258 return (SGILOGDATAFMT_16BIT
);
1259 case PACK(1, 8, SAMPLEFORMAT_VOID
):
1260 case PACK(1, 8, SAMPLEFORMAT_UINT
):
1261 return (SGILOGDATAFMT_8BIT
);
1264 return (SGILOGDATAFMT_UNKNOWN
);
1268 #define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
1269 #define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
1272 multiply_ms(tmsize_t m1
, tmsize_t m2
)
1274 if( m1
== 0 || m2
> TIFF_TMSIZE_T_MAX
/ m1
)
1280 LogL16InitState(TIFF
* tif
)
1282 static const char module
[] = "LogL16InitState";
1283 TIFFDirectory
*td
= &tif
->tif_dir
;
1284 LogLuvState
* sp
= DecoderState(tif
);
1287 assert(td
->td_photometric
== PHOTOMETRIC_LOGL
);
1289 if( td
->td_samplesperpixel
!= 1 )
1291 TIFFErrorExt(tif
->tif_clientdata
, module
,
1292 "Sorry, can not handle LogL image with %s=%d",
1293 "Samples/pixel", td
->td_samplesperpixel
);
1297 /* for some reason, we can't do this in TIFFInitLogL16 */
1298 if (sp
->user_datafmt
== SGILOGDATAFMT_UNKNOWN
)
1299 sp
->user_datafmt
= LogL16GuessDataFmt(td
);
1300 switch (sp
->user_datafmt
) {
1301 case SGILOGDATAFMT_FLOAT
:
1302 sp
->pixel_size
= sizeof (float);
1304 case SGILOGDATAFMT_16BIT
:
1305 sp
->pixel_size
= sizeof (int16
);
1307 case SGILOGDATAFMT_8BIT
:
1308 sp
->pixel_size
= sizeof (uint8
);
1311 TIFFErrorExt(tif
->tif_clientdata
, module
,
1312 "No support for converting user data format to LogL");
1316 sp
->tbuflen
= multiply_ms(td
->td_tilewidth
, td
->td_tilelength
);
1317 else if( td
->td_rowsperstrip
< td
->td_imagelength
)
1318 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_rowsperstrip
);
1320 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_imagelength
);
1321 if (multiply_ms(sp
->tbuflen
, sizeof (int16
)) == 0 ||
1322 (sp
->tbuf
= (uint8
*) _TIFFmalloc(sp
->tbuflen
* sizeof (int16
))) == NULL
) {
1323 TIFFErrorExt(tif
->tif_clientdata
, module
, "No space for SGILog translation buffer");
1330 LogLuvGuessDataFmt(TIFFDirectory
*td
)
1335 * If the user didn't tell us their datafmt,
1336 * take our best guess from the bitspersample.
1338 #define PACK(a,b) (((a)<<3)|(b))
1339 switch (PACK(td
->td_bitspersample
, td
->td_sampleformat
)) {
1340 case PACK(32, SAMPLEFORMAT_IEEEFP
):
1341 guess
= SGILOGDATAFMT_FLOAT
;
1343 case PACK(32, SAMPLEFORMAT_VOID
):
1344 case PACK(32, SAMPLEFORMAT_UINT
):
1345 case PACK(32, SAMPLEFORMAT_INT
):
1346 guess
= SGILOGDATAFMT_RAW
;
1348 case PACK(16, SAMPLEFORMAT_VOID
):
1349 case PACK(16, SAMPLEFORMAT_INT
):
1350 case PACK(16, SAMPLEFORMAT_UINT
):
1351 guess
= SGILOGDATAFMT_16BIT
;
1353 case PACK( 8, SAMPLEFORMAT_VOID
):
1354 case PACK( 8, SAMPLEFORMAT_UINT
):
1355 guess
= SGILOGDATAFMT_8BIT
;
1358 guess
= SGILOGDATAFMT_UNKNOWN
;
1363 * Double-check samples per pixel.
1365 switch (td
->td_samplesperpixel
) {
1367 if (guess
!= SGILOGDATAFMT_RAW
)
1368 guess
= SGILOGDATAFMT_UNKNOWN
;
1371 if (guess
== SGILOGDATAFMT_RAW
)
1372 guess
= SGILOGDATAFMT_UNKNOWN
;
1375 guess
= SGILOGDATAFMT_UNKNOWN
;
1382 LogLuvInitState(TIFF
* tif
)
1384 static const char module
[] = "LogLuvInitState";
1385 TIFFDirectory
* td
= &tif
->tif_dir
;
1386 LogLuvState
* sp
= DecoderState(tif
);
1389 assert(td
->td_photometric
== PHOTOMETRIC_LOGLUV
);
1391 /* for some reason, we can't do this in TIFFInitLogLuv */
1392 if (td
->td_planarconfig
!= PLANARCONFIG_CONTIG
) {
1393 TIFFErrorExt(tif
->tif_clientdata
, module
,
1394 "SGILog compression cannot handle non-contiguous data");
1397 if (sp
->user_datafmt
== SGILOGDATAFMT_UNKNOWN
)
1398 sp
->user_datafmt
= LogLuvGuessDataFmt(td
);
1399 switch (sp
->user_datafmt
) {
1400 case SGILOGDATAFMT_FLOAT
:
1401 sp
->pixel_size
= 3*sizeof (float);
1403 case SGILOGDATAFMT_16BIT
:
1404 sp
->pixel_size
= 3*sizeof (int16
);
1406 case SGILOGDATAFMT_RAW
:
1407 sp
->pixel_size
= sizeof (uint32
);
1409 case SGILOGDATAFMT_8BIT
:
1410 sp
->pixel_size
= 3*sizeof (uint8
);
1413 TIFFErrorExt(tif
->tif_clientdata
, module
,
1414 "No support for converting user data format to LogLuv");
1418 sp
->tbuflen
= multiply_ms(td
->td_tilewidth
, td
->td_tilelength
);
1419 else if( td
->td_rowsperstrip
< td
->td_imagelength
)
1420 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_rowsperstrip
);
1422 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_imagelength
);
1423 if (multiply_ms(sp
->tbuflen
, sizeof (uint32
)) == 0 ||
1424 (sp
->tbuf
= (uint8
*) _TIFFmalloc(sp
->tbuflen
* sizeof (uint32
))) == NULL
) {
1425 TIFFErrorExt(tif
->tif_clientdata
, module
, "No space for SGILog translation buffer");
1432 LogLuvFixupTags(TIFF
* tif
)
1439 LogLuvSetupDecode(TIFF
* tif
)
1441 static const char module
[] = "LogLuvSetupDecode";
1442 LogLuvState
* sp
= DecoderState(tif
);
1443 TIFFDirectory
* td
= &tif
->tif_dir
;
1445 tif
->tif_postdecode
= _TIFFNoPostDecode
;
1446 switch (td
->td_photometric
) {
1447 case PHOTOMETRIC_LOGLUV
:
1448 if (!LogLuvInitState(tif
))
1450 if (td
->td_compression
== COMPRESSION_SGILOG24
) {
1451 tif
->tif_decoderow
= LogLuvDecode24
;
1452 switch (sp
->user_datafmt
) {
1453 case SGILOGDATAFMT_FLOAT
:
1454 sp
->tfunc
= Luv24toXYZ
;
1456 case SGILOGDATAFMT_16BIT
:
1457 sp
->tfunc
= Luv24toLuv48
;
1459 case SGILOGDATAFMT_8BIT
:
1460 sp
->tfunc
= Luv24toRGB
;
1464 tif
->tif_decoderow
= LogLuvDecode32
;
1465 switch (sp
->user_datafmt
) {
1466 case SGILOGDATAFMT_FLOAT
:
1467 sp
->tfunc
= Luv32toXYZ
;
1469 case SGILOGDATAFMT_16BIT
:
1470 sp
->tfunc
= Luv32toLuv48
;
1472 case SGILOGDATAFMT_8BIT
:
1473 sp
->tfunc
= Luv32toRGB
;
1478 case PHOTOMETRIC_LOGL
:
1479 if (!LogL16InitState(tif
))
1481 tif
->tif_decoderow
= LogL16Decode
;
1482 switch (sp
->user_datafmt
) {
1483 case SGILOGDATAFMT_FLOAT
:
1486 case SGILOGDATAFMT_8BIT
:
1487 sp
->tfunc
= L16toGry
;
1492 TIFFErrorExt(tif
->tif_clientdata
, module
,
1493 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1494 td
->td_photometric
, "must be either LogLUV or LogL");
1501 LogLuvSetupEncode(TIFF
* tif
)
1503 static const char module
[] = "LogLuvSetupEncode";
1504 LogLuvState
* sp
= EncoderState(tif
);
1505 TIFFDirectory
* td
= &tif
->tif_dir
;
1507 switch (td
->td_photometric
) {
1508 case PHOTOMETRIC_LOGLUV
:
1509 if (!LogLuvInitState(tif
))
1511 if (td
->td_compression
== COMPRESSION_SGILOG24
) {
1512 tif
->tif_encoderow
= LogLuvEncode24
;
1513 switch (sp
->user_datafmt
) {
1514 case SGILOGDATAFMT_FLOAT
:
1515 sp
->tfunc
= Luv24fromXYZ
;
1517 case SGILOGDATAFMT_16BIT
:
1518 sp
->tfunc
= Luv24fromLuv48
;
1520 case SGILOGDATAFMT_RAW
:
1526 tif
->tif_encoderow
= LogLuvEncode32
;
1527 switch (sp
->user_datafmt
) {
1528 case SGILOGDATAFMT_FLOAT
:
1529 sp
->tfunc
= Luv32fromXYZ
;
1531 case SGILOGDATAFMT_16BIT
:
1532 sp
->tfunc
= Luv32fromLuv48
;
1534 case SGILOGDATAFMT_RAW
:
1541 case PHOTOMETRIC_LOGL
:
1542 if (!LogL16InitState(tif
))
1544 tif
->tif_encoderow
= LogL16Encode
;
1545 switch (sp
->user_datafmt
) {
1546 case SGILOGDATAFMT_FLOAT
:
1547 sp
->tfunc
= L16fromY
;
1549 case SGILOGDATAFMT_16BIT
:
1556 TIFFErrorExt(tif
->tif_clientdata
, module
,
1557 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1558 td
->td_photometric
, "must be either LogLUV or LogL");
1561 sp
->encoder_state
= 1;
1564 TIFFErrorExt(tif
->tif_clientdata
, module
,
1565 "SGILog compression supported only for %s, or raw data",
1566 td
->td_photometric
== PHOTOMETRIC_LOGL
? "Y, L" : "XYZ, Luv");
1571 LogLuvClose(TIFF
* tif
)
1573 LogLuvState
* sp
= (LogLuvState
*) tif
->tif_data
;
1574 TIFFDirectory
*td
= &tif
->tif_dir
;
1578 * For consistency, we always want to write out the same
1579 * bitspersample and sampleformat for our TIFF file,
1580 * regardless of the data format being used by the application.
1581 * Since this routine is called after tags have been set but
1582 * before they have been recorded in the file, we reset them here.
1583 * Note: this is really a nasty approach. See PixarLogClose
1585 if( sp
->encoder_state
)
1587 /* See PixarLogClose. Might avoid issues with tags whose size depends
1588 * on those below, but not completely sure this is enough. */
1589 td
->td_samplesperpixel
=
1590 (td
->td_photometric
== PHOTOMETRIC_LOGL
) ? 1 : 3;
1591 td
->td_bitspersample
= 16;
1592 td
->td_sampleformat
= SAMPLEFORMAT_INT
;
1597 LogLuvCleanup(TIFF
* tif
)
1599 LogLuvState
* sp
= (LogLuvState
*)tif
->tif_data
;
1603 tif
->tif_tagmethods
.vgetfield
= sp
->vgetparent
;
1604 tif
->tif_tagmethods
.vsetfield
= sp
->vsetparent
;
1607 _TIFFfree(sp
->tbuf
);
1609 tif
->tif_data
= NULL
;
1611 _TIFFSetDefaultCompressionState(tif
);
1615 LogLuvVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1617 static const char module
[] = "LogLuvVSetField";
1618 LogLuvState
* sp
= DecoderState(tif
);
1622 case TIFFTAG_SGILOGDATAFMT
:
1623 sp
->user_datafmt
= (int) va_arg(ap
, int);
1625 * Tweak the TIFF header so that the rest of libtiff knows what
1626 * size of data will be passed between app and library, and
1627 * assume that the app knows what it is doing and is not
1628 * confused by these header manipulations...
1630 switch (sp
->user_datafmt
) {
1631 case SGILOGDATAFMT_FLOAT
:
1633 fmt
= SAMPLEFORMAT_IEEEFP
;
1635 case SGILOGDATAFMT_16BIT
:
1637 fmt
= SAMPLEFORMAT_INT
;
1639 case SGILOGDATAFMT_RAW
:
1641 fmt
= SAMPLEFORMAT_UINT
;
1642 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 1);
1644 case SGILOGDATAFMT_8BIT
:
1646 fmt
= SAMPLEFORMAT_UINT
;
1649 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
1650 "Unknown data format %d for LogLuv compression",
1654 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, bps
);
1655 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, fmt
);
1657 * Must recalculate sizes should bits/sample change.
1659 tif
->tif_tilesize
= isTiled(tif
) ? TIFFTileSize(tif
) : (tmsize_t
) -1;
1660 tif
->tif_scanlinesize
= TIFFScanlineSize(tif
);
1662 case TIFFTAG_SGILOGENCODE
:
1663 sp
->encode_meth
= (int) va_arg(ap
, int);
1664 if (sp
->encode_meth
!= SGILOGENCODE_NODITHER
&&
1665 sp
->encode_meth
!= SGILOGENCODE_RANDITHER
) {
1666 TIFFErrorExt(tif
->tif_clientdata
, module
,
1667 "Unknown encoding %d for LogLuv compression",
1673 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1678 LogLuvVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1680 LogLuvState
*sp
= (LogLuvState
*)tif
->tif_data
;
1683 case TIFFTAG_SGILOGDATAFMT
:
1684 *va_arg(ap
, int*) = sp
->user_datafmt
;
1687 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1691 static const TIFFField LogLuvFields
[] = {
1692 { TIFFTAG_SGILOGDATAFMT
, 0, 0, TIFF_SHORT
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "SGILogDataFmt", NULL
},
1693 { TIFFTAG_SGILOGENCODE
, 0, 0, TIFF_SHORT
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "SGILogEncode", NULL
}
1697 TIFFInitSGILog(TIFF
* tif
, int scheme
)
1699 static const char module
[] = "TIFFInitSGILog";
1702 assert(scheme
== COMPRESSION_SGILOG24
|| scheme
== COMPRESSION_SGILOG
);
1705 * Merge codec-specific tag information.
1707 if (!_TIFFMergeFields(tif
, LogLuvFields
,
1708 TIFFArrayCount(LogLuvFields
))) {
1709 TIFFErrorExt(tif
->tif_clientdata
, module
,
1710 "Merging SGILog codec-specific tags failed");
1715 * Allocate state block so tag methods have storage to record values.
1717 tif
->tif_data
= (uint8
*) _TIFFmalloc(sizeof (LogLuvState
));
1718 if (tif
->tif_data
== NULL
)
1720 sp
= (LogLuvState
*) tif
->tif_data
;
1721 _TIFFmemset((void*)sp
, 0, sizeof (*sp
));
1722 sp
->user_datafmt
= SGILOGDATAFMT_UNKNOWN
;
1723 sp
->encode_meth
= (scheme
== COMPRESSION_SGILOG24
) ?
1724 SGILOGENCODE_RANDITHER
: SGILOGENCODE_NODITHER
;
1725 sp
->tfunc
= _logLuvNop
;
1728 * Install codec methods.
1729 * NB: tif_decoderow & tif_encoderow are filled
1732 tif
->tif_fixuptags
= LogLuvFixupTags
;
1733 tif
->tif_setupdecode
= LogLuvSetupDecode
;
1734 tif
->tif_decodestrip
= LogLuvDecodeStrip
;
1735 tif
->tif_decodetile
= LogLuvDecodeTile
;
1736 tif
->tif_setupencode
= LogLuvSetupEncode
;
1737 tif
->tif_encodestrip
= LogLuvEncodeStrip
;
1738 tif
->tif_encodetile
= LogLuvEncodeTile
;
1739 tif
->tif_close
= LogLuvClose
;
1740 tif
->tif_cleanup
= LogLuvCleanup
;
1743 * Override parent get/set field methods.
1745 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1746 tif
->tif_tagmethods
.vgetfield
= LogLuvVGetField
; /* hook for codec tags */
1747 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1748 tif
->tif_tagmethods
.vsetfield
= LogLuvVSetField
; /* hook for codec tags */
1752 TIFFErrorExt(tif
->tif_clientdata
, module
,
1753 "%s: No space for LogLuv state block", tif
->tif_name
);
1756 #endif /* LOGLUV_SUPPORT */
1758 /* vim: set ts=8 sts=8 sw=8 noet: */