roentgen b75cab
/* $Id: tif_luv.c,v 1.35 2011-04-02 20:54:09 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1997 Greg Ward Larson
roentgen b75cab
 * Copyright (c) 1997 Silicon Graphics, Inc.
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
 * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any
roentgen b75cab
 * advertising or publicity relating to the software without the specific,
roentgen b75cab
 * prior written permission of Sam Leffler, Greg Larson 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 SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE
roentgen b75cab
 * FOR 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 LOGLUV_SUPPORT
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 * LogLuv compression support for high dynamic range images.
roentgen b75cab
 *
roentgen b75cab
 * Contributed by Greg Larson.
roentgen b75cab
 *
roentgen b75cab
 * LogLuv image support uses the TIFF library to store 16 or 10-bit
roentgen b75cab
 * log luminance values with 8 bits each of u and v or a 14-bit index.
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 integer values.  A 16-bit luminance is interpreted
roentgen b75cab
 * as a sign bit followed by a 15-bit integer that is converted
roentgen b75cab
 * to and from a linear magnitude using the transformation:
roentgen b75cab
 *
roentgen b75cab
 *	L = 2^( (Le+.5)/256 - 64 )		# real from 15-bit
roentgen b75cab
 *
roentgen b75cab
 *	Le = floor( 256*(log2(L) + 64) )	# 15-bit from real
roentgen b75cab
 *
roentgen b75cab
 * The actual conversion to world luminance units in candelas per sq. meter
roentgen b75cab
 * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
roentgen b75cab
 * This value is usually set such that a reasonable exposure comes from
roentgen b75cab
 * clamping decoded luminances above 1 to 1 in the displayed image.
roentgen b75cab
 *
roentgen b75cab
 * The 16-bit values for u and v may be converted to real values by dividing
roentgen b75cab
 * each by 32768.  (This allows for negative values, which aren't useful as
roentgen b75cab
 * far as we know, but are left in case of future improvements in human
roentgen b75cab
 * color vision.)
roentgen b75cab
 *
roentgen b75cab
 * Conversion from (u,v), which is actually the CIE (u',v') system for
roentgen b75cab
 * you color scientists, is accomplished by the following transformation:
roentgen b75cab
 *
roentgen b75cab
 *	u = 4*x / (-2*x + 12*y + 3)
roentgen b75cab
 *	v = 9*y / (-2*x + 12*y + 3)
roentgen b75cab
 *
roentgen b75cab
 *	x = 9*u / (6*u - 16*v + 12)
roentgen b75cab
 *	y = 4*v / (6*u - 16*v + 12)
roentgen b75cab
 *
roentgen b75cab
 * This process is greatly simplified by passing 32-bit IEEE floats
roentgen b75cab
 * for each of three CIE XYZ coordinates.  The codec then takes care
roentgen b75cab
 * of conversion to and from LogLuv, though the application is still
roentgen b75cab
 * responsible for interpreting the TIFFTAG_STONITS calibration factor.
roentgen b75cab
 *
roentgen b75cab
 * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white
roentgen b75cab
 * point of (x,y)=(1/3,1/3).  However, most color systems assume some other
roentgen b75cab
 * white point, such as D65, and an absolute color conversion to XYZ then
roentgen b75cab
 * to another color space with a different white point may introduce an
roentgen b75cab
 * unwanted color cast to the image.  It is often desirable, therefore, to
roentgen b75cab
 * perform a white point conversion that maps the input white to [1 1 1]
roentgen b75cab
 * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT
roentgen b75cab
 * tag value.  A decoder that demands absolute color calibration may use
roentgen b75cab
 * this white point tag to get back the original colors, but usually it
roentgen b75cab
 * will be ignored and the new white point will be used instead that
roentgen b75cab
 * matches the output color space.
roentgen b75cab
 *
roentgen b75cab
 * Pixel information is compressed into one of two basic encodings, depending
roentgen b75cab
 * on the setting of the compression tag, which is one of COMPRESSION_SGILOG
roentgen b75cab
 * or COMPRESSION_SGILOG24.  For COMPRESSION_SGILOG, greyscale data is
roentgen b75cab
 * stored as:
roentgen b75cab
 *
roentgen b75cab
 *	 1       15
roentgen b75cab
 *	|-+---------------|
roentgen b75cab
 *
roentgen b75cab
 * COMPRESSION_SGILOG color data is stored as:
roentgen b75cab
 *
roentgen b75cab
 *	 1       15           8        8
roentgen b75cab
 *	|-+---------------|--------+--------|
roentgen b75cab
 *	 S       Le           ue       ve
roentgen b75cab
 *
roentgen b75cab
 * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
roentgen b75cab
 *
roentgen b75cab
 *	     10           14
roentgen b75cab
 *	|----------|--------------|
roentgen b75cab
 *	     Le'          Ce
roentgen b75cab
 *
roentgen b75cab
 * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
roentgen b75cab
 * encoded as an index for optimal color resolution.  The 10 log bits are
roentgen b75cab
 * defined by the following conversions:
roentgen b75cab
 *
roentgen b75cab
 *	L = 2^((Le'+.5)/64 - 12)		# real from 10-bit
roentgen b75cab
 *
roentgen b75cab
 *	Le' = floor( 64*(log2(L) + 12) )	# 10-bit from real
roentgen b75cab
 *
roentgen b75cab
 * The 10 bits of the smaller format may be converted into the 15 bits of
roentgen b75cab
 * the larger format by multiplying by 4 and adding 13314.  Obviously,
roentgen b75cab
 * a smaller range of magnitudes is covered (about 5 orders of magnitude
roentgen b75cab
 * instead of 38), and the lack of a sign bit means that negative luminances
roentgen b75cab
 * are not allowed.  (Well, they aren't allowed in the real world, either,
roentgen b75cab
 * but they are useful for certain types of image processing.)
roentgen b75cab
 *
roentgen b75cab
 * The desired user format is controlled by the setting the internal
roentgen b75cab
 * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
roentgen b75cab
 *  SGILOGDATAFMT_FLOAT       = IEEE 32-bit float XYZ values
roentgen b75cab
 *  SGILOGDATAFMT_16BIT	      = 16-bit integer encodings of logL, u and v
roentgen b75cab
 * Raw data i/o is also possible using:
roentgen b75cab
 *  SGILOGDATAFMT_RAW         = 32-bit unsigned integer with encoded pixel
roentgen b75cab
 * In addition, the following decoding is provided for ease of display:
roentgen b75cab
 *  SGILOGDATAFMT_8BIT        = 8-bit default RGB gamma-corrected values
roentgen b75cab
 *
roentgen b75cab
 * For grayscale images, we provide the following data formats:
roentgen b75cab
 *  SGILOGDATAFMT_FLOAT       = IEEE 32-bit float Y values
roentgen b75cab
 *  SGILOGDATAFMT_16BIT       = 16-bit integer w/ encoded luminance
roentgen b75cab
 *  SGILOGDATAFMT_8BIT        = 8-bit gray monitor values
roentgen b75cab
 *
roentgen b75cab
 * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
roentgen b75cab
 * scheme by separating the logL, u and v bytes for each row and applying
roentgen b75cab
 * a PackBits type of compression.  Since the 24-bit encoding is not
roentgen b75cab
 * adaptive, the 32-bit color format takes less space in many cases.
roentgen b75cab
 *
roentgen b75cab
 * Further control is provided over the conversion from higher-resolution
roentgen b75cab
 * formats to final encoded values through the pseudo tag
roentgen b75cab
 * TIFFTAG_SGILOGENCODE:
roentgen b75cab
 *  SGILOGENCODE_NODITHER     = do not dither encoded values
roentgen b75cab
 *  SGILOGENCODE_RANDITHER    = apply random dithering during encoding
roentgen b75cab
 *
roentgen b75cab
 * The default value of this tag is SGILOGENCODE_NODITHER for
roentgen b75cab
 * COMPRESSION_SGILOG to maximize run-length encoding and
roentgen b75cab
 * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn
roentgen b75cab
 * quantization errors into noise.
roentgen b75cab
 */
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
/*
roentgen b75cab
 * State block for each open TIFF
roentgen b75cab
 * file using LogLuv compression/decompression.
roentgen b75cab
 */
