roentgen b75cab
/* $Id: tif_aux.c,v 1.26 2010-07-01 15:33:28 dron Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1991-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-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 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 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 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
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 *
roentgen b75cab
 * Auxiliary Support Routines.
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
#include "tif_predict.h"
roentgen b75cab
#include <math.h></math.h>
roentgen b75cab
roentgen b75cab
uint32
roentgen b75cab
_TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
roentgen b75cab
{
roentgen b75cab
	uint32 bytes = first * second;
roentgen b75cab
roentgen b75cab
	if (second && bytes / second != first) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
roentgen b75cab
		bytes = 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return bytes;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
uint64
roentgen b75cab
_TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
roentgen b75cab
{
roentgen b75cab
	uint64 bytes = first * second;
roentgen b75cab
roentgen b75cab
	if (second && bytes / second != first) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
roentgen b75cab
		bytes = 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return bytes;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void*
roentgen b75cab
_TIFFCheckRealloc(TIFF* tif, void* buffer,
roentgen b75cab
		  tmsize_t nmemb, tmsize_t elem_size, const char* what)
roentgen b75cab
{
roentgen b75cab
	void* cp = NULL;
roentgen b75cab
	tmsize_t bytes = nmemb * elem_size;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * XXX: Check for integer overflow.
roentgen b75cab
	 */
roentgen b75cab
	if (nmemb && elem_size && bytes / elem_size == nmemb)
roentgen b75cab
		cp = _TIFFrealloc(buffer, bytes);
roentgen b75cab
roentgen b75cab
	if (cp == NULL) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
			     "Failed to allocate memory for %s "
roentgen b75cab
			     "(%ld elements of %ld bytes each)",
roentgen b75cab
			     what,(long) nmemb, (long) elem_size);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return cp;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void*
