1 /* $Id: tif_dir.c,v 1.121 2015-05-31 23:11:43 bfriesen Exp $ */
4 * Copyright (c) 1988-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 * Directory Tag Get & Set Routines.
31 * (and also some miscellaneous stuff)
36 * These are used in the backwards compatibility code...
38 #define DATATYPE_VOID 0 /* !untyped data */
39 #define DATATYPE_INT 1 /* !signed integer data */
40 #define DATATYPE_UINT 2 /* !unsigned integer data */
41 #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
44 setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
47 _TIFFfree(*vpp), *vpp = 0;
49 tmsize_t bytes = (tmsize_t)(nmemb * elem_size);
50 if (elem_size && bytes / elem_size == nmemb)
51 *vpp = (void*) _TIFFmalloc(bytes);
53 _TIFFmemcpy(*vpp, vp, bytes);
56 void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
57 { setByteArray(vpp, vp, n, 1); }
58 void _TIFFsetString(char** cpp, char* cp)
59 { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
60 void _TIFFsetNString(char** cpp, char* cp, uint32 n)
61 { setByteArray((void**) cpp, (void*) cp, n, 1); }
62 void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
63 { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
64 void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
65 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
66 void _TIFFsetLong8Array(uint64** lpp, uint64* lp, uint32 n)
67 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint64)); }
68 void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
69 { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
70 void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
71 { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
74 setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
78 *vpp = _TIFFmalloc(nmemb*sizeof(double));
82 ((double*)*vpp)[nmemb] = value;
87 * Install extra samples information.
90 setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
92 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
93 #define EXTRASAMPLE_COREL_UNASSALPHA 999
98 *v = (uint16) va_arg(ap, uint16_vap);
99 if ((uint16) *v > td->td_samplesperpixel)
101 va = va_arg(ap, uint16*);
102 if (*v > 0 && va == NULL) /* typically missing param */
104 for (i = 0; i < *v; i++) {
105 if (va[i] > EXTRASAMPLE_UNASSALPHA) {
107 * XXX: Corel Draw is known to produce incorrect
108 * ExtraSamples tags which must be patched here if we
109 * want to be able to open some of the damaged TIFF
112 if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
113 va[i] = EXTRASAMPLE_UNASSALPHA;
118 td->td_extrasamples = (uint16) *v;
119 _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
122 #undef EXTRASAMPLE_COREL_UNASSALPHA
126 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
127 * zero if the ink names are not as expected.
130 checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
132 TIFFDirectory* td = &tif->tif_dir;
133 uint16 i = td->td_samplesperpixel;
136 const char* ep = s+slen;
139 for (; cp < ep && *cp != '\0'; cp++) {}
144 return ((uint32)(cp-s));
147 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
148 "%s: Invalid InkNames value; expecting %d names, found %d",
150 td->td_samplesperpixel,
151 td->td_samplesperpixel-i);
156 _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
158 static const char module[] = "_TIFFVSetField";
160 TIFFDirectory* td = &tif->tif_dir;
165 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
166 uint32 standard_tag = tag;
167 if( fip == NULL ) /* cannot happen since OkToChangeTag() already checks it */
170 * We want to force the custom code to be used for custom
171 * fields even if the tag happens to match a well known
172 * one - important for reinterpreted handling of standard
173 * tag values in custom directories (ie. EXIF)
175 if (fip->field_bit == FIELD_CUSTOM) {
179 switch (standard_tag) {
180 case TIFFTAG_SUBFILETYPE:
181 td->td_subfiletype = (uint32) va_arg(ap, uint32);
183 case TIFFTAG_IMAGEWIDTH:
184 td->td_imagewidth = (uint32) va_arg(ap, uint32);
186 case TIFFTAG_IMAGELENGTH:
187 td->td_imagelength = (uint32) va_arg(ap, uint32);
189 case TIFFTAG_BITSPERSAMPLE:
190 td->td_bitspersample = (uint16) va_arg(ap, uint16_vap);
192 * If the data require post-decoding processing to byte-swap
193 * samples, set it up here. Note that since tags are required
194 * to be ordered, compression code can override this behaviour
195 * in the setup method if it wants to roll the post decoding
196 * work in with its normal work.
198 if (tif->tif_flags & TIFF_SWAB) {
199 if (td->td_bitspersample == 8)
200 tif->tif_postdecode = _TIFFNoPostDecode;
201 else if (td->td_bitspersample == 16)
202 tif->tif_postdecode = _TIFFSwab16BitData;
203 else if (td->td_bitspersample == 24)
204 tif->tif_postdecode = _TIFFSwab24BitData;
205 else if (td->td_bitspersample == 32)
206 tif->tif_postdecode = _TIFFSwab32BitData;
207 else if (td->td_bitspersample == 64)
208 tif->tif_postdecode = _TIFFSwab64BitData;
209 else if (td->td_bitspersample == 128) /* two 64's */
210 tif->tif_postdecode = _TIFFSwab64BitData;
213 case TIFFTAG_COMPRESSION:
214 v = (uint16) va_arg(ap, uint16_vap);
216 * If we're changing the compression scheme, the notify the
217 * previous module so that it can cleanup any state it's
220 if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
221 if ((uint32)td->td_compression == v)
223 (*tif->tif_cleanup)(tif);
224 tif->tif_flags &= ~TIFF_CODERSETUP;
227 * Setup new compression routine state.
229 if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
230 td->td_compression = (uint16) v;
234 case TIFFTAG_PHOTOMETRIC:
235 td->td_photometric = (uint16) va_arg(ap, uint16_vap);
237 case TIFFTAG_THRESHHOLDING:
238 td->td_threshholding = (uint16) va_arg(ap, uint16_vap);
240 case TIFFTAG_FILLORDER:
241 v = (uint16) va_arg(ap, uint16_vap);
242 if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
244 td->td_fillorder = (uint16) v;
246 case TIFFTAG_ORIENTATION:
247 v = (uint16) va_arg(ap, uint16_vap);
248 if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
251 td->td_orientation = (uint16) v;
253 case TIFFTAG_SAMPLESPERPIXEL:
254 v = (uint16) va_arg(ap, uint16_vap);
257 td->td_samplesperpixel = (uint16) v;
259 case TIFFTAG_ROWSPERSTRIP:
260 v32 = (uint32) va_arg(ap, uint32);
263 td->td_rowsperstrip = v32;
264 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
265 td->td_tilelength = v32;
266 td->td_tilewidth = td->td_imagewidth;
269 case TIFFTAG_MINSAMPLEVALUE:
270 td->td_minsamplevalue = (uint16) va_arg(ap, uint16_vap);
272 case TIFFTAG_MAXSAMPLEVALUE:
273 td->td_maxsamplevalue = (uint16) va_arg(ap, uint16_vap);
275 case TIFFTAG_SMINSAMPLEVALUE:
276 if (tif->tif_flags & TIFF_PERSAMPLE)
277 _TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
279 setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
281 case TIFFTAG_SMAXSAMPLEVALUE:
282 if (tif->tif_flags & TIFF_PERSAMPLE)
283 _TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
285 setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
287 case TIFFTAG_XRESOLUTION:
288 dblval = va_arg(ap, double);
291 td->td_xresolution = (float) dblval;
293 case TIFFTAG_YRESOLUTION:
294 dblval = va_arg(ap, double);
297 td->td_yresolution = (float) dblval;
299 case TIFFTAG_PLANARCONFIG:
300 v = (uint16) va_arg(ap, uint16_vap);
301 if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
303 td->td_planarconfig = (uint16) v;
305 case TIFFTAG_XPOSITION:
306 td->td_xposition = (float) va_arg(ap, double);
308 case TIFFTAG_YPOSITION:
309 td->td_yposition = (float) va_arg(ap, double);
311 case TIFFTAG_RESOLUTIONUNIT:
312 v = (uint16) va_arg(ap, uint16_vap);
313 if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
315 td->td_resolutionunit = (uint16) v;
317 case TIFFTAG_PAGENUMBER:
318 td->td_pagenumber[0] = (uint16) va_arg(ap, uint16_vap);
319 td->td_pagenumber[1] = (uint16) va_arg(ap, uint16_vap);
321 case TIFFTAG_HALFTONEHINTS:
322 td->td_halftonehints[0] = (uint16) va_arg(ap, uint16_vap);
323 td->td_halftonehints[1] = (uint16) va_arg(ap, uint16_vap);
325 case TIFFTAG_COLORMAP:
326 v32 = (uint32)(1L<<td->td_bitspersample);
327 _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
328 _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
329 _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
331 case TIFFTAG_EXTRASAMPLES:
332 if (!setExtraSamples(td, ap, &v))
335 case TIFFTAG_MATTEING:
336 td->td_extrasamples = (((uint16) va_arg(ap, uint16_vap)) != 0);
337 if (td->td_extrasamples) {
338 uint16 sv = EXTRASAMPLE_ASSOCALPHA;
339 _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
342 case TIFFTAG_TILEWIDTH:
343 v32 = (uint32) va_arg(ap, uint32);
345 if (tif->tif_mode != O_RDONLY)
347 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
348 "Nonstandard tile width %d, convert file", v32);
350 td->td_tilewidth = v32;
351 tif->tif_flags |= TIFF_ISTILED;
353 case TIFFTAG_TILELENGTH:
354 v32 = (uint32) va_arg(ap, uint32);
356 if (tif->tif_mode != O_RDONLY)
358 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
359 "Nonstandard tile length %d, convert file", v32);
361 td->td_tilelength = v32;
362 tif->tif_flags |= TIFF_ISTILED;
364 case TIFFTAG_TILEDEPTH:
365 v32 = (uint32) va_arg(ap, uint32);
368 td->td_tiledepth = v32;
370 case TIFFTAG_DATATYPE:
371 v = (uint16) va_arg(ap, uint16_vap);
373 case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
374 case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
375 case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
376 case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
377 default: goto badvalue;
379 td->td_sampleformat = (uint16) v;
381 case TIFFTAG_SAMPLEFORMAT:
382 v = (uint16) va_arg(ap, uint16_vap);
383 if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
385 td->td_sampleformat = (uint16) v;
387 /* Try to fix up the SWAB function for complex data. */
388 if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
389 && td->td_bitspersample == 32
390 && tif->tif_postdecode == _TIFFSwab32BitData )
391 tif->tif_postdecode = _TIFFSwab16BitData;
392 else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
393 || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
394 && td->td_bitspersample == 64
395 && tif->tif_postdecode == _TIFFSwab64BitData )
396 tif->tif_postdecode = _TIFFSwab32BitData;
398 case TIFFTAG_IMAGEDEPTH:
399 td->td_imagedepth = (uint32) va_arg(ap, uint32);
402 if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
403 td->td_nsubifd = (uint16) va_arg(ap, uint16_vap);
404 _TIFFsetLong8Array(&td->td_subifd, (uint64*) va_arg(ap, uint64*),
405 (long) td->td_nsubifd);
407 TIFFErrorExt(tif->tif_clientdata, module,
408 "%s: Sorry, cannot nest SubIFDs",
413 case TIFFTAG_YCBCRPOSITIONING:
414 td->td_ycbcrpositioning = (uint16) va_arg(ap, uint16_vap);
416 case TIFFTAG_YCBCRSUBSAMPLING:
417 td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, uint16_vap);
418 td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, uint16_vap);
420 case TIFFTAG_TRANSFERFUNCTION:
421 v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
422 for (i = 0; i < v; i++)
423 _TIFFsetShortArray(&td->td_transferfunction[i],
424 va_arg(ap, uint16*), 1L<<td->td_bitspersample);
426 case TIFFTAG_REFERENCEBLACKWHITE:
427 /* XXX should check for null range */
428 _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
430 case TIFFTAG_INKNAMES:
431 v = (uint16) va_arg(ap, uint16_vap);
432 s = va_arg(ap, char*);
433 v = checkInkNamesString(tif, v, s);
436 _TIFFsetNString(&td->td_inknames, s, v);
437 td->td_inknameslen = v;
440 case TIFFTAG_PERSAMPLE:
441 v = (uint16) va_arg(ap, uint16_vap);
442 if( v == PERSAMPLE_MULTI )
443 tif->tif_flags |= TIFF_PERSAMPLE;
445 tif->tif_flags &= ~TIFF_PERSAMPLE;
449 int tv_size, iCustom;
452 * This can happen if multiple images are open with different
453 * codecs which have private tags. The global tag information
454 * table may then have tags that are valid for one file but not
455 * the other. If the client tries to set a tag that is not valid
456 * for the image's codec then we'll arrive here. This
457 * happens, for example, when tiffcp is used to convert between
458 * compression schemes and codec-specific tags are blindly copied.
460 if(fip->field_bit != FIELD_CUSTOM) {
461 TIFFErrorExt(tif->tif_clientdata, module,
462 "%s: Invalid %stag \"%s\" (not supported by codec)",
463 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
470 * Find the existing entry for this custom value.
473 for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
474 if (td->td_customValues[iCustom].info->field_tag == tag) {
475 tv = td->td_customValues + iCustom;
476 if (tv->value != NULL) {
477 _TIFFfree(tv->value);
485 * Grow the custom list if the entry was not found.
488 TIFFTagValue *new_customValues;
490 td->td_customValueCount++;
491 new_customValues = (TIFFTagValue *)
492 _TIFFrealloc(td->td_customValues,
493 sizeof(TIFFTagValue) * td->td_customValueCount);
494 if (!new_customValues) {
495 TIFFErrorExt(tif->tif_clientdata, module,
496 "%s: Failed to allocate space for list of custom values",
502 td->td_customValues = new_customValues;
504 tv = td->td_customValues + (td->td_customValueCount - 1);
511 * Set custom value ... save a copy of the custom tag value.
513 tv_size = _TIFFDataSize(fip->field_type);
516 TIFFErrorExt(tif->tif_clientdata, module,
517 "%s: Bad field type %d for \"%s\"",
518 tif->tif_name, fip->field_type,
523 if (fip->field_type == TIFF_ASCII)
527 if (fip->field_passcount)
529 assert(fip->field_writecount==TIFF_VARIABLE2);
530 ma=(uint32)va_arg(ap,uint32);
531 mb=(char*)va_arg(ap,char*);
535 mb=(char*)va_arg(ap,char*);
536 ma=(uint32)(strlen(mb)+1);
539 setByteArray(&tv->value,mb,ma,1);
543 if (fip->field_passcount) {
544 if (fip->field_writecount == TIFF_VARIABLE2)
545 tv->count = (uint32) va_arg(ap, uint32);
547 tv->count = (int) va_arg(ap, int);
548 } else if (fip->field_writecount == TIFF_VARIABLE
549 || fip->field_writecount == TIFF_VARIABLE2)
551 else if (fip->field_writecount == TIFF_SPP)
552 tv->count = td->td_samplesperpixel;
554 tv->count = fip->field_writecount;
556 if (tv->count == 0) {
558 TIFFErrorExt(tif->tif_clientdata, module,
559 "%s: Null count for \"%s\" (type "
560 "%d, writecount %d, passcount %d)",
564 fip->field_writecount,
565 fip->field_passcount);
569 tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
570 "custom tag binary object");
576 if (fip->field_tag == TIFFTAG_DOTRANGE
577 && strcmp(fip->field_name,"DotRange") == 0) {
578 /* TODO: This is an evil exception and should not have been
579 handled this way ... likely best if we move it into
580 the directory structure with an explicit field in
581 libtiff 4.1 and assign it a FIELD_ value */
583 v[0] = (uint16)va_arg(ap, int);
584 v[1] = (uint16)va_arg(ap, int);
585 _TIFFmemcpy(tv->value, &v, 4);
588 else if (fip->field_passcount
589 || fip->field_writecount == TIFF_VARIABLE
590 || fip->field_writecount == TIFF_VARIABLE2
591 || fip->field_writecount == TIFF_SPP
593 _TIFFmemcpy(tv->value, va_arg(ap, void *),
594 tv->count * tv_size);
596 char *val = (char *)tv->value;
597 assert( tv->count == 1 );
599 switch (fip->field_type) {
603 uint8 v = (uint8)va_arg(ap, int);
604 _TIFFmemcpy(val, &v, tv_size);
609 int8 v = (int8)va_arg(ap, int);
610 _TIFFmemcpy(val, &v, tv_size);
615 uint16 v = (uint16)va_arg(ap, int);
616 _TIFFmemcpy(val, &v, tv_size);
621 int16 v = (int16)va_arg(ap, int);
622 _TIFFmemcpy(val, &v, tv_size);
628 uint32 v = va_arg(ap, uint32);
629 _TIFFmemcpy(val, &v, tv_size);
634 int32 v = va_arg(ap, int32);
635 _TIFFmemcpy(val, &v, tv_size);
641 uint64 v = va_arg(ap, uint64);
642 _TIFFmemcpy(val, &v, tv_size);
647 int64 v = va_arg(ap, int64);
648 _TIFFmemcpy(val, &v, tv_size);
655 float v = (float)va_arg(ap, double);
656 _TIFFmemcpy(val, &v, tv_size);
661 double v = va_arg(ap, double);
662 _TIFFmemcpy(val, &v, tv_size);
666 _TIFFmemset(val, 0, tv_size);
675 const TIFFField* fip=TIFFFieldWithTag(tif,tag);
677 TIFFSetFieldBit(tif, fip->field_bit);
678 tif->tif_flags |= TIFF_DIRTYDIRECT;
686 const TIFFField* fip=TIFFFieldWithTag(tif,tag);
687 TIFFErrorExt(tif->tif_clientdata, module,
688 "%s: Bad value %u for \"%s\" tag",
690 fip ? fip->field_name : "Unknown");
696 const TIFFField* fip=TIFFFieldWithTag(tif,tag);
697 TIFFErrorExt(tif->tif_clientdata, module,
698 "%s: Bad value %u for \"%s\" tag",
700 fip ? fip->field_name : "Unknown");
706 const TIFFField* fip=TIFFFieldWithTag(tif,tag);
707 TIFFErrorExt(tif->tif_clientdata, module,
708 "%s: Bad value %f for \"%s\" tag",
709 tif->tif_name, dblval,
710 fip ? fip->field_name : "Unknown");
717 * Return 1/0 according to whether or not
718 * it is permissible to set the tag's value.
719 * Note that we allow ImageLength to be changed
720 * so that we can append and extend to images.
721 * Any other tag may not be altered once writing
722 * has commenced, unless its value has no effect
723 * on the format of the data that is written.
726 OkToChangeTag(TIFF* tif, uint32 tag)
728 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
729 if (!fip) { /* unknown tag */
730 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
731 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
734 if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
735 !fip->field_oktochange) {
737 * Consult info table to see if tag can be changed
738 * after we've started writing. We only allow changes
739 * to those tags that don't/shouldn't affect the
740 * compression and/or format of the data.
742 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
743 "%s: Cannot modify tag \"%s\" while writing",
744 tif->tif_name, fip->field_name);
751 * Record the value of a field in the
752 * internal directory structure. The
753 * field will be written to the file
754 * when/if the directory structure is
758 TIFFSetField(TIFF* tif, uint32 tag, ...)
764 status = TIFFVSetField(tif, tag, ap);
770 * Clear the contents of the field in the internal structure.
773 TIFFUnsetField(TIFF* tif, uint32 tag)
775 const TIFFField *fip = TIFFFieldWithTag(tif, tag);
776 TIFFDirectory* td = &tif->tif_dir;
781 if( fip->field_bit != FIELD_CUSTOM )
782 TIFFClrFieldBit(tif, fip->field_bit);
785 TIFFTagValue *tv = NULL;
788 for (i = 0; i < td->td_customValueCount; i++) {
790 tv = td->td_customValues + i;
791 if( tv->info->field_tag == tag )
795 if( i < td->td_customValueCount )
797 _TIFFfree(tv->value);
798 for( ; i < td->td_customValueCount-1; i++) {
799 td->td_customValues[i] = td->td_customValues[i+1];
801 td->td_customValueCount--;
805 tif->tif_flags |= TIFF_DIRTYDIRECT;
811 * Like TIFFSetField, but taking a varargs
812 * parameter list. This routine is useful
813 * for building higher-level interfaces on
814 * top of the library.
817 TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
819 return OkToChangeTag(tif, tag) ?
820 (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
824 _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
826 TIFFDirectory* td = &tif->tif_dir;
828 uint32 standard_tag = tag;
829 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
830 if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
834 * We want to force the custom code to be used for custom
835 * fields even if the tag happens to match a well known
836 * one - important for reinterpreted handling of standard
837 * tag values in custom directories (ie. EXIF)
839 if (fip->field_bit == FIELD_CUSTOM) {
843 switch (standard_tag) {
844 case TIFFTAG_SUBFILETYPE:
845 *va_arg(ap, uint32*) = td->td_subfiletype;
847 case TIFFTAG_IMAGEWIDTH:
848 *va_arg(ap, uint32*) = td->td_imagewidth;
850 case TIFFTAG_IMAGELENGTH:
851 *va_arg(ap, uint32*) = td->td_imagelength;
853 case TIFFTAG_BITSPERSAMPLE:
854 *va_arg(ap, uint16*) = td->td_bitspersample;
856 case TIFFTAG_COMPRESSION:
857 *va_arg(ap, uint16*) = td->td_compression;
859 case TIFFTAG_PHOTOMETRIC:
860 *va_arg(ap, uint16*) = td->td_photometric;
862 case TIFFTAG_THRESHHOLDING:
863 *va_arg(ap, uint16*) = td->td_threshholding;
865 case TIFFTAG_FILLORDER:
866 *va_arg(ap, uint16*) = td->td_fillorder;
868 case TIFFTAG_ORIENTATION:
869 *va_arg(ap, uint16*) = td->td_orientation;
871 case TIFFTAG_SAMPLESPERPIXEL:
872 *va_arg(ap, uint16*) = td->td_samplesperpixel;
874 case TIFFTAG_ROWSPERSTRIP:
875 *va_arg(ap, uint32*) = td->td_rowsperstrip;
877 case TIFFTAG_MINSAMPLEVALUE:
878 *va_arg(ap, uint16*) = td->td_minsamplevalue;
880 case TIFFTAG_MAXSAMPLEVALUE:
881 *va_arg(ap, uint16*) = td->td_maxsamplevalue;
883 case TIFFTAG_SMINSAMPLEVALUE:
884 if (tif->tif_flags & TIFF_PERSAMPLE)
885 *va_arg(ap, double**) = td->td_sminsamplevalue;
888 /* libtiff historially treats this as a single value. */
890 double v = td->td_sminsamplevalue[0];
891 for (i=1; i < td->td_samplesperpixel; ++i)
892 if( td->td_sminsamplevalue[i] < v )
893 v = td->td_sminsamplevalue[i];
894 *va_arg(ap, double*) = v;
897 case TIFFTAG_SMAXSAMPLEVALUE:
898 if (tif->tif_flags & TIFF_PERSAMPLE)
899 *va_arg(ap, double**) = td->td_smaxsamplevalue;
902 /* libtiff historially treats this as a single value. */
904 double v = td->td_smaxsamplevalue[0];
905 for (i=1; i < td->td_samplesperpixel; ++i)
906 if( td->td_smaxsamplevalue[i] > v )
907 v = td->td_smaxsamplevalue[i];
908 *va_arg(ap, double*) = v;
911 case TIFFTAG_XRESOLUTION:
912 *va_arg(ap, float*) = td->td_xresolution;
914 case TIFFTAG_YRESOLUTION:
915 *va_arg(ap, float*) = td->td_yresolution;
917 case TIFFTAG_PLANARCONFIG:
918 *va_arg(ap, uint16*) = td->td_planarconfig;
920 case TIFFTAG_XPOSITION:
921 *va_arg(ap, float*) = td->td_xposition;
923 case TIFFTAG_YPOSITION:
924 *va_arg(ap, float*) = td->td_yposition;
926 case TIFFTAG_RESOLUTIONUNIT:
927 *va_arg(ap, uint16*) = td->td_resolutionunit;
929 case TIFFTAG_PAGENUMBER:
930 *va_arg(ap, uint16*) = td->td_pagenumber[0];
931 *va_arg(ap, uint16*) = td->td_pagenumber[1];
933 case TIFFTAG_HALFTONEHINTS:
934 *va_arg(ap, uint16*) = td->td_halftonehints[0];
935 *va_arg(ap, uint16*) = td->td_halftonehints[1];
937 case TIFFTAG_COLORMAP:
938 *va_arg(ap, uint16**) = td->td_colormap[0];
939 *va_arg(ap, uint16**) = td->td_colormap[1];
940 *va_arg(ap, uint16**) = td->td_colormap[2];
942 case TIFFTAG_STRIPOFFSETS:
943 case TIFFTAG_TILEOFFSETS:
944 _TIFFFillStriles( tif );
945 *va_arg(ap, uint64**) = td->td_stripoffset;
947 case TIFFTAG_STRIPBYTECOUNTS:
948 case TIFFTAG_TILEBYTECOUNTS:
949 _TIFFFillStriles( tif );
950 *va_arg(ap, uint64**) = td->td_stripbytecount;
952 case TIFFTAG_MATTEING:
953 *va_arg(ap, uint16*) =
954 (td->td_extrasamples == 1 &&
955 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
957 case TIFFTAG_EXTRASAMPLES:
958 *va_arg(ap, uint16*) = td->td_extrasamples;
959 *va_arg(ap, uint16**) = td->td_sampleinfo;
961 case TIFFTAG_TILEWIDTH:
962 *va_arg(ap, uint32*) = td->td_tilewidth;
964 case TIFFTAG_TILELENGTH:
965 *va_arg(ap, uint32*) = td->td_tilelength;
967 case TIFFTAG_TILEDEPTH:
968 *va_arg(ap, uint32*) = td->td_tiledepth;
970 case TIFFTAG_DATATYPE:
971 switch (td->td_sampleformat) {
972 case SAMPLEFORMAT_UINT:
973 *va_arg(ap, uint16*) = DATATYPE_UINT;
975 case SAMPLEFORMAT_INT:
976 *va_arg(ap, uint16*) = DATATYPE_INT;
978 case SAMPLEFORMAT_IEEEFP:
979 *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
981 case SAMPLEFORMAT_VOID:
982 *va_arg(ap, uint16*) = DATATYPE_VOID;
986 case TIFFTAG_SAMPLEFORMAT:
987 *va_arg(ap, uint16*) = td->td_sampleformat;
989 case TIFFTAG_IMAGEDEPTH:
990 *va_arg(ap, uint32*) = td->td_imagedepth;
993 *va_arg(ap, uint16*) = td->td_nsubifd;
994 *va_arg(ap, uint64**) = td->td_subifd;
996 case TIFFTAG_YCBCRPOSITIONING:
997 *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
999 case TIFFTAG_YCBCRSUBSAMPLING:
1000 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
1001 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
1003 case TIFFTAG_TRANSFERFUNCTION:
1004 *va_arg(ap, uint16**) = td->td_transferfunction[0];
1005 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
1006 *va_arg(ap, uint16**) = td->td_transferfunction[1];
1007 *va_arg(ap, uint16**) = td->td_transferfunction[2];
1010 case TIFFTAG_REFERENCEBLACKWHITE:
1011 *va_arg(ap, float**) = td->td_refblackwhite;
1013 case TIFFTAG_INKNAMES:
1014 *va_arg(ap, char**) = td->td_inknames;
1021 * This can happen if multiple images are open
1022 * with different codecs which have private
1023 * tags. The global tag information table may
1024 * then have tags that are valid for one file
1025 * but not the other. If the client tries to
1026 * get a tag that is not valid for the image's
1027 * codec then we'll arrive here.
1029 if( fip->field_bit != FIELD_CUSTOM )
1031 TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
1032 "%s: Invalid %stag \"%s\" "
1033 "(not supported by codec)",
1035 isPseudoTag(tag) ? "pseudo-" : "",
1042 * Do we have a custom value?
1045 for (i = 0; i < td->td_customValueCount; i++) {
1046 TIFFTagValue *tv = td->td_customValues + i;
1048 if (tv->info->field_tag != tag)
1051 if (fip->field_passcount) {
1052 if (fip->field_readcount == TIFF_VARIABLE2)
1053 *va_arg(ap, uint32*) = (uint32)tv->count;
1054 else /* Assume TIFF_VARIABLE */
1055 *va_arg(ap, uint16*) = (uint16)tv->count;
1056 *va_arg(ap, void **) = tv->value;
1058 } else if (fip->field_tag == TIFFTAG_DOTRANGE
1059 && strcmp(fip->field_name,"DotRange") == 0) {
1060 /* TODO: This is an evil exception and should not have been
1061 handled this way ... likely best if we move it into
1062 the directory structure with an explicit field in
1063 libtiff 4.1 and assign it a FIELD_ value */
1064 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[0];
1065 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[1];
1068 if (fip->field_type == TIFF_ASCII
1069 || fip->field_readcount == TIFF_VARIABLE
1070 || fip->field_readcount == TIFF_VARIABLE2
1071 || fip->field_readcount == TIFF_SPP
1073 *va_arg(ap, void **) = tv->value;
1076 char *val = (char *)tv->value;
1077 assert( tv->count == 1 );
1078 switch (fip->field_type) {
1080 case TIFF_UNDEFINED:
1081 *va_arg(ap, uint8*) =
1086 *va_arg(ap, int8*) =
1091 *va_arg(ap, uint16*) =
1096 *va_arg(ap, int16*) =
1102 *va_arg(ap, uint32*) =
1107 *va_arg(ap, int32*) =
1113 *va_arg(ap, uint64*) =
1118 *va_arg(ap, int64*) =
1123 case TIFF_SRATIONAL:
1125 *va_arg(ap, float*) =
1130 *va_arg(ap, double*) =
1148 * Return the value of a field in the
1149 * internal directory structure.
1152 TIFFGetField(TIFF* tif, uint32 tag, ...)
1158 status = TIFFVGetField(tif, tag, ap);
1164 * Like TIFFGetField, but taking a varargs
1165 * parameter list. This routine is useful
1166 * for building higher-level interfaces on
1167 * top of the library.
1170 TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
1172 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
1173 return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
1174 (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
1177 #define CleanupField(member) { \
1179 _TIFFfree(td->member); \
1185 * Release storage associated with a directory.
1188 TIFFFreeDirectory(TIFF* tif)
1190 TIFFDirectory *td = &tif->tif_dir;
1193 _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
1194 CleanupField(td_sminsamplevalue);
1195 CleanupField(td_smaxsamplevalue);
1196 CleanupField(td_colormap[0]);
1197 CleanupField(td_colormap[1]);
1198 CleanupField(td_colormap[2]);
1199 CleanupField(td_sampleinfo);
1200 CleanupField(td_subifd);
1201 CleanupField(td_inknames);
1202 CleanupField(td_refblackwhite);
1203 CleanupField(td_transferfunction[0]);
1204 CleanupField(td_transferfunction[1]);
1205 CleanupField(td_transferfunction[2]);
1206 CleanupField(td_stripoffset);
1207 CleanupField(td_stripbytecount);
1208 TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
1209 TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
1211 /* Cleanup custom tag values */
1212 for( i = 0; i < td->td_customValueCount; i++ ) {
1213 if (td->td_customValues[i].value)
1214 _TIFFfree(td->td_customValues[i].value);
1217 td->td_customValueCount = 0;
1218 CleanupField(td_customValues);
1220 #if defined(DEFER_STRILE_LOAD)
1221 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
1222 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
1228 * Client Tag extension support (from Niles Ritter).
1230 static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
1233 TIFFSetTagExtender(TIFFExtendProc extender)
1235 TIFFExtendProc prev = _TIFFextender;
1236 _TIFFextender = extender;
1241 * Setup for a new directory. Should we automatically call
1242 * TIFFWriteDirectory() if the current one is dirty?
1244 * The newly created directory will not exist on the file till
1245 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1248 TIFFCreateDirectory(TIFF* tif)
1250 TIFFDefaultDirectory(tif);
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;
1261 TIFFCreateCustomDirectory(TIFF* tif, const TIFFFieldArray* infoarray)
1263 TIFFDefaultDirectory(tif);
1266 * Reset the field definitions to match the application provided list.
1267 * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
1268 * based on it's assumption this is an image directory.
1270 _TIFFSetupFields(tif, infoarray);
1272 tif->tif_diroff = 0;
1273 tif->tif_nextdiroff = 0;
1274 tif->tif_curoff = 0;
1275 tif->tif_row = (uint32) -1;
1276 tif->tif_curstrip = (uint32) -1;
1282 TIFFCreateEXIFDirectory(TIFF* tif)
1284 const TIFFFieldArray* exifFieldArray;
1285 exifFieldArray = _TIFFGetExifFields();
1286 return TIFFCreateCustomDirectory(tif, exifFieldArray);
1290 * Setup a default directory structure.
1293 TIFFDefaultDirectory(TIFF* tif)
1295 register TIFFDirectory* td = &tif->tif_dir;
1296 const TIFFFieldArray* tiffFieldArray;
1298 tiffFieldArray = _TIFFGetFields();
1299 _TIFFSetupFields(tif, tiffFieldArray);
1301 _TIFFmemset(td, 0, sizeof (*td));
1302 td->td_fillorder = FILLORDER_MSB2LSB;
1303 td->td_bitspersample = 1;
1304 td->td_threshholding = THRESHHOLD_BILEVEL;
1305 td->td_orientation = ORIENTATION_TOPLEFT;
1306 td->td_samplesperpixel = 1;
1307 td->td_rowsperstrip = (uint32) -1;
1308 td->td_tilewidth = 0;
1309 td->td_tilelength = 0;
1310 td->td_tiledepth = 1;
1311 td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
1312 td->td_resolutionunit = RESUNIT_INCH;
1313 td->td_sampleformat = SAMPLEFORMAT_UINT;
1314 td->td_imagedepth = 1;
1315 td->td_ycbcrsubsampling[0] = 2;
1316 td->td_ycbcrsubsampling[1] = 2;
1317 td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
1318 tif->tif_postdecode = _TIFFNoPostDecode;
1319 tif->tif_foundfield = NULL;
1320 tif->tif_tagmethods.vsetfield = _TIFFVSetField;
1321 tif->tif_tagmethods.vgetfield = _TIFFVGetField;
1322 tif->tif_tagmethods.printdir = NULL;
1324 * Give client code a chance to install their own
1325 * tag extensions & methods, prior to compression overloads,
1326 * but do some prior cleanup first. (http://trac.osgeo.org/gdal/ticket/5054)
1328 if (tif->tif_nfieldscompat > 0) {
1331 for (i = 0; i < tif->tif_nfieldscompat; i++) {
1332 if (tif->tif_fieldscompat[i].allocated_size)
1333 _TIFFfree(tif->tif_fieldscompat[i].fields);
1335 _TIFFfree(tif->tif_fieldscompat);
1336 tif->tif_nfieldscompat = 0;
1337 tif->tif_fieldscompat = NULL;
1340 (*_TIFFextender)(tif);
1341 (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1343 * NB: The directory is marked dirty as a result of setting
1344 * up the default compression scheme. However, this really
1345 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1346 * if the user does something. We could just do the setup
1347 * by hand, but it seems better to use the normal mechanism
1348 * (i.e. TIFFSetField).
1350 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1353 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1354 * we clear the ISTILED flag when setting up a new directory.
1355 * Should we also be clearing stuff like INSUBIFD?
1357 tif->tif_flags &= ~TIFF_ISTILED;
1363 TIFFAdvanceDirectory(TIFF* tif, uint64* nextdir, uint64* off)
1365 static const char module[] = "TIFFAdvanceDirectory";
1368 uint64 poff=*nextdir;
1369 if (!(tif->tif_flags&TIFF_BIGTIFF))
1371 tmsize_t poffa,poffb,poffc,poffd;
1374 poffa=(tmsize_t)poff;
1375 poffb=poffa+sizeof(uint16);
1376 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint16))||(poffb>tif->tif_size))
1378 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1382 _TIFFmemcpy(&dircount,tif->tif_base+poffa,sizeof(uint16));
1383 if (tif->tif_flags&TIFF_SWAB)
1384 TIFFSwabShort(&dircount);
1385 poffc=poffb+dircount*12;
1386 poffd=poffc+sizeof(uint32);
1387 if ((poffc<poffb)||(poffc<dircount*12)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint32))||(poffd>tif->tif_size))
1389 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1394 _TIFFmemcpy(&nextdir32,tif->tif_base+poffc,sizeof(uint32));
1395 if (tif->tif_flags&TIFF_SWAB)
1396 TIFFSwabLong(&nextdir32);
1401 tmsize_t poffa,poffb,poffc,poffd;
1404 poffa=(tmsize_t)poff;
1405 poffb=poffa+sizeof(uint64);
1406 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint64))||(poffb>tif->tif_size))
1408 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1411 _TIFFmemcpy(&dircount64,tif->tif_base+poffa,sizeof(uint64));
1412 if (tif->tif_flags&TIFF_SWAB)
1413 TIFFSwabLong8(&dircount64);
1414 if (dircount64>0xFFFF)
1416 TIFFErrorExt(tif->tif_clientdata,module,"Sanity check on directory count failed");
1419 dircount16=(uint16)dircount64;
1420 poffc=poffb+dircount16*20;
1421 poffd=poffc+sizeof(uint64);
1422 if ((poffc<poffb)||(poffc<dircount16*20)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint64))||(poffd>tif->tif_size))
1424 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1429 _TIFFmemcpy(nextdir,tif->tif_base+poffc,sizeof(uint64));
1430 if (tif->tif_flags&TIFF_SWAB)
1431 TIFFSwabLong8(nextdir);
1437 if (!(tif->tif_flags&TIFF_BIGTIFF))
1441 if (!SeekOK(tif, *nextdir) ||
1442 !ReadOK(tif, &dircount, sizeof (uint16))) {
1443 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1447 if (tif->tif_flags & TIFF_SWAB)
1448 TIFFSwabShort(&dircount);
1450 *off = TIFFSeekFile(tif,
1451 dircount*12, SEEK_CUR);
1453 (void) TIFFSeekFile(tif,
1454 dircount*12, SEEK_CUR);
1455 if (!ReadOK(tif, &nextdir32, sizeof (uint32))) {
1456 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
1460 if (tif->tif_flags & TIFF_SWAB)
1461 TIFFSwabLong(&nextdir32);
1468 if (!SeekOK(tif, *nextdir) ||
1469 !ReadOK(tif, &dircount64, sizeof (uint64))) {
1470 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1474 if (tif->tif_flags & TIFF_SWAB)
1475 TIFFSwabLong8(&dircount64);
1476 if (dircount64>0xFFFF)
1478 TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
1481 dircount16 = (uint16)dircount64;
1483 *off = TIFFSeekFile(tif,
1484 dircount16*20, SEEK_CUR);
1486 (void) TIFFSeekFile(tif,
1487 dircount16*20, SEEK_CUR);
1488 if (!ReadOK(tif, nextdir, sizeof (uint64))) {
1489 TIFFErrorExt(tif->tif_clientdata, module,
1490 "%s: Error fetching directory link",
1494 if (tif->tif_flags & TIFF_SWAB)
1495 TIFFSwabLong8(nextdir);
1502 * Count the number of directories in a file.
1505 TIFFNumberOfDirectories(TIFF* tif)
1507 static const char module[] = "TIFFNumberOfDirectories";
1510 if (!(tif->tif_flags&TIFF_BIGTIFF))
1511 nextdir = tif->tif_header.classic.tiff_diroff;
1513 nextdir = tif->tif_header.big.tiff_diroff;
1515 while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
1522 TIFFErrorExt(tif->tif_clientdata, module,
1523 "Directory count exceeded 65535 limit,"
1524 " giving up on counting.");
1532 * Set the n-th directory as the current directory.
1533 * NB: Directories are numbered starting at 0.
1536 TIFFSetDirectory(TIFF* tif, uint16 dirn)
1541 if (!(tif->tif_flags&TIFF_BIGTIFF))
1542 nextdir = tif->tif_header.classic.tiff_diroff;
1544 nextdir = tif->tif_header.big.tiff_diroff;
1545 for (n = dirn; n > 0 && nextdir != 0; n--)
1546 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1548 tif->tif_nextdiroff = nextdir;
1550 * Set curdir to the actual directory index. The
1551 * -1 is because TIFFReadDirectory will increment
1552 * tif_curdir after successfully reading the directory.
1554 tif->tif_curdir = (dirn - n) - 1;
1556 * Reset tif_dirnumber counter and start new list of seen directories.
1557 * We need this to prevent IFD loops.
1559 tif->tif_dirnumber = 0;
1560 return (TIFFReadDirectory(tif));
1564 * Set the current directory to be the directory
1565 * located at the specified file offset. This interface
1566 * is used mainly to access directories linked with
1567 * the SubIFD tag (e.g. thumbnail images).
1570 TIFFSetSubDirectory(TIFF* tif, uint64 diroff)
1572 tif->tif_nextdiroff = diroff;
1574 * Reset tif_dirnumber counter and start new list of seen directories.
1575 * We need this to prevent IFD loops.
1577 tif->tif_dirnumber = 0;
1578 return (TIFFReadDirectory(tif));
1582 * Return file offset of the current directory.
1585 TIFFCurrentDirOffset(TIFF* tif)
1587 return (tif->tif_diroff);
1591 * Return an indication of whether or not we are
1592 * at the last directory in the file.
1595 TIFFLastDirectory(TIFF* tif)
1597 return (tif->tif_nextdiroff == 0);
1601 * Unlink the specified directory from the directory chain.
1604 TIFFUnlinkDirectory(TIFF* tif, uint16 dirn)
1606 static const char module[] = "TIFFUnlinkDirectory";
1611 if (tif->tif_mode == O_RDONLY) {
1612 TIFFErrorExt(tif->tif_clientdata, module,
1613 "Can not unlink directory in read-only file");
1617 * Go to the directory before the one we want
1618 * to unlink and nab the offset of the link
1619 * field we'll need to patch.
1621 if (!(tif->tif_flags&TIFF_BIGTIFF))
1623 nextdir = tif->tif_header.classic.tiff_diroff;
1628 nextdir = tif->tif_header.big.tiff_diroff;
1631 for (n = dirn-1; n > 0; n--) {
1633 TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
1636 if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
1640 * Advance to the directory to be unlinked and fetch
1641 * the offset of the directory that follows.
1643 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1646 * Go back and patch the link field of the preceding
1647 * directory to point to the offset of the directory
1650 (void) TIFFSeekFile(tif, off, SEEK_SET);
1651 if (!(tif->tif_flags&TIFF_BIGTIFF))
1654 nextdir32=(uint32)nextdir;
1655 assert((uint64)nextdir32==nextdir);
1656 if (tif->tif_flags & TIFF_SWAB)
1657 TIFFSwabLong(&nextdir32);
1658 if (!WriteOK(tif, &nextdir32, sizeof (uint32))) {
1659 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1665 if (tif->tif_flags & TIFF_SWAB)
1666 TIFFSwabLong8(&nextdir);
1667 if (!WriteOK(tif, &nextdir, sizeof (uint64))) {
1668 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1673 * Leave directory state setup safely. We don't have
1674 * facilities for doing inserting and removing directories,
1675 * so it's safest to just invalidate everything. This
1676 * means that the caller can only append to the directory
1679 (*tif->tif_cleanup)(tif);
1680 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
1681 _TIFFfree(tif->tif_rawdata);
1682 tif->tif_rawdata = NULL;
1684 tif->tif_rawdataoff = 0;
1685 tif->tif_rawdataloaded = 0;
1687 tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE|TIFF_BUF4WRITE);
1688 TIFFFreeDirectory(tif);
1689 TIFFDefaultDirectory(tif);
1690 tif->tif_diroff = 0; /* force link on next write */
1691 tif->tif_nextdiroff = 0; /* next write must be at end */
1692 tif->tif_curoff = 0;
1693 tif->tif_row = (uint32) -1;
1694 tif->tif_curstrip = (uint32) -1;
1698 /* vim: set ts=8 sts=8 sw=8 noet: */