1 /* PDFlib GmbH cvsid: $Id: tif_auxx.c,v 1.8.2.1 2006/12/27 15:03:32 rjs Exp $ */
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 tdata_t
_TIFFCheckMalloc(TIFF * tif,size_t nmemb,size_t elem_size,const char * what)37 _TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
38 {
39         tdata_t *cp = NULL;
40         tsize_t bytes = nmemb * elem_size;
41 
42         /*
43          * XXX: Check for integer overflow.
44          */
45         if (nmemb && elem_size && bytes / elem_size == nmemb)
46                 cp = (tdata_t *)_TIFFmalloc(bytes);
47 
48         if (cp == NULL)
49                 _TIFFError(tif, tif->tif_name, "No space %s", what);
50 
51         return (cp);
52 }
53 
54 static int
TIFFDefaultTransferFunction(TIFF * tif,TIFFDirectory * td)55 TIFFDefaultTransferFunction(TIFF* tif, TIFFDirectory* td)
56 {
57 	uint16 **tf = td->td_transferfunction;
58 	tsize_t i, n, nbytes;
59 
60  	tf[0] = tf[1] = tf[2] = 0;
61  	if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2)
62  		return 0;
63 
64  	n = 1<<td->td_bitspersample;
65  	nbytes = n * sizeof (uint16);
66  	if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
67  		return 0;
68 	tf[0][0] = 0;
69 	for (i = 1; i < n; i++) {
70 		double t = (double)i/((double) n-1.);
71 		tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
72 	}
73 
74 	if (td->td_samplesperpixel - td->td_extrasamples > 1) {
75  		if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
76  			goto bad;
77  		_TIFFmemcpy(tf[1], tf[0], nbytes);
78  		if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
79  			goto bad;
80  		_TIFFmemcpy(tf[2], tf[0], nbytes);
81 	}
82  	return 1;
83 
84  bad:
85  	if (tf[0])
86  		_TIFFfree(tf[0]);
87  	if (tf[1])
88  		_TIFFfree(tf[1]);
89  	if (tf[2])
90  		_TIFFfree(tf[2]);
91  	tf[0] = tf[1] = tf[2] = 0;
92  	return 0;
93 }
94 
95 static int
TIFFDefaultRefBlackWhite(TIFF * tif,TIFFDirectory * td)96 TIFFDefaultRefBlackWhite(TIFF *tif, TIFFDirectory* td)
97 {
98         int i;
99 
100         if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
101                 return 0;
102         if (td->td_photometric == PHOTOMETRIC_YCBCR) {
103                 /*
104                  * YCbCr (Class Y) images must have the ReferenceBlackWhite
105                  * tag set. Fix the broken images, which lacks that tag.
106                  */
107                 td->td_refblackwhite[0] = 0.0F;
108                 td->td_refblackwhite[1] = td->td_refblackwhite[3] =
109                         td->td_refblackwhite[5] = 255.0F;
110                 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
111         } else {
112                 /*
113                  * Assume RGB (Class R)
114                  */
115                 for (i = 0; i < 3; i++) {
116                     td->td_refblackwhite[2*i+0] = 0;
117                     td->td_refblackwhite[2*i+1] =
118                             (float)((1L<<td->td_bitspersample)-1L);
119                 }
120         }
121         return 1;
122 }
123 
124 
125 /*
126  * Like TIFFGetField, but return any default
127  * value if the tag is not present in the directory.
128  *
129  * NB:	We use the value in the directory, rather than
130  *	explcit values so that defaults exist only one
131  *	place in the library -- in TIFFDefaultDirectory.
132  */
133 int
TIFFVGetFieldDefaulted(TIFF * tif,ttag_t tag,va_list ap)134 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
135 {
136 	TIFFDirectory *td = &tif->tif_dir;
137 
138 	if (TIFFVGetField(tif, tag, ap))
139 		return (1);
140 	switch (tag) {
141 	case TIFFTAG_SUBFILETYPE:
142 		*va_arg(ap, uint32 *) = td->td_subfiletype;
143 		return (1);
144 	case TIFFTAG_BITSPERSAMPLE:
145 		*va_arg(ap, uint16 *) = td->td_bitspersample;
146 		return (1);
147 	case TIFFTAG_THRESHHOLDING:
148 		*va_arg(ap, uint16 *) = td->td_threshholding;
149 		return (1);
150 	case TIFFTAG_FILLORDER:
151 		*va_arg(ap, uint16 *) = td->td_fillorder;
152 		return (1);
153 	case TIFFTAG_ORIENTATION:
154 		*va_arg(ap, uint16 *) = td->td_orientation;
155 		return (1);
156 	case TIFFTAG_SAMPLESPERPIXEL:
157 		*va_arg(ap, uint16 *) = td->td_samplesperpixel;
158 		return (1);
159 	case TIFFTAG_ROWSPERSTRIP:
160 		*va_arg(ap, uint32 *) = td->td_rowsperstrip;
161 		return (1);
162 	case TIFFTAG_MINSAMPLEVALUE:
163 		*va_arg(ap, uint16 *) = td->td_minsamplevalue;
164 		return (1);
165 	case TIFFTAG_MAXSAMPLEVALUE:
166 		*va_arg(ap, uint16 *) = td->td_maxsamplevalue;
167 		return (1);
168 	case TIFFTAG_PLANARCONFIG:
169 		*va_arg(ap, uint16 *) = td->td_planarconfig;
170 		return (1);
171 	case TIFFTAG_RESOLUTIONUNIT:
172 		*va_arg(ap, uint16 *) = td->td_resolutionunit;
173 		return (1);
174 	case TIFFTAG_PREDICTOR:
175                 {
176 		TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
177 		*va_arg(ap, uint16*) = (uint16) sp->predictor;
178 		return (1);
179                 }
180 	case TIFFTAG_DOTRANGE:
181 		*va_arg(ap, uint16 *) = 0;
182 		*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
183 		return (1);
184 	case TIFFTAG_INKSET:
185 		*va_arg(ap, uint16 *) = td->td_inkset;
186 		return (1);
187 	case TIFFTAG_NUMBEROFINKS:
188 		*va_arg(ap, uint16 *) = td->td_ninks;
189 		return (1);
190 	case TIFFTAG_EXTRASAMPLES:
191 		*va_arg(ap, uint16 *) = td->td_extrasamples;
192 		*va_arg(ap, uint16 **) = td->td_sampleinfo;
193 		return (1);
194 	case TIFFTAG_MATTEING:
195 		*va_arg(ap, uint16 *) =
196 		    (td->td_extrasamples == 1 &&
197 		     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
198 		return (1);
199 	case TIFFTAG_TILEDEPTH:
200 		*va_arg(ap, uint32 *) = td->td_tiledepth;
201 		return (1);
202 	case TIFFTAG_DATATYPE:
203 		*va_arg(ap, uint16 *) = td->td_sampleformat-1;
204 		return (1);
205 	case TIFFTAG_SAMPLEFORMAT:
206 		*va_arg(ap, uint16 *) = td->td_sampleformat;
207                 return(1);
208 	case TIFFTAG_IMAGEDEPTH:
209 		*va_arg(ap, uint32 *) = td->td_imagedepth;
210 		return (1);
211 	case TIFFTAG_YCBCRCOEFFICIENTS:
212 		if (!td->td_ycbcrcoeffs) {
213 			td->td_ycbcrcoeffs = (float *)
214 			    _TIFFmalloc(3*sizeof (float));
215  			if (!td->td_ycbcrcoeffs)
216  				return (0);
217 			/* defaults are from CCIR Recommendation 601-1 */
218 			td->td_ycbcrcoeffs[0] = 0.299f;
219 			td->td_ycbcrcoeffs[1] = 0.587f;
220 			td->td_ycbcrcoeffs[2] = 0.114f;
221 		}
222 		*va_arg(ap, float **) = td->td_ycbcrcoeffs;
223 		return (1);
224 	case TIFFTAG_YCBCRSUBSAMPLING:
225 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
226 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
227 		return (1);
228 	case TIFFTAG_YCBCRPOSITIONING:
229 		*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
230 		return (1);
231 	case TIFFTAG_WHITEPOINT:
232 		if (!td->td_whitepoint) {
233 			td->td_whitepoint = (float *)
234 				_TIFFmalloc(2 * sizeof (float));
235  			if (!td->td_whitepoint)
236  				return (0);
237 			/* TIFF 6.0 specification says that it is no default
238 			   value for the WhitePoint, but AdobePhotoshop TIFF
239 			   Technical Note tells that it should be CIE D50. */
240 			td->td_whitepoint[0] =
241 				D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
242 			td->td_whitepoint[1] =
243 				D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
244 		}
245 		*va_arg(ap, float **) = td->td_whitepoint;
246 		return (1);
247 	case TIFFTAG_TRANSFERFUNCTION:
248 		if (!td->td_transferfunction[0] &&
249 		    !TIFFDefaultTransferFunction(tif, td)) {
250 			_TIFFError(tif, tif->tif_name,
251 				"No space for \"TransferFunction\" tag");
252 			return (0);
253 		}
254 		*va_arg(ap, uint16 **) = td->td_transferfunction[0];
255 		if (td->td_samplesperpixel - td->td_extrasamples > 1) {
256 			*va_arg(ap, uint16 **) = td->td_transferfunction[1];
257 			*va_arg(ap, uint16 **) = td->td_transferfunction[2];
258 		}
259 		return (1);
260 	case TIFFTAG_REFERENCEBLACKWHITE:
261 		if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(tif, td))
262 			return (0);
263 		*va_arg(ap, float **) = td->td_refblackwhite;
264 		return (1);
265 	}
266 	return (0);
267 }
268 
269 /*
270  * Like TIFFGetField, but return any default
271  * value if the tag is not present in the directory.
272  */
273 int
TIFFGetFieldDefaulted(TIFF * tif,ttag_t tag,...)274 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
275 {
276 	int ok;
277 	va_list ap;
278 
279 	va_start(ap, tag);
280 	ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
281 	va_end(ap);
282 	return (ok);
283 }
284