3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
29 * Directory Tag Get & Set Routines.
30 * (and also some miscellaneous stuff)
35 * These are used in the backwards compatibility code...
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 */
43 setByteArray(void** vpp
, void* vp
, size_t nmemb
, size_t elem_size
)
46 _TIFFfree(*vpp
), *vpp
= 0;
48 tmsize_t bytes
= (tmsize_t
)(nmemb
* elem_size
);
49 if (elem_size
&& bytes
/ elem_size
== nmemb
)
50 *vpp
= (void*) _TIFFmalloc(bytes
);
52 _TIFFmemcpy(*vpp
, vp
, bytes
);
55 void _TIFFsetByteArray(void** vpp
, void* vp
, uint32 n
)
56 { setByteArray(vpp
, vp
, n
, 1); }
57 void _TIFFsetString(char** cpp
, char* cp
)
58 { setByteArray((void**) cpp
, (void*) cp
, strlen(cp
)+1, 1); }
59 void _TIFFsetNString(char** cpp
, char* cp
, uint32 n
)
60 { setByteArray((void**) cpp
, (void*) cp
, n
, 1); }
61 void _TIFFsetShortArray(uint16
** wpp
, uint16
* wp
, uint32 n
)
62 { setByteArray((void**) wpp
, (void*) wp
, n
, sizeof (uint16
)); }
63 void _TIFFsetLongArray(uint32
** lpp
, uint32
* lp
, uint32 n
)
64 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint32
)); }
65 void _TIFFsetLong8Array(uint64
** lpp
, uint64
* lp
, uint32 n
)
66 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint64
)); }
67 void _TIFFsetFloatArray(float** fpp
, float* fp
, uint32 n
)
68 { setByteArray((void**) fpp
, (void*) fp
, n
, sizeof (float)); }
69 void _TIFFsetDoubleArray(double** dpp
, double* dp
, uint32 n
)
70 { setByteArray((void**) dpp
, (void*) dp
, n
, sizeof (double)); }
73 setDoubleArrayOneValue(double** vpp
, double value
, size_t nmemb
)
77 *vpp
= _TIFFmalloc(nmemb
*sizeof(double));
81 ((double*)*vpp
)[nmemb
] = value
;
86 * Install extra samples information.
89 setExtraSamples(TIFFDirectory
* td
, va_list ap
, uint32
* v
)
91 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
92 #define EXTRASAMPLE_COREL_UNASSALPHA 999
97 *v
= (uint16
) va_arg(ap
, uint16_vap
);
98 if ((uint16
) *v
> td
->td_samplesperpixel
)
100 va
= va_arg(ap
, uint16
*);
101 if (*v
> 0 && va
== NULL
) /* typically missing param */
103 for (i
= 0; i
< *v
; i
++) {
104 if (va
[i
] > EXTRASAMPLE_UNASSALPHA
) {
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
111 if (va
[i
] == EXTRASAMPLE_COREL_UNASSALPHA
)
112 va
[i
] = EXTRASAMPLE_UNASSALPHA
;
117 td
->td_extrasamples
= (uint16
) *v
;
118 _TIFFsetShortArray(&td
->td_sampleinfo
, va
, td
->td_extrasamples
);
121 #undef EXTRASAMPLE_COREL_UNASSALPHA
125 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
126 * zero if the ink names are not as expected.
129 checkInkNamesString(TIFF
* tif
, uint32 slen
, const char* s
)
131 TIFFDirectory
* td
= &tif
->tif_dir
;
132 uint16 i
= td
->td_samplesperpixel
;
135 const char* ep
= s
+slen
;
138 for (; cp
< ep
&& *cp
!= '\0'; cp
++) {}
143 return ((uint32
)(cp
-s
));
146 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
147 "%s: Invalid InkNames value; expecting %d names, found %d",
149 td
->td_samplesperpixel
,
150 td
->td_samplesperpixel
-i
);
155 _TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
157 static const char module[] = "_TIFFVSetField";
159 TIFFDirectory
* td
= &tif
->tif_dir
;
163 const TIFFField
*fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
164 uint32 standard_tag
= tag
;
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)
172 if (fip
->field_bit
== FIELD_CUSTOM
) {
176 switch (standard_tag
) {
177 case TIFFTAG_SUBFILETYPE
:
178 td
->td_subfiletype
= (uint32
) va_arg(ap
, uint32
);
180 case TIFFTAG_IMAGEWIDTH
:
181 td
->td_imagewidth
= (uint32
) va_arg(ap
, uint32
);
183 case TIFFTAG_IMAGELENGTH
:
184 td
->td_imagelength
= (uint32
) va_arg(ap
, uint32
);
186 case TIFFTAG_BITSPERSAMPLE
:
187 td
->td_bitspersample
= (uint16
) va_arg(ap
, uint16_vap
);
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.
195 if (tif
->tif_flags
& TIFF_SWAB
) {
196 if (td
->td_bitspersample
== 8)
197 tif
->tif_postdecode
= _TIFFNoPostDecode
;
198 else if (td
->td_bitspersample
== 16)
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
;
210 case TIFFTAG_COMPRESSION
:
211 v
= (uint16
) va_arg(ap
, uint16_vap
);
213 * If we're changing the compression scheme, the notify the
214 * previous module so that it can cleanup any state it's
217 if (TIFFFieldSet(tif
, FIELD_COMPRESSION
)) {
218 if ((uint32
)td
->td_compression
== v
)
220 (*tif
->tif_cleanup
)(tif
);
221 tif
->tif_flags
&= ~TIFF_CODERSETUP
;
224 * Setup new compression routine state.
226 if( (status
= TIFFSetCompressionScheme(tif
, v
)) != 0 )
227 td
->td_compression
= (uint16
) v
;
231 case TIFFTAG_PHOTOMETRIC
:
232 td
->td_photometric
= (uint16
) va_arg(ap
, uint16_vap
);
234 case TIFFTAG_THRESHHOLDING
:
235 td
->td_threshholding
= (uint16
) va_arg(ap
, uint16_vap
);
237 case TIFFTAG_FILLORDER
:
238 v
= (uint16
) va_arg(ap
, uint16_vap
);
239 if (v
!= FILLORDER_LSB2MSB
&& v
!= FILLORDER_MSB2LSB
)
241 td
->td_fillorder
= (uint16
) v
;
243 case TIFFTAG_ORIENTATION
:
244 v
= (uint16
) va_arg(ap
, uint16_vap
);
245 if (v
< ORIENTATION_TOPLEFT
|| ORIENTATION_LEFTBOT
< v
)
248 td
->td_orientation
= (uint16
) v
;
250 case TIFFTAG_SAMPLESPERPIXEL
:
251 v
= (uint16
) va_arg(ap
, uint16_vap
);
254 td
->td_samplesperpixel
= (uint16
) v
;
256 case TIFFTAG_ROWSPERSTRIP
:
257 v32
= (uint32
) va_arg(ap
, uint32
);
260 td
->td_rowsperstrip
= v32
;
261 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) {
262 td
->td_tilelength
= v32
;
263 td
->td_tilewidth
= td
->td_imagewidth
;
266 case TIFFTAG_MINSAMPLEVALUE
:
267 td
->td_minsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
269 case TIFFTAG_MAXSAMPLEVALUE
:
270 td
->td_maxsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
272 case TIFFTAG_SMINSAMPLEVALUE
:
273 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
274 _TIFFsetDoubleArray(&td
->td_sminsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
276 setDoubleArrayOneValue(&td
->td_sminsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
278 case TIFFTAG_SMAXSAMPLEVALUE
:
279 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
280 _TIFFsetDoubleArray(&td
->td_smaxsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
282 setDoubleArrayOneValue(&td
->td_smaxsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
284 case TIFFTAG_XRESOLUTION
:
285 td
->td_xresolution
= (float) va_arg(ap
, double);
287 case TIFFTAG_YRESOLUTION
:
288 td
->td_yresolution
= (float) va_arg(ap
, double);
290 case TIFFTAG_PLANARCONFIG
:
291 v
= (uint16
) va_arg(ap
, uint16_vap
);
292 if (v
!= PLANARCONFIG_CONTIG
&& v
!= PLANARCONFIG_SEPARATE
)
294 td
->td_planarconfig
= (uint16
) v
;
296 case TIFFTAG_XPOSITION
:
297 td
->td_xposition
= (float) va_arg(ap
, double);
299 case TIFFTAG_YPOSITION
:
300 td
->td_yposition
= (float) va_arg(ap
, double);
302 case TIFFTAG_RESOLUTIONUNIT
:
303 v
= (uint16
) va_arg(ap
, uint16_vap
);
304 if (v
< RESUNIT_NONE
|| RESUNIT_CENTIMETER
< v
)
306 td
->td_resolutionunit
= (uint16
) v
;
308 case TIFFTAG_PAGENUMBER
:
309 td
->td_pagenumber
[0] = (uint16
) va_arg(ap
, uint16_vap
);
310 td
->td_pagenumber
[1] = (uint16
) va_arg(ap
, uint16_vap
);
312 case TIFFTAG_HALFTONEHINTS
:
313 td
->td_halftonehints
[0] = (uint16
) va_arg(ap
, uint16_vap
);
314 td
->td_halftonehints
[1] = (uint16
) va_arg(ap
, uint16_vap
);
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
);
322 case TIFFTAG_EXTRASAMPLES
:
323 if (!setExtraSamples(td
, ap
, &v
))
326 case TIFFTAG_MATTEING
:
327 td
->td_extrasamples
= (((uint16
) va_arg(ap
, uint16_vap
)) != 0);
328 if (td
->td_extrasamples
) {
329 uint16 sv
= EXTRASAMPLE_ASSOCALPHA
;
330 _TIFFsetShortArray(&td
->td_sampleinfo
, &sv
, 1);
333 case TIFFTAG_TILEWIDTH
:
334 v32
= (uint32
) va_arg(ap
, uint32
);
336 if (tif
->tif_mode
!= O_RDONLY
)
338 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
339 "Nonstandard tile width %d, convert file", v32
);
341 td
->td_tilewidth
= v32
;
342 tif
->tif_flags
|= TIFF_ISTILED
;
344 case TIFFTAG_TILELENGTH
:
345 v32
= (uint32
) va_arg(ap
, uint32
);
347 if (tif
->tif_mode
!= O_RDONLY
)
349 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
350 "Nonstandard tile length %d, convert file", v32
);
352 td
->td_tilelength
= v32
;
353 tif
->tif_flags
|= TIFF_ISTILED
;
355 case TIFFTAG_TILEDEPTH
:
356 v32
= (uint32
) va_arg(ap
, uint32
);
359 td
->td_tiledepth
= v32
;
361 case TIFFTAG_DATATYPE
:
362 v
= (uint16
) va_arg(ap
, uint16_vap
);
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
;
370 td
->td_sampleformat
= (uint16
) v
;
372 case TIFFTAG_SAMPLEFORMAT
:
373 v
= (uint16
) va_arg(ap
, uint16_vap
);
374 if (v
< SAMPLEFORMAT_UINT
|| SAMPLEFORMAT_COMPLEXIEEEFP
< v
)
376 td
->td_sampleformat
= (uint16
) v
;
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
;
389 case TIFFTAG_IMAGEDEPTH
:
390 td
->td_imagedepth
= (uint32
) va_arg(ap
, uint32
);
393 if ((tif
->tif_flags
& TIFF_INSUBIFD
) == 0) {
394 td
->td_nsubifd
= (uint16
) va_arg(ap
, uint16_vap
);
395 _TIFFsetLong8Array(&td
->td_subifd
, (uint64
*) va_arg(ap
, uint64
*),
396 (long) td
->td_nsubifd
);
398 TIFFErrorExt(tif
->tif_clientdata
, module,
399 "%s: Sorry, cannot nest SubIFDs",
404 case TIFFTAG_YCBCRPOSITIONING
:
405 td
->td_ycbcrpositioning
= (uint16
) va_arg(ap
, uint16_vap
);
407 case TIFFTAG_YCBCRSUBSAMPLING
:
408 td
->td_ycbcrsubsampling
[0] = (uint16
) va_arg(ap
, uint16_vap
);
409 td
->td_ycbcrsubsampling
[1] = (uint16
) va_arg(ap
, uint16_vap
);
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
);
417 case TIFFTAG_REFERENCEBLACKWHITE
:
418 /* XXX should check for null range */
419 _TIFFsetFloatArray(&td
->td_refblackwhite
, va_arg(ap
, float*), 6);
421 case TIFFTAG_INKNAMES
:
422 v
= (uint16
) va_arg(ap
, uint16_vap
);
423 s
= va_arg(ap
, char*);
424 v
= checkInkNamesString(tif
, v
, s
);
427 _TIFFsetNString(&td
->td_inknames
, s
, v
);
428 td
->td_inknameslen
= v
;
431 case TIFFTAG_PERSAMPLE
:
432 v
= (uint16
) va_arg(ap
, uint16_vap
);
433 if( v
== PERSAMPLE_MULTI
)
434 tif
->tif_flags
|= TIFF_PERSAMPLE
;
436 tif
->tif_flags
&= ~TIFF_PERSAMPLE
;
440 int tv_size
, iCustom
;
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.
451 if(fip
== NULL
|| fip
->field_bit
!= FIELD_CUSTOM
) {
452 TIFFErrorExt(tif
->tif_clientdata
, module,
453 "%s: Invalid %stag \"%s\" (not supported by codec)",
454 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "",
455 fip
? fip
->field_name
: "Unknown");
461 * Find the existing entry for this custom value.
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
);
476 * Grow the custom list if the entry was not found.
479 TIFFTagValue
*new_customValues
;
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",
493 td
->td_customValues
= new_customValues
;
495 tv
= td
->td_customValues
+ (td
->td_customValueCount
- 1);
502 * Set custom value ... save a copy of the custom tag value.
504 tv_size
= _TIFFDataSize(fip
->field_type
);
507 TIFFErrorExt(tif
->tif_clientdata
, module,
508 "%s: Bad field type %d for \"%s\"",
509 tif
->tif_name
, fip
->field_type
,
514 if (fip
->field_type
== TIFF_ASCII
)
518 if (fip
->field_passcount
)
520 assert(fip
->field_writecount
==TIFF_VARIABLE2
);
521 ma
=(uint32
)va_arg(ap
,uint32
);
522 mb
=(char*)va_arg(ap
,char*);
526 mb
=(char*)va_arg(ap
,char*);
527 ma
=(uint32
)(strlen(mb
)+1);
530 setByteArray(&tv
->value
,mb
,ma
,1);
534 if (fip
->field_passcount
) {
535 if (fip
->field_writecount
== TIFF_VARIABLE2
)
536 tv
->count
= (uint32
) va_arg(ap
, uint32
);
538 tv
->count
= (int) va_arg(ap
, int);
539 } else if (fip
->field_writecount
== TIFF_VARIABLE
540 || fip
->field_writecount
== TIFF_VARIABLE2
)
542 else if (fip
->field_writecount
== TIFF_SPP
)
543 tv
->count
= td
->td_samplesperpixel
;
545 tv
->count
= fip
->field_writecount
;
547 if (tv
->count
== 0) {
549 TIFFErrorExt(tif
->tif_clientdata
, module,
550 "%s: Null count for \"%s\" (type "
551 "%d, writecount %d, passcount %d)",
555 fip
->field_writecount
,
556 fip
->field_passcount
);
560 tv
->value
= _TIFFCheckMalloc(tif
, tv
->count
, tv_size
,
561 "custom tag binary object");
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 */
574 v
[0] = (uint16
)va_arg(ap
, int);
575 v
[1] = (uint16
)va_arg(ap
, int);
576 _TIFFmemcpy(tv
->value
, &v
, 4);
579 else if (fip
->field_passcount
580 || fip
->field_writecount
== TIFF_VARIABLE
581 || fip
->field_writecount
== TIFF_VARIABLE2
582 || fip
->field_writecount
== TIFF_SPP
584 _TIFFmemcpy(tv
->value
, va_arg(ap
, void *),
585 tv
->count
* tv_size
);
587 char *val
= (char *)tv
->value
;
588 assert( tv
->count
== 1 );
590 switch (fip
->field_type
) {
594 uint8 v
= (uint8
)va_arg(ap
, int);
595 _TIFFmemcpy(val
, &v
, tv_size
);
600 int8 v
= (int8
)va_arg(ap
, int);
601 _TIFFmemcpy(val
, &v
, tv_size
);
606 uint16 v
= (uint16
)va_arg(ap
, int);
607 _TIFFmemcpy(val
, &v
, tv_size
);
612 int16 v
= (int16
)va_arg(ap
, int);
613 _TIFFmemcpy(val
, &v
, tv_size
);
619 uint32 v
= va_arg(ap
, uint32
);
620 _TIFFmemcpy(val
, &v
, tv_size
);
625 int32 v
= va_arg(ap
, int32
);
626 _TIFFmemcpy(val
, &v
, tv_size
);
632 uint64 v
= va_arg(ap
, uint64
);
633 _TIFFmemcpy(val
, &v
, tv_size
);
638 int64 v
= va_arg(ap
, int64
);
639 _TIFFmemcpy(val
, &v
, tv_size
);
646 float v
= (float)va_arg(ap
, double);
647 _TIFFmemcpy(val
, &v
, tv_size
);
652 double v
= va_arg(ap
, double);
653 _TIFFmemcpy(val
, &v
, tv_size
);
657 _TIFFmemset(val
, 0, tv_size
);
666 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
668 TIFFSetFieldBit(tif
, fip
->field_bit
);
669 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
677 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
678 TIFFErrorExt(tif
->tif_clientdata
, module,
679 "%s: Bad value %u for \"%s\" tag",
681 fip
? fip
->field_name
: "Unknown");
687 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
688 TIFFErrorExt(tif
->tif_clientdata
, module,
689 "%s: Bad value %u for \"%s\" tag",
691 fip
? fip
->field_name
: "Unknown");
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.
707 OkToChangeTag(TIFF
* tif
, uint32 tag
)
709 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
710 if (!fip
) { /* unknown tag */
711 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField", "%s: Unknown %stag %u",
712 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", tag
);
715 if (tag
!= TIFFTAG_IMAGELENGTH
&& (tif
->tif_flags
& TIFF_BEENWRITING
) &&
716 !fip
->field_oktochange
) {
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.
723 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
724 "%s: Cannot modify tag \"%s\" while writing",
725 tif
->tif_name
, fip
->field_name
);
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
739 TIFFSetField(TIFF
* tif
, uint32 tag
, ...)
745 status
= TIFFVSetField(tif
, tag
, ap
);
751 * Clear the contents of the field in the internal structure.
754 TIFFUnsetField(TIFF
* tif
, uint32 tag
)
756 const TIFFField
*fip
= TIFFFieldWithTag(tif
, tag
);
757 TIFFDirectory
* td
= &tif
->tif_dir
;
762 if( fip
->field_bit
!= FIELD_CUSTOM
)
763 TIFFClrFieldBit(tif
, fip
->field_bit
);
766 TIFFTagValue
*tv
= NULL
;
769 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
771 tv
= td
->td_customValues
+ i
;
772 if( tv
->info
->field_tag
== tag
)
776 if( i
< td
->td_customValueCount
)
778 _TIFFfree(tv
->value
);
779 for( ; i
< td
->td_customValueCount
-1; i
++) {
780 td
->td_customValues
[i
] = td
->td_customValues
[i
+1];
782 td
->td_customValueCount
--;
786 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
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.
798 TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
800 return OkToChangeTag(tif
, tag
) ?
801 (*tif
->tif_tagmethods
.vsetfield
)(tif
, tag
, ap
) : 0;
805 _TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
807 TIFFDirectory
* td
= &tif
->tif_dir
;
809 uint32 standard_tag
= tag
;
810 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
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)
818 if (fip
->field_bit
== FIELD_CUSTOM
) {
822 switch (standard_tag
) {
823 case TIFFTAG_SUBFILETYPE
:
824 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
826 case TIFFTAG_IMAGEWIDTH
:
827 *va_arg(ap
, uint32
*) = td
->td_imagewidth
;
829 case TIFFTAG_IMAGELENGTH
:
830 *va_arg(ap
, uint32
*) = td
->td_imagelength
;
832 case TIFFTAG_BITSPERSAMPLE
:
833 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
835 case TIFFTAG_COMPRESSION
:
836 *va_arg(ap
, uint16
*) = td
->td_compression
;
838 case TIFFTAG_PHOTOMETRIC
:
839 *va_arg(ap
, uint16
*) = td
->td_photometric
;
841 case TIFFTAG_THRESHHOLDING
:
842 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
844 case TIFFTAG_FILLORDER
:
845 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
847 case TIFFTAG_ORIENTATION
:
848 *va_arg(ap
, uint16
*) = td
->td_orientation
;
850 case TIFFTAG_SAMPLESPERPIXEL
:
851 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
853 case TIFFTAG_ROWSPERSTRIP
:
854 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
856 case TIFFTAG_MINSAMPLEVALUE
:
857 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
859 case TIFFTAG_MAXSAMPLEVALUE
:
860 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
862 case TIFFTAG_SMINSAMPLEVALUE
:
863 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
864 *va_arg(ap
, double**) = td
->td_sminsamplevalue
;
867 /* libtiff historially treats this as a single value. */
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
;
876 case TIFFTAG_SMAXSAMPLEVALUE
:
877 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
878 *va_arg(ap
, double**) = td
->td_smaxsamplevalue
;
881 /* libtiff historially treats this as a single value. */
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
;
890 case TIFFTAG_XRESOLUTION
:
891 *va_arg(ap
, float*) = td
->td_xresolution
;
893 case TIFFTAG_YRESOLUTION
:
894 *va_arg(ap
, float*) = td
->td_yresolution
;
896 case TIFFTAG_PLANARCONFIG
:
897 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
899 case TIFFTAG_XPOSITION
:
900 *va_arg(ap
, float*) = td
->td_xposition
;
902 case TIFFTAG_YPOSITION
:
903 *va_arg(ap
, float*) = td
->td_yposition
;
905 case TIFFTAG_RESOLUTIONUNIT
:
906 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
908 case TIFFTAG_PAGENUMBER
:
909 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[0];
910 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[1];
912 case TIFFTAG_HALFTONEHINTS
:
913 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[0];
914 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[1];
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];
921 case TIFFTAG_STRIPOFFSETS
:
922 case TIFFTAG_TILEOFFSETS
:
923 _TIFFFillStriles( tif
);
924 *va_arg(ap
, uint64
**) = td
->td_stripoffset
;
926 case TIFFTAG_STRIPBYTECOUNTS
:
927 case TIFFTAG_TILEBYTECOUNTS
:
928 _TIFFFillStriles( tif
);
929 *va_arg(ap
, uint64
**) = td
->td_stripbytecount
;
931 case TIFFTAG_MATTEING
:
932 *va_arg(ap
, uint16
*) =
933 (td
->td_extrasamples
== 1 &&
934 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
936 case TIFFTAG_EXTRASAMPLES
:
937 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
938 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
940 case TIFFTAG_TILEWIDTH
:
941 *va_arg(ap
, uint32
*) = td
->td_tilewidth
;
943 case TIFFTAG_TILELENGTH
:
944 *va_arg(ap
, uint32
*) = td
->td_tilelength
;
946 case TIFFTAG_TILEDEPTH
:
947 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
949 case TIFFTAG_DATATYPE
:
950 switch (td
->td_sampleformat
) {
951 case SAMPLEFORMAT_UINT
:
952 *va_arg(ap
, uint16
*) = DATATYPE_UINT
;
954 case SAMPLEFORMAT_INT
:
955 *va_arg(ap
, uint16
*) = DATATYPE_INT
;
957 case SAMPLEFORMAT_IEEEFP
:
958 *va_arg(ap
, uint16
*) = DATATYPE_IEEEFP
;
960 case SAMPLEFORMAT_VOID
:
961 *va_arg(ap
, uint16
*) = DATATYPE_VOID
;
965 case TIFFTAG_SAMPLEFORMAT
:
966 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
968 case TIFFTAG_IMAGEDEPTH
:
969 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
972 *va_arg(ap
, uint16
*) = td
->td_nsubifd
;
973 *va_arg(ap
, uint64
**) = td
->td_subifd
;
975 case TIFFTAG_YCBCRPOSITIONING
:
976 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
978 case TIFFTAG_YCBCRSUBSAMPLING
:
979 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
980 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
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];
989 case TIFFTAG_REFERENCEBLACKWHITE
:
990 *va_arg(ap
, float**) = td
->td_refblackwhite
;
992 case TIFFTAG_INKNAMES
:
993 *va_arg(ap
, char**) = td
->td_inknames
;
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.
1008 if( fip
== NULL
|| fip
->field_bit
!= FIELD_CUSTOM
)
1010 TIFFErrorExt(tif
->tif_clientdata
, "_TIFFVGetField",
1011 "%s: Invalid %stag \"%s\" "
1012 "(not supported by codec)",
1014 isPseudoTag(tag
) ? "pseudo-" : "",
1015 fip
? fip
->field_name
: "Unknown");
1021 * Do we have a custom value?
1024 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
1025 TIFFTagValue
*tv
= td
->td_customValues
+ i
;
1027 if (tv
->info
->field_tag
!= tag
)
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
;
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];
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
1052 *va_arg(ap
, void **) = tv
->value
;
1055 char *val
= (char *)tv
->value
;
1056 assert( tv
->count
== 1 );
1057 switch (fip
->field_type
) {
1059 case TIFF_UNDEFINED
:
1060 *va_arg(ap
, uint8
*) =
1065 *va_arg(ap
, int8
*) =
1070 *va_arg(ap
, uint16
*) =
1075 *va_arg(ap
, int16
*) =
1081 *va_arg(ap
, uint32
*) =
1086 *va_arg(ap
, int32
*) =
1092 *va_arg(ap
, uint64
*) =
1097 *va_arg(ap
, int64
*) =
1102 case TIFF_SRATIONAL
:
1104 *va_arg(ap
, float*) =
1109 *va_arg(ap
, double*) =
1127 * Return the value of a field in the
1128 * internal directory structure.
1131 TIFFGetField(TIFF
* tif
, uint32 tag
, ...)
1137 status
= TIFFVGetField(tif
, tag
, ap
);
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.
1149 TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1151 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
1152 return (fip
&& (isPseudoTag(tag
) || TIFFFieldSet(tif
, fip
->field_bit
)) ?
1153 (*tif
->tif_tagmethods
.vgetfield
)(tif
, tag
, ap
) : 0);
1156 #define CleanupField(member) { \
1158 _TIFFfree(td->member); \
1164 * Release storage associated with a directory.
1167 TIFFFreeDirectory(TIFF
* tif
)
1169 TIFFDirectory
*td
= &tif
->tif_dir
;
1172 _TIFFmemset(td
->td_fieldsset
, 0, FIELD_SETLONGS
);
1173 CleanupField(td_sminsamplevalue
);
1174 CleanupField(td_smaxsamplevalue
);
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
);
1181 CleanupField(td_refblackwhite
);
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
);
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
);
1196 td
->td_customValueCount
= 0;
1197 CleanupField(td_customValues
);
1199 #if defined(DEFER_STRILE_LOAD)
1200 _TIFFmemset( &(td
->td_stripoffset_entry
), 0, sizeof(TIFFDirEntry
));
1201 _TIFFmemset( &(td
->td_stripbytecount_entry
), 0, sizeof(TIFFDirEntry
));
1207 * Client Tag extension support (from Niles Ritter).
1209 static TIFFExtendProc _TIFFextender
= (TIFFExtendProc
) NULL
;
1212 TIFFSetTagExtender(TIFFExtendProc extender
)
1214 TIFFExtendProc prev
= _TIFFextender
;
1215 _TIFFextender
= extender
;
1220 * Setup for a new directory. Should we automatically call
1221 * TIFFWriteDirectory() if the current one is dirty?
1223 * The newly created directory will not exist on the file till
1224 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1227 TIFFCreateDirectory(TIFF
* tif
)
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;
1240 TIFFCreateCustomDirectory(TIFF
* tif
, const TIFFFieldArray
* infoarray
)
1242 TIFFDefaultDirectory(tif
);
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.
1249 _TIFFSetupFields(tif
, infoarray
);
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 TIFFCreateEXIFDirectory(TIFF
* tif
)
1263 const TIFFFieldArray
* exifFieldArray
;
1264 exifFieldArray
= _TIFFGetExifFields();
1265 return TIFFCreateCustomDirectory(tif
, exifFieldArray
);
1269 * Setup a default directory structure.
1272 TIFFDefaultDirectory(TIFF
* tif
)
1274 register TIFFDirectory
* td
= &tif
->tif_dir
;
1275 const TIFFFieldArray
* tiffFieldArray
;
1277 tiffFieldArray
= _TIFFGetFields();
1278 _TIFFSetupFields(tif
, tiffFieldArray
);
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;
1290 td
->td_stripbytecountsorted
= 1; /* Our own arrays always sorted. */
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
;
1297 tif
->tif_postdecode
= _TIFFNoPostDecode
;
1298 tif
->tif_foundfield
= NULL
;
1299 tif
->tif_tagmethods
.vsetfield
= _TIFFVSetField
;
1300 tif
->tif_tagmethods
.vgetfield
= _TIFFVGetField
;
1301 tif
->tif_tagmethods
.printdir
= NULL
;
1303 * Give client code a chance to install their own
1304 * tag extensions & methods, prior to compression overloads.
1307 (*_TIFFextender
)(tif
);
1308 (void) TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
);
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).
1317 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
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?
1324 tif
->tif_flags
&= ~TIFF_ISTILED
;
1330 TIFFAdvanceDirectory(TIFF
* tif
, uint64
* nextdir
, uint64
* off
)
1332 static const char module[] = "TIFFAdvanceDirectory";
1335 uint64 poff
=*nextdir
;
1336 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1338 tmsize_t poffa
,poffb
,poffc
,poffd
;
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
))
1345 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory count");
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
))
1355 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory link");
1360 _TIFFmemcpy(&nextdir32
,tif
->tif_base
+poffc
,sizeof(uint32
));
1361 if (tif
->tif_flags
&TIFF_SWAB
)
1362 TIFFSwabLong(&nextdir32
);
1367 tmsize_t poffa
,poffb
,poffc
,poffd
;
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
))
1374 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory count");
1377 _TIFFmemcpy(&dircount64
,tif
->tif_base
+poffa
,sizeof(uint64
));
1378 if (tif
->tif_flags
&TIFF_SWAB
)
1379 TIFFSwabLong8(&dircount64
);
1380 if (dircount64
>0xFFFF)
1382 TIFFErrorExt(tif
->tif_clientdata
,module,"Sanity check on directory count failed");
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
))
1390 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory link");
1395 _TIFFmemcpy(nextdir
,tif
->tif_base
+poffc
,sizeof(uint64
));
1396 if (tif
->tif_flags
&TIFF_SWAB
)
1397 TIFFSwabLong8(nextdir
);
1403 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1407 if (!SeekOK(tif
, *nextdir
) ||
1408 !ReadOK(tif
, &dircount
, sizeof (uint16
))) {
1409 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: Error fetching directory count",
1413 if (tif
->tif_flags
& TIFF_SWAB
)
1414 TIFFSwabShort(&dircount
);
1416 *off
= TIFFSeekFile(tif
,
1417 dircount
*12, SEEK_CUR
);
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",
1426 if (tif
->tif_flags
& TIFF_SWAB
)
1427 TIFFSwabLong(&nextdir32
);
1434 if (!SeekOK(tif
, *nextdir
) ||
1435 !ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
1436 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: Error fetching directory count",
1440 if (tif
->tif_flags
& TIFF_SWAB
)
1441 TIFFSwabLong8(&dircount64
);
1442 if (dircount64
>0xFFFF)
1444 TIFFErrorExt(tif
->tif_clientdata
, module, "Error fetching directory count");
1447 dircount16
= (uint16
)dircount64
;
1449 *off
= TIFFSeekFile(tif
,
1450 dircount16
*20, SEEK_CUR
);
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",
1459 if (tif
->tif_flags
& TIFF_SWAB
)
1460 TIFFSwabLong8(nextdir
);
1467 * Count the number of directories in a file.
1470 TIFFNumberOfDirectories(TIFF
* tif
)
1474 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1475 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1477 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1479 while (nextdir
!= 0 && TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1485 * Set the n-th directory as the current directory.
1486 * NB: Directories are numbered starting at 0.
1489 TIFFSetDirectory(TIFF
* tif
, uint16 dirn
)
1494 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1495 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1497 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1498 for (n
= dirn
; n
> 0 && nextdir
!= 0; n
--)
1499 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1501 tif
->tif_nextdiroff
= nextdir
;
1503 * Set curdir to the actual directory index. The
1504 * -1 is because TIFFReadDirectory will increment
1505 * tif_curdir after successfully reading the directory.
1507 tif
->tif_curdir
= (dirn
- n
) - 1;
1509 * Reset tif_dirnumber counter and start new list of seen directories.
1510 * We need this to prevent IFD loops.
1512 tif
->tif_dirnumber
= 0;
1513 return (TIFFReadDirectory(tif
));
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).
1523 TIFFSetSubDirectory(TIFF
* tif
, uint64 diroff
)
1525 tif
->tif_nextdiroff
= diroff
;
1527 * Reset tif_dirnumber counter and start new list of seen directories.
1528 * We need this to prevent IFD loops.
1530 tif
->tif_dirnumber
= 0;
1531 return (TIFFReadDirectory(tif
));
1535 * Return file offset of the current directory.
1538 TIFFCurrentDirOffset(TIFF
* tif
)
1540 return (tif
->tif_diroff
);
1544 * Return an indication of whether or not we are
1545 * at the last directory in the file.
1548 TIFFLastDirectory(TIFF
* tif
)
1550 return (tif
->tif_nextdiroff
== 0);
1554 * Unlink the specified directory from the directory chain.
1557 TIFFUnlinkDirectory(TIFF
* tif
, uint16 dirn
)
1559 static const char module[] = "TIFFUnlinkDirectory";
1564 if (tif
->tif_mode
== O_RDONLY
) {
1565 TIFFErrorExt(tif
->tif_clientdata
, module,
1566 "Can not unlink directory in read-only file");
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.
1574 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1576 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1581 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1584 for (n
= dirn
-1; n
> 0; n
--) {
1586 TIFFErrorExt(tif
->tif_clientdata
, module, "Directory %d does not exist", dirn
);
1589 if (!TIFFAdvanceDirectory(tif
, &nextdir
, &off
))
1593 * Advance to the directory to be unlinked and fetch
1594 * the offset of the directory that follows.
1596 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1599 * Go back and patch the link field of the preceding
1600 * directory to point to the offset of the directory
1603 (void) TIFFSeekFile(tif
, off
, SEEK_SET
);
1604 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
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");
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");
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
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
;
1637 tif
->tif_rawdataoff
= 0;
1638 tif
->tif_rawdataloaded
= 0;
1640 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
|TIFF_POSTENCODE
|TIFF_BUF4WRITE
);
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;
1647 tif
->tif_curstrip
= (uint32
) -1;
1651 /* vim: set ts=8 sts=8 sw=8 noet: */