1 /* $Id: tif_aux.c,v 1.26 2010-07-01 15:33:28 dron Exp $ */
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
30 * Auxiliary Support Routines.
34 #include <tif_predict.h>
38 _TIFFMultiply32(TIFF
* tif
, uint32 first
, uint32 second
, const char* where
)
40 uint32 bytes
= first
* second
;
42 if (second
&& bytes
/ second
!= first
) {
43 TIFFErrorExt(tif
->tif_clientdata
, where
, "Integer overflow in %s", where
);
51 _TIFFMultiply64(TIFF
* tif
, uint64 first
, uint64 second
, const char* where
)
53 uint64 bytes
= first
* second
;
55 if (second
&& bytes
/ second
!= first
) {
56 TIFFErrorExt(tif
->tif_clientdata
, where
, "Integer overflow in %s", where
);
64 _TIFFCheckRealloc(TIFF
* tif
, void* buffer
,
65 tmsize_t nmemb
, tmsize_t elem_size
, const char* what
)
68 tmsize_t bytes
= nmemb
* elem_size
;
71 * XXX: Check for integer overflow.
73 if (nmemb
&& elem_size
&& bytes
/ elem_size
== nmemb
)
74 cp
= _TIFFrealloc(buffer
, bytes
);
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
);
87 _TIFFCheckMalloc(TIFF
* tif
, tmsize_t nmemb
, tmsize_t elem_size
, const char* what
)
89 return _TIFFCheckRealloc(tif
, NULL
, nmemb
, elem_size
, what
);
93 TIFFDefaultTransferFunction(TIFFDirectory
* td
)
95 uint16
**tf
= td
->td_transferfunction
;
96 tmsize_t i
, n
, nbytes
;
98 tf
[0] = tf
[1] = tf
[2] = 0;
99 if (td
->td_bitspersample
>= sizeof(tmsize_t
) * 8 - 2)
102 n
= ((tmsize_t
)1)<<td
->td_bitspersample
;
103 nbytes
= n
* sizeof (uint16
);
104 if (!(tf
[0] = (uint16
*)_TIFFmalloc(nbytes
)))
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);
112 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
113 if (!(tf
[1] = (uint16
*)_TIFFmalloc(nbytes
)))
115 _TIFFmemcpy(tf
[1], tf
[0], nbytes
);
116 if (!(tf
[2] = (uint16
*)_TIFFmalloc(nbytes
)))
118 _TIFFmemcpy(tf
[2], tf
[0], nbytes
);
129 tf
[0] = tf
[1] = tf
[2] = 0;
134 TIFFDefaultRefBlackWhite(TIFFDirectory
* td
)
138 if (!(td
->td_refblackwhite
= (float *)_TIFFmalloc(6*sizeof (float))))
140 if (td
->td_photometric
== PHOTOMETRIC_YCBCR
) {
142 * YCbCr (Class Y) images must have the ReferenceBlackWhite
143 * tag set. Fix the broken images, which lacks that tag.
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
;
151 * Assume RGB (Class R)
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);
163 * Like TIFFGetField, but return any default
164 * value if the tag is not present in the directory.
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.
171 TIFFVGetFieldDefaulted(TIFF
* tif
, uint32 tag
, va_list ap
)
173 TIFFDirectory
*td
= &tif
->tif_dir
;
175 if (TIFFVGetField(tif
, tag
, ap
))
178 case TIFFTAG_SUBFILETYPE
:
179 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
181 case TIFFTAG_BITSPERSAMPLE
:
182 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
184 case TIFFTAG_THRESHHOLDING
:
185 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
187 case TIFFTAG_FILLORDER
:
188 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
190 case TIFFTAG_ORIENTATION
:
191 *va_arg(ap
, uint16
*) = td
->td_orientation
;
193 case TIFFTAG_SAMPLESPERPIXEL
:
194 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
196 case TIFFTAG_ROWSPERSTRIP
:
197 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
199 case TIFFTAG_MINSAMPLEVALUE
:
200 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
202 case TIFFTAG_MAXSAMPLEVALUE
:
203 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
205 case TIFFTAG_PLANARCONFIG
:
206 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
208 case TIFFTAG_RESOLUTIONUNIT
:
209 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
211 case TIFFTAG_PREDICTOR
:
213 TIFFPredictorState
* sp
= (TIFFPredictorState
*) tif
->tif_data
;
214 *va_arg(ap
, uint16
*) = (uint16
) sp
->predictor
;
217 case TIFFTAG_DOTRANGE
:
218 *va_arg(ap
, uint16
*) = 0;
219 *va_arg(ap
, uint16
*) = (1<<td
->td_bitspersample
)-1;
222 *va_arg(ap
, uint16
*) = INKSET_CMYK
;
224 case TIFFTAG_NUMBEROFINKS
:
225 *va_arg(ap
, uint16
*) = 4;
227 case TIFFTAG_EXTRASAMPLES
:
228 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
229 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
231 case TIFFTAG_MATTEING
:
232 *va_arg(ap
, uint16
*) =
233 (td
->td_extrasamples
== 1 &&
234 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
236 case TIFFTAG_TILEDEPTH
:
237 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
239 case TIFFTAG_DATATYPE
:
240 *va_arg(ap
, uint16
*) = td
->td_sampleformat
-1;
242 case TIFFTAG_SAMPLEFORMAT
:
243 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
245 case TIFFTAG_IMAGEDEPTH
:
246 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
248 case TIFFTAG_YCBCRCOEFFICIENTS
:
250 /* defaults are from CCIR Recommendation 601-1 */
251 static float ycbcrcoeffs
[] = { 0.299f
, 0.587f
, 0.114f
};
252 *va_arg(ap
, float **) = ycbcrcoeffs
;
255 case TIFFTAG_YCBCRSUBSAMPLING
:
256 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
257 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
259 case TIFFTAG_YCBCRPOSITIONING
:
260 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
262 case TIFFTAG_WHITEPOINT
:
264 static float whitepoint
[2];
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
;
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");
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];
286 case TIFFTAG_REFERENCEBLACKWHITE
:
287 if (!td
->td_refblackwhite
&& !TIFFDefaultRefBlackWhite(td
))
289 *va_arg(ap
, float **) = td
->td_refblackwhite
;
296 * Like TIFFGetField, but return any default
297 * value if the tag is not present in the directory.
300 TIFFGetFieldDefaulted(TIFF
* tif
, uint32 tag
, ...)
306 ok
= TIFFVGetFieldDefaulted(tif
, tag
, ap
);
316 struct _Int64Parts part
;
321 _TIFFUInt64ToFloat(uint64 ui64
)
326 if (i
.part
.high
>= 0) {
327 return (float)i
.value
;
330 df
= (long double)i
.value
;
331 df
+= 18446744073709551616.0; /* adding 2**64 */
337 _TIFFUInt64ToDouble(uint64 ui64
)
342 if (i
.part
.high
>= 0) {
343 return (double)i
.value
;
346 df
= (long double)i
.value
;
347 df
+= 18446744073709551616.0; /* adding 2**64 */
352 /* vim: set ts=8 sts=8 sw=8 noet: */