1 /* $Id: tif_dir.c,v 1.131 2017-07-11 21:38:04 erouault 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)
37 * These are used in the backwards compatibility code...
39 #define DATATYPE_VOID 0 /* !untyped data */
40 #define DATATYPE_INT 1 /* !signed integer data */
41 #define DATATYPE_UINT 2 /* !unsigned integer data */
42 #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
45 setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
52 tmsize_t bytes = (tmsize_t)(nmemb * elem_size);
53 if (elem_size && bytes / elem_size == nmemb)
54 *vpp = (void*) _TIFFmalloc(bytes);
56 _TIFFmemcpy(*vpp, vp, bytes);
59 void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
60 { setByteArray(vpp, vp, n, 1); }
61 void _TIFFsetString(char** cpp, char* cp)
62 { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
63 static void _TIFFsetNString(char** cpp, char* cp, uint32 n)
64 { setByteArray((void**) cpp, (void*) cp, n, 1); }
65 void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
66 { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
67 void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
68 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
69 static void _TIFFsetLong8Array(uint64** lpp, uint64* lp, uint32 n)
70 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint64)); }
71 void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
72 { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
73 void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
74 { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
77 setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
81 *vpp = _TIFFmalloc(nmemb*sizeof(double));
85 ((double*)*vpp)[nmemb] = value;
90 * Install extra samples information.
93 setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
95 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
96 #define EXTRASAMPLE_COREL_UNASSALPHA 999
101 *v = (uint16) va_arg(ap, uint16_vap);
102 if ((uint16) *v > td->td_samplesperpixel)
104 va = va_arg(ap, uint16*);
105 if (*v > 0 && va == NULL) /* typically missing param */
107 for (i = 0; i < *v; i++) {
108 if (va[i] > EXTRASAMPLE_UNASSALPHA) {
110 * XXX: Corel Draw is known to produce incorrect
111 * ExtraSamples tags which must be patched here if we
112 * want to be able to open some of the damaged TIFF
115 if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
116 va[i] = EXTRASAMPLE_UNASSALPHA;
121 td->td_extrasamples = (uint16) *v;
122 _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
125 #undef EXTRASAMPLE_COREL_UNASSALPHA
129 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
130 * zero if the ink names are not as expected.
133 checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
135 TIFFDirectory* td = &tif->tif_dir;
136 uint16 i = td->td_samplesperpixel;
139 const char* ep = s+slen;
142 for (; cp < ep && *cp != '\0'; cp++) {}
147 return ((uint32)(cp-s));
150 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
151 "%s: Invalid InkNames value; expecting %d names, found %d",
153 td->td_samplesperpixel,
154 td->td_samplesperpixel-i);
158 static float TIFFClampDoubleToFloat( double val )
168 _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
170 static const char module[] = "_TIFFVSetField";
172 TIFFDirectory* td = &tif->tif_dir;
177 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
178 uint32 standard_tag = tag;
179 if( fip == NULL ) /* cannot happen since OkToChangeTag() already checks it */
182 * We want to force the custom code to be used for custom
183 * fields even if the tag happens to match a well known
184 * one - important for reinterpreted handling of standard
185 * tag values in custom directories (i.e. EXIF)
187 if (fip->field_bit == FIELD_CUSTOM) {
191 switch (standard_tag) {
192 case TIFFTAG_SUBFILETYPE:
193 td->td_subfiletype = (uint32) va_arg(ap, uint32);
195 case TIFFTAG_IMAGEWIDTH:
196 td->td_imagewidth = (uint32) va_arg(ap, uint32);
198 case TIFFTAG_IMAGELENGTH:
199 td->td_imagelength = (uint32) va_arg(ap, uint32);
201 case TIFFTAG_BITSPERSAMPLE:
202 td->td_bitspersample = (uint16) va_arg(ap, uint16_vap);
204 * If the data require post-decoding processing to byte-swap
205 * samples, set it up here. Note that since tags are required
206 * to be ordered, compression code can override this behaviour
207 * in the setup method if it wants to roll the post decoding
208 * work in with its normal work.
210 if (tif->tif_flags & TIFF_SWAB) {
211 if (td->td_bitspersample == 8)
212 tif->tif_postdecode = _TIFFNoPostDecode;
213 else if (td->td_bitspersample == 16)
214 tif->tif_postdecode = _TIFFSwab16BitData;
215 else if (td->td_bitspersample == 24)
216 tif->tif_postdecode = _TIFFSwab24BitData;
217 else if (td->td_bitspersample == 32)
218 tif->tif_postdecode = _TIFFSwab32BitData;
219 else if (td->td_bitspersample == 64)
220 tif->tif_postdecode = _TIFFSwab64BitData;
221 else if (td->td_bitspersample == 128) /* two 64's */
222 tif->tif_postdecode = _TIFFSwab64BitData;
225 case TIFFTAG_COMPRESSION:
226 v = (uint16) va_arg(ap, uint16_vap);
228 * If we're changing the compression scheme, the notify the
229 * previous module so that it can cleanup any state it's
232 if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
233 if ((uint32)td->td_compression == v)
235 (*tif->tif_cleanup)(tif);
236 tif->tif_flags &= ~TIFF_CODERSETUP;
239 * Setup new compression routine state.
241 if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
242 td->td_compression = (uint16) v;
246 case TIFFTAG_PHOTOMETRIC:
247 td->td_photometric = (uint16) va_arg(ap, uint16_vap);
249 case TIFFTAG_THRESHHOLDING:
250 td->td_threshholding = (uint16) va_arg(ap, uint16_vap);
252 case TIFFTAG_FILLORDER:
253 v = (uint16) va_arg(ap, uint16_vap);
254 if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
256 td->td_fillorder = (uint16) v;
258 case TIFFTAG_ORIENTATION:
259 v = (uint16) va_arg(ap, uint16_vap);
260 if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
263 td->td_orientation = (uint16) v;
265 case TIFFTAG_SAMPLESPERPIXEL:
266 v = (uint16) va_arg(ap, uint16_vap);
269 if( v != td->td_samplesperpixel )
271 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
272 if( td->td_sminsamplevalue != NULL )
274 TIFFWarningExt(tif->tif_clientdata,module,
275 "SamplesPerPixel tag value is changing, "
276 "but SMinSampleValue tag was read with a different value. Cancelling it");
277 TIFFClrFieldBit(tif,FIELD_SMINSAMPLEVALUE);
278 _TIFFfree(td->td_sminsamplevalue);
279 td->td_sminsamplevalue = NULL;
281 if( td->td_smaxsamplevalue != NULL )
283 TIFFWarningExt(tif->tif_clientdata,module,
284 "SamplesPerPixel tag value is changing, "
285 "but SMaxSampleValue tag was read with a different value. Cancelling it");
286 TIFFClrFieldBit(tif,FIELD_SMAXSAMPLEVALUE);
287 _TIFFfree(td->td_smaxsamplevalue);
288 td->td_smaxsamplevalue = NULL;
291 td->td_samplesperpixel = (uint16) v;
293 case TIFFTAG_ROWSPERSTRIP:
294 v32 = (uint32) va_arg(ap, uint32);
297 td->td_rowsperstrip = v32;
298 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
299 td->td_tilelength = v32;
300 td->td_tilewidth = td->td_imagewidth;
303 case TIFFTAG_MINSAMPLEVALUE:
304 td->td_minsamplevalue = (uint16) va_arg(ap, uint16_vap);
306 case TIFFTAG_MAXSAMPLEVALUE:
307 td->td_maxsamplevalue = (uint16) va_arg(ap, uint16_vap);
309 case TIFFTAG_SMINSAMPLEVALUE:
310 if (tif->tif_flags & TIFF_PERSAMPLE)
311 _TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
313 setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
315 case TIFFTAG_SMAXSAMPLEVALUE:
316 if (tif->tif_flags & TIFF_PERSAMPLE)
317 _TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
319 setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
321 case TIFFTAG_XRESOLUTION:
322 dblval = va_arg(ap, double);
325 td->td_xresolution = TIFFClampDoubleToFloat( dblval );
327 case TIFFTAG_YRESOLUTION:
328 dblval = va_arg(ap, double);
331 td->td_yresolution = TIFFClampDoubleToFloat( dblval );
333 case TIFFTAG_PLANARCONFIG:
334 v = (uint16) va_arg(ap, uint16_vap);
335 if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
337 td->td_planarconfig = (uint16) v;
339 case TIFFTAG_XPOSITION:
340 td->td_xposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
342 case TIFFTAG_YPOSITION:
343 td->td_yposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
345 case TIFFTAG_RESOLUTIONUNIT:
346 v = (uint16) va_arg(ap, uint16_vap);
347 if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
349 td->td_resolutionunit = (uint16) v;
351 case TIFFTAG_PAGENUMBER:
352 td->td_pagenumber[0] = (uint16) va_arg(ap, uint16_vap);
353 td->td_pagenumber[1] = (uint16) va_arg(ap, uint16_vap);
355 case TIFFTAG_HALFTONEHINTS:
356 td->td_halftonehints[0] = (uint16) va_arg(ap, uint16_vap);
357 td->td_halftonehints[1] = (uint16) va_arg(ap, uint16_vap);
359 case TIFFTAG_COLORMAP:
360 v32 = (uint32)(1L<<td->td_bitspersample);
361 _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
362 _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
363 _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
365 case TIFFTAG_EXTRASAMPLES:
366 if (!setExtraSamples(td, ap, &v))
369 case TIFFTAG_MATTEING:
370 td->td_extrasamples = (((uint16) va_arg(ap, uint16_vap)) != 0);
371 if (td->td_extrasamples) {
372 uint16 sv = EXTRASAMPLE_ASSOCALPHA;
373 _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
376 case TIFFTAG_TILEWIDTH:
377 v32 = (uint32) va_arg(ap, uint32);
379 if (tif->tif_mode != O_RDONLY)
381 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
382 "Nonstandard tile width %d, convert file", v32);
384 td->td_tilewidth = v32;
385 tif->tif_flags |= TIFF_ISTILED;
387 case TIFFTAG_TILELENGTH:
388 v32 = (uint32) va_arg(ap, uint32);
390 if (tif->tif_mode != O_RDONLY)
392 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
393 "Nonstandard tile length %d, convert file", v32);
395 td->td_tilelength = v32;
396 tif->tif_flags |= TIFF_ISTILED;
398 case TIFFTAG_TILEDEPTH:
399 v32 = (uint32) va_arg(ap, uint32);
402 td->td_tiledepth = v32;
404 case TIFFTAG_DATATYPE:
405 v = (uint16) va_arg(ap, uint16_vap);
407 case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
408 case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
409 case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
410 case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
411 default: goto badvalue;
413 td->td_sampleformat = (uint16) v;
415 case TIFFTAG_SAMPLEFORMAT:
416 v = (uint16) va_arg(ap, uint16_vap);
417 if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
419 td->td_sampleformat = (uint16) v;
421 /* Try to fix up the SWAB function for complex data. */
422 if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
423 && td->td_bitspersample == 32
424 && tif->tif_postdecode == _TIFFSwab32BitData )
425 tif->tif_postdecode = _TIFFSwab16BitData;
426 else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
427 || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
428 && td->td_bitspersample == 64
429 && tif->tif_postdecode == _TIFFSwab64BitData )
430 tif->tif_postdecode = _TIFFSwab32BitData;
432 case TIFFTAG_IMAGEDEPTH:
433 td->td_imagedepth = (uint32) va_arg(ap, uint32);
436 if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
437 td->td_nsubifd = (uint16) va_arg(ap, uint16_vap);
438 _TIFFsetLong8Array(&td->td_subifd, (uint64*) va_arg(ap, uint64*),
439 (uint32) td->td_nsubifd);
441 TIFFErrorExt(tif->tif_clientdata, module,
442 "%s: Sorry, cannot nest SubIFDs",
447 case TIFFTAG_YCBCRPOSITIONING:
448 td->td_ycbcrpositioning = (uint16) va_arg(ap, uint16_vap);
450 case TIFFTAG_YCBCRSUBSAMPLING:
451 td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, uint16_vap);
452 td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, uint16_vap);
454 case TIFFTAG_TRANSFERFUNCTION:
455 v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
456 for (i = 0; i < v; i++)
457 _TIFFsetShortArray(&td->td_transferfunction[i],
458 va_arg(ap, uint16*), 1U<<td->td_bitspersample);
460 case TIFFTAG_REFERENCEBLACKWHITE:
461 /* XXX should check for null range */
462 _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
464 case TIFFTAG_INKNAMES:
465 v = (uint16) va_arg(ap, uint16_vap);
466 s = va_arg(ap, char*);
467 v = checkInkNamesString(tif, v, s);
470 _TIFFsetNString(&td->td_inknames, s, v);
471 td->td_inknameslen = v;
474 case TIFFTAG_PERSAMPLE:
475 v = (uint16) va_arg(ap, uint16_vap);
476 if( v == PERSAMPLE_MULTI )
477 tif->tif_flags |= TIFF_PERSAMPLE;
479 tif->tif_flags &= ~TIFF_PERSAMPLE;
483 int tv_size, iCustom;
486 * This can happen if multiple images are open with different
487 * codecs which have private tags. The global tag information
488 * table may then have tags that are valid for one file but not
489 * the other. If the client tries to set a tag that is not valid
490 * for the image's codec then we'll arrive here. This
491 * happens, for example, when tiffcp is used to convert between
492 * compression schemes and codec-specific tags are blindly copied.
494 if(fip->field_bit != FIELD_CUSTOM) {
495 TIFFErrorExt(tif->tif_clientdata, module,
496 "%s: Invalid %stag \"%s\" (not supported by codec)",
497 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
504 * Find the existing entry for this custom value.
507 for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
508 if (td->td_customValues[iCustom].info->field_tag == tag) {
509 tv = td->td_customValues + iCustom;
510 if (tv->value != NULL) {
511 _TIFFfree(tv->value);
519 * Grow the custom list if the entry was not found.
522 TIFFTagValue *new_customValues;
524 td->td_customValueCount++;
525 new_customValues = (TIFFTagValue *)
526 _TIFFrealloc(td->td_customValues,
527 sizeof(TIFFTagValue) * td->td_customValueCount);
528 if (!new_customValues) {
529 TIFFErrorExt(tif->tif_clientdata, module,
530 "%s: Failed to allocate space for list of custom values",
536 td->td_customValues = new_customValues;
538 tv = td->td_customValues + (td->td_customValueCount - 1);
545 * Set custom value ... save a copy of the custom tag value.
547 tv_size = _TIFFDataSize(fip->field_type);
550 TIFFErrorExt(tif->tif_clientdata, module,
551 "%s: Bad field type %d for \"%s\"",
552 tif->tif_name, fip->field_type,
557 if (fip->field_type == TIFF_ASCII)
561 if (fip->field_passcount)
563 assert(fip->field_writecount==TIFF_VARIABLE2);
564 ma=(uint32)va_arg(ap,uint32);
565 mb=(char*)va_arg(ap,char*);
569 mb=(char*)va_arg(ap,char*);
570 ma=(uint32)(strlen(mb)+1);
573 setByteArray(&tv->value,mb,ma,1);
577 if (fip->field_passcount) {
578 if (fip->field_writecount == TIFF_VARIABLE2)
579 tv->count = (uint32) va_arg(ap, uint32);
581 tv->count = (int) va_arg(ap, int);
582 } else if (fip->field_writecount == TIFF_VARIABLE
583 || fip->field_writecount == TIFF_VARIABLE2)
585 else if (fip->field_writecount == TIFF_SPP)
586 tv->count = td->td_samplesperpixel;
588 tv->count = fip->field_writecount;
590 if (tv->count == 0) {
592 TIFFErrorExt(tif->tif_clientdata, module,
593 "%s: Null count for \"%s\" (type "
594 "%d, writecount %d, passcount %d)",
598 fip->field_writecount,
599 fip->field_passcount);
603 tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
604 "custom tag binary object");
610 if (fip->field_tag == TIFFTAG_DOTRANGE
611 && strcmp(fip->field_name,"DotRange") == 0) {
612 /* TODO: This is an evil exception and should not have been
613 handled this way ... likely best if we move it into
614 the directory structure with an explicit field in
615 libtiff 4.1 and assign it a FIELD_ value */
617 v2[0] = (uint16)va_arg(ap, int);
618 v2[1] = (uint16)va_arg(ap, int);
619 _TIFFmemcpy(tv->value, &v2, 4);
622 else if (fip->field_passcount
623 || fip->field_writecount == TIFF_VARIABLE
624 || fip->field_writecount == TIFF_VARIABLE2
625 || fip->field_writecount == TIFF_SPP
627 _TIFFmemcpy(tv->value, va_arg(ap, void *),
628 tv->count * tv_size);
630 char *val = (char *)tv->value;
631 assert( tv->count == 1 );
633 switch (fip->field_type) {
637 uint8 v2 = (uint8)va_arg(ap, int);
638 _TIFFmemcpy(val, &v2, tv_size);
643 int8 v2 = (int8)va_arg(ap, int);
644 _TIFFmemcpy(val, &v2, tv_size);
649 uint16 v2 = (uint16)va_arg(ap, int);
650 _TIFFmemcpy(val, &v2, tv_size);
655 int16 v2 = (int16)va_arg(ap, int);
656 _TIFFmemcpy(val, &v2, tv_size);
662 uint32 v2 = va_arg(ap, uint32);
663 _TIFFmemcpy(val, &v2, tv_size);
668 int32 v2 = va_arg(ap, int32);
669 _TIFFmemcpy(val, &v2, tv_size);
675 uint64 v2 = va_arg(ap, uint64);
676 _TIFFmemcpy(val, &v2, tv_size);
681 int64 v2 = va_arg(ap, int64);
682 _TIFFmemcpy(val, &v2, tv_size);
689 float v2 = TIFFClampDoubleToFloat(va_arg(ap, double));
690 _TIFFmemcpy(val, &v2, tv_size);
695 double v2 = va_arg(ap, double);
696 _TIFFmemcpy(val, &v2, tv_size);
700 _TIFFmemset(val, 0, tv_size);
709 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
711 TIFFSetFieldBit(tif, fip2->field_bit);
712 tif->tif_flags |= TIFF_DIRTYDIRECT;
720 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
721 TIFFErrorExt(tif->tif_clientdata, module,
722 "%s: Bad value %u for \"%s\" tag",
724 fip2 ? fip2->field_name : "Unknown");
730 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
731 TIFFErrorExt(tif->tif_clientdata, module,
732 "%s: Bad value %u for \"%s\" tag",
734 fip2 ? fip2->field_name : "Unknown");
740 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
741 TIFFErrorExt(tif->tif_clientdata, module,
742 "%s: Bad value %f for \"%s\" tag",
743 tif->tif_name, dblval,
744 fip2 ? fip2->field_name : "Unknown");
751 * Return 1/0 according to whether or not
752 * it is permissible to set the tag's value.
753 * Note that we allow ImageLength to be changed
754 * so that we can append and extend to images.
755 * Any other tag may not be altered once writing
756 * has commenced, unless its value has no effect
757 * on the format of the data that is written.
760 OkToChangeTag(TIFF* tif, uint32 tag)
762 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
763 if (!fip) { /* unknown tag */
764 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
765 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
768 if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
769 !fip->field_oktochange) {
771 * Consult info table to see if tag can be changed
772 * after we've started writing. We only allow changes
773 * to those tags that don't/shouldn't affect the
774 * compression and/or format of the data.
776 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
777 "%s: Cannot modify tag \"%s\" while writing",
778 tif->tif_name, fip->field_name);
785 * Record the value of a field in the
786 * internal directory structure. The
787 * field will be written to the file
788 * when/if the directory structure is
792 TIFFSetField(TIFF* tif, uint32 tag, ...)
798 status = TIFFVSetField(tif, tag, ap);
804 * Clear the contents of the field in the internal structure.
807 TIFFUnsetField(TIFF* tif, uint32 tag)
809 const TIFFField *fip = TIFFFieldWithTag(tif, tag);
810 TIFFDirectory* td = &tif->tif_dir;
815 if( fip->field_bit != FIELD_CUSTOM )
816 TIFFClrFieldBit(tif, fip->field_bit);
819 TIFFTagValue *tv = NULL;
822 for (i = 0; i < td->td_customValueCount; i++) {
824 tv = td->td_customValues + i;
825 if( tv->info->field_tag == tag )
829 if( i < td->td_customValueCount )
831 _TIFFfree(tv->value);
832 for( ; i < td->td_customValueCount-1; i++) {
833 td->td_customValues[i] = td->td_customValues[i+1];
835 td->td_customValueCount--;
839 tif->tif_flags |= TIFF_DIRTYDIRECT;
845 * Like TIFFSetField, but taking a varargs
846 * parameter list. This routine is useful
847 * for building higher-level interfaces on
848 * top of the library.
851 TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
853 return OkToChangeTag(tif, tag) ?
854 (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
858 _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
860 TIFFDirectory* td = &tif->tif_dir;
862 uint32 standard_tag = tag;
863 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
864 if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
867 if( tag == TIFFTAG_NUMBEROFINKS )
870 for (i = 0; i < td->td_customValueCount; i++) {
872 TIFFTagValue *tv = td->td_customValues + i;
873 if (tv->info->field_tag != tag)
875 if( tv->value == NULL )
877 val = *(uint16 *)tv->value;
878 /* Truncate to SamplesPerPixel, since the */
879 /* setting code for INKNAMES assume that there are SamplesPerPixel */
881 /* Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2599 */
882 if( val > td->td_samplesperpixel )
884 TIFFWarningExt(tif->tif_clientdata,"_TIFFVGetField",
885 "Truncating NumberOfInks from %u to %u",
886 val, td->td_samplesperpixel);
887 val = td->td_samplesperpixel;
889 *va_arg(ap, uint16*) = val;
896 * We want to force the custom code to be used for custom
897 * fields even if the tag happens to match a well known
898 * one - important for reinterpreted handling of standard
899 * tag values in custom directories (i.e. EXIF)
901 if (fip->field_bit == FIELD_CUSTOM) {
905 switch (standard_tag) {
906 case TIFFTAG_SUBFILETYPE:
907 *va_arg(ap, uint32*) = td->td_subfiletype;
909 case TIFFTAG_IMAGEWIDTH:
910 *va_arg(ap, uint32*) = td->td_imagewidth;
912 case TIFFTAG_IMAGELENGTH:
913 *va_arg(ap, uint32*) = td->td_imagelength;
915 case TIFFTAG_BITSPERSAMPLE:
916 *va_arg(ap, uint16*) = td->td_bitspersample;
918 case TIFFTAG_COMPRESSION:
919 *va_arg(ap, uint16*) = td->td_compression;
921 case TIFFTAG_PHOTOMETRIC:
922 *va_arg(ap, uint16*) = td->td_photometric;
924 case TIFFTAG_THRESHHOLDING:
925 *va_arg(ap, uint16*) = td->td_threshholding;
927 case TIFFTAG_FILLORDER:
928 *va_arg(ap, uint16*) = td->td_fillorder;
930 case TIFFTAG_ORIENTATION:
931 *va_arg(ap, uint16*) = td->td_orientation;
933 case TIFFTAG_SAMPLESPERPIXEL:
934 *va_arg(ap, uint16*) = td->td_samplesperpixel;
936 case TIFFTAG_ROWSPERSTRIP:
937 *va_arg(ap, uint32*) = td->td_rowsperstrip;
939 case TIFFTAG_MINSAMPLEVALUE:
940 *va_arg(ap, uint16*) = td->td_minsamplevalue;
942 case TIFFTAG_MAXSAMPLEVALUE:
943 *va_arg(ap, uint16*) = td->td_maxsamplevalue;
945 case TIFFTAG_SMINSAMPLEVALUE:
946 if (tif->tif_flags & TIFF_PERSAMPLE)
947 *va_arg(ap, double**) = td->td_sminsamplevalue;
950 /* libtiff historically treats this as a single value. */
952 double v = td->td_sminsamplevalue[0];
953 for (i=1; i < td->td_samplesperpixel; ++i)
954 if( td->td_sminsamplevalue[i] < v )
955 v = td->td_sminsamplevalue[i];
956 *va_arg(ap, double*) = v;
959 case TIFFTAG_SMAXSAMPLEVALUE:
960 if (tif->tif_flags & TIFF_PERSAMPLE)
961 *va_arg(ap, double**) = td->td_smaxsamplevalue;
964 /* libtiff historically treats this as a single value. */
966 double v = td->td_smaxsamplevalue[0];
967 for (i=1; i < td->td_samplesperpixel; ++i)
968 if( td->td_smaxsamplevalue[i] > v )
969 v = td->td_smaxsamplevalue[i];
970 *va_arg(ap, double*) = v;
973 case TIFFTAG_XRESOLUTION:
974 *va_arg(ap, float*) = td->td_xresolution;
976 case TIFFTAG_YRESOLUTION:
977 *va_arg(ap, float*) = td->td_yresolution;
979 case TIFFTAG_PLANARCONFIG:
980 *va_arg(ap, uint16*) = td->td_planarconfig;
982 case TIFFTAG_XPOSITION:
983 *va_arg(ap, float*) = td->td_xposition;
985 case TIFFTAG_YPOSITION:
986 *va_arg(ap, float*) = td->td_yposition;
988 case TIFFTAG_RESOLUTIONUNIT:
989 *va_arg(ap, uint16*) = td->td_resolutionunit;
991 case TIFFTAG_PAGENUMBER:
992 *va_arg(ap, uint16*) = td->td_pagenumber[0];
993 *va_arg(ap, uint16*) = td->td_pagenumber[1];
995 case TIFFTAG_HALFTONEHINTS:
996 *va_arg(ap, uint16*) = td->td_halftonehints[0];
997 *va_arg(ap, uint16*) = td->td_halftonehints[1];
999 case TIFFTAG_COLORMAP:
1000 *va_arg(ap, uint16**) = td->td_colormap[0];
1001 *va_arg(ap, uint16**) = td->td_colormap[1];
1002 *va_arg(ap, uint16**) = td->td_colormap[2];
1004 case TIFFTAG_STRIPOFFSETS:
1005 case TIFFTAG_TILEOFFSETS:
1006 _TIFFFillStriles( tif );
1007 *va_arg(ap, uint64**) = td->td_stripoffset;
1009 case TIFFTAG_STRIPBYTECOUNTS:
1010 case TIFFTAG_TILEBYTECOUNTS:
1011 _TIFFFillStriles( tif );
1012 *va_arg(ap, uint64**) = td->td_stripbytecount;
1014 case TIFFTAG_MATTEING:
1015 *va_arg(ap, uint16*) =
1016 (td->td_extrasamples == 1 &&
1017 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
1019 case TIFFTAG_EXTRASAMPLES:
1020 *va_arg(ap, uint16*) = td->td_extrasamples;
1021 *va_arg(ap, uint16**) = td->td_sampleinfo;
1023 case TIFFTAG_TILEWIDTH:
1024 *va_arg(ap, uint32*) = td->td_tilewidth;
1026 case TIFFTAG_TILELENGTH:
1027 *va_arg(ap, uint32*) = td->td_tilelength;
1029 case TIFFTAG_TILEDEPTH:
1030 *va_arg(ap, uint32*) = td->td_tiledepth;
1032 case TIFFTAG_DATATYPE:
1033 switch (td->td_sampleformat) {
1034 case SAMPLEFORMAT_UINT:
1035 *va_arg(ap, uint16*) = DATATYPE_UINT;
1037 case SAMPLEFORMAT_INT:
1038 *va_arg(ap, uint16*) = DATATYPE_INT;
1040 case SAMPLEFORMAT_IEEEFP:
1041 *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
1043 case SAMPLEFORMAT_VOID:
1044 *va_arg(ap, uint16*) = DATATYPE_VOID;
1048 case TIFFTAG_SAMPLEFORMAT:
1049 *va_arg(ap, uint16*) = td->td_sampleformat;
1051 case TIFFTAG_IMAGEDEPTH:
1052 *va_arg(ap, uint32*) = td->td_imagedepth;
1054 case TIFFTAG_SUBIFD:
1055 *va_arg(ap, uint16*) = td->td_nsubifd;
1056 *va_arg(ap, uint64**) = td->td_subifd;
1058 case TIFFTAG_YCBCRPOSITIONING:
1059 *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
1061 case TIFFTAG_YCBCRSUBSAMPLING:
1062 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
1063 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
1065 case TIFFTAG_TRANSFERFUNCTION:
1066 *va_arg(ap, uint16**) = td->td_transferfunction[0];
1067 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
1068 *va_arg(ap, uint16**) = td->td_transferfunction[1];
1069 *va_arg(ap, uint16**) = td->td_transferfunction[2];
1072 case TIFFTAG_REFERENCEBLACKWHITE:
1073 *va_arg(ap, float**) = td->td_refblackwhite;
1075 case TIFFTAG_INKNAMES:
1076 *va_arg(ap, char**) = td->td_inknames;
1083 * This can happen if multiple images are open
1084 * with different codecs which have private
1085 * tags. The global tag information table may
1086 * then have tags that are valid for one file
1087 * but not the other. If the client tries to
1088 * get a tag that is not valid for the image's
1089 * codec then we'll arrive here.
1091 if( fip->field_bit != FIELD_CUSTOM )
1093 TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
1094 "%s: Invalid %stag \"%s\" "
1095 "(not supported by codec)",
1097 isPseudoTag(tag) ? "pseudo-" : "",
1104 * Do we have a custom value?
1107 for (i = 0; i < td->td_customValueCount; i++) {
1108 TIFFTagValue *tv = td->td_customValues + i;
1110 if (tv->info->field_tag != tag)
1113 if (fip->field_passcount) {
1114 if (fip->field_readcount == TIFF_VARIABLE2)
1115 *va_arg(ap, uint32*) = (uint32)tv->count;
1116 else /* Assume TIFF_VARIABLE */
1117 *va_arg(ap, uint16*) = (uint16)tv->count;
1118 *va_arg(ap, void **) = tv->value;
1120 } else if (fip->field_tag == TIFFTAG_DOTRANGE
1121 && strcmp(fip->field_name,"DotRange") == 0) {
1122 /* TODO: This is an evil exception and should not have been
1123 handled this way ... likely best if we move it into
1124 the directory structure with an explicit field in
1125 libtiff 4.1 and assign it a FIELD_ value */
1126 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[0];
1127 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[1];
1130 if (fip->field_type == TIFF_ASCII
1131 || fip->field_readcount == TIFF_VARIABLE
1132 || fip->field_readcount == TIFF_VARIABLE2
1133 || fip->field_readcount == TIFF_SPP
1135 *va_arg(ap, void **) = tv->value;
1138 char *val = (char *)tv->value;
1139 assert( tv->count == 1 );
1140 switch (fip->field_type) {
1142 case TIFF_UNDEFINED:
1143 *va_arg(ap, uint8*) =
1148 *va_arg(ap, int8*) =
1153 *va_arg(ap, uint16*) =
1158 *va_arg(ap, int16*) =
1164 *va_arg(ap, uint32*) =
1169 *va_arg(ap, int32*) =
1175 *va_arg(ap, uint64*) =
1180 *va_arg(ap, int64*) =
1185 case TIFF_SRATIONAL:
1187 *va_arg(ap, float*) =
1192 *va_arg(ap, double*) =
1210 * Return the value of a field in the
1211 * internal directory structure.
1214 TIFFGetField(TIFF* tif, uint32 tag, ...)
1220 status = TIFFVGetField(tif, tag, ap);
1226 * Like TIFFGetField, but taking a varargs
1227 * parameter list. This routine is useful
1228 * for building higher-level interfaces on
1229 * top of the library.
1232 TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
1234 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
1235 return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
1236 (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
1239 #define CleanupField(member) { \
1241 _TIFFfree(td->member); \
1247 * Release storage associated with a directory.
1250 TIFFFreeDirectory(TIFF* tif)
1252 TIFFDirectory *td = &tif->tif_dir;
1255 _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
1256 CleanupField(td_sminsamplevalue);
1257 CleanupField(td_smaxsamplevalue);
1258 CleanupField(td_colormap[0]);
1259 CleanupField(td_colormap[1]);
1260 CleanupField(td_colormap[2]);
1261 CleanupField(td_sampleinfo);
1262 CleanupField(td_subifd);
1263 CleanupField(td_inknames);
1264 CleanupField(td_refblackwhite);
1265 CleanupField(td_transferfunction[0]);
1266 CleanupField(td_transferfunction[1]);
1267 CleanupField(td_transferfunction[2]);
1268 CleanupField(td_stripoffset);
1269 CleanupField(td_stripbytecount);
1270 TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
1271 TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
1273 /* Cleanup custom tag values */
1274 for( i = 0; i < td->td_customValueCount; i++ ) {
1275 if (td->td_customValues[i].value)
1276 _TIFFfree(td->td_customValues[i].value);
1279 td->td_customValueCount = 0;
1280 CleanupField(td_customValues);
1282 #if defined(DEFER_STRILE_LOAD)
1283 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
1284 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
1290 * Client Tag extension support (from Niles Ritter).
1292 static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
1295 TIFFSetTagExtender(TIFFExtendProc extender)
1297 TIFFExtendProc prev = _TIFFextender;
1298 _TIFFextender = extender;
1303 * Setup for a new directory. Should we automatically call
1304 * TIFFWriteDirectory() if the current one is dirty?
1306 * The newly created directory will not exist on the file till
1307 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1310 TIFFCreateDirectory(TIFF* tif)
1312 TIFFDefaultDirectory(tif);
1313 tif->tif_diroff = 0;
1314 tif->tif_nextdiroff = 0;
1315 tif->tif_curoff = 0;
1316 tif->tif_row = (uint32) -1;
1317 tif->tif_curstrip = (uint32) -1;
1323 TIFFCreateCustomDirectory(TIFF* tif, const TIFFFieldArray* infoarray)
1325 TIFFDefaultDirectory(tif);
1328 * Reset the field definitions to match the application provided list.
1329 * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
1330 * based on it's assumption this is an image directory.
1332 _TIFFSetupFields(tif, infoarray);
1334 tif->tif_diroff = 0;
1335 tif->tif_nextdiroff = 0;
1336 tif->tif_curoff = 0;
1337 tif->tif_row = (uint32) -1;
1338 tif->tif_curstrip = (uint32) -1;
1344 TIFFCreateEXIFDirectory(TIFF* tif)
1346 const TIFFFieldArray* exifFieldArray;
1347 exifFieldArray = _TIFFGetExifFields();
1348 return TIFFCreateCustomDirectory(tif, exifFieldArray);
1352 * Setup a default directory structure.
1355 TIFFDefaultDirectory(TIFF* tif)
1357 register TIFFDirectory* td = &tif->tif_dir;
1358 const TIFFFieldArray* tiffFieldArray;
1360 tiffFieldArray = _TIFFGetFields();
1361 _TIFFSetupFields(tif, tiffFieldArray);
1363 _TIFFmemset(td, 0, sizeof (*td));
1364 td->td_fillorder = FILLORDER_MSB2LSB;
1365 td->td_bitspersample = 1;
1366 td->td_threshholding = THRESHHOLD_BILEVEL;
1367 td->td_orientation = ORIENTATION_TOPLEFT;
1368 td->td_samplesperpixel = 1;
1369 td->td_rowsperstrip = (uint32) -1;
1370 td->td_tilewidth = 0;
1371 td->td_tilelength = 0;
1372 td->td_tiledepth = 1;
1373 td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
1374 td->td_resolutionunit = RESUNIT_INCH;
1375 td->td_sampleformat = SAMPLEFORMAT_UINT;
1376 td->td_imagedepth = 1;
1377 td->td_ycbcrsubsampling[0] = 2;
1378 td->td_ycbcrsubsampling[1] = 2;
1379 td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
1380 tif->tif_postdecode = _TIFFNoPostDecode;
1381 tif->tif_foundfield = NULL;
1382 tif->tif_tagmethods.vsetfield = _TIFFVSetField;
1383 tif->tif_tagmethods.vgetfield = _TIFFVGetField;
1384 tif->tif_tagmethods.printdir = NULL;
1386 * Give client code a chance to install their own
1387 * tag extensions & methods, prior to compression overloads,
1388 * but do some prior cleanup first. (http://trac.osgeo.org/gdal/ticket/5054)
1390 if (tif->tif_nfieldscompat > 0) {
1393 for (i = 0; i < tif->tif_nfieldscompat; i++) {
1394 if (tif->tif_fieldscompat[i].allocated_size)
1395 _TIFFfree(tif->tif_fieldscompat[i].fields);
1397 _TIFFfree(tif->tif_fieldscompat);
1398 tif->tif_nfieldscompat = 0;
1399 tif->tif_fieldscompat = NULL;
1402 (*_TIFFextender)(tif);
1403 (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1405 * NB: The directory is marked dirty as a result of setting
1406 * up the default compression scheme. However, this really
1407 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1408 * if the user does something. We could just do the setup
1409 * by hand, but it seems better to use the normal mechanism
1410 * (i.e. TIFFSetField).
1412 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1415 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1416 * we clear the ISTILED flag when setting up a new directory.
1417 * Should we also be clearing stuff like INSUBIFD?
1419 tif->tif_flags &= ~TIFF_ISTILED;
1425 TIFFAdvanceDirectory(TIFF* tif, uint64* nextdir, uint64* off)
1427 static const char module[] = "TIFFAdvanceDirectory";
1430 uint64 poff=*nextdir;
1431 if (!(tif->tif_flags&TIFF_BIGTIFF))
1433 tmsize_t poffa,poffb,poffc,poffd;
1436 poffa=(tmsize_t)poff;
1437 poffb=poffa+sizeof(uint16);
1438 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint16))||(poffb>tif->tif_size))
1440 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1444 _TIFFmemcpy(&dircount,tif->tif_base+poffa,sizeof(uint16));
1445 if (tif->tif_flags&TIFF_SWAB)
1446 TIFFSwabShort(&dircount);
1447 poffc=poffb+dircount*12;
1448 poffd=poffc+sizeof(uint32);
1449 if ((poffc<poffb)||(poffc<dircount*12)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint32))||(poffd>tif->tif_size))
1451 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1456 _TIFFmemcpy(&nextdir32,tif->tif_base+poffc,sizeof(uint32));
1457 if (tif->tif_flags&TIFF_SWAB)
1458 TIFFSwabLong(&nextdir32);
1463 tmsize_t poffa,poffb,poffc,poffd;
1466 poffa=(tmsize_t)poff;
1467 poffb=poffa+sizeof(uint64);
1468 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint64))||(poffb>tif->tif_size))
1470 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1473 _TIFFmemcpy(&dircount64,tif->tif_base+poffa,sizeof(uint64));
1474 if (tif->tif_flags&TIFF_SWAB)
1475 TIFFSwabLong8(&dircount64);
1476 if (dircount64>0xFFFF)
1478 TIFFErrorExt(tif->tif_clientdata,module,"Sanity check on directory count failed");
1481 dircount16=(uint16)dircount64;
1482 poffc=poffb+dircount16*20;
1483 poffd=poffc+sizeof(uint64);
1484 if ((poffc<poffb)||(poffc<dircount16*20)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint64))||(poffd>tif->tif_size))
1486 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1491 _TIFFmemcpy(nextdir,tif->tif_base+poffc,sizeof(uint64));
1492 if (tif->tif_flags&TIFF_SWAB)
1493 TIFFSwabLong8(nextdir);
1499 if (!(tif->tif_flags&TIFF_BIGTIFF))
1503 if (!SeekOK(tif, *nextdir) ||
1504 !ReadOK(tif, &dircount, sizeof (uint16))) {
1505 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1509 if (tif->tif_flags & TIFF_SWAB)
1510 TIFFSwabShort(&dircount);
1512 *off = TIFFSeekFile(tif,
1513 dircount*12, SEEK_CUR);
1515 (void) TIFFSeekFile(tif,
1516 dircount*12, SEEK_CUR);
1517 if (!ReadOK(tif, &nextdir32, sizeof (uint32))) {
1518 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
1522 if (tif->tif_flags & TIFF_SWAB)
1523 TIFFSwabLong(&nextdir32);
1530 if (!SeekOK(tif, *nextdir) ||
1531 !ReadOK(tif, &dircount64, sizeof (uint64))) {
1532 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1536 if (tif->tif_flags & TIFF_SWAB)
1537 TIFFSwabLong8(&dircount64);
1538 if (dircount64>0xFFFF)
1540 TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
1543 dircount16 = (uint16)dircount64;
1545 *off = TIFFSeekFile(tif,
1546 dircount16*20, SEEK_CUR);
1548 (void) TIFFSeekFile(tif,
1549 dircount16*20, SEEK_CUR);
1550 if (!ReadOK(tif, nextdir, sizeof (uint64))) {
1551 TIFFErrorExt(tif->tif_clientdata, module,
1552 "%s: Error fetching directory link",
1556 if (tif->tif_flags & TIFF_SWAB)
1557 TIFFSwabLong8(nextdir);
1564 * Count the number of directories in a file.
1567 TIFFNumberOfDirectories(TIFF* tif)
1569 static const char module[] = "TIFFNumberOfDirectories";
1572 if (!(tif->tif_flags&TIFF_BIGTIFF))
1573 nextdir = tif->tif_header.classic.tiff_diroff;
1575 nextdir = tif->tif_header.big.tiff_diroff;
1577 while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
1584 TIFFErrorExt(tif->tif_clientdata, module,
1585 "Directory count exceeded 65535 limit,"
1586 " giving up on counting.");
1594 * Set the n-th directory as the current directory.
1595 * NB: Directories are numbered starting at 0.
1598 TIFFSetDirectory(TIFF* tif, uint16 dirn)
1603 if (!(tif->tif_flags&TIFF_BIGTIFF))
1604 nextdir = tif->tif_header.classic.tiff_diroff;
1606 nextdir = tif->tif_header.big.tiff_diroff;
1607 for (n = dirn; n > 0 && nextdir != 0; n--)
1608 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1610 tif->tif_nextdiroff = nextdir;
1612 * Set curdir to the actual directory index. The
1613 * -1 is because TIFFReadDirectory will increment
1614 * tif_curdir after successfully reading the directory.
1616 tif->tif_curdir = (dirn - n) - 1;
1618 * Reset tif_dirnumber counter and start new list of seen directories.
1619 * We need this to prevent IFD loops.
1621 tif->tif_dirnumber = 0;
1622 return (TIFFReadDirectory(tif));
1626 * Set the current directory to be the directory
1627 * located at the specified file offset. This interface
1628 * is used mainly to access directories linked with
1629 * the SubIFD tag (e.g. thumbnail images).
1632 TIFFSetSubDirectory(TIFF* tif, uint64 diroff)
1634 tif->tif_nextdiroff = diroff;
1636 * Reset tif_dirnumber counter and start new list of seen directories.
1637 * We need this to prevent IFD loops.
1639 tif->tif_dirnumber = 0;
1640 return (TIFFReadDirectory(tif));
1644 * Return file offset of the current directory.
1647 TIFFCurrentDirOffset(TIFF* tif)
1649 return (tif->tif_diroff);
1653 * Return an indication of whether or not we are
1654 * at the last directory in the file.
1657 TIFFLastDirectory(TIFF* tif)
1659 return (tif->tif_nextdiroff == 0);
1663 * Unlink the specified directory from the directory chain.
1666 TIFFUnlinkDirectory(TIFF* tif, uint16 dirn)
1668 static const char module[] = "TIFFUnlinkDirectory";
1673 if (tif->tif_mode == O_RDONLY) {
1674 TIFFErrorExt(tif->tif_clientdata, module,
1675 "Can not unlink directory in read-only file");
1679 * Go to the directory before the one we want
1680 * to unlink and nab the offset of the link
1681 * field we'll need to patch.
1683 if (!(tif->tif_flags&TIFF_BIGTIFF))
1685 nextdir = tif->tif_header.classic.tiff_diroff;
1690 nextdir = tif->tif_header.big.tiff_diroff;
1693 for (n = dirn-1; n > 0; n--) {
1695 TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
1698 if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
1702 * Advance to the directory to be unlinked and fetch
1703 * the offset of the directory that follows.
1705 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1708 * Go back and patch the link field of the preceding
1709 * directory to point to the offset of the directory
1712 (void) TIFFSeekFile(tif, off, SEEK_SET);
1713 if (!(tif->tif_flags&TIFF_BIGTIFF))
1716 nextdir32=(uint32)nextdir;
1717 assert((uint64)nextdir32==nextdir);
1718 if (tif->tif_flags & TIFF_SWAB)
1719 TIFFSwabLong(&nextdir32);
1720 if (!WriteOK(tif, &nextdir32, sizeof (uint32))) {
1721 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1727 if (tif->tif_flags & TIFF_SWAB)
1728 TIFFSwabLong8(&nextdir);
1729 if (!WriteOK(tif, &nextdir, sizeof (uint64))) {
1730 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1735 * Leave directory state setup safely. We don't have
1736 * facilities for doing inserting and removing directories,
1737 * so it's safest to just invalidate everything. This
1738 * means that the caller can only append to the directory
1741 (*tif->tif_cleanup)(tif);
1742 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
1743 _TIFFfree(tif->tif_rawdata);
1744 tif->tif_rawdata = NULL;
1746 tif->tif_rawdataoff = 0;
1747 tif->tif_rawdataloaded = 0;
1749 tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE|TIFF_BUF4WRITE);
1750 TIFFFreeDirectory(tif);
1751 TIFFDefaultDirectory(tif);
1752 tif->tif_diroff = 0; /* force link on next write */
1753 tif->tif_nextdiroff = 0; /* next write must be at end */
1754 tif->tif_curoff = 0;
1755 tif->tif_row = (uint32) -1;
1756 tif->tif_curstrip = (uint32) -1;
1760 /* vim: set ts=8 sts=8 sw=8 noet: */