roentgen b75cab
/* $Id: tif_pixarlog.c,v 1.38 2012-06-21 01:01:53 fwarmerdam Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1996-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1996 Pixar
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Pixar, Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Pixar, Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL PIXAR, SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
#ifdef PIXARLOG_SUPPORT
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 * PixarLog Compression Support
roentgen b75cab
 *
roentgen b75cab
 * Contributed by Dan McCoy.
roentgen b75cab
 *
roentgen b75cab
 * PixarLog film support uses the TIFF library to store companded
roentgen b75cab
 * 11 bit values into a tiff file, which are compressed using the 
roentgen b75cab
 * zip compressor.  
roentgen b75cab
 *
roentgen b75cab
 * The codec can take as input and produce as output 32-bit IEEE float values 
roentgen b75cab
 * as well as 16-bit or 8-bit unsigned integer values.
roentgen b75cab
 *
roentgen b75cab
 * On writing any of the above are converted into the internal
roentgen b75cab
 * 11-bit log format.   In the case of  8 and 16 bit values, the
roentgen b75cab
 * input is assumed to be unsigned linear color values that represent
roentgen b75cab
 * the range 0-1.  In the case of IEEE values, the 0-1 range is assumed to
roentgen b75cab
 * be the normal linear color range, in addition over 1 values are
roentgen b75cab
 * accepted up to a value of about 25.0 to encode "hot" hightlights and such.
roentgen b75cab
 * The encoding is lossless for 8-bit values, slightly lossy for the
roentgen b75cab
 * other bit depths.  The actual color precision should be better
roentgen b75cab
 * than the human eye can perceive with extra room to allow for
roentgen b75cab
 * error introduced by further image computation.  As with any quantized
roentgen b75cab
 * color format, it is possible to perform image calculations which
roentgen b75cab
 * expose the quantization error. This format should certainly be less 
roentgen b75cab
 * susceptable to such errors than standard 8-bit encodings, but more
roentgen b75cab
 * susceptable than straight 16-bit or 32-bit encodings.
roentgen b75cab
 *
roentgen b75cab
 * On reading the internal format is converted to the desired output format.
roentgen b75cab
 * The program can request which format it desires by setting the internal
roentgen b75cab
 * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
roentgen b75cab
 *  PIXARLOGDATAFMT_FLOAT     = provide IEEE float values.
roentgen b75cab
 *  PIXARLOGDATAFMT_16BIT     = provide unsigned 16-bit integer values
roentgen b75cab
 *  PIXARLOGDATAFMT_8BIT      = provide unsigned 8-bit integer values
roentgen b75cab
 *
roentgen b75cab
 * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
roentgen b75cab
 * values with the difference that if there are exactly three or four channels
roentgen b75cab
 * (rgb or rgba) it swaps the channel order (bgr or abgr).
roentgen b75cab
 *
roentgen b75cab
 * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
roentgen b75cab
 * packed in 16-bit values.   However no tools are supplied for interpreting
roentgen b75cab
 * these values.
roentgen b75cab
 *
roentgen b75cab
 * "hot" (over 1.0) areas written in floating point get clamped to
roentgen b75cab
 * 1.0 in the integer data types.
roentgen b75cab
 *
roentgen b75cab
 * When the file is closed after writing, the bit depth and sample format
roentgen b75cab
 * are set always to appear as if 8-bit data has been written into it.
roentgen b75cab
 * That way a naive program unaware of the particulars of the encoding
roentgen b75cab
 * gets the format it is most likely able to handle.
roentgen b75cab
 *
roentgen b75cab
 * The codec does it's own horizontal differencing step on the coded
roentgen b75cab
 * values so the libraries predictor stuff should be turned off.
roentgen b75cab
 * The codec also handle byte swapping the encoded values as necessary
roentgen b75cab
 * since the library does not have the information necessary
roentgen b75cab
 * to know the bit depth of the raw unencoded buffer.
roentgen b75cab
 *
roentgen b75cab
 * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc.
roentgen b75cab
 * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT
roentgen b75cab
 * as noted in http://trac.osgeo.org/gdal/ticket/3894.   FrankW - Jan'11
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include "tif_predict.h"
roentgen b75cab
#include "zlib.h"
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
#include <math.h></math.h>
roentgen b75cab
roentgen b75cab
/* Tables for converting to/from 11 bit coded values */
roentgen b75cab
roentgen b75cab
#define  TSIZE	 2048		/* decode table size (11-bit tokens) */
roentgen b75cab
#define  TSIZEP1 2049		/* Plus one for slop */
roentgen b75cab
#define  ONE	 1250		/* token value of 1.0 exactly */
roentgen b75cab
#define  RATIO	 1.004		/* nominal ratio for log part */
roentgen b75cab
roentgen b75cab
#define CODE_MASK 0x7ff         /* 11 bits. */
roentgen b75cab
roentgen b75cab
static float  Fltsize;
roentgen b75cab
static float  LogK1, LogK2;
roentgen b75cab
roentgen b75cab
#define REPEAT(n, op)   { int i; i=n; do { i--; op; } while (i>0); }
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
roentgen b75cab
	float *ToLinearF)
roentgen b75cab
{
roentgen b75cab
    register unsigned int  cr, cg, cb, ca, mask;
roentgen b75cab
    register float  t0, t1, t2, t3;
roentgen b75cab
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	mask = CODE_MASK;
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    t0 = ToLinearF[cr = (wp[0] & mask)];
roentgen b75cab
	    t1 = ToLinearF[cg = (wp[1] & mask)];
roentgen b75cab
	    t2 = ToLinearF[cb = (wp[2] & mask)];
roentgen b75cab
	    op[0] = t0;
roentgen b75cab
	    op[1] = t1;
roentgen b75cab
	    op[2] = t2;
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 3;
roentgen b75cab
		op += 3;
roentgen b75cab
		n -= 3;
roentgen b75cab
		t0 = ToLinearF[(cr += wp[0]) & mask];
roentgen b75cab
		t1 = ToLinearF[(cg += wp[1]) & mask];
roentgen b75cab
		t2 = ToLinearF[(cb += wp[2]) & mask];
roentgen b75cab
		op[0] = t0;
roentgen b75cab
		op[1] = t1;
roentgen b75cab
		op[2] = t2;
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    t0 = ToLinearF[cr = (wp[0] & mask)];
roentgen b75cab
	    t1 = ToLinearF[cg = (wp[1] & mask)];
roentgen b75cab
	    t2 = ToLinearF[cb = (wp[2] & mask)];
roentgen b75cab
	    t3 = ToLinearF[ca = (wp[3] & mask)];
roentgen b75cab
	    op[0] = t0;
roentgen b75cab
	    op[1] = t1;
roentgen b75cab
	    op[2] = t2;
roentgen b75cab
	    op[3] = t3;
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 4;
roentgen b75cab
		op += 4;
roentgen b75cab
		n -= 4;
roentgen b75cab
		t0 = ToLinearF[(cr += wp[0]) & mask];
roentgen b75cab
		t1 = ToLinearF[(cg += wp[1]) & mask];
roentgen b75cab
		t2 = ToLinearF[(cb += wp[2]) & mask];
roentgen b75cab
		t3 = ToLinearF[(ca += wp[3]) & mask];
roentgen b75cab
		op[0] = t0;
roentgen b75cab
		op[1] = t1;
roentgen b75cab
		op[2] = t2;
roentgen b75cab
		op[3] = t3;
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    REPEAT(stride, *op = ToLinearF[*wp&mask]; wp++; op++)
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride,
roentgen b75cab
		    wp[stride] += *wp; *op = ToLinearF[*wp&mask]; wp++; op++)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalAccumulate12(uint16 *wp, int n, int stride, int16 *op,
roentgen b75cab
	float *ToLinearF)
