LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_luv.c
Go to the documentation of this file.
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
30#include "tiffiop.h"
31#ifdef LOGLUV_SUPPORT
32
33/*
34 * TIFF Library.
35 * LogLuv compression support for high dynamic range images.
36 *
37 * Contributed by Greg Larson.
38 *
39 * LogLuv image support uses the TIFF library to store 16 or 10-bit
40 * log luminance values with 8 bits each of u and v or a 14-bit index.
41 *
42 * The codec can take as input and produce as output 32-bit IEEE float values
43 * as well as 16-bit integer values. A 16-bit luminance is interpreted
44 * as a sign bit followed by a 15-bit integer that is converted
45 * to and from a linear magnitude using the transformation:
46 *
47 * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit
48 *
49 * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real
50 *
51 * The actual conversion to world luminance units in candelas per sq. meter
52 * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
53 * This value is usually set such that a reasonable exposure comes from
54 * clamping decoded luminances above 1 to 1 in the displayed image.
55 *
56 * The 16-bit values for u and v may be converted to real values by dividing
57 * each by 32768. (This allows for negative values, which aren't useful as
58 * far as we know, but are left in case of future improvements in human
59 * color vision.)
60 *
61 * Conversion from (u,v), which is actually the CIE (u',v') system for
62 * you color scientists, is accomplished by the following transformation:
63 *
64 * u = 4*x / (-2*x + 12*y + 3)
65 * v = 9*y / (-2*x + 12*y + 3)
66 *
67 * x = 9*u / (6*u - 16*v + 12)
68 * y = 4*v / (6*u - 16*v + 12)
69 *
70 * This process is greatly simplified by passing 32-bit IEEE floats
71 * for each of three CIE XYZ coordinates. The codec then takes care
72 * of conversion to and from LogLuv, though the application is still
73 * responsible for interpreting the TIFFTAG_STONITS calibration factor.
74 *
75 * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white
76 * point of (x,y)=(1/3,1/3). However, most color systems assume some other
77 * white point, such as D65, and an absolute color conversion to XYZ then
78 * to another color space with a different white point may introduce an
79 * unwanted color cast to the image. It is often desirable, therefore, to
80 * perform a white point conversion that maps the input white to [1 1 1]
81 * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT
82 * tag value. A decoder that demands absolute color calibration may use
83 * this white point tag to get back the original colors, but usually it
84 * will be ignored and the new white point will be used instead that
85 * matches the output color space.
86 *
87 * Pixel information is compressed into one of two basic encodings, depending
88 * on the setting of the compression tag, which is one of COMPRESSION_SGILOG
89 * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is
90 * stored as:
91 *
92 * 1 15
93 * |-+---------------|
94 *
95 * COMPRESSION_SGILOG color data is stored as:
96 *
97 * 1 15 8 8
98 * |-+---------------|--------+--------|
99 * S Le ue ve
100 *
101 * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
102 *
103 * 10 14
104 * |----------|--------------|
105 * Le' Ce
106 *
107 * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
108 * encoded as an index for optimal color resolution. The 10 log bits are
109 * defined by the following conversions:
110 *
111 * L = 2^((Le'+.5)/64 - 12) # real from 10-bit
112 *
113 * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real
114 *
115 * The 10 bits of the smaller format may be converted into the 15 bits of
116 * the larger format by multiplying by 4 and adding 13314. Obviously,
117 * a smaller range of magnitudes is covered (about 5 orders of magnitude
118 * instead of 38), and the lack of a sign bit means that negative luminances
119 * are not allowed. (Well, they aren't allowed in the real world, either,
120 * but they are useful for certain types of image processing.)
121 *
122 * The desired user format is controlled by the setting the internal
123 * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
124 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values
125 * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v
126 * Raw data i/o is also possible using:
127 * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel
128 * In addition, the following decoding is provided for ease of display:
129 * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values
130 *
131 * For grayscale images, we provide the following data formats:
132 * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values
133 * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance
134 * SGILOGDATAFMT_8BIT = 8-bit gray monitor values
135 *
136 * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
137 * scheme by separating the logL, u and v bytes for each row and applying
138 * a PackBits type of compression. Since the 24-bit encoding is not
139 * adaptive, the 32-bit color format takes less space in many cases.
140 *
141 * Further control is provided over the conversion from higher-resolution
142 * formats to final encoded values through the pseudo tag
143 * TIFFTAG_SGILOGENCODE:
144 * SGILOGENCODE_NODITHER = do not dither encoded values
145 * SGILOGENCODE_RANDITHER = apply random dithering during encoding
146 *
147 * The default value of this tag is SGILOGENCODE_NODITHER for
148 * COMPRESSION_SGILOG to maximize run-length encoding and
149 * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn
150 * quantization errors into noise.
151 */
152
153#include <limits.h>
154#include <math.h>
155#include <stdio.h>
156#include <stdlib.h>
157#include <time.h>
158
159/*
160 * State block for each open TIFF
161 * file using LogLuv compression/decompression.
162 */
168
224
225#define DecoderState(tif) ((LogLuvState *)(tif)->tif_data)
226#define EncoderState(tif) ((LogLuvState *)(tif)->tif_data)
227
228#define SGILOGDATAFMT_UNKNOWN -1
229
230#define MINRUN 4 /* minimum run length */
231
258static int LogLuvReserve(TIFF *tif, uint8_t **opp, tmsize_t *occ, tmsize_t need)
259{
260 if (*occ >= need)
261 return (1);
262 tif->tif_rawcp = *opp;
263 tif->tif_rawcc = tif->tif_rawdatasize - *occ;
264 if (!TIFFFlushData1(tif))
265 return (0);
266 *opp = tif->tif_rawcp;
267 *occ = tif->tif_rawdatasize - tif->tif_rawcc;
268 return (1);
269}
270
290static int LogLuvFits(TIFF *tif, const char *module, const uint8_t *op,
291 tmsize_t need)
292{
293 if (need <= (tmsize_t)(tif->tif_rawdata + tif->tif_rawdatasize - op))
294 return (1);
295 TIFFErrorExtR(tif, module,
296 "Raw data buffer of %" TIFF_SSIZE_FORMAT
297 " bytes is too small for SGILog encoding",
298 tif->tif_rawdatasize);
299 return (0);
300}
301
302static void L16toL16(LogLuvState *sp, uint8_t *op, tmsize_t n);
303static void L16fromL16(LogLuvState *sp, uint8_t *op, tmsize_t n);
304static void LuvRawToRaw(LogLuvState *sp, uint8_t *op, tmsize_t n);
305static void LuvRawFromRaw(LogLuvState *sp, uint8_t *op, tmsize_t n);
306
307/*
308 * Decode a string of 16-bit gray pixels.
309 */
335static int LogL16Decode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
336{
337 static const char module[] = "LogL16Decode";
338 LogLuvState *sp = DecoderState(tif);
339 int shft;
340 tmsize_t i;
341 tmsize_t npixels;
342 unsigned char *bp;
343 int16_t *tp;
344 int16_t b;
345 tmsize_t cc;
346 int rc;
347
348 (void)s;
349 assert(s == 0);
350 assert(sp != NULL);
351
352 npixels = occ / sp->pixel_size;
353
354 if (sp->tbuflen < npixels)
355 {
356 /* Clear the part of the buffer that is not written. */
357 _TIFFmemset(op, 0, occ);
358 TIFFErrorExtR(tif, module, "Translation buffer too short");
359 return (0);
360 }
361 tp = (int16_t *)sp->tbuf;
362 _TIFFmemset((void *)tp, 0, (tmsize_t)((size_t)npixels * sizeof(tp[0])));
363
364 bp = (unsigned char *)tif->tif_rawcp;
365 cc = tif->tif_rawcc;
366 /* get each byte string */
367 for (shft = 8; shft >= 0; shft -= 8)
368 {
369 for (i = 0; i < npixels && cc > 0;)
370 {
371 if (*bp >= 128)
372 { /* run */
373 if (cc < 2)
374 break;
375 rc = *bp++ + (2 - 128);
376 b = (int16_t)(*bp++ << shft);
377 cc -= 2;
378 while (rc-- && i < npixels)
379 tp[i++] |= b;
380 }
381 else
382 { /* non-run */
383 rc = *bp++; /* nul is noop */
384 while (--cc && rc-- && i < npixels)
385 tp[i++] |= (int16_t)(*bp++ << shft);
386 }
387 }
388 if (i != npixels)
389 {
390 /* Clear the part of the buffer that is not written. */
391 _TIFFmemset(op, 0, occ);
392 TIFFErrorExtR(tif, module,
393 "Not enough data at row %" PRIu32
394 " (short %" TIFF_SSIZE_FORMAT " pixels)",
395 tif->tif_dir.td_row, npixels - i);
396 tif->tif_rawcp = (uint8_t *)bp;
397 tif->tif_rawcc = cc;
398 return (0);
399 }
400 }
401 if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
402 L16toL16(sp, op, npixels);
403 else
404 (*sp->tfunc)(sp, op, npixels);
405 tif->tif_rawcp = (uint8_t *)bp;
406 tif->tif_rawcc = cc;
407 return (1);
408}
409
410/*
411 * Decode a string of 24-bit pixels.
412 */
437static int LogLuvDecode24(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
438{
439 static const char module[] = "LogLuvDecode24";
440 LogLuvState *sp = DecoderState(tif);
441 tmsize_t cc;
442 tmsize_t i;
443 tmsize_t npixels;
444 unsigned char *bp;
445 uint32_t *tp;
446
447 (void)s;
448 assert(s == 0);
449 assert(sp != NULL);
450
451 npixels = occ / sp->pixel_size;
452
453 if (sp->tbuflen < npixels)
454 {
455 /* Clear the part of the buffer that is not written. */
456 _TIFFmemset(op, 0, occ);
457 TIFFErrorExtR(tif, module, "Translation buffer too short");
458 return (0);
459 }
460 tp = (uint32_t *)sp->tbuf;
461 /* copy to array of uint32_t */
462 bp = (unsigned char *)tif->tif_rawcp;
463 cc = tif->tif_rawcc;
464 for (i = 0; i < npixels && cc >= 3; i++)
465 {
466 tp[i] = (uint32_t)bp[0] << 16 | (uint32_t)bp[1] << 8 | bp[2];
467 bp += 3;
468 cc -= 3;
469 }
470 tif->tif_rawcp = (uint8_t *)bp;
471 tif->tif_rawcc = cc;
472 if (i != npixels)
473 {
474 /* Clear the part of the buffer that is not written. */
475 _TIFFmemset(op, 0, occ);
476 TIFFErrorExtR(tif, module,
477 "Not enough data at row %" PRIu32
478 " (short %" TIFF_SSIZE_FORMAT " pixels)",
479 tif->tif_dir.td_row, npixels - i);
480 return (0);
481 }
482 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
483 LuvRawToRaw(sp, op, npixels);
484 else
485 (*sp->tfunc)(sp, op, npixels);
486 return (1);
487}
488
489/*
490 * Decode a string of 32-bit pixels.
491 */
516static int LogLuvDecode32(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
517{
518 static const char module[] = "LogLuvDecode32";
519 LogLuvState *sp;
520 int shft;
521 tmsize_t i;
522 tmsize_t npixels;
523 unsigned char *bp;
524 uint32_t *tp;
525 uint32_t b;
526 tmsize_t cc;
527 int rc;
528
529 (void)s;
530 assert(s == 0);
531 sp = DecoderState(tif);
532 assert(sp != NULL);
533
534 npixels = occ / sp->pixel_size;
535
536 if (sp->tbuflen < npixels)
537 {
538 /* Clear the part of the buffer that is not written. */
539 _TIFFmemset(op, 0, occ);
540 TIFFErrorExtR(tif, module, "Translation buffer too short");
541 return (0);
542 }
543 tp = (uint32_t *)sp->tbuf;
544 _TIFFmemset((void *)tp, 0, (tmsize_t)((size_t)npixels * sizeof(tp[0])));
545
546 bp = (unsigned char *)tif->tif_rawcp;
547 cc = tif->tif_rawcc;
548 /* get each byte string */
549 for (shft = 24; shft >= 0; shft -= 8)
550 {
551 for (i = 0; i < npixels && cc > 0;)
552 {
553 if (*bp >= 128)
554 { /* run */
555 if (cc < 2)
556 break;
557 rc = *bp++ + (2 - 128);
558 b = (uint32_t)*bp++ << shft;
559 cc -= 2;
560 while (rc-- && i < npixels)
561 tp[i++] |= b;
562 }
563 else
564 { /* non-run */
565 rc = *bp++; /* nul is noop */
566 while (--cc && rc-- && i < npixels)
567 tp[i++] |= (uint32_t)*bp++ << shft;
568 }
569 }
570 if (i != npixels)
571 {
572 /* Clear the part of the buffer that is not written. */
573 _TIFFmemset(op, 0, occ);
574 TIFFErrorExtR(tif, module,
575 "Not enough data at row %" PRIu32
576 " (short %" TIFF_SSIZE_FORMAT " pixels)",
577 tif->tif_dir.td_row, npixels - i);
578 tif->tif_rawcp = (uint8_t *)bp;
579 tif->tif_rawcc = cc;
580 return (0);
581 }
582 }
583 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
584 LuvRawToRaw(sp, op, npixels);
585 else
586 (*sp->tfunc)(sp, op, npixels);
587 tif->tif_rawcp = (uint8_t *)bp;
588 tif->tif_rawcc = cc;
589 return (1);
590}
591
592/*
593 * Decode a strip of pixels. We break it into rows to
594 * maintain synchrony with the encode algorithm, which
595 * is row by row.
596 */
619static int LogLuvDecodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
620{
621 tmsize_t rowlen = TIFFScanlineSize(tif);
622
623 if (rowlen == 0)
624 {
625 /* Clear the part of the buffer that is not written. */
626 _TIFFmemset(bp, 0, cc);
627 return 0;
628 }
629
630 if (cc % rowlen != 0)
631 {
632 /* Clear the part of the buffer that is not written. */
633 _TIFFmemset(bp, 0, cc);
634 TIFFErrorExtR(tif, "LogLuvDecodeStrip",
635 "Fractional scanline cannot be read");
636 return 0;
637 }
638 while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
639 {
640 bp += rowlen;
641 cc -= rowlen;
642 }
643 if (cc)
644 {
645 /* Clear the part of the buffer that is not written. */
646 _TIFFmemset(bp, 0, cc);
647 return (0);
648 }
649 return (1);
650}
651
652/*
653 * Decode a tile of pixels. We break it into rows to
654 * maintain synchrony with the encode algorithm, which
655 * is row by row.
656 */
679static int LogLuvDecodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
680{
681 tmsize_t rowlen = TIFFTileRowSize(tif);
682
683 if (rowlen == 0)
684 {
685 /* Clear the part of the buffer that is not written. */
686 _TIFFmemset(bp, 0, cc);
687 return 0;
688 }
689
690 if (cc % rowlen != 0)
691 {
692 /* Clear the part of the buffer that is not written. */
693 _TIFFmemset(bp, 0, cc);
694 TIFFErrorExtR(tif, "LogLuvDecodeTile",
695 "Fractional scanline cannot be read");
696 return 0;
697 }
698 while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
699 {
700 bp += rowlen;
701 cc -= rowlen;
702 }
703 if (cc)
704 {
705 /* Clear the part of the buffer that is not written. */
706 _TIFFmemset(bp, 0, cc);
707 return (0);
708 }
709 return (1);
710}
711
712/*
713 * Encode a row of 16-bit pixels.
714 */
739static int LogL16Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
740{
741 static const char module[] = "LogL16Encode";
742 LogLuvState *sp = EncoderState(tif);
743 int shft;
744 tmsize_t i;
745 tmsize_t j;
746 tmsize_t npixels;
747 uint8_t *op;
748 int16_t *tp;
749 int16_t b;
750 tmsize_t occ;
751 int rc = 0, mask;
752 tmsize_t beg;
753
754 (void)s;
755 assert(s == 0);
756 assert(sp != NULL);
757 npixels = cc / sp->pixel_size;
758
759 tp = (int16_t *)sp->tbuf;
760 if (sp->tbuflen < npixels)
761 {
762 TIFFErrorExtR(tif, module, "Translation buffer too short");
763 return (0);
764 }
765 if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
766 L16fromL16(sp, bp, npixels);
767 else
768 (*sp->tfunc)(sp, bp, npixels);
769 /* compress each byte string */
770 op = tif->tif_rawcp;
771 occ = tif->tif_rawdatasize - tif->tif_rawcc;
772 for (shft = 8; shft >= 0; shft -= 8)
773 {
774 for (i = 0; i < npixels; i += rc)
775 {
776 if (!LogLuvReserve(tif, &op, &occ, 4))
777 return (0);
778 mask = 0xff << shft; /* find next run */
779 for (beg = i; beg < npixels; beg += rc)
780 {
781 b = (int16_t)(tp[beg] & mask);
782 rc = 1;
783 while (rc < 127 + 2 && beg + rc < npixels &&
784 (tp[beg + rc] & mask) == b)
785 rc++;
786 if (rc >= MINRUN)
787 break; /* long enough */
788 }
789 if (beg - i > 1 && beg - i < MINRUN)
790 {
791 b = (int16_t)(tp[i] & mask); /*check short run */
792 j = i + 1;
793 while ((tp[j++] & mask) == b)
794 if (j == beg)
795 {
796 if (!LogLuvFits(tif, module, op, 2))
797 return (0);
798 *op++ = (uint8_t)(128 - 2 + j - i);
799 *op++ = (uint8_t)(b >> shft);
800 occ -= 2;
801 i = beg;
802 break;
803 }
804 }
805 while (i < beg)
806 { /* write out non-run */
807 if ((j = beg - i) > 127)
808 j = 127;
809 if (!LogLuvReserve(tif, &op, &occ, j + 3))
810 return (0);
811 if (!LogLuvFits(tif, module, op, j + 1))
812 return (0);
813 *op++ = (uint8_t)j;
814 occ--;
815 while (j--)
816 {
817 *op++ = (uint8_t)(tp[i++] >> shft & 0xff);
818 occ--;
819 }
820 }
821 if (rc >= MINRUN)
822 { /* write out run */
823 if (!LogLuvFits(tif, module, op, 2))
824 return (0);
825 *op++ = (uint8_t)(128 - 2 + rc);
826 *op++ = (uint8_t)(tp[beg] >> shft & 0xff);
827 occ -= 2;
828 }
829 else
830 rc = 0;
831 }
832 }
833 tif->tif_rawcp = op;
834 tif->tif_rawcc = tif->tif_rawdatasize - occ;
835
836 return (1);
837}
838
839/*
840 * Encode a row of 24-bit pixels.
841 */
866static int LogLuvEncode24(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
867{
868 static const char module[] = "LogLuvEncode24";
869 LogLuvState *sp = EncoderState(tif);
870 tmsize_t i;
871 tmsize_t npixels;
872 tmsize_t occ;
873 uint8_t *op;
874 uint32_t *tp;
875
876 (void)s;
877 assert(s == 0);
878 assert(sp != NULL);
879 npixels = cc / sp->pixel_size;
880
881 tp = (uint32_t *)sp->tbuf;
882 if (sp->tbuflen < npixels)
883 {
884 TIFFErrorExtR(tif, module, "Translation buffer too short");
885 return (0);
886 }
887 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
888 LuvRawFromRaw(sp, bp, npixels);
889 else
890 (*sp->tfunc)(sp, bp, npixels);
891 /* write out encoded pixels */
892 op = tif->tif_rawcp;
893 occ = tif->tif_rawdatasize - tif->tif_rawcc;
894 for (i = npixels; i--;)
895 {
896 if (!LogLuvReserve(tif, &op, &occ, 3))
897 return (0);
898 if (!LogLuvFits(tif, module, op, 3))
899 return (0);
900 *op++ = (uint8_t)(*tp >> 16);
901 *op++ = (uint8_t)(*tp >> 8 & 0xff);
902 *op++ = (uint8_t)(*tp++ & 0xff);
903 occ -= 3;
904 }
905 tif->tif_rawcp = op;
906 tif->tif_rawcc = tif->tif_rawdatasize - occ;
907
908 return (1);
909}
910
911/*
912 * Encode a row of 32-bit pixels.
913 */
938static int LogLuvEncode32(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
939{
940 static const char module[] = "LogLuvEncode32";
941 LogLuvState *sp = EncoderState(tif);
942 int shft;
943 tmsize_t i;
944 tmsize_t j;
945 tmsize_t npixels;
946 uint8_t *op;
947 uint32_t *tp;
948 uint32_t b;
949 tmsize_t occ;
950 int rc = 0;
951 tmsize_t beg;
952
953 (void)s;
954 assert(s == 0);
955 assert(sp != NULL);
956
957 npixels = cc / sp->pixel_size;
958
959 tp = (uint32_t *)sp->tbuf;
960 if (sp->tbuflen < npixels)
961 {
962 TIFFErrorExtR(tif, module, "Translation buffer too short");
963 return (0);
964 }
965 if (sp->user_datafmt == SGILOGDATAFMT_RAW)
966 LuvRawFromRaw(sp, bp, npixels);
967 else
968 (*sp->tfunc)(sp, bp, npixels);
969 /* compress each byte string */
970 op = tif->tif_rawcp;
971 occ = tif->tif_rawdatasize - tif->tif_rawcc;
972 for (shft = 24; shft >= 0; shft -= 8)
973 {
974 const uint32_t mask = 0xffU << shft; /* find next run */
975 for (i = 0; i < npixels; i += rc)
976 {
977 if (!LogLuvReserve(tif, &op, &occ, 4))
978 return (0);
979 for (beg = i; beg < npixels; beg += rc)
980 {
981 b = tp[beg] & mask;
982 rc = 1;
983 while (rc < 127 + 2 && beg + rc < npixels &&
984 (tp[beg + rc] & mask) == b)
985 rc++;
986 if (rc >= MINRUN)
987 break; /* long enough */
988 }
989 if (beg - i > 1 && beg - i < MINRUN)
990 {
991 b = tp[i] & mask; /* check short run */
992 j = i + 1;
993 while ((tp[j++] & mask) == b)
994 if (j == beg)
995 {
996 if (!LogLuvFits(tif, module, op, 2))
997 return (0);
998 *op++ = (uint8_t)(128 - 2 + j - i);
999 *op++ = (uint8_t)(b >> shft);
1000 occ -= 2;
1001 i = beg;
1002 break;
1003 }
1004 }
1005 while (i < beg)
1006 { /* write out non-run */
1007 if ((j = beg - i) > 127)
1008 j = 127;
1009 if (!LogLuvReserve(tif, &op, &occ, j + 3))
1010 return (0);
1011 if (!LogLuvFits(tif, module, op, j + 1))
1012 return (0);
1013 *op++ = (uint8_t)j;
1014 occ--;
1015 while (j--)
1016 {
1017 *op++ = (uint8_t)(tp[i++] >> shft & 0xff);
1018 occ--;
1019 }
1020 }
1021 if (rc >= MINRUN)
1022 { /* write out run */
1023 if (!LogLuvFits(tif, module, op, 2))
1024 return (0);
1025 *op++ = (uint8_t)(128 - 2 + rc);
1026 *op++ = (uint8_t)(tp[beg] >> shft & 0xff);
1027 occ -= 2;
1028 }
1029 else
1030 rc = 0;
1031 }
1032 }
1033 tif->tif_rawcp = op;
1034 tif->tif_rawcc = tif->tif_rawdatasize - occ;
1035
1036 return (1);
1037}
1038
1039/*
1040 * Encode a strip of pixels. We break it into rows to
1041 * avoid encoding runs across row boundaries.
1042 */
1063static int LogLuvEncodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1064{
1065 tmsize_t rowlen = TIFFScanlineSize(tif);
1066
1067 if (rowlen == 0)
1068 return 0;
1069
1070 if (cc % rowlen != 0)
1071 {
1072 TIFFErrorExtR(tif, "LogLuvEncodeStrip",
1073 "Fractional scanline cannot be written");
1074 return 0;
1075 }
1076 while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
1077 {
1078 bp += rowlen;
1079 cc -= rowlen;
1080 }
1081 return (cc == 0);
1082}
1083
1084/*
1085 * Encode a tile of pixels. We break it into rows to
1086 * avoid encoding runs across row boundaries.
1087 */
1108static int LogLuvEncodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1109{
1110 tmsize_t rowlen = TIFFTileRowSize(tif);
1111
1112 if (rowlen == 0)
1113 return 0;
1114
1115 if (cc % rowlen != 0)
1116 {
1117 TIFFErrorExtR(tif, "LogLuvEncodeTile",
1118 "Fractional scanline cannot be written");
1119 return 0;
1120 }
1121 while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
1122 {
1123 bp += rowlen;
1124 cc -= rowlen;
1125 }
1126 return (cc == 0);
1127}
1128
1129/*
1130 * Encode/Decode functions for converting to and from user formats.
1131 */
1132
1133#include "uvcode.h"
1134
1135#ifndef UVSCALE
1136#define U_NEU 0.210526316
1137#define V_NEU 0.473684211
1138#define UVSCALE 410.
1139#endif
1140
1141#ifndef M_LN2
1142#define M_LN2 0.69314718055994530942
1143#endif
1144#ifndef M_PI
1145#define M_PI 3.14159265358979323846
1146#endif
1147
1148#define TIFF_RAND_MAX 32767
1149
1150// From POSIX.1-2001 as an example of an implementation of rand()
1163static uint32_t _TIFFRand(void)
1164{
1165 static uint32_t nCounter = 0;
1166 if (!nCounter)
1167 nCounter = (uint32_t)(time(NULL) & UINT32_MAX);
1168 ++nCounter;
1169 uint32_t nCounterLocal =
1170 (uint32_t)(((uint64_t)(nCounter) * 1103515245U + 12345U) & UINT32_MAX);
1171 nCounter = nCounterLocal;
1172 return (nCounterLocal / 65536U) % (TIFF_RAND_MAX + 1);
1173}
1174
1189static int tiff_itrunc(double x, int m)
1190{
1191 if (m == SGILOGENCODE_NODITHER)
1192 return (int)x;
1193 return (int)(x + _TIFFRand() * (1. / TIFF_RAND_MAX) - .5);
1194}
1195
1196#if !LOGLUV_PUBLIC
1197static
1198#endif
1199 double LogL16toY(int p16) /* compute luminance from 16-bit LogL */
1200{
1201 int Le = p16 & 0x7fff;
1202 double Y;
1203
1204 if (!Le)
1205 return (0.);
1206 Y = exp(M_LN2 / 256. * (Le + .5) - M_LN2 * 64.);
1207 return (!(p16 & 0x8000) ? Y : -Y);
1208}
1209
1210#if !LOGLUV_PUBLIC
1211static
1212#endif
1213 int LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */
1214{
1215 if (Y >= 1.8371976e19)
1216 return (0x7fff);
1217 if (Y <= -1.8371976e19)
1218 return (0xffff);
1219 if (Y > 5.4136769e-20)
1220 return tiff_itrunc(256. * (log2(Y) + 64.), em);
1221 if (Y < -5.4136769e-20)
1222 return (~0x7fff | tiff_itrunc(256. * (log2(-Y) + 64.), em));
1223 return (0);
1224}
1225
1226/*
1227 * SGILOGDATAFMT_* buffers are application-facing user data buffers in native
1228 * byte order. The helpers below do not perform TIFF file byte-order
1229 * conversion; they only avoid unaligned typed access to public byte buffers.
1230 * Use fixed-size memcpy() calls directly so optimizing compilers can expand
1231 * them in these per-pixel paths. _TIFFmemcpy() is an out-of-line wrapper in
1232 * non-LTO builds.
1233 */
1245static float LogLuvLoadFloatNativeUnaligned(const uint8_t *cp)
1246{
1247 float v;
1248 memcpy(&v, cp, sizeof(v));
1249 return v;
1250}
1251
1263static void LogLuvStoreFloatNativeUnaligned(uint8_t *cp, float v)
1264{
1265 memcpy(cp, &v, sizeof(v));
1266}
1267
1280static int16_t LogLuvLoad16NativeUnaligned(const uint8_t *cp)
1281{
1282 int16_t v;
1283 memcpy(&v, cp, sizeof(v));
1284 return v;
1285}
1286
1299static void LogLuvStore16NativeUnaligned(uint8_t *cp, int16_t v)
1300{
1301 memcpy(cp, &v, sizeof(v));
1302}
1303
1316static uint32_t LogLuvLoad32NativeUnaligned(const uint8_t *cp)
1317{
1318 uint32_t v;
1319 memcpy(&v, cp, sizeof(v));
1320 return v;
1321}
1322
1335static void LogLuvStore32NativeUnaligned(uint8_t *cp, uint32_t v)
1336{
1337 memcpy(cp, &v, sizeof(v));
1338}
1339
1356static void L16toL16(LogLuvState *sp, uint8_t *op, tmsize_t n)
1357{
1358 int16_t *l16 = (int16_t *)sp->tbuf;
1359
1360 while (n-- > 0)
1361 {
1362 LogLuvStore16NativeUnaligned(op, *l16++);
1363 op += sizeof(int16_t);
1364 }
1365}
1366
1383static void L16fromL16(LogLuvState *sp, uint8_t *op, tmsize_t n)
1384{
1385 int16_t *l16 = (int16_t *)sp->tbuf;
1386
1387 while (n-- > 0)
1388 {
1389 *l16++ = LogLuvLoad16NativeUnaligned(op);
1390 op += sizeof(int16_t);
1391 }
1392}
1393
1410static void LuvRawToRaw(LogLuvState *sp, uint8_t *op, tmsize_t n)
1411{
1412 uint32_t *luv = (uint32_t *)sp->tbuf;
1413
1414 while (n-- > 0)
1415 {
1416 LogLuvStore32NativeUnaligned(op, *luv++);
1417 op += sizeof(uint32_t);
1418 }
1419}
1420
1437static void LuvRawFromRaw(LogLuvState *sp, uint8_t *op, tmsize_t n)
1438{
1439 uint32_t *luv = (uint32_t *)sp->tbuf;
1440
1441 while (n-- > 0)
1442 {
1443 *luv++ = LogLuvLoad32NativeUnaligned(op);
1444 op += sizeof(uint32_t);
1445 }
1446}
1447
1464static void L16toY(LogLuvState *sp, uint8_t *op, tmsize_t n)
1465{
1466 int16_t *l16 = (int16_t *)sp->tbuf;
1467
1468 while (n-- > 0)
1469 {
1470 LogLuvStoreFloatNativeUnaligned(op, (float)LogL16toY(*l16++));
1471 op += sizeof(float);
1472 }
1473}
1474
1490static void L16toGry(LogLuvState *sp, uint8_t *op, tmsize_t n)
1491{
1492 int16_t *l16 = (int16_t *)sp->tbuf;
1493 uint8_t *gp = (uint8_t *)op;
1494
1495 while (n-- > 0)
1496 {
1497 double Y = LogL16toY(*l16++);
1498 *gp++ = (uint8_t)((Y <= 0.) ? 0
1499 : (Y >= 1.) ? 255
1500 : (int)(256. * sqrt(Y)));
1501 }
1502}
1503
1522static void L16fromY(LogLuvState *sp, uint8_t *op, tmsize_t n)
1523{
1524 int16_t *l16 = (int16_t *)sp->tbuf;
1525
1526 while (n-- > 0)
1527 {
1528 *l16++ = (int16_t)(LogL16fromY(
1530 op += sizeof(float);
1531 }
1532}
1533
1534#if !LOGLUV_PUBLIC
1535static
1536#endif
1537 void XYZtoRGB24(float *xyz, uint8_t *rgb)
1538{
1539 double r, g, b;
1540 /* assume CCIR-709 primaries */
1541 r = 2.690 * (double)xyz[0] + -1.276 * (double)xyz[1] +
1542 -0.414 * (double)xyz[2];
1543 g = -1.022 * (double)xyz[0] + 1.978 * (double)xyz[1] +
1544 0.044 * (double)xyz[2];
1545 b = 0.061 * (double)xyz[0] + -0.224 * (double)xyz[1] +
1546 1.163 * (double)xyz[2];
1547 /* assume 2.0 gamma for speed */
1548 /* could use integer sqrt approx., but this is probably faster */
1549 rgb[0] = (uint8_t)((r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256. * sqrt(r)));
1550 rgb[1] = (uint8_t)((g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256. * sqrt(g)));
1551 rgb[2] = (uint8_t)((b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256. * sqrt(b)));
1552}
1553
1554#if !LOGLUV_PUBLIC
1555static
1556#endif
1557 double LogL10toY(int p10) /* compute luminance from 10-bit LogL */
1558{
1559 if (p10 == 0)
1560 return (0.);
1561 return (exp(M_LN2 / 64. * (p10 + .5) - M_LN2 * 12.));
1562}
1563
1564#if !LOGLUV_PUBLIC
1565static
1566#endif
1567 int LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */
1568{
1569 if (Y >= 15.742)
1570 return (0x3ff);
1571 else if (Y <= .00024283)
1572 return (0);
1573 else
1574 return tiff_itrunc(64. * (log2(Y) + 12.), em);
1575}
1576
1577#define NANGLES 100
1578#define uv2ang(u, v) \
1579 ((NANGLES * .499999999 / M_PI) * atan2((v) - V_NEU, (u) - U_NEU) + \
1580 .5 * NANGLES)
1581
1596static int oog_encode(double u, double v) /* encode out-of-gamut chroma */
1597{
1598 static int oog_table[NANGLES];
1599 static int initialized = 0;
1600 int i;
1601
1602 if (!initialized)
1603 { /* set up perimeter table */
1604 double eps[NANGLES], ua, va, ang, epsa;
1605 int ui, vi, ustep;
1606 for (i = NANGLES; i--;)
1607 eps[i] = 2.;
1608 for (vi = UV_NVS; vi--;)
1609 {
1610 va = (double)UV_VSTART + ((double)vi + .5) * (double)UV_SQSIZ;
1611 ustep = uv_row[vi].nus - 1;
1612 if (vi == UV_NVS - 1 || vi == 0 || ustep <= 0)
1613 ustep = 1;
1614 for (ui = uv_row[vi].nus - 1; ui >= 0; ui -= ustep)
1615 {
1616 ua = (double)uv_row[vi].ustart +
1617 ((double)ui + .5) * (double)UV_SQSIZ;
1618 ang = uv2ang(ua, va);
1619 i = (int)ang;
1620 epsa = fabs(ang - (i + .5));
1621 if (epsa < eps[i])
1622 {
1623 oog_table[i] = uv_row[vi].ncum + ui;
1624 eps[i] = epsa;
1625 }
1626 }
1627 }
1628 for (i = NANGLES; i--;) /* fill any holes */
1629 if (eps[i] > 1.5)
1630 {
1631 int i1, i2;
1632 for (i1 = 1; i1 < NANGLES / 2; i1++)
1633 if (eps[(i + i1) % NANGLES] < 1.5)
1634 break;
1635 for (i2 = 1; i2 < NANGLES / 2; i2++)
1636 if (eps[(i + NANGLES - i2) % NANGLES] < 1.5)
1637 break;
1638 if (i1 < i2)
1639 oog_table[i] = oog_table[(i + i1) % NANGLES];
1640 else
1641 oog_table[i] = oog_table[(i + NANGLES - i2) % NANGLES];
1642 }
1643 initialized = 1;
1644 }
1645 i = (int)uv2ang(u, v); /* look up hue angle */
1646 return (oog_table[i]);
1647}
1648
1649#undef uv2ang
1650#undef NANGLES
1651
1652#if !LOGLUV_PUBLIC
1653static
1654#endif
1655 int uv_encode(double u, double v, int em) /* encode (u',v') coordinates */
1656{
1657 unsigned int vi;
1658 int ui;
1659
1660 /* check for NaN */
1661 if (isnan(u) || isnan(v))
1662 {
1663 u = U_NEU;
1664 v = V_NEU;
1665 }
1666
1667 if ((double)v < (double)UV_VSTART)
1668 return oog_encode(u, v);
1669 vi = (unsigned int)tiff_itrunc(
1670 ((double)v - (double)UV_VSTART) * (1. / (double)UV_SQSIZ), em);
1671 if (vi >= UV_NVS)
1672 return oog_encode(u, v);
1673 if ((double)u < (double)uv_row[vi].ustart)
1674 return oog_encode(u, v);
1675 ui = tiff_itrunc(
1676 ((double)u - (double)uv_row[vi].ustart) * (1. / (double)UV_SQSIZ), em);
1677 if (ui >= uv_row[vi].nus)
1678 return oog_encode(u, v);
1679
1680 return (uv_row[vi].ncum + ui);
1681}
1682
1683#if !LOGLUV_PUBLIC
1684static
1685#endif
1686 int uv_decode(double *up, double *vp, int c) /* decode (u',v') index */
1687{
1688 unsigned int upper, lower;
1689 int ui;
1690 unsigned int vi;
1691
1692 if (c < 0 || c >= UV_NDIVS)
1693 return (-1);
1694 lower = 0; /* binary search */
1695 upper = UV_NVS;
1696 while (upper - lower > 1)
1697 {
1698 vi = (lower + upper) >> 1;
1699 ui = c - uv_row[vi].ncum;
1700 if (ui > 0)
1701 lower = vi;
1702 else if (ui < 0)
1703 upper = vi;
1704 else
1705 {
1706 lower = vi;
1707 break;
1708 }
1709 }
1710 vi = lower;
1711 ui = c - uv_row[vi].ncum;
1712 *up = (double)uv_row[vi].ustart + ((double)ui + .5) * (double)UV_SQSIZ;
1713 *vp = (double)UV_VSTART + ((double)vi + .5) * (double)UV_SQSIZ;
1714 return (0);
1715}
1716
1717#if !LOGLUV_PUBLIC
1718static
1719#endif
1720 void LogLuv24toXYZ(uint32_t p, float *XYZ)
1721{
1722 int Ce;
1723 double L, u, v, s, x, y;
1724 /* decode luminance */
1725 L = LogL10toY(p >> 14 & 0x3ff);
1726 if (L <= 0.)
1727 {
1728 XYZ[0] = XYZ[1] = XYZ[2] = 0.;
1729 return;
1730 }
1731 /* decode color */
1732 Ce = p & 0x3fff;
1733 if (uv_decode(&u, &v, Ce) < 0)
1734 {
1735 u = U_NEU;
1736 v = V_NEU;
1737 }
1738 s = 1. / (6. * u - 16. * v + 12.);
1739 x = 9. * u * s;
1740 y = 4. * v * s;
1741 /* convert to XYZ */
1742 XYZ[0] = (float)(x / y * L);
1743 XYZ[1] = (float)L;
1744 XYZ[2] = (float)((1. - x - y) / y * L);
1745}
1746
1747#if !LOGLUV_PUBLIC
1748static
1749#endif
1750 uint32_t LogLuv24fromXYZ(float *XYZ, int em)
1751{
1752 int Le, Ce;
1753 double u, v, s;
1754 /* encode luminance */
1755 Le = LogL10fromY((double)XYZ[1], em);
1756 /* encode color */
1757 s = (double)XYZ[0] + 15. * (double)XYZ[1] + 3. * (double)XYZ[2];
1758 if (!Le || s <= 0.)
1759 {
1760 u = U_NEU;
1761 v = V_NEU;
1762 }
1763 else
1764 {
1765 u = 4. * (double)XYZ[0] / s;
1766 v = 9. * (double)XYZ[1] / s;
1767 }
1768 Ce = uv_encode(u, v, em);
1769 if (Ce < 0) /* never happens */
1771 /* combine encodings */
1772 return (uint32_t)Le << 14 | (uint32_t)Ce;
1773}
1774
1791static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1792{
1793 uint32_t *luv = (uint32_t *)sp->tbuf;
1794
1795 while (n-- > 0)
1796 {
1797 float xyz[3];
1798 LogLuv24toXYZ(*luv, xyz);
1800 LogLuvStoreFloatNativeUnaligned(op + sizeof(float), xyz[1]);
1801 LogLuvStoreFloatNativeUnaligned(op + 2 * sizeof(float), xyz[2]);
1802 op += 3 * sizeof(float);
1803 luv++;
1804 }
1805}
1806
1824static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1825{
1826 uint32_t *luv = (uint32_t *)sp->tbuf;
1827
1828 while (n-- > 0)
1829 {
1830 double u, v;
1831 int16_t luv0;
1832 int16_t luv1;
1833 int16_t luv2;
1834
1835 luv0 = (int16_t)((*luv >> 12 & 0xffd) + 13314);
1836 if (uv_decode(&u, &v, *luv & 0x3fff) < 0)
1837 {
1838 u = U_NEU;
1839 v = V_NEU;
1840 }
1841 luv1 = (int16_t)(u * (1 << 15));
1842 luv2 = (int16_t)(v * (1 << 15));
1844 LogLuvStore16NativeUnaligned(op + sizeof(int16_t), luv1);
1845 LogLuvStore16NativeUnaligned(op + 2 * sizeof(int16_t), luv2);
1846 op += 3 * sizeof(int16_t);
1847 luv++;
1848 }
1849}
1850
1867static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
1868{
1869 uint32_t *luv = (uint32_t *)sp->tbuf;
1870 uint8_t *rgb = (uint8_t *)op;
1871
1872 while (n-- > 0)
1873 {
1874 float xyz[3];
1875
1876 LogLuv24toXYZ(*luv++, xyz);
1877 XYZtoRGB24(xyz, rgb);
1878 rgb += 3;
1879 }
1880}
1881
1902static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
1903{
1904 uint32_t *luv = (uint32_t *)sp->tbuf;
1905
1906 while (n-- > 0)
1907 {
1908 float xyz[3];
1909 xyz[0] = LogLuvLoadFloatNativeUnaligned(op);
1910 xyz[1] = LogLuvLoadFloatNativeUnaligned(op + sizeof(float));
1911 xyz[2] = LogLuvLoadFloatNativeUnaligned(op + 2 * sizeof(float));
1912 *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth);
1913 op += 3 * sizeof(float);
1914 }
1915}
1916
1938static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
1939{
1940 uint32_t *luv = (uint32_t *)sp->tbuf;
1941
1942 while (n-- > 0)
1943 {
1944 int Le, Ce;
1945 int16_t luv0 = LogLuvLoad16NativeUnaligned(op);
1946 int16_t luv1 = LogLuvLoad16NativeUnaligned(op + sizeof(int16_t));
1947 int16_t luv2 = LogLuvLoad16NativeUnaligned(op + 2 * sizeof(int16_t));
1948
1949 if (luv0 <= 0)
1950 Le = 0;
1951 else if (luv0 >= (1 << 12) + 3314)
1952 Le = (1 << 10) - 1;
1953 else if (sp->encode_meth == SGILOGENCODE_NODITHER)
1954 Le = (luv0 - 3314) >> 2;
1955 else
1956 Le = tiff_itrunc(.25 * (luv0 - 3314.), sp->encode_meth);
1957
1958 Ce = uv_encode((luv1 + .5) / (1 << 15), (luv2 + .5) / (1 << 15),
1959 sp->encode_meth);
1960 if (Ce < 0) /* never happens */
1962 *luv++ = (uint32_t)Le << 14 | (uint32_t)Ce;
1963 op += 3 * sizeof(int16_t);
1964 }
1965}
1966
1967#if !LOGLUV_PUBLIC
1968static
1969#endif
1970 void LogLuv32toXYZ(uint32_t p, float *XYZ)
1971{
1972 double L, u, v, s, x, y;
1973 /* decode luminance */
1974 L = LogL16toY((int)p >> 16);
1975 if (L <= 0.)
1976 {
1977 XYZ[0] = XYZ[1] = XYZ[2] = 0.;
1978 return;
1979 }
1980 /* decode color */
1981 u = 1. / UVSCALE * ((p >> 8 & 0xff) + .5);
1982 v = 1. / UVSCALE * ((p & 0xff) + .5);
1983 s = 1. / (6. * u - 16. * v + 12.);
1984 x = 9. * u * s;
1985 y = 4. * v * s;
1986 /* convert to XYZ */
1987 XYZ[0] = (float)(x / y * L);
1988 XYZ[1] = (float)L;
1989 XYZ[2] = (float)((1. - x - y) / y * L);
1990}
1991
1992#if !LOGLUV_PUBLIC
1993static
1994#endif
1995 uint32_t LogLuv32fromXYZ(float *XYZ, int em)
1996{
1997 unsigned int Le, ue, ve;
1998 double u, v, s;
1999 /* encode luminance */
2000 Le = (unsigned int)LogL16fromY((double)XYZ[1], em);
2001 /* encode color */
2002 s = (double)XYZ[0] + 15. * (double)XYZ[1] + 3. * (double)XYZ[2];
2003 if (!Le || s <= 0.)
2004 {
2005 u = U_NEU;
2006 v = V_NEU;
2007 }
2008 else
2009 {
2010 u = 4. * (double)XYZ[0] / s;
2011 v = 9. * (double)XYZ[1] / s;
2012 }
2013 if (u <= 0.)
2014 ue = 0;
2015 else
2016 ue = (unsigned int)tiff_itrunc(UVSCALE * u, em);
2017 if (ue > 255)
2018 ue = 255;
2019 if (v <= 0.)
2020 ve = 0;
2021 else
2022 ve = (unsigned int)tiff_itrunc(UVSCALE * v, em);
2023 if (ve > 255)
2024 ve = 255;
2025 /* combine encodings */
2026 return (Le << 16 | ue << 8 | ve);
2027}
2028
2045static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
2046{
2047 uint32_t *luv = (uint32_t *)sp->tbuf;
2048
2049 while (n-- > 0)
2050 {
2051 float xyz[3];
2052 LogLuv32toXYZ(*luv++, xyz);
2054 LogLuvStoreFloatNativeUnaligned(op + sizeof(float), xyz[1]);
2055 LogLuvStoreFloatNativeUnaligned(op + 2 * sizeof(float), xyz[2]);
2056 op += 3 * sizeof(float);
2057 }
2058}
2059
2077static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
2078{
2079 uint32_t *luv = (uint32_t *)sp->tbuf;
2080
2081 while (n-- > 0)
2082 {
2083 double u, v;
2084 int16_t luv0 = (int16_t)(*luv >> 16);
2085 int16_t luv1;
2086 int16_t luv2;
2087
2088 u = 1. / UVSCALE * ((*luv >> 8 & 0xff) + .5);
2089 v = 1. / UVSCALE * ((*luv & 0xff) + .5);
2090 luv1 = (int16_t)(u * (1 << 15));
2091 luv2 = (int16_t)(v * (1 << 15));
2093 LogLuvStore16NativeUnaligned(op + sizeof(int16_t), luv1);
2094 LogLuvStore16NativeUnaligned(op + 2 * sizeof(int16_t), luv2);
2095 op += 3 * sizeof(int16_t);
2096 luv++;
2097 }
2098}
2099
2116static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
2117{
2118 uint32_t *luv = (uint32_t *)sp->tbuf;
2119 uint8_t *rgb = (uint8_t *)op;
2120
2121 while (n-- > 0)
2122 {
2123 float xyz[3];
2124
2125 LogLuv32toXYZ(*luv++, xyz);
2126 XYZtoRGB24(xyz, rgb);
2127 rgb += 3;
2128 }
2129}
2130
2150static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
2151{
2152 uint32_t *luv = (uint32_t *)sp->tbuf;
2153
2154 while (n-- > 0)
2155 {
2156 float xyz[3];
2157 xyz[0] = LogLuvLoadFloatNativeUnaligned(op);
2158 xyz[1] = LogLuvLoadFloatNativeUnaligned(op + sizeof(float));
2159 xyz[2] = LogLuvLoadFloatNativeUnaligned(op + 2 * sizeof(float));
2160 *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth);
2161 op += 3 * sizeof(float);
2162 }
2163}
2164
2185static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
2186{
2187 uint32_t *luv = (uint32_t *)sp->tbuf;
2188
2190 {
2191 while (n-- > 0)
2192 {
2193 int16_t luv0 = LogLuvLoad16NativeUnaligned(op);
2194 int16_t luv1 = LogLuvLoad16NativeUnaligned(op + sizeof(int16_t));
2195 int16_t luv2 =
2196 LogLuvLoad16NativeUnaligned(op + 2 * sizeof(int16_t));
2197 *luv++ = (uint32_t)luv0 << 16 |
2198 ((uint32_t)luv1 * (uint32_t)(UVSCALE + .5) >> 7 & 0xff00) |
2199 ((uint32_t)luv2 * (uint32_t)(UVSCALE + .5) >> 15 & 0xff);
2200 op += 3 * sizeof(int16_t);
2201 }
2202 return;
2203 }
2204 while (n-- > 0)
2205 {
2206 int16_t luv0 = LogLuvLoad16NativeUnaligned(op);
2207 int16_t luv1 = LogLuvLoad16NativeUnaligned(op + sizeof(int16_t));
2208 int16_t luv2 = LogLuvLoad16NativeUnaligned(op + 2 * sizeof(int16_t));
2209 *luv++ = (uint32_t)luv0 << 16 |
2210 ((uint32_t)tiff_itrunc(luv1 * (UVSCALE / (1 << 15)),
2211 sp->encode_meth)
2212 << 8 &
2213 0xff00) |
2214 ((uint32_t)tiff_itrunc(luv2 * (UVSCALE / (1 << 15)),
2215 sp->encode_meth) &
2216 0xff);
2217 op += 3 * sizeof(int16_t);
2218 }
2219}
2220
2235static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n)
2236{
2237 (void)sp;
2238 (void)op;
2239 (void)n;
2240}
2241
2254{
2255#define PACK(s, b, f) (((b) << 6) | ((s) << 3) | (f))
2256 switch (
2258 {
2259 case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
2260 return (SGILOGDATAFMT_FLOAT);
2261 case PACK(1, 16, SAMPLEFORMAT_VOID):
2262 case PACK(1, 16, SAMPLEFORMAT_INT):
2263 case PACK(1, 16, SAMPLEFORMAT_UINT):
2264 return (SGILOGDATAFMT_16BIT);
2265 case PACK(1, 8, SAMPLEFORMAT_VOID):
2266 case PACK(1, 8, SAMPLEFORMAT_UINT):
2267 return (SGILOGDATAFMT_8BIT);
2268 default:
2269 break;
2270 }
2271#undef PACK
2272 return (SGILOGDATAFMT_UNKNOWN);
2273}
2274
2289static tmsize_t multiply_ms(TIFF *tif, const char *module, tmsize_t m1,
2290 tmsize_t m2)
2291{
2292 return _TIFFMultiplySSize(tif, m1, m2, module);
2293}
2294
2311static int LogL16InitState(TIFF *tif)
2312{
2313 static const char module[] = "LogL16InitState";
2314 TIFFDirectory *td = &tif->tif_dir;
2315 LogLuvState *sp = DecoderState(tif);
2316
2317 assert(sp != NULL);
2318 assert(td->td_photometric == PHOTOMETRIC_LOGL);
2319
2320 if (td->td_samplesperpixel != 1)
2321 {
2322 TIFFErrorExtR(tif, module,
2323 "Sorry, can not handle LogL image with %s=%" PRIu16,
2324 "Samples/pixel", td->td_samplesperpixel);
2325 return 0;
2326 }
2327
2328 /* for some reason, we can't do this in TIFFInitLogL16 */
2331 switch (sp->user_datafmt)
2332 {
2334 sp->pixel_size = sizeof(float);
2335 break;
2337 sp->pixel_size = sizeof(int16_t);
2338 break;
2339 case SGILOGDATAFMT_8BIT:
2340 sp->pixel_size = sizeof(uint8_t);
2341 break;
2342 default:
2343 TIFFErrorExtR(tif, module,
2344 "No support for converting user data format to LogL");
2345 return (0);
2346 }
2347 if (isTiled(tif))
2348 sp->tbuflen =
2349 multiply_ms(tif, module, td->td_tilewidth, td->td_tilelength);
2350 else if (td->td_rowsperstrip < td->td_imagelength)
2351 sp->tbuflen =
2352 multiply_ms(tif, module, td->td_imagewidth, td->td_rowsperstrip);
2353 else
2354 sp->tbuflen =
2355 multiply_ms(tif, module, td->td_imagewidth, td->td_imagelength);
2356 if (sp->tbuflen == 0)
2357 return (0); /* already reported by multiply_ms() */
2358 _TIFFfreeExt(tif, sp->tbuf);
2359 sp->tbuf = NULL;
2360 if (multiply_ms(tif, module, sp->tbuflen, (tmsize_t)sizeof(int16_t)) == 0 ||
2361 (sp->tbuf = (uint8_t *)_TIFFmallocExt(
2362 tif, (tmsize_t)((size_t)sp->tbuflen * sizeof(int16_t)))) == NULL)
2363 {
2364 TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
2365 return (0);
2366 }
2367 return (1);
2368}
2369
2383{
2384 int guess;
2385
2386 /*
2387 * If the user didn't tell us their datafmt,
2388 * take our best guess from the bitspersample.
2389 */
2390#define PACK(a, b) (((a) << 3) | (b))
2391 switch (PACK(td->td_bitspersample, td->td_sampleformat))
2392 {
2393 case PACK(32, SAMPLEFORMAT_IEEEFP):
2394 guess = SGILOGDATAFMT_FLOAT;
2395 break;
2396 case PACK(32, SAMPLEFORMAT_VOID):
2397 case PACK(32, SAMPLEFORMAT_UINT):
2398 case PACK(32, SAMPLEFORMAT_INT):
2399 guess = SGILOGDATAFMT_RAW;
2400 break;
2401 case PACK(16, SAMPLEFORMAT_VOID):
2402 case PACK(16, SAMPLEFORMAT_INT):
2403 case PACK(16, SAMPLEFORMAT_UINT):
2404 guess = SGILOGDATAFMT_16BIT;
2405 break;
2406 case PACK(8, SAMPLEFORMAT_VOID):
2407 case PACK(8, SAMPLEFORMAT_UINT):
2408 guess = SGILOGDATAFMT_8BIT;
2409 break;
2410 default:
2411 guess = SGILOGDATAFMT_UNKNOWN;
2412 break;
2413#undef PACK
2414 }
2415 /*
2416 * Double-check samples per pixel.
2417 */
2418 switch (td->td_samplesperpixel)
2419 {
2420 case 1:
2421 if (guess != SGILOGDATAFMT_RAW)
2422 guess = SGILOGDATAFMT_UNKNOWN;
2423 break;
2424 case 3:
2425 if (guess == SGILOGDATAFMT_RAW)
2426 guess = SGILOGDATAFMT_UNKNOWN;
2427 break;
2428 default:
2429 guess = SGILOGDATAFMT_UNKNOWN;
2430 break;
2431 }
2432 return (guess);
2433}
2434
2452static int LogLuvInitState(TIFF *tif)
2453{
2454 static const char module[] = "LogLuvInitState";
2455 TIFFDirectory *td = &tif->tif_dir;
2456 LogLuvState *sp = DecoderState(tif);
2457
2458 assert(sp != NULL);
2459 assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
2460
2461 /* for some reason, we can't do this in TIFFInitLogLuv */
2462 if (td->td_planarconfig != PLANARCONFIG_CONTIG)
2463 {
2464 TIFFErrorExtR(tif, module,
2465 "SGILog compression cannot handle non-contiguous data");
2466 return (0);
2467 }
2470 switch (sp->user_datafmt)
2471 {
2473 sp->pixel_size = 3 * sizeof(float);
2474 break;
2476 sp->pixel_size = 3 * sizeof(int16_t);
2477 break;
2478 case SGILOGDATAFMT_RAW:
2479 sp->pixel_size = sizeof(uint32_t);
2480 break;
2481 case SGILOGDATAFMT_8BIT:
2482 sp->pixel_size = 3 * sizeof(uint8_t);
2483 break;
2484 default:
2486 tif, module,
2487 "No support for converting user data format to LogLuv");
2488 return (0);
2489 }
2490 if (isTiled(tif))
2491 sp->tbuflen =
2492 multiply_ms(tif, module, td->td_tilewidth, td->td_tilelength);
2493 else if (td->td_rowsperstrip < td->td_imagelength)
2494 sp->tbuflen =
2495 multiply_ms(tif, module, td->td_imagewidth, td->td_rowsperstrip);
2496 else
2497 sp->tbuflen =
2498 multiply_ms(tif, module, td->td_imagewidth, td->td_imagelength);
2499 if (sp->tbuflen == 0)
2500 return (0); /* already reported by multiply_ms() */
2501 _TIFFfreeExt(tif, sp->tbuf);
2502 sp->tbuf = NULL;
2503 if (multiply_ms(tif, module, sp->tbuflen, (tmsize_t)sizeof(uint32_t)) ==
2504 0 ||
2505 (sp->tbuf = (uint8_t *)_TIFFmallocExt(
2506 tif, (tmsize_t)((size_t)sp->tbuflen * sizeof(uint32_t)))) == NULL)
2507 {
2508 TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
2509 return (0);
2510 }
2511 return (1);
2512}
2513
2523static int LogLuvFixupTags(TIFF *tif)
2524{
2525 (void)tif;
2526 return (1);
2527}
2528
2546static int LogLuvSetupDecode(TIFF *tif)
2547{
2548 static const char module[] = "LogLuvSetupDecode";
2549 LogLuvState *sp = DecoderState(tif);
2550 TIFFDirectory *td = &tif->tif_dir;
2551
2553 switch (td->td_photometric)
2554 {
2555 case PHOTOMETRIC_LOGLUV:
2556 if (!LogLuvInitState(tif))
2557 break;
2559 {
2561 switch (sp->user_datafmt)
2562 {
2564 sp->tfunc = Luv24toXYZ;
2565 break;
2567 sp->tfunc = Luv24toLuv48;
2568 break;
2569 case SGILOGDATAFMT_8BIT:
2570 sp->tfunc = Luv24toRGB;
2571 break;
2572 default:
2573 break;
2574 }
2575 }
2576 else
2577 {
2579 switch (sp->user_datafmt)
2580 {
2582 sp->tfunc = Luv32toXYZ;
2583 break;
2585 sp->tfunc = Luv32toLuv48;
2586 break;
2587 case SGILOGDATAFMT_8BIT:
2588 sp->tfunc = Luv32toRGB;
2589 break;
2590 default:
2591 break;
2592 }
2593 }
2594 return (1);
2595 case PHOTOMETRIC_LOGL:
2596 if (!LogL16InitState(tif))
2597 break;
2599 switch (sp->user_datafmt)
2600 {
2602 sp->tfunc = L16toY;
2603 break;
2604 case SGILOGDATAFMT_8BIT:
2605 sp->tfunc = L16toGry;
2606 break;
2607 default:
2608 break;
2609 }
2610 return (1);
2611 default:
2612 TIFFErrorExtR(tif, module,
2613 "Inappropriate photometric interpretation %" PRIu16
2614 " for SGILog compression; %s",
2615 td->td_photometric, "must be either LogLUV or LogL");
2616 break;
2617 }
2618 return (0);
2619}
2620
2636static int LogLuvSetupEncode(TIFF *tif)
2637{
2638 static const char module[] = "LogLuvSetupEncode";
2639 LogLuvState *sp = EncoderState(tif);
2640 TIFFDirectory *td = &tif->tif_dir;
2641
2642 switch (td->td_photometric)
2643 {
2644 case PHOTOMETRIC_LOGLUV:
2645 if (!LogLuvInitState(tif))
2646 return (0);
2648 {
2650 switch (sp->user_datafmt)
2651 {
2653 sp->tfunc = Luv24fromXYZ;
2654 break;
2656 sp->tfunc = Luv24fromLuv48;
2657 break;
2658 case SGILOGDATAFMT_RAW:
2659 break;
2660 default:
2661 goto notsupported;
2662 }
2663 }
2664 else
2665 {
2667 switch (sp->user_datafmt)
2668 {
2670 sp->tfunc = Luv32fromXYZ;
2671 break;
2673 sp->tfunc = Luv32fromLuv48;
2674 break;
2675 case SGILOGDATAFMT_RAW:
2676 break;
2677 default:
2678 goto notsupported;
2679 }
2680 }
2681 break;
2682 case PHOTOMETRIC_LOGL:
2683 if (!LogL16InitState(tif))
2684 return (0);
2686 switch (sp->user_datafmt)
2687 {
2689 sp->tfunc = L16fromY;
2690 break;
2692 break;
2693 default:
2694 goto notsupported;
2695 }
2696 break;
2697 default:
2698 TIFFErrorExtR(tif, module,
2699 "Inappropriate photometric interpretation %" PRIu16
2700 " for SGILog compression; %s",
2701 td->td_photometric, "must be either LogLUV or LogL");
2702 return (0);
2703 }
2704 sp->encoder_state = 1;
2705 return (1);
2706notsupported:
2707 TIFFErrorExtR(tif, module,
2708 "SGILog compression supported only for %s, or raw data",
2709 td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
2710 return (0);
2711}
2712
2731static void LogLuvClose(TIFF *tif)
2732{
2733 LogLuvState *sp = (LogLuvState *)tif->tif_data;
2734 TIFFDirectory *td = &tif->tif_dir;
2735
2736 assert(sp != 0);
2737 /*
2738 * For consistency, we always want to write out the same
2739 * bitspersample and sampleformat for our TIFF file,
2740 * regardless of the data format being used by the application.
2741 * Since this routine is called after tags have been set but
2742 * before they have been recorded in the file, we reset them here.
2743 * Note: this is really a nasty approach. See PixarLogClose
2744 */
2745 if (sp->encoder_state)
2746 {
2747 uint16_t new_spp =
2748 (uint16_t)((td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3);
2749 uint16_t new_bps = 16;
2750
2751 /* Invalidate tags whose allocation size depends on BitsPerSample
2752 * or SamplesPerPixel before we change those values. We must use
2753 * direct assignment here (not TIFFSetField) because
2754 * OkToChangeTag() rightfully blocks BPS/SPP changes after
2755 * TIFF_BEENWRITING is set. See PixarLogClose / bugzilla 2604 for
2756 * the same class of issue. */
2757 if (new_bps != td->td_bitspersample ||
2758 new_spp != td->td_samplesperpixel)
2759 {
2760 for (int i = 0; i < 3; i++)
2761 {
2762 _TIFFfreeExt(tif, td->td_colormap[i]);
2763 td->td_colormap[i] = NULL;
2764 _TIFFfreeExt(tif, td->td_transferfunction[i]);
2765 td->td_transferfunction[i] = NULL;
2766 }
2767 td->td_colormap_count = 0;
2768 td->td_colormap_alloc = 0;
2773 td->td_sminsamplevalue = NULL;
2776 td->td_smaxsamplevalue = NULL;
2778 }
2779 td->td_samplesperpixel = new_spp;
2780 td->td_bitspersample = new_bps;
2782 }
2783}
2784
2797static void LogLuvCleanup(TIFF *tif)
2798{
2799 LogLuvState *sp = (LogLuvState *)tif->tif_data;
2800
2801 assert(sp != 0);
2802
2805
2806 if (sp->tbuf)
2807 _TIFFfreeExt(tif, sp->tbuf);
2808 _TIFFfreeExt(tif, sp);
2809 tif->tif_data = NULL;
2810
2812}
2813
2836static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap)
2837{
2838 static const char module[] = "LogLuvVSetField";
2839 LogLuvState *sp = DecoderState(tif);
2840 int bps, fmt, old_datafmt;
2841
2842 switch (tag)
2843 {
2845 old_datafmt = sp->user_datafmt;
2846 sp->user_datafmt = (int)va_arg(ap, int);
2847 /*
2848 * Tweak the TIFF header so that the rest of libtiff knows what
2849 * size of data will be passed between app and library, and
2850 * assume that the app knows what it is doing and is not
2851 * confused by these header manipulations...
2852 */
2853 switch (sp->user_datafmt)
2854 {
2856 bps = 32;
2857 fmt = SAMPLEFORMAT_IEEEFP;
2858 break;
2860 bps = 16;
2861 fmt = SAMPLEFORMAT_INT;
2862 break;
2863 case SGILOGDATAFMT_RAW:
2864 bps = 32;
2865 fmt = SAMPLEFORMAT_UINT;
2867 return (0);
2868 break;
2869 case SGILOGDATAFMT_8BIT:
2870 bps = 8;
2871 fmt = SAMPLEFORMAT_UINT;
2872 break;
2873 default:
2875 tif, tif->tif_name,
2876 "Unknown data format %d for LogLuv compression",
2877 sp->user_datafmt);
2878 return (0);
2879 }
2880 /* Undo the samples/pixel change made for SGILOGDATAFMT_RAW. */
2881 if (old_datafmt == SGILOGDATAFMT_RAW &&
2882 sp->user_datafmt != SGILOGDATAFMT_RAW &&
2884 {
2886 return (0);
2887 }
2888
2889 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps))
2890 return (0);
2891 if (!TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt))
2892 return (0);
2893 /*
2894 * Must recalculate sizes should bits/sample change.
2895 */
2896 tif->tif_dir.td_tilesize =
2897 isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)-1;
2899 /*
2900 * pixel_size, tfunc and tbuf depend on the data format: redo
2901 * the codec setup if it has already been done.
2902 */
2903 if (sp->user_datafmt != old_datafmt &&
2904 (tif->tif_flags & TIFF_CODERSETUP))
2905 {
2906 /*
2907 * pixel_size, tfunc and tbuf belong to the old data format.
2908 * Redo the decoder setup before the next row is decoded.
2909 * If that fails, drop the setup and invalidate the current
2910 * strip and tile so the next read retries setup instead of
2911 * using the half-updated state.
2912 */
2913 if (!LogLuvSetupDecode(tif))
2914 {
2915 tif->tif_flags &= ~TIFF_CODERSETUP;
2917 tif->tif_dir.td_curtile = NOTILE;
2918 return (0);
2919 }
2920 }
2921 return (1);
2923 sp->encode_meth = (int)va_arg(ap, int);
2924 if (sp->encode_meth != SGILOGENCODE_NODITHER &&
2925 sp->encode_meth != SGILOGENCODE_RANDITHER)
2926 {
2927 TIFFErrorExtR(tif, module,
2928 "Unknown encoding %d for LogLuv compression",
2929 sp->encode_meth);
2930 return (0);
2931 }
2932 return (1);
2933 default:
2934 return (*sp->vsetparent)(tif, tag, ap);
2935 }
2936}
2937
2954static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap)
2955{
2956 LogLuvState *sp = (LogLuvState *)tif->tif_data;
2957
2958 switch (tag)
2959 {
2961 *va_arg(ap, int *) = sp->user_datafmt;
2962 return (1);
2963 default:
2964 return (*sp->vgetparent)(tif, tag, ap);
2965 }
2966}
2967
2975static const TIFFField LogLuvFields[] = {
2977 TRUE, FALSE, "SGILogDataFmt", NULL},
2979 TRUE, FALSE, "SGILogEncode", NULL}};
2980
2981int TIFFInitSGILog(TIFF *tif, int scheme)
2982{
2983 static const char module[] = "TIFFInitSGILog";
2984 LogLuvState *sp;
2985
2986 assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
2987
2988 /*
2989 * Merge codec-specific tag information.
2990 */
2992 {
2993 TIFFErrorExtR(tif, module, "Merging SGILog codec-specific tags failed");
2994 return 0;
2995 }
2996
2997 /*
2998 * Allocate state block so tag methods have storage to record values.
2999 */
3000 tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LogLuvState));
3001 if (tif->tif_data == NULL)
3002 goto bad;
3003 sp = (LogLuvState *)tif->tif_data;
3004 _TIFFmemset((void *)sp, 0, sizeof(*sp));
3005 sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
3006 sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? SGILOGENCODE_RANDITHER
3008 sp->tfunc = _logLuvNop;
3009
3010 /*
3011 * Install codec methods.
3012 * NB: tif_decoderow & tif_encoderow are filled
3013 * in at setup time.
3014 */
3022 tif->tif_close = LogLuvClose;
3024
3025 /*
3026 * Override parent get/set field methods.
3027 */
3028 sp->vgetparent = tif->tif_tagmethods.vgetfield;
3029 tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */
3030 sp->vsetparent = tif->tif_tagmethods.vsetfield;
3031 tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */
3032
3033 return (1);
3034bad:
3035 TIFFErrorExtR(tif, module, "%s: No space for LogLuv state block",
3036 tif->tif_name);
3037 return (0);
3038}
3039#endif /* LOGLUV_SUPPORT */
3040
tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
Check and multiply two signed-size operands.
Definition tif_aux.c:91
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
Definition tif_config.h:294
#define FIELD_TRANSFERFUNCTION
Index the transfer function presence bit; not an on-disk tag number.
Definition tif_dir.h:580
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define FIELD_SMAXSAMPLEVALUE
Index the per-sample maximum presence bit; not an on-disk tag number.
Definition tif_dir.h:544
#define FIELD_COLORMAP
Index the colormap presence bit; not an on-disk tag number.
Definition tif_dir.h:524
#define NOSTRIP
Represent the absence of a current strip with UINT32_MAX.
Definition tif_dir.h:250
#define FIELD_SMINSAMPLEVALUE
Index the per-sample minimum presence bit; not an on-disk tag number.
Definition tif_dir.h:539
#define FIELD_PSEUDO
Share bit zero with FIELD_IGNORE for pseudo-tags.
Definition tif_dir.h:620
#define NOTILE
Represent the absence of a current tile with UINT32_MAX.
Definition tif_dir.h:264
#define TIFFClrFieldBit(tif, field)
Clear a directory presence bit.
Definition tif_dir.h:688
@ TIFF_SETGET_INT
Promoted int scalar representation.
Definition tif_dir.h:762
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
Leave decoded bytes unchanged.
Definition tif_read.c:2209
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
#define U_NEU
Select neutral u-prime chromaticity.
Definition tiffio.h:5168
static int LogLuvInitState(TIFF *tif)
Validate contiguous LogLuv format and allocate packed translation pixels.
Definition tif_luv.c:2452
static int LogLuvDecode32(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Decode four compressed colour byte planes and translate one row.
Definition tif_luv.c:516
static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap)
Set SGILog user format or dithering and update representation metadata.
Definition tif_luv.c:2836
static const TIFFField LogLuvFields[]
Define SGILog user format and encoding-method pseudo-tags.
Definition tif_luv.c:2975
static void LuvRawFromRaw(LogLuvState *sp, uint8_t *op, tmsize_t n)
Copy native caller LogLuv words into staging.
Definition tif_luv.c:1437
void(* tfunc)(LogLuvState *, uint8_t *, tmsize_t)
Translate pixels between caller bytes and packed codec staging.
Definition tif_luv.c:214
static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
Quantise native float XYZ triplets into LogLuv32 staging.
Definition tif_luv.c:2150
#define UVSCALE
Scale uniform eight-bit chromaticity for LogLuv32.
Definition tiffio.h:5178
static int LogLuvSetupEncode(TIFF *tif)
Select SGILog row encoding and input conversion.
Definition tif_luv.c:2636
static void LogLuvClose(TIFF *tif)
Normalise encoded SGILog directory sample metadata before writing.
Definition tif_luv.c:2731
#define EncoderState(tif)
Borrow handle-owned SGILog encode state.
Definition tif_luv.c:226
#define TIFF_RAND_MAX
Limit SGILog pseudo-random output to fifteen bits.
Definition tif_luv.c:1148
static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogLuv24 codes to native signed Luv triplets.
Definition tif_luv.c:1824
static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
Quantise native signed Luv triplets into LogLuv32 staging.
Definition tif_luv.c:2185
#define PACK(s, b, f)
Combine directory format fields for a local inference switch.
static void L16toL16(LogLuvState *sp, uint8_t *op, tmsize_t n)
Copy encoded luminance words from staging to native caller bytes.
Definition tif_luv.c:1356
static void LogLuvCleanup(TIFF *tif)
Restore SGILog tag methods and release translation/codec state.
Definition tif_luv.c:2797
static int LogLuvFits(TIFF *tif, const char *module, const uint8_t *op, tmsize_t need)
Verify that need bytes fit before the raw buffer end.
Definition tif_luv.c:290
static int LogL16GuessDataFmt(TIFFDirectory *td)
Infer grayscale SGILog input format from directory fields.
Definition tif_luv.c:2253
static int LogLuvDecodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Decode complete SGILog strip rows through the selected row callback.
Definition tif_luv.c:619
#define SGILOGDATAFMT_UNKNOWN
Mark a user representation not yet inferred.
Definition tif_luv.c:228
#define V_NEU
Select neutral v-prime chromaticity.
Definition tiffio.h:5173
static int LogL16InitState(TIFF *tif)
Validate grayscale SGILog format and allocate translation pixels.
Definition tif_luv.c:2311
static int LogLuvReserve(TIFF *tif, uint8_t **opp, tmsize_t *occ, tmsize_t need)
Flush the raw data buffer when fewer than need bytes remain.
Definition tif_luv.c:258
#define NANGLES
Partition the gamut perimeter into one hundred hue angles.
Definition tif_luv.c:1577
#define DecoderState(tif)
Borrow handle-owned SGILog decode state.
Definition tif_luv.c:225
static uint32_t _TIFFRand(void)
Advance the shared SGILog pseudo-random dither generator.
Definition tif_luv.c:1163
#define MINRUN
Choose four pixels as the minimum byte-plane run.
Definition tif_luv.c:230
static int LogLuvDecode24(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Decode three stored bytes per packed colour pixel and translate one row.
Definition tif_luv.c:437
static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n)
Perform no SGILog pixel translation.
Definition tif_luv.c:2235
static void LuvRawToRaw(LogLuvState *sp, uint8_t *op, tmsize_t n)
Copy packed LogLuv words from staging to native caller bytes.
Definition tif_luv.c:1410
static void L16toGry(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogL16 values to clipped gamma-two grayscale bytes.
Definition tif_luv.c:1490
static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogLuv24 codes to native float XYZ triplets.
Definition tif_luv.c:1791
static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve SGILog user format or delegate another tag query.
Definition tif_luv.c:2954
static int LogLuvGuessDataFmt(TIFFDirectory *td)
Infer colour SGILog input format from directory fields.
Definition tif_luv.c:2382
static void L16fromY(LogLuvState *sp, uint8_t *op, tmsize_t n)
Quantise native float luminance into staged LogL16 codes.
Definition tif_luv.c:1522
static float LogLuvLoadFloatNativeUnaligned(const uint8_t *cp)
Read one native-order float through unaligned bytes.
Definition tif_luv.c:1245
static void LogLuvStore16NativeUnaligned(uint8_t *cp, int16_t v)
Write one native-order signed sixteen-bit value through unaligned bytes.
Definition tif_luv.c:1299
#define M_LN2
Supply the natural logarithm of two when absent.
Definition tif_luv.c:1142
static int oog_encode(double u, double v)
Map out-of-gamut chromaticity to a hue-matched boundary bin.
Definition tif_luv.c:1596
static int LogLuvDecodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Decode complete SGILog tile rows through the selected row callback.
Definition tif_luv.c:679
static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogLuv32 codes to native float XYZ triplets.
Definition tif_luv.c:2045
static void L16fromL16(LogLuvState *sp, uint8_t *op, tmsize_t n)
Copy native caller luminance words into staging.
Definition tif_luv.c:1383
static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogLuv32 codes to clipped gamma-two RGB bytes.
Definition tif_luv.c:2116
static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogLuv32 codes to native signed Luv triplets.
Definition tif_luv.c:2077
static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
Quantise native float XYZ triplets into LogLuv24 staging.
Definition tif_luv.c:1902
static int16_t LogLuvLoad16NativeUnaligned(const uint8_t *cp)
Read one native-order signed sixteen-bit value through unaligned bytes.
Definition tif_luv.c:1280
static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogLuv24 codes to clipped gamma-two RGB bytes.
Definition tif_luv.c:1867
static int LogLuvEncode24(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Translate one input row and emit three-byte packed colour pixels.
Definition tif_luv.c:866
static int LogLuvEncodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Encode complete SGILog tile rows through the selected row callback.
Definition tif_luv.c:1108
static int tiff_itrunc(double x, int m)
Quantise a finite value with optional shared random dithering.
Definition tif_luv.c:1189
static int LogL16Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Translate one input row and emit luminance byte-plane runs.
Definition tif_luv.c:739
static uint32_t LogLuvLoad32NativeUnaligned(const uint8_t *cp)
Read one native-order unsigned thirty-two-bit value through unaligned bytes.
Definition tif_luv.c:1316
static void LogLuvStore32NativeUnaligned(uint8_t *cp, uint32_t v)
Write one native-order unsigned thirty-two-bit value through unaligned bytes.
Definition tif_luv.c:1335
#define uv2ang(u, v)
Map finite chromaticity to a bounded hue lookup angle.
Definition tif_luv.c:1578
static int LogLuvSetupDecode(TIFF *tif)
Select SGILog row decoding and application-format conversion.
Definition tif_luv.c:2546
static const struct @4 uv_row[UV_NVS]
Map each v-prime row to its u-prime start and bin index interval.
static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
Quantise native signed Luv triplets into LogLuv24 staging.
Definition tif_luv.c:1938
static int LogLuvEncode32(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Translate one input row and emit colour byte-plane runs.
Definition tif_luv.c:938
static void LogLuvStoreFloatNativeUnaligned(uint8_t *cp, float v)
Write one native-order float through unaligned bytes.
Definition tif_luv.c:1263
static void L16toY(LogLuvState *sp, uint8_t *op, tmsize_t n)
Convert staged LogL16 values to native float luminance.
Definition tif_luv.c:1464
int TIFFInitSGILog(TIFF *tif, int scheme)
Allocate SGILog codec state and install row dispatch and tag hooks.
Definition tif_luv.c:2981
static int LogLuvFixupTags(TIFF *tif)
Accept SGILog tag fixup without changes.
Definition tif_luv.c:2523
static int LogLuvEncodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Encode complete SGILog strip rows through the selected row callback.
Definition tif_luv.c:1063
static int LogL16Decode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Decode two compressed luminance byte planes and translate one row.
Definition tif_luv.c:335
static tmsize_t multiply_ms(TIFF *tif, const char *module, tmsize_t m1, tmsize_t m2)
Multiply translation-buffer extents with signed-size checking.
Definition tif_luv.c:2289
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define TIFF_CODERSETUP
Mark completed codec setup.
Definition tiffiop.h:431
#define TRUE
Represent the legacy internal true value.
Definition tiffiop.h:131
#define FALSE
Represent the legacy internal false value.
Definition tiffiop.h:136
#define TIFFArrayCount(a)
Compute the element count of an actual array.
Definition tiffiop.h:1239
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:307
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:383
tmsize_t TIFFTileRowSize(TIFF *tif)
Compute the packed byte count of one tile row.
Definition tif_tile.c:276
int LogL10fromY(double Y, int em)
Encode linear luminance as a 10-bit logarithmic value.
Definition tif_luv.c:1567
int uv_decode(double *up, double *vp, int c)
Decode a quantised chromaticity index to a bin-centre pair.
Definition tif_luv.c:1686
void LogLuv32toXYZ(uint32_t p, float *XYZ)
Decode one packed 32-bit LogLuv pixel to linear XYZ.
Definition tif_luv.c:1970
double LogL10toY(int p10)
Decode 10-bit logarithmic luminance to a linear value.
Definition tif_luv.c:1557
int LogL16fromY(double Y, int em)
Encode linear luminance as a 16-bit logarithmic value.
Definition tif_luv.c:1213
void XYZtoRGB24(float *xyz, uint8_t *rgb)
Convert a linear XYZ triplet to display RGB bytes.
Definition tif_luv.c:1537
void LogLuv24toXYZ(uint32_t p, float *XYZ)
Decode one packed 24-bit LogLuv pixel to linear XYZ.
Definition tif_luv.c:1720
uint32_t LogLuv32fromXYZ(float *XYZ, int em)
Encode linear XYZ as one packed 32-bit LogLuv pixel.
Definition tif_luv.c:1995
uint32_t LogLuv24fromXYZ(float *XYZ, int em)
Encode linear XYZ as one packed 24-bit LogLuv pixel.
Definition tif_luv.c:1750
double LogL16toY(int p16)
Decode 16-bit logarithmic luminance to a linear value.
Definition tif_luv.c:1199
int uv_encode(double u, double v, int em)
Encode chromaticity using the irregular LogLuv quantisation table.
Definition tif_luv.c:1655
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
#define SGILOGDATAFMT_RAW
Uninterpreted data.
Definition tiff.h:1623
#define TIFFTAG_SGILOGENCODE
Pseudo-tag: SGILog data encoding control.
Definition tiff.h:1627
#define SGILOGENCODE_RANDITHER
Randomly dither encoded values.
Definition tiff.h:1631
#define SGILOGDATAFMT_8BIT
8-bit RGB monitor values.
Definition tiff.h:1625
#define SGILOGDATAFMT_FLOAT
IEEE float samples.
Definition tiff.h:1619
#define TIFFTAG_SGILOGDATAFMT
Pseudo-tag: SGILog user data format.
Definition tiff.h:1617
#define SGILOGENCODE_NODITHER
Do not dither encoded values.
Definition tiff.h:1629
#define SGILOGDATAFMT_16BIT
16-bit samples.
Definition tiff.h:1621
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Set a tag value in the current in-memory directory.
Definition tif_dir.c:1627
int(* TIFFVSetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag setter.
Definition tiffio.h:1521
#define TIFFTAG_BITSPERSAMPLE
TIFF tag: Bits per channel (sample).
Definition tiff.h:298
#define TIFFTAG_SAMPLESPERPIXEL
TIFF tag: Samples per pixel.
Definition tiff.h:456
#define PHOTOMETRIC_LOGL
CIE Log2(L).
Definition tiff.h:406
#define COMPRESSION_SGILOG
SGI Log Luminance RLE.
Definition tiff.h:358
#define SAMPLEFORMAT_UINT
Unsigned integer data (introduced in TIFF 6.0).
Definition tiff.h:610
#define PHOTOMETRIC_LOGLUV
CIE Log2(L) (u',v').
Definition tiff.h:408
#define SAMPLEFORMAT_IEEEFP
IEEE floating point data (introduced in TIFF 6.0).
Definition tiff.h:614
#define COMPRESSION_SGILOG24
SGI Log 24-bit packed.
Definition tiff.h:360
#define TIFFTAG_SAMPLEFORMAT
TIFF tag: Data sample format (introduced in TIFF 6.0).
Definition tiff.h:608
#define SAMPLEFORMAT_VOID
Untyped data (introduced in TIFF 6.0).
Definition tiff.h:616
#define SAMPLEFORMAT_INT
Signed integer data (introduced in TIFF 6.0).
Definition tiff.h:612
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
@ TIFF_SHORT
Unsigned 16-bit integer.
Definition tiff.h:237
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_colormap_alloc
Number of entries allocated in each ColorMap plane, zero when none is installed.
Definition tif_dir.h:228
tmsize_t td_tilesize
Cached decoded tile size in bytes; minus one when invalidated.
Definition tif_dir.h:259
uint16_t td_compression
COMPRESSION_* codec identifier.
Definition tif_dir.h:172
uint16_t td_photometric
PHOTOMETRIC_* interpretation.
Definition tif_dir.h:174
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
Definition tif_dir.h:245
double * td_sminsamplevalue
Owned SamplesPerPixel-length array of minimum values, or NULL.
Definition tif_dir.h:190
uint16_t td_bitspersample
Bits in each sample.
Definition tif_dir.h:168
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
uint16_t * td_transferfunction[3]
Up to three owned gamma curves of td_transferfunction_count entries each.
Definition tif_dir.h:320
uint32_t td_colormap_count
Number of entries in each ColorMap plane that the directory uses, zero when none is installed.
Definition tif_dir.h:223
double * td_smaxsamplevalue
Owned SamplesPerPixel-length array of maximum values, or NULL.
Definition tif_dir.h:192
uint16_t * td_colormap[3]
Three owned palette planes of td_colormap_alloc entries each, or NULL slots; presence is FIELD_COLORM...
Definition tif_dir.h:218
uint16_t td_sampleformat
SAMPLEFORMAT_* interpretation of samples.
Definition tif_dir.h:170
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
uint32_t td_curtile
Current tile index, or NOTILE.
Definition tif_dir.h:256
uint32_t td_transferfunction_count
Number of entries in each installed TransferFunction curve, zero when none is installed; the only rec...
Definition tif_dir.h:325
uint32_t td_curstrip
Current strip index, or NOSTRIP.
Definition tif_dir.h:242
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
Definition tiffio.h:1577
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Definition tiffio.h:1575
Describe a field's file type and internal argument contract.
Definition tif_dir.h:993
Own SGILog translation pixels and selected format conversion.
Definition tif_luv.c:177
int encode_meth
Cached no-dither/random-dither mode; invalid attempts can remain after rejection.
Definition tif_luv.c:192
int encoder_state
One after successful encoder setup, controlling close-time metadata changes.
Definition tif_luv.c:182
TIFFVSetMethod vsetparent
Borrowed saved parent setter restored during cleanup.
Definition tif_luv.c:222
int user_datafmt
Cached application format, initially UNKNOWN; rejected assignments can remain cached.
Definition tif_luv.c:187
uint8_t * tbuf
Owned Ext aligned translation storage, released during cleanup.
Definition tif_luv.c:199
tmsize_t tbuflen
Translation capacity in pixels, not bytes.
Definition tif_luv.c:201
int pixel_size
Positive bytes per application-facing pixel after setup.
Definition tif_luv.c:194
TIFFVSetMethod vgetparent
Borrowed saved parent getter under the declared compatible method type.
Definition tif_luv.c:220
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
Definition tiffiop.h:649
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
Definition tiffiop.h:653
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
Definition tiffiop.h:742
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
Definition tiffiop.h:626
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
Definition tiffiop.h:727
char * tif_name
Current filename pointer; may refer to inline or client-supplied storage.
Definition tiffiop.h:394
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
Definition tiffiop.h:643
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
Definition tiffiop.h:632
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
Definition tiffiop.h:645
TIFFVoidMethod tif_close
Codec close callback; distinct from the client I/O close callback.
Definition tiffiop.h:657
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
Definition tiffiop.h:661
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
Definition tiffiop.h:675
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
Definition tiffiop.h:681
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
Definition tiffiop.h:686
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Definition tiffiop.h:651
Declare private library state, callbacks, and implementation helpers.
Preserve the historical LogLuv chromaticity-bin table.
#define UV_VSTART
Locate the lower v-prime edge of the first row.
Definition uvcode.h:16
short ncum
Count all bins preceding this row.
Definition uvcode.h:33
short nus
Count horizontal bins in this chromaticity row.
Definition uvcode.h:31
#define UV_NVS
Count the 163 v-prime rows.
Definition uvcode.h:18
#define UV_NDIVS
Count all 16289 chromaticity bins.
Definition uvcode.h:14
#define UV_SQSIZ
Set the chromaticity bin side length.
Definition uvcode.h:12
float ustart
Lower u-prime boundary of this row.
Definition uvcode.h:29