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.
30 #include "tif_predict.h"
35 uint32_t _TIFFMultiply32(TIFF
*tif
, uint32_t first
, uint32_t second
,
38 if (second
&& first
> UINT32_MAX
/ second
)
40 TIFFErrorExtR(tif
, where
, "Integer overflow in %s", where
);
44 return first
* second
;
47 uint64_t _TIFFMultiply64(TIFF
*tif
, uint64_t first
, uint64_t second
,
50 if (second
&& first
> UINT64_MAX
/ second
)
52 TIFFErrorExtR(tif
, where
, "Integer overflow in %s", where
);
56 return first
* second
;
59 tmsize_t
_TIFFMultiplySSize(TIFF
*tif
, tmsize_t first
, tmsize_t second
,
62 if (first
<= 0 || second
<= 0)
64 if (tif
!= NULL
&& where
!= NULL
)
66 TIFFErrorExtR(tif
, where
,
67 "Invalid argument to _TIFFMultiplySSize() in %s",
73 if (first
> TIFF_TMSIZE_T_MAX
/ second
)
75 if (tif
!= NULL
&& where
!= NULL
)
77 TIFFErrorExtR(tif
, where
, "Integer overflow in %s", where
);
81 return first
* second
;
84 tmsize_t
_TIFFCastUInt64ToSSize(TIFF
*tif
, uint64_t val
, const char *module
)
86 if (val
> (uint64_t)TIFF_TMSIZE_T_MAX
)
88 if (tif
!= NULL
&& module
!= NULL
)
90 TIFFErrorExtR(tif
, module
, "Integer overflow");
97 void *_TIFFCheckRealloc(TIFF
*tif
, void *buffer
, tmsize_t nmemb
,
98 tmsize_t elem_size
, const char *what
)
101 tmsize_t count
= _TIFFMultiplySSize(tif
, nmemb
, elem_size
, NULL
);
103 * Check for integer overflow.
107 cp
= _TIFFreallocExt(tif
, buffer
, count
);
112 TIFFErrorExtR(tif
, tif
->tif_name
,
113 "Failed to allocate memory for %s "
114 "(%" TIFF_SSIZE_FORMAT
" elements of %" TIFF_SSIZE_FORMAT
116 what
, nmemb
, elem_size
);
122 void *_TIFFCheckMalloc(TIFF
*tif
, tmsize_t nmemb
, tmsize_t elem_size
,
125 return _TIFFCheckRealloc(tif
, NULL
, nmemb
, elem_size
, what
);
128 static int TIFFDefaultTransferFunction(TIFF
*tif
, TIFFDirectory
*td
)
130 uint16_t **tf
= td
->td_transferfunction
;
131 tmsize_t i
, n
, nbytes
;
133 tf
[0] = tf
[1] = tf
[2] = 0;
134 if (td
->td_bitspersample
>= sizeof(tmsize_t
) * 8 - 2)
137 n
= ((tmsize_t
)1) << td
->td_bitspersample
;
138 nbytes
= n
* sizeof(uint16_t);
139 tf
[0] = (uint16_t *)_TIFFmallocExt(tif
, nbytes
);
143 for (i
= 1; i
< n
; i
++)
145 double t
= (double)i
/ ((double)n
- 1.);
146 tf
[0][i
] = (uint16_t)floor(65535. * pow(t
, 2.2) + .5);
149 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1)
151 tf
[1] = (uint16_t *)_TIFFmallocExt(tif
, nbytes
);
154 _TIFFmemcpy(tf
[1], tf
[0], nbytes
);
155 tf
[2] = (uint16_t *)_TIFFmallocExt(tif
, nbytes
);
158 _TIFFmemcpy(tf
[2], tf
[0], nbytes
);
164 _TIFFfreeExt(tif
, tf
[0]);
166 _TIFFfreeExt(tif
, tf
[1]);
168 _TIFFfreeExt(tif
, tf
[2]);
169 tf
[0] = tf
[1] = tf
[2] = 0;
173 static int TIFFDefaultRefBlackWhite(TIFF
*tif
, TIFFDirectory
*td
)
177 td
->td_refblackwhite
= (float *)_TIFFmallocExt(tif
, 6 * sizeof(float));
178 if (td
->td_refblackwhite
== NULL
)
180 if (td
->td_photometric
== PHOTOMETRIC_YCBCR
)
183 * YCbCr (Class Y) images must have the ReferenceBlackWhite
184 * tag set. Fix the broken images, which lacks that tag.
186 td
->td_refblackwhite
[0] = 0.0F
;
187 td
->td_refblackwhite
[1] = td
->td_refblackwhite
[3] =
188 td
->td_refblackwhite
[5] = 255.0F
;
189 td
->td_refblackwhite
[2] = td
->td_refblackwhite
[4] = 128.0F
;
194 * Assume RGB (Class R)
196 for (i
= 0; i
< 3; i
++)
198 td
->td_refblackwhite
[2 * i
+ 0] = 0;
199 td
->td_refblackwhite
[2 * i
+ 1] =
200 (float)((1L << td
->td_bitspersample
) - 1L);
207 * Like TIFFGetField, but return any default
208 * value if the tag is not present in the directory.
210 * NB: We use the value in the directory, rather than
211 * explicit values so that defaults exist only one
212 * place in the library -- in TIFFDefaultDirectory.
214 int TIFFVGetFieldDefaulted(TIFF
*tif
, uint32_t tag
, va_list ap
)
216 TIFFDirectory
*td
= &tif
->tif_dir
;
218 if (TIFFVGetField(tif
, tag
, ap
))
222 case TIFFTAG_SUBFILETYPE
:
223 *va_arg(ap
, uint32_t *) = td
->td_subfiletype
;
225 case TIFFTAG_BITSPERSAMPLE
:
226 *va_arg(ap
, uint16_t *) = td
->td_bitspersample
;
228 case TIFFTAG_THRESHHOLDING
:
229 *va_arg(ap
, uint16_t *) = td
->td_threshholding
;
231 case TIFFTAG_FILLORDER
:
232 *va_arg(ap
, uint16_t *) = td
->td_fillorder
;
234 case TIFFTAG_ORIENTATION
:
235 *va_arg(ap
, uint16_t *) = td
->td_orientation
;
237 case TIFFTAG_SAMPLESPERPIXEL
:
238 *va_arg(ap
, uint16_t *) = td
->td_samplesperpixel
;
240 case TIFFTAG_ROWSPERSTRIP
:
241 *va_arg(ap
, uint32_t *) = td
->td_rowsperstrip
;
243 case TIFFTAG_MINSAMPLEVALUE
:
244 *va_arg(ap
, uint16_t *) = td
->td_minsamplevalue
;
246 case TIFFTAG_MAXSAMPLEVALUE
:
248 uint16_t maxsamplevalue
;
249 /* td_bitspersample=1 is always set in TIFFDefaultDirectory().
250 * Therefore, td_maxsamplevalue has to be re-calculated in
251 * TIFFGetFieldDefaulted(). */
252 if (td
->td_bitspersample
> 0)
254 /* This shift operation into a uint16_t limits the value to
255 * 65535 even if td_bitspersamle is > 16 */
256 if (td
->td_bitspersample
<= 16)
258 maxsamplevalue
= (1 << td
->td_bitspersample
) -
259 1; /* 2**(BitsPerSample) - 1 */
263 maxsamplevalue
= 65535;
270 *va_arg(ap
, uint16_t *) = maxsamplevalue
;
273 case TIFFTAG_PLANARCONFIG
:
274 *va_arg(ap
, uint16_t *) = td
->td_planarconfig
;
276 case TIFFTAG_RESOLUTIONUNIT
:
277 *va_arg(ap
, uint16_t *) = td
->td_resolutionunit
;
279 case TIFFTAG_PREDICTOR
:
281 TIFFPredictorState
*sp
= (TIFFPredictorState
*)tif
->tif_data
;
286 "Cannot get \"Predictor\" tag as plugin is not configured");
287 *va_arg(ap
, uint16_t *) = 0;
290 *va_arg(ap
, uint16_t *) = (uint16_t)sp
->predictor
;
293 case TIFFTAG_DOTRANGE
:
294 *va_arg(ap
, uint16_t *) = 0;
295 *va_arg(ap
, uint16_t *) = (1 << td
->td_bitspersample
) - 1;
298 *va_arg(ap
, uint16_t *) = INKSET_CMYK
;
300 case TIFFTAG_NUMBEROFINKS
:
301 *va_arg(ap
, uint16_t *) = 4;
303 case TIFFTAG_EXTRASAMPLES
:
304 *va_arg(ap
, uint16_t *) = td
->td_extrasamples
;
305 *va_arg(ap
, const uint16_t **) = td
->td_sampleinfo
;
307 case TIFFTAG_MATTEING
:
308 *va_arg(ap
, uint16_t *) =
309 (td
->td_extrasamples
== 1 &&
310 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
312 case TIFFTAG_TILEDEPTH
:
313 *va_arg(ap
, uint32_t *) = td
->td_tiledepth
;
315 case TIFFTAG_DATATYPE
:
316 *va_arg(ap
, uint16_t *) = td
->td_sampleformat
- 1;
318 case TIFFTAG_SAMPLEFORMAT
:
319 *va_arg(ap
, uint16_t *) = td
->td_sampleformat
;
321 case TIFFTAG_IMAGEDEPTH
:
322 *va_arg(ap
, uint32_t *) = td
->td_imagedepth
;
324 case TIFFTAG_YCBCRCOEFFICIENTS
:
326 /* defaults are from CCIR Recommendation 601-1 */
327 static const float ycbcrcoeffs
[] = {0.299f
, 0.587f
, 0.114f
};
328 *va_arg(ap
, const float **) = ycbcrcoeffs
;
331 case TIFFTAG_YCBCRSUBSAMPLING
:
332 *va_arg(ap
, uint16_t *) = td
->td_ycbcrsubsampling
[0];
333 *va_arg(ap
, uint16_t *) = td
->td_ycbcrsubsampling
[1];
335 case TIFFTAG_YCBCRPOSITIONING
:
336 *va_arg(ap
, uint16_t *) = td
->td_ycbcrpositioning
;
338 case TIFFTAG_WHITEPOINT
:
340 /* TIFF 6.0 specification tells that it is no default
341 value for the WhitePoint, but AdobePhotoshop TIFF
342 Technical Note tells that it should be CIE D50. */
343 static const float whitepoint
[] = {
344 D50_X0
/ (D50_X0
+ D50_Y0
+ D50_Z0
),
345 D50_Y0
/ (D50_X0
+ D50_Y0
+ D50_Z0
)};
346 *va_arg(ap
, const float **) = whitepoint
;
349 case TIFFTAG_TRANSFERFUNCTION
:
350 if (!td
->td_transferfunction
[0] &&
351 !TIFFDefaultTransferFunction(tif
, td
))
353 TIFFErrorExtR(tif
, tif
->tif_name
,
354 "No space for \"TransferFunction\" tag");
357 *va_arg(ap
, const uint16_t **) = td
->td_transferfunction
[0];
358 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1)
360 *va_arg(ap
, const uint16_t **) = td
->td_transferfunction
[1];
361 *va_arg(ap
, const uint16_t **) = td
->td_transferfunction
[2];
364 case TIFFTAG_REFERENCEBLACKWHITE
:
365 if (!td
->td_refblackwhite
&& !TIFFDefaultRefBlackWhite(tif
, td
))
367 *va_arg(ap
, const float **) = td
->td_refblackwhite
;
374 * Like TIFFGetField, but return any default
375 * value if the tag is not present in the directory.
377 int TIFFGetFieldDefaulted(TIFF
*tif
, uint32_t tag
, ...)
383 ok
= TIFFVGetFieldDefaulted(tif
, tag
, ap
);
395 struct _Int64Parts part
;
399 float _TIFFUInt64ToFloat(uint64_t ui64
)
404 if (i
.part
.high
>= 0)
406 return (float)i
.value
;
411 df
= (long double)i
.value
;
412 df
+= 18446744073709551616.0; /* adding 2**64 */
417 double _TIFFUInt64ToDouble(uint64_t ui64
)
422 if (i
.part
.high
>= 0)
424 return (double)i
.value
;
429 df
= (long double)i
.value
;
430 df
+= 18446744073709551616.0; /* adding 2**64 */
435 float _TIFFClampDoubleToFloat(double val
)
444 uint32_t _TIFFClampDoubleToUInt32(double val
)
448 if (val
> 0xFFFFFFFFU
|| val
!= val
)
450 return (uint32_t)val
;
453 int _TIFFSeekOK(TIFF
*tif
, toff_t off
)
455 /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
456 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
457 return off
<= (~(uint64_t)0) / 2 && TIFFSeekFile(tif
, off
, SEEK_SET
) == off
;