roentgen b75cab
{
roentgen b75cab
    register unsigned int  cr, cg, cb, ca, mask;
roentgen b75cab
    register float  t0, t1, t2, t3;
roentgen b75cab
roentgen b75cab
#define SCALE12 2048.0F
roentgen b75cab
#define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071)
roentgen b75cab
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	mask = CODE_MASK;
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
roentgen b75cab
	    t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
roentgen b75cab
	    t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
roentgen b75cab
	    op[0] = CLAMP12(t0);
roentgen b75cab
	    op[1] = CLAMP12(t1);
roentgen b75cab
	    op[2] = CLAMP12(t2);
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 3;
roentgen b75cab
		op += 3;
roentgen b75cab
		n -= 3;
roentgen b75cab
		t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
roentgen b75cab
		t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
roentgen b75cab
		t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
roentgen b75cab
		op[0] = CLAMP12(t0);
roentgen b75cab
		op[1] = CLAMP12(t1);
roentgen b75cab
		op[2] = CLAMP12(t2);
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
roentgen b75cab
	    t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
roentgen b75cab
	    t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
roentgen b75cab
	    t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
roentgen b75cab
	    op[0] = CLAMP12(t0);
roentgen b75cab
	    op[1] = CLAMP12(t1);
roentgen b75cab
	    op[2] = CLAMP12(t2);
roentgen b75cab
	    op[3] = CLAMP12(t3);
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 4;
roentgen b75cab
		op += 4;
roentgen b75cab
		n -= 4;
roentgen b75cab
		t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
roentgen b75cab
		t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
roentgen b75cab
		t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
roentgen b75cab
		t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
roentgen b75cab
		op[0] = CLAMP12(t0);
roentgen b75cab
		op[1] = CLAMP12(t1);
roentgen b75cab
		op[2] = CLAMP12(t2);
roentgen b75cab
		op[3] = CLAMP12(t3);
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    REPEAT(stride, t0 = ToLinearF[*wp&mask] * SCALE12;
roentgen b75cab
                           *op = CLAMP12(t0); wp++; op++)
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride,
roentgen b75cab
		    wp[stride] += *wp; t0 = ToLinearF[wp[stride]&mask]*SCALE12;
roentgen b75cab
		    *op = CLAMP12(t0);  wp++; op++)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalAccumulate16(uint16 *wp, int n, int stride, uint16 *op,
roentgen b75cab
	uint16 *ToLinear16)
roentgen b75cab
{
roentgen b75cab
    register unsigned int  cr, cg, cb, ca, mask;
roentgen b75cab
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	mask = CODE_MASK;
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    op[0] = ToLinear16[cr = (wp[0] & mask)];
roentgen b75cab
	    op[1] = ToLinear16[cg = (wp[1] & mask)];
roentgen b75cab
	    op[2] = ToLinear16[cb = (wp[2] & mask)];
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 3;
roentgen b75cab
		op += 3;
roentgen b75cab
		n -= 3;
roentgen b75cab
		op[0] = ToLinear16[(cr += wp[0]) & mask];
roentgen b75cab
		op[1] = ToLinear16[(cg += wp[1]) & mask];
roentgen b75cab
		op[2] = ToLinear16[(cb += wp[2]) & mask];
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    op[0] = ToLinear16[cr = (wp[0] & mask)];
roentgen b75cab
	    op[1] = ToLinear16[cg = (wp[1] & mask)];
roentgen b75cab
	    op[2] = ToLinear16[cb = (wp[2] & mask)];
roentgen b75cab
	    op[3] = ToLinear16[ca = (wp[3] & mask)];
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 4;
roentgen b75cab
		op += 4;
roentgen b75cab
		n -= 4;
roentgen b75cab
		op[0] = ToLinear16[(cr += wp[0]) & mask];
roentgen b75cab
		op[1] = ToLinear16[(cg += wp[1]) & mask];
roentgen b75cab
		op[2] = ToLinear16[(cb += wp[2]) & mask];
roentgen b75cab
		op[3] = ToLinear16[(ca += wp[3]) & mask];
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    REPEAT(stride, *op = ToLinear16[*wp&mask]; wp++; op++)
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride,
roentgen b75cab
		    wp[stride] += *wp; *op = ToLinear16[*wp&mask]; wp++; op++)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* 
roentgen b75cab
 * Returns the log encoded 11-bit values with the horizontal
roentgen b75cab
 * differencing undone.
roentgen b75cab
 */
roentgen b75cab
static void
roentgen b75cab
horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
roentgen b75cab
{
roentgen b75cab
    register unsigned int  cr, cg, cb, ca, mask;
roentgen b75cab
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	mask = CODE_MASK;
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    op[0] = cr = wp[0];  op[1] = cg = wp[1];  op[2] = cb = wp[2];
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 3;
roentgen b75cab
		op += 3;
roentgen b75cab
		n -= 3;
roentgen b75cab
		op[0] = (cr += wp[0]) & mask;
roentgen b75cab
		op[1] = (cg += wp[1]) & mask;
roentgen b75cab
		op[2] = (cb += wp[2]) & mask;
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    op[0] = cr = wp[0];  op[1] = cg = wp[1];
roentgen b75cab
	    op[2] = cb = wp[2];  op[3] = ca = wp[3];
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		wp += 4;
roentgen b75cab
		op += 4;
roentgen b75cab
		n -= 4;
roentgen b75cab
		op[0] = (cr += wp[0]) & mask;
roentgen b75cab
		op[1] = (cg += wp[1]) & mask;
roentgen b75cab
		op[2] = (cb += wp[2]) & mask;
roentgen b75cab
		op[3] = (ca += wp[3]) & mask;
roentgen b75cab
	    } 
roentgen b75cab
	} else {
roentgen b75cab
	    REPEAT(stride, *op = *wp&mask; wp++; op++)
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride,
roentgen b75cab
		    wp[stride] += *wp; *op = *wp&mask; wp++; op++)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalAccumulate8(uint16 *wp, int n, int stride, unsigned char *op,
roentgen b75cab
	unsigned char *ToLinear8)
