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