]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/libtiff/tif_dir.c
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / tiff / libtiff / tif_dir.c
CommitLineData
8414a40c
VZ
1
2/*
3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26/*
27 * TIFF Library.
28 *
29 * Directory Tag Get & Set Routines.
30 * (and also some miscellaneous stuff)
31 */
32#include "tiffiop.h"
33
34/*
35 * These are used in the backwards compatibility code...
36 */
37#define DATATYPE_VOID 0 /* !untyped data */
38#define DATATYPE_INT 1 /* !signed integer data */
39#define DATATYPE_UINT 2 /* !unsigned integer data */
40#define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
41
42static void
43setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
44{
45 if (*vpp)
46 _TIFFfree(*vpp), *vpp = 0;
47 if (vp) {
80ed523f 48 tmsize_t bytes = (tmsize_t)(nmemb * elem_size);
8414a40c
VZ
49 if (elem_size && bytes / elem_size == nmemb)
50 *vpp = (void*) _TIFFmalloc(bytes);
51 if (*vpp)
52 _TIFFmemcpy(*vpp, vp, bytes);
53 }
54}
55void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
56 { setByteArray(vpp, vp, n, 1); }
57void _TIFFsetString(char** cpp, char* cp)
58 { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
59void _TIFFsetNString(char** cpp, char* cp, uint32 n)
60 { setByteArray((void**) cpp, (void*) cp, n, 1); }
61void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
62 { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
63void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
64 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
80ed523f
VZ
65void _TIFFsetLong8Array(uint64** lpp, uint64* lp, uint32 n)
66 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint64)); }
8414a40c
VZ
67void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
68 { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
69void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
70 { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
71
80ed523f
VZ
72static void
73setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
74{
75 if (*vpp)
76 _TIFFfree(*vpp);
77 *vpp = _TIFFmalloc(nmemb*sizeof(double));
78 if (*vpp)
79 {
80 while (nmemb--)
81 ((double*)*vpp)[nmemb] = value;
82 }
83}
84
8414a40c
VZ
85/*
86 * Install extra samples information.
87 */
88static int
89setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
90{
80ed523f
VZ
91/* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
92#define EXTRASAMPLE_COREL_UNASSALPHA 999
93
8414a40c
VZ
94 uint16* va;
95 uint32 i;
96
80ed523f 97 *v = (uint16) va_arg(ap, uint16_vap);
8414a40c 98 if ((uint16) *v > td->td_samplesperpixel)
80ed523f 99 return 0;
8414a40c
VZ
100 va = va_arg(ap, uint16*);
101 if (*v > 0 && va == NULL) /* typically missing param */
80ed523f
VZ
102 return 0;
103 for (i = 0; i < *v; i++) {
104 if (va[i] > EXTRASAMPLE_UNASSALPHA) {
105 /*
106 * XXX: Corel Draw is known to produce incorrect
107 * ExtraSamples tags which must be patched here if we
108 * want to be able to open some of the damaged TIFF
109 * files:
110 */
111 if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
112 va[i] = EXTRASAMPLE_UNASSALPHA;
113 else
114 return 0;
115 }
116 }
8414a40c
VZ
117 td->td_extrasamples = (uint16) *v;
118 _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
80ed523f
VZ
119 return 1;
120
121#undef EXTRASAMPLE_COREL_UNASSALPHA
8414a40c
VZ
122}
123
80ed523f
VZ
124/*
125 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
126 * zero if the ink names are not as expected.
127 */
8414a40c
VZ
128static uint32
129checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
130{
131 TIFFDirectory* td = &tif->tif_dir;
132 uint16 i = td->td_samplesperpixel;
133
134 if (slen > 0) {
135 const char* ep = s+slen;
136 const char* cp = s;
137 for (; i > 0; i--) {
80ed523f
VZ
138 for (; cp < ep && *cp != '\0'; cp++) {}
139 if (cp >= ep)
140 goto bad;
8414a40c
VZ
141 cp++; /* skip \0 */
142 }
80ed523f 143 return ((uint32)(cp-s));
8414a40c
VZ
144 }
145bad:
146 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
147 "%s: Invalid InkNames value; expecting %d names, found %d",
148 tif->tif_name,
149 td->td_samplesperpixel,
150 td->td_samplesperpixel-i);
151 return (0);
152}
153
154static int
80ed523f 155_TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
8414a40c
VZ
156{
157 static const char module[] = "_TIFFVSetField";
80ed523f 158
8414a40c
VZ
159 TIFFDirectory* td = &tif->tif_dir;
160 int status = 1;
161 uint32 v32, i, v;
162 char* s;
80ed523f
VZ
163 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
164 uint32 standard_tag = tag;
165
166 /*
167 * We want to force the custom code to be used for custom
168 * fields even if the tag happens to match a well known
169 * one - important for reinterpreted handling of standard
170 * tag values in custom directories (ie. EXIF)
171 */
172 if (fip->field_bit == FIELD_CUSTOM) {
173 standard_tag = 0;
174 }
8414a40c 175
80ed523f 176 switch (standard_tag) {
8414a40c 177 case TIFFTAG_SUBFILETYPE:
80ed523f 178 td->td_subfiletype = (uint32) va_arg(ap, uint32);
8414a40c
VZ
179 break;
180 case TIFFTAG_IMAGEWIDTH:
80ed523f 181 td->td_imagewidth = (uint32) va_arg(ap, uint32);
8414a40c
VZ
182 break;
183 case TIFFTAG_IMAGELENGTH:
80ed523f 184 td->td_imagelength = (uint32) va_arg(ap, uint32);
8414a40c
VZ
185 break;
186 case TIFFTAG_BITSPERSAMPLE:
80ed523f 187 td->td_bitspersample = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
188 /*
189 * If the data require post-decoding processing to byte-swap
190 * samples, set it up here. Note that since tags are required
191 * to be ordered, compression code can override this behaviour
192 * in the setup method if it wants to roll the post decoding
193 * work in with its normal work.
194 */
195 if (tif->tif_flags & TIFF_SWAB) {
80ed523f
VZ
196 if (td->td_bitspersample == 8)
197 tif->tif_postdecode = _TIFFNoPostDecode;
198 else if (td->td_bitspersample == 16)
8414a40c
VZ
199 tif->tif_postdecode = _TIFFSwab16BitData;
200 else if (td->td_bitspersample == 24)
201 tif->tif_postdecode = _TIFFSwab24BitData;
202 else if (td->td_bitspersample == 32)
203 tif->tif_postdecode = _TIFFSwab32BitData;
204 else if (td->td_bitspersample == 64)
205 tif->tif_postdecode = _TIFFSwab64BitData;
206 else if (td->td_bitspersample == 128) /* two 64's */
207 tif->tif_postdecode = _TIFFSwab64BitData;
208 }
209 break;
210 case TIFFTAG_COMPRESSION:
80ed523f 211 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
212 /*
213 * If we're changing the compression scheme, the notify the
214 * previous module so that it can cleanup any state it's
215 * setup.
216 */
217 if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
80ed523f 218 if ((uint32)td->td_compression == v)
8414a40c
VZ
219 break;
220 (*tif->tif_cleanup)(tif);
221 tif->tif_flags &= ~TIFF_CODERSETUP;
222 }
223 /*
224 * Setup new compression routine state.
225 */
226 if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
80ed523f
VZ
227 td->td_compression = (uint16) v;
228 else
229 status = 0;
8414a40c
VZ
230 break;
231 case TIFFTAG_PHOTOMETRIC:
80ed523f 232 td->td_photometric = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
233 break;
234 case TIFFTAG_THRESHHOLDING:
80ed523f 235 td->td_threshholding = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
236 break;
237 case TIFFTAG_FILLORDER:
80ed523f 238 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
239 if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
240 goto badvalue;
241 td->td_fillorder = (uint16) v;
242 break;
8414a40c 243 case TIFFTAG_ORIENTATION:
80ed523f
VZ
244 v = (uint16) va_arg(ap, uint16_vap);
245 if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
246 goto badvalue;
247 else
8414a40c
VZ
248 td->td_orientation = (uint16) v;
249 break;
250 case TIFFTAG_SAMPLESPERPIXEL:
80ed523f 251 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
252 if (v == 0)
253 goto badvalue;
254 td->td_samplesperpixel = (uint16) v;
255 break;
256 case TIFFTAG_ROWSPERSTRIP:
80ed523f 257 v32 = (uint32) va_arg(ap, uint32);
8414a40c
VZ
258 if (v32 == 0)
259 goto badvalue32;
260 td->td_rowsperstrip = v32;
261 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
262 td->td_tilelength = v32;
263 td->td_tilewidth = td->td_imagewidth;
264 }
265 break;
266 case TIFFTAG_MINSAMPLEVALUE:
80ed523f 267 td->td_minsamplevalue = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
268 break;
269 case TIFFTAG_MAXSAMPLEVALUE:
80ed523f 270 td->td_maxsamplevalue = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
271 break;
272 case TIFFTAG_SMINSAMPLEVALUE:
80ed523f
VZ
273 if (tif->tif_flags & TIFF_PERSAMPLE)
274 _TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
275 else
276 setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
8414a40c
VZ
277 break;
278 case TIFFTAG_SMAXSAMPLEVALUE:
80ed523f
VZ
279 if (tif->tif_flags & TIFF_PERSAMPLE)
280 _TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
281 else
282 setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
8414a40c
VZ
283 break;
284 case TIFFTAG_XRESOLUTION:
285 td->td_xresolution = (float) va_arg(ap, double);
286 break;
287 case TIFFTAG_YRESOLUTION:
288 td->td_yresolution = (float) va_arg(ap, double);
289 break;
290 case TIFFTAG_PLANARCONFIG:
80ed523f 291 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
292 if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
293 goto badvalue;
294 td->td_planarconfig = (uint16) v;
295 break;
296 case TIFFTAG_XPOSITION:
297 td->td_xposition = (float) va_arg(ap, double);
298 break;
299 case TIFFTAG_YPOSITION:
300 td->td_yposition = (float) va_arg(ap, double);
301 break;
302 case TIFFTAG_RESOLUTIONUNIT:
80ed523f 303 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
304 if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
305 goto badvalue;
306 td->td_resolutionunit = (uint16) v;
307 break;
308 case TIFFTAG_PAGENUMBER:
80ed523f
VZ
309 td->td_pagenumber[0] = (uint16) va_arg(ap, uint16_vap);
310 td->td_pagenumber[1] = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
311 break;
312 case TIFFTAG_HALFTONEHINTS:
80ed523f
VZ
313 td->td_halftonehints[0] = (uint16) va_arg(ap, uint16_vap);
314 td->td_halftonehints[1] = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
315 break;
316 case TIFFTAG_COLORMAP:
317 v32 = (uint32)(1L<<td->td_bitspersample);
318 _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
319 _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
320 _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
321 break;
322 case TIFFTAG_EXTRASAMPLES:
323 if (!setExtraSamples(td, ap, &v))
324 goto badvalue;
325 break;
326 case TIFFTAG_MATTEING:
80ed523f 327 td->td_extrasamples = (((uint16) va_arg(ap, uint16_vap)) != 0);
8414a40c
VZ
328 if (td->td_extrasamples) {
329 uint16 sv = EXTRASAMPLE_ASSOCALPHA;
330 _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
331 }
332 break;
333 case TIFFTAG_TILEWIDTH:
80ed523f 334 v32 = (uint32) va_arg(ap, uint32);
8414a40c
VZ
335 if (v32 % 16) {
336 if (tif->tif_mode != O_RDONLY)
337 goto badvalue32;
338 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
339 "Nonstandard tile width %d, convert file", v32);
340 }
341 td->td_tilewidth = v32;
342 tif->tif_flags |= TIFF_ISTILED;
343 break;
344 case TIFFTAG_TILELENGTH:
80ed523f 345 v32 = (uint32) va_arg(ap, uint32);
8414a40c
VZ
346 if (v32 % 16) {
347 if (tif->tif_mode != O_RDONLY)
348 goto badvalue32;
349 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
350 "Nonstandard tile length %d, convert file", v32);
351 }
352 td->td_tilelength = v32;
353 tif->tif_flags |= TIFF_ISTILED;
354 break;
355 case TIFFTAG_TILEDEPTH:
80ed523f 356 v32 = (uint32) va_arg(ap, uint32);
8414a40c
VZ
357 if (v32 == 0)
358 goto badvalue32;
359 td->td_tiledepth = v32;
360 break;
361 case TIFFTAG_DATATYPE:
80ed523f 362 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
363 switch (v) {
364 case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
365 case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
366 case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
367 case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
368 default: goto badvalue;
369 }
370 td->td_sampleformat = (uint16) v;
371 break;
372 case TIFFTAG_SAMPLEFORMAT:
80ed523f 373 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
374 if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
375 goto badvalue;
376 td->td_sampleformat = (uint16) v;
377
80ed523f
VZ
378 /* Try to fix up the SWAB function for complex data. */
379 if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
380 && td->td_bitspersample == 32
381 && tif->tif_postdecode == _TIFFSwab32BitData )
382 tif->tif_postdecode = _TIFFSwab16BitData;
383 else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
384 || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
385 && td->td_bitspersample == 64
386 && tif->tif_postdecode == _TIFFSwab64BitData )
387 tif->tif_postdecode = _TIFFSwab32BitData;
8414a40c
VZ
388 break;
389 case TIFFTAG_IMAGEDEPTH:
80ed523f 390 td->td_imagedepth = (uint32) va_arg(ap, uint32);
8414a40c
VZ
391 break;
392 case TIFFTAG_SUBIFD:
393 if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
80ed523f
VZ
394 td->td_nsubifd = (uint16) va_arg(ap, uint16_vap);
395 _TIFFsetLong8Array(&td->td_subifd, (uint64*) va_arg(ap, uint64*),
8414a40c
VZ
396 (long) td->td_nsubifd);
397 } else {
80ed523f
VZ
398 TIFFErrorExt(tif->tif_clientdata, module,
399 "%s: Sorry, cannot nest SubIFDs",
400 tif->tif_name);
8414a40c
VZ
401 status = 0;
402 }
403 break;
404 case TIFFTAG_YCBCRPOSITIONING:
80ed523f 405 td->td_ycbcrpositioning = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
406 break;
407 case TIFFTAG_YCBCRSUBSAMPLING:
80ed523f
VZ
408 td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, uint16_vap);
409 td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
410 break;
411 case TIFFTAG_TRANSFERFUNCTION:
412 v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
413 for (i = 0; i < v; i++)
414 _TIFFsetShortArray(&td->td_transferfunction[i],
415 va_arg(ap, uint16*), 1L<<td->td_bitspersample);
416 break;
80ed523f
VZ
417 case TIFFTAG_REFERENCEBLACKWHITE:
418 /* XXX should check for null range */
419 _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
420 break;
8414a40c 421 case TIFFTAG_INKNAMES:
80ed523f 422 v = (uint16) va_arg(ap, uint16_vap);
8414a40c
VZ
423 s = va_arg(ap, char*);
424 v = checkInkNamesString(tif, v, s);
80ed523f 425 status = v > 0;
8414a40c
VZ
426 if( v > 0 ) {
427 _TIFFsetNString(&td->td_inknames, s, v);
428 td->td_inknameslen = v;
429 }
430 break;
80ed523f
VZ
431 case TIFFTAG_PERSAMPLE:
432 v = (uint16) va_arg(ap, uint16_vap);
433 if( v == PERSAMPLE_MULTI )
434 tif->tif_flags |= TIFF_PERSAMPLE;
435 else
436 tif->tif_flags &= ~TIFF_PERSAMPLE;
8414a40c 437 break;
80ed523f
VZ
438 default: {
439 TIFFTagValue *tv;
440 int tv_size, iCustom;
8414a40c 441
80ed523f
VZ
442 /*
443 * This can happen if multiple images are open with different
444 * codecs which have private tags. The global tag information
445 * table may then have tags that are valid for one file but not
446 * the other. If the client tries to set a tag that is not valid
447 * for the image's codec then we'll arrive here. This
448 * happens, for example, when tiffcp is used to convert between
449 * compression schemes and codec-specific tags are blindly copied.
450 */
451 if(fip == NULL || fip->field_bit != FIELD_CUSTOM) {
8414a40c 452 TIFFErrorExt(tif->tif_clientdata, module,
80ed523f
VZ
453 "%s: Invalid %stag \"%s\" (not supported by codec)",
454 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
455 fip ? fip->field_name : "Unknown");
8414a40c 456 status = 0;
80ed523f 457 break;
8414a40c
VZ
458 }
459
80ed523f
VZ
460 /*
461 * Find the existing entry for this custom value.
462 */
463 tv = NULL;
464 for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
465 if (td->td_customValues[iCustom].info->field_tag == tag) {
466 tv = td->td_customValues + iCustom;
467 if (tv->value != NULL) {
468 _TIFFfree(tv->value);
469 tv->value = NULL;
470 }
471 break;
472 }
473 }
8414a40c 474
80ed523f
VZ
475 /*
476 * Grow the custom list if the entry was not found.
477 */
478 if(tv == NULL) {
479 TIFFTagValue *new_customValues;
8414a40c 480
80ed523f
VZ
481 td->td_customValueCount++;
482 new_customValues = (TIFFTagValue *)
483 _TIFFrealloc(td->td_customValues,
484 sizeof(TIFFTagValue) * td->td_customValueCount);
485 if (!new_customValues) {
486 TIFFErrorExt(tif->tif_clientdata, module,
487 "%s: Failed to allocate space for list of custom values",
488 tif->tif_name);
489 status = 0;
490 goto end;
491 }
492
493 td->td_customValues = new_customValues;
494
495 tv = td->td_customValues + (td->td_customValueCount - 1);
496 tv->info = fip;
497 tv->value = NULL;
498 tv->count = 0;
8414a40c
VZ
499 }
500
80ed523f
VZ
501 /*
502 * Set custom value ... save a copy of the custom tag value.
503 */
504 tv_size = _TIFFDataSize(fip->field_type);
505 if (tv_size == 0) {
506 status = 0;
507 TIFFErrorExt(tif->tif_clientdata, module,
508 "%s: Bad field type %d for \"%s\"",
509 tif->tif_name, fip->field_type,
510 fip->field_name);
511 goto end;
512 }
513
514 if (fip->field_type == TIFF_ASCII)
515 {
516 uint32 ma;
517 char* mb;
518 if (fip->field_passcount)
519 {
520 assert(fip->field_writecount==TIFF_VARIABLE2);
521 ma=(uint32)va_arg(ap,uint32);
522 mb=(char*)va_arg(ap,char*);
523 }
524 else
525 {
526 mb=(char*)va_arg(ap,char*);
527 ma=(uint32)(strlen(mb)+1);
528 }
529 tv->count=ma;
530 setByteArray(&tv->value,mb,ma,1);
531 }
532 else
533 {
534 if (fip->field_passcount) {
535 if (fip->field_writecount == TIFF_VARIABLE2)
536 tv->count = (uint32) va_arg(ap, uint32);
537 else
538 tv->count = (int) va_arg(ap, int);
539 } else if (fip->field_writecount == TIFF_VARIABLE
540 || fip->field_writecount == TIFF_VARIABLE2)
541 tv->count = 1;
542 else if (fip->field_writecount == TIFF_SPP)
543 tv->count = td->td_samplesperpixel;
544 else
545 tv->count = fip->field_writecount;
546
547 if (tv->count == 0) {
548 status = 0;
549 TIFFErrorExt(tif->tif_clientdata, module,
550 "%s: Null count for \"%s\" (type "
551 "%d, writecount %d, passcount %d)",
552 tif->tif_name,
553 fip->field_name,
554 fip->field_type,
555 fip->field_writecount,
556 fip->field_passcount);
557 goto end;
558 }
559
560 tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
561 "custom tag binary object");
562 if (!tv->value) {
563 status = 0;
564 goto end;
565 }
566
567 if (fip->field_tag == TIFFTAG_DOTRANGE
568 && strcmp(fip->field_name,"DotRange") == 0) {
569 /* TODO: This is an evil exception and should not have been
570 handled this way ... likely best if we move it into
571 the directory structure with an explicit field in
572 libtiff 4.1 and assign it a FIELD_ value */
573 uint16 v[2];
574 v[0] = (uint16)va_arg(ap, int);
575 v[1] = (uint16)va_arg(ap, int);
576 _TIFFmemcpy(tv->value, &v, 4);
577 }
578
579 else if (fip->field_passcount
580 || fip->field_writecount == TIFF_VARIABLE
581 || fip->field_writecount == TIFF_VARIABLE2
582 || fip->field_writecount == TIFF_SPP
583 || tv->count > 1) {
584 _TIFFmemcpy(tv->value, va_arg(ap, void *),
585 tv->count * tv_size);
586 } else {
587 char *val = (char *)tv->value;
588 assert( tv->count == 1 );
589
590 switch (fip->field_type) {
8414a40c
VZ
591 case TIFF_BYTE:
592 case TIFF_UNDEFINED:
80ed523f
VZ
593 {
594 uint8 v = (uint8)va_arg(ap, int);
595 _TIFFmemcpy(val, &v, tv_size);
596 }
597 break;
8414a40c 598 case TIFF_SBYTE:
80ed523f
VZ
599 {
600 int8 v = (int8)va_arg(ap, int);
601 _TIFFmemcpy(val, &v, tv_size);
602 }
603 break;
8414a40c 604 case TIFF_SHORT:
80ed523f
VZ
605 {
606 uint16 v = (uint16)va_arg(ap, int);
607 _TIFFmemcpy(val, &v, tv_size);
608 }
609 break;
8414a40c 610 case TIFF_SSHORT:
80ed523f
VZ
611 {
612 int16 v = (int16)va_arg(ap, int);
613 _TIFFmemcpy(val, &v, tv_size);
614 }
615 break;
8414a40c
VZ
616 case TIFF_LONG:
617 case TIFF_IFD:
80ed523f
VZ
618 {
619 uint32 v = va_arg(ap, uint32);
620 _TIFFmemcpy(val, &v, tv_size);
621 }
622 break;
8414a40c 623 case TIFF_SLONG:
80ed523f
VZ
624 {
625 int32 v = va_arg(ap, int32);
626 _TIFFmemcpy(val, &v, tv_size);
627 }
628 break;
629 case TIFF_LONG8:
630 case TIFF_IFD8:
631 {
632 uint64 v = va_arg(ap, uint64);
633 _TIFFmemcpy(val, &v, tv_size);
634 }
635 break;
636 case TIFF_SLONG8:
637 {
638 int64 v = va_arg(ap, int64);
639 _TIFFmemcpy(val, &v, tv_size);
640 }
641 break;
8414a40c
VZ
642 case TIFF_RATIONAL:
643 case TIFF_SRATIONAL:
644 case TIFF_FLOAT:
80ed523f
VZ
645 {
646 float v = (float)va_arg(ap, double);
647 _TIFFmemcpy(val, &v, tv_size);
648 }
649 break;
8414a40c 650 case TIFF_DOUBLE:
80ed523f
VZ
651 {
652 double v = va_arg(ap, double);
653 _TIFFmemcpy(val, &v, tv_size);
654 }
655 break;
8414a40c 656 default:
80ed523f
VZ
657 _TIFFmemset(val, 0, tv_size);
658 status = 0;
659 break;
660 }
661 }
8414a40c 662 }
80ed523f 663 }
8414a40c
VZ
664 }
665 if (status) {
80ed523f
VZ
666 const TIFFField* fip=TIFFFieldWithTag(tif,tag);
667 if (fip)
668 TIFFSetFieldBit(tif, fip->field_bit);
8414a40c
VZ
669 tif->tif_flags |= TIFF_DIRTYDIRECT;
670 }
671
672end:
673 va_end(ap);
674 return (status);
675badvalue:
80ed523f
VZ
676 {
677 const TIFFField* fip=TIFFFieldWithTag(tif,tag);
678 TIFFErrorExt(tif->tif_clientdata, module,
679 "%s: Bad value %u for \"%s\" tag",
680 tif->tif_name, v,
681 fip ? fip->field_name : "Unknown");
682 va_end(ap);
683 }
8414a40c
VZ
684 return (0);
685badvalue32:
80ed523f
VZ
686 {
687 const TIFFField* fip=TIFFFieldWithTag(tif,tag);
688 TIFFErrorExt(tif->tif_clientdata, module,
689 "%s: Bad value %u for \"%s\" tag",
690 tif->tif_name, v32,
691 fip ? fip->field_name : "Unknown");
692 va_end(ap);
693 }
8414a40c
VZ
694 return (0);
695}
696
697/*
698 * Return 1/0 according to whether or not
699 * it is permissible to set the tag's value.
700 * Note that we allow ImageLength to be changed
701 * so that we can append and extend to images.
702 * Any other tag may not be altered once writing
703 * has commenced, unless its value has no effect
704 * on the format of the data that is written.
705 */
706static int
80ed523f 707OkToChangeTag(TIFF* tif, uint32 tag)
8414a40c 708{
80ed523f 709 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
8414a40c
VZ
710 if (!fip) { /* unknown tag */
711 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
712 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
713 return (0);
714 }
715 if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
716 !fip->field_oktochange) {
717 /*
718 * Consult info table to see if tag can be changed
719 * after we've started writing. We only allow changes
720 * to those tags that don't/shouldn't affect the
721 * compression and/or format of the data.
722 */
723 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
724 "%s: Cannot modify tag \"%s\" while writing",
725 tif->tif_name, fip->field_name);
726 return (0);
727 }
728 return (1);
729}
730
731/*
732 * Record the value of a field in the
733 * internal directory structure. The
734 * field will be written to the file
735 * when/if the directory structure is
736 * updated.
737 */
738int
80ed523f 739TIFFSetField(TIFF* tif, uint32 tag, ...)
8414a40c
VZ
740{
741 va_list ap;
742 int status;
743
744 va_start(ap, tag);
745 status = TIFFVSetField(tif, tag, ap);
746 va_end(ap);
747 return (status);
748}
749
80ed523f
VZ
750/*
751 * Clear the contents of the field in the internal structure.
752 */
753int
754TIFFUnsetField(TIFF* tif, uint32 tag)
755{
756 const TIFFField *fip = TIFFFieldWithTag(tif, tag);
757 TIFFDirectory* td = &tif->tif_dir;
758
759 if( !fip )
760 return 0;
761
762 if( fip->field_bit != FIELD_CUSTOM )
763 TIFFClrFieldBit(tif, fip->field_bit);
764 else
765 {
766 TIFFTagValue *tv = NULL;
767 int i;
768
769 for (i = 0; i < td->td_customValueCount; i++) {
770
771 tv = td->td_customValues + i;
772 if( tv->info->field_tag == tag )
773 break;
774 }
775
776 if( i < td->td_customValueCount )
777 {
778 _TIFFfree(tv->value);
779 for( ; i < td->td_customValueCount-1; i++) {
780 td->td_customValues[i] = td->td_customValues[i+1];
781 }
782 td->td_customValueCount--;
783 }
784 }
785
786 tif->tif_flags |= TIFF_DIRTYDIRECT;
787
788 return (1);
789}
790
8414a40c
VZ
791/*
792 * Like TIFFSetField, but taking a varargs
793 * parameter list. This routine is useful
794 * for building higher-level interfaces on
795 * top of the library.
796 */
797int
80ed523f 798TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
8414a40c
VZ
799{
800 return OkToChangeTag(tif, tag) ?
801 (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
802}
803
804static int
80ed523f 805_TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
8414a40c 806{
80ed523f
VZ
807 TIFFDirectory* td = &tif->tif_dir;
808 int ret_val = 1;
809 uint32 standard_tag = tag;
810 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
811
812 /*
813 * We want to force the custom code to be used for custom
814 * fields even if the tag happens to match a well known
815 * one - important for reinterpreted handling of standard
816 * tag values in custom directories (ie. EXIF)
817 */
818 if (fip->field_bit == FIELD_CUSTOM) {
819 standard_tag = 0;
820 }
8414a40c 821
80ed523f
VZ
822 switch (standard_tag) {
823 case TIFFTAG_SUBFILETYPE:
824 *va_arg(ap, uint32*) = td->td_subfiletype;
825 break;
826 case TIFFTAG_IMAGEWIDTH:
827 *va_arg(ap, uint32*) = td->td_imagewidth;
828 break;
829 case TIFFTAG_IMAGELENGTH:
830 *va_arg(ap, uint32*) = td->td_imagelength;
831 break;
832 case TIFFTAG_BITSPERSAMPLE:
833 *va_arg(ap, uint16*) = td->td_bitspersample;
834 break;
835 case TIFFTAG_COMPRESSION:
836 *va_arg(ap, uint16*) = td->td_compression;
837 break;
838 case TIFFTAG_PHOTOMETRIC:
839 *va_arg(ap, uint16*) = td->td_photometric;
840 break;
841 case TIFFTAG_THRESHHOLDING:
842 *va_arg(ap, uint16*) = td->td_threshholding;
843 break;
844 case TIFFTAG_FILLORDER:
845 *va_arg(ap, uint16*) = td->td_fillorder;
846 break;
847 case TIFFTAG_ORIENTATION:
848 *va_arg(ap, uint16*) = td->td_orientation;
849 break;
850 case TIFFTAG_SAMPLESPERPIXEL:
851 *va_arg(ap, uint16*) = td->td_samplesperpixel;
852 break;
853 case TIFFTAG_ROWSPERSTRIP:
854 *va_arg(ap, uint32*) = td->td_rowsperstrip;
855 break;
856 case TIFFTAG_MINSAMPLEVALUE:
857 *va_arg(ap, uint16*) = td->td_minsamplevalue;
858 break;
859 case TIFFTAG_MAXSAMPLEVALUE:
860 *va_arg(ap, uint16*) = td->td_maxsamplevalue;
861 break;
862 case TIFFTAG_SMINSAMPLEVALUE:
863 if (tif->tif_flags & TIFF_PERSAMPLE)
864 *va_arg(ap, double**) = td->td_sminsamplevalue;
865 else
866 {
867 /* libtiff historially treats this as a single value. */
868 uint16 i;
869 double v = td->td_sminsamplevalue[0];
870 for (i=1; i < td->td_samplesperpixel; ++i)
871 if( td->td_sminsamplevalue[i] < v )
872 v = td->td_sminsamplevalue[i];
873 *va_arg(ap, double*) = v;
874 }
875 break;
876 case TIFFTAG_SMAXSAMPLEVALUE:
877 if (tif->tif_flags & TIFF_PERSAMPLE)
878 *va_arg(ap, double**) = td->td_smaxsamplevalue;
879 else
880 {
881 /* libtiff historially treats this as a single value. */
882 uint16 i;
883 double v = td->td_smaxsamplevalue[0];
884 for (i=1; i < td->td_samplesperpixel; ++i)
885 if( td->td_smaxsamplevalue[i] > v )
886 v = td->td_smaxsamplevalue[i];
887 *va_arg(ap, double*) = v;
888 }
889 break;
890 case TIFFTAG_XRESOLUTION:
891 *va_arg(ap, float*) = td->td_xresolution;
892 break;
893 case TIFFTAG_YRESOLUTION:
894 *va_arg(ap, float*) = td->td_yresolution;
895 break;
896 case TIFFTAG_PLANARCONFIG:
897 *va_arg(ap, uint16*) = td->td_planarconfig;
898 break;
899 case TIFFTAG_XPOSITION:
900 *va_arg(ap, float*) = td->td_xposition;
901 break;
902 case TIFFTAG_YPOSITION:
903 *va_arg(ap, float*) = td->td_yposition;
904 break;
905 case TIFFTAG_RESOLUTIONUNIT:
906 *va_arg(ap, uint16*) = td->td_resolutionunit;
907 break;
908 case TIFFTAG_PAGENUMBER:
909 *va_arg(ap, uint16*) = td->td_pagenumber[0];
910 *va_arg(ap, uint16*) = td->td_pagenumber[1];
911 break;
912 case TIFFTAG_HALFTONEHINTS:
913 *va_arg(ap, uint16*) = td->td_halftonehints[0];
914 *va_arg(ap, uint16*) = td->td_halftonehints[1];
915 break;
916 case TIFFTAG_COLORMAP:
917 *va_arg(ap, uint16**) = td->td_colormap[0];
918 *va_arg(ap, uint16**) = td->td_colormap[1];
919 *va_arg(ap, uint16**) = td->td_colormap[2];
920 break;
921 case TIFFTAG_STRIPOFFSETS:
922 case TIFFTAG_TILEOFFSETS:
923 _TIFFFillStriles( tif );
924 *va_arg(ap, uint64**) = td->td_stripoffset;
925 break;
926 case TIFFTAG_STRIPBYTECOUNTS:
927 case TIFFTAG_TILEBYTECOUNTS:
928 _TIFFFillStriles( tif );
929 *va_arg(ap, uint64**) = td->td_stripbytecount;
930 break;
931 case TIFFTAG_MATTEING:
932 *va_arg(ap, uint16*) =
933 (td->td_extrasamples == 1 &&
934 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
935 break;
936 case TIFFTAG_EXTRASAMPLES:
937 *va_arg(ap, uint16*) = td->td_extrasamples;
938 *va_arg(ap, uint16**) = td->td_sampleinfo;
939 break;
940 case TIFFTAG_TILEWIDTH:
941 *va_arg(ap, uint32*) = td->td_tilewidth;
942 break;
943 case TIFFTAG_TILELENGTH:
944 *va_arg(ap, uint32*) = td->td_tilelength;
945 break;
946 case TIFFTAG_TILEDEPTH:
947 *va_arg(ap, uint32*) = td->td_tiledepth;
948 break;
949 case TIFFTAG_DATATYPE:
950 switch (td->td_sampleformat) {
951 case SAMPLEFORMAT_UINT:
952 *va_arg(ap, uint16*) = DATATYPE_UINT;
953 break;
954 case SAMPLEFORMAT_INT:
955 *va_arg(ap, uint16*) = DATATYPE_INT;
956 break;
957 case SAMPLEFORMAT_IEEEFP:
958 *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
959 break;
960 case SAMPLEFORMAT_VOID:
961 *va_arg(ap, uint16*) = DATATYPE_VOID;
962 break;
963 }
964 break;
965 case TIFFTAG_SAMPLEFORMAT:
966 *va_arg(ap, uint16*) = td->td_sampleformat;
967 break;
968 case TIFFTAG_IMAGEDEPTH:
969 *va_arg(ap, uint32*) = td->td_imagedepth;
970 break;
971 case TIFFTAG_SUBIFD:
972 *va_arg(ap, uint16*) = td->td_nsubifd;
973 *va_arg(ap, uint64**) = td->td_subifd;
974 break;
975 case TIFFTAG_YCBCRPOSITIONING:
976 *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
977 break;
978 case TIFFTAG_YCBCRSUBSAMPLING:
979 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
980 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
981 break;
982 case TIFFTAG_TRANSFERFUNCTION:
983 *va_arg(ap, uint16**) = td->td_transferfunction[0];
984 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
985 *va_arg(ap, uint16**) = td->td_transferfunction[1];
986 *va_arg(ap, uint16**) = td->td_transferfunction[2];
987 }
988 break;
989 case TIFFTAG_REFERENCEBLACKWHITE:
990 *va_arg(ap, float**) = td->td_refblackwhite;
991 break;
992 case TIFFTAG_INKNAMES:
993 *va_arg(ap, char**) = td->td_inknames;
994 break;
995 default:
996 {
997 int i;
8414a40c 998
80ed523f
VZ
999 /*
1000 * This can happen if multiple images are open
1001 * with different codecs which have private
1002 * tags. The global tag information table may
1003 * then have tags that are valid for one file
1004 * but not the other. If the client tries to
1005 * get a tag that is not valid for the image's
1006 * codec then we'll arrive here.
1007 */
1008 if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
1009 {
1010 TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
1011 "%s: Invalid %stag \"%s\" "
1012 "(not supported by codec)",
1013 tif->tif_name,
1014 isPseudoTag(tag) ? "pseudo-" : "",
1015 fip ? fip->field_name : "Unknown");
1016 ret_val = 0;
1017 break;
1018 }
8414a40c 1019
80ed523f
VZ
1020 /*
1021 * Do we have a custom value?
1022 */
1023 ret_val = 0;
1024 for (i = 0; i < td->td_customValueCount; i++) {
1025 TIFFTagValue *tv = td->td_customValues + i;
8414a40c 1026
80ed523f
VZ
1027 if (tv->info->field_tag != tag)
1028 continue;
1029
1030 if (fip->field_passcount) {
1031 if (fip->field_readcount == TIFF_VARIABLE2)
1032 *va_arg(ap, uint32*) = (uint32)tv->count;
1033 else /* Assume TIFF_VARIABLE */
1034 *va_arg(ap, uint16*) = (uint16)tv->count;
1035 *va_arg(ap, void **) = tv->value;
8414a40c 1036 ret_val = 1;
80ed523f
VZ
1037 } else if (fip->field_tag == TIFFTAG_DOTRANGE
1038 && strcmp(fip->field_name,"DotRange") == 0) {
1039 /* TODO: This is an evil exception and should not have been
1040 handled this way ... likely best if we move it into
1041 the directory structure with an explicit field in
1042 libtiff 4.1 and assign it a FIELD_ value */
1043 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[0];
1044 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[1];
8414a40c 1045 ret_val = 1;
80ed523f
VZ
1046 } else {
1047 if (fip->field_type == TIFF_ASCII
1048 || fip->field_readcount == TIFF_VARIABLE
1049 || fip->field_readcount == TIFF_VARIABLE2
1050 || fip->field_readcount == TIFF_SPP
1051 || tv->count > 1) {
1052 *va_arg(ap, void **) = tv->value;
1053 ret_val = 1;
1054 } else {
1055 char *val = (char *)tv->value;
1056 assert( tv->count == 1 );
1057 switch (fip->field_type) {
1058 case TIFF_BYTE:
1059 case TIFF_UNDEFINED:
1060 *va_arg(ap, uint8*) =
1061 *(uint8 *)val;
1062 ret_val = 1;
1063 break;
1064 case TIFF_SBYTE:
1065 *va_arg(ap, int8*) =
1066 *(int8 *)val;
1067 ret_val = 1;
1068 break;
1069 case TIFF_SHORT:
1070 *va_arg(ap, uint16*) =
1071 *(uint16 *)val;
1072 ret_val = 1;
1073 break;
1074 case TIFF_SSHORT:
1075 *va_arg(ap, int16*) =
1076 *(int16 *)val;
1077 ret_val = 1;
1078 break;
1079 case TIFF_LONG:
1080 case TIFF_IFD:
1081 *va_arg(ap, uint32*) =
1082 *(uint32 *)val;
1083 ret_val = 1;
1084 break;
1085 case TIFF_SLONG:
1086 *va_arg(ap, int32*) =
1087 *(int32 *)val;
1088 ret_val = 1;
1089 break;
1090 case TIFF_LONG8:
1091 case TIFF_IFD8:
1092 *va_arg(ap, uint64*) =
1093 *(uint64 *)val;
1094 ret_val = 1;
1095 break;
1096 case TIFF_SLONG8:
1097 *va_arg(ap, int64*) =
1098 *(int64 *)val;
1099 ret_val = 1;
1100 break;
1101 case TIFF_RATIONAL:
1102 case TIFF_SRATIONAL:
1103 case TIFF_FLOAT:
1104 *va_arg(ap, float*) =
1105 *(float *)val;
1106 ret_val = 1;
1107 break;
1108 case TIFF_DOUBLE:
1109 *va_arg(ap, double*) =
1110 *(double *)val;
1111 ret_val = 1;
1112 break;
1113 default:
1114 ret_val = 0;
1115 break;
1116 }
1117 }
1118 }
1119 break;
8414a40c 1120 }
8414a40c 1121 }
80ed523f
VZ
1122 }
1123 return(ret_val);
8414a40c
VZ
1124}
1125
1126/*
1127 * Return the value of a field in the
1128 * internal directory structure.
1129 */
1130int
80ed523f 1131TIFFGetField(TIFF* tif, uint32 tag, ...)
8414a40c
VZ
1132{
1133 int status;
1134 va_list ap;
1135
1136 va_start(ap, tag);
1137 status = TIFFVGetField(tif, tag, ap);
1138 va_end(ap);
1139 return (status);
1140}
1141
1142/*
1143 * Like TIFFGetField, but taking a varargs
1144 * parameter list. This routine is useful
1145 * for building higher-level interfaces on
1146 * top of the library.
1147 */
1148int
80ed523f 1149TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
8414a40c 1150{
80ed523f 1151 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
8414a40c
VZ
1152 return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
1153 (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
1154}
1155
1156#define CleanupField(member) { \
1157 if (td->member) { \
1158 _TIFFfree(td->member); \
1159 td->member = 0; \
1160 } \
1161}
1162
1163/*
1164 * Release storage associated with a directory.
1165 */
1166void
1167TIFFFreeDirectory(TIFF* tif)
1168{
1169 TIFFDirectory *td = &tif->tif_dir;
1170 int i;
1171
1172 _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
80ed523f
VZ
1173 CleanupField(td_sminsamplevalue);
1174 CleanupField(td_smaxsamplevalue);
8414a40c
VZ
1175 CleanupField(td_colormap[0]);
1176 CleanupField(td_colormap[1]);
1177 CleanupField(td_colormap[2]);
1178 CleanupField(td_sampleinfo);
1179 CleanupField(td_subifd);
1180 CleanupField(td_inknames);
80ed523f 1181 CleanupField(td_refblackwhite);
8414a40c
VZ
1182 CleanupField(td_transferfunction[0]);
1183 CleanupField(td_transferfunction[1]);
1184 CleanupField(td_transferfunction[2]);
1185 CleanupField(td_stripoffset);
1186 CleanupField(td_stripbytecount);
1187 TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
1188 TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
1189
1190 /* Cleanup custom tag values */
1191 for( i = 0; i < td->td_customValueCount; i++ ) {
1192 if (td->td_customValues[i].value)
1193 _TIFFfree(td->td_customValues[i].value);
1194 }
1195
1196 td->td_customValueCount = 0;
1197 CleanupField(td_customValues);
80ed523f
VZ
1198
1199#if defined(DEFER_STRILE_LOAD)
1200 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
1201 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
1202#endif
8414a40c
VZ
1203}
1204#undef CleanupField
1205
1206/*
1207 * Client Tag extension support (from Niles Ritter).
1208 */
1209static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
1210
1211TIFFExtendProc
1212TIFFSetTagExtender(TIFFExtendProc extender)
1213{
1214 TIFFExtendProc prev = _TIFFextender;
1215 _TIFFextender = extender;
1216 return (prev);
1217}
1218
1219/*
1220 * Setup for a new directory. Should we automatically call
1221 * TIFFWriteDirectory() if the current one is dirty?
1222 *
1223 * The newly created directory will not exist on the file till
1224 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1225 */
1226int
1227TIFFCreateDirectory(TIFF* tif)
1228{
80ed523f
VZ
1229 TIFFDefaultDirectory(tif);
1230 tif->tif_diroff = 0;
1231 tif->tif_nextdiroff = 0;
1232 tif->tif_curoff = 0;
1233 tif->tif_row = (uint32) -1;
1234 tif->tif_curstrip = (uint32) -1;
1235
1236 return 0;
1237}
1238
1239int
1240TIFFCreateCustomDirectory(TIFF* tif, const TIFFFieldArray* infoarray)
1241{
1242 TIFFDefaultDirectory(tif);
1243
1244 /*
1245 * Reset the field definitions to match the application provided list.
1246 * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
1247 * based on it's assumption this is an image directory.
1248 */
1249 _TIFFSetupFields(tif, infoarray);
1250
1251 tif->tif_diroff = 0;
1252 tif->tif_nextdiroff = 0;
1253 tif->tif_curoff = 0;
1254 tif->tif_row = (uint32) -1;
1255 tif->tif_curstrip = (uint32) -1;
1256
1257 return 0;
1258}
1259
1260int
1261TIFFCreateEXIFDirectory(TIFF* tif)
1262{
1263 const TIFFFieldArray* exifFieldArray;
1264 exifFieldArray = _TIFFGetExifFields();
1265 return TIFFCreateCustomDirectory(tif, exifFieldArray);
8414a40c
VZ
1266}
1267
1268/*
1269 * Setup a default directory structure.
1270 */
1271int
1272TIFFDefaultDirectory(TIFF* tif)
1273{
1274 register TIFFDirectory* td = &tif->tif_dir;
80ed523f 1275 const TIFFFieldArray* tiffFieldArray;
8414a40c 1276
80ed523f
VZ
1277 tiffFieldArray = _TIFFGetFields();
1278 _TIFFSetupFields(tif, tiffFieldArray);
8414a40c
VZ
1279
1280 _TIFFmemset(td, 0, sizeof (*td));
1281 td->td_fillorder = FILLORDER_MSB2LSB;
1282 td->td_bitspersample = 1;
1283 td->td_threshholding = THRESHHOLD_BILEVEL;
1284 td->td_orientation = ORIENTATION_TOPLEFT;
1285 td->td_samplesperpixel = 1;
1286 td->td_rowsperstrip = (uint32) -1;
1287 td->td_tilewidth = 0;
1288 td->td_tilelength = 0;
1289 td->td_tiledepth = 1;
80ed523f 1290 td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
8414a40c
VZ
1291 td->td_resolutionunit = RESUNIT_INCH;
1292 td->td_sampleformat = SAMPLEFORMAT_UINT;
1293 td->td_imagedepth = 1;
1294 td->td_ycbcrsubsampling[0] = 2;
1295 td->td_ycbcrsubsampling[1] = 2;
1296 td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
80ed523f
VZ
1297 tif->tif_postdecode = _TIFFNoPostDecode;
1298 tif->tif_foundfield = NULL;
1299 tif->tif_tagmethods.vsetfield = _TIFFVSetField;
8414a40c
VZ
1300 tif->tif_tagmethods.vgetfield = _TIFFVGetField;
1301 tif->tif_tagmethods.printdir = NULL;
1302 /*
1303 * Give client code a chance to install their own
1304 * tag extensions & methods, prior to compression overloads.
1305 */
1306 if (_TIFFextender)
1307 (*_TIFFextender)(tif);
1308 (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1309 /*
1310 * NB: The directory is marked dirty as a result of setting
1311 * up the default compression scheme. However, this really
1312 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1313 * if the user does something. We could just do the setup
1314 * by hand, but it seems better to use the normal mechanism
1315 * (i.e. TIFFSetField).
1316 */
1317 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1318
80ed523f
VZ
1319 /*
1320 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1321 * we clear the ISTILED flag when setting up a new directory.
1322 * Should we also be clearing stuff like INSUBIFD?
1323 */
1324 tif->tif_flags &= ~TIFF_ISTILED;
8414a40c
VZ
1325
1326 return (1);
1327}
1328
1329static int
80ed523f 1330TIFFAdvanceDirectory(TIFF* tif, uint64* nextdir, uint64* off)
8414a40c 1331{
80ed523f
VZ
1332 static const char module[] = "TIFFAdvanceDirectory";
1333 if (isMapped(tif))
1334 {
1335 uint64 poff=*nextdir;
1336 if (!(tif->tif_flags&TIFF_BIGTIFF))
1337 {
1338 tmsize_t poffa,poffb,poffc,poffd;
1339 uint16 dircount;
1340 uint32 nextdir32;
1341 poffa=(tmsize_t)poff;
1342 poffb=poffa+sizeof(uint16);
1343 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint16))||(poffb>tif->tif_size))
1344 {
1345 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1346 return(0);
1347 }
1348 _TIFFmemcpy(&dircount,tif->tif_base+poffa,sizeof(uint16));
1349 if (tif->tif_flags&TIFF_SWAB)
1350 TIFFSwabShort(&dircount);
1351 poffc=poffb+dircount*12;
1352 poffd=poffc+sizeof(uint32);
1353 if ((poffc<poffb)||(poffc<dircount*12)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint32))||(poffd>tif->tif_size))
1354 {
1355 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1356 return(0);
1357 }
1358 if (off!=NULL)
1359 *off=(uint64)poffc;
1360 _TIFFmemcpy(&nextdir32,tif->tif_base+poffc,sizeof(uint32));
1361 if (tif->tif_flags&TIFF_SWAB)
1362 TIFFSwabLong(&nextdir32);
1363 *nextdir=nextdir32;
1364 }
1365 else
1366 {
1367 tmsize_t poffa,poffb,poffc,poffd;
1368 uint64 dircount64;
1369 uint16 dircount16;
1370 poffa=(tmsize_t)poff;
1371 poffb=poffa+sizeof(uint64);
1372 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint64))||(poffb>tif->tif_size))
1373 {
1374 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1375 return(0);
1376 }
1377 _TIFFmemcpy(&dircount64,tif->tif_base+poffa,sizeof(uint64));
1378 if (tif->tif_flags&TIFF_SWAB)
1379 TIFFSwabLong8(&dircount64);
1380 if (dircount64>0xFFFF)
1381 {
1382 TIFFErrorExt(tif->tif_clientdata,module,"Sanity check on directory count failed");
1383 return(0);
1384 }
1385 dircount16=(uint16)dircount64;
1386 poffc=poffb+dircount16*20;
1387 poffd=poffc+sizeof(uint64);
1388 if ((poffc<poffb)||(poffc<dircount16*20)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint64))||(poffd>tif->tif_size))
1389 {
1390 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1391 return(0);
1392 }
1393 if (off!=NULL)
1394 *off=(uint64)poffc;
1395 _TIFFmemcpy(nextdir,tif->tif_base+poffc,sizeof(uint64));
1396 if (tif->tif_flags&TIFF_SWAB)
1397 TIFFSwabLong8(nextdir);
1398 }
1399 return(1);
1400 }
1401 else
1402 {
1403 if (!(tif->tif_flags&TIFF_BIGTIFF))
1404 {
1405 uint16 dircount;
1406 uint32 nextdir32;
1407 if (!SeekOK(tif, *nextdir) ||
1408 !ReadOK(tif, &dircount, sizeof (uint16))) {
1409 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1410 tif->tif_name);
1411 return (0);
1412 }
1413 if (tif->tif_flags & TIFF_SWAB)
1414 TIFFSwabShort(&dircount);
1415 if (off != NULL)
1416 *off = TIFFSeekFile(tif,
1417 dircount*12, SEEK_CUR);
1418 else
1419 (void) TIFFSeekFile(tif,
1420 dircount*12, SEEK_CUR);
1421 if (!ReadOK(tif, &nextdir32, sizeof (uint32))) {
1422 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
1423 tif->tif_name);
1424 return (0);
1425 }
1426 if (tif->tif_flags & TIFF_SWAB)
1427 TIFFSwabLong(&nextdir32);
1428 *nextdir=nextdir32;
1429 }
1430 else
1431 {
1432 uint64 dircount64;
1433 uint16 dircount16;
1434 if (!SeekOK(tif, *nextdir) ||
1435 !ReadOK(tif, &dircount64, sizeof (uint64))) {
1436 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1437 tif->tif_name);
1438 return (0);
1439 }
1440 if (tif->tif_flags & TIFF_SWAB)
1441 TIFFSwabLong8(&dircount64);
1442 if (dircount64>0xFFFF)
1443 {
1444 TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
1445 return(0);
1446 }
1447 dircount16 = (uint16)dircount64;
1448 if (off != NULL)
1449 *off = TIFFSeekFile(tif,
1450 dircount16*20, SEEK_CUR);
1451 else
1452 (void) TIFFSeekFile(tif,
1453 dircount16*20, SEEK_CUR);
1454 if (!ReadOK(tif, nextdir, sizeof (uint64))) {
1455 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
1456 tif->tif_name);
1457 return (0);
1458 }
1459 if (tif->tif_flags & TIFF_SWAB)
1460 TIFFSwabLong8(nextdir);
1461 }
1462 return (1);
1463 }
8414a40c
VZ
1464}
1465
1466/*
1467 * Count the number of directories in a file.
1468 */
80ed523f 1469uint16
8414a40c
VZ
1470TIFFNumberOfDirectories(TIFF* tif)
1471{
80ed523f
VZ
1472 uint64 nextdir;
1473 uint16 n;
1474 if (!(tif->tif_flags&TIFF_BIGTIFF))
1475 nextdir = tif->tif_header.classic.tiff_diroff;
1476 else
1477 nextdir = tif->tif_header.big.tiff_diroff;
1478 n = 0;
1479 while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
1480 n++;
1481 return (n);
8414a40c
VZ
1482}
1483
1484/*
1485 * Set the n-th directory as the current directory.
1486 * NB: Directories are numbered starting at 0.
1487 */
1488int
80ed523f 1489TIFFSetDirectory(TIFF* tif, uint16 dirn)
8414a40c 1490{
80ed523f
VZ
1491 uint64 nextdir;
1492 uint16 n;
8414a40c 1493
80ed523f
VZ
1494 if (!(tif->tif_flags&TIFF_BIGTIFF))
1495 nextdir = tif->tif_header.classic.tiff_diroff;
1496 else
1497 nextdir = tif->tif_header.big.tiff_diroff;
8414a40c
VZ
1498 for (n = dirn; n > 0 && nextdir != 0; n--)
1499 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1500 return (0);
1501 tif->tif_nextdiroff = nextdir;
1502 /*
1503 * Set curdir to the actual directory index. The
1504 * -1 is because TIFFReadDirectory will increment
1505 * tif_curdir after successfully reading the directory.
1506 */
1507 tif->tif_curdir = (dirn - n) - 1;
1508 /*
1509 * Reset tif_dirnumber counter and start new list of seen directories.
1510 * We need this to prevent IFD loops.
1511 */
1512 tif->tif_dirnumber = 0;
1513 return (TIFFReadDirectory(tif));
1514}
1515
1516/*
1517 * Set the current directory to be the directory
1518 * located at the specified file offset. This interface
1519 * is used mainly to access directories linked with
1520 * the SubIFD tag (e.g. thumbnail images).
1521 */
1522int
80ed523f 1523TIFFSetSubDirectory(TIFF* tif, uint64 diroff)
8414a40c
VZ
1524{
1525 tif->tif_nextdiroff = diroff;
1526 /*
1527 * Reset tif_dirnumber counter and start new list of seen directories.
1528 * We need this to prevent IFD loops.
1529 */
1530 tif->tif_dirnumber = 0;
1531 return (TIFFReadDirectory(tif));
1532}
1533
1534/*
1535 * Return file offset of the current directory.
1536 */
80ed523f 1537uint64
8414a40c
VZ
1538TIFFCurrentDirOffset(TIFF* tif)
1539{
1540 return (tif->tif_diroff);
1541}
1542
1543/*
1544 * Return an indication of whether or not we are
1545 * at the last directory in the file.
1546 */
1547int
1548TIFFLastDirectory(TIFF* tif)
1549{
1550 return (tif->tif_nextdiroff == 0);
1551}
1552
1553/*
1554 * Unlink the specified directory from the directory chain.
1555 */
1556int
80ed523f 1557TIFFUnlinkDirectory(TIFF* tif, uint16 dirn)
8414a40c
VZ
1558{
1559 static const char module[] = "TIFFUnlinkDirectory";
80ed523f
VZ
1560 uint64 nextdir;
1561 uint64 off;
1562 uint16 n;
8414a40c
VZ
1563
1564 if (tif->tif_mode == O_RDONLY) {
1565 TIFFErrorExt(tif->tif_clientdata, module,
1566 "Can not unlink directory in read-only file");
1567 return (0);
1568 }
1569 /*
1570 * Go to the directory before the one we want
1571 * to unlink and nab the offset of the link
1572 * field we'll need to patch.
1573 */
80ed523f
VZ
1574 if (!(tif->tif_flags&TIFF_BIGTIFF))
1575 {
1576 nextdir = tif->tif_header.classic.tiff_diroff;
1577 off = 4;
1578 }
1579 else
1580 {
1581 nextdir = tif->tif_header.big.tiff_diroff;
1582 off = 8;
1583 }
8414a40c
VZ
1584 for (n = dirn-1; n > 0; n--) {
1585 if (nextdir == 0) {
1586 TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
1587 return (0);
1588 }
1589 if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
1590 return (0);
1591 }
1592 /*
1593 * Advance to the directory to be unlinked and fetch
1594 * the offset of the directory that follows.
1595 */
1596 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1597 return (0);
1598 /*
1599 * Go back and patch the link field of the preceding
1600 * directory to point to the offset of the directory
1601 * that follows.
1602 */
1603 (void) TIFFSeekFile(tif, off, SEEK_SET);
80ed523f
VZ
1604 if (!(tif->tif_flags&TIFF_BIGTIFF))
1605 {
1606 uint32 nextdir32;
1607 nextdir32=(uint32)nextdir;
1608 assert((uint64)nextdir32==nextdir);
1609 if (tif->tif_flags & TIFF_SWAB)
1610 TIFFSwabLong(&nextdir32);
1611 if (!WriteOK(tif, &nextdir32, sizeof (uint32))) {
1612 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1613 return (0);
1614 }
1615 }
1616 else
1617 {
1618 if (tif->tif_flags & TIFF_SWAB)
1619 TIFFSwabLong8(&nextdir);
1620 if (!WriteOK(tif, &nextdir, sizeof (uint64))) {
1621 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1622 return (0);
1623 }
8414a40c
VZ
1624 }
1625 /*
1626 * Leave directory state setup safely. We don't have
1627 * facilities for doing inserting and removing directories,
1628 * so it's safest to just invalidate everything. This
1629 * means that the caller can only append to the directory
1630 * chain.
1631 */
1632 (*tif->tif_cleanup)(tif);
1633 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
1634 _TIFFfree(tif->tif_rawdata);
1635 tif->tif_rawdata = NULL;
1636 tif->tif_rawcc = 0;
80ed523f
VZ
1637 tif->tif_rawdataoff = 0;
1638 tif->tif_rawdataloaded = 0;
8414a40c 1639 }
80ed523f 1640 tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE|TIFF_BUF4WRITE);
8414a40c
VZ
1641 TIFFFreeDirectory(tif);
1642 TIFFDefaultDirectory(tif);
1643 tif->tif_diroff = 0; /* force link on next write */
1644 tif->tif_nextdiroff = 0; /* next write must be at end */
1645 tif->tif_curoff = 0;
1646 tif->tif_row = (uint32) -1;
80ed523f 1647 tif->tif_curstrip = (uint32) -1;
8414a40c
VZ
1648 return (1);
1649}
1650
8414a40c 1651/* vim: set ts=8 sts=8 sw=8 noet: */
80ed523f
VZ
1652/*
1653 * Local Variables:
1654 * mode: c
1655 * c-basic-offset: 8
1656 * fill-column: 78
1657 * End:
1658 */