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
34 uint32
_TIFFMultiply32(TIFF * tif,uint32 first,uint32 second,const char * where)35 _TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
36 {
37 uint32 bytes = first * second;
38
39 if (second && bytes / second != first) {
40 TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
41 bytes = 0;
42 }
43
44 return bytes;
45 }
46
47 uint64
_TIFFMultiply64(TIFF * tif,uint64 first,uint64 second,const char * where)48 _TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
49 {
50 uint64 bytes = first * second;
51
52 if (second && bytes / second != first) {
53 TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
54 bytes = 0;
55 }
56
57 return bytes;
58 }
59
60 void*
_TIFFCheckRealloc(TIFF * tif,void * buffer,tmsize_t nmemb,tmsize_t elem_size,const char * what)61 _TIFFCheckRealloc(TIFF* tif, void* buffer,
62 tmsize_t nmemb, tmsize_t elem_size, const char* what)
63 {
64 void* cp = NULL;
65 tmsize_t bytes = nmemb * elem_size;
66
67 /*
68 * XXX: Check for integer overflow.
69 */
70 if (nmemb && elem_size && bytes / elem_size == nmemb)
71 cp = _TIFFrealloc(buffer, bytes);
72
73 if (cp == NULL) {
74 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
75 "Failed to allocate memory for %s "
76 "(%ld elements of %ld bytes each)",
77 what,(long) nmemb, (long) elem_size);
78 }
79
80 return cp;
81 }
82
83 void*
_TIFFCheckMalloc(TIFF * tif,tmsize_t nmemb,tmsize_t elem_size,const char * what)84 _TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
85 {
86 return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
87 }
88
89 static int
TIFFDefaultTransferFunction(TIFFDirectory * td)90 TIFFDefaultTransferFunction(TIFFDirectory* td)
91 {
92 uint16 **tf = td->td_transferfunction;
93 tmsize_t i, n, nbytes;
94
95 tf[0] = tf[1] = tf[2] = 0;
96 if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
97 return 0;
98
99 n = ((tmsize_t)1)<<td->td_bitspersample;
100 nbytes = n * sizeof (uint16);
101 tf[0] = (uint16 *)_TIFFmalloc(nbytes);
102 if (tf[0] == NULL)
103 return 0;
104 tf[0][0] = 0;
105 for (i = 1; i < n; i++) {
106 double t = (double)i/((double) n-1.);
107 tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
108 }
109
110 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
111 tf[1] = (uint16 *)_TIFFmalloc(nbytes);
112 if(tf[1] == NULL)
113 goto bad;
114 _TIFFmemcpy(tf[1], tf[0], nbytes);
115 tf[2] = (uint16 *)_TIFFmalloc(nbytes);
116 if (tf[2] == NULL)
117 goto bad;
118 _TIFFmemcpy(tf[2], tf[0], nbytes);
119 }
120 return 1;
121
122 bad:
123 if (tf[0])
124 _TIFFfree(tf[0]);
125 if (tf[1])
126 _TIFFfree(tf[1]);
127 if (tf[2])
128 _TIFFfree(tf[2]);
129 tf[0] = tf[1] = tf[2] = 0;
130 return 0;
131 }
132
133 static int
TIFFDefaultRefBlackWhite(TIFFDirectory * td)134 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
135 {
136 int i;
137
138 td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float));
139 if (td->td_refblackwhite == NULL)
140 return 0;
141 if (td->td_photometric == PHOTOMETRIC_YCBCR) {
142 /*
143 * YCbCr (Class Y) images must have the ReferenceBlackWhite
144 * tag set. Fix the broken images, which lacks that tag.
145 */
146 td->td_refblackwhite[0] = 0.0F;
147 td->td_refblackwhite[1] = td->td_refblackwhite[3] =
148 td->td_refblackwhite[5] = 255.0F;
149 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
150 } else {
151 /*
152 * Assume RGB (Class R)
153 */
154 for (i = 0; i < 3; i++) {
155 td->td_refblackwhite[2*i+0] = 0;
156 td->td_refblackwhite[2*i+1] =
157 (float)((1L<<td->td_bitspersample)-1L);
158 }
159 }
160 return 1;
161 }
162
163 /*
164 * Like TIFFGetField, but return any default
165 * value if the tag is not present in the directory.
166 *
167 * NB: We use the value in the directory, rather than
168 * explicit values so that defaults exist only one
169 * place in the library -- in TIFFDefaultDirectory.
170 */
171 int
TIFFVGetFieldDefaulted(TIFF * tif,uint32 tag,va_list ap)172 TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
173 {
174 TIFFDirectory *td = &tif->tif_dir;
175
176 if (TIFFVGetField(tif, tag, ap))
177 return (1);
178 switch (tag) {
179 case TIFFTAG_SUBFILETYPE:
180 *va_arg(ap, uint32 *) = td->td_subfiletype;
181 return (1);
182 case TIFFTAG_BITSPERSAMPLE:
183 *va_arg(ap, uint16 *) = td->td_bitspersample;
184 return (1);
185 case TIFFTAG_THRESHHOLDING:
186 *va_arg(ap, uint16 *) = td->td_threshholding;
187 return (1);
188 case TIFFTAG_FILLORDER:
189 *va_arg(ap, uint16 *) = td->td_fillorder;
190 return (1);
191 case TIFFTAG_ORIENTATION:
192 *va_arg(ap, uint16 *) = td->td_orientation;
193 return (1);
194 case TIFFTAG_SAMPLESPERPIXEL:
195 *va_arg(ap, uint16 *) = td->td_samplesperpixel;
196 return (1);
197 case TIFFTAG_ROWSPERSTRIP:
198 *va_arg(ap, uint32 *) = td->td_rowsperstrip;
199 return (1);
200 case TIFFTAG_MINSAMPLEVALUE:
201 *va_arg(ap, uint16 *) = td->td_minsamplevalue;
202 return (1);
203 case TIFFTAG_MAXSAMPLEVALUE:
204 *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
205 return (1);
206 case TIFFTAG_PLANARCONFIG:
207 *va_arg(ap, uint16 *) = td->td_planarconfig;
208 return (1);
209 case TIFFTAG_RESOLUTIONUNIT:
210 *va_arg(ap, uint16 *) = td->td_resolutionunit;
211 return (1);
212 case TIFFTAG_PREDICTOR:
213 {
214 TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
215 if( sp == NULL )
216 {
217 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
218 "Cannot get \"Predictor\" tag as plugin is not configured");
219 *va_arg(ap, uint16*) = 0;
220 return 0;
221 }
222 *va_arg(ap, uint16*) = (uint16) sp->predictor;
223 return 1;
224 }
225 case TIFFTAG_DOTRANGE:
226 *va_arg(ap, uint16 *) = 0;
227 *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
228 return (1);
229 case TIFFTAG_INKSET:
230 *va_arg(ap, uint16 *) = INKSET_CMYK;
231 return 1;
232 case TIFFTAG_NUMBEROFINKS:
233 *va_arg(ap, uint16 *) = 4;
234 return (1);
235 case TIFFTAG_EXTRASAMPLES:
236 *va_arg(ap, uint16 *) = td->td_extrasamples;
237 *va_arg(ap, uint16 **) = td->td_sampleinfo;
238 return (1);
239 case TIFFTAG_MATTEING:
240 *va_arg(ap, uint16 *) =
241 (td->td_extrasamples == 1 &&
242 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
243 return (1);
244 case TIFFTAG_TILEDEPTH:
245 *va_arg(ap, uint32 *) = td->td_tiledepth;
246 return (1);
247 case TIFFTAG_DATATYPE:
248 *va_arg(ap, uint16 *) = td->td_sampleformat-1;
249 return (1);
250 case TIFFTAG_SAMPLEFORMAT:
251 *va_arg(ap, uint16 *) = td->td_sampleformat;
252 return(1);
253 case TIFFTAG_IMAGEDEPTH:
254 *va_arg(ap, uint32 *) = td->td_imagedepth;
255 return (1);
256 case TIFFTAG_YCBCRCOEFFICIENTS:
257 {
258 /* defaults are from CCIR Recommendation 601-1 */
259 static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
260 *va_arg(ap, float **) = ycbcrcoeffs;
261 return 1;
262 }
263 case TIFFTAG_YCBCRSUBSAMPLING:
264 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
265 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
266 return (1);
267 case TIFFTAG_YCBCRPOSITIONING:
268 *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
269 return (1);
270 case TIFFTAG_WHITEPOINT:
271 {
272 static float whitepoint[2];
273
274 /* TIFF 6.0 specification tells that it is no default
275 value for the WhitePoint, but AdobePhotoshop TIFF
276 Technical Note tells that it should be CIE D50. */
277 whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
278 whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
279 *va_arg(ap, float **) = whitepoint;
280 return 1;
281 }
282 case TIFFTAG_TRANSFERFUNCTION:
283 if (!td->td_transferfunction[0] &&
284 !TIFFDefaultTransferFunction(td)) {
285 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
286 return (0);
287 }
288 *va_arg(ap, uint16 **) = td->td_transferfunction[0];
289 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
290 *va_arg(ap, uint16 **) = td->td_transferfunction[1];
291 *va_arg(ap, uint16 **) = td->td_transferfunction[2];
292 }
293 return (1);
294 case TIFFTAG_REFERENCEBLACKWHITE:
295 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
296 return (0);
297 *va_arg(ap, float **) = td->td_refblackwhite;
298 return (1);
299 }
300 return 0;
301 }
302
303 /*
304 * Like TIFFGetField, but return any default
305 * value if the tag is not present in the directory.
306 */
307 int
TIFFGetFieldDefaulted(TIFF * tif,uint32 tag,...)308 TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
309 {
310 int ok;
311 va_list ap;
312
313 va_start(ap, tag);
314 ok = TIFFVGetFieldDefaulted(tif, tag, ap);
315 va_end(ap);
316 return (ok);
317 }
318
319 struct _Int64Parts {
320 int32 low, high;
321 };
322
323 typedef union {
324 struct _Int64Parts part;
325 int64 value;
326 } _Int64;
327
328 float
_TIFFUInt64ToFloat(uint64 ui64)329 _TIFFUInt64ToFloat(uint64 ui64)
330 {
331 _Int64 i;
332
333 i.value = ui64;
334 if (i.part.high >= 0) {
335 return (float)i.value;
336 } else {
337 long double df;
338 df = (long double)i.value;
339 df += 18446744073709551616.0; /* adding 2**64 */
340 return (float)df;
341 }
342 }
343
344 double
_TIFFUInt64ToDouble(uint64 ui64)345 _TIFFUInt64ToDouble(uint64 ui64)
346 {
347 _Int64 i;
348
349 i.value = ui64;
350 if (i.part.high >= 0) {
351 return (double)i.value;
352 } else {
353 long double df;
354 df = (long double)i.value;
355 df += 18446744073709551616.0; /* adding 2**64 */
356 return (double)df;
357 }
358 }
359
_TIFFSeekOK(TIFF * tif,toff_t off)360 int _TIFFSeekOK(TIFF* tif, toff_t off)
361 {
362 /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
363 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
364 return off <= (~(uint64)0)/2 && TIFFSeekFile(tif,off,SEEK_SET)==off;
365 }
366
367 /* vim: set ts=8 sts=8 sw=8 noet: */
368 /*
369 * Local Variables:
370 * mode: c
371 * c-basic-offset: 8
372 * fill-column: 78
373 * End:
374 */
375