roentgen b75cab
typedef struct logLuvState LogLuvState;
roentgen b75cab
roentgen b75cab
struct logLuvState {
roentgen b75cab
	int                     user_datafmt;   /* user data format */
roentgen b75cab
	int                     encode_meth;    /* encoding method */
roentgen b75cab
	int                     pixel_size;     /* bytes per pixel */
roentgen b75cab
roentgen b75cab
	uint8*                  tbuf;           /* translation buffer */
roentgen b75cab
	tmsize_t                tbuflen;        /* buffer length */
roentgen b75cab
	void (*tfunc)(LogLuvState*, uint8*, tmsize_t);
roentgen b75cab
roentgen b75cab
	TIFFVSetMethod          vgetparent;     /* super-class method */
roentgen b75cab
	TIFFVSetMethod          vsetparent;     /* super-class method */
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
#define DecoderState(tif)	((LogLuvState*) (tif)->tif_data)
roentgen b75cab
#define EncoderState(tif)	((LogLuvState*) (tif)->tif_data)
roentgen b75cab
roentgen b75cab
#define SGILOGDATAFMT_UNKNOWN -1
roentgen b75cab
roentgen b75cab
#define MINRUN 4 /* minimum run length */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Decode a string of 16-bit gray pixels.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogL16Decode";
roentgen b75cab
	LogLuvState* sp = DecoderState(tif);
roentgen b75cab
	int shft;
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t npixels;
roentgen b75cab
	unsigned char* bp;
roentgen b75cab
	int16* tp;
roentgen b75cab
	int16 b;
roentgen b75cab
	tmsize_t cc;
roentgen b75cab
	int rc;
roentgen b75cab
roentgen b75cab
	assert(s == 0);
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
roentgen b75cab
	npixels = occ / sp->pixel_size;
roentgen b75cab
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
roentgen b75cab
		tp = (int16*) op;
roentgen b75cab
	else {
roentgen b75cab
		assert(sp->tbuflen >= npixels);
roentgen b75cab
		tp = (int16*) sp->tbuf;
roentgen b75cab
	}
roentgen b75cab
	_TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
roentgen b75cab
roentgen b75cab
	bp = (unsigned char*) tif->tif_rawcp;
roentgen b75cab
	cc = tif->tif_rawcc;
roentgen b75cab
	/* get each byte string */
roentgen b75cab
	for (shft = 2*8; (shft -= 8) >= 0; ) {
roentgen b75cab
		for (i = 0; i < npixels && cc > 0; )
roentgen b75cab
			if (*bp >= 128) {		/* run */
roentgen b75cab
				rc = *bp++ + (2-128);   /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
roentgen b75cab
				b = (int16)(*bp++ << shft);
roentgen b75cab
				cc -= 2;
roentgen b75cab
				while (rc-- && i < npixels)
roentgen b75cab
					tp[i++] |= b;
roentgen b75cab
			} else {			/* non-run */
roentgen b75cab
				rc = *bp++;		/* nul is noop */
roentgen b75cab
				while (--cc && rc-- && i < npixels)
roentgen b75cab
					tp[i++] |= (int16)*bp++ << shft;
roentgen b75cab
			}
roentgen b75cab
		if (i != npixels) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Not enough data at row %lu (short %I64d pixels)",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned __int64) (npixels - i));
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Not enough data at row %lu (short %llu pixels)",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long long) (npixels - i));
roentgen b75cab
#endif
roentgen b75cab
			tif->tif_rawcp = (uint8*) bp;
roentgen b75cab
			tif->tif_rawcc = cc;
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	(*sp->tfunc)(sp, op, npixels);
roentgen b75cab
	tif->tif_rawcp = (uint8*) bp;
roentgen b75cab
	tif->tif_rawcc = cc;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Decode a string of 24-bit pixels.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvDecode24(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogLuvDecode24";
roentgen b75cab
	LogLuvState* sp = DecoderState(tif);
roentgen b75cab
	tmsize_t cc;
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t npixels;
roentgen b75cab
	unsigned char* bp;
roentgen b75cab
	uint32* tp;
roentgen b75cab
roentgen b75cab
	assert(s == 0);
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
roentgen b75cab
	npixels = occ / sp->pixel_size;
roentgen b75cab
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_RAW)
roentgen b75cab
		tp = (uint32 *)op;
roentgen b75cab
	else {
roentgen b75cab
		assert(sp->tbuflen >= npixels);
roentgen b75cab
		tp = (uint32 *) sp->tbuf;
roentgen b75cab
	}
roentgen b75cab
	/* copy to array of uint32 */
roentgen b75cab
	bp = (unsigned char*) tif->tif_rawcp;
roentgen b75cab
	cc = tif->tif_rawcc;
roentgen b75cab
	for (i = 0; i < npixels && cc > 0; i++) {
roentgen b75cab
		tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
roentgen b75cab
		bp += 3;
roentgen b75cab
		cc -= 3;
roentgen b75cab
	}
roentgen b75cab
	tif->tif_rawcp = (uint8*) bp;
roentgen b75cab
	tif->tif_rawcc = cc;
roentgen b75cab
	if (i != npixels) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			"Not enough data at row %lu (short %I64d pixels)",
roentgen b75cab
			     (unsigned long) tif->tif_row,
roentgen b75cab
			     (unsigned __int64) (npixels - i));
roentgen b75cab
#else
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			"Not enough data at row %lu (short %llu pixels)",
roentgen b75cab
			     (unsigned long) tif->tif_row,
roentgen b75cab
			     (unsigned long long) (npixels - i));
roentgen b75cab
#endif
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	(*sp->tfunc)(sp, op, npixels);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Decode a string of 32-bit pixels.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogLuvDecode32";
roentgen b75cab
	LogLuvState* sp;
roentgen b75cab
	int shft;
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t npixels;
roentgen b75cab
	unsigned char* bp;
roentgen b75cab
	uint32* tp;
roentgen b75cab
	uint32 b;
roentgen b75cab
	tmsize_t cc;
roentgen b75cab
	int rc;
roentgen b75cab
roentgen b75cab
	assert(s == 0);
roentgen b75cab
	sp = DecoderState(tif);
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
roentgen b75cab
	npixels = occ / sp->pixel_size;
roentgen b75cab
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_RAW)
roentgen b75cab
		tp = (uint32*) op;
roentgen b75cab
	else {
roentgen b75cab
		assert(sp->tbuflen >= npixels);
roentgen b75cab
		tp = (uint32*) sp->tbuf;
roentgen b75cab
	}
roentgen b75cab
	_TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
roentgen b75cab
roentgen b75cab
	bp = (unsigned char*) tif->tif_rawcp;
roentgen b75cab
	cc = tif->tif_rawcc;
roentgen b75cab
	/* get each byte string */
roentgen b75cab
	for (shft = 4*8; (shft -= 8) >= 0; ) {
roentgen b75cab
		for (i = 0; i < npixels && cc > 0; )
roentgen b75cab
			if (*bp >= 128) {		/* run */
roentgen b75cab
				rc = *bp++ + (2-128);
roentgen b75cab
				b = (uint32)*bp++ << shft;
roentgen b75cab
				cc -= 2;                /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
roentgen b75cab
				while (rc-- && i < npixels)
roentgen b75cab
					tp[i++] |= b;
roentgen b75cab
			} else {			/* non-run */
roentgen b75cab
				rc = *bp++;		/* nul is noop */
roentgen b75cab
				while (--cc && rc-- && i < npixels)
roentgen b75cab
					tp[i++] |= (uint32)*bp++ << shft;
roentgen b75cab
			}
roentgen b75cab
		if (i != npixels) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			"Not enough data at row %lu (short %I64d pixels)",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned __int64) (npixels - i));
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			"Not enough data at row %lu (short %llu pixels)",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long long) (npixels - i));
roentgen b75cab
#endif
roentgen b75cab
			tif->tif_rawcp = (uint8*) bp;
roentgen b75cab
			tif->tif_rawcc = cc;
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	(*sp->tfunc)(sp, op, npixels);
roentgen b75cab
	tif->tif_rawcp = (uint8*) bp;
roentgen b75cab
	tif->tif_rawcc = cc;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Decode a strip of pixels.  We break it into rows to
