1 /* $Id$ */
2 
3 /*
4  * Copyright (c) 1991-1997 Sam Leffler
5  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26 
27 /*
28  * TIFF Library.
29  *
30  * Auxiliary Support Routines.
31  */
32 #include "tiffiop.h"
33 #include "tif_predict.h"
34 #include <math.h>
35 
36 uint32
_TIFFMultiply32(TIFF * tif,uint32 first,uint32 second,const char * where)37 _TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
38 {
39 	uint32 bytes = first * second;
40 
41 	if (second && bytes / second != first) {
42 		TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
43 		bytes = 0;
44 	}
45 
46 	return bytes;
47 }
48 
49 uint64
_TIFFMultiply64(TIFF * tif,uint64 first,uint64 second,const char * where)50 _TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
51 {
52 	uint64 bytes = first * second;
53 
54 	if (second && bytes / second != first) {
55 		TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
56 		bytes = 0;
57 	}
58 
59 	return bytes;
60 }
61 
62 void*
_TIFFCheckRealloc(TIFF * tif,void * buffer,tmsize_t nmemb,tmsize_t elem_size,const char * what)63 _TIFFCheckRealloc(TIFF* tif, void* buffer,
64 		  tmsize_t nmemb, tmsize_t elem_size, const char* what)
65 {
66 	void* cp = NULL;
67 	tmsize_t bytes = nmemb * elem_size;
68 
69 	/*
70 	 * XXX: Check for integer overflow.
71 	 */
72 	if (nmemb && elem_size && bytes / elem_size == nmemb)
73 		cp = _TIFFrealloc(buffer, bytes);
74 
75 	if (cp == NULL) {
76 		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
77 			     "Failed to allocate memory for %s "
78 			     "(%ld elements of %ld bytes each)",
79 			     what,(long) nmemb, (long) elem_size);
80 	}
81 
82 	return cp;
83 }
84 
85 void*
_TIFFCheckMalloc(TIFF * tif,tmsize_t nmemb,tmsize_t elem_size,const char * what)86 _TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
87 {
88 	return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
89 }
90 
91 static int
TIFFDefaultTransferFunction(TIFFDirectory * td)92 TIFFDefaultTransferFunction(TIFFDirectory* td)
93 {
94 	uint16 **tf = td->td_transferfunction;
95 	tmsize_t i, n, nbytes;
96 
97 	tf[0] = tf[1] = tf[2] = 0;
98 	if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
99 		return 0;
100 
101 	n = ((tmsize_t)1)<<td->td_bitspersample;
102 	nbytes = n * sizeof (uint16);
103         tf[0] = (uint16 *)_TIFFmalloc(nbytes);
104 	if (tf[0] == NULL)
105 		return 0;
106 	tf[0][0] = 0;
107 	for (i = 1; i < n; i++) {
108 		double t = (double)i/((double) n-1.);
109 		tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
110 	}
111 
112 	if (td->td_samplesperpixel - td->td_extrasamples > 1) {
113                 tf[1] = (uint16 *)_TIFFmalloc(nbytes);
114 		if(tf[1] == NULL)
115 			goto bad;
116 		_TIFFmemcpy(tf[1], tf[0], nbytes);
117                 tf[2] = (uint16 *)_TIFFmalloc(nbytes);
118 		if (tf[2] == NULL)
119 			goto bad;
120 		_TIFFmemcpy(tf[2], tf[0], nbytes);
121 	}
122 	return 1;
123 
124 bad:
125 	if (tf[0])
126 		_TIFFfree(tf[0]);
127 	if (tf[1])
128 		_TIFFfree(tf[1]);
129 	if (tf[2])
130 		_TIFFfree(tf[2]);
131 	tf[0] = tf[1] = tf[2] = 0;
132 	return 0;
133 }
134 
135 static int
TIFFDefaultRefBlackWhite(TIFFDirectory * td)136 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
137 {
138 	int i;
139 
140         td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float));
141 	if (td->td_refblackwhite == NULL)
142 		return 0;
143         if (td->td_photometric == PHOTOMETRIC_YCBCR) {
144 		/*
145 		 * YCbCr (Class Y) images must have the ReferenceBlackWhite
146 		 * tag set. Fix the broken images, which lacks that tag.
147 		 */
148 		td->td_refblackwhite[0] = 0.0F;
149 		td->td_refblackwhite[1] = td->td_refblackwhite[3] =
150 			td->td_refblackwhite[5] = 255.0F;
151 		td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
152 	} else {
153 		/*
154 		 * Assume RGB (Class R)
155 		 */
156 		for (i = 0; i < 3; i++) {
157 		    td->td_refblackwhite[2*i+0] = 0;
158 		    td->td_refblackwhite[2*i+1] =
159 			    (float)((1L<<td->td_bitspersample)-1L);
160 		}
161 	}
162 	return 1;
163 }
164 
165 /*
166  * Like TIFFGetField, but return any default
167  * value if the tag is not present in the directory.
168  *
169  * NB:	We use the value in the directory, rather than
170  *	explicit values so that defaults exist only one
171  *	place in the library -- in TIFFDefaultDirectory.
172  */
173 int
TIFFVGetFieldDefaulted(TIFF * tif,uint32 tag,va_list ap)174 TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
175 {
176 	TIFFDirectory *td = &tif->tif_dir;
177 
178 	if (TIFFVGetField(tif, tag, ap))
179 		return (1);
180 	switch (tag) {
181 	case TIFFTAG_SUBFILETYPE:
182 		*va_arg(ap, uint32 *) = td->td_subfiletype;
183 		return (1);
184 	case TIFFTAG_BITSPERSAMPLE:
185 		*va_arg(ap, uint16 *) = td->td_bitspersample;
186 		return (1);
187 	case TIFFTAG_THRESHHOLDING:
188 		*va_arg(ap, uint16 *) = td->td_threshholding;
189 		return (1);
190 	case TIFFTAG_FILLORDER:
191 		*va_arg(ap, uint16 *) = td->td_fillorder;
192 		return (1);
193 	case TIFFTAG_ORIENTATION:
194 		*va_arg(ap, uint16 *) = td->td_orientation;
195 		return (1);
196 	case TIFFTAG_SAMPLESPERPIXEL:
197 		*va_arg(ap, uint16 *) = td->td_samplesperpixel;
198 		return (1);
199 	case TIFFTAG_ROWSPERSTRIP:
200 		*va_arg(ap, uint32 *) = td->td_rowsperstrip;
201 		return (1);
202 	case TIFFTAG_MINSAMPLEVALUE:
203 		*va_arg(ap, uint16 *) = td->td_minsamplevalue;
204 		return (1);
205 	case TIFFTAG_MAXSAMPLEVALUE:
206 		*va_arg(ap, uint16 *) = td->td_maxsamplevalue;
207 		return (1);
208 	case TIFFTAG_PLANARCONFIG:
209 		*va_arg(ap, uint16 *) = td->td_planarconfig;
210 		return (1);
211 	case TIFFTAG_RESOLUTIONUNIT:
212 		*va_arg(ap, uint16 *) = td->td_resolutionunit;
213 		return (1);
214 	case TIFFTAG_PREDICTOR:
215                 {
216 			TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
217 			*va_arg(ap, uint16*) = (uint16) sp->predictor;
218 			return 1;
219                 }
220 	case TIFFTAG_DOTRANGE:
221 		*va_arg(ap, uint16 *) = 0;
222 		*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
223 		return (1);
224 	case TIFFTAG_INKSET:
225 		*va_arg(ap, uint16 *) = INKSET_CMYK;
226 		return 1;
227 	case TIFFTAG_NUMBEROFINKS:
228 		*va_arg(ap, uint16 *) = 4;
229 		return (1);
230 	case TIFFTAG_EXTRASAMPLES:
231 		*va_arg(ap, uint16 *) = td->td_extrasamples;
232 		*va_arg(ap, uint16 **) = td->td_sampleinfo;
233 		return (1);
234 	case TIFFTAG_MATTEING:
235 		*va_arg(ap, uint16 *) =
236 		    (td->td_extrasamples == 1 &&
237 		     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
238 		return (1);
239 	case TIFFTAG_TILEDEPTH:
240 		*va_arg(ap, uint32 *) = td->td_tiledepth;
241 		return (1);
242 	case TIFFTAG_DATATYPE:
243 		*va_arg(ap, uint16 *) = td->td_sampleformat-1;
244 		return (1);
245 	case TIFFTAG_SAMPLEFORMAT:
246 		*va_arg(ap, uint16 *) = td->td_sampleformat;
247                 return(1);
248 	case TIFFTAG_IMAGEDEPTH:
249 		*va_arg(ap, uint32 *) = td->td_imagedepth;
250 		return (1);
251 	case TIFFTAG_YCBCRCOEFFICIENTS:
252 		{
253 			/* defaults are from CCIR Recommendation 601-1 */
254 			static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
255 			*va_arg(ap, float **) = ycbcrcoeffs;
256 			return 1;
257 		}
258 	case TIFFTAG_YCBCRSUBSAMPLING:
259 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
260 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
261 		return (1);
262 	case TIFFTAG_YCBCRPOSITIONING:
263 		*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
264 		return (1);
265 	case TIFFTAG_WHITEPOINT:
266 		{
267 			static float whitepoint[2];
268 
269 			/* TIFF 6.0 specification tells that it is no default
270 			   value for the WhitePoint, but AdobePhotoshop TIFF
271 			   Technical Note tells that it should be CIE D50. */
272 			whitepoint[0] =	D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
273 			whitepoint[1] =	D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
274 			*va_arg(ap, float **) = whitepoint;
275 			return 1;
276 		}
277 	case TIFFTAG_TRANSFERFUNCTION:
278 		if (!td->td_transferfunction[0] &&
279 		    !TIFFDefaultTransferFunction(td)) {
280 			TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
281 			return (0);
282 		}
283 		*va_arg(ap, uint16 **) = td->td_transferfunction[0];
284 		if (td->td_samplesperpixel - td->td_extrasamples > 1) {
285 			*va_arg(ap, uint16 **) = td->td_transferfunction[1];
286 			*va_arg(ap, uint16 **) = td->td_transferfunction[2];
287 		}
288 		return (1);
289 	case TIFFTAG_REFERENCEBLACKWHITE:
290 		if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
291 			return (0);
292 		*va_arg(ap, float **) = td->td_refblackwhite;
293 		return (1);
294 	}
295 	return 0;
296 }
297 
298 /*
299  * Like TIFFGetField, but return any default
300  * value if the tag is not present in the directory.
301  */
302 int
TIFFGetFieldDefaulted(TIFF * tif,uint32 tag,...)303 TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
304 {
305 	int ok;
306 	va_list ap;
307 
308 	va_start(ap, tag);
309 	ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
310 	va_end(ap);
311 	return (ok);
312 }
313 
314 struct _Int64Parts {
315 	int32 low, high;
316 };
317 
318 typedef union {
319 	struct _Int64Parts part;
320 	int64 value;
321 } _Int64;
322 
323 float
_TIFFUInt64ToFloat(uint64 ui64)324 _TIFFUInt64ToFloat(uint64 ui64)
325 {
326 	_Int64 i;
327 
328 	i.value = ui64;
329 	if (i.part.high >= 0) {
330 		return (float)i.value;
331 	} else {
332 		long double df;
333 		df = (long double)i.value;
334 		df += 18446744073709551616.0; /* adding 2**64 */
335 		return (float)df;
336 	}
337 }
338 
339 double
_TIFFUInt64ToDouble(uint64 ui64)340 _TIFFUInt64ToDouble(uint64 ui64)
341 {
342 	_Int64 i;
343 
344 	i.value = ui64;
345 	if (i.part.high >= 0) {
346 		return (double)i.value;
347 	} else {
348 		long double df;
349 		df = (long double)i.value;
350 		df += 18446744073709551616.0; /* adding 2**64 */
351 		return (double)df;
352 	}
353 }
354 
355 /* vim: set ts=8 sts=8 sw=8 noet: */
356 /*
357  * Local Variables:
358  * mode: c
359  * c-basic-offset: 8
360  * fill-column: 78
361  * End:
362  */
363