roentgen b75cab
_TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
roentgen b75cab
{
roentgen b75cab
	return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);  
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFDefaultTransferFunction(TIFFDirectory* td)
roentgen b75cab
{
roentgen b75cab
	uint16 **tf = td->td_transferfunction;
roentgen b75cab
	tmsize_t i, n, nbytes;
roentgen b75cab
roentgen b75cab
	tf[0] = tf[1] = tf[2] = 0;
roentgen b75cab
	if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
roentgen b75cab
		return 0;
roentgen b75cab
roentgen b75cab
	n = ((tmsize_t)1)<<td->td_bitspersample;</td->
roentgen b75cab
	nbytes = n * sizeof (uint16);
roentgen b75cab
	if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
roentgen b75cab
		return 0;
roentgen b75cab
	tf[0][0] = 0;
roentgen b75cab
	for (i = 1; i < n; i++) {
roentgen b75cab
		double t = (double)i/((double) n-1.);
roentgen b75cab
		tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (td->td_samplesperpixel - td->td_extrasamples > 1) {
roentgen b75cab
		if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
roentgen b75cab
			goto bad;
roentgen b75cab
		_TIFFmemcpy(tf[1], tf[0], nbytes);
roentgen b75cab
		if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
roentgen b75cab
			goto bad;
roentgen b75cab
		_TIFFmemcpy(tf[2], tf[0], nbytes);
roentgen b75cab
	}
roentgen b75cab
	return 1;
roentgen b75cab
roentgen b75cab
bad:
roentgen b75cab
	if (tf[0])
roentgen b75cab
		_TIFFfree(tf[0]);
roentgen b75cab
	if (tf[1])
roentgen b75cab
		_TIFFfree(tf[1]);
roentgen b75cab
	if (tf[2])
roentgen b75cab
		_TIFFfree(tf[2]);
roentgen b75cab
	tf[0] = tf[1] = tf[2] = 0;
roentgen b75cab
	return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFDefaultRefBlackWhite(TIFFDirectory* td)
roentgen b75cab
{
roentgen b75cab
	int i;
roentgen b75cab
roentgen b75cab
	if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
roentgen b75cab
		return 0;
roentgen b75cab
        if (td->td_photometric == PHOTOMETRIC_YCBCR) {
roentgen b75cab
		/*
roentgen b75cab
		 * YCbCr (Class Y) images must have the ReferenceBlackWhite
roentgen b75cab
		 * tag set. Fix the broken images, which lacks that tag.
roentgen b75cab
		 */
roentgen b75cab
		td->td_refblackwhite[0] = 0.0F;
roentgen b75cab
		td->td_refblackwhite[1] = td->td_refblackwhite[3] =
roentgen b75cab
			td->td_refblackwhite[5] = 255.0F;
roentgen b75cab
		td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
roentgen b75cab
	} else {
roentgen b75cab
		/*
roentgen b75cab
		 * Assume RGB (Class R)
roentgen b75cab
		 */
roentgen b75cab
		for (i = 0; i < 3; i++) {
roentgen b75cab
		    td->td_refblackwhite[2*i+0] = 0;
roentgen b75cab
		    td->td_refblackwhite[2*i+1] =
roentgen b75cab
			    (float)((1L<<td->td_bitspersample)-1L);</td->
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Like TIFFGetField, but return any default
roentgen b75cab
 * value if the tag is not present in the directory.
roentgen b75cab
 *
roentgen b75cab
 * NB:	We use the value in the directory, rather than
roentgen b75cab
 *	explcit values so that defaults exist only one
roentgen b75cab
 *	place in the library -- in TIFFDefaultDirectory.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	if (TIFFVGetField(tif, tag, ap))
roentgen b75cab
		return (1);
roentgen b75cab
	switch (tag) {
roentgen b75cab
	case TIFFTAG_SUBFILETYPE:
roentgen b75cab
		*va_arg(ap, uint32 *) = td->td_subfiletype;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_BITSPERSAMPLE:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_bitspersample;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_THRESHHOLDING:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_threshholding;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_FILLORDER:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_fillorder;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_ORIENTATION:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_orientation;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_SAMPLESPERPIXEL:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_samplesperpixel;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_ROWSPERSTRIP:
roentgen b75cab
		*va_arg(ap, uint32 *) = td->td_rowsperstrip;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_MINSAMPLEVALUE:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_minsamplevalue;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_MAXSAMPLEVALUE:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_maxsamplevalue;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_PLANARCONFIG:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_planarconfig;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_RESOLUTIONUNIT:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_resolutionunit;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_PREDICTOR:
roentgen b75cab
                {
roentgen b75cab
			TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
roentgen b75cab
			*va_arg(ap, uint16*) = (uint16) sp->predictor;
roentgen b75cab
			return 1;
roentgen b75cab
                }
roentgen b75cab
	case TIFFTAG_DOTRANGE:
roentgen b75cab
		*va_arg(ap, uint16 *) = 0;
roentgen b75cab
		*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;</td->
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_INKSET:
roentgen b75cab
		*va_arg(ap, uint16 *) = INKSET_CMYK;
roentgen b75cab
		return 1;
roentgen b75cab
	case TIFFTAG_NUMBEROFINKS:
roentgen b75cab
		*va_arg(ap, uint16 *) = 4;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_EXTRASAMPLES:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_extrasamples;
roentgen b75cab
		*va_arg(ap, uint16 **) = td->td_sampleinfo;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_MATTEING:
roentgen b75cab
		*va_arg(ap, uint16 *) =
roentgen b75cab
		    (td->td_extrasamples == 1 &&
roentgen b75cab
		     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_TILEDEPTH:
roentgen b75cab
		*va_arg(ap, uint32 *) = td->td_tiledepth;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_DATATYPE:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_sampleformat-1;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_SAMPLEFORMAT:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_sampleformat;
roentgen b75cab
                return(1);
roentgen b75cab
	case TIFFTAG_IMAGEDEPTH:
roentgen b75cab
		*va_arg(ap, uint32 *) = td->td_imagedepth;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_YCBCRCOEFFICIENTS:
roentgen b75cab
		{
roentgen b75cab
			/* defaults are from CCIR Recommendation 601-1 */
roentgen b75cab
			static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
roentgen b75cab
			*va_arg(ap, float **) = ycbcrcoeffs;
roentgen b75cab
			return 1;
roentgen b75cab
		}
roentgen b75cab
	case TIFFTAG_YCBCRSUBSAMPLING:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_YCBCRPOSITIONING:
roentgen b75cab
		*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_WHITEPOINT:
roentgen b75cab
		{
roentgen b75cab
			static float whitepoint[2];
roentgen b75cab
roentgen b75cab
			/* TIFF 6.0 specification tells that it is no default
roentgen b75cab
			   value for the WhitePoint, but AdobePhotoshop TIFF
roentgen b75cab
			   Technical Note tells that it should be CIE D50. */
roentgen b75cab
			whitepoint[0] =	D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
roentgen b75cab
			whitepoint[1] =	D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
roentgen b75cab
			*va_arg(ap, float **) = whitepoint;
roentgen b75cab
			return 1;
roentgen b75cab
		}
roentgen b75cab
	case TIFFTAG_TRANSFERFUNCTION:
roentgen b75cab
		if (!td->td_transferfunction[0] &&
roentgen b75cab
		    !TIFFDefaultTransferFunction(td)) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		*va_arg(ap, uint16 **) = td->td_transferfunction[0];
roentgen b75cab
		if (td->td_samplesperpixel - td->td_extrasamples > 1) {
roentgen b75cab
			*va_arg(ap, uint16 **) = td->td_transferfunction[1];
roentgen b75cab
			*va_arg(ap, uint16 **) = td->td_transferfunction[2];
roentgen b75cab
		}
roentgen b75cab
		return (1);
roentgen b75cab
	case TIFFTAG_REFERENCEBLACKWHITE:
roentgen b75cab
		if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
roentgen b75cab
			return (0);
roentgen b75cab
		*va_arg(ap, float **) = td->td_refblackwhite;
roentgen b75cab
		return (1);
roentgen b75cab
	}
roentgen b75cab
	return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Like TIFFGetField, but return any default
roentgen b75cab
 * value if the tag is not present in the directory.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
roentgen b75cab
{
roentgen b75cab
	int ok;
roentgen b75cab
	va_list ap;
roentgen b75cab
roentgen b75cab
	va_start(ap, tag);
roentgen b75cab
	ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
roentgen b75cab
	va_end(ap);
roentgen b75cab
	return (ok);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
struct _Int64Parts {
roentgen b75cab
	int32 low, high;
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
typedef union {
roentgen b75cab
	struct _Int64Parts part;
roentgen b75cab
	int64 value;
roentgen b75cab
} _Int64;
roentgen b75cab
roentgen b75cab
float
roentgen b75cab
_TIFFUInt64ToFloat(uint64 ui64)
roentgen b75cab
{
roentgen b75cab
	_Int64 i;
roentgen b75cab
roentgen b75cab
	i.value = ui64;
roentgen b75cab
	if (i.part.high >= 0) {
roentgen b75cab
		return (float)i.value;
roentgen b75cab
	} else {
roentgen b75cab
		long double df;
roentgen b75cab
		df = (long double)i.value;
roentgen b75cab
		df += 18446744073709551616.0; /* adding 2**64 */
roentgen b75cab
		return (float)df;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
double
roentgen b75cab
_TIFFUInt64ToDouble(uint64 ui64)
roentgen b75cab
{
roentgen b75cab
	_Int64 i;
roentgen b75cab
roentgen b75cab
	i.value = ui64;
roentgen b75cab
	if (i.part.high >= 0) {
roentgen b75cab
		return (double)i.value;
roentgen b75cab
	} else {
roentgen b75cab
		long double df;
roentgen b75cab
		df = (long double)i.value;
roentgen b75cab
		df += 18446744073709551616.0; /* adding 2**64 */
roentgen b75cab
		return (double)df;
roentgen b75cab
	}
roentgen b75cab
}
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
 */