roentgen b75cab
{
roentgen b75cab
    register unsigned int  cr, cg, cb, ca, mask;
roentgen b75cab
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	mask = CODE_MASK;
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    op[0] = ToLinear8[cr = (wp[0] & mask)];
roentgen b75cab
	    op[1] = ToLinear8[cg = (wp[1] & mask)];
roentgen b75cab
	    op[2] = ToLinear8[cb = (wp[2] & mask)];
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 3;
roentgen b75cab
		wp += 3;
roentgen b75cab
		op += 3;
roentgen b75cab
		op[0] = ToLinear8[(cr += wp[0]) & mask];
roentgen b75cab
		op[1] = ToLinear8[(cg += wp[1]) & mask];
roentgen b75cab
		op[2] = ToLinear8[(cb += wp[2]) & mask];
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    op[0] = ToLinear8[cr = (wp[0] & mask)];
roentgen b75cab
	    op[1] = ToLinear8[cg = (wp[1] & mask)];
roentgen b75cab
	    op[2] = ToLinear8[cb = (wp[2] & mask)];
roentgen b75cab
	    op[3] = ToLinear8[ca = (wp[3] & mask)];
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 4;
roentgen b75cab
		wp += 4;
roentgen b75cab
		op += 4;
roentgen b75cab
		op[0] = ToLinear8[(cr += wp[0]) & mask];
roentgen b75cab
		op[1] = ToLinear8[(cg += wp[1]) & mask];
roentgen b75cab
		op[2] = ToLinear8[(cb += wp[2]) & mask];
roentgen b75cab
		op[3] = ToLinear8[(ca += wp[3]) & mask];
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride,
roentgen b75cab
		    wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
roentgen b75cab
	unsigned char *ToLinear8)
