kusano 7d535a
/* $Id: tif_aux.c,v 1.26 2010-07-01 15:33:28 dron Exp $ */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Copyright (c) 1991-1997 Sam Leffler
kusano 7d535a
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
kusano 7d535a
 *
kusano 7d535a
 * Permission to use, copy, modify, distribute, and sell this software and 
kusano 7d535a
 * its documentation for any purpose is hereby granted without fee, provided
kusano 7d535a
 * that (i) the above copyright notices and this permission notice appear in
kusano 7d535a
 * all copies of the software and related documentation, and (ii) the names of
kusano 7d535a
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
kusano 7d535a
 * publicity relating to the software without the specific, prior written
kusano 7d535a
 * permission of Sam Leffler and Silicon Graphics.
kusano 7d535a
 * 
kusano 7d535a
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
kusano 7d535a
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
kusano 7d535a
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
kusano 7d535a
 * 
kusano 7d535a
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
kusano 7d535a
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
kusano 7d535a
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
kusano 7d535a
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
kusano 7d535a
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
kusano 7d535a
 * OF THIS SOFTWARE.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * TIFF Library.
kusano 7d535a
 *
kusano 7d535a
 * Auxiliary Support Routines.
kusano 7d535a
 */
kusano 7d535a
#include "tiffiop.h"
kusano 7d535a
#include "tif_predict.h"
kusano 7d535a
#include <math.h></math.h>
kusano 7d535a
kusano 7d535a
uint32
kusano 7d535a
_TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
kusano 7d535a
{
kusano 7d535a
	uint32 bytes = first * second;
kusano 7d535a
kusano 7d535a
	if (second && bytes / second != first) {
kusano 7d535a
		TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
kusano 7d535a
		bytes = 0;
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
	return bytes;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
uint64
kusano 7d535a
_TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
kusano 7d535a
{
kusano 7d535a
	uint64 bytes = first * second;
kusano 7d535a
kusano 7d535a
	if (second && bytes / second != first) {
kusano 7d535a
		TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
kusano 7d535a
		bytes = 0;
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
	return bytes;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
void*
kusano 7d535a
_TIFFCheckRealloc(TIFF* tif, void* buffer,
kusano 7d535a
		  tmsize_t nmemb, tmsize_t elem_size, const char* what)
kusano 7d535a
{
kusano 7d535a
	void* cp = NULL;
kusano 7d535a
	tmsize_t bytes = nmemb * elem_size;
kusano 7d535a
kusano 7d535a
	/*
kusano 7d535a
	 * XXX: Check for integer overflow.
kusano 7d535a
	 */
kusano 7d535a
	if (nmemb && elem_size && bytes / elem_size == nmemb)
kusano 7d535a
		cp = _TIFFrealloc(buffer, bytes);
kusano 7d535a
kusano 7d535a
	if (cp == NULL) {
kusano 7d535a
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
kusano 7d535a
			     "Failed to allocate memory for %s "
kusano 7d535a
			     "(%ld elements of %ld bytes each)",
kusano 7d535a
			     what,(long) nmemb, (long) elem_size);
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
	return cp;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
void*
kusano 7d535a
_TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
kusano 7d535a
{
kusano 7d535a
	return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);  
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
TIFFDefaultTransferFunction(TIFFDirectory* td)
kusano 7d535a
{
kusano 7d535a
	uint16 **tf = td->td_transferfunction;
kusano 7d535a
	tmsize_t i, n, nbytes;
kusano 7d535a
kusano 7d535a
	tf[0] = tf[1] = tf[2] = 0;
kusano 7d535a
	if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
kusano 7d535a
		return 0;
kusano 7d535a
kusano 7d535a
	n = ((tmsize_t)1)<<td->td_bitspersample;</td->
kusano 7d535a
	nbytes = n * sizeof (uint16);
kusano 7d535a
	if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
kusano 7d535a
		return 0;
kusano 7d535a
	tf[0][0] = 0;
kusano 7d535a
	for (i = 1; i < n; i++) {
kusano 7d535a
		double t = (double)i/((double) n-1.);
kusano 7d535a
		tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
	if (td->td_samplesperpixel - td->td_extrasamples > 1) {
kusano 7d535a
		if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
kusano 7d535a
			goto bad;
kusano 7d535a
		_TIFFmemcpy(tf[1], tf[0], nbytes);
kusano 7d535a
		if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
kusano 7d535a
			goto bad;
kusano 7d535a
		_TIFFmemcpy(tf[2], tf[0], nbytes);
kusano 7d535a
	}
kusano 7d535a
	return 1;
kusano 7d535a
kusano 7d535a
bad:
kusano 7d535a
	if (tf[0])
kusano 7d535a
		_TIFFfree(tf[0]);
kusano 7d535a
	if (tf[1])
kusano 7d535a
		_TIFFfree(tf[1]);
kusano 7d535a
	if (tf[2])
kusano 7d535a
		_TIFFfree(tf[2]);
kusano 7d535a
	tf[0] = tf[1] = tf[2] = 0;
kusano 7d535a
	return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
TIFFDefaultRefBlackWhite(TIFFDirectory* td)
kusano 7d535a
{
kusano 7d535a
	int i;
kusano 7d535a
kusano 7d535a
	if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
kusano 7d535a
		return 0;
kusano 7d535a
        if (td->td_photometric == PHOTOMETRIC_YCBCR) {
kusano 7d535a
		/*
kusano 7d535a
		 * YCbCr (Class Y) images must have the ReferenceBlackWhite
kusano 7d535a
		 * tag set. Fix the broken images, which lacks that tag.
kusano 7d535a
		 */
kusano 7d535a
		td->td_refblackwhite[0] = 0.0F;
kusano 7d535a
		td->td_refblackwhite[1] = td->td_refblackwhite[3] =
kusano 7d535a
			td->td_refblackwhite[5] = 255.0F;
kusano 7d535a
		td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
kusano 7d535a
	} else {
kusano 7d535a
		/*
kusano 7d535a
		 * Assume RGB (Class R)
kusano 7d535a
		 */
kusano 7d535a
		for (i = 0; i < 3; i++) {
kusano 7d535a
		    td->td_refblackwhite[2*i+0] = 0;
kusano 7d535a
		    td->td_refblackwhite[2*i+1] =
kusano 7d535a
			    (float)((1L<<td->td_bitspersample)-1L);</td->
kusano 7d535a
		}
kusano 7d535a
	}
kusano 7d535a
	return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Like TIFFGetField, but return any default
kusano 7d535a
 * value if the tag is not present in the directory.
kusano 7d535a
 *
kusano 7d535a
 * NB:	We use the value in the directory, rather than
kusano 7d535a
 *	explcit values so that defaults exist only one
kusano 7d535a
 *	place in the library -- in TIFFDefaultDirectory.
kusano 7d535a
 */
kusano 7d535a
int
kusano 7d535a
TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
kusano 7d535a
{
kusano 7d535a
	TIFFDirectory *td = &tif->tif_dir;
kusano 7d535a
kusano 7d535a
	if (TIFFVGetField(tif, tag, ap))
kusano 7d535a
		return (1);
kusano 7d535a
	switch (tag) {
kusano 7d535a
	case TIFFTAG_SUBFILETYPE:
kusano 7d535a
		*va_arg(ap, uint32 *) = td->td_subfiletype;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_BITSPERSAMPLE:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_bitspersample;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_THRESHHOLDING:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_threshholding;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_FILLORDER:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_fillorder;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_ORIENTATION:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_orientation;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_SAMPLESPERPIXEL:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_samplesperpixel;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_ROWSPERSTRIP:
kusano 7d535a
		*va_arg(ap, uint32 *) = td->td_rowsperstrip;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_MINSAMPLEVALUE:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_minsamplevalue;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_MAXSAMPLEVALUE:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_maxsamplevalue;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_PLANARCONFIG:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_planarconfig;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_RESOLUTIONUNIT:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_resolutionunit;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_PREDICTOR:
kusano 7d535a
                {
kusano 7d535a
			TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
kusano 7d535a
			*va_arg(ap, uint16*) = (uint16) sp->predictor;
kusano 7d535a
			return 1;
kusano 7d535a
                }
kusano 7d535a
	case TIFFTAG_DOTRANGE:
kusano 7d535a
		*va_arg(ap, uint16 *) = 0;
kusano 7d535a
		*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;</td->
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_INKSET:
kusano 7d535a
		*va_arg(ap, uint16 *) = INKSET_CMYK;
kusano 7d535a
		return 1;
kusano 7d535a
	case TIFFTAG_NUMBEROFINKS:
kusano 7d535a
		*va_arg(ap, uint16 *) = 4;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_EXTRASAMPLES:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_extrasamples;
kusano 7d535a
		*va_arg(ap, uint16 **) = td->td_sampleinfo;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_MATTEING:
kusano 7d535a
		*va_arg(ap, uint16 *) =
kusano 7d535a
		    (td->td_extrasamples == 1 &&
kusano 7d535a
		     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_TILEDEPTH:
kusano 7d535a
		*va_arg(ap, uint32 *) = td->td_tiledepth;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_DATATYPE:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_sampleformat-1;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_SAMPLEFORMAT:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_sampleformat;
kusano 7d535a
                return(1);
kusano 7d535a
	case TIFFTAG_IMAGEDEPTH:
kusano 7d535a
		*va_arg(ap, uint32 *) = td->td_imagedepth;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_YCBCRCOEFFICIENTS:
kusano 7d535a
		{
kusano 7d535a
			/* defaults are from CCIR Recommendation 601-1 */
kusano 7d535a
			static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
kusano 7d535a
			*va_arg(ap, float **) = ycbcrcoeffs;
kusano 7d535a
			return 1;
kusano 7d535a
		}
kusano 7d535a
	case TIFFTAG_YCBCRSUBSAMPLING:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_YCBCRPOSITIONING:
kusano 7d535a
		*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_WHITEPOINT:
kusano 7d535a
		{
kusano 7d535a
			static float whitepoint[2];
kusano 7d535a
kusano 7d535a
			/* TIFF 6.0 specification tells that it is no default
kusano 7d535a
			   value for the WhitePoint, but AdobePhotoshop TIFF
kusano 7d535a
			   Technical Note tells that it should be CIE D50. */
kusano 7d535a
			whitepoint[0] =	D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
kusano 7d535a
			whitepoint[1] =	D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
kusano 7d535a
			*va_arg(ap, float **) = whitepoint;
kusano 7d535a
			return 1;
kusano 7d535a
		}
kusano 7d535a
	case TIFFTAG_TRANSFERFUNCTION:
kusano 7d535a
		if (!td->td_transferfunction[0] &&
kusano 7d535a
		    !TIFFDefaultTransferFunction(td)) {
kusano 7d535a
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
kusano 7d535a
			return (0);
kusano 7d535a
		}
kusano 7d535a
		*va_arg(ap, uint16 **) = td->td_transferfunction[0];
kusano 7d535a
		if (td->td_samplesperpixel - td->td_extrasamples > 1) {
kusano 7d535a
			*va_arg(ap, uint16 **) = td->td_transferfunction[1];
kusano 7d535a
			*va_arg(ap, uint16 **) = td->td_transferfunction[2];
kusano 7d535a
		}
kusano 7d535a
		return (1);
kusano 7d535a
	case TIFFTAG_REFERENCEBLACKWHITE:
kusano 7d535a
		if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
kusano 7d535a
			return (0);
kusano 7d535a
		*va_arg(ap, float **) = td->td_refblackwhite;
kusano 7d535a
		return (1);
kusano 7d535a
	}
kusano 7d535a
	return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Like TIFFGetField, but return any default
kusano 7d535a
 * value if the tag is not present in the directory.
kusano 7d535a
 */
kusano 7d535a
int
kusano 7d535a
TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
kusano 7d535a
{
kusano 7d535a
	int ok;
kusano 7d535a
	va_list ap;
kusano 7d535a
kusano 7d535a
	va_start(ap, tag);
kusano 7d535a
	ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
kusano 7d535a
	va_end(ap);
kusano 7d535a
	return (ok);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
struct _Int64Parts {
kusano 7d535a
	int32 low, high;
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
typedef union {
kusano 7d535a
	struct _Int64Parts part;
kusano 7d535a
	int64 value;
kusano 7d535a
} _Int64;
kusano 7d535a
kusano 7d535a
float
kusano 7d535a
_TIFFUInt64ToFloat(uint64 ui64)
kusano 7d535a
{
kusano 7d535a
	_Int64 i;
kusano 7d535a
kusano 7d535a
	i.value = ui64;
kusano 7d535a
	if (i.part.high >= 0) {
kusano 7d535a
		return (float)i.value;
kusano 7d535a
	} else {
kusano 7d535a
		long double df;
kusano 7d535a
		df = (long double)i.value;
kusano 7d535a
		df += 18446744073709551616.0; /* adding 2**64 */
kusano 7d535a
		return (float)df;
kusano 7d535a
	}
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
double
kusano 7d535a
_TIFFUInt64ToDouble(uint64 ui64)
kusano 7d535a
{
kusano 7d535a
	_Int64 i;
kusano 7d535a
kusano 7d535a
	i.value = ui64;
kusano 7d535a
	if (i.part.high >= 0) {
kusano 7d535a
		return (double)i.value;
kusano 7d535a
	} else {
kusano 7d535a
		long double df;
kusano 7d535a
		df = (long double)i.value;
kusano 7d535a
		df += 18446744073709551616.0; /* adding 2**64 */
kusano 7d535a
		return (double)df;
kusano 7d535a
	}
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/* vim: set ts=8 sts=8 sw=8 noet: */
kusano 7d535a
/*
kusano 7d535a
 * Local Variables:
kusano 7d535a
 * mode: c
kusano 7d535a
 * c-basic-offset: 8
kusano 7d535a
 * fill-column: 78
kusano 7d535a
 * End:
kusano 7d535a
 */