2 * Copyright (c) 1996-1997 Sam Leffler
3 * Copyright (c) 1996 Pixar
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 * Pixar, Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Pixar, Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL PIXAR, SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * 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
26 #ifdef PIXARLOG_SUPPORT
30 * PixarLog Compression Support
32 * Contributed by Dan McCoy.
34 * PixarLog film support uses the TIFF library to store companded
35 * 11 bit values into a tiff file, which are compressed using the
38 * The codec can take as input and produce as output 32-bit IEEE float values
39 * as well as 16-bit or 8-bit unsigned integer values.
41 * On writing any of the above are converted into the internal
42 * 11-bit log format. In the case of 8 and 16 bit values, the
43 * input is assumed to be unsigned linear color values that represent
44 * the range 0-1. In the case of IEEE values, the 0-1 range is assumed to
45 * be the normal linear color range, in addition over 1 values are
46 * accepted up to a value of about 25.0 to encode "hot" highlights and such.
47 * The encoding is lossless for 8-bit values, slightly lossy for the
48 * other bit depths. The actual color precision should be better
49 * than the human eye can perceive with extra room to allow for
50 * error introduced by further image computation. As with any quantized
51 * color format, it is possible to perform image calculations which
52 * expose the quantization error. This format should certainly be less
53 * susceptable to such errors than standard 8-bit encodings, but more
54 * susceptable than straight 16-bit or 32-bit encodings.
56 * On reading the internal format is converted to the desired output format.
57 * The program can request which format it desires by setting the internal
58 * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
59 * PIXARLOGDATAFMT_FLOAT = provide IEEE float values.
60 * PIXARLOGDATAFMT_16BIT = provide unsigned 16-bit integer values
61 * PIXARLOGDATAFMT_8BIT = provide unsigned 8-bit integer values
63 * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
64 * values with the difference that if there are exactly three or four channels
65 * (rgb or rgba) it swaps the channel order (bgr or abgr).
67 * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
68 * packed in 16-bit values. However no tools are supplied for interpreting
71 * "hot" (over 1.0) areas written in floating point get clamped to
72 * 1.0 in the integer data types.
74 * When the file is closed after writing, the bit depth and sample format
75 * are set always to appear as if 8-bit data has been written into it.
76 * That way a naive program unaware of the particulars of the encoding
77 * gets the format it is most likely able to handle.
79 * The codec does it's own horizontal differencing step on the coded
80 * values so the libraries predictor stuff should be turned off.
81 * The codec also handle byte swapping the encoded values as necessary
82 * since the library does not have the information necessary
83 * to know the bit depth of the raw unencoded buffer.
87 #include "tif_predict.h"
95 /* Tables for converting to/from 11 bit coded values */
97 #define TSIZE 2048 /* decode table size (11-bit tokens) */
98 #define TSIZEP1 2049 /* Plus one for slop */
99 #define ONE 1250 /* token value of 1.0 exactly */
100 #define RATIO 1.004 /* nominal ratio for log part */
102 #define CODE_MASK 0x7ff /* 11 bits. */
104 static float Fltsize
;
105 static float LogK1
, LogK2
;
107 #define REPEAT(n, op) { int i; i=n; do { i--; op; } while (i>0); }
110 horizontalAccumulateF(uint16
*wp
, int n
, int stride
, float *op
,
113 register unsigned int cr
, cg
, cb
, ca
, mask
;
114 register float t0
, t1
, t2
, t3
;
119 t0
= ToLinearF
[cr
= wp
[0]];
120 t1
= ToLinearF
[cg
= wp
[1]];
121 t2
= ToLinearF
[cb
= wp
[2]];
130 t0
= ToLinearF
[(cr
+= wp
[0]) & mask
];
131 t1
= ToLinearF
[(cg
+= wp
[1]) & mask
];
132 t2
= ToLinearF
[(cb
+= wp
[2]) & mask
];
137 } else if (stride
== 4) {
138 t0
= ToLinearF
[cr
= wp
[0]];
139 t1
= ToLinearF
[cg
= wp
[1]];
140 t2
= ToLinearF
[cb
= wp
[2]];
141 t3
= ToLinearF
[ca
= wp
[3]];
151 t0
= ToLinearF
[(cr
+= wp
[0]) & mask
];
152 t1
= ToLinearF
[(cg
+= wp
[1]) & mask
];
153 t2
= ToLinearF
[(cb
+= wp
[2]) & mask
];
154 t3
= ToLinearF
[(ca
+= wp
[3]) & mask
];
161 REPEAT(stride
, *op
= ToLinearF
[*wp
&mask
]; wp
++; op
++)
165 wp
[stride
] += *wp
; *op
= ToLinearF
[*wp
&mask
]; wp
++; op
++)
173 horizontalAccumulate12(uint16
*wp
, int n
, int stride
, int16
*op
,
176 register unsigned int cr
, cg
, cb
, ca
, mask
;
177 register float t0
, t1
, t2
, t3
;
179 #define SCALE12 2048.0
180 #define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071)
185 t0
= ToLinearF
[cr
= wp
[0]] * SCALE12
;
186 t1
= ToLinearF
[cg
= wp
[1]] * SCALE12
;
187 t2
= ToLinearF
[cb
= wp
[2]] * SCALE12
;
196 t0
= ToLinearF
[(cr
+= wp
[0]) & mask
] * SCALE12
;
197 t1
= ToLinearF
[(cg
+= wp
[1]) & mask
] * SCALE12
;
198 t2
= ToLinearF
[(cb
+= wp
[2]) & mask
] * SCALE12
;
203 } else if (stride
== 4) {
204 t0
= ToLinearF
[cr
= wp
[0]] * SCALE12
;
205 t1
= ToLinearF
[cg
= wp
[1]] * SCALE12
;
206 t2
= ToLinearF
[cb
= wp
[2]] * SCALE12
;
207 t3
= ToLinearF
[ca
= wp
[3]] * SCALE12
;
217 t0
= ToLinearF
[(cr
+= wp
[0]) & mask
] * SCALE12
;
218 t1
= ToLinearF
[(cg
+= wp
[1]) & mask
] * SCALE12
;
219 t2
= ToLinearF
[(cb
+= wp
[2]) & mask
] * SCALE12
;
220 t3
= ToLinearF
[(ca
+= wp
[3]) & mask
] * SCALE12
;
227 REPEAT(stride
, t0
= ToLinearF
[*wp
&mask
] * SCALE12
;
228 *op
= CLAMP12(t0
); wp
++; op
++)
232 wp
[stride
] += *wp
; t0
= ToLinearF
[wp
[stride
]&mask
]*SCALE12
;
233 *op
= CLAMP12(t0
); wp
++; op
++)
241 horizontalAccumulate16(uint16
*wp
, int n
, int stride
, uint16
*op
,
244 register unsigned int cr
, cg
, cb
, ca
, mask
;
249 op
[0] = ToLinear16
[cr
= wp
[0]];
250 op
[1] = ToLinear16
[cg
= wp
[1]];
251 op
[2] = ToLinear16
[cb
= wp
[2]];
257 op
[0] = ToLinear16
[(cr
+= wp
[0]) & mask
];
258 op
[1] = ToLinear16
[(cg
+= wp
[1]) & mask
];
259 op
[2] = ToLinear16
[(cb
+= wp
[2]) & mask
];
261 } else if (stride
== 4) {
262 op
[0] = ToLinear16
[cr
= wp
[0]];
263 op
[1] = ToLinear16
[cg
= wp
[1]];
264 op
[2] = ToLinear16
[cb
= wp
[2]];
265 op
[3] = ToLinear16
[ca
= wp
[3]];
271 op
[0] = ToLinear16
[(cr
+= wp
[0]) & mask
];
272 op
[1] = ToLinear16
[(cg
+= wp
[1]) & mask
];
273 op
[2] = ToLinear16
[(cb
+= wp
[2]) & mask
];
274 op
[3] = ToLinear16
[(ca
+= wp
[3]) & mask
];
277 REPEAT(stride
, *op
= ToLinear16
[*wp
&mask
]; wp
++; op
++)
281 wp
[stride
] += *wp
; *op
= ToLinear16
[*wp
&mask
]; wp
++; op
++)
289 * Returns the log encoded 11-bit values with the horizontal
290 * differencing undone.
293 horizontalAccumulate11(uint16
*wp
, int n
, int stride
, uint16
*op
)
295 register unsigned int cr
, cg
, cb
, ca
, mask
;
300 op
[0] = cr
= wp
[0]; op
[1] = cg
= wp
[1]; op
[2] = cb
= wp
[2];
306 op
[0] = (cr
+= wp
[0]) & mask
;
307 op
[1] = (cg
+= wp
[1]) & mask
;
308 op
[2] = (cb
+= wp
[2]) & mask
;
310 } else if (stride
== 4) {
311 op
[0] = cr
= wp
[0]; op
[1] = cg
= wp
[1];
312 op
[2] = cb
= wp
[2]; op
[3] = ca
= wp
[3];
318 op
[0] = (cr
+= wp
[0]) & mask
;
319 op
[1] = (cg
+= wp
[1]) & mask
;
320 op
[2] = (cb
+= wp
[2]) & mask
;
321 op
[3] = (ca
+= wp
[3]) & mask
;
324 REPEAT(stride
, *op
= *wp
&mask
; wp
++; op
++)
328 wp
[stride
] += *wp
; *op
= *wp
&mask
; wp
++; op
++)
336 horizontalAccumulate8(uint16
*wp
, int n
, int stride
, unsigned char *op
,
337 unsigned char *ToLinear8
)
339 register unsigned int cr
, cg
, cb
, ca
, mask
;
344 op
[0] = ToLinear8
[cr
= wp
[0]];
345 op
[1] = ToLinear8
[cg
= wp
[1]];
346 op
[2] = ToLinear8
[cb
= wp
[2]];
352 op
[0] = ToLinear8
[(cr
+= wp
[0]) & mask
];
353 op
[1] = ToLinear8
[(cg
+= wp
[1]) & mask
];
354 op
[2] = ToLinear8
[(cb
+= wp
[2]) & mask
];
356 } else if (stride
== 4) {
357 op
[0] = ToLinear8
[cr
= wp
[0]];
358 op
[1] = ToLinear8
[cg
= wp
[1]];
359 op
[2] = ToLinear8
[cb
= wp
[2]];
360 op
[3] = ToLinear8
[ca
= wp
[3]];
366 op
[0] = ToLinear8
[(cr
+= wp
[0]) & mask
];
367 op
[1] = ToLinear8
[(cg
+= wp
[1]) & mask
];
368 op
[2] = ToLinear8
[(cb
+= wp
[2]) & mask
];
369 op
[3] = ToLinear8
[(ca
+= wp
[3]) & mask
];
372 REPEAT(stride
, *op
= ToLinear8
[*wp
&mask
]; wp
++; op
++)
376 wp
[stride
] += *wp
; *op
= ToLinear8
[*wp
&mask
]; wp
++; op
++)
385 horizontalAccumulate8abgr(uint16
*wp
, int n
, int stride
, unsigned char *op
,
386 unsigned char *ToLinear8
)
388 register unsigned int cr
, cg
, cb
, ca
, mask
;
389 register unsigned char t0
, t1
, t2
, t3
;
395 t1
= ToLinear8
[cb
= wp
[2]];
396 t2
= ToLinear8
[cg
= wp
[1]];
397 t3
= ToLinear8
[cr
= wp
[0]];
407 t1
= ToLinear8
[(cb
+= wp
[2]) & mask
];
408 t2
= ToLinear8
[(cg
+= wp
[1]) & mask
];
409 t3
= ToLinear8
[(cr
+= wp
[0]) & mask
];
414 } else if (stride
== 4) {
415 t0
= ToLinear8
[ca
= wp
[3]];
416 t1
= ToLinear8
[cb
= wp
[2]];
417 t2
= ToLinear8
[cg
= wp
[1]];
418 t3
= ToLinear8
[cr
= wp
[0]];
428 t0
= ToLinear8
[(ca
+= wp
[3]) & mask
];
429 t1
= ToLinear8
[(cb
+= wp
[2]) & mask
];
430 t2
= ToLinear8
[(cg
+= wp
[1]) & mask
];
431 t3
= ToLinear8
[(cr
+= wp
[0]) & mask
];
438 REPEAT(stride
, *op
= ToLinear8
[*wp
&mask
]; wp
++; op
++)
442 wp
[stride
] += *wp
; *op
= ToLinear8
[*wp
&mask
]; wp
++; op
++)
450 * State block for each open TIFF
451 * file using PixarLog compression/decompression.
454 TIFFPredictorState predict
;
461 #define PLSTATE_INIT 1
463 TIFFVSetMethod vgetparent
; /* super-class method */
464 TIFFVSetMethod vsetparent
; /* super-class method */
468 unsigned char *ToLinear8
;
470 uint16
*From14
; /* Really for 16-bit data, but we shift down 2 */
476 PixarLogMakeTables(PixarLogState
*sp
)
480 * We make several tables here to convert between various external
481 * representations (float, 16-bit, and 8-bit) and the internal
482 * 11-bit companded representation. The 11-bit representation has two
483 * distinct regions. A linear bottom end up through .018316 in steps
484 * of about .000073, and a region of constant ratio up to about 25.
485 * These floating point numbers are stored in the main table ToLinearF.
486 * All other tables are derived from this one. The tables (and the
487 * ratios) are continuous at the internal seam.
492 double b
, c
, linstep
, v
;
495 unsigned char *ToLinear8
;
497 uint16
*From14
; /* Really for 16-bit data, but we shift down 2 */
501 nlin
= (int)1./c
; /* nlin must be an integer */
503 b
= exp(-c
*ONE
); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
504 linstep
= b
*c
*exp(1.);
506 LogK1
= 1./c
; /* if (v >= 2) token = k1*log(v*k2) */
508 lt2size
= (int)(2./linstep
) + 1;
509 FromLT2
= (uint16
*)_TIFFmalloc(lt2size
*sizeof(uint16
));
510 From14
= (uint16
*)_TIFFmalloc(16384*sizeof(uint16
));
511 From8
= (uint16
*)_TIFFmalloc(256*sizeof(uint16
));
512 ToLinearF
= (float *)_TIFFmalloc(TSIZEP1
* sizeof(float));
513 ToLinear16
= (uint16
*)_TIFFmalloc(TSIZEP1
* sizeof(uint16
));
514 ToLinear8
= (unsigned char *)_TIFFmalloc(TSIZEP1
* sizeof(unsigned char));
515 if (FromLT2
== NULL
|| From14
== NULL
|| From8
== NULL
||
516 ToLinearF
== NULL
|| ToLinear16
== NULL
|| ToLinear8
== NULL
) {
517 if (FromLT2
) _TIFFfree(FromLT2
);
518 if (From14
) _TIFFfree(From14
);
519 if (From8
) _TIFFfree(From8
);
520 if (ToLinearF
) _TIFFfree(ToLinearF
);
521 if (ToLinear16
) _TIFFfree(ToLinear16
);
522 if (ToLinear8
) _TIFFfree(ToLinear8
);
526 sp
->ToLinearF
= NULL
;
527 sp
->ToLinear16
= NULL
;
528 sp
->ToLinear8
= NULL
;
534 for (i
= 0; i
< nlin
; i
++) {
539 for (i
= nlin
; i
< TSIZE
; i
++)
540 ToLinearF
[j
++] = b
*exp(c
*i
);
542 ToLinearF
[2048] = ToLinearF
[2047];
544 for (i
= 0; i
< TSIZEP1
; i
++) {
545 v
= ToLinearF
[i
]*65535.0 + 0.5;
546 ToLinear16
[i
] = (v
> 65535.0) ? 65535 : (uint16
)v
;
547 v
= ToLinearF
[i
]*255.0 + 0.5;
548 ToLinear8
[i
] = (v
> 255.0) ? 255 : (unsigned char)v
;
552 for (i
= 0; i
< lt2size
; i
++) {
553 if ((i
*linstep
)*(i
*linstep
) > ToLinearF
[j
]*ToLinearF
[j
+1])
559 * Since we lose info anyway on 16-bit data, we set up a 14-bit
560 * table and shift 16-bit values down two bits on input.
561 * saves a little table space.
564 for (i
= 0; i
< 16384; i
++) {
565 while ((i
/16383.)*(i
/16383.) > ToLinearF
[j
]*ToLinearF
[j
+1])
571 for (i
= 0; i
< 256; i
++) {
572 while ((i
/255.)*(i
/255.) > ToLinearF
[j
]*ToLinearF
[j
+1])
579 sp
->ToLinearF
= ToLinearF
;
580 sp
->ToLinear16
= ToLinear16
;
581 sp
->ToLinear8
= ToLinear8
;
582 sp
->FromLT2
= FromLT2
;
589 #define DecoderState(tif) ((PixarLogState*) (tif)->tif_data)
590 #define EncoderState(tif) ((PixarLogState*) (tif)->tif_data)
592 static int PixarLogEncode(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
593 static int PixarLogDecode(TIFF
*, tidata_t
, tsize_t
, tsample_t
);
595 #define N(a) (sizeof(a)/sizeof(a[0]))
596 #define PIXARLOGDATAFMT_UNKNOWN -1
599 PixarLogGuessDataFmt(TIFFDirectory
*td
)
601 int guess
= PIXARLOGDATAFMT_UNKNOWN
;
602 int format
= td
->td_sampleformat
;
604 /* If the user didn't tell us his datafmt,
605 * take our best guess from the bitspersample.
607 switch (td
->td_bitspersample
) {
609 if (format
== SAMPLEFORMAT_IEEEFP
)
610 guess
= PIXARLOGDATAFMT_FLOAT
;
613 if (format
== SAMPLEFORMAT_VOID
|| format
== SAMPLEFORMAT_UINT
)
614 guess
= PIXARLOGDATAFMT_16BIT
;
617 if (format
== SAMPLEFORMAT_VOID
|| format
== SAMPLEFORMAT_INT
)
618 guess
= PIXARLOGDATAFMT_12BITPICIO
;
621 if (format
== SAMPLEFORMAT_VOID
|| format
== SAMPLEFORMAT_UINT
)
622 guess
= PIXARLOGDATAFMT_11BITLOG
;
625 if (format
== SAMPLEFORMAT_VOID
|| format
== SAMPLEFORMAT_UINT
)
626 guess
= PIXARLOGDATAFMT_8BIT
;
634 multiply(size_t m1
, size_t m2
)
636 uint32 bytes
= m1
* m2
;
638 if (m1
&& bytes
/ m1
!= m2
)
645 PixarLogSetupDecode(TIFF
* tif
)
647 TIFFDirectory
*td
= &tif
->tif_dir
;
648 PixarLogState
* sp
= DecoderState(tif
);
650 static const char module[] = "PixarLogSetupDecode";
654 /* Make sure no byte swapping happens on the data
655 * after decompression. */
656 tif
->tif_postdecode
= _TIFFNoPostDecode
;
658 /* for some reason, we can't do this in TIFFInitPixarLog */
660 sp
->stride
= (td
->td_planarconfig
== PLANARCONFIG_CONTIG
?
661 td
->td_samplesperpixel
: 1);
662 tbuf_size
= multiply(multiply(multiply(sp
->stride
, td
->td_imagewidth
),
663 td
->td_rowsperstrip
), sizeof(uint16
));
666 sp
->tbuf
= (uint16
*) _TIFFmalloc(tbuf_size
);
667 if (sp
->tbuf
== NULL
)
669 if (sp
->user_datafmt
== PIXARLOGDATAFMT_UNKNOWN
)
670 sp
->user_datafmt
= PixarLogGuessDataFmt(td
);
671 if (sp
->user_datafmt
== PIXARLOGDATAFMT_UNKNOWN
) {
673 "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
674 td
->td_bitspersample
);
678 if (inflateInit(&sp
->stream
) != Z_OK
) {
679 TIFFError(module, "%s: %s", tif
->tif_name
, sp
->stream
.msg
);
682 sp
->state
|= PLSTATE_INIT
;
688 * Setup state for decoding a strip.
691 PixarLogPreDecode(TIFF
* tif
, tsample_t s
)
693 PixarLogState
* sp
= DecoderState(tif
);
697 sp
->stream
.next_in
= tif
->tif_rawdata
;
698 sp
->stream
.avail_in
= tif
->tif_rawcc
;
699 return (inflateReset(&sp
->stream
) == Z_OK
);
703 PixarLogDecode(TIFF
* tif
, tidata_t op
, tsize_t occ
, tsample_t s
)
705 TIFFDirectory
*td
= &tif
->tif_dir
;
706 PixarLogState
* sp
= DecoderState(tif
);
707 static const char module[] = "PixarLogDecode";
708 int i
, nsamples
, llen
;
711 switch (sp
->user_datafmt
) {
712 case PIXARLOGDATAFMT_FLOAT
:
713 nsamples
= occ
/ sizeof(float); /* XXX float == 32 bits */
715 case PIXARLOGDATAFMT_16BIT
:
716 case PIXARLOGDATAFMT_12BITPICIO
:
717 case PIXARLOGDATAFMT_11BITLOG
:
718 nsamples
= occ
/ sizeof(uint16
); /* XXX uint16 == 16 bits */
720 case PIXARLOGDATAFMT_8BIT
:
721 case PIXARLOGDATAFMT_8BITABGR
:
725 TIFFError(tif
->tif_name
,
726 "%d bit input not supported in PixarLog",
727 td
->td_bitspersample
);
731 llen
= sp
->stride
* td
->td_imagewidth
;
735 sp
->stream
.next_out
= (unsigned char *) sp
->tbuf
;
736 sp
->stream
.avail_out
= nsamples
* sizeof(uint16
);
738 int state
= inflate(&sp
->stream
, Z_PARTIAL_FLUSH
);
739 if (state
== Z_STREAM_END
) {
742 if (state
== Z_DATA_ERROR
) {
744 "%s: Decoding error at scanline %d, %s",
745 tif
->tif_name
, tif
->tif_row
, sp
->stream
.msg
);
746 if (inflateSync(&sp
->stream
) != Z_OK
)
751 TIFFError(module, "%s: zlib error: %s",
752 tif
->tif_name
, sp
->stream
.msg
);
755 } while (sp
->stream
.avail_out
> 0);
757 /* hopefully, we got all the bytes we needed */
758 if (sp
->stream
.avail_out
!= 0) {
760 "%s: Not enough data at scanline %d (short %d bytes)",
761 tif
->tif_name
, tif
->tif_row
, sp
->stream
.avail_out
);
766 /* Swap bytes in the data if from a different endian machine. */
767 if (tif
->tif_flags
& TIFF_SWAB
)
768 TIFFSwabArrayOfShort(up
, nsamples
);
770 for (i
= 0; i
< nsamples
; i
+= llen
, up
+= llen
) {
771 switch (sp
->user_datafmt
) {
772 case PIXARLOGDATAFMT_FLOAT
:
773 horizontalAccumulateF(up
, llen
, sp
->stride
,
774 (float *)op
, sp
->ToLinearF
);
775 op
+= llen
* sizeof(float);
777 case PIXARLOGDATAFMT_16BIT
:
778 horizontalAccumulate16(up
, llen
, sp
->stride
,
779 (uint16
*)op
, sp
->ToLinear16
);
780 op
+= llen
* sizeof(uint16
);
782 case PIXARLOGDATAFMT_12BITPICIO
:
783 horizontalAccumulate12(up
, llen
, sp
->stride
,
784 (int16
*)op
, sp
->ToLinearF
);
785 op
+= llen
* sizeof(int16
);
787 case PIXARLOGDATAFMT_11BITLOG
:
788 horizontalAccumulate11(up
, llen
, sp
->stride
,
790 op
+= llen
* sizeof(uint16
);
792 case PIXARLOGDATAFMT_8BIT
:
793 horizontalAccumulate8(up
, llen
, sp
->stride
,
794 (unsigned char *)op
, sp
->ToLinear8
);
795 op
+= llen
* sizeof(unsigned char);
797 case PIXARLOGDATAFMT_8BITABGR
:
798 horizontalAccumulate8abgr(up
, llen
, sp
->stride
,
799 (unsigned char *)op
, sp
->ToLinear8
);
800 op
+= llen
* sizeof(unsigned char);
803 TIFFError(tif
->tif_name
,
804 "PixarLogDecode: unsupported bits/sample: %d",
805 td
->td_bitspersample
);
814 PixarLogSetupEncode(TIFF
* tif
)
816 TIFFDirectory
*td
= &tif
->tif_dir
;
817 PixarLogState
* sp
= EncoderState(tif
);
819 static const char module[] = "PixarLogSetupEncode";
823 /* for some reason, we can't do this in TIFFInitPixarLog */
825 sp
->stride
= (td
->td_planarconfig
== PLANARCONFIG_CONTIG
?
826 td
->td_samplesperpixel
: 1);
827 tbuf_size
= multiply(multiply(multiply(sp
->stride
, td
->td_imagewidth
),
828 td
->td_rowsperstrip
), sizeof(uint16
));
831 sp
->tbuf
= (uint16
*) _TIFFmalloc(tbuf_size
);
832 if (sp
->tbuf
== NULL
)
834 if (sp
->user_datafmt
== PIXARLOGDATAFMT_UNKNOWN
)
835 sp
->user_datafmt
= PixarLogGuessDataFmt(td
);
836 if (sp
->user_datafmt
== PIXARLOGDATAFMT_UNKNOWN
) {
837 TIFFError(module, "PixarLog compression can't handle %d bit linear encodings", td
->td_bitspersample
);
841 if (deflateInit(&sp
->stream
, sp
->quality
) != Z_OK
) {
842 TIFFError(module, "%s: %s", tif
->tif_name
, sp
->stream
.msg
);
845 sp
->state
|= PLSTATE_INIT
;
851 * Reset encoding state at the start of a strip.
854 PixarLogPreEncode(TIFF
* tif
, tsample_t s
)
856 PixarLogState
*sp
= EncoderState(tif
);
860 sp
->stream
.next_out
= tif
->tif_rawdata
;
861 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
862 return (deflateReset(&sp
->stream
) == Z_OK
);
866 horizontalDifferenceF(float *ip
, int n
, int stride
, uint16
*wp
, uint16
*FromLT2
)
869 register int r1
, g1
, b1
, a1
, r2
, g2
, b2
, a2
, mask
;
870 register float fltsize
= Fltsize
;
872 #define CLAMP(v) ( (v<(float)0.) ? 0 \
873 : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
874 : (v>(float)24.2) ? 2047 \
875 : LogK1*log(v*LogK2) + 0.5 )
880 r2
= wp
[0] = CLAMP(ip
[0]); g2
= wp
[1] = CLAMP(ip
[1]);
881 b2
= wp
[2] = CLAMP(ip
[2]);
887 r1
= CLAMP(ip
[0]); wp
[0] = (r1
-r2
) & mask
; r2
= r1
;
888 g1
= CLAMP(ip
[1]); wp
[1] = (g1
-g2
) & mask
; g2
= g1
;
889 b1
= CLAMP(ip
[2]); wp
[2] = (b1
-b2
) & mask
; b2
= b1
;
891 } else if (stride
== 4) {
892 r2
= wp
[0] = CLAMP(ip
[0]); g2
= wp
[1] = CLAMP(ip
[1]);
893 b2
= wp
[2] = CLAMP(ip
[2]); a2
= wp
[3] = CLAMP(ip
[3]);
899 r1
= CLAMP(ip
[0]); wp
[0] = (r1
-r2
) & mask
; r2
= r1
;
900 g1
= CLAMP(ip
[1]); wp
[1] = (g1
-g2
) & mask
; g2
= g1
;
901 b1
= CLAMP(ip
[2]); wp
[2] = (b1
-b2
) & mask
; b2
= b1
;
902 a1
= CLAMP(ip
[3]); wp
[3] = (a1
-a2
) & mask
; a2
= a1
;
905 ip
+= n
- 1; /* point to last one */
906 wp
+= n
- 1; /* point to last one */
909 REPEAT(stride
, wp
[0] = CLAMP(ip
[0]);
915 REPEAT(stride
, wp
[0] = CLAMP(ip
[0]); wp
--; ip
--)
921 horizontalDifference16(unsigned short *ip
, int n
, int stride
,
922 unsigned short *wp
, uint16
*From14
)
924 register int r1
, g1
, b1
, a1
, r2
, g2
, b2
, a2
, mask
;
926 /* assumption is unsigned pixel values */
928 #define CLAMP(v) From14[(v) >> 2]
933 r2
= wp
[0] = CLAMP(ip
[0]); g2
= wp
[1] = CLAMP(ip
[1]);
934 b2
= wp
[2] = CLAMP(ip
[2]);
940 r1
= CLAMP(ip
[0]); wp
[0] = (r1
-r2
) & mask
; r2
= r1
;
941 g1
= CLAMP(ip
[1]); wp
[1] = (g1
-g2
) & mask
; g2
= g1
;
942 b1
= CLAMP(ip
[2]); wp
[2] = (b1
-b2
) & mask
; b2
= b1
;
944 } else if (stride
== 4) {
945 r2
= wp
[0] = CLAMP(ip
[0]); g2
= wp
[1] = CLAMP(ip
[1]);
946 b2
= wp
[2] = CLAMP(ip
[2]); a2
= wp
[3] = CLAMP(ip
[3]);
952 r1
= CLAMP(ip
[0]); wp
[0] = (r1
-r2
) & mask
; r2
= r1
;
953 g1
= CLAMP(ip
[1]); wp
[1] = (g1
-g2
) & mask
; g2
= g1
;
954 b1
= CLAMP(ip
[2]); wp
[2] = (b1
-b2
) & mask
; b2
= b1
;
955 a1
= CLAMP(ip
[3]); wp
[3] = (a1
-a2
) & mask
; a2
= a1
;
958 ip
+= n
- 1; /* point to last one */
959 wp
+= n
- 1; /* point to last one */
962 REPEAT(stride
, wp
[0] = CLAMP(ip
[0]);
968 REPEAT(stride
, wp
[0] = CLAMP(ip
[0]); wp
--; ip
--)
975 horizontalDifference8(unsigned char *ip
, int n
, int stride
,
976 unsigned short *wp
, uint16
*From8
)
978 register int r1
, g1
, b1
, a1
, r2
, g2
, b2
, a2
, mask
;
981 #define CLAMP(v) (From8[(v)])
986 r2
= wp
[0] = CLAMP(ip
[0]); g2
= wp
[1] = CLAMP(ip
[1]);
987 b2
= wp
[2] = CLAMP(ip
[2]);
991 r1
= CLAMP(ip
[3]); wp
[3] = (r1
-r2
) & mask
; r2
= r1
;
992 g1
= CLAMP(ip
[4]); wp
[4] = (g1
-g2
) & mask
; g2
= g1
;
993 b1
= CLAMP(ip
[5]); wp
[5] = (b1
-b2
) & mask
; b2
= b1
;
997 } else if (stride
== 4) {
998 r2
= wp
[0] = CLAMP(ip
[0]); g2
= wp
[1] = CLAMP(ip
[1]);
999 b2
= wp
[2] = CLAMP(ip
[2]); a2
= wp
[3] = CLAMP(ip
[3]);
1003 r1
= CLAMP(ip
[4]); wp
[4] = (r1
-r2
) & mask
; r2
= r1
;
1004 g1
= CLAMP(ip
[5]); wp
[5] = (g1
-g2
) & mask
; g2
= g1
;
1005 b1
= CLAMP(ip
[6]); wp
[6] = (b1
-b2
) & mask
; b2
= b1
;
1006 a1
= CLAMP(ip
[7]); wp
[7] = (a1
-a2
) & mask
; a2
= a1
;
1011 wp
+= n
+ stride
- 1; /* point to last one */
1012 ip
+= n
+ stride
- 1; /* point to last one */
1015 REPEAT(stride
, wp
[0] = CLAMP(ip
[0]);
1016 wp
[stride
] -= wp
[0];
1021 REPEAT(stride
, wp
[0] = CLAMP(ip
[0]); wp
--; ip
--)
1027 * Encode a chunk of pixels.
1030 PixarLogEncode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
1032 TIFFDirectory
*td
= &tif
->tif_dir
;
1033 PixarLogState
*sp
= EncoderState(tif
);
1034 static const char module[] = "PixarLogEncode";
1036 unsigned short * up
;
1040 switch (sp
->user_datafmt
) {
1041 case PIXARLOGDATAFMT_FLOAT
:
1042 n
= cc
/ sizeof(float); /* XXX float == 32 bits */
1044 case PIXARLOGDATAFMT_16BIT
:
1045 case PIXARLOGDATAFMT_12BITPICIO
:
1046 case PIXARLOGDATAFMT_11BITLOG
:
1047 n
= cc
/ sizeof(uint16
); /* XXX uint16 == 16 bits */
1049 case PIXARLOGDATAFMT_8BIT
:
1050 case PIXARLOGDATAFMT_8BITABGR
:
1054 TIFFError(tif
->tif_name
,
1055 "%d bit input not supported in PixarLog",
1056 td
->td_bitspersample
);
1060 llen
= sp
->stride
* td
->td_imagewidth
;
1062 for (i
= 0, up
= sp
->tbuf
; i
< n
; i
+= llen
, up
+= llen
) {
1063 switch (sp
->user_datafmt
) {
1064 case PIXARLOGDATAFMT_FLOAT
:
1065 horizontalDifferenceF((float *)bp
, llen
,
1066 sp
->stride
, up
, sp
->FromLT2
);
1067 bp
+= llen
* sizeof(float);
1069 case PIXARLOGDATAFMT_16BIT
:
1070 horizontalDifference16((uint16
*)bp
, llen
,
1071 sp
->stride
, up
, sp
->From14
);
1072 bp
+= llen
* sizeof(uint16
);
1074 case PIXARLOGDATAFMT_8BIT
:
1075 horizontalDifference8((unsigned char *)bp
, llen
,
1076 sp
->stride
, up
, sp
->From8
);
1077 bp
+= llen
* sizeof(unsigned char);
1080 TIFFError(tif
->tif_name
,
1081 "%d bit input not supported in PixarLog",
1082 td
->td_bitspersample
);
1087 sp
->stream
.next_in
= (unsigned char *) sp
->tbuf
;
1088 sp
->stream
.avail_in
= n
* sizeof(uint16
);
1091 if (deflate(&sp
->stream
, Z_NO_FLUSH
) != Z_OK
) {
1092 TIFFError(module, "%s: Encoder error: %s",
1093 tif
->tif_name
, sp
->stream
.msg
);
1096 if (sp
->stream
.avail_out
== 0) {
1097 tif
->tif_rawcc
= tif
->tif_rawdatasize
;
1098 TIFFFlushData1(tif
);
1099 sp
->stream
.next_out
= tif
->tif_rawdata
;
1100 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
1102 } while (sp
->stream
.avail_in
> 0);
1107 * Finish off an encoded strip by flushing the last
1108 * string and tacking on an End Of Information code.
1112 PixarLogPostEncode(TIFF
* tif
)
1114 PixarLogState
*sp
= EncoderState(tif
);
1115 static const char module[] = "PixarLogPostEncode";
1118 sp
->stream
.avail_in
= 0;
1121 state
= deflate(&sp
->stream
, Z_FINISH
);
1125 if (sp
->stream
.avail_out
!= tif
->tif_rawdatasize
) {
1127 tif
->tif_rawdatasize
- sp
->stream
.avail_out
;
1128 TIFFFlushData1(tif
);
1129 sp
->stream
.next_out
= tif
->tif_rawdata
;
1130 sp
->stream
.avail_out
= tif
->tif_rawdatasize
;
1134 TIFFError(module, "%s: zlib error: %s",
1135 tif
->tif_name
, sp
->stream
.msg
);
1138 } while (state
!= Z_STREAM_END
);
1143 PixarLogClose(TIFF
* tif
)
1145 TIFFDirectory
*td
= &tif
->tif_dir
;
1147 /* In a really sneaky maneuver, on close, we covertly modify both
1148 * bitspersample and sampleformat in the directory to indicate
1149 * 8-bit linear. This way, the decode "just works" even for
1150 * readers that don't know about PixarLog, or how to set
1151 * the PIXARLOGDATFMT pseudo-tag.
1153 td
->td_bitspersample
= 8;
1154 td
->td_sampleformat
= SAMPLEFORMAT_UINT
;
1158 PixarLogCleanup(TIFF
* tif
)
1160 PixarLogState
* sp
= (PixarLogState
*) tif
->tif_data
;
1163 if (sp
->FromLT2
) _TIFFfree(sp
->FromLT2
);
1164 if (sp
->From14
) _TIFFfree(sp
->From14
);
1165 if (sp
->From8
) _TIFFfree(sp
->From8
);
1166 if (sp
->ToLinearF
) _TIFFfree(sp
->ToLinearF
);
1167 if (sp
->ToLinear16
) _TIFFfree(sp
->ToLinear16
);
1168 if (sp
->ToLinear8
) _TIFFfree(sp
->ToLinear8
);
1169 if (sp
->state
&PLSTATE_INIT
) {
1170 if (tif
->tif_mode
== O_RDONLY
)
1171 inflateEnd(&sp
->stream
);
1173 deflateEnd(&sp
->stream
);
1176 _TIFFfree(sp
->tbuf
);
1178 tif
->tif_data
= NULL
;
1183 PixarLogVSetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1185 PixarLogState
*sp
= (PixarLogState
*)tif
->tif_data
;
1187 static const char module[] = "PixarLogVSetField";
1190 case TIFFTAG_PIXARLOGQUALITY
:
1191 sp
->quality
= va_arg(ap
, int);
1192 if (tif
->tif_mode
!= O_RDONLY
&& (sp
->state
&PLSTATE_INIT
)) {
1193 if (deflateParams(&sp
->stream
,
1194 sp
->quality
, Z_DEFAULT_STRATEGY
) != Z_OK
) {
1195 TIFFError(module, "%s: zlib error: %s",
1196 tif
->tif_name
, sp
->stream
.msg
);
1201 case TIFFTAG_PIXARLOGDATAFMT
:
1202 sp
->user_datafmt
= va_arg(ap
, int);
1203 /* Tweak the TIFF header so that the rest of libtiff knows what
1204 * size of data will be passed between app and library, and
1205 * assume that the app knows what it is doing and is not
1206 * confused by these header manipulations...
1208 switch (sp
->user_datafmt
) {
1209 case PIXARLOGDATAFMT_8BIT
:
1210 case PIXARLOGDATAFMT_8BITABGR
:
1211 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 8);
1212 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_UINT
);
1214 case PIXARLOGDATAFMT_11BITLOG
:
1215 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 16);
1216 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_UINT
);
1218 case PIXARLOGDATAFMT_12BITPICIO
:
1219 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 16);
1220 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_INT
);
1222 case PIXARLOGDATAFMT_16BIT
:
1223 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 16);
1224 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_UINT
);
1226 case PIXARLOGDATAFMT_FLOAT
:
1227 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 32);
1228 TIFFSetField(tif
, TIFFTAG_SAMPLEFORMAT
, SAMPLEFORMAT_IEEEFP
);
1232 * Must recalculate sizes should bits/sample change.
1234 tif
->tif_tilesize
= TIFFTileSize(tif
);
1235 tif
->tif_scanlinesize
= TIFFScanlineSize(tif
);
1236 result
= 1; /* NB: pseudo tag */
1239 result
= (*sp
->vsetparent
)(tif
, tag
, ap
);
1245 PixarLogVGetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1247 PixarLogState
*sp
= (PixarLogState
*)tif
->tif_data
;
1250 case TIFFTAG_PIXARLOGQUALITY
:
1251 *va_arg(ap
, int*) = sp
->quality
;
1253 case TIFFTAG_PIXARLOGDATAFMT
:
1254 *va_arg(ap
, int*) = sp
->user_datafmt
;
1257 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1262 static const TIFFFieldInfo pixarlogFieldInfo
[] = {
1263 {TIFFTAG_PIXARLOGDATAFMT
,0,0,TIFF_ANY
, FIELD_PSEUDO
,FALSE
,FALSE
,""},
1264 {TIFFTAG_PIXARLOGQUALITY
,0,0,TIFF_ANY
, FIELD_PSEUDO
,FALSE
,FALSE
,""}
1268 TIFFInitPixarLog(TIFF
* tif
, int scheme
)
1272 assert(scheme
== COMPRESSION_PIXARLOG
);
1275 * Allocate state block so tag methods have storage to record values.
1277 tif
->tif_data
= (tidata_t
) _TIFFmalloc(sizeof (PixarLogState
));
1278 if (tif
->tif_data
== NULL
)
1280 sp
= (PixarLogState
*) tif
->tif_data
;
1281 memset(sp
, 0, sizeof (*sp
));
1282 sp
->stream
.data_type
= Z_BINARY
;
1283 sp
->user_datafmt
= PIXARLOGDATAFMT_UNKNOWN
;
1286 * Install codec methods.
1288 tif
->tif_setupdecode
= PixarLogSetupDecode
;
1289 tif
->tif_predecode
= PixarLogPreDecode
;
1290 tif
->tif_decoderow
= PixarLogDecode
;
1291 tif
->tif_decodestrip
= PixarLogDecode
;
1292 tif
->tif_decodetile
= PixarLogDecode
;
1293 tif
->tif_setupencode
= PixarLogSetupEncode
;
1294 tif
->tif_preencode
= PixarLogPreEncode
;
1295 tif
->tif_postencode
= PixarLogPostEncode
;
1296 tif
->tif_encoderow
= PixarLogEncode
;
1297 tif
->tif_encodestrip
= PixarLogEncode
;
1298 tif
->tif_encodetile
= PixarLogEncode
;
1299 tif
->tif_close
= PixarLogClose
;
1300 tif
->tif_cleanup
= PixarLogCleanup
;
1302 /* Override SetField so we can handle our private pseudo-tag */
1303 _TIFFMergeFieldInfo(tif
, pixarlogFieldInfo
, N(pixarlogFieldInfo
));
1304 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1305 tif
->tif_tagmethods
.vgetfield
= PixarLogVGetField
; /* hook for codec tags */
1306 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1307 tif
->tif_tagmethods
.vsetfield
= PixarLogVSetField
; /* hook for codec tags */
1309 /* Default values for codec-specific fields */
1310 sp
->quality
= Z_DEFAULT_COMPRESSION
; /* default comp. level */
1313 /* we don't wish to use the predictor,
1314 * the default is none, which predictor value 1
1316 (void) TIFFPredictorInit(tif
);
1319 * build the companding tables
1321 PixarLogMakeTables(sp
);
1325 TIFFError("TIFFInitPixarLog", "No space for PixarLog state block");
1328 #endif /* PIXARLOG_SUPPORT */