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