1 /* $Id: tif_pixarlog.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1996-1997 Sam Leffler
5 * Copyright (c) 1996 Pixar
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Pixar, Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Pixar, Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL PIXAR, SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 #ifdef PIXARLOG_SUPPORT
32 * PixarLog Compression Support
34 * Contributed by Dan McCoy.
36 * PixarLog film support uses the TIFF library to store companded
37 * 11 bit values into a tiff file, which are compressed using the
40 * The codec can take as input and produce as output 32-bit IEEE float values
41 * as well as 16-bit or 8-bit unsigned integer values.
43 * On writing any of the above are converted into the internal
44 * 11-bit log format. In the case of 8 and 16 bit values, the
45 * input is assumed to be unsigned linear color values that represent
46 * the range 0-1. In the case of IEEE values, the 0-1 range is assumed to
47 * be the normal linear color range, in addition over 1 values are
48 * accepted up to a value of about 25.0 to encode "hot" hightlights and such.
49 * The encoding is lossless for 8-bit values, slightly lossy for the
50 * other bit depths. The actual color precision should be better
51 * than the human eye can perceive with extra room to allow for
52 * error introduced by further image computation. As with any quantized
53 * color format, it is possible to perform image calculations which
54 * expose the quantization error. This format should certainly be less
55 * susceptable to such errors than standard 8-bit encodings, but more
56 * susceptable than straight 16-bit or 32-bit encodings.
58 * On reading the internal format is converted to the desired output format.
59 * The program can request which format it desires by setting the internal
60 * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
61 * PIXARLOGDATAFMT_FLOAT = provide IEEE float values.
62 * PIXARLOGDATAFMT_16BIT = provide unsigned 16-bit integer values
63 * PIXARLOGDATAFMT_8BIT = provide unsigned 8-bit integer values
65 * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
66 * values with the difference that if there are exactly three or four channels
67 * (rgb or rgba) it swaps the channel order (bgr or abgr).
69 * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
70 * packed in 16-bit values. However no tools are supplied for interpreting
73 * "hot" (over 1.0) areas written in floating point get clamped to
74 * 1.0 in the integer data types.
76 * When the file is closed after writing, the bit depth and sample format
77 * are set always to appear as if 8-bit data has been written into it.
78 * That way a naive program unaware of the particulars of the encoding
79 * gets the format it is most likely able to handle.
81 * The codec does it's own horizontal differencing step on the coded
82 * values so the libraries predictor stuff should be turned off.
83 * The codec also handle byte swapping the encoded values as necessary
84 * since the library does not have the information necessary
85 * to know the bit depth of the raw unencoded buffer.
89 #include "tif_predict.h"
96 /* Tables for converting to/from 11 bit coded values */
98 #define TSIZE 2048 /* decode table size (11-bit tokens) */
99 #define TSIZEP1 2049 /* Plus one for slop */
100 #define ONE 1250 /* token value of 1.0 exactly */
101 #define RATIO 1.004 /* nominal ratio for log part */
103 #define CODE_MASK 0x7ff /* 11 bits. */
105 static float Fltsize;
106 static float LogK1, LogK2;
108 #define REPEAT(n, op) { int i; i=n; do { i--; op; } while (i>0); }
111 horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
114 register unsigned int cr, cg, cb, ca, mask;
115 register float t0, t1, t2, t3;
120 t0 = ToLinearF[cr = wp[0]];
121 t1 = ToLinearF[cg = wp[1]];
122 t2 = ToLinearF[cb = wp[2]];
131 t0 = ToLinearF[(cr += wp[0]) & mask];
132 t1 = ToLinearF[(cg += wp[1]) & mask];
133 t2 = ToLinearF[(cb += wp[2]) & mask];
138 } else if (stride == 4) {
139 t0 = ToLinearF[cr = wp[0]];
140 t1 = ToLinearF[cg = wp[1]];
141 t2 = ToLinearF[cb = wp[2]];
142 t3 = ToLinearF[ca = wp[3]];
152 t0 = ToLinearF[(cr += wp[0]) & mask];
153 t1 = ToLinearF[(cg += wp[1]) & mask];
154 t2 = ToLinearF[(cb += wp[2]) & mask];
155 t3 = ToLinearF[(ca += wp[3]) & mask];
162 REPEAT(stride, *op = ToLinearF[*wp&mask]; wp++; op++)
166 wp[stride] += *wp; *op = ToLinearF[*wp&mask]; wp++; op++)
174 horizontalAccumulate12(uint16 *wp, int n, int stride, int16 *op,
177 register unsigned int cr, cg, cb, ca, mask;
178 register float t0, t1, t2, t3;
180 #define SCALE12 2048.0F
181 #define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071)
186 t0 = ToLinearF[cr = wp[0]] * SCALE12;
187 t1 = ToLinearF[cg = wp[1]] * SCALE12;
188 t2 = ToLinearF[cb = wp[2]] * SCALE12;
197 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
198 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
199 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
204 } else if (stride == 4) {
205 t0 = ToLinearF[cr = wp[0]] * SCALE12;
206 t1 = ToLinearF[cg = wp[1]] * SCALE12;
207 t2 = ToLinearF[cb = wp[2]] * SCALE12;
208 t3 = ToLinearF[ca = wp[3]] * SCALE12;
218 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
219 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
220 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
221 t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
228 REPEAT(stride, t0 = ToLinearF[*wp&mask] * SCALE12;
229 *op = CLAMP12(t0); wp++; op++)
233 wp[stride] += *wp; t0 = ToLinearF[wp[stride]&mask]*SCALE12;
234 *op = CLAMP12(t0); wp++; op++)
242 horizontalAccumulate16(uint16 *wp, int n, int stride, uint16 *op,
245 register unsigned int cr, cg, cb, ca, mask;
250 op[0] = ToLinear16[cr = wp[0]];
251 op[1] = ToLinear16[cg = wp[1]];
252 op[2] = ToLinear16[cb = wp[2]];
258 op[0] = ToLinear16[(cr += wp[0]) & mask];
259 op[1] = ToLinear16[(cg += wp[1]) & mask];
260 op[2] = ToLinear16[(cb += wp[2]) & mask];
262 } else if (stride == 4) {
263 op[0] = ToLinear16[cr = wp[0]];
264 op[1] = ToLinear16[cg = wp[1]];
265 op[2] = ToLinear16[cb = wp[2]];
266 op[3] = ToLinear16[ca = wp[3]];
272 op[0] = ToLinear16[(cr += wp[0]) & mask];
273 op[1] = ToLinear16[(cg += wp[1]) & mask];
274 op[2] = ToLinear16[(cb += wp[2]) & mask];
275 op[3] = ToLinear16[(ca += wp[3]) & mask];
278 REPEAT(stride, *op = ToLinear16[*wp&mask]; wp++; op++)
282 wp[stride] += *wp; *op = ToLinear16[*wp&mask]; wp++; op++)
290 * Returns the log encoded 11-bit values with the horizontal
291 * differencing undone.
294 horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
296 register unsigned int cr, cg, cb, ca, mask;
301 op[0] = cr = wp[0]; op[1] = cg = wp[1]; op[2] = cb = wp[2];
307 op[0] = (cr += wp[0]) & mask;
308 op[1] = (cg += wp[1]) & mask;
309 op[2] = (cb += wp[2]) & mask;
311 } else if (stride == 4) {
312 op[0] = cr = wp[0]; op[1] = cg = wp[1];
313 op[2] = cb = wp[2]; op[3] = ca = wp[3];
319 op[0] = (cr += wp[0]) & mask;
320 op[1] = (cg += wp[1]) & mask;
321 op[2] = (cb += wp[2]) & mask;
322 op[3] = (ca += wp[3]) & mask;
325 REPEAT(stride, *op = *wp&mask; wp++; op++)
329 wp[stride] += *wp; *op = *wp&mask; wp++; op++)
337 horizontalAccumulate8(uint16 *wp, int n, int stride, unsigned char *op,
338 unsigned char *ToLinear8)
340 register unsigned int cr, cg, cb, ca, mask;
345 op[0] = ToLinear8[cr = wp[0]];
346 op[1] = ToLinear8[cg = wp[1]];
347 op[2] = ToLinear8[cb = wp[2]];
353 op[0] = ToLinear8[(cr += wp[0]) & mask];
354 op[1] = ToLinear8[(cg += wp[1]) & mask];
355 op[2] = ToLinear8[(cb += wp[2]) & mask];
357 } else if (stride == 4) {
358 op[0] = ToLinear8[cr = wp[0]];
359 op[1] = ToLinear8[cg = wp[1]];
360 op[2] = ToLinear8[cb = wp[2]];
361 op[3] = ToLinear8[ca = wp[3]];
367 op[0] = ToLinear8[(cr += wp[0]) & mask];
368 op[1] = ToLinear8[(cg += wp[1]) & mask];
369 op[2] = ToLinear8[(cb += wp[2]) & mask];
370 op[3] = ToLinear8[(ca += wp[3]) & mask];
373 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
377 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
386 horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
387 unsigned char *ToLinear8)
389 register unsigned int cr, cg, cb, ca, mask;
390 register unsigned char t0, t1, t2, t3;
396 t1 = ToLinear8[cb = wp[2]];
397 t2 = ToLinear8[cg = wp[1]];
398 t3 = ToLinear8[cr = wp[0]];
408 t1 = ToLinear8[(cb += wp[2]) & mask];
409 t2 = ToLinear8[(cg += wp[1]) & mask];
410 t3 = ToLinear8[(cr += wp[0]) & mask];
415 } else if (stride == 4) {
416 t0 = ToLinear8[ca = wp[3]];
417 t1 = ToLinear8[cb = wp[2]];
418 t2 = ToLinear8[cg = wp[1]];
419 t3 = ToLinear8[cr = wp[0]];
429 t0 = ToLinear8[(ca += wp[3]) & mask];
430 t1 = ToLinear8[(cb += wp[2]) & mask];
431 t2 = ToLinear8[(cg += wp[1]) & mask];
432 t3 = ToLinear8[(cr += wp[0]) & mask];
439 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
443 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
451 * State block for each open TIFF
452 * file using PixarLog compression/decompression.
455 TIFFPredictorState predict;
462 #define PLSTATE_INIT 1
464 TIFFVSetMethod vgetparent; /* super-class method */
465 TIFFVSetMethod vsetparent; /* super-class method */
469 unsigned char *ToLinear8;
471 uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
477 PixarLogMakeTables(PixarLogState *sp)
481 * We make several tables here to convert between various external
482 * representations (float, 16-bit, and 8-bit) and the internal
483 * 11-bit companded representation. The 11-bit representation has two
484 * distinct regions. A linear bottom end up through .018316 in steps
485 * of about .000073, and a region of constant ratio up to about 25.
486 * These floating point numbers are stored in the main table ToLinearF.
487 * All other tables are derived from this one. The tables (and the
488 * ratios) are continuous at the internal seam.
493 double b, c, linstep, v;
496 unsigned char *ToLinear8;
498 uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
502 nlin = (int)(1./c); /* nlin must be an integer */
504 b = exp(-c*ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
505 linstep = b*c*exp(1.);
507 LogK1 = (float)(1./c); /* if (v >= 2) token = k1*log(v*k2) */
508 LogK2 = (float)(1./b);
509 lt2size = (int)(2./linstep) + 1;
510 FromLT2 = (uint16 *)_TIFFmalloc(lt2size*sizeof(uint16));
511 From14 = (uint16 *)_TIFFmalloc(16384*sizeof(uint16));
512 From8 = (uint16 *)_TIFFmalloc(256*sizeof(uint16));
513 ToLinearF = (float *)_TIFFmalloc(TSIZEP1 * sizeof(float));
514 ToLinear16 = (uint16 *)_TIFFmalloc(TSIZEP1 * sizeof(uint16));
515 ToLinear8 = (unsigned char *)_TIFFmalloc(TSIZEP1 * sizeof(unsigned char));
516 if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
517 ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) {
518 if (FromLT2) _TIFFfree(FromLT2);
519 if (From14) _TIFFfree(From14);
520 if (From8) _TIFFfree(From8);
521 if (ToLinearF) _TIFFfree(ToLinearF);
522 if (ToLinear16) _TIFFfree(ToLinear16);
523 if (ToLinear8) _TIFFfree(ToLinear8);
527 sp->ToLinearF = NULL;
528 sp->ToLinear16 = NULL;
529 sp->ToLinear8 = NULL;
535 for (i = 0; i < nlin; i++) {
537 ToLinearF[j++] = (float)v;
540 for (i = nlin; i < TSIZE; i++)
541 ToLinearF[j++] = (float)(b*exp(c*i));
543 ToLinearF[2048] = ToLinearF[2047];
545 for (i = 0; i < TSIZEP1; i++) {
546 v = ToLinearF[i]*65535.0 + 0.5;
547 ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16)v;
548 v = ToLinearF[i]*255.0 + 0.5;
549 ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v;
553 for (i = 0; i < lt2size; i++) {
554 if ((i*linstep)*(i*linstep) > ToLinearF[j]*ToLinearF[j+1])
560 * Since we lose info anyway on 16-bit data, we set up a 14-bit
561 * table and shift 16-bit values down two bits on input.
562 * saves a little table space.
565 for (i = 0; i < 16384; i++) {
566 while ((i/16383.)*(i/16383.) > ToLinearF[j]*ToLinearF[j+1])
572 for (i = 0; i < 256; i++) {
573 while ((i/255.)*(i/255.) > ToLinearF[j]*ToLinearF[j+1])
578 Fltsize = (float)(lt2size/2);
580 sp->ToLinearF = ToLinearF;
581 sp->ToLinear16 = ToLinear16;
582 sp->ToLinear8 = ToLinear8;
583 sp->FromLT2 = FromLT2;
590 #define DecoderState(tif) ((PixarLogState*) (tif)->tif_data)
591 #define EncoderState(tif) ((PixarLogState*) (tif)->tif_data)
593 static int PixarLogEncode(TIFF*, tidata_t, tsize_t, tsample_t);
594 static int PixarLogDecode(TIFF*, tidata_t, tsize_t, tsample_t);
596 #define N(a) (sizeof(a)/sizeof(a[0]))
597 #define PIXARLOGDATAFMT_UNKNOWN -1
600 PixarLogGuessDataFmt(TIFFDirectory *td)
602 int guess = PIXARLOGDATAFMT_UNKNOWN;
603 int format = td->td_sampleformat;
605 /* If the user didn't tell us his datafmt,
606 * take our best guess from the bitspersample.
608 switch (td->td_bitspersample) {
610 if (format == SAMPLEFORMAT_IEEEFP)
611 guess = PIXARLOGDATAFMT_FLOAT;
614 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
615 guess = PIXARLOGDATAFMT_16BIT;
618 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
619 guess = PIXARLOGDATAFMT_12BITPICIO;
622 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
623 guess = PIXARLOGDATAFMT_11BITLOG;
626 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
627 guess = PIXARLOGDATAFMT_8BIT;
635 multiply(size_t m1, size_t m2)
637 uint32 bytes = m1 * m2;
639 if (m1 && bytes / m1 != m2)
646 PixarLogSetupDecode(TIFF* tif)
648 TIFFDirectory *td = &tif->tif_dir;
649 PixarLogState* sp = DecoderState(tif);
651 static const char module[] = "PixarLogSetupDecode";
655 /* Make sure no byte swapping happens on the data
656 * after decompression. */
657 tif->tif_postdecode = _TIFFNoPostDecode;
659 /* for some reason, we can't do this in TIFFInitPixarLog */
661 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
662 td->td_samplesperpixel : 1);
663 tbuf_size = multiply(multiply(multiply(sp->stride, td->td_imagewidth),
664 td->td_rowsperstrip), sizeof(uint16));
667 sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
668 if (sp->tbuf == NULL)
670 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
671 sp->user_datafmt = PixarLogGuessDataFmt(td);
672 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
674 "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
675 td->td_bitspersample);
679 if (inflateInit(&sp->stream) != Z_OK) {
680 TIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
683 sp->state |= PLSTATE_INIT;
689 * Setup state for decoding a strip.
692 PixarLogPreDecode(TIFF* tif, tsample_t s)
694 PixarLogState* sp = DecoderState(tif);
698 sp->stream.next_in = tif->tif_rawdata;
699 sp->stream.avail_in = tif->tif_rawcc;
700 return (inflateReset(&sp->stream) == Z_OK);
704 PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
706 TIFFDirectory *td = &tif->tif_dir;
707 PixarLogState* sp = DecoderState(tif);
708 static const char module[] = "PixarLogDecode";
709 int i, nsamples, llen;
712 switch (sp->user_datafmt) {
713 case PIXARLOGDATAFMT_FLOAT:
714 nsamples = occ / sizeof(float); /* XXX float == 32 bits */
716 case PIXARLOGDATAFMT_16BIT:
717 case PIXARLOGDATAFMT_12BITPICIO:
718 case PIXARLOGDATAFMT_11BITLOG:
719 nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
721 case PIXARLOGDATAFMT_8BIT:
722 case PIXARLOGDATAFMT_8BITABGR:
726 TIFFError(tif->tif_name,
727 "%d bit input not supported in PixarLog",
728 td->td_bitspersample);
732 llen = sp->stride * td->td_imagewidth;
736 sp->stream.next_out = (unsigned char *) sp->tbuf;
737 sp->stream.avail_out = nsamples * sizeof(uint16);
739 int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
740 if (state == Z_STREAM_END) {
743 if (state == Z_DATA_ERROR) {
745 "%s: Decoding error at scanline %d, %s",
746 tif->tif_name, tif->tif_row, sp->stream.msg);
747 if (inflateSync(&sp->stream) != Z_OK)
752 TIFFError(module, "%s: zlib error: %s",
753 tif->tif_name, sp->stream.msg);
756 } while (sp->stream.avail_out > 0);
758 /* hopefully, we got all the bytes we needed */
759 if (sp->stream.avail_out != 0) {
761 "%s: Not enough data at scanline %d (short %d bytes)",
762 tif->tif_name, tif->tif_row, sp->stream.avail_out);
767 /* Swap bytes in the data if from a different endian machine. */
768 if (tif->tif_flags & TIFF_SWAB)
769 TIFFSwabArrayOfShort(up, nsamples);
771 for (i = 0; i < nsamples; i += llen, up += llen) {
772 switch (sp->user_datafmt) {
773 case PIXARLOGDATAFMT_FLOAT:
774 horizontalAccumulateF(up, llen, sp->stride,
775 (float *)op, sp->ToLinearF);
776 op += llen * sizeof(float);
778 case PIXARLOGDATAFMT_16BIT:
779 horizontalAccumulate16(up, llen, sp->stride,
780 (uint16 *)op, sp->ToLinear16);
781 op += llen * sizeof(uint16);
783 case PIXARLOGDATAFMT_12BITPICIO:
784 horizontalAccumulate12(up, llen, sp->stride,
785 (int16 *)op, sp->ToLinearF);
786 op += llen * sizeof(int16);
788 case PIXARLOGDATAFMT_11BITLOG:
789 horizontalAccumulate11(up, llen, sp->stride,
791 op += llen * sizeof(uint16);
793 case PIXARLOGDATAFMT_8BIT:
794 horizontalAccumulate8(up, llen, sp->stride,
795 (unsigned char *)op, sp->ToLinear8);
796 op += llen * sizeof(unsigned char);
798 case PIXARLOGDATAFMT_8BITABGR:
799 horizontalAccumulate8abgr(up, llen, sp->stride,
800 (unsigned char *)op, sp->ToLinear8);
801 op += llen * sizeof(unsigned char);
804 TIFFError(tif->tif_name,
805 "PixarLogDecode: unsupported bits/sample: %d",
806 td->td_bitspersample);
815 PixarLogSetupEncode(TIFF* tif)
817 TIFFDirectory *td = &tif->tif_dir;
818 PixarLogState* sp = EncoderState(tif);
820 static const char module[] = "PixarLogSetupEncode";
824 /* for some reason, we can't do this in TIFFInitPixarLog */
826 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
827 td->td_samplesperpixel : 1);
828 tbuf_size = multiply(multiply(multiply(sp->stride, td->td_imagewidth),
829 td->td_rowsperstrip), sizeof(uint16));
832 sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
833 if (sp->tbuf == NULL)
835 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
836 sp->user_datafmt = PixarLogGuessDataFmt(td);
837 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
838 TIFFError(module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
842 if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
843 TIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
846 sp->state |= PLSTATE_INIT;
852 * Reset encoding state at the start of a strip.
855 PixarLogPreEncode(TIFF* tif, tsample_t s)
857 PixarLogState *sp = EncoderState(tif);
861 sp->stream.next_out = tif->tif_rawdata;
862 sp->stream.avail_out = tif->tif_rawdatasize;
863 return (deflateReset(&sp->stream) == Z_OK);
867 horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
870 int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
871 float fltsize = Fltsize;
873 #define CLAMP(v) ( (v<(float)0.) ? 0 \
874 : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
875 : (v>(float)24.2) ? 2047 \
876 : LogK1*log(v*LogK2) + 0.5 )
881 r2 = wp[0] = (uint16) CLAMP(ip[0]);
882 g2 = wp[1] = (uint16) CLAMP(ip[1]);
883 b2 = wp[2] = (uint16) CLAMP(ip[2]);
889 r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
890 g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
891 b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
893 } else if (stride == 4) {
894 r2 = wp[0] = (uint16) CLAMP(ip[0]);
895 g2 = wp[1] = (uint16) CLAMP(ip[1]);
896 b2 = wp[2] = (uint16) CLAMP(ip[2]);
897 a2 = wp[3] = (uint16) CLAMP(ip[3]);
903 r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
904 g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
905 b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
906 a1 = (int32) CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
909 ip += n - 1; /* point to last one */
910 wp += n - 1; /* point to last one */
913 REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]);
919 REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp--; ip--)
925 horizontalDifference16(unsigned short *ip, int n, int stride,
926 unsigned short *wp, uint16 *From14)
928 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
930 /* assumption is unsigned pixel values */
932 #define CLAMP(v) From14[(v) >> 2]
937 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
938 b2 = wp[2] = CLAMP(ip[2]);
944 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
945 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
946 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
948 } else if (stride == 4) {
949 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
950 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
956 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
957 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
958 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
959 a1 = CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
962 ip += n - 1; /* point to last one */
963 wp += n - 1; /* point to last one */
966 REPEAT(stride, wp[0] = CLAMP(ip[0]);
972 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
979 horizontalDifference8(unsigned char *ip, int n, int stride,
980 unsigned short *wp, uint16 *From8)
982 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
985 #define CLAMP(v) (From8[(v)])
990 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
991 b2 = wp[2] = CLAMP(ip[2]);
995 r1 = CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
996 g1 = CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
997 b1 = CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
1001 } else if (stride == 4) {
1002 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1003 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
1007 r1 = CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
1008 g1 = CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
1009 b1 = CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
1010 a1 = CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
1015 wp += n + stride - 1; /* point to last one */
1016 ip += n + stride - 1; /* point to last one */
1019 REPEAT(stride, wp[0] = CLAMP(ip[0]);
1020 wp[stride] -= wp[0];
1025 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
1031 * Encode a chunk of pixels.
1034 PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
1036 TIFFDirectory *td = &tif->tif_dir;
1037 PixarLogState *sp = EncoderState(tif);
1038 static const char module[] = "PixarLogEncode";
1040 unsigned short * up;
1044 switch (sp->user_datafmt) {
1045 case PIXARLOGDATAFMT_FLOAT:
1046 n = cc / sizeof(float); /* XXX float == 32 bits */
1048 case PIXARLOGDATAFMT_16BIT:
1049 case PIXARLOGDATAFMT_12BITPICIO:
1050 case PIXARLOGDATAFMT_11BITLOG:
1051 n = cc / sizeof(uint16); /* XXX uint16 == 16 bits */
1053 case PIXARLOGDATAFMT_8BIT:
1054 case PIXARLOGDATAFMT_8BITABGR:
1058 TIFFError(tif->tif_name,
1059 "%d bit input not supported in PixarLog",
1060 td->td_bitspersample);
1064 llen = sp->stride * td->td_imagewidth;
1066 for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
1067 switch (sp->user_datafmt) {
1068 case PIXARLOGDATAFMT_FLOAT:
1069 horizontalDifferenceF((float *)bp, llen,
1070 sp->stride, up, sp->FromLT2);
1071 bp += llen * sizeof(float);
1073 case PIXARLOGDATAFMT_16BIT:
1074 horizontalDifference16((uint16 *)bp, llen,
1075 sp->stride, up, sp->From14);
1076 bp += llen * sizeof(uint16);
1078 case PIXARLOGDATAFMT_8BIT:
1079 horizontalDifference8((unsigned char *)bp, llen,
1080 sp->stride, up, sp->From8);
1081 bp += llen * sizeof(unsigned char);
1084 TIFFError(tif->tif_name,
1085 "%d bit input not supported in PixarLog",
1086 td->td_bitspersample);
1091 sp->stream.next_in = (unsigned char *) sp->tbuf;
1092 sp->stream.avail_in = n * sizeof(uint16);
1095 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
1096 TIFFError(module, "%s: Encoder error: %s",
1097 tif->tif_name, sp->stream.msg);
1100 if (sp->stream.avail_out == 0) {
1101 tif->tif_rawcc = tif->tif_rawdatasize;
1102 TIFFFlushData1(tif);
1103 sp->stream.next_out = tif->tif_rawdata;
1104 sp->stream.avail_out = tif->tif_rawdatasize;
1106 } while (sp->stream.avail_in > 0);
1111 * Finish off an encoded strip by flushing the last
1112 * string and tacking on an End Of Information code.
1116 PixarLogPostEncode(TIFF* tif)
1118 PixarLogState *sp = EncoderState(tif);
1119 static const char module[] = "PixarLogPostEncode";
1122 sp->stream.avail_in = 0;
1125 state = deflate(&sp->stream, Z_FINISH);
1129 if (sp->stream.avail_out != tif->tif_rawdatasize) {
1131 tif->tif_rawdatasize - sp->stream.avail_out;
1132 TIFFFlushData1(tif);
1133 sp->stream.next_out = tif->tif_rawdata;
1134 sp->stream.avail_out = tif->tif_rawdatasize;
1138 TIFFError(module, "%s: zlib error: %s",
1139 tif->tif_name, sp->stream.msg);
1142 } while (state != Z_STREAM_END);
1147 PixarLogClose(TIFF* tif)
1149 TIFFDirectory *td = &tif->tif_dir;
1151 /* In a really sneaky maneuver, on close, we covertly modify both
1152 * bitspersample and sampleformat in the directory to indicate
1153 * 8-bit linear. This way, the decode "just works" even for
1154 * readers that don't know about PixarLog, or how to set
1155 * the PIXARLOGDATFMT pseudo-tag.
1157 td->td_bitspersample = 8;
1158 td->td_sampleformat = SAMPLEFORMAT_UINT;
1162 PixarLogCleanup(TIFF* tif)
1164 PixarLogState* sp = (PixarLogState*) tif->tif_data;
1167 if (sp->FromLT2) _TIFFfree(sp->FromLT2);
1168 if (sp->From14) _TIFFfree(sp->From14);
1169 if (sp->From8) _TIFFfree(sp->From8);
1170 if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
1171 if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
1172 if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
1173 if (sp->state&PLSTATE_INIT) {
1174 if (tif->tif_mode == O_RDONLY)
1175 inflateEnd(&sp->stream);
1177 deflateEnd(&sp->stream);
1180 _TIFFfree(sp->tbuf);
1182 tif->tif_data = NULL;
1187 PixarLogVSetField(TIFF* tif, ttag_t tag, va_list ap)
1189 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1191 static const char module[] = "PixarLogVSetField";
1194 case TIFFTAG_PIXARLOGQUALITY:
1195 sp->quality = va_arg(ap, int);
1196 if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
1197 if (deflateParams(&sp->stream,
1198 sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
1199 TIFFError(module, "%s: zlib error: %s",
1200 tif->tif_name, sp->stream.msg);
1205 case TIFFTAG_PIXARLOGDATAFMT:
1206 sp->user_datafmt = va_arg(ap, int);
1207 /* Tweak the TIFF header so that the rest of libtiff knows what
1208 * size of data will be passed between app and library, and
1209 * assume that the app knows what it is doing and is not
1210 * confused by these header manipulations...
1212 switch (sp->user_datafmt) {
1213 case PIXARLOGDATAFMT_8BIT:
1214 case PIXARLOGDATAFMT_8BITABGR:
1215 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
1216 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1218 case PIXARLOGDATAFMT_11BITLOG:
1219 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1220 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1222 case PIXARLOGDATAFMT_12BITPICIO:
1223 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1224 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
1226 case PIXARLOGDATAFMT_16BIT:
1227 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1228 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1230 case PIXARLOGDATAFMT_FLOAT:
1231 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
1232 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
1236 * Must recalculate sizes should bits/sample change.
1238 tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
1239 tif->tif_scanlinesize = TIFFScanlineSize(tif);
1240 result = 1; /* NB: pseudo tag */
1243 result = (*sp->vsetparent)(tif, tag, ap);
1249 PixarLogVGetField(TIFF* tif, ttag_t tag, va_list ap)
1251 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1254 case TIFFTAG_PIXARLOGQUALITY:
1255 *va_arg(ap, int*) = sp->quality;
1257 case TIFFTAG_PIXARLOGDATAFMT:
1258 *va_arg(ap, int*) = sp->user_datafmt;
1261 return (*sp->vgetparent)(tif, tag, ap);
1266 static const TIFFFieldInfo pixarlogFieldInfo[] = {
1267 {TIFFTAG_PIXARLOGDATAFMT,0,0,TIFF_ANY, FIELD_PSEUDO,FALSE,FALSE,""},
1268 {TIFFTAG_PIXARLOGQUALITY,0,0,TIFF_ANY, FIELD_PSEUDO,FALSE,FALSE,""}
1272 TIFFInitPixarLog(TIFF* tif, int scheme)
1276 assert(scheme == COMPRESSION_PIXARLOG);
1279 * Allocate state block so tag methods have storage to record values.
1281 tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (PixarLogState));
1282 if (tif->tif_data == NULL)
1284 sp = (PixarLogState*) tif->tif_data;
1285 _TIFFmemset(sp, 0, sizeof (*sp));
1286 sp->stream.data_type = Z_BINARY;
1287 sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
1290 * Install codec methods.
1292 tif->tif_setupdecode = PixarLogSetupDecode;
1293 tif->tif_predecode = PixarLogPreDecode;
1294 tif->tif_decoderow = PixarLogDecode;
1295 tif->tif_decodestrip = PixarLogDecode;
1296 tif->tif_decodetile = PixarLogDecode;
1297 tif->tif_setupencode = PixarLogSetupEncode;
1298 tif->tif_preencode = PixarLogPreEncode;
1299 tif->tif_postencode = PixarLogPostEncode;
1300 tif->tif_encoderow = PixarLogEncode;
1301 tif->tif_encodestrip = PixarLogEncode;
1302 tif->tif_encodetile = PixarLogEncode;
1303 tif->tif_close = PixarLogClose;
1304 tif->tif_cleanup = PixarLogCleanup;
1306 /* Override SetField so we can handle our private pseudo-tag */
1307 _TIFFMergeFieldInfo(tif, pixarlogFieldInfo, N(pixarlogFieldInfo));
1308 sp->vgetparent = tif->tif_tagmethods.vgetfield;
1309 tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
1310 sp->vsetparent = tif->tif_tagmethods.vsetfield;
1311 tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
1313 /* Default values for codec-specific fields */
1314 sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
1317 /* we don't wish to use the predictor,
1318 * the default is none, which predictor value 1
1320 (void) TIFFPredictorInit(tif);
1323 * build the companding tables
1325 PixarLogMakeTables(sp);
1329 TIFFError("TIFFInitPixarLog", "No space for PixarLog state block");
1332 #endif /* PIXARLOG_SUPPORT */
1334 /* vim: set ts=8 sts=8 sw=8 noet: */