]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_luv.c
Added Calctrl.cpp to sources list
[wxWidgets.git] / src / tiff / tif_luv.c
1 /*
2 * Copyright (c) 1997 Greg Ward Larson
3 * Copyright (c) 1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any
10 * advertising or publicity relating to the software without the specific,
11 * prior written permission of Sam Leffler, Greg Larson and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE
18 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include "tiffiop.h"
26 #ifdef LOGLUV_SUPPORT
27
28 /*
29 * TIFF Library.
30 * LogLuv compression support for high dynamic range images.
31 *
32 * Contributed by Greg Larson.
33 *
34 * LogLuv image support uses the TIFF library to store 16 or 10-bit
35 * log luminance values with 8 bits each of u and v or a 14-bit index.
36 *
37 * The codec can take as input and produce as output 32-bit IEEE float values
38 * as well as 16-bit integer values. A 16-bit luminance is interpreted
39 * as a sign bit followed by a 15-bit integer that is converted
40 * to and from a linear magnitude using the transformation:
41 *
42 * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit
43 *
44 * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real
45 *
46 * The actual conversion to world luminance units in candelas per sq. meter
47 * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
48 * This value is usually set such that a reasonable exposure comes from
49 * clamping decoded luminances above 1 to 1 in the displayed image.
50 *
51 * The 16-bit values for u and v may be converted to real values by dividing
52 * each by 32768. (This allows for negative values, which aren't useful as
53 * far as we know, but are left in case of future improvements in human
54 * color vision.)
55 *
56 * Conversion from (u,v), which is actually the CIE (u',v') system for
57 * you color scientists, is accomplished by the following transformation:
58 *
59 * u = 4*x / (-2*x + 12*y + 3)
60 * v = 9*y / (-2*x + 12*y + 3)
61 *
62 * x = 9*u / (6*u - 16*v + 12)
63 * y = 4*v / (6*u - 16*v + 12)
64 *
65 * This process is greatly simplified by passing 32-bit IEEE floats
66 * for each of three CIE XYZ coordinates. The codec then takes care
67 * of conversion to and from LogLuv, though the application is still
68 * responsible for interpreting the TIFFTAG_STONITS calibration factor.
69 *
70 * The information is compressed into one of two basic encodings, depending on
71 * the setting of the compression tag, which is one of COMPRESSION_SGILOG
72 * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is
73 * stored as:
74 *
75 * 1 15
76 * |-+---------------|
77 *
78 * COMPRESSION_SGILOG color data is stored as:
79 *
80 * 1 15 8 8
81 * |-+---------------|--------+--------|
82 * S Le ue ve
83 *
84 * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
85 *
86 * 10 14
87 * |----------|--------------|
88 * Le' Ce
89 *
90 * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
91 * encoded as an index for optimal color resolution. The 10 log bits are
92 * defined by the following conversions:
93 *
94 * L = 2^((Le'+.5)/64 - 12) # real from 10-bit
95 *
96 * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real
97 *
98 * The 10 bits of the smaller format may be converted into the 15 bits of
99 * the larger format by multiplying by 4 and adding 13314. Obviously,
100 * a smaller range of magnitudes is covered (about 5 orders of magnitude
101 * instead of 38), and the lack of a sign bit means that negative luminances
102 * are not allowed. (Well, they aren't allowed in the real world, either,
103 * but they are useful for certain types of image processing.)
104 *
105 * The desired user format is controlled by the setting the internal
106 * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
107 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values
108 * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v
109 * Raw data i/o is also possible using:
110 * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel
111 * In addition, the following decoding is provided for ease of display:
112 * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values
113 *
114 * For grayscale images, we provide the following data formats:
115 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values
116 * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance
117 * SGILOGDATAFMT_8BIT = 8-bit gray monitor values
118 *
119 * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
120 * scheme by separating the logL, u and v bytes for each row and applying
121 * a PackBits type of compression. Since the 24-bit encoding is not
122 * adaptive, the 32-bit color format takes less space in many cases.
123 */
124
125 #include <stdio.h>
126 #include <assert.h>
127 #include <stdlib.h>
128 #include <math.h>
129
130 /*
131 * State block for each open TIFF
132 * file using LogLuv compression/decompression.
133 */
134 typedef struct logLuvState LogLuvState;
135
136 struct logLuvState {
137 int user_datafmt; /* user data format */
138 int pixel_size; /* bytes per pixel */
139
140 tidata_t* tbuf; /* translation buffer */
141 short tbuflen; /* buffer length */
142 void (*tfunc)(LogLuvState*, tidata_t, int);
143
144 TIFFVSetMethod vgetparent; /* super-class method */
145 TIFFVSetMethod vsetparent; /* super-class method */
146 };
147
148 #define DecoderState(tif) ((LogLuvState*) (tif)->tif_data)
149 #define EncoderState(tif) ((LogLuvState*) (tif)->tif_data)
150
151 #define N(a) (sizeof(a)/sizeof(a[0]))
152 #define SGILOGDATAFMT_UNKNOWN -1
153
154 #define MINRUN 4 /* minimum run length */
155
156 /*
157 * Decode a string of 16-bit gray pixels.
158 */
159 static int
160 LogL16Decode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
161 {
162 LogLuvState* sp = DecoderState(tif);
163 int shft, i, npixels;
164 u_char* bp;
165 int16* tp;
166 int16 b;
167 int cc, rc;
168
169 assert(s == 0);
170 assert(sp != NULL);
171
172 npixels = occ / sp->pixel_size;
173
174 if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
175 tp = (int16*) op;
176 else {
177 assert(sp->tbuflen >= npixels);
178 tp = (int16*) sp->tbuf;
179 }
180 _TIFFmemset((tdata_t) tp, 0, npixels*sizeof (tp[0]));
181
182 bp = (u_char*) tif->tif_rawcp;
183 cc = tif->tif_rawcc;
184 /* get each byte string */
185 for (shft = 2*8; (shft -= 8) >= 0; ) {
186 for (i = 0; i < npixels && cc > 0; )
187 if (*bp >= 128) { /* run */
188 rc = *bp++ + (2-128);
189 b = (int16)*bp++ << shft;
190 cc -= 2;
191 while (rc--)
192 tp[i++] |= b;
193 } else { /* non-run */
194 rc = *bp++; /* nul is noop */
195 while (--cc && rc--)
196 tp[i++] |= (int16)*bp++ << shft;
197 }
198 if (i != npixels) {
199 TIFFError(tif->tif_name,
200 "LogL16Decode: Not enough data at row %d (short %d pixels)",
201 tif->tif_row, npixels - i);
202 tif->tif_rawcp = (tidata_t) bp;
203 tif->tif_rawcc = cc;
204 return (0);
205 }
206 }
207 (*sp->tfunc)(sp, op, npixels);
208 tif->tif_rawcp = (tidata_t) bp;
209 tif->tif_rawcc = cc;
210 return (1);
211 }
212
213 /*
214 * Decode a string of 24-bit pixels.
215 */
216 static int
217 LogLuvDecode24(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
218 {
219 LogLuvState* sp = DecoderState(tif);
220 int cc, i, npixels;
221 u_char* bp;
222 uint32* tp;
223
224 assert(s == 0);
225 assert(sp != NULL);
226
227 npixels = occ / sp->pixel_size;
228
229 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
230 tp = (uint32 *)op;
231 else {
232 assert(sp->tbuflen >= npixels);
233 tp = (uint32 *) sp->tbuf;
234 }
235 _TIFFmemset((tdata_t) tp, 0, npixels*sizeof (tp[0]));
236 /* copy to array of uint32 */
237 bp = (u_char*) tif->tif_rawcp;
238 cc = tif->tif_rawcc;
239 for (i = 0; i < npixels && cc > 0; i++) {
240 tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
241 bp += 3;
242 cc -= 3;
243 }
244 tif->tif_rawcp = (tidata_t) bp;
245 tif->tif_rawcc = cc;
246 if (i != npixels) {
247 TIFFError(tif->tif_name,
248 "LogLuvDecode24: Not enough data at row %d (short %d pixels)",
249 tif->tif_row, npixels - i);
250 return (0);
251 }
252 (*sp->tfunc)(sp, op, npixels);
253 return (1);
254 }
255
256 /*
257 * Decode a string of 32-bit pixels.
258 */
259 static int
260 LogLuvDecode32(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
261 {
262 LogLuvState* sp;
263 int shft, i, npixels;
264 u_char* bp;
265 uint32* tp;
266 uint32 b;
267 int cc, rc;
268
269 assert(s == 0);
270 sp = DecoderState(tif);
271 assert(sp != NULL);
272
273 npixels = occ / sp->pixel_size;
274
275 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
276 tp = (uint32*) op;
277 else {
278 assert(sp->tbuflen >= npixels);
279 tp = (uint32*) sp->tbuf;
280 }
281 _TIFFmemset((tdata_t) tp, 0, npixels*sizeof (tp[0]));
282
283 bp = (u_char*) tif->tif_rawcp;
284 cc = tif->tif_rawcc;
285 /* get each byte string */
286 for (shft = 4*8; (shft -= 8) >= 0; ) {
287 for (i = 0; i < npixels && cc > 0; )
288 if (*bp >= 128) { /* run */
289 rc = *bp++ + (2-128);
290 b = (uint32)*bp++ << shft;
291 cc -= 2;
292 while (rc--)
293 tp[i++] |= b;
294 } else { /* non-run */
295 rc = *bp++; /* nul is noop */
296 while (--cc && rc--)
297 tp[i++] |= (uint32)*bp++ << shft;
298 }
299 if (i != npixels) {
300 TIFFError(tif->tif_name,
301 "LogLuvDecode32: Not enough data at row %d (short %d pixels)",
302 tif->tif_row, npixels - i);
303 tif->tif_rawcp = (tidata_t) bp;
304 tif->tif_rawcc = cc;
305 return (0);
306 }
307 }
308 (*sp->tfunc)(sp, op, npixels);
309 tif->tif_rawcp = (tidata_t) bp;
310 tif->tif_rawcc = cc;
311 return (1);
312 }
313
314 /*
315 * Decode a strip of pixels. We break it into rows to
316 * maintain synchrony with the encode algorithm, which
317 * is row by row.
318 */
319 static int
320 LogLuvDecodeStrip(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
321 {
322 tsize_t rowlen = TIFFScanlineSize(tif);
323
324 assert(cc%rowlen == 0);
325 while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
326 bp += rowlen, cc -= rowlen;
327 return (cc == 0);
328 }
329
330 /*
331 * Decode a tile of pixels. We break it into rows to
332 * maintain synchrony with the encode algorithm, which
333 * is row by row.
334 */
335 static int
336 LogLuvDecodeTile(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
337 {
338 tsize_t rowlen = TIFFTileRowSize(tif);
339
340 assert(cc%rowlen == 0);
341 while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
342 bp += rowlen, cc -= rowlen;
343 return (cc == 0);
344 }
345
346 /*
347 * Encode a row of 16-bit pixels.
348 */
349 static int
350 LogL16Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
351 {
352 LogLuvState* sp = EncoderState(tif);
353 int shft, i, j, npixels;
354 tidata_t op;
355 int16* tp;
356 int16 b;
357 int occ, rc=0, mask, beg;
358
359 assert(s == 0);
360 assert(sp != NULL);
361 npixels = cc / sp->pixel_size;
362
363 if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
364 tp = (int16*) bp;
365 else {
366 tp = (int16*) sp->tbuf;
367 assert(sp->tbuflen >= npixels);
368 (*sp->tfunc)(sp, bp, npixels);
369 }
370 /* compress each byte string */
371 op = tif->tif_rawcp;
372 occ = tif->tif_rawdatasize - tif->tif_rawcc;
373 for (shft = 2*8; (shft -= 8) >= 0; )
374 for (i = 0; i < npixels; i += rc) {
375 if (occ < 4) {
376 tif->tif_rawcp = op;
377 tif->tif_rawcc = tif->tif_rawdatasize - occ;
378 if (!TIFFFlushData1(tif))
379 return (-1);
380 op = tif->tif_rawcp;
381 occ = tif->tif_rawdatasize - tif->tif_rawcc;
382 }
383 mask = 0xff << shft; /* find next run */
384 for (beg = i; beg < npixels; beg += rc) {
385 b = tp[beg] & mask;
386 rc = 1;
387 while (rc < 127+2 && beg+rc < npixels &&
388 (tp[beg+rc] & mask) == b)
389 rc++;
390 if (rc >= MINRUN)
391 break; /* long enough */
392 }
393 if (beg-i > 1 && beg-i < MINRUN) {
394 b = tp[i] & mask; /* check short run */
395 j = i+1;
396 while ((tp[j++] & mask) == b)
397 if (j == beg) {
398 *op++ = 128-2+j-i;
399 *op++ = b >> shft;
400 occ -= 2;
401 i = beg;
402 break;
403 }
404 }
405 while (i < beg) { /* write out non-run */
406 if ((j = beg-i) > 127) j = 127;
407 if (occ < j+3) {
408 tif->tif_rawcp = op;
409 tif->tif_rawcc = tif->tif_rawdatasize - occ;
410 if (!TIFFFlushData1(tif))
411 return (-1);
412 op = tif->tif_rawcp;
413 occ = tif->tif_rawdatasize - tif->tif_rawcc;
414 }
415 *op++ = j; occ--;
416 while (j--) {
417 *op++ = tp[i++] >> shft & 0xff;
418 occ--;
419 }
420 }
421 if (rc >= MINRUN) { /* write out run */
422 *op++ = 128-2+rc;
423 *op++ = tp[beg] >> shft & 0xff;
424 occ -= 2;
425 } else
426 rc = 0;
427 }
428 tif->tif_rawcp = op;
429 tif->tif_rawcc = tif->tif_rawdatasize - occ;
430
431 return (0);
432 }
433
434 /*
435 * Encode a row of 24-bit pixels.
436 */
437 static int
438 LogLuvEncode24(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
439 {
440 LogLuvState* sp = EncoderState(tif);
441 int i, npixels, occ;
442 tidata_t op;
443 uint32* tp;
444
445 assert(s == 0);
446 assert(sp != NULL);
447 npixels = cc / sp->pixel_size;
448
449 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
450 tp = (uint32*) bp;
451 else {
452 tp = (uint32*) sp->tbuf;
453 assert(sp->tbuflen >= npixels);
454 (*sp->tfunc)(sp, bp, npixels);
455 }
456 /* write out encoded pixels */
457 op = tif->tif_rawcp;
458 occ = tif->tif_rawdatasize - tif->tif_rawcc;
459 for (i = npixels; i--; ) {
460 if (occ < 3) {
461 tif->tif_rawcp = op;
462 tif->tif_rawcc = tif->tif_rawdatasize - occ;
463 if (!TIFFFlushData1(tif))
464 return (-1);
465 op = tif->tif_rawcp;
466 occ = tif->tif_rawdatasize - tif->tif_rawcc;
467 }
468 *op++ = *tp >> 16;
469 *op++ = *tp >> 8 & 0xff;
470 *op++ = *tp++ & 0xff;
471 occ -= 3;
472 }
473 tif->tif_rawcp = op;
474 tif->tif_rawcc = tif->tif_rawdatasize - occ;
475
476 return (0);
477 }
478
479 /*
480 * Encode a row of 32-bit pixels.
481 */
482 static int
483 LogLuvEncode32(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
484 {
485 LogLuvState* sp = EncoderState(tif);
486 int shft, i, j, npixels;
487 tidata_t op;
488 uint32* tp;
489 uint32 b;
490 int occ, rc=0, mask, beg;
491
492 assert(s == 0);
493 assert(sp != NULL);
494
495 npixels = cc / sp->pixel_size;
496
497 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
498 tp = (uint32*) bp;
499 else {
500 tp = (uint32*) sp->tbuf;
501 assert(sp->tbuflen >= npixels);
502 (*sp->tfunc)(sp, bp, npixels);
503 }
504 /* compress each byte string */
505 op = tif->tif_rawcp;
506 occ = tif->tif_rawdatasize - tif->tif_rawcc;
507 for (shft = 4*8; (shft -= 8) >= 0; )
508 for (i = 0; i < npixels; i += rc) {
509 if (occ < 4) {
510 tif->tif_rawcp = op;
511 tif->tif_rawcc = tif->tif_rawdatasize - occ;
512 if (!TIFFFlushData1(tif))
513 return (-1);
514 op = tif->tif_rawcp;
515 occ = tif->tif_rawdatasize - tif->tif_rawcc;
516 }
517 mask = 0xff << shft; /* find next run */
518 for (beg = i; beg < npixels; beg += rc) {
519 b = tp[beg] & mask;
520 rc = 1;
521 while (rc < 127+2 && beg+rc < npixels &&
522 (tp[beg+rc] & mask) == b)
523 rc++;
524 if (rc >= MINRUN)
525 break; /* long enough */
526 }
527 if (beg-i > 1 && beg-i < MINRUN) {
528 b = tp[i] & mask; /* check short run */
529 j = i+1;
530 while ((tp[j++] & mask) == b)
531 if (j == beg) {
532 *op++ = 128-2+j-i;
533 *op++ = b >> shft;
534 occ -= 2;
535 i = beg;
536 break;
537 }
538 }
539 while (i < beg) { /* write out non-run */
540 if ((j = beg-i) > 127) j = 127;
541 if (occ < j+3) {
542 tif->tif_rawcp = op;
543 tif->tif_rawcc = tif->tif_rawdatasize - occ;
544 if (!TIFFFlushData1(tif))
545 return (-1);
546 op = tif->tif_rawcp;
547 occ = tif->tif_rawdatasize - tif->tif_rawcc;
548 }
549 *op++ = j; occ--;
550 while (j--) {
551 *op++ = tp[i++] >> shft & 0xff;
552 occ--;
553 }
554 }
555 if (rc >= MINRUN) { /* write out run */
556 *op++ = 128-2+rc;
557 *op++ = tp[beg] >> shft & 0xff;
558 occ -= 2;
559 } else
560 rc = 0;
561 }
562 tif->tif_rawcp = op;
563 tif->tif_rawcc = tif->tif_rawdatasize - occ;
564
565 return (0);
566 }
567
568 /*
569 * Encode a strip of pixels. We break it into rows to
570 * avoid encoding runs across row boundaries.
571 */
572 static int
573 LogLuvEncodeStrip(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
574 {
575 tsize_t rowlen = TIFFScanlineSize(tif);
576
577 assert(cc%rowlen == 0);
578 while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 0)
579 bp += rowlen, cc -= rowlen;
580 return (cc == 0);
581 }
582
583 /*
584 * Encode a tile of pixels. We break it into rows to
585 * avoid encoding runs across row boundaries.
586 */
587 static int
588 LogLuvEncodeTile(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
589 {
590 tsize_t rowlen = TIFFTileRowSize(tif);
591
592 assert(cc%rowlen == 0);
593 while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 0)
594 bp += rowlen, cc -= rowlen;
595 return (cc == 0);
596 }
597
598 /*
599 * Encode/Decode functions for converting to and from user formats.
600 */
601 #include "uvcode.h"
602
603 #define U_NEU 0.210526316
604 #define V_NEU 0.473684211
605
606 #ifdef M_LN2
607 #define LOGOF2 M_LN2
608 #else
609 #define LOGOF2 0.69314718055994530942
610 #endif
611 #define log2(x) ((1./LOGOF2)*log(x))
612 #define exp2(x) exp(LOGOF2*(x))
613
614 #define UVSCALE 410.
615
616 static double
617 pix16toY(int p16)
618 {
619 int Le = p16 & 0x7fff;
620 double Y;
621
622 if (!Le)
623 return (0.);
624 Y = exp(LOGOF2/256.*(Le+.5) - LOGOF2*64.);
625 if (p16 & 0x8000)
626 return (-Y);
627 return (Y);
628 }
629
630 static int
631 pix16fromY(double Y)
632 {
633 if (Y >= 1.84467e19)
634 return (0x7fff);
635 if (Y <= -1.84467e19)
636 return (0xffff);
637 if (Y > 5.43571e-20)
638 return (int)(256.*(log2(Y) + 64.));
639 if (Y < -5.43571e-20)
640 return (~0x7fff | (int)(256.*(log2(-Y) + 64.)));
641 return (0);
642 }
643
644 static void
645 L16toY(LogLuvState* sp, tidata_t op, int n)
646 {
647 int16* l16 = (int16*) sp->tbuf;
648 float* yp = (float*) op;
649
650 while (n-- > 0)
651 *yp++ = pix16toY(*l16++);
652 }
653
654 static void
655 L16toGry(LogLuvState* sp, tidata_t op, int n)
656 {
657 int16* l16 = (int16*) sp->tbuf;
658 uint8* gp = (uint8*) op;
659
660 while (n-- > 0) {
661 double Y = pix16toY(*l16++);
662 *gp++ = (Y <= 0.) ? 0 : (Y >= 1.) ? 255 : (int)(256.*sqrt(Y));
663 }
664 }
665
666 static void
667 L16fromY(LogLuvState* sp, tidata_t op, int n)
668 {
669 int16* l16 = (int16*) sp->tbuf;
670 float* yp = (float*) op;
671
672 while (n-- > 0)
673 *l16++ = pix16fromY(*yp++);
674 }
675
676 static void
677 XYZtoRGB24(float xyz[3], uint8 rgb[3])
678 {
679 double r, g, b;
680 /* assume CCIR-709 primaries */
681 r = 2.690*xyz[0] + -1.276*xyz[1] + -0.414*xyz[2];
682 g = -1.022*xyz[0] + 1.978*xyz[1] + 0.044*xyz[2];
683 b = 0.061*xyz[0] + -0.224*xyz[1] + 1.163*xyz[2];
684 /* assume 2.0 gamma for speed */
685 /* could use integer sqrt approx., but this is probably faster */
686 rgb[0] = (r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256.*sqrt(r));
687 rgb[1] = (g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256.*sqrt(g));
688 rgb[2] = (b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256.*sqrt(b));
689 }
690
691 static int
692 uv_encode(double u, double v) /* encode (u',v') coordinates */
693 {
694 register int vi, ui;
695
696 if (v < UV_VSTART)
697 return(-1);
698 vi = (v - UV_VSTART)*(1./UV_SQSIZ);
699 if (vi >= UV_NVS)
700 return(-1);
701 if (u < uv_row[vi].ustart)
702 return(-1);
703 ui = (u - uv_row[vi].ustart)*(1./UV_SQSIZ);
704 if (ui >= uv_row[vi].nus)
705 return(-1);
706 return(uv_row[vi].ncum + ui);
707 }
708
709 static int
710 uv_decode(double *up, double *vp, int c) /* decode (u',v') index */
711 {
712 int upper, lower;
713 register int ui, vi;
714
715 if (c < 0 || c >= UV_NDIVS)
716 return(-1);
717 lower = 0; /* binary search */
718 upper = UV_NVS;
719 do {
720 vi = (lower + upper) >> 1;
721 ui = c - uv_row[vi].ncum;
722 if (ui > 0)
723 lower = vi;
724 else if (ui < 0)
725 upper = vi;
726 else
727 break;
728 } while (upper - lower > 1);
729 vi = lower;
730 ui = c - uv_row[vi].ncum;
731 *up = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ;
732 *vp = UV_VSTART + (vi+.5)*UV_SQSIZ;
733 return(0);
734 }
735
736 static void
737 pix24toXYZ(uint32 p, float XYZ[3])
738 {
739 int Le, Ce;
740 double L, u, v, s, x, y;
741 /* decode luminance */
742 Le = p >> 14 & 0x3ff;
743 if (Le == 0) {
744 XYZ[0] = XYZ[1] = XYZ[2] = 0.;
745 return;
746 }
747 L = exp(LOGOF2/64.*(Le+.5) - LOGOF2*12.);
748 /* decode color */
749 Ce = p & 0x3fff;
750 if (uv_decode(&u, &v, Ce) < 0) {
751 u = U_NEU; v = V_NEU;
752 }
753 s = 1./(6.*u - 16.*v + 12.);
754 x = 9.*u * s;
755 y = 4.*v * s;
756 /* convert to XYZ */
757 XYZ[0] = x/y * L;
758 XYZ[1] = L;
759 XYZ[2] = (1.-x-y)/y * L;
760 }
761
762 static uint32
763 pix24fromXYZ(float XYZ[3])
764 {
765 int Le, Ce;
766 double L, u, v, s;
767 /* encode luminance */
768 L = XYZ[1];
769 if (L >= 16.)
770 Le = 0x3ff;
771 else if (L <= 1./4096.)
772 Le = 0;
773 else
774 Le = 64.*(log2(L) + 12.);
775 /* encode color */
776 s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
777 if (s == 0.) {
778 u = U_NEU;
779 v = V_NEU;
780 } else {
781 u = 4.*XYZ[0] / s;
782 v = 9.*XYZ[1] / s;
783 }
784 Ce = uv_encode(u, v);
785 if (Ce < 0)
786 Ce = uv_encode(U_NEU, V_NEU);
787 /* combine encodings */
788 return (Le << 14 | Ce);
789 }
790
791 static void
792 Luv24toXYZ(LogLuvState* sp, tidata_t op, int n)
793 {
794 uint32* luv = (uint32*) sp->tbuf;
795 float* xyz = (float*) op;
796
797 while (n-- > 0) {
798 pix24toXYZ(*luv, xyz);
799 xyz += 3;
800 luv++;
801 }
802 }
803
804 static void
805 Luv24toLuv48(LogLuvState* sp, tidata_t op, int n)
806 {
807 uint32* luv = (uint32*) sp->tbuf;
808 int16* luv3 = (int16*) op;
809
810 while (n-- > 0) {
811 double u, v;
812
813 *luv3++ = (*luv >> 12 & 0xffd) + 13314;
814 if (uv_decode(&u, &v, *luv&0x3fff) < 0) {
815 u = U_NEU;
816 v = V_NEU;
817 }
818 *luv3++ = u * (1L<<15);
819 *luv3++ = v * (1L<<15);
820 luv++;
821 }
822 }
823
824 static void
825 Luv24toRGB(LogLuvState* sp, tidata_t op, int n)
826 {
827 uint32* luv = (uint32*) sp->tbuf;
828 uint8* rgb = (uint8*) op;
829
830 while (n-- > 0) {
831 float xyz[3];
832
833 pix24toXYZ(*luv++, xyz);
834 XYZtoRGB24(xyz, rgb);
835 rgb += 3;
836 }
837 }
838
839 static void
840 Luv24fromXYZ(LogLuvState* sp, tidata_t op, int n)
841 {
842 uint32* luv = (uint32*) sp->tbuf;
843 float* xyz = (float*) op;
844
845 while (n-- > 0) {
846 *luv++ = pix24fromXYZ(xyz);
847 xyz += 3;
848 }
849 }
850
851 static void
852 Luv24fromLuv48(LogLuvState* sp, tidata_t op, int n)
853 {
854 uint32* luv = (uint32*) sp->tbuf;
855 int16* luv3 = (int16*) op;
856
857 while (n-- > 0) {
858 int Le, Ce;
859
860 if (luv3[0] <= 0)
861 Le = 0;
862 else if (luv3[0] >= (1<<12)+3314)
863 Le = (1<<10) - 1;
864 else
865 Le = (luv3[0]-3314) >> 2;
866 Ce = uv_encode((luv[1]+.5)/(1<<15), (luv[2]+.5)/(1<<15));
867 if (Ce < 0)
868 Ce = uv_encode(U_NEU, V_NEU);
869 *luv++ = (uint32)Le << 14 | Ce;
870 luv3 += 3;
871 }
872 }
873
874 static void
875 pix32toXYZ(uint32 p, float XYZ[3])
876 {
877 double L, u, v, s, x, y;
878 /* decode luminance */
879 L = pix16toY((int)p >> 16);
880 if (L == 0.) {
881 XYZ[0] = XYZ[1] = XYZ[2] = 0.;
882 return;
883 }
884 /* decode color */
885 u = 1./UVSCALE * ((p>>8 & 0xff) + .5);
886 v = 1./UVSCALE * ((p & 0xff) + .5);
887 s = 1./(6.*u - 16.*v + 12.);
888 x = 9.*u * s;
889 y = 4.*v * s;
890 /* convert to XYZ */
891 XYZ[0] = x/y * L;
892 XYZ[1] = L;
893 XYZ[2] = (1.-x-y)/y * L;
894 }
895
896 static uint32
897 pix32fromXYZ(float XYZ[3])
898 {
899 unsigned int Le, ue, ve;
900 double u, v, s;
901 /* encode luminance */
902 Le = (unsigned int)pix16fromY(XYZ[1]);
903 /* encode color */
904 s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
905 if (s == 0.) {
906 u = U_NEU;
907 v = V_NEU;
908 } else {
909 u = 4.*XYZ[0] / s;
910 v = 9.*XYZ[1] / s;
911 }
912 if (u <= 0.) ue = 0;
913 else ue = UVSCALE * u;
914 if (ue > 255) ue = 255;
915 if (v <= 0.) ve = 0;
916 else ve = UVSCALE * v;
917 if (ve > 255) ve = 255;
918 /* combine encodings */
919 return (Le << 16 | ue << 8 | ve);
920 }
921
922 static void
923 Luv32toXYZ(LogLuvState* sp, tidata_t op, int n)
924 {
925 uint32* luv = (uint32*) sp->tbuf;
926 float* xyz = (float*) op;
927
928 while (n-- > 0) {
929 pix32toXYZ(*luv++, xyz);
930 xyz += 3;
931 }
932 }
933
934 static void
935 Luv32toLuv48(LogLuvState* sp, tidata_t op, int n)
936 {
937 uint32* luv = (uint32*) sp->tbuf;
938 int16* luv3 = (int16*) op;
939
940 while (n-- > 0) {
941 double u, v;
942
943 *luv3++ = *luv >> 16;
944 u = 1./UVSCALE * ((*luv>>8 & 0xff) + .5);
945 v = 1./UVSCALE * ((*luv & 0xff) + .5);
946 *luv3++ = u * (1L<<15);
947 *luv3++ = v * (1L<<15);
948 luv++;
949 }
950 }
951
952 static void
953 Luv32toRGB(LogLuvState* sp, tidata_t op, int n)
954 {
955 uint32* luv = (uint32*) sp->tbuf;
956 uint8* rgb = (uint8*) op;
957
958 while (n-- > 0) {
959 float xyz[3];
960
961 pix32toXYZ(*luv++, xyz);
962 XYZtoRGB24(xyz, rgb);
963 rgb += 3;
964 }
965 }
966
967 static void
968 Luv32fromXYZ(LogLuvState* sp, tidata_t op, int n)
969 {
970 uint32* luv = (uint32*) sp->tbuf;
971 float* xyz = (float*) op;
972
973 while (n-- > 0) {
974 *luv++ = pix32fromXYZ(xyz);
975 xyz += 3;
976 }
977 }
978
979 static void
980 Luv32fromLuv48(LogLuvState* sp, tidata_t op, int n)
981 {
982 uint32* luv = (uint32*) sp->tbuf;
983 int16* luv3 = (int16*) op;
984
985 while (n-- > 0) {
986 *luv++ = (uint32)luv3[0] << 16 |
987 (luv3[1]*(uint32)(UVSCALE+.5) >> 7 & 0xff00) |
988 (luv3[2]*(uint32)(UVSCALE+.5) >> 15 & 0xff);
989 luv3 += 3;
990 }
991 }
992
993 static void
994 _logLuvNop(LogLuvState* sp, tidata_t op, int n)
995 {
996 (void) sp; (void) op; (void) n;
997 }
998
999 static int
1000 LogL16GuessDataFmt(TIFFDirectory *td)
1001 {
1002 #define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f))
1003 switch (PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat)) {
1004 case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
1005 return (SGILOGDATAFMT_FLOAT);
1006 case PACK(1, 16, SAMPLEFORMAT_VOID):
1007 case PACK(1, 16, SAMPLEFORMAT_INT):
1008 case PACK(1, 16, SAMPLEFORMAT_UINT):
1009 return (SGILOGDATAFMT_16BIT);
1010 case PACK(1, 8, SAMPLEFORMAT_VOID):
1011 case PACK(1, 8, SAMPLEFORMAT_UINT):
1012 return (SGILOGDATAFMT_8BIT);
1013 }
1014 #undef PACK
1015 return (SGILOGDATAFMT_UNKNOWN);
1016 }
1017
1018 static int
1019 LogL16InitState(TIFF* tif)
1020 {
1021 TIFFDirectory *td = &tif->tif_dir;
1022 LogLuvState* sp = DecoderState(tif);
1023 static const char module[] = "LogL16InitState";
1024
1025 assert(sp != NULL);
1026 assert(td->td_photometric == PHOTOMETRIC_LOGL);
1027
1028 /* for some reason, we can't do this in TIFFInitLogL16 */
1029 if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
1030 sp->user_datafmt = LogL16GuessDataFmt(td);
1031 switch (sp->user_datafmt) {
1032 case SGILOGDATAFMT_FLOAT:
1033 sp->pixel_size = sizeof (float);
1034 break;
1035 case SGILOGDATAFMT_16BIT:
1036 sp->pixel_size = sizeof (int16);
1037 break;
1038 case SGILOGDATAFMT_8BIT:
1039 sp->pixel_size = sizeof (uint8);
1040 break;
1041 default:
1042 TIFFError(tif->tif_name,
1043 "No support for converting user data format to LogL");
1044 return (0);
1045 }
1046 sp->tbuflen = td->td_imagewidth * td->td_rowsperstrip;
1047 sp->tbuf = (tidata_t*) _TIFFmalloc(sp->tbuflen * sizeof (int16));
1048 if (sp->tbuf == NULL) {
1049 TIFFError(module, "%s: No space for SGILog translation buffer",
1050 tif->tif_name);
1051 return (0);
1052 }
1053 return (1);
1054 }
1055
1056 static int
1057 LogLuvGuessDataFmt(TIFFDirectory *td)
1058 {
1059 int guess;
1060
1061 /*
1062 * If the user didn't tell us their datafmt,
1063 * take our best guess from the bitspersample.
1064 */
1065 #define PACK(a,b) (((a)<<3)|(b))
1066 switch (PACK(td->td_bitspersample, td->td_sampleformat)) {
1067 case PACK(32, SAMPLEFORMAT_IEEEFP):
1068 guess = SGILOGDATAFMT_FLOAT;
1069 break;
1070 case PACK(32, SAMPLEFORMAT_VOID):
1071 case PACK(32, SAMPLEFORMAT_UINT):
1072 case PACK(32, SAMPLEFORMAT_INT):
1073 guess = SGILOGDATAFMT_RAW;
1074 break;
1075 case PACK(16, SAMPLEFORMAT_VOID):
1076 case PACK(16, SAMPLEFORMAT_INT):
1077 case PACK(16, SAMPLEFORMAT_UINT):
1078 guess = SGILOGDATAFMT_16BIT;
1079 break;
1080 case PACK( 8, SAMPLEFORMAT_VOID):
1081 case PACK( 8, SAMPLEFORMAT_UINT):
1082 guess = SGILOGDATAFMT_8BIT;
1083 break;
1084 default:
1085 guess = SGILOGDATAFMT_UNKNOWN;
1086 break;
1087 #undef PACK
1088 }
1089 /*
1090 * Double-check samples per pixel.
1091 */
1092 switch (td->td_samplesperpixel) {
1093 case 1:
1094 if (guess != SGILOGDATAFMT_RAW)
1095 guess = SGILOGDATAFMT_UNKNOWN;
1096 break;
1097 case 3:
1098 if (guess == SGILOGDATAFMT_RAW)
1099 guess = SGILOGDATAFMT_UNKNOWN;
1100 break;
1101 default:
1102 guess = SGILOGDATAFMT_UNKNOWN;
1103 break;
1104 }
1105 return (guess);
1106 }
1107
1108 static int
1109 LogLuvInitState(TIFF* tif)
1110 {
1111 TIFFDirectory* td = &tif->tif_dir;
1112 LogLuvState* sp = DecoderState(tif);
1113 static const char module[] = "LogLuvInitState";
1114
1115 assert(sp != NULL);
1116 assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
1117
1118 /* for some reason, we can't do this in TIFFInitLogLuv */
1119 if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
1120 TIFFError(module,
1121 "SGILog compression cannot handle non-contiguous data");
1122 return (0);
1123 }
1124 if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
1125 sp->user_datafmt = LogLuvGuessDataFmt(td);
1126 switch (sp->user_datafmt) {
1127 case SGILOGDATAFMT_FLOAT:
1128 sp->pixel_size = 3*sizeof (float);
1129 break;
1130 case SGILOGDATAFMT_16BIT:
1131 sp->pixel_size = 3*sizeof (int16);
1132 break;
1133 case SGILOGDATAFMT_RAW:
1134 sp->pixel_size = sizeof (uint32);
1135 break;
1136 case SGILOGDATAFMT_8BIT:
1137 sp->pixel_size = 3*sizeof (uint8);
1138 break;
1139 default:
1140 TIFFError(tif->tif_name,
1141 "No support for converting user data format to LogLuv");
1142 return (0);
1143 }
1144 sp->tbuflen = td->td_imagewidth * td->td_rowsperstrip;
1145 sp->tbuf = (tidata_t*) _TIFFmalloc(sp->tbuflen * sizeof (uint32));
1146 if (sp->tbuf == NULL) {
1147 TIFFError(module, "%s: No space for SGILog translation buffer",
1148 tif->tif_name);
1149 return (0);
1150 }
1151 return (1);
1152 }
1153
1154 static int
1155 LogLuvSetupDecode(TIFF* tif)
1156 {
1157 LogLuvState* sp = DecoderState(tif);
1158 TIFFDirectory* td = &tif->tif_dir;
1159
1160 tif->tif_postdecode = _TIFFNoPostDecode;
1161 switch (td->td_photometric) {
1162 case PHOTOMETRIC_LOGLUV:
1163 if (!LogLuvInitState(tif))
1164 break;
1165 if (td->td_compression == COMPRESSION_SGILOG24) {
1166 tif->tif_decoderow = LogLuvDecode24;
1167 switch (sp->user_datafmt) {
1168 case SGILOGDATAFMT_FLOAT:
1169 sp->tfunc = Luv24toXYZ;
1170 break;
1171 case SGILOGDATAFMT_16BIT:
1172 sp->tfunc = Luv24toLuv48;
1173 break;
1174 case SGILOGDATAFMT_8BIT:
1175 sp->tfunc = Luv24toRGB;
1176 break;
1177 }
1178 } else {
1179 tif->tif_decoderow = LogLuvDecode32;
1180 switch (sp->user_datafmt) {
1181 case SGILOGDATAFMT_FLOAT:
1182 sp->tfunc = Luv32toXYZ;
1183 break;
1184 case SGILOGDATAFMT_16BIT:
1185 sp->tfunc = Luv32toLuv48;
1186 break;
1187 case SGILOGDATAFMT_8BIT:
1188 sp->tfunc = Luv32toRGB;
1189 break;
1190 }
1191 }
1192 return (1);
1193 case PHOTOMETRIC_LOGL:
1194 if (!LogL16InitState(tif))
1195 break;
1196 tif->tif_decoderow = LogL16Decode;
1197 switch (sp->user_datafmt) {
1198 case SGILOGDATAFMT_FLOAT:
1199 sp->tfunc = L16toY;
1200 break;
1201 case SGILOGDATAFMT_8BIT:
1202 sp->tfunc = L16toGry;
1203 break;
1204 }
1205 return (1);
1206 default:
1207 TIFFError(tif->tif_name,
1208 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1209 td->td_photometric, "must be either LogLUV or LogL");
1210 break;
1211 }
1212 return (0);
1213 }
1214
1215 static int
1216 LogLuvSetupEncode(TIFF* tif)
1217 {
1218 LogLuvState* sp = EncoderState(tif);
1219 TIFFDirectory* td = &tif->tif_dir;
1220
1221 switch (td->td_photometric) {
1222 case PHOTOMETRIC_LOGLUV:
1223 if (!LogLuvInitState(tif))
1224 break;
1225 if (td->td_compression == COMPRESSION_SGILOG24) {
1226 tif->tif_encoderow = LogLuvEncode24;
1227 switch (sp->user_datafmt) {
1228 case SGILOGDATAFMT_FLOAT:
1229 sp->tfunc = Luv24fromXYZ;
1230 break;
1231 case SGILOGDATAFMT_16BIT:
1232 sp->tfunc = Luv24fromLuv48;
1233 break;
1234 case SGILOGDATAFMT_RAW:
1235 break;
1236 default:
1237 goto notsupported;
1238 }
1239 } else {
1240 tif->tif_encoderow = LogLuvEncode32;
1241 switch (sp->user_datafmt) {
1242 case SGILOGDATAFMT_FLOAT:
1243 sp->tfunc = Luv32fromXYZ;
1244 break;
1245 case SGILOGDATAFMT_16BIT:
1246 sp->tfunc = Luv32fromLuv48;
1247 break;
1248 case SGILOGDATAFMT_RAW:
1249 break;
1250 default:
1251 goto notsupported;
1252 }
1253 }
1254 break;
1255 case PHOTOMETRIC_LOGL:
1256 if (!LogL16InitState(tif))
1257 break;
1258 tif->tif_encoderow = LogL16Encode;
1259 switch (sp->user_datafmt) {
1260 case SGILOGDATAFMT_FLOAT:
1261 sp->tfunc = L16fromY;
1262 break;
1263 case SGILOGDATAFMT_16BIT:
1264 break;
1265 default:
1266 goto notsupported;
1267 }
1268 break;
1269 default:
1270 TIFFError(tif->tif_name,
1271 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1272 td->td_photometric, "must be either LogLUV or LogL");
1273 break;
1274 }
1275 return (1);
1276 notsupported:
1277 TIFFError(tif->tif_name,
1278 "SGILog compression supported only for %s, or raw data",
1279 td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
1280 return (0);
1281 }
1282
1283 static void
1284 LogLuvClose(TIFF* tif)
1285 {
1286 TIFFDirectory *td = &tif->tif_dir;
1287
1288 /*
1289 * For consistency, we always want to write out the same
1290 * bitspersample and sampleformat for our TIFF file,
1291 * regardless of the data format being used by the application.
1292 * Since this routine is called after tags have been set but
1293 * before they have been recorded in the file, we reset them here.
1294 */
1295 td->td_samplesperpixel =
1296 (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
1297 td->td_bitspersample = 16;
1298 td->td_sampleformat = SAMPLEFORMAT_INT;
1299 }
1300
1301 static void
1302 LogLuvCleanup(TIFF* tif)
1303 {
1304 LogLuvState* sp = (LogLuvState *)tif->tif_data;
1305
1306 if (sp) {
1307 if (sp->tbuf)
1308 _TIFFfree(sp->tbuf);
1309 _TIFFfree(sp);
1310 tif->tif_data = NULL;
1311 }
1312 }
1313
1314 static int
1315 LogLuvVSetField(TIFF* tif, ttag_t tag, va_list ap)
1316 {
1317 LogLuvState* sp = DecoderState(tif);
1318 int bps, fmt;
1319
1320 switch (tag) {
1321 case TIFFTAG_SGILOGDATAFMT:
1322 sp->user_datafmt = va_arg(ap, int);
1323 /*
1324 * Tweak the TIFF header so that the rest of libtiff knows what
1325 * size of data will be passed between app and library, and
1326 * assume that the app knows what it is doing and is not
1327 * confused by these header manipulations...
1328 */
1329 switch (sp->user_datafmt) {
1330 case SGILOGDATAFMT_FLOAT:
1331 bps = 32, fmt = SAMPLEFORMAT_IEEEFP;
1332 break;
1333 case SGILOGDATAFMT_16BIT:
1334 bps = 16, fmt = SAMPLEFORMAT_INT;
1335 break;
1336 case SGILOGDATAFMT_RAW:
1337 bps = 32, fmt = SAMPLEFORMAT_UINT;
1338 break;
1339 case SGILOGDATAFMT_8BIT:
1340 bps = 8, fmt = SAMPLEFORMAT_UINT;
1341 break;
1342 default:
1343 TIFFError(tif->tif_name,
1344 "Unknown data format %d for LogLuv compression",
1345 sp->user_datafmt);
1346 return (0);
1347 }
1348 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1349 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt);
1350 /*
1351 * Must recalculate sizes should bits/sample change.
1352 */
1353 tif->tif_tilesize = TIFFTileSize(tif);
1354 tif->tif_scanlinesize = TIFFScanlineSize(tif);
1355 return (1);
1356 default:
1357 return (*sp->vsetparent)(tif, tag, ap);
1358 }
1359 }
1360
1361 static int
1362 LogLuvVGetField(TIFF* tif, ttag_t tag, va_list ap)
1363 {
1364 LogLuvState *sp = (LogLuvState *)tif->tif_data;
1365
1366 switch (tag) {
1367 case TIFFTAG_SGILOGDATAFMT:
1368 *va_arg(ap, int*) = sp->user_datafmt;
1369 return (1);
1370 default:
1371 return (*sp->vgetparent)(tif, tag, ap);
1372 }
1373 }
1374
1375 static const TIFFFieldInfo LogLuvFieldInfo[] = {
1376 { TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, FIELD_PSEUDO,
1377 TRUE, FALSE, "SGILogDataFmt"}
1378 };
1379
1380 int
1381 TIFFInitSGILog(TIFF* tif, int scheme)
1382 {
1383 static const char module[] = "TIFFInitSGILog";
1384 LogLuvState* sp;
1385
1386 assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
1387
1388 /*
1389 * Allocate state block so tag methods have storage to record values.
1390 */
1391 tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (LogLuvState));
1392 if (tif->tif_data == NULL)
1393 goto bad;
1394 sp = (LogLuvState*) tif->tif_data;
1395 memset(sp, 0, sizeof (*sp));
1396 sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
1397 sp->tfunc = _logLuvNop;
1398
1399 /*
1400 * Install codec methods.
1401 * NB: tif_decoderow & tif_encoderow are filled
1402 * in at setup time.
1403 */
1404 tif->tif_setupdecode = LogLuvSetupDecode;
1405 tif->tif_decodestrip = LogLuvDecodeStrip;
1406 tif->tif_decodetile = LogLuvDecodeTile;
1407 tif->tif_setupencode = LogLuvSetupEncode;
1408 tif->tif_encodestrip = LogLuvEncodeStrip;
1409 tif->tif_encodetile = LogLuvEncodeTile;
1410 tif->tif_close = LogLuvClose;
1411 tif->tif_cleanup = LogLuvCleanup;
1412
1413 /* override SetField so we can handle our private pseudo-tag */
1414 _TIFFMergeFieldInfo(tif, LogLuvFieldInfo, N(LogLuvFieldInfo));
1415 sp->vgetparent = tif->tif_vgetfield;
1416 tif->tif_vgetfield = LogLuvVGetField; /* hook for codec tags */
1417 sp->vsetparent = tif->tif_vsetfield;
1418 tif->tif_vsetfield = LogLuvVSetField; /* hook for codec tags */
1419
1420 return (1);
1421 bad:
1422 TIFFError(module, "%s: No space for LogLuv state block", tif->tif_name);
1423 return (0);
1424 }
1425 #endif /* LOGLUV_SUPPORT */