2 * Copyright (c) 1997 Greg Ward Larson
3 * Copyright (c) 1997 Silicon Graphics, Inc.
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.
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.
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
30 * LogLuv compression support for high dynamic range images.
32 * Contributed by Greg Larson.
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.
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:
42 * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit
44 * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real
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.
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
56 * Conversion from (u,v), which is actually the CIE (u',v') system for
57 * you color scientists, is accomplished by the following transformation:
59 * u = 4*x / (-2*x + 12*y + 3)
60 * v = 9*y / (-2*x + 12*y + 3)
62 * x = 9*u / (6*u - 16*v + 12)
63 * y = 4*v / (6*u - 16*v + 12)
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.
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.
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
90 * COMPRESSION_SGILOG color data is stored as:
93 * |-+---------------|--------+--------|
96 * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
99 * |----------|--------------|
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:
106 * L = 2^((Le'+.5)/64 - 12) # real from 10-bit
108 * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real
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.)
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
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
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.
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
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.
153 * State block for each open TIFF
154 * file using LogLuv compression/decompression.
156 typedef struct logLuvState LogLuvState
;
159 int encoder_state
; /* 1 if encoder correctly initialized */
160 int user_datafmt
; /* user data format */
161 int encode_meth
; /* encoding method */
162 int pixel_size
; /* bytes per pixel */
164 uint8
* tbuf
; /* translation buffer */
165 tmsize_t tbuflen
; /* buffer length */
166 void (*tfunc
)(LogLuvState
*, uint8
*, tmsize_t
);
168 TIFFVSetMethod vgetparent
; /* super-class method */
169 TIFFVSetMethod vsetparent
; /* super-class method */
172 #define DecoderState(tif) ((LogLuvState*) (tif)->tif_data)
173 #define EncoderState(tif) ((LogLuvState*) (tif)->tif_data)
175 #define SGILOGDATAFMT_UNKNOWN -1
177 #define MINRUN 4 /* minimum run length */
180 * Decode a string of 16-bit gray pixels.
183 LogL16Decode(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
185 static const char module
[] = "LogL16Decode";
186 LogLuvState
* sp
= DecoderState(tif
);
199 npixels
= occ
/ sp
->pixel_size
;
201 if (sp
->user_datafmt
== SGILOGDATAFMT_16BIT
)
204 if(sp
->tbuflen
< npixels
) {
205 TIFFErrorExt(tif
->tif_clientdata
, module
,
206 "Translation buffer too short");
209 tp
= (int16
*) sp
->tbuf
;
211 _TIFFmemset((void*) tp
, 0, npixels
*sizeof (tp
[0]));
213 bp
= (unsigned char*) tif
->tif_rawcp
;
215 /* get each byte string */
216 for (shft
= 8; shft
>= 0; shft
-=8) {
217 for (i
= 0; i
< npixels
&& cc
> 0; ) {
218 if (*bp
>= 128) { /* run */
221 rc
= *bp
++ + (2-128);
222 b
= (int16
)(*bp
++ << shft
);
224 while (rc
-- && i
< npixels
)
226 } else { /* non-run */
227 rc
= *bp
++; /* nul is noop */
228 while (--cc
&& rc
-- && i
< npixels
)
229 tp
[i
++] |= (int16
)*bp
++ << shft
;
233 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
234 TIFFErrorExt(tif
->tif_clientdata
, module
,
235 "Not enough data at row %lu (short %I64d pixels)",
236 (unsigned long) tif
->tif_row
,
237 (unsigned __int64
) (npixels
- i
));
239 TIFFErrorExt(tif
->tif_clientdata
, module
,
240 "Not enough data at row %lu (short %llu pixels)",
241 (unsigned long) tif
->tif_row
,
242 (unsigned long long) (npixels
- i
));
244 tif
->tif_rawcp
= (uint8
*) bp
;
249 (*sp
->tfunc
)(sp
, op
, npixels
);
250 tif
->tif_rawcp
= (uint8
*) bp
;
256 * Decode a string of 24-bit pixels.
259 LogLuvDecode24(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
261 static const char module
[] = "LogLuvDecode24";
262 LogLuvState
* sp
= DecoderState(tif
);
272 npixels
= occ
/ sp
->pixel_size
;
274 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
277 if(sp
->tbuflen
< npixels
) {
278 TIFFErrorExt(tif
->tif_clientdata
, module
,
279 "Translation buffer too short");
282 tp
= (uint32
*) sp
->tbuf
;
284 /* copy to array of uint32 */
285 bp
= (unsigned char*) tif
->tif_rawcp
;
287 for (i
= 0; i
< npixels
&& cc
>= 3; i
++) {
288 tp
[i
] = bp
[0] << 16 | bp
[1] << 8 | bp
[2];
292 tif
->tif_rawcp
= (uint8
*) bp
;
295 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
296 TIFFErrorExt(tif
->tif_clientdata
, module
,
297 "Not enough data at row %lu (short %I64d pixels)",
298 (unsigned long) tif
->tif_row
,
299 (unsigned __int64
) (npixels
- i
));
301 TIFFErrorExt(tif
->tif_clientdata
, module
,
302 "Not enough data at row %lu (short %llu pixels)",
303 (unsigned long) tif
->tif_row
,
304 (unsigned long long) (npixels
- i
));
308 (*sp
->tfunc
)(sp
, op
, npixels
);
313 * Decode a string of 32-bit pixels.
316 LogLuvDecode32(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
318 static const char module
[] = "LogLuvDecode32";
330 sp
= DecoderState(tif
);
333 npixels
= occ
/ sp
->pixel_size
;
335 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
338 if(sp
->tbuflen
< npixels
) {
339 TIFFErrorExt(tif
->tif_clientdata
, module
,
340 "Translation buffer too short");
343 tp
= (uint32
*) sp
->tbuf
;
345 _TIFFmemset((void*) tp
, 0, npixels
*sizeof (tp
[0]));
347 bp
= (unsigned char*) tif
->tif_rawcp
;
349 /* get each byte string */
350 for (shft
= 24; shft
>= 0; shft
-=8) {
351 for (i
= 0; i
< npixels
&& cc
> 0; ) {
352 if (*bp
>= 128) { /* run */
355 rc
= *bp
++ + (2-128);
356 b
= (uint32
)*bp
++ << shft
;
358 while (rc
-- && i
< npixels
)
360 } else { /* non-run */
361 rc
= *bp
++; /* nul is noop */
362 while (--cc
&& rc
-- && i
< npixels
)
363 tp
[i
++] |= (uint32
)*bp
++ << shft
;
367 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
368 TIFFErrorExt(tif
->tif_clientdata
, module
,
369 "Not enough data at row %lu (short %I64d pixels)",
370 (unsigned long) tif
->tif_row
,
371 (unsigned __int64
) (npixels
- i
));
373 TIFFErrorExt(tif
->tif_clientdata
, module
,
374 "Not enough data at row %lu (short %llu pixels)",
375 (unsigned long) tif
->tif_row
,
376 (unsigned long long) (npixels
- i
));
378 tif
->tif_rawcp
= (uint8
*) bp
;
383 (*sp
->tfunc
)(sp
, op
, npixels
);
384 tif
->tif_rawcp
= (uint8
*) bp
;
390 * Decode a strip of pixels. We break it into rows to
391 * maintain synchrony with the encode algorithm, which
395 LogLuvDecodeStrip(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
397 tmsize_t rowlen
= TIFFScanlineSize(tif
);
402 assert(cc
%rowlen
== 0);
403 while (cc
&& (*tif
->tif_decoderow
)(tif
, bp
, rowlen
, s
)) {
411 * Decode a tile of pixels. We break it into rows to
412 * maintain synchrony with the encode algorithm, which
416 LogLuvDecodeTile(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
418 tmsize_t rowlen
= TIFFTileRowSize(tif
);
423 assert(cc
%rowlen
== 0);
424 while (cc
&& (*tif
->tif_decoderow
)(tif
, bp
, rowlen
, s
)) {
432 * Encode a row of 16-bit pixels.
435 LogL16Encode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
437 static const char module
[] = "LogL16Encode";
438 LogLuvState
* sp
= EncoderState(tif
);
452 npixels
= cc
/ sp
->pixel_size
;
454 if (sp
->user_datafmt
== SGILOGDATAFMT_16BIT
)
457 tp
= (int16
*) sp
->tbuf
;
458 if(sp
->tbuflen
< npixels
) {
459 TIFFErrorExt(tif
->tif_clientdata
, module
,
460 "Translation buffer too short");
463 (*sp
->tfunc
)(sp
, bp
, npixels
);
465 /* compress each byte string */
467 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
468 for (shft
= 8; shft
>= 0; shft
-=8) {
469 for (i
= 0; i
< npixels
; i
+= rc
) {
472 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
473 if (!TIFFFlushData1(tif
))
476 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
478 mask
= 0xff << shft
; /* find next run */
479 for (beg
= i
; beg
< npixels
; beg
+= rc
) {
480 b
= (int16
) (tp
[beg
] & mask
);
482 while (rc
< 127+2 && beg
+rc
< npixels
&&
483 (tp
[beg
+rc
] & mask
) == b
)
486 break; /* long enough */
488 if (beg
-i
> 1 && beg
-i
< MINRUN
) {
489 b
= (int16
) (tp
[i
] & mask
);/*check short run */
491 while ((tp
[j
++] & mask
) == b
)
493 *op
++ = (uint8
)(128-2+j
-i
);
494 *op
++ = (uint8
)(b
>> shft
);
500 while (i
< beg
) { /* write out non-run */
501 if ((j
= beg
-i
) > 127) j
= 127;
504 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
505 if (!TIFFFlushData1(tif
))
508 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
510 *op
++ = (uint8
) j
; occ
--;
512 *op
++ = (uint8
) (tp
[i
++] >> shft
& 0xff);
516 if (rc
>= MINRUN
) { /* write out run */
517 *op
++ = (uint8
) (128-2+rc
);
518 *op
++ = (uint8
) (tp
[beg
] >> shft
& 0xff);
525 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
531 * Encode a row of 24-bit pixels.
534 LogLuvEncode24(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
536 static const char module
[] = "LogLuvEncode24";
537 LogLuvState
* sp
= EncoderState(tif
);
546 npixels
= cc
/ sp
->pixel_size
;
548 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
551 tp
= (uint32
*) sp
->tbuf
;
552 if(sp
->tbuflen
< npixels
) {
553 TIFFErrorExt(tif
->tif_clientdata
, module
,
554 "Translation buffer too short");
557 (*sp
->tfunc
)(sp
, bp
, npixels
);
559 /* write out encoded pixels */
561 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
562 for (i
= npixels
; i
--; ) {
565 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
566 if (!TIFFFlushData1(tif
))
569 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
571 *op
++ = (uint8
)(*tp
>> 16);
572 *op
++ = (uint8
)(*tp
>> 8 & 0xff);
573 *op
++ = (uint8
)(*tp
++ & 0xff);
577 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
583 * Encode a row of 32-bit pixels.
586 LogLuvEncode32(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
588 static const char module
[] = "LogLuvEncode32";
589 LogLuvState
* sp
= EncoderState(tif
);
604 npixels
= cc
/ sp
->pixel_size
;
606 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
609 tp
= (uint32
*) sp
->tbuf
;
610 if(sp
->tbuflen
< npixels
) {
611 TIFFErrorExt(tif
->tif_clientdata
, module
,
612 "Translation buffer too short");
615 (*sp
->tfunc
)(sp
, bp
, npixels
);
617 /* compress each byte string */
619 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
620 for (shft
= 24; shft
>= 0; shft
-=8) {
621 for (i
= 0; i
< npixels
; i
+= rc
) {
624 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
625 if (!TIFFFlushData1(tif
))
628 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
630 mask
= 0xff << shft
; /* find next run */
631 for (beg
= i
; beg
< npixels
; beg
+= rc
) {
634 while (rc
< 127+2 && beg
+rc
< npixels
&&
635 (tp
[beg
+rc
] & mask
) == b
)
638 break; /* long enough */
640 if (beg
-i
> 1 && beg
-i
< MINRUN
) {
641 b
= tp
[i
] & mask
; /* check short run */
643 while ((tp
[j
++] & mask
) == b
)
645 *op
++ = (uint8
)(128-2+j
-i
);
646 *op
++ = (uint8
)(b
>> shft
);
652 while (i
< beg
) { /* write out non-run */
653 if ((j
= beg
-i
) > 127) j
= 127;
656 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
657 if (!TIFFFlushData1(tif
))
660 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
662 *op
++ = (uint8
) j
; occ
--;
664 *op
++ = (uint8
)(tp
[i
++] >> shft
& 0xff);
668 if (rc
>= MINRUN
) { /* write out run */
669 *op
++ = (uint8
) (128-2+rc
);
670 *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 static int itrunc(double x
, int m
)
747 if( m
== SGILOGENCODE_NODITHER
)
749 /* Silence CoverityScan warning about bad crypto function */
750 /* coverity[dont_call] */
751 return (int)(x
+ rand()*(1./RAND_MAX
) - .5);
758 LogL16toY(int p16
) /* compute luminance from 16-bit LogL */
760 int Le
= p16
& 0x7fff;
765 Y
= exp(M_LN2
/256.*(Le
+.5) - M_LN2
*64.);
766 return (!(p16
& 0x8000) ? Y
: -Y
);
773 LogL16fromY(double Y
, int em
) /* get 16-bit LogL from Y */
775 if (Y
>= 1.8371976e19
)
777 if (Y
<= -1.8371976e19
)
779 if (Y
> 5.4136769e-20)
780 return itrunc(256.*(log2(Y
) + 64.), em
);
781 if (Y
< -5.4136769e-20)
782 return (~0x7fff | itrunc(256.*(log2(-Y
) + 64.), em
));
787 L16toY(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
789 int16
* l16
= (int16
*) sp
->tbuf
;
790 float* yp
= (float*) op
;
793 *yp
++ = (float)LogL16toY(*l16
++);
797 L16toGry(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
799 int16
* l16
= (int16
*) sp
->tbuf
;
800 uint8
* gp
= (uint8
*) op
;
803 double Y
= LogL16toY(*l16
++);
804 *gp
++ = (uint8
) ((Y
<= 0.) ? 0 : (Y
>= 1.) ? 255 : (int)(256.*sqrt(Y
)));
809 L16fromY(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
811 int16
* l16
= (int16
*) sp
->tbuf
;
812 float* yp
= (float*) op
;
815 *l16
++ = (int16
) (LogL16fromY(*yp
++, sp
->encode_meth
));
822 XYZtoRGB24(float xyz
[3], uint8 rgb
[3])
825 /* assume CCIR-709 primaries */
826 r
= 2.690*xyz
[0] + -1.276*xyz
[1] + -0.414*xyz
[2];
827 g
= -1.022*xyz
[0] + 1.978*xyz
[1] + 0.044*xyz
[2];
828 b
= 0.061*xyz
[0] + -0.224*xyz
[1] + 1.163*xyz
[2];
829 /* assume 2.0 gamma for speed */
830 /* could use integer sqrt approx., but this is probably faster */
831 rgb
[0] = (uint8
)((r
<=0.) ? 0 : (r
>= 1.) ? 255 : (int)(256.*sqrt(r
)));
832 rgb
[1] = (uint8
)((g
<=0.) ? 0 : (g
>= 1.) ? 255 : (int)(256.*sqrt(g
)));
833 rgb
[2] = (uint8
)((b
<=0.) ? 0 : (b
>= 1.) ? 255 : (int)(256.*sqrt(b
)));
840 LogL10toY(int p10
) /* compute luminance from 10-bit LogL */
844 return (exp(M_LN2
/64.*(p10
+.5) - M_LN2
*12.));
851 LogL10fromY(double Y
, int em
) /* get 10-bit LogL from Y */
855 else if (Y
<= .00024283)
858 return itrunc(64.*(log2(Y
) + 12.), em
);
862 #define uv2ang(u, v) ( (NANGLES*.499999999/M_PI) \
863 * atan2((v)-V_NEU,(u)-U_NEU) + .5*NANGLES )
866 oog_encode(double u
, double v
) /* encode out-of-gamut chroma */
868 static int oog_table
[NANGLES
];
869 static int initialized
= 0;
872 if (!initialized
) { /* set up perimeter table */
873 double eps
[NANGLES
], ua
, va
, ang
, epsa
;
875 for (i
= NANGLES
; i
--; )
877 for (vi
= UV_NVS
; vi
--; ) {
878 va
= UV_VSTART
+ (vi
+.5)*UV_SQSIZ
;
879 ustep
= uv_row
[vi
].nus
-1;
880 if (vi
== UV_NVS
-1 || vi
== 0 || ustep
<= 0)
882 for (ui
= uv_row
[vi
].nus
-1; ui
>= 0; ui
-= ustep
) {
883 ua
= uv_row
[vi
].ustart
+ (ui
+.5)*UV_SQSIZ
;
884 ang
= uv2ang(ua
, va
);
886 epsa
= fabs(ang
- (i
+.5));
888 oog_table
[i
] = uv_row
[vi
].ncum
+ ui
;
893 for (i
= NANGLES
; i
--; ) /* fill any holes */
896 for (i1
= 1; i1
< NANGLES
/2; i1
++)
897 if (eps
[(i
+i1
)%NANGLES
] < 1.5)
899 for (i2
= 1; i2
< NANGLES
/2; i2
++)
900 if (eps
[(i
+NANGLES
-i2
)%NANGLES
] < 1.5)
904 oog_table
[(i
+i1
)%NANGLES
];
907 oog_table
[(i
+NANGLES
-i2
)%NANGLES
];
911 i
= (int) uv2ang(u
, v
); /* look up hue angle */
912 return (oog_table
[i
]);
922 uv_encode(double u
, double v
, int em
) /* encode (u',v') coordinates */
927 return oog_encode(u
, v
);
928 vi
= itrunc((v
- UV_VSTART
)*(1./UV_SQSIZ
), em
);
930 return oog_encode(u
, v
);
931 if (u
< uv_row
[vi
].ustart
)
932 return oog_encode(u
, v
);
933 ui
= itrunc((u
- uv_row
[vi
].ustart
)*(1./UV_SQSIZ
), em
);
934 if (ui
>= uv_row
[vi
].nus
)
935 return oog_encode(u
, v
);
937 return (uv_row
[vi
].ncum
+ ui
);
944 uv_decode(double *up
, double *vp
, int c
) /* decode (u',v') index */
949 if (c
< 0 || c
>= UV_NDIVS
)
951 lower
= 0; /* binary search */
953 while (upper
- lower
> 1) {
954 vi
= (lower
+ upper
) >> 1;
955 ui
= c
- uv_row
[vi
].ncum
;
966 ui
= c
- uv_row
[vi
].ncum
;
967 *up
= uv_row
[vi
].ustart
+ (ui
+.5)*UV_SQSIZ
;
968 *vp
= UV_VSTART
+ (vi
+.5)*UV_SQSIZ
;
976 LogLuv24toXYZ(uint32 p
, float XYZ
[3])
979 double L
, u
, v
, s
, x
, y
;
980 /* decode luminance */
981 L
= LogL10toY(p
>>14 & 0x3ff);
983 XYZ
[0] = XYZ
[1] = XYZ
[2] = 0.;
988 if (uv_decode(&u
, &v
, Ce
) < 0) {
989 u
= U_NEU
; v
= V_NEU
;
991 s
= 1./(6.*u
- 16.*v
+ 12.);
995 XYZ
[0] = (float)(x
/y
* L
);
997 XYZ
[2] = (float)((1.-x
-y
)/y
* L
);
1004 LogLuv24fromXYZ(float XYZ
[3], int em
)
1008 /* encode luminance */
1009 Le
= LogL10fromY(XYZ
[1], em
);
1011 s
= XYZ
[0] + 15.*XYZ
[1] + 3.*XYZ
[2];
1012 if (!Le
|| s
<= 0.) {
1019 Ce
= uv_encode(u
, v
, em
);
1020 if (Ce
< 0) /* never happens */
1021 Ce
= uv_encode(U_NEU
, V_NEU
, SGILOGENCODE_NODITHER
);
1022 /* combine encodings */
1023 return (Le
<< 14 | Ce
);
1027 Luv24toXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1029 uint32
* luv
= (uint32
*) sp
->tbuf
;
1030 float* xyz
= (float*) op
;
1033 LogLuv24toXYZ(*luv
, xyz
);
1040 Luv24toLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1042 uint32
* luv
= (uint32
*) sp
->tbuf
;
1043 int16
* luv3
= (int16
*) op
;
1048 *luv3
++ = (int16
)((*luv
>> 12 & 0xffd) + 13314);
1049 if (uv_decode(&u
, &v
, *luv
&0x3fff) < 0) {
1053 *luv3
++ = (int16
)(u
* (1L<<15));
1054 *luv3
++ = (int16
)(v
* (1L<<15));
1060 Luv24toRGB(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1062 uint32
* luv
= (uint32
*) sp
->tbuf
;
1063 uint8
* rgb
= (uint8
*) op
;
1068 LogLuv24toXYZ(*luv
++, xyz
);
1069 XYZtoRGB24(xyz
, rgb
);
1075 Luv24fromXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1077 uint32
* luv
= (uint32
*) sp
->tbuf
;
1078 float* xyz
= (float*) op
;
1081 *luv
++ = LogLuv24fromXYZ(xyz
, sp
->encode_meth
);
1087 Luv24fromLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1089 uint32
* luv
= (uint32
*) sp
->tbuf
;
1090 int16
* luv3
= (int16
*) op
;
1097 else if (luv3
[0] >= (1<<12)+3314)
1099 else if (sp
->encode_meth
== SGILOGENCODE_NODITHER
)
1100 Le
= (luv3
[0]-3314) >> 2;
1102 Le
= itrunc(.25*(luv3
[0]-3314.), sp
->encode_meth
);
1104 Ce
= uv_encode((luv3
[1]+.5)/(1<<15), (luv3
[2]+.5)/(1<<15),
1106 if (Ce
< 0) /* never happens */
1107 Ce
= uv_encode(U_NEU
, V_NEU
, SGILOGENCODE_NODITHER
);
1108 *luv
++ = (uint32
)Le
<< 14 | Ce
;
1117 LogLuv32toXYZ(uint32 p
, float XYZ
[3])
1119 double L
, u
, v
, s
, x
, y
;
1120 /* decode luminance */
1121 L
= LogL16toY((int)p
>> 16);
1123 XYZ
[0] = XYZ
[1] = XYZ
[2] = 0.;
1127 u
= 1./UVSCALE
* ((p
>>8 & 0xff) + .5);
1128 v
= 1./UVSCALE
* ((p
& 0xff) + .5);
1129 s
= 1./(6.*u
- 16.*v
+ 12.);
1132 /* convert to XYZ */
1133 XYZ
[0] = (float)(x
/y
* L
);
1135 XYZ
[2] = (float)((1.-x
-y
)/y
* L
);
1142 LogLuv32fromXYZ(float XYZ
[3], int em
)
1144 unsigned int Le
, ue
, ve
;
1146 /* encode luminance */
1147 Le
= (unsigned int)LogL16fromY(XYZ
[1], em
);
1149 s
= XYZ
[0] + 15.*XYZ
[1] + 3.*XYZ
[2];
1150 if (!Le
|| s
<= 0.) {
1157 if (u
<= 0.) ue
= 0;
1158 else ue
= itrunc(UVSCALE
*u
, em
);
1159 if (ue
> 255) ue
= 255;
1160 if (v
<= 0.) ve
= 0;
1161 else ve
= itrunc(UVSCALE
*v
, em
);
1162 if (ve
> 255) ve
= 255;
1163 /* combine encodings */
1164 return (Le
<< 16 | ue
<< 8 | ve
);
1168 Luv32toXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1170 uint32
* luv
= (uint32
*) sp
->tbuf
;
1171 float* xyz
= (float*) op
;
1174 LogLuv32toXYZ(*luv
++, xyz
);
1180 Luv32toLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1182 uint32
* luv
= (uint32
*) sp
->tbuf
;
1183 int16
* luv3
= (int16
*) op
;
1188 *luv3
++ = (int16
)(*luv
>> 16);
1189 u
= 1./UVSCALE
* ((*luv
>>8 & 0xff) + .5);
1190 v
= 1./UVSCALE
* ((*luv
& 0xff) + .5);
1191 *luv3
++ = (int16
)(u
* (1L<<15));
1192 *luv3
++ = (int16
)(v
* (1L<<15));
1198 Luv32toRGB(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1200 uint32
* luv
= (uint32
*) sp
->tbuf
;
1201 uint8
* rgb
= (uint8
*) op
;
1206 LogLuv32toXYZ(*luv
++, xyz
);
1207 XYZtoRGB24(xyz
, rgb
);
1213 Luv32fromXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1215 uint32
* luv
= (uint32
*) sp
->tbuf
;
1216 float* xyz
= (float*) op
;
1219 *luv
++ = LogLuv32fromXYZ(xyz
, sp
->encode_meth
);
1225 Luv32fromLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1227 uint32
* luv
= (uint32
*) sp
->tbuf
;
1228 int16
* luv3
= (int16
*) op
;
1230 if (sp
->encode_meth
== SGILOGENCODE_NODITHER
) {
1232 *luv
++ = (uint32
)luv3
[0] << 16 |
1233 (luv3
[1]*(uint32
)(UVSCALE
+.5) >> 7 & 0xff00) |
1234 (luv3
[2]*(uint32
)(UVSCALE
+.5) >> 15 & 0xff);
1240 *luv
++ = (uint32
)luv3
[0] << 16 |
1241 (itrunc(luv3
[1]*(UVSCALE
/(1<<15)), sp
->encode_meth
) << 8 & 0xff00) |
1242 (itrunc(luv3
[2]*(UVSCALE
/(1<<15)), sp
->encode_meth
) & 0xff);
1248 _logLuvNop(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1250 (void) sp
; (void) op
; (void) n
;
1254 LogL16GuessDataFmt(TIFFDirectory
*td
)
1256 #define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f))
1257 switch (PACK(td
->td_samplesperpixel
, td
->td_bitspersample
, td
->td_sampleformat
)) {
1258 case PACK(1, 32, SAMPLEFORMAT_IEEEFP
):
1259 return (SGILOGDATAFMT_FLOAT
);
1260 case PACK(1, 16, SAMPLEFORMAT_VOID
):
1261 case PACK(1, 16, SAMPLEFORMAT_INT
):
1262 case PACK(1, 16, SAMPLEFORMAT_UINT
):
1263 return (SGILOGDATAFMT_16BIT
);
1264 case PACK(1, 8, SAMPLEFORMAT_VOID
):
1265 case PACK(1, 8, SAMPLEFORMAT_UINT
):
1266 return (SGILOGDATAFMT_8BIT
);
1269 return (SGILOGDATAFMT_UNKNOWN
);
1273 multiply_ms(tmsize_t m1
, tmsize_t m2
)
1275 return _TIFFMultiplySSize(NULL
, m1
, m2
, NULL
);
1279 LogL16InitState(TIFF
* tif
)
1281 static const char module
[] = "LogL16InitState";
1282 TIFFDirectory
*td
= &tif
->tif_dir
;
1283 LogLuvState
* sp
= DecoderState(tif
);
1286 assert(td
->td_photometric
== PHOTOMETRIC_LOGL
);
1288 if( td
->td_samplesperpixel
!= 1 )
1290 TIFFErrorExt(tif
->tif_clientdata
, module
,
1291 "Sorry, can not handle LogL image with %s=%d",
1292 "Samples/pixel", td
->td_samplesperpixel
);
1296 /* for some reason, we can't do this in TIFFInitLogL16 */
1297 if (sp
->user_datafmt
== SGILOGDATAFMT_UNKNOWN
)
1298 sp
->user_datafmt
= LogL16GuessDataFmt(td
);
1299 switch (sp
->user_datafmt
) {
1300 case SGILOGDATAFMT_FLOAT
:
1301 sp
->pixel_size
= sizeof (float);
1303 case SGILOGDATAFMT_16BIT
:
1304 sp
->pixel_size
= sizeof (int16
);
1306 case SGILOGDATAFMT_8BIT
:
1307 sp
->pixel_size
= sizeof (uint8
);
1310 TIFFErrorExt(tif
->tif_clientdata
, module
,
1311 "No support for converting user data format to LogL");
1315 sp
->tbuflen
= multiply_ms(td
->td_tilewidth
, td
->td_tilelength
);
1316 else if( td
->td_rowsperstrip
< td
->td_imagelength
)
1317 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_rowsperstrip
);
1319 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_imagelength
);
1320 if (multiply_ms(sp
->tbuflen
, sizeof (int16
)) == 0 ||
1321 (sp
->tbuf
= (uint8
*) _TIFFmalloc(sp
->tbuflen
* sizeof (int16
))) == NULL
) {
1322 TIFFErrorExt(tif
->tif_clientdata
, module
, "No space for SGILog translation buffer");
1329 LogLuvGuessDataFmt(TIFFDirectory
*td
)
1334 * If the user didn't tell us their datafmt,
1335 * take our best guess from the bitspersample.
1337 #define PACK(a,b) (((a)<<3)|(b))
1338 switch (PACK(td
->td_bitspersample
, td
->td_sampleformat
)) {
1339 case PACK(32, SAMPLEFORMAT_IEEEFP
):
1340 guess
= SGILOGDATAFMT_FLOAT
;
1342 case PACK(32, SAMPLEFORMAT_VOID
):
1343 case PACK(32, SAMPLEFORMAT_UINT
):
1344 case PACK(32, SAMPLEFORMAT_INT
):
1345 guess
= SGILOGDATAFMT_RAW
;
1347 case PACK(16, SAMPLEFORMAT_VOID
):
1348 case PACK(16, SAMPLEFORMAT_INT
):
1349 case PACK(16, SAMPLEFORMAT_UINT
):
1350 guess
= SGILOGDATAFMT_16BIT
;
1352 case PACK( 8, SAMPLEFORMAT_VOID
):
1353 case PACK( 8, SAMPLEFORMAT_UINT
):
1354 guess
= SGILOGDATAFMT_8BIT
;
1357 guess
= SGILOGDATAFMT_UNKNOWN
;
1362 * Double-check samples per pixel.
1364 switch (td
->td_samplesperpixel
) {
1366 if (guess
!= SGILOGDATAFMT_RAW
)
1367 guess
= SGILOGDATAFMT_UNKNOWN
;
1370 if (guess
== SGILOGDATAFMT_RAW
)
1371 guess
= SGILOGDATAFMT_UNKNOWN
;
1374 guess
= SGILOGDATAFMT_UNKNOWN
;
1381 LogLuvInitState(TIFF
* tif
)
1383 static const char module
[] = "LogLuvInitState";
1384 TIFFDirectory
* td
= &tif
->tif_dir
;
1385 LogLuvState
* sp
= DecoderState(tif
);
1388 assert(td
->td_photometric
== PHOTOMETRIC_LOGLUV
);
1390 /* for some reason, we can't do this in TIFFInitLogLuv */
1391 if (td
->td_planarconfig
!= PLANARCONFIG_CONTIG
) {
1392 TIFFErrorExt(tif
->tif_clientdata
, module
,
1393 "SGILog compression cannot handle non-contiguous data");
1396 if (sp
->user_datafmt
== SGILOGDATAFMT_UNKNOWN
)
1397 sp
->user_datafmt
= LogLuvGuessDataFmt(td
);
1398 switch (sp
->user_datafmt
) {
1399 case SGILOGDATAFMT_FLOAT
:
1400 sp
->pixel_size
= 3*sizeof (float);
1402 case SGILOGDATAFMT_16BIT
:
1403 sp
->pixel_size
= 3*sizeof (int16
);
1405 case SGILOGDATAFMT_RAW
:
1406 sp
->pixel_size
= sizeof (uint32
);
1408 case SGILOGDATAFMT_8BIT
:
1409 sp
->pixel_size
= 3*sizeof (uint8
);
1412 TIFFErrorExt(tif
->tif_clientdata
, module
,
1413 "No support for converting user data format to LogLuv");
1417 sp
->tbuflen
= multiply_ms(td
->td_tilewidth
, td
->td_tilelength
);
1418 else if( td
->td_rowsperstrip
< td
->td_imagelength
)
1419 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_rowsperstrip
);
1421 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_imagelength
);
1422 if (multiply_ms(sp
->tbuflen
, sizeof (uint32
)) == 0 ||
1423 (sp
->tbuf
= (uint8
*) _TIFFmalloc(sp
->tbuflen
* sizeof (uint32
))) == NULL
) {
1424 TIFFErrorExt(tif
->tif_clientdata
, module
, "No space for SGILog translation buffer");
1431 LogLuvFixupTags(TIFF
* tif
)
1438 LogLuvSetupDecode(TIFF
* tif
)
1440 static const char module
[] = "LogLuvSetupDecode";
1441 LogLuvState
* sp
= DecoderState(tif
);
1442 TIFFDirectory
* td
= &tif
->tif_dir
;
1444 tif
->tif_postdecode
= _TIFFNoPostDecode
;
1445 switch (td
->td_photometric
) {
1446 case PHOTOMETRIC_LOGLUV
:
1447 if (!LogLuvInitState(tif
))
1449 if (td
->td_compression
== COMPRESSION_SGILOG24
) {
1450 tif
->tif_decoderow
= LogLuvDecode24
;
1451 switch (sp
->user_datafmt
) {
1452 case SGILOGDATAFMT_FLOAT
:
1453 sp
->tfunc
= Luv24toXYZ
;
1455 case SGILOGDATAFMT_16BIT
:
1456 sp
->tfunc
= Luv24toLuv48
;
1458 case SGILOGDATAFMT_8BIT
:
1459 sp
->tfunc
= Luv24toRGB
;
1463 tif
->tif_decoderow
= LogLuvDecode32
;
1464 switch (sp
->user_datafmt
) {
1465 case SGILOGDATAFMT_FLOAT
:
1466 sp
->tfunc
= Luv32toXYZ
;
1468 case SGILOGDATAFMT_16BIT
:
1469 sp
->tfunc
= Luv32toLuv48
;
1471 case SGILOGDATAFMT_8BIT
:
1472 sp
->tfunc
= Luv32toRGB
;
1477 case PHOTOMETRIC_LOGL
:
1478 if (!LogL16InitState(tif
))
1480 tif
->tif_decoderow
= LogL16Decode
;
1481 switch (sp
->user_datafmt
) {
1482 case SGILOGDATAFMT_FLOAT
:
1485 case SGILOGDATAFMT_8BIT
:
1486 sp
->tfunc
= L16toGry
;
1491 TIFFErrorExt(tif
->tif_clientdata
, module
,
1492 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1493 td
->td_photometric
, "must be either LogLUV or LogL");
1500 LogLuvSetupEncode(TIFF
* tif
)
1502 static const char module
[] = "LogLuvSetupEncode";
1503 LogLuvState
* sp
= EncoderState(tif
);
1504 TIFFDirectory
* td
= &tif
->tif_dir
;
1506 switch (td
->td_photometric
) {
1507 case PHOTOMETRIC_LOGLUV
:
1508 if (!LogLuvInitState(tif
))
1510 if (td
->td_compression
== COMPRESSION_SGILOG24
) {
1511 tif
->tif_encoderow
= LogLuvEncode24
;
1512 switch (sp
->user_datafmt
) {
1513 case SGILOGDATAFMT_FLOAT
:
1514 sp
->tfunc
= Luv24fromXYZ
;
1516 case SGILOGDATAFMT_16BIT
:
1517 sp
->tfunc
= Luv24fromLuv48
;
1519 case SGILOGDATAFMT_RAW
:
1525 tif
->tif_encoderow
= LogLuvEncode32
;
1526 switch (sp
->user_datafmt
) {
1527 case SGILOGDATAFMT_FLOAT
:
1528 sp
->tfunc
= Luv32fromXYZ
;
1530 case SGILOGDATAFMT_16BIT
:
1531 sp
->tfunc
= Luv32fromLuv48
;
1533 case SGILOGDATAFMT_RAW
:
1540 case PHOTOMETRIC_LOGL
:
1541 if (!LogL16InitState(tif
))
1543 tif
->tif_encoderow
= LogL16Encode
;
1544 switch (sp
->user_datafmt
) {
1545 case SGILOGDATAFMT_FLOAT
:
1546 sp
->tfunc
= L16fromY
;
1548 case SGILOGDATAFMT_16BIT
:
1555 TIFFErrorExt(tif
->tif_clientdata
, module
,
1556 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1557 td
->td_photometric
, "must be either LogLUV or LogL");
1560 sp
->encoder_state
= 1;
1563 TIFFErrorExt(tif
->tif_clientdata
, module
,
1564 "SGILog compression supported only for %s, or raw data",
1565 td
->td_photometric
== PHOTOMETRIC_LOGL
? "Y, L" : "XYZ, Luv");
1570 LogLuvClose(TIFF
* tif
)
1572 LogLuvState
* sp
= (LogLuvState
*) tif
->tif_data
;
1573 TIFFDirectory
*td
= &tif
->tif_dir
;
1577 * For consistency, we always want to write out the same
1578 * bitspersample and sampleformat for our TIFF file,
1579 * regardless of the data format being used by the application.
1580 * Since this routine is called after tags have been set but
1581 * before they have been recorded in the file, we reset them here.
1582 * Note: this is really a nasty approach. See PixarLogClose
1584 if( sp
->encoder_state
)
1586 /* See PixarLogClose. Might avoid issues with tags whose size depends
1587 * on those below, but not completely sure this is enough. */
1588 td
->td_samplesperpixel
=
1589 (td
->td_photometric
== PHOTOMETRIC_LOGL
) ? 1 : 3;
1590 td
->td_bitspersample
= 16;
1591 td
->td_sampleformat
= SAMPLEFORMAT_INT
;
1596 LogLuvCleanup(TIFF
* tif
)
1598 LogLuvState
* sp
= (LogLuvState
*)tif
->tif_data
;
1602 tif
->tif_tagmethods
.vgetfield
= sp
->vgetparent
;
1603 tif
->tif_tagmethods
.vsetfield
= sp
->vsetparent
;
1606 _TIFFfree(sp
->tbuf
);
1608 tif
->tif_data
= NULL
;
1610 _TIFFSetDefaultCompressionState(tif
);
1614 LogLuvVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1616 static const char module
[] = "LogLuvVSetField";
1617 LogLuvState
* sp
= DecoderState(tif
);
1621 case TIFFTAG_SGILOGDATAFMT
:
1622 sp
->user_datafmt
= (int) va_arg(ap
, int);
1624 * Tweak the TIFF header so that the rest of libtiff knows what
1625 * size of data will be passed between app and library, and
1626 * assume that the app knows what it is doing and is not
1627 * confused by these header manipulations...
1629 switch (sp
->user_datafmt
) {
1630 case SGILOGDATAFMT_FLOAT
:
1632 fmt
= SAMPLEFORMAT_IEEEFP
;
1634 case SGILOGDATAFMT_16BIT
:
1636 fmt
= SAMPLEFORMAT_INT
;
1638 case SGILOGDATAFMT_RAW
:
1640 fmt
= SAMPLEFORMAT_UINT
;
1641 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 1);
1643 case SGILOGDATAFMT_8BIT
:
1645 fmt
= SAMPLEFORMAT_UINT
;
1648 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
1649 "Unknown data format %d for LogLuv compression",
1653 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, bps
);
1654 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, fmt
);
1656 * Must recalculate sizes should bits/sample change.
1658 tif
->tif_tilesize
= isTiled(tif
) ? TIFFTileSize(tif
) : (tmsize_t
) -1;
1659 tif
->tif_scanlinesize
= TIFFScanlineSize(tif
);
1661 case TIFFTAG_SGILOGENCODE
:
1662 sp
->encode_meth
= (int) va_arg(ap
, int);
1663 if (sp
->encode_meth
!= SGILOGENCODE_NODITHER
&&
1664 sp
->encode_meth
!= SGILOGENCODE_RANDITHER
) {
1665 TIFFErrorExt(tif
->tif_clientdata
, module
,
1666 "Unknown encoding %d for LogLuv compression",
1672 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1677 LogLuvVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1679 LogLuvState
*sp
= (LogLuvState
*)tif
->tif_data
;
1682 case TIFFTAG_SGILOGDATAFMT
:
1683 *va_arg(ap
, int*) = sp
->user_datafmt
;
1686 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1690 static const TIFFField LogLuvFields
[] = {
1691 { TIFFTAG_SGILOGDATAFMT
, 0, 0, TIFF_SHORT
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "SGILogDataFmt", NULL
},
1692 { TIFFTAG_SGILOGENCODE
, 0, 0, TIFF_SHORT
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "SGILogEncode", NULL
}
1696 TIFFInitSGILog(TIFF
* tif
, int scheme
)
1698 static const char module
[] = "TIFFInitSGILog";
1701 assert(scheme
== COMPRESSION_SGILOG24
|| scheme
== COMPRESSION_SGILOG
);
1704 * Merge codec-specific tag information.
1706 if (!_TIFFMergeFields(tif
, LogLuvFields
,
1707 TIFFArrayCount(LogLuvFields
))) {
1708 TIFFErrorExt(tif
->tif_clientdata
, module
,
1709 "Merging SGILog codec-specific tags failed");
1714 * Allocate state block so tag methods have storage to record values.
1716 tif
->tif_data
= (uint8
*) _TIFFmalloc(sizeof (LogLuvState
));
1717 if (tif
->tif_data
== NULL
)
1719 sp
= (LogLuvState
*) tif
->tif_data
;
1720 _TIFFmemset((void*)sp
, 0, sizeof (*sp
));
1721 sp
->user_datafmt
= SGILOGDATAFMT_UNKNOWN
;
1722 sp
->encode_meth
= (scheme
== COMPRESSION_SGILOG24
) ?
1723 SGILOGENCODE_RANDITHER
: SGILOGENCODE_NODITHER
;
1724 sp
->tfunc
= _logLuvNop
;
1727 * Install codec methods.
1728 * NB: tif_decoderow & tif_encoderow are filled
1731 tif
->tif_fixuptags
= LogLuvFixupTags
;
1732 tif
->tif_setupdecode
= LogLuvSetupDecode
;
1733 tif
->tif_decodestrip
= LogLuvDecodeStrip
;
1734 tif
->tif_decodetile
= LogLuvDecodeTile
;
1735 tif
->tif_setupencode
= LogLuvSetupEncode
;
1736 tif
->tif_encodestrip
= LogLuvEncodeStrip
;
1737 tif
->tif_encodetile
= LogLuvEncodeTile
;
1738 tif
->tif_close
= LogLuvClose
;
1739 tif
->tif_cleanup
= LogLuvCleanup
;
1742 * Override parent get/set field methods.
1744 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1745 tif
->tif_tagmethods
.vgetfield
= LogLuvVGetField
; /* hook for codec tags */
1746 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1747 tif
->tif_tagmethods
.vsetfield
= LogLuvVSetField
; /* hook for codec tags */
1751 TIFFErrorExt(tif
->tif_clientdata
, module
,
1752 "%s: No space for LogLuv state block", tif
->tif_name
);
1755 #endif /* LOGLUV_SUPPORT */
1757 /* vim: set ts=8 sts=8 sw=8 noet: */