3 * Copyright (c) 1997 Greg Ward Larson
4 * Copyright (c) 1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any
11 * advertising or publicity relating to the software without the specific,
12 * prior written permission of Sam Leffler, Greg Larson and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE
19 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
31 * LogLuv compression support for high dynamic range images.
33 * Contributed by Greg Larson.
35 * LogLuv image support uses the TIFF library to store 16 or 10-bit
36 * log luminance values with 8 bits each of u and v or a 14-bit index.
38 * The codec can take as input and produce as output 32-bit IEEE float values
39 * as well as 16-bit integer values. A 16-bit luminance is interpreted
40 * as a sign bit followed by a 15-bit integer that is converted
41 * to and from a linear magnitude using the transformation:
43 * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit
45 * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real
47 * The actual conversion to world luminance units in candelas per sq. meter
48 * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
49 * This value is usually set such that a reasonable exposure comes from
50 * clamping decoded luminances above 1 to 1 in the displayed image.
52 * The 16-bit values for u and v may be converted to real values by dividing
53 * each by 32768. (This allows for negative values, which aren't useful as
54 * far as we know, but are left in case of future improvements in human
57 * Conversion from (u,v), which is actually the CIE (u',v') system for
58 * you color scientists, is accomplished by the following transformation:
60 * u = 4*x / (-2*x + 12*y + 3)
61 * v = 9*y / (-2*x + 12*y + 3)
63 * x = 9*u / (6*u - 16*v + 12)
64 * y = 4*v / (6*u - 16*v + 12)
66 * This process is greatly simplified by passing 32-bit IEEE floats
67 * for each of three CIE XYZ coordinates. The codec then takes care
68 * of conversion to and from LogLuv, though the application is still
69 * responsible for interpreting the TIFFTAG_STONITS calibration factor.
71 * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white
72 * point of (x,y)=(1/3,1/3). However, most color systems assume some other
73 * white point, such as D65, and an absolute color conversion to XYZ then
74 * to another color space with a different white point may introduce an
75 * unwanted color cast to the image. It is often desirable, therefore, to
76 * perform a white point conversion that maps the input white to [1 1 1]
77 * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT
78 * tag value. A decoder that demands absolute color calibration may use
79 * this white point tag to get back the original colors, but usually it
80 * will be ignored and the new white point will be used instead that
81 * matches the output color space.
83 * Pixel information is compressed into one of two basic encodings, depending
84 * on the setting of the compression tag, which is one of COMPRESSION_SGILOG
85 * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is
91 * COMPRESSION_SGILOG color data is stored as:
94 * |-+---------------|--------+--------|
97 * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
100 * |----------|--------------|
103 * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
104 * encoded as an index for optimal color resolution. The 10 log bits are
105 * defined by the following conversions:
107 * L = 2^((Le'+.5)/64 - 12) # real from 10-bit
109 * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real
111 * The 10 bits of the smaller format may be converted into the 15 bits of
112 * the larger format by multiplying by 4 and adding 13314. Obviously,
113 * a smaller range of magnitudes is covered (about 5 orders of magnitude
114 * instead of 38), and the lack of a sign bit means that negative luminances
115 * are not allowed. (Well, they aren't allowed in the real world, either,
116 * but they are useful for certain types of image processing.)
118 * The desired user format is controlled by the setting the internal
119 * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
120 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values
121 * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v
122 * Raw data i/o is also possible using:
123 * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel
124 * In addition, the following decoding is provided for ease of display:
125 * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values
127 * For grayscale images, we provide the following data formats:
128 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values
129 * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance
130 * SGILOGDATAFMT_8BIT = 8-bit gray monitor values
132 * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
133 * scheme by separating the logL, u and v bytes for each row and applying
134 * a PackBits type of compression. Since the 24-bit encoding is not
135 * adaptive, the 32-bit color format takes less space in many cases.
137 * Further control is provided over the conversion from higher-resolution
138 * formats to final encoded values through the pseudo tag
139 * TIFFTAG_SGILOGENCODE:
140 * SGILOGENCODE_NODITHER = do not dither encoded values
141 * SGILOGENCODE_RANDITHER = apply random dithering during encoding
143 * The default value of this tag is SGILOGENCODE_NODITHER for
144 * COMPRESSION_SGILOG to maximize run-length encoding and
145 * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn
146 * quantization errors into noise.
154 * State block for each open TIFF
155 * file using LogLuv compression/decompression.
157 typedef struct logLuvState LogLuvState
;
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 assert(sp
->tbuflen
>= npixels
);
205 tp
= (int16
*) sp
->tbuf
;
207 _TIFFmemset((void*) tp
, 0, npixels
*sizeof (tp
[0]));
209 bp
= (unsigned char*) tif
->tif_rawcp
;
211 /* get each byte string */
212 for (shft
= 2*8; (shft
-= 8) >= 0; ) {
213 for (i
= 0; i
< npixels
&& cc
> 0; )
214 if (*bp
>= 128) { /* run */
215 rc
= *bp
++ + (2-128); /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
216 b
= (int16
)(*bp
++ << shft
);
218 while (rc
-- && i
< npixels
)
220 } else { /* non-run */
221 rc
= *bp
++; /* nul is noop */
222 while (--cc
&& rc
-- && i
< npixels
)
223 tp
[i
++] |= (int16
)*bp
++ << shft
;
226 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
227 TIFFErrorExt(tif
->tif_clientdata
, module,
228 "Not enough data at row %lu (short %I64d pixels)",
229 (unsigned long) tif
->tif_row
,
230 (unsigned __int64
) (npixels
- i
));
232 TIFFErrorExt(tif
->tif_clientdata
, module,
233 "Not enough data at row %lu (short %llu pixels)",
234 (unsigned long) tif
->tif_row
,
235 (unsigned long long) (npixels
- i
));
237 tif
->tif_rawcp
= (uint8
*) bp
;
242 (*sp
->tfunc
)(sp
, op
, npixels
);
243 tif
->tif_rawcp
= (uint8
*) bp
;
249 * Decode a string of 24-bit pixels.
252 LogLuvDecode24(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
254 static const char module[] = "LogLuvDecode24";
255 LogLuvState
* sp
= DecoderState(tif
);
265 npixels
= occ
/ sp
->pixel_size
;
267 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
270 assert(sp
->tbuflen
>= npixels
);
271 tp
= (uint32
*) sp
->tbuf
;
273 /* copy to array of uint32 */
274 bp
= (unsigned char*) tif
->tif_rawcp
;
276 for (i
= 0; i
< npixels
&& cc
> 0; i
++) {
277 tp
[i
] = bp
[0] << 16 | bp
[1] << 8 | bp
[2];
281 tif
->tif_rawcp
= (uint8
*) bp
;
284 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
285 TIFFErrorExt(tif
->tif_clientdata
, module,
286 "Not enough data at row %lu (short %I64d pixels)",
287 (unsigned long) tif
->tif_row
,
288 (unsigned __int64
) (npixels
- i
));
290 TIFFErrorExt(tif
->tif_clientdata
, module,
291 "Not enough data at row %lu (short %llu pixels)",
292 (unsigned long) tif
->tif_row
,
293 (unsigned long long) (npixels
- i
));
297 (*sp
->tfunc
)(sp
, op
, npixels
);
302 * Decode a string of 32-bit pixels.
305 LogLuvDecode32(TIFF
* tif
, uint8
* op
, tmsize_t occ
, uint16 s
)
307 static const char module[] = "LogLuvDecode32";
319 sp
= DecoderState(tif
);
322 npixels
= occ
/ sp
->pixel_size
;
324 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
327 assert(sp
->tbuflen
>= npixels
);
328 tp
= (uint32
*) sp
->tbuf
;
330 _TIFFmemset((void*) tp
, 0, npixels
*sizeof (tp
[0]));
332 bp
= (unsigned char*) tif
->tif_rawcp
;
334 /* get each byte string */
335 for (shft
= 4*8; (shft
-= 8) >= 0; ) {
336 for (i
= 0; i
< npixels
&& cc
> 0; )
337 if (*bp
>= 128) { /* run */
338 rc
= *bp
++ + (2-128);
339 b
= (uint32
)*bp
++ << shft
;
340 cc
-= 2; /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
341 while (rc
-- && i
< npixels
)
343 } else { /* non-run */
344 rc
= *bp
++; /* nul is noop */
345 while (--cc
&& rc
-- && i
< npixels
)
346 tp
[i
++] |= (uint32
)*bp
++ << shft
;
349 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
350 TIFFErrorExt(tif
->tif_clientdata
, module,
351 "Not enough data at row %lu (short %I64d pixels)",
352 (unsigned long) tif
->tif_row
,
353 (unsigned __int64
) (npixels
- i
));
355 TIFFErrorExt(tif
->tif_clientdata
, module,
356 "Not enough data at row %lu (short %llu pixels)",
357 (unsigned long) tif
->tif_row
,
358 (unsigned long long) (npixels
- i
));
360 tif
->tif_rawcp
= (uint8
*) bp
;
365 (*sp
->tfunc
)(sp
, op
, npixels
);
366 tif
->tif_rawcp
= (uint8
*) bp
;
372 * Decode a strip of pixels. We break it into rows to
373 * maintain synchrony with the encode algorithm, which
377 LogLuvDecodeStrip(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
379 tmsize_t rowlen
= TIFFScanlineSize(tif
);
381 assert(cc%rowlen
== 0);
382 while (cc
&& (*tif
->tif_decoderow
)(tif
, bp
, rowlen
, s
))
383 bp
+= rowlen
, cc
-= rowlen
;
388 * Decode a tile of pixels. We break it into rows to
389 * maintain synchrony with the encode algorithm, which
393 LogLuvDecodeTile(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
395 tmsize_t rowlen
= TIFFTileRowSize(tif
);
397 assert(cc%rowlen
== 0);
398 while (cc
&& (*tif
->tif_decoderow
)(tif
, bp
, rowlen
, s
))
399 bp
+= rowlen
, cc
-= rowlen
;
404 * Encode a row of 16-bit pixels.
407 LogL16Encode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
409 LogLuvState
* sp
= EncoderState(tif
);
423 npixels
= cc
/ sp
->pixel_size
;
425 if (sp
->user_datafmt
== SGILOGDATAFMT_16BIT
)
428 tp
= (int16
*) sp
->tbuf
;
429 assert(sp
->tbuflen
>= npixels
);
430 (*sp
->tfunc
)(sp
, bp
, npixels
);
432 /* compress each byte string */
434 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
435 for (shft
= 2*8; (shft
-= 8) >= 0; )
436 for (i
= 0; i
< npixels
; i
+= rc
) {
439 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
440 if (!TIFFFlushData1(tif
))
443 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
445 mask
= 0xff << shft
; /* find next run */
446 for (beg
= i
; beg
< npixels
; beg
+= rc
) {
447 b
= (int16
) (tp
[beg
] & mask
);
449 while (rc
< 127+2 && beg
+rc
< npixels
&&
450 (tp
[beg
+rc
] & mask
) == b
)
453 break; /* long enough */
455 if (beg
-i
> 1 && beg
-i
< MINRUN
) {
456 b
= (int16
) (tp
[i
] & mask
);/*check short run */
458 while ((tp
[j
++] & mask
) == b
)
460 *op
++ = (uint8
)(128-2+j
-i
);
461 *op
++ = (uint8
)(b
>> shft
);
467 while (i
< beg
) { /* write out non-run */
468 if ((j
= beg
-i
) > 127) j
= 127;
471 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
472 if (!TIFFFlushData1(tif
))
475 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
477 *op
++ = (uint8
) j
; occ
--;
479 *op
++ = (uint8
) (tp
[i
++] >> shft
& 0xff);
483 if (rc
>= MINRUN
) { /* write out run */
484 *op
++ = (uint8
) (128-2+rc
);
485 *op
++ = (uint8
) (tp
[beg
] >> shft
& 0xff);
491 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
497 * Encode a row of 24-bit pixels.
500 LogLuvEncode24(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
502 LogLuvState
* sp
= EncoderState(tif
);
511 npixels
= cc
/ sp
->pixel_size
;
513 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
516 tp
= (uint32
*) sp
->tbuf
;
517 assert(sp
->tbuflen
>= npixels
);
518 (*sp
->tfunc
)(sp
, bp
, npixels
);
520 /* write out encoded pixels */
522 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
523 for (i
= npixels
; i
--; ) {
526 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
527 if (!TIFFFlushData1(tif
))
530 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
532 *op
++ = (uint8
)(*tp
>> 16);
533 *op
++ = (uint8
)(*tp
>> 8 & 0xff);
534 *op
++ = (uint8
)(*tp
++ & 0xff);
538 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
544 * Encode a row of 32-bit pixels.
547 LogLuvEncode32(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
549 LogLuvState
* sp
= EncoderState(tif
);
564 npixels
= cc
/ sp
->pixel_size
;
566 if (sp
->user_datafmt
== SGILOGDATAFMT_RAW
)
569 tp
= (uint32
*) sp
->tbuf
;
570 assert(sp
->tbuflen
>= npixels
);
571 (*sp
->tfunc
)(sp
, bp
, npixels
);
573 /* compress each byte string */
575 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
576 for (shft
= 4*8; (shft
-= 8) >= 0; )
577 for (i
= 0; i
< npixels
; i
+= rc
) {
580 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
581 if (!TIFFFlushData1(tif
))
584 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
586 mask
= 0xff << shft
; /* find next run */
587 for (beg
= i
; beg
< npixels
; beg
+= rc
) {
590 while (rc
< 127+2 && beg
+rc
< npixels
&&
591 (tp
[beg
+rc
] & mask
) == b
)
594 break; /* long enough */
596 if (beg
-i
> 1 && beg
-i
< MINRUN
) {
597 b
= tp
[i
] & mask
; /* check short run */
599 while ((tp
[j
++] & mask
) == b
)
601 *op
++ = (uint8
)(128-2+j
-i
);
602 *op
++ = (uint8
)(b
>> shft
);
608 while (i
< beg
) { /* write out non-run */
609 if ((j
= beg
-i
) > 127) j
= 127;
612 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
613 if (!TIFFFlushData1(tif
))
616 occ
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
618 *op
++ = (uint8
) j
; occ
--;
620 *op
++ = (uint8
)(tp
[i
++] >> shft
& 0xff);
624 if (rc
>= MINRUN
) { /* write out run */
625 *op
++ = (uint8
) (128-2+rc
);
626 *op
++ = (uint8
)(tp
[beg
] >> shft
& 0xff);
632 tif
->tif_rawcc
= tif
->tif_rawdatasize
- occ
;
638 * Encode a strip of pixels. We break it into rows to
639 * avoid encoding runs across row boundaries.
642 LogLuvEncodeStrip(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
644 tmsize_t rowlen
= TIFFScanlineSize(tif
);
646 assert(cc%rowlen
== 0);
647 while (cc
&& (*tif
->tif_encoderow
)(tif
, bp
, rowlen
, s
) == 1)
648 bp
+= rowlen
, cc
-= rowlen
;
653 * Encode a tile of pixels. We break it into rows to
654 * avoid encoding runs across row boundaries.
657 LogLuvEncodeTile(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
659 tmsize_t rowlen
= TIFFTileRowSize(tif
);
661 assert(cc%rowlen
== 0);
662 while (cc
&& (*tif
->tif_encoderow
)(tif
, bp
, rowlen
, s
) == 1)
663 bp
+= rowlen
, cc
-= rowlen
;
668 * Encode/Decode functions for converting to and from user formats.
674 #define U_NEU 0.210526316
675 #define V_NEU 0.473684211
680 #define M_LN2 0.69314718055994530942
683 #define M_PI 3.14159265358979323846
685 #define log2(x) ((1./M_LN2)*log(x))
686 #define exp2(x) exp(M_LN2*(x))
688 #define itrunc(x,m) ((m)==SGILOGENCODE_NODITHER ? \
690 (int)((x) + rand()*(1./RAND_MAX) - .5))
696 LogL16toY(int p16
) /* compute luminance from 16-bit LogL */
698 int Le
= p16
& 0x7fff;
703 Y
= exp(M_LN2
/256.*(Le
+.5) - M_LN2
*64.);
704 return (!(p16
& 0x8000) ? Y
: -Y
);
711 LogL16fromY(double Y
, int em
) /* get 16-bit LogL from Y */
713 if (Y
>= 1.8371976e19
)
715 if (Y
<= -1.8371976e19
)
717 if (Y
> 5.4136769e-20)
718 return itrunc(256.*(log2(Y
) + 64.), em
);
719 if (Y
< -5.4136769e-20)
720 return (~0x7fff | itrunc(256.*(log2(-Y
) + 64.), em
));
725 L16toY(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
727 int16
* l16
= (int16
*) sp
->tbuf
;
728 float* yp
= (float*) op
;
731 *yp
++ = (float)LogL16toY(*l16
++);
735 L16toGry(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
737 int16
* l16
= (int16
*) sp
->tbuf
;
738 uint8
* gp
= (uint8
*) op
;
741 double Y
= LogL16toY(*l16
++);
742 *gp
++ = (uint8
) ((Y
<= 0.) ? 0 : (Y
>= 1.) ? 255 : (int)(256.*sqrt(Y
)));
747 L16fromY(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
749 int16
* l16
= (int16
*) sp
->tbuf
;
750 float* yp
= (float*) op
;
753 *l16
++ = (int16
) (LogL16fromY(*yp
++, sp
->encode_meth
));
760 XYZtoRGB24(float xyz
[3], uint8 rgb
[3])
763 /* assume CCIR-709 primaries */
764 r
= 2.690*xyz
[0] + -1.276*xyz
[1] + -0.414*xyz
[2];
765 g
= -1.022*xyz
[0] + 1.978*xyz
[1] + 0.044*xyz
[2];
766 b
= 0.061*xyz
[0] + -0.224*xyz
[1] + 1.163*xyz
[2];
767 /* assume 2.0 gamma for speed */
768 /* could use integer sqrt approx., but this is probably faster */
769 rgb
[0] = (uint8
)((r
<=0.) ? 0 : (r
>= 1.) ? 255 : (int)(256.*sqrt(r
)));
770 rgb
[1] = (uint8
)((g
<=0.) ? 0 : (g
>= 1.) ? 255 : (int)(256.*sqrt(g
)));
771 rgb
[2] = (uint8
)((b
<=0.) ? 0 : (b
>= 1.) ? 255 : (int)(256.*sqrt(b
)));
778 LogL10toY(int p10
) /* compute luminance from 10-bit LogL */
782 return (exp(M_LN2
/64.*(p10
+.5) - M_LN2
*12.));
789 LogL10fromY(double Y
, int em
) /* get 10-bit LogL from Y */
793 else if (Y
<= .00024283)
796 return itrunc(64.*(log2(Y
) + 12.), em
);
800 #define uv2ang(u, v) ( (NANGLES*.499999999/M_PI) \
801 * atan2((v)-V_NEU,(u)-U_NEU) + .5*NANGLES )
804 oog_encode(double u
, double v
) /* encode out-of-gamut chroma */
806 static int oog_table
[NANGLES
];
807 static int initialized
= 0;
810 if (!initialized
) { /* set up perimeter table */
811 double eps
[NANGLES
], ua
, va
, ang
, epsa
;
813 for (i
= NANGLES
; i
--; )
815 for (vi
= UV_NVS
; vi
--; ) {
816 va
= UV_VSTART
+ (vi
+.5)*UV_SQSIZ
;
817 ustep
= uv_row
[vi
].nus
-1;
818 if (vi
== UV_NVS
-1 || vi
== 0 || ustep
<= 0)
820 for (ui
= uv_row
[vi
].nus
-1; ui
>= 0; ui
-= ustep
) {
821 ua
= uv_row
[vi
].ustart
+ (ui
+.5)*UV_SQSIZ
;
822 ang
= uv2ang(ua
, va
);
824 epsa
= fabs(ang
- (i
+.5));
826 oog_table
[i
] = uv_row
[vi
].ncum
+ ui
;
831 for (i
= NANGLES
; i
--; ) /* fill any holes */
834 for (i1
= 1; i1
< NANGLES
/2; i1
++)
835 if (eps
[(i
+i1
)%NANGLES
] < 1.5)
837 for (i2
= 1; i2
< NANGLES
/2; i2
++)
838 if (eps
[(i
+NANGLES
-i2
)%NANGLES
] < 1.5)
842 oog_table
[(i
+i1
)%NANGLES
];
845 oog_table
[(i
+NANGLES
-i2
)%NANGLES
];
849 i
= (int) uv2ang(u
, v
); /* look up hue angle */
850 return (oog_table
[i
]);
860 uv_encode(double u
, double v
, int em
) /* encode (u',v') coordinates */
865 return oog_encode(u
, v
);
866 vi
= itrunc((v
- UV_VSTART
)*(1./UV_SQSIZ
), em
);
868 return oog_encode(u
, v
);
869 if (u
< uv_row
[vi
].ustart
)
870 return oog_encode(u
, v
);
871 ui
= itrunc((u
- uv_row
[vi
].ustart
)*(1./UV_SQSIZ
), em
);
872 if (ui
>= uv_row
[vi
].nus
)
873 return oog_encode(u
, v
);
875 return (uv_row
[vi
].ncum
+ ui
);
882 uv_decode(double *up
, double *vp
, int c
) /* decode (u',v') index */
887 if (c
< 0 || c
>= UV_NDIVS
)
889 lower
= 0; /* binary search */
891 while (upper
- lower
> 1) {
892 vi
= (lower
+ upper
) >> 1;
893 ui
= c
- uv_row
[vi
].ncum
;
904 ui
= c
- uv_row
[vi
].ncum
;
905 *up
= uv_row
[vi
].ustart
+ (ui
+.5)*UV_SQSIZ
;
906 *vp
= UV_VSTART
+ (vi
+.5)*UV_SQSIZ
;
914 LogLuv24toXYZ(uint32 p
, float XYZ
[3])
917 double L
, u
, v
, s
, x
, y
;
918 /* decode luminance */
919 L
= LogL10toY(p
>>14 & 0x3ff);
921 XYZ
[0] = XYZ
[1] = XYZ
[2] = 0.;
926 if (uv_decode(&u
, &v
, Ce
) < 0) {
927 u
= U_NEU
; v
= V_NEU
;
929 s
= 1./(6.*u
- 16.*v
+ 12.);
933 XYZ
[0] = (float)(x
/y
* L
);
935 XYZ
[2] = (float)((1.-x
-y
)/y
* L
);
942 LogLuv24fromXYZ(float XYZ
[3], int em
)
946 /* encode luminance */
947 Le
= LogL10fromY(XYZ
[1], em
);
949 s
= XYZ
[0] + 15.*XYZ
[1] + 3.*XYZ
[2];
950 if (!Le
|| s
<= 0.) {
957 Ce
= uv_encode(u
, v
, em
);
958 if (Ce
< 0) /* never happens */
959 Ce
= uv_encode(U_NEU
, V_NEU
, SGILOGENCODE_NODITHER
);
960 /* combine encodings */
961 return (Le
<< 14 | Ce
);
965 Luv24toXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
967 uint32
* luv
= (uint32
*) sp
->tbuf
;
968 float* xyz
= (float*) op
;
971 LogLuv24toXYZ(*luv
, xyz
);
978 Luv24toLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
980 uint32
* luv
= (uint32
*) sp
->tbuf
;
981 int16
* luv3
= (int16
*) op
;
986 *luv3
++ = (int16
)((*luv
>> 12 & 0xffd) + 13314);
987 if (uv_decode(&u
, &v
, *luv
&0x3fff) < 0) {
991 *luv3
++ = (int16
)(u
* (1L<<15));
992 *luv3
++ = (int16
)(v
* (1L<<15));
998 Luv24toRGB(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1000 uint32
* luv
= (uint32
*) sp
->tbuf
;
1001 uint8
* rgb
= (uint8
*) op
;
1006 LogLuv24toXYZ(*luv
++, xyz
);
1007 XYZtoRGB24(xyz
, rgb
);
1013 Luv24fromXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1015 uint32
* luv
= (uint32
*) sp
->tbuf
;
1016 float* xyz
= (float*) op
;
1019 *luv
++ = LogLuv24fromXYZ(xyz
, sp
->encode_meth
);
1025 Luv24fromLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1027 uint32
* luv
= (uint32
*) sp
->tbuf
;
1028 int16
* luv3
= (int16
*) op
;
1035 else if (luv3
[0] >= (1<<12)+3314)
1037 else if (sp
->encode_meth
== SGILOGENCODE_NODITHER
)
1038 Le
= (luv3
[0]-3314) >> 2;
1040 Le
= itrunc(.25*(luv3
[0]-3314.), sp
->encode_meth
);
1042 Ce
= uv_encode((luv3
[1]+.5)/(1<<15), (luv3
[2]+.5)/(1<<15),
1044 if (Ce
< 0) /* never happens */
1045 Ce
= uv_encode(U_NEU
, V_NEU
, SGILOGENCODE_NODITHER
);
1046 *luv
++ = (uint32
)Le
<< 14 | Ce
;
1055 LogLuv32toXYZ(uint32 p
, float XYZ
[3])
1057 double L
, u
, v
, s
, x
, y
;
1058 /* decode luminance */
1059 L
= LogL16toY((int)p
>> 16);
1061 XYZ
[0] = XYZ
[1] = XYZ
[2] = 0.;
1065 u
= 1./UVSCALE
* ((p
>>8 & 0xff) + .5);
1066 v
= 1./UVSCALE
* ((p
& 0xff) + .5);
1067 s
= 1./(6.*u
- 16.*v
+ 12.);
1070 /* convert to XYZ */
1071 XYZ
[0] = (float)(x
/y
* L
);
1073 XYZ
[2] = (float)((1.-x
-y
)/y
* L
);
1080 LogLuv32fromXYZ(float XYZ
[3], int em
)
1082 unsigned int Le
, ue
, ve
;
1084 /* encode luminance */
1085 Le
= (unsigned int)LogL16fromY(XYZ
[1], em
);
1087 s
= XYZ
[0] + 15.*XYZ
[1] + 3.*XYZ
[2];
1088 if (!Le
|| s
<= 0.) {
1095 if (u
<= 0.) ue
= 0;
1096 else ue
= itrunc(UVSCALE
*u
, em
);
1097 if (ue
> 255) ue
= 255;
1098 if (v
<= 0.) ve
= 0;
1099 else ve
= itrunc(UVSCALE
*v
, em
);
1100 if (ve
> 255) ve
= 255;
1101 /* combine encodings */
1102 return (Le
<< 16 | ue
<< 8 | ve
);
1106 Luv32toXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1108 uint32
* luv
= (uint32
*) sp
->tbuf
;
1109 float* xyz
= (float*) op
;
1112 LogLuv32toXYZ(*luv
++, xyz
);
1118 Luv32toLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1120 uint32
* luv
= (uint32
*) sp
->tbuf
;
1121 int16
* luv3
= (int16
*) op
;
1126 *luv3
++ = (int16
)(*luv
>> 16);
1127 u
= 1./UVSCALE
* ((*luv
>>8 & 0xff) + .5);
1128 v
= 1./UVSCALE
* ((*luv
& 0xff) + .5);
1129 *luv3
++ = (int16
)(u
* (1L<<15));
1130 *luv3
++ = (int16
)(v
* (1L<<15));
1136 Luv32toRGB(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1138 uint32
* luv
= (uint32
*) sp
->tbuf
;
1139 uint8
* rgb
= (uint8
*) op
;
1144 LogLuv32toXYZ(*luv
++, xyz
);
1145 XYZtoRGB24(xyz
, rgb
);
1151 Luv32fromXYZ(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1153 uint32
* luv
= (uint32
*) sp
->tbuf
;
1154 float* xyz
= (float*) op
;
1157 *luv
++ = LogLuv32fromXYZ(xyz
, sp
->encode_meth
);
1163 Luv32fromLuv48(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1165 uint32
* luv
= (uint32
*) sp
->tbuf
;
1166 int16
* luv3
= (int16
*) op
;
1168 if (sp
->encode_meth
== SGILOGENCODE_NODITHER
) {
1170 *luv
++ = (uint32
)luv3
[0] << 16 |
1171 (luv3
[1]*(uint32
)(UVSCALE
+.5) >> 7 & 0xff00) |
1172 (luv3
[2]*(uint32
)(UVSCALE
+.5) >> 15 & 0xff);
1178 *luv
++ = (uint32
)luv3
[0] << 16 |
1179 (itrunc(luv3
[1]*(UVSCALE
/(1<<15)), sp
->encode_meth
) << 8 & 0xff00) |
1180 (itrunc(luv3
[2]*(UVSCALE
/(1<<15)), sp
->encode_meth
) & 0xff);
1186 _logLuvNop(LogLuvState
* sp
, uint8
* op
, tmsize_t n
)
1188 (void) sp
; (void) op
; (void) n
;
1192 LogL16GuessDataFmt(TIFFDirectory
*td
)
1194 #define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f))
1195 switch (PACK(td
->td_samplesperpixel
, td
->td_bitspersample
, td
->td_sampleformat
)) {
1196 case PACK(1, 32, SAMPLEFORMAT_IEEEFP
):
1197 return (SGILOGDATAFMT_FLOAT
);
1198 case PACK(1, 16, SAMPLEFORMAT_VOID
):
1199 case PACK(1, 16, SAMPLEFORMAT_INT
):
1200 case PACK(1, 16, SAMPLEFORMAT_UINT
):
1201 return (SGILOGDATAFMT_16BIT
);
1202 case PACK(1, 8, SAMPLEFORMAT_VOID
):
1203 case PACK(1, 8, SAMPLEFORMAT_UINT
):
1204 return (SGILOGDATAFMT_8BIT
);
1207 return (SGILOGDATAFMT_UNKNOWN
);
1211 multiply_ms(tmsize_t m1
, tmsize_t m2
)
1213 tmsize_t bytes
= m1
* m2
;
1215 if (m1
&& bytes
/ m1
!= m2
)
1222 LogL16InitState(TIFF
* tif
)
1224 static const char module[] = "LogL16InitState";
1225 TIFFDirectory
*td
= &tif
->tif_dir
;
1226 LogLuvState
* sp
= DecoderState(tif
);
1229 assert(td
->td_photometric
== PHOTOMETRIC_LOGL
);
1231 /* for some reason, we can't do this in TIFFInitLogL16 */
1232 if (sp
->user_datafmt
== SGILOGDATAFMT_UNKNOWN
)
1233 sp
->user_datafmt
= LogL16GuessDataFmt(td
);
1234 switch (sp
->user_datafmt
) {
1235 case SGILOGDATAFMT_FLOAT
:
1236 sp
->pixel_size
= sizeof (float);
1238 case SGILOGDATAFMT_16BIT
:
1239 sp
->pixel_size
= sizeof (int16
);
1241 case SGILOGDATAFMT_8BIT
:
1242 sp
->pixel_size
= sizeof (uint8
);
1245 TIFFErrorExt(tif
->tif_clientdata
, module,
1246 "No support for converting user data format to LogL");
1250 sp
->tbuflen
= multiply_ms(td
->td_tilewidth
, td
->td_tilelength
);
1252 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_rowsperstrip
);
1253 if (multiply_ms(sp
->tbuflen
, sizeof (int16
)) == 0 ||
1254 (sp
->tbuf
= (uint8
*) _TIFFmalloc(sp
->tbuflen
* sizeof (int16
))) == NULL
) {
1255 TIFFErrorExt(tif
->tif_clientdata
, module, "No space for SGILog translation buffer");
1262 LogLuvGuessDataFmt(TIFFDirectory
*td
)
1267 * If the user didn't tell us their datafmt,
1268 * take our best guess from the bitspersample.
1270 #define PACK(a,b) (((a)<<3)|(b))
1271 switch (PACK(td
->td_bitspersample
, td
->td_sampleformat
)) {
1272 case PACK(32, SAMPLEFORMAT_IEEEFP
):
1273 guess
= SGILOGDATAFMT_FLOAT
;
1275 case PACK(32, SAMPLEFORMAT_VOID
):
1276 case PACK(32, SAMPLEFORMAT_UINT
):
1277 case PACK(32, SAMPLEFORMAT_INT
):
1278 guess
= SGILOGDATAFMT_RAW
;
1280 case PACK(16, SAMPLEFORMAT_VOID
):
1281 case PACK(16, SAMPLEFORMAT_INT
):
1282 case PACK(16, SAMPLEFORMAT_UINT
):
1283 guess
= SGILOGDATAFMT_16BIT
;
1285 case PACK( 8, SAMPLEFORMAT_VOID
):
1286 case PACK( 8, SAMPLEFORMAT_UINT
):
1287 guess
= SGILOGDATAFMT_8BIT
;
1290 guess
= SGILOGDATAFMT_UNKNOWN
;
1295 * Double-check samples per pixel.
1297 switch (td
->td_samplesperpixel
) {
1299 if (guess
!= SGILOGDATAFMT_RAW
)
1300 guess
= SGILOGDATAFMT_UNKNOWN
;
1303 if (guess
== SGILOGDATAFMT_RAW
)
1304 guess
= SGILOGDATAFMT_UNKNOWN
;
1307 guess
= SGILOGDATAFMT_UNKNOWN
;
1314 LogLuvInitState(TIFF
* tif
)
1316 static const char module[] = "LogLuvInitState";
1317 TIFFDirectory
* td
= &tif
->tif_dir
;
1318 LogLuvState
* sp
= DecoderState(tif
);
1321 assert(td
->td_photometric
== PHOTOMETRIC_LOGLUV
);
1323 /* for some reason, we can't do this in TIFFInitLogLuv */
1324 if (td
->td_planarconfig
!= PLANARCONFIG_CONTIG
) {
1325 TIFFErrorExt(tif
->tif_clientdata
, module,
1326 "SGILog compression cannot handle non-contiguous data");
1329 if (sp
->user_datafmt
== SGILOGDATAFMT_UNKNOWN
)
1330 sp
->user_datafmt
= LogLuvGuessDataFmt(td
);
1331 switch (sp
->user_datafmt
) {
1332 case SGILOGDATAFMT_FLOAT
:
1333 sp
->pixel_size
= 3*sizeof (float);
1335 case SGILOGDATAFMT_16BIT
:
1336 sp
->pixel_size
= 3*sizeof (int16
);
1338 case SGILOGDATAFMT_RAW
:
1339 sp
->pixel_size
= sizeof (uint32
);
1341 case SGILOGDATAFMT_8BIT
:
1342 sp
->pixel_size
= 3*sizeof (uint8
);
1345 TIFFErrorExt(tif
->tif_clientdata
, module,
1346 "No support for converting user data format to LogLuv");
1350 sp
->tbuflen
= multiply_ms(td
->td_tilewidth
, td
->td_tilelength
);
1352 sp
->tbuflen
= multiply_ms(td
->td_imagewidth
, td
->td_rowsperstrip
);
1353 if (multiply_ms(sp
->tbuflen
, sizeof (uint32
)) == 0 ||
1354 (sp
->tbuf
= (uint8
*) _TIFFmalloc(sp
->tbuflen
* sizeof (uint32
))) == NULL
) {
1355 TIFFErrorExt(tif
->tif_clientdata
, module, "No space for SGILog translation buffer");
1362 LogLuvFixupTags(TIFF
* tif
)
1369 LogLuvSetupDecode(TIFF
* tif
)
1371 static const char module[] = "LogLuvSetupDecode";
1372 LogLuvState
* sp
= DecoderState(tif
);
1373 TIFFDirectory
* td
= &tif
->tif_dir
;
1375 tif
->tif_postdecode
= _TIFFNoPostDecode
;
1376 switch (td
->td_photometric
) {
1377 case PHOTOMETRIC_LOGLUV
:
1378 if (!LogLuvInitState(tif
))
1380 if (td
->td_compression
== COMPRESSION_SGILOG24
) {
1381 tif
->tif_decoderow
= LogLuvDecode24
;
1382 switch (sp
->user_datafmt
) {
1383 case SGILOGDATAFMT_FLOAT
:
1384 sp
->tfunc
= Luv24toXYZ
;
1386 case SGILOGDATAFMT_16BIT
:
1387 sp
->tfunc
= Luv24toLuv48
;
1389 case SGILOGDATAFMT_8BIT
:
1390 sp
->tfunc
= Luv24toRGB
;
1394 tif
->tif_decoderow
= LogLuvDecode32
;
1395 switch (sp
->user_datafmt
) {
1396 case SGILOGDATAFMT_FLOAT
:
1397 sp
->tfunc
= Luv32toXYZ
;
1399 case SGILOGDATAFMT_16BIT
:
1400 sp
->tfunc
= Luv32toLuv48
;
1402 case SGILOGDATAFMT_8BIT
:
1403 sp
->tfunc
= Luv32toRGB
;
1408 case PHOTOMETRIC_LOGL
:
1409 if (!LogL16InitState(tif
))
1411 tif
->tif_decoderow
= LogL16Decode
;
1412 switch (sp
->user_datafmt
) {
1413 case SGILOGDATAFMT_FLOAT
:
1416 case SGILOGDATAFMT_8BIT
:
1417 sp
->tfunc
= L16toGry
;
1422 TIFFErrorExt(tif
->tif_clientdata
, module,
1423 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1424 td
->td_photometric
, "must be either LogLUV or LogL");
1431 LogLuvSetupEncode(TIFF
* tif
)
1433 static const char module[] = "LogLuvSetupEncode";
1434 LogLuvState
* sp
= EncoderState(tif
);
1435 TIFFDirectory
* td
= &tif
->tif_dir
;
1437 switch (td
->td_photometric
) {
1438 case PHOTOMETRIC_LOGLUV
:
1439 if (!LogLuvInitState(tif
))
1441 if (td
->td_compression
== COMPRESSION_SGILOG24
) {
1442 tif
->tif_encoderow
= LogLuvEncode24
;
1443 switch (sp
->user_datafmt
) {
1444 case SGILOGDATAFMT_FLOAT
:
1445 sp
->tfunc
= Luv24fromXYZ
;
1447 case SGILOGDATAFMT_16BIT
:
1448 sp
->tfunc
= Luv24fromLuv48
;
1450 case SGILOGDATAFMT_RAW
:
1456 tif
->tif_encoderow
= LogLuvEncode32
;
1457 switch (sp
->user_datafmt
) {
1458 case SGILOGDATAFMT_FLOAT
:
1459 sp
->tfunc
= Luv32fromXYZ
;
1461 case SGILOGDATAFMT_16BIT
:
1462 sp
->tfunc
= Luv32fromLuv48
;
1464 case SGILOGDATAFMT_RAW
:
1471 case PHOTOMETRIC_LOGL
:
1472 if (!LogL16InitState(tif
))
1474 tif
->tif_encoderow
= LogL16Encode
;
1475 switch (sp
->user_datafmt
) {
1476 case SGILOGDATAFMT_FLOAT
:
1477 sp
->tfunc
= L16fromY
;
1479 case SGILOGDATAFMT_16BIT
:
1486 TIFFErrorExt(tif
->tif_clientdata
, module,
1487 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1488 td
->td_photometric
, "must be either LogLUV or LogL");
1493 TIFFErrorExt(tif
->tif_clientdata
, module,
1494 "SGILog compression supported only for %s, or raw data",
1495 td
->td_photometric
== PHOTOMETRIC_LOGL
? "Y, L" : "XYZ, Luv");
1500 LogLuvClose(TIFF
* tif
)
1502 TIFFDirectory
*td
= &tif
->tif_dir
;
1505 * For consistency, we always want to write out the same
1506 * bitspersample and sampleformat for our TIFF file,
1507 * regardless of the data format being used by the application.
1508 * Since this routine is called after tags have been set but
1509 * before they have been recorded in the file, we reset them here.
1511 td
->td_samplesperpixel
=
1512 (td
->td_photometric
== PHOTOMETRIC_LOGL
) ? 1 : 3;
1513 td
->td_bitspersample
= 16;
1514 td
->td_sampleformat
= SAMPLEFORMAT_INT
;
1518 LogLuvCleanup(TIFF
* tif
)
1520 LogLuvState
* sp
= (LogLuvState
*)tif
->tif_data
;
1524 tif
->tif_tagmethods
.vgetfield
= sp
->vgetparent
;
1525 tif
->tif_tagmethods
.vsetfield
= sp
->vsetparent
;
1528 _TIFFfree(sp
->tbuf
);
1530 tif
->tif_data
= NULL
;
1532 _TIFFSetDefaultCompressionState(tif
);
1536 LogLuvVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1538 static const char module[] = "LogLuvVSetField";
1539 LogLuvState
* sp
= DecoderState(tif
);
1543 case TIFFTAG_SGILOGDATAFMT
:
1544 sp
->user_datafmt
= (int) va_arg(ap
, int);
1546 * Tweak the TIFF header so that the rest of libtiff knows what
1547 * size of data will be passed between app and library, and
1548 * assume that the app knows what it is doing and is not
1549 * confused by these header manipulations...
1551 switch (sp
->user_datafmt
) {
1552 case SGILOGDATAFMT_FLOAT
:
1553 bps
= 32, fmt
= SAMPLEFORMAT_IEEEFP
;
1555 case SGILOGDATAFMT_16BIT
:
1556 bps
= 16, fmt
= SAMPLEFORMAT_INT
;
1558 case SGILOGDATAFMT_RAW
:
1559 bps
= 32, fmt
= SAMPLEFORMAT_UINT
;
1560 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 1);
1562 case SGILOGDATAFMT_8BIT
:
1563 bps
= 8, fmt
= SAMPLEFORMAT_UINT
;
1566 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
1567 "Unknown data format %d for LogLuv compression",
1571 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, bps
);
1572 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, fmt
);
1574 * Must recalculate sizes should bits/sample change.
1576 tif
->tif_tilesize
= isTiled(tif
) ? TIFFTileSize(tif
) : (tmsize_t
) -1;
1577 tif
->tif_scanlinesize
= TIFFScanlineSize(tif
);
1579 case TIFFTAG_SGILOGENCODE
:
1580 sp
->encode_meth
= (int) va_arg(ap
, int);
1581 if (sp
->encode_meth
!= SGILOGENCODE_NODITHER
&&
1582 sp
->encode_meth
!= SGILOGENCODE_RANDITHER
) {
1583 TIFFErrorExt(tif
->tif_clientdata
, module,
1584 "Unknown encoding %d for LogLuv compression",
1590 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1595 LogLuvVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1597 LogLuvState
*sp
= (LogLuvState
*)tif
->tif_data
;
1600 case TIFFTAG_SGILOGDATAFMT
:
1601 *va_arg(ap
, int*) = sp
->user_datafmt
;
1604 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1608 static const TIFFField LogLuvFields
[] = {
1609 { TIFFTAG_SGILOGDATAFMT
, 0, 0, TIFF_SHORT
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "SGILogDataFmt", NULL
},
1610 { TIFFTAG_SGILOGENCODE
, 0, 0, TIFF_SHORT
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, TRUE
, FALSE
, "SGILogEncode", NULL
}
1614 TIFFInitSGILog(TIFF
* tif
, int scheme
)
1616 static const char module[] = "TIFFInitSGILog";
1619 assert(scheme
== COMPRESSION_SGILOG24
|| scheme
== COMPRESSION_SGILOG
);
1622 * Merge codec-specific tag information.
1624 if (!_TIFFMergeFields(tif
, LogLuvFields
,
1625 TIFFArrayCount(LogLuvFields
))) {
1626 TIFFErrorExt(tif
->tif_clientdata
, module,
1627 "Merging SGILog codec-specific tags failed");
1632 * Allocate state block so tag methods have storage to record values.
1634 tif
->tif_data
= (uint8
*) _TIFFmalloc(sizeof (LogLuvState
));
1635 if (tif
->tif_data
== NULL
)
1637 sp
= (LogLuvState
*) tif
->tif_data
;
1638 _TIFFmemset((void*)sp
, 0, sizeof (*sp
));
1639 sp
->user_datafmt
= SGILOGDATAFMT_UNKNOWN
;
1640 sp
->encode_meth
= (scheme
== COMPRESSION_SGILOG24
) ?
1641 SGILOGENCODE_RANDITHER
: SGILOGENCODE_NODITHER
;
1642 sp
->tfunc
= _logLuvNop
;
1645 * Install codec methods.
1646 * NB: tif_decoderow & tif_encoderow are filled
1649 tif
->tif_fixuptags
= LogLuvFixupTags
;
1650 tif
->tif_setupdecode
= LogLuvSetupDecode
;
1651 tif
->tif_decodestrip
= LogLuvDecodeStrip
;
1652 tif
->tif_decodetile
= LogLuvDecodeTile
;
1653 tif
->tif_setupencode
= LogLuvSetupEncode
;
1654 tif
->tif_encodestrip
= LogLuvEncodeStrip
;
1655 tif
->tif_encodetile
= LogLuvEncodeTile
;
1656 tif
->tif_close
= LogLuvClose
;
1657 tif
->tif_cleanup
= LogLuvCleanup
;
1660 * Override parent get/set field methods.
1662 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1663 tif
->tif_tagmethods
.vgetfield
= LogLuvVGetField
; /* hook for codec tags */
1664 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1665 tif
->tif_tagmethods
.vsetfield
= LogLuvVSetField
; /* hook for codec tags */
1669 TIFFErrorExt(tif
->tif_clientdata
, module,
1670 "%s: No space for LogLuv state block", tif
->tif_name
);
1673 #endif /* LOGLUV_SUPPORT */
1675 /* vim: set ts=8 sts=8 sw=8 noet: */