ReactOS 0.4.16-dev-2613-g9533ad7
tif_aux.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1991-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
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.
12 *
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.
16 *
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
22 * OF THIS SOFTWARE.
23 */
24
25/*
26 * TIFF Library.
27 *
28 * Auxiliary Support Routines.
29 */
30#include "tif_predict.h"
31#include "tiffiop.h"
32#include <float.h>
33#include <math.h>
34
36 const char *where)
37{
38 if (second && first > UINT32_MAX / second)
39 {
40 TIFFErrorExtR(tif, where, "Integer overflow in %s", where);
41 return 0;
42 }
43
44 return first * second;
45}
46
48 const char *where)
49{
50 if (second && first > UINT64_MAX / second)
51 {
52 TIFFErrorExtR(tif, where, "Integer overflow in %s", where);
53 return 0;
54 }
55
56 return first * second;
57}
58
60 const char *where)
61{
62 if (first <= 0 || second <= 0)
63 {
64 if (tif != NULL && where != NULL)
65 {
66 TIFFErrorExtR(tif, where,
67 "Invalid argument to _TIFFMultiplySSize() in %s",
68 where);
69 }
70 return 0;
71 }
72
73 if (first > TIFF_TMSIZE_T_MAX / second)
74 {
75 if (tif != NULL && where != NULL)
76 {
77 TIFFErrorExtR(tif, where, "Integer overflow in %s", where);
78 }
79 return 0;
80 }
81 return first * second;
82}
83
85{
87 {
88 if (tif != NULL && module != NULL)
89 {
90 TIFFErrorExtR(tif, module, "Integer overflow");
91 }
92 return 0;
93 }
94 return (tmsize_t)val;
95}
96
97void *_TIFFCheckRealloc(TIFF *tif, void *buffer, tmsize_t nmemb,
98 tmsize_t elem_size, const char *what)
99{
100 void *cp = NULL;
101 tmsize_t count = _TIFFMultiplySSize(tif, nmemb, elem_size, NULL);
102 /*
103 * Check for integer overflow.
104 */
105 if (count != 0)
106 {
108 }
109
110 if (cp == NULL)
111 {
112 TIFFErrorExtR(tif, tif->tif_name,
113 "Failed to allocate memory for %s "
114 "(%" TIFF_SSIZE_FORMAT " elements of %" TIFF_SSIZE_FORMAT
115 " bytes each)",
116 what, nmemb, elem_size);
117 }
118
119 return cp;
120}
121
122void *_TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size,
123 const char *what)
124{
125 return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
126}
127
129{
131 tmsize_t i, n, nbytes;
132
133 tf[0] = tf[1] = tf[2] = 0;
134 // Do not try to generate a default TransferFunction beyond 24 bits.
135 // This otherwise leads to insane amounts, resulting in denial of service
136 // See https://github.com/OSGeo/gdal/issues/10875
137 if (td->td_bitspersample > 24)
138 return 0;
139
140 n = ((tmsize_t)1) << td->td_bitspersample;
141 nbytes = n * sizeof(uint16_t);
142 tf[0] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
143 if (tf[0] == NULL)
144 return 0;
145 tf[0][0] = 0;
146 for (i = 1; i < n; i++)
147 {
148 double t = (double)i / ((double)n - 1.);
149 tf[0][i] = (uint16_t)floor(65535. * pow(t, 2.2) + .5);
150 }
151
152 if (td->td_samplesperpixel - td->td_extrasamples > 1)
153 {
154 tf[1] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
155 if (tf[1] == NULL)
156 goto bad;
157 _TIFFmemcpy(tf[1], tf[0], nbytes);
158 tf[2] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
159 if (tf[2] == NULL)
160 goto bad;
161 _TIFFmemcpy(tf[2], tf[0], nbytes);
162 }
163 return 1;
164
165bad:
166 if (tf[0])
167 _TIFFfreeExt(tif, tf[0]);
168 if (tf[1])
169 _TIFFfreeExt(tif, tf[1]);
170 if (tf[2])
171 _TIFFfreeExt(tif, tf[2]);
172 tf[0] = tf[1] = tf[2] = 0;
173 return 0;
174}
175
177{
178 int i;
179
180 td->td_refblackwhite = (float *)_TIFFmallocExt(tif, 6 * sizeof(float));
181 if (td->td_refblackwhite == NULL)
182 return 0;
184 {
185 /*
186 * YCbCr (Class Y) images must have the ReferenceBlackWhite
187 * tag set. Fix the broken images, which lacks that tag.
188 */
189 td->td_refblackwhite[0] = 0.0F;
190 td->td_refblackwhite[1] = td->td_refblackwhite[3] =
191 td->td_refblackwhite[5] = 255.0F;
192 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
193 }
194 else
195 {
196 /*
197 * Assume RGB (Class R)
198 */
199 for (i = 0; i < 3; i++)
200 {
201 td->td_refblackwhite[2 * i + 0] = 0;
202 td->td_refblackwhite[2 * i + 1] =
203 (float)((1L << td->td_bitspersample) - 1L);
204 }
205 }
206 return 1;
207}
208
209/*
210 * Like TIFFGetField, but return any default
211 * value if the tag is not present in the directory.
212 *
213 * NB: We use the value in the directory, rather than
214 * explicit values so that defaults exist only one
215 * place in the library -- in TIFFDefaultDirectory.
216 */
218{
219 TIFFDirectory *td = &tif->tif_dir;
220
221 if (TIFFVGetField(tif, tag, ap))
222 return (1);
223 switch (tag)
224 {
226 *va_arg(ap, uint32_t *) = td->td_subfiletype;
227 return (1);
230 return (1);
233 return (1);
235 *va_arg(ap, uint16_t *) = td->td_fillorder;
236 return (1);
238 *va_arg(ap, uint16_t *) = td->td_orientation;
239 return (1);
242 return (1);
245 return (1);
248 return (1);
250 {
251 uint16_t maxsamplevalue;
252 /* td_bitspersample=1 is always set in TIFFDefaultDirectory().
253 * Therefore, td_maxsamplevalue has to be re-calculated in
254 * TIFFGetFieldDefaulted(). */
255 if (td->td_bitspersample > 0)
256 {
257 /* This shift operation into a uint16_t limits the value to
258 * 65535 even if td_bitspersamle is > 16 */
259 if (td->td_bitspersample <= 16)
260 {
261 maxsamplevalue = (1 << td->td_bitspersample) -
262 1; /* 2**(BitsPerSample) - 1 */
263 }
264 else
265 {
266 maxsamplevalue = 65535;
267 }
268 }
269 else
270 {
271 maxsamplevalue = 0;
272 }
273 *va_arg(ap, uint16_t *) = maxsamplevalue;
274 return (1);
275 }
278 return (1);
281 return (1);
283 {
285 if (sp == NULL)
286 {
288 tif, tif->tif_name,
289 "Cannot get \"Predictor\" tag as plugin is not configured");
290 *va_arg(ap, uint16_t *) = 0;
291 return 0;
292 }
293 *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor;
294 return 1;
295 }
296 case TIFFTAG_DOTRANGE:
297 *va_arg(ap, uint16_t *) = 0;
298 *va_arg(ap, uint16_t *) = (1 << td->td_bitspersample) - 1;
299 return (1);
300 case TIFFTAG_INKSET:
302 return 1;
304 *va_arg(ap, uint16_t *) = 4;
305 return (1);
308 *va_arg(ap, const uint16_t **) = td->td_sampleinfo;
309 return (1);
310 case TIFFTAG_MATTEING:
311 *va_arg(ap, uint16_t *) =
312 (td->td_extrasamples == 1 &&
314 return (1);
316 *va_arg(ap, uint32_t *) = td->td_tiledepth;
317 return (1);
318 case TIFFTAG_DATATYPE:
319 *va_arg(ap, uint16_t *) = td->td_sampleformat - 1;
320 return (1);
323 return (1);
325 *va_arg(ap, uint32_t *) = td->td_imagedepth;
326 return (1);
328 {
329 /* defaults are from CCIR Recommendation 601-1 */
330 static const float ycbcrcoeffs[] = {0.299f, 0.587f, 0.114f};
331 *va_arg(ap, const float **) = ycbcrcoeffs;
332 return 1;
333 }
337 return (1);
340 return (1);
342 {
343 /* TIFF 6.0 specification tells that it is no default
344 value for the WhitePoint, but AdobePhotoshop TIFF
345 Technical Note tells that it should be CIE D50. */
346 static const float whitepoint[] = {
347 D50_X0 / (D50_X0 + D50_Y0 + D50_Z0),
348 D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0)};
349 *va_arg(ap, const float **) = whitepoint;
350 return 1;
351 }
353 if (!td->td_transferfunction[0] &&
355 {
356 TIFFErrorExtR(tif, tif->tif_name,
357 "No space for \"TransferFunction\" tag");
358 return (0);
359 }
360 *va_arg(ap, const uint16_t **) = td->td_transferfunction[0];
361 if (td->td_samplesperpixel - td->td_extrasamples > 1)
362 {
363 *va_arg(ap, const uint16_t **) = td->td_transferfunction[1];
364 *va_arg(ap, const uint16_t **) = td->td_transferfunction[2];
365 }
366 return (1);
368 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(tif, td))
369 return (0);
370 *va_arg(ap, const float **) = td->td_refblackwhite;
371 return (1);
372 }
373 return 0;
374}
375
376/*
377 * Like TIFFGetField, but return any default
378 * value if the tag is not present in the directory.
379 */
381{
382 int ok;
383 va_list ap;
384
385 va_start(ap, tag);
387 va_end(ap);
388 return (ok);
389}
390
392{
393 if (val > FLT_MAX)
394 return FLT_MAX;
395 if (val < -FLT_MAX)
396 return -FLT_MAX;
397 return (float)val;
398}
399
401{
402 if (val < 0)
403 return 0;
404 if (val > 0xFFFFFFFFU || val != val)
405 return 0xFFFFFFFFU;
406 return (uint32_t)val;
407}
408
409int _TIFFSeekOK(TIFF *tif, toff_t off)
410{
411 /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
412 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
413 return off <= (~(uint64_t)0) / 2 && TIFFSeekFile(tif, off, SEEK_SET) == off;
414}
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
#define FLT_MAX
Definition: float.h:37
_ACRTIMP double __cdecl floor(double)
Definition: floor.c:18
#define va_end(v)
Definition: stdarg.h:28
#define va_arg(v, l)
Definition: stdarg.h:27
#define va_start(v, l)
Definition: stdarg.h:26
unsigned short uint16_t
Definition: stdint.h:35
#define UINT64_MAX
Definition: stdint.h:86
#define UINT32_MAX
Definition: stdint.h:85
char * va_list
Definition: vadefs.h:50
double pow(double x, double y)
Definition: freeldr.c:179
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLdouble GLdouble t
Definition: gl.h:2047
GLdouble n
Definition: glext.h:7729
GLuint buffer
Definition: glext.h:5915
const GLint * first
Definition: glext.h:5794
GLuint GLfloat * val
Definition: glext.h:7180
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HFONT tf
Definition: icontest.c:17
#define SEEK_SET
Definition: jmemansi.c:26
if(dx< 0)
Definition: linetemp.h:194
POINT cp
Definition: magnifier.c:59
static const WCHAR sp[]
Definition: suminfo.c:287
static const char mbstate_t *static wchar_t const char mbstate_t *static const wchar_t int *static double
Definition: string.c:91
static float(__cdecl *square_half_float)(float x
#define uint64_t
Definition: nsiface.idl:62
#define uint16_t
Definition: nsiface.idl:60
uint32_t td_tiledepth
Definition: tif_dir.h:84
uint16_t td_fillorder
Definition: tif_dir.h:91
uint16_t * td_sampleinfo
Definition: tif_dir.h:106
float * td_refblackwhite
Definition: tif_dir.h:136
uint32_t td_rowsperstrip
Definition: tif_dir.h:94
uint16_t td_photometric
Definition: tif_dir.h:89
uint16_t td_extrasamples
Definition: tif_dir.h:105
uint16_t td_threshholding
Definition: tif_dir.h:90
uint16_t td_minsamplevalue
Definition: tif_dir.h:95
uint16_t td_bitspersample
Definition: tif_dir.h:86
uint16_t * td_transferfunction[3]
Definition: tif_dir.h:135
uint32_t td_imagedepth
Definition: tif_dir.h:83
uint16_t td_orientation
Definition: tif_dir.h:92
uint16_t td_resolutionunit
Definition: tif_dir.h:99
uint16_t td_planarconfig
Definition: tif_dir.h:100
uint16_t td_sampleformat
Definition: tif_dir.h:87
uint16_t td_ycbcrsubsampling[2]
Definition: tif_dir.h:132
uint16_t td_samplesperpixel
Definition: tif_dir.h:93
uint32_t td_subfiletype
Definition: tif_dir.h:85
uint16_t td_ycbcrpositioning
Definition: tif_dir.h:133
Definition: ecma_167.h:138
Definition: tiffiop.h:113
char * tif_name
Definition: tiffiop.h:114
TIFFDirectory tif_dir
Definition: tiffiop.h:157
uint8_t * tif_data
Definition: tiffiop.h:216
int TIFFVGetFieldDefaulted(TIFF *tif, uint32_t tag, va_list ap)
Definition: tif_aux.c:217
uint32_t _TIFFClampDoubleToUInt32(double val)
Definition: tif_aux.c:400
int _TIFFSeekOK(TIFF *tif, toff_t off)
Definition: tif_aux.c:409
int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag,...)
Definition: tif_aux.c:380
uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second, const char *where)
Definition: tif_aux.c:35
void * _TIFFCheckRealloc(TIFF *tif, void *buffer, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Definition: tif_aux.c:97
static int TIFFDefaultTransferFunction(TIFF *tif, TIFFDirectory *td)
Definition: tif_aux.c:128
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Definition: tif_aux.c:122
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Definition: tif_aux.c:84
float _TIFFClampDoubleToFloat(double val)
Definition: tif_aux.c:391
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Definition: tif_aux.c:47
static int TIFFDefaultRefBlackWhite(TIFF *tif, TIFFDirectory *td)
Definition: tif_aux.c:176
tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
Definition: tif_aux.c:59
#define TIFF_SSIZE_FORMAT
Definition: tif_config.h:176
int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
Definition: tif_dir.c:1599
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_error.c:107
void _TIFFfreeExt(TIFF *tif, void *p)
Definition: tif_open.c:275
void * _TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
Definition: tif_open.c:235
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Definition: tif_open.c:173
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:355
#define TIFFTAG_BITSPERSAMPLE
Definition: tiff.h:180
#define TIFFTAG_RESOLUTIONUNIT
Definition: tiff.h:287
#define TIFFTAG_WHITEPOINT
Definition: tiff.h:307
#define TIFFTAG_DOTRANGE
Definition: tiff.h:327
#define EXTRASAMPLE_ASSOCALPHA
Definition: tiff.h:331
#define TIFFTAG_FILLORDER
Definition: tiff.h:240
#define TIFFTAG_SAMPLESPERPIXEL
Definition: tiff.h:257
#define TIFFTAG_INKSET
Definition: tiff.h:322
#define TIFFTAG_DATATYPE
Definition: tiff.h:406
#define TIFFTAG_IMAGEDEPTH
Definition: tiff.h:407
#define TIFFTAG_MAXSAMPLEVALUE
Definition: tiff.h:261
#define TIFFTAG_TILEDEPTH
Definition: tiff.h:408
#define TIFFTAG_EXTRASAMPLES
Definition: tiff.h:329
#define TIFFTAG_YCBCRCOEFFICIENTS
Definition: tiff.h:387
#define TIFFTAG_ORIENTATION
Definition: tiff.h:248
#define TIFFTAG_MATTEING
Definition: tiff.h:405
#define TIFFTAG_MINSAMPLEVALUE
Definition: tiff.h:260
#define TIFFTAG_NUMBEROFINKS
Definition: tiff.h:326
#define PHOTOMETRIC_YCBCR
Definition: tiff.h:227
#define TIFFTAG_TRANSFERFUNCTION
Definition: tiff.h:298
#define INKSET_CMYK
Definition: tiff.h:323
#define TIFFTAG_YCBCRSUBSAMPLING
Definition: tiff.h:388
#define TIFFTAG_ROWSPERSTRIP
Definition: tiff.h:258
#define TIFFTAG_SAMPLEFORMAT
Definition: tiff.h:333
#define TIFFTAG_REFERENCEBLACKWHITE
Definition: tiff.h:392
#define TIFFTAG_PLANARCONFIG
Definition: tiff.h:264
#define TIFFTAG_SUBFILETYPE
Definition: tiff.h:170
#define TIFFTAG_PREDICTOR
Definition: tiff.h:303
#define TIFFTAG_YCBCRPOSITIONING
Definition: tiff.h:389
#define TIFFTAG_THRESHHOLDING
Definition: tiff.h:234
#define D50_X0
Definition: tiffio.h:132
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:67
#define TIFF_TMSIZE_T_MAX
Definition: tiffio.h:68
uint64_t toff_t
Definition: tiffio.h:70
#define D50_Y0
Definition: tiffio.h:133
#define D50_Z0
Definition: tiffio.h:134
#define TIFFSeekFile(tif, off, whence)
Definition: tiffiop.h:282
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36