roentgen b75cab
{
roentgen b75cab
    register unsigned int  cr, cg, cb, ca, mask;
roentgen b75cab
    register unsigned char  t0, t1, t2, t3;
roentgen b75cab
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	mask = CODE_MASK;
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    op[0] = 0;
roentgen b75cab
	    t1 = ToLinear8[cb = (wp[2] & mask)];
roentgen b75cab
	    t2 = ToLinear8[cg = (wp[1] & mask)];
roentgen b75cab
	    t3 = ToLinear8[cr = (wp[0] & mask)];
roentgen b75cab
	    op[1] = t1;
roentgen b75cab
	    op[2] = t2;
roentgen b75cab
	    op[3] = t3;
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 3;
roentgen b75cab
		wp += 3;
roentgen b75cab
		op += 4;
roentgen b75cab
		op[0] = 0;
roentgen b75cab
		t1 = ToLinear8[(cb += wp[2]) & mask];
roentgen b75cab
		t2 = ToLinear8[(cg += wp[1]) & mask];
roentgen b75cab
		t3 = ToLinear8[(cr += wp[0]) & mask];
roentgen b75cab
		op[1] = t1;
roentgen b75cab
		op[2] = t2;
roentgen b75cab
		op[3] = t3;
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    t0 = ToLinear8[ca = (wp[3] & mask)];
roentgen b75cab
	    t1 = ToLinear8[cb = (wp[2] & mask)];
roentgen b75cab
	    t2 = ToLinear8[cg = (wp[1] & mask)];
roentgen b75cab
	    t3 = ToLinear8[cr = (wp[0] & mask)];
roentgen b75cab
	    op[0] = t0;
roentgen b75cab
	    op[1] = t1;
roentgen b75cab
	    op[2] = t2;
roentgen b75cab
	    op[3] = t3;
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 4;
roentgen b75cab
		wp += 4;
roentgen b75cab
		op += 4;
roentgen b75cab
		t0 = ToLinear8[(ca += wp[3]) & mask];
roentgen b75cab
		t1 = ToLinear8[(cb += wp[2]) & mask];
roentgen b75cab
		t2 = ToLinear8[(cg += wp[1]) & mask];
roentgen b75cab
		t3 = ToLinear8[(cr += wp[0]) & mask];
roentgen b75cab
		op[0] = t0;
roentgen b75cab
		op[1] = t1;
roentgen b75cab
		op[2] = t2;
roentgen b75cab
		op[3] = t3;
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride,
roentgen b75cab
		    wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * State block for each open TIFF
roentgen b75cab
 * file using PixarLog compression/decompression.
roentgen b75cab
 */
roentgen b75cab
typedef	struct {
roentgen b75cab
	TIFFPredictorState	predict;
roentgen b75cab
	z_stream		stream;
roentgen b75cab
	uint16			*tbuf; 
roentgen b75cab
	uint16			stride;
roentgen b75cab
	int			state;
roentgen b75cab
	int			user_datafmt;
roentgen b75cab
	int			quality;
roentgen b75cab
#define PLSTATE_INIT 1
roentgen b75cab
roentgen b75cab
	TIFFVSetMethod		vgetparent;	/* super-class method */
roentgen b75cab
	TIFFVSetMethod		vsetparent;	/* super-class method */
roentgen b75cab
roentgen b75cab
	float *ToLinearF;
roentgen b75cab
	uint16 *ToLinear16;
roentgen b75cab
	unsigned char *ToLinear8;
roentgen b75cab
	uint16  *FromLT2;
roentgen b75cab
	uint16  *From14; /* Really for 16-bit data, but we shift down 2 */
roentgen b75cab
	uint16  *From8;
roentgen b75cab
	
roentgen b75cab
} PixarLogState;
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogMakeTables(PixarLogState *sp)
roentgen b75cab
{
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 *    We make several tables here to convert between various external
roentgen b75cab
 *    representations (float, 16-bit, and 8-bit) and the internal
roentgen b75cab
 *    11-bit companded representation.  The 11-bit representation has two
roentgen b75cab
 *    distinct regions.  A linear bottom end up through .018316 in steps
roentgen b75cab
 *    of about .000073, and a region of constant ratio up to about 25.
roentgen b75cab
 *    These floating point numbers are stored in the main table ToLinearF. 
roentgen b75cab
 *    All other tables are derived from this one.  The tables (and the
roentgen b75cab
 *    ratios) are continuous at the internal seam.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
    int  nlin, lt2size;
roentgen b75cab
    int  i, j;
roentgen b75cab
    double  b, c, linstep, v;
roentgen b75cab
    float *ToLinearF;
roentgen b75cab
    uint16 *ToLinear16;
roentgen b75cab
    unsigned char *ToLinear8;
roentgen b75cab
    uint16  *FromLT2;
roentgen b75cab
    uint16  *From14; /* Really for 16-bit data, but we shift down 2 */
roentgen b75cab
    uint16  *From8;
roentgen b75cab
roentgen b75cab
    c = log(RATIO);	
roentgen b75cab
    nlin = (int)(1./c);	/* nlin must be an integer */
roentgen b75cab
    c = 1./nlin;
roentgen b75cab
    b = exp(-c*ONE);	/* multiplicative scale factor [b*exp(c*ONE) = 1] */
roentgen b75cab
    linstep = b*c*exp(1.);
roentgen b75cab
roentgen b75cab
    LogK1 = (float)(1./c);	/* if (v >= 2)  token = k1*log(v*k2) */
roentgen b75cab
    LogK2 = (float)(1./b);
roentgen b75cab
    lt2size = (int)(2./linstep) + 1;
roentgen b75cab
    FromLT2 = (uint16 *)_TIFFmalloc(lt2size*sizeof(uint16));
roentgen b75cab
    From14 = (uint16 *)_TIFFmalloc(16384*sizeof(uint16));
roentgen b75cab
    From8 = (uint16 *)_TIFFmalloc(256*sizeof(uint16));
roentgen b75cab
    ToLinearF = (float *)_TIFFmalloc(TSIZEP1 * sizeof(float));
roentgen b75cab
    ToLinear16 = (uint16 *)_TIFFmalloc(TSIZEP1 * sizeof(uint16));
roentgen b75cab
    ToLinear8 = (unsigned char *)_TIFFmalloc(TSIZEP1 * sizeof(unsigned char));
roentgen b75cab
    if (FromLT2 == NULL || From14  == NULL || From8   == NULL ||
roentgen b75cab
	 ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) {
roentgen b75cab
	if (FromLT2) _TIFFfree(FromLT2);
roentgen b75cab
	if (From14) _TIFFfree(From14);
roentgen b75cab
	if (From8) _TIFFfree(From8);
roentgen b75cab
	if (ToLinearF) _TIFFfree(ToLinearF);
roentgen b75cab
	if (ToLinear16) _TIFFfree(ToLinear16);
roentgen b75cab
	if (ToLinear8) _TIFFfree(ToLinear8);
roentgen b75cab
	sp->FromLT2 = NULL;
roentgen b75cab
	sp->From14 = NULL;
roentgen b75cab
	sp->From8 = NULL;
roentgen b75cab
	sp->ToLinearF = NULL;
roentgen b75cab
	sp->ToLinear16 = NULL;
roentgen b75cab
	sp->ToLinear8 = NULL;
roentgen b75cab
	return 0;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    j = 0;
roentgen b75cab
roentgen b75cab
    for (i = 0; i < nlin; i++)  {
roentgen b75cab
	v = i * linstep;
roentgen b75cab
	ToLinearF[j++] = (float)v;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    for (i = nlin; i < TSIZE; i++)
roentgen b75cab
	ToLinearF[j++] = (float)(b*exp(c*i));
roentgen b75cab
roentgen b75cab
    ToLinearF[2048] = ToLinearF[2047];
roentgen b75cab
roentgen b75cab
    for (i = 0; i < TSIZEP1; i++)  {
roentgen b75cab
	v = ToLinearF[i]*65535.0 + 0.5;
roentgen b75cab
	ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16)v;
roentgen b75cab
	v = ToLinearF[i]*255.0  + 0.5;
roentgen b75cab
	ToLinear8[i]  = (v > 255.0) ? 255 : (unsigned char)v;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    j = 0;
roentgen b75cab
    for (i = 0; i < lt2size; i++)  {
roentgen b75cab
	if ((i*linstep)*(i*linstep) > ToLinearF[j]*ToLinearF[j+1])
roentgen b75cab
	    j++;
roentgen b75cab
	FromLT2[i] = j;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
     * Since we lose info anyway on 16-bit data, we set up a 14-bit
roentgen b75cab
     * table and shift 16-bit values down two bits on input.
roentgen b75cab
     * saves a little table space.
roentgen b75cab
     */
roentgen b75cab
    j = 0;
roentgen b75cab
    for (i = 0; i < 16384; i++)  {
roentgen b75cab
	while ((i/16383.)*(i/16383.) > ToLinearF[j]*ToLinearF[j+1])
roentgen b75cab
	    j++;
roentgen b75cab
	From14[i] = j;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    j = 0;
roentgen b75cab
    for (i = 0; i < 256; i++)  {
roentgen b75cab
	while ((i/255.)*(i/255.) > ToLinearF[j]*ToLinearF[j+1])
roentgen b75cab
	    j++;
roentgen b75cab
	From8[i] = j;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    Fltsize = (float)(lt2size/2);
roentgen b75cab
roentgen b75cab
    sp->ToLinearF = ToLinearF;
roentgen b75cab
    sp->ToLinear16 = ToLinear16;
roentgen b75cab
    sp->ToLinear8 = ToLinear8;
roentgen b75cab
    sp->FromLT2 = FromLT2;
roentgen b75cab
    sp->From14 = From14;
roentgen b75cab
    sp->From8 = From8;
roentgen b75cab
roentgen b75cab
    return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define DecoderState(tif)	((PixarLogState*) (tif)->tif_data)
roentgen b75cab
#define EncoderState(tif)	((PixarLogState*) (tif)->tif_data)
roentgen b75cab
roentgen b75cab
static int PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
roentgen b75cab
static int PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
roentgen b75cab
roentgen b75cab
#define PIXARLOGDATAFMT_UNKNOWN	-1
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogGuessDataFmt(TIFFDirectory *td)
roentgen b75cab
{
roentgen b75cab
	int guess = PIXARLOGDATAFMT_UNKNOWN;
roentgen b75cab
	int format = td->td_sampleformat;
roentgen b75cab
roentgen b75cab
	/* If the user didn't tell us his datafmt,
roentgen b75cab
	 * take our best guess from the bitspersample.
roentgen b75cab
	 */
roentgen b75cab
	switch (td->td_bitspersample) {
roentgen b75cab
	 case 32:
roentgen b75cab
		if (format == SAMPLEFORMAT_IEEEFP)
roentgen b75cab
			guess = PIXARLOGDATAFMT_FLOAT;
roentgen b75cab
		break;
roentgen b75cab
	 case 16:
roentgen b75cab
		if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
roentgen b75cab
			guess = PIXARLOGDATAFMT_16BIT;
roentgen b75cab
		break;
roentgen b75cab
	 case 12:
roentgen b75cab
		if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
roentgen b75cab
			guess = PIXARLOGDATAFMT_12BITPICIO;
roentgen b75cab
		break;
roentgen b75cab
	 case 11:
roentgen b75cab
		if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
roentgen b75cab
			guess = PIXARLOGDATAFMT_11BITLOG;
roentgen b75cab
		break;
roentgen b75cab
	 case 8:
roentgen b75cab
		if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
roentgen b75cab
			guess = PIXARLOGDATAFMT_8BIT;
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return guess;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static tmsize_t
roentgen b75cab
multiply_ms(tmsize_t m1, tmsize_t m2)
roentgen b75cab
{
roentgen b75cab
	tmsize_t bytes = m1 * m2;
roentgen b75cab
roentgen b75cab
	if (m1 && bytes / m1 != m2)
roentgen b75cab
		bytes = 0;
roentgen b75cab
roentgen b75cab
	return bytes;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogFixupTags(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	(void) tif;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogSetupDecode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PixarLogSetupDecode";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	PixarLogState* sp = DecoderState(tif);
roentgen b75cab
	tmsize_t tbuf_size;
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
roentgen b75cab
	/* Make sure no byte swapping happens on the data
roentgen b75cab
	 * after decompression. */
roentgen b75cab
	tif->tif_postdecode = _TIFFNoPostDecode;  
roentgen b75cab
roentgen b75cab
	/* for some reason, we can't do this in TIFFInitPixarLog */
roentgen b75cab
roentgen b75cab
	sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
roentgen b75cab
	    td->td_samplesperpixel : 1);
roentgen b75cab
	tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
roentgen b75cab
				      td->td_rowsperstrip), sizeof(uint16));
roentgen b75cab
	if (tbuf_size == 0)
roentgen b75cab
		return (0);   /* TODO: this is an error return without error report through TIFFErrorExt */
roentgen b75cab
	sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size+sizeof(uint16)*sp->stride);
roentgen b75cab
	if (sp->tbuf == NULL)
roentgen b75cab
		return (0);
roentgen b75cab
	if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
roentgen b75cab
		sp->user_datafmt = PixarLogGuessDataFmt(td);
roentgen b75cab
	if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			"PixarLog compression can't handle bits depth/data format combination (depth: %d)", 
roentgen b75cab
			td->td_bitspersample);
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (inflateInit(&sp->stream) != Z_OK) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
roentgen b75cab
		return (0);
roentgen b75cab
	} else {
roentgen b75cab
		sp->state |= PLSTATE_INIT;
roentgen b75cab
		return (1);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Setup state for decoding a strip.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
PixarLogPreDecode(TIFF* tif, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PixarLogPreDecode";
roentgen b75cab
	PixarLogState* sp = DecoderState(tif);
roentgen b75cab
roentgen b75cab
	(void) s;
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	sp->stream.next_in = tif->tif_rawdata;
roentgen b75cab
	assert(sizeof(sp->stream.avail_in)==4);  /* if this assert gets raised,
roentgen b75cab
	    we need to simplify this code to reflect a ZLib that is likely updated
roentgen b75cab
	    to deal with 8byte memory sizes, though this code will respond
roentgen b75cab
	    apropriately even before we simplify it */
roentgen b75cab
	sp->stream.avail_in = (uInt) tif->tif_rawcc;
roentgen b75cab
	if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	return (inflateReset(&sp->stream) == Z_OK);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PixarLogDecode";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	PixarLogState* sp = DecoderState(tif);
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t nsamples;
roentgen b75cab
	int llen;
roentgen b75cab
	uint16 *up;
roentgen b75cab
roentgen b75cab
	switch (sp->user_datafmt) {
roentgen b75cab
	case PIXARLOGDATAFMT_FLOAT:
roentgen b75cab
		nsamples = occ / sizeof(float);	/* XXX float == 32 bits */
roentgen b75cab
		break;
roentgen b75cab
	case PIXARLOGDATAFMT_16BIT:
roentgen b75cab
	case PIXARLOGDATAFMT_12BITPICIO:
roentgen b75cab
	case PIXARLOGDATAFMT_11BITLOG:
roentgen b75cab
		nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
roentgen b75cab
		break;
roentgen b75cab
	case PIXARLOGDATAFMT_8BIT:
roentgen b75cab
	case PIXARLOGDATAFMT_8BITABGR:
roentgen b75cab
		nsamples = occ;
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			"%d bit input not supported in PixarLog",
roentgen b75cab
			td->td_bitspersample);
roentgen b75cab
		return 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	llen = sp->stride * td->td_imagewidth;
roentgen b75cab
roentgen b75cab
	(void) s;
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	sp->stream.next_out = (unsigned char *) sp->tbuf;
roentgen b75cab
	assert(sizeof(sp->stream.avail_out)==4);  /* if this assert gets raised,
roentgen b75cab
	    we need to simplify this code to reflect a ZLib that is likely updated
roentgen b75cab
	    to deal with 8byte memory sizes, though this code will respond
roentgen b75cab
	    apropriately even before we simplify it */
roentgen b75cab
	sp->stream.avail_out = (uInt) (nsamples * sizeof(uint16));
roentgen b75cab
	if (sp->stream.avail_out != nsamples * sizeof(uint16))
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	do {
roentgen b75cab
		int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
roentgen b75cab
		if (state == Z_STREAM_END) {
roentgen b75cab
			break;			/* XXX */
roentgen b75cab
		}
roentgen b75cab
		if (state == Z_DATA_ERROR) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Decoding error at scanline %lu, %s",
roentgen b75cab
			    (unsigned long) tif->tif_row, sp->stream.msg);
roentgen b75cab
			if (inflateSync(&sp->stream) != Z_OK)
roentgen b75cab
				return (0);
roentgen b75cab
			continue;
roentgen b75cab
		}
roentgen b75cab
		if (state != Z_OK) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
roentgen b75cab
			    sp->stream.msg);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
	} while (sp->stream.avail_out > 0);
roentgen b75cab
roentgen b75cab
	/* hopefully, we got all the bytes we needed */
roentgen b75cab
	if (sp->stream.avail_out != 0) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
roentgen b75cab
		    (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	up = sp->tbuf;
roentgen b75cab
	/* Swap bytes in the data if from a different endian machine. */
roentgen b75cab
	if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfShort(up, nsamples);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * if llen is not an exact multiple of nsamples, the decode operation
roentgen b75cab
	 * may overflow the output buffer, so truncate it enough to prevent
roentgen b75cab
	 * that but still salvage as much data as possible.
roentgen b75cab
	 */
roentgen b75cab
	if (nsamples % llen) { 
roentgen b75cab
		TIFFWarningExt(tif->tif_clientdata, module,
roentgen b75cab
			"stride %lu is not a multiple of sample count, "
roentgen b75cab
			"%lu, data truncated.", (unsigned long) llen, (unsigned long) nsamples);
roentgen b75cab
		nsamples -= nsamples % llen;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	for (i = 0; i < nsamples; i += llen, up += llen) {
roentgen b75cab
		switch (sp->user_datafmt)  {
roentgen b75cab
		case PIXARLOGDATAFMT_FLOAT:
roentgen b75cab
			horizontalAccumulateF(up, llen, sp->stride,
roentgen b75cab
					(float *)op, sp->ToLinearF);
roentgen b75cab
			op += llen * sizeof(float);
roentgen b75cab
			break;
roentgen b75cab
		case PIXARLOGDATAFMT_16BIT:
roentgen b75cab
			horizontalAccumulate16(up, llen, sp->stride,
roentgen b75cab
					(uint16 *)op, sp->ToLinear16);
roentgen b75cab
			op += llen * sizeof(uint16);
roentgen b75cab
			break;
roentgen b75cab
		case PIXARLOGDATAFMT_12BITPICIO:
roentgen b75cab
			horizontalAccumulate12(up, llen, sp->stride,
roentgen b75cab
					(int16 *)op, sp->ToLinearF);
roentgen b75cab
			op += llen * sizeof(int16);
roentgen b75cab
			break;
roentgen b75cab
		case PIXARLOGDATAFMT_11BITLOG:
roentgen b75cab
			horizontalAccumulate11(up, llen, sp->stride,
roentgen b75cab
					(uint16 *)op);
roentgen b75cab
			op += llen * sizeof(uint16);
roentgen b75cab
			break;
roentgen b75cab
		case PIXARLOGDATAFMT_8BIT:
roentgen b75cab
			horizontalAccumulate8(up, llen, sp->stride,
roentgen b75cab
					(unsigned char *)op, sp->ToLinear8);
roentgen b75cab
			op += llen * sizeof(unsigned char);
roentgen b75cab
			break;
roentgen b75cab
		case PIXARLOGDATAFMT_8BITABGR:
roentgen b75cab
			horizontalAccumulate8abgr(up, llen, sp->stride,
roentgen b75cab
					(unsigned char *)op, sp->ToLinear8);
roentgen b75cab
			op += llen * sizeof(unsigned char);
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				  "Unsupported bits/sample: %d",
roentgen b75cab
				  td->td_bitspersample);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogSetupEncode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PixarLogSetupEncode";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	PixarLogState* sp = EncoderState(tif);
roentgen b75cab
	tmsize_t tbuf_size;
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
roentgen b75cab
	/* for some reason, we can't do this in TIFFInitPixarLog */
roentgen b75cab
roentgen b75cab
	sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
roentgen b75cab
	    td->td_samplesperpixel : 1);
roentgen b75cab
	tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
roentgen b75cab
				      td->td_rowsperstrip), sizeof(uint16));
roentgen b75cab
	if (tbuf_size == 0)
roentgen b75cab
		return (0);  /* TODO: this is an error return without error report through TIFFErrorExt */
roentgen b75cab
	sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
roentgen b75cab
	if (sp->tbuf == NULL)
roentgen b75cab
		return (0);
roentgen b75cab
	if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
roentgen b75cab
		sp->user_datafmt = PixarLogGuessDataFmt(td);
roentgen b75cab
	if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
roentgen b75cab
		return (0);
roentgen b75cab
	} else {
roentgen b75cab
		sp->state |= PLSTATE_INIT;
roentgen b75cab
		return (1);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Reset encoding state at the start of a strip.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
PixarLogPreEncode(TIFF* tif, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PixarLogPreEncode";
roentgen b75cab
	PixarLogState *sp = EncoderState(tif);
roentgen b75cab
roentgen b75cab
	(void) s;
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	sp->stream.next_out = tif->tif_rawdata;
roentgen b75cab
	assert(sizeof(sp->stream.avail_out)==4);  /* if this assert gets raised,
roentgen b75cab
	    we need to simplify this code to reflect a ZLib that is likely updated
roentgen b75cab
	    to deal with 8byte memory sizes, though this code will respond
roentgen b75cab
	    apropriately even before we simplify it */
roentgen b75cab
	sp->stream.avail_out = tif->tif_rawdatasize;
roentgen b75cab
	if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	return (deflateReset(&sp->stream) == Z_OK);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
roentgen b75cab
{
roentgen b75cab
    int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
roentgen b75cab
    float fltsize = Fltsize;
roentgen b75cab
roentgen b75cab
#define  CLAMP(v) ( (v<(float)0.)   ? 0				\
roentgen b75cab
		  : (v<(float)2.)   ? FromLT2[(int)(v*fltsize)]	\
roentgen b75cab
		  : (v>(float)24.2) ? 2047			\
roentgen b75cab
		  : LogK1*log(v*LogK2) + 0.5 )
roentgen b75cab
roentgen b75cab
    mask = CODE_MASK;
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    r2 = wp[0] = (uint16) CLAMP(ip[0]);
roentgen b75cab
	    g2 = wp[1] = (uint16) CLAMP(ip[1]);
roentgen b75cab
	    b2 = wp[2] = (uint16) CLAMP(ip[2]);
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 3;
roentgen b75cab
		wp += 3;
roentgen b75cab
		ip += 3;
roentgen b75cab
		r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
roentgen b75cab
		g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
roentgen b75cab
		b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    r2 = wp[0] = (uint16) CLAMP(ip[0]);
roentgen b75cab
	    g2 = wp[1] = (uint16) CLAMP(ip[1]);
roentgen b75cab
	    b2 = wp[2] = (uint16) CLAMP(ip[2]);
roentgen b75cab
	    a2 = wp[3] = (uint16) CLAMP(ip[3]);
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 4;
roentgen b75cab
		wp += 4;
roentgen b75cab
		ip += 4;
roentgen b75cab
		r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
roentgen b75cab
		g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
roentgen b75cab
		b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
roentgen b75cab
		a1 = (int32) CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    ip += n - 1;	/* point to last one */
roentgen b75cab
	    wp += n - 1;	/* point to last one */
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]);
roentgen b75cab
				wp[stride] -= wp[0];
roentgen b75cab
				wp[stride] &= mask;
roentgen b75cab
				wp--; ip--)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	    REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp--; ip--)
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalDifference16(unsigned short *ip, int n, int stride, 
roentgen b75cab
	unsigned short *wp, uint16 *From14)
roentgen b75cab
{
roentgen b75cab
    register int  r1, g1, b1, a1, r2, g2, b2, a2, mask;
roentgen b75cab
roentgen b75cab
/* assumption is unsigned pixel values */
roentgen b75cab
#undef   CLAMP
roentgen b75cab
#define  CLAMP(v) From14[(v) >> 2]
roentgen b75cab
roentgen b75cab
    mask = CODE_MASK;
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    r2 = wp[0] = CLAMP(ip[0]);  g2 = wp[1] = CLAMP(ip[1]);
roentgen b75cab
	    b2 = wp[2] = CLAMP(ip[2]);
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 3;
roentgen b75cab
		wp += 3;
roentgen b75cab
		ip += 3;
roentgen b75cab
		r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
roentgen b75cab
		g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
roentgen b75cab
		b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    r2 = wp[0] = CLAMP(ip[0]);  g2 = wp[1] = CLAMP(ip[1]);
roentgen b75cab
	    b2 = wp[2] = CLAMP(ip[2]);  a2 = wp[3] = CLAMP(ip[3]);
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 4;
roentgen b75cab
		wp += 4;
roentgen b75cab
		ip += 4;
roentgen b75cab
		r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
roentgen b75cab
		g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
roentgen b75cab
		b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
roentgen b75cab
		a1 = CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    ip += n - 1;	/* point to last one */
roentgen b75cab
	    wp += n - 1;	/* point to last one */
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride, wp[0] = CLAMP(ip[0]);
roentgen b75cab
				wp[stride] -= wp[0];
roentgen b75cab
				wp[stride] &= mask;
roentgen b75cab
				wp--; ip--)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	    REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horizontalDifference8(unsigned char *ip, int n, int stride, 
roentgen b75cab
	unsigned short *wp, uint16 *From8)
roentgen b75cab
{
roentgen b75cab
    register int  r1, g1, b1, a1, r2, g2, b2, a2, mask;
roentgen b75cab
roentgen b75cab
#undef	 CLAMP
roentgen b75cab
#define  CLAMP(v) (From8[(v)])
roentgen b75cab
roentgen b75cab
    mask = CODE_MASK;
roentgen b75cab
    if (n >= stride) {
roentgen b75cab
	if (stride == 3) {
roentgen b75cab
	    r2 = wp[0] = CLAMP(ip[0]);  g2 = wp[1] = CLAMP(ip[1]);
roentgen b75cab
	    b2 = wp[2] = CLAMP(ip[2]);
roentgen b75cab
	    n -= 3;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 3;
roentgen b75cab
		r1 = CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
roentgen b75cab
		g1 = CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
roentgen b75cab
		b1 = CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
roentgen b75cab
		wp += 3;
roentgen b75cab
		ip += 3;
roentgen b75cab
	    }
roentgen b75cab
	} else if (stride == 4) {
roentgen b75cab
	    r2 = wp[0] = CLAMP(ip[0]);  g2 = wp[1] = CLAMP(ip[1]);
roentgen b75cab
	    b2 = wp[2] = CLAMP(ip[2]);  a2 = wp[3] = CLAMP(ip[3]);
roentgen b75cab
	    n -= 4;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		n -= 4;
roentgen b75cab
		r1 = CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
roentgen b75cab
		g1 = CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
roentgen b75cab
		b1 = CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
roentgen b75cab
		a1 = CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
roentgen b75cab
		wp += 4;
roentgen b75cab
		ip += 4;
roentgen b75cab
	    }
roentgen b75cab
	} else {
roentgen b75cab
	    wp += n + stride - 1;	/* point to last one */
roentgen b75cab
	    ip += n + stride - 1;	/* point to last one */
roentgen b75cab
	    n -= stride;
roentgen b75cab
	    while (n > 0) {
roentgen b75cab
		REPEAT(stride, wp[0] = CLAMP(ip[0]);
roentgen b75cab
				wp[stride] -= wp[0];
roentgen b75cab
				wp[stride] &= mask;
roentgen b75cab
				wp--; ip--)
roentgen b75cab
		n -= stride;
roentgen b75cab
	    }
roentgen b75cab
	    REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode a chunk of pixels.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PixarLogEncode";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	PixarLogState *sp = EncoderState(tif);
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t n;
roentgen b75cab
	int llen;
roentgen b75cab
	unsigned short * up;
roentgen b75cab
roentgen b75cab
	(void) s;
roentgen b75cab
roentgen b75cab
	switch (sp->user_datafmt) {
roentgen b75cab
	case PIXARLOGDATAFMT_FLOAT:
roentgen b75cab
		n = cc / sizeof(float);		/* XXX float == 32 bits */
roentgen b75cab
		break;
roentgen b75cab
	case PIXARLOGDATAFMT_16BIT:
roentgen b75cab
	case PIXARLOGDATAFMT_12BITPICIO:
roentgen b75cab
	case PIXARLOGDATAFMT_11BITLOG:
roentgen b75cab
		n = cc / sizeof(uint16);	/* XXX uint16 == 16 bits */
roentgen b75cab
		break;
roentgen b75cab
	case PIXARLOGDATAFMT_8BIT:
roentgen b75cab
	case PIXARLOGDATAFMT_8BITABGR:
roentgen b75cab
		n = cc;
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			"%d bit input not supported in PixarLog",
roentgen b75cab
			td->td_bitspersample);
roentgen b75cab
		return 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	llen = sp->stride * td->td_imagewidth;
roentgen b75cab
roentgen b75cab
	for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
roentgen b75cab
		switch (sp->user_datafmt)  {
roentgen b75cab
		case PIXARLOGDATAFMT_FLOAT:
roentgen b75cab
			horizontalDifferenceF((float *)bp, llen, 
roentgen b75cab
				sp->stride, up, sp->FromLT2);
roentgen b75cab
			bp += llen * sizeof(float);
roentgen b75cab
			break;
roentgen b75cab
		case PIXARLOGDATAFMT_16BIT:
roentgen b75cab
			horizontalDifference16((uint16 *)bp, llen, 
roentgen b75cab
				sp->stride, up, sp->From14);
roentgen b75cab
			bp += llen * sizeof(uint16);
roentgen b75cab
			break;
roentgen b75cab
		case PIXARLOGDATAFMT_8BIT:
roentgen b75cab
			horizontalDifference8((unsigned char *)bp, llen, 
roentgen b75cab
				sp->stride, up, sp->From8);
roentgen b75cab
			bp += llen * sizeof(unsigned char);
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				"%d bit input not supported in PixarLog",
roentgen b75cab
				td->td_bitspersample);
roentgen b75cab
			return 0;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
 
roentgen b75cab
	sp->stream.next_in = (unsigned char *) sp->tbuf;
roentgen b75cab
	assert(sizeof(sp->stream.avail_in)==4);  /* if this assert gets raised,
roentgen b75cab
	    we need to simplify this code to reflect a ZLib that is likely updated
roentgen b75cab
	    to deal with 8byte memory sizes, though this code will respond
roentgen b75cab
	    apropriately even before we simplify it */
roentgen b75cab
	sp->stream.avail_in = (uInt) (n * sizeof(uint16));
roentgen b75cab
	if ((sp->stream.avail_in / sizeof(uint16)) != (uInt) n)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "ZLib cannot deal with buffers this size");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	do {
roentgen b75cab
		if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
roentgen b75cab
			    sp->stream.msg);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		if (sp->stream.avail_out == 0) {
roentgen b75cab
			tif->tif_rawcc = tif->tif_rawdatasize;
roentgen b75cab
			TIFFFlushData1(tif);
roentgen b75cab
			sp->stream.next_out = tif->tif_rawdata;
roentgen b75cab
			sp->stream.avail_out = (uInt) tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in PixarLogPreEncode */
roentgen b75cab
		}
roentgen b75cab
	} while (sp->stream.avail_in > 0);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Finish off an encoded strip by flushing the last
