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