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