1 /* $Id: tif_aux.c,v 1.31 2017-11-17 20:21:00 erouault 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 tf
[0] = (uint16
*)_TIFFmalloc(nbytes
);
108 for (i
= 1; i
< n
; i
++) {
109 double t
= (double)i
/((double) n
-1.);
110 tf
[0][i
] = (uint16
)floor(65535.*pow(t
, 2.2) + .5);
113 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
114 tf
[1] = (uint16
*)_TIFFmalloc(nbytes
);
117 _TIFFmemcpy(tf
[1], tf
[0], nbytes
);
118 tf
[2] = (uint16
*)_TIFFmalloc(nbytes
);
121 _TIFFmemcpy(tf
[2], tf
[0], nbytes
);
132 tf
[0] = tf
[1] = tf
[2] = 0;
137 TIFFDefaultRefBlackWhite(TIFFDirectory
* td
)
141 td
->td_refblackwhite
= (float *)_TIFFmalloc(6*sizeof (float));
142 if (td
->td_refblackwhite
== NULL
)
144 if (td
->td_photometric
== PHOTOMETRIC_YCBCR
) {
146 * YCbCr (Class Y) images must have the ReferenceBlackWhite
147 * tag set. Fix the broken images, which lacks that tag.
149 td
->td_refblackwhite
[0] = 0.0F
;
150 td
->td_refblackwhite
[1] = td
->td_refblackwhite
[3] =
151 td
->td_refblackwhite
[5] = 255.0F
;
152 td
->td_refblackwhite
[2] = td
->td_refblackwhite
[4] = 128.0F
;
155 * Assume RGB (Class R)
157 for (i
= 0; i
< 3; i
++) {
158 td
->td_refblackwhite
[2*i
+0] = 0;
159 td
->td_refblackwhite
[2*i
+1] =
160 (float)((1L<<td
->td_bitspersample
)-1L);
167 * Like TIFFGetField, but return any default
168 * value if the tag is not present in the directory.
170 * NB: We use the value in the directory, rather than
171 * explicit values so that defaults exist only one
172 * place in the library -- in TIFFDefaultDirectory.
175 TIFFVGetFieldDefaulted(TIFF
* tif
, uint32 tag
, va_list ap
)
177 TIFFDirectory
*td
= &tif
->tif_dir
;
179 if (TIFFVGetField(tif
, tag
, ap
))
182 case TIFFTAG_SUBFILETYPE
:
183 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
185 case TIFFTAG_BITSPERSAMPLE
:
186 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
188 case TIFFTAG_THRESHHOLDING
:
189 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
191 case TIFFTAG_FILLORDER
:
192 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
194 case TIFFTAG_ORIENTATION
:
195 *va_arg(ap
, uint16
*) = td
->td_orientation
;
197 case TIFFTAG_SAMPLESPERPIXEL
:
198 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
200 case TIFFTAG_ROWSPERSTRIP
:
201 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
203 case TIFFTAG_MINSAMPLEVALUE
:
204 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
206 case TIFFTAG_MAXSAMPLEVALUE
:
207 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
209 case TIFFTAG_PLANARCONFIG
:
210 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
212 case TIFFTAG_RESOLUTIONUNIT
:
213 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
215 case TIFFTAG_PREDICTOR
:
217 TIFFPredictorState
* sp
= (TIFFPredictorState
*) tif
->tif_data
;
220 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
221 "Cannot get \"Predictor\" tag as plugin is not configured");
222 *va_arg(ap
, uint16
*) = 0;
225 *va_arg(ap
, uint16
*) = (uint16
) sp
->predictor
;
228 case TIFFTAG_DOTRANGE
:
229 *va_arg(ap
, uint16
*) = 0;
230 *va_arg(ap
, uint16
*) = (1<<td
->td_bitspersample
)-1;
233 *va_arg(ap
, uint16
*) = INKSET_CMYK
;
235 case TIFFTAG_NUMBEROFINKS
:
236 *va_arg(ap
, uint16
*) = 4;
238 case TIFFTAG_EXTRASAMPLES
:
239 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
240 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
242 case TIFFTAG_MATTEING
:
243 *va_arg(ap
, uint16
*) =
244 (td
->td_extrasamples
== 1 &&
245 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
247 case TIFFTAG_TILEDEPTH
:
248 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
250 case TIFFTAG_DATATYPE
:
251 *va_arg(ap
, uint16
*) = td
->td_sampleformat
-1;
253 case TIFFTAG_SAMPLEFORMAT
:
254 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
256 case TIFFTAG_IMAGEDEPTH
:
257 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
259 case TIFFTAG_YCBCRCOEFFICIENTS
:
261 /* defaults are from CCIR Recommendation 601-1 */
262 static float ycbcrcoeffs
[] = { 0.299f
, 0.587f
, 0.114f
};
263 *va_arg(ap
, float **) = ycbcrcoeffs
;
266 case TIFFTAG_YCBCRSUBSAMPLING
:
267 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
268 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
270 case TIFFTAG_YCBCRPOSITIONING
:
271 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
273 case TIFFTAG_WHITEPOINT
:
275 static float whitepoint
[2];
277 /* TIFF 6.0 specification tells that it is no default
278 value for the WhitePoint, but AdobePhotoshop TIFF
279 Technical Note tells that it should be CIE D50. */
280 whitepoint
[0] = D50_X0
/ (D50_X0
+ D50_Y0
+ D50_Z0
);
281 whitepoint
[1] = D50_Y0
/ (D50_X0
+ D50_Y0
+ D50_Z0
);
282 *va_arg(ap
, float **) = whitepoint
;
285 case TIFFTAG_TRANSFERFUNCTION
:
286 if (!td
->td_transferfunction
[0] &&
287 !TIFFDefaultTransferFunction(td
)) {
288 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, "No space for \"TransferFunction\" tag");
291 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0];
292 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
293 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1];
294 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2];
297 case TIFFTAG_REFERENCEBLACKWHITE
:
298 if (!td
->td_refblackwhite
&& !TIFFDefaultRefBlackWhite(td
))
300 *va_arg(ap
, float **) = td
->td_refblackwhite
;
307 * Like TIFFGetField, but return any default
308 * value if the tag is not present in the directory.
311 TIFFGetFieldDefaulted(TIFF
* tif
, uint32 tag
, ...)
317 ok
= TIFFVGetFieldDefaulted(tif
, tag
, ap
);
327 struct _Int64Parts part
;
332 _TIFFUInt64ToFloat(uint64 ui64
)
337 if (i
.part
.high
>= 0) {
338 return (float)i
.value
;
341 df
= (long double)i
.value
;
342 df
+= 18446744073709551616.0; /* adding 2**64 */
348 _TIFFUInt64ToDouble(uint64 ui64
)
353 if (i
.part
.high
>= 0) {
354 return (double)i
.value
;
357 df
= (long double)i
.value
;
358 df
+= 18446744073709551616.0; /* adding 2**64 */
363 int _TIFFSeekOK(TIFF
* tif
, toff_t off
)
365 /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
366 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
367 return off
<= (~(uint64
)0)/2 && TIFFSeekFile(tif
,off
,SEEK_SET
)==off
;
370 /* vim: set ts=8 sts=8 sw=8 noet: */