roentgen b75cab
 * string and tacking on an End Of Information code.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogPostEncode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PixarLogPostEncode";
roentgen b75cab
	PixarLogState *sp = EncoderState(tif);
roentgen b75cab
	int state;
roentgen b75cab
roentgen b75cab
	sp->stream.avail_in = 0;
roentgen b75cab
roentgen b75cab
	do {
roentgen b75cab
		state = deflate(&sp->stream, Z_FINISH);
roentgen b75cab
		switch (state) {
roentgen b75cab
		case Z_STREAM_END:
roentgen b75cab
		case Z_OK:
roentgen b75cab
		    if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
roentgen b75cab
			    tif->tif_rawcc =
roentgen b75cab
				tif->tif_rawdatasize - sp->stream.avail_out;
roentgen b75cab
			    TIFFFlushData1(tif);
roentgen b75cab
			    sp->stream.next_out = tif->tif_rawdata;
roentgen b75cab
			    sp->stream.avail_out = (uInt) tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in PixarLogPreEncode */
roentgen b75cab
		    }
roentgen b75cab
		    break;
roentgen b75cab
		default:
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
roentgen b75cab
			sp->stream.msg);
roentgen b75cab
		    return (0);
roentgen b75cab
		}
roentgen b75cab
	} while (state != Z_STREAM_END);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
