[APPHELP][APPHELP_APITEST] Update db apitests to succeed from 2k3 to 10, paving the...
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_aux.c
1 /* $Id: tif_aux.c,v 1.26 2010-07-01 15:33:28 dron 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 if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
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 if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
114 goto bad;
115 _TIFFmemcpy(tf[1], tf[0], nbytes);
116 if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
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
134 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
135 {
136 int i;
137
138 if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
139 return 0;
140 if (td->td_photometric == PHOTOMETRIC_YCBCR) {
141 /*
142 * YCbCr (Class Y) images must have the ReferenceBlackWhite
143 * tag set. Fix the broken images, which lacks that tag.
144 */
145 td->td_refblackwhite[0] = 0.0F;
146 td->td_refblackwhite[1] = td->td_refblackwhite[3] =
147 td->td_refblackwhite[5] = 255.0F;
148 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
149 } else {
150 /*
151 * Assume RGB (Class R)
152 */
153 for (i = 0; i < 3; i++) {
154 td->td_refblackwhite[2*i+0] = 0;
155 td->td_refblackwhite[2*i+1] =
156 (float)((1L<<td->td_bitspersample)-1L);
157 }
158 }
159 return 1;
160 }
161
162 /*
163 * Like TIFFGetField, but return any default
164 * value if the tag is not present in the directory.
165 *
166 * NB: We use the value in the directory, rather than
167 * explcit values so that defaults exist only one
168 * place in the library -- in TIFFDefaultDirectory.
169 */
170 int
171 TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
172 {
173 TIFFDirectory *td = &tif->tif_dir;
174
175 if (TIFFVGetField(tif, tag, ap))
176 return (1);
177 switch (tag) {
178 case TIFFTAG_SUBFILETYPE:
179 *va_arg(ap, uint32 *) = td->td_subfiletype;
180 return (1);
181 case TIFFTAG_BITSPERSAMPLE:
182 *va_arg(ap, uint16 *) = td->td_bitspersample;
183 return (1);
184 case TIFFTAG_THRESHHOLDING:
185 *va_arg(ap, uint16 *) = td->td_threshholding;
186 return (1);
187 case TIFFTAG_FILLORDER:
188 *va_arg(ap, uint16 *) = td->td_fillorder;
189 return (1);
190 case TIFFTAG_ORIENTATION:
191 *va_arg(ap, uint16 *) = td->td_orientation;
192 return (1);
193 case TIFFTAG_SAMPLESPERPIXEL:
194 *va_arg(ap, uint16 *) = td->td_samplesperpixel;
195 return (1);
196 case TIFFTAG_ROWSPERSTRIP:
197 *va_arg(ap, uint32 *) = td->td_rowsperstrip;
198 return (1);
199 case TIFFTAG_MINSAMPLEVALUE:
200 *va_arg(ap, uint16 *) = td->td_minsamplevalue;
201 return (1);
202 case TIFFTAG_MAXSAMPLEVALUE:
203 *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
204 return (1);
205 case TIFFTAG_PLANARCONFIG:
206 *va_arg(ap, uint16 *) = td->td_planarconfig;
207 return (1);
208 case TIFFTAG_RESOLUTIONUNIT:
209 *va_arg(ap, uint16 *) = td->td_resolutionunit;
210 return (1);
211 case TIFFTAG_PREDICTOR:
212 {
213 TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
214 *va_arg(ap, uint16*) = (uint16) sp->predictor;
215 return 1;
216 }
217 case TIFFTAG_DOTRANGE:
218 *va_arg(ap, uint16 *) = 0;
219 *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
220 return (1);
221 case TIFFTAG_INKSET:
222 *va_arg(ap, uint16 *) = INKSET_CMYK;
223 return 1;
224 case TIFFTAG_NUMBEROFINKS:
225 *va_arg(ap, uint16 *) = 4;
226 return (1);
227 case TIFFTAG_EXTRASAMPLES:
228 *va_arg(ap, uint16 *) = td->td_extrasamples;
229 *va_arg(ap, uint16 **) = td->td_sampleinfo;
230 return (1);
231 case TIFFTAG_MATTEING:
232 *va_arg(ap, uint16 *) =
233 (td->td_extrasamples == 1 &&
234 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
235 return (1);
236 case TIFFTAG_TILEDEPTH:
237 *va_arg(ap, uint32 *) = td->td_tiledepth;
238 return (1);
239 case TIFFTAG_DATATYPE:
240 *va_arg(ap, uint16 *) = td->td_sampleformat-1;
241 return (1);
242 case TIFFTAG_SAMPLEFORMAT:
243 *va_arg(ap, uint16 *) = td->td_sampleformat;
244 return(1);
245 case TIFFTAG_IMAGEDEPTH:
246 *va_arg(ap, uint32 *) = td->td_imagedepth;
247 return (1);
248 case TIFFTAG_YCBCRCOEFFICIENTS:
249 {
250 /* defaults are from CCIR Recommendation 601-1 */
251 static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
252 *va_arg(ap, float **) = ycbcrcoeffs;
253 return 1;
254 }
255 case TIFFTAG_YCBCRSUBSAMPLING:
256 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
257 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
258 return (1);
259 case TIFFTAG_YCBCRPOSITIONING:
260 *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
261 return (1);
262 case TIFFTAG_WHITEPOINT:
263 {
264 static float whitepoint[2];
265
266 /* TIFF 6.0 specification tells that it is no default
267 value for the WhitePoint, but AdobePhotoshop TIFF
268 Technical Note tells that it should be CIE D50. */
269 whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
270 whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
271 *va_arg(ap, float **) = whitepoint;
272 return 1;
273 }
274 case TIFFTAG_TRANSFERFUNCTION:
275 if (!td->td_transferfunction[0] &&
276 !TIFFDefaultTransferFunction(td)) {
277 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
278 return (0);
279 }
280 *va_arg(ap, uint16 **) = td->td_transferfunction[0];
281 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
282 *va_arg(ap, uint16 **) = td->td_transferfunction[1];
283 *va_arg(ap, uint16 **) = td->td_transferfunction[2];
284 }
285 return (1);
286 case TIFFTAG_REFERENCEBLACKWHITE:
287 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
288 return (0);
289 *va_arg(ap, float **) = td->td_refblackwhite;
290 return (1);
291 }
292 return 0;
293 }
294
295 /*
296 * Like TIFFGetField, but return any default
297 * value if the tag is not present in the directory.
298 */
299 int
300 TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
301 {
302 int ok;
303 va_list ap;
304
305 va_start(ap, tag);
306 ok = TIFFVGetFieldDefaulted(tif, tag, ap);
307 va_end(ap);
308 return (ok);
309 }
310
311 struct _Int64Parts {
312 int32 low, high;
313 };
314
315 typedef union {
316 struct _Int64Parts part;
317 int64 value;
318 } _Int64;
319
320 float
321 _TIFFUInt64ToFloat(uint64 ui64)
322 {
323 _Int64 i;
324
325 i.value = ui64;
326 if (i.part.high >= 0) {
327 return (float)i.value;
328 } else {
329 long double df;
330 df = (long double)i.value;
331 df += 18446744073709551616.0; /* adding 2**64 */
332 return (float)df;
333 }
334 }
335
336 double
337 _TIFFUInt64ToDouble(uint64 ui64)
338 {
339 _Int64 i;
340
341 i.value = ui64;
342 if (i.part.high >= 0) {
343 return (double)i.value;
344 } else {
345 long double df;
346 df = (long double)i.value;
347 df += 18446744073709551616.0; /* adding 2**64 */
348 return (double)df;
349 }
350 }
351
352 /* vim: set ts=8 sts=8 sw=8 noet: */
353 /*
354 * Local Variables:
355 * mode: c
356 * c-basic-offset: 8
357 * fill-column: 78
358 * End:
359 */