1 /* $Id: tif_pixarlog.c,v 1.38 2012-06-21 01:01:53 fwarmerdam 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.
87 * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc.
88 * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT
89 * as noted in http://trac.osgeo.org/gdal/ticket/3894. FrankW - Jan'11
92 #include "tif_predict.h"
99 /* Tables for converting to/from 11 bit coded values */
101 #define TSIZE 2048 /* decode table size (11-bit tokens) */
102 #define TSIZEP1 2049 /* Plus one for slop */
103 #define ONE 1250 /* token value of 1.0 exactly */
104 #define RATIO 1.004 /* nominal ratio for log part */
106 #define CODE_MASK 0x7ff /* 11 bits. */
108 static float Fltsize;
109 static float LogK1, LogK2;
111 #define REPEAT(n, op) { int i; i=n; do { i--; op; } while (i>0); }
114 horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
117 register unsigned int cr, cg, cb, ca, mask;
118 register float t0, t1, t2, t3;
123 t0 = ToLinearF[cr = (wp[0] & mask)];
124 t1 = ToLinearF[cg = (wp[1] & mask)];
125 t2 = ToLinearF[cb = (wp[2] & mask)];
134 t0 = ToLinearF[(cr += wp[0]) & mask];
135 t1 = ToLinearF[(cg += wp[1]) & mask];
136 t2 = ToLinearF[(cb += wp[2]) & mask];
141 } else if (stride == 4) {
142 t0 = ToLinearF[cr = (wp[0] & mask)];
143 t1 = ToLinearF[cg = (wp[1] & mask)];
144 t2 = ToLinearF[cb = (wp[2] & mask)];
145 t3 = ToLinearF[ca = (wp[3] & mask)];
155 t0 = ToLinearF[(cr += wp[0]) & mask];
156 t1 = ToLinearF[(cg += wp[1]) & mask];
157 t2 = ToLinearF[(cb += wp[2]) & mask];
158 t3 = ToLinearF[(ca += wp[3]) & mask];
165 REPEAT(stride, *op = ToLinearF[*wp&mask]; wp++; op++)
169 wp[stride] += *wp; *op = ToLinearF[*wp&mask]; wp++; op++)
177 horizontalAccumulate12(uint16 *wp, int n, int stride, int16 *op,
180 register unsigned int cr, cg, cb, ca, mask;
181 register float t0, t1, t2, t3;
183 #define SCALE12 2048.0F
184 #define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071)
189 t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
190 t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
191 t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
200 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
201 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
202 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
207 } else if (stride == 4) {
208 t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
209 t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
210 t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
211 t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
221 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
222 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
223 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
224 t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
231 REPEAT(stride, t0 = ToLinearF[*wp&mask] * SCALE12;
232 *op = CLAMP12(t0); wp++; op++)
236 wp[stride] += *wp; t0 = ToLinearF[wp[stride]&mask]*SCALE12;
237 *op = CLAMP12(t0); wp++; op++)
245 horizontalAccumulate16(uint16 *wp, int n, int stride, uint16 *op,
248 register unsigned int cr, cg, cb, ca, mask;
253 op[0] = ToLinear16[cr = (wp[0] & mask)];
254 op[1] = ToLinear16[cg = (wp[1] & mask)];
255 op[2] = ToLinear16[cb = (wp[2] & mask)];
261 op[0] = ToLinear16[(cr += wp[0]) & mask];
262 op[1] = ToLinear16[(cg += wp[1]) & mask];
263 op[2] = ToLinear16[(cb += wp[2]) & mask];
265 } else if (stride == 4) {
266 op[0] = ToLinear16[cr = (wp[0] & mask)];
267 op[1] = ToLinear16[cg = (wp[1] & mask)];
268 op[2] = ToLinear16[cb = (wp[2] & mask)];
269 op[3] = ToLinear16[ca = (wp[3] & mask)];
275 op[0] = ToLinear16[(cr += wp[0]) & mask];
276 op[1] = ToLinear16[(cg += wp[1]) & mask];
277 op[2] = ToLinear16[(cb += wp[2]) & mask];
278 op[3] = ToLinear16[(ca += wp[3]) & mask];
281 REPEAT(stride, *op = ToLinear16[*wp&mask]; wp++; op++)
285 wp[stride] += *wp; *op = ToLinear16[*wp&mask]; wp++; op++)
293 * Returns the log encoded 11-bit values with the horizontal
294 * differencing undone.
297 horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
299 register unsigned int cr, cg, cb, ca, mask;
304 op[0] = cr = wp[0]; op[1] = cg = wp[1]; op[2] = cb = wp[2];
310 op[0] = (cr += wp[0]) & mask;
311 op[1] = (cg += wp[1]) & mask;
312 op[2] = (cb += wp[2]) & mask;
314 } else if (stride == 4) {
315 op[0] = cr = wp[0]; op[1] = cg = wp[1];
316 op[2] = cb = wp[2]; op[3] = ca = wp[3];
322 op[0] = (cr += wp[0]) & mask;
323 op[1] = (cg += wp[1]) & mask;
324 op[2] = (cb += wp[2]) & mask;
325 op[3] = (ca += wp[3]) & mask;
328 REPEAT(stride, *op = *wp&mask; wp++; op++)
332 wp[stride] += *wp; *op = *wp&mask; wp++; op++)
340 horizontalAccumulate8(uint16 *wp, int n, int stride, unsigned char *op,
341 unsigned char *ToLinear8)
343 register unsigned int cr, cg, cb, ca, mask;
348 op[0] = ToLinear8[cr = (wp[0] & mask)];
349 op[1] = ToLinear8[cg = (wp[1] & mask)];
350 op[2] = ToLinear8[cb = (wp[2] & mask)];
356 op[0] = ToLinear8[(cr += wp[0]) & mask];
357 op[1] = ToLinear8[(cg += wp[1]) & mask];
358 op[2] = ToLinear8[(cb += wp[2]) & mask];
360 } else if (stride == 4) {
361 op[0] = ToLinear8[cr = (wp[0] & mask)];
362 op[1] = ToLinear8[cg = (wp[1] & mask)];
363 op[2] = ToLinear8[cb = (wp[2] & mask)];
364 op[3] = ToLinear8[ca = (wp[3] & mask)];
370 op[0] = ToLinear8[(cr += wp[0]) & mask];
371 op[1] = ToLinear8[(cg += wp[1]) & mask];
372 op[2] = ToLinear8[(cb += wp[2]) & mask];
373 op[3] = ToLinear8[(ca += wp[3]) & mask];
376 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
380 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
389 horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
390 unsigned char *ToLinear8)
392 register unsigned int cr, cg, cb, ca, mask;
393 register unsigned char t0, t1, t2, t3;
399 t1 = ToLinear8[cb = (wp[2] & mask)];
400 t2 = ToLinear8[cg = (wp[1] & mask)];
401 t3 = ToLinear8[cr = (wp[0] & mask)];
411 t1 = ToLinear8[(cb += wp[2]) & mask];
412 t2 = ToLinear8[(cg += wp[1]) & mask];
413 t3 = ToLinear8[(cr += wp[0]) & mask];
418 } else if (stride == 4) {
419 t0 = ToLinear8[ca = (wp[3] & mask)];
420 t1 = ToLinear8[cb = (wp[2] & mask)];
421 t2 = ToLinear8[cg = (wp[1] & mask)];
422 t3 = ToLinear8[cr = (wp[0] & mask)];
432 t0 = ToLinear8[(ca += wp[3]) & mask];
433 t1 = ToLinear8[(cb += wp[2]) & mask];
434 t2 = ToLinear8[(cg += wp[1]) & mask];
435 t3 = ToLinear8[(cr += wp[0]) & mask];
442 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
446 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
454 * State block for each open TIFF
455 * file using PixarLog compression/decompression.
458 TIFFPredictorState predict;
465 #define PLSTATE_INIT 1
467 TIFFVSetMethod vgetparent; /* super-class method */
468 TIFFVSetMethod vsetparent; /* super-class method */
472 unsigned char *ToLinear8;
474 uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
480 PixarLogMakeTables(PixarLogState *sp)
484 * We make several tables here to convert between various external
485 * representations (float, 16-bit, and 8-bit) and the internal
486 * 11-bit companded representation. The 11-bit representation has two
487 * distinct regions. A linear bottom end up through .018316 in steps
488 * of about .000073, and a region of constant ratio up to about 25.
489 * These floating point numbers are stored in the main table ToLinearF.
490 * All other tables are derived from this one. The tables (and the
491 * ratios) are continuous at the internal seam.
496 double b, c, linstep, v;
499 unsigned char *ToLinear8;
501 uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
505 nlin = (int)(1./c); /* nlin must be an integer */
507 b = exp(-c*ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
508 linstep = b*c*exp(1.);
510 LogK1 = (float)(1./c); /* if (v >= 2) token = k1*log(v*k2) */
511 LogK2 = (float)(1./b);
512 lt2size = (int)(2./linstep) + 1;
513 FromLT2 = (uint16 *)_TIFFmalloc(lt2size*sizeof(uint16));
514 From14 = (uint16 *)_TIFFmalloc(16384*sizeof(uint16));
515 From8 = (uint16 *)_TIFFmalloc(256*sizeof(uint16));
516 ToLinearF = (float *)_TIFFmalloc(TSIZEP1 * sizeof(float));
517 ToLinear16 = (uint16 *)_TIFFmalloc(TSIZEP1 * sizeof(uint16));
518 ToLinear8 = (unsigned char *)_TIFFmalloc(TSIZEP1 * sizeof(unsigned char));
519 if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
520 ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) {
521 if (FromLT2) _TIFFfree(FromLT2);
522 if (From14) _TIFFfree(From14);
523 if (From8) _TIFFfree(From8);
524 if (ToLinearF) _TIFFfree(ToLinearF);
525 if (ToLinear16) _TIFFfree(ToLinear16);
526 if (ToLinear8) _TIFFfree(ToLinear8);
530 sp->ToLinearF = NULL;
531 sp->ToLinear16 = NULL;
532 sp->ToLinear8 = NULL;
538 for (i = 0; i < nlin; i++) {
540 ToLinearF[j++] = (float)v;
543 for (i = nlin; i < TSIZE; i++)
544 ToLinearF[j++] = (float)(b*exp(c*i));
546 ToLinearF[2048] = ToLinearF[2047];
548 for (i = 0; i < TSIZEP1; i++) {
549 v = ToLinearF[i]*65535.0 + 0.5;
550 ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16)v;
551 v = ToLinearF[i]*255.0 + 0.5;
552 ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v;
556 for (i = 0; i < lt2size; i++) {
557 if ((i*linstep)*(i*linstep) > ToLinearF[j]*ToLinearF[j+1])
563 * Since we lose info anyway on 16-bit data, we set up a 14-bit
564 * table and shift 16-bit values down two bits on input.
565 * saves a little table space.
568 for (i = 0; i < 16384; i++) {
569 while ((i/16383.)*(i/16383.) > ToLinearF[j]*ToLinearF[j+1])
575 for (i = 0; i < 256; i++) {
576 while ((i/255.)*(i/255.) > ToLinearF[j]*ToLinearF[j+1])
581 Fltsize = (float)(lt2size/2);
583 sp->ToLinearF = ToLinearF;
584 sp->ToLinear16 = ToLinear16;
585 sp->ToLinear8 = ToLinear8;
586 sp->FromLT2 = FromLT2;
593 #define DecoderState(tif) ((PixarLogState*) (tif)->tif_data)
594 #define EncoderState(tif) ((PixarLogState*) (tif)->tif_data)
596 static int PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
597 static int PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
599 #define PIXARLOGDATAFMT_UNKNOWN -1
602 PixarLogGuessDataFmt(TIFFDirectory *td)
604 int guess = PIXARLOGDATAFMT_UNKNOWN;
605 int format = td->td_sampleformat;
607 /* If the user didn't tell us his datafmt,
608 * take our best guess from the bitspersample.
610 switch (td->td_bitspersample) {
612 if (format == SAMPLEFORMAT_IEEEFP)
613 guess = PIXARLOGDATAFMT_FLOAT;
616 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
617 guess = PIXARLOGDATAFMT_16BIT;
620 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
621 guess = PIXARLOGDATAFMT_12BITPICIO;
624 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
625 guess = PIXARLOGDATAFMT_11BITLOG;
628 if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
629 guess = PIXARLOGDATAFMT_8BIT;
637 multiply_ms(tmsize_t m1, tmsize_t m2)
639 tmsize_t bytes = m1 * m2;
641 if (m1 && bytes / m1 != m2)
648 PixarLogFixupTags(TIFF* tif)
655 PixarLogSetupDecode(TIFF* tif)
657 static const char module[] = "PixarLogSetupDecode";
658 TIFFDirectory *td = &tif->tif_dir;
659 PixarLogState* sp = DecoderState(tif);
664 /* Make sure no byte swapping happens on the data
665 * after decompression. */
666 tif->tif_postdecode = _TIFFNoPostDecode;
668 /* for some reason, we can't do this in TIFFInitPixarLog */
670 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
671 td->td_samplesperpixel : 1);
672 tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
673 td->td_rowsperstrip), sizeof(uint16));
675 return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
676 sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size+sizeof(uint16)*sp->stride);
677 if (sp->tbuf == NULL)
679 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
680 sp->user_datafmt = PixarLogGuessDataFmt(td);
681 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
682 TIFFErrorExt(tif->tif_clientdata, module,
683 "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
684 td->td_bitspersample);
688 if (inflateInit(&sp->stream) != Z_OK) {
689 TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
692 sp->state |= PLSTATE_INIT;
698 * Setup state for decoding a strip.
701 PixarLogPreDecode(TIFF* tif, uint16 s)
703 static const char module[] = "PixarLogPreDecode";
704 PixarLogState* sp = DecoderState(tif);
708 sp->stream.next_in = tif->tif_rawdata;
709 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
710 we need to simplify this code to reflect a ZLib that is likely updated
711 to deal with 8byte memory sizes, though this code will respond
712 apropriately even before we simplify it */
713 sp->stream.avail_in = (uInt) tif->tif_rawcc;
714 if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
716 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
719 return (inflateReset(&sp->stream) == Z_OK);
723 PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
725 static const char module[] = "PixarLogDecode";
726 TIFFDirectory *td = &tif->tif_dir;
727 PixarLogState* sp = DecoderState(tif);
733 switch (sp->user_datafmt) {
734 case PIXARLOGDATAFMT_FLOAT:
735 nsamples = occ / sizeof(float); /* XXX float == 32 bits */
737 case PIXARLOGDATAFMT_16BIT:
738 case PIXARLOGDATAFMT_12BITPICIO:
739 case PIXARLOGDATAFMT_11BITLOG:
740 nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
742 case PIXARLOGDATAFMT_8BIT:
743 case PIXARLOGDATAFMT_8BITABGR:
747 TIFFErrorExt(tif->tif_clientdata, module,
748 "%d bit input not supported in PixarLog",
749 td->td_bitspersample);
753 llen = sp->stride * td->td_imagewidth;
757 sp->stream.next_out = (unsigned char *) sp->tbuf;
758 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
759 we need to simplify this code to reflect a ZLib that is likely updated
760 to deal with 8byte memory sizes, though this code will respond
761 apropriately even before we simplify it */
762 sp->stream.avail_out = (uInt) (nsamples * sizeof(uint16));
763 if (sp->stream.avail_out != nsamples * sizeof(uint16))
765 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
769 int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
770 if (state == Z_STREAM_END) {
773 if (state == Z_DATA_ERROR) {
774 TIFFErrorExt(tif->tif_clientdata, module,
775 "Decoding error at scanline %lu, %s",
776 (unsigned long) tif->tif_row, sp->stream.msg);
777 if (inflateSync(&sp->stream) != Z_OK)
782 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
786 } while (sp->stream.avail_out > 0);
788 /* hopefully, we got all the bytes we needed */
789 if (sp->stream.avail_out != 0) {
790 TIFFErrorExt(tif->tif_clientdata, module,
791 "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
792 (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
797 /* Swap bytes in the data if from a different endian machine. */
798 if (tif->tif_flags & TIFF_SWAB)
799 TIFFSwabArrayOfShort(up, nsamples);
802 * if llen is not an exact multiple of nsamples, the decode operation
803 * may overflow the output buffer, so truncate it enough to prevent
804 * that but still salvage as much data as possible.
806 if (nsamples % llen) {
807 TIFFWarningExt(tif->tif_clientdata, module,
808 "stride %lu is not a multiple of sample count, "
809 "%lu, data truncated.", (unsigned long) llen, (unsigned long) nsamples);
810 nsamples -= nsamples % llen;
813 for (i = 0; i < nsamples; i += llen, up += llen) {
814 switch (sp->user_datafmt) {
815 case PIXARLOGDATAFMT_FLOAT:
816 horizontalAccumulateF(up, llen, sp->stride,
817 (float *)op, sp->ToLinearF);
818 op += llen * sizeof(float);
820 case PIXARLOGDATAFMT_16BIT:
821 horizontalAccumulate16(up, llen, sp->stride,
822 (uint16 *)op, sp->ToLinear16);
823 op += llen * sizeof(uint16);
825 case PIXARLOGDATAFMT_12BITPICIO:
826 horizontalAccumulate12(up, llen, sp->stride,
827 (int16 *)op, sp->ToLinearF);
828 op += llen * sizeof(int16);
830 case PIXARLOGDATAFMT_11BITLOG:
831 horizontalAccumulate11(up, llen, sp->stride,
833 op += llen * sizeof(uint16);
835 case PIXARLOGDATAFMT_8BIT:
836 horizontalAccumulate8(up, llen, sp->stride,
837 (unsigned char *)op, sp->ToLinear8);
838 op += llen * sizeof(unsigned char);
840 case PIXARLOGDATAFMT_8BITABGR:
841 horizontalAccumulate8abgr(up, llen, sp->stride,
842 (unsigned char *)op, sp->ToLinear8);
843 op += llen * sizeof(unsigned char);
846 TIFFErrorExt(tif->tif_clientdata, module,
847 "Unsupported bits/sample: %d",
848 td->td_bitspersample);
857 PixarLogSetupEncode(TIFF* tif)
859 static const char module[] = "PixarLogSetupEncode";
860 TIFFDirectory *td = &tif->tif_dir;
861 PixarLogState* sp = EncoderState(tif);
866 /* for some reason, we can't do this in TIFFInitPixarLog */
868 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
869 td->td_samplesperpixel : 1);
870 tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
871 td->td_rowsperstrip), sizeof(uint16));
873 return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
874 sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
875 if (sp->tbuf == NULL)
877 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
878 sp->user_datafmt = PixarLogGuessDataFmt(td);
879 if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
880 TIFFErrorExt(tif->tif_clientdata, module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
884 if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
885 TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
888 sp->state |= PLSTATE_INIT;
894 * Reset encoding state at the start of a strip.
897 PixarLogPreEncode(TIFF* tif, uint16 s)
899 static const char module[] = "PixarLogPreEncode";
900 PixarLogState *sp = EncoderState(tif);
904 sp->stream.next_out = tif->tif_rawdata;
905 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
906 we need to simplify this code to reflect a ZLib that is likely updated
907 to deal with 8byte memory sizes, though this code will respond
908 apropriately even before we simplify it */
909 sp->stream.avail_out = tif->tif_rawdatasize;
910 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
912 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
915 return (deflateReset(&sp->stream) == Z_OK);
919 horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
921 int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
922 float fltsize = Fltsize;
924 #define CLAMP(v) ( (v<(float)0.) ? 0 \
925 : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
926 : (v>(float)24.2) ? 2047 \
927 : LogK1*log(v*LogK2) + 0.5 )
932 r2 = wp[0] = (uint16) CLAMP(ip[0]);
933 g2 = wp[1] = (uint16) CLAMP(ip[1]);
934 b2 = wp[2] = (uint16) CLAMP(ip[2]);
940 r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
941 g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
942 b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
944 } else if (stride == 4) {
945 r2 = wp[0] = (uint16) CLAMP(ip[0]);
946 g2 = wp[1] = (uint16) CLAMP(ip[1]);
947 b2 = wp[2] = (uint16) CLAMP(ip[2]);
948 a2 = wp[3] = (uint16) CLAMP(ip[3]);
954 r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
955 g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
956 b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
957 a1 = (int32) CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
960 ip += n - 1; /* point to last one */
961 wp += n - 1; /* point to last one */
964 REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]);
970 REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp--; ip--)
976 horizontalDifference16(unsigned short *ip, int n, int stride,
977 unsigned short *wp, uint16 *From14)
979 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
981 /* assumption is unsigned pixel values */
983 #define CLAMP(v) From14[(v) >> 2]
988 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
989 b2 = wp[2] = CLAMP(ip[2]);
995 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
996 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
997 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
999 } else if (stride == 4) {
1000 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1001 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
1007 r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
1008 g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
1009 b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
1010 a1 = CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
1013 ip += n - 1; /* point to last one */
1014 wp += n - 1; /* point to last one */
1017 REPEAT(stride, wp[0] = CLAMP(ip[0]);
1018 wp[stride] -= wp[0];
1023 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
1030 horizontalDifference8(unsigned char *ip, int n, int stride,
1031 unsigned short *wp, uint16 *From8)
1033 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1036 #define CLAMP(v) (From8[(v)])
1041 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1042 b2 = wp[2] = CLAMP(ip[2]);
1046 r1 = CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
1047 g1 = CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
1048 b1 = CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
1052 } else if (stride == 4) {
1053 r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
1054 b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
1058 r1 = CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
1059 g1 = CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
1060 b1 = CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
1061 a1 = CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
1066 wp += n + stride - 1; /* point to last one */
1067 ip += n + stride - 1; /* point to last one */
1070 REPEAT(stride, wp[0] = CLAMP(ip[0]);
1071 wp[stride] -= wp[0];
1076 REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
1082 * Encode a chunk of pixels.
1085 PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1087 static const char module[] = "PixarLogEncode";
1088 TIFFDirectory *td = &tif->tif_dir;
1089 PixarLogState *sp = EncoderState(tif);
1093 unsigned short * up;
1097 switch (sp->user_datafmt) {
1098 case PIXARLOGDATAFMT_FLOAT:
1099 n = cc / sizeof(float); /* XXX float == 32 bits */
1101 case PIXARLOGDATAFMT_16BIT:
1102 case PIXARLOGDATAFMT_12BITPICIO:
1103 case PIXARLOGDATAFMT_11BITLOG:
1104 n = cc / sizeof(uint16); /* XXX uint16 == 16 bits */
1106 case PIXARLOGDATAFMT_8BIT:
1107 case PIXARLOGDATAFMT_8BITABGR:
1111 TIFFErrorExt(tif->tif_clientdata, module,
1112 "%d bit input not supported in PixarLog",
1113 td->td_bitspersample);
1117 llen = sp->stride * td->td_imagewidth;
1119 for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
1120 switch (sp->user_datafmt) {
1121 case PIXARLOGDATAFMT_FLOAT:
1122 horizontalDifferenceF((float *)bp, llen,
1123 sp->stride, up, sp->FromLT2);
1124 bp += llen * sizeof(float);
1126 case PIXARLOGDATAFMT_16BIT:
1127 horizontalDifference16((uint16 *)bp, llen,
1128 sp->stride, up, sp->From14);
1129 bp += llen * sizeof(uint16);
1131 case PIXARLOGDATAFMT_8BIT:
1132 horizontalDifference8((unsigned char *)bp, llen,
1133 sp->stride, up, sp->From8);
1134 bp += llen * sizeof(unsigned char);
1137 TIFFErrorExt(tif->tif_clientdata, module,
1138 "%d bit input not supported in PixarLog",
1139 td->td_bitspersample);
1144 sp->stream.next_in = (unsigned char *) sp->tbuf;
1145 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
1146 we need to simplify this code to reflect a ZLib that is likely updated
1147 to deal with 8byte memory sizes, though this code will respond
1148 apropriately even before we simplify it */
1149 sp->stream.avail_in = (uInt) (n * sizeof(uint16));
1150 if ((sp->stream.avail_in / sizeof(uint16)) != (uInt) n)
1152 TIFFErrorExt(tif->tif_clientdata, module,
1153 "ZLib cannot deal with buffers this size");
1158 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
1159 TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
1163 if (sp->stream.avail_out == 0) {
1164 tif->tif_rawcc = tif->tif_rawdatasize;
1165 TIFFFlushData1(tif);
1166 sp->stream.next_out = tif->tif_rawdata;
1167 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
1169 } while (sp->stream.avail_in > 0);
1174 * Finish off an encoded strip by flushing the last
1175 * string and tacking on an End Of Information code.
1179 PixarLogPostEncode(TIFF* tif)
1181 static const char module[] = "PixarLogPostEncode";
1182 PixarLogState *sp = EncoderState(tif);
1185 sp->stream.avail_in = 0;
1188 state = deflate(&sp->stream, Z_FINISH);
1192 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
1194 tif->tif_rawdatasize - sp->stream.avail_out;
1195 TIFFFlushData1(tif);
1196 sp->stream.next_out = tif->tif_rawdata;
1197 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
1201 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
1205 } while (state != Z_STREAM_END);
1210 PixarLogClose(TIFF* tif)
1212 TIFFDirectory *td = &tif->tif_dir;
1214 /* In a really sneaky (and really incorrect, and untruthfull, and
1215 * troublesome, and error-prone) maneuver that completely goes against
1216 * the spirit of TIFF, and breaks TIFF, on close, we covertly
1217 * modify both bitspersample and sampleformat in the directory to
1218 * indicate 8-bit linear. This way, the decode "just works" even for
1219 * readers that don't know about PixarLog, or how to set
1220 * the PIXARLOGDATFMT pseudo-tag.
1222 td->td_bitspersample = 8;
1223 td->td_sampleformat = SAMPLEFORMAT_UINT;
1227 PixarLogCleanup(TIFF* tif)
1229 PixarLogState* sp = (PixarLogState*) tif->tif_data;
1233 (void)TIFFPredictorCleanup(tif);
1235 tif->tif_tagmethods.vgetfield = sp->vgetparent;
1236 tif->tif_tagmethods.vsetfield = sp->vsetparent;
1238 if (sp->FromLT2) _TIFFfree(sp->FromLT2);
1239 if (sp->From14) _TIFFfree(sp->From14);
1240 if (sp->From8) _TIFFfree(sp->From8);
1241 if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
1242 if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
1243 if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
1244 if (sp->state&PLSTATE_INIT) {
1245 if (tif->tif_mode == O_RDONLY)
1246 inflateEnd(&sp->stream);
1248 deflateEnd(&sp->stream);
1251 _TIFFfree(sp->tbuf);
1253 tif->tif_data = NULL;
1255 _TIFFSetDefaultCompressionState(tif);
1259 PixarLogVSetField(TIFF* tif, uint32 tag, va_list ap)
1261 static const char module[] = "PixarLogVSetField";
1262 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1266 case TIFFTAG_PIXARLOGQUALITY:
1267 sp->quality = (int) va_arg(ap, int);
1268 if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
1269 if (deflateParams(&sp->stream,
1270 sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
1271 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
1277 case TIFFTAG_PIXARLOGDATAFMT:
1278 sp->user_datafmt = (int) va_arg(ap, int);
1279 /* Tweak the TIFF header so that the rest of libtiff knows what
1280 * size of data will be passed between app and library, and
1281 * assume that the app knows what it is doing and is not
1282 * confused by these header manipulations...
1284 switch (sp->user_datafmt) {
1285 case PIXARLOGDATAFMT_8BIT:
1286 case PIXARLOGDATAFMT_8BITABGR:
1287 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
1288 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1290 case PIXARLOGDATAFMT_11BITLOG:
1291 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1292 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1294 case PIXARLOGDATAFMT_12BITPICIO:
1295 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1296 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
1298 case PIXARLOGDATAFMT_16BIT:
1299 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1300 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1302 case PIXARLOGDATAFMT_FLOAT:
1303 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
1304 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
1308 * Must recalculate sizes should bits/sample change.
1310 tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
1311 tif->tif_scanlinesize = TIFFScanlineSize(tif);
1312 result = 1; /* NB: pseudo tag */
1315 result = (*sp->vsetparent)(tif, tag, ap);
1321 PixarLogVGetField(TIFF* tif, uint32 tag, va_list ap)
1323 PixarLogState *sp = (PixarLogState *)tif->tif_data;
1326 case TIFFTAG_PIXARLOGQUALITY:
1327 *va_arg(ap, int*) = sp->quality;
1329 case TIFFTAG_PIXARLOGDATAFMT:
1330 *va_arg(ap, int*) = sp->user_datafmt;
1333 return (*sp->vgetparent)(tif, tag, ap);
1338 static const TIFFField pixarlogFields[] = {
1339 {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
1340 {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}
1344 TIFFInitPixarLog(TIFF* tif, int scheme)
1346 static const char module[] = "TIFFInitPixarLog";
1350 assert(scheme == COMPRESSION_PIXARLOG);
1353 * Merge codec-specific tag information.
1355 if (!_TIFFMergeFields(tif, pixarlogFields,
1356 TIFFArrayCount(pixarlogFields))) {
1357 TIFFErrorExt(tif->tif_clientdata, module,
1358 "Merging PixarLog codec-specific tags failed");
1363 * Allocate state block so tag methods have storage to record values.
1365 tif->tif_data = (uint8*) _TIFFmalloc(sizeof (PixarLogState));
1366 if (tif->tif_data == NULL)
1368 sp = (PixarLogState*) tif->tif_data;
1369 _TIFFmemset(sp, 0, sizeof (*sp));
1370 sp->stream.data_type = Z_BINARY;
1371 sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
1374 * Install codec methods.
1376 tif->tif_fixuptags = PixarLogFixupTags;
1377 tif->tif_setupdecode = PixarLogSetupDecode;
1378 tif->tif_predecode = PixarLogPreDecode;
1379 tif->tif_decoderow = PixarLogDecode;
1380 tif->tif_decodestrip = PixarLogDecode;
1381 tif->tif_decodetile = PixarLogDecode;
1382 tif->tif_setupencode = PixarLogSetupEncode;
1383 tif->tif_preencode = PixarLogPreEncode;
1384 tif->tif_postencode = PixarLogPostEncode;
1385 tif->tif_encoderow = PixarLogEncode;
1386 tif->tif_encodestrip = PixarLogEncode;
1387 tif->tif_encodetile = PixarLogEncode;
1388 tif->tif_close = PixarLogClose;
1389 tif->tif_cleanup = PixarLogCleanup;
1391 /* Override SetField so we can handle our private pseudo-tag */
1392 sp->vgetparent = tif->tif_tagmethods.vgetfield;
1393 tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
1394 sp->vsetparent = tif->tif_tagmethods.vsetfield;
1395 tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
1397 /* Default values for codec-specific fields */
1398 sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
1401 /* we don't wish to use the predictor,
1402 * the default is none, which predictor value 1
1404 (void) TIFFPredictorInit(tif);
1407 * build the companding tables
1409 PixarLogMakeTables(sp);
1413 TIFFErrorExt(tif->tif_clientdata, module,
1414 "No space for PixarLog state block");
1417 #endif /* PIXARLOG_SUPPORT */
1419 /* vim: set ts=8 sts=8 sw=8 noet: */