2 * Copyright (c) 1991-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Auxiliary Support Routines.
32 #include "tif_predict.h"
37 _TIFFMultiply32(TIFF
* tif
, uint32 first
, uint32 second
, const char* where
)
39 if (second
&& first
> TIFF_UINT32_MAX
/ second
) {
40 TIFFErrorExt(tif
->tif_clientdata
, where
, "Integer overflow in %s", where
);
44 return first
* second
;
48 _TIFFMultiply64(TIFF
* tif
, uint64 first
, uint64 second
, const char* where
)
50 if (second
&& first
> TIFF_UINT64_MAX
/ second
) {
51 TIFFErrorExt(tif
->tif_clientdata
, where
, "Integer overflow in %s", where
);
55 return first
* second
;
59 _TIFFMultiplySSize(TIFF
* tif
, tmsize_t first
, tmsize_t second
, const char* where
)
61 if( first
<= 0 || second
<= 0 )
63 if( tif
!= NULL
&& where
!= NULL
)
65 TIFFErrorExt(tif
->tif_clientdata
, where
,
66 "Invalid argument to _TIFFMultiplySSize() in %s", where
);
71 if( first
> TIFF_TMSIZE_T_MAX
/ second
)
73 if( tif
!= NULL
&& where
!= NULL
)
75 TIFFErrorExt(tif
->tif_clientdata
, where
,
76 "Integer overflow in %s", where
);
80 return first
* second
;
83 tmsize_t
_TIFFCastUInt64ToSSize(TIFF
* tif
, uint64 val
, const char* module
)
85 if( val
> (uint64
)TIFF_TMSIZE_T_MAX
)
87 if( tif
!= NULL
&& module
!= NULL
)
89 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer overflow");
97 _TIFFCheckRealloc(TIFF
* tif
, void* buffer
,
98 tmsize_t nmemb
, tmsize_t elem_size
, const char* what
)
101 tmsize_t count
= _TIFFMultiplySSize(tif
, nmemb
, elem_size
, NULL
);
103 * Check for integer overflow.
107 cp
= _TIFFrealloc(buffer
, count
);
111 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
112 "Failed to allocate memory for %s "
113 "(%ld elements of %ld bytes each)",
114 what
,(long) nmemb
, (long) elem_size
);
121 _TIFFCheckMalloc(TIFF
* tif
, tmsize_t nmemb
, tmsize_t elem_size
, const char* what
)
123 return _TIFFCheckRealloc(tif
, NULL
, nmemb
, elem_size
, what
);
127 TIFFDefaultTransferFunction(TIFFDirectory
* td
)
129 uint16
**tf
= td
->td_transferfunction
;
130 tmsize_t i
, n
, nbytes
;
132 tf
[0] = tf
[1] = tf
[2] = 0;
133 if (td
->td_bitspersample
>= sizeof(tmsize_t
) * 8 - 2)
136 n
= ((tmsize_t
)1)<<td
->td_bitspersample
;
137 nbytes
= n
* sizeof (uint16
);
138 tf
[0] = (uint16
*)_TIFFmalloc(nbytes
);
142 for (i
= 1; i
< n
; i
++) {
143 double t
= (double)i
/((double) n
-1.);
144 tf
[0][i
] = (uint16
)floor(65535.*pow(t
, 2.2) + .5);
147 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
148 tf
[1] = (uint16
*)_TIFFmalloc(nbytes
);
151 _TIFFmemcpy(tf
[1], tf
[0], nbytes
);
152 tf
[2] = (uint16
*)_TIFFmalloc(nbytes
);
155 _TIFFmemcpy(tf
[2], tf
[0], nbytes
);
166 tf
[0] = tf
[1] = tf
[2] = 0;
171 TIFFDefaultRefBlackWhite(TIFFDirectory
* td
)
175 td
->td_refblackwhite
= (float *)_TIFFmalloc(6*sizeof (float));
176 if (td
->td_refblackwhite
== NULL
)
178 if (td
->td_photometric
== PHOTOMETRIC_YCBCR
) {
180 * YCbCr (Class Y) images must have the ReferenceBlackWhite
181 * tag set. Fix the broken images, which lacks that tag.
183 td
->td_refblackwhite
[0] = 0.0F
;
184 td
->td_refblackwhite
[1] = td
->td_refblackwhite
[3] =
185 td
->td_refblackwhite
[5] = 255.0F
;
186 td
->td_refblackwhite
[2] = td
->td_refblackwhite
[4] = 128.0F
;
189 * Assume RGB (Class R)
191 for (i
= 0; i
< 3; i
++) {
192 td
->td_refblackwhite
[2*i
+0] = 0;
193 td
->td_refblackwhite
[2*i
+1] =
194 (float)((1L<<td
->td_bitspersample
)-1L);
201 * Like TIFFGetField, but return any default
202 * value if the tag is not present in the directory.
204 * NB: We use the value in the directory, rather than
205 * explicit values so that defaults exist only one
206 * place in the library -- in TIFFDefaultDirectory.
209 TIFFVGetFieldDefaulted(TIFF
* tif
, uint32 tag
, va_list ap
)
211 TIFFDirectory
*td
= &tif
->tif_dir
;
213 if (TIFFVGetField(tif
, tag
, ap
))
216 case TIFFTAG_SUBFILETYPE
:
217 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
219 case TIFFTAG_BITSPERSAMPLE
:
220 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
222 case TIFFTAG_THRESHHOLDING
:
223 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
225 case TIFFTAG_FILLORDER
:
226 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
228 case TIFFTAG_ORIENTATION
:
229 *va_arg(ap
, uint16
*) = td
->td_orientation
;
231 case TIFFTAG_SAMPLESPERPIXEL
:
232 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
234 case TIFFTAG_ROWSPERSTRIP
:
235 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
237 case TIFFTAG_MINSAMPLEVALUE
:
238 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
240 case TIFFTAG_MAXSAMPLEVALUE
:
241 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
243 case TIFFTAG_PLANARCONFIG
:
244 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
246 case TIFFTAG_RESOLUTIONUNIT
:
247 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
249 case TIFFTAG_PREDICTOR
:
251 TIFFPredictorState
* sp
= (TIFFPredictorState
*) tif
->tif_data
;
254 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
255 "Cannot get \"Predictor\" tag as plugin is not configured");
256 *va_arg(ap
, uint16
*) = 0;
259 *va_arg(ap
, uint16
*) = (uint16
) sp
->predictor
;
262 case TIFFTAG_DOTRANGE
:
263 *va_arg(ap
, uint16
*) = 0;
264 *va_arg(ap
, uint16
*) = (1<<td
->td_bitspersample
)-1;
267 *va_arg(ap
, uint16
*) = INKSET_CMYK
;
269 case TIFFTAG_NUMBEROFINKS
:
270 *va_arg(ap
, uint16
*) = 4;
272 case TIFFTAG_EXTRASAMPLES
:
273 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
274 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
276 case TIFFTAG_MATTEING
:
277 *va_arg(ap
, uint16
*) =
278 (td
->td_extrasamples
== 1 &&
279 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
281 case TIFFTAG_TILEDEPTH
:
282 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
284 case TIFFTAG_DATATYPE
:
285 *va_arg(ap
, uint16
*) = td
->td_sampleformat
-1;
287 case TIFFTAG_SAMPLEFORMAT
:
288 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
290 case TIFFTAG_IMAGEDEPTH
:
291 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
293 case TIFFTAG_YCBCRCOEFFICIENTS
:
295 /* defaults are from CCIR Recommendation 601-1 */
296 static float ycbcrcoeffs
[] = { 0.299f
, 0.587f
, 0.114f
};
297 *va_arg(ap
, float **) = ycbcrcoeffs
;
300 case TIFFTAG_YCBCRSUBSAMPLING
:
301 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
302 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
304 case TIFFTAG_YCBCRPOSITIONING
:
305 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
307 case TIFFTAG_WHITEPOINT
:
309 static float whitepoint
[2];
311 /* TIFF 6.0 specification tells that it is no default
312 value for the WhitePoint, but AdobePhotoshop TIFF
313 Technical Note tells that it should be CIE D50. */
314 whitepoint
[0] = D50_X0
/ (D50_X0
+ D50_Y0
+ D50_Z0
);
315 whitepoint
[1] = D50_Y0
/ (D50_X0
+ D50_Y0
+ D50_Z0
);
316 *va_arg(ap
, float **) = whitepoint
;
319 case TIFFTAG_TRANSFERFUNCTION
:
320 if (!td
->td_transferfunction
[0] &&
321 !TIFFDefaultTransferFunction(td
)) {
322 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, "No space for \"TransferFunction\" tag");
325 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0];
326 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
327 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1];
328 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2];
331 case TIFFTAG_REFERENCEBLACKWHITE
:
332 if (!td
->td_refblackwhite
&& !TIFFDefaultRefBlackWhite(td
))
334 *va_arg(ap
, float **) = td
->td_refblackwhite
;
341 * Like TIFFGetField, but return any default
342 * value if the tag is not present in the directory.
345 TIFFGetFieldDefaulted(TIFF
* tif
, uint32 tag
, ...)
351 ok
= TIFFVGetFieldDefaulted(tif
, tag
, ap
);
361 struct _Int64Parts part
;
366 _TIFFUInt64ToFloat(uint64 ui64
)
371 if (i
.part
.high
>= 0) {
372 return (float)i
.value
;
375 df
= (long double)i
.value
;
376 df
+= 18446744073709551616.0; /* adding 2**64 */
382 _TIFFUInt64ToDouble(uint64 ui64
)
387 if (i
.part
.high
>= 0) {
388 return (double)i
.value
;
391 df
= (long double)i
.value
;
392 df
+= 18446744073709551616.0; /* adding 2**64 */
397 float _TIFFClampDoubleToFloat( double val
)
406 int _TIFFSeekOK(TIFF
* tif
, toff_t off
)
408 /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
409 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
410 return off
<= (~(uint64
)0)/2 && TIFFSeekFile(tif
,off
,SEEK_SET
)==off
;
413 /* vim: set ts=8 sts=8 sw=8 noet: */