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.
33 #include "tif_predict.h"
37 _TIFFMultiply32(TIFF
* tif
, uint32 first
, uint32 second
, const char* where
)
39 uint32 bytes
= first
* second
;
41 if (second
&& bytes
/ second
!= first
) {
42 TIFFErrorExt(tif
->tif_clientdata
, where
, "Integer overflow in %s", where
);
50 _TIFFMultiply64(TIFF
* tif
, uint64 first
, uint64 second
, const char* where
)
52 uint64 bytes
= first
* second
;
54 if (second
&& bytes
/ second
!= first
) {
55 TIFFErrorExt(tif
->tif_clientdata
, where
, "Integer overflow in %s", where
);
63 _TIFFCheckRealloc(TIFF
* tif
, void* buffer
,
64 tmsize_t nmemb
, tmsize_t elem_size
, const char* what
)
67 tmsize_t bytes
= nmemb
* elem_size
;
70 * XXX: Check for integer overflow.
72 if (nmemb
&& elem_size
&& bytes
/ elem_size
== nmemb
)
73 cp
= _TIFFrealloc(buffer
, bytes
);
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
);
86 _TIFFCheckMalloc(TIFF
* tif
, tmsize_t nmemb
, tmsize_t elem_size
, const char* what
)
88 return _TIFFCheckRealloc(tif
, NULL
, nmemb
, elem_size
, what
);
92 TIFFDefaultTransferFunction(TIFFDirectory
* td
)
94 uint16
**tf
= td
->td_transferfunction
;
95 tmsize_t i
, n
, nbytes
;
97 tf
[0] = tf
[1] = tf
[2] = 0;
98 if (td
->td_bitspersample
>= sizeof(tmsize_t
) * 8 - 2)
101 n
= ((tmsize_t
)1)<<td
->td_bitspersample
;
102 nbytes
= n
* sizeof (uint16
);
103 if (!(tf
[0] = (uint16
*)_TIFFmalloc(nbytes
)))
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);
111 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
112 if (!(tf
[1] = (uint16
*)_TIFFmalloc(nbytes
)))
114 _TIFFmemcpy(tf
[1], tf
[0], nbytes
);
115 if (!(tf
[2] = (uint16
*)_TIFFmalloc(nbytes
)))
117 _TIFFmemcpy(tf
[2], tf
[0], nbytes
);
128 tf
[0] = tf
[1] = tf
[2] = 0;
133 TIFFDefaultRefBlackWhite(TIFFDirectory
* td
)
137 if (!(td
->td_refblackwhite
= (float *)_TIFFmalloc(6*sizeof (float))))
139 if (td
->td_photometric
== PHOTOMETRIC_YCBCR
) {
141 * YCbCr (Class Y) images must have the ReferenceBlackWhite
142 * tag set. Fix the broken images, which lacks that tag.
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
;
150 * Assume RGB (Class R)
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);
162 * Like TIFFGetField, but return any default
163 * value if the tag is not present in the directory.
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.
170 TIFFVGetFieldDefaulted(TIFF
* tif
, uint32 tag
, va_list ap
)
172 TIFFDirectory
*td
= &tif
->tif_dir
;
174 if (TIFFVGetField(tif
, tag
, ap
))
177 case TIFFTAG_SUBFILETYPE
:
178 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
180 case TIFFTAG_BITSPERSAMPLE
:
181 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
183 case TIFFTAG_THRESHHOLDING
:
184 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
186 case TIFFTAG_FILLORDER
:
187 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
189 case TIFFTAG_ORIENTATION
:
190 *va_arg(ap
, uint16
*) = td
->td_orientation
;
192 case TIFFTAG_SAMPLESPERPIXEL
:
193 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
195 case TIFFTAG_ROWSPERSTRIP
:
196 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
198 case TIFFTAG_MINSAMPLEVALUE
:
199 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
201 case TIFFTAG_MAXSAMPLEVALUE
:
202 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
204 case TIFFTAG_PLANARCONFIG
:
205 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
207 case TIFFTAG_RESOLUTIONUNIT
:
208 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
210 case TIFFTAG_PREDICTOR
:
212 TIFFPredictorState
* sp
= (TIFFPredictorState
*) tif
->tif_data
;
213 *va_arg(ap
, uint16
*) = (uint16
) sp
->predictor
;
216 case TIFFTAG_DOTRANGE
:
217 *va_arg(ap
, uint16
*) = 0;
218 *va_arg(ap
, uint16
*) = (1<<td
->td_bitspersample
)-1;
221 *va_arg(ap
, uint16
*) = INKSET_CMYK
;
223 case TIFFTAG_NUMBEROFINKS
:
224 *va_arg(ap
, uint16
*) = 4;
226 case TIFFTAG_EXTRASAMPLES
:
227 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
228 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
230 case TIFFTAG_MATTEING
:
231 *va_arg(ap
, uint16
*) =
232 (td
->td_extrasamples
== 1 &&
233 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
235 case TIFFTAG_TILEDEPTH
:
236 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
238 case TIFFTAG_DATATYPE
:
239 *va_arg(ap
, uint16
*) = td
->td_sampleformat
-1;
241 case TIFFTAG_SAMPLEFORMAT
:
242 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
244 case TIFFTAG_IMAGEDEPTH
:
245 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
247 case TIFFTAG_YCBCRCOEFFICIENTS
:
249 /* defaults are from CCIR Recommendation 601-1 */
250 static float ycbcrcoeffs
[] = { 0.299f
, 0.587f
, 0.114f
};
251 *va_arg(ap
, float **) = ycbcrcoeffs
;
254 case TIFFTAG_YCBCRSUBSAMPLING
:
255 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
256 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
258 case TIFFTAG_YCBCRPOSITIONING
:
259 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
261 case TIFFTAG_WHITEPOINT
:
263 static float whitepoint
[2];
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
;
273 case TIFFTAG_TRANSFERFUNCTION
:
274 if (!td
->td_transferfunction
[0] &&
275 !TIFFDefaultTransferFunction(td
)) {
276 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, "No space for \"TransferFunction\" tag");
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];
285 case TIFFTAG_REFERENCEBLACKWHITE
:
286 if (!td
->td_refblackwhite
&& !TIFFDefaultRefBlackWhite(td
))
288 *va_arg(ap
, float **) = td
->td_refblackwhite
;
295 * Like TIFFGetField, but return any default
296 * value if the tag is not present in the directory.
299 TIFFGetFieldDefaulted(TIFF
* tif
, uint32 tag
, ...)
305 ok
= TIFFVGetFieldDefaulted(tif
, tag
, ap
);
315 struct _Int64Parts part
;
320 _TIFFUInt64ToFloat(uint64 ui64
)
325 if (i
.part
.high
>= 0) {
326 return (float)i
.value
;
329 df
= (long double)i
.value
;
330 df
+= 18446744073709551616.0; /* adding 2**64 */
336 _TIFFUInt64ToDouble(uint64 ui64
)
341 if (i
.part
.high
>= 0) {
342 return (double)i
.value
;
345 df
= (long double)i
.value
;
346 df
+= 18446744073709551616.0; /* adding 2**64 */
351 /* vim: set ts=8 sts=8 sw=8 noet: */