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