roentgen b75cab
 * maintain synchrony with the encode algorithm, which
roentgen b75cab
 * is row by row.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvDecodeStrip(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	tmsize_t rowlen = TIFFScanlineSize(tif);
roentgen b75cab
roentgen b75cab
	assert(cc%rowlen == 0);
roentgen b75cab
	while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
roentgen b75cab
		bp += rowlen, cc -= rowlen;
roentgen b75cab
	return (cc == 0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Decode a tile of pixels.  We break it into rows to
roentgen b75cab
 * maintain synchrony with the encode algorithm, which
roentgen b75cab
 * is row by row.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvDecodeTile(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	tmsize_t rowlen = TIFFTileRowSize(tif);
roentgen b75cab
roentgen b75cab
	assert(cc%rowlen == 0);
roentgen b75cab
	while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
roentgen b75cab
		bp += rowlen, cc -= rowlen;
roentgen b75cab
	return (cc == 0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode a row of 16-bit pixels.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	LogLuvState* sp = EncoderState(tif);
roentgen b75cab
	int shft;
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t j;
roentgen b75cab
	tmsize_t npixels;
roentgen b75cab
	uint8* op;
roentgen b75cab
	int16* tp;
roentgen b75cab
	int16 b;
roentgen b75cab
	tmsize_t occ;
roentgen b75cab
	int rc=0, mask;
roentgen b75cab
	tmsize_t beg;
roentgen b75cab
roentgen b75cab
	assert(s == 0);
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	npixels = cc / sp->pixel_size;
roentgen b75cab
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
roentgen b75cab
		tp = (int16*) bp;
roentgen b75cab
	else {
roentgen b75cab
		tp = (int16*) sp->tbuf;
roentgen b75cab
		assert(sp->tbuflen >= npixels);
roentgen b75cab
		(*sp->tfunc)(sp, bp, npixels);
roentgen b75cab
	}
roentgen b75cab
	/* compress each byte string */
roentgen b75cab
	op = tif->tif_rawcp;
roentgen b75cab
	occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
	for (shft = 2*8; (shft -= 8) >= 0; )
roentgen b75cab
		for (i = 0; i < npixels; i += rc) {
roentgen b75cab
			if (occ < 4) {
roentgen b75cab
				tif->tif_rawcp = op;
roentgen b75cab
				tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
				if (!TIFFFlushData1(tif))
roentgen b75cab
					return (-1);
roentgen b75cab
				op = tif->tif_rawcp;
roentgen b75cab
				occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
			}
roentgen b75cab
			mask = 0xff << shft;		/* find next run */
roentgen b75cab
			for (beg = i; beg < npixels; beg += rc) {
roentgen b75cab
				b = (int16) (tp[beg] & mask);
roentgen b75cab
				rc = 1;
roentgen b75cab
				while (rc < 127+2 && beg+rc < npixels &&
roentgen b75cab
				    (tp[beg+rc] & mask) == b)
roentgen b75cab
					rc++;
roentgen b75cab
				if (rc >= MINRUN)
roentgen b75cab
					break;		/* long enough */
roentgen b75cab
			}
roentgen b75cab
			if (beg-i > 1 && beg-i < MINRUN) {
roentgen b75cab
				b = (int16) (tp[i] & mask);/*check short run */
roentgen b75cab
				j = i+1;
roentgen b75cab
				while ((tp[j++] & mask) == b)
roentgen b75cab
					if (j == beg) {
roentgen b75cab
						*op++ = (uint8)(128-2+j-i);
roentgen b75cab
						*op++ = (uint8)(b >> shft);
roentgen b75cab
						occ -= 2;
roentgen b75cab
						i = beg;
roentgen b75cab
						break;
roentgen b75cab
					}
roentgen b75cab
			}
roentgen b75cab
			while (i < beg) {		/* write out non-run */
roentgen b75cab
				if ((j = beg-i) > 127) j = 127;
roentgen b75cab
				if (occ < j+3) {
roentgen b75cab
					tif->tif_rawcp = op;
roentgen b75cab
					tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
					if (!TIFFFlushData1(tif))
roentgen b75cab
						return (-1);
roentgen b75cab
					op = tif->tif_rawcp;
roentgen b75cab
					occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
				}
roentgen b75cab
				*op++ = (uint8) j; occ--;
roentgen b75cab
				while (j--) {
roentgen b75cab
					*op++ = (uint8) (tp[i++] >> shft & 0xff);
roentgen b75cab
					occ--;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			if (rc >= MINRUN) {		/* write out run */
roentgen b75cab
				*op++ = (uint8) (128-2+rc);
roentgen b75cab
				*op++ = (uint8) (tp[beg] >> shft & 0xff);
roentgen b75cab
				occ -= 2;
roentgen b75cab
			} else
roentgen b75cab
				rc = 0;
roentgen b75cab
		}
roentgen b75cab
	tif->tif_rawcp = op;
roentgen b75cab
	tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode a row of 24-bit pixels.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	LogLuvState* sp = EncoderState(tif);
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t npixels;
roentgen b75cab
	tmsize_t occ;
roentgen b75cab
	uint8* op;
roentgen b75cab
	uint32* tp;
roentgen b75cab
roentgen b75cab
	assert(s == 0);
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	npixels = cc / sp->pixel_size;
roentgen b75cab
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_RAW)
roentgen b75cab
		tp = (uint32*) bp;
roentgen b75cab
	else {
roentgen b75cab
		tp = (uint32*) sp->tbuf;
roentgen b75cab
		assert(sp->tbuflen >= npixels);
roentgen b75cab
		(*sp->tfunc)(sp, bp, npixels);
roentgen b75cab
	}
roentgen b75cab
	/* write out encoded pixels */
roentgen b75cab
	op = tif->tif_rawcp;
roentgen b75cab
	occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
	for (i = npixels; i--; ) {
roentgen b75cab
		if (occ < 3) {
roentgen b75cab
			tif->tif_rawcp = op;
roentgen b75cab
			tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
			if (!TIFFFlushData1(tif))
roentgen b75cab
				return (-1);
roentgen b75cab
			op = tif->tif_rawcp;
roentgen b75cab
			occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
		}
roentgen b75cab
		*op++ = (uint8)(*tp >> 16);
roentgen b75cab
		*op++ = (uint8)(*tp >> 8 & 0xff);
roentgen b75cab
		*op++ = (uint8)(*tp++ & 0xff);
roentgen b75cab
		occ -= 3;
roentgen b75cab
	}
roentgen b75cab
	tif->tif_rawcp = op;
roentgen b75cab
	tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode a row of 32-bit pixels.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	LogLuvState* sp = EncoderState(tif);
roentgen b75cab
	int shft;
roentgen b75cab
	tmsize_t i;
roentgen b75cab
	tmsize_t j;
roentgen b75cab
	tmsize_t npixels;
roentgen b75cab
	uint8* op;
roentgen b75cab
	uint32* tp;
roentgen b75cab
	uint32 b;
roentgen b75cab
	tmsize_t occ;
roentgen b75cab
	int rc=0, mask;
roentgen b75cab
	tmsize_t beg;
roentgen b75cab
roentgen b75cab
	assert(s == 0);
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
roentgen b75cab
	npixels = cc / sp->pixel_size;
roentgen b75cab
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_RAW)
roentgen b75cab
		tp = (uint32*) bp;
roentgen b75cab
	else {
roentgen b75cab
		tp = (uint32*) sp->tbuf;
roentgen b75cab
		assert(sp->tbuflen >= npixels);
roentgen b75cab
		(*sp->tfunc)(sp, bp, npixels);
roentgen b75cab
	}
roentgen b75cab
	/* compress each byte string */
roentgen b75cab
	op = tif->tif_rawcp;
roentgen b75cab
	occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
	for (shft = 4*8; (shft -= 8) >= 0; )
roentgen b75cab
		for (i = 0; i < npixels; i += rc) {
roentgen b75cab
			if (occ < 4) {
roentgen b75cab
				tif->tif_rawcp = op;
roentgen b75cab
				tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
				if (!TIFFFlushData1(tif))
roentgen b75cab
					return (-1);
roentgen b75cab
				op = tif->tif_rawcp;
roentgen b75cab
				occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
			}
roentgen b75cab
			mask = 0xff << shft;		/* find next run */
roentgen b75cab
			for (beg = i; beg < npixels; beg += rc) {
roentgen b75cab
				b = tp[beg] & mask;
roentgen b75cab
				rc = 1;
roentgen b75cab
				while (rc < 127+2 && beg+rc < npixels &&
roentgen b75cab
						(tp[beg+rc] & mask) == b)
roentgen b75cab
					rc++;
roentgen b75cab
				if (rc >= MINRUN)
roentgen b75cab
					break;		/* long enough */
roentgen b75cab
			}
roentgen b75cab
			if (beg-i > 1 && beg-i < MINRUN) {
roentgen b75cab
				b = tp[i] & mask;	/* check short run */
roentgen b75cab
				j = i+1;
roentgen b75cab
				while ((tp[j++] & mask) == b)
roentgen b75cab
					if (j == beg) {
roentgen b75cab
						*op++ = (uint8)(128-2+j-i);
roentgen b75cab
						*op++ = (uint8)(b >> shft);
roentgen b75cab
						occ -= 2;
roentgen b75cab
						i = beg;
roentgen b75cab
						break;
roentgen b75cab
					}
roentgen b75cab
			}
roentgen b75cab
			while (i < beg) {		/* write out non-run */
roentgen b75cab
				if ((j = beg-i) > 127) j = 127;
roentgen b75cab
				if (occ < j+3) {
roentgen b75cab
					tif->tif_rawcp = op;
roentgen b75cab
					tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
					if (!TIFFFlushData1(tif))
roentgen b75cab
						return (-1);
roentgen b75cab
					op = tif->tif_rawcp;
roentgen b75cab
					occ = tif->tif_rawdatasize - tif->tif_rawcc;
roentgen b75cab
				}
roentgen b75cab
				*op++ = (uint8) j; occ--;
roentgen b75cab
				while (j--) {
roentgen b75cab
					*op++ = (uint8)(tp[i++] >> shft & 0xff);
roentgen b75cab
					occ--;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			if (rc >= MINRUN) {		/* write out run */
roentgen b75cab
				*op++ = (uint8) (128-2+rc);
roentgen b75cab
				*op++ = (uint8)(tp[beg] >> shft & 0xff);
roentgen b75cab
				occ -= 2;
roentgen b75cab
			} else
roentgen b75cab
				rc = 0;
roentgen b75cab
		}
roentgen b75cab
	tif->tif_rawcp = op;
roentgen b75cab
	tif->tif_rawcc = tif->tif_rawdatasize - occ;
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode a strip of pixels.  We break it into rows to
roentgen b75cab
 * avoid encoding runs across row boundaries.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvEncodeStrip(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	tmsize_t rowlen = TIFFScanlineSize(tif);
roentgen b75cab
roentgen b75cab
	assert(cc%rowlen == 0);
roentgen b75cab
	while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
roentgen b75cab
		bp += rowlen, cc -= rowlen;
roentgen b75cab
	return (cc == 0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode a tile of pixels.  We break it into rows to
roentgen b75cab
 * avoid encoding runs across row boundaries.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
LogLuvEncodeTile(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	tmsize_t rowlen = TIFFTileRowSize(tif);
roentgen b75cab
roentgen b75cab
	assert(cc%rowlen == 0);
roentgen b75cab
	while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
roentgen b75cab
		bp += rowlen, cc -= rowlen;
roentgen b75cab
	return (cc == 0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode/Decode functions for converting to and from user formats.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include "uvcode.h"
roentgen b75cab
roentgen b75cab
#ifndef UVSCALE
roentgen b75cab
#define U_NEU		0.210526316
roentgen b75cab
#define V_NEU		0.473684211
roentgen b75cab
#define UVSCALE		410.
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifndef	M_LN2
roentgen b75cab
#define M_LN2		0.69314718055994530942
roentgen b75cab
#endif
roentgen b75cab
#ifndef M_PI
roentgen b75cab
#define M_PI		3.14159265358979323846
roentgen b75cab
#endif
roentgen b75cab
#define log2(x)		((1./M_LN2)*log(x))
roentgen b75cab
#define exp2(x)		exp(M_LN2*(x))
roentgen b75cab
roentgen b75cab
#define itrunc(x,m)	((m)==SGILOGENCODE_NODITHER ? \
roentgen b75cab
				(int)(x) : \
roentgen b75cab
				(int)((x) + rand()*(1./RAND_MAX) - .5))
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
double
roentgen b75cab
LogL16toY(int p16)		/* compute luminance from 16-bit LogL */
roentgen b75cab
{
roentgen b75cab
	int	Le = p16 & 0x7fff;
roentgen b75cab
	double	Y;
roentgen b75cab
roentgen b75cab
	if (!Le)
roentgen b75cab
		return (0.);
roentgen b75cab
	Y = exp(M_LN2/256.*(Le+.5) - M_LN2*64.);
roentgen b75cab
	return (!(p16 & 0x8000) ? Y : -Y);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
int
roentgen b75cab
LogL16fromY(double Y, int em)	/* get 16-bit LogL from Y */
roentgen b75cab
{
roentgen b75cab
	if (Y >= 1.8371976e19)
roentgen b75cab
		return (0x7fff);
roentgen b75cab
	if (Y <= -1.8371976e19)
roentgen b75cab
		return (0xffff);
roentgen b75cab
	if (Y > 5.4136769e-20)
roentgen b75cab
		return itrunc(256.*(log2(Y) + 64.), em);
roentgen b75cab
	if (Y < -5.4136769e-20)
roentgen b75cab
		return (~0x7fff | itrunc(256.*(log2(-Y) + 64.), em));
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
L16toY(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	int16* l16 = (int16*) sp->tbuf;
roentgen b75cab
	float* yp = (float*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0)
roentgen b75cab
		*yp++ = (float)LogL16toY(*l16++);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
L16toGry(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	int16* l16 = (int16*) sp->tbuf;
roentgen b75cab
	uint8* gp = (uint8*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		double Y = LogL16toY(*l16++);
roentgen b75cab
		*gp++ = (uint8) ((Y <= 0.) ? 0 : (Y >= 1.) ? 255 : (int)(256.*sqrt(Y)));
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
L16fromY(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	int16* l16 = (int16*) sp->tbuf;
roentgen b75cab
	float* yp = (float*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0)
roentgen b75cab
		*l16++ = (int16) (LogL16fromY(*yp++, sp->encode_meth));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
void
roentgen b75cab
XYZtoRGB24(float xyz[3], uint8 rgb[3])
roentgen b75cab
{
roentgen b75cab
	double	r, g, b;
roentgen b75cab
					/* assume CCIR-709 primaries */
roentgen b75cab
	r =  2.690*xyz[0] + -1.276*xyz[1] + -0.414*xyz[2];
roentgen b75cab
	g = -1.022*xyz[0] +  1.978*xyz[1] +  0.044*xyz[2];
roentgen b75cab
	b =  0.061*xyz[0] + -0.224*xyz[1] +  1.163*xyz[2];
roentgen b75cab
					/* assume 2.0 gamma for speed */
roentgen b75cab
	/* could use integer sqrt approx., but this is probably faster */
roentgen b75cab
	rgb[0] = (uint8)((r<=0.) ? 0 : (r >= 1.) ? 255 : (int)(256.*sqrt(r)));
roentgen b75cab
	rgb[1] = (uint8)((g<=0.) ? 0 : (g >= 1.) ? 255 : (int)(256.*sqrt(g)));
roentgen b75cab
	rgb[2] = (uint8)((b<=0.) ? 0 : (b >= 1.) ? 255 : (int)(256.*sqrt(b)));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
double
roentgen b75cab
LogL10toY(int p10)		/* compute luminance from 10-bit LogL */
roentgen b75cab
{
roentgen b75cab
	if (p10 == 0)
roentgen b75cab
		return (0.);
roentgen b75cab
	return (exp(M_LN2/64.*(p10+.5) - M_LN2*12.));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
int
roentgen b75cab
LogL10fromY(double Y, int em)	/* get 10-bit LogL from Y */
roentgen b75cab
{
roentgen b75cab
	if (Y >= 15.742)
roentgen b75cab
		return (0x3ff);
roentgen b75cab
	else if (Y <= .00024283)
roentgen b75cab
		return (0);
roentgen b75cab
	else
roentgen b75cab
		return itrunc(64.*(log2(Y) + 12.), em);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define NANGLES		100
roentgen b75cab
#define uv2ang(u, v)	( (NANGLES*.499999999/M_PI) \
roentgen b75cab
				* atan2((v)-V_NEU,(u)-U_NEU) + .5*NANGLES )
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
oog_encode(double u, double v)		/* encode out-of-gamut chroma */
roentgen b75cab
{
roentgen b75cab
	static int	oog_table[NANGLES];
roentgen b75cab
	static int	initialized = 0;
roentgen b75cab
	register int	i;
roentgen b75cab
roentgen b75cab
	if (!initialized) {		/* set up perimeter table */
roentgen b75cab
		double	eps[NANGLES], ua, va, ang, epsa;
roentgen b75cab
		int	ui, vi, ustep;
roentgen b75cab
		for (i = NANGLES; i--; )
roentgen b75cab
			eps[i] = 2.;
roentgen b75cab
		for (vi = UV_NVS; vi--; ) {
roentgen b75cab
			va = UV_VSTART + (vi+.5)*UV_SQSIZ;
roentgen b75cab
			ustep = uv_row[vi].nus-1;
roentgen b75cab
			if (vi == UV_NVS-1 || vi == 0 || ustep <= 0)
roentgen b75cab
				ustep = 1;
roentgen b75cab
			for (ui = uv_row[vi].nus-1; ui >= 0; ui -= ustep) {
roentgen b75cab
				ua = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ;
roentgen b75cab
				ang = uv2ang(ua, va);
roentgen b75cab
				i = (int) ang;
roentgen b75cab
				epsa = fabs(ang - (i+.5));
roentgen b75cab
				if (epsa < eps[i]) {
roentgen b75cab
					oog_table[i] = uv_row[vi].ncum + ui;
roentgen b75cab
					eps[i] = epsa;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		for (i = NANGLES; i--; )	/* fill any holes */
roentgen b75cab
			if (eps[i] > 1.5) {
roentgen b75cab
				int	i1, i2;
roentgen b75cab
				for (i1 = 1; i1 < NANGLES/2; i1++)
roentgen b75cab
					if (eps[(i+i1)%NANGLES] < 1.5)
roentgen b75cab
						break;
roentgen b75cab
				for (i2 = 1; i2 < NANGLES/2; i2++)
roentgen b75cab
					if (eps[(i+NANGLES-i2)%NANGLES] < 1.5)
roentgen b75cab
						break;
roentgen b75cab
				if (i1 < i2)
roentgen b75cab
					oog_table[i] =
roentgen b75cab
						oog_table[(i+i1)%NANGLES];
roentgen b75cab
				else
roentgen b75cab
					oog_table[i] =
roentgen b75cab
						oog_table[(i+NANGLES-i2)%NANGLES];
roentgen b75cab
			}
roentgen b75cab
		initialized = 1;
roentgen b75cab
	}
roentgen b75cab
	i = (int) uv2ang(u, v);		/* look up hue angle */
roentgen b75cab
	return (oog_table[i]);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#undef uv2ang
roentgen b75cab
#undef NANGLES
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
int
roentgen b75cab
uv_encode(double u, double v, int em)	/* encode (u',v') coordinates */
roentgen b75cab
{
roentgen b75cab
	register int	vi, ui;
roentgen b75cab
roentgen b75cab
	if (v < UV_VSTART)
roentgen b75cab
		return oog_encode(u, v);
roentgen b75cab
	vi = itrunc((v - UV_VSTART)*(1./UV_SQSIZ), em);
roentgen b75cab
	if (vi >= UV_NVS)
roentgen b75cab
		return oog_encode(u, v);
roentgen b75cab
	if (u < uv_row[vi].ustart)
roentgen b75cab
		return oog_encode(u, v);
roentgen b75cab
	ui = itrunc((u - uv_row[vi].ustart)*(1./UV_SQSIZ), em);
roentgen b75cab
	if (ui >= uv_row[vi].nus)
roentgen b75cab
		return oog_encode(u, v);
roentgen b75cab
roentgen b75cab
	return (uv_row[vi].ncum + ui);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
int
roentgen b75cab
uv_decode(double *up, double *vp, int c)	/* decode (u',v') index */
roentgen b75cab
{
roentgen b75cab
	int	upper, lower;
roentgen b75cab
	register int	ui, vi;
roentgen b75cab
roentgen b75cab
	if (c < 0 || c >= UV_NDIVS)
roentgen b75cab
		return (-1);
roentgen b75cab
	lower = 0;				/* binary search */
roentgen b75cab
	upper = UV_NVS;
roentgen b75cab
	while (upper - lower > 1) {
roentgen b75cab
		vi = (lower + upper) >> 1;
roentgen b75cab
		ui = c - uv_row[vi].ncum;
roentgen b75cab
		if (ui > 0)
roentgen b75cab
			lower = vi;
roentgen b75cab
		else if (ui < 0)
roentgen b75cab
			upper = vi;
roentgen b75cab
		else {
roentgen b75cab
			lower = vi;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	vi = lower;
roentgen b75cab
	ui = c - uv_row[vi].ncum;
roentgen b75cab
	*up = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ;
roentgen b75cab
	*vp = UV_VSTART + (vi+.5)*UV_SQSIZ;
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
void
roentgen b75cab
LogLuv24toXYZ(uint32 p, float XYZ[3])
roentgen b75cab
{
roentgen b75cab
	int	Ce;
roentgen b75cab
	double	L, u, v, s, x, y;
roentgen b75cab
					/* decode luminance */
roentgen b75cab
	L = LogL10toY(p>>14 & 0x3ff);
roentgen b75cab
	if (L <= 0.) {
roentgen b75cab
		XYZ[0] = XYZ[1] = XYZ[2] = 0.;
roentgen b75cab
		return;
roentgen b75cab
	}
roentgen b75cab
					/* decode color */
roentgen b75cab
	Ce = p & 0x3fff;
roentgen b75cab
	if (uv_decode(&u, &v, Ce) < 0) {
roentgen b75cab
		u = U_NEU; v = V_NEU;
roentgen b75cab
	}
roentgen b75cab
	s = 1./(6.*u - 16.*v + 12.);
roentgen b75cab
	x = 9.*u * s;
roentgen b75cab
	y = 4.*v * s;
roentgen b75cab
					/* convert to XYZ */
roentgen b75cab
	XYZ[0] = (float)(x/y * L);
roentgen b75cab
	XYZ[1] = (float)L;
roentgen b75cab
	XYZ[2] = (float)((1.-x-y)/y * L);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
uint32
roentgen b75cab
LogLuv24fromXYZ(float XYZ[3], int em)
roentgen b75cab
{
roentgen b75cab
	int	Le, Ce;
roentgen b75cab
	double	u, v, s;
roentgen b75cab
					/* encode luminance */
roentgen b75cab
	Le = LogL10fromY(XYZ[1], em);
roentgen b75cab
					/* encode color */
roentgen b75cab
	s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
roentgen b75cab
	if (!Le || s <= 0.) {
roentgen b75cab
		u = U_NEU;
roentgen b75cab
		v = V_NEU;
roentgen b75cab
	} else {
roentgen b75cab
		u = 4.*XYZ[0] / s;
roentgen b75cab
		v = 9.*XYZ[1] / s;
roentgen b75cab
	}
roentgen b75cab
	Ce = uv_encode(u, v, em);
roentgen b75cab
	if (Ce < 0)			/* never happens */
roentgen b75cab
		Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
roentgen b75cab
					/* combine encodings */
roentgen b75cab
	return (Le << 14 | Ce);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv24toXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	float* xyz = (float*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		LogLuv24toXYZ(*luv, xyz);
roentgen b75cab
		xyz += 3;
roentgen b75cab
		luv++;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv24toLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	int16* luv3 = (int16*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		double u, v;
roentgen b75cab
roentgen b75cab
		*luv3++ = (int16)((*luv >> 12 & 0xffd) + 13314);
roentgen b75cab
		if (uv_decode(&u, &v, *luv&0x3fff) < 0) {
roentgen b75cab
			u = U_NEU;
roentgen b75cab
			v = V_NEU;
roentgen b75cab
		}
roentgen b75cab
		*luv3++ = (int16)(u * (1L<<15));
roentgen b75cab
		*luv3++ = (int16)(v * (1L<<15));
roentgen b75cab
		luv++;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv24toRGB(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	uint8* rgb = (uint8*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		float xyz[3];
roentgen b75cab
roentgen b75cab
		LogLuv24toXYZ(*luv++, xyz);
roentgen b75cab
		XYZtoRGB24(xyz, rgb);
roentgen b75cab
		rgb += 3;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv24fromXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	float* xyz = (float*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		*luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth);
roentgen b75cab
		xyz += 3;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv24fromLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	int16* luv3 = (int16*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		int Le, Ce;
roentgen b75cab
roentgen b75cab
		if (luv3[0] <= 0)
roentgen b75cab
			Le = 0;
roentgen b75cab
		else if (luv3[0] >= (1<<12)+3314)
roentgen b75cab
			Le = (1<<10) - 1;
roentgen b75cab
		else if (sp->encode_meth == SGILOGENCODE_NODITHER)
roentgen b75cab
			Le = (luv3[0]-3314) >> 2;
roentgen b75cab
		else
roentgen b75cab
			Le = itrunc(.25*(luv3[0]-3314.), sp->encode_meth);
roentgen b75cab
roentgen b75cab
		Ce = uv_encode((luv3[1]+.5)/(1<<15), (luv3[2]+.5)/(1<<15),
roentgen b75cab
					sp->encode_meth);
roentgen b75cab
		if (Ce < 0)	/* never happens */
roentgen b75cab
			Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
roentgen b75cab
		*luv++ = (uint32)Le << 14 | Ce;
roentgen b75cab
		luv3 += 3;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
void
roentgen b75cab
LogLuv32toXYZ(uint32 p, float XYZ[3])
roentgen b75cab
{
roentgen b75cab
	double	L, u, v, s, x, y;
roentgen b75cab
					/* decode luminance */
roentgen b75cab
	L = LogL16toY((int)p >> 16);
roentgen b75cab
	if (L <= 0.) {
roentgen b75cab
		XYZ[0] = XYZ[1] = XYZ[2] = 0.;
roentgen b75cab
		return;
roentgen b75cab
	}
roentgen b75cab
					/* decode color */
roentgen b75cab
	u = 1./UVSCALE * ((p>>8 & 0xff) + .5);
roentgen b75cab
	v = 1./UVSCALE * ((p & 0xff) + .5);
roentgen b75cab
	s = 1./(6.*u - 16.*v + 12.);
roentgen b75cab
	x = 9.*u * s;
roentgen b75cab
	y = 4.*v * s;
roentgen b75cab
					/* convert to XYZ */
roentgen b75cab
	XYZ[0] = (float)(x/y * L);
roentgen b75cab
	XYZ[1] = (float)L;
roentgen b75cab
	XYZ[2] = (float)((1.-x-y)/y * L);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if !LOGLUV_PUBLIC
roentgen b75cab
static
roentgen b75cab
#endif
roentgen b75cab
uint32
roentgen b75cab
LogLuv32fromXYZ(float XYZ[3], int em)
roentgen b75cab
{
roentgen b75cab
	unsigned int	Le, ue, ve;
roentgen b75cab
	double	u, v, s;
roentgen b75cab
					/* encode luminance */
roentgen b75cab
	Le = (unsigned int)LogL16fromY(XYZ[1], em);
roentgen b75cab
					/* encode color */
roentgen b75cab
	s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
roentgen b75cab
	if (!Le || s <= 0.) {
roentgen b75cab
		u = U_NEU;
roentgen b75cab
		v = V_NEU;
roentgen b75cab
	} else {
roentgen b75cab
		u = 4.*XYZ[0] / s;
roentgen b75cab
		v = 9.*XYZ[1] / s;
roentgen b75cab
	}
roentgen b75cab
	if (u <= 0.) ue = 0;
roentgen b75cab
	else ue = itrunc(UVSCALE*u, em);
roentgen b75cab
	if (ue > 255) ue = 255;
roentgen b75cab
	if (v <= 0.) ve = 0;
roentgen b75cab
	else ve = itrunc(UVSCALE*v, em);
roentgen b75cab
	if (ve > 255) ve = 255;
roentgen b75cab
					/* combine encodings */
roentgen b75cab
	return (Le << 16 | ue << 8 | ve);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv32toXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	float* xyz = (float*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		LogLuv32toXYZ(*luv++, xyz);
roentgen b75cab
		xyz += 3;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv32toLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	int16* luv3 = (int16*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		double u, v;
roentgen b75cab
roentgen b75cab
		*luv3++ = (int16)(*luv >> 16);
roentgen b75cab
		u = 1./UVSCALE * ((*luv>>8 & 0xff) + .5);
roentgen b75cab
		v = 1./UVSCALE * ((*luv & 0xff) + .5);
roentgen b75cab
		*luv3++ = (int16)(u * (1L<<15));
roentgen b75cab
		*luv3++ = (int16)(v * (1L<<15));
roentgen b75cab
		luv++;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv32toRGB(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	uint8* rgb = (uint8*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		float xyz[3];
roentgen b75cab
roentgen b75cab
		LogLuv32toXYZ(*luv++, xyz);
roentgen b75cab
		XYZtoRGB24(xyz, rgb);
roentgen b75cab
		rgb += 3;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv32fromXYZ(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;  
roentgen b75cab
	float* xyz = (float*) op;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		*luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth);
roentgen b75cab
		xyz += 3;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Luv32fromLuv48(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	uint32* luv = (uint32*) sp->tbuf;
roentgen b75cab
	int16* luv3 = (int16*) op;
roentgen b75cab
roentgen b75cab
	if (sp->encode_meth == SGILOGENCODE_NODITHER) {
roentgen b75cab
		while (n-- > 0) {
roentgen b75cab
			*luv++ = (uint32)luv3[0] << 16 |
roentgen b75cab
				(luv3[1]*(uint32)(UVSCALE+.5) >> 7 & 0xff00) |
roentgen b75cab
				(luv3[2]*(uint32)(UVSCALE+.5) >> 15 & 0xff);
roentgen b75cab
			luv3 += 3;
roentgen b75cab
		}
roentgen b75cab
		return;
roentgen b75cab
	}
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		*luv++ = (uint32)luv3[0] << 16 |
roentgen b75cab
	(itrunc(luv3[1]*(UVSCALE/(1<<15)), sp->encode_meth) << 8 & 0xff00) |
roentgen b75cab
		(itrunc(luv3[2]*(UVSCALE/(1<<15)), sp->encode_meth) & 0xff);
roentgen b75cab
		luv3 += 3;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
_logLuvNop(LogLuvState* sp, uint8* op, tmsize_t n)
roentgen b75cab
{
roentgen b75cab
	(void) sp; (void) op; (void) n;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
LogL16GuessDataFmt(TIFFDirectory *td)
roentgen b75cab
{
roentgen b75cab
#define	PACK(s,b,f)	(((b)<<6)|((s)<<3)|(f))
roentgen b75cab
	switch (PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat)) {
roentgen b75cab
	case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
roentgen b75cab
		return (SGILOGDATAFMT_FLOAT);
roentgen b75cab
	case PACK(1, 16, SAMPLEFORMAT_VOID):
roentgen b75cab
	case PACK(1, 16, SAMPLEFORMAT_INT):
roentgen b75cab
	case PACK(1, 16, SAMPLEFORMAT_UINT):
roentgen b75cab
		return (SGILOGDATAFMT_16BIT);
roentgen b75cab
	case PACK(1,  8, SAMPLEFORMAT_VOID):
roentgen b75cab
	case PACK(1,  8, SAMPLEFORMAT_UINT):
roentgen b75cab
		return (SGILOGDATAFMT_8BIT);
roentgen b75cab
	}
roentgen b75cab
#undef PACK
roentgen b75cab
	return (SGILOGDATAFMT_UNKNOWN);
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
LogL16InitState(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogL16InitState";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	LogLuvState* sp = DecoderState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(td->td_photometric == PHOTOMETRIC_LOGL);
roentgen b75cab
roentgen b75cab
	/* for some reason, we can't do this in TIFFInitLogL16 */
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
roentgen b75cab
		sp->user_datafmt = LogL16GuessDataFmt(td);
roentgen b75cab
	switch (sp->user_datafmt) {
roentgen b75cab
	case SGILOGDATAFMT_FLOAT:
roentgen b75cab
		sp->pixel_size = sizeof (float);
roentgen b75cab
		break;
roentgen b75cab
	case SGILOGDATAFMT_16BIT:
roentgen b75cab
		sp->pixel_size = sizeof (int16);
roentgen b75cab
		break;
roentgen b75cab
	case SGILOGDATAFMT_8BIT:
roentgen b75cab
		sp->pixel_size = sizeof (uint8);
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "No support for converting user data format to LogL");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
        if( isTiled(tif) )
roentgen b75cab
            sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
roentgen b75cab
        else
roentgen b75cab
            sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
roentgen b75cab
	if (multiply_ms(sp->tbuflen, sizeof (int16)) == 0 ||
roentgen b75cab
	    (sp->tbuf = (uint8*) _TIFFmalloc(sp->tbuflen * sizeof (int16))) == NULL) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "No space for SGILog translation buffer");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
LogLuvGuessDataFmt(TIFFDirectory *td)
roentgen b75cab
{
roentgen b75cab
	int guess;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * If the user didn't tell us their datafmt,
roentgen b75cab
	 * take our best guess from the bitspersample.
roentgen b75cab
	 */
roentgen b75cab
#define	PACK(a,b)	(((a)<<3)|(b))
roentgen b75cab
	switch (PACK(td->td_bitspersample, td->td_sampleformat)) {
roentgen b75cab
	case PACK(32, SAMPLEFORMAT_IEEEFP):
roentgen b75cab
		guess = SGILOGDATAFMT_FLOAT;
roentgen b75cab
		break;
roentgen b75cab
	case PACK(32, SAMPLEFORMAT_VOID):
roentgen b75cab
	case PACK(32, SAMPLEFORMAT_UINT):
roentgen b75cab
	case PACK(32, SAMPLEFORMAT_INT):
roentgen b75cab
		guess = SGILOGDATAFMT_RAW;
roentgen b75cab
		break;
roentgen b75cab
	case PACK(16, SAMPLEFORMAT_VOID):
roentgen b75cab
	case PACK(16, SAMPLEFORMAT_INT):
roentgen b75cab
	case PACK(16, SAMPLEFORMAT_UINT):
roentgen b75cab
		guess = SGILOGDATAFMT_16BIT;
roentgen b75cab
		break;
roentgen b75cab
	case PACK( 8, SAMPLEFORMAT_VOID):
roentgen b75cab
	case PACK( 8, SAMPLEFORMAT_UINT):
roentgen b75cab
		guess = SGILOGDATAFMT_8BIT;
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		guess = SGILOGDATAFMT_UNKNOWN;
roentgen b75cab
		break;
roentgen b75cab
#undef PACK
roentgen b75cab
	}
roentgen b75cab
	/*
roentgen b75cab
	 * Double-check samples per pixel.
roentgen b75cab
	 */
roentgen b75cab
	switch (td->td_samplesperpixel) {
roentgen b75cab
	case 1:
roentgen b75cab
		if (guess != SGILOGDATAFMT_RAW)
roentgen b75cab
			guess = SGILOGDATAFMT_UNKNOWN;
roentgen b75cab
		break;
roentgen b75cab
	case 3:
roentgen b75cab
		if (guess == SGILOGDATAFMT_RAW)
roentgen b75cab
			guess = SGILOGDATAFMT_UNKNOWN;
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		guess = SGILOGDATAFMT_UNKNOWN;
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	return (guess);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
LogLuvInitState(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogLuvInitState";
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
	LogLuvState* sp = DecoderState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
roentgen b75cab
roentgen b75cab
	/* for some reason, we can't do this in TIFFInitLogLuv */
roentgen b75cab
	if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "SGILog compression cannot handle non-contiguous data");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
roentgen b75cab
		sp->user_datafmt = LogLuvGuessDataFmt(td);
roentgen b75cab
	switch (sp->user_datafmt) {
roentgen b75cab
	case SGILOGDATAFMT_FLOAT:
roentgen b75cab
		sp->pixel_size = 3*sizeof (float);
roentgen b75cab
		break;
roentgen b75cab
	case SGILOGDATAFMT_16BIT:
roentgen b75cab
		sp->pixel_size = 3*sizeof (int16);
roentgen b75cab
		break;
roentgen b75cab
	case SGILOGDATAFMT_RAW:
roentgen b75cab
		sp->pixel_size = sizeof (uint32);
roentgen b75cab
		break;
roentgen b75cab
	case SGILOGDATAFMT_8BIT:
roentgen b75cab
		sp->pixel_size = 3*sizeof (uint8);
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "No support for converting user data format to LogLuv");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
        if( isTiled(tif) )
roentgen b75cab
            sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
roentgen b75cab
        else
roentgen b75cab
            sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
roentgen b75cab
	if (multiply_ms(sp->tbuflen, sizeof (uint32)) == 0 ||
roentgen b75cab
	    (sp->tbuf = (uint8*) _TIFFmalloc(sp->tbuflen * sizeof (uint32))) == NULL) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "No space for SGILog translation buffer");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
LogLuvFixupTags(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	(void) tif;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
LogLuvSetupDecode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogLuvSetupDecode";
roentgen b75cab
	LogLuvState* sp = DecoderState(tif);
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	tif->tif_postdecode = _TIFFNoPostDecode;
roentgen b75cab
	switch (td->td_photometric) {
roentgen b75cab
	case PHOTOMETRIC_LOGLUV:
roentgen b75cab
		if (!LogLuvInitState(tif))
roentgen b75cab
			break;
roentgen b75cab
		if (td->td_compression == COMPRESSION_SGILOG24) {
roentgen b75cab
			tif->tif_decoderow = LogLuvDecode24;
roentgen b75cab
			switch (sp->user_datafmt) {
roentgen b75cab
			case SGILOGDATAFMT_FLOAT:
roentgen b75cab
				sp->tfunc = Luv24toXYZ;  
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_16BIT:
roentgen b75cab
				sp->tfunc = Luv24toLuv48;  
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_8BIT:
roentgen b75cab
				sp->tfunc = Luv24toRGB;
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
		} else {
roentgen b75cab
			tif->tif_decoderow = LogLuvDecode32;
roentgen b75cab
			switch (sp->user_datafmt) {
roentgen b75cab
			case SGILOGDATAFMT_FLOAT:
roentgen b75cab
				sp->tfunc = Luv32toXYZ;
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_16BIT:
roentgen b75cab
				sp->tfunc = Luv32toLuv48;
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_8BIT:
roentgen b75cab
				sp->tfunc = Luv32toRGB;
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		return (1);
roentgen b75cab
	case PHOTOMETRIC_LOGL:
roentgen b75cab
		if (!LogL16InitState(tif))
roentgen b75cab
			break;
roentgen b75cab
		tif->tif_decoderow = LogL16Decode;
roentgen b75cab
		switch (sp->user_datafmt) {
roentgen b75cab
		case SGILOGDATAFMT_FLOAT:
roentgen b75cab
			sp->tfunc = L16toY;
roentgen b75cab
			break;
roentgen b75cab
		case SGILOGDATAFMT_8BIT:
roentgen b75cab
			sp->tfunc = L16toGry;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		return (1);
roentgen b75cab
	default:
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "Inappropriate photometric interpretation %d for SGILog compression; %s",
roentgen b75cab
		    td->td_photometric, "must be either LogLUV or LogL");
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
LogLuvSetupEncode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogLuvSetupEncode";
roentgen b75cab
	LogLuvState* sp = EncoderState(tif);
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	switch (td->td_photometric) {
roentgen b75cab
	case PHOTOMETRIC_LOGLUV:
roentgen b75cab
		if (!LogLuvInitState(tif))
roentgen b75cab
			break;
roentgen b75cab
		if (td->td_compression == COMPRESSION_SGILOG24) {
roentgen b75cab
			tif->tif_encoderow = LogLuvEncode24;
roentgen b75cab
			switch (sp->user_datafmt) {
roentgen b75cab
			case SGILOGDATAFMT_FLOAT:
roentgen b75cab
				sp->tfunc = Luv24fromXYZ;
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_16BIT:
roentgen b75cab
				sp->tfunc = Luv24fromLuv48;  
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_RAW:
roentgen b75cab
				break;
roentgen b75cab
			default:
roentgen b75cab
				goto notsupported;
roentgen b75cab
			}
roentgen b75cab
		} else {
roentgen b75cab
			tif->tif_encoderow = LogLuvEncode32;  
roentgen b75cab
			switch (sp->user_datafmt) {
roentgen b75cab
			case SGILOGDATAFMT_FLOAT:
roentgen b75cab
				sp->tfunc = Luv32fromXYZ;  
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_16BIT:
roentgen b75cab
				sp->tfunc = Luv32fromLuv48;  
roentgen b75cab
				break;
roentgen b75cab
			case SGILOGDATAFMT_RAW:
roentgen b75cab
				break;
roentgen b75cab
			default:
roentgen b75cab
				goto notsupported;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case PHOTOMETRIC_LOGL:
roentgen b75cab
		if (!LogL16InitState(tif))
roentgen b75cab
			break;
roentgen b75cab
		tif->tif_encoderow = LogL16Encode;  
roentgen b75cab
		switch (sp->user_datafmt) {
roentgen b75cab
		case SGILOGDATAFMT_FLOAT:
roentgen b75cab
			sp->tfunc = L16fromY;
roentgen b75cab
			break;
roentgen b75cab
		case SGILOGDATAFMT_16BIT:
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			goto notsupported;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "Inappropriate photometric interpretation %d for SGILog compression; %s",
roentgen b75cab
		    td->td_photometric, "must be either LogLUV or LogL");
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
notsupported:
roentgen b75cab
	TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
	    "SGILog compression supported only for %s, or raw data",
roentgen b75cab
	    td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
LogLuvClose(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * For consistency, we always want to write out the same
roentgen b75cab
	 * bitspersample and sampleformat for our TIFF file,
roentgen b75cab
	 * regardless of the data format being used by the application.
roentgen b75cab
	 * Since this routine is called after tags have been set but
roentgen b75cab
	 * before they have been recorded in the file, we reset them here.
roentgen b75cab
	 */
roentgen b75cab
	td->td_samplesperpixel =
roentgen b75cab
	    (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
roentgen b75cab
	td->td_bitspersample = 16;
roentgen b75cab
	td->td_sampleformat = SAMPLEFORMAT_INT;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
LogLuvCleanup(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	LogLuvState* sp = (LogLuvState *)tif->tif_data;
roentgen b75cab
roentgen b75cab
	assert(sp != 0);
roentgen b75cab
roentgen b75cab
	tif->tif_tagmethods.vgetfield = sp->vgetparent;
roentgen b75cab
	tif->tif_tagmethods.vsetfield = sp->vsetparent;
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
LogLuvVSetField(TIFF* tif, uint32 tag, va_list ap)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "LogLuvVSetField";
roentgen b75cab
	LogLuvState* sp = DecoderState(tif);
roentgen b75cab
	int bps, fmt;
roentgen b75cab
roentgen b75cab
	switch (tag) {
roentgen b75cab
	case TIFFTAG_SGILOGDATAFMT:
roentgen b75cab
		sp->user_datafmt = (int) va_arg(ap, int);
roentgen b75cab
		/*
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 SGILOGDATAFMT_FLOAT:
roentgen b75cab
			bps = 32, fmt = SAMPLEFORMAT_IEEEFP;
roentgen b75cab
			break;
roentgen b75cab
		case SGILOGDATAFMT_16BIT:
roentgen b75cab
			bps = 16, fmt = SAMPLEFORMAT_INT;
roentgen b75cab
			break;
roentgen b75cab
		case SGILOGDATAFMT_RAW:
roentgen b75cab
			bps = 32, fmt = SAMPLEFORMAT_UINT;
roentgen b75cab
			TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
roentgen b75cab
			break;
roentgen b75cab
		case SGILOGDATAFMT_8BIT:
roentgen b75cab
			bps = 8, fmt = SAMPLEFORMAT_UINT;
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
			    "Unknown data format %d for LogLuv compression",
roentgen b75cab
			    sp->user_datafmt);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
roentgen b75cab
		TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt);
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
		return (1);
roentgen b75cab
	case TIFFTAG_SGILOGENCODE:
roentgen b75cab
		sp->encode_meth = (int) va_arg(ap, int);
roentgen b75cab
		if (sp->encode_meth != SGILOGENCODE_NODITHER &&
roentgen b75cab
		    sp->encode_meth != SGILOGENCODE_RANDITHER) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Unknown encoding %d for LogLuv compression",
roentgen b75cab
			    sp->encode_meth);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		return (1);
roentgen b75cab
	default:
roentgen b75cab
		return (*sp->vsetparent)(tif, tag, ap);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
LogLuvVGetField(TIFF* tif, uint32 tag, va_list ap)
roentgen b75cab
{
roentgen b75cab
	LogLuvState *sp = (LogLuvState *)tif->tif_data;
roentgen b75cab
roentgen b75cab
	switch (tag) {
roentgen b75cab
	case TIFFTAG_SGILOGDATAFMT:
roentgen b75cab
		*va_arg(ap, int*) = sp->user_datafmt;
roentgen b75cab
		return (1);
roentgen b75cab
	default:
roentgen b75cab
		return (*sp->vgetparent)(tif, tag, ap);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static const TIFFField LogLuvFields[] = {
roentgen b75cab
    { TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogDataFmt", NULL},
roentgen b75cab
    { TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogEncode", NULL}
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFInitSGILog(TIFF* tif, int scheme)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFInitSGILog";
roentgen b75cab
	LogLuvState* sp;
roentgen b75cab
roentgen b75cab
	assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Merge codec-specific tag information.
roentgen b75cab
	 */
roentgen b75cab
	if (!_TIFFMergeFields(tif, LogLuvFields,
roentgen b75cab
			      TIFFArrayCount(LogLuvFields))) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "Merging SGILog 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 (LogLuvState));
roentgen b75cab
	if (tif->tif_data == NULL)
roentgen b75cab
		goto bad;
roentgen b75cab
	sp = (LogLuvState*) tif->tif_data;
roentgen b75cab
	_TIFFmemset((void*)sp, 0, sizeof (*sp));
roentgen b75cab
	sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
roentgen b75cab
	sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ?
roentgen b75cab
	    SGILOGENCODE_RANDITHER : SGILOGENCODE_NODITHER;
roentgen b75cab
	sp->tfunc = _logLuvNop;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Install codec methods.
roentgen b75cab
	 * NB: tif_decoderow & tif_encoderow are filled
roentgen b75cab
	 *     in at setup time.
roentgen b75cab
	 */
roentgen b75cab
	tif->tif_fixuptags = LogLuvFixupTags;  
roentgen b75cab
	tif->tif_setupdecode = LogLuvSetupDecode;
roentgen b75cab
	tif->tif_decodestrip = LogLuvDecodeStrip;
roentgen b75cab
	tif->tif_decodetile = LogLuvDecodeTile;
roentgen b75cab
	tif->tif_setupencode = LogLuvSetupEncode;
roentgen b75cab
	tif->tif_encodestrip = LogLuvEncodeStrip;  
roentgen b75cab
	tif->tif_encodetile = LogLuvEncodeTile;
roentgen b75cab
	tif->tif_close = LogLuvClose;
roentgen b75cab
	tif->tif_cleanup = LogLuvCleanup;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Override parent get/set field methods.
roentgen b75cab
	 */
roentgen b75cab
	sp->vgetparent = tif->tif_tagmethods.vgetfield;
roentgen b75cab
	tif->tif_tagmethods.vgetfield = LogLuvVGetField;   /* hook for codec tags */
roentgen b75cab
	sp->vsetparent = tif->tif_tagmethods.vsetfield;
roentgen b75cab
	tif->tif_tagmethods.vsetfield = LogLuvVSetField;   /* hook for codec tags */
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
bad:
roentgen b75cab
	TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		     "%s: No space for LogLuv state block", tif->tif_name);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
#endif /* LOGLUV_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
 */