PixarLogClose(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	/* In a really sneaky (and really incorrect, and untruthfull, and
roentgen b75cab
	 * troublesome, and error-prone) maneuver that completely goes against
roentgen b75cab
	 * the spirit of TIFF, and breaks TIFF, on close, we covertly
roentgen b75cab
	 * modify both bitspersample and sampleformat in the directory to
roentgen b75cab
	 * indicate 8-bit linear.  This way, the decode "just works" even for
roentgen b75cab
	 * readers that don't know about PixarLog, or how to set
roentgen b75cab
	 * the PIXARLOGDATFMT pseudo-tag.
roentgen b75cab
	 */
roentgen b75cab
	td->td_bitspersample = 8;
roentgen b75cab
	td->td_sampleformat = SAMPLEFORMAT_UINT;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
PixarLogCleanup(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	PixarLogState* sp = (PixarLogState*) tif->tif_data;
roentgen b75cab
roentgen b75cab
	assert(sp != 0);
roentgen b75cab
roentgen b75cab
	(void)TIFFPredictorCleanup(tif);
roentgen b75cab
roentgen b75cab
	tif->tif_tagmethods.vgetfield = sp->vgetparent;
roentgen b75cab
	tif->tif_tagmethods.vsetfield = sp->vsetparent;
roentgen b75cab
roentgen b75cab
	if (sp->FromLT2) _TIFFfree(sp->FromLT2);
roentgen b75cab
	if (sp->From14) _TIFFfree(sp->From14);
roentgen b75cab
	if (sp->From8) _TIFFfree(sp->From8);
roentgen b75cab
	if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
roentgen b75cab
	if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
roentgen b75cab
	if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
roentgen b75cab
	if (sp->state&PLSTATE_INIT) {
roentgen b75cab
		if (tif->tif_mode == O_RDONLY)
roentgen b75cab
			inflateEnd(&sp->stream);
roentgen b75cab
		else
roentgen b75cab
			deflateEnd(&sp->stream);
roentgen b75cab
	}
roentgen b75cab
	if (sp->tbuf)
roentgen b75cab
		_TIFFfree(sp->tbuf);
roentgen b75cab
	_TIFFfree(sp);
roentgen b75cab
	tif->tif_data = NULL;
roentgen b75cab
roentgen b75cab
	_TIFFSetDefaultCompressionState(tif);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogVSetField(TIFF* tif, uint32 tag, va_list ap)
roentgen b75cab
{
roentgen b75cab
    static const char module[] = "PixarLogVSetField";
roentgen b75cab
    PixarLogState *sp = (PixarLogState *)tif->tif_data;
roentgen b75cab
    int result;
roentgen b75cab
roentgen b75cab
    switch (tag) {
roentgen b75cab
     case TIFFTAG_PIXARLOGQUALITY:
roentgen b75cab
		sp->quality = (int) va_arg(ap, int);
roentgen b75cab
		if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
roentgen b75cab
			if (deflateParams(&sp->stream,
roentgen b75cab
			    sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
roentgen b75cab
					sp->stream.msg);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		return (1);
roentgen b75cab
     case TIFFTAG_PIXARLOGDATAFMT:
roentgen b75cab
	sp->user_datafmt = (int) va_arg(ap, int);
roentgen b75cab
	/* Tweak the TIFF header so that the rest of libtiff knows what
roentgen b75cab
	 * size of data will be passed between app and library, and
roentgen b75cab
	 * assume that the app knows what it is doing and is not
roentgen b75cab
	 * confused by these header manipulations...
roentgen b75cab
	 */
roentgen b75cab
	switch (sp->user_datafmt) {
roentgen b75cab
	 case PIXARLOGDATAFMT_8BIT:
roentgen b75cab
	 case PIXARLOGDATAFMT_8BITABGR:
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
roentgen b75cab
	    break;
roentgen b75cab
	 case PIXARLOGDATAFMT_11BITLOG:
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
roentgen b75cab
	    break;
roentgen b75cab
	 case PIXARLOGDATAFMT_12BITPICIO:
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
roentgen b75cab
	    break;
roentgen b75cab
	 case PIXARLOGDATAFMT_16BIT:
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
roentgen b75cab
	    break;
roentgen b75cab
	 case PIXARLOGDATAFMT_FLOAT:
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
roentgen b75cab
	    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
roentgen b75cab
	    break;
roentgen b75cab
	}
roentgen b75cab
	/*
roentgen b75cab
	 * Must recalculate sizes should bits/sample change.
roentgen b75cab
	 */
roentgen b75cab
	tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
roentgen b75cab
	tif->tif_scanlinesize = TIFFScanlineSize(tif);
roentgen b75cab
	result = 1;		/* NB: pseudo tag */
roentgen b75cab
	break;
roentgen b75cab
     default:
roentgen b75cab
	result = (*sp->vsetparent)(tif, tag, ap);
roentgen b75cab
    }
roentgen b75cab
    return (result);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PixarLogVGetField(TIFF* tif, uint32 tag, va_list ap)
roentgen b75cab
{
roentgen b75cab
    PixarLogState *sp = (PixarLogState *)tif->tif_data;
roentgen b75cab
roentgen b75cab
    switch (tag) {
roentgen b75cab
     case TIFFTAG_PIXARLOGQUALITY:
roentgen b75cab
	*va_arg(ap, int*) = sp->quality;
roentgen b75cab
	break;
roentgen b75cab
     case TIFFTAG_PIXARLOGDATAFMT:
roentgen b75cab
	*va_arg(ap, int*) = sp->user_datafmt;
roentgen b75cab
	break;
roentgen b75cab
     default:
roentgen b75cab
	return (*sp->vgetparent)(tif, tag, ap);
roentgen b75cab
    }
roentgen b75cab
    return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static const TIFFField pixarlogFields[] = {
roentgen b75cab
    {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
roentgen b75cab
    {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFInitPixarLog(TIFF* tif, int scheme)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFInitPixarLog";
roentgen b75cab
roentgen b75cab
	PixarLogState* sp;
roentgen b75cab
roentgen b75cab
	assert(scheme == COMPRESSION_PIXARLOG);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Merge codec-specific tag information.
roentgen b75cab
	 */
roentgen b75cab
	if (!_TIFFMergeFields(tif, pixarlogFields,
roentgen b75cab
			      TIFFArrayCount(pixarlogFields))) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "Merging PixarLog codec-specific tags failed");
roentgen b75cab
		return 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Allocate state block so tag methods have storage to record values.
roentgen b75cab
	 */
roentgen b75cab
	tif->tif_data = (uint8*) _TIFFmalloc(sizeof (PixarLogState));
roentgen b75cab
	if (tif->tif_data == NULL)
roentgen b75cab
		goto bad;
roentgen b75cab
	sp = (PixarLogState*) tif->tif_data;
roentgen b75cab
	_TIFFmemset(sp, 0, sizeof (*sp));
roentgen b75cab
	sp->stream.data_type = Z_BINARY;
roentgen b75cab
	sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Install codec methods.
roentgen b75cab
	 */
roentgen b75cab
	tif->tif_fixuptags = PixarLogFixupTags; 
roentgen b75cab
	tif->tif_setupdecode = PixarLogSetupDecode;
roentgen b75cab
	tif->tif_predecode = PixarLogPreDecode;
roentgen b75cab
	tif->tif_decoderow = PixarLogDecode;
roentgen b75cab
	tif->tif_decodestrip = PixarLogDecode;  
roentgen b75cab
	tif->tif_decodetile = PixarLogDecode;
roentgen b75cab
	tif->tif_setupencode = PixarLogSetupEncode;
roentgen b75cab
	tif->tif_preencode = PixarLogPreEncode;
roentgen b75cab
	tif->tif_postencode = PixarLogPostEncode;
roentgen b75cab
	tif->tif_encoderow = PixarLogEncode;  
roentgen b75cab
	tif->tif_encodestrip = PixarLogEncode;
roentgen b75cab
	tif->tif_encodetile = PixarLogEncode;  
roentgen b75cab
	tif->tif_close = PixarLogClose;
roentgen b75cab
	tif->tif_cleanup = PixarLogCleanup;
roentgen b75cab
roentgen b75cab
	/* Override SetField so we can handle our private pseudo-tag */
roentgen b75cab
	sp->vgetparent = tif->tif_tagmethods.vgetfield;
roentgen b75cab
	tif->tif_tagmethods.vgetfield = PixarLogVGetField;   /* hook for codec tags */
roentgen b75cab
	sp->vsetparent = tif->tif_tagmethods.vsetfield;
roentgen b75cab
	tif->tif_tagmethods.vsetfield = PixarLogVSetField;   /* hook for codec tags */
roentgen b75cab
roentgen b75cab
	/* Default values for codec-specific fields */
roentgen b75cab
	sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
roentgen b75cab
	sp->state = 0;
roentgen b75cab
roentgen b75cab
	/* we don't wish to use the predictor, 
roentgen b75cab
	 * the default is none, which predictor value 1
roentgen b75cab
	 */
roentgen b75cab
	(void) TIFFPredictorInit(tif);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * build the companding tables 
roentgen b75cab
	 */
roentgen b75cab
	PixarLogMakeTables(sp);
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
bad:
roentgen b75cab
	TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		     "No space for PixarLog state block");
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
#endif /* PIXARLOG_SUPPORT */
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */